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