feat: add less busy worker choice strategy
[poolifier.git] / tests / pools / selection-strategies / selection-strategies.test.js
CommitLineData
a61a0724 1const { expect } = require('expect')
a35560ba
S
2const {
3 WorkerChoiceStrategies,
4 DynamicThreadPool,
2ced693a
JB
5 FixedThreadPool,
6 FixedClusterPool
15d56315 7} = require('../../../lib/index')
a35560ba
S
8
9describe('Selection strategies test suite', () => {
e1ffb94f
JB
10 const min = 0
11 const max = 3
12
a35560ba
S
13 it('Verify that WorkerChoiceStrategies enumeration provides string values', () => {
14 expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN')
737c6d97 15 expect(WorkerChoiceStrategies.LESS_USED).toBe('LESS_USED')
168c526f 16 expect(WorkerChoiceStrategies.LESS_BUSY).toBe('LESS_BUSY')
23ff945a 17 expect(WorkerChoiceStrategies.FAIR_SHARE).toBe('FAIR_SHARE')
b3432a63
JB
18 expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe(
19 'WEIGHTED_ROUND_ROBIN'
20 )
a35560ba
S
21 })
22
e843b904 23 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
e843b904
JB
24 const pool = new DynamicThreadPool(
25 min,
26 max,
27 './tests/worker-files/thread/testWorker.js'
28 )
29 expect(pool.opts.workerChoiceStrategy).toBe(
30 WorkerChoiceStrategies.ROUND_ROBIN
31 )
32 // We need to clean up the resources after our test
33 await pool.destroy()
34 })
35
d2f7b7a2
JB
36 it('Verify ROUND_ROBIN strategy is taken at pool creation', async () => {
37 const pool = new FixedThreadPool(
38 max,
39 './tests/worker-files/thread/testWorker.js',
40 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
41 )
42 expect(pool.opts.workerChoiceStrategy).toBe(
43 WorkerChoiceStrategies.ROUND_ROBIN
44 )
45 expect(
ffcbbad8 46 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerId
d2f7b7a2
JB
47 ).toBe(0)
48 // We need to clean up the resources after our test
49 await pool.destroy()
50 })
51
e843b904 52 it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
e843b904
JB
53 const pool = new DynamicThreadPool(
54 min,
55 max,
56 './tests/worker-files/thread/testWorker.js'
57 )
58 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
59 expect(pool.opts.workerChoiceStrategy).toBe(
60 WorkerChoiceStrategies.ROUND_ROBIN
61 )
62 // We need to clean up the resources after our test
63 await pool.destroy()
64 })
65
10fcfaf4 66 it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
10fcfaf4
JB
67 let pool = new FixedThreadPool(
68 max,
69 './tests/worker-files/thread/testWorker.js'
70 )
71 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
72 expect(
73 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
74 .requiredStatistics.runTime
75 ).toBe(false)
fd7ebd49 76 await pool.destroy()
10fcfaf4
JB
77 pool = new DynamicThreadPool(
78 min,
79 max,
80 './tests/worker-files/thread/testWorker.js'
81 )
82 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
83 expect(
84 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
85 .requiredStatistics.runTime
86 ).toBe(false)
87 // We need to clean up the resources after our test
88 await pool.destroy()
89 })
90
bdaf31cd 91 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
bdaf31cd
JB
92 const pool = new FixedThreadPool(
93 max,
94 './tests/worker-files/thread/testWorker.js',
95 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
96 )
97 expect(pool.opts.workerChoiceStrategy).toBe(
98 WorkerChoiceStrategies.ROUND_ROBIN
99 )
100 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
101 const promises = []
102 for (let i = 0; i < max * 2; i++) {
6db75ad9 103 promises.push(pool.execute())
bdaf31cd
JB
104 }
105 await Promise.all(promises)
106 // We need to clean up the resources after our test
107 await pool.destroy()
108 })
109
110 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
bdaf31cd
JB
111 const pool = new DynamicThreadPool(
112 min,
113 max,
114 './tests/worker-files/thread/testWorker.js',
115 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
116 )
117 expect(pool.opts.workerChoiceStrategy).toBe(
118 WorkerChoiceStrategies.ROUND_ROBIN
119 )
120 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
121 const promises = []
122 for (let i = 0; i < max * 2; i++) {
6db75ad9 123 promises.push(pool.execute())
bdaf31cd
JB
124 }
125 await Promise.all(promises)
126 // We need to clean up the resources after our test
127 await pool.destroy()
128 })
129
2ced693a
JB
130 it('Verify ROUND_ROBIN strategy runtime behavior', async () => {
131 let pool = new FixedClusterPool(
132 max,
133 './tests/worker-files/cluster/testWorker.js'
134 )
135 let results = new Set()
136 for (let i = 0; i < max; i++) {
137 results.add(pool.chooseWorker().id)
138 }
139 expect(results.size).toBe(max)
140 await pool.destroy()
141 pool = new FixedThreadPool(max, './tests/worker-files/thread/testWorker.js')
142 results = new Set()
143 for (let i = 0; i < max; i++) {
144 results.add(pool.chooseWorker().threadId)
145 }
146 expect(results.size).toBe(max)
147 await pool.destroy()
148 })
149
a6f7f1b4
JB
150 it('Verify ROUND_ROBIN strategy internals are resets after setting it', async () => {
151 let pool = new FixedThreadPool(
152 max,
153 './tests/worker-files/thread/testWorker.js',
154 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
155 )
38f6e859 156 expect(
ffcbbad8 157 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerId
38f6e859 158 ).toBeUndefined()
a6f7f1b4
JB
159 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
160 expect(
ffcbbad8 161 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerId
a6f7f1b4
JB
162 ).toBe(0)
163 await pool.destroy()
164 pool = new DynamicThreadPool(
165 min,
166 max,
167 './tests/worker-files/thread/testWorker.js',
168 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
169 )
38f6e859
JB
170 expect(
171 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
ffcbbad8 172 .workerChoiceStrategy.nextWorkerId
38f6e859 173 ).toBeUndefined()
a6f7f1b4
JB
174 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
175 expect(
176 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
ffcbbad8 177 .workerChoiceStrategy.nextWorkerId
a6f7f1b4
JB
178 ).toBe(0)
179 // We need to clean up the resources after our test
180 await pool.destroy()
181 })
182
737c6d97 183 it('Verify LESS_USED strategy is taken at pool creation', async () => {
a35560ba
S
184 const pool = new FixedThreadPool(
185 max,
186 './tests/worker-files/thread/testWorker.js',
737c6d97 187 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED }
a35560ba 188 )
b98ec2e6 189 expect(pool.opts.workerChoiceStrategy).toBe(
737c6d97 190 WorkerChoiceStrategies.LESS_USED
b98ec2e6
JB
191 )
192 // We need to clean up the resources after our test
193 await pool.destroy()
194 })
a35560ba 195
737c6d97 196 it('Verify LESS_USED strategy can be set after pool creation', async () => {
b98ec2e6
JB
197 const pool = new FixedThreadPool(
198 max,
199 './tests/worker-files/thread/testWorker.js'
200 )
737c6d97 201 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED)
a35560ba 202 expect(pool.opts.workerChoiceStrategy).toBe(
737c6d97 203 WorkerChoiceStrategies.LESS_USED
a35560ba 204 )
b98ec2e6
JB
205 // We need to clean up the resources after our test
206 await pool.destroy()
207 })
a35560ba 208
737c6d97 209 it('Verify LESS_USED strategy default tasks usage statistics requirements', async () => {
10fcfaf4
JB
210 let pool = new FixedThreadPool(
211 max,
212 './tests/worker-files/thread/testWorker.js'
213 )
737c6d97 214 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED)
10fcfaf4
JB
215 expect(
216 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
217 .requiredStatistics.runTime
218 ).toBe(false)
fd7ebd49 219 await pool.destroy()
10fcfaf4
JB
220 pool = new DynamicThreadPool(
221 min,
222 max,
223 './tests/worker-files/thread/testWorker.js'
224 )
737c6d97 225 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED)
10fcfaf4
JB
226 expect(
227 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
228 .requiredStatistics.runTime
229 ).toBe(false)
230 // We need to clean up the resources after our test
231 await pool.destroy()
232 })
233
737c6d97 234 it('Verify LESS_USED strategy can be run in a fixed pool', async () => {
b98ec2e6
JB
235 const pool = new FixedThreadPool(
236 max,
237 './tests/worker-files/thread/testWorker.js',
737c6d97 238 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED }
b98ec2e6 239 )
168c526f 240 // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose`
a35560ba
S
241 const promises = []
242 for (let i = 0; i < max * 2; i++) {
6db75ad9 243 promises.push(pool.execute())
a35560ba
S
244 }
245 await Promise.all(promises)
a35560ba
S
246 // We need to clean up the resources after our test
247 await pool.destroy()
248 })
249
737c6d97 250 it('Verify LESS_USED strategy can be run in a dynamic pool', async () => {
ff5e76e1
JB
251 const pool = new DynamicThreadPool(
252 min,
253 max,
254 './tests/worker-files/thread/testWorker.js',
737c6d97 255 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED }
ff5e76e1 256 )
168c526f
JB
257 // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose`
258 const promises = []
259 for (let i = 0; i < max * 2; i++) {
260 promises.push(pool.execute())
261 }
262 await Promise.all(promises)
263 // We need to clean up the resources after our test
264 await pool.destroy()
265 })
266
267 it('Verify LESS_BUSY strategy is taken at pool creation', async () => {
268 const pool = new FixedThreadPool(
269 max,
270 './tests/worker-files/thread/testWorker.js',
271 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY }
272 )
273 expect(pool.opts.workerChoiceStrategy).toBe(
274 WorkerChoiceStrategies.LESS_BUSY
275 )
276 // We need to clean up the resources after our test
277 await pool.destroy()
278 })
279
280 it('Verify LESS_BUSY strategy can be set after pool creation', async () => {
281 const pool = new FixedThreadPool(
282 max,
283 './tests/worker-files/thread/testWorker.js'
284 )
285 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY)
286 expect(pool.opts.workerChoiceStrategy).toBe(
287 WorkerChoiceStrategies.LESS_BUSY
288 )
289 // We need to clean up the resources after our test
290 await pool.destroy()
291 })
292
293 it('Verify LESS_BUSY strategy default tasks usage statistics requirements', async () => {
294 let pool = new FixedThreadPool(
295 max,
296 './tests/worker-files/thread/testWorker.js'
297 )
298 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY)
299 expect(
300 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
301 .requiredStatistics.runTime
302 ).toBe(true)
303 await pool.destroy()
304 pool = new DynamicThreadPool(
305 min,
306 max,
307 './tests/worker-files/thread/testWorker.js'
308 )
309 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY)
310 expect(
311 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
312 .requiredStatistics.runTime
313 ).toBe(true)
314 // We need to clean up the resources after our test
315 await pool.destroy()
316 })
317
318 it('Verify LESS_BUSY strategy can be run in a fixed pool', async () => {
319 const pool = new FixedThreadPool(
320 max,
321 './tests/worker-files/thread/testWorker.js',
322 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY }
323 )
324 // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose`
325 const promises = []
326 for (let i = 0; i < max * 2; i++) {
327 promises.push(pool.execute())
328 }
329 await Promise.all(promises)
330 // We need to clean up the resources after our test
331 await pool.destroy()
332 })
333
334 it('Verify LESS_BUSY strategy can be run in a dynamic pool', async () => {
335 const pool = new DynamicThreadPool(
336 min,
337 max,
338 './tests/worker-files/thread/testWorker.js',
339 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY }
340 )
341 // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose`
ff5e76e1
JB
342 const promises = []
343 for (let i = 0; i < max * 2; i++) {
6db75ad9 344 promises.push(pool.execute())
ff5e76e1
JB
345 }
346 await Promise.all(promises)
ff5e76e1
JB
347 // We need to clean up the resources after our test
348 await pool.destroy()
349 })
350
23ff945a 351 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
23ff945a
JB
352 const pool = new FixedThreadPool(
353 max,
354 './tests/worker-files/thread/testWorker.js',
355 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
356 )
357 expect(pool.opts.workerChoiceStrategy).toBe(
358 WorkerChoiceStrategies.FAIR_SHARE
359 )
d2f7b7a2
JB
360 for (const worker of pool.workerChoiceStrategyContext
361 .getWorkerChoiceStrategy()
362 .workerLastVirtualTaskTimestamp.keys()) {
6e7fa401 363 expect(
d2f7b7a2
JB
364 pool.workerChoiceStrategyContext
365 .getWorkerChoiceStrategy()
366 .workerLastVirtualTaskTimestamp.get(worker).start
6e7fa401
JB
367 ).toBe(0)
368 expect(
d2f7b7a2
JB
369 pool.workerChoiceStrategyContext
370 .getWorkerChoiceStrategy()
371 .workerLastVirtualTaskTimestamp.get(worker).end
6e7fa401
JB
372 ).toBe(0)
373 }
23ff945a
JB
374 // We need to clean up the resources after our test
375 await pool.destroy()
376 })
377
378 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
23ff945a
JB
379 const pool = new FixedThreadPool(
380 max,
381 './tests/worker-files/thread/testWorker.js'
382 )
383 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
384 expect(pool.opts.workerChoiceStrategy).toBe(
385 WorkerChoiceStrategies.FAIR_SHARE
386 )
387 // We need to clean up the resources after our test
388 await pool.destroy()
389 })
390
10fcfaf4 391 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
10fcfaf4
JB
392 let pool = new FixedThreadPool(
393 max,
394 './tests/worker-files/thread/testWorker.js'
395 )
396 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
397 expect(
398 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
399 .requiredStatistics.runTime
400 ).toBe(true)
fd7ebd49 401 await pool.destroy()
10fcfaf4
JB
402 pool = new DynamicThreadPool(
403 min,
404 max,
405 './tests/worker-files/thread/testWorker.js'
406 )
407 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
408 expect(
409 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
410 .requiredStatistics.runTime
411 ).toBe(true)
412 // We need to clean up the resources after our test
413 await pool.destroy()
414 })
415
23ff945a 416 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
23ff945a
JB
417 const pool = new FixedThreadPool(
418 max,
419 './tests/worker-files/thread/testWorker.js',
420 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
421 )
422 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
423 const promises = []
424 for (let i = 0; i < max * 2; i++) {
6db75ad9 425 promises.push(pool.execute())
23ff945a
JB
426 }
427 await Promise.all(promises)
428 // We need to clean up the resources after our test
429 await pool.destroy()
430 })
431
432 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
23ff945a
JB
433 const pool = new DynamicThreadPool(
434 min,
435 max,
436 './tests/worker-files/thread/testWorker.js',
437 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
438 )
439 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
440 const promises = []
441 for (let i = 0; i < max * 2; i++) {
6db75ad9 442 promises.push(pool.execute())
23ff945a
JB
443 }
444 await Promise.all(promises)
445 // We need to clean up the resources after our test
446 await pool.destroy()
447 })
448
a6f7f1b4 449 it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => {
f0829c53 450 let pool = new FixedThreadPool(
caeb9817
JB
451 max,
452 './tests/worker-files/thread/testWorker.js'
453 )
454 expect(
d2f7b7a2 455 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
caeb9817
JB
456 .workerLastVirtualTaskTimestamp
457 ).toBeUndefined()
458 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
d2f7b7a2
JB
459 for (const worker of pool.workerChoiceStrategyContext
460 .getWorkerChoiceStrategy()
461 .workerLastVirtualTaskTimestamp.keys()) {
caeb9817 462 expect(
d2f7b7a2
JB
463 pool.workerChoiceStrategyContext
464 .getWorkerChoiceStrategy()
465 .workerLastVirtualTaskTimestamp.get(worker).start
caeb9817
JB
466 ).toBe(0)
467 expect(
d2f7b7a2
JB
468 pool.workerChoiceStrategyContext
469 .getWorkerChoiceStrategy()
470 .workerLastVirtualTaskTimestamp.get(worker).end
caeb9817
JB
471 ).toBe(0)
472 }
f0829c53
JB
473 await pool.destroy()
474 pool = new DynamicThreadPool(
475 min,
476 max,
477 './tests/worker-files/thread/testWorker.js'
478 )
479 expect(
d2f7b7a2
JB
480 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
481 .workerChoiceStrategy.workerLastVirtualTaskTimestamp
f0829c53
JB
482 ).toBeUndefined()
483 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
d2f7b7a2
JB
484 for (const worker of pool.workerChoiceStrategyContext
485 .getWorkerChoiceStrategy()
486 .workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) {
f0829c53 487 expect(
d2f7b7a2
JB
488 pool.workerChoiceStrategyContext
489 .getWorkerChoiceStrategy()
490 .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(worker).start
f0829c53
JB
491 ).toBe(0)
492 expect(
d2f7b7a2
JB
493 pool.workerChoiceStrategyContext
494 .getWorkerChoiceStrategy()
495 .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(worker).end
f0829c53
JB
496 ).toBe(0)
497 }
caeb9817
JB
498 // We need to clean up the resources after our test
499 await pool.destroy()
500 })
501
b3432a63 502 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
b3432a63
JB
503 const pool = new FixedThreadPool(
504 max,
505 './tests/worker-files/thread/testWorker.js',
506 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
507 )
508 expect(pool.opts.workerChoiceStrategy).toBe(
509 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
510 )
d2f7b7a2 511 expect(
ffcbbad8 512 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
d2f7b7a2
JB
513 ).toBe(0)
514 expect(
515 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
516 .defaultWorkerWeight
517 ).toBeGreaterThan(0)
518 for (const worker of pool.workerChoiceStrategyContext
519 .getWorkerChoiceStrategy()
520 .workersTaskRunTime.keys()) {
521 expect(
522 pool.workerChoiceStrategyContext
523 .getWorkerChoiceStrategy()
524 .workersTaskRunTime.get(worker).weight
525 ).toBeGreaterThan(0)
6e7fa401 526 expect(
d2f7b7a2
JB
527 pool.workerChoiceStrategyContext
528 .getWorkerChoiceStrategy()
529 .workersTaskRunTime.get(worker).runTime
6e7fa401
JB
530 ).toBe(0)
531 }
b3432a63
JB
532 // We need to clean up the resources after our test
533 await pool.destroy()
534 })
535
536 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
b3432a63
JB
537 const pool = new FixedThreadPool(
538 max,
539 './tests/worker-files/thread/testWorker.js'
540 )
541 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
542 expect(pool.opts.workerChoiceStrategy).toBe(
543 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
544 )
545 // We need to clean up the resources after our test
546 await pool.destroy()
547 })
548
10fcfaf4 549 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
10fcfaf4
JB
550 let pool = new FixedThreadPool(
551 max,
552 './tests/worker-files/thread/testWorker.js'
553 )
554 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
555 expect(
556 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
557 .requiredStatistics.runTime
558 ).toBe(true)
fd7ebd49 559 await pool.destroy()
10fcfaf4
JB
560 pool = new DynamicThreadPool(
561 min,
562 max,
563 './tests/worker-files/thread/testWorker.js'
564 )
565 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
566 expect(
567 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
568 .requiredStatistics.runTime
569 ).toBe(true)
570 // We need to clean up the resources after our test
571 await pool.destroy()
572 })
573
b3432a63 574 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
b3432a63
JB
575 const pool = new FixedThreadPool(
576 max,
577 './tests/worker-files/thread/testWorker.js',
578 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
579 )
580 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
581 const promises = []
582 for (let i = 0; i < max * 2; i++) {
6db75ad9 583 promises.push(pool.execute())
b3432a63
JB
584 }
585 await Promise.all(promises)
586 // We need to clean up the resources after our test
587 await pool.destroy()
588 })
589
590 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
b3432a63
JB
591 const pool = new DynamicThreadPool(
592 min,
593 max,
594 './tests/worker-files/thread/testWorker.js',
595 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
596 )
597 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
598 const promises = []
599 for (let i = 0; i < max * 2; i++) {
6db75ad9 600 promises.push(pool.execute())
b3432a63
JB
601 }
602 await Promise.all(promises)
603 // We need to clean up the resources after our test
604 await pool.destroy()
605 })
606
a6f7f1b4 607 it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
f0829c53 608 let pool = new FixedThreadPool(
caeb9817
JB
609 max,
610 './tests/worker-files/thread/testWorker.js'
611 )
38f6e859 612 expect(
ffcbbad8 613 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
38f6e859
JB
614 ).toBeUndefined()
615 expect(
616 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
617 .defaultWorkerWeight
618 ).toBeUndefined()
caeb9817 619 expect(
d2f7b7a2
JB
620 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
621 .workersTaskRunTime
caeb9817
JB
622 ).toBeUndefined()
623 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
a6f7f1b4 624 expect(
ffcbbad8 625 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
a6f7f1b4
JB
626 ).toBe(0)
627 expect(
628 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
629 .defaultWorkerWeight
630 ).toBeGreaterThan(0)
d2f7b7a2
JB
631 for (const worker of pool.workerChoiceStrategyContext
632 .getWorkerChoiceStrategy()
633 .workersTaskRunTime.keys()) {
caeb9817 634 expect(
d2f7b7a2
JB
635 pool.workerChoiceStrategyContext
636 .getWorkerChoiceStrategy()
637 .workersTaskRunTime.get(worker).runTime
caeb9817
JB
638 ).toBe(0)
639 }
f0829c53
JB
640 await pool.destroy()
641 pool = new DynamicThreadPool(
642 min,
643 max,
644 './tests/worker-files/thread/testWorker.js'
645 )
38f6e859
JB
646 expect(
647 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
ffcbbad8 648 .workerChoiceStrategy.currentWorkerId
38f6e859
JB
649 ).toBeUndefined()
650 expect(
651 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
652 .workerChoiceStrategy.defaultWorkerWeight
653 ).toBeUndefined()
f0829c53 654 expect(
d2f7b7a2
JB
655 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
656 .workerChoiceStrategy.workersTaskRunTime
f0829c53
JB
657 ).toBeUndefined()
658 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
a6f7f1b4
JB
659 expect(
660 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
ffcbbad8 661 .workerChoiceStrategy.currentWorkerId
a6f7f1b4
JB
662 ).toBe(0)
663 expect(
664 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
665 .workerChoiceStrategy.defaultWorkerWeight
666 ).toBeGreaterThan(0)
d2f7b7a2
JB
667 for (const worker of pool.workerChoiceStrategyContext
668 .getWorkerChoiceStrategy()
669 .workerChoiceStrategy.workersTaskRunTime.keys()) {
f0829c53 670 expect(
d2f7b7a2
JB
671 pool.workerChoiceStrategyContext
672 .getWorkerChoiceStrategy()
673 .workerChoiceStrategy.workersTaskRunTime.get(worker).runTime
f0829c53
JB
674 ).toBe(0)
675 }
caeb9817
JB
676 // We need to clean up the resources after our test
677 await pool.destroy()
678 })
679
a35560ba 680 it('Verify unknown strategies throw error', () => {
a35560ba
S
681 expect(
682 () =>
683 new DynamicThreadPool(
684 min,
685 max,
686 './tests/worker-files/thread/testWorker.js',
1927ee67 687 { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
a35560ba
S
688 )
689 ).toThrowError(
690 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
691 )
692 })
693})