fix: ensure worker removal impact is propated to 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/index')
8
9 describe('Selection strategies test suite', () => {
10 const min = 0
11 const max = 3
12
13 it('Verify that WorkerChoiceStrategies enumeration provides string values', () => {
14 expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN')
15 expect(WorkerChoiceStrategies.LESS_USED).toBe('LESS_USED')
16 expect(WorkerChoiceStrategies.LESS_BUSY).toBe('LESS_BUSY')
17 expect(WorkerChoiceStrategies.FAIR_SHARE).toBe('FAIR_SHARE')
18 expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe(
19 'WEIGHTED_ROUND_ROBIN'
20 )
21 })
22
23 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
24 const pool = new DynamicThreadPool(
25 min,
26 max,
27 './tests/worker-files/thread/testWorker.js'
28 )
29 expect(pool.opts.workerChoiceStrategy).toBe(
30 WorkerChoiceStrategies.ROUND_ROBIN
31 )
32 // We need to clean up the resources after our test
33 await pool.destroy()
34 })
35
36 it('Verify ROUND_ROBIN strategy is taken at pool creation', async () => {
37 const pool = new FixedThreadPool(
38 max,
39 './tests/worker-files/thread/testWorker.js',
40 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
41 )
42 expect(pool.opts.workerChoiceStrategy).toBe(
43 WorkerChoiceStrategies.ROUND_ROBIN
44 )
45 expect(
46 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerId
47 ).toBe(0)
48 // We need to clean up the resources after our test
49 await pool.destroy()
50 })
51
52 it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
53 const pool = new DynamicThreadPool(
54 min,
55 max,
56 './tests/worker-files/thread/testWorker.js'
57 )
58 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
59 expect(pool.opts.workerChoiceStrategy).toBe(
60 WorkerChoiceStrategies.ROUND_ROBIN
61 )
62 // We need to clean up the resources after our test
63 await pool.destroy()
64 })
65
66 it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
67 let pool = new FixedThreadPool(
68 max,
69 './tests/worker-files/thread/testWorker.js'
70 )
71 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
72 expect(
73 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
74 ).toBe(false)
75 await pool.destroy()
76 pool = new DynamicThreadPool(
77 min,
78 max,
79 './tests/worker-files/thread/testWorker.js'
80 )
81 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
82 expect(
83 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
84 ).toBe(false)
85 // We need to clean up the resources after our test
86 await pool.destroy()
87 })
88
89 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
90 const pool = new FixedThreadPool(
91 max,
92 './tests/worker-files/thread/testWorker.js',
93 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
94 )
95 expect(pool.opts.workerChoiceStrategy).toBe(
96 WorkerChoiceStrategies.ROUND_ROBIN
97 )
98 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
99 const promises = []
100 for (let i = 0; i < max * 2; i++) {
101 promises.push(pool.execute())
102 }
103 await Promise.all(promises)
104 // We need to clean up the resources after our test
105 await pool.destroy()
106 })
107
108 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
109 const pool = new DynamicThreadPool(
110 min,
111 max,
112 './tests/worker-files/thread/testWorker.js',
113 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
114 )
115 expect(pool.opts.workerChoiceStrategy).toBe(
116 WorkerChoiceStrategies.ROUND_ROBIN
117 )
118 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
119 const promises = []
120 for (let i = 0; i < max * 2; i++) {
121 promises.push(pool.execute())
122 }
123 await Promise.all(promises)
124 // We need to clean up the resources after our test
125 await pool.destroy()
126 })
127
128 it('Verify ROUND_ROBIN strategy runtime behavior', async () => {
129 let pool = new FixedClusterPool(
130 max,
131 './tests/worker-files/cluster/testWorker.js'
132 )
133 let results = new Set()
134 for (let i = 0; i < max; i++) {
135 results.add(pool.chooseWorker()[1].id)
136 }
137 expect(results.size).toBe(max)
138 await pool.destroy()
139 pool = new FixedThreadPool(max, './tests/worker-files/thread/testWorker.js')
140 results = new Set()
141 for (let i = 0; i < max; i++) {
142 results.add(pool.chooseWorker()[1].threadId)
143 }
144 expect(results.size).toBe(max)
145 await pool.destroy()
146 })
147
148 it('Verify ROUND_ROBIN strategy internals are resets after setting it', async () => {
149 let pool = new FixedThreadPool(
150 max,
151 './tests/worker-files/thread/testWorker.js',
152 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
153 )
154 expect(
155 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerId
156 ).toBeUndefined()
157 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
158 expect(
159 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerId
160 ).toBe(0)
161 await pool.destroy()
162 pool = new DynamicThreadPool(
163 min,
164 max,
165 './tests/worker-files/thread/testWorker.js',
166 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
167 )
168 expect(
169 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
170 .workerChoiceStrategy.nextWorkerId
171 ).toBeUndefined()
172 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
173 expect(
174 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
175 .workerChoiceStrategy.nextWorkerId
176 ).toBe(0)
177 // We need to clean up the resources after our test
178 await pool.destroy()
179 })
180
181 it('Verify LESS_USED strategy is taken at pool creation', async () => {
182 const pool = new FixedThreadPool(
183 max,
184 './tests/worker-files/thread/testWorker.js',
185 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED }
186 )
187 expect(pool.opts.workerChoiceStrategy).toBe(
188 WorkerChoiceStrategies.LESS_USED
189 )
190 // We need to clean up the resources after our test
191 await pool.destroy()
192 })
193
194 it('Verify LESS_USED strategy can be set after pool creation', async () => {
195 const pool = new FixedThreadPool(
196 max,
197 './tests/worker-files/thread/testWorker.js'
198 )
199 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED)
200 expect(pool.opts.workerChoiceStrategy).toBe(
201 WorkerChoiceStrategies.LESS_USED
202 )
203 // We need to clean up the resources after our test
204 await pool.destroy()
205 })
206
207 it('Verify LESS_USED strategy default tasks usage statistics requirements', async () => {
208 let pool = new FixedThreadPool(
209 max,
210 './tests/worker-files/thread/testWorker.js'
211 )
212 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED)
213 expect(
214 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
215 ).toBe(false)
216 await pool.destroy()
217 pool = new DynamicThreadPool(
218 min,
219 max,
220 './tests/worker-files/thread/testWorker.js'
221 )
222 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED)
223 expect(
224 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
225 ).toBe(false)
226 // We need to clean up the resources after our test
227 await pool.destroy()
228 })
229
230 it('Verify LESS_USED strategy can be run in a fixed pool', async () => {
231 const pool = new FixedThreadPool(
232 max,
233 './tests/worker-files/thread/testWorker.js',
234 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED }
235 )
236 // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose`
237 const promises = []
238 for (let i = 0; i < max * 2; i++) {
239 promises.push(pool.execute())
240 }
241 await Promise.all(promises)
242 // We need to clean up the resources after our test
243 await pool.destroy()
244 })
245
246 it('Verify LESS_USED strategy can be run in a dynamic pool', async () => {
247 const pool = new DynamicThreadPool(
248 min,
249 max,
250 './tests/worker-files/thread/testWorker.js',
251 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED }
252 )
253 // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose`
254 const promises = []
255 for (let i = 0; i < max * 2; i++) {
256 promises.push(pool.execute())
257 }
258 await Promise.all(promises)
259 // We need to clean up the resources after our test
260 await pool.destroy()
261 })
262
263 it('Verify LESS_BUSY strategy is taken at pool creation', async () => {
264 const pool = new FixedThreadPool(
265 max,
266 './tests/worker-files/thread/testWorker.js',
267 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY }
268 )
269 expect(pool.opts.workerChoiceStrategy).toBe(
270 WorkerChoiceStrategies.LESS_BUSY
271 )
272 // We need to clean up the resources after our test
273 await pool.destroy()
274 })
275
276 it('Verify LESS_BUSY strategy can be set after pool creation', async () => {
277 const pool = new FixedThreadPool(
278 max,
279 './tests/worker-files/thread/testWorker.js'
280 )
281 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY)
282 expect(pool.opts.workerChoiceStrategy).toBe(
283 WorkerChoiceStrategies.LESS_BUSY
284 )
285 // We need to clean up the resources after our test
286 await pool.destroy()
287 })
288
289 it('Verify LESS_BUSY strategy default tasks usage statistics requirements', async () => {
290 let pool = new FixedThreadPool(
291 max,
292 './tests/worker-files/thread/testWorker.js'
293 )
294 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY)
295 expect(
296 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
297 ).toBe(true)
298 await pool.destroy()
299 pool = new DynamicThreadPool(
300 min,
301 max,
302 './tests/worker-files/thread/testWorker.js'
303 )
304 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY)
305 expect(
306 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
307 ).toBe(true)
308 // We need to clean up the resources after our test
309 await pool.destroy()
310 })
311
312 it('Verify LESS_BUSY strategy can be run in a fixed pool', async () => {
313 const pool = new FixedThreadPool(
314 max,
315 './tests/worker-files/thread/testWorker.js',
316 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY }
317 )
318 // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose`
319 const promises = []
320 for (let i = 0; i < max * 2; i++) {
321 promises.push(pool.execute())
322 }
323 await Promise.all(promises)
324 // We need to clean up the resources after our test
325 await pool.destroy()
326 })
327
328 it('Verify LESS_BUSY strategy can be run in a dynamic pool', async () => {
329 const pool = new DynamicThreadPool(
330 min,
331 max,
332 './tests/worker-files/thread/testWorker.js',
333 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY }
334 )
335 // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose`
336 const promises = []
337 for (let i = 0; i < max * 2; i++) {
338 promises.push(pool.execute())
339 }
340 await Promise.all(promises)
341 // We need to clean up the resources after our test
342 await pool.destroy()
343 })
344
345 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
346 const pool = new FixedThreadPool(
347 max,
348 './tests/worker-files/thread/testWorker.js',
349 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
350 )
351 expect(pool.opts.workerChoiceStrategy).toBe(
352 WorkerChoiceStrategies.FAIR_SHARE
353 )
354 for (const workerKey of pool.workerChoiceStrategyContext
355 .getWorkerChoiceStrategy()
356 .workerLastVirtualTaskTimestamp.keys()) {
357 expect(
358 pool.workerChoiceStrategyContext
359 .getWorkerChoiceStrategy()
360 .workerLastVirtualTaskTimestamp.get(workerKey).start
361 ).toBe(0)
362 expect(
363 pool.workerChoiceStrategyContext
364 .getWorkerChoiceStrategy()
365 .workerLastVirtualTaskTimestamp.get(workerKey).end
366 ).toBe(0)
367 }
368 // We need to clean up the resources after our test
369 await pool.destroy()
370 })
371
372 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
373 const pool = new FixedThreadPool(
374 max,
375 './tests/worker-files/thread/testWorker.js'
376 )
377 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
378 expect(pool.opts.workerChoiceStrategy).toBe(
379 WorkerChoiceStrategies.FAIR_SHARE
380 )
381 // We need to clean up the resources after our test
382 await pool.destroy()
383 })
384
385 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
386 let pool = new FixedThreadPool(
387 max,
388 './tests/worker-files/thread/testWorker.js'
389 )
390 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
391 expect(
392 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
393 ).toBe(true)
394 await pool.destroy()
395 pool = new DynamicThreadPool(
396 min,
397 max,
398 './tests/worker-files/thread/testWorker.js'
399 )
400 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
401 expect(
402 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
403 ).toBe(true)
404 // We need to clean up the resources after our test
405 await pool.destroy()
406 })
407
408 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
409 const pool = new FixedThreadPool(
410 max,
411 './tests/worker-files/thread/testWorker.js',
412 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
413 )
414 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
415 const promises = []
416 for (let i = 0; i < max * 2; i++) {
417 promises.push(pool.execute())
418 }
419 await Promise.all(promises)
420 expect(
421 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
422 .workerLastVirtualTaskTimestamp.size
423 ).toBe(pool.workers.length)
424 // We need to clean up the resources after our test
425 await pool.destroy()
426 })
427
428 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
429 const pool = new DynamicThreadPool(
430 min,
431 max,
432 './tests/worker-files/thread/testWorker.js',
433 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
434 )
435 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
436 const promises = []
437 for (let i = 0; i < max * 2; i++) {
438 promises.push(pool.execute())
439 }
440 await Promise.all(promises)
441 // expect(
442 // pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
443 // .workerChoiceStrategy.workerLastVirtualTaskTimestamp.size
444 // ).toBe(pool.workers.length)
445 // We need to clean up the resources after our test
446 await pool.destroy()
447 })
448
449 it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => {
450 let pool = new FixedThreadPool(
451 max,
452 './tests/worker-files/thread/testWorker.js'
453 )
454 expect(
455 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
456 .workerLastVirtualTaskTimestamp
457 ).toBeUndefined()
458 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
459 for (const workerKey of pool.workerChoiceStrategyContext
460 .getWorkerChoiceStrategy()
461 .workerLastVirtualTaskTimestamp.keys()) {
462 expect(
463 pool.workerChoiceStrategyContext
464 .getWorkerChoiceStrategy()
465 .workerLastVirtualTaskTimestamp.get(workerKey).start
466 ).toBe(0)
467 expect(
468 pool.workerChoiceStrategyContext
469 .getWorkerChoiceStrategy()
470 .workerLastVirtualTaskTimestamp.get(workerKey).end
471 ).toBe(0)
472 }
473 await pool.destroy()
474 pool = new DynamicThreadPool(
475 min,
476 max,
477 './tests/worker-files/thread/testWorker.js'
478 )
479 expect(
480 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
481 .workerChoiceStrategy.workerLastVirtualTaskTimestamp
482 ).toBeUndefined()
483 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
484 for (const workerKey of pool.workerChoiceStrategyContext
485 .getWorkerChoiceStrategy()
486 .workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) {
487 expect(
488 pool.workerChoiceStrategyContext
489 .getWorkerChoiceStrategy()
490 .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(workerKey)
491 .start
492 ).toBe(0)
493 expect(
494 pool.workerChoiceStrategyContext
495 .getWorkerChoiceStrategy()
496 .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(workerKey)
497 .end
498 ).toBe(0)
499 }
500 // We need to clean up the resources after our test
501 await pool.destroy()
502 })
503
504 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
505 const pool = new FixedThreadPool(
506 max,
507 './tests/worker-files/thread/testWorker.js',
508 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
509 )
510 expect(pool.opts.workerChoiceStrategy).toBe(
511 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
512 )
513 expect(
514 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
515 ).toBe(0)
516 expect(
517 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
518 .defaultWorkerWeight
519 ).toBeGreaterThan(0)
520 for (const workerKey of pool.workerChoiceStrategyContext
521 .getWorkerChoiceStrategy()
522 .workersTaskRunTime.keys()) {
523 expect(
524 pool.workerChoiceStrategyContext
525 .getWorkerChoiceStrategy()
526 .workersTaskRunTime.get(workerKey).weight
527 ).toBeGreaterThan(0)
528 expect(
529 pool.workerChoiceStrategyContext
530 .getWorkerChoiceStrategy()
531 .workersTaskRunTime.get(workerKey).runTime
532 ).toBe(0)
533 }
534 // We need to clean up the resources after our test
535 await pool.destroy()
536 })
537
538 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
539 const pool = new FixedThreadPool(
540 max,
541 './tests/worker-files/thread/testWorker.js'
542 )
543 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
544 expect(pool.opts.workerChoiceStrategy).toBe(
545 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
546 )
547 // We need to clean up the resources after our test
548 await pool.destroy()
549 })
550
551 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
552 let pool = new FixedThreadPool(
553 max,
554 './tests/worker-files/thread/testWorker.js'
555 )
556 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
557 expect(
558 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
559 ).toBe(true)
560 await pool.destroy()
561 pool = new DynamicThreadPool(
562 min,
563 max,
564 './tests/worker-files/thread/testWorker.js'
565 )
566 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
567 expect(
568 pool.workerChoiceStrategyContext.getRequiredStatistics().runTime
569 ).toBe(true)
570 // We need to clean up the resources after our test
571 await pool.destroy()
572 })
573
574 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
575 const pool = new FixedThreadPool(
576 max,
577 './tests/worker-files/thread/testWorker.js',
578 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
579 )
580 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
581 const promises = []
582 for (let i = 0; i < max * 2; i++) {
583 promises.push(pool.execute())
584 }
585 await Promise.all(promises)
586 expect(
587 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
588 .workersTaskRunTime.size
589 ).toBe(pool.workers.length)
590 // We need to clean up the resources after our test
591 await pool.destroy()
592 })
593
594 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
595 const pool = new DynamicThreadPool(
596 min,
597 max,
598 './tests/worker-files/thread/testWorker.js',
599 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
600 )
601 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
602 const promises = []
603 for (let i = 0; i < max * 2; i++) {
604 promises.push(pool.execute())
605 }
606 await Promise.all(promises)
607 // expect(
608 // pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
609 // .workerChoiceStrategy.workersTaskRunTime.size
610 // ).toBe(pool.workers.length)
611 // We need to clean up the resources after our test
612 await pool.destroy()
613 })
614
615 it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
616 let pool = new FixedThreadPool(
617 max,
618 './tests/worker-files/thread/testWorker.js'
619 )
620 expect(
621 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
622 ).toBeUndefined()
623 expect(
624 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
625 .defaultWorkerWeight
626 ).toBeUndefined()
627 expect(
628 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
629 .workersTaskRunTime
630 ).toBeUndefined()
631 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
632 expect(
633 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
634 ).toBe(0)
635 expect(
636 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
637 .defaultWorkerWeight
638 ).toBeGreaterThan(0)
639 for (const workerKey of pool.workerChoiceStrategyContext
640 .getWorkerChoiceStrategy()
641 .workersTaskRunTime.keys()) {
642 expect(
643 pool.workerChoiceStrategyContext
644 .getWorkerChoiceStrategy()
645 .workersTaskRunTime.get(workerKey).runTime
646 ).toBe(0)
647 }
648 await pool.destroy()
649 pool = new DynamicThreadPool(
650 min,
651 max,
652 './tests/worker-files/thread/testWorker.js'
653 )
654 expect(
655 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
656 .workerChoiceStrategy.currentWorkerId
657 ).toBeUndefined()
658 expect(
659 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
660 .workerChoiceStrategy.defaultWorkerWeight
661 ).toBeUndefined()
662 expect(
663 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
664 .workerChoiceStrategy.workersTaskRunTime
665 ).toBeUndefined()
666 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
667 expect(
668 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
669 .workerChoiceStrategy.currentWorkerId
670 ).toBe(0)
671 expect(
672 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
673 .workerChoiceStrategy.defaultWorkerWeight
674 ).toBeGreaterThan(0)
675 for (const workerKey of pool.workerChoiceStrategyContext
676 .getWorkerChoiceStrategy()
677 .workerChoiceStrategy.workersTaskRunTime.keys()) {
678 expect(
679 pool.workerChoiceStrategyContext
680 .getWorkerChoiceStrategy()
681 .workerChoiceStrategy.workersTaskRunTime.get(workerKey).runTime
682 ).toBe(0)
683 }
684 // We need to clean up the resources after our test
685 await pool.destroy()
686 })
687
688 it('Verify unknown strategies throw error', () => {
689 expect(
690 () =>
691 new DynamicThreadPool(
692 min,
693 max,
694 './tests/worker-files/thread/testWorker.js',
695 { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
696 )
697 ).toThrowError(
698 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
699 )
700 })
701 })