1 const { expect
} = require('expect')
3 WorkerChoiceStrategies
,
7 } = require('../../../lib/index')
9 describe('Selection strategies test suite', () => {
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'
23 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
24 const pool
= new DynamicThreadPool(
27 './tests/worker-files/thread/testWorker.js'
29 expect(pool
.opts
.workerChoiceStrategy
).toBe(
30 WorkerChoiceStrategies
.ROUND_ROBIN
32 // We need to clean up the resources after our test
36 it('Verify ROUND_ROBIN strategy is taken at pool creation', async () => {
37 const pool
= new FixedThreadPool(
39 './tests/worker-files/thread/testWorker.js',
40 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
42 expect(pool
.opts
.workerChoiceStrategy
).toBe(
43 WorkerChoiceStrategies
.ROUND_ROBIN
46 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy().nextWorkerId
48 // We need to clean up the resources after our test
52 it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
53 const pool
= new DynamicThreadPool(
56 './tests/worker-files/thread/testWorker.js'
58 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
59 expect(pool
.opts
.workerChoiceStrategy
).toBe(
60 WorkerChoiceStrategies
.ROUND_ROBIN
62 // We need to clean up the resources after our test
66 it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
67 let pool
= new FixedThreadPool(
69 './tests/worker-files/thread/testWorker.js'
71 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
73 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
74 .requiredStatistics
.runTime
77 pool
= new DynamicThreadPool(
80 './tests/worker-files/thread/testWorker.js'
82 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
84 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
85 .requiredStatistics
.runTime
87 // We need to clean up the resources after our test
91 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
92 const pool
= new FixedThreadPool(
94 './tests/worker-files/thread/testWorker.js',
95 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
97 expect(pool
.opts
.workerChoiceStrategy
).toBe(
98 WorkerChoiceStrategies
.ROUND_ROBIN
100 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
102 for (let i
= 0; i
< max
* 2; i
++) {
103 promises
.push(pool
.execute())
105 await Promise
.all(promises
)
106 // We need to clean up the resources after our test
110 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
111 const pool
= new DynamicThreadPool(
114 './tests/worker-files/thread/testWorker.js',
115 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
117 expect(pool
.opts
.workerChoiceStrategy
).toBe(
118 WorkerChoiceStrategies
.ROUND_ROBIN
120 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
122 for (let i
= 0; i
< max
* 2; i
++) {
123 promises
.push(pool
.execute())
125 await Promise
.all(promises
)
126 // We need to clean up the resources after our test
130 it('Verify ROUND_ROBIN strategy runtime behavior', async () => {
131 let pool
= new FixedClusterPool(
133 './tests/worker-files/cluster/testWorker.js'
135 let results
= new Set()
136 for (let i
= 0; i
< max
; i
++) {
137 results
.add(pool
.chooseWorker().id
)
139 expect(results
.size
).toBe(max
)
141 pool
= new FixedThreadPool(max
, './tests/worker-files/thread/testWorker.js')
143 for (let i
= 0; i
< max
; i
++) {
144 results
.add(pool
.chooseWorker().threadId
)
146 expect(results
.size
).toBe(max
)
150 it('Verify ROUND_ROBIN strategy internals are resets after setting it', async () => {
151 let pool
= new FixedThreadPool(
153 './tests/worker-files/thread/testWorker.js',
154 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
157 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy().nextWorkerId
159 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
161 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy().nextWorkerId
164 pool
= new DynamicThreadPool(
167 './tests/worker-files/thread/testWorker.js',
168 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
171 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
172 .workerChoiceStrategy
.nextWorkerId
174 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
176 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
177 .workerChoiceStrategy
.nextWorkerId
179 // We need to clean up the resources after our test
183 it('Verify LESS_USED strategy is taken at pool creation', async () => {
184 const pool
= new FixedThreadPool(
186 './tests/worker-files/thread/testWorker.js',
187 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_USED
}
189 expect(pool
.opts
.workerChoiceStrategy
).toBe(
190 WorkerChoiceStrategies
.LESS_USED
192 // We need to clean up the resources after our test
196 it('Verify LESS_USED strategy can be set after pool creation', async () => {
197 const pool
= new FixedThreadPool(
199 './tests/worker-files/thread/testWorker.js'
201 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_USED
)
202 expect(pool
.opts
.workerChoiceStrategy
).toBe(
203 WorkerChoiceStrategies
.LESS_USED
205 // We need to clean up the resources after our test
209 it('Verify LESS_USED strategy default tasks usage statistics requirements', async () => {
210 let pool
= new FixedThreadPool(
212 './tests/worker-files/thread/testWorker.js'
214 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_USED
)
216 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
217 .requiredStatistics
.runTime
220 pool
= new DynamicThreadPool(
223 './tests/worker-files/thread/testWorker.js'
225 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_USED
)
227 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
228 .requiredStatistics
.runTime
230 // We need to clean up the resources after our test
234 it('Verify LESS_USED strategy can be run in a fixed pool', async () => {
235 const pool
= new FixedThreadPool(
237 './tests/worker-files/thread/testWorker.js',
238 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_USED
}
240 // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose`
242 for (let i
= 0; i
< max
* 2; i
++) {
243 promises
.push(pool
.execute())
245 await Promise
.all(promises
)
246 // We need to clean up the resources after our test
250 it('Verify LESS_USED strategy can be run in a dynamic pool', async () => {
251 const pool
= new DynamicThreadPool(
254 './tests/worker-files/thread/testWorker.js',
255 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_USED
}
257 // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose`
259 for (let i
= 0; i
< max
* 2; i
++) {
260 promises
.push(pool
.execute())
262 await Promise
.all(promises
)
263 // We need to clean up the resources after our test
267 it('Verify LESS_BUSY strategy is taken at pool creation', async () => {
268 const pool
= new FixedThreadPool(
270 './tests/worker-files/thread/testWorker.js',
271 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_BUSY
}
273 expect(pool
.opts
.workerChoiceStrategy
).toBe(
274 WorkerChoiceStrategies
.LESS_BUSY
276 // We need to clean up the resources after our test
280 it('Verify LESS_BUSY strategy can be set after pool creation', async () => {
281 const pool
= new FixedThreadPool(
283 './tests/worker-files/thread/testWorker.js'
285 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_BUSY
)
286 expect(pool
.opts
.workerChoiceStrategy
).toBe(
287 WorkerChoiceStrategies
.LESS_BUSY
289 // We need to clean up the resources after our test
293 it('Verify LESS_BUSY strategy default tasks usage statistics requirements', async () => {
294 let pool
= new FixedThreadPool(
296 './tests/worker-files/thread/testWorker.js'
298 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_BUSY
)
300 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
301 .requiredStatistics
.runTime
304 pool
= new DynamicThreadPool(
307 './tests/worker-files/thread/testWorker.js'
309 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_BUSY
)
311 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
312 .requiredStatistics
.runTime
314 // We need to clean up the resources after our test
318 it('Verify LESS_BUSY strategy can be run in a fixed pool', async () => {
319 const pool
= new FixedThreadPool(
321 './tests/worker-files/thread/testWorker.js',
322 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_BUSY
}
324 // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose`
326 for (let i
= 0; i
< max
* 2; i
++) {
327 promises
.push(pool
.execute())
329 await Promise
.all(promises
)
330 // We need to clean up the resources after our test
334 it('Verify LESS_BUSY strategy can be run in a dynamic pool', async () => {
335 const pool
= new DynamicThreadPool(
338 './tests/worker-files/thread/testWorker.js',
339 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_BUSY
}
341 // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose`
343 for (let i
= 0; i
< max
* 2; i
++) {
344 promises
.push(pool
.execute())
346 await Promise
.all(promises
)
347 // We need to clean up the resources after our test
351 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
352 const pool
= new FixedThreadPool(
354 './tests/worker-files/thread/testWorker.js',
355 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
357 expect(pool
.opts
.workerChoiceStrategy
).toBe(
358 WorkerChoiceStrategies
.FAIR_SHARE
360 for (const worker
of pool
.workerChoiceStrategyContext
361 .getWorkerChoiceStrategy()
362 .workerLastVirtualTaskTimestamp
.keys()) {
364 pool
.workerChoiceStrategyContext
365 .getWorkerChoiceStrategy()
366 .workerLastVirtualTaskTimestamp
.get(worker
).start
369 pool
.workerChoiceStrategyContext
370 .getWorkerChoiceStrategy()
371 .workerLastVirtualTaskTimestamp
.get(worker
).end
374 // We need to clean up the resources after our test
378 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
379 const pool
= new FixedThreadPool(
381 './tests/worker-files/thread/testWorker.js'
383 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
384 expect(pool
.opts
.workerChoiceStrategy
).toBe(
385 WorkerChoiceStrategies
.FAIR_SHARE
387 // We need to clean up the resources after our test
391 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
392 let pool
= new FixedThreadPool(
394 './tests/worker-files/thread/testWorker.js'
396 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
398 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
399 .requiredStatistics
.runTime
402 pool
= new DynamicThreadPool(
405 './tests/worker-files/thread/testWorker.js'
407 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
409 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
410 .requiredStatistics
.runTime
412 // We need to clean up the resources after our test
416 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
417 const pool
= new FixedThreadPool(
419 './tests/worker-files/thread/testWorker.js',
420 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
422 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
424 for (let i
= 0; i
< max
* 2; i
++) {
425 promises
.push(pool
.execute())
427 await Promise
.all(promises
)
428 // We need to clean up the resources after our test
432 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
433 const pool
= new DynamicThreadPool(
436 './tests/worker-files/thread/testWorker.js',
437 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
439 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
441 for (let i
= 0; i
< max
* 2; i
++) {
442 promises
.push(pool
.execute())
444 await Promise
.all(promises
)
445 // We need to clean up the resources after our test
449 it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => {
450 let pool
= new FixedThreadPool(
452 './tests/worker-files/thread/testWorker.js'
455 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
456 .workerLastVirtualTaskTimestamp
458 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
459 for (const worker
of pool
.workerChoiceStrategyContext
460 .getWorkerChoiceStrategy()
461 .workerLastVirtualTaskTimestamp
.keys()) {
463 pool
.workerChoiceStrategyContext
464 .getWorkerChoiceStrategy()
465 .workerLastVirtualTaskTimestamp
.get(worker
).start
468 pool
.workerChoiceStrategyContext
469 .getWorkerChoiceStrategy()
470 .workerLastVirtualTaskTimestamp
.get(worker
).end
474 pool
= new DynamicThreadPool(
477 './tests/worker-files/thread/testWorker.js'
480 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
481 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
483 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
484 for (const worker
of pool
.workerChoiceStrategyContext
485 .getWorkerChoiceStrategy()
486 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.keys()) {
488 pool
.workerChoiceStrategyContext
489 .getWorkerChoiceStrategy()
490 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(worker
).start
493 pool
.workerChoiceStrategyContext
494 .getWorkerChoiceStrategy()
495 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(worker
).end
498 // We need to clean up the resources after our test
502 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
503 const pool
= new FixedThreadPool(
505 './tests/worker-files/thread/testWorker.js',
506 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
508 expect(pool
.opts
.workerChoiceStrategy
).toBe(
509 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
512 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy().currentWorkerId
515 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
518 for (const worker
of pool
.workerChoiceStrategyContext
519 .getWorkerChoiceStrategy()
520 .workersTaskRunTime
.keys()) {
522 pool
.workerChoiceStrategyContext
523 .getWorkerChoiceStrategy()
524 .workersTaskRunTime
.get(worker
).weight
527 pool
.workerChoiceStrategyContext
528 .getWorkerChoiceStrategy()
529 .workersTaskRunTime
.get(worker
).runTime
532 // We need to clean up the resources after our test
536 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
537 const pool
= new FixedThreadPool(
539 './tests/worker-files/thread/testWorker.js'
541 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
542 expect(pool
.opts
.workerChoiceStrategy
).toBe(
543 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
545 // We need to clean up the resources after our test
549 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
550 let pool
= new FixedThreadPool(
552 './tests/worker-files/thread/testWorker.js'
554 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
556 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
557 .requiredStatistics
.runTime
560 pool
= new DynamicThreadPool(
563 './tests/worker-files/thread/testWorker.js'
565 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
567 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
568 .requiredStatistics
.runTime
570 // We need to clean up the resources after our test
574 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
575 const pool
= new FixedThreadPool(
577 './tests/worker-files/thread/testWorker.js',
578 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
580 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
582 for (let i
= 0; i
< max
* 2; i
++) {
583 promises
.push(pool
.execute())
585 await Promise
.all(promises
)
586 // We need to clean up the resources after our test
590 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
591 const pool
= new DynamicThreadPool(
594 './tests/worker-files/thread/testWorker.js',
595 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
597 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
599 for (let i
= 0; i
< max
* 2; i
++) {
600 promises
.push(pool
.execute())
602 await Promise
.all(promises
)
603 // We need to clean up the resources after our test
607 it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
608 let pool
= new FixedThreadPool(
610 './tests/worker-files/thread/testWorker.js'
613 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy().currentWorkerId
616 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
620 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
623 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
625 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy().currentWorkerId
628 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
631 for (const worker
of pool
.workerChoiceStrategyContext
632 .getWorkerChoiceStrategy()
633 .workersTaskRunTime
.keys()) {
635 pool
.workerChoiceStrategyContext
636 .getWorkerChoiceStrategy()
637 .workersTaskRunTime
.get(worker
).runTime
641 pool
= new DynamicThreadPool(
644 './tests/worker-files/thread/testWorker.js'
647 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
648 .workerChoiceStrategy
.currentWorkerId
651 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
652 .workerChoiceStrategy
.defaultWorkerWeight
655 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
656 .workerChoiceStrategy
.workersTaskRunTime
658 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
660 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
661 .workerChoiceStrategy
.currentWorkerId
664 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
665 .workerChoiceStrategy
.defaultWorkerWeight
667 for (const worker
of pool
.workerChoiceStrategyContext
668 .getWorkerChoiceStrategy()
669 .workerChoiceStrategy
.workersTaskRunTime
.keys()) {
671 pool
.workerChoiceStrategyContext
672 .getWorkerChoiceStrategy()
673 .workerChoiceStrategy
.workersTaskRunTime
.get(worker
).runTime
676 // We need to clean up the resources after our test
680 it('Verify unknown strategies throw error', () => {
683 new DynamicThreadPool(
686 './tests/worker-files/thread/testWorker.js',
687 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
690 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")