1 const { expect
} = require('expect')
3 WorkerChoiceStrategies
,
7 } = require('../../../lib/index')
9 describe('Selection strategies test suite', () => {
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'
23 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
24 const pool
= new DynamicThreadPool(
27 './tests/worker-files/thread/testWorker.js'
29 expect(pool
.opts
.workerChoiceStrategy
).toBe(
30 WorkerChoiceStrategies
.ROUND_ROBIN
32 // We need to clean up the resources after our test
36 it('Verify ROUND_ROBIN strategy is taken at pool creation', async () => {
37 const pool
= new FixedThreadPool(
39 './tests/worker-files/thread/testWorker.js',
40 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
42 expect(pool
.opts
.workerChoiceStrategy
).toBe(
43 WorkerChoiceStrategies
.ROUND_ROBIN
45 expect(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
46 WorkerChoiceStrategies
.ROUND_ROBIN
49 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
50 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
53 // We need to clean up the resources after our test
57 it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
58 const pool
= new DynamicThreadPool(
61 './tests/worker-files/thread/testWorker.js'
63 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
64 expect(pool
.opts
.workerChoiceStrategy
).toBe(
65 WorkerChoiceStrategies
.ROUND_ROBIN
67 expect(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
68 WorkerChoiceStrategies
.ROUND_ROBIN
70 // We need to clean up the resources after our test
74 it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
75 let pool
= new FixedThreadPool(
77 './tests/worker-files/thread/testWorker.js',
78 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
81 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
84 pool
.workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
87 pool
.workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
90 pool
= new DynamicThreadPool(
93 './tests/worker-files/thread/testWorker.js',
94 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
97 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
100 pool
.workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
103 pool
.workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
105 // We need to clean up the resources after our test
109 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
110 const pool
= new FixedThreadPool(
112 './tests/worker-files/thread/testWorker.js',
113 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
115 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
117 for (let i
= 0; i
< max
* 2; i
++) {
118 promises
.push(pool
.execute())
120 await Promise
.all(promises
)
121 // We need to clean up the resources after our test
125 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
126 const pool
= new DynamicThreadPool(
129 './tests/worker-files/thread/testWorker.js',
130 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
132 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
134 for (let i
= 0; i
< max
* 2; i
++) {
135 promises
.push(pool
.execute())
137 await Promise
.all(promises
)
138 // We need to clean up the resources after our test
142 it('Verify ROUND_ROBIN strategy runtime behavior', async () => {
143 let pool
= new FixedClusterPool(
145 './tests/worker-files/cluster/testWorker.js'
147 let results
= new Set()
148 for (let i
= 0; i
< max
; i
++) {
149 results
.add(pool
.chooseWorkerNode()[1].worker
.id
)
151 expect(results
.size
).toBe(max
)
153 pool
= new FixedThreadPool(max
, './tests/worker-files/thread/testWorker.js')
155 for (let i
= 0; i
< max
; i
++) {
156 results
.add(pool
.chooseWorkerNode()[1].worker
.threadId
)
158 expect(results
.size
).toBe(max
)
162 it('Verify ROUND_ROBIN strategy internals are resets after setting it', async () => {
163 let pool
= new FixedThreadPool(
165 './tests/worker-files/thread/testWorker.js',
166 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
169 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
170 WorkerChoiceStrategies
.ROUND_ROBIN
173 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
175 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
176 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
180 pool
= new DynamicThreadPool(
183 './tests/worker-files/thread/testWorker.js',
184 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
187 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
188 WorkerChoiceStrategies
.ROUND_ROBIN
191 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
193 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
194 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
197 // We need to clean up the resources after our test
201 it('Verify LESS_USED strategy is taken at pool creation', async () => {
202 const pool
= new FixedThreadPool(
204 './tests/worker-files/thread/testWorker.js',
205 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_USED
}
207 expect(pool
.opts
.workerChoiceStrategy
).toBe(
208 WorkerChoiceStrategies
.LESS_USED
210 expect(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
211 WorkerChoiceStrategies
.LESS_USED
213 // We need to clean up the resources after our test
217 it('Verify LESS_USED strategy can be set after pool creation', async () => {
218 const pool
= new FixedThreadPool(
220 './tests/worker-files/thread/testWorker.js'
222 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_USED
)
223 expect(pool
.opts
.workerChoiceStrategy
).toBe(
224 WorkerChoiceStrategies
.LESS_USED
226 expect(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
227 WorkerChoiceStrategies
.LESS_USED
229 // We need to clean up the resources after our test
233 it('Verify LESS_USED strategy default tasks usage statistics requirements', async () => {
234 let pool
= new FixedThreadPool(
236 './tests/worker-files/thread/testWorker.js',
237 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_USED
}
240 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
243 pool
.workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
246 pool
.workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
249 pool
= new DynamicThreadPool(
252 './tests/worker-files/thread/testWorker.js',
253 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_USED
}
256 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
259 pool
.workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
262 pool
.workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
264 // We need to clean up the resources after our test
268 it('Verify LESS_USED strategy can be run in a fixed pool', async () => {
269 const pool
= new FixedThreadPool(
271 './tests/worker-files/thread/testWorker.js',
272 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_USED
}
274 // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose`
276 for (let i
= 0; i
< max
* 2; i
++) {
277 promises
.push(pool
.execute())
279 await Promise
.all(promises
)
280 // We need to clean up the resources after our test
284 it('Verify LESS_USED strategy can be run in a dynamic pool', async () => {
285 const pool
= new DynamicThreadPool(
288 './tests/worker-files/thread/testWorker.js',
289 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_USED
}
291 // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose`
293 for (let i
= 0; i
< max
* 2; i
++) {
294 promises
.push(pool
.execute())
296 await Promise
.all(promises
)
297 // We need to clean up the resources after our test
301 it('Verify LESS_BUSY strategy is taken at pool creation', async () => {
302 const pool
= new FixedThreadPool(
304 './tests/worker-files/thread/testWorker.js',
305 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_BUSY
}
307 expect(pool
.opts
.workerChoiceStrategy
).toBe(
308 WorkerChoiceStrategies
.LESS_BUSY
310 expect(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
311 WorkerChoiceStrategies
.LESS_BUSY
313 // We need to clean up the resources after our test
317 it('Verify LESS_BUSY strategy can be set after pool creation', async () => {
318 const pool
= new FixedThreadPool(
320 './tests/worker-files/thread/testWorker.js'
322 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_BUSY
)
323 expect(pool
.opts
.workerChoiceStrategy
).toBe(
324 WorkerChoiceStrategies
.LESS_BUSY
326 expect(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
327 WorkerChoiceStrategies
.LESS_BUSY
329 // We need to clean up the resources after our test
333 it('Verify LESS_BUSY strategy default tasks usage statistics requirements', async () => {
334 let pool
= new FixedThreadPool(
336 './tests/worker-files/thread/testWorker.js',
337 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_BUSY
}
340 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
343 pool
.workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
346 pool
.workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
349 pool
= new DynamicThreadPool(
352 './tests/worker-files/thread/testWorker.js',
353 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_BUSY
}
356 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
359 pool
.workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
362 pool
.workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
364 // We need to clean up the resources after our test
368 it('Verify LESS_BUSY strategy can be run in a fixed pool', async () => {
369 const pool
= new FixedThreadPool(
371 './tests/worker-files/thread/testWorker.js',
372 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_BUSY
}
374 // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose`
376 for (let i
= 0; i
< max
* 2; i
++) {
377 promises
.push(pool
.execute())
379 await Promise
.all(promises
)
380 // We need to clean up the resources after our test
384 it('Verify LESS_BUSY strategy can be run in a dynamic pool', async () => {
385 const pool
= new DynamicThreadPool(
388 './tests/worker-files/thread/testWorker.js',
389 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_BUSY
}
391 // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose`
393 for (let i
= 0; i
< max
* 2; i
++) {
394 promises
.push(pool
.execute())
396 await Promise
.all(promises
)
397 // We need to clean up the resources after our test
401 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
402 const pool
= new FixedThreadPool(
404 './tests/worker-files/thread/testWorker.js',
405 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
407 expect(pool
.opts
.workerChoiceStrategy
).toBe(
408 WorkerChoiceStrategies
.FAIR_SHARE
410 expect(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
411 WorkerChoiceStrategies
.FAIR_SHARE
413 for (const workerNodeKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategies
414 .get(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
)
415 .workerLastVirtualTaskTimestamp
.keys()) {
417 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
418 .get(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
)
419 .workerLastVirtualTaskTimestamp
.get(workerNodeKey
).start
422 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
423 .get(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
)
424 .workerLastVirtualTaskTimestamp
.get(workerNodeKey
).end
427 // We need to clean up the resources after our test
431 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
432 const pool
= new FixedThreadPool(
434 './tests/worker-files/thread/testWorker.js'
436 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
437 expect(pool
.opts
.workerChoiceStrategy
).toBe(
438 WorkerChoiceStrategies
.FAIR_SHARE
440 expect(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
441 WorkerChoiceStrategies
.FAIR_SHARE
443 // We need to clean up the resources after our test
447 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
448 let pool
= new FixedThreadPool(
450 './tests/worker-files/thread/testWorker.js',
451 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
454 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
457 pool
.workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
460 pool
.workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
463 pool
= new DynamicThreadPool(
466 './tests/worker-files/thread/testWorker.js',
467 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
470 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
473 pool
.workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
476 pool
.workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
478 // We need to clean up the resources after our test
482 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
483 const pool
= new FixedThreadPool(
485 './tests/worker-files/thread/testWorker.js',
486 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
488 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
490 for (let i
= 0; i
< max
* 2; i
++) {
491 promises
.push(pool
.execute())
493 await Promise
.all(promises
)
495 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
496 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
497 ).workerLastVirtualTaskTimestamp
.size
498 ).toBe(pool
.workerNodes
.length
)
499 // We need to clean up the resources after our test
503 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
504 const pool
= new DynamicThreadPool(
507 './tests/worker-files/thread/testWorker.js',
508 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
510 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
512 const maxMultiplier
= 2
513 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
514 promises
.push(pool
.execute())
516 await Promise
.all(promises
)
517 // if (process.platform !== 'win32') {
519 // pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
520 // pool.workerChoiceStrategyContext.workerChoiceStrategy
521 // ).workerLastVirtualTaskTimestamp.size
522 // ).toBe(pool.workerNodes.length)
524 // We need to clean up the resources after our test
528 it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => {
529 let pool
= new FixedThreadPool(
531 './tests/worker-files/thread/testWorker.js'
534 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
535 WorkerChoiceStrategies
.FAIR_SHARE
536 ).workerLastVirtualTaskTimestamp
538 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
539 for (const workerNodeKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategies
540 .get(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
)
541 .workerLastVirtualTaskTimestamp
.keys()) {
543 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
544 .get(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
)
545 .workerLastVirtualTaskTimestamp
.get(workerNodeKey
).start
548 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
549 .get(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
)
550 .workerLastVirtualTaskTimestamp
.get(workerNodeKey
).end
554 pool
= new DynamicThreadPool(
557 './tests/worker-files/thread/testWorker.js'
560 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
561 WorkerChoiceStrategies
.FAIR_SHARE
562 ).workerLastVirtualTaskTimestamp
564 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
565 for (const workerNodeKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategies
566 .get(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
)
567 .workerLastVirtualTaskTimestamp
.keys()) {
569 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
570 .get(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
)
571 .workerLastVirtualTaskTimestamp
.get(workerNodeKey
).start
574 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
575 .get(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
)
576 .workerLastVirtualTaskTimestamp
.get(workerNodeKey
).end
579 // We need to clean up the resources after our test
583 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
584 const pool
= new FixedThreadPool(
586 './tests/worker-files/thread/testWorker.js',
587 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
589 expect(pool
.opts
.workerChoiceStrategy
).toBe(
590 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
592 expect(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
593 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
596 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
597 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
598 ).currentWorkerNodeId
601 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
602 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
603 ).defaultWorkerWeight
605 for (const workerNodeKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategies
606 .get(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
)
607 .workersTaskRunTime
.keys()) {
609 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
610 .get(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
)
611 .workersTaskRunTime
.get(workerNodeKey
).weight
614 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
615 .get(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
)
616 .workersTaskRunTime
.get(workerNodeKey
).runTime
619 // We need to clean up the resources after our test
623 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
624 const pool
= new FixedThreadPool(
626 './tests/worker-files/thread/testWorker.js'
628 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
629 expect(pool
.opts
.workerChoiceStrategy
).toBe(
630 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
632 expect(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
633 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
635 // We need to clean up the resources after our test
639 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
640 let pool
= new FixedThreadPool(
642 './tests/worker-files/thread/testWorker.js',
643 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
646 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
649 pool
.workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
652 pool
.workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
655 pool
= new DynamicThreadPool(
658 './tests/worker-files/thread/testWorker.js',
659 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
662 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
665 pool
.workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
668 pool
.workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
670 // We need to clean up the resources after our test
674 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
675 const pool
= new FixedThreadPool(
677 './tests/worker-files/thread/testWorker.js',
678 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
680 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
682 for (let i
= 0; i
< max
* 2; i
++) {
683 promises
.push(pool
.execute())
685 await Promise
.all(promises
)
687 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
688 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
689 ).workersTaskRunTime
.size
690 ).toBe(pool
.workerNodes
.length
)
691 // We need to clean up the resources after our test
695 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
696 const pool
= new DynamicThreadPool(
699 './tests/worker-files/thread/testWorker.js',
700 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
702 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
704 const maxMultiplier
=
705 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
706 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
707 ).defaultWorkerWeight
* 50
708 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
709 promises
.push(pool
.execute())
711 await Promise
.all(promises
)
712 if (process
.platform
!== 'win32') {
714 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
715 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
716 ).workersTaskRunTime
.size
717 ).toBe(pool
.workerNodes
.length
)
719 // We need to clean up the resources after our test
723 it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
724 let pool
= new FixedThreadPool(
726 './tests/worker-files/thread/testWorker.js'
729 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
730 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
731 ).currentWorkerNodeId
734 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
735 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
736 ).defaultWorkerWeight
739 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
740 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
743 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
745 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
746 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
747 ).currentWorkerNodeId
750 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
751 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
752 ).defaultWorkerWeight
754 for (const workerNodeKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategies
755 .get(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
)
756 .workersTaskRunTime
.keys()) {
758 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
759 .get(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
)
760 .workersTaskRunTime
.get(workerNodeKey
).runTime
764 pool
= new DynamicThreadPool(
767 './tests/worker-files/thread/testWorker.js'
770 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
771 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
772 ).currentWorkerNodeId
775 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
776 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
777 ).defaultWorkerWeight
780 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
781 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
784 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
786 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
787 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
788 ).currentWorkerNodeId
791 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
792 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
793 ).defaultWorkerWeight
795 for (const workerNodeKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategies
796 .get(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
)
797 .workersTaskRunTime
.keys()) {
799 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
800 .get(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
)
801 .workersTaskRunTime
.get(workerNodeKey
).runTime
804 // We need to clean up the resources after our test
808 it('Verify unknown strategies throw error', () => {
811 new DynamicThreadPool(
814 './tests/worker-files/thread/testWorker.js',
815 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
818 new Error("Invalid worker choice strategy 'UNKNOWN_STRATEGY'")