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