refactor: refine prettier configuration
[poolifier.git] / tests / pools / selection-strategies / worker-choice-strategy-context.test.mjs
index dc898e4ff04cd8ccb554a1215191896354757928..eb854b58badd66a4a16b1fcd9c49b445d06f8c93 100644 (file)
@@ -4,15 +4,15 @@ import {
   DynamicThreadPool,
   FixedThreadPool,
   WorkerChoiceStrategies
-} from '../../../lib/index.js'
-import { WorkerChoiceStrategyContext } from '../../../lib/pools/selection-strategies/worker-choice-strategy-context.js'
-import { RoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy.js'
-import { LeastUsedWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/least-used-worker-choice-strategy.js'
-import { LeastBusyWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/least-busy-worker-choice-strategy.js'
-import { LeastEluWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/least-elu-worker-choice-strategy.js'
-import { FairShareWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy.js'
-import { WeightedRoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.js'
-import { InterleavedWeightedRoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.js'
+} from '../../../lib/index.cjs'
+import { WorkerChoiceStrategyContext } from '../../../lib/pools/selection-strategies/worker-choice-strategy-context.cjs'
+import { RoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy.cjs'
+import { LeastUsedWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/least-used-worker-choice-strategy.cjs'
+import { LeastBusyWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/least-busy-worker-choice-strategy.cjs'
+import { LeastEluWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/least-elu-worker-choice-strategy.cjs'
+import { FairShareWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy.cjs'
+import { WeightedRoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.cjs'
+import { InterleavedWeightedRoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.cjs'
 
 describe('Worker choice strategy context test suite', () => {
   const min = 1
@@ -41,22 +41,83 @@ describe('Worker choice strategy context test suite', () => {
   })
 
   it('Verify that constructor() initializes the context with all the available worker choice strategies', () => {
-    const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
-      fixedPool
+    let workerChoiceStrategyContext = new WorkerChoiceStrategyContext(fixedPool)
+    expect(workerChoiceStrategyContext.workerChoiceStrategies.size).toBe(
+      Object.keys(WorkerChoiceStrategies).length
     )
+    workerChoiceStrategyContext = new WorkerChoiceStrategyContext(dynamicPool)
     expect(workerChoiceStrategyContext.workerChoiceStrategies.size).toBe(
       Object.keys(WorkerChoiceStrategies).length
     )
   })
 
-  it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
+  it('Verify that constructor() initializes the context with retries attribute properly set', () => {
+    let workerChoiceStrategyContext = new WorkerChoiceStrategyContext(fixedPool)
+    expect(workerChoiceStrategyContext.retries).toBe(fixedPool.info.maxSize * 2)
+    workerChoiceStrategyContext = new WorkerChoiceStrategyContext(dynamicPool)
+    expect(workerChoiceStrategyContext.retries).toBe(
+      dynamicPool.info.maxSize * 2
+    )
+  })
+
+  it('Verify that execute() throws error if null or undefined is returned after retries', () => {
+    const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+      fixedPool
+    )
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+      WorkerChoiceStrategies.ROUND_ROBIN
+    )
+    const workerChoiceStrategyUndefinedStub = createStubInstance(
+      RoundRobinWorkerChoiceStrategy,
+      {
+        choose: stub().returns(undefined)
+      }
+    )
+    workerChoiceStrategyContext.workerChoiceStrategies.set(
+      workerChoiceStrategyContext.workerChoiceStrategy,
+      workerChoiceStrategyUndefinedStub
+    )
+    expect(() => workerChoiceStrategyContext.execute()).toThrow(
+      new Error(
+        `Worker node key chosen is null or undefined after ${workerChoiceStrategyContext.retries} retries`
+      )
+    )
+    const workerChoiceStrategyNullStub = createStubInstance(
+      RoundRobinWorkerChoiceStrategy,
+      {
+        choose: stub().returns(null)
+      }
+    )
+    workerChoiceStrategyContext.workerChoiceStrategies.set(
+      workerChoiceStrategyContext.workerChoiceStrategy,
+      workerChoiceStrategyNullStub
+    )
+    expect(() => workerChoiceStrategyContext.execute()).toThrow(
+      new Error(
+        `Worker node key chosen is null or undefined after ${workerChoiceStrategyContext.retries} retries`
+      )
+    )
+  })
+
+  it('Verify that execute() retry until a worker node is chosen', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       fixedPool
     )
     const workerChoiceStrategyStub = createStubInstance(
       RoundRobinWorkerChoiceStrategy,
       {
-        choose: stub().returns(0)
+        choose: stub()
+          .onCall(0)
+          .returns(undefined)
+          .onCall(1)
+          .returns(undefined)
+          .onCall(2)
+          .returns(undefined)
+          .onCall(3)
+          .returns(undefined)
+          .onCall(4)
+          .returns(undefined)
+          .returns(1)
       }
     )
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
@@ -70,25 +131,19 @@ describe('Worker choice strategy context test suite', () => {
     expect(
       workerChoiceStrategyContext.workerChoiceStrategies.get(
         workerChoiceStrategyContext.workerChoiceStrategy
-      ).choose.calledOnce
-    ).toBe(true)
-    expect(chosenWorkerKey).toBe(0)
+      ).choose.callCount
+    ).toBe(6)
+    expect(chosenWorkerKey).toBe(1)
   })
 
-  it('Verify that execute() throws error if null or undefined is returned after retries', () => {
+  it('Verify that execute() return the worker node key chosen by the strategy with fixed pool', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       fixedPool
     )
-    const workerChoiceStrategyUndefinedStub = createStubInstance(
-      RoundRobinWorkerChoiceStrategy,
-      {
-        choose: stub().returns(undefined)
-      }
-    )
-    const workerChoiceStrategyNullStub = createStubInstance(
+    const workerChoiceStrategyStub = createStubInstance(
       RoundRobinWorkerChoiceStrategy,
       {
-        choose: stub().returns(null)
+        choose: stub().returns(0)
       }
     )
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
@@ -96,21 +151,18 @@ describe('Worker choice strategy context test suite', () => {
     )
     workerChoiceStrategyContext.workerChoiceStrategies.set(
       workerChoiceStrategyContext.workerChoiceStrategy,
-      workerChoiceStrategyUndefinedStub
-    )
-    expect(() => workerChoiceStrategyContext.execute()).toThrowError(
-      new Error('Worker node key chosen is null or undefined after 6 retries')
-    )
-    workerChoiceStrategyContext.workerChoiceStrategies.set(
-      workerChoiceStrategyContext.workerChoiceStrategy,
-      workerChoiceStrategyNullStub
-    )
-    expect(() => workerChoiceStrategyContext.execute()).toThrowError(
-      new Error('Worker node key chosen is null or undefined after 6 retries')
+      workerChoiceStrategyStub
     )
+    const chosenWorkerKey = workerChoiceStrategyContext.execute()
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategies.get(
+        workerChoiceStrategyContext.workerChoiceStrategy
+      ).choose.calledOnce
+    ).toBe(true)
+    expect(chosenWorkerKey).toBe(0)
   })
 
-  it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
+  it('Verify that execute() return the worker node key chosen by the strategy with dynamic pool', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       dynamicPool
     )