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_RECENTLY_USED
).toBe('LESS_RECENTLY_USED')
16 expect(WorkerChoiceStrategies
.FAIR_SHARE
).toBe('FAIR_SHARE')
17 expect(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
).toBe(
18 'WEIGHTED_ROUND_ROBIN'
22 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
23 const pool
= new DynamicThreadPool(
26 './tests/worker-files/thread/testWorker.js'
28 expect(pool
.opts
.workerChoiceStrategy
).toBe(
29 WorkerChoiceStrategies
.ROUND_ROBIN
31 // We need to clean up the resources after our test
35 it('Verify ROUND_ROBIN strategy is taken at pool creation', async () => {
36 const pool
= new FixedThreadPool(
38 './tests/worker-files/thread/testWorker.js',
39 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
41 expect(pool
.opts
.workerChoiceStrategy
).toBe(
42 WorkerChoiceStrategies
.ROUND_ROBIN
45 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy().nextWorkerIndex
47 // We need to clean up the resources after our test
51 it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
52 const pool
= new DynamicThreadPool(
55 './tests/worker-files/thread/testWorker.js'
57 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
58 expect(pool
.opts
.workerChoiceStrategy
).toBe(
59 WorkerChoiceStrategies
.ROUND_ROBIN
61 // We need to clean up the resources after our test
65 it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
66 let pool
= new FixedThreadPool(
68 './tests/worker-files/thread/testWorker.js'
70 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
72 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
73 .requiredStatistics
.runTime
76 pool
= new DynamicThreadPool(
79 './tests/worker-files/thread/testWorker.js'
81 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
83 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
84 .requiredStatistics
.runTime
86 // We need to clean up the resources after our test
90 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
91 const pool
= new FixedThreadPool(
93 './tests/worker-files/thread/testWorker.js',
94 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
96 expect(pool
.opts
.workerChoiceStrategy
).toBe(
97 WorkerChoiceStrategies
.ROUND_ROBIN
99 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
101 for (let i
= 0; i
< max
* 2; i
++) {
102 promises
.push(pool
.execute())
104 await Promise
.all(promises
)
105 // We need to clean up the resources after our test
109 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
110 const pool
= new DynamicThreadPool(
113 './tests/worker-files/thread/testWorker.js',
114 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
116 expect(pool
.opts
.workerChoiceStrategy
).toBe(
117 WorkerChoiceStrategies
.ROUND_ROBIN
119 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
121 for (let i
= 0; i
< max
* 2; i
++) {
122 promises
.push(pool
.execute())
124 await Promise
.all(promises
)
125 // We need to clean up the resources after our test
129 it('Verify ROUND_ROBIN strategy runtime behavior', async () => {
130 let pool
= new FixedClusterPool(
132 './tests/worker-files/cluster/testWorker.js'
134 let results
= new Set()
135 for (let i
= 0; i
< max
; i
++) {
136 results
.add(pool
.chooseWorker().id
)
138 expect(results
.size
).toBe(max
)
140 pool
= new FixedThreadPool(max
, './tests/worker-files/thread/testWorker.js')
142 for (let i
= 0; i
< max
; i
++) {
143 results
.add(pool
.chooseWorker().threadId
)
145 expect(results
.size
).toBe(max
)
149 it('Verify ROUND_ROBIN strategy internals are resets after setting it', async () => {
150 let pool
= new FixedThreadPool(
152 './tests/worker-files/thread/testWorker.js',
153 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
156 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy().nextWorkerIndex
158 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
160 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy().nextWorkerIndex
163 pool
= new DynamicThreadPool(
166 './tests/worker-files/thread/testWorker.js',
167 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
170 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
171 .workerChoiceStrategy
.nextWorkerIndex
173 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
175 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
176 .workerChoiceStrategy
.nextWorkerIndex
178 // We need to clean up the resources after our test
182 it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
183 const pool
= new FixedThreadPool(
185 './tests/worker-files/thread/testWorker.js',
186 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
188 expect(pool
.opts
.workerChoiceStrategy
).toBe(
189 WorkerChoiceStrategies
.LESS_RECENTLY_USED
191 // We need to clean up the resources after our test
195 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
196 const pool
= new FixedThreadPool(
198 './tests/worker-files/thread/testWorker.js'
200 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
201 expect(pool
.opts
.workerChoiceStrategy
).toBe(
202 WorkerChoiceStrategies
.LESS_RECENTLY_USED
204 // We need to clean up the resources after our test
208 it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => {
209 let pool
= new FixedThreadPool(
211 './tests/worker-files/thread/testWorker.js'
213 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
215 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
216 .requiredStatistics
.runTime
219 pool
= new DynamicThreadPool(
222 './tests/worker-files/thread/testWorker.js'
224 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
226 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
227 .requiredStatistics
.runTime
229 // We need to clean up the resources after our test
233 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
234 const pool
= new FixedThreadPool(
236 './tests/worker-files/thread/testWorker.js',
237 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
239 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
241 for (let i
= 0; i
< max
* 2; i
++) {
242 promises
.push(pool
.execute())
244 await Promise
.all(promises
)
245 // We need to clean up the resources after our test
249 it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
250 const pool
= new DynamicThreadPool(
253 './tests/worker-files/thread/testWorker.js',
254 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
256 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
258 for (let i
= 0; i
< max
* 2; i
++) {
259 promises
.push(pool
.execute())
261 await Promise
.all(promises
)
262 // We need to clean up the resources after our test
266 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
267 const pool
= new FixedThreadPool(
269 './tests/worker-files/thread/testWorker.js',
270 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
272 expect(pool
.opts
.workerChoiceStrategy
).toBe(
273 WorkerChoiceStrategies
.FAIR_SHARE
275 for (const worker
of pool
.workerChoiceStrategyContext
276 .getWorkerChoiceStrategy()
277 .workerLastVirtualTaskTimestamp
.keys()) {
279 pool
.workerChoiceStrategyContext
280 .getWorkerChoiceStrategy()
281 .workerLastVirtualTaskTimestamp
.get(worker
).start
284 pool
.workerChoiceStrategyContext
285 .getWorkerChoiceStrategy()
286 .workerLastVirtualTaskTimestamp
.get(worker
).end
289 // We need to clean up the resources after our test
293 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
294 const pool
= new FixedThreadPool(
296 './tests/worker-files/thread/testWorker.js'
298 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
299 expect(pool
.opts
.workerChoiceStrategy
).toBe(
300 WorkerChoiceStrategies
.FAIR_SHARE
302 // We need to clean up the resources after our test
306 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
307 let pool
= new FixedThreadPool(
309 './tests/worker-files/thread/testWorker.js'
311 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
313 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
314 .requiredStatistics
.runTime
317 pool
= new DynamicThreadPool(
320 './tests/worker-files/thread/testWorker.js'
322 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
324 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
325 .requiredStatistics
.runTime
327 // We need to clean up the resources after our test
331 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
332 const pool
= new FixedThreadPool(
334 './tests/worker-files/thread/testWorker.js',
335 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
337 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
339 for (let i
= 0; i
< max
* 2; i
++) {
340 promises
.push(pool
.execute())
342 await Promise
.all(promises
)
343 // We need to clean up the resources after our test
347 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
348 const pool
= new DynamicThreadPool(
351 './tests/worker-files/thread/testWorker.js',
352 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
354 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
356 for (let i
= 0; i
< max
* 2; i
++) {
357 promises
.push(pool
.execute())
359 await Promise
.all(promises
)
360 // We need to clean up the resources after our test
364 it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => {
365 let pool
= new FixedThreadPool(
367 './tests/worker-files/thread/testWorker.js'
370 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
371 .workerLastVirtualTaskTimestamp
373 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
374 for (const worker
of pool
.workerChoiceStrategyContext
375 .getWorkerChoiceStrategy()
376 .workerLastVirtualTaskTimestamp
.keys()) {
378 pool
.workerChoiceStrategyContext
379 .getWorkerChoiceStrategy()
380 .workerLastVirtualTaskTimestamp
.get(worker
).start
383 pool
.workerChoiceStrategyContext
384 .getWorkerChoiceStrategy()
385 .workerLastVirtualTaskTimestamp
.get(worker
).end
389 pool
= new DynamicThreadPool(
392 './tests/worker-files/thread/testWorker.js'
395 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
396 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
398 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
399 for (const worker
of pool
.workerChoiceStrategyContext
400 .getWorkerChoiceStrategy()
401 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.keys()) {
403 pool
.workerChoiceStrategyContext
404 .getWorkerChoiceStrategy()
405 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(worker
).start
408 pool
.workerChoiceStrategyContext
409 .getWorkerChoiceStrategy()
410 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(worker
).end
413 // We need to clean up the resources after our test
417 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
418 const pool
= new FixedThreadPool(
420 './tests/worker-files/thread/testWorker.js',
421 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
423 expect(pool
.opts
.workerChoiceStrategy
).toBe(
424 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
427 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
431 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
434 for (const worker
of pool
.workerChoiceStrategyContext
435 .getWorkerChoiceStrategy()
436 .workersTaskRunTime
.keys()) {
438 pool
.workerChoiceStrategyContext
439 .getWorkerChoiceStrategy()
440 .workersTaskRunTime
.get(worker
).weight
443 pool
.workerChoiceStrategyContext
444 .getWorkerChoiceStrategy()
445 .workersTaskRunTime
.get(worker
).runTime
448 // We need to clean up the resources after our test
452 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
453 const pool
= new FixedThreadPool(
455 './tests/worker-files/thread/testWorker.js'
457 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
458 expect(pool
.opts
.workerChoiceStrategy
).toBe(
459 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
461 // We need to clean up the resources after our test
465 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
466 let pool
= new FixedThreadPool(
468 './tests/worker-files/thread/testWorker.js'
470 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
472 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
473 .requiredStatistics
.runTime
476 pool
= new DynamicThreadPool(
479 './tests/worker-files/thread/testWorker.js'
481 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
483 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
484 .requiredStatistics
.runTime
486 // We need to clean up the resources after our test
490 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
491 const pool
= new FixedThreadPool(
493 './tests/worker-files/thread/testWorker.js',
494 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
496 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
498 for (let i
= 0; i
< max
* 2; i
++) {
499 promises
.push(pool
.execute())
501 await Promise
.all(promises
)
502 // We need to clean up the resources after our test
506 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
507 const pool
= new DynamicThreadPool(
510 './tests/worker-files/thread/testWorker.js',
511 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
513 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
515 for (let i
= 0; i
< max
* 2; i
++) {
516 promises
.push(pool
.execute())
518 await Promise
.all(promises
)
519 // We need to clean up the resources after our test
523 it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
524 let pool
= new FixedThreadPool(
526 './tests/worker-files/thread/testWorker.js'
529 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
533 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
537 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
540 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
542 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
546 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
549 for (const worker
of pool
.workerChoiceStrategyContext
550 .getWorkerChoiceStrategy()
551 .workersTaskRunTime
.keys()) {
553 pool
.workerChoiceStrategyContext
554 .getWorkerChoiceStrategy()
555 .workersTaskRunTime
.get(worker
).runTime
559 pool
= new DynamicThreadPool(
562 './tests/worker-files/thread/testWorker.js'
565 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
566 .workerChoiceStrategy
.currentWorkerIndex
569 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
570 .workerChoiceStrategy
.defaultWorkerWeight
573 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
574 .workerChoiceStrategy
.workersTaskRunTime
576 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
578 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
579 .workerChoiceStrategy
.currentWorkerIndex
582 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
583 .workerChoiceStrategy
.defaultWorkerWeight
585 for (const worker
of pool
.workerChoiceStrategyContext
586 .getWorkerChoiceStrategy()
587 .workerChoiceStrategy
.workersTaskRunTime
.keys()) {
589 pool
.workerChoiceStrategyContext
590 .getWorkerChoiceStrategy()
591 .workerChoiceStrategy
.workersTaskRunTime
.get(worker
).runTime
594 // We need to clean up the resources after our test
598 it('Verify unknown strategies throw error', () => {
601 new DynamicThreadPool(
604 './tests/worker-files/thread/testWorker.js',
605 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
608 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")