test: cleanup test name
[poolifier.git] / tests / pools / selection-strategies / selection-strategies.test.mjs
CommitLineData
bcfb06ce
JB
1import { randomInt } from 'node:crypto'
2
a074ffee 3import { expect } from 'expect'
ded253e2
JB
4
5import { CircularArray } from '../../../lib/circular-array.cjs'
a074ffee 6import {
999ef664 7 DynamicClusterPool,
a35560ba 8 DynamicThreadPool,
3d6dd312 9 FixedClusterPool,
2ced693a 10 FixedThreadPool,
3d6dd312 11 WorkerChoiceStrategies
d35e5717 12} from '../../../lib/index.cjs'
a35560ba
S
13
14describe('Selection strategies test suite', () => {
e1ffb94f
JB
15 const min = 0
16 const max = 3
17
a35560ba
S
18 it('Verify that WorkerChoiceStrategies enumeration provides string values', () => {
19 expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN')
e4543b14
JB
20 expect(WorkerChoiceStrategies.LEAST_USED).toBe('LEAST_USED')
21 expect(WorkerChoiceStrategies.LEAST_BUSY).toBe('LEAST_BUSY')
a7bbf44a 22 expect(WorkerChoiceStrategies.LEAST_ELU).toBe('LEAST_ELU')
23ff945a 23 expect(WorkerChoiceStrategies.FAIR_SHARE).toBe('FAIR_SHARE')
b3432a63
JB
24 expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe(
25 'WEIGHTED_ROUND_ROBIN'
26 )
feec6e8c
JB
27 expect(WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN).toBe(
28 'INTERLEAVED_WEIGHTED_ROUND_ROBIN'
29 )
a35560ba
S
30 })
31
e843b904 32 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
e843b904
JB
33 const pool = new DynamicThreadPool(
34 min,
35 max,
b2fd3f4a 36 './tests/worker-files/thread/testWorker.mjs'
e843b904
JB
37 )
38 expect(pool.opts.workerChoiceStrategy).toBe(
39 WorkerChoiceStrategies.ROUND_ROBIN
40 )
bcfb06ce
JB
41 expect(pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy).toBe(
42 WorkerChoiceStrategies.ROUND_ROBIN
43 )
e843b904
JB
44 // We need to clean up the resources after our test
45 await pool.destroy()
46 })
47
594bfb84
JB
48 it('Verify available strategies are taken at pool creation', async () => {
49 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
50 const pool = new FixedThreadPool(
51 max,
b2fd3f4a 52 './tests/worker-files/thread/testWorker.mjs',
594bfb84
JB
53 { workerChoiceStrategy }
54 )
55 expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy)
bcfb06ce
JB
56 expect(
57 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
58 ).toBe(workerChoiceStrategy)
594bfb84
JB
59 await pool.destroy()
60 }
d2f7b7a2
JB
61 })
62
594bfb84
JB
63 it('Verify available strategies can be set after pool creation', async () => {
64 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
65 const pool = new DynamicThreadPool(
66 min,
67 max,
b2fd3f4a 68 './tests/worker-files/thread/testWorker.mjs'
594bfb84
JB
69 )
70 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
71 expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy)
bcfb06ce
JB
72 expect(
73 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
74 ).toBe(workerChoiceStrategy)
999ef664
JB
75 await pool.destroy()
76 }
77 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
78 const pool = new DynamicClusterPool(
79 min,
80 max,
d35e5717 81 './tests/worker-files/cluster/testWorker.cjs'
999ef664 82 )
26ce26ca 83 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
999ef664 84 expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy)
bcfb06ce
JB
85 expect(
86 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
87 ).toBe(workerChoiceStrategy)
594bfb84
JB
88 await pool.destroy()
89 }
90 })
91
92 it('Verify available strategies default internals at pool creation', async () => {
594bfb84 93 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
bcfb06ce
JB
94 const pool = new FixedThreadPool(
95 max,
96 './tests/worker-files/thread/testWorker.mjs',
97 { workerChoiceStrategy }
98 )
54f4e726 99 expect(
bcfb06ce 100 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
54f4e726
JB
101 workerChoiceStrategy
102 ).nextWorkerNodeKey
103 ).toBe(0)
104 expect(
bcfb06ce 105 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
54f4e726
JB
106 workerChoiceStrategy
107 ).previousWorkerNodeKey
108 ).toBe(0)
f3a91bac 109 if (
594bfb84
JB
110 workerChoiceStrategy === WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
111 ) {
54f4e726 112 expect(
bcfb06ce 113 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
54f4e726 114 workerChoiceStrategy
f3a91bac 115 ).workerNodeVirtualTaskRunTime
594bfb84 116 ).toBe(0)
54f4e726
JB
117 } else if (
118 workerChoiceStrategy ===
119 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
120 ) {
08f3f44c 121 expect(
bcfb06ce 122 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
08f3f44c 123 workerChoiceStrategy
f3a91bac 124 ).workerNodeVirtualTaskRunTime
08f3f44c 125 ).toBe(0)
54f4e726 126 expect(
bcfb06ce 127 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
54f4e726
JB
128 workerChoiceStrategy
129 ).roundId
130 ).toBe(0)
131 expect(
bcfb06ce 132 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
54f4e726
JB
133 workerChoiceStrategy
134 ).workerNodeId
135 ).toBe(0)
136 expect(
bcfb06ce 137 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
54f4e726 138 workerChoiceStrategy
00e1bdeb
JB
139 ).roundWeights.length
140 ).toBe(1)
9ef8fa71
JB
141 expect(
142 Number.isSafeInteger(
bcfb06ce 143 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
9ef8fa71
JB
144 workerChoiceStrategy
145 ).roundWeights[0]
146 )
147 ).toBe(true)
594bfb84 148 }
bcfb06ce 149 await pool.destroy()
594bfb84 150 }
e843b904
JB
151 })
152
6c6afb84
JB
153 it('Verify ROUND_ROBIN strategy default policy', async () => {
154 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
155 let pool = new FixedThreadPool(
156 max,
b2fd3f4a 157 './tests/worker-files/thread/testWorker.mjs',
6c6afb84
JB
158 { workerChoiceStrategy }
159 )
bcfb06ce 160 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
94407def 161 dynamicWorkerUsage: false,
b1aae695 162 dynamicWorkerReady: true
6c6afb84
JB
163 })
164 await pool.destroy()
165 pool = new DynamicThreadPool(
166 min,
167 max,
b2fd3f4a 168 './tests/worker-files/thread/testWorker.mjs',
6c6afb84
JB
169 { workerChoiceStrategy }
170 )
bcfb06ce 171 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
94407def 172 dynamicWorkerUsage: false,
b1aae695 173 dynamicWorkerReady: true
6c6afb84
JB
174 })
175 // We need to clean up the resources after our test
176 await pool.destroy()
177 })
178
179 it('Verify ROUND_ROBIN strategy default tasks statistics requirements', async () => {
594bfb84 180 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
10fcfaf4
JB
181 let pool = new FixedThreadPool(
182 max,
b2fd3f4a 183 './tests/worker-files/thread/testWorker.mjs',
594bfb84 184 { workerChoiceStrategy }
10fcfaf4 185 )
87de9ff5 186 expect(
bcfb06ce 187 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
87de9ff5 188 ).toStrictEqual({
932fc8be
JB
189 runTime: {
190 aggregate: false,
191 average: false,
192 median: false
193 },
194 waitTime: {
195 aggregate: false,
196 average: false,
197 median: false
198 },
5df69fab
JB
199 elu: {
200 aggregate: false,
201 average: false,
202 median: false
203 }
86bf340d 204 })
fd7ebd49 205 await pool.destroy()
10fcfaf4
JB
206 pool = new DynamicThreadPool(
207 min,
208 max,
b2fd3f4a 209 './tests/worker-files/thread/testWorker.mjs',
594bfb84 210 { workerChoiceStrategy }
10fcfaf4 211 )
87de9ff5 212 expect(
bcfb06ce 213 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
87de9ff5 214 ).toStrictEqual({
932fc8be
JB
215 runTime: {
216 aggregate: false,
217 average: false,
218 median: false
219 },
220 waitTime: {
221 aggregate: false,
222 average: false,
223 median: false
224 },
5df69fab
JB
225 elu: {
226 aggregate: false,
227 average: false,
228 median: false
229 }
86bf340d 230 })
10fcfaf4
JB
231 // We need to clean up the resources after our test
232 await pool.destroy()
233 })
234
bdaf31cd 235 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
516dcb0d 236 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
bdaf31cd
JB
237 const pool = new FixedThreadPool(
238 max,
b2fd3f4a 239 './tests/worker-files/thread/testWorker.mjs',
516dcb0d 240 { workerChoiceStrategy }
bdaf31cd 241 )
bdaf31cd 242 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
ee9f5295 243 const promises = new Set()
a20f0ba5
JB
244 const maxMultiplier = 2
245 for (let i = 0; i < max * maxMultiplier; i++) {
ee9f5295 246 promises.add(pool.execute())
e211bc18
JB
247 }
248 await Promise.all(promises)
249 for (const workerNode of pool.workerNodes) {
465b2940 250 expect(workerNode.usage).toStrictEqual({
a4e07f72
JB
251 tasks: {
252 executed: maxMultiplier,
253 executing: 0,
254 queued: 0,
df593701 255 maxQueued: 0,
463226a4 256 sequentiallyStolen: 0,
5ad42e34 257 stolen: 0,
a4e07f72
JB
258 failed: 0
259 },
260 runTime: {
4ba4c7f9 261 history: new CircularArray()
a4e07f72
JB
262 },
263 waitTime: {
4ba4c7f9 264 history: new CircularArray()
a4e07f72 265 },
5df69fab
JB
266 elu: {
267 idle: {
4ba4c7f9 268 history: new CircularArray()
5df69fab
JB
269 },
270 active: {
4ba4c7f9 271 history: new CircularArray()
71514351 272 }
5df69fab 273 }
e211bc18 274 })
bdaf31cd 275 }
9458090a 276 expect(
bcfb06ce
JB
277 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
278 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
9b106837 279 ).nextWorkerNodeKey
9458090a 280 ).toBe(0)
086fd843 281 expect(
bcfb06ce
JB
282 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
283 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
086fd843
JB
284 ).previousWorkerNodeKey
285 ).toBe(pool.workerNodes.length - 1)
bdaf31cd
JB
286 // We need to clean up the resources after our test
287 await pool.destroy()
288 })
289
290 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
516dcb0d 291 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
bdaf31cd
JB
292 const pool = new DynamicThreadPool(
293 min,
294 max,
b2fd3f4a 295 './tests/worker-files/thread/testWorker.mjs',
516dcb0d 296 { workerChoiceStrategy }
bdaf31cd 297 )
bdaf31cd 298 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
ee9f5295 299 const promises = new Set()
a20f0ba5
JB
300 const maxMultiplier = 2
301 for (let i = 0; i < max * maxMultiplier; i++) {
ee9f5295 302 promises.add(pool.execute())
e211bc18
JB
303 }
304 await Promise.all(promises)
305 for (const workerNode of pool.workerNodes) {
465b2940 306 expect(workerNode.usage).toStrictEqual({
a4e07f72 307 tasks: {
94407def 308 executed: expect.any(Number),
a4e07f72
JB
309 executing: 0,
310 queued: 0,
df593701 311 maxQueued: 0,
463226a4 312 sequentiallyStolen: 0,
5ad42e34 313 stolen: 0,
a4e07f72
JB
314 failed: 0
315 },
316 runTime: {
4ba4c7f9 317 history: new CircularArray()
a4e07f72
JB
318 },
319 waitTime: {
4ba4c7f9 320 history: new CircularArray()
a4e07f72 321 },
5df69fab
JB
322 elu: {
323 idle: {
4ba4c7f9 324 history: new CircularArray()
5df69fab
JB
325 },
326 active: {
4ba4c7f9 327 history: new CircularArray()
71514351 328 }
5df69fab 329 }
e211bc18 330 })
94407def
JB
331 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
332 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
333 max * maxMultiplier
334 )
bdaf31cd 335 }
9458090a 336 expect(
bcfb06ce
JB
337 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
338 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
9b106837 339 ).nextWorkerNodeKey
9458090a 340 ).toBe(0)
086fd843 341 expect(
bcfb06ce
JB
342 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
343 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
086fd843
JB
344 ).previousWorkerNodeKey
345 ).toBe(pool.workerNodes.length - 1)
bdaf31cd
JB
346 // We need to clean up the resources after our test
347 await pool.destroy()
348 })
349
2ced693a 350 it('Verify ROUND_ROBIN strategy runtime behavior', async () => {
594bfb84 351 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
2ced693a
JB
352 let pool = new FixedClusterPool(
353 max,
d35e5717 354 './tests/worker-files/cluster/testWorker.cjs',
594bfb84 355 { workerChoiceStrategy }
2ced693a
JB
356 )
357 let results = new Set()
358 for (let i = 0; i < max; i++) {
51a8af40 359 results.add(pool.workerNodes[pool.chooseWorkerNode()].info.id)
2ced693a
JB
360 }
361 expect(results.size).toBe(max)
362 await pool.destroy()
594bfb84
JB
363 pool = new FixedThreadPool(
364 max,
b2fd3f4a 365 './tests/worker-files/thread/testWorker.mjs',
594bfb84
JB
366 { workerChoiceStrategy }
367 )
2ced693a
JB
368 results = new Set()
369 for (let i = 0; i < max; i++) {
51a8af40 370 results.add(pool.workerNodes[pool.chooseWorkerNode()].info.id)
2ced693a
JB
371 }
372 expect(results.size).toBe(max)
373 await pool.destroy()
374 })
375
bcfb06ce 376 it("Verify ROUND_ROBIN strategy internals aren't reset after setting it", async () => {
594bfb84 377 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
a6f7f1b4
JB
378 let pool = new FixedThreadPool(
379 max,
b2fd3f4a 380 './tests/worker-files/thread/testWorker.mjs',
bcfb06ce 381 { workerChoiceStrategy }
a6f7f1b4 382 )
bcfb06ce
JB
383 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
384 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
385 ).nextWorkerNodeKey = randomInt(1, max - 1)
386 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
387 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
388 ).previousWorkerNodeKey = randomInt(1, max - 1)
086fd843 389 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
38f6e859 390 expect(
bcfb06ce
JB
391 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
392 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
9b106837 393 ).nextWorkerNodeKey
bcfb06ce 394 ).toBeGreaterThan(0)
a6f7f1b4 395 expect(
bcfb06ce
JB
396 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
397 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
086fd843 398 ).previousWorkerNodeKey
bcfb06ce 399 ).toBeGreaterThan(0)
a6f7f1b4
JB
400 await pool.destroy()
401 pool = new DynamicThreadPool(
402 min,
403 max,
b2fd3f4a 404 './tests/worker-files/thread/testWorker.mjs',
bcfb06ce 405 { workerChoiceStrategy }
a6f7f1b4 406 )
bcfb06ce
JB
407 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
408 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
409 ).nextWorkerNodeKey = randomInt(1, max - 1)
410 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
411 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
412 ).previousWorkerNodeKey = randomInt(1, max - 1)
086fd843 413 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
38f6e859 414 expect(
bcfb06ce
JB
415 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
416 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
9b106837 417 ).nextWorkerNodeKey
bcfb06ce 418 ).toBeGreaterThan(0)
a6f7f1b4 419 expect(
bcfb06ce
JB
420 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
421 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
086fd843 422 ).previousWorkerNodeKey
bcfb06ce 423 ).toBeGreaterThan(0)
a6f7f1b4
JB
424 // We need to clean up the resources after our test
425 await pool.destroy()
426 })
427
6c6afb84
JB
428 it('Verify LEAST_USED strategy default policy', async () => {
429 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_USED
430 let pool = new FixedThreadPool(
431 max,
b2fd3f4a 432 './tests/worker-files/thread/testWorker.mjs',
6c6afb84
JB
433 { workerChoiceStrategy }
434 )
bcfb06ce 435 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
b1aae695
JB
436 dynamicWorkerUsage: false,
437 dynamicWorkerReady: true
6c6afb84
JB
438 })
439 await pool.destroy()
440 pool = new DynamicThreadPool(
441 min,
442 max,
b2fd3f4a 443 './tests/worker-files/thread/testWorker.mjs',
6c6afb84
JB
444 { workerChoiceStrategy }
445 )
bcfb06ce 446 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
b1aae695
JB
447 dynamicWorkerUsage: false,
448 dynamicWorkerReady: true
6c6afb84
JB
449 })
450 // We need to clean up the resources after our test
451 await pool.destroy()
452 })
453
454 it('Verify LEAST_USED strategy default tasks statistics requirements', async () => {
e4543b14 455 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_USED
10fcfaf4
JB
456 let pool = new FixedThreadPool(
457 max,
b2fd3f4a 458 './tests/worker-files/thread/testWorker.mjs',
594bfb84 459 { workerChoiceStrategy }
10fcfaf4 460 )
87de9ff5 461 expect(
bcfb06ce 462 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
87de9ff5 463 ).toStrictEqual({
932fc8be
JB
464 runTime: {
465 aggregate: false,
466 average: false,
467 median: false
468 },
469 waitTime: {
470 aggregate: false,
471 average: false,
472 median: false
473 },
5df69fab
JB
474 elu: {
475 aggregate: false,
476 average: false,
477 median: false
478 }
86bf340d 479 })
fd7ebd49 480 await pool.destroy()
10fcfaf4
JB
481 pool = new DynamicThreadPool(
482 min,
483 max,
b2fd3f4a 484 './tests/worker-files/thread/testWorker.mjs',
594bfb84 485 { workerChoiceStrategy }
10fcfaf4 486 )
87de9ff5 487 expect(
bcfb06ce 488 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
87de9ff5 489 ).toStrictEqual({
932fc8be
JB
490 runTime: {
491 aggregate: false,
492 average: false,
493 median: false
494 },
495 waitTime: {
496 aggregate: false,
497 average: false,
498 median: false
499 },
5df69fab
JB
500 elu: {
501 aggregate: false,
502 average: false,
503 median: false
504 }
86bf340d 505 })
10fcfaf4
JB
506 // We need to clean up the resources after our test
507 await pool.destroy()
508 })
509
e4543b14 510 it('Verify LEAST_USED strategy can be run in a fixed pool', async () => {
b98ec2e6
JB
511 const pool = new FixedThreadPool(
512 max,
b2fd3f4a 513 './tests/worker-files/thread/testWorker.mjs',
e4543b14 514 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED }
b98ec2e6 515 )
e4543b14 516 // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose`
ee9f5295 517 const promises = new Set()
a20f0ba5
JB
518 const maxMultiplier = 2
519 for (let i = 0; i < max * maxMultiplier; i++) {
ee9f5295 520 promises.add(pool.execute())
e211bc18
JB
521 }
522 await Promise.all(promises)
523 for (const workerNode of pool.workerNodes) {
465b2940 524 expect(workerNode.usage).toStrictEqual({
a4e07f72 525 tasks: {
76407b8e 526 executed: expect.any(Number),
a4e07f72
JB
527 executing: 0,
528 queued: 0,
df593701 529 maxQueued: 0,
463226a4 530 sequentiallyStolen: 0,
5ad42e34 531 stolen: 0,
a4e07f72
JB
532 failed: 0
533 },
534 runTime: {
4ba4c7f9 535 history: new CircularArray()
a4e07f72
JB
536 },
537 waitTime: {
4ba4c7f9 538 history: new CircularArray()
a4e07f72 539 },
5df69fab
JB
540 elu: {
541 idle: {
4ba4c7f9 542 history: new CircularArray()
5df69fab
JB
543 },
544 active: {
4ba4c7f9 545 history: new CircularArray()
71514351 546 }
5df69fab 547 }
e211bc18 548 })
465b2940
JB
549 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
550 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
76407b8e
JB
551 max * maxMultiplier
552 )
a35560ba 553 }
e5c68e12 554 expect(
bcfb06ce
JB
555 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
556 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
557 ).nextWorkerNodeKey
558 ).toEqual(expect.any(Number))
559 expect(
bcfb06ce
JB
560 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
561 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
562 ).previousWorkerNodeKey
563 ).toEqual(expect.any(Number))
a35560ba
S
564 // We need to clean up the resources after our test
565 await pool.destroy()
566 })
567
e4543b14 568 it('Verify LEAST_USED strategy can be run in a dynamic pool', async () => {
ff5e76e1
JB
569 const pool = new DynamicThreadPool(
570 min,
571 max,
b2fd3f4a 572 './tests/worker-files/thread/testWorker.mjs',
e4543b14 573 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED }
ff5e76e1 574 )
e4543b14 575 // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose`
ee9f5295 576 const promises = new Set()
a20f0ba5
JB
577 const maxMultiplier = 2
578 for (let i = 0; i < max * maxMultiplier; i++) {
ee9f5295 579 promises.add(pool.execute())
e211bc18
JB
580 }
581 await Promise.all(promises)
582 for (const workerNode of pool.workerNodes) {
465b2940 583 expect(workerNode.usage).toStrictEqual({
a4e07f72 584 tasks: {
76407b8e 585 executed: expect.any(Number),
a4e07f72
JB
586 executing: 0,
587 queued: 0,
df593701 588 maxQueued: 0,
463226a4 589 sequentiallyStolen: 0,
5ad42e34 590 stolen: 0,
a4e07f72
JB
591 failed: 0
592 },
593 runTime: {
4ba4c7f9 594 history: new CircularArray()
a4e07f72
JB
595 },
596 waitTime: {
4ba4c7f9 597 history: new CircularArray()
a4e07f72 598 },
5df69fab
JB
599 elu: {
600 idle: {
4ba4c7f9 601 history: new CircularArray()
5df69fab
JB
602 },
603 active: {
4ba4c7f9 604 history: new CircularArray()
71514351 605 }
5df69fab 606 }
e211bc18 607 })
465b2940
JB
608 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
609 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
76407b8e
JB
610 max * maxMultiplier
611 )
168c526f 612 }
e5c68e12 613 expect(
bcfb06ce
JB
614 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
615 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
616 ).nextWorkerNodeKey
617 ).toEqual(expect.any(Number))
618 expect(
bcfb06ce
JB
619 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
620 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
621 ).previousWorkerNodeKey
622 ).toEqual(expect.any(Number))
168c526f
JB
623 // We need to clean up the resources after our test
624 await pool.destroy()
625 })
626
6c6afb84
JB
627 it('Verify LEAST_BUSY strategy default policy', async () => {
628 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_BUSY
629 let pool = new FixedThreadPool(
630 max,
b2fd3f4a 631 './tests/worker-files/thread/testWorker.mjs',
6c6afb84
JB
632 { workerChoiceStrategy }
633 )
bcfb06ce 634 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
b1aae695
JB
635 dynamicWorkerUsage: false,
636 dynamicWorkerReady: true
6c6afb84
JB
637 })
638 await pool.destroy()
639 pool = new DynamicThreadPool(
640 min,
641 max,
b2fd3f4a 642 './tests/worker-files/thread/testWorker.mjs',
6c6afb84
JB
643 { workerChoiceStrategy }
644 )
bcfb06ce 645 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
b1aae695
JB
646 dynamicWorkerUsage: false,
647 dynamicWorkerReady: true
6c6afb84
JB
648 })
649 // We need to clean up the resources after our test
650 await pool.destroy()
651 })
652
653 it('Verify LEAST_BUSY strategy default tasks statistics requirements', async () => {
e4543b14 654 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_BUSY
168c526f
JB
655 let pool = new FixedThreadPool(
656 max,
b2fd3f4a 657 './tests/worker-files/thread/testWorker.mjs',
594bfb84 658 { workerChoiceStrategy }
168c526f 659 )
87de9ff5 660 expect(
bcfb06ce 661 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
87de9ff5 662 ).toStrictEqual({
932fc8be
JB
663 runTime: {
664 aggregate: true,
665 average: false,
666 median: false
667 },
668 waitTime: {
669 aggregate: true,
670 average: false,
671 median: false
672 },
5df69fab
JB
673 elu: {
674 aggregate: false,
675 average: false,
676 median: false
677 }
86bf340d 678 })
168c526f
JB
679 await pool.destroy()
680 pool = new DynamicThreadPool(
681 min,
682 max,
b2fd3f4a 683 './tests/worker-files/thread/testWorker.mjs',
594bfb84 684 { workerChoiceStrategy }
168c526f 685 )
87de9ff5 686 expect(
bcfb06ce 687 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
87de9ff5 688 ).toStrictEqual({
932fc8be
JB
689 runTime: {
690 aggregate: true,
691 average: false,
692 median: false
693 },
694 waitTime: {
695 aggregate: true,
696 average: false,
697 median: false
698 },
5df69fab
JB
699 elu: {
700 aggregate: false,
701 average: false,
702 median: false
703 }
86bf340d 704 })
168c526f
JB
705 // We need to clean up the resources after our test
706 await pool.destroy()
707 })
708
e4543b14 709 it('Verify LEAST_BUSY strategy can be run in a fixed pool', async () => {
168c526f
JB
710 const pool = new FixedThreadPool(
711 max,
b2fd3f4a 712 './tests/worker-files/thread/testWorker.mjs',
e4543b14 713 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY }
168c526f 714 )
e4543b14 715 // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose`
ee9f5295 716 const promises = new Set()
a20f0ba5
JB
717 const maxMultiplier = 2
718 for (let i = 0; i < max * maxMultiplier; i++) {
ee9f5295 719 promises.add(pool.execute())
e211bc18
JB
720 }
721 await Promise.all(promises)
722 for (const workerNode of pool.workerNodes) {
619f403b 723 expect(workerNode.usage).toStrictEqual({
a4e07f72
JB
724 tasks: {
725 executed: expect.any(Number),
726 executing: 0,
727 queued: 0,
df593701 728 maxQueued: 0,
463226a4 729 sequentiallyStolen: 0,
5ad42e34 730 stolen: 0,
a4e07f72
JB
731 failed: 0
732 },
619f403b 733 runTime: expect.objectContaining({
a4e07f72 734 history: expect.any(CircularArray)
619f403b
JB
735 }),
736 waitTime: expect.objectContaining({
a4e07f72 737 history: expect.any(CircularArray)
619f403b 738 }),
5df69fab
JB
739 elu: {
740 idle: {
619f403b 741 history: new CircularArray()
5df69fab
JB
742 },
743 active: {
619f403b 744 history: new CircularArray()
71514351 745 }
5df69fab 746 }
e211bc18 747 })
465b2940
JB
748 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
749 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
a4e07f72
JB
750 max * maxMultiplier
751 )
19dbc45b
JB
752 if (workerNode.usage.runTime.aggregate == null) {
753 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
754 } else {
755 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
756 }
757 if (workerNode.usage.waitTime.aggregate == null) {
758 expect(workerNode.usage.waitTime.aggregate).toBeUndefined()
759 } else {
760 expect(workerNode.usage.waitTime.aggregate).toBeGreaterThan(0)
761 }
168c526f 762 }
e5c68e12 763 expect(
bcfb06ce
JB
764 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
765 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
766 ).nextWorkerNodeKey
767 ).toEqual(expect.any(Number))
768 expect(
bcfb06ce
JB
769 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
770 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
771 ).previousWorkerNodeKey
772 ).toEqual(expect.any(Number))
168c526f
JB
773 // We need to clean up the resources after our test
774 await pool.destroy()
775 })
776
e4543b14 777 it('Verify LEAST_BUSY strategy can be run in a dynamic pool', async () => {
168c526f
JB
778 const pool = new DynamicThreadPool(
779 min,
780 max,
b2fd3f4a 781 './tests/worker-files/thread/testWorker.mjs',
e4543b14 782 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY }
168c526f 783 )
e4543b14 784 // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose`
ee9f5295 785 const promises = new Set()
a20f0ba5
JB
786 const maxMultiplier = 2
787 for (let i = 0; i < max * maxMultiplier; i++) {
ee9f5295 788 promises.add(pool.execute())
e211bc18
JB
789 }
790 await Promise.all(promises)
791 for (const workerNode of pool.workerNodes) {
619f403b 792 expect(workerNode.usage).toStrictEqual({
a4e07f72
JB
793 tasks: {
794 executed: expect.any(Number),
795 executing: 0,
796 queued: 0,
df593701 797 maxQueued: 0,
463226a4 798 sequentiallyStolen: 0,
5ad42e34 799 stolen: 0,
a4e07f72
JB
800 failed: 0
801 },
619f403b 802 runTime: expect.objectContaining({
a4e07f72 803 history: expect.any(CircularArray)
619f403b
JB
804 }),
805 waitTime: expect.objectContaining({
a4e07f72 806 history: expect.any(CircularArray)
619f403b 807 }),
5df69fab
JB
808 elu: {
809 idle: {
619f403b 810 history: new CircularArray()
5df69fab
JB
811 },
812 active: {
619f403b 813 history: new CircularArray()
71514351 814 }
5df69fab 815 }
e211bc18 816 })
465b2940
JB
817 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
818 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
a4e07f72
JB
819 max * maxMultiplier
820 )
19dbc45b
JB
821 if (workerNode.usage.runTime.aggregate == null) {
822 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
823 } else {
824 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
825 }
826 if (workerNode.usage.waitTime.aggregate == null) {
827 expect(workerNode.usage.waitTime.aggregate).toBeUndefined()
828 } else {
829 expect(workerNode.usage.waitTime.aggregate).toBeGreaterThan(0)
830 }
ff5e76e1 831 }
e5c68e12 832 expect(
bcfb06ce
JB
833 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
834 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
835 ).nextWorkerNodeKey
836 ).toEqual(expect.any(Number))
837 expect(
bcfb06ce
JB
838 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
839 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
840 ).previousWorkerNodeKey
841 ).toEqual(expect.any(Number))
ff5e76e1
JB
842 // We need to clean up the resources after our test
843 await pool.destroy()
844 })
845
6c6afb84
JB
846 it('Verify LEAST_ELU strategy default policy', async () => {
847 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_ELU
848 let pool = new FixedThreadPool(
849 max,
b2fd3f4a 850 './tests/worker-files/thread/testWorker.mjs',
6c6afb84
JB
851 { workerChoiceStrategy }
852 )
bcfb06ce 853 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
b1aae695
JB
854 dynamicWorkerUsage: false,
855 dynamicWorkerReady: true
6c6afb84
JB
856 })
857 await pool.destroy()
858 pool = new DynamicThreadPool(
859 min,
860 max,
b2fd3f4a 861 './tests/worker-files/thread/testWorker.mjs',
6c6afb84
JB
862 { workerChoiceStrategy }
863 )
bcfb06ce 864 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
b1aae695
JB
865 dynamicWorkerUsage: false,
866 dynamicWorkerReady: true
6c6afb84
JB
867 })
868 // We need to clean up the resources after our test
869 await pool.destroy()
870 })
871
872 it('Verify LEAST_ELU strategy default tasks statistics requirements', async () => {
a7bbf44a
JB
873 const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_ELU
874 let pool = new FixedThreadPool(
875 max,
b2fd3f4a 876 './tests/worker-files/thread/testWorker.mjs',
a7bbf44a
JB
877 { workerChoiceStrategy }
878 )
05302647 879 expect(
bcfb06ce 880 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
05302647 881 ).toStrictEqual({
e460940e
JB
882 runTime: {
883 aggregate: false,
884 average: false,
885 median: false
886 },
887 waitTime: {
888 aggregate: false,
889 average: false,
890 median: false
891 },
5df69fab
JB
892 elu: {
893 aggregate: true,
894 average: false,
895 median: false
896 }
a7bbf44a
JB
897 })
898 await pool.destroy()
899 pool = new DynamicThreadPool(
900 min,
901 max,
b2fd3f4a 902 './tests/worker-files/thread/testWorker.mjs',
a7bbf44a
JB
903 { workerChoiceStrategy }
904 )
05302647 905 expect(
bcfb06ce 906 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
05302647 907 ).toStrictEqual({
e460940e
JB
908 runTime: {
909 aggregate: false,
910 average: false,
911 median: false
912 },
913 waitTime: {
914 aggregate: false,
915 average: false,
916 median: false
917 },
5df69fab
JB
918 elu: {
919 aggregate: true,
920 average: false,
921 median: false
922 }
a7bbf44a
JB
923 })
924 // We need to clean up the resources after our test
925 await pool.destroy()
926 })
927
ae9cf3c8 928 it('Verify LEAST_ELU strategy can be run in a fixed pool', async () => {
c5ad42cd
JB
929 const pool = new FixedThreadPool(
930 max,
b2fd3f4a 931 './tests/worker-files/thread/testWorker.mjs',
c5ad42cd
JB
932 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_ELU }
933 )
934 // TODO: Create a better test to cover `LeastEluWorkerChoiceStrategy#choose`
dd38581f 935 const promises = new Set()
c5ad42cd
JB
936 const maxMultiplier = 2
937 for (let i = 0; i < max * maxMultiplier; i++) {
dd38581f 938 promises.add(pool.execute())
c5ad42cd 939 }
dd38581f 940 await Promise.all(promises)
c5ad42cd 941 for (const workerNode of pool.workerNodes) {
619f403b 942 expect(workerNode.usage).toStrictEqual({
c5ad42cd
JB
943 tasks: {
944 executed: expect.any(Number),
945 executing: 0,
946 queued: 0,
df593701 947 maxQueued: 0,
463226a4 948 sequentiallyStolen: 0,
5ad42e34 949 stolen: 0,
c5ad42cd
JB
950 failed: 0
951 },
952 runTime: {
619f403b 953 history: new CircularArray()
a1347286
JB
954 },
955 waitTime: {
619f403b 956 history: new CircularArray()
5df69fab 957 },
619f403b
JB
958 elu: expect.objectContaining({
959 idle: expect.objectContaining({
5df69fab 960 history: expect.any(CircularArray)
619f403b
JB
961 }),
962 active: expect.objectContaining({
5df69fab 963 history: expect.any(CircularArray)
619f403b
JB
964 })
965 })
5df69fab 966 })
465b2940
JB
967 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
968 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
a1347286
JB
969 max * maxMultiplier
970 )
7db63069
JB
971 if (workerNode.usage.elu.active.aggregate == null) {
972 expect(workerNode.usage.elu.active.aggregate).toBeUndefined()
973 } else {
974 expect(workerNode.usage.elu.active.aggregate).toBeGreaterThan(0)
975 }
976 if (workerNode.usage.elu.idle.aggregate == null) {
977 expect(workerNode.usage.elu.idle.aggregate).toBeUndefined()
978 } else {
979 expect(workerNode.usage.elu.idle.aggregate).toBeGreaterThanOrEqual(0)
980 }
71514351
JB
981 if (workerNode.usage.elu.utilization == null) {
982 expect(workerNode.usage.elu.utilization).toBeUndefined()
983 } else {
984 expect(workerNode.usage.elu.utilization).toBeGreaterThanOrEqual(0)
985 expect(workerNode.usage.elu.utilization).toBeLessThanOrEqual(1)
986 }
a1347286 987 }
e5c68e12 988 expect(
bcfb06ce
JB
989 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
990 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
991 ).nextWorkerNodeKey
992 ).toEqual(expect.any(Number))
993 expect(
bcfb06ce
JB
994 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
995 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
996 ).previousWorkerNodeKey
997 ).toEqual(expect.any(Number))
a1347286
JB
998 // We need to clean up the resources after our test
999 await pool.destroy()
1000 })
1001
1002 it('Verify LEAST_ELU strategy can be run in a dynamic pool', async () => {
1003 const pool = new DynamicThreadPool(
1004 min,
1005 max,
b2fd3f4a 1006 './tests/worker-files/thread/testWorker.mjs',
a1347286
JB
1007 { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_ELU }
1008 )
1009 // TODO: Create a better test to cover `LeastEluWorkerChoiceStrategy#choose`
dd38581f 1010 const promises = new Set()
a1347286
JB
1011 const maxMultiplier = 2
1012 for (let i = 0; i < max * maxMultiplier; i++) {
dd38581f 1013 promises.add(pool.execute())
a1347286 1014 }
dd38581f 1015 await Promise.all(promises)
a1347286 1016 for (const workerNode of pool.workerNodes) {
619f403b 1017 expect(workerNode.usage).toStrictEqual({
a1347286
JB
1018 tasks: {
1019 executed: expect.any(Number),
1020 executing: 0,
1021 queued: 0,
df593701 1022 maxQueued: 0,
463226a4 1023 sequentiallyStolen: 0,
5ad42e34 1024 stolen: 0,
a1347286
JB
1025 failed: 0
1026 },
1027 runTime: {
619f403b 1028 history: new CircularArray()
c5ad42cd
JB
1029 },
1030 waitTime: {
619f403b 1031 history: new CircularArray()
5df69fab 1032 },
619f403b
JB
1033 elu: expect.objectContaining({
1034 idle: expect.objectContaining({
5df69fab 1035 history: expect.any(CircularArray)
619f403b
JB
1036 }),
1037 active: expect.objectContaining({
5df69fab 1038 history: expect.any(CircularArray)
619f403b
JB
1039 })
1040 })
5df69fab 1041 })
465b2940
JB
1042 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1043 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
c5ad42cd
JB
1044 max * maxMultiplier
1045 )
7db63069
JB
1046 if (workerNode.usage.elu.active.aggregate == null) {
1047 expect(workerNode.usage.elu.active.aggregate).toBeUndefined()
1048 } else {
1049 expect(workerNode.usage.elu.active.aggregate).toBeGreaterThan(0)
1050 }
1051 if (workerNode.usage.elu.idle.aggregate == null) {
1052 expect(workerNode.usage.elu.idle.aggregate).toBeUndefined()
1053 } else {
1054 expect(workerNode.usage.elu.idle.aggregate).toBeGreaterThanOrEqual(0)
1055 }
71514351
JB
1056 if (workerNode.usage.elu.utilization == null) {
1057 expect(workerNode.usage.elu.utilization).toBeUndefined()
1058 } else {
1059 expect(workerNode.usage.elu.utilization).toBeGreaterThanOrEqual(0)
1060 expect(workerNode.usage.elu.utilization).toBeLessThanOrEqual(1)
1061 }
c5ad42cd 1062 }
e5c68e12 1063 expect(
bcfb06ce
JB
1064 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1065 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
1066 ).nextWorkerNodeKey
1067 ).toEqual(expect.any(Number))
1068 expect(
bcfb06ce
JB
1069 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1070 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
1071 ).previousWorkerNodeKey
1072 ).toEqual(expect.any(Number))
c5ad42cd
JB
1073 // We need to clean up the resources after our test
1074 await pool.destroy()
1075 })
1076
6c6afb84
JB
1077 it('Verify FAIR_SHARE strategy default policy', async () => {
1078 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
1079 let pool = new FixedThreadPool(
1080 max,
b2fd3f4a 1081 './tests/worker-files/thread/testWorker.mjs',
6c6afb84
JB
1082 { workerChoiceStrategy }
1083 )
bcfb06ce 1084 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
b1aae695
JB
1085 dynamicWorkerUsage: false,
1086 dynamicWorkerReady: true
6c6afb84
JB
1087 })
1088 await pool.destroy()
1089 pool = new DynamicThreadPool(
1090 min,
1091 max,
b2fd3f4a 1092 './tests/worker-files/thread/testWorker.mjs',
6c6afb84
JB
1093 { workerChoiceStrategy }
1094 )
bcfb06ce 1095 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
b1aae695
JB
1096 dynamicWorkerUsage: false,
1097 dynamicWorkerReady: true
6c6afb84
JB
1098 })
1099 // We need to clean up the resources after our test
1100 await pool.destroy()
1101 })
1102
1103 it('Verify FAIR_SHARE strategy default tasks statistics requirements', async () => {
594bfb84 1104 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
10fcfaf4
JB
1105 let pool = new FixedThreadPool(
1106 max,
b2fd3f4a 1107 './tests/worker-files/thread/testWorker.mjs',
594bfb84 1108 { workerChoiceStrategy }
10fcfaf4 1109 )
87de9ff5 1110 expect(
bcfb06ce 1111 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
87de9ff5 1112 ).toStrictEqual({
932fc8be
JB
1113 runTime: {
1114 aggregate: true,
1115 average: true,
1116 median: false
1117 },
1118 waitTime: {
1119 aggregate: false,
1120 average: false,
1121 median: false
1122 },
5df69fab 1123 elu: {
9adcefab
JB
1124 aggregate: true,
1125 average: true,
5df69fab
JB
1126 median: false
1127 }
86bf340d 1128 })
fd7ebd49 1129 await pool.destroy()
10fcfaf4
JB
1130 pool = new DynamicThreadPool(
1131 min,
1132 max,
b2fd3f4a 1133 './tests/worker-files/thread/testWorker.mjs',
594bfb84 1134 { workerChoiceStrategy }
10fcfaf4 1135 )
87de9ff5 1136 expect(
bcfb06ce 1137 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
87de9ff5 1138 ).toStrictEqual({
932fc8be
JB
1139 runTime: {
1140 aggregate: true,
1141 average: true,
1142 median: false
1143 },
1144 waitTime: {
1145 aggregate: false,
1146 average: false,
1147 median: false
1148 },
5df69fab 1149 elu: {
9adcefab
JB
1150 aggregate: true,
1151 average: true,
5df69fab
JB
1152 median: false
1153 }
86bf340d 1154 })
10fcfaf4
JB
1155 // We need to clean up the resources after our test
1156 await pool.destroy()
1157 })
1158
23ff945a 1159 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
23ff945a
JB
1160 const pool = new FixedThreadPool(
1161 max,
b2fd3f4a 1162 './tests/worker-files/thread/testWorker.mjs',
23ff945a
JB
1163 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
1164 )
1165 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
ee9f5295 1166 const promises = new Set()
a20f0ba5
JB
1167 const maxMultiplier = 2
1168 for (let i = 0; i < max * maxMultiplier; i++) {
ee9f5295 1169 promises.add(pool.execute())
23ff945a 1170 }
e211bc18 1171 await Promise.all(promises)
138d29a8 1172 for (const workerNode of pool.workerNodes) {
619f403b 1173 expect(workerNode.usage).toStrictEqual({
a4e07f72 1174 tasks: {
d33be430 1175 executed: expect.any(Number),
a4e07f72
JB
1176 executing: 0,
1177 queued: 0,
df593701 1178 maxQueued: 0,
463226a4 1179 sequentiallyStolen: 0,
5ad42e34 1180 stolen: 0,
a4e07f72
JB
1181 failed: 0
1182 },
619f403b 1183 runTime: expect.objectContaining({
a4e07f72 1184 history: expect.any(CircularArray)
619f403b 1185 }),
a4e07f72 1186 waitTime: {
619f403b 1187 history: new CircularArray()
a4e07f72 1188 },
619f403b
JB
1189 elu: expect.objectContaining({
1190 idle: expect.objectContaining({
5df69fab 1191 history: expect.any(CircularArray)
619f403b
JB
1192 }),
1193 active: expect.objectContaining({
5df69fab 1194 history: expect.any(CircularArray)
619f403b
JB
1195 })
1196 })
86bf340d 1197 })
465b2940
JB
1198 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1199 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
d33be430
JB
1200 max * maxMultiplier
1201 )
71514351
JB
1202 if (workerNode.usage.runTime.aggregate == null) {
1203 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1204 } else {
1205 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1206 }
1207 if (workerNode.usage.runTime.average == null) {
1208 expect(workerNode.usage.runTime.average).toBeUndefined()
1209 } else {
1210 expect(workerNode.usage.runTime.average).toBeGreaterThan(0)
1211 }
7db63069
JB
1212 if (workerNode.usage.elu.active.aggregate == null) {
1213 expect(workerNode.usage.elu.active.aggregate).toBeUndefined()
1214 } else {
1215 expect(workerNode.usage.elu.active.aggregate).toBeGreaterThan(0)
1216 }
1217 if (workerNode.usage.elu.idle.aggregate == null) {
1218 expect(workerNode.usage.elu.idle.aggregate).toBeUndefined()
1219 } else {
1220 expect(workerNode.usage.elu.idle.aggregate).toBeGreaterThanOrEqual(0)
1221 }
71514351
JB
1222 if (workerNode.usage.elu.utilization == null) {
1223 expect(workerNode.usage.elu.utilization).toBeUndefined()
1224 } else {
1225 expect(workerNode.usage.elu.utilization).toBeGreaterThanOrEqual(0)
1226 expect(workerNode.usage.elu.utilization).toBeLessThanOrEqual(1)
1227 }
946b809b 1228 expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeGreaterThan(0)
138d29a8 1229 }
e5c68e12 1230 expect(
bcfb06ce
JB
1231 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1232 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
1233 ).nextWorkerNodeKey
1234 ).toEqual(expect.any(Number))
1235 expect(
bcfb06ce
JB
1236 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1237 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
1238 ).previousWorkerNodeKey
1239 ).toEqual(expect.any(Number))
23ff945a
JB
1240 // We need to clean up the resources after our test
1241 await pool.destroy()
1242 })
1243
1244 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
23ff945a
JB
1245 const pool = new DynamicThreadPool(
1246 min,
1247 max,
b2fd3f4a 1248 './tests/worker-files/thread/testWorker.mjs',
23ff945a
JB
1249 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
1250 )
1251 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
ee9f5295 1252 const promises = new Set()
f7070eee 1253 const maxMultiplier = 2
804a889e 1254 for (let i = 0; i < max * maxMultiplier; i++) {
ee9f5295 1255 promises.add(pool.execute())
23ff945a 1256 }
e211bc18 1257 await Promise.all(promises)
138d29a8 1258 for (const workerNode of pool.workerNodes) {
619f403b 1259 expect(workerNode.usage).toStrictEqual({
a4e07f72 1260 tasks: {
6c6afb84 1261 executed: expect.any(Number),
a4e07f72
JB
1262 executing: 0,
1263 queued: 0,
df593701 1264 maxQueued: 0,
463226a4 1265 sequentiallyStolen: 0,
5ad42e34 1266 stolen: 0,
a4e07f72
JB
1267 failed: 0
1268 },
619f403b 1269 runTime: expect.objectContaining({
a4e07f72 1270 history: expect.any(CircularArray)
619f403b 1271 }),
a4e07f72 1272 waitTime: {
619f403b 1273 history: new CircularArray()
a4e07f72 1274 },
619f403b
JB
1275 elu: expect.objectContaining({
1276 idle: expect.objectContaining({
5df69fab 1277 history: expect.any(CircularArray)
619f403b
JB
1278 }),
1279 active: expect.objectContaining({
5df69fab 1280 history: expect.any(CircularArray)
619f403b
JB
1281 })
1282 })
86bf340d 1283 })
465b2940
JB
1284 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1285 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
6c6afb84
JB
1286 max * maxMultiplier
1287 )
71514351
JB
1288 if (workerNode.usage.runTime.aggregate == null) {
1289 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1290 } else {
1291 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1292 }
1293 if (workerNode.usage.runTime.average == null) {
1294 expect(workerNode.usage.runTime.average).toBeUndefined()
1295 } else {
1296 expect(workerNode.usage.runTime.average).toBeGreaterThan(0)
1297 }
7db63069
JB
1298 if (workerNode.usage.elu.active.aggregate == null) {
1299 expect(workerNode.usage.elu.active.aggregate).toBeUndefined()
1300 } else {
1301 expect(workerNode.usage.elu.active.aggregate).toBeGreaterThan(0)
1302 }
1303 if (workerNode.usage.elu.idle.aggregate == null) {
1304 expect(workerNode.usage.elu.idle.aggregate).toBeUndefined()
1305 } else {
1306 expect(workerNode.usage.elu.idle.aggregate).toBeGreaterThanOrEqual(0)
1307 }
71514351
JB
1308 if (workerNode.usage.elu.utilization == null) {
1309 expect(workerNode.usage.elu.utilization).toBeUndefined()
1310 } else {
1311 expect(workerNode.usage.elu.utilization).toBeGreaterThanOrEqual(0)
1312 expect(workerNode.usage.elu.utilization).toBeLessThanOrEqual(1)
1313 }
946b809b 1314 expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeGreaterThan(0)
138d29a8 1315 }
e5c68e12 1316 expect(
bcfb06ce
JB
1317 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1318 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
1319 ).nextWorkerNodeKey
1320 ).toEqual(expect.any(Number))
1321 expect(
bcfb06ce
JB
1322 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1323 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
1324 ).previousWorkerNodeKey
1325 ).toEqual(expect.any(Number))
23ff945a
JB
1326 // We need to clean up the resources after our test
1327 await pool.destroy()
1328 })
1329
9e775f96 1330 it('Verify FAIR_SHARE strategy can be run in a dynamic pool with median runtime statistic', async () => {
010d7020
JB
1331 const pool = new DynamicThreadPool(
1332 min,
1333 max,
b2fd3f4a 1334 './tests/worker-files/thread/testWorker.mjs',
010d7020
JB
1335 {
1336 workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE,
1337 workerChoiceStrategyOptions: {
932fc8be 1338 runTime: { median: true }
010d7020
JB
1339 }
1340 }
1341 )
1342 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
ee9f5295 1343 const promises = new Set()
010d7020
JB
1344 const maxMultiplier = 2
1345 for (let i = 0; i < max * maxMultiplier; i++) {
ee9f5295 1346 promises.add(pool.execute())
010d7020 1347 }
e211bc18 1348 await Promise.all(promises)
010d7020 1349 for (const workerNode of pool.workerNodes) {
619f403b 1350 expect(workerNode.usage).toStrictEqual({
a4e07f72 1351 tasks: {
6c6afb84 1352 executed: expect.any(Number),
a4e07f72
JB
1353 executing: 0,
1354 queued: 0,
df593701 1355 maxQueued: 0,
463226a4 1356 sequentiallyStolen: 0,
5ad42e34 1357 stolen: 0,
a4e07f72
JB
1358 failed: 0
1359 },
619f403b 1360 runTime: expect.objectContaining({
a4e07f72 1361 history: expect.any(CircularArray)
619f403b 1362 }),
a4e07f72 1363 waitTime: {
619f403b 1364 history: new CircularArray()
a4e07f72 1365 },
619f403b
JB
1366 elu: expect.objectContaining({
1367 idle: expect.objectContaining({
5df69fab 1368 history: expect.any(CircularArray)
619f403b
JB
1369 }),
1370 active: expect.objectContaining({
5df69fab 1371 history: expect.any(CircularArray)
619f403b
JB
1372 })
1373 })
86bf340d 1374 })
465b2940
JB
1375 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1376 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
6c6afb84
JB
1377 max * maxMultiplier
1378 )
71514351
JB
1379 if (workerNode.usage.runTime.aggregate == null) {
1380 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1381 } else {
1382 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1383 }
1384 if (workerNode.usage.runTime.median == null) {
1385 expect(workerNode.usage.runTime.median).toBeUndefined()
1386 } else {
1387 expect(workerNode.usage.runTime.median).toBeGreaterThan(0)
1388 }
7db63069
JB
1389 if (workerNode.usage.elu.active.aggregate == null) {
1390 expect(workerNode.usage.elu.active.aggregate).toBeUndefined()
1391 } else {
1392 expect(workerNode.usage.elu.active.aggregate).toBeGreaterThan(0)
1393 }
1394 if (workerNode.usage.elu.idle.aggregate == null) {
1395 expect(workerNode.usage.elu.idle.aggregate).toBeUndefined()
1396 } else {
1397 expect(workerNode.usage.elu.idle.aggregate).toBeGreaterThanOrEqual(0)
1398 }
71514351
JB
1399 if (workerNode.usage.elu.utilization == null) {
1400 expect(workerNode.usage.elu.utilization).toBeUndefined()
1401 } else {
1402 expect(workerNode.usage.elu.utilization).toBeGreaterThanOrEqual(0)
1403 expect(workerNode.usage.elu.utilization).toBeLessThanOrEqual(1)
1404 }
946b809b 1405 expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeGreaterThan(0)
010d7020 1406 }
2b4fddb8 1407 expect(
bcfb06ce
JB
1408 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1409 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
1410 ).nextWorkerNodeKey
1411 ).toEqual(expect.any(Number))
1412 expect(
bcfb06ce
JB
1413 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1414 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
e5c68e12
JB
1415 ).previousWorkerNodeKey
1416 ).toEqual(expect.any(Number))
010d7020
JB
1417 // We need to clean up the resources after our test
1418 await pool.destroy()
1419 })
1420
bcfb06ce 1421 it("Verify FAIR_SHARE strategy internals aren't reset after setting it", async () => {
594bfb84 1422 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
f0829c53 1423 let pool = new FixedThreadPool(
caeb9817 1424 max,
b2fd3f4a 1425 './tests/worker-files/thread/testWorker.mjs'
caeb9817 1426 )
f3a91bac
JB
1427 for (const workerNode of pool.workerNodes) {
1428 workerNode.strategyData = {
1429 virtualTaskEndTimestamp: performance.now()
1430 }
1431 }
594bfb84 1432 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
f3a91bac 1433 for (const workerNode of pool.workerNodes) {
bcfb06ce 1434 expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeGreaterThan(0)
f3a91bac 1435 }
f0829c53
JB
1436 await pool.destroy()
1437 pool = new DynamicThreadPool(
1438 min,
1439 max,
b2fd3f4a 1440 './tests/worker-files/thread/testWorker.mjs'
f0829c53 1441 )
f3a91bac
JB
1442 for (const workerNode of pool.workerNodes) {
1443 workerNode.strategyData = {
1444 virtualTaskEndTimestamp: performance.now()
1445 }
1446 }
594bfb84 1447 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
f3a91bac 1448 for (const workerNode of pool.workerNodes) {
bcfb06ce 1449 expect(workerNode.strategyData.virtualTaskEndTimestamp).toBeGreaterThan(0)
f3a91bac 1450 }
caeb9817
JB
1451 // We need to clean up the resources after our test
1452 await pool.destroy()
1453 })
1454
6c6afb84
JB
1455 it('Verify WEIGHTED_ROUND_ROBIN strategy default policy', async () => {
1456 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
1457 let pool = new FixedThreadPool(
1458 max,
b2fd3f4a 1459 './tests/worker-files/thread/testWorker.mjs',
6c6afb84
JB
1460 { workerChoiceStrategy }
1461 )
bcfb06ce 1462 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
b1aae695
JB
1463 dynamicWorkerUsage: false,
1464 dynamicWorkerReady: true
6c6afb84
JB
1465 })
1466 await pool.destroy()
1467 pool = new DynamicThreadPool(
1468 min,
1469 max,
b2fd3f4a 1470 './tests/worker-files/thread/testWorker.mjs',
6c6afb84
JB
1471 { workerChoiceStrategy }
1472 )
bcfb06ce 1473 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
b1aae695
JB
1474 dynamicWorkerUsage: false,
1475 dynamicWorkerReady: true
6c6afb84
JB
1476 })
1477 // We need to clean up the resources after our test
1478 await pool.destroy()
1479 })
1480
1481 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks statistics requirements', async () => {
594bfb84 1482 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
10fcfaf4
JB
1483 let pool = new FixedThreadPool(
1484 max,
b2fd3f4a 1485 './tests/worker-files/thread/testWorker.mjs',
594bfb84 1486 { workerChoiceStrategy }
10fcfaf4 1487 )
87de9ff5 1488 expect(
bcfb06ce 1489 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
87de9ff5 1490 ).toStrictEqual({
932fc8be
JB
1491 runTime: {
1492 aggregate: true,
1493 average: true,
1494 median: false
1495 },
1496 waitTime: {
1497 aggregate: false,
1498 average: false,
1499 median: false
1500 },
5df69fab
JB
1501 elu: {
1502 aggregate: false,
1503 average: false,
1504 median: false
1505 }
86bf340d 1506 })
fd7ebd49 1507 await pool.destroy()
10fcfaf4
JB
1508 pool = new DynamicThreadPool(
1509 min,
1510 max,
b2fd3f4a 1511 './tests/worker-files/thread/testWorker.mjs',
594bfb84 1512 { workerChoiceStrategy }
10fcfaf4 1513 )
87de9ff5 1514 expect(
bcfb06ce 1515 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
87de9ff5 1516 ).toStrictEqual({
932fc8be
JB
1517 runTime: {
1518 aggregate: true,
1519 average: true,
1520 median: false
1521 },
1522 waitTime: {
1523 aggregate: false,
1524 average: false,
1525 median: false
1526 },
5df69fab
JB
1527 elu: {
1528 aggregate: false,
1529 average: false,
1530 median: false
1531 }
86bf340d 1532 })
10fcfaf4
JB
1533 // We need to clean up the resources after our test
1534 await pool.destroy()
1535 })
1536
b3432a63 1537 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
b3432a63
JB
1538 const pool = new FixedThreadPool(
1539 max,
b2fd3f4a 1540 './tests/worker-files/thread/testWorker.mjs',
b3432a63
JB
1541 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
1542 )
1543 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
ee9f5295 1544 const promises = new Set()
a20f0ba5
JB
1545 const maxMultiplier = 2
1546 for (let i = 0; i < max * maxMultiplier; i++) {
ee9f5295 1547 promises.add(pool.execute())
b3432a63 1548 }
e211bc18 1549 await Promise.all(promises)
138d29a8 1550 for (const workerNode of pool.workerNodes) {
465b2940 1551 expect(workerNode.usage).toStrictEqual({
a4e07f72
JB
1552 tasks: {
1553 executed: expect.any(Number),
1554 executing: 0,
1555 queued: 0,
df593701 1556 maxQueued: 0,
463226a4 1557 sequentiallyStolen: 0,
5ad42e34 1558 stolen: 0,
a4e07f72
JB
1559 failed: 0
1560 },
71514351 1561 runTime: expect.objectContaining({
a4e07f72 1562 history: expect.any(CircularArray)
71514351 1563 }),
a4e07f72 1564 waitTime: {
619f403b 1565 history: new CircularArray()
a4e07f72 1566 },
5df69fab
JB
1567 elu: {
1568 idle: {
619f403b 1569 history: new CircularArray()
5df69fab
JB
1570 },
1571 active: {
619f403b 1572 history: new CircularArray()
71514351 1573 }
5df69fab 1574 }
86bf340d 1575 })
465b2940
JB
1576 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1577 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
a4e07f72
JB
1578 max * maxMultiplier
1579 )
71514351
JB
1580 if (workerNode.usage.runTime.aggregate == null) {
1581 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1582 } else {
1583 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1584 }
1585 if (workerNode.usage.runTime.average == null) {
1586 expect(workerNode.usage.runTime.average).toBeUndefined()
1587 } else {
1588 expect(workerNode.usage.runTime.average).toBeGreaterThan(0)
1589 }
138d29a8 1590 }
516dcb0d 1591 expect(
bcfb06ce
JB
1592 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1593 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
516dcb0d
JB
1594 ).nextWorkerNodeKey
1595 ).toBe(0)
1596 expect(
bcfb06ce
JB
1597 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1598 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
516dcb0d 1599 ).previousWorkerNodeKey
11cc661f 1600 ).toEqual(0)
08f3f44c 1601 expect(
bcfb06ce
JB
1602 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1603 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
f3a91bac 1604 ).workerNodeVirtualTaskRunTime
08f3f44c 1605 ).toBeGreaterThanOrEqual(0)
b3432a63
JB
1606 // We need to clean up the resources after our test
1607 await pool.destroy()
1608 })
1609
1610 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
b3432a63
JB
1611 const pool = new DynamicThreadPool(
1612 min,
1613 max,
b2fd3f4a 1614 './tests/worker-files/thread/testWorker.mjs',
b3432a63
JB
1615 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
1616 )
1617 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
ee9f5295 1618 const promises = new Set()
138d29a8 1619 const maxMultiplier = 2
5502c07c 1620 for (let i = 0; i < max * maxMultiplier; i++) {
ee9f5295 1621 promises.add(pool.execute())
b3432a63 1622 }
e211bc18 1623 await Promise.all(promises)
138d29a8 1624 for (const workerNode of pool.workerNodes) {
465b2940 1625 expect(workerNode.usage).toStrictEqual({
a4e07f72
JB
1626 tasks: {
1627 executed: expect.any(Number),
1628 executing: 0,
1629 queued: 0,
df593701 1630 maxQueued: 0,
463226a4 1631 sequentiallyStolen: 0,
5ad42e34 1632 stolen: 0,
a4e07f72
JB
1633 failed: 0
1634 },
b1aae695 1635 runTime: expect.objectContaining({
a4e07f72 1636 history: expect.any(CircularArray)
b1aae695 1637 }),
a4e07f72 1638 waitTime: {
619f403b 1639 history: new CircularArray()
a4e07f72 1640 },
5df69fab
JB
1641 elu: {
1642 idle: {
619f403b 1643 history: new CircularArray()
5df69fab
JB
1644 },
1645 active: {
619f403b 1646 history: new CircularArray()
71514351 1647 }
5df69fab 1648 }
86bf340d 1649 })
465b2940
JB
1650 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1651 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
a4e07f72
JB
1652 max * maxMultiplier
1653 )
b1aae695
JB
1654 if (workerNode.usage.runTime.aggregate == null) {
1655 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1656 } else {
1657 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1658 }
1659 if (workerNode.usage.runTime.average == null) {
1660 expect(workerNode.usage.runTime.average).toBeUndefined()
1661 } else {
1662 expect(workerNode.usage.runTime.average).toBeGreaterThan(0)
1663 }
138d29a8 1664 }
516dcb0d 1665 expect(
bcfb06ce
JB
1666 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1667 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
516dcb0d 1668 ).nextWorkerNodeKey
11cc661f 1669 ).toEqual(0)
516dcb0d 1670 expect(
bcfb06ce
JB
1671 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1672 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
516dcb0d 1673 ).previousWorkerNodeKey
11cc661f 1674 ).toEqual(0)
2b4fddb8 1675 expect(
bcfb06ce
JB
1676 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1677 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
f3a91bac 1678 ).workerNodeVirtualTaskRunTime
2b4fddb8 1679 ).toBeGreaterThanOrEqual(0)
b3432a63
JB
1680 // We need to clean up the resources after our test
1681 await pool.destroy()
1682 })
1683
9e775f96 1684 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool with median runtime statistic', async () => {
010d7020
JB
1685 const pool = new DynamicThreadPool(
1686 min,
1687 max,
b2fd3f4a 1688 './tests/worker-files/thread/testWorker.mjs',
010d7020
JB
1689 {
1690 workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
1691 workerChoiceStrategyOptions: {
932fc8be 1692 runTime: { median: true }
010d7020
JB
1693 }
1694 }
1695 )
1696 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
ee9f5295 1697 const promises = new Set()
010d7020
JB
1698 const maxMultiplier = 2
1699 for (let i = 0; i < max * maxMultiplier; i++) {
ee9f5295 1700 promises.add(pool.execute())
010d7020 1701 }
e211bc18 1702 await Promise.all(promises)
010d7020 1703 for (const workerNode of pool.workerNodes) {
465b2940 1704 expect(workerNode.usage).toStrictEqual({
a4e07f72
JB
1705 tasks: {
1706 executed: expect.any(Number),
1707 executing: 0,
1708 queued: 0,
df593701 1709 maxQueued: 0,
463226a4 1710 sequentiallyStolen: 0,
5ad42e34 1711 stolen: 0,
a4e07f72
JB
1712 failed: 0
1713 },
b1aae695 1714 runTime: expect.objectContaining({
a4e07f72 1715 history: expect.any(CircularArray)
b1aae695 1716 }),
a4e07f72 1717 waitTime: {
619f403b 1718 history: new CircularArray()
a4e07f72 1719 },
5df69fab
JB
1720 elu: {
1721 idle: {
619f403b 1722 history: new CircularArray()
5df69fab
JB
1723 },
1724 active: {
619f403b 1725 history: new CircularArray()
71514351 1726 }
5df69fab 1727 }
86bf340d 1728 })
465b2940
JB
1729 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1730 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
a4e07f72
JB
1731 max * maxMultiplier
1732 )
b1aae695
JB
1733 if (workerNode.usage.runTime.aggregate == null) {
1734 expect(workerNode.usage.runTime.aggregate).toBeUndefined()
1735 } else {
1736 expect(workerNode.usage.runTime.aggregate).toBeGreaterThan(0)
1737 }
1738 if (workerNode.usage.runTime.median == null) {
1739 expect(workerNode.usage.runTime.median).toBeUndefined()
1740 } else {
1741 expect(workerNode.usage.runTime.median).toBeGreaterThan(0)
1742 }
010d7020 1743 }
516dcb0d 1744 expect(
bcfb06ce
JB
1745 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1746 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
516dcb0d 1747 ).nextWorkerNodeKey
11cc661f 1748 ).toEqual(0)
516dcb0d 1749 expect(
bcfb06ce
JB
1750 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1751 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
516dcb0d 1752 ).previousWorkerNodeKey
11cc661f 1753 ).toEqual(0)
08f3f44c 1754 expect(
bcfb06ce
JB
1755 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1756 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
f3a91bac 1757 ).workerNodeVirtualTaskRunTime
08f3f44c 1758 ).toBeGreaterThanOrEqual(0)
010d7020
JB
1759 // We need to clean up the resources after our test
1760 await pool.destroy()
1761 })
1762
bcfb06ce 1763 it("Verify WEIGHTED_ROUND_ROBIN strategy internals aren't reset after setting it", async () => {
594bfb84 1764 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
f0829c53 1765 let pool = new FixedThreadPool(
caeb9817 1766 max,
bcfb06ce
JB
1767 './tests/worker-files/thread/testWorker.mjs',
1768 { workerChoiceStrategy }
caeb9817 1769 )
bcfb06ce
JB
1770 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1771 workerChoiceStrategy
1772 ).nextWorkerNodeKey = randomInt(1, max - 1)
1773 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1774 workerChoiceStrategy
1775 ).previousWorkerNodeKey = randomInt(1, max - 1)
1776 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1777 workerChoiceStrategy
1778 ).workerNodeVirtualTaskRunTime = randomInt(100, 1000)
594bfb84 1779 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
a6f7f1b4 1780 expect(
bcfb06ce
JB
1781 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1782 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
9b106837 1783 ).nextWorkerNodeKey
bcfb06ce 1784 ).toBeGreaterThan(0)
086fd843 1785 expect(
bcfb06ce
JB
1786 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1787 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
086fd843 1788 ).previousWorkerNodeKey
bcfb06ce 1789 ).toBeGreaterThan(0)
08f3f44c 1790 expect(
bcfb06ce
JB
1791 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1792 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
f3a91bac 1793 ).workerNodeVirtualTaskRunTime
bcfb06ce 1794 ).toBeGreaterThan(99)
f0829c53
JB
1795 await pool.destroy()
1796 pool = new DynamicThreadPool(
1797 min,
1798 max,
bcfb06ce
JB
1799 './tests/worker-files/thread/testWorker.mjs',
1800 { workerChoiceStrategy }
f0829c53 1801 )
bcfb06ce
JB
1802 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1803 workerChoiceStrategy
1804 ).nextWorkerNodeKey = randomInt(1, max - 1)
1805 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1806 workerChoiceStrategy
1807 ).previousWorkerNodeKey = randomInt(1, max - 1)
1808 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1809 workerChoiceStrategy
1810 ).workerNodeVirtualTaskRunTime = randomInt(100, 1000)
594bfb84 1811 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
a6f7f1b4 1812 expect(
bcfb06ce
JB
1813 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1814 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
9b106837 1815 ).nextWorkerNodeKey
bcfb06ce 1816 ).toBeGreaterThan(0)
086fd843 1817 expect(
bcfb06ce
JB
1818 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1819 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
086fd843 1820 ).previousWorkerNodeKey
bcfb06ce 1821 ).toBeGreaterThan(0)
08f3f44c 1822 expect(
bcfb06ce
JB
1823 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1824 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
f3a91bac 1825 ).workerNodeVirtualTaskRunTime
bcfb06ce 1826 ).toBeGreaterThan(99)
caeb9817
JB
1827 // We need to clean up the resources after our test
1828 await pool.destroy()
1829 })
1830
6c6afb84
JB
1831 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy default policy', async () => {
1832 const workerChoiceStrategy =
1833 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1834 let pool = new FixedThreadPool(
1835 max,
b2fd3f4a 1836 './tests/worker-files/thread/testWorker.mjs',
6c6afb84
JB
1837 { workerChoiceStrategy }
1838 )
bcfb06ce 1839 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
b1aae695
JB
1840 dynamicWorkerUsage: false,
1841 dynamicWorkerReady: true
6c6afb84
JB
1842 })
1843 await pool.destroy()
1844 pool = new DynamicThreadPool(
1845 min,
1846 max,
b2fd3f4a 1847 './tests/worker-files/thread/testWorker.mjs',
6c6afb84
JB
1848 { workerChoiceStrategy }
1849 )
bcfb06ce 1850 expect(pool.workerChoiceStrategiesContext.getPolicy()).toStrictEqual({
b1aae695
JB
1851 dynamicWorkerUsage: false,
1852 dynamicWorkerReady: true
6c6afb84
JB
1853 })
1854 // We need to clean up the resources after our test
1855 await pool.destroy()
1856 })
1857
1858 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy default tasks statistics requirements', async () => {
e52fb978
JB
1859 const workerChoiceStrategy =
1860 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1861 let pool = new FixedThreadPool(
1862 max,
b2fd3f4a 1863 './tests/worker-files/thread/testWorker.mjs',
e52fb978
JB
1864 { workerChoiceStrategy }
1865 )
87de9ff5 1866 expect(
bcfb06ce 1867 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
87de9ff5 1868 ).toStrictEqual({
932fc8be 1869 runTime: {
619f403b
JB
1870 aggregate: true,
1871 average: true,
932fc8be
JB
1872 median: false
1873 },
1874 waitTime: {
1875 aggregate: false,
1876 average: false,
1877 median: false
1878 },
5df69fab
JB
1879 elu: {
1880 aggregate: false,
1881 average: false,
1882 median: false
1883 }
e52fb978
JB
1884 })
1885 await pool.destroy()
1886 pool = new DynamicThreadPool(
1887 min,
1888 max,
b2fd3f4a 1889 './tests/worker-files/thread/testWorker.mjs',
e52fb978
JB
1890 { workerChoiceStrategy }
1891 )
87de9ff5 1892 expect(
bcfb06ce 1893 pool.workerChoiceStrategiesContext.getTaskStatisticsRequirements()
87de9ff5 1894 ).toStrictEqual({
932fc8be 1895 runTime: {
619f403b
JB
1896 aggregate: true,
1897 average: true,
932fc8be
JB
1898 median: false
1899 },
1900 waitTime: {
1901 aggregate: false,
1902 average: false,
1903 median: false
1904 },
5df69fab
JB
1905 elu: {
1906 aggregate: false,
1907 average: false,
1908 median: false
1909 }
e52fb978
JB
1910 })
1911 // We need to clean up the resources after our test
1912 await pool.destroy()
1913 })
1914
9ef8fa71 1915 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
e62e7646
JB
1916 const pool = new FixedThreadPool(
1917 max,
b2fd3f4a 1918 './tests/worker-files/thread/testWorker.mjs',
e62e7646
JB
1919 {
1920 workerChoiceStrategy:
1921 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
1922 }
1923 )
1924 // TODO: Create a better test to cover `InterleavedWeightedRoundRobinWorkerChoiceStrategy#choose`
1925 const promises = new Set()
1926 const maxMultiplier = 2
1927 for (let i = 0; i < max * maxMultiplier; i++) {
1928 promises.add(pool.execute())
1929 }
1930 await Promise.all(promises)
1931 for (const workerNode of pool.workerNodes) {
465b2940 1932 expect(workerNode.usage).toStrictEqual({
a4e07f72 1933 tasks: {
619f403b 1934 executed: expect.any(Number),
a4e07f72
JB
1935 executing: 0,
1936 queued: 0,
df593701 1937 maxQueued: 0,
463226a4 1938 sequentiallyStolen: 0,
5ad42e34 1939 stolen: 0,
a4e07f72
JB
1940 failed: 0
1941 },
619f403b
JB
1942 runTime: expect.objectContaining({
1943 history: expect.any(CircularArray)
1944 }),
a4e07f72 1945 waitTime: {
4ba4c7f9 1946 history: new CircularArray()
a4e07f72 1947 },
5df69fab
JB
1948 elu: {
1949 idle: {
4ba4c7f9 1950 history: new CircularArray()
5df69fab
JB
1951 },
1952 active: {
4ba4c7f9 1953 history: new CircularArray()
71514351 1954 }
5df69fab 1955 }
e62e7646 1956 })
619f403b
JB
1957 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
1958 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
1959 max * maxMultiplier
1960 )
e62e7646 1961 }
e62e7646 1962 expect(
bcfb06ce
JB
1963 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1964 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
d33be430 1965 ).roundId
e62e7646 1966 ).toBe(0)
619f403b 1967 expect(
bcfb06ce
JB
1968 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1969 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
619f403b
JB
1970 ).workerNodeId
1971 ).toBe(0)
e62e7646 1972 expect(
bcfb06ce
JB
1973 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1974 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
9b106837 1975 ).nextWorkerNodeKey
e62e7646 1976 ).toBe(0)
086fd843 1977 expect(
bcfb06ce
JB
1978 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1979 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
086fd843 1980 ).previousWorkerNodeKey
11cc661f 1981 ).toEqual(0)
e62e7646 1982 expect(
bcfb06ce
JB
1983 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1984 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
00e1bdeb
JB
1985 ).roundWeights.length
1986 ).toBe(1)
9ef8fa71
JB
1987 expect(
1988 Number.isSafeInteger(
bcfb06ce
JB
1989 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
1990 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
9ef8fa71
JB
1991 ).roundWeights[0]
1992 )
1993 ).toBe(true)
e62e7646
JB
1994 // We need to clean up the resources after our test
1995 await pool.destroy()
1996 })
1997
9ef8fa71 1998 it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
e62e7646
JB
1999 const pool = new DynamicThreadPool(
2000 min,
2001 max,
b2fd3f4a 2002 './tests/worker-files/thread/testWorker.mjs',
e62e7646
JB
2003 {
2004 workerChoiceStrategy:
2005 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
2006 }
2007 )
2008 // TODO: Create a better test to cover `InterleavedWeightedRoundRobinWorkerChoiceStrategy#choose`
2009 const promises = new Set()
2010 const maxMultiplier = 2
2011 for (let i = 0; i < max * maxMultiplier; i++) {
2012 promises.add(pool.execute())
2013 }
2014 await Promise.all(promises)
2015 for (const workerNode of pool.workerNodes) {
465b2940 2016 expect(workerNode.usage).toStrictEqual({
a4e07f72 2017 tasks: {
b1aae695 2018 executed: expect.any(Number),
a4e07f72
JB
2019 executing: 0,
2020 queued: 0,
df593701 2021 maxQueued: 0,
463226a4 2022 sequentiallyStolen: 0,
5ad42e34 2023 stolen: 0,
a4e07f72
JB
2024 failed: 0
2025 },
619f403b
JB
2026 runTime: expect.objectContaining({
2027 history: expect.any(CircularArray)
2028 }),
a4e07f72 2029 waitTime: {
4ba4c7f9 2030 history: new CircularArray()
a4e07f72 2031 },
5df69fab
JB
2032 elu: {
2033 idle: {
4ba4c7f9 2034 history: new CircularArray()
5df69fab
JB
2035 },
2036 active: {
4ba4c7f9 2037 history: new CircularArray()
71514351 2038 }
5df69fab 2039 }
e62e7646 2040 })
e43b8c2d
JB
2041 expect(workerNode.usage.tasks.executed).toBeGreaterThanOrEqual(0)
2042 expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
2043 max * maxMultiplier
2044 )
e62e7646 2045 }
e62e7646 2046 expect(
bcfb06ce
JB
2047 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2048 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
d33be430 2049 ).roundId
e62e7646 2050 ).toBe(0)
619f403b 2051 expect(
bcfb06ce
JB
2052 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2053 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
619f403b
JB
2054 ).workerNodeId
2055 ).toBe(0)
e62e7646 2056 expect(
bcfb06ce
JB
2057 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2058 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
9b106837 2059 ).nextWorkerNodeKey
d2c73f82 2060 ).toBe(0)
086fd843 2061 expect(
bcfb06ce
JB
2062 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2063 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
086fd843 2064 ).previousWorkerNodeKey
11cc661f 2065 ).toEqual(0)
e62e7646 2066 expect(
bcfb06ce
JB
2067 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2068 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
00e1bdeb
JB
2069 ).roundWeights.length
2070 ).toBe(1)
9ef8fa71
JB
2071 expect(
2072 Number.isSafeInteger(
bcfb06ce
JB
2073 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2074 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
9ef8fa71
JB
2075 ).roundWeights[0]
2076 )
2077 ).toBe(true)
e62e7646
JB
2078 // We need to clean up the resources after our test
2079 await pool.destroy()
2080 })
2081
aa78531a 2082 it("Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy internals aren't reset after setting it", async () => {
8c3ec188
JB
2083 const workerChoiceStrategy =
2084 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN
2085 let pool = new FixedThreadPool(
2086 max,
bcfb06ce
JB
2087 './tests/worker-files/thread/testWorker.mjs',
2088 { workerChoiceStrategy }
8c3ec188 2089 )
bcfb06ce
JB
2090 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2091 workerChoiceStrategy
2092 ).roundId = randomInt(1, max - 1)
2093 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2094 workerChoiceStrategy
2095 ).workerNodeId = randomInt(1, max - 1)
2096 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2097 workerChoiceStrategy
2098 ).nextWorkerNodeKey = randomInt(1, max - 1)
2099 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2100 workerChoiceStrategy
2101 ).previousWorkerNodeKey = randomInt(1, max - 1)
2102 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2103 workerChoiceStrategy
2104 ).roundWeights = [randomInt(1, max - 1), randomInt(1, max - 1)]
8c3ec188
JB
2105 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
2106 expect(
bcfb06ce
JB
2107 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2108 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
d33be430 2109 ).roundId
bcfb06ce 2110 ).toBeGreaterThan(0)
086fd843 2111 expect(
bcfb06ce
JB
2112 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2113 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
086fd843 2114 ).workerNodeId
bcfb06ce 2115 ).toBeGreaterThan(0)
8c3ec188 2116 expect(
bcfb06ce
JB
2117 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2118 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
9b106837 2119 ).nextWorkerNodeKey
bcfb06ce 2120 ).toBeGreaterThan(0)
086fd843 2121 expect(
bcfb06ce
JB
2122 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2123 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
086fd843 2124 ).previousWorkerNodeKey
bcfb06ce 2125 ).toBeGreaterThan(0)
8c3ec188 2126 expect(
bcfb06ce
JB
2127 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2128 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
00e1bdeb 2129 ).roundWeights.length
bcfb06ce 2130 ).toBeGreaterThan(1)
9ef8fa71
JB
2131 expect(
2132 Number.isSafeInteger(
bcfb06ce
JB
2133 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2134 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
9ef8fa71
JB
2135 ).roundWeights[0]
2136 )
2137 ).toBe(true)
8c3ec188
JB
2138 await pool.destroy()
2139 pool = new DynamicThreadPool(
2140 min,
2141 max,
bcfb06ce
JB
2142 './tests/worker-files/thread/testWorker.mjs',
2143 { workerChoiceStrategy }
8c3ec188 2144 )
bcfb06ce
JB
2145 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2146 workerChoiceStrategy
2147 ).roundId = randomInt(1, max - 1)
2148 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2149 workerChoiceStrategy
2150 ).workerNodeId = randomInt(1, max - 1)
2151 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2152 workerChoiceStrategy
2153 ).nextWorkerNodeKey = randomInt(1, max - 1)
2154 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2155 workerChoiceStrategy
2156 ).previousWorkerNodeKey = randomInt(1, max - 1)
2157 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2158 workerChoiceStrategy
2159 ).roundWeights = [randomInt(1, max - 1), randomInt(1, max - 1)]
8c3ec188 2160 pool.setWorkerChoiceStrategy(workerChoiceStrategy)
086fd843 2161 expect(
bcfb06ce
JB
2162 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2163 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
086fd843 2164 ).roundId
bcfb06ce 2165 ).toBeGreaterThan(0)
086fd843 2166 expect(
bcfb06ce
JB
2167 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2168 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
086fd843 2169 ).workerNodeId
bcfb06ce 2170 ).toBeGreaterThan(0)
8c3ec188 2171 expect(
bcfb06ce
JB
2172 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2173 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
9b106837 2174 ).nextWorkerNodeKey
bcfb06ce 2175 ).toBeGreaterThan(0)
086fd843 2176 expect(
bcfb06ce
JB
2177 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2178 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
086fd843 2179 ).previousWorkerNodeKey
bcfb06ce 2180 ).toBeGreaterThan(0)
8c3ec188 2181 expect(
bcfb06ce
JB
2182 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2183 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
00e1bdeb 2184 ).roundWeights.length
bcfb06ce 2185 ).toBeGreaterThan(1)
9ef8fa71
JB
2186 expect(
2187 Number.isSafeInteger(
bcfb06ce
JB
2188 pool.workerChoiceStrategiesContext.workerChoiceStrategies.get(
2189 pool.workerChoiceStrategiesContext.defaultWorkerChoiceStrategy
9ef8fa71
JB
2190 ).roundWeights[0]
2191 )
2192 ).toBe(true)
8c3ec188
JB
2193 // We need to clean up the resources after our test
2194 await pool.destroy()
2195 })
2196
89b09b26 2197 it('Verify unknown strategy throw error', () => {
a35560ba
S
2198 expect(
2199 () =>
2200 new DynamicThreadPool(
2201 min,
2202 max,
b2fd3f4a 2203 './tests/worker-files/thread/testWorker.mjs',
1927ee67 2204 { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
a35560ba 2205 )
948faff7 2206 ).toThrow("Invalid worker choice strategy 'UNKNOWN_STRATEGY'")
a35560ba
S
2207 })
2208})