1 const { expect
} = require('expect')
3 WorkerChoiceStrategies
,
7 } = require('../../../lib')
8 const { CircularArray
} = require('../../../lib/circular-array')
10 describe('Selection strategies test suite', () => {
14 it('Verify that WorkerChoiceStrategies enumeration provides string values', () => {
15 expect(WorkerChoiceStrategies
.ROUND_ROBIN
).toBe('ROUND_ROBIN')
16 expect(WorkerChoiceStrategies
.LEAST_USED
).toBe('LEAST_USED')
17 expect(WorkerChoiceStrategies
.LEAST_BUSY
).toBe('LEAST_BUSY')
18 expect(WorkerChoiceStrategies
.FAIR_SHARE
).toBe('FAIR_SHARE')
19 expect(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
).toBe(
20 'WEIGHTED_ROUND_ROBIN'
24 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
25 const pool
= new DynamicThreadPool(
28 './tests/worker-files/thread/testWorker.js'
30 expect(pool
.opts
.workerChoiceStrategy
).toBe(
31 WorkerChoiceStrategies
.ROUND_ROBIN
33 // We need to clean up the resources after our test
37 it('Verify available strategies are taken at pool creation', async () => {
38 for (const workerChoiceStrategy
of Object
.values(WorkerChoiceStrategies
)) {
39 const pool
= new FixedThreadPool(
41 './tests/worker-files/thread/testWorker.js',
42 { workerChoiceStrategy
}
44 expect(pool
.opts
.workerChoiceStrategy
).toBe(workerChoiceStrategy
)
45 expect(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
52 it('Verify available strategies can be set after pool creation', async () => {
53 for (const workerChoiceStrategy
of Object
.values(WorkerChoiceStrategies
)) {
54 const pool
= new DynamicThreadPool(
57 './tests/worker-files/thread/testWorker.js'
59 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
)
60 expect(pool
.opts
.workerChoiceStrategy
).toBe(workerChoiceStrategy
)
61 expect(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
68 it('Verify available strategies default internals at pool creation', async () => {
69 const pool
= new FixedThreadPool(
71 './tests/worker-files/thread/testWorker.js'
73 for (const workerChoiceStrategy
of Object
.values(WorkerChoiceStrategies
)) {
74 if (workerChoiceStrategy
=== WorkerChoiceStrategies
.ROUND_ROBIN
) {
76 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
80 } else if (workerChoiceStrategy
=== WorkerChoiceStrategies
.FAIR_SHARE
) {
82 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
84 ).workersVirtualTaskEndTimestamp
85 ).toBeInstanceOf(Array
)
87 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
89 ).workersVirtualTaskEndTimestamp
.length
92 workerChoiceStrategy
=== WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
95 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
100 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
102 ).defaultWorkerWeight
105 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
107 ).workerVirtualTaskRunTime
114 it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
115 const workerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
116 let pool
= new FixedThreadPool(
118 './tests/worker-files/thread/testWorker.js',
119 { workerChoiceStrategy
}
122 pool
.workerChoiceStrategyContext
.getRequiredStatistics()
132 pool
= new DynamicThreadPool(
135 './tests/worker-files/thread/testWorker.js',
136 { workerChoiceStrategy
}
139 pool
.workerChoiceStrategyContext
.getRequiredStatistics()
148 // We need to clean up the resources after our test
152 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
153 const pool
= new FixedThreadPool(
155 './tests/worker-files/thread/testWorker.js',
156 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
158 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
159 const promises
= new Set()
160 const maxMultiplier
= 2
161 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
162 promises
.add(pool
.execute())
164 await Promise
.all(promises
)
165 for (const workerNode
of pool
.workerNodes
) {
166 expect(workerNode
.tasksUsage
).toStrictEqual({
170 runTimeHistory
: expect
.any(CircularArray
),
174 waitTimeHistory
: expect
.any(CircularArray
),
180 // We need to clean up the resources after our test
184 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
185 const pool
= new DynamicThreadPool(
188 './tests/worker-files/thread/testWorker.js',
189 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
191 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
192 const promises
= new Set()
193 const maxMultiplier
= 2
194 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
195 promises
.add(pool
.execute())
197 await Promise
.all(promises
)
198 for (const workerNode
of pool
.workerNodes
) {
199 expect(workerNode
.tasksUsage
).toStrictEqual({
203 runTimeHistory
: expect
.any(CircularArray
),
207 waitTimeHistory
: expect
.any(CircularArray
),
213 // We need to clean up the resources after our test
217 it('Verify ROUND_ROBIN strategy runtime behavior', async () => {
218 const workerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
219 let pool
= new FixedClusterPool(
221 './tests/worker-files/cluster/testWorker.js',
222 { workerChoiceStrategy
}
224 let results
= new Set()
225 for (let i
= 0; i
< max
; i
++) {
226 results
.add(pool
.workerNodes
[pool
.chooseWorkerNode()].worker
.id
)
228 expect(results
.size
).toBe(max
)
230 pool
= new FixedThreadPool(
232 './tests/worker-files/thread/testWorker.js',
233 { workerChoiceStrategy
}
236 for (let i
= 0; i
< max
; i
++) {
237 results
.add(pool
.workerNodes
[pool
.chooseWorkerNode()].worker
.threadId
)
239 expect(results
.size
).toBe(max
)
243 it('Verify ROUND_ROBIN strategy internals are resets after setting it', async () => {
244 const workerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
245 let pool
= new FixedThreadPool(
247 './tests/worker-files/thread/testWorker.js',
248 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
251 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
255 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
)
257 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
258 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
262 pool
= new DynamicThreadPool(
265 './tests/worker-files/thread/testWorker.js',
266 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
269 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
273 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
)
275 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
276 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
279 // We need to clean up the resources after our test
283 it('Verify LEAST_USED strategy default tasks usage statistics requirements', async () => {
284 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_USED
285 let pool
= new FixedThreadPool(
287 './tests/worker-files/thread/testWorker.js',
288 { workerChoiceStrategy
}
291 pool
.workerChoiceStrategyContext
.getRequiredStatistics()
301 pool
= new DynamicThreadPool(
304 './tests/worker-files/thread/testWorker.js',
305 { workerChoiceStrategy
}
308 pool
.workerChoiceStrategyContext
.getRequiredStatistics()
317 // We need to clean up the resources after our test
321 it('Verify LEAST_USED strategy can be run in a fixed pool', async () => {
322 const pool
= new FixedThreadPool(
324 './tests/worker-files/thread/testWorker.js',
325 { workerChoiceStrategy
: WorkerChoiceStrategies
.LEAST_USED
}
327 // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose`
328 const promises
= new Set()
329 const maxMultiplier
= 2
330 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
331 promises
.add(pool
.execute())
333 await Promise
.all(promises
)
334 for (const workerNode
of pool
.workerNodes
) {
335 expect(workerNode
.tasksUsage
).toStrictEqual({
339 runTimeHistory
: expect
.any(CircularArray
),
343 waitTimeHistory
: expect
.any(CircularArray
),
349 // We need to clean up the resources after our test
353 it('Verify LEAST_USED strategy can be run in a dynamic pool', async () => {
354 const pool
= new DynamicThreadPool(
357 './tests/worker-files/thread/testWorker.js',
358 { workerChoiceStrategy
: WorkerChoiceStrategies
.LEAST_USED
}
360 // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose`
361 const promises
= new Set()
362 const maxMultiplier
= 2
363 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
364 promises
.add(pool
.execute())
366 await Promise
.all(promises
)
367 for (const workerNode
of pool
.workerNodes
) {
368 expect(workerNode
.tasksUsage
).toStrictEqual({
372 runTimeHistory
: expect
.any(CircularArray
),
376 waitTimeHistory
: expect
.any(CircularArray
),
382 // We need to clean up the resources after our test
386 it('Verify LEAST_BUSY strategy default tasks usage statistics requirements', async () => {
387 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_BUSY
388 let pool
= new FixedThreadPool(
390 './tests/worker-files/thread/testWorker.js',
391 { workerChoiceStrategy
}
394 pool
.workerChoiceStrategyContext
.getRequiredStatistics()
404 pool
= new DynamicThreadPool(
407 './tests/worker-files/thread/testWorker.js',
408 { workerChoiceStrategy
}
411 pool
.workerChoiceStrategyContext
.getRequiredStatistics()
420 // We need to clean up the resources after our test
424 it('Verify LEAST_BUSY strategy can be run in a fixed pool', async () => {
425 const pool
= new FixedThreadPool(
427 './tests/worker-files/thread/testWorker.js',
428 { workerChoiceStrategy
: WorkerChoiceStrategies
.LEAST_BUSY
}
430 // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose`
431 const promises
= new Set()
432 const maxMultiplier
= 2
433 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
434 promises
.add(pool
.execute())
436 await Promise
.all(promises
)
437 for (const workerNode
of pool
.workerNodes
) {
438 expect(workerNode
.tasksUsage
).toStrictEqual({
439 run
: expect
.any(Number
),
441 runTime
: expect
.any(Number
),
442 runTimeHistory
: expect
.any(CircularArray
),
446 waitTimeHistory
: expect
.any(CircularArray
),
451 expect(workerNode
.tasksUsage
.run
).toBeGreaterThan(0)
452 expect(workerNode
.tasksUsage
.run
).toBeLessThanOrEqual(max
* maxMultiplier
)
453 expect(workerNode
.tasksUsage
.runTime
).toBeGreaterThan(0)
455 // We need to clean up the resources after our test
459 it('Verify LEAST_BUSY strategy can be run in a dynamic pool', async () => {
460 const pool
= new DynamicThreadPool(
463 './tests/worker-files/thread/testWorker.js',
464 { workerChoiceStrategy
: WorkerChoiceStrategies
.LEAST_BUSY
}
466 // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose`
467 const promises
= new Set()
468 const maxMultiplier
= 2
469 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
470 promises
.add(pool
.execute())
472 await Promise
.all(promises
)
473 for (const workerNode
of pool
.workerNodes
) {
474 expect(workerNode
.tasksUsage
).toStrictEqual({
475 run
: expect
.any(Number
),
477 runTime
: expect
.any(Number
),
478 runTimeHistory
: expect
.any(CircularArray
),
482 waitTimeHistory
: expect
.any(CircularArray
),
487 expect(workerNode
.tasksUsage
.run
).toBeGreaterThan(0)
488 expect(workerNode
.tasksUsage
.run
).toBeLessThanOrEqual(max
* maxMultiplier
)
489 expect(workerNode
.tasksUsage
.runTime
).toBeGreaterThan(0)
491 // We need to clean up the resources after our test
495 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
496 const workerChoiceStrategy
= WorkerChoiceStrategies
.FAIR_SHARE
497 let pool
= new FixedThreadPool(
499 './tests/worker-files/thread/testWorker.js',
500 { workerChoiceStrategy
}
503 pool
.workerChoiceStrategyContext
.getRequiredStatistics()
513 pool
= new DynamicThreadPool(
516 './tests/worker-files/thread/testWorker.js',
517 { workerChoiceStrategy
}
520 pool
.workerChoiceStrategyContext
.getRequiredStatistics()
529 // We need to clean up the resources after our test
533 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
534 const pool
= new FixedThreadPool(
536 './tests/worker-files/thread/testWorker.js',
537 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
539 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
540 const promises
= new Set()
541 const maxMultiplier
= 2
542 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
543 promises
.add(pool
.execute())
545 await Promise
.all(promises
)
546 for (const workerNode
of pool
.workerNodes
) {
547 expect(workerNode
.tasksUsage
).toStrictEqual({
550 runTime
: expect
.any(Number
),
551 runTimeHistory
: expect
.any(CircularArray
),
552 avgRunTime
: expect
.any(Number
),
555 waitTimeHistory
: expect
.any(CircularArray
),
560 expect(workerNode
.tasksUsage
.runTime
).toBeGreaterThan(0)
561 expect(workerNode
.tasksUsage
.avgRunTime
).toBeGreaterThan(0)
564 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
565 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
566 ).workersVirtualTaskEndTimestamp
.length
567 ).toBe(pool
.workerNodes
.length
)
568 // We need to clean up the resources after our test
572 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
573 const pool
= new DynamicThreadPool(
576 './tests/worker-files/thread/testWorker.js',
577 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
579 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
580 const promises
= new Set()
581 const maxMultiplier
= 2
582 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
583 promises
.add(pool
.execute())
585 await Promise
.all(promises
)
586 for (const workerNode
of pool
.workerNodes
) {
587 expect(workerNode
.tasksUsage
).toStrictEqual({
590 runTime
: expect
.any(Number
),
591 runTimeHistory
: expect
.any(CircularArray
),
592 avgRunTime
: expect
.any(Number
),
595 waitTimeHistory
: expect
.any(CircularArray
),
600 expect(workerNode
.tasksUsage
.runTime
).toBeGreaterThan(0)
601 expect(workerNode
.tasksUsage
.avgRunTime
).toBeGreaterThan(0)
604 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
605 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
606 ).workersVirtualTaskEndTimestamp
.length
607 ).toBe(pool
.workerNodes
.length
)
608 // We need to clean up the resources after our test
612 it('Verify FAIR_SHARE strategy can be run in a dynamic pool with median runtime statistic', async () => {
613 const pool
= new DynamicThreadPool(
616 './tests/worker-files/thread/testWorker.js',
618 workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
,
619 workerChoiceStrategyOptions
: {
624 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
625 const promises
= new Set()
626 const maxMultiplier
= 2
627 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
628 promises
.add(pool
.execute())
630 await Promise
.all(promises
)
631 for (const workerNode
of pool
.workerNodes
) {
632 expect(workerNode
.tasksUsage
).toStrictEqual({
635 runTime
: expect
.any(Number
),
636 runTimeHistory
: expect
.any(CircularArray
),
638 medRunTime
: expect
.any(Number
),
640 waitTimeHistory
: expect
.any(CircularArray
),
645 expect(workerNode
.tasksUsage
.runTime
).toBeGreaterThan(0)
646 expect(workerNode
.tasksUsage
.medRunTime
).toBeGreaterThan(0)
649 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
650 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
651 ).workersVirtualTaskEndTimestamp
.length
652 ).toBe(pool
.workerNodes
.length
)
653 // We need to clean up the resources after our test
657 it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => {
658 const workerChoiceStrategy
= WorkerChoiceStrategies
.FAIR_SHARE
659 let pool
= new FixedThreadPool(
661 './tests/worker-files/thread/testWorker.js'
664 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
666 ).workersVirtualTaskEndTimestamp
667 ).toBeInstanceOf(Array
)
669 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
671 ).workersVirtualTaskEndTimestamp
.length
673 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
675 ).workersVirtualTaskEndTimestamp
[0] = performance
.now()
677 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
679 ).workersVirtualTaskEndTimestamp
.length
681 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
)
683 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
685 ).workersVirtualTaskEndTimestamp
686 ).toBeInstanceOf(Array
)
688 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
690 ).workersVirtualTaskEndTimestamp
.length
693 pool
= new DynamicThreadPool(
696 './tests/worker-files/thread/testWorker.js'
699 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
701 ).workersVirtualTaskEndTimestamp
702 ).toBeInstanceOf(Array
)
704 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
706 ).workersVirtualTaskEndTimestamp
.length
708 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
710 ).workersVirtualTaskEndTimestamp
[0] = performance
.now()
712 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
714 ).workersVirtualTaskEndTimestamp
.length
716 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
)
718 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
720 ).workersVirtualTaskEndTimestamp
721 ).toBeInstanceOf(Array
)
723 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
725 ).workersVirtualTaskEndTimestamp
.length
727 // We need to clean up the resources after our test
731 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
732 const workerChoiceStrategy
= WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
733 let pool
= new FixedThreadPool(
735 './tests/worker-files/thread/testWorker.js',
736 { workerChoiceStrategy
}
739 pool
.workerChoiceStrategyContext
.getRequiredStatistics()
749 pool
= new DynamicThreadPool(
752 './tests/worker-files/thread/testWorker.js',
753 { workerChoiceStrategy
}
756 pool
.workerChoiceStrategyContext
.getRequiredStatistics()
765 // We need to clean up the resources after our test
769 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
770 const pool
= new FixedThreadPool(
772 './tests/worker-files/thread/testWorker.js',
773 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
775 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
776 const promises
= new Set()
777 const maxMultiplier
= 2
778 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
779 promises
.add(pool
.execute())
781 await Promise
.all(promises
)
782 for (const workerNode
of pool
.workerNodes
) {
783 expect(workerNode
.tasksUsage
).toStrictEqual({
784 run
: expect
.any(Number
),
786 runTime
: expect
.any(Number
),
787 runTimeHistory
: expect
.any(CircularArray
),
788 avgRunTime
: expect
.any(Number
),
791 waitTimeHistory
: expect
.any(CircularArray
),
796 expect(workerNode
.tasksUsage
.run
).toBeGreaterThanOrEqual(0)
797 expect(workerNode
.tasksUsage
.run
).toBeLessThanOrEqual(max
* maxMultiplier
)
798 expect(workerNode
.tasksUsage
.runTime
).toBeGreaterThanOrEqual(0)
799 expect(workerNode
.tasksUsage
.avgRunTime
).toBeGreaterThanOrEqual(0)
802 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
803 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
804 ).defaultWorkerWeight
807 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
808 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
809 ).workerVirtualTaskRunTime
810 ).toBeGreaterThanOrEqual(0)
811 // We need to clean up the resources after our test
815 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
816 const pool
= new DynamicThreadPool(
819 './tests/worker-files/thread/testWorker.js',
820 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
822 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
823 const promises
= new Set()
824 const maxMultiplier
= 2
825 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
826 promises
.add(pool
.execute())
828 await Promise
.all(promises
)
829 for (const workerNode
of pool
.workerNodes
) {
830 expect(workerNode
.tasksUsage
).toStrictEqual({
831 run
: expect
.any(Number
),
833 runTime
: expect
.any(Number
),
834 runTimeHistory
: expect
.any(CircularArray
),
835 avgRunTime
: expect
.any(Number
),
838 waitTimeHistory
: expect
.any(CircularArray
),
843 expect(workerNode
.tasksUsage
.run
).toBeGreaterThan(0)
844 expect(workerNode
.tasksUsage
.run
).toBeLessThanOrEqual(max
* maxMultiplier
)
845 expect(workerNode
.tasksUsage
.runTime
).toBeGreaterThan(0)
846 expect(workerNode
.tasksUsage
.avgRunTime
).toBeGreaterThan(0)
849 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
850 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
851 ).defaultWorkerWeight
854 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
855 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
856 ).workerVirtualTaskRunTime
857 ).toBeGreaterThanOrEqual(0)
858 // We need to clean up the resources after our test
862 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool with median runtime statistic', async () => {
863 const pool
= new DynamicThreadPool(
866 './tests/worker-files/thread/testWorker.js',
868 workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
,
869 workerChoiceStrategyOptions
: {
874 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
875 const promises
= new Set()
876 const maxMultiplier
= 2
877 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
878 promises
.add(pool
.execute())
880 await Promise
.all(promises
)
881 for (const workerNode
of pool
.workerNodes
) {
882 expect(workerNode
.tasksUsage
).toStrictEqual({
883 run
: expect
.any(Number
),
885 runTime
: expect
.any(Number
),
886 runTimeHistory
: expect
.any(CircularArray
),
888 medRunTime
: expect
.any(Number
),
890 waitTimeHistory
: expect
.any(CircularArray
),
895 expect(workerNode
.tasksUsage
.run
).toBeGreaterThan(0)
896 expect(workerNode
.tasksUsage
.run
).toBeLessThanOrEqual(max
* maxMultiplier
)
897 expect(workerNode
.tasksUsage
.runTime
).toBeGreaterThan(0)
898 expect(workerNode
.tasksUsage
.medRunTime
).toBeGreaterThan(0)
901 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
902 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
903 ).defaultWorkerWeight
906 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
907 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
908 ).workerVirtualTaskRunTime
909 ).toBeGreaterThanOrEqual(0)
910 // We need to clean up the resources after our test
914 it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
915 const workerChoiceStrategy
= WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
916 let pool
= new FixedThreadPool(
918 './tests/worker-files/thread/testWorker.js'
921 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
923 ).currentWorkerNodeId
926 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
928 ).defaultWorkerWeight
931 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
933 ).workerVirtualTaskRunTime
935 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
)
937 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
938 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
939 ).currentWorkerNodeId
942 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
943 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
944 ).defaultWorkerWeight
947 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
949 ).workerVirtualTaskRunTime
952 pool
= new DynamicThreadPool(
955 './tests/worker-files/thread/testWorker.js'
958 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
960 ).currentWorkerNodeId
963 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
965 ).defaultWorkerWeight
968 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
970 ).workerVirtualTaskRunTime
972 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
)
974 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
975 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
976 ).currentWorkerNodeId
979 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
980 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
981 ).defaultWorkerWeight
984 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
986 ).workerVirtualTaskRunTime
988 // We need to clean up the resources after our test
992 it('Verify unknown strategy throw error', () => {
995 new DynamicThreadPool(
998 './tests/worker-files/thread/testWorker.js',
999 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
1001 ).toThrowError("Invalid worker choice strategy 'UNKNOWN_STRATEGY'")