test: enhance worker choice strategies tests
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 14 Apr 2023 12:45:11 +0000 (14:45 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 14 Apr 2023 12:45:11 +0000 (14:45 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/selection-strategies/worker-choice-strategy-context.ts
tests/pools/cluster/dynamic.test.js
tests/pools/selection-strategies/selection-strategies.test.js
tests/pools/selection-strategies/worker-choice-strategy-context.test.js
tests/pools/thread/dynamic.test.js

index 91441ace5a58d8ca6756a21b679a3dd13b73df55..b075c4eac5ae3cd9cd88b41553263b11e2e5af95 100644 (file)
@@ -35,12 +35,12 @@ export class WorkerChoiceStrategyContext<
    * Worker choice strategy context constructor.
    *
    * @param pool - The pool instance.
-   * @param workerChoiceStrategyType - The worker choice strategy.
+   * @param workerChoiceStrategy - The worker choice strategy.
    * @param opts - The worker choice strategy options.
    */
   public constructor (
     pool: IPool<Worker, Data, Response>,
-    private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN,
+    private workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN,
     opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
   ) {
     this.execute.bind(this)
@@ -82,7 +82,7 @@ export class WorkerChoiceStrategyContext<
   public getRequiredStatistics (): RequiredStatistics {
     return (
       this.workerChoiceStrategies.get(
-        this.workerChoiceStrategyType
+        this.workerChoiceStrategy
       ) as IWorkerChoiceStrategy
     ).requiredStatistics
   }
@@ -95,10 +95,10 @@ export class WorkerChoiceStrategyContext<
   public setWorkerChoiceStrategy (
     workerChoiceStrategy: WorkerChoiceStrategy
   ): void {
-    if (this.workerChoiceStrategyType !== workerChoiceStrategy) {
-      this.workerChoiceStrategyType = workerChoiceStrategy
+    if (this.workerChoiceStrategy !== workerChoiceStrategy) {
+      this.workerChoiceStrategy = workerChoiceStrategy
     }
-    this.workerChoiceStrategies.get(this.workerChoiceStrategyType)?.reset()
+    this.workerChoiceStrategies.get(this.workerChoiceStrategy)?.reset()
   }
 
   /**
@@ -109,7 +109,7 @@ export class WorkerChoiceStrategyContext<
   public execute (): number {
     return (
       this.workerChoiceStrategies.get(
-        this.workerChoiceStrategyType
+        this.workerChoiceStrategy
       ) as IWorkerChoiceStrategy
     ).choose()
   }
@@ -123,7 +123,7 @@ export class WorkerChoiceStrategyContext<
   public remove (workerNodeKey: number): boolean {
     return (
       this.workerChoiceStrategies.get(
-        this.workerChoiceStrategyType
+        this.workerChoiceStrategy
       ) as IWorkerChoiceStrategy
     ).remove(workerNodeKey)
   }
index 33a5ab8c013bac7bb4d114fa4dda0c9cfff1153b..0da467ca8685157e453872a84e0760f11aa2641e 100644 (file)
@@ -100,6 +100,11 @@ describe('Dynamic cluster pool test suite', () => {
     expect(longRunningPool.workerNodes.length).toBe(max)
     await TestUtils.waitExits(longRunningPool, max - min)
     expect(longRunningPool.workerNodes.length).toBe(min)
+    expect(
+      longRunningPool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        longRunningPool.workerChoiceStrategyContext.workerChoiceStrategy
+      ).nextWorkerNodeId
+    ).toBeLessThan(longRunningPool.workerNodes.length)
     // We need to clean up the resources after our test
     await longRunningPool.destroy()
   })
index 2fff1b13772e43c4acad99ad48e7441fda474dee..d4d2ec1b233c3cc0eda0c78621733cc9441478d9 100644 (file)
@@ -42,9 +42,12 @@ describe('Selection strategies test suite', () => {
     expect(pool.opts.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.ROUND_ROBIN
     )
+    expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+      WorkerChoiceStrategies.ROUND_ROBIN
+    )
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
-        WorkerChoiceStrategies.ROUND_ROBIN
+        pool.workerChoiceStrategyContext.workerChoiceStrategy
       ).nextWorkerNodeId
     ).toBe(0)
     // We need to clean up the resources after our test
@@ -61,6 +64,9 @@ describe('Selection strategies test suite', () => {
     expect(pool.opts.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.ROUND_ROBIN
     )
+    expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+      WorkerChoiceStrategies.ROUND_ROBIN
+    )
     // We need to clean up the resources after our test
     await pool.destroy()
   })
@@ -68,9 +74,9 @@ describe('Selection strategies test suite', () => {
   it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
     let pool = new FixedThreadPool(
       max,
-      './tests/worker-files/thread/testWorker.js'
+      './tests/worker-files/thread/testWorker.js',
+      { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
     )
-    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
     ).toBe(false)
@@ -84,9 +90,9 @@ describe('Selection strategies test suite', () => {
     pool = new DynamicThreadPool(
       min,
       max,
-      './tests/worker-files/thread/testWorker.js'
+      './tests/worker-files/thread/testWorker.js',
+      { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
     )
-    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
     ).toBe(false)
@@ -106,9 +112,6 @@ describe('Selection strategies test suite', () => {
       './tests/worker-files/thread/testWorker.js',
       { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
     )
-    expect(pool.opts.workerChoiceStrategy).toBe(
-      WorkerChoiceStrategies.ROUND_ROBIN
-    )
     // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
     const promises = []
     for (let i = 0; i < max * 2; i++) {
@@ -126,9 +129,6 @@ describe('Selection strategies test suite', () => {
       './tests/worker-files/thread/testWorker.js',
       { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
     )
-    expect(pool.opts.workerChoiceStrategy).toBe(
-      WorkerChoiceStrategies.ROUND_ROBIN
-    )
     // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
     const promises = []
     for (let i = 0; i < max * 2; i++) {
@@ -173,7 +173,7 @@ describe('Selection strategies test suite', () => {
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
-        WorkerChoiceStrategies.ROUND_ROBIN
+        pool.workerChoiceStrategyContext.workerChoiceStrategy
       ).nextWorkerNodeId
     ).toBe(0)
     await pool.destroy()
@@ -191,7 +191,7 @@ describe('Selection strategies test suite', () => {
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
-        WorkerChoiceStrategies.ROUND_ROBIN
+        pool.workerChoiceStrategyContext.workerChoiceStrategy
       ).nextWorkerNodeId
     ).toBe(0)
     // We need to clean up the resources after our test
@@ -207,6 +207,9 @@ describe('Selection strategies test suite', () => {
     expect(pool.opts.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.LESS_USED
     )
+    expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+      WorkerChoiceStrategies.LESS_USED
+    )
     // We need to clean up the resources after our test
     await pool.destroy()
   })
@@ -220,6 +223,9 @@ describe('Selection strategies test suite', () => {
     expect(pool.opts.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.LESS_USED
     )
+    expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+      WorkerChoiceStrategies.LESS_USED
+    )
     // We need to clean up the resources after our test
     await pool.destroy()
   })
@@ -227,9 +233,9 @@ describe('Selection strategies test suite', () => {
   it('Verify LESS_USED strategy default tasks usage statistics requirements', async () => {
     let pool = new FixedThreadPool(
       max,
-      './tests/worker-files/thread/testWorker.js'
+      './tests/worker-files/thread/testWorker.js',
+      { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED }
     )
-    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED)
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
     ).toBe(false)
@@ -243,9 +249,9 @@ describe('Selection strategies test suite', () => {
     pool = new DynamicThreadPool(
       min,
       max,
-      './tests/worker-files/thread/testWorker.js'
+      './tests/worker-files/thread/testWorker.js',
+      { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED }
     )
-    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED)
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
     ).toBe(false)
@@ -301,6 +307,9 @@ describe('Selection strategies test suite', () => {
     expect(pool.opts.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.LESS_BUSY
     )
+    expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+      WorkerChoiceStrategies.LESS_BUSY
+    )
     // We need to clean up the resources after our test
     await pool.destroy()
   })
@@ -314,6 +323,9 @@ describe('Selection strategies test suite', () => {
     expect(pool.opts.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.LESS_BUSY
     )
+    expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+      WorkerChoiceStrategies.LESS_BUSY
+    )
     // We need to clean up the resources after our test
     await pool.destroy()
   })
@@ -321,9 +333,9 @@ describe('Selection strategies test suite', () => {
   it('Verify LESS_BUSY strategy default tasks usage statistics requirements', async () => {
     let pool = new FixedThreadPool(
       max,
-      './tests/worker-files/thread/testWorker.js'
+      './tests/worker-files/thread/testWorker.js',
+      { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY }
     )
-    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY)
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
     ).toBe(true)
@@ -337,9 +349,9 @@ describe('Selection strategies test suite', () => {
     pool = new DynamicThreadPool(
       min,
       max,
-      './tests/worker-files/thread/testWorker.js'
+      './tests/worker-files/thread/testWorker.js',
+      { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY }
     )
-    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY)
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
     ).toBe(true)
@@ -395,17 +407,20 @@ describe('Selection strategies test suite', () => {
     expect(pool.opts.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.FAIR_SHARE
     )
+    expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+      WorkerChoiceStrategies.FAIR_SHARE
+    )
     for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
-      .get(WorkerChoiceStrategies.FAIR_SHARE)
+      .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
       .workerLastVirtualTaskTimestamp.keys()) {
       expect(
         pool.workerChoiceStrategyContext.workerChoiceStrategies
-          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
           .workerLastVirtualTaskTimestamp.get(workerNodeKey).start
       ).toBe(0)
       expect(
         pool.workerChoiceStrategyContext.workerChoiceStrategies
-          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
           .workerLastVirtualTaskTimestamp.get(workerNodeKey).end
       ).toBe(0)
     }
@@ -422,6 +437,9 @@ describe('Selection strategies test suite', () => {
     expect(pool.opts.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.FAIR_SHARE
     )
+    expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+      WorkerChoiceStrategies.FAIR_SHARE
+    )
     // We need to clean up the resources after our test
     await pool.destroy()
   })
@@ -429,9 +447,9 @@ describe('Selection strategies test suite', () => {
   it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
     let pool = new FixedThreadPool(
       max,
-      './tests/worker-files/thread/testWorker.js'
+      './tests/worker-files/thread/testWorker.js',
+      { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
     )
-    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
     ).toBe(true)
@@ -445,9 +463,9 @@ describe('Selection strategies test suite', () => {
     pool = new DynamicThreadPool(
       min,
       max,
-      './tests/worker-files/thread/testWorker.js'
+      './tests/worker-files/thread/testWorker.js',
+      { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
     )
-    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
     ).toBe(true)
@@ -475,7 +493,7 @@ describe('Selection strategies test suite', () => {
     await Promise.all(promises)
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
-        WorkerChoiceStrategies.FAIR_SHARE
+        pool.workerChoiceStrategyContext.workerChoiceStrategy
       ).workerLastVirtualTaskTimestamp.size
     ).toBe(pool.workerNodes.length)
     // We need to clean up the resources after our test
@@ -499,7 +517,7 @@ describe('Selection strategies test suite', () => {
     // if (process.platform !== 'win32') {
     //   expect(
     //     pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
-    //       WorkerChoiceStrategies.FAIR_SHARE
+    //       pool.workerChoiceStrategyContext.workerChoiceStrategy
     //     ).workerLastVirtualTaskTimestamp.size
     //   ).toBe(pool.workerNodes.length)
     // }
@@ -519,16 +537,16 @@ describe('Selection strategies test suite', () => {
     ).toBeDefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
     for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
-      .get(WorkerChoiceStrategies.FAIR_SHARE)
+      .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
       .workerLastVirtualTaskTimestamp.keys()) {
       expect(
         pool.workerChoiceStrategyContext.workerChoiceStrategies
-          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
           .workerLastVirtualTaskTimestamp.get(workerNodeKey).start
       ).toBe(0)
       expect(
         pool.workerChoiceStrategyContext.workerChoiceStrategies
-          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
           .workerLastVirtualTaskTimestamp.get(workerNodeKey).end
       ).toBe(0)
     }
@@ -545,16 +563,16 @@ describe('Selection strategies test suite', () => {
     ).toBeDefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
     for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
-      .get(WorkerChoiceStrategies.FAIR_SHARE)
+      .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
       .workerLastVirtualTaskTimestamp.keys()) {
       expect(
         pool.workerChoiceStrategyContext.workerChoiceStrategies
-          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
           .workerLastVirtualTaskTimestamp.get(workerNodeKey).start
       ).toBe(0)
       expect(
         pool.workerChoiceStrategyContext.workerChoiceStrategies
-          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
           .workerLastVirtualTaskTimestamp.get(workerNodeKey).end
       ).toBe(0)
     }
@@ -571,27 +589,30 @@ describe('Selection strategies test suite', () => {
     expect(pool.opts.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
     )
+    expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+      WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+    )
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
-        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+        pool.workerChoiceStrategyContext.workerChoiceStrategy
       ).currentWorkerNodeId
     ).toBe(0)
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
-        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+        pool.workerChoiceStrategyContext.workerChoiceStrategy
       ).defaultWorkerWeight
     ).toBeGreaterThan(0)
     for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
-      .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+      .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
       .workersTaskRunTime.keys()) {
       expect(
         pool.workerChoiceStrategyContext.workerChoiceStrategies
-          .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+          .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
           .workersTaskRunTime.get(workerNodeKey).weight
       ).toBeGreaterThan(0)
       expect(
         pool.workerChoiceStrategyContext.workerChoiceStrategies
-          .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+          .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
           .workersTaskRunTime.get(workerNodeKey).runTime
       ).toBe(0)
     }
@@ -608,6 +629,9 @@ describe('Selection strategies test suite', () => {
     expect(pool.opts.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
     )
+    expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+      WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+    )
     // We need to clean up the resources after our test
     await pool.destroy()
   })
@@ -615,9 +639,9 @@ describe('Selection strategies test suite', () => {
   it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
     let pool = new FixedThreadPool(
       max,
-      './tests/worker-files/thread/testWorker.js'
+      './tests/worker-files/thread/testWorker.js',
+      { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
     )
-    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
     ).toBe(true)
@@ -631,9 +655,9 @@ describe('Selection strategies test suite', () => {
     pool = new DynamicThreadPool(
       min,
       max,
-      './tests/worker-files/thread/testWorker.js'
+      './tests/worker-files/thread/testWorker.js',
+      { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
     )
-    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
     ).toBe(true)
@@ -661,7 +685,7 @@ describe('Selection strategies test suite', () => {
     await Promise.all(promises)
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
-        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+        pool.workerChoiceStrategyContext.workerChoiceStrategy
       ).workersTaskRunTime.size
     ).toBe(pool.workerNodes.length)
     // We need to clean up the resources after our test
@@ -679,7 +703,7 @@ describe('Selection strategies test suite', () => {
     const promises = []
     const maxMultiplier =
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
-        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+        pool.workerChoiceStrategyContext.workerChoiceStrategy
       ).defaultWorkerWeight * 50
     for (let i = 0; i < max * maxMultiplier; i++) {
       promises.push(pool.execute())
@@ -688,7 +712,7 @@ describe('Selection strategies test suite', () => {
     if (process.platform !== 'win32') {
       expect(
         pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
-          WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+          pool.workerChoiceStrategyContext.workerChoiceStrategy
         ).workersTaskRunTime.size
       ).toBe(pool.workerNodes.length)
     }
@@ -719,20 +743,20 @@ describe('Selection strategies test suite', () => {
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
-        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+        pool.workerChoiceStrategyContext.workerChoiceStrategy
       ).currentWorkerNodeId
     ).toBe(0)
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
-        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+        pool.workerChoiceStrategyContext.workerChoiceStrategy
       ).defaultWorkerWeight
     ).toBeGreaterThan(0)
     for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
-      .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+      .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
       .workersTaskRunTime.keys()) {
       expect(
         pool.workerChoiceStrategyContext.workerChoiceStrategies
-          .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+          .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
           .workersTaskRunTime.get(workerNodeKey).runTime
       ).toBe(0)
     }
@@ -760,20 +784,20 @@ describe('Selection strategies test suite', () => {
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
-        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+        pool.workerChoiceStrategyContext.workerChoiceStrategy
       ).currentWorkerNodeId
     ).toBe(0)
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
-        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+        pool.workerChoiceStrategyContext.workerChoiceStrategy
       ).defaultWorkerWeight
     ).toBeGreaterThan(0)
     for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
-      .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+      .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
       .workersTaskRunTime.keys()) {
       expect(
         pool.workerChoiceStrategyContext.workerChoiceStrategies
-          .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+          .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
           .workersTaskRunTime.get(workerNodeKey).runTime
       ).toBe(0)
     }
index a8f187eee6098fab5bb6c8ede3ec5cff434da3d3..c571478cbba66e3810e7cb66c740dd681cc95353 100644 (file)
@@ -69,17 +69,17 @@ describe('Worker choice strategy context test suite', () => {
         choose: sinon.stub().returns(0)
       }
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.ROUND_ROBIN
     )
     workerChoiceStrategyContext.workerChoiceStrategies.set(
-      workerChoiceStrategyContext.workerChoiceStrategyType,
+      workerChoiceStrategyContext.workerChoiceStrategy,
       WorkerChoiceStrategyStub
     )
     const chosenWorkerKey = workerChoiceStrategyContext.execute()
     expect(
       workerChoiceStrategyContext.workerChoiceStrategies.get(
-        workerChoiceStrategyContext.workerChoiceStrategyType
+        workerChoiceStrategyContext.workerChoiceStrategy
       ).choose.calledOnce
     ).toBe(true)
     expect(chosenWorkerKey).toBe(0)
@@ -95,17 +95,17 @@ describe('Worker choice strategy context test suite', () => {
         choose: sinon.stub().returns(0)
       }
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.ROUND_ROBIN
     )
     workerChoiceStrategyContext.workerChoiceStrategies.set(
-      workerChoiceStrategyContext.workerChoiceStrategyType,
+      workerChoiceStrategyContext.workerChoiceStrategy,
       WorkerChoiceStrategyStub
     )
     const chosenWorkerKey = workerChoiceStrategyContext.execute()
     expect(
       workerChoiceStrategyContext.workerChoiceStrategies.get(
-        workerChoiceStrategyContext.workerChoiceStrategyType
+        workerChoiceStrategyContext.workerChoiceStrategy
       ).choose.calledOnce
     ).toBe(true)
     expect(chosenWorkerKey).toBe(0)
@@ -125,7 +125,7 @@ describe('Worker choice strategy context test suite', () => {
         WorkerChoiceStrategies.ROUND_ROBIN
       )
     ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
-    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.ROUND_ROBIN
     )
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
@@ -136,7 +136,7 @@ describe('Worker choice strategy context test suite', () => {
         WorkerChoiceStrategies.ROUND_ROBIN
       )
     ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
-    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.ROUND_ROBIN
     )
   })
@@ -155,7 +155,7 @@ describe('Worker choice strategy context test suite', () => {
         WorkerChoiceStrategies.ROUND_ROBIN
       )
     ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
-    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.ROUND_ROBIN
     )
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
@@ -166,7 +166,7 @@ describe('Worker choice strategy context test suite', () => {
         WorkerChoiceStrategies.ROUND_ROBIN
       )
     ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
-    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.ROUND_ROBIN
     )
   })
@@ -188,7 +188,7 @@ describe('Worker choice strategy context test suite', () => {
         WorkerChoiceStrategies.LESS_USED
       )
     ).toBeInstanceOf(LessUsedWorkerChoiceStrategy)
-    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.LESS_USED
     )
   })
@@ -210,7 +210,7 @@ describe('Worker choice strategy context test suite', () => {
         WorkerChoiceStrategies.LESS_USED
       )
     ).toBeInstanceOf(LessUsedWorkerChoiceStrategy)
-    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.LESS_USED
     )
   })
@@ -232,7 +232,7 @@ describe('Worker choice strategy context test suite', () => {
         WorkerChoiceStrategies.LESS_BUSY
       )
     ).toBeInstanceOf(LessBusyWorkerChoiceStrategy)
-    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.LESS_BUSY
     )
   })
@@ -254,7 +254,7 @@ describe('Worker choice strategy context test suite', () => {
         WorkerChoiceStrategies.LESS_BUSY
       )
     ).toBeInstanceOf(LessBusyWorkerChoiceStrategy)
-    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.LESS_BUSY
     )
   })
@@ -276,7 +276,7 @@ describe('Worker choice strategy context test suite', () => {
         WorkerChoiceStrategies.FAIR_SHARE
       )
     ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
-    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.FAIR_SHARE
     )
   })
@@ -298,7 +298,7 @@ describe('Worker choice strategy context test suite', () => {
         WorkerChoiceStrategies.FAIR_SHARE
       )
     ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
-    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.FAIR_SHARE
     )
   })
@@ -320,7 +320,7 @@ describe('Worker choice strategy context test suite', () => {
         WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
       )
     ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
-    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
     )
   })
@@ -342,7 +342,7 @@ describe('Worker choice strategy context test suite', () => {
         WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
       )
     ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
-    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
     )
   })
index feb4955d9a9951fbd2dc341028acb76bc638c661..11d91128f3dc2716ae3901b21a132e3aa10996dc 100644 (file)
@@ -100,6 +100,11 @@ describe('Dynamic thread pool test suite', () => {
     expect(longRunningPool.workerNodes.length).toBe(max)
     await TestUtils.waitExits(longRunningPool, max - min)
     expect(longRunningPool.workerNodes.length).toBe(min)
+    expect(
+      longRunningPool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        longRunningPool.workerChoiceStrategyContext.workerChoiceStrategy
+      ).nextWorkerNodeId
+    ).toBeLessThan(longRunningPool.workerNodes.length)
     // We need to clean up the resources after our test
     await longRunningPool.destroy()
   })