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()[1].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()[1].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 workerKey
of pool
.workerChoiceStrategyContext
361 .getWorkerChoiceStrategy()
362 .workerLastVirtualTaskTimestamp
.keys()) {
364 pool
.workerChoiceStrategyContext
365 .getWorkerChoiceStrategy()
366 .workerLastVirtualTaskTimestamp
.get(workerKey
).start
369 pool
.workerChoiceStrategyContext
370 .getWorkerChoiceStrategy()
371 .workerLastVirtualTaskTimestamp
.get(workerKey
).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 workerKey
of pool
.workerChoiceStrategyContext
460 .getWorkerChoiceStrategy()
461 .workerLastVirtualTaskTimestamp
.keys()) {
463 pool
.workerChoiceStrategyContext
464 .getWorkerChoiceStrategy()
465 .workerLastVirtualTaskTimestamp
.get(workerKey
).start
468 pool
.workerChoiceStrategyContext
469 .getWorkerChoiceStrategy()
470 .workerLastVirtualTaskTimestamp
.get(workerKey
).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 workerKey
of pool
.workerChoiceStrategyContext
485 .getWorkerChoiceStrategy()
486 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.keys()) {
488 pool
.workerChoiceStrategyContext
489 .getWorkerChoiceStrategy()
490 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(workerKey
)
494 pool
.workerChoiceStrategyContext
495 .getWorkerChoiceStrategy()
496 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(workerKey
)
500 // We need to clean up the resources after our test
504 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
505 const pool
= new FixedThreadPool(
507 './tests/worker-files/thread/testWorker.js',
508 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
510 expect(pool
.opts
.workerChoiceStrategy
).toBe(
511 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
514 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy().currentWorkerId
517 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
520 for (const workerKey
of pool
.workerChoiceStrategyContext
521 .getWorkerChoiceStrategy()
522 .workersTaskRunTime
.keys()) {
524 pool
.workerChoiceStrategyContext
525 .getWorkerChoiceStrategy()
526 .workersTaskRunTime
.get(workerKey
).weight
529 pool
.workerChoiceStrategyContext
530 .getWorkerChoiceStrategy()
531 .workersTaskRunTime
.get(workerKey
).runTime
534 // We need to clean up the resources after our test
538 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
539 const pool
= new FixedThreadPool(
541 './tests/worker-files/thread/testWorker.js'
543 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
544 expect(pool
.opts
.workerChoiceStrategy
).toBe(
545 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
547 // We need to clean up the resources after our test
551 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
552 let pool
= new FixedThreadPool(
554 './tests/worker-files/thread/testWorker.js'
556 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
558 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
559 .requiredStatistics
.runTime
562 pool
= new DynamicThreadPool(
565 './tests/worker-files/thread/testWorker.js'
567 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
569 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
570 .requiredStatistics
.runTime
572 // We need to clean up the resources after our test
576 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
577 const pool
= new FixedThreadPool(
579 './tests/worker-files/thread/testWorker.js',
580 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
582 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
584 for (let i
= 0; i
< max
* 2; i
++) {
585 promises
.push(pool
.execute())
587 await Promise
.all(promises
)
588 // We need to clean up the resources after our test
592 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
593 const pool
= new DynamicThreadPool(
596 './tests/worker-files/thread/testWorker.js',
597 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
599 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
601 for (let i
= 0; i
< max
* 2; i
++) {
602 promises
.push(pool
.execute())
604 await Promise
.all(promises
)
605 // We need to clean up the resources after our test
609 it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
610 let pool
= new FixedThreadPool(
612 './tests/worker-files/thread/testWorker.js'
615 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy().currentWorkerId
618 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
622 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
625 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
627 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy().currentWorkerId
630 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
633 for (const workerKey
of pool
.workerChoiceStrategyContext
634 .getWorkerChoiceStrategy()
635 .workersTaskRunTime
.keys()) {
637 pool
.workerChoiceStrategyContext
638 .getWorkerChoiceStrategy()
639 .workersTaskRunTime
.get(workerKey
).runTime
643 pool
= new DynamicThreadPool(
646 './tests/worker-files/thread/testWorker.js'
649 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
650 .workerChoiceStrategy
.currentWorkerId
653 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
654 .workerChoiceStrategy
.defaultWorkerWeight
657 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
658 .workerChoiceStrategy
.workersTaskRunTime
660 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
662 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
663 .workerChoiceStrategy
.currentWorkerId
666 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
667 .workerChoiceStrategy
.defaultWorkerWeight
669 for (const workerKey
of pool
.workerChoiceStrategyContext
670 .getWorkerChoiceStrategy()
671 .workerChoiceStrategy
.workersTaskRunTime
.keys()) {
673 pool
.workerChoiceStrategyContext
674 .getWorkerChoiceStrategy()
675 .workerChoiceStrategy
.workersTaskRunTime
.get(workerKey
).runTime
678 // We need to clean up the resources after our test
682 it('Verify unknown strategies throw error', () => {
685 new DynamicThreadPool(
688 './tests/worker-files/thread/testWorker.js',
689 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
692 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")