From 0220f124adb20bbaebb927962b9346daa95a1d96 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 13 Oct 2022 21:35:50 +0200 Subject: [PATCH] Add tests for WRR worker choice strategy MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- ...round-robin-worker-choice-strategy.test.js | 41 +++++++++++++++++++ .../worker-choice-strategy-context.test.js | 8 ++-- 2 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.js 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', () => { -- 2.34.1