| 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 | |
| 27 | describe('Worker choice strategy context test suite', () => { |
| 28 | const min = 1 |
| 29 | const max = 3 |
| 30 | let fixedPool, dynamicPool |
| 31 | |
| 32 | before(() => { |
| 33 | fixedPool = new FixedThreadPool( |
| 34 | max, |
| 35 | './tests/worker-files/thread/testWorker.js' |
| 36 | ) |
| 37 | dynamicPool = new DynamicThreadPool( |
| 38 | min, |
| 39 | max, |
| 40 | './tests/worker-files/thread/testWorker.js' |
| 41 | ) |
| 42 | }) |
| 43 | |
| 44 | afterEach(() => { |
| 45 | sinon.restore() |
| 46 | }) |
| 47 | |
| 48 | after(async () => { |
| 49 | await fixedPool.destroy() |
| 50 | await dynamicPool.destroy() |
| 51 | }) |
| 52 | |
| 53 | it('Verify that constructor() initializes the context with all the available worker choice strategies', () => { |
| 54 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 55 | fixedPool |
| 56 | ) |
| 57 | expect(workerChoiceStrategyContext.workerChoiceStrategies.size).toBe( |
| 58 | Object.keys(WorkerChoiceStrategies).length |
| 59 | ) |
| 60 | }) |
| 61 | |
| 62 | it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => { |
| 63 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 64 | fixedPool |
| 65 | ) |
| 66 | const WorkerChoiceStrategyStub = sinon.createStubInstance( |
| 67 | RoundRobinWorkerChoiceStrategy, |
| 68 | { |
| 69 | choose: sinon.stub().returns(0) |
| 70 | } |
| 71 | ) |
| 72 | expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( |
| 73 | WorkerChoiceStrategies.ROUND_ROBIN |
| 74 | ) |
| 75 | workerChoiceStrategyContext.workerChoiceStrategies.set( |
| 76 | workerChoiceStrategyContext.workerChoiceStrategyType, |
| 77 | WorkerChoiceStrategyStub |
| 78 | ) |
| 79 | const chosenWorkerKey = workerChoiceStrategyContext.execute() |
| 80 | expect( |
| 81 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 82 | workerChoiceStrategyContext.workerChoiceStrategyType |
| 83 | ).choose.calledOnce |
| 84 | ).toBe(true) |
| 85 | expect(chosenWorkerKey).toBe(0) |
| 86 | }) |
| 87 | |
| 88 | it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => { |
| 89 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 90 | dynamicPool |
| 91 | ) |
| 92 | const WorkerChoiceStrategyStub = sinon.createStubInstance( |
| 93 | RoundRobinWorkerChoiceStrategy, |
| 94 | { |
| 95 | choose: sinon.stub().returns(0) |
| 96 | } |
| 97 | ) |
| 98 | expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( |
| 99 | WorkerChoiceStrategies.ROUND_ROBIN |
| 100 | ) |
| 101 | workerChoiceStrategyContext.workerChoiceStrategies.set( |
| 102 | workerChoiceStrategyContext.workerChoiceStrategyType, |
| 103 | WorkerChoiceStrategyStub |
| 104 | ) |
| 105 | const chosenWorkerKey = workerChoiceStrategyContext.execute() |
| 106 | expect( |
| 107 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 108 | workerChoiceStrategyContext.workerChoiceStrategyType |
| 109 | ).choose.calledOnce |
| 110 | ).toBe(true) |
| 111 | expect(chosenWorkerKey).toBe(0) |
| 112 | }) |
| 113 | |
| 114 | it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => { |
| 115 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 116 | fixedPool |
| 117 | ) |
| 118 | expect( |
| 119 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 120 | WorkerChoiceStrategies.ROUND_ROBIN |
| 121 | ).isDynamicPool |
| 122 | ).toBe(false) |
| 123 | expect( |
| 124 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 125 | WorkerChoiceStrategies.ROUND_ROBIN |
| 126 | ) |
| 127 | ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) |
| 128 | expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( |
| 129 | WorkerChoiceStrategies.ROUND_ROBIN |
| 130 | ) |
| 131 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 132 | WorkerChoiceStrategies.ROUND_ROBIN |
| 133 | ) |
| 134 | expect( |
| 135 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 136 | WorkerChoiceStrategies.ROUND_ROBIN |
| 137 | ) |
| 138 | ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) |
| 139 | expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( |
| 140 | WorkerChoiceStrategies.ROUND_ROBIN |
| 141 | ) |
| 142 | }) |
| 143 | |
| 144 | it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => { |
| 145 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 146 | dynamicPool |
| 147 | ) |
| 148 | expect( |
| 149 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 150 | WorkerChoiceStrategies.ROUND_ROBIN |
| 151 | ).isDynamicPool |
| 152 | ).toBe(true) |
| 153 | expect( |
| 154 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 155 | WorkerChoiceStrategies.ROUND_ROBIN |
| 156 | ) |
| 157 | ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) |
| 158 | expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( |
| 159 | WorkerChoiceStrategies.ROUND_ROBIN |
| 160 | ) |
| 161 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 162 | WorkerChoiceStrategies.ROUND_ROBIN |
| 163 | ) |
| 164 | expect( |
| 165 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 166 | WorkerChoiceStrategies.ROUND_ROBIN |
| 167 | ) |
| 168 | ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) |
| 169 | expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( |
| 170 | WorkerChoiceStrategies.ROUND_ROBIN |
| 171 | ) |
| 172 | }) |
| 173 | |
| 174 | it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => { |
| 175 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 176 | fixedPool |
| 177 | ) |
| 178 | expect( |
| 179 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 180 | WorkerChoiceStrategies.LESS_USED |
| 181 | ).isDynamicPool |
| 182 | ).toBe(false) |
| 183 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 184 | WorkerChoiceStrategies.LESS_USED |
| 185 | ) |
| 186 | expect( |
| 187 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 188 | WorkerChoiceStrategies.LESS_USED |
| 189 | ) |
| 190 | ).toBeInstanceOf(LessUsedWorkerChoiceStrategy) |
| 191 | expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( |
| 192 | WorkerChoiceStrategies.LESS_USED |
| 193 | ) |
| 194 | }) |
| 195 | |
| 196 | it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => { |
| 197 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 198 | dynamicPool |
| 199 | ) |
| 200 | expect( |
| 201 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 202 | WorkerChoiceStrategies.LESS_USED |
| 203 | ).isDynamicPool |
| 204 | ).toBe(true) |
| 205 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 206 | WorkerChoiceStrategies.LESS_USED |
| 207 | ) |
| 208 | expect( |
| 209 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 210 | WorkerChoiceStrategies.LESS_USED |
| 211 | ) |
| 212 | ).toBeInstanceOf(LessUsedWorkerChoiceStrategy) |
| 213 | expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( |
| 214 | WorkerChoiceStrategies.LESS_USED |
| 215 | ) |
| 216 | }) |
| 217 | |
| 218 | it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => { |
| 219 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 220 | fixedPool |
| 221 | ) |
| 222 | expect( |
| 223 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 224 | WorkerChoiceStrategies.LESS_BUSY |
| 225 | ).isDynamicPool |
| 226 | ).toBe(false) |
| 227 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 228 | WorkerChoiceStrategies.LESS_BUSY |
| 229 | ) |
| 230 | expect( |
| 231 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 232 | WorkerChoiceStrategies.LESS_BUSY |
| 233 | ) |
| 234 | ).toBeInstanceOf(LessBusyWorkerChoiceStrategy) |
| 235 | expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( |
| 236 | WorkerChoiceStrategies.LESS_BUSY |
| 237 | ) |
| 238 | }) |
| 239 | |
| 240 | it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => { |
| 241 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 242 | dynamicPool |
| 243 | ) |
| 244 | expect( |
| 245 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 246 | WorkerChoiceStrategies.LESS_BUSY |
| 247 | ).isDynamicPool |
| 248 | ).toBe(true) |
| 249 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 250 | WorkerChoiceStrategies.LESS_BUSY |
| 251 | ) |
| 252 | expect( |
| 253 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 254 | WorkerChoiceStrategies.LESS_BUSY |
| 255 | ) |
| 256 | ).toBeInstanceOf(LessBusyWorkerChoiceStrategy) |
| 257 | expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( |
| 258 | WorkerChoiceStrategies.LESS_BUSY |
| 259 | ) |
| 260 | }) |
| 261 | |
| 262 | it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => { |
| 263 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 264 | fixedPool |
| 265 | ) |
| 266 | expect( |
| 267 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 268 | WorkerChoiceStrategies.FAIR_SHARE |
| 269 | ).isDynamicPool |
| 270 | ).toBe(false) |
| 271 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 272 | WorkerChoiceStrategies.FAIR_SHARE |
| 273 | ) |
| 274 | expect( |
| 275 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 276 | WorkerChoiceStrategies.FAIR_SHARE |
| 277 | ) |
| 278 | ).toBeInstanceOf(FairShareWorkerChoiceStrategy) |
| 279 | expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( |
| 280 | WorkerChoiceStrategies.FAIR_SHARE |
| 281 | ) |
| 282 | }) |
| 283 | |
| 284 | it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => { |
| 285 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 286 | dynamicPool |
| 287 | ) |
| 288 | expect( |
| 289 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 290 | WorkerChoiceStrategies.FAIR_SHARE |
| 291 | ).isDynamicPool |
| 292 | ).toBe(true) |
| 293 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 294 | WorkerChoiceStrategies.FAIR_SHARE |
| 295 | ) |
| 296 | expect( |
| 297 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 298 | WorkerChoiceStrategies.FAIR_SHARE |
| 299 | ) |
| 300 | ).toBeInstanceOf(FairShareWorkerChoiceStrategy) |
| 301 | expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( |
| 302 | WorkerChoiceStrategies.FAIR_SHARE |
| 303 | ) |
| 304 | }) |
| 305 | |
| 306 | it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => { |
| 307 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 308 | fixedPool |
| 309 | ) |
| 310 | expect( |
| 311 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 312 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
| 313 | ).isDynamicPool |
| 314 | ).toBe(false) |
| 315 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 316 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
| 317 | ) |
| 318 | expect( |
| 319 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 320 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
| 321 | ) |
| 322 | ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy) |
| 323 | expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( |
| 324 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
| 325 | ) |
| 326 | }) |
| 327 | |
| 328 | it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => { |
| 329 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
| 330 | dynamicPool |
| 331 | ) |
| 332 | expect( |
| 333 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 334 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
| 335 | ).isDynamicPool |
| 336 | ).toBe(true) |
| 337 | workerChoiceStrategyContext.setWorkerChoiceStrategy( |
| 338 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
| 339 | ) |
| 340 | expect( |
| 341 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
| 342 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
| 343 | ) |
| 344 | ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy) |
| 345 | expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( |
| 346 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
| 347 | ) |
| 348 | }) |
| 349 | }) |