85ec426359c3fdbc0e0830404fe02d56886d7d99
[poolifier.git] / tests / pools / selection-strategies / worker-choice-strategy-context.test.mjs
1 import { expect } from 'expect'
2 import { createStubInstance, restore, stub } from 'sinon'
3 import {
4 DynamicThreadPool,
5 FixedThreadPool,
6 WorkerChoiceStrategies
7 } from '../../../lib/index.cjs'
8 import { WorkerChoiceStrategyContext } from '../../../lib/pools/selection-strategies/worker-choice-strategy-context.cjs'
9 import { RoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy.cjs'
10 import { LeastUsedWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/least-used-worker-choice-strategy.cjs'
11 import { LeastBusyWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/least-busy-worker-choice-strategy.cjs'
12 import { LeastEluWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/least-elu-worker-choice-strategy.cjs'
13 import { FairShareWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy.cjs'
14 import { WeightedRoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.cjs'
15 import { InterleavedWeightedRoundRobinWorkerChoiceStrategy } from '../../../lib/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.cjs'
16
17 describe('Worker choice strategy context test suite', () => {
18 const min = 1
19 const max = 3
20 let fixedPool, dynamicPool
21
22 before(() => {
23 fixedPool = new FixedThreadPool(
24 max,
25 './tests/worker-files/thread/testWorker.mjs'
26 )
27 dynamicPool = new DynamicThreadPool(
28 min,
29 max,
30 './tests/worker-files/thread/testWorker.mjs'
31 )
32 })
33
34 afterEach(() => {
35 restore()
36 })
37
38 after(async () => {
39 await fixedPool.destroy()
40 await dynamicPool.destroy()
41 })
42
43 it('Verify that constructor() initializes the context with all the available worker choice strategies', () => {
44 let workerChoiceStrategyContext = new WorkerChoiceStrategyContext(fixedPool)
45 expect(workerChoiceStrategyContext.workerChoiceStrategies.size).toBe(
46 Object.keys(WorkerChoiceStrategies).length
47 )
48 workerChoiceStrategyContext = new WorkerChoiceStrategyContext(dynamicPool)
49 expect(workerChoiceStrategyContext.workerChoiceStrategies.size).toBe(
50 Object.keys(WorkerChoiceStrategies).length
51 )
52 })
53
54 it('Verify that constructor() initializes the context with retries attribute properly set', () => {
55 let workerChoiceStrategyContext = new WorkerChoiceStrategyContext(fixedPool)
56 expect(workerChoiceStrategyContext.retries).toBe(fixedPool.info.maxSize * 2)
57 workerChoiceStrategyContext = new WorkerChoiceStrategyContext(dynamicPool)
58 expect(workerChoiceStrategyContext.retries).toBe(
59 dynamicPool.info.maxSize * 2
60 )
61 })
62
63 it('Verify that execute() return the worker node key chosen by the strategy with fixed pool', () => {
64 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
65 fixedPool
66 )
67 const workerChoiceStrategyStub = createStubInstance(
68 RoundRobinWorkerChoiceStrategy,
69 {
70 hasPoolWorkerNodesReady: stub().returns(true),
71 choose: stub().returns(0)
72 }
73 )
74 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
75 WorkerChoiceStrategies.ROUND_ROBIN
76 )
77 workerChoiceStrategyContext.workerChoiceStrategies.set(
78 workerChoiceStrategyContext.workerChoiceStrategy,
79 workerChoiceStrategyStub
80 )
81 const chosenWorkerKey = workerChoiceStrategyContext.execute()
82 expect(
83 workerChoiceStrategyContext.workerChoiceStrategies.get(
84 workerChoiceStrategyContext.workerChoiceStrategy
85 ).choose.calledOnce
86 ).toBe(true)
87 expect(chosenWorkerKey).toBe(0)
88 })
89
90 it('Verify that execute() throws error if null or undefined is returned after retries', () => {
91 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
92 fixedPool
93 )
94 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
95 WorkerChoiceStrategies.ROUND_ROBIN
96 )
97 const workerChoiceStrategyUndefinedStub = createStubInstance(
98 RoundRobinWorkerChoiceStrategy,
99 {
100 hasPoolWorkerNodesReady: stub().returns(true),
101 choose: stub().returns(undefined)
102 }
103 )
104 workerChoiceStrategyContext.workerChoiceStrategies.set(
105 workerChoiceStrategyContext.workerChoiceStrategy,
106 workerChoiceStrategyUndefinedStub
107 )
108 expect(() => workerChoiceStrategyContext.execute()).toThrow(
109 new Error(
110 `Worker node key chosen is null or undefined after ${workerChoiceStrategyContext.retries} retries`
111 )
112 )
113 const workerChoiceStrategyNullStub = createStubInstance(
114 RoundRobinWorkerChoiceStrategy,
115 {
116 hasPoolWorkerNodesReady: stub().returns(true),
117 choose: stub().returns(null)
118 }
119 )
120 workerChoiceStrategyContext.workerChoiceStrategies.set(
121 workerChoiceStrategyContext.workerChoiceStrategy,
122 workerChoiceStrategyNullStub
123 )
124 expect(() => workerChoiceStrategyContext.execute()).toThrow(
125 new Error(
126 `Worker node key chosen is null or undefined after ${workerChoiceStrategyContext.retries} retries`
127 )
128 )
129 })
130
131 it('Verify that execute() retry until a worker node is ready and chosen', () => {
132 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
133 fixedPool
134 )
135 const workerChoiceStrategyStub = createStubInstance(
136 RoundRobinWorkerChoiceStrategy,
137 {
138 hasPoolWorkerNodesReady: stub()
139 .onCall(0)
140 .returns(false)
141 .onCall(1)
142 .returns(false)
143 .onCall(2)
144 .returns(false)
145 .onCall(3)
146 .returns(false)
147 .onCall(4)
148 .returns(false)
149 .returns(true),
150 choose: stub().returns(1)
151 }
152 )
153 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
154 WorkerChoiceStrategies.ROUND_ROBIN
155 )
156 workerChoiceStrategyContext.workerChoiceStrategies.set(
157 workerChoiceStrategyContext.workerChoiceStrategy,
158 workerChoiceStrategyStub
159 )
160 const chosenWorkerKey = workerChoiceStrategyContext.execute()
161 expect(
162 workerChoiceStrategyContext.workerChoiceStrategies.get(
163 workerChoiceStrategyContext.workerChoiceStrategy
164 ).hasPoolWorkerNodesReady.callCount
165 ).toBe(6)
166 expect(
167 workerChoiceStrategyContext.workerChoiceStrategies.get(
168 workerChoiceStrategyContext.workerChoiceStrategy
169 ).choose.callCount
170 ).toBe(1)
171 expect(chosenWorkerKey).toBe(1)
172 })
173
174 it('Verify that execute() throws error if worker choice strategy recursion reach the maximum depth', () => {
175 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
176 fixedPool
177 )
178 const workerChoiceStrategyStub = createStubInstance(
179 RoundRobinWorkerChoiceStrategy,
180 {
181 hasPoolWorkerNodesReady: stub().returns(false),
182 choose: stub().returns(0)
183 }
184 )
185 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
186 WorkerChoiceStrategies.ROUND_ROBIN
187 )
188 workerChoiceStrategyContext.workerChoiceStrategies.set(
189 workerChoiceStrategyContext.workerChoiceStrategy,
190 workerChoiceStrategyStub
191 )
192 expect(() => workerChoiceStrategyContext.execute()).toThrow(
193 new RangeError('Maximum call stack size exceeded')
194 )
195 })
196
197 it('Verify that execute() return the worker node key chosen by the strategy with dynamic pool', () => {
198 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
199 dynamicPool
200 )
201 const workerChoiceStrategyStub = createStubInstance(
202 RoundRobinWorkerChoiceStrategy,
203 {
204 hasPoolWorkerNodesReady: stub().returns(true),
205 choose: stub().returns(0)
206 }
207 )
208 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
209 WorkerChoiceStrategies.ROUND_ROBIN
210 )
211 workerChoiceStrategyContext.workerChoiceStrategies.set(
212 workerChoiceStrategyContext.workerChoiceStrategy,
213 workerChoiceStrategyStub
214 )
215 const chosenWorkerKey = workerChoiceStrategyContext.execute()
216 expect(
217 workerChoiceStrategyContext.workerChoiceStrategies.get(
218 workerChoiceStrategyContext.workerChoiceStrategy
219 ).choose.calledOnce
220 ).toBe(true)
221 expect(chosenWorkerKey).toBe(0)
222 })
223
224 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
225 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
226 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
227 fixedPool
228 )
229 expect(
230 workerChoiceStrategyContext.workerChoiceStrategies.get(
231 workerChoiceStrategy
232 )
233 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
234 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
235 workerChoiceStrategy
236 )
237 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
238 expect(
239 workerChoiceStrategyContext.workerChoiceStrategies.get(
240 workerChoiceStrategy
241 )
242 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
243 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
244 workerChoiceStrategy
245 )
246 })
247
248 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
249 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
250 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
251 dynamicPool
252 )
253 expect(
254 workerChoiceStrategyContext.workerChoiceStrategies.get(
255 workerChoiceStrategy
256 )
257 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
258 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
259 workerChoiceStrategy
260 )
261 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
262 expect(
263 workerChoiceStrategyContext.workerChoiceStrategies.get(
264 workerChoiceStrategy
265 )
266 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
267 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
268 workerChoiceStrategy
269 )
270 })
271
272 it('Verify that setWorkerChoiceStrategy() works with LEAST_USED and fixed pool', () => {
273 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_USED
274 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
275 fixedPool
276 )
277 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
278 expect(
279 workerChoiceStrategyContext.workerChoiceStrategies.get(
280 workerChoiceStrategy
281 )
282 ).toBeInstanceOf(LeastUsedWorkerChoiceStrategy)
283 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
284 workerChoiceStrategy
285 )
286 })
287
288 it('Verify that setWorkerChoiceStrategy() works with LEAST_USED and dynamic pool', () => {
289 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_USED
290 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
291 dynamicPool
292 )
293 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
294 expect(
295 workerChoiceStrategyContext.workerChoiceStrategies.get(
296 workerChoiceStrategy
297 )
298 ).toBeInstanceOf(LeastUsedWorkerChoiceStrategy)
299 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
300 workerChoiceStrategy
301 )
302 })
303
304 it('Verify that setWorkerChoiceStrategy() works with LEAST_BUSY and fixed pool', () => {
305 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_BUSY
306 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
307 fixedPool
308 )
309 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
310 expect(
311 workerChoiceStrategyContext.workerChoiceStrategies.get(
312 workerChoiceStrategy
313 )
314 ).toBeInstanceOf(LeastBusyWorkerChoiceStrategy)
315 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
316 workerChoiceStrategy
317 )
318 })
319
320 it('Verify that setWorkerChoiceStrategy() works with LEAST_BUSY and dynamic pool', () => {
321 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_BUSY
322 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
323 dynamicPool
324 )
325 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
326 expect(
327 workerChoiceStrategyContext.workerChoiceStrategies.get(
328 workerChoiceStrategy
329 )
330 ).toBeInstanceOf(LeastBusyWorkerChoiceStrategy)
331 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
332 workerChoiceStrategy
333 )
334 })
335
336 it('Verify that setWorkerChoiceStrategy() works with LEAST_ELU and fixed pool', () => {
337 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_ELU
338 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
339 fixedPool
340 )
341 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
342 expect(
343 workerChoiceStrategyContext.workerChoiceStrategies.get(
344 workerChoiceStrategy
345 )
346 ).toBeInstanceOf(LeastEluWorkerChoiceStrategy)
347 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
348 workerChoiceStrategy
349 )
350 })
351
352 it('Verify that setWorkerChoiceStrategy() works with LEAST_ELU and dynamic pool', () => {
353 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_ELU
354 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
355 dynamicPool
356 )
357 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
358 expect(
359 workerChoiceStrategyContext.workerChoiceStrategies.get(
360 workerChoiceStrategy
361 )
362 ).toBeInstanceOf(LeastEluWorkerChoiceStrategy)
363 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
364 workerChoiceStrategy
365 )
366 })
367
368 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
369 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
370 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
371 fixedPool
372 )
373 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
374 expect(
375 workerChoiceStrategyContext.workerChoiceStrategies.get(
376 workerChoiceStrategy
377 )
378 ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
379 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
380 workerChoiceStrategy
381 )
382 })
383
384 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
385 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
386 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
387 dynamicPool
388 )
389 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
390 expect(
391 workerChoiceStrategyContext.workerChoiceStrategies.get(
392 workerChoiceStrategy
393 )
394 ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
395 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
396 workerChoiceStrategy
397 )
398 })
399
400 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
401 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
402 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
403 fixedPool
404 )
405 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
406 expect(
407 workerChoiceStrategyContext.workerChoiceStrategies.get(
408 workerChoiceStrategy
409 )
410 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
411 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
412 workerChoiceStrategy
413 )
414 })
415
416 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
417 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
418 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
419 dynamicPool
420 )
421 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
422 expect(
423 workerChoiceStrategyContext.workerChoiceStrategies.get(
424 workerChoiceStrategy
425 )
426 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
427 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
428 workerChoiceStrategy
429 )
430 })
431
432 it('Verify that setWorkerChoiceStrategy() works with INTERLEAVED_WEIGHTED_ROUND_ROBIN and fixed pool', () => {
433 const workerChoiceStrategy =
434 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
435 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
436 fixedPool
437 )
438 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
439 expect(
440 workerChoiceStrategyContext.workerChoiceStrategies.get(
441 workerChoiceStrategy
442 )
443 ).toBeInstanceOf(InterleavedWeightedRoundRobinWorkerChoiceStrategy)
444 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
445 workerChoiceStrategy
446 )
447 })
448
449 it('Verify that setWorkerChoiceStrategy() works with INTERLEAVED_WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
450 const workerChoiceStrategy =
451 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
452 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
453 dynamicPool
454 )
455 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
456 expect(
457 workerChoiceStrategyContext.workerChoiceStrategies.get(
458 workerChoiceStrategy
459 )
460 ).toBeInstanceOf(InterleavedWeightedRoundRobinWorkerChoiceStrategy)
461 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
462 workerChoiceStrategy
463 )
464 })
465
466 it('Verify that worker choice strategy options enable median runtime pool statistics', () => {
467 const wwrWorkerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
468 let workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
469 fixedPool,
470 wwrWorkerChoiceStrategy,
471 {
472 runTime: { median: true }
473 }
474 )
475 expect(
476 workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime
477 .average
478 ).toBe(false)
479 expect(
480 workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.median
481 ).toBe(true)
482 workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
483 dynamicPool,
484 wwrWorkerChoiceStrategy,
485 {
486 runTime: { median: true }
487 }
488 )
489 expect(
490 workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime
491 .average
492 ).toBe(false)
493 expect(
494 workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.median
495 ).toBe(true)
496 const fsWorkerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
497 workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
498 fixedPool,
499 fsWorkerChoiceStrategy,
500 {
501 runTime: { median: true }
502 }
503 )
504 expect(
505 workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime
506 .average
507 ).toBe(false)
508 expect(
509 workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.median
510 ).toBe(true)
511 workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
512 dynamicPool,
513 fsWorkerChoiceStrategy,
514 {
515 runTime: { median: true }
516 }
517 )
518 expect(
519 workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime
520 .average
521 ).toBe(false)
522 expect(
523 workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.median
524 ).toBe(true)
525 })
526 })