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