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
}
134 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
136 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy().nextWorkerIndex
139 pool
= new DynamicThreadPool(
142 './tests/worker-files/thread/testWorker.js',
143 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
145 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
147 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
148 .workerChoiceStrategy
.nextWorkerIndex
150 // We need to clean up the resources after our test
154 it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
155 const pool
= new FixedThreadPool(
157 './tests/worker-files/thread/testWorker.js',
158 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
160 expect(pool
.opts
.workerChoiceStrategy
).toBe(
161 WorkerChoiceStrategies
.LESS_RECENTLY_USED
163 // We need to clean up the resources after our test
167 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
168 const pool
= new FixedThreadPool(
170 './tests/worker-files/thread/testWorker.js'
172 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
173 expect(pool
.opts
.workerChoiceStrategy
).toBe(
174 WorkerChoiceStrategies
.LESS_RECENTLY_USED
176 // We need to clean up the resources after our test
180 it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => {
181 let pool
= new FixedThreadPool(
183 './tests/worker-files/thread/testWorker.js'
185 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
187 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
188 .requiredStatistics
.runTime
191 pool
= new DynamicThreadPool(
194 './tests/worker-files/thread/testWorker.js'
196 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
198 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
199 .requiredStatistics
.runTime
201 // We need to clean up the resources after our test
205 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
206 const pool
= new FixedThreadPool(
208 './tests/worker-files/thread/testWorker.js',
209 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
211 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
213 for (let i
= 0; i
< max
* 2; i
++) {
214 promises
.push(pool
.execute())
216 await Promise
.all(promises
)
217 // We need to clean up the resources after our test
221 it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
222 const pool
= new DynamicThreadPool(
225 './tests/worker-files/thread/testWorker.js',
226 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
228 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
230 for (let i
= 0; i
< max
* 2; i
++) {
231 promises
.push(pool
.execute())
233 await Promise
.all(promises
)
234 // We need to clean up the resources after our test
238 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
239 const pool
= new FixedThreadPool(
241 './tests/worker-files/thread/testWorker.js',
242 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
244 expect(pool
.opts
.workerChoiceStrategy
).toBe(
245 WorkerChoiceStrategies
.FAIR_SHARE
247 for (const worker
of pool
.workerChoiceStrategyContext
248 .getWorkerChoiceStrategy()
249 .workerLastVirtualTaskTimestamp
.keys()) {
251 pool
.workerChoiceStrategyContext
252 .getWorkerChoiceStrategy()
253 .workerLastVirtualTaskTimestamp
.get(worker
).start
256 pool
.workerChoiceStrategyContext
257 .getWorkerChoiceStrategy()
258 .workerLastVirtualTaskTimestamp
.get(worker
).end
261 // We need to clean up the resources after our test
265 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
266 const pool
= new FixedThreadPool(
268 './tests/worker-files/thread/testWorker.js'
270 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
271 expect(pool
.opts
.workerChoiceStrategy
).toBe(
272 WorkerChoiceStrategies
.FAIR_SHARE
274 // We need to clean up the resources after our test
278 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
279 let pool
= new FixedThreadPool(
281 './tests/worker-files/thread/testWorker.js'
283 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
285 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
286 .requiredStatistics
.runTime
289 pool
= new DynamicThreadPool(
292 './tests/worker-files/thread/testWorker.js'
294 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
296 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
297 .requiredStatistics
.runTime
299 // We need to clean up the resources after our test
303 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
304 const pool
= new FixedThreadPool(
306 './tests/worker-files/thread/testWorker.js',
307 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
309 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
311 for (let i
= 0; i
< max
* 2; i
++) {
312 promises
.push(pool
.execute())
314 await Promise
.all(promises
)
315 // We need to clean up the resources after our test
319 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
320 const pool
= new DynamicThreadPool(
323 './tests/worker-files/thread/testWorker.js',
324 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
326 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
328 for (let i
= 0; i
< max
* 2; i
++) {
329 promises
.push(pool
.execute())
331 await Promise
.all(promises
)
332 // We need to clean up the resources after our test
336 it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => {
337 let pool
= new FixedThreadPool(
339 './tests/worker-files/thread/testWorker.js'
342 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
343 .workerLastVirtualTaskTimestamp
345 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
346 for (const worker
of pool
.workerChoiceStrategyContext
347 .getWorkerChoiceStrategy()
348 .workerLastVirtualTaskTimestamp
.keys()) {
350 pool
.workerChoiceStrategyContext
351 .getWorkerChoiceStrategy()
352 .workerLastVirtualTaskTimestamp
.get(worker
).start
355 pool
.workerChoiceStrategyContext
356 .getWorkerChoiceStrategy()
357 .workerLastVirtualTaskTimestamp
.get(worker
).end
361 pool
= new DynamicThreadPool(
364 './tests/worker-files/thread/testWorker.js'
367 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
368 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
370 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
371 for (const worker
of pool
.workerChoiceStrategyContext
372 .getWorkerChoiceStrategy()
373 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.keys()) {
375 pool
.workerChoiceStrategyContext
376 .getWorkerChoiceStrategy()
377 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(worker
).start
380 pool
.workerChoiceStrategyContext
381 .getWorkerChoiceStrategy()
382 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(worker
).end
385 // We need to clean up the resources after our test
389 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
390 const pool
= new FixedThreadPool(
392 './tests/worker-files/thread/testWorker.js',
393 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
395 expect(pool
.opts
.workerChoiceStrategy
).toBe(
396 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
399 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
403 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
407 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
410 for (const worker
of pool
.workerChoiceStrategyContext
411 .getWorkerChoiceStrategy()
412 .workersTaskRunTime
.keys()) {
414 pool
.workerChoiceStrategyContext
415 .getWorkerChoiceStrategy()
416 .workersTaskRunTime
.get(worker
).weight
419 pool
.workerChoiceStrategyContext
420 .getWorkerChoiceStrategy()
421 .workersTaskRunTime
.get(worker
).runTime
424 // We need to clean up the resources after our test
428 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
429 const pool
= new FixedThreadPool(
431 './tests/worker-files/thread/testWorker.js'
433 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
434 expect(pool
.opts
.workerChoiceStrategy
).toBe(
435 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
437 // We need to clean up the resources after our test
441 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
442 let pool
= new FixedThreadPool(
444 './tests/worker-files/thread/testWorker.js'
446 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
448 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
449 .requiredStatistics
.runTime
452 pool
= new DynamicThreadPool(
455 './tests/worker-files/thread/testWorker.js'
457 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
459 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
460 .requiredStatistics
.runTime
462 // We need to clean up the resources after our test
466 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
467 const pool
= new FixedThreadPool(
469 './tests/worker-files/thread/testWorker.js',
470 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
472 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
474 for (let i
= 0; i
< max
* 2; i
++) {
475 promises
.push(pool
.execute())
477 await Promise
.all(promises
)
478 // We need to clean up the resources after our test
482 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
483 const pool
= new DynamicThreadPool(
486 './tests/worker-files/thread/testWorker.js',
487 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
489 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
491 for (let i
= 0; i
< max
* 2; i
++) {
492 promises
.push(pool
.execute())
494 await Promise
.all(promises
)
495 // We need to clean up the resources after our test
499 it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
500 let pool
= new FixedThreadPool(
502 './tests/worker-files/thread/testWorker.js'
505 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
508 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
510 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
514 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
518 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
521 for (const worker
of pool
.workerChoiceStrategyContext
522 .getWorkerChoiceStrategy()
523 .workersTaskRunTime
.keys()) {
525 pool
.workerChoiceStrategyContext
526 .getWorkerChoiceStrategy()
527 .workersTaskRunTime
.get(worker
).runTime
531 pool
= new DynamicThreadPool(
534 './tests/worker-files/thread/testWorker.js'
537 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
538 .workerChoiceStrategy
.workersTaskRunTime
540 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
542 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
543 .workerChoiceStrategy
.previousWorkerIndex
546 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
547 .workerChoiceStrategy
.currentWorkerIndex
550 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
551 .workerChoiceStrategy
.defaultWorkerWeight
553 for (const worker
of pool
.workerChoiceStrategyContext
554 .getWorkerChoiceStrategy()
555 .workerChoiceStrategy
.workersTaskRunTime
.keys()) {
557 pool
.workerChoiceStrategyContext
558 .getWorkerChoiceStrategy()
559 .workerChoiceStrategy
.workersTaskRunTime
.get(worker
).runTime
562 // We need to clean up the resources after our test
566 it('Verify unknown strategies throw error', () => {
569 new DynamicThreadPool(
572 './tests/worker-files/thread/testWorker.js',
573 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
576 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")