X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fselection-strategies%2Fworker-choice-strategy-context.test.js;h=3ce8b9fe040218f9b8e3be0b7a184b87737af0a6;hb=3032893add6cc97da7b0600e21df2865ad1f675c;hp=75fcb9f25741e52d78f5b1ebcb11f65112233522;hpb=40ad1d27cb93fc21c700ed6ba96711104fd320c6;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 75fcb9f2..3ce8b9fe 100644 --- a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js +++ b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js @@ -5,6 +5,9 @@ const { DynamicThreadPool, WorkerChoiceStrategies } = require('../../../lib/index') +const { + WorkerChoiceStrategyContext +} = require('../../../lib/pools/selection-strategies/worker-choice-strategy-context') const { RoundRobinWorkerChoiceStrategy } = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy') @@ -12,23 +15,41 @@ const { LessRecentlyUsedWorkerChoiceStrategy } = require('../../../lib/pools/selection-strategies/less-recently-used-worker-choice-strategy') const { - WorkerChoiceStrategyContext -} = require('../../../lib/pools/selection-strategies/worker-choice-strategy-context') + 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 { DynamicPoolWorkerChoiceStrategy } = require('../../../lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy') describe('Worker choice strategy context test suite', () => { + const min = 1 + const max = 3 let fixedPool, dynamicPool - beforeEach(() => { - fixedPool = sinon.createStubInstance(FixedThreadPool) - dynamicPool = sinon.createStubInstance(DynamicThreadPool) + + before(() => { + fixedPool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js' + ) + dynamicPool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js' + ) }) afterEach(() => { sinon.restore() }) + after(async () => { + await fixedPool.destroy() + await dynamicPool.destroy() + }) + it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => { const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool @@ -40,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 + workerChoiceStrategyContext.getWorkerChoiceStrategy().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', () => { @@ -58,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 + workerChoiceStrategyContext.getWorkerChoiceStrategy().choose.calledOnce ).toBe(true) - expect(worker).toBe('worker') + expect(chosenWorker).toBe('worker') }) it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => { @@ -72,21 +93,24 @@ describe('Worker choice strategy context test suite', () => { workerChoiceStrategyContext.setWorkerChoiceStrategy( WorkerChoiceStrategies.ROUND_ROBIN ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( - RoundRobinWorkerChoiceStrategy - ) + expect( + workerChoiceStrategyContext.getWorkerChoiceStrategy() + ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) }) - it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => { + it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => { const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( dynamicPool ) workerChoiceStrategyContext.setWorkerChoiceStrategy( WorkerChoiceStrategies.ROUND_ROBIN ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( - DynamicPoolWorkerChoiceStrategy - ) + expect( + workerChoiceStrategyContext.getWorkerChoiceStrategy() + ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy) + expect( + workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy + ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) }) it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => { @@ -96,20 +120,77 @@ describe('Worker choice strategy context test suite', () => { workerChoiceStrategyContext.setWorkerChoiceStrategy( WorkerChoiceStrategies.LESS_RECENTLY_USED ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( - LessRecentlyUsedWorkerChoiceStrategy - ) + expect( + workerChoiceStrategyContext.getWorkerChoiceStrategy() + ).toBeInstanceOf(LessRecentlyUsedWorkerChoiceStrategy) }) - it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => { + it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and dynamic pool', () => { const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( dynamicPool ) workerChoiceStrategyContext.setWorkerChoiceStrategy( WorkerChoiceStrategies.LESS_RECENTLY_USED ) - expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( - DynamicPoolWorkerChoiceStrategy + expect( + workerChoiceStrategyContext.getWorkerChoiceStrategy() + ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy) + expect( + workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy + ).toBeInstanceOf(LessRecentlyUsedWorkerChoiceStrategy) + }) + + it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => { + const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( + fixedPool + ) + workerChoiceStrategyContext.setWorkerChoiceStrategy( + WorkerChoiceStrategies.FAIR_SHARE ) + expect( + workerChoiceStrategyContext.getWorkerChoiceStrategy() + ).toBeInstanceOf(FairShareWorkerChoiceStrategy) + }) + + it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => { + const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( + dynamicPool + ) + workerChoiceStrategyContext.setWorkerChoiceStrategy( + WorkerChoiceStrategies.FAIR_SHARE + ) + expect( + workerChoiceStrategyContext.getWorkerChoiceStrategy() + ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy) + expect( + workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy + ).toBeInstanceOf(FairShareWorkerChoiceStrategy) + }) + + it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => { + const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( + fixedPool + ) + workerChoiceStrategyContext.setWorkerChoiceStrategy( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ) + expect( + workerChoiceStrategyContext.getWorkerChoiceStrategy() + ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy) + }) + + it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => { + const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( + dynamicPool + ) + workerChoiceStrategyContext.setWorkerChoiceStrategy( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ) + expect( + workerChoiceStrategyContext.getWorkerChoiceStrategy() + ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy) + expect( + workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy + ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy) }) })