1 const { expect
} = require('expect')
3 WorkerChoiceStrategies
,
6 } = require('../../../lib/index')
8 describe('Selection strategies test suite', () => {
12 it('Verify that WorkerChoiceStrategies enumeration provides string values', () => {
13 expect(WorkerChoiceStrategies
.ROUND_ROBIN
).toBe('ROUND_ROBIN')
14 expect(WorkerChoiceStrategies
.LESS_RECENTLY_USED
).toBe('LESS_RECENTLY_USED')
15 expect(WorkerChoiceStrategies
.FAIR_SHARE
).toBe('FAIR_SHARE')
16 expect(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
).toBe(
17 'WEIGHTED_ROUND_ROBIN'
21 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
22 const pool
= new DynamicThreadPool(
25 './tests/worker-files/thread/testWorker.js'
27 expect(pool
.opts
.workerChoiceStrategy
).toBe(
28 WorkerChoiceStrategies
.ROUND_ROBIN
30 // We need to clean up the resources after our test
34 it('Verify ROUND_ROBIN strategy is taken at pool creation', async () => {
35 const pool
= new FixedThreadPool(
37 './tests/worker-files/thread/testWorker.js',
38 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
40 expect(pool
.opts
.workerChoiceStrategy
).toBe(
41 WorkerChoiceStrategies
.ROUND_ROBIN
44 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy().nextWorkerIndex
46 // We need to clean up the resources after our test
50 it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
51 const pool
= new DynamicThreadPool(
54 './tests/worker-files/thread/testWorker.js'
56 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
57 expect(pool
.opts
.workerChoiceStrategy
).toBe(
58 WorkerChoiceStrategies
.ROUND_ROBIN
60 // We need to clean up the resources after our test
64 it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
65 let pool
= new FixedThreadPool(
67 './tests/worker-files/thread/testWorker.js'
69 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
71 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
72 .requiredStatistics
.runTime
75 pool
= new DynamicThreadPool(
78 './tests/worker-files/thread/testWorker.js'
80 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
82 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
83 .requiredStatistics
.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 internals are resets after setting it', async () => {
129 let pool
= new FixedThreadPool(
131 './tests/worker-files/thread/testWorker.js',
132 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
135 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy().nextWorkerIndex
137 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
139 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy().nextWorkerIndex
142 pool
= new DynamicThreadPool(
145 './tests/worker-files/thread/testWorker.js',
146 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
149 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
150 .workerChoiceStrategy
.nextWorkerIndex
152 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
154 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
155 .workerChoiceStrategy
.nextWorkerIndex
157 // We need to clean up the resources after our test
161 it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
162 const pool
= new FixedThreadPool(
164 './tests/worker-files/thread/testWorker.js',
165 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
167 expect(pool
.opts
.workerChoiceStrategy
).toBe(
168 WorkerChoiceStrategies
.LESS_RECENTLY_USED
170 // We need to clean up the resources after our test
174 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
175 const pool
= new FixedThreadPool(
177 './tests/worker-files/thread/testWorker.js'
179 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
180 expect(pool
.opts
.workerChoiceStrategy
).toBe(
181 WorkerChoiceStrategies
.LESS_RECENTLY_USED
183 // We need to clean up the resources after our test
187 it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => {
188 let pool
= new FixedThreadPool(
190 './tests/worker-files/thread/testWorker.js'
192 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
194 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
195 .requiredStatistics
.runTime
198 pool
= new DynamicThreadPool(
201 './tests/worker-files/thread/testWorker.js'
203 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
205 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
206 .requiredStatistics
.runTime
208 // We need to clean up the resources after our test
212 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
213 const pool
= new FixedThreadPool(
215 './tests/worker-files/thread/testWorker.js',
216 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
218 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
220 for (let i
= 0; i
< max
* 2; i
++) {
221 promises
.push(pool
.execute())
223 await Promise
.all(promises
)
224 // We need to clean up the resources after our test
228 it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
229 const pool
= new DynamicThreadPool(
232 './tests/worker-files/thread/testWorker.js',
233 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
235 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
237 for (let i
= 0; i
< max
* 2; i
++) {
238 promises
.push(pool
.execute())
240 await Promise
.all(promises
)
241 // We need to clean up the resources after our test
245 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
246 const pool
= new FixedThreadPool(
248 './tests/worker-files/thread/testWorker.js',
249 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
251 expect(pool
.opts
.workerChoiceStrategy
).toBe(
252 WorkerChoiceStrategies
.FAIR_SHARE
254 for (const worker
of pool
.workerChoiceStrategyContext
255 .getWorkerChoiceStrategy()
256 .workerLastVirtualTaskTimestamp
.keys()) {
258 pool
.workerChoiceStrategyContext
259 .getWorkerChoiceStrategy()
260 .workerLastVirtualTaskTimestamp
.get(worker
).start
263 pool
.workerChoiceStrategyContext
264 .getWorkerChoiceStrategy()
265 .workerLastVirtualTaskTimestamp
.get(worker
).end
268 // We need to clean up the resources after our test
272 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
273 const pool
= new FixedThreadPool(
275 './tests/worker-files/thread/testWorker.js'
277 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
278 expect(pool
.opts
.workerChoiceStrategy
).toBe(
279 WorkerChoiceStrategies
.FAIR_SHARE
281 // We need to clean up the resources after our test
285 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
286 let pool
= new FixedThreadPool(
288 './tests/worker-files/thread/testWorker.js'
290 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
292 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
293 .requiredStatistics
.runTime
296 pool
= new DynamicThreadPool(
299 './tests/worker-files/thread/testWorker.js'
301 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
303 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
304 .requiredStatistics
.runTime
306 // We need to clean up the resources after our test
310 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
311 const pool
= new FixedThreadPool(
313 './tests/worker-files/thread/testWorker.js',
314 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
316 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
318 for (let i
= 0; i
< max
* 2; i
++) {
319 promises
.push(pool
.execute())
321 await Promise
.all(promises
)
322 // We need to clean up the resources after our test
326 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
327 const pool
= new DynamicThreadPool(
330 './tests/worker-files/thread/testWorker.js',
331 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
333 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
335 for (let i
= 0; i
< max
* 2; i
++) {
336 promises
.push(pool
.execute())
338 await Promise
.all(promises
)
339 // We need to clean up the resources after our test
343 it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => {
344 let pool
= new FixedThreadPool(
346 './tests/worker-files/thread/testWorker.js'
349 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
350 .workerLastVirtualTaskTimestamp
352 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
353 for (const worker
of pool
.workerChoiceStrategyContext
354 .getWorkerChoiceStrategy()
355 .workerLastVirtualTaskTimestamp
.keys()) {
357 pool
.workerChoiceStrategyContext
358 .getWorkerChoiceStrategy()
359 .workerLastVirtualTaskTimestamp
.get(worker
).start
362 pool
.workerChoiceStrategyContext
363 .getWorkerChoiceStrategy()
364 .workerLastVirtualTaskTimestamp
.get(worker
).end
368 pool
= new DynamicThreadPool(
371 './tests/worker-files/thread/testWorker.js'
374 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
375 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
377 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
378 for (const worker
of pool
.workerChoiceStrategyContext
379 .getWorkerChoiceStrategy()
380 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.keys()) {
382 pool
.workerChoiceStrategyContext
383 .getWorkerChoiceStrategy()
384 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(worker
).start
387 pool
.workerChoiceStrategyContext
388 .getWorkerChoiceStrategy()
389 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(worker
).end
392 // We need to clean up the resources after our test
396 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
397 const pool
= new FixedThreadPool(
399 './tests/worker-files/thread/testWorker.js',
400 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
402 expect(pool
.opts
.workerChoiceStrategy
).toBe(
403 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
406 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
410 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
413 for (const worker
of pool
.workerChoiceStrategyContext
414 .getWorkerChoiceStrategy()
415 .workersTaskRunTime
.keys()) {
417 pool
.workerChoiceStrategyContext
418 .getWorkerChoiceStrategy()
419 .workersTaskRunTime
.get(worker
).weight
422 pool
.workerChoiceStrategyContext
423 .getWorkerChoiceStrategy()
424 .workersTaskRunTime
.get(worker
).runTime
427 // We need to clean up the resources after our test
431 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
432 const pool
= new FixedThreadPool(
434 './tests/worker-files/thread/testWorker.js'
436 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
437 expect(pool
.opts
.workerChoiceStrategy
).toBe(
438 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
440 // We need to clean up the resources after our test
444 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
445 let pool
= new FixedThreadPool(
447 './tests/worker-files/thread/testWorker.js'
449 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
451 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
452 .requiredStatistics
.runTime
455 pool
= new DynamicThreadPool(
458 './tests/worker-files/thread/testWorker.js'
460 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
462 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
463 .requiredStatistics
.runTime
465 // We need to clean up the resources after our test
469 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
470 const pool
= new FixedThreadPool(
472 './tests/worker-files/thread/testWorker.js',
473 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
475 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
477 for (let i
= 0; i
< max
* 2; i
++) {
478 promises
.push(pool
.execute())
480 await Promise
.all(promises
)
481 // We need to clean up the resources after our test
485 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
486 const pool
= new DynamicThreadPool(
489 './tests/worker-files/thread/testWorker.js',
490 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
492 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
494 for (let i
= 0; i
< max
* 2; i
++) {
495 promises
.push(pool
.execute())
497 await Promise
.all(promises
)
498 // We need to clean up the resources after our test
502 it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
503 let pool
= new FixedThreadPool(
505 './tests/worker-files/thread/testWorker.js'
508 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
512 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
516 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
519 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
521 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
525 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
528 for (const worker
of pool
.workerChoiceStrategyContext
529 .getWorkerChoiceStrategy()
530 .workersTaskRunTime
.keys()) {
532 pool
.workerChoiceStrategyContext
533 .getWorkerChoiceStrategy()
534 .workersTaskRunTime
.get(worker
).runTime
538 pool
= new DynamicThreadPool(
541 './tests/worker-files/thread/testWorker.js'
544 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
545 .workerChoiceStrategy
.currentWorkerIndex
548 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
549 .workerChoiceStrategy
.defaultWorkerWeight
552 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
553 .workerChoiceStrategy
.workersTaskRunTime
555 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
557 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
558 .workerChoiceStrategy
.currentWorkerIndex
561 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
562 .workerChoiceStrategy
.defaultWorkerWeight
564 for (const worker
of pool
.workerChoiceStrategyContext
565 .getWorkerChoiceStrategy()
566 .workerChoiceStrategy
.workersTaskRunTime
.keys()) {
568 pool
.workerChoiceStrategyContext
569 .getWorkerChoiceStrategy()
570 .workerChoiceStrategy
.workersTaskRunTime
.get(worker
).runTime
573 // We need to clean up the resources after our test
577 it('Verify unknown strategies throw error', () => {
580 new DynamicThreadPool(
583 './tests/worker-files/thread/testWorker.js',
584 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
587 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")