X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fselection-strategies%2Fworker-choice-strategy-context.test.mjs;h=6615b8c445f0b069b645577017f2725685a0bafa;hb=e254f4275039e82b3cfe6fe5d4e060ad53e6b9a3;hp=f9b1c8d590ac39e9d8657bc9348456483808af0a;hpb=a074ffee1b46f43d0dcfba58128748c7492104dd;p=poolifier.git diff --git a/tests/pools/selection-strategies/worker-choice-strategy-context.test.mjs b/tests/pools/selection-strategies/worker-choice-strategy-context.test.mjs index f9b1c8d5..6615b8c4 100644 --- a/tests/pools/selection-strategies/worker-choice-strategy-context.test.mjs +++ b/tests/pools/selection-strategies/worker-choice-strategy-context.test.mjs @@ -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 @@ -22,12 +22,12 @@ describe('Worker choice strategy context test suite', () => { before(() => { fixedPool = new FixedThreadPool( max, - './tests/worker-files/thread/testWorker.js' + './tests/worker-files/thread/testWorker.mjs' ) dynamicPool = new DynamicThreadPool( min, max, - './tests/worker-files/thread/testWorker.js' + './tests/worker-files/thread/testWorker.mjs' ) }) @@ -49,13 +49,14 @@ describe('Worker choice strategy context test suite', () => { ) }) - it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => { + it('Verify that execute() return the worker node key chosen by the strategy with fixed pool', () => { const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool ) - const WorkerChoiceStrategyStub = createStubInstance( + const workerChoiceStrategyStub = createStubInstance( RoundRobinWorkerChoiceStrategy, { + hasPoolWorkerNodesReady: stub().returns(true), choose: stub().returns(0) } ) @@ -64,7 +65,7 @@ describe('Worker choice strategy context test suite', () => { ) workerChoiceStrategyContext.workerChoiceStrategies.set( workerChoiceStrategyContext.workerChoiceStrategy, - WorkerChoiceStrategyStub + workerChoiceStrategyStub ) const chosenWorkerKey = workerChoiceStrategyContext.execute() expect( @@ -79,44 +80,123 @@ describe('Worker choice strategy context test suite', () => { const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool ) - const WorkerChoiceStrategyUndefinedStub = createStubInstance( + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + WorkerChoiceStrategies.ROUND_ROBIN + ) + const workerChoiceStrategyUndefinedStub = createStubInstance( RoundRobinWorkerChoiceStrategy, { + hasPoolWorkerNodesReady: stub().returns(true), choose: stub().returns(undefined) } ) - const WorkerChoiceStrategyNullStub = createStubInstance( + 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, { + hasPoolWorkerNodesReady: stub().returns(true), 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 ${ + fixedPool.info.maxSize + + Object.keys(workerChoiceStrategyContext.opts.weights).length + } retries` + ) + ) + }) + + it('Verify that execute() retry until a worker node is ready and chosen', () => { + const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( + fixedPool + ) + const workerChoiceStrategyStub = createStubInstance( + RoundRobinWorkerChoiceStrategy, + { + hasPoolWorkerNodesReady: stub() + .onCall(0) + .returns(false) + .onCall(1) + .returns(false) + .onCall(2) + .returns(false) + .onCall(3) + .returns(false) + .onCall(4) + .returns(false) + .returns(true), + choose: stub().returns(1) + } + ) expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.ROUND_ROBIN ) workerChoiceStrategyContext.workerChoiceStrategies.set( workerChoiceStrategyContext.workerChoiceStrategy, - WorkerChoiceStrategyUndefinedStub + workerChoiceStrategyStub ) - expect(() => workerChoiceStrategyContext.execute()).toThrowError( - new Error('Worker node key chosen is null or undefined after 6 retries') + const chosenWorkerKey = workerChoiceStrategyContext.execute() + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategyContext.workerChoiceStrategy + ).hasPoolWorkerNodesReady.callCount + ).toBe(6) + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategyContext.workerChoiceStrategy + ).choose.callCount + ).toBe(1) + expect(chosenWorkerKey).toBe(1) + }) + + it('Verify that execute() throws error if worker choice strategy recursion reach the maximum depth', () => { + const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( + fixedPool + ) + const workerChoiceStrategyStub = createStubInstance( + RoundRobinWorkerChoiceStrategy, + { + hasPoolWorkerNodesReady: stub().returns(false), + choose: stub().returns(0) + } + ) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + WorkerChoiceStrategies.ROUND_ROBIN ) workerChoiceStrategyContext.workerChoiceStrategies.set( workerChoiceStrategyContext.workerChoiceStrategy, - WorkerChoiceStrategyNullStub + workerChoiceStrategyStub ) - expect(() => workerChoiceStrategyContext.execute()).toThrowError( - new Error('Worker node key chosen is null or undefined after 6 retries') + expect(() => workerChoiceStrategyContext.execute()).toThrow( + new RangeError('Maximum call stack size exceeded') ) }) - 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 ) - const WorkerChoiceStrategyStub = createStubInstance( + const workerChoiceStrategyStub = createStubInstance( RoundRobinWorkerChoiceStrategy, { + hasPoolWorkerNodesReady: stub().returns(true), choose: stub().returns(0) } ) @@ -125,7 +205,7 @@ describe('Worker choice strategy context test suite', () => { ) workerChoiceStrategyContext.workerChoiceStrategies.set( workerChoiceStrategyContext.workerChoiceStrategy, - WorkerChoiceStrategyStub + workerChoiceStrategyStub ) const chosenWorkerKey = workerChoiceStrategyContext.execute() expect(