a62571797c08010ef6da62b947c5a81e67af1e84
[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()[1].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()[1].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 workerKey of pool.workerChoiceStrategyContext
361 .getWorkerChoiceStrategy()
362 .workerLastVirtualTaskTimestamp.keys()) {
363 expect(
364 pool.workerChoiceStrategyContext
365 .getWorkerChoiceStrategy()
366 .workerLastVirtualTaskTimestamp.get(workerKey).start
367 ).toBe(0)
368 expect(
369 pool.workerChoiceStrategyContext
370 .getWorkerChoiceStrategy()
371 .workerLastVirtualTaskTimestamp.get(workerKey).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 workerKey of pool.workerChoiceStrategyContext
460 .getWorkerChoiceStrategy()
461 .workerLastVirtualTaskTimestamp.keys()) {
462 expect(
463 pool.workerChoiceStrategyContext
464 .getWorkerChoiceStrategy()
465 .workerLastVirtualTaskTimestamp.get(workerKey).start
466 ).toBe(0)
467 expect(
468 pool.workerChoiceStrategyContext
469 .getWorkerChoiceStrategy()
470 .workerLastVirtualTaskTimestamp.get(workerKey).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 workerKey of pool.workerChoiceStrategyContext
485 .getWorkerChoiceStrategy()
486 .workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) {
487 expect(
488 pool.workerChoiceStrategyContext
489 .getWorkerChoiceStrategy()
490 .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(workerKey)
491 .start
492 ).toBe(0)
493 expect(
494 pool.workerChoiceStrategyContext
495 .getWorkerChoiceStrategy()
496 .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(workerKey)
497 .end
498 ).toBe(0)
499 }
500 // We need to clean up the resources after our test
501 await pool.destroy()
502 })
503
504 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
505 const pool = new FixedThreadPool(
506 max,
507 './tests/worker-files/thread/testWorker.js',
508 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
509 )
510 expect(pool.opts.workerChoiceStrategy).toBe(
511 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
512 )
513 expect(
514 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
515 ).toBe(0)
516 expect(
517 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
518 .defaultWorkerWeight
519 ).toBeGreaterThan(0)
520 for (const workerKey of pool.workerChoiceStrategyContext
521 .getWorkerChoiceStrategy()
522 .workersTaskRunTime.keys()) {
523 expect(
524 pool.workerChoiceStrategyContext
525 .getWorkerChoiceStrategy()
526 .workersTaskRunTime.get(workerKey).weight
527 ).toBeGreaterThan(0)
528 expect(
529 pool.workerChoiceStrategyContext
530 .getWorkerChoiceStrategy()
531 .workersTaskRunTime.get(workerKey).runTime
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 can be set after pool creation', async () => {
539 const pool = new FixedThreadPool(
540 max,
541 './tests/worker-files/thread/testWorker.js'
542 )
543 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
544 expect(pool.opts.workerChoiceStrategy).toBe(
545 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
546 )
547 // We need to clean up the resources after our test
548 await pool.destroy()
549 })
550
551 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
552 let pool = new FixedThreadPool(
553 max,
554 './tests/worker-files/thread/testWorker.js'
555 )
556 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
557 expect(
558 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
559 .requiredStatistics.runTime
560 ).toBe(true)
561 await pool.destroy()
562 pool = new DynamicThreadPool(
563 min,
564 max,
565 './tests/worker-files/thread/testWorker.js'
566 )
567 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
568 expect(
569 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
570 .requiredStatistics.runTime
571 ).toBe(true)
572 // We need to clean up the resources after our test
573 await pool.destroy()
574 })
575
576 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
577 const pool = new FixedThreadPool(
578 max,
579 './tests/worker-files/thread/testWorker.js',
580 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
581 )
582 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
583 const promises = []
584 for (let i = 0; i < max * 2; i++) {
585 promises.push(pool.execute())
586 }
587 await Promise.all(promises)
588 // We need to clean up the resources after our test
589 await pool.destroy()
590 })
591
592 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
593 const pool = new DynamicThreadPool(
594 min,
595 max,
596 './tests/worker-files/thread/testWorker.js',
597 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
598 )
599 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
600 const promises = []
601 for (let i = 0; i < max * 2; i++) {
602 promises.push(pool.execute())
603 }
604 await Promise.all(promises)
605 // We need to clean up the resources after our test
606 await pool.destroy()
607 })
608
609 it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
610 let pool = new FixedThreadPool(
611 max,
612 './tests/worker-files/thread/testWorker.js'
613 )
614 expect(
615 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
616 ).toBeUndefined()
617 expect(
618 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
619 .defaultWorkerWeight
620 ).toBeUndefined()
621 expect(
622 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
623 .workersTaskRunTime
624 ).toBeUndefined()
625 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
626 expect(
627 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
628 ).toBe(0)
629 expect(
630 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
631 .defaultWorkerWeight
632 ).toBeGreaterThan(0)
633 for (const workerKey of pool.workerChoiceStrategyContext
634 .getWorkerChoiceStrategy()
635 .workersTaskRunTime.keys()) {
636 expect(
637 pool.workerChoiceStrategyContext
638 .getWorkerChoiceStrategy()
639 .workersTaskRunTime.get(workerKey).runTime
640 ).toBe(0)
641 }
642 await pool.destroy()
643 pool = new DynamicThreadPool(
644 min,
645 max,
646 './tests/worker-files/thread/testWorker.js'
647 )
648 expect(
649 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
650 .workerChoiceStrategy.currentWorkerId
651 ).toBeUndefined()
652 expect(
653 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
654 .workerChoiceStrategy.defaultWorkerWeight
655 ).toBeUndefined()
656 expect(
657 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
658 .workerChoiceStrategy.workersTaskRunTime
659 ).toBeUndefined()
660 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
661 expect(
662 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
663 .workerChoiceStrategy.currentWorkerId
664 ).toBe(0)
665 expect(
666 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
667 .workerChoiceStrategy.defaultWorkerWeight
668 ).toBeGreaterThan(0)
669 for (const workerKey of pool.workerChoiceStrategyContext
670 .getWorkerChoiceStrategy()
671 .workerChoiceStrategy.workersTaskRunTime.keys()) {
672 expect(
673 pool.workerChoiceStrategyContext
674 .getWorkerChoiceStrategy()
675 .workerChoiceStrategy.workersTaskRunTime.get(workerKey).runTime
676 ).toBe(0)
677 }
678 // We need to clean up the resources after our test
679 await pool.destroy()
680 })
681
682 it('Verify unknown strategies throw error', () => {
683 expect(
684 () =>
685 new DynamicThreadPool(
686 min,
687 max,
688 './tests/worker-files/thread/testWorker.js',
689 { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
690 )
691 ).toThrowError(
692 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
693 )
694 })
695 })