test: add tests for LEAST_ELU worker choice strategy
[poolifier.git] / tests / pools / selection-strategies / selection-strategies.test.js
1 const { expect } = require('expect')
2 const {
3 WorkerChoiceStrategies,
4 DynamicThreadPool,
5 FixedThreadPool,
6 FixedClusterPool
7 } = require('../../../lib')
8 const { CircularArray } = require('../../../lib/circular-array')
9
10 describe('Selection strategies test suite', () => {
11 const min = 0
12 const max = 3
13
14 it('Verify that WorkerChoiceStrategies enumeration provides string values', () => {
15 expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN')
16 expect(WorkerChoiceStrategies.LEAST_USED).toBe('LEAST_USED')
17 expect(WorkerChoiceStrategies.LEAST_BUSY).toBe('LEAST_BUSY')
18 expect(WorkerChoiceStrategies.LEAST_ELU).toBe('LEAST_ELU')
19 expect(WorkerChoiceStrategies.FAIR_SHARE).toBe('FAIR_SHARE')
20 expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe(
21 'WEIGHTED_ROUND_ROBIN'
22 )
23 expect(WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN).toBe(
24 'INTERLEAVED_WEIGHTED_ROUND_ROBIN'
25 )
26 })
27
28 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
29 const pool = new DynamicThreadPool(
30 min,
31 max,
32 './tests/worker-files/thread/testWorker.js'
33 )
34 expect(pool.opts.workerChoiceStrategy).toBe(
35 WorkerChoiceStrategies.ROUND_ROBIN
36 )
37 // We need to clean up the resources after our test
38 await pool.destroy()
39 })
40
41 it('Verify available strategies are taken at pool creation', async () => {
42 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
43 const pool = new FixedThreadPool(
44 max,
45 './tests/worker-files/thread/testWorker.js',
46 { workerChoiceStrategy }
47 )
48 expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy)
49 expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe(
50 workerChoiceStrategy
51 )
52 await pool.destroy()
53 }
54 })
55
56 it('Verify available strategies can be set after pool creation', async () => {
57 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
58 const pool = new DynamicThreadPool(
59 min,
60 max,
61 './tests/worker-files/thread/testWorker.js'
62 )
63 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
64 expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy)
65 expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe(
66 workerChoiceStrategy
67 )
68 await pool.destroy()
69 }
70 })
71
72 it('Verify available strategies default internals at pool creation', async () => {
73 const pool = new FixedThreadPool(
74 max,
75 './tests/worker-files/thread/testWorker.js'
76 )
77 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
78 if (workerChoiceStrategy === WorkerChoiceStrategies.ROUND_ROBIN) {
79 expect(
80 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
81 workerChoiceStrategy
82 ).nextWorkerNodeId
83 ).toBe(0)
84 } else if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) {
85 expect(
86 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
87 workerChoiceStrategy
88 ).workersVirtualTaskEndTimestamp
89 ).toBeInstanceOf(Array)
90 expect(
91 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
92 workerChoiceStrategy
93 ).workersVirtualTaskEndTimestamp.length
94 ).toBe(0)
95 } else if (
96 workerChoiceStrategy === WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
97 ) {
98 expect(
99 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
100 workerChoiceStrategy
101 ).currentWorkerNodeId
102 ).toBe(0)
103 expect(
104 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
105 workerChoiceStrategy
106 ).defaultWorkerWeight
107 ).toBeGreaterThan(0)
108 expect(
109 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
110 workerChoiceStrategy
111 ).workerVirtualTaskRunTime
112 ).toBe(0)
113 }
114 }
115 await pool.destroy()
116 })
117
118 it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
119 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
120 let pool = new FixedThreadPool(
121 max,
122 './tests/worker-files/thread/testWorker.js',
123 { workerChoiceStrategy }
124 )
125 expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
126 runTime: false,
127 avgRunTime: false,
128 medRunTime: false,
129 waitTime: false,
130 avgWaitTime: false,
131 medWaitTime: false,
132 elu: false
133 })
134 await pool.destroy()
135 pool = new DynamicThreadPool(
136 min,
137 max,
138 './tests/worker-files/thread/testWorker.js',
139 { workerChoiceStrategy }
140 )
141 expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
142 runTime: false,
143 avgRunTime: false,
144 medRunTime: false,
145 waitTime: false,
146 avgWaitTime: false,
147 medWaitTime: false,
148 elu: false
149 })
150 // We need to clean up the resources after our test
151 await pool.destroy()
152 })
153
154 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
155 const pool = new FixedThreadPool(
156 max,
157 './tests/worker-files/thread/testWorker.js',
158 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
159 )
160 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
161 const promises = new Set()
162 const maxMultiplier = 2
163 for (let i = 0; i < max * maxMultiplier; i++) {
164 promises.add(pool.execute())
165 }
166 await Promise.all(promises)
167 for (const workerNode of pool.workerNodes) {
168 expect(workerNode.workerUsage).toStrictEqual({
169 tasks: {
170 executed: maxMultiplier,
171 executing: 0,
172 queued: 0,
173 failed: 0
174 },
175 runTime: {
176 aggregation: 0,
177 average: 0,
178 median: 0,
179 history: expect.any(CircularArray)
180 },
181 waitTime: {
182 aggregation: 0,
183 average: 0,
184 median: 0,
185 history: expect.any(CircularArray)
186 },
187 elu: undefined
188 })
189 }
190 expect(
191 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
192 WorkerChoiceStrategies.ROUND_ROBIN
193 ).nextWorkerNodeId
194 ).toBe(0)
195 // We need to clean up the resources after our test
196 await pool.destroy()
197 })
198
199 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
200 const pool = new DynamicThreadPool(
201 min,
202 max,
203 './tests/worker-files/thread/testWorker.js',
204 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
205 )
206 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
207 const promises = new Set()
208 const maxMultiplier = 2
209 for (let i = 0; i < max * maxMultiplier; i++) {
210 promises.add(pool.execute())
211 }
212 await Promise.all(promises)
213 for (const workerNode of pool.workerNodes) {
214 expect(workerNode.workerUsage).toStrictEqual({
215 tasks: {
216 executed: maxMultiplier,
217 executing: 0,
218 queued: 0,
219 failed: 0
220 },
221 runTime: {
222 aggregation: 0,
223 average: 0,
224 median: 0,
225 history: expect.any(CircularArray)
226 },
227 waitTime: {
228 aggregation: 0,
229 average: 0,
230 median: 0,
231 history: expect.any(CircularArray)
232 },
233 elu: undefined
234 })
235 }
236 expect(
237 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
238 WorkerChoiceStrategies.ROUND_ROBIN
239 ).nextWorkerNodeId
240 ).toBe(0)
241 // We need to clean up the resources after our test
242 await pool.destroy()
243 })
244
245 it('Verify ROUND_ROBIN strategy runtime behavior', async () => {
246 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
247 let pool = new FixedClusterPool(
248 max,
249 './tests/worker-files/cluster/testWorker.js',
250 { workerChoiceStrategy }
251 )
252 let results = new Set()
253 for (let i = 0; i < max; i++) {
254 results.add(pool.workerNodes[pool.chooseWorkerNode()].worker.id)
255 }
256 expect(results.size).toBe(max)
257 await pool.destroy()
258 pool = new FixedThreadPool(
259 max,
260 './tests/worker-files/thread/testWorker.js',
261 { workerChoiceStrategy }
262 )
263 results = new Set()
264 for (let i = 0; i < max; i++) {
265 results.add(pool.workerNodes[pool.chooseWorkerNode()].worker.threadId)
266 }
267 expect(results.size).toBe(max)
268 await pool.destroy()
269 })
270
271 it('Verify ROUND_ROBIN strategy internals are resets after setting it', async () => {
272 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
273 let pool = new FixedThreadPool(
274 max,
275 './tests/worker-files/thread/testWorker.js',
276 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
277 )
278 expect(
279 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
280 workerChoiceStrategy
281 ).nextWorkerNodeId
282 ).toBeDefined()
283 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
284 expect(
285 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
286 pool.workerChoiceStrategyContext.workerChoiceStrategy
287 ).nextWorkerNodeId
288 ).toBe(0)
289 await pool.destroy()
290 pool = new DynamicThreadPool(
291 min,
292 max,
293 './tests/worker-files/thread/testWorker.js',
294 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
295 )
296 expect(
297 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
298 workerChoiceStrategy
299 ).nextWorkerNodeId
300 ).toBeDefined()
301 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
302 expect(
303 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
304 pool.workerChoiceStrategyContext.workerChoiceStrategy
305 ).nextWorkerNodeId
306 ).toBe(0)
307 // We need to clean up the resources after our test
308 await pool.destroy()
309 })
310
311 it('Verify LEAST_USED strategy default tasks usage statistics requirements', async () => {
312 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_USED
313 let pool = new FixedThreadPool(
314 max,
315 './tests/worker-files/thread/testWorker.js',
316 { workerChoiceStrategy }
317 )
318 expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
319 runTime: false,
320 avgRunTime: false,
321 medRunTime: false,
322 waitTime: false,
323 avgWaitTime: false,
324 medWaitTime: false,
325 elu: false
326 })
327 await pool.destroy()
328 pool = new DynamicThreadPool(
329 min,
330 max,
331 './tests/worker-files/thread/testWorker.js',
332 { workerChoiceStrategy }
333 )
334 expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
335 runTime: false,
336 avgRunTime: false,
337 medRunTime: false,
338 waitTime: false,
339 avgWaitTime: false,
340 medWaitTime: false,
341 elu: false
342 })
343 // We need to clean up the resources after our test
344 await pool.destroy()
345 })
346
347 it('Verify LEAST_USED strategy can be run in a fixed pool', async () => {
348 const pool = new FixedThreadPool(
349 max,
350 './tests/worker-files/thread/testWorker.js',
351 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED }
352 )
353 // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose`
354 const promises = new Set()
355 const maxMultiplier = 2
356 for (let i = 0; i < max * maxMultiplier; i++) {
357 promises.add(pool.execute())
358 }
359 await Promise.all(promises)
360 for (const workerNode of pool.workerNodes) {
361 expect(workerNode.workerUsage).toStrictEqual({
362 tasks: {
363 executed: maxMultiplier,
364 executing: 0,
365 queued: 0,
366 failed: 0
367 },
368 runTime: {
369 aggregation: 0,
370 average: 0,
371 median: 0,
372 history: expect.any(CircularArray)
373 },
374 waitTime: {
375 aggregation: 0,
376 average: 0,
377 median: 0,
378 history: expect.any(CircularArray)
379 },
380 elu: undefined
381 })
382 }
383 // We need to clean up the resources after our test
384 await pool.destroy()
385 })
386
387 it('Verify LEAST_USED strategy can be run in a dynamic pool', async () => {
388 const pool = new DynamicThreadPool(
389 min,
390 max,
391 './tests/worker-files/thread/testWorker.js',
392 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED }
393 )
394 // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose`
395 const promises = new Set()
396 const maxMultiplier = 2
397 for (let i = 0; i < max * maxMultiplier; i++) {
398 promises.add(pool.execute())
399 }
400 await Promise.all(promises)
401 for (const workerNode of pool.workerNodes) {
402 expect(workerNode.workerUsage).toStrictEqual({
403 tasks: {
404 executed: maxMultiplier,
405 executing: 0,
406 queued: 0,
407 failed: 0
408 },
409 runTime: {
410 aggregation: 0,
411 average: 0,
412 median: 0,
413 history: expect.any(CircularArray)
414 },
415 waitTime: {
416 aggregation: 0,
417 average: 0,
418 median: 0,
419 history: expect.any(CircularArray)
420 },
421
422 elu: undefined
423 })
424 }
425 // We need to clean up the resources after our test
426 await pool.destroy()
427 })
428
429 it('Verify LEAST_BUSY strategy default tasks usage statistics requirements', async () => {
430 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_BUSY
431 let pool = new FixedThreadPool(
432 max,
433 './tests/worker-files/thread/testWorker.js',
434 { workerChoiceStrategy }
435 )
436 expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
437 runTime: true,
438 avgRunTime: false,
439 medRunTime: false,
440 waitTime: false,
441 avgWaitTime: false,
442 medWaitTime: false,
443 elu: false
444 })
445 await pool.destroy()
446 pool = new DynamicThreadPool(
447 min,
448 max,
449 './tests/worker-files/thread/testWorker.js',
450 { workerChoiceStrategy }
451 )
452 expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
453 runTime: true,
454 avgRunTime: false,
455 medRunTime: false,
456 waitTime: false,
457 avgWaitTime: false,
458 medWaitTime: false,
459 elu: false
460 })
461 // We need to clean up the resources after our test
462 await pool.destroy()
463 })
464
465 it('Verify LEAST_BUSY strategy can be run in a fixed pool', async () => {
466 const pool = new FixedThreadPool(
467 max,
468 './tests/worker-files/thread/testWorker.js',
469 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY }
470 )
471 // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose`
472 const promises = new Set()
473 const maxMultiplier = 2
474 for (let i = 0; i < max * maxMultiplier; i++) {
475 promises.add(pool.execute())
476 }
477 await Promise.all(promises)
478 for (const workerNode of pool.workerNodes) {
479 expect(workerNode.workerUsage).toStrictEqual({
480 tasks: {
481 executed: expect.any(Number),
482 executing: 0,
483 queued: 0,
484 failed: 0
485 },
486 runTime: {
487 aggregation: expect.any(Number),
488 average: 0,
489 median: 0,
490 history: expect.any(CircularArray)
491 },
492 waitTime: {
493 aggregation: 0,
494 average: 0,
495 median: 0,
496 history: expect.any(CircularArray)
497 },
498 elu: undefined
499 })
500 expect(workerNode.workerUsage.tasks.executed).toBeGreaterThanOrEqual(0)
501 expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual(
502 max * maxMultiplier
503 )
504 expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThanOrEqual(
505 0
506 )
507 }
508 // We need to clean up the resources after our test
509 await pool.destroy()
510 })
511
512 it('Verify LEAST_BUSY strategy can be run in a dynamic pool', async () => {
513 const pool = new DynamicThreadPool(
514 min,
515 max,
516 './tests/worker-files/thread/testWorker.js',
517 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY }
518 )
519 // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose`
520 const promises = new Set()
521 const maxMultiplier = 2
522 for (let i = 0; i < max * maxMultiplier; i++) {
523 promises.add(pool.execute())
524 }
525 await Promise.all(promises)
526 for (const workerNode of pool.workerNodes) {
527 expect(workerNode.workerUsage).toStrictEqual({
528 tasks: {
529 executed: expect.any(Number),
530 executing: 0,
531 queued: 0,
532 failed: 0
533 },
534 runTime: {
535 aggregation: expect.any(Number),
536 average: 0,
537 median: 0,
538 history: expect.any(CircularArray)
539 },
540 waitTime: {
541 aggregation: 0,
542 average: 0,
543 median: 0,
544 history: expect.any(CircularArray)
545 },
546 elu: undefined
547 })
548 expect(workerNode.workerUsage.tasks.executed).toBeGreaterThan(0)
549 expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual(
550 max * maxMultiplier
551 )
552 expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0)
553 }
554 // We need to clean up the resources after our test
555 await pool.destroy()
556 })
557
558 it('Verify LEAST_ELU strategy default tasks usage statistics requirements', async () => {
559 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_ELU
560 let pool = new FixedThreadPool(
561 max,
562 './tests/worker-files/thread/testWorker.js',
563 { workerChoiceStrategy }
564 )
565 expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
566 runTime: false,
567 avgRunTime: false,
568 medRunTime: false,
569 waitTime: false,
570 avgWaitTime: false,
571 medWaitTime: false,
572 elu: true
573 })
574 await pool.destroy()
575 pool = new DynamicThreadPool(
576 min,
577 max,
578 './tests/worker-files/thread/testWorker.js',
579 { workerChoiceStrategy }
580 )
581 expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
582 runTime: false,
583 avgRunTime: false,
584 medRunTime: false,
585 waitTime: false,
586 avgWaitTime: false,
587 medWaitTime: false,
588 elu: true
589 })
590 // We need to clean up the resources after our test
591 await pool.destroy()
592 })
593
594 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
595 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
596 let pool = new FixedThreadPool(
597 max,
598 './tests/worker-files/thread/testWorker.js',
599 { workerChoiceStrategy }
600 )
601 expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
602 runTime: true,
603 avgRunTime: true,
604 medRunTime: false,
605 waitTime: false,
606 avgWaitTime: false,
607 medWaitTime: false,
608 elu: false
609 })
610 await pool.destroy()
611 pool = new DynamicThreadPool(
612 min,
613 max,
614 './tests/worker-files/thread/testWorker.js',
615 { workerChoiceStrategy }
616 )
617 expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
618 runTime: true,
619 avgRunTime: true,
620 medRunTime: false,
621 waitTime: false,
622 avgWaitTime: false,
623 medWaitTime: false,
624 elu: false
625 })
626 // We need to clean up the resources after our test
627 await pool.destroy()
628 })
629
630 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
631 const pool = new FixedThreadPool(
632 max,
633 './tests/worker-files/thread/testWorker.js',
634 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
635 )
636 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
637 const promises = new Set()
638 const maxMultiplier = 2
639 for (let i = 0; i < max * maxMultiplier; i++) {
640 promises.add(pool.execute())
641 }
642 await Promise.all(promises)
643 for (const workerNode of pool.workerNodes) {
644 expect(workerNode.workerUsage).toStrictEqual({
645 tasks: {
646 executed: maxMultiplier,
647 executing: 0,
648 queued: 0,
649 failed: 0
650 },
651 runTime: {
652 aggregation: expect.any(Number),
653 average: expect.any(Number),
654 median: 0,
655 history: expect.any(CircularArray)
656 },
657 waitTime: {
658 aggregation: 0,
659 average: 0,
660 median: 0,
661 history: expect.any(CircularArray)
662 },
663 elu: undefined
664 })
665 expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0)
666 expect(workerNode.workerUsage.runTime.average).toBeGreaterThan(0)
667 }
668 expect(
669 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
670 pool.workerChoiceStrategyContext.workerChoiceStrategy
671 ).workersVirtualTaskEndTimestamp.length
672 ).toBe(pool.workerNodes.length)
673 // We need to clean up the resources after our test
674 await pool.destroy()
675 })
676
677 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
678 const pool = new DynamicThreadPool(
679 min,
680 max,
681 './tests/worker-files/thread/testWorker.js',
682 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
683 )
684 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
685 const promises = new Set()
686 const maxMultiplier = 2
687 for (let i = 0; i < max * maxMultiplier; i++) {
688 promises.add(pool.execute())
689 }
690 await Promise.all(promises)
691 for (const workerNode of pool.workerNodes) {
692 expect(workerNode.workerUsage).toStrictEqual({
693 tasks: {
694 executed: maxMultiplier,
695 executing: 0,
696 queued: 0,
697 failed: 0
698 },
699 runTime: {
700 aggregation: expect.any(Number),
701 average: expect.any(Number),
702 median: 0,
703 history: expect.any(CircularArray)
704 },
705 waitTime: {
706 aggregation: 0,
707 average: 0,
708 median: 0,
709 history: expect.any(CircularArray)
710 },
711 elu: undefined
712 })
713 expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0)
714 expect(workerNode.workerUsage.runTime.average).toBeGreaterThan(0)
715 }
716 expect(
717 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
718 pool.workerChoiceStrategyContext.workerChoiceStrategy
719 ).workersVirtualTaskEndTimestamp.length
720 ).toBe(pool.workerNodes.length)
721 // We need to clean up the resources after our test
722 await pool.destroy()
723 })
724
725 it('Verify FAIR_SHARE strategy can be run in a dynamic pool with median runtime statistic', async () => {
726 const pool = new DynamicThreadPool(
727 min,
728 max,
729 './tests/worker-files/thread/testWorker.js',
730 {
731 workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE,
732 workerChoiceStrategyOptions: {
733 medRunTime: true
734 }
735 }
736 )
737 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
738 const promises = new Set()
739 const maxMultiplier = 2
740 for (let i = 0; i < max * maxMultiplier; i++) {
741 promises.add(pool.execute())
742 }
743 await Promise.all(promises)
744 for (const workerNode of pool.workerNodes) {
745 expect(workerNode.workerUsage).toStrictEqual({
746 tasks: {
747 executed: maxMultiplier,
748 executing: 0,
749 queued: 0,
750 failed: 0
751 },
752 runTime: {
753 aggregation: expect.any(Number),
754 average: 0,
755 median: expect.any(Number),
756 history: expect.any(CircularArray)
757 },
758 waitTime: {
759 aggregation: 0,
760 average: 0,
761 median: 0,
762 history: expect.any(CircularArray)
763 },
764 elu: undefined
765 })
766 expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0)
767 expect(workerNode.workerUsage.runTime.median).toBeGreaterThan(0)
768 }
769 expect(
770 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
771 pool.workerChoiceStrategyContext.workerChoiceStrategy
772 ).workersVirtualTaskEndTimestamp.length
773 ).toBe(pool.workerNodes.length)
774 // We need to clean up the resources after our test
775 await pool.destroy()
776 })
777
778 it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => {
779 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
780 let pool = new FixedThreadPool(
781 max,
782 './tests/worker-files/thread/testWorker.js'
783 )
784 expect(
785 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
786 workerChoiceStrategy
787 ).workersVirtualTaskEndTimestamp
788 ).toBeInstanceOf(Array)
789 expect(
790 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
791 workerChoiceStrategy
792 ).workersVirtualTaskEndTimestamp.length
793 ).toBe(0)
794 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
795 workerChoiceStrategy
796 ).workersVirtualTaskEndTimestamp[0] = performance.now()
797 expect(
798 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
799 workerChoiceStrategy
800 ).workersVirtualTaskEndTimestamp.length
801 ).toBe(1)
802 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
803 expect(
804 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
805 workerChoiceStrategy
806 ).workersVirtualTaskEndTimestamp
807 ).toBeInstanceOf(Array)
808 expect(
809 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
810 workerChoiceStrategy
811 ).workersVirtualTaskEndTimestamp.length
812 ).toBe(0)
813 await pool.destroy()
814 pool = new DynamicThreadPool(
815 min,
816 max,
817 './tests/worker-files/thread/testWorker.js'
818 )
819 expect(
820 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
821 workerChoiceStrategy
822 ).workersVirtualTaskEndTimestamp
823 ).toBeInstanceOf(Array)
824 expect(
825 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
826 workerChoiceStrategy
827 ).workersVirtualTaskEndTimestamp.length
828 ).toBe(0)
829 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
830 workerChoiceStrategy
831 ).workersVirtualTaskEndTimestamp[0] = performance.now()
832 expect(
833 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
834 workerChoiceStrategy
835 ).workersVirtualTaskEndTimestamp.length
836 ).toBe(1)
837 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
838 expect(
839 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
840 workerChoiceStrategy
841 ).workersVirtualTaskEndTimestamp
842 ).toBeInstanceOf(Array)
843 expect(
844 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
845 workerChoiceStrategy
846 ).workersVirtualTaskEndTimestamp.length
847 ).toBe(0)
848 // We need to clean up the resources after our test
849 await pool.destroy()
850 })
851
852 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
853 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
854 let pool = new FixedThreadPool(
855 max,
856 './tests/worker-files/thread/testWorker.js',
857 { workerChoiceStrategy }
858 )
859 expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
860 runTime: true,
861 avgRunTime: true,
862 medRunTime: false,
863 waitTime: false,
864 avgWaitTime: false,
865 medWaitTime: false,
866 elu: false
867 })
868 await pool.destroy()
869 pool = new DynamicThreadPool(
870 min,
871 max,
872 './tests/worker-files/thread/testWorker.js',
873 { workerChoiceStrategy }
874 )
875 expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
876 runTime: true,
877 avgRunTime: true,
878 medRunTime: false,
879 waitTime: false,
880 avgWaitTime: false,
881 medWaitTime: false,
882 elu: false
883 })
884 // We need to clean up the resources after our test
885 await pool.destroy()
886 })
887
888 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
889 const pool = new FixedThreadPool(
890 max,
891 './tests/worker-files/thread/testWorker.js',
892 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
893 )
894 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
895 const promises = new Set()
896 const maxMultiplier = 2
897 for (let i = 0; i < max * maxMultiplier; i++) {
898 promises.add(pool.execute())
899 }
900 await Promise.all(promises)
901 for (const workerNode of pool.workerNodes) {
902 expect(workerNode.workerUsage).toStrictEqual({
903 tasks: {
904 executed: expect.any(Number),
905 executing: 0,
906 queued: 0,
907 failed: 0
908 },
909 runTime: {
910 aggregation: expect.any(Number),
911 average: expect.any(Number),
912 median: 0,
913 history: expect.any(CircularArray)
914 },
915 waitTime: {
916 aggregation: 0,
917 average: 0,
918 median: 0,
919 history: expect.any(CircularArray)
920 },
921 elu: undefined
922 })
923 expect(workerNode.workerUsage.tasks.executed).toBeGreaterThanOrEqual(0)
924 expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual(
925 max * maxMultiplier
926 )
927 expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThanOrEqual(
928 0
929 )
930 expect(workerNode.workerUsage.runTime.average).toBeGreaterThanOrEqual(0)
931 }
932 expect(
933 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
934 pool.workerChoiceStrategyContext.workerChoiceStrategy
935 ).defaultWorkerWeight
936 ).toBeGreaterThan(0)
937 expect(
938 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
939 pool.workerChoiceStrategyContext.workerChoiceStrategy
940 ).workerVirtualTaskRunTime
941 ).toBeGreaterThanOrEqual(0)
942 // We need to clean up the resources after our test
943 await pool.destroy()
944 })
945
946 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
947 const pool = new DynamicThreadPool(
948 min,
949 max,
950 './tests/worker-files/thread/testWorker.js',
951 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
952 )
953 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
954 const promises = new Set()
955 const maxMultiplier = 2
956 for (let i = 0; i < max * maxMultiplier; i++) {
957 promises.add(pool.execute())
958 }
959 await Promise.all(promises)
960 for (const workerNode of pool.workerNodes) {
961 expect(workerNode.workerUsage).toStrictEqual({
962 tasks: {
963 executed: expect.any(Number),
964 executing: 0,
965 queued: 0,
966 failed: 0
967 },
968 runTime: {
969 aggregation: expect.any(Number),
970 average: expect.any(Number),
971 median: 0,
972 history: expect.any(CircularArray)
973 },
974 waitTime: {
975 aggregation: 0,
976 average: 0,
977 median: 0,
978 history: expect.any(CircularArray)
979 },
980 elu: undefined
981 })
982 expect(workerNode.workerUsage.tasks.executed).toBeGreaterThan(0)
983 expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual(
984 max * maxMultiplier
985 )
986 expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0)
987 expect(workerNode.workerUsage.runTime.average).toBeGreaterThan(0)
988 }
989 expect(
990 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
991 pool.workerChoiceStrategyContext.workerChoiceStrategy
992 ).defaultWorkerWeight
993 ).toBeGreaterThan(0)
994 expect(
995 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
996 pool.workerChoiceStrategyContext.workerChoiceStrategy
997 ).workerVirtualTaskRunTime
998 ).toBeGreaterThanOrEqual(0)
999 // We need to clean up the resources after our test
1000 await pool.destroy()
1001 })
1002
1003 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool with median runtime statistic', async () => {
1004 const pool = new DynamicThreadPool(
1005 min,
1006 max,
1007 './tests/worker-files/thread/testWorker.js',
1008 {
1009 workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
1010 workerChoiceStrategyOptions: {
1011 medRunTime: true
1012 }
1013 }
1014 )
1015 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
1016 const promises = new Set()
1017 const maxMultiplier = 2
1018 for (let i = 0; i < max * maxMultiplier; i++) {
1019 promises.add(pool.execute())
1020 }
1021 await Promise.all(promises)
1022 for (const workerNode of pool.workerNodes) {
1023 expect(workerNode.workerUsage).toStrictEqual({
1024 tasks: {
1025 executed: expect.any(Number),
1026 executing: 0,
1027 queued: 0,
1028 failed: 0
1029 },
1030 runTime: {
1031 aggregation: expect.any(Number),
1032 average: 0,
1033 median: expect.any(Number),
1034 history: expect.any(CircularArray)
1035 },
1036 waitTime: {
1037 aggregation: 0,
1038 average: 0,
1039 median: 0,
1040 history: expect.any(CircularArray)
1041 },
1042 elu: undefined
1043 })
1044 expect(workerNode.workerUsage.tasks.executed).toBeGreaterThan(0)
1045 expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual(
1046 max * maxMultiplier
1047 )
1048 expect(workerNode.workerUsage.runTime.aggregation).toBeGreaterThan(0)
1049 expect(workerNode.workerUsage.runTime.median).toBeGreaterThan(0)
1050 }
1051 expect(
1052 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1053 pool.workerChoiceStrategyContext.workerChoiceStrategy
1054 ).defaultWorkerWeight
1055 ).toBeGreaterThan(0)
1056 expect(
1057 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1058 pool.workerChoiceStrategyContext.workerChoiceStrategy
1059 ).workerVirtualTaskRunTime
1060 ).toBeGreaterThanOrEqual(0)
1061 // We need to clean up the resources after our test
1062 await pool.destroy()
1063 })
1064
1065 it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
1066 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
1067 let pool = new FixedThreadPool(
1068 max,
1069 './tests/worker-files/thread/testWorker.js'
1070 )
1071 expect(
1072 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1073 workerChoiceStrategy
1074 ).currentWorkerNodeId
1075 ).toBeDefined()
1076 expect(
1077 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1078 workerChoiceStrategy
1079 ).defaultWorkerWeight
1080 ).toBeDefined()
1081 expect(
1082 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1083 workerChoiceStrategy
1084 ).workerVirtualTaskRunTime
1085 ).toBeDefined()
1086 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
1087 expect(
1088 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1089 pool.workerChoiceStrategyContext.workerChoiceStrategy
1090 ).currentWorkerNodeId
1091 ).toBe(0)
1092 expect(
1093 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1094 pool.workerChoiceStrategyContext.workerChoiceStrategy
1095 ).defaultWorkerWeight
1096 ).toBeGreaterThan(0)
1097 expect(
1098 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1099 workerChoiceStrategy
1100 ).workerVirtualTaskRunTime
1101 ).toBe(0)
1102 await pool.destroy()
1103 pool = new DynamicThreadPool(
1104 min,
1105 max,
1106 './tests/worker-files/thread/testWorker.js'
1107 )
1108 expect(
1109 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1110 workerChoiceStrategy
1111 ).currentWorkerNodeId
1112 ).toBeDefined()
1113 expect(
1114 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1115 workerChoiceStrategy
1116 ).defaultWorkerWeight
1117 ).toBeDefined()
1118 expect(
1119 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1120 workerChoiceStrategy
1121 ).workerVirtualTaskRunTime
1122 ).toBeDefined()
1123 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
1124 expect(
1125 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1126 pool.workerChoiceStrategyContext.workerChoiceStrategy
1127 ).currentWorkerNodeId
1128 ).toBe(0)
1129 expect(
1130 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1131 pool.workerChoiceStrategyContext.workerChoiceStrategy
1132 ).defaultWorkerWeight
1133 ).toBeGreaterThan(0)
1134 expect(
1135 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1136 workerChoiceStrategy
1137 ).workerVirtualTaskRunTime
1138 ).toBe(0)
1139 // We need to clean up the resources after our test
1140 await pool.destroy()
1141 })
1142
1143 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
1144 const workerChoiceStrategy =
1145 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1146 let pool = new FixedThreadPool(
1147 max,
1148 './tests/worker-files/thread/testWorker.js',
1149 { workerChoiceStrategy }
1150 )
1151 expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
1152 runTime: false,
1153 avgRunTime: false,
1154 medRunTime: false,
1155 waitTime: false,
1156 avgWaitTime: false,
1157 medWaitTime: false,
1158 elu: false
1159 })
1160 await pool.destroy()
1161 pool = new DynamicThreadPool(
1162 min,
1163 max,
1164 './tests/worker-files/thread/testWorker.js',
1165 { workerChoiceStrategy }
1166 )
1167 expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
1168 runTime: false,
1169 avgRunTime: false,
1170 medRunTime: false,
1171 waitTime: false,
1172 avgWaitTime: false,
1173 medWaitTime: false,
1174 elu: false
1175 })
1176 // We need to clean up the resources after our test
1177 await pool.destroy()
1178 })
1179
1180 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
1181 const pool = new FixedThreadPool(
1182 max,
1183 './tests/worker-files/thread/testWorker.js',
1184 {
1185 workerChoiceStrategy:
1186 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1187 }
1188 )
1189 // TODO: Create a better test to cover `InterleavedWeightedRoundRobinWorkerChoiceStrategy#choose`
1190 const promises = new Set()
1191 const maxMultiplier = 2
1192 for (let i = 0; i < max * maxMultiplier; i++) {
1193 promises.add(pool.execute())
1194 }
1195 await Promise.all(promises)
1196 for (const workerNode of pool.workerNodes) {
1197 expect(workerNode.workerUsage).toStrictEqual({
1198 tasks: {
1199 executed: maxMultiplier,
1200 executing: 0,
1201 queued: 0,
1202 failed: 0
1203 },
1204 runTime: {
1205 aggregation: 0,
1206 average: 0,
1207 median: 0,
1208 history: expect.any(CircularArray)
1209 },
1210 waitTime: {
1211 aggregation: 0,
1212 average: 0,
1213 median: 0,
1214 history: expect.any(CircularArray)
1215 },
1216 elu: undefined
1217 })
1218 }
1219 expect(
1220 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1221 pool.workerChoiceStrategyContext.workerChoiceStrategy
1222 ).defaultWorkerWeight
1223 ).toBeGreaterThan(0)
1224 expect(
1225 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1226 pool.workerChoiceStrategyContext.workerChoiceStrategy
1227 ).currentRoundId
1228 ).toBe(0)
1229 expect(
1230 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1231 pool.workerChoiceStrategyContext.workerChoiceStrategy
1232 ).currentWorkerNodeId
1233 ).toBe(0)
1234 expect(
1235 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1236 pool.workerChoiceStrategyContext.workerChoiceStrategy
1237 ).roundWeights
1238 ).toStrictEqual([
1239 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1240 pool.workerChoiceStrategyContext.workerChoiceStrategy
1241 ).defaultWorkerWeight
1242 ])
1243 // We need to clean up the resources after our test
1244 await pool.destroy()
1245 })
1246
1247 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
1248 const pool = new DynamicThreadPool(
1249 min,
1250 max,
1251 './tests/worker-files/thread/testWorker.js',
1252 {
1253 workerChoiceStrategy:
1254 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1255 }
1256 )
1257 // TODO: Create a better test to cover `InterleavedWeightedRoundRobinWorkerChoiceStrategy#choose`
1258 const promises = new Set()
1259 const maxMultiplier = 2
1260 for (let i = 0; i < max * maxMultiplier; i++) {
1261 promises.add(pool.execute())
1262 }
1263 await Promise.all(promises)
1264 for (const workerNode of pool.workerNodes) {
1265 expect(workerNode.workerUsage).toStrictEqual({
1266 tasks: {
1267 executed: maxMultiplier,
1268 executing: 0,
1269 queued: 0,
1270 failed: 0
1271 },
1272 runTime: {
1273 aggregation: 0,
1274 average: 0,
1275 median: 0,
1276 history: expect.any(CircularArray)
1277 },
1278 waitTime: {
1279 aggregation: 0,
1280 average: 0,
1281 median: 0,
1282 history: expect.any(CircularArray)
1283 },
1284 elu: undefined
1285 })
1286 }
1287 expect(
1288 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1289 pool.workerChoiceStrategyContext.workerChoiceStrategy
1290 ).defaultWorkerWeight
1291 ).toBeGreaterThan(0)
1292 expect(
1293 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1294 pool.workerChoiceStrategyContext.workerChoiceStrategy
1295 ).currentRoundId
1296 ).toBe(0)
1297 expect(
1298 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1299 pool.workerChoiceStrategyContext.workerChoiceStrategy
1300 ).currentWorkerNodeId
1301 ).toBe(0)
1302 expect(
1303 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1304 pool.workerChoiceStrategyContext.workerChoiceStrategy
1305 ).roundWeights
1306 ).toStrictEqual([
1307 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1308 pool.workerChoiceStrategyContext.workerChoiceStrategy
1309 ).defaultWorkerWeight
1310 ])
1311 // We need to clean up the resources after our test
1312 await pool.destroy()
1313 })
1314
1315 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
1316 const workerChoiceStrategy =
1317 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1318 let pool = new FixedThreadPool(
1319 max,
1320 './tests/worker-files/thread/testWorker.js'
1321 )
1322 expect(
1323 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1324 workerChoiceStrategy
1325 ).currentRoundId
1326 ).toBeDefined()
1327 expect(
1328 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1329 workerChoiceStrategy
1330 ).currentWorkerNodeId
1331 ).toBeDefined()
1332 expect(
1333 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1334 workerChoiceStrategy
1335 ).defaultWorkerWeight
1336 ).toBeDefined()
1337 expect(
1338 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1339 workerChoiceStrategy
1340 ).roundWeights
1341 ).toBeDefined()
1342 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
1343 expect(
1344 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1345 workerChoiceStrategy
1346 ).currentRoundId
1347 ).toBe(0)
1348 expect(
1349 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1350 pool.workerChoiceStrategyContext.workerChoiceStrategy
1351 ).currentWorkerNodeId
1352 ).toBe(0)
1353 expect(
1354 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1355 pool.workerChoiceStrategyContext.workerChoiceStrategy
1356 ).defaultWorkerWeight
1357 ).toBeGreaterThan(0)
1358 expect(
1359 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1360 workerChoiceStrategy
1361 ).roundWeights
1362 ).toStrictEqual([
1363 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1364 pool.workerChoiceStrategyContext.workerChoiceStrategy
1365 ).defaultWorkerWeight
1366 ])
1367 await pool.destroy()
1368 pool = new DynamicThreadPool(
1369 min,
1370 max,
1371 './tests/worker-files/thread/testWorker.js'
1372 )
1373 expect(
1374 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1375 workerChoiceStrategy
1376 ).currentRoundId
1377 ).toBeDefined()
1378 expect(
1379 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1380 workerChoiceStrategy
1381 ).currentWorkerNodeId
1382 ).toBeDefined()
1383 expect(
1384 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1385 workerChoiceStrategy
1386 ).defaultWorkerWeight
1387 ).toBeDefined()
1388 expect(
1389 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1390 workerChoiceStrategy
1391 ).roundWeights
1392 ).toBeDefined()
1393 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
1394 expect(
1395 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1396 pool.workerChoiceStrategyContext.workerChoiceStrategy
1397 ).currentWorkerNodeId
1398 ).toBe(0)
1399 expect(
1400 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1401 pool.workerChoiceStrategyContext.workerChoiceStrategy
1402 ).defaultWorkerWeight
1403 ).toBeGreaterThan(0)
1404 expect(
1405 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1406 workerChoiceStrategy
1407 ).roundWeights
1408 ).toStrictEqual([
1409 pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
1410 pool.workerChoiceStrategyContext.workerChoiceStrategy
1411 ).defaultWorkerWeight
1412 ])
1413 // We need to clean up the resources after our test
1414 await pool.destroy()
1415 })
1416
1417 it('Verify unknown strategy throw error', () => {
1418 expect(
1419 () =>
1420 new DynamicThreadPool(
1421 min,
1422 max,
1423 './tests/worker-files/thread/testWorker.js',
1424 { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
1425 )
1426 ).toThrowError("Invalid worker choice strategy 'UNKNOWN_STRATEGY'")
1427 })
1428 })