1 import { randomInt } from 'node:crypto'
3 import { expect } from 'expect'
5 import { CircularArray } from '../../../lib/circular-array.cjs'
11 WorkerChoiceStrategies
12 } from '../../../lib/index.cjs'
14 describe('Selection strategies test suite', () => {
18 it('Verify that WorkerChoiceStrategies enumeration provides string values', () => {
19 expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN')
20 expect(WorkerChoiceStrategies.LEAST_USED).toBe('LEAST_USED')
21 expect(WorkerChoiceStrategies.LEAST_BUSY).toBe('LEAST_BUSY')
22 expect(WorkerChoiceStrategies.LEAST_ELU).toBe('LEAST_ELU')
23 expect(WorkerChoiceStrategies.FAIR_SHARE).toBe('FAIR_SHARE')
24 expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe(
25 'WEIGHTED_ROUND_ROBIN'
27 expect(WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN).toBe(
28 'INTERLEAVED_WEIGHTED_ROUND_ROBIN'
32 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
33 const pool = new DynamicThreadPool(
36 './tests/worker-files/thread/testWorker.mjs'
38 expect(pool.opts.workerChoiceStrategy).toBe(
39 WorkerChoiceStrategies.ROUND_ROBIN
41 expect(pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy).toBe(
42 WorkerChoiceStrategies.ROUND_ROBIN
44 // We need to clean up the resources after our test
48 it('Verify available strategies are taken at pool creation', async () => {
49 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
50 const pool = new FixedThreadPool(
52 './tests/worker-files/thread/testWorker.mjs',
53 { workerChoiceStrategy }
55 expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy)
57 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
58 ).toBe(workerChoiceStrategy)
63 it('Verify available strategies can be set after pool creation', async () => {
64 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
65 const pool = new DynamicThreadPool(
68 './tests/worker-files/thread/testWorker.mjs'
70 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
71 expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy)
73 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
74 ).toBe(workerChoiceStrategy)
77 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
78 const pool = new DynamicClusterPool(
81 './tests/worker-files/cluster/testWorker.cjs'
83 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
84 expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy)
86 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
87 ).toBe(workerChoiceStrategy)
92 it('Verify available strategies default internals at pool creation', async () => {
93 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
94 const pool = new FixedThreadPool(
96 './tests/worker-files/thread/testWorker.mjs',
97 { workerChoiceStrategy }
100 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
105 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
107 ).previousWorkerNodeKey
110 workerChoiceStrategy === WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
113 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
115 ).workerNodeVirtualTaskRunTime
118 workerChoiceStrategy ===
119 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
122 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
124 ).workerNodeVirtualTaskRunTime
127 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
132 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
137 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
139 ).roundWeights.length
142 Number.isSafeInteger(
143 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
153 it('Verify ROUND_ROBIN strategy default policy', async () => {
154 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
155 let pool = new FixedThreadPool(
157 './tests/worker-files/thread/testWorker.mjs',
158 { workerChoiceStrategy }
160 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
161 dynamicWorkerUsage: false,
162 dynamicWorkerReady: true
165 pool = new DynamicThreadPool(
168 './tests/worker-files/thread/testWorker.mjs',
169 { workerChoiceStrategy }
171 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
172 dynamicWorkerUsage: false,
173 dynamicWorkerReady: true
175 // We need to clean up the resources after our test
179 it('Verify ROUND_ROBIN strategy default tasks statistics requirements', async () => {
180 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
181 let pool = new FixedThreadPool(
183 './tests/worker-files/thread/testWorker.mjs',
184 { workerChoiceStrategy }
187 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
206 pool = new DynamicThreadPool(
209 './tests/worker-files/thread/testWorker.mjs',
210 { workerChoiceStrategy }
213 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
231 // We need to clean up the resources after our test
235 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
236 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
237 const pool = new FixedThreadPool(
239 './tests/worker-files/thread/testWorker.mjs',
240 { workerChoiceStrategy }
242 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
243 const promises = new Set()
244 const maxMultiplier = 2
245 for (let i = 0; i < max * maxMultiplier; i++) {
246 promises.add(pool.execute())
248 await Promise.all(promises)
249 for (const workerNode of pool.workerNodes) {
250 expect(workerNode.usage).toStrictEqual({
252 executed: maxMultiplier,
256 sequentiallyStolen: 0,
261 history: new CircularArray()
264 history: new CircularArray()
268 history: new CircularArray()
271 history: new CircularArray()
277 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
278 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
282 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
283 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
284 ).previousWorkerNodeKey
285 ).toBe(pool.workerNodes.length - 1)
286 // We need to clean up the resources after our test
290 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
291 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
292 const pool = new DynamicThreadPool(
295 './tests/worker-files/thread/testWorker.mjs',
296 { workerChoiceStrategy }
298 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
299 const promises = new Set()
300 const maxMultiplier = 2
301 for (let i = 0; i < max * maxMultiplier; i++) {
302 promises.add(pool.execute())
304 await Promise.all(promises)
305 for (const workerNode of pool.workerNodes) {
306 expect(workerNode.usage).toStrictEqual({
308 executed: expect.any(Number),
312 sequentiallyStolen: 0,
317 history: new CircularArray()
320 history: new CircularArray()
324 history: new CircularArray()
327 history: new CircularArray()
331 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
332 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
337 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
338 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
342 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
343 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
344 ).previousWorkerNodeKey
345 ).toBe(pool.workerNodes.length - 1)
346 // We need to clean up the resources after our test
350 it('Verify ROUND_ROBIN strategy runtime behavior', async () => {
351 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
352 let pool = new FixedClusterPool(
354 './tests/worker-files/cluster/testWorker.cjs',
355 { workerChoiceStrategy }
357 let results = new Set()
358 for (let i = 0; i < max; i++) {
359 results.add(pool.workerNodes[pool.chooseWorkerNode()].info.id)
361 expect(results.size).toBe(max)
363 pool = new FixedThreadPool(
365 './tests/worker-files/thread/testWorker.mjs',
366 { workerChoiceStrategy }
369 for (let i = 0; i < max; i++) {
370 results.add(pool.workerNodes[pool.chooseWorkerNode()].info.id)
372 expect(results.size).toBe(max)
376 it("Verify ROUND_ROBIN strategy internals aren't reset after setting it", async () => {
377 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
378 let pool = new FixedThreadPool(
380 './tests/worker-files/thread/testWorker.mjs',
381 { workerChoiceStrategy }
383 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
384 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
385 ).nextWorkerNodeKey = randomInt(1, max - 1)
386 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
387 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
388 ).previousWorkerNodeKey = randomInt(1, max - 1)
389 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
391 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
392 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
396 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
397 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
398 ).previousWorkerNodeKey
401 pool = new DynamicThreadPool(
404 './tests/worker-files/thread/testWorker.mjs',
405 { workerChoiceStrategy }
407 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
408 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
409 ).nextWorkerNodeKey = randomInt(1, max - 1)
410 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
411 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
412 ).previousWorkerNodeKey = randomInt(1, max - 1)
413 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
415 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
416 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
420 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
421 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
422 ).previousWorkerNodeKey
424 // We need to clean up the resources after our test
428 it('Verify LEAST_USED strategy default policy', async () => {
429 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_USED
430 let pool = new FixedThreadPool(
432 './tests/worker-files/thread/testWorker.mjs',
433 { workerChoiceStrategy }
435 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
436 dynamicWorkerUsage: false,
437 dynamicWorkerReady: true
440 pool = new DynamicThreadPool(
443 './tests/worker-files/thread/testWorker.mjs',
444 { workerChoiceStrategy }
446 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
447 dynamicWorkerUsage: false,
448 dynamicWorkerReady: true
450 // We need to clean up the resources after our test
454 it('Verify LEAST_USED strategy default tasks statistics requirements', async () => {
455 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_USED
456 let pool = new FixedThreadPool(
458 './tests/worker-files/thread/testWorker.mjs',
459 { workerChoiceStrategy }
462 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
481 pool = new DynamicThreadPool(
484 './tests/worker-files/thread/testWorker.mjs',
485 { workerChoiceStrategy }
488 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
506 // We need to clean up the resources after our test
510 it('Verify LEAST_USED strategy can be run in a fixed pool', async () => {
511 const pool = new FixedThreadPool(
513 './tests/worker-files/thread/testWorker.mjs',
514 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED }
516 // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose`
517 const promises = new Set()
518 const maxMultiplier = 2
519 for (let i = 0; i < max * maxMultiplier; i++) {
520 promises.add(pool.execute())
522 await Promise.all(promises)
523 for (const workerNode of pool.workerNodes) {
524 expect(workerNode.usage).toStrictEqual({
526 executed: expect.any(Number),
530 sequentiallyStolen: 0,
535 history: new CircularArray()
538 history: new CircularArray()
542 history: new CircularArray()
545 history: new CircularArray()
549 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
550 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
555 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
556 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
558 ).toEqual(expect.any(Number))
560 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
561 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
562 ).previousWorkerNodeKey
563 ).toEqual(expect.any(Number))
564 // We need to clean up the resources after our test
568 it('Verify LEAST_USED strategy can be run in a dynamic pool', async () => {
569 const pool = new DynamicThreadPool(
572 './tests/worker-files/thread/testWorker.mjs',
573 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED }
575 // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose`
576 const promises = new Set()
577 const maxMultiplier = 2
578 for (let i = 0; i < max * maxMultiplier; i++) {
579 promises.add(pool.execute())
581 await Promise.all(promises)
582 for (const workerNode of pool.workerNodes) {
583 expect(workerNode.usage).toStrictEqual({
585 executed: expect.any(Number),
589 sequentiallyStolen: 0,
594 history: new CircularArray()
597 history: new CircularArray()
601 history: new CircularArray()
604 history: new CircularArray()
608 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
609 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
614 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
615 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
617 ).toEqual(expect.any(Number))
619 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
620 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
621 ).previousWorkerNodeKey
622 ).toEqual(expect.any(Number))
623 // We need to clean up the resources after our test
627 it('Verify LEAST_BUSY strategy default policy', async () => {
628 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_BUSY
629 let pool = new FixedThreadPool(
631 './tests/worker-files/thread/testWorker.mjs',
632 { workerChoiceStrategy }
634 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
635 dynamicWorkerUsage: false,
636 dynamicWorkerReady: true
639 pool = new DynamicThreadPool(
642 './tests/worker-files/thread/testWorker.mjs',
643 { workerChoiceStrategy }
645 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
646 dynamicWorkerUsage: false,
647 dynamicWorkerReady: true
649 // We need to clean up the resources after our test
653 it('Verify LEAST_BUSY strategy default tasks statistics requirements', async () => {
654 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_BUSY
655 let pool = new FixedThreadPool(
657 './tests/worker-files/thread/testWorker.mjs',
658 { workerChoiceStrategy }
661 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
680 pool = new DynamicThreadPool(
683 './tests/worker-files/thread/testWorker.mjs',
684 { workerChoiceStrategy }
687 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
705 // We need to clean up the resources after our test
709 it('Verify LEAST_BUSY strategy can be run in a fixed pool', async () => {
710 const pool = new FixedThreadPool(
712 './tests/worker-files/thread/testWorker.mjs',
713 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY }
715 // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose`
716 const promises = new Set()
717 const maxMultiplier = 2
718 for (let i = 0; i < max * maxMultiplier; i++) {
719 promises.add(pool.execute())
721 await Promise.all(promises)
722 for (const workerNode of pool.workerNodes) {
723 expect(workerNode.usage).toStrictEqual({
725 executed: expect.any(Number),
729 sequentiallyStolen: 0,
733 runTime: expect.objectContaining({
734 history: expect.any(CircularArray)
736 waitTime: expect.objectContaining({
737 history: expect.any(CircularArray)
741 history: new CircularArray()
744 history: new CircularArray()
748 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
749 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
752 if (workerNode.usage.runTime.aggregate == null) {
753 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
755 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
757 if (workerNode.usage.waitTime.aggregate == null) {
758 expect(workerNode.usage.waitTime.aggregate).toBeUndefined()
760 expect(workerNode.usage.waitTime.aggregate).toBeGreaterThan(0)
764 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
765 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
767 ).toEqual(expect.any(Number))
769 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
770 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
771 ).previousWorkerNodeKey
772 ).toEqual(expect.any(Number))
773 // We need to clean up the resources after our test
777 it('Verify LEAST_BUSY strategy can be run in a dynamic pool', async () => {
778 const pool = new DynamicThreadPool(
781 './tests/worker-files/thread/testWorker.mjs',
782 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY }
784 // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose`
785 const promises = new Set()
786 const maxMultiplier = 2
787 for (let i = 0; i < max * maxMultiplier; i++) {
788 promises.add(pool.execute())
790 await Promise.all(promises)
791 for (const workerNode of pool.workerNodes) {
792 expect(workerNode.usage).toStrictEqual({
794 executed: expect.any(Number),
798 sequentiallyStolen: 0,
802 runTime: expect.objectContaining({
803 history: expect.any(CircularArray)
805 waitTime: expect.objectContaining({
806 history: expect.any(CircularArray)
810 history: new CircularArray()
813 history: new CircularArray()
817 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
818 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
821 if (workerNode.usage.runTime.aggregate == null) {
822 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
824 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
826 if (workerNode.usage.waitTime.aggregate == null) {
827 expect(workerNode.usage.waitTime.aggregate).toBeUndefined()
829 expect(workerNode.usage.waitTime.aggregate).toBeGreaterThan(0)
833 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
834 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
836 ).toEqual(expect.any(Number))
838 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
839 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
840 ).previousWorkerNodeKey
841 ).toEqual(expect.any(Number))
842 // We need to clean up the resources after our test
846 it('Verify LEAST_ELU strategy default policy', async () => {
847 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_ELU
848 let pool = new FixedThreadPool(
850 './tests/worker-files/thread/testWorker.mjs',
851 { workerChoiceStrategy }
853 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
854 dynamicWorkerUsage: false,
855 dynamicWorkerReady: true
858 pool = new DynamicThreadPool(
861 './tests/worker-files/thread/testWorker.mjs',
862 { workerChoiceStrategy }
864 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
865 dynamicWorkerUsage: false,
866 dynamicWorkerReady: true
868 // We need to clean up the resources after our test
872 it('Verify LEAST_ELU strategy default tasks statistics requirements', async () => {
873 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_ELU
874 let pool = new FixedThreadPool(
876 './tests/worker-files/thread/testWorker.mjs',
877 { workerChoiceStrategy }
880 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
899 pool = new DynamicThreadPool(
902 './tests/worker-files/thread/testWorker.mjs',
903 { workerChoiceStrategy }
906 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
924 // We need to clean up the resources after our test
928 it('Verify LEAST_ELU strategy can be run in a fixed pool', async () => {
929 const pool = new FixedThreadPool(
931 './tests/worker-files/thread/testWorker.mjs',
932 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_ELU }
934 // TODO: Create a better test to cover `LeastEluWorkerChoiceStrategy#choose`
935 const promises = new Set()
936 const maxMultiplier = 2
937 for (let i = 0; i < max * maxMultiplier; i++) {
938 promises.add(pool.execute())
940 await Promise.all(promises)
941 for (const workerNode of pool.workerNodes) {
942 expect(workerNode.usage).toStrictEqual({
944 executed: expect.any(Number),
948 sequentiallyStolen: 0,
953 history: new CircularArray()
956 history: new CircularArray()
958 elu: expect.objectContaining({
959 idle: expect.objectContaining({
960 history: expect.any(CircularArray)
962 active: expect.objectContaining({
963 history: expect.any(CircularArray)
967 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
968 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
971 if (workerNode.usage.elu.active.aggregate == null) {
972 expect(workerNode.usage.elu.active.aggregate).toBeUndefined()
974 expect(workerNode.usage.elu.active.aggregate).toBeGreaterThan(0)
976 if (workerNode.usage.elu.idle.aggregate == null) {
977 expect(workerNode.usage.elu.idle.aggregate).toBeUndefined()
979 expect(workerNode.usage.elu.idle.aggregate).toBeGreaterThanOrEqual(0)
981 if (workerNode.usage.elu.utilization == null) {
982 expect(workerNode.usage.elu.utilization).toBeUndefined()
984 expect(workerNode.usage.elu.utilization).toBeGreaterThanOrEqual(0)
985 expect(workerNode.usage.elu.utilization).toBeLessThanOrEqual(1)
989 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
990 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
992 ).toEqual(expect.any(Number))
994 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
995 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
996 ).previousWorkerNodeKey
997 ).toEqual(expect.any(Number))
998 // We need to clean up the resources after our test
1002 it('Verify LEAST_ELU strategy can be run in a dynamic pool', async () => {
1003 const pool = new DynamicThreadPool(
1006 './tests/worker-files/thread/testWorker.mjs',
1007 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_ELU }
1009 // TODO: Create a better test to cover `LeastEluWorkerChoiceStrategy#choose`
1010 const promises = new Set()
1011 const maxMultiplier = 2
1012 for (let i = 0; i < max * maxMultiplier; i++) {
1013 promises.add(pool.execute())
1015 await Promise.all(promises)
1016 for (const workerNode of pool.workerNodes) {
1017 expect(workerNode.usage).toStrictEqual({
1019 executed: expect.any(Number),
1023 sequentiallyStolen: 0,
1028 history: new CircularArray()
1031 history: new CircularArray()
1033 elu: expect.objectContaining({
1034 idle: expect.objectContaining({
1035 history: expect.any(CircularArray)
1037 active: expect.objectContaining({
1038 history: expect.any(CircularArray)
1042 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1043 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
1046 if (workerNode.usage.elu.active.aggregate == null) {
1047 expect(workerNode.usage.elu.active.aggregate).toBeUndefined()
1049 expect(workerNode.usage.elu.active.aggregate).toBeGreaterThan(0)
1051 if (workerNode.usage.elu.idle.aggregate == null) {
1052 expect(workerNode.usage.elu.idle.aggregate).toBeUndefined()
1054 expect(workerNode.usage.elu.idle.aggregate).toBeGreaterThanOrEqual(0)
1056 if (workerNode.usage.elu.utilization == null) {
1057 expect(workerNode.usage.elu.utilization).toBeUndefined()
1059 expect(workerNode.usage.elu.utilization).toBeGreaterThanOrEqual(0)
1060 expect(workerNode.usage.elu.utilization).toBeLessThanOrEqual(1)
1064 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1065 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1067 ).toEqual(expect.any(Number))
1069 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1070 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1071 ).previousWorkerNodeKey
1072 ).toEqual(expect.any(Number))
1073 // We need to clean up the resources after our test
1074 await pool.destroy()
1077 it('Verify FAIR_SHARE strategy default policy', async () => {
1078 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
1079 let pool = new FixedThreadPool(
1081 './tests/worker-files/thread/testWorker.mjs',
1082 { workerChoiceStrategy }
1084 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
1085 dynamicWorkerUsage: false,
1086 dynamicWorkerReady: true
1088 await pool.destroy()
1089 pool = new DynamicThreadPool(
1092 './tests/worker-files/thread/testWorker.mjs',
1093 { workerChoiceStrategy }
1095 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
1096 dynamicWorkerUsage: false,
1097 dynamicWorkerReady: true
1099 // We need to clean up the resources after our test
1100 await pool.destroy()
1103 it('Verify FAIR_SHARE strategy default tasks statistics requirements', async () => {
1104 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
1105 let pool = new FixedThreadPool(
1107 './tests/worker-files/thread/testWorker.mjs',
1108 { workerChoiceStrategy }
1111 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
1129 await pool.destroy()
1130 pool = new DynamicThreadPool(
1133 './tests/worker-files/thread/testWorker.mjs',
1134 { workerChoiceStrategy }
1137 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
1155 // We need to clean up the resources after our test
1156 await pool.destroy()
1159 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
1160 const pool = new FixedThreadPool(
1162 './tests/worker-files/thread/testWorker.mjs',
1163 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
1165 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
1166 const promises = new Set()
1167 const maxMultiplier = 2
1168 for (let i = 0; i < max * maxMultiplier; i++) {
1169 promises.add(pool.execute())
1171 await Promise.all(promises)
1172 for (const workerNode of pool.workerNodes) {
1173 expect(workerNode.usage).toStrictEqual({
1175 executed: expect.any(Number),
1179 sequentiallyStolen: 0,
1183 runTime: expect.objectContaining({
1184 history: expect.any(CircularArray)
1187 history: new CircularArray()
1189 elu: expect.objectContaining({
1190 idle: expect.objectContaining({
1191 history: expect.any(CircularArray)
1193 active: expect.objectContaining({
1194 history: expect.any(CircularArray)
1198 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1199 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
1202 if (workerNode.usage.runTime.aggregate == null) {
1203 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1205 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1207 if (workerNode.usage.runTime.average == null) {
1208 expect(workerNode.usage.runTime.average).toBeUndefined()
1210 expect(workerNode.usage.runTime.average).toBeGreaterThan(0)
1212 if (workerNode.usage.elu.active.aggregate == null) {
1213 expect(workerNode.usage.elu.active.aggregate).toBeUndefined()
1215 expect(workerNode.usage.elu.active.aggregate).toBeGreaterThan(0)
1217 if (workerNode.usage.elu.idle.aggregate == null) {
1218 expect(workerNode.usage.elu.idle.aggregate).toBeUndefined()
1220 expect(workerNode.usage.elu.idle.aggregate).toBeGreaterThanOrEqual(0)
1222 if (workerNode.usage.elu.utilization == null) {
1223 expect(workerNode.usage.elu.utilization).toBeUndefined()
1225 expect(workerNode.usage.elu.utilization).toBeGreaterThanOrEqual(0)
1226 expect(workerNode.usage.elu.utilization).toBeLessThanOrEqual(1)
1228 expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeGreaterThan(0)
1231 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1232 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1234 ).toEqual(expect.any(Number))
1236 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1237 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1238 ).previousWorkerNodeKey
1239 ).toEqual(expect.any(Number))
1240 // We need to clean up the resources after our test
1241 await pool.destroy()
1244 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
1245 const pool = new DynamicThreadPool(
1248 './tests/worker-files/thread/testWorker.mjs',
1249 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
1251 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
1252 const promises = new Set()
1253 const maxMultiplier = 2
1254 for (let i = 0; i < max * maxMultiplier; i++) {
1255 promises.add(pool.execute())
1257 await Promise.all(promises)
1258 for (const workerNode of pool.workerNodes) {
1259 expect(workerNode.usage).toStrictEqual({
1261 executed: expect.any(Number),
1265 sequentiallyStolen: 0,
1269 runTime: expect.objectContaining({
1270 history: expect.any(CircularArray)
1273 history: new CircularArray()
1275 elu: expect.objectContaining({
1276 idle: expect.objectContaining({
1277 history: expect.any(CircularArray)
1279 active: expect.objectContaining({
1280 history: expect.any(CircularArray)
1284 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1285 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
1288 if (workerNode.usage.runTime.aggregate == null) {
1289 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1291 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1293 if (workerNode.usage.runTime.average == null) {
1294 expect(workerNode.usage.runTime.average).toBeUndefined()
1296 expect(workerNode.usage.runTime.average).toBeGreaterThan(0)
1298 if (workerNode.usage.elu.active.aggregate == null) {
1299 expect(workerNode.usage.elu.active.aggregate).toBeUndefined()
1301 expect(workerNode.usage.elu.active.aggregate).toBeGreaterThan(0)
1303 if (workerNode.usage.elu.idle.aggregate == null) {
1304 expect(workerNode.usage.elu.idle.aggregate).toBeUndefined()
1306 expect(workerNode.usage.elu.idle.aggregate).toBeGreaterThanOrEqual(0)
1308 if (workerNode.usage.elu.utilization == null) {
1309 expect(workerNode.usage.elu.utilization).toBeUndefined()
1311 expect(workerNode.usage.elu.utilization).toBeGreaterThanOrEqual(0)
1312 expect(workerNode.usage.elu.utilization).toBeLessThanOrEqual(1)
1314 expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeGreaterThan(0)
1317 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1318 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1320 ).toEqual(expect.any(Number))
1322 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1323 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1324 ).previousWorkerNodeKey
1325 ).toEqual(expect.any(Number))
1326 // We need to clean up the resources after our test
1327 await pool.destroy()
1330 it('Verify FAIR_SHARE strategy can be run in a dynamic pool with median runtime statistic', async () => {
1331 const pool = new DynamicThreadPool(
1334 './tests/worker-files/thread/testWorker.mjs',
1336 workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE,
1337 workerChoiceStrategyOptions: {
1338 runTime: { median: true }
1342 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
1343 const promises = new Set()
1344 const maxMultiplier = 2
1345 for (let i = 0; i < max * maxMultiplier; i++) {
1346 promises.add(pool.execute())
1348 await Promise.all(promises)
1349 for (const workerNode of pool.workerNodes) {
1350 expect(workerNode.usage).toStrictEqual({
1352 executed: expect.any(Number),
1356 sequentiallyStolen: 0,
1360 runTime: expect.objectContaining({
1361 history: expect.any(CircularArray)
1364 history: new CircularArray()
1366 elu: expect.objectContaining({
1367 idle: expect.objectContaining({
1368 history: expect.any(CircularArray)
1370 active: expect.objectContaining({
1371 history: expect.any(CircularArray)
1375 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1376 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
1379 if (workerNode.usage.runTime.aggregate == null) {
1380 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1382 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1384 if (workerNode.usage.runTime.median == null) {
1385 expect(workerNode.usage.runTime.median).toBeUndefined()
1387 expect(workerNode.usage.runTime.median).toBeGreaterThan(0)
1389 if (workerNode.usage.elu.active.aggregate == null) {
1390 expect(workerNode.usage.elu.active.aggregate).toBeUndefined()
1392 expect(workerNode.usage.elu.active.aggregate).toBeGreaterThan(0)
1394 if (workerNode.usage.elu.idle.aggregate == null) {
1395 expect(workerNode.usage.elu.idle.aggregate).toBeUndefined()
1397 expect(workerNode.usage.elu.idle.aggregate).toBeGreaterThanOrEqual(0)
1399 if (workerNode.usage.elu.utilization == null) {
1400 expect(workerNode.usage.elu.utilization).toBeUndefined()
1402 expect(workerNode.usage.elu.utilization).toBeGreaterThanOrEqual(0)
1403 expect(workerNode.usage.elu.utilization).toBeLessThanOrEqual(1)
1405 expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeGreaterThan(0)
1408 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1409 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1411 ).toEqual(expect.any(Number))
1413 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1414 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1415 ).previousWorkerNodeKey
1416 ).toEqual(expect.any(Number))
1417 // We need to clean up the resources after our test
1418 await pool.destroy()
1421 it("Verify FAIR_SHARE strategy internals aren't reset after setting it", async () => {
1422 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
1423 let pool = new FixedThreadPool(
1425 './tests/worker-files/thread/testWorker.mjs'
1427 for (const workerNode of pool.workerNodes) {
1428 workerNode.strategyData = {
1429 virtualTaskEndTimestamp: performance.now()
1432 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
1433 for (const workerNode of pool.workerNodes) {
1434 expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeGreaterThan(0)
1436 await pool.destroy()
1437 pool = new DynamicThreadPool(
1440 './tests/worker-files/thread/testWorker.mjs'
1442 for (const workerNode of pool.workerNodes) {
1443 workerNode.strategyData = {
1444 virtualTaskEndTimestamp: performance.now()
1447 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
1448 for (const workerNode of pool.workerNodes) {
1449 expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeGreaterThan(0)
1451 // We need to clean up the resources after our test
1452 await pool.destroy()
1455 it('Verify WEIGHTED_ROUND_ROBIN strategy default policy', async () => {
1456 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
1457 let pool = new FixedThreadPool(
1459 './tests/worker-files/thread/testWorker.mjs',
1460 { workerChoiceStrategy }
1462 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
1463 dynamicWorkerUsage: false,
1464 dynamicWorkerReady: true
1466 await pool.destroy()
1467 pool = new DynamicThreadPool(
1470 './tests/worker-files/thread/testWorker.mjs',
1471 { workerChoiceStrategy }
1473 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
1474 dynamicWorkerUsage: false,
1475 dynamicWorkerReady: true
1477 // We need to clean up the resources after our test
1478 await pool.destroy()
1481 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks statistics requirements', async () => {
1482 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
1483 let pool = new FixedThreadPool(
1485 './tests/worker-files/thread/testWorker.mjs',
1486 { workerChoiceStrategy }
1489 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
1507 await pool.destroy()
1508 pool = new DynamicThreadPool(
1511 './tests/worker-files/thread/testWorker.mjs',
1512 { workerChoiceStrategy }
1515 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
1533 // We need to clean up the resources after our test
1534 await pool.destroy()
1537 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
1538 const pool = new FixedThreadPool(
1540 './tests/worker-files/thread/testWorker.mjs',
1541 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
1543 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
1544 const promises = new Set()
1545 const maxMultiplier = 2
1546 for (let i = 0; i < max * maxMultiplier; i++) {
1547 promises.add(pool.execute())
1549 await Promise.all(promises)
1550 for (const workerNode of pool.workerNodes) {
1551 expect(workerNode.usage).toStrictEqual({
1553 executed: expect.any(Number),
1557 sequentiallyStolen: 0,
1561 runTime: expect.objectContaining({
1562 history: expect.any(CircularArray)
1565 history: new CircularArray()
1569 history: new CircularArray()
1572 history: new CircularArray()
1576 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1577 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
1580 if (workerNode.usage.runTime.aggregate == null) {
1581 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1583 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1585 if (workerNode.usage.runTime.average == null) {
1586 expect(workerNode.usage.runTime.average).toBeUndefined()
1588 expect(workerNode.usage.runTime.average).toBeGreaterThan(0)
1592 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1593 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1597 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1598 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1599 ).previousWorkerNodeKey
1602 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1603 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1604 ).workerNodeVirtualTaskRunTime
1605 ).toBeGreaterThanOrEqual(0)
1606 // We need to clean up the resources after our test
1607 await pool.destroy()
1610 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
1611 const pool = new DynamicThreadPool(
1614 './tests/worker-files/thread/testWorker.mjs',
1615 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
1617 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
1618 const promises = new Set()
1619 const maxMultiplier = 2
1620 for (let i = 0; i < max * maxMultiplier; i++) {
1621 promises.add(pool.execute())
1623 await Promise.all(promises)
1624 for (const workerNode of pool.workerNodes) {
1625 expect(workerNode.usage).toStrictEqual({
1627 executed: expect.any(Number),
1631 sequentiallyStolen: 0,
1635 runTime: expect.objectContaining({
1636 history: expect.any(CircularArray)
1639 history: new CircularArray()
1643 history: new CircularArray()
1646 history: new CircularArray()
1650 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1651 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
1654 if (workerNode.usage.runTime.aggregate == null) {
1655 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1657 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1659 if (workerNode.usage.runTime.average == null) {
1660 expect(workerNode.usage.runTime.average).toBeUndefined()
1662 expect(workerNode.usage.runTime.average).toBeGreaterThan(0)
1666 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1667 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1671 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1672 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1673 ).previousWorkerNodeKey
1676 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1677 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1678 ).workerNodeVirtualTaskRunTime
1679 ).toBeGreaterThanOrEqual(0)
1680 // We need to clean up the resources after our test
1681 await pool.destroy()
1684 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool with median runtime statistic', async () => {
1685 const pool = new DynamicThreadPool(
1688 './tests/worker-files/thread/testWorker.mjs',
1690 workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
1691 workerChoiceStrategyOptions: {
1692 runTime: { median: true }
1696 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
1697 const promises = new Set()
1698 const maxMultiplier = 2
1699 for (let i = 0; i < max * maxMultiplier; i++) {
1700 promises.add(pool.execute())
1702 await Promise.all(promises)
1703 for (const workerNode of pool.workerNodes) {
1704 expect(workerNode.usage).toStrictEqual({
1706 executed: expect.any(Number),
1710 sequentiallyStolen: 0,
1714 runTime: expect.objectContaining({
1715 history: expect.any(CircularArray)
1718 history: new CircularArray()
1722 history: new CircularArray()
1725 history: new CircularArray()
1729 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1730 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
1733 if (workerNode.usage.runTime.aggregate == null) {
1734 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1736 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1738 if (workerNode.usage.runTime.median == null) {
1739 expect(workerNode.usage.runTime.median).toBeUndefined()
1741 expect(workerNode.usage.runTime.median).toBeGreaterThan(0)
1745 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1746 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1750 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1751 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1752 ).previousWorkerNodeKey
1755 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1756 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1757 ).workerNodeVirtualTaskRunTime
1758 ).toBeGreaterThanOrEqual(0)
1759 // We need to clean up the resources after our test
1760 await pool.destroy()
1763 it("Verify WEIGHTED_ROUND_ROBIN strategy internals aren't reset after setting it", async () => {
1764 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
1765 let pool = new FixedThreadPool(
1767 './tests/worker-files/thread/testWorker.mjs',
1768 { workerChoiceStrategy }
1770 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1771 workerChoiceStrategy
1772 ).nextWorkerNodeKey = randomInt(1, max - 1)
1773 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1774 workerChoiceStrategy
1775 ).previousWorkerNodeKey = randomInt(1, max - 1)
1776 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1777 workerChoiceStrategy
1778 ).workerNodeVirtualTaskRunTime = randomInt(100, 1000)
1779 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
1781 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1782 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1784 ).toBeGreaterThan(0)
1786 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1787 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1788 ).previousWorkerNodeKey
1789 ).toBeGreaterThan(0)
1791 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1792 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1793 ).workerNodeVirtualTaskRunTime
1794 ).toBeGreaterThan(99)
1795 await pool.destroy()
1796 pool = new DynamicThreadPool(
1799 './tests/worker-files/thread/testWorker.mjs',
1800 { workerChoiceStrategy }
1802 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1803 workerChoiceStrategy
1804 ).nextWorkerNodeKey = randomInt(1, max - 1)
1805 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1806 workerChoiceStrategy
1807 ).previousWorkerNodeKey = randomInt(1, max - 1)
1808 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1809 workerChoiceStrategy
1810 ).workerNodeVirtualTaskRunTime = randomInt(100, 1000)
1811 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
1813 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1814 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1816 ).toBeGreaterThan(0)
1818 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1819 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1820 ).previousWorkerNodeKey
1821 ).toBeGreaterThan(0)
1823 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1824 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1825 ).workerNodeVirtualTaskRunTime
1826 ).toBeGreaterThan(99)
1827 // We need to clean up the resources after our test
1828 await pool.destroy()
1831 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy default policy', async () => {
1832 const workerChoiceStrategy =
1833 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1834 let pool = new FixedThreadPool(
1836 './tests/worker-files/thread/testWorker.mjs',
1837 { workerChoiceStrategy }
1839 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
1840 dynamicWorkerUsage: false,
1841 dynamicWorkerReady: true
1843 await pool.destroy()
1844 pool = new DynamicThreadPool(
1847 './tests/worker-files/thread/testWorker.mjs',
1848 { workerChoiceStrategy }
1850 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
1851 dynamicWorkerUsage: false,
1852 dynamicWorkerReady: true
1854 // We need to clean up the resources after our test
1855 await pool.destroy()
1858 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy default tasks statistics requirements', async () => {
1859 const workerChoiceStrategy =
1860 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1861 let pool = new FixedThreadPool(
1863 './tests/worker-files/thread/testWorker.mjs',
1864 { workerChoiceStrategy }
1867 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
1885 await pool.destroy()
1886 pool = new DynamicThreadPool(
1889 './tests/worker-files/thread/testWorker.mjs',
1890 { workerChoiceStrategy }
1893 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
1911 // We need to clean up the resources after our test
1912 await pool.destroy()
1915 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
1916 const pool = new FixedThreadPool(
1918 './tests/worker-files/thread/testWorker.mjs',
1920 workerChoiceStrategy:
1921 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1924 // TODO: Create a better test to cover `InterleavedWeightedRoundRobinWorkerChoiceStrategy#choose`
1925 const promises = new Set()
1926 const maxMultiplier = 2
1927 for (let i = 0; i < max * maxMultiplier; i++) {
1928 promises.add(pool.execute())
1930 await Promise.all(promises)
1931 for (const workerNode of pool.workerNodes) {
1932 expect(workerNode.usage).toStrictEqual({
1934 executed: expect.any(Number),
1938 sequentiallyStolen: 0,
1942 runTime: expect.objectContaining({
1943 history: expect.any(CircularArray)
1946 history: new CircularArray()
1950 history: new CircularArray()
1953 history: new CircularArray()
1957 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1958 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
1963 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1964 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1968 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1969 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1973 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1974 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1978 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1979 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1980 ).previousWorkerNodeKey
1983 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1984 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1985 ).roundWeights.length
1988 Number.isSafeInteger(
1989 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1990 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1994 // We need to clean up the resources after our test
1995 await pool.destroy()
1998 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
1999 const pool = new DynamicThreadPool(
2002 './tests/worker-files/thread/testWorker.mjs',
2004 workerChoiceStrategy:
2005 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
2008 // TODO: Create a better test to cover `InterleavedWeightedRoundRobinWorkerChoiceStrategy#choose`
2009 const promises = new Set()
2010 const maxMultiplier = 2
2011 for (let i = 0; i < max * maxMultiplier; i++) {
2012 promises.add(pool.execute())
2014 await Promise.all(promises)
2015 for (const workerNode of pool.workerNodes) {
2016 expect(workerNode.usage).toStrictEqual({
2018 executed: expect.any(Number),
2022 sequentiallyStolen: 0,
2026 runTime: expect.objectContaining({
2027 history: expect.any(CircularArray)
2030 history: new CircularArray()
2034 history: new CircularArray()
2037 history: new CircularArray()
2041 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
2042 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
2047 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2048 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2052 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2053 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2057 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2058 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2062 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2063 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2064 ).previousWorkerNodeKey
2067 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2068 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2069 ).roundWeights.length
2072 Number.isSafeInteger(
2073 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2074 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2078 // We need to clean up the resources after our test
2079 await pool.destroy()
2082 it("Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy internals aren't resets after setting it", async () => {
2083 const workerChoiceStrategy =
2084 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
2085 let pool = new FixedThreadPool(
2087 './tests/worker-files/thread/testWorker.mjs',
2088 { workerChoiceStrategy }
2090 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2091 workerChoiceStrategy
2092 ).roundId = randomInt(1, max - 1)
2093 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2094 workerChoiceStrategy
2095 ).workerNodeId = randomInt(1, max - 1)
2096 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2097 workerChoiceStrategy
2098 ).nextWorkerNodeKey = randomInt(1, max - 1)
2099 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2100 workerChoiceStrategy
2101 ).previousWorkerNodeKey = randomInt(1, max - 1)
2102 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2103 workerChoiceStrategy
2104 ).roundWeights = [randomInt(1, max - 1), randomInt(1, max - 1)]
2105 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
2107 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2108 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2110 ).toBeGreaterThan(0)
2112 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2113 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2115 ).toBeGreaterThan(0)
2117 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2118 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2120 ).toBeGreaterThan(0)
2122 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2123 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2124 ).previousWorkerNodeKey
2125 ).toBeGreaterThan(0)
2127 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2128 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2129 ).roundWeights.length
2130 ).toBeGreaterThan(1)
2132 Number.isSafeInteger(
2133 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2134 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2138 await pool.destroy()
2139 pool = new DynamicThreadPool(
2142 './tests/worker-files/thread/testWorker.mjs',
2143 { workerChoiceStrategy }
2145 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2146 workerChoiceStrategy
2147 ).roundId = randomInt(1, max - 1)
2148 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2149 workerChoiceStrategy
2150 ).workerNodeId = randomInt(1, max - 1)
2151 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2152 workerChoiceStrategy
2153 ).nextWorkerNodeKey = randomInt(1, max - 1)
2154 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2155 workerChoiceStrategy
2156 ).previousWorkerNodeKey = randomInt(1, max - 1)
2157 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2158 workerChoiceStrategy
2159 ).roundWeights = [randomInt(1, max - 1), randomInt(1, max - 1)]
2160 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
2162 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2163 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2165 ).toBeGreaterThan(0)
2167 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2168 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2170 ).toBeGreaterThan(0)
2172 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2173 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2175 ).toBeGreaterThan(0)
2177 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2178 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2179 ).previousWorkerNodeKey
2180 ).toBeGreaterThan(0)
2182 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2183 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2184 ).roundWeights.length
2185 ).toBeGreaterThan(1)
2187 Number.isSafeInteger(
2188 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2189 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2193 // We need to clean up the resources after our test
2194 await pool.destroy()
2197 it('Verify unknown strategy throw error', () => {
2200 new DynamicThreadPool(
2203 './tests/worker-files/thread/testWorker.mjs',
2204 { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
2206 ).toThrow("Invalid worker choice strategy 'UNKNOWN_STRATEGY'")