From: Jérôme Benoit Date: Sat, 8 Oct 2022 15:58:57 +0000 (+0200) Subject: Add UTs for WorkerChoiceStrategyContext X-Git-Tag: v2.2.1~6 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=40ad1d27cb93fc21c700ed6ba96711104fd320c6;p=poolifier.git Add UTs for WorkerChoiceStrategyContext Signed-off-by: Jérôme Benoit --- diff --git a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js new file mode 100644 index 00000000..75fcb9f2 --- /dev/null +++ b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js @@ -0,0 +1,115 @@ +const { expect } = require('expect') +const sinon = require('sinon') +const { + FixedThreadPool, + DynamicThreadPool, + WorkerChoiceStrategies +} = require('../../../lib/index') +const { + RoundRobinWorkerChoiceStrategy +} = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy') +const { + LessRecentlyUsedWorkerChoiceStrategy +} = require('../../../lib/pools/selection-strategies/less-recently-used-worker-choice-strategy') +const { + WorkerChoiceStrategyContext +} = require('../../../lib/pools/selection-strategies/worker-choice-strategy-context') +const { + DynamicPoolWorkerChoiceStrategy +} = require('../../../lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy') + +describe('Worker choice strategy context test suite', () => { + let fixedPool, dynamicPool + beforeEach(() => { + fixedPool = sinon.createStubInstance(FixedThreadPool) + dynamicPool = sinon.createStubInstance(DynamicThreadPool) + }) + + afterEach(() => { + sinon.restore() + }) + + it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => { + const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( + fixedPool + ) + const WorkerChoiceStrategyStub = sinon.createStubInstance( + RoundRobinWorkerChoiceStrategy, + { + choose: sinon.stub().returns('worker') + } + ) + workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub + const worker = workerChoiceStrategyContext.execute() + expect( + workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce + ).toBe(true) + expect(worker).toBe('worker') + }) + + it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => { + const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( + dynamicPool + ) + const WorkerChoiceStrategyStub = sinon.createStubInstance( + RoundRobinWorkerChoiceStrategy, + { + choose: sinon.stub().returns('worker') + } + ) + workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub + const worker = workerChoiceStrategyContext.execute() + expect( + workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce + ).toBe(true) + expect(worker).toBe('worker') + }) + + it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => { + const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( + fixedPool + ) + workerChoiceStrategyContext.setWorkerChoiceStrategy( + WorkerChoiceStrategies.ROUND_ROBIN + ) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( + RoundRobinWorkerChoiceStrategy + ) + }) + + it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => { + const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( + dynamicPool + ) + workerChoiceStrategyContext.setWorkerChoiceStrategy( + WorkerChoiceStrategies.ROUND_ROBIN + ) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( + DynamicPoolWorkerChoiceStrategy + ) + }) + + it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => { + const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( + fixedPool + ) + workerChoiceStrategyContext.setWorkerChoiceStrategy( + WorkerChoiceStrategies.LESS_RECENTLY_USED + ) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( + LessRecentlyUsedWorkerChoiceStrategy + ) + }) + + it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => { + const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( + dynamicPool + ) + workerChoiceStrategyContext.setWorkerChoiceStrategy( + WorkerChoiceStrategies.LESS_RECENTLY_USED + ) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( + DynamicPoolWorkerChoiceStrategy + ) + }) +})