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