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 LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
129 const pool
= new FixedThreadPool(
131 './tests/worker-files/thread/testWorker.js',
132 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
134 expect(pool
.opts
.workerChoiceStrategy
).toBe(
135 WorkerChoiceStrategies
.LESS_RECENTLY_USED
137 // We need to clean up the resources after our test
141 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
142 const pool
= new FixedThreadPool(
144 './tests/worker-files/thread/testWorker.js'
146 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
147 expect(pool
.opts
.workerChoiceStrategy
).toBe(
148 WorkerChoiceStrategies
.LESS_RECENTLY_USED
150 // We need to clean up the resources after our test
154 it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => {
155 let pool
= new FixedThreadPool(
157 './tests/worker-files/thread/testWorker.js'
159 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
161 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
162 .requiredStatistics
.runTime
165 pool
= new DynamicThreadPool(
168 './tests/worker-files/thread/testWorker.js'
170 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
172 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
173 .requiredStatistics
.runTime
175 // We need to clean up the resources after our test
179 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
180 const pool
= new FixedThreadPool(
182 './tests/worker-files/thread/testWorker.js',
183 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
185 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
187 for (let i
= 0; i
< max
* 2; i
++) {
188 promises
.push(pool
.execute())
190 await Promise
.all(promises
)
191 // We need to clean up the resources after our test
195 it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
196 const pool
= new DynamicThreadPool(
199 './tests/worker-files/thread/testWorker.js',
200 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
202 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
204 for (let i
= 0; i
< max
* 2; i
++) {
205 promises
.push(pool
.execute())
207 await Promise
.all(promises
)
208 // We need to clean up the resources after our test
212 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
213 const pool
= new FixedThreadPool(
215 './tests/worker-files/thread/testWorker.js',
216 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
218 expect(pool
.opts
.workerChoiceStrategy
).toBe(
219 WorkerChoiceStrategies
.FAIR_SHARE
221 for (const worker
of pool
.workerChoiceStrategyContext
222 .getWorkerChoiceStrategy()
223 .workerLastVirtualTaskTimestamp
.keys()) {
225 pool
.workerChoiceStrategyContext
226 .getWorkerChoiceStrategy()
227 .workerLastVirtualTaskTimestamp
.get(worker
).start
230 pool
.workerChoiceStrategyContext
231 .getWorkerChoiceStrategy()
232 .workerLastVirtualTaskTimestamp
.get(worker
).end
235 // We need to clean up the resources after our test
239 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
240 const pool
= new FixedThreadPool(
242 './tests/worker-files/thread/testWorker.js'
244 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
245 expect(pool
.opts
.workerChoiceStrategy
).toBe(
246 WorkerChoiceStrategies
.FAIR_SHARE
248 // We need to clean up the resources after our test
252 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
253 let pool
= new FixedThreadPool(
255 './tests/worker-files/thread/testWorker.js'
257 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
259 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
260 .requiredStatistics
.runTime
263 pool
= new DynamicThreadPool(
266 './tests/worker-files/thread/testWorker.js'
268 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
270 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
271 .requiredStatistics
.runTime
273 // We need to clean up the resources after our test
277 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
278 const pool
= new FixedThreadPool(
280 './tests/worker-files/thread/testWorker.js',
281 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
283 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
285 for (let i
= 0; i
< max
* 2; i
++) {
286 promises
.push(pool
.execute())
288 await Promise
.all(promises
)
289 // We need to clean up the resources after our test
293 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
294 const pool
= new DynamicThreadPool(
297 './tests/worker-files/thread/testWorker.js',
298 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
300 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
302 for (let i
= 0; i
< max
* 2; i
++) {
303 promises
.push(pool
.execute())
305 await Promise
.all(promises
)
306 // We need to clean up the resources after our test
310 it('Verify FAIR_SHARE strategy statistics are resets after setting it', async () => {
311 let pool
= new FixedThreadPool(
313 './tests/worker-files/thread/testWorker.js'
316 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
317 .workerLastVirtualTaskTimestamp
319 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
320 for (const worker
of pool
.workerChoiceStrategyContext
321 .getWorkerChoiceStrategy()
322 .workerLastVirtualTaskTimestamp
.keys()) {
324 pool
.workerChoiceStrategyContext
325 .getWorkerChoiceStrategy()
326 .workerLastVirtualTaskTimestamp
.get(worker
).start
329 pool
.workerChoiceStrategyContext
330 .getWorkerChoiceStrategy()
331 .workerLastVirtualTaskTimestamp
.get(worker
).end
335 pool
= new DynamicThreadPool(
338 './tests/worker-files/thread/testWorker.js'
341 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
342 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
344 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
345 for (const worker
of pool
.workerChoiceStrategyContext
346 .getWorkerChoiceStrategy()
347 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.keys()) {
349 pool
.workerChoiceStrategyContext
350 .getWorkerChoiceStrategy()
351 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(worker
).start
354 pool
.workerChoiceStrategyContext
355 .getWorkerChoiceStrategy()
356 .workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(worker
).end
359 // We need to clean up the resources after our test
363 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
364 const pool
= new FixedThreadPool(
366 './tests/worker-files/thread/testWorker.js',
367 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
369 expect(pool
.opts
.workerChoiceStrategy
).toBe(
370 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
373 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
377 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
381 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
384 for (const worker
of pool
.workerChoiceStrategyContext
385 .getWorkerChoiceStrategy()
386 .workersTaskRunTime
.keys()) {
388 pool
.workerChoiceStrategyContext
389 .getWorkerChoiceStrategy()
390 .workersTaskRunTime
.get(worker
).weight
393 pool
.workerChoiceStrategyContext
394 .getWorkerChoiceStrategy()
395 .workersTaskRunTime
.get(worker
).runTime
398 // We need to clean up the resources after our test
402 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
403 const pool
= new FixedThreadPool(
405 './tests/worker-files/thread/testWorker.js'
407 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
408 expect(pool
.opts
.workerChoiceStrategy
).toBe(
409 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
411 // We need to clean up the resources after our test
415 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
416 let pool
= new FixedThreadPool(
418 './tests/worker-files/thread/testWorker.js'
420 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
422 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
423 .requiredStatistics
.runTime
426 pool
= new DynamicThreadPool(
429 './tests/worker-files/thread/testWorker.js'
431 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
433 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
434 .requiredStatistics
.runTime
436 // We need to clean up the resources after our test
440 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
441 const pool
= new FixedThreadPool(
443 './tests/worker-files/thread/testWorker.js',
444 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
446 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
448 for (let i
= 0; i
< max
* 2; i
++) {
449 promises
.push(pool
.execute())
451 await Promise
.all(promises
)
452 // We need to clean up the resources after our test
456 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
457 const pool
= new DynamicThreadPool(
460 './tests/worker-files/thread/testWorker.js',
461 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
463 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
465 for (let i
= 0; i
< max
* 2; i
++) {
466 promises
.push(pool
.execute())
468 await Promise
.all(promises
)
469 // We need to clean up the resources after our test
473 it('Verify WEIGHTED_ROUND_ROBIN strategy statistics are resets after setting it', async () => {
474 let pool
= new FixedThreadPool(
476 './tests/worker-files/thread/testWorker.js'
479 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
482 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
483 for (const worker
of pool
.workerChoiceStrategyContext
484 .getWorkerChoiceStrategy()
485 .workersTaskRunTime
.keys()) {
487 pool
.workerChoiceStrategyContext
488 .getWorkerChoiceStrategy()
489 .workersTaskRunTime
.get(worker
).runTime
493 pool
= new DynamicThreadPool(
496 './tests/worker-files/thread/testWorker.js'
499 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
500 .workerChoiceStrategy
.workersTaskRunTime
502 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
503 for (const worker
of pool
.workerChoiceStrategyContext
504 .getWorkerChoiceStrategy()
505 .workerChoiceStrategy
.workersTaskRunTime
.keys()) {
507 pool
.workerChoiceStrategyContext
508 .getWorkerChoiceStrategy()
509 .workerChoiceStrategy
.workersTaskRunTime
.get(worker
).runTime
512 // We need to clean up the resources after our test
516 it('Verify unknown strategies throw error', () => {
519 new DynamicThreadPool(
522 './tests/worker-files/thread/testWorker.js',
523 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
526 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")