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