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