perf: use worker key as much as possible instead of a reference to the
[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++) {
c923ce56 137 results.add(pool.chooseWorker()[1].id)
2ced693a
JB
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++) {
c923ce56 144 results.add(pool.chooseWorker()[1].threadId)
2ced693a
JB
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 )
c923ce56 360 for (const workerKey of pool.workerChoiceStrategyContext
d2f7b7a2
JB
361 .getWorkerChoiceStrategy()
362 .workerLastVirtualTaskTimestamp.keys()) {
6e7fa401 363 expect(
d2f7b7a2
JB
364 pool.workerChoiceStrategyContext
365 .getWorkerChoiceStrategy()
c923ce56 366 .workerLastVirtualTaskTimestamp.get(workerKey).start
6e7fa401
JB
367 ).toBe(0)
368 expect(
d2f7b7a2
JB
369 pool.workerChoiceStrategyContext
370 .getWorkerChoiceStrategy()
c923ce56 371 .workerLastVirtualTaskTimestamp.get(workerKey).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)
c923ce56 459 for (const workerKey of pool.workerChoiceStrategyContext
d2f7b7a2
JB
460 .getWorkerChoiceStrategy()
461 .workerLastVirtualTaskTimestamp.keys()) {
caeb9817 462 expect(
d2f7b7a2
JB
463 pool.workerChoiceStrategyContext
464 .getWorkerChoiceStrategy()
c923ce56 465 .workerLastVirtualTaskTimestamp.get(workerKey).start
caeb9817
JB
466 ).toBe(0)
467 expect(
d2f7b7a2
JB
468 pool.workerChoiceStrategyContext
469 .getWorkerChoiceStrategy()
c923ce56 470 .workerLastVirtualTaskTimestamp.get(workerKey).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)
c923ce56 484 for (const workerKey of pool.workerChoiceStrategyContext
d2f7b7a2
JB
485 .getWorkerChoiceStrategy()
486 .workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) {
f0829c53 487 expect(
d2f7b7a2
JB
488 pool.workerChoiceStrategyContext
489 .getWorkerChoiceStrategy()
c923ce56
JB
490 .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(workerKey)
491 .start
f0829c53
JB
492 ).toBe(0)
493 expect(
d2f7b7a2
JB
494 pool.workerChoiceStrategyContext
495 .getWorkerChoiceStrategy()
c923ce56
JB
496 .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(workerKey)
497 .end
f0829c53
JB
498 ).toBe(0)
499 }
caeb9817
JB
500 // We need to clean up the resources after our test
501 await pool.destroy()
502 })
503
b3432a63 504 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
b3432a63
JB
505 const pool = new FixedThreadPool(
506 max,
507 './tests/worker-files/thread/testWorker.js',
508 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
509 )
510 expect(pool.opts.workerChoiceStrategy).toBe(
511 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
512 )
d2f7b7a2 513 expect(
ffcbbad8 514 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
d2f7b7a2
JB
515 ).toBe(0)
516 expect(
517 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
518 .defaultWorkerWeight
519 ).toBeGreaterThan(0)
c923ce56 520 for (const workerKey of pool.workerChoiceStrategyContext
d2f7b7a2
JB
521 .getWorkerChoiceStrategy()
522 .workersTaskRunTime.keys()) {
523 expect(
524 pool.workerChoiceStrategyContext
525 .getWorkerChoiceStrategy()
c923ce56 526 .workersTaskRunTime.get(workerKey).weight
d2f7b7a2 527 ).toBeGreaterThan(0)
6e7fa401 528 expect(
d2f7b7a2
JB
529 pool.workerChoiceStrategyContext
530 .getWorkerChoiceStrategy()
c923ce56 531 .workersTaskRunTime.get(workerKey).runTime
6e7fa401
JB
532 ).toBe(0)
533 }
b3432a63
JB
534 // We need to clean up the resources after our test
535 await pool.destroy()
536 })
537
538 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
b3432a63
JB
539 const pool = new FixedThreadPool(
540 max,
541 './tests/worker-files/thread/testWorker.js'
542 )
543 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
544 expect(pool.opts.workerChoiceStrategy).toBe(
545 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
546 )
547 // We need to clean up the resources after our test
548 await pool.destroy()
549 })
550
10fcfaf4 551 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
10fcfaf4
JB
552 let pool = new FixedThreadPool(
553 max,
554 './tests/worker-files/thread/testWorker.js'
555 )
556 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
557 expect(
558 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
559 .requiredStatistics.runTime
560 ).toBe(true)
fd7ebd49 561 await pool.destroy()
10fcfaf4
JB
562 pool = new DynamicThreadPool(
563 min,
564 max,
565 './tests/worker-files/thread/testWorker.js'
566 )
567 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
568 expect(
569 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
570 .requiredStatistics.runTime
571 ).toBe(true)
572 // We need to clean up the resources after our test
573 await pool.destroy()
574 })
575
b3432a63 576 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
b3432a63
JB
577 const pool = new FixedThreadPool(
578 max,
579 './tests/worker-files/thread/testWorker.js',
580 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
581 )
582 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
583 const promises = []
584 for (let i = 0; i < max * 2; i++) {
6db75ad9 585 promises.push(pool.execute())
b3432a63
JB
586 }
587 await Promise.all(promises)
588 // We need to clean up the resources after our test
589 await pool.destroy()
590 })
591
592 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
b3432a63
JB
593 const pool = new DynamicThreadPool(
594 min,
595 max,
596 './tests/worker-files/thread/testWorker.js',
597 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
598 )
599 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
600 const promises = []
601 for (let i = 0; i < max * 2; i++) {
6db75ad9 602 promises.push(pool.execute())
b3432a63
JB
603 }
604 await Promise.all(promises)
605 // We need to clean up the resources after our test
606 await pool.destroy()
607 })
608
a6f7f1b4 609 it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
f0829c53 610 let pool = new FixedThreadPool(
caeb9817
JB
611 max,
612 './tests/worker-files/thread/testWorker.js'
613 )
38f6e859 614 expect(
ffcbbad8 615 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
38f6e859
JB
616 ).toBeUndefined()
617 expect(
618 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
619 .defaultWorkerWeight
620 ).toBeUndefined()
caeb9817 621 expect(
d2f7b7a2
JB
622 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
623 .workersTaskRunTime
caeb9817
JB
624 ).toBeUndefined()
625 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
a6f7f1b4 626 expect(
ffcbbad8 627 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId
a6f7f1b4
JB
628 ).toBe(0)
629 expect(
630 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
631 .defaultWorkerWeight
632 ).toBeGreaterThan(0)
c923ce56 633 for (const workerKey of pool.workerChoiceStrategyContext
d2f7b7a2
JB
634 .getWorkerChoiceStrategy()
635 .workersTaskRunTime.keys()) {
caeb9817 636 expect(
d2f7b7a2
JB
637 pool.workerChoiceStrategyContext
638 .getWorkerChoiceStrategy()
c923ce56 639 .workersTaskRunTime.get(workerKey).runTime
caeb9817
JB
640 ).toBe(0)
641 }
f0829c53
JB
642 await pool.destroy()
643 pool = new DynamicThreadPool(
644 min,
645 max,
646 './tests/worker-files/thread/testWorker.js'
647 )
38f6e859
JB
648 expect(
649 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
ffcbbad8 650 .workerChoiceStrategy.currentWorkerId
38f6e859
JB
651 ).toBeUndefined()
652 expect(
653 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
654 .workerChoiceStrategy.defaultWorkerWeight
655 ).toBeUndefined()
f0829c53 656 expect(
d2f7b7a2
JB
657 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
658 .workerChoiceStrategy.workersTaskRunTime
f0829c53
JB
659 ).toBeUndefined()
660 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
a6f7f1b4
JB
661 expect(
662 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
ffcbbad8 663 .workerChoiceStrategy.currentWorkerId
a6f7f1b4
JB
664 ).toBe(0)
665 expect(
666 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
667 .workerChoiceStrategy.defaultWorkerWeight
668 ).toBeGreaterThan(0)
c923ce56 669 for (const workerKey of pool.workerChoiceStrategyContext
d2f7b7a2
JB
670 .getWorkerChoiceStrategy()
671 .workerChoiceStrategy.workersTaskRunTime.keys()) {
f0829c53 672 expect(
d2f7b7a2
JB
673 pool.workerChoiceStrategyContext
674 .getWorkerChoiceStrategy()
c923ce56 675 .workerChoiceStrategy.workersTaskRunTime.get(workerKey).runTime
f0829c53
JB
676 ).toBe(0)
677 }
caeb9817
JB
678 // We need to clean up the resources after our test
679 await pool.destroy()
680 })
681
a35560ba 682 it('Verify unknown strategies throw error', () => {
a35560ba
S
683 expect(
684 () =>
685 new DynamicThreadPool(
686 min,
687 max,
688 './tests/worker-files/thread/testWorker.js',
1927ee67 689 { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
a35560ba
S
690 )
691 ).toThrowError(
692 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
693 )
694 })
695})