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