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
.workerChoiceStrategy
.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
.getRequiredStatistics().runTime
76 pool
= new DynamicThreadPool(
79 './tests/worker-files/thread/testWorker.js'
81 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
83 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
85 // We need to clean up the resources after our test
89 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
90 const pool
= new FixedThreadPool(
92 './tests/worker-files/thread/testWorker.js',
93 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
95 expect(pool
.opts
.workerChoiceStrategy
).toBe(
96 WorkerChoiceStrategies
.ROUND_ROBIN
98 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
100 for (let i
= 0; i
< max
* 2; i
++) {
101 promises
.push(pool
.execute())
103 await Promise
.all(promises
)
104 // We need to clean up the resources after our test
108 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
109 const pool
= new DynamicThreadPool(
112 './tests/worker-files/thread/testWorker.js',
113 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
115 expect(pool
.opts
.workerChoiceStrategy
).toBe(
116 WorkerChoiceStrategies
.ROUND_ROBIN
118 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
120 for (let i
= 0; i
< max
* 2; i
++) {
121 promises
.push(pool
.execute())
123 await Promise
.all(promises
)
124 // We need to clean up the resources after our test
128 it('Verify ROUND_ROBIN strategy runtime behavior', async () => {
129 let pool
= new FixedClusterPool(
131 './tests/worker-files/cluster/testWorker.js'
133 let results
= new Set()
134 for (let i
= 0; i
< max
; i
++) {
135 results
.add(pool
.chooseWorker()[1].id
)
137 expect(results
.size
).toBe(max
)
139 pool
= new FixedThreadPool(max
, './tests/worker-files/thread/testWorker.js')
141 for (let i
= 0; i
< max
; i
++) {
142 results
.add(pool
.chooseWorker()[1].threadId
)
144 expect(results
.size
).toBe(max
)
148 it('Verify ROUND_ROBIN strategy internals are resets after setting it', async () => {
149 let pool
= new FixedThreadPool(
151 './tests/worker-files/thread/testWorker.js',
152 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
155 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.nextWorkerId
157 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
159 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.nextWorkerId
162 pool
= new DynamicThreadPool(
165 './tests/worker-files/thread/testWorker.js',
166 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
169 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
172 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
174 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
177 // We need to clean up the resources after our test
181 it('Verify LESS_USED strategy is taken at pool creation', async () => {
182 const pool
= new FixedThreadPool(
184 './tests/worker-files/thread/testWorker.js',
185 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_USED
}
187 expect(pool
.opts
.workerChoiceStrategy
).toBe(
188 WorkerChoiceStrategies
.LESS_USED
190 // We need to clean up the resources after our test
194 it('Verify LESS_USED strategy can be set after pool creation', async () => {
195 const pool
= new FixedThreadPool(
197 './tests/worker-files/thread/testWorker.js'
199 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_USED
)
200 expect(pool
.opts
.workerChoiceStrategy
).toBe(
201 WorkerChoiceStrategies
.LESS_USED
203 // We need to clean up the resources after our test
207 it('Verify LESS_USED strategy default tasks usage statistics requirements', async () => {
208 let pool
= new FixedThreadPool(
210 './tests/worker-files/thread/testWorker.js'
212 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_USED
)
214 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
217 pool
= new DynamicThreadPool(
220 './tests/worker-files/thread/testWorker.js'
222 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_USED
)
224 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
226 // We need to clean up the resources after our test
230 it('Verify LESS_USED strategy can be run in a fixed pool', async () => {
231 const pool
= new FixedThreadPool(
233 './tests/worker-files/thread/testWorker.js',
234 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_USED
}
236 // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose`
238 for (let i
= 0; i
< max
* 2; i
++) {
239 promises
.push(pool
.execute())
241 await Promise
.all(promises
)
242 // We need to clean up the resources after our test
246 it('Verify LESS_USED strategy can be run in a dynamic pool', async () => {
247 const pool
= new DynamicThreadPool(
250 './tests/worker-files/thread/testWorker.js',
251 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_USED
}
253 // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose`
255 for (let i
= 0; i
< max
* 2; i
++) {
256 promises
.push(pool
.execute())
258 await Promise
.all(promises
)
259 // We need to clean up the resources after our test
263 it('Verify LESS_BUSY strategy is taken at pool creation', async () => {
264 const pool
= new FixedThreadPool(
266 './tests/worker-files/thread/testWorker.js',
267 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_BUSY
}
269 expect(pool
.opts
.workerChoiceStrategy
).toBe(
270 WorkerChoiceStrategies
.LESS_BUSY
272 // We need to clean up the resources after our test
276 it('Verify LESS_BUSY strategy can be set after pool creation', async () => {
277 const pool
= new FixedThreadPool(
279 './tests/worker-files/thread/testWorker.js'
281 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_BUSY
)
282 expect(pool
.opts
.workerChoiceStrategy
).toBe(
283 WorkerChoiceStrategies
.LESS_BUSY
285 // We need to clean up the resources after our test
289 it('Verify LESS_BUSY strategy default tasks usage statistics requirements', async () => {
290 let pool
= new FixedThreadPool(
292 './tests/worker-files/thread/testWorker.js'
294 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_BUSY
)
296 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
299 pool
= new DynamicThreadPool(
302 './tests/worker-files/thread/testWorker.js'
304 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_BUSY
)
306 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
308 // We need to clean up the resources after our test
312 it('Verify LESS_BUSY strategy can be run in a fixed pool', async () => {
313 const pool
= new FixedThreadPool(
315 './tests/worker-files/thread/testWorker.js',
316 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_BUSY
}
318 // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose`
320 for (let i
= 0; i
< max
* 2; i
++) {
321 promises
.push(pool
.execute())
323 await Promise
.all(promises
)
324 // We need to clean up the resources after our test
328 it('Verify LESS_BUSY strategy can be run in a dynamic pool', async () => {
329 const pool
= new DynamicThreadPool(
332 './tests/worker-files/thread/testWorker.js',
333 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_BUSY
}
335 // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose`
337 for (let i
= 0; i
< max
* 2; i
++) {
338 promises
.push(pool
.execute())
340 await Promise
.all(promises
)
341 // We need to clean up the resources after our test
345 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
346 const pool
= new FixedThreadPool(
348 './tests/worker-files/thread/testWorker.js',
349 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
351 expect(pool
.opts
.workerChoiceStrategy
).toBe(
352 WorkerChoiceStrategies
.FAIR_SHARE
354 for (const workerKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.keys()) {
356 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
361 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
366 // We need to clean up the resources after our test
370 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
371 const pool
= new FixedThreadPool(
373 './tests/worker-files/thread/testWorker.js'
375 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
376 expect(pool
.opts
.workerChoiceStrategy
).toBe(
377 WorkerChoiceStrategies
.FAIR_SHARE
379 // We need to clean up the resources after our test
383 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
384 let pool
= new FixedThreadPool(
386 './tests/worker-files/thread/testWorker.js'
388 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
390 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
393 pool
= new DynamicThreadPool(
396 './tests/worker-files/thread/testWorker.js'
398 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
400 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
402 // We need to clean up the resources after our test
406 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
407 const pool
= new FixedThreadPool(
409 './tests/worker-files/thread/testWorker.js',
410 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
412 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
414 for (let i
= 0; i
< max
* 2; i
++) {
415 promises
.push(pool
.execute())
417 await Promise
.all(promises
)
419 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
420 .workerLastVirtualTaskTimestamp
.size
421 ).toBe(pool
.workers
.length
)
422 // We need to clean up the resources after our test
426 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
427 const pool
= new DynamicThreadPool(
430 './tests/worker-files/thread/testWorker.js',
431 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
433 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
435 const maxMultiplier
= 4
436 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
437 promises
.push(pool
.execute())
439 await Promise
.all(promises
)
441 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
442 .workerLastVirtualTaskTimestamp
.size
443 ).toBe(pool
.workers
.length
)
444 // We need to clean up the resources after our test
448 it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => {
449 let pool
= new FixedThreadPool(
451 './tests/worker-files/thread/testWorker.js'
454 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
455 .workerLastVirtualTaskTimestamp
457 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
458 for (const workerKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.keys()) {
460 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
465 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
471 pool
= new DynamicThreadPool(
474 './tests/worker-files/thread/testWorker.js'
477 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
478 .workerLastVirtualTaskTimestamp
480 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
481 for (const workerKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.keys()) {
483 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
488 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
493 // We need to clean up the resources after our test
497 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
498 const pool
= new FixedThreadPool(
500 './tests/worker-files/thread/testWorker.js',
501 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
503 expect(pool
.opts
.workerChoiceStrategy
).toBe(
504 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
507 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.currentWorkerId
510 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.defaultWorkerWeight
512 for (const workerKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
.keys()) {
514 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
.get(
519 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
.get(
524 // We need to clean up the resources after our test
528 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
529 const pool
= new FixedThreadPool(
531 './tests/worker-files/thread/testWorker.js'
533 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
534 expect(pool
.opts
.workerChoiceStrategy
).toBe(
535 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
537 // We need to clean up the resources after our test
541 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
542 let pool
= new FixedThreadPool(
544 './tests/worker-files/thread/testWorker.js'
546 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
548 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
551 pool
= new DynamicThreadPool(
554 './tests/worker-files/thread/testWorker.js'
556 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
558 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
560 // We need to clean up the resources after our test
564 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
565 const pool
= new FixedThreadPool(
567 './tests/worker-files/thread/testWorker.js',
568 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
570 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
572 for (let i
= 0; i
< max
* 2; i
++) {
573 promises
.push(pool
.execute())
575 await Promise
.all(promises
)
577 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
579 ).toBe(pool
.workers
.length
)
580 // We need to clean up the resources after our test
584 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
585 const pool
= new DynamicThreadPool(
588 './tests/worker-files/thread/testWorker.js',
589 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
591 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
593 const maxMultiplier
=
594 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
596 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
597 promises
.push(pool
.execute())
599 await Promise
.all(promises
)
601 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
602 .workersTaskRunTime
.size
603 ).toBe(pool
.workers
.length
)
604 // We need to clean up the resources after our test
608 it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
609 let pool
= new FixedThreadPool(
611 './tests/worker-files/thread/testWorker.js'
614 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.currentWorkerId
617 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.defaultWorkerWeight
620 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
622 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
624 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.currentWorkerId
627 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.defaultWorkerWeight
629 for (const workerKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
.keys()) {
631 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
.get(
637 pool
= new DynamicThreadPool(
640 './tests/worker-files/thread/testWorker.js'
643 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
647 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
651 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
654 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
656 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
660 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
663 for (const workerKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
.workersTaskRunTime
.keys()) {
665 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
.workersTaskRunTime
.get(
670 // We need to clean up the resources after our test
674 it('Verify unknown strategies throw error', () => {
677 new DynamicThreadPool(
680 './tests/worker-files/thread/testWorker.js',
681 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
684 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")