Tests: improve strategies initialization coverage
[poolifier.git] / tests / pools / selection-strategies / selection-strategies.test.js
CommitLineData
a61a0724 1const { expect } = require('expect')
a35560ba
S
2const {
3 WorkerChoiceStrategies,
4 DynamicThreadPool,
5 FixedThreadPool
15d56315 6} = require('../../../lib/index')
a35560ba
S
7
8describe('Selection strategies test suite', () => {
e1ffb94f
JB
9 const min = 0
10 const max = 3
11
a35560ba
S
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')
23ff945a 15 expect(WorkerChoiceStrategies.FAIR_SHARE).toBe('FAIR_SHARE')
b3432a63
JB
16 expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe(
17 'WEIGHTED_ROUND_ROBIN'
18 )
a35560ba
S
19 })
20
e843b904 21 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
e843b904
JB
22 const pool = new DynamicThreadPool(
23 min,
24 max,
25 './tests/worker-files/thread/testWorker.js'
26 )
27 expect(pool.opts.workerChoiceStrategy).toBe(
28 WorkerChoiceStrategies.ROUND_ROBIN
29 )
30 // We need to clean up the resources after our test
31 await pool.destroy()
32 })
33
d2f7b7a2
JB
34 it('Verify ROUND_ROBIN strategy is taken at pool creation', async () => {
35 const pool = new FixedThreadPool(
36 max,
37 './tests/worker-files/thread/testWorker.js',
38 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
39 )
40 expect(pool.opts.workerChoiceStrategy).toBe(
41 WorkerChoiceStrategies.ROUND_ROBIN
42 )
43 expect(
44 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerIndex
45 ).toBe(0)
46 // We need to clean up the resources after our test
47 await pool.destroy()
48 })
49
e843b904 50 it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
e843b904
JB
51 const pool = new DynamicThreadPool(
52 min,
53 max,
54 './tests/worker-files/thread/testWorker.js'
55 )
56 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
57 expect(pool.opts.workerChoiceStrategy).toBe(
58 WorkerChoiceStrategies.ROUND_ROBIN
59 )
60 // We need to clean up the resources after our test
61 await pool.destroy()
62 })
63
10fcfaf4 64 it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
10fcfaf4
JB
65 let pool = new FixedThreadPool(
66 max,
67 './tests/worker-files/thread/testWorker.js'
68 )
69 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
70 expect(
71 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
72 .requiredStatistics.runTime
73 ).toBe(false)
fd7ebd49 74 await pool.destroy()
10fcfaf4
JB
75 pool = new DynamicThreadPool(
76 min,
77 max,
78 './tests/worker-files/thread/testWorker.js'
79 )
80 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
81 expect(
82 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
83 .requiredStatistics.runTime
84 ).toBe(false)
85 // We need to clean up the resources after our test
86 await pool.destroy()
87 })
88
bdaf31cd 89 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
bdaf31cd
JB
90 const pool = new FixedThreadPool(
91 max,
92 './tests/worker-files/thread/testWorker.js',
93 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
94 )
95 expect(pool.opts.workerChoiceStrategy).toBe(
96 WorkerChoiceStrategies.ROUND_ROBIN
97 )
98 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
99 const promises = []
100 for (let i = 0; i < max * 2; i++) {
6db75ad9 101 promises.push(pool.execute())
bdaf31cd
JB
102 }
103 await Promise.all(promises)
104 // We need to clean up the resources after our test
105 await pool.destroy()
106 })
107
108 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
bdaf31cd
JB
109 const pool = new DynamicThreadPool(
110 min,
111 max,
112 './tests/worker-files/thread/testWorker.js',
113 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
114 )
115 expect(pool.opts.workerChoiceStrategy).toBe(
116 WorkerChoiceStrategies.ROUND_ROBIN
117 )
118 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
119 const promises = []
120 for (let i = 0; i < max * 2; i++) {
6db75ad9 121 promises.push(pool.execute())
bdaf31cd
JB
122 }
123 await Promise.all(promises)
124 // We need to clean up the resources after our test
125 await pool.destroy()
126 })
127
b98ec2e6 128 it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
a35560ba
S
129 const pool = new FixedThreadPool(
130 max,
131 './tests/worker-files/thread/testWorker.js',
132 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
133 )
b98ec2e6
JB
134 expect(pool.opts.workerChoiceStrategy).toBe(
135 WorkerChoiceStrategies.LESS_RECENTLY_USED
136 )
137 // We need to clean up the resources after our test
138 await pool.destroy()
139 })
a35560ba 140
b98ec2e6 141 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
b98ec2e6
JB
142 const pool = new FixedThreadPool(
143 max,
144 './tests/worker-files/thread/testWorker.js'
145 )
146 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
a35560ba
S
147 expect(pool.opts.workerChoiceStrategy).toBe(
148 WorkerChoiceStrategies.LESS_RECENTLY_USED
149 )
b98ec2e6
JB
150 // We need to clean up the resources after our test
151 await pool.destroy()
152 })
a35560ba 153
10fcfaf4 154 it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => {
10fcfaf4
JB
155 let pool = new FixedThreadPool(
156 max,
157 './tests/worker-files/thread/testWorker.js'
158 )
159 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
160 expect(
161 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
162 .requiredStatistics.runTime
163 ).toBe(false)
fd7ebd49 164 await pool.destroy()
10fcfaf4
JB
165 pool = new DynamicThreadPool(
166 min,
167 max,
168 './tests/worker-files/thread/testWorker.js'
169 )
170 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
171 expect(
172 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
173 .requiredStatistics.runTime
174 ).toBe(false)
175 // We need to clean up the resources after our test
176 await pool.destroy()
177 })
178
ff5e76e1 179 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
b98ec2e6
JB
180 const pool = new FixedThreadPool(
181 max,
182 './tests/worker-files/thread/testWorker.js',
183 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
184 )
a35560ba
S
185 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
186 const promises = []
187 for (let i = 0; i < max * 2; i++) {
6db75ad9 188 promises.push(pool.execute())
a35560ba
S
189 }
190 await Promise.all(promises)
a35560ba
S
191 // We need to clean up the resources after our test
192 await pool.destroy()
193 })
194
ff5e76e1 195 it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
ff5e76e1
JB
196 const pool = new DynamicThreadPool(
197 min,
198 max,
199 './tests/worker-files/thread/testWorker.js',
200 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
201 )
202 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
203 const promises = []
204 for (let i = 0; i < max * 2; i++) {
6db75ad9 205 promises.push(pool.execute())
ff5e76e1
JB
206 }
207 await Promise.all(promises)
ff5e76e1
JB
208 // We need to clean up the resources after our test
209 await pool.destroy()
210 })
211
23ff945a 212 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
23ff945a
JB
213 const pool = new FixedThreadPool(
214 max,
215 './tests/worker-files/thread/testWorker.js',
216 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
217 )
218 expect(pool.opts.workerChoiceStrategy).toBe(
219 WorkerChoiceStrategies.FAIR_SHARE
220 )
d2f7b7a2
JB
221 for (const worker of pool.workerChoiceStrategyContext
222 .getWorkerChoiceStrategy()
223 .workerLastVirtualTaskTimestamp.keys()) {
6e7fa401 224 expect(
d2f7b7a2
JB
225 pool.workerChoiceStrategyContext
226 .getWorkerChoiceStrategy()
227 .workerLastVirtualTaskTimestamp.get(worker).start
6e7fa401
JB
228 ).toBe(0)
229 expect(
d2f7b7a2
JB
230 pool.workerChoiceStrategyContext
231 .getWorkerChoiceStrategy()
232 .workerLastVirtualTaskTimestamp.get(worker).end
6e7fa401
JB
233 ).toBe(0)
234 }
23ff945a
JB
235 // We need to clean up the resources after our test
236 await pool.destroy()
237 })
238
239 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
23ff945a
JB
240 const pool = new FixedThreadPool(
241 max,
242 './tests/worker-files/thread/testWorker.js'
243 )
244 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
245 expect(pool.opts.workerChoiceStrategy).toBe(
246 WorkerChoiceStrategies.FAIR_SHARE
247 )
248 // We need to clean up the resources after our test
249 await pool.destroy()
250 })
251
10fcfaf4 252 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
10fcfaf4
JB
253 let pool = new FixedThreadPool(
254 max,
255 './tests/worker-files/thread/testWorker.js'
256 )
257 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
258 expect(
259 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
260 .requiredStatistics.runTime
261 ).toBe(true)
fd7ebd49 262 await pool.destroy()
10fcfaf4
JB
263 pool = new DynamicThreadPool(
264 min,
265 max,
266 './tests/worker-files/thread/testWorker.js'
267 )
268 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
269 expect(
270 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
271 .requiredStatistics.runTime
272 ).toBe(true)
273 // We need to clean up the resources after our test
274 await pool.destroy()
275 })
276
23ff945a 277 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
23ff945a
JB
278 const pool = new FixedThreadPool(
279 max,
280 './tests/worker-files/thread/testWorker.js',
281 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
282 )
283 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
284 const promises = []
285 for (let i = 0; i < max * 2; i++) {
6db75ad9 286 promises.push(pool.execute())
23ff945a
JB
287 }
288 await Promise.all(promises)
289 // We need to clean up the resources after our test
290 await pool.destroy()
291 })
292
293 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
23ff945a
JB
294 const pool = new DynamicThreadPool(
295 min,
296 max,
297 './tests/worker-files/thread/testWorker.js',
298 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
299 )
300 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
301 const promises = []
302 for (let i = 0; i < max * 2; i++) {
6db75ad9 303 promises.push(pool.execute())
23ff945a
JB
304 }
305 await Promise.all(promises)
306 // We need to clean up the resources after our test
307 await pool.destroy()
308 })
309
caeb9817 310 it('Verify FAIR_SHARE strategy statistics are resets after setting it', async () => {
f0829c53 311 let pool = new FixedThreadPool(
caeb9817
JB
312 max,
313 './tests/worker-files/thread/testWorker.js'
314 )
315 expect(
d2f7b7a2 316 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
caeb9817
JB
317 .workerLastVirtualTaskTimestamp
318 ).toBeUndefined()
319 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
d2f7b7a2
JB
320 for (const worker of pool.workerChoiceStrategyContext
321 .getWorkerChoiceStrategy()
322 .workerLastVirtualTaskTimestamp.keys()) {
caeb9817 323 expect(
d2f7b7a2
JB
324 pool.workerChoiceStrategyContext
325 .getWorkerChoiceStrategy()
326 .workerLastVirtualTaskTimestamp.get(worker).start
caeb9817
JB
327 ).toBe(0)
328 expect(
d2f7b7a2
JB
329 pool.workerChoiceStrategyContext
330 .getWorkerChoiceStrategy()
331 .workerLastVirtualTaskTimestamp.get(worker).end
caeb9817
JB
332 ).toBe(0)
333 }
f0829c53
JB
334 await pool.destroy()
335 pool = new DynamicThreadPool(
336 min,
337 max,
338 './tests/worker-files/thread/testWorker.js'
339 )
340 expect(
d2f7b7a2
JB
341 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
342 .workerChoiceStrategy.workerLastVirtualTaskTimestamp
f0829c53
JB
343 ).toBeUndefined()
344 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
d2f7b7a2
JB
345 for (const worker of pool.workerChoiceStrategyContext
346 .getWorkerChoiceStrategy()
347 .workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) {
f0829c53 348 expect(
d2f7b7a2
JB
349 pool.workerChoiceStrategyContext
350 .getWorkerChoiceStrategy()
351 .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(worker).start
f0829c53
JB
352 ).toBe(0)
353 expect(
d2f7b7a2
JB
354 pool.workerChoiceStrategyContext
355 .getWorkerChoiceStrategy()
356 .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(worker).end
f0829c53
JB
357 ).toBe(0)
358 }
caeb9817
JB
359 // We need to clean up the resources after our test
360 await pool.destroy()
361 })
362
b3432a63 363 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
b3432a63
JB
364 const pool = new FixedThreadPool(
365 max,
366 './tests/worker-files/thread/testWorker.js',
367 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
368 )
369 expect(pool.opts.workerChoiceStrategy).toBe(
370 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
371 )
d2f7b7a2
JB
372 expect(
373 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
374 .previousWorkerIndex
375 ).toBe(0)
376 expect(
377 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
378 .currentWorkerIndex
379 ).toBe(0)
380 expect(
381 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
382 .defaultWorkerWeight
383 ).toBeGreaterThan(0)
384 for (const worker of pool.workerChoiceStrategyContext
385 .getWorkerChoiceStrategy()
386 .workersTaskRunTime.keys()) {
387 expect(
388 pool.workerChoiceStrategyContext
389 .getWorkerChoiceStrategy()
390 .workersTaskRunTime.get(worker).weight
391 ).toBeGreaterThan(0)
6e7fa401 392 expect(
d2f7b7a2
JB
393 pool.workerChoiceStrategyContext
394 .getWorkerChoiceStrategy()
395 .workersTaskRunTime.get(worker).runTime
6e7fa401
JB
396 ).toBe(0)
397 }
b3432a63
JB
398 // We need to clean up the resources after our test
399 await pool.destroy()
400 })
401
402 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
b3432a63
JB
403 const pool = new FixedThreadPool(
404 max,
405 './tests/worker-files/thread/testWorker.js'
406 )
407 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
408 expect(pool.opts.workerChoiceStrategy).toBe(
409 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
410 )
411 // We need to clean up the resources after our test
412 await pool.destroy()
413 })
414
10fcfaf4 415 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
10fcfaf4
JB
416 let pool = new FixedThreadPool(
417 max,
418 './tests/worker-files/thread/testWorker.js'
419 )
420 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
421 expect(
422 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
423 .requiredStatistics.runTime
424 ).toBe(true)
fd7ebd49 425 await pool.destroy()
10fcfaf4
JB
426 pool = new DynamicThreadPool(
427 min,
428 max,
429 './tests/worker-files/thread/testWorker.js'
430 )
431 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
432 expect(
433 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
434 .requiredStatistics.runTime
435 ).toBe(true)
436 // We need to clean up the resources after our test
437 await pool.destroy()
438 })
439
b3432a63 440 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
b3432a63
JB
441 const pool = new FixedThreadPool(
442 max,
443 './tests/worker-files/thread/testWorker.js',
444 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
445 )
446 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
447 const promises = []
448 for (let i = 0; i < max * 2; i++) {
6db75ad9 449 promises.push(pool.execute())
b3432a63
JB
450 }
451 await Promise.all(promises)
452 // We need to clean up the resources after our test
453 await pool.destroy()
454 })
455
456 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
b3432a63
JB
457 const pool = new DynamicThreadPool(
458 min,
459 max,
460 './tests/worker-files/thread/testWorker.js',
461 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
462 )
463 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
464 const promises = []
465 for (let i = 0; i < max * 2; i++) {
6db75ad9 466 promises.push(pool.execute())
b3432a63
JB
467 }
468 await Promise.all(promises)
469 // We need to clean up the resources after our test
470 await pool.destroy()
471 })
472
caeb9817 473 it('Verify WEIGHTED_ROUND_ROBIN strategy statistics are resets after setting it', async () => {
f0829c53 474 let pool = new FixedThreadPool(
caeb9817
JB
475 max,
476 './tests/worker-files/thread/testWorker.js'
477 )
478 expect(
d2f7b7a2
JB
479 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
480 .workersTaskRunTime
caeb9817
JB
481 ).toBeUndefined()
482 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
d2f7b7a2
JB
483 for (const worker of pool.workerChoiceStrategyContext
484 .getWorkerChoiceStrategy()
485 .workersTaskRunTime.keys()) {
caeb9817 486 expect(
d2f7b7a2
JB
487 pool.workerChoiceStrategyContext
488 .getWorkerChoiceStrategy()
489 .workersTaskRunTime.get(worker).runTime
caeb9817
JB
490 ).toBe(0)
491 }
f0829c53
JB
492 await pool.destroy()
493 pool = new DynamicThreadPool(
494 min,
495 max,
496 './tests/worker-files/thread/testWorker.js'
497 )
498 expect(
d2f7b7a2
JB
499 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
500 .workerChoiceStrategy.workersTaskRunTime
f0829c53
JB
501 ).toBeUndefined()
502 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
d2f7b7a2
JB
503 for (const worker of pool.workerChoiceStrategyContext
504 .getWorkerChoiceStrategy()
505 .workerChoiceStrategy.workersTaskRunTime.keys()) {
f0829c53 506 expect(
d2f7b7a2
JB
507 pool.workerChoiceStrategyContext
508 .getWorkerChoiceStrategy()
509 .workerChoiceStrategy.workersTaskRunTime.get(worker).runTime
f0829c53
JB
510 ).toBe(0)
511 }
caeb9817
JB
512 // We need to clean up the resources after our test
513 await pool.destroy()
514 })
515
a35560ba 516 it('Verify unknown strategies throw error', () => {
a35560ba
S
517 expect(
518 () =>
519 new DynamicThreadPool(
520 min,
521 max,
522 './tests/worker-files/thread/testWorker.js',
1927ee67 523 { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
a35560ba
S
524 )
525 ).toThrowError(
526 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
527 )
528 })
529})