Merge dependabot/npm_and_yarn/examples/typescript/smtp-client-pool/poolifier-4.0...
[poolifier.git] / tests / pools / selection-strategies / selection-strategies.test.mjs
1 import { randomInt } from 'node:crypto'
2
3 import { expect } from 'expect'
4
5 import { CircularArray } from '../../../lib/circular-array.cjs'
6 import {
7 DynamicClusterPool,
8 DynamicThreadPool,
9 FixedClusterPool,
10 FixedThreadPool,
11 WorkerChoiceStrategies
12 } from '../../../lib/index.cjs'
13
14 describe('Selection strategies test suite', () => {
15 const min = 0
16 const max = 3
17
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'
26 )
27 expect(WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN).toBe(
28 'INTERLEAVED_WEIGHTED_ROUND_ROBIN'
29 )
30 })
31
32 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
33 const pool = new DynamicThreadPool(
34 min,
35 max,
36 './tests/worker-files/thread/testWorker.mjs'
37 )
38 expect(pool.opts.workerChoiceStrategy).toBe(
39 WorkerChoiceStrategies.ROUND_ROBIN
40 )
41 expect(pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy).toBe(
42 WorkerChoiceStrategies.ROUND_ROBIN
43 )
44 // We need to clean up the resources after our test
45 await pool.destroy()
46 })
47
48 it('Verify available strategies are taken at pool creation', async () => {
49 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
50 const pool = new FixedThreadPool(
51 max,
52 './tests/worker-files/thread/testWorker.mjs',
53 { workerChoiceStrategy }
54 )
55 expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy)
56 expect(
57 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
58 ).toBe(workerChoiceStrategy)
59 await pool.destroy()
60 }
61 })
62
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(
66 min,
67 max,
68 './tests/worker-files/thread/testWorker.mjs'
69 )
70 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
71 expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy)
72 expect(
73 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
74 ).toBe(workerChoiceStrategy)
75 await pool.destroy()
76 }
77 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
78 const pool = new DynamicClusterPool(
79 min,
80 max,
81 './tests/worker-files/cluster/testWorker.cjs'
82 )
83 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
84 expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy)
85 expect(
86 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
87 ).toBe(workerChoiceStrategy)
88 await pool.destroy()
89 }
90 })
91
92 it('Verify available strategies default internals at pool creation', async () => {
93 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
94 const pool = new FixedThreadPool(
95 max,
96 './tests/worker-files/thread/testWorker.mjs',
97 { workerChoiceStrategy }
98 )
99 expect(
100 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
101 workerChoiceStrategy
102 ).nextWorkerNodeKey
103 ).toBe(0)
104 expect(
105 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
106 workerChoiceStrategy
107 ).previousWorkerNodeKey
108 ).toBe(0)
109 if (
110 workerChoiceStrategy === WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
111 ) {
112 expect(
113 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
114 workerChoiceStrategy
115 ).workerNodeVirtualTaskExecutionTime
116 ).toBe(0)
117 } else if (
118 workerChoiceStrategy ===
119 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
120 ) {
121 expect(
122 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
123 workerChoiceStrategy
124 ).workerNodeVirtualTaskExecutionTime
125 ).toBe(0)
126 expect(
127 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
128 workerChoiceStrategy
129 ).roundId
130 ).toBe(0)
131 expect(
132 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
133 workerChoiceStrategy
134 ).workerNodeId
135 ).toBe(0)
136 expect(
137 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
138 workerChoiceStrategy
139 ).roundWeights.length
140 ).toBe(1)
141 expect(
142 Number.isSafeInteger(
143 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
144 workerChoiceStrategy
145 ).roundWeights[0]
146 )
147 ).toBe(true)
148 }
149 await pool.destroy()
150 }
151 })
152
153 it('Verify ROUND_ROBIN strategy default policy', async () => {
154 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
155 let pool = new FixedThreadPool(
156 max,
157 './tests/worker-files/thread/testWorker.mjs',
158 { workerChoiceStrategy }
159 )
160 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
161 dynamicWorkerUsage: false,
162 dynamicWorkerReady: true
163 })
164 await pool.destroy()
165 pool = new DynamicThreadPool(
166 min,
167 max,
168 './tests/worker-files/thread/testWorker.mjs',
169 { workerChoiceStrategy }
170 )
171 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
172 dynamicWorkerUsage: false,
173 dynamicWorkerReady: true
174 })
175 // We need to clean up the resources after our test
176 await pool.destroy()
177 })
178
179 it('Verify ROUND_ROBIN strategy default tasks statistics requirements', async () => {
180 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
181 let pool = new FixedThreadPool(
182 max,
183 './tests/worker-files/thread/testWorker.mjs',
184 { workerChoiceStrategy }
185 )
186 expect(
187 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
188 ).toStrictEqual({
189 runTime: {
190 aggregate: false,
191 average: false,
192 median: false
193 },
194 waitTime: {
195 aggregate: false,
196 average: false,
197 median: false
198 },
199 elu: {
200 aggregate: false,
201 average: false,
202 median: false
203 }
204 })
205 await pool.destroy()
206 pool = new DynamicThreadPool(
207 min,
208 max,
209 './tests/worker-files/thread/testWorker.mjs',
210 { workerChoiceStrategy }
211 )
212 expect(
213 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
214 ).toStrictEqual({
215 runTime: {
216 aggregate: false,
217 average: false,
218 median: false
219 },
220 waitTime: {
221 aggregate: false,
222 average: false,
223 median: false
224 },
225 elu: {
226 aggregate: false,
227 average: false,
228 median: false
229 }
230 })
231 // We need to clean up the resources after our test
232 await pool.destroy()
233 })
234
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(
238 max,
239 './tests/worker-files/thread/testWorker.mjs',
240 { workerChoiceStrategy }
241 )
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())
247 }
248 await Promise.all(promises)
249 for (const workerNode of pool.workerNodes) {
250 expect(workerNode.usage).toStrictEqual({
251 tasks: {
252 executed: maxMultiplier,
253 executing: 0,
254 queued: 0,
255 maxQueued: 0,
256 sequentiallyStolen: 0,
257 stolen: 0,
258 failed: 0
259 },
260 runTime: {
261 history: new CircularArray()
262 },
263 waitTime: {
264 history: new CircularArray()
265 },
266 elu: {
267 idle: {
268 history: new CircularArray()
269 },
270 active: {
271 history: new CircularArray()
272 }
273 }
274 })
275 }
276 expect(
277 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
278 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
279 ).nextWorkerNodeKey
280 ).toBe(0)
281 expect(
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
287 await pool.destroy()
288 })
289
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(
293 min,
294 max,
295 './tests/worker-files/thread/testWorker.mjs',
296 { workerChoiceStrategy }
297 )
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())
303 }
304 await Promise.all(promises)
305 for (const workerNode of pool.workerNodes) {
306 expect(workerNode.usage).toStrictEqual({
307 tasks: {
308 executed: expect.any(Number),
309 executing: 0,
310 queued: 0,
311 maxQueued: 0,
312 sequentiallyStolen: 0,
313 stolen: 0,
314 failed: 0
315 },
316 runTime: {
317 history: new CircularArray()
318 },
319 waitTime: {
320 history: new CircularArray()
321 },
322 elu: {
323 idle: {
324 history: new CircularArray()
325 },
326 active: {
327 history: new CircularArray()
328 }
329 }
330 })
331 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
332 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
333 max * maxMultiplier
334 )
335 }
336 expect(
337 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
338 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
339 ).nextWorkerNodeKey
340 ).toBe(0)
341 expect(
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
347 await pool.destroy()
348 })
349
350 it('Verify ROUND_ROBIN strategy runtime behavior', async () => {
351 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
352 let pool = new FixedClusterPool(
353 max,
354 './tests/worker-files/cluster/testWorker.cjs',
355 { workerChoiceStrategy }
356 )
357 let results = new Set()
358 for (let i = 0; i < max; i++) {
359 results.add(pool.workerNodes[pool.chooseWorkerNode()].info.id)
360 }
361 expect(results.size).toBe(max)
362 await pool.destroy()
363 pool = new FixedThreadPool(
364 max,
365 './tests/worker-files/thread/testWorker.mjs',
366 { workerChoiceStrategy }
367 )
368 results = new Set()
369 for (let i = 0; i < max; i++) {
370 results.add(pool.workerNodes[pool.chooseWorkerNode()].info.id)
371 }
372 expect(results.size).toBe(max)
373 await pool.destroy()
374 })
375
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(
379 max,
380 './tests/worker-files/thread/testWorker.mjs',
381 { workerChoiceStrategy }
382 )
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)
390 expect(
391 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
392 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
393 ).nextWorkerNodeKey
394 ).toBeGreaterThan(0)
395 expect(
396 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
397 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
398 ).previousWorkerNodeKey
399 ).toBeGreaterThan(0)
400 await pool.destroy()
401 pool = new DynamicThreadPool(
402 min,
403 max,
404 './tests/worker-files/thread/testWorker.mjs',
405 { workerChoiceStrategy }
406 )
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)
414 expect(
415 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
416 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
417 ).nextWorkerNodeKey
418 ).toBeGreaterThan(0)
419 expect(
420 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
421 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
422 ).previousWorkerNodeKey
423 ).toBeGreaterThan(0)
424 // We need to clean up the resources after our test
425 await pool.destroy()
426 })
427
428 it('Verify LEAST_USED strategy default policy', async () => {
429 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_USED
430 let pool = new FixedThreadPool(
431 max,
432 './tests/worker-files/thread/testWorker.mjs',
433 { workerChoiceStrategy }
434 )
435 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
436 dynamicWorkerUsage: false,
437 dynamicWorkerReady: true
438 })
439 await pool.destroy()
440 pool = new DynamicThreadPool(
441 min,
442 max,
443 './tests/worker-files/thread/testWorker.mjs',
444 { workerChoiceStrategy }
445 )
446 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
447 dynamicWorkerUsage: false,
448 dynamicWorkerReady: true
449 })
450 // We need to clean up the resources after our test
451 await pool.destroy()
452 })
453
454 it('Verify LEAST_USED strategy default tasks statistics requirements', async () => {
455 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_USED
456 let pool = new FixedThreadPool(
457 max,
458 './tests/worker-files/thread/testWorker.mjs',
459 { workerChoiceStrategy }
460 )
461 expect(
462 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
463 ).toStrictEqual({
464 runTime: {
465 aggregate: false,
466 average: false,
467 median: false
468 },
469 waitTime: {
470 aggregate: false,
471 average: false,
472 median: false
473 },
474 elu: {
475 aggregate: false,
476 average: false,
477 median: false
478 }
479 })
480 await pool.destroy()
481 pool = new DynamicThreadPool(
482 min,
483 max,
484 './tests/worker-files/thread/testWorker.mjs',
485 { workerChoiceStrategy }
486 )
487 expect(
488 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
489 ).toStrictEqual({
490 runTime: {
491 aggregate: false,
492 average: false,
493 median: false
494 },
495 waitTime: {
496 aggregate: false,
497 average: false,
498 median: false
499 },
500 elu: {
501 aggregate: false,
502 average: false,
503 median: false
504 }
505 })
506 // We need to clean up the resources after our test
507 await pool.destroy()
508 })
509
510 it('Verify LEAST_USED strategy can be run in a fixed pool', async () => {
511 const pool = new FixedThreadPool(
512 max,
513 './tests/worker-files/thread/testWorker.mjs',
514 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED }
515 )
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())
521 }
522 await Promise.all(promises)
523 for (const workerNode of pool.workerNodes) {
524 expect(workerNode.usage).toStrictEqual({
525 tasks: {
526 executed: expect.any(Number),
527 executing: 0,
528 queued: 0,
529 maxQueued: 0,
530 sequentiallyStolen: 0,
531 stolen: 0,
532 failed: 0
533 },
534 runTime: {
535 history: new CircularArray()
536 },
537 waitTime: {
538 history: new CircularArray()
539 },
540 elu: {
541 idle: {
542 history: new CircularArray()
543 },
544 active: {
545 history: new CircularArray()
546 }
547 }
548 })
549 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
550 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
551 max * maxMultiplier
552 )
553 }
554 expect(
555 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
556 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
557 ).nextWorkerNodeKey
558 ).toEqual(expect.any(Number))
559 expect(
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
565 await pool.destroy()
566 })
567
568 it('Verify LEAST_USED strategy can be run in a dynamic pool', async () => {
569 const pool = new DynamicThreadPool(
570 min,
571 max,
572 './tests/worker-files/thread/testWorker.mjs',
573 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED }
574 )
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())
580 }
581 await Promise.all(promises)
582 for (const workerNode of pool.workerNodes) {
583 expect(workerNode.usage).toStrictEqual({
584 tasks: {
585 executed: expect.any(Number),
586 executing: 0,
587 queued: 0,
588 maxQueued: 0,
589 sequentiallyStolen: 0,
590 stolen: 0,
591 failed: 0
592 },
593 runTime: {
594 history: new CircularArray()
595 },
596 waitTime: {
597 history: new CircularArray()
598 },
599 elu: {
600 idle: {
601 history: new CircularArray()
602 },
603 active: {
604 history: new CircularArray()
605 }
606 }
607 })
608 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
609 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
610 max * maxMultiplier
611 )
612 }
613 expect(
614 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
615 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
616 ).nextWorkerNodeKey
617 ).toEqual(expect.any(Number))
618 expect(
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
624 await pool.destroy()
625 })
626
627 it('Verify LEAST_BUSY strategy default policy', async () => {
628 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_BUSY
629 let pool = new FixedThreadPool(
630 max,
631 './tests/worker-files/thread/testWorker.mjs',
632 { workerChoiceStrategy }
633 )
634 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
635 dynamicWorkerUsage: false,
636 dynamicWorkerReady: true
637 })
638 await pool.destroy()
639 pool = new DynamicThreadPool(
640 min,
641 max,
642 './tests/worker-files/thread/testWorker.mjs',
643 { workerChoiceStrategy }
644 )
645 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
646 dynamicWorkerUsage: false,
647 dynamicWorkerReady: true
648 })
649 // We need to clean up the resources after our test
650 await pool.destroy()
651 })
652
653 it('Verify LEAST_BUSY strategy default tasks statistics requirements', async () => {
654 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_BUSY
655 let pool = new FixedThreadPool(
656 max,
657 './tests/worker-files/thread/testWorker.mjs',
658 { workerChoiceStrategy }
659 )
660 expect(
661 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
662 ).toStrictEqual({
663 runTime: {
664 aggregate: true,
665 average: false,
666 median: false
667 },
668 waitTime: {
669 aggregate: true,
670 average: false,
671 median: false
672 },
673 elu: {
674 aggregate: false,
675 average: false,
676 median: false
677 }
678 })
679 await pool.destroy()
680 pool = new DynamicThreadPool(
681 min,
682 max,
683 './tests/worker-files/thread/testWorker.mjs',
684 { workerChoiceStrategy }
685 )
686 expect(
687 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
688 ).toStrictEqual({
689 runTime: {
690 aggregate: true,
691 average: false,
692 median: false
693 },
694 waitTime: {
695 aggregate: true,
696 average: false,
697 median: false
698 },
699 elu: {
700 aggregate: false,
701 average: false,
702 median: false
703 }
704 })
705 // We need to clean up the resources after our test
706 await pool.destroy()
707 })
708
709 it('Verify LEAST_BUSY strategy can be run in a fixed pool', async () => {
710 const pool = new FixedThreadPool(
711 max,
712 './tests/worker-files/thread/testWorker.mjs',
713 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY }
714 )
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())
720 }
721 await Promise.all(promises)
722 for (const workerNode of pool.workerNodes) {
723 expect(workerNode.usage).toStrictEqual({
724 tasks: {
725 executed: expect.any(Number),
726 executing: 0,
727 queued: 0,
728 maxQueued: 0,
729 sequentiallyStolen: 0,
730 stolen: 0,
731 failed: 0
732 },
733 runTime: expect.objectContaining({
734 history: expect.any(CircularArray)
735 }),
736 waitTime: expect.objectContaining({
737 history: expect.any(CircularArray)
738 }),
739 elu: {
740 idle: {
741 history: new CircularArray()
742 },
743 active: {
744 history: new CircularArray()
745 }
746 }
747 })
748 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
749 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
750 max * maxMultiplier
751 )
752 if (workerNode.usage.runTime.aggregate == null) {
753 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
754 } else {
755 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
756 }
757 if (workerNode.usage.waitTime.aggregate == null) {
758 expect(workerNode.usage.waitTime.aggregate).toBeUndefined()
759 } else {
760 expect(workerNode.usage.waitTime.aggregate).toBeGreaterThan(0)
761 }
762 }
763 expect(
764 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
765 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
766 ).nextWorkerNodeKey
767 ).toEqual(expect.any(Number))
768 expect(
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
774 await pool.destroy()
775 })
776
777 it('Verify LEAST_BUSY strategy can be run in a dynamic pool', async () => {
778 const pool = new DynamicThreadPool(
779 min,
780 max,
781 './tests/worker-files/thread/testWorker.mjs',
782 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY }
783 )
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())
789 }
790 await Promise.all(promises)
791 for (const workerNode of pool.workerNodes) {
792 expect(workerNode.usage).toStrictEqual({
793 tasks: {
794 executed: expect.any(Number),
795 executing: 0,
796 queued: 0,
797 maxQueued: 0,
798 sequentiallyStolen: 0,
799 stolen: 0,
800 failed: 0
801 },
802 runTime: expect.objectContaining({
803 history: expect.any(CircularArray)
804 }),
805 waitTime: expect.objectContaining({
806 history: expect.any(CircularArray)
807 }),
808 elu: {
809 idle: {
810 history: new CircularArray()
811 },
812 active: {
813 history: new CircularArray()
814 }
815 }
816 })
817 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
818 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
819 max * maxMultiplier
820 )
821 if (workerNode.usage.runTime.aggregate == null) {
822 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
823 } else {
824 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
825 }
826 if (workerNode.usage.waitTime.aggregate == null) {
827 expect(workerNode.usage.waitTime.aggregate).toBeUndefined()
828 } else {
829 expect(workerNode.usage.waitTime.aggregate).toBeGreaterThan(0)
830 }
831 }
832 expect(
833 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
834 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
835 ).nextWorkerNodeKey
836 ).toEqual(expect.any(Number))
837 expect(
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
843 await pool.destroy()
844 })
845
846 it('Verify LEAST_ELU strategy default policy', async () => {
847 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_ELU
848 let pool = new FixedThreadPool(
849 max,
850 './tests/worker-files/thread/testWorker.mjs',
851 { workerChoiceStrategy }
852 )
853 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
854 dynamicWorkerUsage: false,
855 dynamicWorkerReady: true
856 })
857 await pool.destroy()
858 pool = new DynamicThreadPool(
859 min,
860 max,
861 './tests/worker-files/thread/testWorker.mjs',
862 { workerChoiceStrategy }
863 )
864 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
865 dynamicWorkerUsage: false,
866 dynamicWorkerReady: true
867 })
868 // We need to clean up the resources after our test
869 await pool.destroy()
870 })
871
872 it('Verify LEAST_ELU strategy default tasks statistics requirements', async () => {
873 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_ELU
874 let pool = new FixedThreadPool(
875 max,
876 './tests/worker-files/thread/testWorker.mjs',
877 { workerChoiceStrategy }
878 )
879 expect(
880 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
881 ).toStrictEqual({
882 runTime: {
883 aggregate: false,
884 average: false,
885 median: false
886 },
887 waitTime: {
888 aggregate: false,
889 average: false,
890 median: false
891 },
892 elu: {
893 aggregate: true,
894 average: false,
895 median: false
896 }
897 })
898 await pool.destroy()
899 pool = new DynamicThreadPool(
900 min,
901 max,
902 './tests/worker-files/thread/testWorker.mjs',
903 { workerChoiceStrategy }
904 )
905 expect(
906 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
907 ).toStrictEqual({
908 runTime: {
909 aggregate: false,
910 average: false,
911 median: false
912 },
913 waitTime: {
914 aggregate: false,
915 average: false,
916 median: false
917 },
918 elu: {
919 aggregate: true,
920 average: false,
921 median: false
922 }
923 })
924 // We need to clean up the resources after our test
925 await pool.destroy()
926 })
927
928 it('Verify LEAST_ELU strategy can be run in a fixed pool', async () => {
929 const pool = new FixedThreadPool(
930 max,
931 './tests/worker-files/thread/testWorker.mjs',
932 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_ELU }
933 )
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())
939 }
940 await Promise.all(promises)
941 for (const workerNode of pool.workerNodes) {
942 expect(workerNode.usage).toStrictEqual({
943 tasks: {
944 executed: expect.any(Number),
945 executing: 0,
946 queued: 0,
947 maxQueued: 0,
948 sequentiallyStolen: 0,
949 stolen: 0,
950 failed: 0
951 },
952 runTime: {
953 history: new CircularArray()
954 },
955 waitTime: {
956 history: new CircularArray()
957 },
958 elu: expect.objectContaining({
959 idle: expect.objectContaining({
960 history: expect.any(CircularArray)
961 }),
962 active: expect.objectContaining({
963 history: expect.any(CircularArray)
964 })
965 })
966 })
967 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
968 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
969 max * maxMultiplier
970 )
971 if (workerNode.usage.elu.active.aggregate == null) {
972 expect(workerNode.usage.elu.active.aggregate).toBeUndefined()
973 } else {
974 expect(workerNode.usage.elu.active.aggregate).toBeGreaterThan(0)
975 }
976 if (workerNode.usage.elu.idle.aggregate == null) {
977 expect(workerNode.usage.elu.idle.aggregate).toBeUndefined()
978 } else {
979 expect(workerNode.usage.elu.idle.aggregate).toBeGreaterThanOrEqual(0)
980 }
981 if (workerNode.usage.elu.utilization == null) {
982 expect(workerNode.usage.elu.utilization).toBeUndefined()
983 } else {
984 expect(workerNode.usage.elu.utilization).toBeGreaterThanOrEqual(0)
985 expect(workerNode.usage.elu.utilization).toBeLessThanOrEqual(1)
986 }
987 }
988 expect(
989 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
990 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
991 ).nextWorkerNodeKey
992 ).toEqual(expect.any(Number))
993 expect(
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
999 await pool.destroy()
1000 })
1001
1002 it('Verify LEAST_ELU strategy can be run in a dynamic pool', async () => {
1003 const pool = new DynamicThreadPool(
1004 min,
1005 max,
1006 './tests/worker-files/thread/testWorker.mjs',
1007 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_ELU }
1008 )
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())
1014 }
1015 await Promise.all(promises)
1016 for (const workerNode of pool.workerNodes) {
1017 expect(workerNode.usage).toStrictEqual({
1018 tasks: {
1019 executed: expect.any(Number),
1020 executing: 0,
1021 queued: 0,
1022 maxQueued: 0,
1023 sequentiallyStolen: 0,
1024 stolen: 0,
1025 failed: 0
1026 },
1027 runTime: {
1028 history: new CircularArray()
1029 },
1030 waitTime: {
1031 history: new CircularArray()
1032 },
1033 elu: expect.objectContaining({
1034 idle: expect.objectContaining({
1035 history: expect.any(CircularArray)
1036 }),
1037 active: expect.objectContaining({
1038 history: expect.any(CircularArray)
1039 })
1040 })
1041 })
1042 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1043 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
1044 max * maxMultiplier
1045 )
1046 if (workerNode.usage.elu.active.aggregate == null) {
1047 expect(workerNode.usage.elu.active.aggregate).toBeUndefined()
1048 } else {
1049 expect(workerNode.usage.elu.active.aggregate).toBeGreaterThan(0)
1050 }
1051 if (workerNode.usage.elu.idle.aggregate == null) {
1052 expect(workerNode.usage.elu.idle.aggregate).toBeUndefined()
1053 } else {
1054 expect(workerNode.usage.elu.idle.aggregate).toBeGreaterThanOrEqual(0)
1055 }
1056 if (workerNode.usage.elu.utilization == null) {
1057 expect(workerNode.usage.elu.utilization).toBeUndefined()
1058 } else {
1059 expect(workerNode.usage.elu.utilization).toBeGreaterThanOrEqual(0)
1060 expect(workerNode.usage.elu.utilization).toBeLessThanOrEqual(1)
1061 }
1062 }
1063 expect(
1064 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1065 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1066 ).nextWorkerNodeKey
1067 ).toEqual(expect.any(Number))
1068 expect(
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()
1075 })
1076
1077 it('Verify FAIR_SHARE strategy default policy', async () => {
1078 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
1079 let pool = new FixedThreadPool(
1080 max,
1081 './tests/worker-files/thread/testWorker.mjs',
1082 { workerChoiceStrategy }
1083 )
1084 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
1085 dynamicWorkerUsage: false,
1086 dynamicWorkerReady: true
1087 })
1088 await pool.destroy()
1089 pool = new DynamicThreadPool(
1090 min,
1091 max,
1092 './tests/worker-files/thread/testWorker.mjs',
1093 { workerChoiceStrategy }
1094 )
1095 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
1096 dynamicWorkerUsage: false,
1097 dynamicWorkerReady: true
1098 })
1099 // We need to clean up the resources after our test
1100 await pool.destroy()
1101 })
1102
1103 it('Verify FAIR_SHARE strategy default tasks statistics requirements', async () => {
1104 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
1105 let pool = new FixedThreadPool(
1106 max,
1107 './tests/worker-files/thread/testWorker.mjs',
1108 { workerChoiceStrategy }
1109 )
1110 expect(
1111 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
1112 ).toStrictEqual({
1113 runTime: {
1114 aggregate: true,
1115 average: true,
1116 median: false
1117 },
1118 waitTime: {
1119 aggregate: true,
1120 average: true,
1121 median: false
1122 },
1123 elu: {
1124 aggregate: true,
1125 average: true,
1126 median: false
1127 }
1128 })
1129 await pool.destroy()
1130 pool = new DynamicThreadPool(
1131 min,
1132 max,
1133 './tests/worker-files/thread/testWorker.mjs',
1134 { workerChoiceStrategy }
1135 )
1136 expect(
1137 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
1138 ).toStrictEqual({
1139 runTime: {
1140 aggregate: true,
1141 average: true,
1142 median: false
1143 },
1144 waitTime: {
1145 aggregate: true,
1146 average: true,
1147 median: false
1148 },
1149 elu: {
1150 aggregate: true,
1151 average: true,
1152 median: false
1153 }
1154 })
1155 // We need to clean up the resources after our test
1156 await pool.destroy()
1157 })
1158
1159 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
1160 const pool = new FixedThreadPool(
1161 max,
1162 './tests/worker-files/thread/testWorker.mjs',
1163 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
1164 )
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())
1170 }
1171 await Promise.all(promises)
1172 for (const workerNode of pool.workerNodes) {
1173 expect(workerNode.usage).toStrictEqual({
1174 tasks: {
1175 executed: expect.any(Number),
1176 executing: 0,
1177 queued: 0,
1178 maxQueued: 0,
1179 sequentiallyStolen: 0,
1180 stolen: 0,
1181 failed: 0
1182 },
1183 runTime: expect.objectContaining({
1184 history: expect.any(CircularArray)
1185 }),
1186 waitTime: expect.objectContaining({
1187 history: expect.any(CircularArray)
1188 }),
1189 elu: expect.objectContaining({
1190 idle: expect.objectContaining({
1191 history: expect.any(CircularArray)
1192 }),
1193 active: expect.objectContaining({
1194 history: expect.any(CircularArray)
1195 })
1196 })
1197 })
1198 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1199 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
1200 max * maxMultiplier
1201 )
1202 if (workerNode.usage.runTime.aggregate == null) {
1203 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1204 } else {
1205 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1206 }
1207 if (workerNode.usage.runTime.average == null) {
1208 expect(workerNode.usage.runTime.average).toBeUndefined()
1209 } else {
1210 expect(workerNode.usage.runTime.average).toBeGreaterThan(0)
1211 }
1212 if (workerNode.usage.waitTime.aggregate == null) {
1213 expect(workerNode.usage.waitTime.aggregate).toBeUndefined()
1214 } else {
1215 expect(workerNode.usage.waitTime.aggregate).toBeGreaterThan(0)
1216 }
1217 if (workerNode.usage.waitTime.average == null) {
1218 expect(workerNode.usage.waitTime.average).toBeUndefined()
1219 } else {
1220 expect(workerNode.usage.waitTime.average).toBeGreaterThan(0)
1221 }
1222 if (workerNode.usage.elu.active.aggregate == null) {
1223 expect(workerNode.usage.elu.active.aggregate).toBeUndefined()
1224 } else {
1225 expect(workerNode.usage.elu.active.aggregate).toBeGreaterThan(0)
1226 }
1227 if (workerNode.usage.elu.idle.aggregate == null) {
1228 expect(workerNode.usage.elu.idle.aggregate).toBeUndefined()
1229 } else {
1230 expect(workerNode.usage.elu.idle.aggregate).toBeGreaterThanOrEqual(0)
1231 }
1232 if (workerNode.usage.elu.utilization == null) {
1233 expect(workerNode.usage.elu.utilization).toBeUndefined()
1234 } else {
1235 expect(workerNode.usage.elu.utilization).toBeGreaterThanOrEqual(0)
1236 expect(workerNode.usage.elu.utilization).toBeLessThanOrEqual(1)
1237 }
1238 expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeGreaterThan(0)
1239 }
1240 expect(
1241 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1242 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1243 ).nextWorkerNodeKey
1244 ).toEqual(expect.any(Number))
1245 expect(
1246 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1247 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1248 ).previousWorkerNodeKey
1249 ).toEqual(expect.any(Number))
1250 // We need to clean up the resources after our test
1251 await pool.destroy()
1252 })
1253
1254 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
1255 const pool = new DynamicThreadPool(
1256 min,
1257 max,
1258 './tests/worker-files/thread/testWorker.mjs',
1259 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
1260 )
1261 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
1262 const promises = new Set()
1263 const maxMultiplier = 2
1264 for (let i = 0; i < max * maxMultiplier; i++) {
1265 promises.add(pool.execute())
1266 }
1267 await Promise.all(promises)
1268 for (const workerNode of pool.workerNodes) {
1269 expect(workerNode.usage).toStrictEqual({
1270 tasks: {
1271 executed: expect.any(Number),
1272 executing: 0,
1273 queued: 0,
1274 maxQueued: 0,
1275 sequentiallyStolen: 0,
1276 stolen: 0,
1277 failed: 0
1278 },
1279 runTime: expect.objectContaining({
1280 history: expect.any(CircularArray)
1281 }),
1282 waitTime: expect.objectContaining({
1283 history: expect.any(CircularArray)
1284 }),
1285 elu: expect.objectContaining({
1286 idle: expect.objectContaining({
1287 history: expect.any(CircularArray)
1288 }),
1289 active: expect.objectContaining({
1290 history: expect.any(CircularArray)
1291 })
1292 })
1293 })
1294 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1295 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
1296 max * maxMultiplier
1297 )
1298 if (workerNode.usage.runTime.aggregate == null) {
1299 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1300 } else {
1301 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1302 }
1303 if (workerNode.usage.runTime.average == null) {
1304 expect(workerNode.usage.runTime.average).toBeUndefined()
1305 } else {
1306 expect(workerNode.usage.runTime.average).toBeGreaterThan(0)
1307 }
1308 if (workerNode.usage.waitTime.aggregate == null) {
1309 expect(workerNode.usage.waitTime.aggregate).toBeUndefined()
1310 } else {
1311 expect(workerNode.usage.waitTime.aggregate).toBeGreaterThan(0)
1312 }
1313 if (workerNode.usage.waitTime.average == null) {
1314 expect(workerNode.usage.waitTime.average).toBeUndefined()
1315 } else {
1316 expect(workerNode.usage.waitTime.average).toBeGreaterThan(0)
1317 }
1318 if (workerNode.usage.elu.active.aggregate == null) {
1319 expect(workerNode.usage.elu.active.aggregate).toBeUndefined()
1320 } else {
1321 expect(workerNode.usage.elu.active.aggregate).toBeGreaterThan(0)
1322 }
1323 if (workerNode.usage.elu.idle.aggregate == null) {
1324 expect(workerNode.usage.elu.idle.aggregate).toBeUndefined()
1325 } else {
1326 expect(workerNode.usage.elu.idle.aggregate).toBeGreaterThanOrEqual(0)
1327 }
1328 if (workerNode.usage.elu.utilization == null) {
1329 expect(workerNode.usage.elu.utilization).toBeUndefined()
1330 } else {
1331 expect(workerNode.usage.elu.utilization).toBeGreaterThanOrEqual(0)
1332 expect(workerNode.usage.elu.utilization).toBeLessThanOrEqual(1)
1333 }
1334 expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeGreaterThan(0)
1335 }
1336 expect(
1337 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1338 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1339 ).nextWorkerNodeKey
1340 ).toEqual(expect.any(Number))
1341 expect(
1342 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1343 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1344 ).previousWorkerNodeKey
1345 ).toEqual(expect.any(Number))
1346 // We need to clean up the resources after our test
1347 await pool.destroy()
1348 })
1349
1350 it('Verify FAIR_SHARE strategy can be run in a dynamic pool with median runtime statistic', async () => {
1351 const pool = new DynamicThreadPool(
1352 min,
1353 max,
1354 './tests/worker-files/thread/testWorker.mjs',
1355 {
1356 workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE,
1357 workerChoiceStrategyOptions: {
1358 runTime: { median: true }
1359 }
1360 }
1361 )
1362 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
1363 const promises = new Set()
1364 const maxMultiplier = 2
1365 for (let i = 0; i < max * maxMultiplier; i++) {
1366 promises.add(pool.execute())
1367 }
1368 await Promise.all(promises)
1369 for (const workerNode of pool.workerNodes) {
1370 expect(workerNode.usage).toStrictEqual({
1371 tasks: {
1372 executed: expect.any(Number),
1373 executing: 0,
1374 queued: 0,
1375 maxQueued: 0,
1376 sequentiallyStolen: 0,
1377 stolen: 0,
1378 failed: 0
1379 },
1380 runTime: expect.objectContaining({
1381 history: expect.any(CircularArray)
1382 }),
1383 waitTime: expect.objectContaining({
1384 history: expect.any(CircularArray)
1385 }),
1386 elu: expect.objectContaining({
1387 idle: expect.objectContaining({
1388 history: expect.any(CircularArray)
1389 }),
1390 active: expect.objectContaining({
1391 history: expect.any(CircularArray)
1392 })
1393 })
1394 })
1395 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1396 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
1397 max * maxMultiplier
1398 )
1399 if (workerNode.usage.runTime.aggregate == null) {
1400 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1401 } else {
1402 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1403 }
1404 if (workerNode.usage.runTime.median == null) {
1405 expect(workerNode.usage.runTime.median).toBeUndefined()
1406 } else {
1407 expect(workerNode.usage.runTime.median).toBeGreaterThan(0)
1408 }
1409 if (workerNode.usage.waitTime.aggregate == null) {
1410 expect(workerNode.usage.waitTime.aggregate).toBeUndefined()
1411 } else {
1412 expect(workerNode.usage.waitTime.aggregate).toBeGreaterThan(0)
1413 }
1414 if (workerNode.usage.waitTime.median == null) {
1415 expect(workerNode.usage.waitTime.median).toBeUndefined()
1416 } else {
1417 expect(workerNode.usage.waitTime.median).toBeGreaterThan(0)
1418 }
1419 if (workerNode.usage.elu.active.aggregate == null) {
1420 expect(workerNode.usage.elu.active.aggregate).toBeUndefined()
1421 } else {
1422 expect(workerNode.usage.elu.active.aggregate).toBeGreaterThan(0)
1423 }
1424 if (workerNode.usage.elu.idle.aggregate == null) {
1425 expect(workerNode.usage.elu.idle.aggregate).toBeUndefined()
1426 } else {
1427 expect(workerNode.usage.elu.idle.aggregate).toBeGreaterThanOrEqual(0)
1428 }
1429 if (workerNode.usage.elu.utilization == null) {
1430 expect(workerNode.usage.elu.utilization).toBeUndefined()
1431 } else {
1432 expect(workerNode.usage.elu.utilization).toBeGreaterThanOrEqual(0)
1433 expect(workerNode.usage.elu.utilization).toBeLessThanOrEqual(1)
1434 }
1435 expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeGreaterThan(0)
1436 }
1437 expect(
1438 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1439 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1440 ).nextWorkerNodeKey
1441 ).toEqual(expect.any(Number))
1442 expect(
1443 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1444 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1445 ).previousWorkerNodeKey
1446 ).toEqual(expect.any(Number))
1447 // We need to clean up the resources after our test
1448 await pool.destroy()
1449 })
1450
1451 it("Verify FAIR_SHARE strategy internals aren't reset after setting it", async () => {
1452 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
1453 let pool = new FixedThreadPool(
1454 max,
1455 './tests/worker-files/thread/testWorker.mjs'
1456 )
1457 for (const workerNode of pool.workerNodes) {
1458 workerNode.strategyData = {
1459 virtualTaskEndTimestamp: performance.now()
1460 }
1461 }
1462 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
1463 for (const workerNode of pool.workerNodes) {
1464 expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeGreaterThan(0)
1465 }
1466 await pool.destroy()
1467 pool = new DynamicThreadPool(
1468 min,
1469 max,
1470 './tests/worker-files/thread/testWorker.mjs'
1471 )
1472 for (const workerNode of pool.workerNodes) {
1473 workerNode.strategyData = {
1474 virtualTaskEndTimestamp: performance.now()
1475 }
1476 }
1477 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
1478 for (const workerNode of pool.workerNodes) {
1479 expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeGreaterThan(0)
1480 }
1481 // We need to clean up the resources after our test
1482 await pool.destroy()
1483 })
1484
1485 it('Verify WEIGHTED_ROUND_ROBIN strategy default policy', async () => {
1486 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
1487 let pool = new FixedThreadPool(
1488 max,
1489 './tests/worker-files/thread/testWorker.mjs',
1490 { workerChoiceStrategy }
1491 )
1492 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
1493 dynamicWorkerUsage: false,
1494 dynamicWorkerReady: true
1495 })
1496 await pool.destroy()
1497 pool = new DynamicThreadPool(
1498 min,
1499 max,
1500 './tests/worker-files/thread/testWorker.mjs',
1501 { workerChoiceStrategy }
1502 )
1503 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
1504 dynamicWorkerUsage: false,
1505 dynamicWorkerReady: true
1506 })
1507 // We need to clean up the resources after our test
1508 await pool.destroy()
1509 })
1510
1511 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks statistics requirements', async () => {
1512 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
1513 let pool = new FixedThreadPool(
1514 max,
1515 './tests/worker-files/thread/testWorker.mjs',
1516 { workerChoiceStrategy }
1517 )
1518 expect(
1519 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
1520 ).toStrictEqual({
1521 runTime: {
1522 aggregate: true,
1523 average: true,
1524 median: false
1525 },
1526 waitTime: {
1527 aggregate: true,
1528 average: true,
1529 median: false
1530 },
1531 elu: {
1532 aggregate: false,
1533 average: false,
1534 median: false
1535 }
1536 })
1537 await pool.destroy()
1538 pool = new DynamicThreadPool(
1539 min,
1540 max,
1541 './tests/worker-files/thread/testWorker.mjs',
1542 { workerChoiceStrategy }
1543 )
1544 expect(
1545 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
1546 ).toStrictEqual({
1547 runTime: {
1548 aggregate: true,
1549 average: true,
1550 median: false
1551 },
1552 waitTime: {
1553 aggregate: true,
1554 average: true,
1555 median: false
1556 },
1557 elu: {
1558 aggregate: false,
1559 average: false,
1560 median: false
1561 }
1562 })
1563 // We need to clean up the resources after our test
1564 await pool.destroy()
1565 })
1566
1567 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
1568 const pool = new FixedThreadPool(
1569 max,
1570 './tests/worker-files/thread/testWorker.mjs',
1571 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
1572 )
1573 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
1574 const promises = new Set()
1575 const maxMultiplier = 2
1576 for (let i = 0; i < max * maxMultiplier; i++) {
1577 promises.add(pool.execute())
1578 }
1579 await Promise.all(promises)
1580 for (const workerNode of pool.workerNodes) {
1581 expect(workerNode.usage).toStrictEqual({
1582 tasks: {
1583 executed: expect.any(Number),
1584 executing: 0,
1585 queued: 0,
1586 maxQueued: 0,
1587 sequentiallyStolen: 0,
1588 stolen: 0,
1589 failed: 0
1590 },
1591 runTime: expect.objectContaining({
1592 history: expect.any(CircularArray)
1593 }),
1594 waitTime: expect.objectContaining({
1595 history: expect.any(CircularArray)
1596 }),
1597 elu: {
1598 idle: {
1599 history: new CircularArray()
1600 },
1601 active: {
1602 history: new CircularArray()
1603 }
1604 }
1605 })
1606 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1607 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
1608 max * maxMultiplier
1609 )
1610 if (workerNode.usage.runTime.aggregate == null) {
1611 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1612 } else {
1613 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1614 }
1615 if (workerNode.usage.runTime.average == null) {
1616 expect(workerNode.usage.runTime.average).toBeUndefined()
1617 } else {
1618 expect(workerNode.usage.runTime.average).toBeGreaterThan(0)
1619 }
1620 if (workerNode.usage.waitTime.aggregate == null) {
1621 expect(workerNode.usage.waitTime.aggregate).toBeUndefined()
1622 } else {
1623 expect(workerNode.usage.waitTime.aggregate).toBeGreaterThan(0)
1624 }
1625 if (workerNode.usage.waitTime.average == null) {
1626 expect(workerNode.usage.waitTime.average).toBeUndefined()
1627 } else {
1628 expect(workerNode.usage.waitTime.average).toBeGreaterThan(0)
1629 }
1630 }
1631 expect(
1632 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1633 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1634 ).nextWorkerNodeKey
1635 ).toBe(0)
1636 expect(
1637 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1638 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1639 ).previousWorkerNodeKey
1640 ).toEqual(0)
1641 expect(
1642 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1643 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1644 ).workerNodeVirtualTaskExecutionTime
1645 ).toBeGreaterThanOrEqual(0)
1646 // We need to clean up the resources after our test
1647 await pool.destroy()
1648 })
1649
1650 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
1651 const pool = new DynamicThreadPool(
1652 min,
1653 max,
1654 './tests/worker-files/thread/testWorker.mjs',
1655 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
1656 )
1657 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
1658 const promises = new Set()
1659 const maxMultiplier = 2
1660 for (let i = 0; i < max * maxMultiplier; i++) {
1661 promises.add(pool.execute())
1662 }
1663 await Promise.all(promises)
1664 for (const workerNode of pool.workerNodes) {
1665 expect(workerNode.usage).toStrictEqual({
1666 tasks: {
1667 executed: expect.any(Number),
1668 executing: 0,
1669 queued: 0,
1670 maxQueued: 0,
1671 sequentiallyStolen: 0,
1672 stolen: 0,
1673 failed: 0
1674 },
1675 runTime: expect.objectContaining({
1676 history: expect.any(CircularArray)
1677 }),
1678 waitTime: expect.objectContaining({
1679 history: expect.any(CircularArray)
1680 }),
1681 elu: {
1682 idle: {
1683 history: new CircularArray()
1684 },
1685 active: {
1686 history: new CircularArray()
1687 }
1688 }
1689 })
1690 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1691 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
1692 max * maxMultiplier
1693 )
1694 if (workerNode.usage.runTime.aggregate == null) {
1695 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1696 } else {
1697 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1698 }
1699 if (workerNode.usage.runTime.average == null) {
1700 expect(workerNode.usage.runTime.average).toBeUndefined()
1701 } else {
1702 expect(workerNode.usage.runTime.average).toBeGreaterThan(0)
1703 }
1704 if (workerNode.usage.waitTime.aggregate == null) {
1705 expect(workerNode.usage.waitTime.aggregate).toBeUndefined()
1706 } else {
1707 expect(workerNode.usage.waitTime.aggregate).toBeGreaterThan(0)
1708 }
1709 if (workerNode.usage.waitTime.average == null) {
1710 expect(workerNode.usage.waitTime.average).toBeUndefined()
1711 } else {
1712 expect(workerNode.usage.waitTime.average).toBeGreaterThan(0)
1713 }
1714 }
1715 expect(
1716 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1717 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1718 ).nextWorkerNodeKey
1719 ).toEqual(0)
1720 expect(
1721 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1722 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1723 ).previousWorkerNodeKey
1724 ).toEqual(0)
1725 expect(
1726 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1727 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1728 ).workerNodeVirtualTaskExecutionTime
1729 ).toBeGreaterThanOrEqual(0)
1730 // We need to clean up the resources after our test
1731 await pool.destroy()
1732 })
1733
1734 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool with median runtime statistic', async () => {
1735 const pool = new DynamicThreadPool(
1736 min,
1737 max,
1738 './tests/worker-files/thread/testWorker.mjs',
1739 {
1740 workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
1741 workerChoiceStrategyOptions: {
1742 runTime: { median: true }
1743 }
1744 }
1745 )
1746 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
1747 const promises = new Set()
1748 const maxMultiplier = 2
1749 for (let i = 0; i < max * maxMultiplier; i++) {
1750 promises.add(pool.execute())
1751 }
1752 await Promise.all(promises)
1753 for (const workerNode of pool.workerNodes) {
1754 expect(workerNode.usage).toStrictEqual({
1755 tasks: {
1756 executed: expect.any(Number),
1757 executing: 0,
1758 queued: 0,
1759 maxQueued: 0,
1760 sequentiallyStolen: 0,
1761 stolen: 0,
1762 failed: 0
1763 },
1764 runTime: expect.objectContaining({
1765 history: expect.any(CircularArray)
1766 }),
1767 waitTime: expect.objectContaining({
1768 history: expect.any(CircularArray)
1769 }),
1770 elu: {
1771 idle: {
1772 history: new CircularArray()
1773 },
1774 active: {
1775 history: new CircularArray()
1776 }
1777 }
1778 })
1779 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1780 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
1781 max * maxMultiplier
1782 )
1783 if (workerNode.usage.runTime.aggregate == null) {
1784 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1785 } else {
1786 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1787 }
1788 if (workerNode.usage.runTime.median == null) {
1789 expect(workerNode.usage.runTime.median).toBeUndefined()
1790 } else {
1791 expect(workerNode.usage.runTime.median).toBeGreaterThan(0)
1792 }
1793 if (workerNode.usage.waitTime.aggregate == null) {
1794 expect(workerNode.usage.waitTime.aggregate).toBeUndefined()
1795 } else {
1796 expect(workerNode.usage.waitTime.aggregate).toBeGreaterThan(0)
1797 }
1798 if (workerNode.usage.waitTime.median == null) {
1799 expect(workerNode.usage.waitTime.median).toBeUndefined()
1800 } else {
1801 expect(workerNode.usage.waitTime.median).toBeGreaterThan(0)
1802 }
1803 }
1804 expect(
1805 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1806 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1807 ).nextWorkerNodeKey
1808 ).toEqual(0)
1809 expect(
1810 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1811 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1812 ).previousWorkerNodeKey
1813 ).toEqual(0)
1814 expect(
1815 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1816 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1817 ).workerNodeVirtualTaskExecutionTime
1818 ).toBeGreaterThanOrEqual(0)
1819 // We need to clean up the resources after our test
1820 await pool.destroy()
1821 })
1822
1823 it("Verify WEIGHTED_ROUND_ROBIN strategy internals aren't reset after setting it", async () => {
1824 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
1825 let pool = new FixedThreadPool(
1826 max,
1827 './tests/worker-files/thread/testWorker.mjs',
1828 { workerChoiceStrategy }
1829 )
1830 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1831 workerChoiceStrategy
1832 ).nextWorkerNodeKey = randomInt(1, max - 1)
1833 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1834 workerChoiceStrategy
1835 ).previousWorkerNodeKey = randomInt(1, max - 1)
1836 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1837 workerChoiceStrategy
1838 ).workerNodeVirtualTaskRunTime = randomInt(100, 1000)
1839 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
1840 expect(
1841 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1842 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1843 ).nextWorkerNodeKey
1844 ).toBeGreaterThan(0)
1845 expect(
1846 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1847 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1848 ).previousWorkerNodeKey
1849 ).toBeGreaterThan(0)
1850 expect(
1851 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1852 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1853 ).workerNodeVirtualTaskRunTime
1854 ).toBeGreaterThan(99)
1855 await pool.destroy()
1856 pool = new DynamicThreadPool(
1857 min,
1858 max,
1859 './tests/worker-files/thread/testWorker.mjs',
1860 { workerChoiceStrategy }
1861 )
1862 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1863 workerChoiceStrategy
1864 ).nextWorkerNodeKey = randomInt(1, max - 1)
1865 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1866 workerChoiceStrategy
1867 ).previousWorkerNodeKey = randomInt(1, max - 1)
1868 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1869 workerChoiceStrategy
1870 ).workerNodeVirtualTaskRunTime = randomInt(100, 1000)
1871 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
1872 expect(
1873 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1874 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1875 ).nextWorkerNodeKey
1876 ).toBeGreaterThan(0)
1877 expect(
1878 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1879 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1880 ).previousWorkerNodeKey
1881 ).toBeGreaterThan(0)
1882 expect(
1883 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1884 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
1885 ).workerNodeVirtualTaskRunTime
1886 ).toBeGreaterThan(99)
1887 // We need to clean up the resources after our test
1888 await pool.destroy()
1889 })
1890
1891 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy default policy', async () => {
1892 const workerChoiceStrategy =
1893 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1894 let pool = new FixedThreadPool(
1895 max,
1896 './tests/worker-files/thread/testWorker.mjs',
1897 { workerChoiceStrategy }
1898 )
1899 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
1900 dynamicWorkerUsage: false,
1901 dynamicWorkerReady: true
1902 })
1903 await pool.destroy()
1904 pool = new DynamicThreadPool(
1905 min,
1906 max,
1907 './tests/worker-files/thread/testWorker.mjs',
1908 { workerChoiceStrategy }
1909 )
1910 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
1911 dynamicWorkerUsage: false,
1912 dynamicWorkerReady: true
1913 })
1914 // We need to clean up the resources after our test
1915 await pool.destroy()
1916 })
1917
1918 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy default tasks statistics requirements', async () => {
1919 const workerChoiceStrategy =
1920 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1921 let pool = new FixedThreadPool(
1922 max,
1923 './tests/worker-files/thread/testWorker.mjs',
1924 { workerChoiceStrategy }
1925 )
1926 expect(
1927 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
1928 ).toStrictEqual({
1929 runTime: {
1930 aggregate: true,
1931 average: true,
1932 median: false
1933 },
1934 waitTime: {
1935 aggregate: true,
1936 average: true,
1937 median: false
1938 },
1939 elu: {
1940 aggregate: false,
1941 average: false,
1942 median: false
1943 }
1944 })
1945 await pool.destroy()
1946 pool = new DynamicThreadPool(
1947 min,
1948 max,
1949 './tests/worker-files/thread/testWorker.mjs',
1950 { workerChoiceStrategy }
1951 )
1952 expect(
1953 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
1954 ).toStrictEqual({
1955 runTime: {
1956 aggregate: true,
1957 average: true,
1958 median: false
1959 },
1960 waitTime: {
1961 aggregate: true,
1962 average: true,
1963 median: false
1964 },
1965 elu: {
1966 aggregate: false,
1967 average: false,
1968 median: false
1969 }
1970 })
1971 // We need to clean up the resources after our test
1972 await pool.destroy()
1973 })
1974
1975 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
1976 const pool = new FixedThreadPool(
1977 max,
1978 './tests/worker-files/thread/testWorker.mjs',
1979 {
1980 workerChoiceStrategy:
1981 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1982 }
1983 )
1984 // TODO: Create a better test to cover `InterleavedWeightedRoundRobinWorkerChoiceStrategy#choose`
1985 const promises = new Set()
1986 const maxMultiplier = 2
1987 for (let i = 0; i < max * maxMultiplier; i++) {
1988 promises.add(pool.execute())
1989 }
1990 await Promise.all(promises)
1991 for (const workerNode of pool.workerNodes) {
1992 expect(workerNode.usage).toStrictEqual({
1993 tasks: {
1994 executed: expect.any(Number),
1995 executing: 0,
1996 queued: 0,
1997 maxQueued: 0,
1998 sequentiallyStolen: 0,
1999 stolen: 0,
2000 failed: 0
2001 },
2002 runTime: expect.objectContaining({
2003 history: expect.any(CircularArray)
2004 }),
2005 waitTime: expect.objectContaining({
2006 history: expect.any(CircularArray)
2007 }),
2008 elu: {
2009 idle: {
2010 history: new CircularArray()
2011 },
2012 active: {
2013 history: new CircularArray()
2014 }
2015 }
2016 })
2017 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
2018 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
2019 max * maxMultiplier
2020 )
2021 if (workerNode.usage.runTime.aggregate == null) {
2022 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
2023 } else {
2024 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
2025 }
2026 if (workerNode.usage.runTime.average == null) {
2027 expect(workerNode.usage.runTime.average).toBeUndefined()
2028 } else {
2029 expect(workerNode.usage.runTime.average).toBeGreaterThan(0)
2030 }
2031 if (workerNode.usage.waitTime.aggregate == null) {
2032 expect(workerNode.usage.waitTime.aggregate).toBeUndefined()
2033 } else {
2034 expect(workerNode.usage.waitTime.aggregate).toBeGreaterThan(0)
2035 }
2036 if (workerNode.usage.waitTime.average == null) {
2037 expect(workerNode.usage.waitTime.average).toBeUndefined()
2038 } else {
2039 expect(workerNode.usage.waitTime.average).toBeGreaterThan(0)
2040 }
2041 }
2042 expect(
2043 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2044 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2045 ).roundId
2046 ).toBe(0)
2047 expect(
2048 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2049 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2050 ).workerNodeId
2051 ).toBe(0)
2052 expect(
2053 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2054 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2055 ).nextWorkerNodeKey
2056 ).toBe(0)
2057 expect(
2058 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2059 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2060 ).previousWorkerNodeKey
2061 ).toEqual(0)
2062 expect(
2063 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2064 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2065 ).roundWeights.length
2066 ).toBe(1)
2067 expect(
2068 Number.isSafeInteger(
2069 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2070 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2071 ).roundWeights[0]
2072 )
2073 ).toBe(true)
2074 // We need to clean up the resources after our test
2075 await pool.destroy()
2076 })
2077
2078 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
2079 const pool = new DynamicThreadPool(
2080 min,
2081 max,
2082 './tests/worker-files/thread/testWorker.mjs',
2083 {
2084 workerChoiceStrategy:
2085 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
2086 }
2087 )
2088 // TODO: Create a better test to cover `InterleavedWeightedRoundRobinWorkerChoiceStrategy#choose`
2089 const promises = new Set()
2090 const maxMultiplier = 2
2091 for (let i = 0; i < max * maxMultiplier; i++) {
2092 promises.add(pool.execute())
2093 }
2094 await Promise.all(promises)
2095 for (const workerNode of pool.workerNodes) {
2096 expect(workerNode.usage).toStrictEqual({
2097 tasks: {
2098 executed: expect.any(Number),
2099 executing: 0,
2100 queued: 0,
2101 maxQueued: 0,
2102 sequentiallyStolen: 0,
2103 stolen: 0,
2104 failed: 0
2105 },
2106 runTime: expect.objectContaining({
2107 history: expect.any(CircularArray)
2108 }),
2109 waitTime: expect.objectContaining({
2110 history: expect.any(CircularArray)
2111 }),
2112 elu: {
2113 idle: {
2114 history: new CircularArray()
2115 },
2116 active: {
2117 history: new CircularArray()
2118 }
2119 }
2120 })
2121 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
2122 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
2123 max * maxMultiplier
2124 )
2125 if (workerNode.usage.runTime.aggregate == null) {
2126 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
2127 } else {
2128 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
2129 }
2130 if (workerNode.usage.runTime.average == null) {
2131 expect(workerNode.usage.runTime.average).toBeUndefined()
2132 } else {
2133 expect(workerNode.usage.runTime.average).toBeGreaterThan(0)
2134 }
2135 if (workerNode.usage.waitTime.aggregate == null) {
2136 expect(workerNode.usage.waitTime.aggregate).toBeUndefined()
2137 } else {
2138 expect(workerNode.usage.waitTime.aggregate).toBeGreaterThan(0)
2139 }
2140 if (workerNode.usage.waitTime.average == null) {
2141 expect(workerNode.usage.waitTime.average).toBeUndefined()
2142 } else {
2143 expect(workerNode.usage.waitTime.average).toBeGreaterThan(0)
2144 }
2145 }
2146 expect(
2147 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2148 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2149 ).roundId
2150 ).toBe(0)
2151 expect(
2152 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2153 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2154 ).workerNodeId
2155 ).toBe(0)
2156 expect(
2157 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2158 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2159 ).nextWorkerNodeKey
2160 ).toBe(0)
2161 expect(
2162 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2163 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2164 ).previousWorkerNodeKey
2165 ).toEqual(0)
2166 expect(
2167 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2168 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2169 ).roundWeights.length
2170 ).toBe(1)
2171 expect(
2172 Number.isSafeInteger(
2173 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2174 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2175 ).roundWeights[0]
2176 )
2177 ).toBe(true)
2178 // We need to clean up the resources after our test
2179 await pool.destroy()
2180 })
2181
2182 it("Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy internals aren't reset after setting it", async () => {
2183 const workerChoiceStrategy =
2184 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
2185 let pool = new FixedThreadPool(
2186 max,
2187 './tests/worker-files/thread/testWorker.mjs',
2188 { workerChoiceStrategy }
2189 )
2190 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2191 workerChoiceStrategy
2192 ).roundId = randomInt(1, max - 1)
2193 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2194 workerChoiceStrategy
2195 ).workerNodeId = randomInt(1, max - 1)
2196 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2197 workerChoiceStrategy
2198 ).nextWorkerNodeKey = randomInt(1, max - 1)
2199 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2200 workerChoiceStrategy
2201 ).previousWorkerNodeKey = randomInt(1, max - 1)
2202 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2203 workerChoiceStrategy
2204 ).roundWeights = [randomInt(1, max - 1), randomInt(1, max - 1)]
2205 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
2206 expect(
2207 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2208 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2209 ).roundId
2210 ).toBeGreaterThan(0)
2211 expect(
2212 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2213 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2214 ).workerNodeId
2215 ).toBeGreaterThan(0)
2216 expect(
2217 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2218 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2219 ).nextWorkerNodeKey
2220 ).toBeGreaterThan(0)
2221 expect(
2222 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2223 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2224 ).previousWorkerNodeKey
2225 ).toBeGreaterThan(0)
2226 expect(
2227 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2228 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2229 ).roundWeights.length
2230 ).toBeGreaterThan(1)
2231 expect(
2232 Number.isSafeInteger(
2233 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2234 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2235 ).roundWeights[0]
2236 )
2237 ).toBe(true)
2238 await pool.destroy()
2239 pool = new DynamicThreadPool(
2240 min,
2241 max,
2242 './tests/worker-files/thread/testWorker.mjs',
2243 { workerChoiceStrategy }
2244 )
2245 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2246 workerChoiceStrategy
2247 ).roundId = randomInt(1, max - 1)
2248 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2249 workerChoiceStrategy
2250 ).workerNodeId = randomInt(1, max - 1)
2251 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2252 workerChoiceStrategy
2253 ).nextWorkerNodeKey = randomInt(1, max - 1)
2254 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2255 workerChoiceStrategy
2256 ).previousWorkerNodeKey = randomInt(1, max - 1)
2257 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2258 workerChoiceStrategy
2259 ).roundWeights = [randomInt(1, max - 1), randomInt(1, max - 1)]
2260 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
2261 expect(
2262 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2263 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2264 ).roundId
2265 ).toBeGreaterThan(0)
2266 expect(
2267 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2268 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2269 ).workerNodeId
2270 ).toBeGreaterThan(0)
2271 expect(
2272 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2273 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2274 ).nextWorkerNodeKey
2275 ).toBeGreaterThan(0)
2276 expect(
2277 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2278 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2279 ).previousWorkerNodeKey
2280 ).toBeGreaterThan(0)
2281 expect(
2282 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2283 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2284 ).roundWeights.length
2285 ).toBeGreaterThan(1)
2286 expect(
2287 Number.isSafeInteger(
2288 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2289 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
2290 ).roundWeights[0]
2291 )
2292 ).toBe(true)
2293 // We need to clean up the resources after our test
2294 await pool.destroy()
2295 })
2296
2297 it('Verify unknown strategy throw error', () => {
2298 expect(
2299 () =>
2300 new DynamicThreadPool(
2301 min,
2302 max,
2303 './tests/worker-files/thread/testWorker.mjs',
2304 { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
2305 )
2306 ).toThrow("Invalid worker choice strategy 'UNKNOWN_STRATEGY'")
2307 })
2308 })