X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fselection-strategies%2Fworker-choice-strategy-context.test.js;h=df8cd25cb375a4aa50db889c7b014e4c17b22f9c;hb=b9da9d7e74c720f83482a09d1b883fc83d04f4ed;hp=a93104d225f3e9c27ff8d252719619d20654bf0a;hpb=51fe3d3cb0858b5e2ad96076b51a6b9daa8f852c;p=poolifier.git diff --git a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js index a93104d2..df8cd25c 100644 --- a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js +++ b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js @@ -4,7 +4,7 @@ const { FixedThreadPool, DynamicThreadPool, WorkerChoiceStrategies -} = require('../../../lib/index') +} = require('../../../lib') const { WorkerChoiceStrategyContext } = require('../../../lib/pools/selection-strategies/worker-choice-strategy-context') @@ -12,17 +12,23 @@ const { RoundRobinWorkerChoiceStrategy } = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy') const { - LessUsedWorkerChoiceStrategy -} = require('../../../lib/pools/selection-strategies/less-used-worker-choice-strategy') + LeastUsedWorkerChoiceStrategy +} = require('../../../lib/pools/selection-strategies/least-used-worker-choice-strategy') const { - LessBusyWorkerChoiceStrategy -} = require('../../../lib/pools/selection-strategies/less-busy-worker-choice-strategy') + LeastBusyWorkerChoiceStrategy +} = require('../../../lib/pools/selection-strategies/least-busy-worker-choice-strategy') +const { + LeastEluWorkerChoiceStrategy +} = require('../../../lib/pools/selection-strategies/least-elu-worker-choice-strategy') const { FairShareWorkerChoiceStrategy } = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy') const { WeightedRoundRobinWorkerChoiceStrategy } = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy') +const { + InterleavedWeightedRoundRobinWorkerChoiceStrategy +} = require('../../../lib/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy') describe('Worker choice strategy context test suite', () => { const min = 1 @@ -50,27 +56,18 @@ describe('Worker choice strategy context test suite', () => { await dynamicPool.destroy() }) - it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => { + it('Verify that constructor() initializes the context with all the available worker choice strategies', () => { const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool ) - const WorkerChoiceStrategyStub = sinon.createStubInstance( - RoundRobinWorkerChoiceStrategy, - { - choose: sinon.stub().returns(0) - } + expect(workerChoiceStrategyContext.workerChoiceStrategies.size).toBe( + Object.keys(WorkerChoiceStrategies).length ) - workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub - const chosenWorkerKey = workerChoiceStrategyContext.execute() - expect( - 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 chosen by the strategy with fixed pool', () => { const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( - dynamicPool + fixedPool ) const WorkerChoiceStrategyStub = sinon.createStubInstance( RoundRobinWorkerChoiceStrategy, @@ -78,261 +75,383 @@ describe('Worker choice strategy context test suite', () => { choose: sinon.stub().returns(0) } ) - workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + WorkerChoiceStrategies.ROUND_ROBIN + ) + workerChoiceStrategyContext.workerChoiceStrategies.set( + workerChoiceStrategyContext.workerChoiceStrategy, + WorkerChoiceStrategyStub + ) const chosenWorkerKey = workerChoiceStrategyContext.execute() expect( - workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategyContext.workerChoiceStrategy + ).choose.calledOnce ).toBe(true) expect(chosenWorkerKey).toBe(0) }) - it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => { + it('Verify that execute() throws error if null or undefined is returned after retries', () => { const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( - RoundRobinWorkerChoiceStrategy + const WorkerChoiceStrategyUndefinedStub = sinon.createStubInstance( + RoundRobinWorkerChoiceStrategy, + { + choose: sinon.stub().returns(undefined) + } ) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( - WorkerChoiceStrategies.ROUND_ROBIN + const WorkerChoiceStrategyNullStub = sinon.createStubInstance( + RoundRobinWorkerChoiceStrategy, + { + choose: sinon.stub().returns(null) + } ) - workerChoiceStrategyContext.setWorkerChoiceStrategy( - fixedPool, + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.ROUND_ROBIN ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( - RoundRobinWorkerChoiceStrategy + workerChoiceStrategyContext.workerChoiceStrategies.set( + workerChoiceStrategyContext.workerChoiceStrategy, + WorkerChoiceStrategyUndefinedStub ) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( - WorkerChoiceStrategies.ROUND_ROBIN + 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') ) }) - it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => { + it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => { const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( dynamicPool ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( - RoundRobinWorkerChoiceStrategy - ) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( - WorkerChoiceStrategies.ROUND_ROBIN + const WorkerChoiceStrategyStub = sinon.createStubInstance( + RoundRobinWorkerChoiceStrategy, + { + choose: sinon.stub().returns(0) + } ) - workerChoiceStrategyContext.setWorkerChoiceStrategy( - dynamicPool, + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.ROUND_ROBIN ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( - RoundRobinWorkerChoiceStrategy - ) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).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 setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => { + it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => { + const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool ) - workerChoiceStrategyContext.setWorkerChoiceStrategy( - fixedPool, - WorkerChoiceStrategies.LESS_USED - ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( - LessUsedWorkerChoiceStrategy + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ) + ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy ) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( - WorkerChoiceStrategies.LESS_USED + workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ) + ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy ) }) - it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => { + it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => { + const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( dynamicPool ) - workerChoiceStrategyContext.setWorkerChoiceStrategy( - dynamicPool, - WorkerChoiceStrategies.LESS_USED - ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( - LessUsedWorkerChoiceStrategy + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ) + ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy ) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( - WorkerChoiceStrategies.LESS_USED + workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ) + ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy ) }) - it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => { + it('Verify that setWorkerChoiceStrategy() works with LEAST_USED and fixed pool', () => { + const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_USED const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool ) - workerChoiceStrategyContext.setWorkerChoiceStrategy( - fixedPool, - WorkerChoiceStrategies.LESS_BUSY - ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( - LessBusyWorkerChoiceStrategy - ) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( - WorkerChoiceStrategies.LESS_BUSY + workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ) + ).toBeInstanceOf(LeastUsedWorkerChoiceStrategy) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy ) }) - it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => { + it('Verify that setWorkerChoiceStrategy() works with LEAST_USED and dynamic pool', () => { + const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_USED const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( dynamicPool ) - workerChoiceStrategyContext.setWorkerChoiceStrategy( - dynamicPool, - WorkerChoiceStrategies.LESS_BUSY - ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( - LessBusyWorkerChoiceStrategy - ) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( - WorkerChoiceStrategies.LESS_BUSY + workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ) + ).toBeInstanceOf(LeastUsedWorkerChoiceStrategy) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy ) }) - it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => { + it('Verify that setWorkerChoiceStrategy() works with LEAST_BUSY and fixed pool', () => { + const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_BUSY const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool ) - workerChoiceStrategyContext.setWorkerChoiceStrategy( - fixedPool, - WorkerChoiceStrategies.FAIR_SHARE - ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( - FairShareWorkerChoiceStrategy - ) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( - WorkerChoiceStrategies.FAIR_SHARE + workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ) + ).toBeInstanceOf(LeastBusyWorkerChoiceStrategy) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy ) }) - it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => { + it('Verify that setWorkerChoiceStrategy() works with LEAST_BUSY and dynamic pool', () => { + const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_BUSY const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( dynamicPool ) - workerChoiceStrategyContext.setWorkerChoiceStrategy( - dynamicPool, - WorkerChoiceStrategies.FAIR_SHARE - ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( - FairShareWorkerChoiceStrategy - ) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( - WorkerChoiceStrategies.FAIR_SHARE + workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ) + ).toBeInstanceOf(LeastBusyWorkerChoiceStrategy) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy ) }) - it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => { + it('Verify that setWorkerChoiceStrategy() works with LEAST_ELU and fixed pool', () => { + const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_ELU const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool ) - workerChoiceStrategyContext.setWorkerChoiceStrategy( - fixedPool, - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN - ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( - WeightedRoundRobinWorkerChoiceStrategy - ) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ) + ).toBeInstanceOf(LeastEluWorkerChoiceStrategy) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy ) }) - it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => { + it('Verify that setWorkerChoiceStrategy() works with LEAST_ELU and dynamic pool', () => { + const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_ELU const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( dynamicPool ) - workerChoiceStrategyContext.setWorkerChoiceStrategy( - dynamicPool, - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN - ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( - WeightedRoundRobinWorkerChoiceStrategy - ) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ) + ).toBeInstanceOf(LeastEluWorkerChoiceStrategy) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy ) }) - it('Verify that getWorkerChoiceStrategy() default return ROUND_ROBIN strategy', () => { + it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => { + const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool ) - const strategy = - workerChoiceStrategyContext.getWorkerChoiceStrategy(fixedPool) - expect(strategy).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) + workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ) + ).toBeInstanceOf(FairShareWorkerChoiceStrategy) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy + ) }) - it('Verify that getWorkerChoiceStrategy() can return ROUND_ROBIN strategy', () => { + it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => { + const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( - fixedPool + dynamicPool ) - const strategy = workerChoiceStrategyContext.getWorkerChoiceStrategy( - fixedPool, - WorkerChoiceStrategies.ROUND_ROBIN + workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ) + ).toBeInstanceOf(FairShareWorkerChoiceStrategy) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy ) - expect(strategy).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) }) - it('Verify that getWorkerChoiceStrategy() can return LESS_USED strategy', () => { + it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => { + const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool ) - const strategy = workerChoiceStrategyContext.getWorkerChoiceStrategy( - fixedPool, - WorkerChoiceStrategies.LESS_USED + workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ) + ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy ) - expect(strategy).toBeInstanceOf(LessUsedWorkerChoiceStrategy) }) - it('Verify that getWorkerChoiceStrategy() can return LESS_BUSY strategy', () => { + it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => { + const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( - fixedPool + dynamicPool ) - const strategy = workerChoiceStrategyContext.getWorkerChoiceStrategy( - fixedPool, - WorkerChoiceStrategies.LESS_BUSY + workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ) + ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy ) - expect(strategy).toBeInstanceOf(LessBusyWorkerChoiceStrategy) }) - it('Verify that getWorkerChoiceStrategy() can return FAIR_SHARE strategy', () => { + it('Verify that setWorkerChoiceStrategy() works with INTERLEAVED_WEIGHTED_ROUND_ROBIN and fixed pool', () => { + const workerChoiceStrategy = + WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool ) - const strategy = workerChoiceStrategyContext.getWorkerChoiceStrategy( - fixedPool, - WorkerChoiceStrategies.FAIR_SHARE + workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ) + ).toBeInstanceOf(InterleavedWeightedRoundRobinWorkerChoiceStrategy) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy ) - expect(strategy).toBeInstanceOf(FairShareWorkerChoiceStrategy) }) - it('Verify that getWorkerChoiceStrategy() can return WEIGHTED_ROUND_ROBIN strategy', () => { + it('Verify that setWorkerChoiceStrategy() works with INTERLEAVED_WEIGHTED_ROUND_ROBIN and dynamic pool', () => { + const workerChoiceStrategy = + WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( - fixedPool + dynamicPool ) - const strategy = workerChoiceStrategyContext.getWorkerChoiceStrategy( - fixedPool, - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ) + ).toBeInstanceOf(InterleavedWeightedRoundRobinWorkerChoiceStrategy) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy ) - expect(strategy).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy) }) - it('Verify that getWorkerChoiceStrategy() throw error on unknown strategy', () => { - const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( - fixedPool + it('Verify that worker choice strategy options enable median runtime pool statistics', () => { + const wwrWorkerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + let workerChoiceStrategyContext = new WorkerChoiceStrategyContext( + fixedPool, + wwrWorkerChoiceStrategy, + { + runTime: { median: true } + } ) - expect(() => { - workerChoiceStrategyContext.getWorkerChoiceStrategy( - fixedPool, - 'UNKNOWN_STRATEGY' - ) - }).toThrowError( - new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found") + expect( + workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime + .average + ).toBe(false) + expect( + workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.median + ).toBe(true) + workerChoiceStrategyContext = new WorkerChoiceStrategyContext( + dynamicPool, + wwrWorkerChoiceStrategy, + { + runTime: { median: true } + } + ) + expect( + workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime + .average + ).toBe(false) + expect( + workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.median + ).toBe(true) + const fsWorkerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE + workerChoiceStrategyContext = new WorkerChoiceStrategyContext( + fixedPool, + fsWorkerChoiceStrategy, + { + runTime: { median: true } + } ) + expect( + workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime + .average + ).toBe(false) + expect( + workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.median + ).toBe(true) + workerChoiceStrategyContext = new WorkerChoiceStrategyContext( + dynamicPool, + fsWorkerChoiceStrategy, + { + runTime: { median: true } + } + ) + expect( + workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime + .average + ).toBe(false) + expect( + workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.median + ).toBe(true) }) })