3292ce5686a5f6a014250ec3373510940d196cf3
[poolifier.git] / tests / pools / selection-strategies / selection-strategies.test.js
1 const { expect } = require('expect')
2 const {
3 WorkerChoiceStrategies,
4 DynamicThreadPool,
5 FixedThreadPool,
6 FixedClusterPool
7 } = require('../../../lib/index')
8
9 describe('Selection strategies test suite', () => {
10 const min = 0
11 const max = 3
12
13 it('Verify that WorkerChoiceStrategies enumeration provides string values', () => {
14 expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN')
15 expect(WorkerChoiceStrategies.LESS_USED).toBe('LESS_USED')
16 expect(WorkerChoiceStrategies.LESS_BUSY).toBe('LESS_BUSY')
17 expect(WorkerChoiceStrategies.FAIR_SHARE).toBe('FAIR_SHARE')
18 expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe(
19 'WEIGHTED_ROUND_ROBIN'
20 )
21 })
22
23 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
24 const pool = new DynamicThreadPool(
25 min,
26 max,
27 './tests/worker-files/thread/testWorker.js'
28 )
29 expect(pool.opts.workerChoiceStrategy).toBe(
30 WorkerChoiceStrategies.ROUND_ROBIN
31 )
32 // We need to clean up the resources after our test
33 await pool.destroy()
34 })
35
36 it('Verify available strategies are taken at pool creation', async () => {
37 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
38 const pool = new FixedThreadPool(
39 max,
40 './tests/worker-files/thread/testWorker.js',
41 { workerChoiceStrategy }
42 )
43 expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy)
44 expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe(
45 workerChoiceStrategy
46 )
47 await pool.destroy()
48 }
49 })
50
51 it('Verify available strategies can be set after pool creation', async () => {
52 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
53 const pool = new DynamicThreadPool(
54 min,
55 max,
56 './tests/worker-files/thread/testWorker.js',
57 { workerChoiceStrategy }
58 )
59 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
60 expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy)
61 expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe(
62 workerChoiceStrategy
63 )
64 await pool.destroy()
65 }
66 })
67
68 it('Verify available strategies default internals at pool creation', async () => {
69 const pool = new FixedThreadPool(
70 max,
71 './tests/worker-files/thread/testWorker.js'
72 )
73 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
74 if (workerChoiceStrategy === WorkerChoiceStrategies.ROUND_ROBIN) {
75 expect(
76 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
77 workerChoiceStrategy
78 ).nextWorkerNodeId
79 ).toBe(0)
80 } else if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
81 for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
82 .get(workerChoiceStrategy)
83 .workerLastVirtualTaskTimestamp.keys()) {
84 expect(
85 pool.workerChoiceStrategyContext.workerChoiceStrategies
86 .get(workerChoiceStrategy)
87 .workerLastVirtualTaskTimestamp.get(workerNodeKey).start
88 ).toBe(0)
89 expect(
90 pool.workerChoiceStrategyContext.workerChoiceStrategies
91 .get(workerChoiceStrategy)
92 .workerLastVirtualTaskTimestamp.get(workerNodeKey).end
93 ).toBe(0)
94 }
95 } else if (
96 workerChoiceStrategy === WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
97 ) {
98 expect(
99 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
100 workerChoiceStrategy
101 ).currentWorkerNodeId
102 ).toBe(0)
103 expect(
104 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
105 workerChoiceStrategy
106 ).defaultWorkerWeight
107 ).toBeGreaterThan(0)
108 for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
109 .get(workerChoiceStrategy)
110 .workersTaskRunTime.keys()) {
111 expect(
112 pool.workerChoiceStrategyContext.workerChoiceStrategies
113 .get(workerChoiceStrategy)
114 .workersTaskRunTime.get(workerNodeKey).weight
115 ).toBeGreaterThan(0)
116 expect(
117 pool.workerChoiceStrategyContext.workerChoiceStrategies
118 .get(workerChoiceStrategy)
119 .workersTaskRunTime.get(workerNodeKey).runTime
120 ).toBe(0)
121 }
122 }
123 }
124 await pool.destroy()
125 })
126
127 it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
128 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
129 let pool = new FixedThreadPool(
130 max,
131 './tests/worker-files/thread/testWorker.js',
132 { workerChoiceStrategy }
133 )
134 expect(
135 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
136 ).toBe(false)
137 expect(
138 pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
139 ).toBe(false)
140 expect(
141 pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
142 ).toBe(false)
143 await pool.destroy()
144 pool = new DynamicThreadPool(
145 min,
146 max,
147 './tests/worker-files/thread/testWorker.js',
148 { workerChoiceStrategy }
149 )
150 expect(
151 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
152 ).toBe(false)
153 expect(
154 pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
155 ).toBe(false)
156 expect(
157 pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
158 ).toBe(false)
159 // We need to clean up the resources after our test
160 await pool.destroy()
161 })
162
163 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
164 const pool = new FixedThreadPool(
165 max,
166 './tests/worker-files/thread/testWorker.js',
167 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
168 )
169 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
170 const promises = []
171 for (let i = 0; i < max * 2; i++) {
172 promises.push(pool.execute())
173 }
174 await Promise.all(promises)
175 // We need to clean up the resources after our test
176 await pool.destroy()
177 })
178
179 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
180 const pool = new DynamicThreadPool(
181 min,
182 max,
183 './tests/worker-files/thread/testWorker.js',
184 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
185 )
186 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
187 const promises = []
188 for (let i = 0; i < max * 2; i++) {
189 promises.push(pool.execute())
190 }
191 await Promise.all(promises)
192 // We need to clean up the resources after our test
193 await pool.destroy()
194 })
195
196 it('Verify ROUND_ROBIN strategy runtime behavior', async () => {
197 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
198 let pool = new FixedClusterPool(
199 max,
200 './tests/worker-files/cluster/testWorker.js',
201 { workerChoiceStrategy }
202 )
203 let results = new Set()
204 for (let i = 0; i < max; i++) {
205 results.add(pool.chooseWorkerNode()[1].worker.id)
206 }
207 expect(results.size).toBe(max)
208 await pool.destroy()
209 pool = new FixedThreadPool(
210 max,
211 './tests/worker-files/thread/testWorker.js',
212 { workerChoiceStrategy }
213 )
214 results = new Set()
215 for (let i = 0; i < max; i++) {
216 results.add(pool.chooseWorkerNode()[1].worker.threadId)
217 }
218 expect(results.size).toBe(max)
219 await pool.destroy()
220 })
221
222 it('Verify ROUND_ROBIN strategy internals are resets after setting it', async () => {
223 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
224 let pool = new FixedThreadPool(
225 max,
226 './tests/worker-files/thread/testWorker.js',
227 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
228 )
229 expect(
230 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
231 workerChoiceStrategy
232 ).nextWorkerNodeId
233 ).toBeDefined()
234 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
235 expect(
236 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
237 pool.workerChoiceStrategyContext.workerChoiceStrategy
238 ).nextWorkerNodeId
239 ).toBe(0)
240 await pool.destroy()
241 pool = new DynamicThreadPool(
242 min,
243 max,
244 './tests/worker-files/thread/testWorker.js',
245 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
246 )
247 expect(
248 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
249 workerChoiceStrategy
250 ).nextWorkerNodeId
251 ).toBeDefined()
252 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
253 expect(
254 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
255 pool.workerChoiceStrategyContext.workerChoiceStrategy
256 ).nextWorkerNodeId
257 ).toBe(0)
258 // We need to clean up the resources after our test
259 await pool.destroy()
260 })
261
262 it('Verify LESS_USED strategy default tasks usage statistics requirements', async () => {
263 const workerChoiceStrategy = WorkerChoiceStrategies.LESS_USED
264 let pool = new FixedThreadPool(
265 max,
266 './tests/worker-files/thread/testWorker.js',
267 { workerChoiceStrategy }
268 )
269 expect(
270 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
271 ).toBe(false)
272 expect(
273 pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
274 ).toBe(false)
275 expect(
276 pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
277 ).toBe(false)
278 await pool.destroy()
279 pool = new DynamicThreadPool(
280 min,
281 max,
282 './tests/worker-files/thread/testWorker.js',
283 { workerChoiceStrategy }
284 )
285 expect(
286 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
287 ).toBe(false)
288 expect(
289 pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
290 ).toBe(false)
291 expect(
292 pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
293 ).toBe(false)
294 // We need to clean up the resources after our test
295 await pool.destroy()
296 })
297
298 it('Verify LESS_USED strategy can be run in a fixed pool', async () => {
299 const pool = new FixedThreadPool(
300 max,
301 './tests/worker-files/thread/testWorker.js',
302 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED }
303 )
304 // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose`
305 const promises = []
306 for (let i = 0; i < max * 2; i++) {
307 promises.push(pool.execute())
308 }
309 await Promise.all(promises)
310 // We need to clean up the resources after our test
311 await pool.destroy()
312 })
313
314 it('Verify LESS_USED strategy can be run in a dynamic pool', async () => {
315 const pool = new DynamicThreadPool(
316 min,
317 max,
318 './tests/worker-files/thread/testWorker.js',
319 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED }
320 )
321 // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose`
322 const promises = []
323 for (let i = 0; i < max * 2; i++) {
324 promises.push(pool.execute())
325 }
326 await Promise.all(promises)
327 // We need to clean up the resources after our test
328 await pool.destroy()
329 })
330
331 it('Verify LESS_BUSY strategy default tasks usage statistics requirements', async () => {
332 const workerChoiceStrategy = WorkerChoiceStrategies.LESS_BUSY
333 let pool = new FixedThreadPool(
334 max,
335 './tests/worker-files/thread/testWorker.js',
336 { workerChoiceStrategy }
337 )
338 expect(
339 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
340 ).toBe(true)
341 expect(
342 pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
343 ).toBe(false)
344 expect(
345 pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
346 ).toBe(false)
347 await pool.destroy()
348 pool = new DynamicThreadPool(
349 min,
350 max,
351 './tests/worker-files/thread/testWorker.js',
352 { workerChoiceStrategy }
353 )
354 expect(
355 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
356 ).toBe(true)
357 expect(
358 pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
359 ).toBe(false)
360 expect(
361 pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
362 ).toBe(false)
363 // We need to clean up the resources after our test
364 await pool.destroy()
365 })
366
367 it('Verify LESS_BUSY strategy can be run in a fixed pool', async () => {
368 const pool = new FixedThreadPool(
369 max,
370 './tests/worker-files/thread/testWorker.js',
371 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY }
372 )
373 // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose`
374 const promises = []
375 for (let i = 0; i < max * 2; i++) {
376 promises.push(pool.execute())
377 }
378 await Promise.all(promises)
379 // We need to clean up the resources after our test
380 await pool.destroy()
381 })
382
383 it('Verify LESS_BUSY strategy can be run in a dynamic pool', async () => {
384 const pool = new DynamicThreadPool(
385 min,
386 max,
387 './tests/worker-files/thread/testWorker.js',
388 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY }
389 )
390 // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose`
391 const promises = []
392 for (let i = 0; i < max * 2; i++) {
393 promises.push(pool.execute())
394 }
395 await Promise.all(promises)
396 // We need to clean up the resources after our test
397 await pool.destroy()
398 })
399
400 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
401 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
402 let pool = new FixedThreadPool(
403 max,
404 './tests/worker-files/thread/testWorker.js',
405 { workerChoiceStrategy }
406 )
407 expect(
408 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
409 ).toBe(true)
410 expect(
411 pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
412 ).toBe(true)
413 expect(
414 pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
415 ).toBe(false)
416 await pool.destroy()
417 pool = new DynamicThreadPool(
418 min,
419 max,
420 './tests/worker-files/thread/testWorker.js',
421 { workerChoiceStrategy }
422 )
423 expect(
424 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
425 ).toBe(true)
426 expect(
427 pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
428 ).toBe(true)
429 expect(
430 pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
431 ).toBe(false)
432 // We need to clean up the resources after our test
433 await pool.destroy()
434 })
435
436 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
437 const pool = new FixedThreadPool(
438 max,
439 './tests/worker-files/thread/testWorker.js',
440 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
441 )
442 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
443 const promises = []
444 for (let i = 0; i < max * 2; i++) {
445 promises.push(pool.execute())
446 }
447 await Promise.all(promises)
448 expect(
449 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
450 pool.workerChoiceStrategyContext.workerChoiceStrategy
451 ).workerLastVirtualTaskTimestamp.size
452 ).toBe(pool.workerNodes.length)
453 // We need to clean up the resources after our test
454 await pool.destroy()
455 })
456
457 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
458 const pool = new DynamicThreadPool(
459 min,
460 max,
461 './tests/worker-files/thread/testWorker.js',
462 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
463 )
464 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
465 const promises = []
466 const maxMultiplier = 2
467 for (let i = 0; i < max * maxMultiplier; i++) {
468 promises.push(pool.execute())
469 }
470 await Promise.all(promises)
471 // if (process.platform !== 'win32') {
472 // expect(
473 // pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
474 // pool.workerChoiceStrategyContext.workerChoiceStrategy
475 // ).workerLastVirtualTaskTimestamp.size
476 // ).toBe(pool.workerNodes.length)
477 // }
478 // We need to clean up the resources after our test
479 await pool.destroy()
480 })
481
482 it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => {
483 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
484 let pool = new FixedThreadPool(
485 max,
486 './tests/worker-files/thread/testWorker.js'
487 )
488 expect(
489 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
490 workerChoiceStrategy
491 ).workerLastVirtualTaskTimestamp
492 ).toBeDefined()
493 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
494 for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
495 .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
496 .workerLastVirtualTaskTimestamp.keys()) {
497 expect(
498 pool.workerChoiceStrategyContext.workerChoiceStrategies
499 .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
500 .workerLastVirtualTaskTimestamp.get(workerNodeKey).start
501 ).toBe(0)
502 expect(
503 pool.workerChoiceStrategyContext.workerChoiceStrategies
504 .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
505 .workerLastVirtualTaskTimestamp.get(workerNodeKey).end
506 ).toBe(0)
507 }
508 await pool.destroy()
509 pool = new DynamicThreadPool(
510 min,
511 max,
512 './tests/worker-files/thread/testWorker.js'
513 )
514 expect(
515 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
516 workerChoiceStrategy
517 ).workerLastVirtualTaskTimestamp
518 ).toBeDefined()
519 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
520 for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
521 .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
522 .workerLastVirtualTaskTimestamp.keys()) {
523 expect(
524 pool.workerChoiceStrategyContext.workerChoiceStrategies
525 .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
526 .workerLastVirtualTaskTimestamp.get(workerNodeKey).start
527 ).toBe(0)
528 expect(
529 pool.workerChoiceStrategyContext.workerChoiceStrategies
530 .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
531 .workerLastVirtualTaskTimestamp.get(workerNodeKey).end
532 ).toBe(0)
533 }
534 // We need to clean up the resources after our test
535 await pool.destroy()
536 })
537
538 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
539 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
540 let pool = new FixedThreadPool(
541 max,
542 './tests/worker-files/thread/testWorker.js',
543 { workerChoiceStrategy }
544 )
545 expect(
546 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
547 ).toBe(true)
548 expect(
549 pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
550 ).toBe(true)
551 expect(
552 pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
553 ).toBe(false)
554 await pool.destroy()
555 pool = new DynamicThreadPool(
556 min,
557 max,
558 './tests/worker-files/thread/testWorker.js',
559 { workerChoiceStrategy }
560 )
561 expect(
562 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
563 ).toBe(true)
564 expect(
565 pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
566 ).toBe(true)
567 expect(
568 pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
569 ).toBe(false)
570 // We need to clean up the resources after our test
571 await pool.destroy()
572 })
573
574 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
575 const pool = new FixedThreadPool(
576 max,
577 './tests/worker-files/thread/testWorker.js',
578 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
579 )
580 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
581 const promises = []
582 for (let i = 0; i < max * 2; i++) {
583 promises.push(pool.execute())
584 }
585 await Promise.all(promises)
586 expect(
587 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
588 pool.workerChoiceStrategyContext.workerChoiceStrategy
589 ).workersTaskRunTime.size
590 ).toBe(pool.workerNodes.length)
591 // We need to clean up the resources after our test
592 await pool.destroy()
593 })
594
595 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
596 const pool = new DynamicThreadPool(
597 min,
598 max,
599 './tests/worker-files/thread/testWorker.js',
600 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
601 )
602 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
603 const promises = []
604 const maxMultiplier =
605 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
606 pool.workerChoiceStrategyContext.workerChoiceStrategy
607 ).defaultWorkerWeight * 50
608 for (let i = 0; i < max * maxMultiplier; i++) {
609 promises.push(pool.execute())
610 }
611 await Promise.all(promises)
612 if (process.platform !== 'win32') {
613 expect(
614 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
615 pool.workerChoiceStrategyContext.workerChoiceStrategy
616 ).workersTaskRunTime.size
617 ).toBe(pool.workerNodes.length)
618 }
619 // We need to clean up the resources after our test
620 await pool.destroy()
621 })
622
623 it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
624 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
625 let pool = new FixedThreadPool(
626 max,
627 './tests/worker-files/thread/testWorker.js'
628 )
629 expect(
630 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
631 workerChoiceStrategy
632 ).currentWorkerNodeId
633 ).toBeDefined()
634 expect(
635 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
636 workerChoiceStrategy
637 ).defaultWorkerWeight
638 ).toBeDefined()
639 expect(
640 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
641 workerChoiceStrategy
642 ).workersTaskRunTime
643 ).toBeDefined()
644 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
645 expect(
646 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
647 pool.workerChoiceStrategyContext.workerChoiceStrategy
648 ).currentWorkerNodeId
649 ).toBe(0)
650 expect(
651 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
652 pool.workerChoiceStrategyContext.workerChoiceStrategy
653 ).defaultWorkerWeight
654 ).toBeGreaterThan(0)
655 for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
656 .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
657 .workersTaskRunTime.keys()) {
658 expect(
659 pool.workerChoiceStrategyContext.workerChoiceStrategies
660 .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
661 .workersTaskRunTime.get(workerNodeKey).runTime
662 ).toBe(0)
663 }
664 await pool.destroy()
665 pool = new DynamicThreadPool(
666 min,
667 max,
668 './tests/worker-files/thread/testWorker.js'
669 )
670 expect(
671 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
672 workerChoiceStrategy
673 ).currentWorkerNodeId
674 ).toBeDefined()
675 expect(
676 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
677 workerChoiceStrategy
678 ).defaultWorkerWeight
679 ).toBeDefined()
680 expect(
681 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
682 workerChoiceStrategy
683 ).workersTaskRunTime
684 ).toBeDefined()
685 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
686 expect(
687 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
688 pool.workerChoiceStrategyContext.workerChoiceStrategy
689 ).currentWorkerNodeId
690 ).toBe(0)
691 expect(
692 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
693 pool.workerChoiceStrategyContext.workerChoiceStrategy
694 ).defaultWorkerWeight
695 ).toBeGreaterThan(0)
696 for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
697 .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
698 .workersTaskRunTime.keys()) {
699 expect(
700 pool.workerChoiceStrategyContext.workerChoiceStrategies
701 .get(pool.workerChoiceStrategyContext.workerChoiceStrategy)
702 .workersTaskRunTime.get(workerNodeKey).runTime
703 ).toBe(0)
704 }
705 // We need to clean up the resources after our test
706 await pool.destroy()
707 })
708
709 it('Verify unknown strategies throw error', () => {
710 expect(
711 () =>
712 new DynamicThreadPool(
713 min,
714 max,
715 './tests/worker-files/thread/testWorker.js',
716 { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
717 )
718 ).toThrowError(
719 new Error("Invalid worker choice strategy 'UNKNOWN_STRATEGY'")
720 )
721 })
722 })