| 1 | const { expect } = require('expect') |
| 2 | const sinon = require('sinon') |
| 3 | const { |
| 4 | FixedThreadPool, |
| 5 | DynamicThreadPool, |
| 6 | WorkerChoiceStrategies |
| 7 | } = require('../../../lib/index') |
| 8 | const { |
| 9 | RoundRobinWorkerChoiceStrategy |
| 10 | } = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy') |
| 11 | const { |
| 12 | LessRecentlyUsedWorkerChoiceStrategy |
| 13 | } = require('../../../lib/pools/selection-strategies/less-recently-used-worker-choice-strategy') |
| 14 | const { |
| 15 | WorkerChoiceStrategyContext |
| 16 | } = require('../../../lib/pools/selection-strategies/worker-choice-strategy-context') |
| 17 | const { |
| 18 | DynamicPoolWorkerChoiceStrategy |
| 19 | } = require('../../../lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy') |
| 20 | |
| 21 | describe('Worker choice strategy context test suite', () => { |
| 22 | let fixedPool, dynamicPool |
| 23 | beforeEach(() => { |
| 24 | fixedPool = sinon.createStubInstance(FixedThreadPool) |
| 25 | dynamicPool = sinon.createStubInstance(DynamicThreadPool) |
| 26 | }) |
| 27 | |
| 28 | afterEach(() => { |
| 29 | sinon.restore() |
| 30 | }) |
| 31 | |
| 32 | it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => { |
| 33 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 34 | fixedPool |
| 35 | ) |
| 36 | const WorkerChoiceStrategyStub = sinon.createStubInstance( |
| 37 | RoundRobinWorkerChoiceStrategy, |
| 38 | { |
| 39 | choose: sinon.stub().returns('worker') |
| 40 | } |
| 41 | ) |
| 42 | workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub |
| 43 | const worker = workerChoiceStrategyContext.execute() |
| 44 | expect( |
| 45 | workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce |
| 46 | ).toBe(true) |
| 47 | expect(worker).toBe('worker') |
| 48 | }) |
| 49 | |
| 50 | it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => { |
| 51 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 52 | dynamicPool |
| 53 | ) |
| 54 | const WorkerChoiceStrategyStub = sinon.createStubInstance( |
| 55 | RoundRobinWorkerChoiceStrategy, |
| 56 | { |
| 57 | choose: sinon.stub().returns('worker') |
| 58 | } |
| 59 | ) |
| 60 | workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub |
| 61 | const worker = workerChoiceStrategyContext.execute() |
| 62 | expect( |
| 63 | workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce |
| 64 | ).toBe(true) |
| 65 | expect(worker).toBe('worker') |
| 66 | }) |
| 67 | |
| 68 | it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => { |
| 69 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 70 | fixedPool |
| 71 | ) |
| 72 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 73 | WorkerChoiceStrategies.ROUND_ROBIN |
| 74 | ) |
| 75 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( |
| 76 | RoundRobinWorkerChoiceStrategy |
| 77 | ) |
| 78 | }) |
| 79 | |
| 80 | it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => { |
| 81 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 82 | dynamicPool |
| 83 | ) |
| 84 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 85 | WorkerChoiceStrategies.ROUND_ROBIN |
| 86 | ) |
| 87 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( |
| 88 | DynamicPoolWorkerChoiceStrategy |
| 89 | ) |
| 90 | }) |
| 91 | |
| 92 | it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => { |
| 93 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 94 | fixedPool |
| 95 | ) |
| 96 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 97 | WorkerChoiceStrategies.LESS_RECENTLY_USED |
| 98 | ) |
| 99 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( |
| 100 | LessRecentlyUsedWorkerChoiceStrategy |
| 101 | ) |
| 102 | }) |
| 103 | |
| 104 | it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => { |
| 105 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 106 | dynamicPool |
| 107 | ) |
| 108 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 109 | WorkerChoiceStrategies.LESS_RECENTLY_USED |
| 110 | ) |
| 111 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( |
| 112 | DynamicPoolWorkerChoiceStrategy |
| 113 | ) |
| 114 | }) |
| 115 | }) |