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=cb9249cf1a1433ed24abac37187cca3de6a9d012;hp=bb8c7fefb3a1567919af0736f698c6e6f4a55359;hpb=6ff68506534dde3c585fd6a6abb299744f7002ca;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 bb8c7fef..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 - ) - const workerChoiceStrategyStub = createStubInstance( - RoundRobinWorkerChoiceStrategy, - { - hasPoolWorkerNodesReady: stub().returns(true), - choose: stub().returns(0) - } - ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( - WorkerChoiceStrategies.ROUND_ROBIN + 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 ) - 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) } ) @@ -95,12 +78,13 @@ describe('Worker choice strategy context test suite', () => { workerChoiceStrategyUndefinedStub ) 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 ${workerChoiceStrategyContext.retries} retries` + ) ) const workerChoiceStrategyNullStub = createStubInstance( RoundRobinWorkerChoiceStrategy, { - hasPoolWorkerNodesReady: stub().returns(true), choose: stub().returns(null) } ) @@ -109,36 +93,31 @@ describe('Worker choice strategy context test suite', () => { 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 ${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) - .onCall(6) - .returns(false) - .onCall(7) - .returns(false) - .onCall(8) - .returns(false) - .returns(true), - choose: stub().returns(1) + .returns(undefined) + .returns(1) } ) expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( @@ -149,27 +128,21 @@ describe('Worker choice strategy context test suite', () => { workerChoiceStrategyStub ) const chosenWorkerKey = workerChoiceStrategyContext.execute() - expect( - workerChoiceStrategyContext.workerChoiceStrategies.get( - workerChoiceStrategyContext.workerChoiceStrategy - ).hasPoolWorkerNodesReady.callCount - ).toBe(12) 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 consecutive executions has been reached', () => { + 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) } ) @@ -180,11 +153,13 @@ describe('Worker choice strategy context test suite', () => { workerChoiceStrategyContext.workerChoiceStrategy, workerChoiceStrategyStub ) - expect(() => workerChoiceStrategyContext.execute()).toThrow( - new RangeError( - 'Worker choice strategy consecutive executions has exceeded the maximum of 10000' - ) - ) + 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', () => { @@ -194,7 +169,6 @@ describe('Worker choice strategy context test suite', () => { const workerChoiceStrategyStub = createStubInstance( RoundRobinWorkerChoiceStrategy, { - hasPoolWorkerNodesReady: stub().returns(true), choose: stub().returns(0) } )