X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fselection-strategies%2Fworker-choice-strategy-context.test.mjs;h=eb854b58badd66a4a16b1fcd9c49b445d06f8c93;hb=17c2dabbf5d353ff67758f20c461468996461a3a;hp=2b8d35d8f485f1ac45ffa6fd203ce33dd6f04073;hpb=c426b04af6b85ef3ec6859929eb906bcea4fcaf7;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 2b8d35d8..eb854b58 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 @@ -41,39 +41,23 @@ 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 node key chosen by the strategy with fixed pool', () => { - const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( - fixedPool + 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 ) - const workerChoiceStrategyStub = createStubInstance( - RoundRobinWorkerChoiceStrategy, - { - hasPoolWorkerNodesReady: stub().returns(true), - choose: stub().returns(0) - } - ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( - WorkerChoiceStrategies.ROUND_ROBIN - ) - workerChoiceStrategyContext.workerChoiceStrategies.set( - workerChoiceStrategyContext.workerChoiceStrategy, - workerChoiceStrategyStub - ) - const chosenWorkerKey = workerChoiceStrategyContext.execute() - expect( - workerChoiceStrategyContext.workerChoiceStrategies.get( - workerChoiceStrategyContext.workerChoiceStrategy - ).choose.calledOnce - ).toBe(true) - expect(chosenWorkerKey).toBe(0) }) it('Verify that execute() throws error if null or undefined is returned after retries', () => { @@ -86,7 +70,6 @@ describe('Worker choice strategy context test suite', () => { const workerChoiceStrategyUndefinedStub = createStubInstance( RoundRobinWorkerChoiceStrategy, { - hasPoolWorkerNodesReady: stub().returns(true), choose: stub().returns(undefined) } ) @@ -96,16 +79,12 @@ describe('Worker choice strategy context test suite', () => { ) 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` + `Worker node key chosen is null or undefined after ${workerChoiceStrategyContext.retries} retries` ) ) const workerChoiceStrategyNullStub = createStubInstance( RoundRobinWorkerChoiceStrategy, { - hasPoolWorkerNodesReady: stub().returns(true), choose: stub().returns(null) } ) @@ -115,34 +94,30 @@ describe('Worker choice strategy context test suite', () => { ) 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` + `Worker node key chosen is null or undefined after ${workerChoiceStrategyContext.retries} retries` ) ) }) - it('Verify that execute() retry until a worker node is ready and chosen', () => { + it('Verify that execute() retry until a worker node is chosen', () => { const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool ) const workerChoiceStrategyStub = createStubInstance( RoundRobinWorkerChoiceStrategy, { - hasPoolWorkerNodesReady: stub() + choose: stub() .onCall(0) - .returns(false) + .returns(undefined) .onCall(1) - .returns(false) + .returns(undefined) .onCall(2) - .returns(false) + .returns(undefined) .onCall(3) - .returns(false) + .returns(undefined) .onCall(4) - .returns(false) - .returns(true), - choose: stub().returns(1) + .returns(undefined) + .returns(1) } ) expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( @@ -153,27 +128,21 @@ describe('Worker choice strategy context test suite', () => { workerChoiceStrategyStub ) const chosenWorkerKey = workerChoiceStrategyContext.execute() - expect( - workerChoiceStrategyContext.workerChoiceStrategies.get( - workerChoiceStrategyContext.workerChoiceStrategy - ).hasPoolWorkerNodesReady.callCount - ).toBe(6) expect( workerChoiceStrategyContext.workerChoiceStrategies.get( workerChoiceStrategyContext.workerChoiceStrategy ).choose.callCount - ).toBe(1) + ).toBe(6) expect(chosenWorkerKey).toBe(1) }) - it('Verify that execute() throws error if worker choice strategy recursion reach the maximum depth', () => { + it('Verify that execute() return the worker node key chosen by the strategy with fixed pool', () => { const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool ) const workerChoiceStrategyStub = createStubInstance( RoundRobinWorkerChoiceStrategy, { - hasPoolWorkerNodesReady: stub().returns(false), choose: stub().returns(0) } ) @@ -184,9 +153,13 @@ describe('Worker choice strategy context test suite', () => { workerChoiceStrategyContext.workerChoiceStrategy, workerChoiceStrategyStub ) - expect(() => workerChoiceStrategyContext.execute()).toThrow( - new RangeError('Maximum call stack size exceeded') - ) + 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 node key chosen by the strategy with dynamic pool', () => { @@ -196,7 +169,6 @@ describe('Worker choice strategy context test suite', () => { const workerChoiceStrategyStub = createStubInstance( RoundRobinWorkerChoiceStrategy, { - hasPoolWorkerNodesReady: stub().returns(true), choose: stub().returns(0) } )