feat: cache in a map worker choice strategy to allow conditionnal reuse
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 5 Apr 2023 21:12:28 +0000 (23:12 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 5 Apr 2023 21:12:28 +0000 (23:12 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
CHANGELOG.md
src/pools/selection-strategies/worker-choice-strategy-context.ts
tests/pools/selection-strategies/selection-strategies.test.js
tests/pools/selection-strategies/worker-choice-strategy-context.test.js

index a2cc015da1f2bb7ec43e843eaa909fd963abb38e..30d1e724d1890029a61b031da8daccd51eab6723 100644 (file)
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Added
 
 - Add `full` event to dynamic pool.
+- Keep worker choice strategy usage in memory for reuse.
 
 ## [2.4.1] - 2023-04-05
 
index 644adfea1f3e9a10dd156a03db31983402438185..90c98c6018a1c786fda04234fd48eb5557b0edec 100644 (file)
@@ -24,7 +24,10 @@ export class WorkerChoiceStrategyContext<
   Data = unknown,
   Response = unknown
 > {
-  private workerChoiceStrategy: IWorkerChoiceStrategy<Worker, Data, Response>
+  private readonly workerChoiceStrategies = new Map<
+  WorkerChoiceStrategy,
+  IWorkerChoiceStrategy<Worker, Data, Response>
+  >()
 
   /**
    * Worker choice strategy context constructor.
@@ -39,10 +42,7 @@ export class WorkerChoiceStrategyContext<
     private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
   ) {
     this.execute.bind(this)
-    this.workerChoiceStrategy = this.getWorkerChoiceStrategy(
-      pool,
-      this.workerChoiceStrategyType
-    )
+    this.registerWorkerChoiceStrategy(pool, workerChoiceStrategyType)
   }
 
   /**
@@ -51,7 +51,11 @@ export class WorkerChoiceStrategyContext<
    * @returns The required statistics.
    */
   public getRequiredStatistics (): RequiredStatistics {
-    return this.workerChoiceStrategy.requiredStatistics
+    return (
+      this.workerChoiceStrategies.get(
+        this.workerChoiceStrategyType
+      ) as IWorkerChoiceStrategy<Worker, Data, Response>
+    ).requiredStatistics
   }
 
   /**
@@ -64,13 +68,10 @@ export class WorkerChoiceStrategyContext<
     workerChoiceStrategy: WorkerChoiceStrategy
   ): void {
     if (this.workerChoiceStrategyType === workerChoiceStrategy) {
-      this.workerChoiceStrategy?.reset()
+      this.workerChoiceStrategies.get(workerChoiceStrategy)?.reset()
     } else {
       this.workerChoiceStrategyType = workerChoiceStrategy
-      this.workerChoiceStrategy = this.getWorkerChoiceStrategy(
-        pool,
-        this.workerChoiceStrategyType
-      )
+      this.registerWorkerChoiceStrategy(pool, workerChoiceStrategy)
     }
   }
 
@@ -80,14 +81,17 @@ export class WorkerChoiceStrategyContext<
    * @returns The key of the chosen one.
    */
   public execute (): number {
+    const workerChoiceStrategy = this.workerChoiceStrategies.get(
+      this.workerChoiceStrategyType
+    ) as IWorkerChoiceStrategy<Worker, Data, Response>
     if (
-      this.workerChoiceStrategy.isDynamicPool &&
-      !this.workerChoiceStrategy.pool.full &&
-      this.workerChoiceStrategy.pool.findFreeWorkerKey() === -1
+      workerChoiceStrategy.isDynamicPool &&
+      !workerChoiceStrategy.pool.full &&
+      workerChoiceStrategy.pool.findFreeWorkerKey() === -1
     ) {
       return this.createWorkerCallback()
     }
-    return this.workerChoiceStrategy.choose()
+    return workerChoiceStrategy.choose()
   }
 
   /**
@@ -97,7 +101,23 @@ export class WorkerChoiceStrategyContext<
    * @returns `true` if the removal is successful, `false` otherwise.
    */
   public remove (workerKey: number): boolean {
-    return this.workerChoiceStrategy.remove(workerKey)
+    return (
+      this.workerChoiceStrategies.get(
+        this.workerChoiceStrategyType
+      ) as IWorkerChoiceStrategy<Worker, Data, Response>
+    ).remove(workerKey)
+  }
+
+  private registerWorkerChoiceStrategy (
+    pool: IPoolInternal<Worker, Data, Response>,
+    workerChoiceStrategy: WorkerChoiceStrategy
+  ): void {
+    if (!this.workerChoiceStrategies.has(workerChoiceStrategy)) {
+      this.workerChoiceStrategies.set(
+        workerChoiceStrategy,
+        this.getWorkerChoiceStrategy(pool, workerChoiceStrategy)
+      )
+    }
   }
 
   /**
index 71a2819bc16eae97e10aff906e6e8349dfae1cc4..254f02de7870e5c8da73ab6c260fa954a176283c 100644 (file)
@@ -43,7 +43,9 @@ describe('Selection strategies test suite', () => {
       WorkerChoiceStrategies.ROUND_ROBIN
     )
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.nextWorkerId
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.ROUND_ROBIN
+      ).nextWorkerId
     ).toBe(0)
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -158,11 +160,15 @@ describe('Selection strategies test suite', () => {
       { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
     )
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.nextWorkerId
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.ROUND_ROBIN
+      )?.nextWorkerId
     ).toBeUndefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.nextWorkerId
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.ROUND_ROBIN
+      ).nextWorkerId
     ).toBe(0)
     await pool.destroy()
     pool = new DynamicThreadPool(
@@ -172,11 +178,15 @@ describe('Selection strategies test suite', () => {
       { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
     )
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.nextWorkerId
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.ROUND_ROBIN
+      )?.nextWorkerId
     ).toBeUndefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.nextWorkerId
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.ROUND_ROBIN
+      ).nextWorkerId
     ).toBe(0)
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -367,16 +377,18 @@ describe('Selection strategies test suite', () => {
     expect(pool.opts.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.FAIR_SHARE
     )
-    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) {
+    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
+      .get(WorkerChoiceStrategies.FAIR_SHARE)
+      .workerLastVirtualTaskTimestamp.keys()) {
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(
-          workerKey
-        ).start
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .workerLastVirtualTaskTimestamp.get(workerKey).start
       ).toBe(0)
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(
-          workerKey
-        ).end
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .workerLastVirtualTaskTimestamp.get(workerKey).end
       ).toBe(0)
     }
     // We need to clean up the resources after our test
@@ -438,8 +450,9 @@ describe('Selection strategies test suite', () => {
     }
     await Promise.all(promises)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy
-        .workerLastVirtualTaskTimestamp.size
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.FAIR_SHARE
+      ).workerLastVirtualTaskTimestamp.size
     ).toBe(pool.workers.length)
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -461,8 +474,9 @@ describe('Selection strategies test suite', () => {
     await Promise.all(promises)
     // if (process.platform !== 'win32') {
     //   expect(
-    //     pool.workerChoiceStrategyContext.workerChoiceStrategy
-    //       .workerLastVirtualTaskTimestamp.size
+    //     pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+    //       WorkerChoiceStrategies.FAIR_SHARE
+    //     ).workerLastVirtualTaskTimestamp.size
     //   ).toBe(pool.workers.length)
     // }
     // We need to clean up the resources after our test
@@ -475,20 +489,23 @@ describe('Selection strategies test suite', () => {
       './tests/worker-files/thread/testWorker.js'
     )
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy
-        .workerLastVirtualTaskTimestamp
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.FAIR_SHARE
+      )?.workerLastVirtualTaskTimestamp
     ).toBeUndefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
-    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) {
+    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
+      .get(WorkerChoiceStrategies.FAIR_SHARE)
+      .workerLastVirtualTaskTimestamp.keys()) {
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(
-          workerKey
-        ).start
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .workerLastVirtualTaskTimestamp.get(workerKey).start
       ).toBe(0)
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(
-          workerKey
-        ).end
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .workerLastVirtualTaskTimestamp.get(workerKey).end
       ).toBe(0)
     }
     await pool.destroy()
@@ -498,20 +515,23 @@ describe('Selection strategies test suite', () => {
       './tests/worker-files/thread/testWorker.js'
     )
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy
-        .workerLastVirtualTaskTimestamp
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.FAIR_SHARE
+      )?.workerLastVirtualTaskTimestamp
     ).toBeUndefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
-    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) {
+    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
+      .get(WorkerChoiceStrategies.FAIR_SHARE)
+      .workerLastVirtualTaskTimestamp.keys()) {
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(
-          workerKey
-        ).start
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .workerLastVirtualTaskTimestamp.get(workerKey).start
       ).toBe(0)
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(
-          workerKey
-        ).end
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .workerLastVirtualTaskTimestamp.get(workerKey).end
       ).toBe(0)
     }
     // We need to clean up the resources after our test
@@ -528,21 +548,27 @@ describe('Selection strategies test suite', () => {
       WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
     )
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.currentWorkerId
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).currentWorkerId
     ).toBe(0)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.defaultWorkerWeight
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).defaultWorkerWeight
     ).toBeGreaterThan(0)
-    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.keys()) {
+    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
+      .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+      .workersTaskRunTime.keys()) {
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.get(
-          workerKey
-        ).weight
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+          .workersTaskRunTime.get(workerKey).weight
       ).toBeGreaterThan(0)
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.get(
-          workerKey
-        ).runTime
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+          .workersTaskRunTime.get(workerKey).runTime
       ).toBe(0)
     }
     // We need to clean up the resources after our test
@@ -604,8 +630,9 @@ describe('Selection strategies test suite', () => {
     }
     await Promise.all(promises)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime
-        .size
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).workersTaskRunTime.size
     ).toBe(pool.workers.length)
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -621,16 +648,18 @@ describe('Selection strategies test suite', () => {
     // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
     const promises = []
     const maxMultiplier =
-      pool.workerChoiceStrategyContext.workerChoiceStrategy
-        .defaultWorkerWeight * 2
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).defaultWorkerWeight * 2
     for (let i = 0; i < max * maxMultiplier; i++) {
       promises.push(pool.execute())
     }
     await Promise.all(promises)
     if (process.platform !== 'win32') {
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime
-          .size
+        pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+          WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+        ).workersTaskRunTime.size
       ).toBe(pool.workers.length)
     }
     // We need to clean up the resources after our test
@@ -643,26 +672,38 @@ describe('Selection strategies test suite', () => {
       './tests/worker-files/thread/testWorker.js'
     )
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.currentWorkerId
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      )?.currentWorkerId
     ).toBeUndefined()
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.defaultWorkerWeight
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      )?.defaultWorkerWeight
     ).toBeUndefined()
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      )?.workersTaskRunTime
     ).toBeUndefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.currentWorkerId
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).currentWorkerId
     ).toBe(0)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.defaultWorkerWeight
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).defaultWorkerWeight
     ).toBeGreaterThan(0)
-    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.keys()) {
+    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
+      .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+      .workersTaskRunTime.keys()) {
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.get(
-          workerKey
-        ).runTime
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+          .workersTaskRunTime.get(workerKey).runTime
       ).toBe(0)
     }
     await pool.destroy()
@@ -672,26 +713,38 @@ describe('Selection strategies test suite', () => {
       './tests/worker-files/thread/testWorker.js'
     )
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.currentWorkerId
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      )?.currentWorkerId
     ).toBeUndefined()
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.defaultWorkerWeight
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      )?.defaultWorkerWeight
     ).toBeUndefined()
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      )?.workersTaskRunTime
     ).toBeUndefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.currentWorkerId
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).currentWorkerId
     ).toBe(0)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.defaultWorkerWeight
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).defaultWorkerWeight
     ).toBeGreaterThan(0)
-    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.keys()) {
+    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
+      .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+      .workersTaskRunTime.keys()) {
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.get(
-          workerKey
-        ).runTime
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+          .workersTaskRunTime.get(workerKey).runTime
       ).toBe(0)
     }
     // We need to clean up the resources after our test
index a93104d225f3e9c27ff8d252719619d20654bf0a..0f02c363c0b2e3a90a5234faa2f4be4669b5959c 100644 (file)
@@ -60,10 +60,18 @@ describe('Worker choice strategy context test suite', () => {
         choose: sinon.stub().returns(0)
       }
     )
-    workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
+    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+      WorkerChoiceStrategies.ROUND_ROBIN
+    )
+    workerChoiceStrategyContext.workerChoiceStrategies.set(
+      workerChoiceStrategyContext.workerChoiceStrategyType,
+      WorkerChoiceStrategyStub
+    )
     const chosenWorkerKey = workerChoiceStrategyContext.execute()
     expect(
-      workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce
+      workerChoiceStrategyContext.workerChoiceStrategies.get(
+        workerChoiceStrategyContext.workerChoiceStrategyType
+      ).choose.calledOnce
     ).toBe(true)
     expect(chosenWorkerKey).toBe(0)
   })
@@ -78,10 +86,18 @@ describe('Worker choice strategy context test suite', () => {
         choose: sinon.stub().returns(0)
       }
     )
-    workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
+    expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
+      WorkerChoiceStrategies.ROUND_ROBIN
+    )
+    workerChoiceStrategyContext.workerChoiceStrategies.set(
+      workerChoiceStrategyContext.workerChoiceStrategyType,
+      WorkerChoiceStrategyStub
+    )
     const chosenWorkerKey = workerChoiceStrategyContext.execute()
     expect(
-      workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce
+      workerChoiceStrategyContext.workerChoiceStrategies.get(
+        workerChoiceStrategyContext.workerChoiceStrategyType
+      ).choose.calledOnce
     ).toBe(true)
     expect(chosenWorkerKey).toBe(0)
   })
@@ -90,9 +106,11 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       fixedPool
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      RoundRobinWorkerChoiceStrategy
-    )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.ROUND_ROBIN
+      )
+    ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
     expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
       WorkerChoiceStrategies.ROUND_ROBIN
     )
@@ -100,9 +118,11 @@ describe('Worker choice strategy context test suite', () => {
       fixedPool,
       WorkerChoiceStrategies.ROUND_ROBIN
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      RoundRobinWorkerChoiceStrategy
-    )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.ROUND_ROBIN
+      )
+    ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
     expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
       WorkerChoiceStrategies.ROUND_ROBIN
     )
@@ -112,9 +132,11 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       dynamicPool
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      RoundRobinWorkerChoiceStrategy
-    )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.ROUND_ROBIN
+      )
+    ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
     expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
       WorkerChoiceStrategies.ROUND_ROBIN
     )
@@ -122,9 +144,11 @@ describe('Worker choice strategy context test suite', () => {
       dynamicPool,
       WorkerChoiceStrategies.ROUND_ROBIN
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      RoundRobinWorkerChoiceStrategy
-    )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.ROUND_ROBIN
+      )
+    ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
     expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
       WorkerChoiceStrategies.ROUND_ROBIN
     )
@@ -138,9 +162,11 @@ describe('Worker choice strategy context test suite', () => {
       fixedPool,
       WorkerChoiceStrategies.LESS_USED
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      LessUsedWorkerChoiceStrategy
-    )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.LESS_USED
+      )
+    ).toBeInstanceOf(LessUsedWorkerChoiceStrategy)
     expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
       WorkerChoiceStrategies.LESS_USED
     )
@@ -154,9 +180,11 @@ describe('Worker choice strategy context test suite', () => {
       dynamicPool,
       WorkerChoiceStrategies.LESS_USED
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      LessUsedWorkerChoiceStrategy
-    )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.LESS_USED
+      )
+    ).toBeInstanceOf(LessUsedWorkerChoiceStrategy)
     expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
       WorkerChoiceStrategies.LESS_USED
     )
@@ -170,9 +198,11 @@ describe('Worker choice strategy context test suite', () => {
       fixedPool,
       WorkerChoiceStrategies.LESS_BUSY
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      LessBusyWorkerChoiceStrategy
-    )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.LESS_BUSY
+      )
+    ).toBeInstanceOf(LessBusyWorkerChoiceStrategy)
     expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
       WorkerChoiceStrategies.LESS_BUSY
     )
@@ -186,9 +216,11 @@ describe('Worker choice strategy context test suite', () => {
       dynamicPool,
       WorkerChoiceStrategies.LESS_BUSY
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      LessBusyWorkerChoiceStrategy
-    )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.LESS_BUSY
+      )
+    ).toBeInstanceOf(LessBusyWorkerChoiceStrategy)
     expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
       WorkerChoiceStrategies.LESS_BUSY
     )
@@ -202,9 +234,11 @@ describe('Worker choice strategy context test suite', () => {
       fixedPool,
       WorkerChoiceStrategies.FAIR_SHARE
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      FairShareWorkerChoiceStrategy
-    )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.FAIR_SHARE
+      )
+    ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
     expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
       WorkerChoiceStrategies.FAIR_SHARE
     )
@@ -218,9 +252,11 @@ describe('Worker choice strategy context test suite', () => {
       dynamicPool,
       WorkerChoiceStrategies.FAIR_SHARE
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      FairShareWorkerChoiceStrategy
-    )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.FAIR_SHARE
+      )
+    ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
     expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
       WorkerChoiceStrategies.FAIR_SHARE
     )
@@ -234,9 +270,11 @@ describe('Worker choice strategy context test suite', () => {
       fixedPool,
       WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      WeightedRoundRobinWorkerChoiceStrategy
-    )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      )
+    ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
     expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
       WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
     )
@@ -250,9 +288,11 @@ describe('Worker choice strategy context test suite', () => {
       dynamicPool,
       WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      WeightedRoundRobinWorkerChoiceStrategy
-    )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      )
+    ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
     expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
       WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
     )