From: Jérôme Benoit Date: Thu, 13 Oct 2022 19:35:50 +0000 (+0200) Subject: Add tests for WRR worker choice strategy X-Git-Tag: v2.3.1~7 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=0220f124adb20bbaebb927962b9346daa95a1d96;p=poolifier.git Add tests for WRR worker choice strategy Signed-off-by: Jérôme Benoit --- diff --git a/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.js b/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.js new file mode 100644 index 00000000..664a447a --- /dev/null +++ b/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.js @@ -0,0 +1,41 @@ +const { expect } = require('expect') +const sinon = require('sinon') +const { FixedThreadPool } = require('../../../lib/index') +const { + WeightedRoundRobinWorkerChoiceStrategy +} = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy') + +describe('Weighted round robin strategy worker choice strategy test suite', () => { + // const min = 1 + const max = 3 + let pool + + before(() => { + pool = new FixedThreadPool(max, './tests/worker-files/thread/testWorker.js') + }) + + afterEach(() => { + sinon.restore() + }) + + after(async () => { + await pool.destroy() + }) + + it.only('Verify that reset() resets internals', () => { + const strategy = new WeightedRoundRobinWorkerChoiceStrategy(pool) + const workersTaskRunTimeClearStub = sinon + .stub(strategy.workersTaskRunTime, 'clear') + .returns() + const initWorkersTaskRunTimeStub = sinon + .stub(strategy, 'initWorkersTaskRunTime') + .returns() + const resetResult = strategy.reset() + expect(resetResult).toBe(true) + expect(strategy.previousWorkerIndex).toBe(0) + expect(strategy.currentWorkerIndex).toBe(0) + expect(strategy.defaultWorkerWeight).toBeGreaterThan(0) + expect(workersTaskRunTimeClearStub.calledOnce).toBe(true) + expect(initWorkersTaskRunTimeStub.calledOnce).toBe(true) + }) +}) 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 c30bda73..8a3a7596 100644 --- a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js +++ b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js @@ -61,11 +61,11 @@ describe('Worker choice strategy context test suite', () => { } ) workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub - const worker = workerChoiceStrategyContext.execute() + const chosenWorker = workerChoiceStrategyContext.execute() expect( workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce ).toBe(true) - expect(worker).toBe('worker') + expect(chosenWorker).toBe('worker') }) it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => { @@ -79,11 +79,11 @@ describe('Worker choice strategy context test suite', () => { } ) workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub - const worker = workerChoiceStrategyContext.execute() + const chosenWorker = workerChoiceStrategyContext.execute() expect( workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce ).toBe(true) - expect(worker).toBe('worker') + expect(chosenWorker).toBe('worker') }) it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {