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