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( | |
5502c07c | 46 | pool.workerChoiceStrategyContext.workerChoiceStrategy.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( |
5502c07c | 155 | pool.workerChoiceStrategyContext.workerChoiceStrategy.nextWorkerId |
38f6e859 | 156 | ).toBeUndefined() |
a6f7f1b4 JB |
157 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) |
158 | expect( | |
5502c07c | 159 | pool.workerChoiceStrategyContext.workerChoiceStrategy.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 | 168 | expect( |
5502c07c JB |
169 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy |
170 | .nextWorkerId | |
38f6e859 | 171 | ).toBeUndefined() |
a6f7f1b4 JB |
172 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) |
173 | expect( | |
5502c07c JB |
174 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy |
175 | .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 | ) | |
5502c07c | 354 | for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) { |
6e7fa401 | 355 | expect( |
5502c07c JB |
356 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( |
357 | workerKey | |
358 | ).start | |
6e7fa401 JB |
359 | ).toBe(0) |
360 | expect( | |
5502c07c JB |
361 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( |
362 | workerKey | |
363 | ).end | |
6e7fa401 JB |
364 | ).toBe(0) |
365 | } | |
23ff945a JB |
366 | // We need to clean up the resources after our test |
367 | await pool.destroy() | |
368 | }) | |
369 | ||
370 | it('Verify FAIR_SHARE strategy can be set after pool creation', async () => { | |
23ff945a JB |
371 | const pool = new FixedThreadPool( |
372 | max, | |
373 | './tests/worker-files/thread/testWorker.js' | |
374 | ) | |
375 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) | |
376 | expect(pool.opts.workerChoiceStrategy).toBe( | |
377 | WorkerChoiceStrategies.FAIR_SHARE | |
378 | ) | |
379 | // We need to clean up the resources after our test | |
380 | await pool.destroy() | |
381 | }) | |
382 | ||
10fcfaf4 | 383 | it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => { |
10fcfaf4 JB |
384 | let pool = new FixedThreadPool( |
385 | max, | |
386 | './tests/worker-files/thread/testWorker.js' | |
387 | ) | |
388 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) | |
389 | expect( | |
97a2abc3 | 390 | pool.workerChoiceStrategyContext.getRequiredStatistics().runTime |
10fcfaf4 | 391 | ).toBe(true) |
fd7ebd49 | 392 | await pool.destroy() |
10fcfaf4 JB |
393 | pool = new DynamicThreadPool( |
394 | min, | |
395 | max, | |
396 | './tests/worker-files/thread/testWorker.js' | |
397 | ) | |
398 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) | |
399 | expect( | |
97a2abc3 | 400 | pool.workerChoiceStrategyContext.getRequiredStatistics().runTime |
10fcfaf4 JB |
401 | ).toBe(true) |
402 | // We need to clean up the resources after our test | |
403 | await pool.destroy() | |
404 | }) | |
405 | ||
23ff945a | 406 | it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => { |
23ff945a JB |
407 | const pool = new FixedThreadPool( |
408 | max, | |
409 | './tests/worker-files/thread/testWorker.js', | |
410 | { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } | |
411 | ) | |
412 | // TODO: Create a better test to cover `FairShareChoiceStrategy#choose` | |
413 | const promises = [] | |
414 | for (let i = 0; i < max * 2; i++) { | |
6db75ad9 | 415 | promises.push(pool.execute()) |
23ff945a JB |
416 | } |
417 | await Promise.all(promises) | |
97a2abc3 | 418 | expect( |
5502c07c | 419 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
97a2abc3 JB |
420 | .workerLastVirtualTaskTimestamp.size |
421 | ).toBe(pool.workers.length) | |
23ff945a JB |
422 | // We need to clean up the resources after our test |
423 | await pool.destroy() | |
424 | }) | |
425 | ||
426 | it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => { | |
23ff945a JB |
427 | const pool = new DynamicThreadPool( |
428 | min, | |
429 | max, | |
430 | './tests/worker-files/thread/testWorker.js', | |
431 | { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } | |
432 | ) | |
433 | // TODO: Create a better test to cover `FairShareChoiceStrategy#choose` | |
434 | const promises = [] | |
9f34a585 | 435 | const maxMultiplier = 10 |
804a889e | 436 | for (let i = 0; i < max * maxMultiplier; i++) { |
6db75ad9 | 437 | promises.push(pool.execute()) |
23ff945a JB |
438 | } |
439 | await Promise.all(promises) | |
9f34a585 JB |
440 | if (process.platform !== 'win32') { |
441 | expect( | |
442 | pool.workerChoiceStrategyContext.workerChoiceStrategy | |
443 | .workerChoiceStrategy.workerLastVirtualTaskTimestamp.size | |
444 | ).toBe(pool.workers.length) | |
445 | } | |
23ff945a JB |
446 | // We need to clean up the resources after our test |
447 | await pool.destroy() | |
448 | }) | |
449 | ||
a6f7f1b4 | 450 | it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => { |
f0829c53 | 451 | let pool = new FixedThreadPool( |
caeb9817 JB |
452 | max, |
453 | './tests/worker-files/thread/testWorker.js' | |
454 | ) | |
455 | expect( | |
5502c07c | 456 | pool.workerChoiceStrategyContext.workerChoiceStrategy |
caeb9817 JB |
457 | .workerLastVirtualTaskTimestamp |
458 | ).toBeUndefined() | |
459 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) | |
5502c07c | 460 | for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) { |
caeb9817 | 461 | expect( |
5502c07c JB |
462 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( |
463 | workerKey | |
464 | ).start | |
caeb9817 JB |
465 | ).toBe(0) |
466 | expect( | |
5502c07c JB |
467 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( |
468 | workerKey | |
469 | ).end | |
caeb9817 JB |
470 | ).toBe(0) |
471 | } | |
f0829c53 JB |
472 | await pool.destroy() |
473 | pool = new DynamicThreadPool( | |
474 | min, | |
475 | max, | |
476 | './tests/worker-files/thread/testWorker.js' | |
477 | ) | |
478 | expect( | |
5502c07c JB |
479 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy |
480 | .workerLastVirtualTaskTimestamp | |
f0829c53 JB |
481 | ).toBeUndefined() |
482 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) | |
5502c07c | 483 | for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) { |
f0829c53 | 484 | expect( |
5502c07c JB |
485 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( |
486 | workerKey | |
487 | ).start | |
f0829c53 JB |
488 | ).toBe(0) |
489 | expect( | |
5502c07c JB |
490 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( |
491 | workerKey | |
492 | ).end | |
f0829c53 JB |
493 | ).toBe(0) |
494 | } | |
caeb9817 JB |
495 | // We need to clean up the resources after our test |
496 | await pool.destroy() | |
497 | }) | |
498 | ||
b3432a63 | 499 | it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => { |
b3432a63 JB |
500 | const pool = new FixedThreadPool( |
501 | max, | |
502 | './tests/worker-files/thread/testWorker.js', | |
503 | { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } | |
504 | ) | |
505 | expect(pool.opts.workerChoiceStrategy).toBe( | |
506 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN | |
507 | ) | |
d2f7b7a2 | 508 | expect( |
5502c07c | 509 | pool.workerChoiceStrategyContext.workerChoiceStrategy.currentWorkerId |
d2f7b7a2 JB |
510 | ).toBe(0) |
511 | expect( | |
5502c07c | 512 | pool.workerChoiceStrategyContext.workerChoiceStrategy.defaultWorkerWeight |
d2f7b7a2 | 513 | ).toBeGreaterThan(0) |
5502c07c | 514 | for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.keys()) { |
d2f7b7a2 | 515 | expect( |
5502c07c JB |
516 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.get( |
517 | workerKey | |
518 | ).weight | |
d2f7b7a2 | 519 | ).toBeGreaterThan(0) |
6e7fa401 | 520 | expect( |
5502c07c JB |
521 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.get( |
522 | workerKey | |
523 | ).runTime | |
6e7fa401 JB |
524 | ).toBe(0) |
525 | } | |
b3432a63 JB |
526 | // We need to clean up the resources after our test |
527 | await pool.destroy() | |
528 | }) | |
529 | ||
530 | it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => { | |
b3432a63 JB |
531 | const pool = new FixedThreadPool( |
532 | max, | |
533 | './tests/worker-files/thread/testWorker.js' | |
534 | ) | |
535 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) | |
536 | expect(pool.opts.workerChoiceStrategy).toBe( | |
537 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN | |
538 | ) | |
539 | // We need to clean up the resources after our test | |
540 | await pool.destroy() | |
541 | }) | |
542 | ||
10fcfaf4 | 543 | it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => { |
10fcfaf4 JB |
544 | let pool = new FixedThreadPool( |
545 | max, | |
546 | './tests/worker-files/thread/testWorker.js' | |
547 | ) | |
548 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) | |
549 | expect( | |
97a2abc3 | 550 | pool.workerChoiceStrategyContext.getRequiredStatistics().runTime |
10fcfaf4 | 551 | ).toBe(true) |
fd7ebd49 | 552 | await pool.destroy() |
10fcfaf4 JB |
553 | pool = new DynamicThreadPool( |
554 | min, | |
555 | max, | |
556 | './tests/worker-files/thread/testWorker.js' | |
557 | ) | |
558 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) | |
559 | expect( | |
97a2abc3 | 560 | pool.workerChoiceStrategyContext.getRequiredStatistics().runTime |
10fcfaf4 JB |
561 | ).toBe(true) |
562 | // We need to clean up the resources after our test | |
563 | await pool.destroy() | |
564 | }) | |
565 | ||
b3432a63 | 566 | it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => { |
b3432a63 JB |
567 | const pool = new FixedThreadPool( |
568 | max, | |
569 | './tests/worker-files/thread/testWorker.js', | |
570 | { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } | |
571 | ) | |
572 | // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` | |
573 | const promises = [] | |
574 | for (let i = 0; i < max * 2; i++) { | |
6db75ad9 | 575 | promises.push(pool.execute()) |
b3432a63 JB |
576 | } |
577 | await Promise.all(promises) | |
97a2abc3 | 578 | expect( |
5502c07c JB |
579 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime |
580 | .size | |
97a2abc3 | 581 | ).toBe(pool.workers.length) |
b3432a63 JB |
582 | // We need to clean up the resources after our test |
583 | await pool.destroy() | |
584 | }) | |
585 | ||
586 | it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => { | |
b3432a63 JB |
587 | const pool = new DynamicThreadPool( |
588 | min, | |
589 | max, | |
590 | './tests/worker-files/thread/testWorker.js', | |
591 | { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } | |
592 | ) | |
593 | // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` | |
594 | const promises = [] | |
5502c07c JB |
595 | const maxMultiplier = |
596 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy | |
a909240d | 597 | .defaultWorkerWeight * 2 |
5502c07c | 598 | for (let i = 0; i < max * maxMultiplier; i++) { |
6db75ad9 | 599 | promises.push(pool.execute()) |
b3432a63 JB |
600 | } |
601 | await Promise.all(promises) | |
9f34a585 JB |
602 | if (process.platform !== 'win32') { |
603 | expect( | |
604 | pool.workerChoiceStrategyContext.workerChoiceStrategy | |
605 | .workerChoiceStrategy.workersTaskRunTime.size | |
606 | ).toBe(pool.workers.length) | |
607 | } | |
b3432a63 JB |
608 | // We need to clean up the resources after our test |
609 | await pool.destroy() | |
610 | }) | |
611 | ||
a6f7f1b4 | 612 | it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => { |
f0829c53 | 613 | let pool = new FixedThreadPool( |
caeb9817 JB |
614 | max, |
615 | './tests/worker-files/thread/testWorker.js' | |
616 | ) | |
38f6e859 | 617 | expect( |
5502c07c | 618 | pool.workerChoiceStrategyContext.workerChoiceStrategy.currentWorkerId |
38f6e859 JB |
619 | ).toBeUndefined() |
620 | expect( | |
5502c07c | 621 | pool.workerChoiceStrategyContext.workerChoiceStrategy.defaultWorkerWeight |
38f6e859 | 622 | ).toBeUndefined() |
caeb9817 | 623 | expect( |
5502c07c | 624 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime |
caeb9817 JB |
625 | ).toBeUndefined() |
626 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) | |
a6f7f1b4 | 627 | expect( |
5502c07c | 628 | pool.workerChoiceStrategyContext.workerChoiceStrategy.currentWorkerId |
a6f7f1b4 JB |
629 | ).toBe(0) |
630 | expect( | |
5502c07c | 631 | pool.workerChoiceStrategyContext.workerChoiceStrategy.defaultWorkerWeight |
a6f7f1b4 | 632 | ).toBeGreaterThan(0) |
5502c07c | 633 | for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.keys()) { |
caeb9817 | 634 | expect( |
5502c07c JB |
635 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.get( |
636 | workerKey | |
637 | ).runTime | |
caeb9817 JB |
638 | ).toBe(0) |
639 | } | |
f0829c53 JB |
640 | await pool.destroy() |
641 | pool = new DynamicThreadPool( | |
642 | min, | |
643 | max, | |
644 | './tests/worker-files/thread/testWorker.js' | |
645 | ) | |
38f6e859 | 646 | expect( |
5502c07c JB |
647 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy |
648 | .currentWorkerId | |
38f6e859 JB |
649 | ).toBeUndefined() |
650 | expect( | |
5502c07c JB |
651 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy |
652 | .defaultWorkerWeight | |
38f6e859 | 653 | ).toBeUndefined() |
f0829c53 | 654 | expect( |
5502c07c JB |
655 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy |
656 | .workersTaskRunTime | |
f0829c53 JB |
657 | ).toBeUndefined() |
658 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) | |
a6f7f1b4 | 659 | expect( |
5502c07c JB |
660 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy |
661 | .currentWorkerId | |
a6f7f1b4 JB |
662 | ).toBe(0) |
663 | expect( | |
5502c07c JB |
664 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy |
665 | .defaultWorkerWeight | |
a6f7f1b4 | 666 | ).toBeGreaterThan(0) |
5502c07c | 667 | for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workersTaskRunTime.keys()) { |
f0829c53 | 668 | expect( |
5502c07c JB |
669 | pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workersTaskRunTime.get( |
670 | workerKey | |
671 | ).runTime | |
f0829c53 JB |
672 | ).toBe(0) |
673 | } | |
caeb9817 JB |
674 | // We need to clean up the resources after our test |
675 | await pool.destroy() | |
676 | }) | |
677 | ||
a35560ba | 678 | it('Verify unknown strategies throw error', () => { |
a35560ba S |
679 | expect( |
680 | () => | |
681 | new DynamicThreadPool( | |
682 | min, | |
683 | max, | |
684 | './tests/worker-files/thread/testWorker.js', | |
1927ee67 | 685 | { workerChoiceStrategy: 'UNKNOWN_STRATEGY' } |
a35560ba S |
686 | ) |
687 | ).toThrowError( | |
688 | new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found") | |
689 | ) | |
690 | }) | |
691 | }) |