From 40ad1d27cb93fc21c700ed6ba96711104fd320c6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 8 Oct 2022 17:58:57 +0200 Subject: [PATCH] Add UTs for WorkerChoiceStrategyContext MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../worker-choice-strategy-context.test.js | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 tests/pools/selection-strategies/worker-choice-strategy-context.test.js 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 + ) + }) +}) -- 2.34.1