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 for (let i
= 0; i
< max
* 2; i
++) {
436 promises
.push(pool
.execute())
438 await Promise
.all(promises
)
440 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
441 .workerLastVirtualTaskTimestamp
.size
442 ).toBe(pool
.workers
.length
)
443 // We need to clean up the resources after our test
447 it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => {
448 let pool
= new FixedThreadPool(
450 './tests/worker-files/thread/testWorker.js'
453 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
454 .workerLastVirtualTaskTimestamp
456 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
457 for (const workerKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.keys()) {
459 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
464 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
470 pool
= new DynamicThreadPool(
473 './tests/worker-files/thread/testWorker.js'
476 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
477 .workerLastVirtualTaskTimestamp
479 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
480 for (const workerKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.keys()) {
482 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
487 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
492 // We need to clean up the resources after our test
496 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
497 const pool
= new FixedThreadPool(
499 './tests/worker-files/thread/testWorker.js',
500 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
502 expect(pool
.opts
.workerChoiceStrategy
).toBe(
503 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
506 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.currentWorkerId
509 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.defaultWorkerWeight
511 for (const workerKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
.keys()) {
513 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
.get(
518 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
.get(
523 // We need to clean up the resources after our test
527 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
528 const pool
= new FixedThreadPool(
530 './tests/worker-files/thread/testWorker.js'
532 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
533 expect(pool
.opts
.workerChoiceStrategy
).toBe(
534 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
536 // We need to clean up the resources after our test
540 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
541 let pool
= new FixedThreadPool(
543 './tests/worker-files/thread/testWorker.js'
545 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
547 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
550 pool
= new DynamicThreadPool(
553 './tests/worker-files/thread/testWorker.js'
555 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
557 pool
.workerChoiceStrategyContext
.getRequiredStatistics().runTime
559 // We need to clean up the resources after our test
563 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
564 const pool
= new FixedThreadPool(
566 './tests/worker-files/thread/testWorker.js',
567 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
569 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
571 for (let i
= 0; i
< max
* 2; i
++) {
572 promises
.push(pool
.execute())
574 await Promise
.all(promises
)
576 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
578 ).toBe(pool
.workers
.length
)
579 // We need to clean up the resources after our test
583 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
584 const pool
= new DynamicThreadPool(
587 './tests/worker-files/thread/testWorker.js',
588 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
590 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
592 const maxMultiplier
=
593 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
595 for (let i
= 0; i
< max
* maxMultiplier
; i
++) {
596 promises
.push(pool
.execute())
598 await Promise
.all(promises
)
600 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
601 .workersTaskRunTime
.size
602 ).toBe(pool
.workers
.length
)
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
.workerChoiceStrategy
.currentWorkerId
616 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.defaultWorkerWeight
619 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
621 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
623 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.currentWorkerId
626 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.defaultWorkerWeight
628 for (const workerKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
.keys()) {
630 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
.get(
636 pool
= new DynamicThreadPool(
639 './tests/worker-files/thread/testWorker.js'
642 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
646 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
650 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
653 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
655 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
659 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
662 for (const workerKey
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
.workersTaskRunTime
.keys()) {
664 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
.workersTaskRunTime
.get(
669 // We need to clean up the resources after our test
673 it('Verify unknown strategies throw error', () => {
676 new DynamicThreadPool(
679 './tests/worker-files/thread/testWorker.js',
680 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
683 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")