build: switch default to ESM
[poolifier.git] / tests / pools / selection-strategies / worker-choice-strategy-context.test.mjs
index a5447b73f7fdaa7b3f5fa6a6905484b1d34f2b2b..6615b8c445f0b069b645577017f2725685a0bafa 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
@@ -80,6 +80,9 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       fixedPool
     )
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+      WorkerChoiceStrategies.ROUND_ROBIN
+    )
     const workerChoiceStrategyUndefinedStub = createStubInstance(
       RoundRobinWorkerChoiceStrategy,
       {
@@ -87,6 +90,18 @@ describe('Worker choice strategy context test suite', () => {
         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 ${
+          fixedPool.info.maxSize +
+          Object.keys(workerChoiceStrategyContext.opts.weights).length
+        } retries`
+      )
+    )
     const workerChoiceStrategyNullStub = createStubInstance(
       RoundRobinWorkerChoiceStrategy,
       {
@@ -94,22 +109,17 @@ describe('Worker choice strategy context test suite', () => {
         choose: stub().returns(null)
       }
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
-      WorkerChoiceStrategies.ROUND_ROBIN
-    )
-    workerChoiceStrategyContext.workerChoiceStrategies.set(
-      workerChoiceStrategyContext.workerChoiceStrategy,
-      workerChoiceStrategyUndefinedStub
-    )
-    expect(() => workerChoiceStrategyContext.execute()).toThrow(
-      new Error('Worker node key chosen is null or undefined after 6 retries')
-    )
     workerChoiceStrategyContext.workerChoiceStrategies.set(
       workerChoiceStrategyContext.workerChoiceStrategy,
       workerChoiceStrategyNullStub
     )
     expect(() => workerChoiceStrategyContext.execute()).toThrow(
-      new Error('Worker node key chosen is null or undefined after 6 retries')
+      new Error(
+        `Worker node key chosen is null or undefined after ${
+          fixedPool.info.maxSize +
+          Object.keys(workerChoiceStrategyContext.opts.weights).length
+        } retries`
+      )
     )
   })
 
@@ -131,12 +141,6 @@ describe('Worker choice strategy context test suite', () => {
           .returns(false)
           .onCall(4)
           .returns(false)
-          .onCall(6)
-          .returns(false)
-          .onCall(7)
-          .returns(false)
-          .onCall(8)
-          .returns(false)
           .returns(true),
         choose: stub().returns(1)
       }
@@ -153,7 +157,7 @@ describe('Worker choice strategy context test suite', () => {
       workerChoiceStrategyContext.workerChoiceStrategies.get(
         workerChoiceStrategyContext.workerChoiceStrategy
       ).hasPoolWorkerNodesReady.callCount
-    ).toBe(12)
+    ).toBe(6)
     expect(
       workerChoiceStrategyContext.workerChoiceStrategies.get(
         workerChoiceStrategyContext.workerChoiceStrategy
@@ -162,7 +166,7 @@ describe('Worker choice strategy context test suite', () => {
     expect(chosenWorkerKey).toBe(1)
   })
 
-  it('Verify that execute() throws error if worker choice strategy consecutive executions has been reached', () => {
+  it('Verify that execute() throws error if worker choice strategy recursion reach the maximum depth', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       fixedPool
     )
@@ -181,9 +185,7 @@ describe('Worker choice strategy context test suite', () => {
       workerChoiceStrategyStub
     )
     expect(() => workerChoiceStrategyContext.execute()).toThrow(
-      new RangeError(
-        'Worker choice strategy consecutive executions has exceeded the maximum of 10000'
-      )
+      new RangeError('Maximum call stack size exceeded')
     )
   })