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