| 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 | WorkerChoiceStrategyContext |
| 10 | } = require('../../../lib/pools/selection-strategies/worker-choice-strategy-context') |
| 11 | const { |
| 12 | RoundRobinWorkerChoiceStrategy |
| 13 | } = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy') |
| 14 | const { |
| 15 | LessUsedWorkerChoiceStrategy |
| 16 | } = require('../../../lib/pools/selection-strategies/less-used-worker-choice-strategy') |
| 17 | const { |
| 18 | LessBusyWorkerChoiceStrategy |
| 19 | } = require('../../../lib/pools/selection-strategies/less-busy-worker-choice-strategy') |
| 20 | const { |
| 21 | FairShareWorkerChoiceStrategy |
| 22 | } = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy') |
| 23 | const { |
| 24 | WeightedRoundRobinWorkerChoiceStrategy |
| 25 | } = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy') |
| 26 | const { |
| 27 | DynamicPoolWorkerChoiceStrategy |
| 28 | } = require('../../../lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy') |
| 29 | |
| 30 | describe('Worker choice strategy context test suite', () => { |
| 31 | const min = 1 |
| 32 | const max = 3 |
| 33 | let fixedPool, dynamicPool |
| 34 | |
| 35 | before(() => { |
| 36 | fixedPool = new FixedThreadPool( |
| 37 | max, |
| 38 | './tests/worker-files/thread/testWorker.js' |
| 39 | ) |
| 40 | dynamicPool = new DynamicThreadPool( |
| 41 | min, |
| 42 | max, |
| 43 | './tests/worker-files/thread/testWorker.js' |
| 44 | ) |
| 45 | }) |
| 46 | |
| 47 | afterEach(() => { |
| 48 | sinon.restore() |
| 49 | }) |
| 50 | |
| 51 | after(async () => { |
| 52 | await fixedPool.destroy() |
| 53 | await dynamicPool.destroy() |
| 54 | }) |
| 55 | |
| 56 | it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => { |
| 57 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 58 | fixedPool |
| 59 | ) |
| 60 | const WorkerChoiceStrategyStub = sinon.createStubInstance( |
| 61 | RoundRobinWorkerChoiceStrategy, |
| 62 | { |
| 63 | choose: sinon.stub().returns(0) |
| 64 | } |
| 65 | ) |
| 66 | workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub |
| 67 | const chosenWorkerKey = workerChoiceStrategyContext.execute() |
| 68 | expect( |
| 69 | workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce |
| 70 | ).toBe(true) |
| 71 | expect(chosenWorkerKey).toBe(0) |
| 72 | }) |
| 73 | |
| 74 | it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => { |
| 75 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 76 | dynamicPool |
| 77 | ) |
| 78 | const WorkerChoiceStrategyStub = sinon.createStubInstance( |
| 79 | RoundRobinWorkerChoiceStrategy, |
| 80 | { |
| 81 | choose: sinon.stub().returns(0) |
| 82 | } |
| 83 | ) |
| 84 | workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub |
| 85 | const chosenWorkerKey = workerChoiceStrategyContext.execute() |
| 86 | expect( |
| 87 | workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce |
| 88 | ).toBe(true) |
| 89 | expect(chosenWorkerKey).toBe(0) |
| 90 | }) |
| 91 | |
| 92 | it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => { |
| 93 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 94 | fixedPool |
| 95 | ) |
| 96 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 97 | WorkerChoiceStrategies.ROUND_ROBIN |
| 98 | ) |
| 99 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( |
| 100 | RoundRobinWorkerChoiceStrategy |
| 101 | ) |
| 102 | }) |
| 103 | |
| 104 | it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => { |
| 105 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 106 | dynamicPool |
| 107 | ) |
| 108 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 109 | WorkerChoiceStrategies.ROUND_ROBIN |
| 110 | ) |
| 111 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( |
| 112 | DynamicPoolWorkerChoiceStrategy |
| 113 | ) |
| 114 | expect( |
| 115 | workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy |
| 116 | ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) |
| 117 | }) |
| 118 | |
| 119 | it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => { |
| 120 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 121 | fixedPool |
| 122 | ) |
| 123 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 124 | WorkerChoiceStrategies.LESS_USED |
| 125 | ) |
| 126 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( |
| 127 | LessUsedWorkerChoiceStrategy |
| 128 | ) |
| 129 | }) |
| 130 | |
| 131 | it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => { |
| 132 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 133 | dynamicPool |
| 134 | ) |
| 135 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 136 | WorkerChoiceStrategies.LESS_USED |
| 137 | ) |
| 138 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( |
| 139 | DynamicPoolWorkerChoiceStrategy |
| 140 | ) |
| 141 | expect( |
| 142 | workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy |
| 143 | ).toBeInstanceOf(LessUsedWorkerChoiceStrategy) |
| 144 | }) |
| 145 | |
| 146 | it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => { |
| 147 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 148 | fixedPool |
| 149 | ) |
| 150 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 151 | WorkerChoiceStrategies.LESS_BUSY |
| 152 | ) |
| 153 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( |
| 154 | LessBusyWorkerChoiceStrategy |
| 155 | ) |
| 156 | }) |
| 157 | |
| 158 | it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => { |
| 159 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 160 | dynamicPool |
| 161 | ) |
| 162 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 163 | WorkerChoiceStrategies.LESS_BUSY |
| 164 | ) |
| 165 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( |
| 166 | DynamicPoolWorkerChoiceStrategy |
| 167 | ) |
| 168 | expect( |
| 169 | workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy |
| 170 | ).toBeInstanceOf(LessBusyWorkerChoiceStrategy) |
| 171 | }) |
| 172 | |
| 173 | it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => { |
| 174 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 175 | fixedPool |
| 176 | ) |
| 177 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 178 | WorkerChoiceStrategies.FAIR_SHARE |
| 179 | ) |
| 180 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( |
| 181 | FairShareWorkerChoiceStrategy |
| 182 | ) |
| 183 | }) |
| 184 | |
| 185 | it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => { |
| 186 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 187 | dynamicPool |
| 188 | ) |
| 189 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 190 | WorkerChoiceStrategies.FAIR_SHARE |
| 191 | ) |
| 192 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( |
| 193 | DynamicPoolWorkerChoiceStrategy |
| 194 | ) |
| 195 | expect( |
| 196 | workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy |
| 197 | ).toBeInstanceOf(FairShareWorkerChoiceStrategy) |
| 198 | }) |
| 199 | |
| 200 | it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => { |
| 201 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 202 | fixedPool |
| 203 | ) |
| 204 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 205 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
| 206 | ) |
| 207 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( |
| 208 | WeightedRoundRobinWorkerChoiceStrategy |
| 209 | ) |
| 210 | }) |
| 211 | |
| 212 | it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => { |
| 213 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 214 | dynamicPool |
| 215 | ) |
| 216 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 217 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
| 218 | ) |
| 219 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( |
| 220 | DynamicPoolWorkerChoiceStrategy |
| 221 | ) |
| 222 | expect( |
| 223 | workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy |
| 224 | ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy) |
| 225 | }) |
| 226 | }) |