1 const { expect
} = require('expect')
8 } = require('../../../lib')
9 const { CircularArray
} = require('../../../lib/circular-array')
11 describe('Selection strategies test suite', () => {
15 it('Verify that WorkerChoiceStrategies enumeration provides string values', () => {
16 expect(WorkerChoiceStrategies
.ROUND_ROBIN
).toBe('ROUND_ROBIN')
17 expect(WorkerChoiceStrategies
.LEAST_USED
).toBe('LEAST_USED')
18 expect(WorkerChoiceStrategies
.LEAST_BUSY
).toBe('LEAST_BUSY')
19 expect(WorkerChoiceStrategies
.LEAST_ELU
).toBe('LEAST_ELU')
20 expect(WorkerChoiceStrategies
.FAIR_SHARE
).toBe('FAIR_SHARE')
21 expect(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
).toBe(
22 'WEIGHTED_ROUND_ROBIN'
24 expect(WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
).toBe(
25 'INTERLEAVED_WEIGHTED_ROUND_ROBIN'
29 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
30 const pool
= new DynamicThreadPool(
33 './tests/worker-files/thread/testWorker.js'
35 expect(pool
.opts
.workerChoiceStrategy
).toBe(
36 WorkerChoiceStrategies
.ROUND_ROBIN
38 // We need to clean up the resources after our test
42 it('Verify available strategies are taken at pool creation', async () => {
43 for (const workerChoiceStrategy
of Object
.values(WorkerChoiceStrategies
)) {
44 const pool
= new FixedThreadPool(
46 './tests/worker-files/thread/testWorker.js',
47 { workerChoiceStrategy
}
49 expect(pool
.opts
.workerChoiceStrategy
).toBe(workerChoiceStrategy
)
50 expect(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
57 it('Verify available strategies can be set after pool creation', async () => {
58 for (const workerChoiceStrategy
of Object
.values(WorkerChoiceStrategies
)) {
59 const pool
= new DynamicThreadPool(
62 './tests/worker-files/thread/testWorker.js'
64 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
)
65 expect(pool
.opts
.workerChoiceStrategy
).toBe(workerChoiceStrategy
)
66 expect(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
69 expect(pool
.opts
.workerChoiceStrategyOptions
).toStrictEqual({
71 runTime
: { median
: false },
72 waitTime
: { median
: false },
73 elu
: { median
: false }
75 expect(pool
.workerChoiceStrategyContext
.opts
).toStrictEqual({
77 runTime
: { median
: false },
78 waitTime
: { median
: false },
79 elu
: { median
: false }
83 for (const workerChoiceStrategy
of Object
.values(WorkerChoiceStrategies
)) {
84 const pool
= new DynamicClusterPool(
87 './tests/worker-files/cluster/testWorker.js'
89 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
, { retries
: 3 })
90 expect(pool
.opts
.workerChoiceStrategy
).toBe(workerChoiceStrategy
)
91 expect(pool
.workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
94 expect(pool
.opts
.workerChoiceStrategyOptions
).toStrictEqual({
96 runTime
: { median
: false },
97 waitTime
: { median
: false },
98 elu
: { median
: false }
100 expect(pool
.workerChoiceStrategyContext
.opts
).toStrictEqual({
102 runTime
: { median
: false },
103 waitTime
: { median
: false },
104 elu
: { median
: false }
110 it('Verify available strategies default internals at pool creation', async () => {
111 const pool
= new FixedThreadPool(
113 './tests/worker-files/thread/testWorker.js'
115 for (const workerChoiceStrategy
of Object
.values(WorkerChoiceStrategies
)) {
116 if (workerChoiceStrategy
=== WorkerChoiceStrategies
.ROUND_ROBIN
) {
118 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
122 } else if (workerChoiceStrategy
=== WorkerChoiceStrategies
.FAIR_SHARE
) {
124 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
126 ).workersVirtualTaskEndTimestamp
127 ).toBeInstanceOf(Array
)
129 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
131 ).workersVirtualTaskEndTimestamp
.length
134 workerChoiceStrategy
=== WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
137 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
142 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
144 ).defaultWorkerWeight
147 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
149 ).workerVirtualTaskRunTime
156 it('Verify ROUND_ROBIN strategy default policy', async () => {
157 const workerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
158 let pool
= new FixedThreadPool(
160 './tests/worker-files/thread/testWorker.js',
161 { workerChoiceStrategy
}
163 expect(pool
.workerChoiceStrategyContext
.getStrategyPolicy()).toStrictEqual({
164 dynamicWorkerUsage
: false,
165 dynamicWorkerReady
: true
168 pool
= new DynamicThreadPool(
171 './tests/worker-files/thread/testWorker.js',
172 { workerChoiceStrategy
}
174 expect(pool
.workerChoiceStrategyContext
.getStrategyPolicy()).toStrictEqual({
175 dynamicWorkerUsage
: false,
176 dynamicWorkerReady
: true
178 // We need to clean up the resources after our test
182 it('Verify ROUND_ROBIN strategy default tasks statistics requirements', async () => {
183 const workerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
184 let pool
= new FixedThreadPool(
186 './tests/worker-files/thread/testWorker.js',
187 { workerChoiceStrategy
}
190 pool
.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
209 pool
= new DynamicThreadPool(
212 './tests/worker-files/thread/testWorker.js',
213 { workerChoiceStrategy
}
216 pool
.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
234 // We need to clean up the resources after our test
238 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
239 const pool
= new FixedThreadPool(
241 './tests/worker-files/thread/testWorker.js',
242 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
244 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
245 const promises
= new Set()
246 const maxMultiplier
= 2
247 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
248 promises
.add(pool
.execute())
250 await Promise
.all(promises
)
251 for (const workerNode
of pool
.workerNodes
) {
252 expect(workerNode
.usage
).toStrictEqual({
254 executed
: maxMultiplier
,
262 history
: new CircularArray()
265 history
: new CircularArray()
269 history
: new CircularArray()
272 history
: new CircularArray()
278 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
279 WorkerChoiceStrategies
.ROUND_ROBIN
282 // We need to clean up the resources after our test
286 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
287 const pool
= new DynamicThreadPool(
290 './tests/worker-files/thread/testWorker.js',
291 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
293 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
294 const promises
= new Set()
295 const maxMultiplier
= 2
296 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
297 promises
.add(pool
.execute())
299 await Promise
.all(promises
)
300 for (const workerNode
of pool
.workerNodes
) {
301 expect(workerNode
.usage
).toStrictEqual({
303 executed
: expect
.any(Number
),
311 history
: new CircularArray()
314 history
: new CircularArray()
318 history
: new CircularArray()
321 history
: new CircularArray()
325 expect(workerNode
.usage
.tasks
.executed
).toBeGreaterThanOrEqual(0)
326 expect(workerNode
.usage
.tasks
.executed
).toBeLessThanOrEqual(
331 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
332 WorkerChoiceStrategies
.ROUND_ROBIN
335 // We need to clean up the resources after our test
339 it('Verify ROUND_ROBIN strategy runtime behavior', async () => {
340 const workerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
341 let pool
= new FixedClusterPool(
343 './tests/worker-files/cluster/testWorker.js',
344 { workerChoiceStrategy
}
346 let results
= new Set()
347 for (let i
= 0; i
< max
; i
++) {
348 results
.add(pool
.workerNodes
[pool
.chooseWorkerNode()].worker
.id
)
350 expect(results
.size
).toBe(max
)
352 pool
= new FixedThreadPool(
354 './tests/worker-files/thread/testWorker.js',
355 { workerChoiceStrategy
}
358 for (let i
= 0; i
< max
; i
++) {
359 results
.add(pool
.workerNodes
[pool
.chooseWorkerNode()].worker
.threadId
)
361 expect(results
.size
).toBe(max
)
365 it('Verify ROUND_ROBIN strategy internals are resets after setting it', async () => {
366 const workerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
367 let pool
= new FixedThreadPool(
369 './tests/worker-files/thread/testWorker.js',
370 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
373 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
377 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
)
379 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
380 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
384 pool
= new DynamicThreadPool(
387 './tests/worker-files/thread/testWorker.js',
388 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
391 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
395 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
)
397 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
398 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
401 // We need to clean up the resources after our test
405 it('Verify LEAST_USED strategy default policy', async () => {
406 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_USED
407 let pool
= new FixedThreadPool(
409 './tests/worker-files/thread/testWorker.js',
410 { workerChoiceStrategy
}
412 expect(pool
.workerChoiceStrategyContext
.getStrategyPolicy()).toStrictEqual({
413 dynamicWorkerUsage
: false,
414 dynamicWorkerReady
: true
417 pool
= new DynamicThreadPool(
420 './tests/worker-files/thread/testWorker.js',
421 { workerChoiceStrategy
}
423 expect(pool
.workerChoiceStrategyContext
.getStrategyPolicy()).toStrictEqual({
424 dynamicWorkerUsage
: false,
425 dynamicWorkerReady
: true
427 // We need to clean up the resources after our test
431 it('Verify LEAST_USED strategy default tasks statistics requirements', async () => {
432 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_USED
433 let pool
= new FixedThreadPool(
435 './tests/worker-files/thread/testWorker.js',
436 { workerChoiceStrategy
}
439 pool
.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
458 pool
= new DynamicThreadPool(
461 './tests/worker-files/thread/testWorker.js',
462 { workerChoiceStrategy
}
465 pool
.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
483 // We need to clean up the resources after our test
487 it('Verify LEAST_USED strategy can be run in a fixed pool', async () => {
488 const pool
= new FixedThreadPool(
490 './tests/worker-files/thread/testWorker.js',
491 { workerChoiceStrategy
: WorkerChoiceStrategies
.LEAST_USED
}
493 // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose`
494 const promises
= new Set()
495 const maxMultiplier
= 2
496 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
497 promises
.add(pool
.execute())
499 await Promise
.all(promises
)
500 for (const workerNode
of pool
.workerNodes
) {
501 expect(workerNode
.usage
).toStrictEqual({
503 executed
: expect
.any(Number
),
511 history
: new CircularArray()
514 history
: new CircularArray()
518 history
: new CircularArray()
521 history
: new CircularArray()
525 expect(workerNode
.usage
.tasks
.executed
).toBeGreaterThanOrEqual(0)
526 expect(workerNode
.usage
.tasks
.executed
).toBeLessThanOrEqual(
530 // We need to clean up the resources after our test
534 it('Verify LEAST_USED strategy can be run in a dynamic pool', async () => {
535 const pool
= new DynamicThreadPool(
538 './tests/worker-files/thread/testWorker.js',
539 { workerChoiceStrategy
: WorkerChoiceStrategies
.LEAST_USED
}
541 // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose`
542 const promises
= new Set()
543 const maxMultiplier
= 2
544 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
545 promises
.add(pool
.execute())
547 await Promise
.all(promises
)
548 for (const workerNode
of pool
.workerNodes
) {
549 expect(workerNode
.usage
).toStrictEqual({
551 executed
: expect
.any(Number
),
559 history
: new CircularArray()
562 history
: new CircularArray()
566 history
: new CircularArray()
569 history
: new CircularArray()
573 expect(workerNode
.usage
.tasks
.executed
).toBeGreaterThanOrEqual(0)
574 expect(workerNode
.usage
.tasks
.executed
).toBeLessThanOrEqual(
578 // We need to clean up the resources after our test
582 it('Verify LEAST_BUSY strategy default policy', async () => {
583 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_BUSY
584 let pool
= new FixedThreadPool(
586 './tests/worker-files/thread/testWorker.js',
587 { workerChoiceStrategy
}
589 expect(pool
.workerChoiceStrategyContext
.getStrategyPolicy()).toStrictEqual({
590 dynamicWorkerUsage
: false,
591 dynamicWorkerReady
: true
594 pool
= new DynamicThreadPool(
597 './tests/worker-files/thread/testWorker.js',
598 { workerChoiceStrategy
}
600 expect(pool
.workerChoiceStrategyContext
.getStrategyPolicy()).toStrictEqual({
601 dynamicWorkerUsage
: false,
602 dynamicWorkerReady
: true
604 // We need to clean up the resources after our test
608 it('Verify LEAST_BUSY strategy default tasks statistics requirements', async () => {
609 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_BUSY
610 let pool
= new FixedThreadPool(
612 './tests/worker-files/thread/testWorker.js',
613 { workerChoiceStrategy
}
616 pool
.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
635 pool
= new DynamicThreadPool(
638 './tests/worker-files/thread/testWorker.js',
639 { workerChoiceStrategy
}
642 pool
.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
660 // We need to clean up the resources after our test
664 it('Verify LEAST_BUSY strategy can be run in a fixed pool', async () => {
665 const pool
= new FixedThreadPool(
667 './tests/worker-files/thread/testWorker.js',
668 { workerChoiceStrategy
: WorkerChoiceStrategies
.LEAST_BUSY
}
670 // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose`
671 const promises
= new Set()
672 const maxMultiplier
= 2
673 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
674 promises
.add(pool
.execute())
676 await Promise
.all(promises
)
677 for (const workerNode
of pool
.workerNodes
) {
678 expect(workerNode
.usage
).toMatchObject({
680 executed
: expect
.any(Number
),
688 history
: expect
.any(CircularArray
)
691 history
: expect
.any(CircularArray
)
695 history
: expect
.any(CircularArray
)
698 history
: expect
.any(CircularArray
)
702 expect(workerNode
.usage
.tasks
.executed
).toBeGreaterThanOrEqual(0)
703 expect(workerNode
.usage
.tasks
.executed
).toBeLessThanOrEqual(
706 if (workerNode
.usage
.runTime
.aggregate
== null) {
707 expect(workerNode
.usage
.runTime
.aggregate
).toBeUndefined()
709 expect(workerNode
.usage
.runTime
.aggregate
).toBeGreaterThan(0)
711 if (workerNode
.usage
.waitTime
.aggregate
== null) {
712 expect(workerNode
.usage
.waitTime
.aggregate
).toBeUndefined()
714 expect(workerNode
.usage
.waitTime
.aggregate
).toBeGreaterThan(0)
717 // We need to clean up the resources after our test
721 it('Verify LEAST_BUSY strategy can be run in a dynamic pool', async () => {
722 const pool
= new DynamicThreadPool(
725 './tests/worker-files/thread/testWorker.js',
726 { workerChoiceStrategy
: WorkerChoiceStrategies
.LEAST_BUSY
}
728 // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose`
729 const promises
= new Set()
730 const maxMultiplier
= 2
731 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
732 promises
.add(pool
.execute())
734 await Promise
.all(promises
)
735 for (const workerNode
of pool
.workerNodes
) {
736 expect(workerNode
.usage
).toMatchObject({
738 executed
: expect
.any(Number
),
746 history
: expect
.any(CircularArray
)
749 history
: expect
.any(CircularArray
)
753 history
: expect
.any(CircularArray
)
756 history
: expect
.any(CircularArray
)
760 expect(workerNode
.usage
.tasks
.executed
).toBeGreaterThanOrEqual(0)
761 expect(workerNode
.usage
.tasks
.executed
).toBeLessThanOrEqual(
764 if (workerNode
.usage
.runTime
.aggregate
== null) {
765 expect(workerNode
.usage
.runTime
.aggregate
).toBeUndefined()
767 expect(workerNode
.usage
.runTime
.aggregate
).toBeGreaterThan(0)
769 if (workerNode
.usage
.waitTime
.aggregate
== null) {
770 expect(workerNode
.usage
.waitTime
.aggregate
).toBeUndefined()
772 expect(workerNode
.usage
.waitTime
.aggregate
).toBeGreaterThan(0)
775 // We need to clean up the resources after our test
779 it('Verify LEAST_ELU strategy default policy', async () => {
780 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_ELU
781 let pool
= new FixedThreadPool(
783 './tests/worker-files/thread/testWorker.js',
784 { workerChoiceStrategy
}
786 expect(pool
.workerChoiceStrategyContext
.getStrategyPolicy()).toStrictEqual({
787 dynamicWorkerUsage
: false,
788 dynamicWorkerReady
: true
791 pool
= new DynamicThreadPool(
794 './tests/worker-files/thread/testWorker.js',
795 { workerChoiceStrategy
}
797 expect(pool
.workerChoiceStrategyContext
.getStrategyPolicy()).toStrictEqual({
798 dynamicWorkerUsage
: false,
799 dynamicWorkerReady
: true
801 // We need to clean up the resources after our test
805 it('Verify LEAST_ELU strategy default tasks statistics requirements', async () => {
806 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_ELU
807 let pool
= new FixedThreadPool(
809 './tests/worker-files/thread/testWorker.js',
810 { workerChoiceStrategy
}
813 pool
.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
832 pool
= new DynamicThreadPool(
835 './tests/worker-files/thread/testWorker.js',
836 { workerChoiceStrategy
}
839 pool
.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
857 // We need to clean up the resources after our test
861 it('Verify LEAST_ELU strategy can be run in a fixed pool', async () => {
862 const pool
= new FixedThreadPool(
864 './tests/worker-files/thread/testWorker.js',
865 { workerChoiceStrategy
: WorkerChoiceStrategies
.LEAST_ELU
}
867 // TODO: Create a better test to cover `LeastEluWorkerChoiceStrategy#choose`
868 const promises
= new Set()
869 const maxMultiplier
= 2
870 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
871 promises
.add(pool
.execute())
873 await Promise
.all(promises
)
874 for (const workerNode
of pool
.workerNodes
) {
875 expect(workerNode
.usage
).toMatchObject({
877 executed
: expect
.any(Number
),
885 history
: expect
.any(CircularArray
)
888 history
: expect
.any(CircularArray
)
892 history
: expect
.any(CircularArray
)
895 history
: expect
.any(CircularArray
)
899 expect(workerNode
.usage
.tasks
.executed
).toBeGreaterThanOrEqual(0)
900 expect(workerNode
.usage
.tasks
.executed
).toBeLessThanOrEqual(
903 if (workerNode
.usage
.elu
.active
.aggregate
== null) {
904 expect(workerNode
.usage
.elu
.active
.aggregate
).toBeUndefined()
906 expect(workerNode
.usage
.elu
.active
.aggregate
).toBeGreaterThan(0)
908 if (workerNode
.usage
.elu
.idle
.aggregate
== null) {
909 expect(workerNode
.usage
.elu
.idle
.aggregate
).toBeUndefined()
911 expect(workerNode
.usage
.elu
.idle
.aggregate
).toBeGreaterThanOrEqual(0)
913 if (workerNode
.usage
.elu
.utilization
== null) {
914 expect(workerNode
.usage
.elu
.utilization
).toBeUndefined()
916 expect(workerNode
.usage
.elu
.utilization
).toBeGreaterThanOrEqual(0)
917 expect(workerNode
.usage
.elu
.utilization
).toBeLessThanOrEqual(1)
920 // We need to clean up the resources after our test
924 it('Verify LEAST_ELU strategy can be run in a dynamic pool', async () => {
925 const pool
= new DynamicThreadPool(
928 './tests/worker-files/thread/testWorker.js',
929 { workerChoiceStrategy
: WorkerChoiceStrategies
.LEAST_ELU
}
931 // TODO: Create a better test to cover `LeastEluWorkerChoiceStrategy#choose`
932 const promises
= new Set()
933 const maxMultiplier
= 2
934 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
935 promises
.add(pool
.execute())
937 await Promise
.all(promises
)
938 for (const workerNode
of pool
.workerNodes
) {
939 expect(workerNode
.usage
).toMatchObject({
941 executed
: expect
.any(Number
),
949 history
: expect
.any(CircularArray
)
952 history
: expect
.any(CircularArray
)
956 history
: expect
.any(CircularArray
)
959 history
: expect
.any(CircularArray
)
963 expect(workerNode
.usage
.tasks
.executed
).toBeGreaterThanOrEqual(0)
964 expect(workerNode
.usage
.tasks
.executed
).toBeLessThanOrEqual(
967 if (workerNode
.usage
.elu
.active
.aggregate
== null) {
968 expect(workerNode
.usage
.elu
.active
.aggregate
).toBeUndefined()
970 expect(workerNode
.usage
.elu
.active
.aggregate
).toBeGreaterThan(0)
972 if (workerNode
.usage
.elu
.idle
.aggregate
== null) {
973 expect(workerNode
.usage
.elu
.idle
.aggregate
).toBeUndefined()
975 expect(workerNode
.usage
.elu
.idle
.aggregate
).toBeGreaterThanOrEqual(0)
977 if (workerNode
.usage
.elu
.utilization
== null) {
978 expect(workerNode
.usage
.elu
.utilization
).toBeUndefined()
980 expect(workerNode
.usage
.elu
.utilization
).toBeGreaterThanOrEqual(0)
981 expect(workerNode
.usage
.elu
.utilization
).toBeLessThanOrEqual(1)
984 // We need to clean up the resources after our test
988 it('Verify FAIR_SHARE strategy default policy', async () => {
989 const workerChoiceStrategy
= WorkerChoiceStrategies
.FAIR_SHARE
990 let pool
= new FixedThreadPool(
992 './tests/worker-files/thread/testWorker.js',
993 { workerChoiceStrategy
}
995 expect(pool
.workerChoiceStrategyContext
.getStrategyPolicy()).toStrictEqual({
996 dynamicWorkerUsage
: false,
997 dynamicWorkerReady
: true
1000 pool
= new DynamicThreadPool(
1003 './tests/worker-files/thread/testWorker.js',
1004 { workerChoiceStrategy
}
1006 expect(pool
.workerChoiceStrategyContext
.getStrategyPolicy()).toStrictEqual({
1007 dynamicWorkerUsage
: false,
1008 dynamicWorkerReady
: true
1010 // We need to clean up the resources after our test
1011 await pool
.destroy()
1014 it('Verify FAIR_SHARE strategy default tasks statistics requirements', async () => {
1015 const workerChoiceStrategy
= WorkerChoiceStrategies
.FAIR_SHARE
1016 let pool
= new FixedThreadPool(
1018 './tests/worker-files/thread/testWorker.js',
1019 { workerChoiceStrategy
}
1022 pool
.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
1040 await pool
.destroy()
1041 pool
= new DynamicThreadPool(
1044 './tests/worker-files/thread/testWorker.js',
1045 { workerChoiceStrategy
}
1048 pool
.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
1066 // We need to clean up the resources after our test
1067 await pool
.destroy()
1070 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
1071 const pool
= new FixedThreadPool(
1073 './tests/worker-files/thread/testWorker.js',
1074 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
1076 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
1077 const promises
= new Set()
1078 const maxMultiplier
= 2
1079 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
1080 promises
.add(pool
.execute())
1082 await Promise
.all(promises
)
1083 for (const workerNode
of pool
.workerNodes
) {
1084 expect(workerNode
.usage
).toMatchObject({
1086 executed
: expect
.any(Number
),
1094 history
: expect
.any(CircularArray
)
1097 history
: expect
.any(CircularArray
)
1101 history
: expect
.any(CircularArray
)
1104 history
: expect
.any(CircularArray
)
1108 expect(workerNode
.usage
.tasks
.executed
).toBeGreaterThanOrEqual(0)
1109 expect(workerNode
.usage
.tasks
.executed
).toBeLessThanOrEqual(
1112 if (workerNode
.usage
.runTime
.aggregate
== null) {
1113 expect(workerNode
.usage
.runTime
.aggregate
).toBeUndefined()
1115 expect(workerNode
.usage
.runTime
.aggregate
).toBeGreaterThan(0)
1117 if (workerNode
.usage
.runTime
.average
== null) {
1118 expect(workerNode
.usage
.runTime
.average
).toBeUndefined()
1120 expect(workerNode
.usage
.runTime
.average
).toBeGreaterThan(0)
1122 if (workerNode
.usage
.elu
.active
.aggregate
== null) {
1123 expect(workerNode
.usage
.elu
.active
.aggregate
).toBeUndefined()
1125 expect(workerNode
.usage
.elu
.active
.aggregate
).toBeGreaterThan(0)
1127 if (workerNode
.usage
.elu
.idle
.aggregate
== null) {
1128 expect(workerNode
.usage
.elu
.idle
.aggregate
).toBeUndefined()
1130 expect(workerNode
.usage
.elu
.idle
.aggregate
).toBeGreaterThanOrEqual(0)
1132 if (workerNode
.usage
.elu
.utilization
== null) {
1133 expect(workerNode
.usage
.elu
.utilization
).toBeUndefined()
1135 expect(workerNode
.usage
.elu
.utilization
).toBeGreaterThanOrEqual(0)
1136 expect(workerNode
.usage
.elu
.utilization
).toBeLessThanOrEqual(1)
1140 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1141 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1142 ).workersVirtualTaskEndTimestamp
.length
1143 ).toBe(pool
.workerNodes
.length
)
1144 // We need to clean up the resources after our test
1145 await pool
.destroy()
1148 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
1149 const pool
= new DynamicThreadPool(
1152 './tests/worker-files/thread/testWorker.js',
1153 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
1155 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
1156 const promises
= new Set()
1157 const maxMultiplier
= 2
1158 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
1159 promises
.add(pool
.execute())
1161 await Promise
.all(promises
)
1162 for (const workerNode
of pool
.workerNodes
) {
1163 expect(workerNode
.usage
).toMatchObject({
1165 executed
: expect
.any(Number
),
1173 history
: expect
.any(CircularArray
)
1176 history
: expect
.any(CircularArray
)
1180 history
: expect
.any(CircularArray
)
1183 history
: expect
.any(CircularArray
)
1187 expect(workerNode
.usage
.tasks
.executed
).toBeGreaterThanOrEqual(0)
1188 expect(workerNode
.usage
.tasks
.executed
).toBeLessThanOrEqual(
1191 if (workerNode
.usage
.runTime
.aggregate
== null) {
1192 expect(workerNode
.usage
.runTime
.aggregate
).toBeUndefined()
1194 expect(workerNode
.usage
.runTime
.aggregate
).toBeGreaterThan(0)
1196 if (workerNode
.usage
.runTime
.average
== null) {
1197 expect(workerNode
.usage
.runTime
.average
).toBeUndefined()
1199 expect(workerNode
.usage
.runTime
.average
).toBeGreaterThan(0)
1201 if (workerNode
.usage
.elu
.active
.aggregate
== null) {
1202 expect(workerNode
.usage
.elu
.active
.aggregate
).toBeUndefined()
1204 expect(workerNode
.usage
.elu
.active
.aggregate
).toBeGreaterThan(0)
1206 if (workerNode
.usage
.elu
.idle
.aggregate
== null) {
1207 expect(workerNode
.usage
.elu
.idle
.aggregate
).toBeUndefined()
1209 expect(workerNode
.usage
.elu
.idle
.aggregate
).toBeGreaterThanOrEqual(0)
1211 if (workerNode
.usage
.elu
.utilization
== null) {
1212 expect(workerNode
.usage
.elu
.utilization
).toBeUndefined()
1214 expect(workerNode
.usage
.elu
.utilization
).toBeGreaterThanOrEqual(0)
1215 expect(workerNode
.usage
.elu
.utilization
).toBeLessThanOrEqual(1)
1219 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1220 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1221 ).workersVirtualTaskEndTimestamp
.length
1222 ).toBe(pool
.workerNodes
.length
)
1223 // We need to clean up the resources after our test
1224 await pool
.destroy()
1227 it('Verify FAIR_SHARE strategy can be run in a dynamic pool with median runtime statistic', async () => {
1228 const pool
= new DynamicThreadPool(
1231 './tests/worker-files/thread/testWorker.js',
1233 workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
,
1234 workerChoiceStrategyOptions
: {
1235 runTime
: { median
: true }
1239 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
1240 const promises
= new Set()
1241 const maxMultiplier
= 2
1242 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
1243 promises
.add(pool
.execute())
1245 await Promise
.all(promises
)
1246 for (const workerNode
of pool
.workerNodes
) {
1247 expect(workerNode
.usage
).toMatchObject({
1249 executed
: expect
.any(Number
),
1257 history
: expect
.any(CircularArray
)
1260 history
: expect
.any(CircularArray
)
1264 history
: expect
.any(CircularArray
)
1267 history
: expect
.any(CircularArray
)
1271 expect(workerNode
.usage
.tasks
.executed
).toBeGreaterThanOrEqual(0)
1272 expect(workerNode
.usage
.tasks
.executed
).toBeLessThanOrEqual(
1275 if (workerNode
.usage
.runTime
.aggregate
== null) {
1276 expect(workerNode
.usage
.runTime
.aggregate
).toBeUndefined()
1278 expect(workerNode
.usage
.runTime
.aggregate
).toBeGreaterThan(0)
1280 if (workerNode
.usage
.runTime
.median
== null) {
1281 expect(workerNode
.usage
.runTime
.median
).toBeUndefined()
1283 expect(workerNode
.usage
.runTime
.median
).toBeGreaterThan(0)
1285 if (workerNode
.usage
.elu
.active
.aggregate
== null) {
1286 expect(workerNode
.usage
.elu
.active
.aggregate
).toBeUndefined()
1288 expect(workerNode
.usage
.elu
.active
.aggregate
).toBeGreaterThan(0)
1290 if (workerNode
.usage
.elu
.idle
.aggregate
== null) {
1291 expect(workerNode
.usage
.elu
.idle
.aggregate
).toBeUndefined()
1293 expect(workerNode
.usage
.elu
.idle
.aggregate
).toBeGreaterThanOrEqual(0)
1295 if (workerNode
.usage
.elu
.utilization
== null) {
1296 expect(workerNode
.usage
.elu
.utilization
).toBeUndefined()
1298 expect(workerNode
.usage
.elu
.utilization
).toBeGreaterThanOrEqual(0)
1299 expect(workerNode
.usage
.elu
.utilization
).toBeLessThanOrEqual(1)
1303 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1304 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1305 ).workersVirtualTaskEndTimestamp
.length
1306 ).toBe(pool
.workerNodes
.length
)
1307 // We need to clean up the resources after our test
1308 await pool
.destroy()
1311 it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => {
1312 const workerChoiceStrategy
= WorkerChoiceStrategies
.FAIR_SHARE
1313 let pool
= new FixedThreadPool(
1315 './tests/worker-files/thread/testWorker.js'
1318 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1319 workerChoiceStrategy
1320 ).workersVirtualTaskEndTimestamp
1321 ).toBeInstanceOf(Array
)
1323 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1324 workerChoiceStrategy
1325 ).workersVirtualTaskEndTimestamp
.length
1327 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1328 workerChoiceStrategy
1329 ).workersVirtualTaskEndTimestamp
[0] = performance
.now()
1331 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1332 workerChoiceStrategy
1333 ).workersVirtualTaskEndTimestamp
.length
1335 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
)
1337 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1338 workerChoiceStrategy
1339 ).workersVirtualTaskEndTimestamp
1340 ).toBeInstanceOf(Array
)
1342 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1343 workerChoiceStrategy
1344 ).workersVirtualTaskEndTimestamp
.length
1346 await pool
.destroy()
1347 pool
= new DynamicThreadPool(
1350 './tests/worker-files/thread/testWorker.js'
1353 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1354 workerChoiceStrategy
1355 ).workersVirtualTaskEndTimestamp
1356 ).toBeInstanceOf(Array
)
1358 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1359 workerChoiceStrategy
1360 ).workersVirtualTaskEndTimestamp
.length
1362 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1363 workerChoiceStrategy
1364 ).workersVirtualTaskEndTimestamp
[0] = performance
.now()
1366 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1367 workerChoiceStrategy
1368 ).workersVirtualTaskEndTimestamp
.length
1370 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
)
1372 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1373 workerChoiceStrategy
1374 ).workersVirtualTaskEndTimestamp
1375 ).toBeInstanceOf(Array
)
1377 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1378 workerChoiceStrategy
1379 ).workersVirtualTaskEndTimestamp
.length
1381 // We need to clean up the resources after our test
1382 await pool
.destroy()
1385 it('Verify WEIGHTED_ROUND_ROBIN strategy default policy', async () => {
1386 const workerChoiceStrategy
= WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
1387 let pool
= new FixedThreadPool(
1389 './tests/worker-files/thread/testWorker.js',
1390 { workerChoiceStrategy
}
1392 expect(pool
.workerChoiceStrategyContext
.getStrategyPolicy()).toStrictEqual({
1393 dynamicWorkerUsage
: false,
1394 dynamicWorkerReady
: true
1396 await pool
.destroy()
1397 pool
= new DynamicThreadPool(
1400 './tests/worker-files/thread/testWorker.js',
1401 { workerChoiceStrategy
}
1403 expect(pool
.workerChoiceStrategyContext
.getStrategyPolicy()).toStrictEqual({
1404 dynamicWorkerUsage
: false,
1405 dynamicWorkerReady
: true
1407 // We need to clean up the resources after our test
1408 await pool
.destroy()
1411 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks statistics requirements', async () => {
1412 const workerChoiceStrategy
= WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
1413 let pool
= new FixedThreadPool(
1415 './tests/worker-files/thread/testWorker.js',
1416 { workerChoiceStrategy
}
1419 pool
.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
1437 await pool
.destroy()
1438 pool
= new DynamicThreadPool(
1441 './tests/worker-files/thread/testWorker.js',
1442 { workerChoiceStrategy
}
1445 pool
.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
1463 // We need to clean up the resources after our test
1464 await pool
.destroy()
1467 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
1468 const pool
= new FixedThreadPool(
1470 './tests/worker-files/thread/testWorker.js',
1471 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
1473 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
1474 const promises
= new Set()
1475 const maxMultiplier
= 2
1476 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
1477 promises
.add(pool
.execute())
1479 await Promise
.all(promises
)
1480 for (const workerNode
of pool
.workerNodes
) {
1481 expect(workerNode
.usage
).toStrictEqual({
1483 executed
: expect
.any(Number
),
1490 runTime
: expect
.objectContaining({
1491 history
: expect
.any(CircularArray
)
1494 history
: expect
.any(CircularArray
)
1498 history
: expect
.any(CircularArray
)
1501 history
: expect
.any(CircularArray
)
1505 expect(workerNode
.usage
.tasks
.executed
).toBeGreaterThanOrEqual(0)
1506 expect(workerNode
.usage
.tasks
.executed
).toBeLessThanOrEqual(
1509 if (workerNode
.usage
.runTime
.aggregate
== null) {
1510 expect(workerNode
.usage
.runTime
.aggregate
).toBeUndefined()
1512 expect(workerNode
.usage
.runTime
.aggregate
).toBeGreaterThan(0)
1514 if (workerNode
.usage
.runTime
.average
== null) {
1515 expect(workerNode
.usage
.runTime
.average
).toBeUndefined()
1517 expect(workerNode
.usage
.runTime
.average
).toBeGreaterThan(0)
1521 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1522 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1523 ).defaultWorkerWeight
1524 ).toBeGreaterThan(0)
1526 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1527 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1528 ).workerVirtualTaskRunTime
1529 ).toBeGreaterThanOrEqual(0)
1530 // We need to clean up the resources after our test
1531 await pool
.destroy()
1534 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
1535 const pool
= new DynamicThreadPool(
1538 './tests/worker-files/thread/testWorker.js',
1539 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
1541 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
1542 const promises
= new Set()
1543 const maxMultiplier
= 2
1544 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
1545 promises
.add(pool
.execute())
1547 await Promise
.all(promises
)
1548 for (const workerNode
of pool
.workerNodes
) {
1549 expect(workerNode
.usage
).toStrictEqual({
1551 executed
: expect
.any(Number
),
1558 runTime
: expect
.objectContaining({
1559 history
: expect
.any(CircularArray
)
1562 history
: expect
.any(CircularArray
)
1566 history
: expect
.any(CircularArray
)
1569 history
: expect
.any(CircularArray
)
1573 expect(workerNode
.usage
.tasks
.executed
).toBeGreaterThanOrEqual(0)
1574 expect(workerNode
.usage
.tasks
.executed
).toBeLessThanOrEqual(
1577 if (workerNode
.usage
.runTime
.aggregate
== null) {
1578 expect(workerNode
.usage
.runTime
.aggregate
).toBeUndefined()
1580 expect(workerNode
.usage
.runTime
.aggregate
).toBeGreaterThan(0)
1582 if (workerNode
.usage
.runTime
.average
== null) {
1583 expect(workerNode
.usage
.runTime
.average
).toBeUndefined()
1585 expect(workerNode
.usage
.runTime
.average
).toBeGreaterThan(0)
1589 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1590 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1591 ).defaultWorkerWeight
1592 ).toBeGreaterThan(0)
1594 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1595 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1596 ).workerVirtualTaskRunTime
1597 ).toBeGreaterThanOrEqual(0)
1598 // We need to clean up the resources after our test
1599 await pool
.destroy()
1602 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool with median runtime statistic', async () => {
1603 const pool
= new DynamicThreadPool(
1606 './tests/worker-files/thread/testWorker.js',
1608 workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
,
1609 workerChoiceStrategyOptions
: {
1610 runTime
: { median
: true }
1614 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
1615 const promises
= new Set()
1616 const maxMultiplier
= 2
1617 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
1618 promises
.add(pool
.execute())
1620 await Promise
.all(promises
)
1621 for (const workerNode
of pool
.workerNodes
) {
1622 expect(workerNode
.usage
).toStrictEqual({
1624 executed
: expect
.any(Number
),
1631 runTime
: expect
.objectContaining({
1632 history
: expect
.any(CircularArray
)
1635 history
: expect
.any(CircularArray
)
1639 history
: expect
.any(CircularArray
)
1642 history
: expect
.any(CircularArray
)
1646 expect(workerNode
.usage
.tasks
.executed
).toBeGreaterThanOrEqual(0)
1647 expect(workerNode
.usage
.tasks
.executed
).toBeLessThanOrEqual(
1650 if (workerNode
.usage
.runTime
.aggregate
== null) {
1651 expect(workerNode
.usage
.runTime
.aggregate
).toBeUndefined()
1653 expect(workerNode
.usage
.runTime
.aggregate
).toBeGreaterThan(0)
1655 if (workerNode
.usage
.runTime
.median
== null) {
1656 expect(workerNode
.usage
.runTime
.median
).toBeUndefined()
1658 expect(workerNode
.usage
.runTime
.median
).toBeGreaterThan(0)
1662 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1663 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1664 ).defaultWorkerWeight
1665 ).toBeGreaterThan(0)
1667 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1668 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1669 ).workerVirtualTaskRunTime
1670 ).toBeGreaterThanOrEqual(0)
1671 // We need to clean up the resources after our test
1672 await pool
.destroy()
1675 it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
1676 const workerChoiceStrategy
= WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
1677 let pool
= new FixedThreadPool(
1679 './tests/worker-files/thread/testWorker.js'
1682 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1683 workerChoiceStrategy
1687 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1688 workerChoiceStrategy
1689 ).defaultWorkerWeight
1692 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1693 workerChoiceStrategy
1694 ).workerVirtualTaskRunTime
1696 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
)
1698 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1699 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1703 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1704 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1705 ).defaultWorkerWeight
1706 ).toBeGreaterThan(0)
1708 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1709 workerChoiceStrategy
1710 ).workerVirtualTaskRunTime
1712 await pool
.destroy()
1713 pool
= new DynamicThreadPool(
1716 './tests/worker-files/thread/testWorker.js'
1719 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1720 workerChoiceStrategy
1724 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1725 workerChoiceStrategy
1726 ).defaultWorkerWeight
1729 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1730 workerChoiceStrategy
1731 ).workerVirtualTaskRunTime
1733 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
)
1735 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1736 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1740 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1741 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1742 ).defaultWorkerWeight
1743 ).toBeGreaterThan(0)
1745 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1746 workerChoiceStrategy
1747 ).workerVirtualTaskRunTime
1749 // We need to clean up the resources after our test
1750 await pool
.destroy()
1753 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy default policy', async () => {
1754 const workerChoiceStrategy
=
1755 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1756 let pool
= new FixedThreadPool(
1758 './tests/worker-files/thread/testWorker.js',
1759 { workerChoiceStrategy
}
1761 expect(pool
.workerChoiceStrategyContext
.getStrategyPolicy()).toStrictEqual({
1762 dynamicWorkerUsage
: false,
1763 dynamicWorkerReady
: true
1765 await pool
.destroy()
1766 pool
= new DynamicThreadPool(
1769 './tests/worker-files/thread/testWorker.js',
1770 { workerChoiceStrategy
}
1772 expect(pool
.workerChoiceStrategyContext
.getStrategyPolicy()).toStrictEqual({
1773 dynamicWorkerUsage
: false,
1774 dynamicWorkerReady
: true
1776 // We need to clean up the resources after our test
1777 await pool
.destroy()
1780 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy default tasks statistics requirements', async () => {
1781 const workerChoiceStrategy
=
1782 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1783 let pool
= new FixedThreadPool(
1785 './tests/worker-files/thread/testWorker.js',
1786 { workerChoiceStrategy
}
1789 pool
.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
1807 await pool
.destroy()
1808 pool
= new DynamicThreadPool(
1811 './tests/worker-files/thread/testWorker.js',
1812 { workerChoiceStrategy
}
1815 pool
.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
1833 // We need to clean up the resources after our test
1834 await pool
.destroy()
1837 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
1838 const pool
= new FixedThreadPool(
1840 './tests/worker-files/thread/testWorker.js',
1842 workerChoiceStrategy
:
1843 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1846 // TODO: Create a better test to cover `InterleavedWeightedRoundRobinWorkerChoiceStrategy#choose`
1847 const promises
= new Set()
1848 const maxMultiplier
= 2
1849 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
1850 promises
.add(pool
.execute())
1852 await Promise
.all(promises
)
1853 for (const workerNode
of pool
.workerNodes
) {
1854 expect(workerNode
.usage
).toStrictEqual({
1856 executed
: maxMultiplier
,
1864 history
: new CircularArray()
1867 history
: new CircularArray()
1871 history
: new CircularArray()
1874 history
: new CircularArray()
1880 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1881 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1882 ).defaultWorkerWeight
1883 ).toBeGreaterThan(0)
1885 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1886 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1890 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1891 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1895 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1896 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1899 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1900 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1901 ).defaultWorkerWeight
1903 // We need to clean up the resources after our test
1904 await pool
.destroy()
1907 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
1908 const pool
= new DynamicThreadPool(
1911 './tests/worker-files/thread/testWorker.js',
1913 workerChoiceStrategy
:
1914 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1917 // TODO: Create a better test to cover `InterleavedWeightedRoundRobinWorkerChoiceStrategy#choose`
1918 const promises
= new Set()
1919 const maxMultiplier
= 2
1920 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
1921 promises
.add(pool
.execute())
1923 await Promise
.all(promises
)
1924 for (const workerNode
of pool
.workerNodes
) {
1925 expect(workerNode
.usage
).toStrictEqual({
1927 executed
: expect
.any(Number
),
1935 history
: new CircularArray()
1938 history
: new CircularArray()
1942 history
: new CircularArray()
1945 history
: new CircularArray()
1949 expect(workerNode
.usage
.tasks
.executed
).toBeGreaterThanOrEqual(0)
1950 expect(workerNode
.usage
.tasks
.executed
).toBeLessThanOrEqual(
1955 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1956 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1957 ).defaultWorkerWeight
1958 ).toBeGreaterThan(0)
1960 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1961 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1965 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1966 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1970 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1971 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1974 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1975 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
1976 ).defaultWorkerWeight
1978 // We need to clean up the resources after our test
1979 await pool
.destroy()
1982 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
1983 const workerChoiceStrategy
=
1984 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1985 let pool
= new FixedThreadPool(
1987 './tests/worker-files/thread/testWorker.js'
1990 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1991 workerChoiceStrategy
1995 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
1996 workerChoiceStrategy
2000 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
2001 workerChoiceStrategy
2002 ).defaultWorkerWeight
2005 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
2006 workerChoiceStrategy
2009 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
)
2011 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
2012 workerChoiceStrategy
2016 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
2017 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
2021 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
2022 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
2023 ).defaultWorkerWeight
2024 ).toBeGreaterThan(0)
2026 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
2027 workerChoiceStrategy
2030 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
2031 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
2032 ).defaultWorkerWeight
2034 await pool
.destroy()
2035 pool
= new DynamicThreadPool(
2038 './tests/worker-files/thread/testWorker.js'
2041 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
2042 workerChoiceStrategy
2046 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
2047 workerChoiceStrategy
2051 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
2052 workerChoiceStrategy
2053 ).defaultWorkerWeight
2056 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
2057 workerChoiceStrategy
2060 pool
.setWorkerChoiceStrategy(workerChoiceStrategy
)
2062 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
2063 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
2067 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
2068 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
2069 ).defaultWorkerWeight
2070 ).toBeGreaterThan(0)
2072 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
2073 workerChoiceStrategy
2076 pool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
2077 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
2078 ).defaultWorkerWeight
2080 // We need to clean up the resources after our test
2081 await pool
.destroy()
2084 it('Verify unknown strategy throw error', () => {
2087 new DynamicThreadPool(
2090 './tests/worker-files/thread/testWorker.js',
2091 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
2093 ).toThrowError("Invalid worker choice strategy 'UNKNOWN_STRATEGY'")