Commit | Line | Data |
---|---|---|
a61a0724 | 1 | const { expect } = require('expect') |
b1aae695 | 2 | const sinon = require('sinon') |
e843b904 | 3 | const { |
70a4f5ea | 4 | DynamicClusterPool, |
9e619829 | 5 | DynamicThreadPool, |
aee46736 | 6 | FixedClusterPool, |
e843b904 | 7 | FixedThreadPool, |
aee46736 | 8 | PoolEvents, |
184855e6 | 9 | PoolTypes, |
3d6dd312 | 10 | WorkerChoiceStrategies, |
184855e6 | 11 | WorkerTypes |
cdace0e5 | 12 | } = require('../../../lib') |
78099a15 | 13 | const { CircularArray } = require('../../../lib/circular-array') |
574b351d | 14 | const { Deque } = require('../../../lib/deque') |
6cd5248f | 15 | const { DEFAULT_TASK_NAME } = require('../../../lib/utils') |
23ccf9d7 | 16 | const { version } = require('../../../package.json') |
2431bdb4 | 17 | const { waitPoolEvents } = require('../../test-utils') |
e1ffb94f JB |
18 | |
19 | describe('Abstract pool test suite', () => { | |
fc027381 | 20 | const numberOfWorkers = 2 |
a8884ffd | 21 | class StubPoolWithIsMain extends FixedThreadPool { |
e1ffb94f JB |
22 | isMain () { |
23 | return false | |
24 | } | |
3ec964d6 | 25 | } |
3ec964d6 | 26 | |
dc021bcc JB |
27 | afterEach(() => { |
28 | sinon.restore() | |
29 | }) | |
30 | ||
3ec964d6 | 31 | it('Simulate pool creation from a non main thread/process', () => { |
8d3782fa JB |
32 | expect( |
33 | () => | |
a8884ffd | 34 | new StubPoolWithIsMain( |
7c0ba920 | 35 | numberOfWorkers, |
8d3782fa JB |
36 | './tests/worker-files/thread/testWorker.js', |
37 | { | |
8ebe6c30 | 38 | errorHandler: (e) => console.error(e) |
8d3782fa JB |
39 | } |
40 | ) | |
04f45163 | 41 | ).toThrowError( |
e695d66f JB |
42 | new Error( |
43 | 'Cannot start a pool from a worker with the same type as the pool' | |
44 | ) | |
04f45163 | 45 | ) |
3ec964d6 | 46 | }) |
c510fea7 | 47 | |
bc61cfe6 JB |
48 | it('Verify that pool statuses properties are set', async () => { |
49 | const pool = new FixedThreadPool( | |
50 | numberOfWorkers, | |
51 | './tests/worker-files/thread/testWorker.js' | |
52 | ) | |
53 | expect(pool.starting).toBe(false) | |
54 | expect(pool.started).toBe(true) | |
55 | await pool.destroy() | |
56 | expect(pool.started).toBe(false) | |
57 | }) | |
58 | ||
c510fea7 | 59 | it('Verify that filePath is checked', () => { |
292ad316 JB |
60 | const expectedError = new Error( |
61 | 'Please specify a file with a worker implementation' | |
62 | ) | |
7c0ba920 | 63 | expect(() => new FixedThreadPool(numberOfWorkers)).toThrowError( |
292ad316 | 64 | expectedError |
8d3782fa | 65 | ) |
7c0ba920 | 66 | expect(() => new FixedThreadPool(numberOfWorkers, '')).toThrowError( |
292ad316 | 67 | expectedError |
8d3782fa | 68 | ) |
3d6dd312 JB |
69 | expect(() => new FixedThreadPool(numberOfWorkers, 0)).toThrowError( |
70 | expectedError | |
71 | ) | |
72 | expect(() => new FixedThreadPool(numberOfWorkers, true)).toThrowError( | |
73 | expectedError | |
74 | ) | |
75 | expect( | |
76 | () => new FixedThreadPool(numberOfWorkers, './dummyWorker.ts') | |
77 | ).toThrowError(new Error("Cannot find the worker file './dummyWorker.ts'")) | |
8d3782fa JB |
78 | }) |
79 | ||
80 | it('Verify that numberOfWorkers is checked', () => { | |
81 | expect(() => new FixedThreadPool()).toThrowError( | |
e695d66f JB |
82 | new Error( |
83 | 'Cannot instantiate a pool without specifying the number of workers' | |
84 | ) | |
8d3782fa JB |
85 | ) |
86 | }) | |
87 | ||
88 | it('Verify that a negative number of workers is checked', () => { | |
89 | expect( | |
90 | () => | |
91 | new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js') | |
92 | ).toThrowError( | |
473c717a JB |
93 | new RangeError( |
94 | 'Cannot instantiate a pool with a negative number of workers' | |
95 | ) | |
8d3782fa JB |
96 | ) |
97 | }) | |
98 | ||
99 | it('Verify that a non integer number of workers is checked', () => { | |
100 | expect( | |
101 | () => | |
102 | new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js') | |
103 | ).toThrowError( | |
473c717a | 104 | new TypeError( |
0d80593b | 105 | 'Cannot instantiate a pool with a non safe integer number of workers' |
8d3782fa JB |
106 | ) |
107 | ) | |
c510fea7 | 108 | }) |
7c0ba920 | 109 | |
216541b6 | 110 | it('Verify that dynamic pool sizing is checked', () => { |
a5ed75b7 JB |
111 | expect( |
112 | () => | |
113 | new DynamicClusterPool( | |
114 | 1, | |
115 | undefined, | |
116 | './tests/worker-files/cluster/testWorker.js' | |
117 | ) | |
118 | ).toThrowError( | |
119 | new TypeError( | |
120 | 'Cannot instantiate a dynamic pool without specifying the maximum pool size' | |
121 | ) | |
122 | ) | |
2761efb4 JB |
123 | expect( |
124 | () => | |
125 | new DynamicThreadPool( | |
126 | 0.5, | |
127 | 1, | |
128 | './tests/worker-files/thread/testWorker.js' | |
129 | ) | |
130 | ).toThrowError( | |
131 | new TypeError( | |
132 | 'Cannot instantiate a pool with a non safe integer number of workers' | |
133 | ) | |
134 | ) | |
135 | expect( | |
136 | () => | |
137 | new DynamicClusterPool( | |
138 | 0, | |
139 | 0.5, | |
a5ed75b7 | 140 | './tests/worker-files/cluster/testWorker.js' |
2761efb4 JB |
141 | ) |
142 | ).toThrowError( | |
143 | new TypeError( | |
144 | 'Cannot instantiate a dynamic pool with a non safe integer maximum pool size' | |
145 | ) | |
146 | ) | |
2431bdb4 JB |
147 | expect( |
148 | () => | |
149 | new DynamicThreadPool(2, 1, './tests/worker-files/thread/testWorker.js') | |
150 | ).toThrowError( | |
151 | new RangeError( | |
152 | 'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size' | |
153 | ) | |
154 | ) | |
155 | expect( | |
156 | () => | |
213cbac6 | 157 | new DynamicThreadPool(0, 0, './tests/worker-files/thread/testWorker.js') |
2431bdb4 JB |
158 | ).toThrowError( |
159 | new RangeError( | |
213cbac6 | 160 | 'Cannot instantiate a dynamic pool with a maximum pool size equal to zero' |
2431bdb4 JB |
161 | ) |
162 | ) | |
21f710aa JB |
163 | expect( |
164 | () => | |
213cbac6 JB |
165 | new DynamicClusterPool( |
166 | 1, | |
167 | 1, | |
168 | './tests/worker-files/cluster/testWorker.js' | |
169 | ) | |
21f710aa JB |
170 | ).toThrowError( |
171 | new RangeError( | |
213cbac6 | 172 | 'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead' |
21f710aa JB |
173 | ) |
174 | ) | |
2431bdb4 JB |
175 | }) |
176 | ||
fd7ebd49 | 177 | it('Verify that pool options are checked', async () => { |
7c0ba920 JB |
178 | let pool = new FixedThreadPool( |
179 | numberOfWorkers, | |
180 | './tests/worker-files/thread/testWorker.js' | |
181 | ) | |
7c0ba920 | 182 | expect(pool.emitter).toBeDefined() |
1f68cede JB |
183 | expect(pool.opts.enableEvents).toBe(true) |
184 | expect(pool.opts.restartWorkerOnError).toBe(true) | |
ff733df7 | 185 | expect(pool.opts.enableTasksQueue).toBe(false) |
d4aeae5a | 186 | expect(pool.opts.tasksQueueOptions).toBeUndefined() |
e843b904 JB |
187 | expect(pool.opts.workerChoiceStrategy).toBe( |
188 | WorkerChoiceStrategies.ROUND_ROBIN | |
189 | ) | |
da309861 | 190 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ |
8c0b113f | 191 | retries: 6, |
8990357d JB |
192 | runTime: { median: false }, |
193 | waitTime: { median: false }, | |
194 | elu: { median: false } | |
195 | }) | |
196 | expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ | |
8c0b113f | 197 | retries: 6, |
932fc8be | 198 | runTime: { median: false }, |
5df69fab JB |
199 | waitTime: { median: false }, |
200 | elu: { median: false } | |
da309861 | 201 | }) |
35cf1c03 JB |
202 | expect(pool.opts.messageHandler).toBeUndefined() |
203 | expect(pool.opts.errorHandler).toBeUndefined() | |
204 | expect(pool.opts.onlineHandler).toBeUndefined() | |
205 | expect(pool.opts.exitHandler).toBeUndefined() | |
fd7ebd49 | 206 | await pool.destroy() |
73bfd59d | 207 | const testHandler = () => console.info('test handler executed') |
7c0ba920 JB |
208 | pool = new FixedThreadPool( |
209 | numberOfWorkers, | |
210 | './tests/worker-files/thread/testWorker.js', | |
211 | { | |
e4543b14 | 212 | workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED, |
49be33fe | 213 | workerChoiceStrategyOptions: { |
932fc8be | 214 | runTime: { median: true }, |
fc027381 | 215 | weights: { 0: 300, 1: 200 } |
49be33fe | 216 | }, |
35cf1c03 | 217 | enableEvents: false, |
1f68cede | 218 | restartWorkerOnError: false, |
ff733df7 | 219 | enableTasksQueue: true, |
d4aeae5a | 220 | tasksQueueOptions: { concurrency: 2 }, |
35cf1c03 JB |
221 | messageHandler: testHandler, |
222 | errorHandler: testHandler, | |
223 | onlineHandler: testHandler, | |
224 | exitHandler: testHandler | |
7c0ba920 JB |
225 | } |
226 | ) | |
7c0ba920 | 227 | expect(pool.emitter).toBeUndefined() |
1f68cede JB |
228 | expect(pool.opts.enableEvents).toBe(false) |
229 | expect(pool.opts.restartWorkerOnError).toBe(false) | |
ff733df7 | 230 | expect(pool.opts.enableTasksQueue).toBe(true) |
20c6f652 JB |
231 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ |
232 | concurrency: 2, | |
ff3f866a | 233 | size: 4 |
20c6f652 | 234 | }) |
e843b904 | 235 | expect(pool.opts.workerChoiceStrategy).toBe( |
e4543b14 | 236 | WorkerChoiceStrategies.LEAST_USED |
e843b904 | 237 | ) |
da309861 | 238 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ |
8c0b113f | 239 | retries: 6, |
932fc8be | 240 | runTime: { median: true }, |
8990357d JB |
241 | waitTime: { median: false }, |
242 | elu: { median: false }, | |
243 | weights: { 0: 300, 1: 200 } | |
244 | }) | |
245 | expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ | |
8c0b113f | 246 | retries: 6, |
8990357d JB |
247 | runTime: { median: true }, |
248 | waitTime: { median: false }, | |
249 | elu: { median: false }, | |
fc027381 | 250 | weights: { 0: 300, 1: 200 } |
da309861 | 251 | }) |
35cf1c03 JB |
252 | expect(pool.opts.messageHandler).toStrictEqual(testHandler) |
253 | expect(pool.opts.errorHandler).toStrictEqual(testHandler) | |
254 | expect(pool.opts.onlineHandler).toStrictEqual(testHandler) | |
255 | expect(pool.opts.exitHandler).toStrictEqual(testHandler) | |
fd7ebd49 | 256 | await pool.destroy() |
7c0ba920 JB |
257 | }) |
258 | ||
a20f0ba5 | 259 | it('Verify that pool options are validated', async () => { |
d4aeae5a JB |
260 | expect( |
261 | () => | |
262 | new FixedThreadPool( | |
263 | numberOfWorkers, | |
264 | './tests/worker-files/thread/testWorker.js', | |
265 | { | |
f0d7f803 | 266 | workerChoiceStrategy: 'invalidStrategy' |
d4aeae5a JB |
267 | } |
268 | ) | |
8735b4e5 JB |
269 | ).toThrowError( |
270 | new Error("Invalid worker choice strategy 'invalidStrategy'") | |
271 | ) | |
7e653ee0 JB |
272 | expect( |
273 | () => | |
274 | new FixedThreadPool( | |
275 | numberOfWorkers, | |
276 | './tests/worker-files/thread/testWorker.js', | |
277 | { | |
278 | workerChoiceStrategyOptions: { | |
8c0b113f | 279 | retries: 'invalidChoiceRetries' |
7e653ee0 JB |
280 | } |
281 | } | |
282 | ) | |
283 | ).toThrowError( | |
284 | new TypeError( | |
8c0b113f | 285 | 'Invalid worker choice strategy options: retries must be an integer' |
7e653ee0 JB |
286 | ) |
287 | ) | |
288 | expect( | |
289 | () => | |
290 | new FixedThreadPool( | |
291 | numberOfWorkers, | |
292 | './tests/worker-files/thread/testWorker.js', | |
293 | { | |
294 | workerChoiceStrategyOptions: { | |
8c0b113f | 295 | retries: -1 |
7e653ee0 JB |
296 | } |
297 | } | |
298 | ) | |
299 | ).toThrowError( | |
300 | new RangeError( | |
8c0b113f | 301 | "Invalid worker choice strategy options: retries '-1' must be greater or equal than zero" |
7e653ee0 JB |
302 | ) |
303 | ) | |
49be33fe JB |
304 | expect( |
305 | () => | |
306 | new FixedThreadPool( | |
307 | numberOfWorkers, | |
308 | './tests/worker-files/thread/testWorker.js', | |
309 | { | |
310 | workerChoiceStrategyOptions: { weights: {} } | |
311 | } | |
312 | ) | |
313 | ).toThrowError( | |
8735b4e5 JB |
314 | new Error( |
315 | 'Invalid worker choice strategy options: must have a weight for each worker node' | |
316 | ) | |
49be33fe | 317 | ) |
f0d7f803 JB |
318 | expect( |
319 | () => | |
320 | new FixedThreadPool( | |
321 | numberOfWorkers, | |
322 | './tests/worker-files/thread/testWorker.js', | |
323 | { | |
324 | workerChoiceStrategyOptions: { measurement: 'invalidMeasurement' } | |
325 | } | |
326 | ) | |
327 | ).toThrowError( | |
8735b4e5 JB |
328 | new Error( |
329 | "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'" | |
330 | ) | |
f0d7f803 | 331 | ) |
5c4c2dee JB |
332 | expect( |
333 | () => | |
334 | new FixedThreadPool( | |
335 | numberOfWorkers, | |
336 | './tests/worker-files/thread/testWorker.js', | |
337 | { | |
338 | enableTasksQueue: true, | |
339 | tasksQueueOptions: 'invalidTasksQueueOptions' | |
340 | } | |
341 | ) | |
342 | ).toThrowError( | |
343 | new TypeError('Invalid tasks queue options: must be a plain object') | |
344 | ) | |
f0d7f803 JB |
345 | expect( |
346 | () => | |
347 | new FixedThreadPool( | |
348 | numberOfWorkers, | |
349 | './tests/worker-files/thread/testWorker.js', | |
350 | { | |
351 | enableTasksQueue: true, | |
352 | tasksQueueOptions: { concurrency: 0 } | |
353 | } | |
354 | ) | |
8735b4e5 | 355 | ).toThrowError( |
e695d66f | 356 | new RangeError( |
20c6f652 | 357 | 'Invalid worker node tasks concurrency: 0 is a negative integer or zero' |
8735b4e5 JB |
358 | ) |
359 | ) | |
f0d7f803 JB |
360 | expect( |
361 | () => | |
362 | new FixedThreadPool( | |
363 | numberOfWorkers, | |
364 | './tests/worker-files/thread/testWorker.js', | |
365 | { | |
366 | enableTasksQueue: true, | |
5c4c2dee | 367 | tasksQueueOptions: { concurrency: -1 } |
f0d7f803 JB |
368 | } |
369 | ) | |
8735b4e5 | 370 | ).toThrowError( |
5c4c2dee JB |
371 | new RangeError( |
372 | 'Invalid worker node tasks concurrency: -1 is a negative integer or zero' | |
373 | ) | |
8735b4e5 | 374 | ) |
f0d7f803 JB |
375 | expect( |
376 | () => | |
377 | new FixedThreadPool( | |
378 | numberOfWorkers, | |
379 | './tests/worker-files/thread/testWorker.js', | |
380 | { | |
381 | enableTasksQueue: true, | |
382 | tasksQueueOptions: { concurrency: 0.2 } | |
383 | } | |
384 | ) | |
8735b4e5 | 385 | ).toThrowError( |
20c6f652 | 386 | new TypeError('Invalid worker node tasks concurrency: must be an integer') |
8735b4e5 | 387 | ) |
5c4c2dee JB |
388 | expect( |
389 | () => | |
390 | new FixedThreadPool( | |
391 | numberOfWorkers, | |
392 | './tests/worker-files/thread/testWorker.js', | |
393 | { | |
394 | enableTasksQueue: true, | |
395 | tasksQueueOptions: { queueMaxSize: 2 } | |
396 | } | |
397 | ) | |
398 | ).toThrowError( | |
399 | new Error( | |
400 | 'Invalid tasks queue options: queueMaxSize is deprecated, please use size instead' | |
401 | ) | |
402 | ) | |
403 | expect( | |
404 | () => | |
405 | new FixedThreadPool( | |
406 | numberOfWorkers, | |
407 | './tests/worker-files/thread/testWorker.js', | |
408 | { | |
409 | enableTasksQueue: true, | |
410 | tasksQueueOptions: { size: 0 } | |
411 | } | |
412 | ) | |
413 | ).toThrowError( | |
414 | new RangeError( | |
415 | 'Invalid worker node tasks queue size: 0 is a negative integer or zero' | |
416 | ) | |
417 | ) | |
418 | expect( | |
419 | () => | |
420 | new FixedThreadPool( | |
421 | numberOfWorkers, | |
422 | './tests/worker-files/thread/testWorker.js', | |
423 | { | |
424 | enableTasksQueue: true, | |
425 | tasksQueueOptions: { size: -1 } | |
426 | } | |
427 | ) | |
428 | ).toThrowError( | |
429 | new RangeError( | |
430 | 'Invalid worker node tasks queue size: -1 is a negative integer or zero' | |
431 | ) | |
432 | ) | |
433 | expect( | |
434 | () => | |
435 | new FixedThreadPool( | |
436 | numberOfWorkers, | |
437 | './tests/worker-files/thread/testWorker.js', | |
438 | { | |
439 | enableTasksQueue: true, | |
440 | tasksQueueOptions: { size: 0.2 } | |
441 | } | |
442 | ) | |
443 | ).toThrowError( | |
444 | new TypeError('Invalid worker node tasks queue size: must be an integer') | |
445 | ) | |
d4aeae5a JB |
446 | }) |
447 | ||
2431bdb4 | 448 | it('Verify that pool worker choice strategy options can be set', async () => { |
a20f0ba5 JB |
449 | const pool = new FixedThreadPool( |
450 | numberOfWorkers, | |
451 | './tests/worker-files/thread/testWorker.js', | |
452 | { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } | |
453 | ) | |
454 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ | |
8c0b113f | 455 | retries: 6, |
8990357d JB |
456 | runTime: { median: false }, |
457 | waitTime: { median: false }, | |
458 | elu: { median: false } | |
459 | }) | |
460 | expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ | |
8c0b113f | 461 | retries: 6, |
932fc8be | 462 | runTime: { median: false }, |
5df69fab JB |
463 | waitTime: { median: false }, |
464 | elu: { median: false } | |
a20f0ba5 JB |
465 | }) |
466 | for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext | |
467 | .workerChoiceStrategies) { | |
86bf340d | 468 | expect(workerChoiceStrategy.opts).toStrictEqual({ |
8c0b113f | 469 | retries: 6, |
932fc8be | 470 | runTime: { median: false }, |
5df69fab JB |
471 | waitTime: { median: false }, |
472 | elu: { median: false } | |
86bf340d | 473 | }) |
a20f0ba5 | 474 | } |
87de9ff5 JB |
475 | expect( |
476 | pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() | |
477 | ).toStrictEqual({ | |
932fc8be JB |
478 | runTime: { |
479 | aggregate: true, | |
480 | average: true, | |
481 | median: false | |
482 | }, | |
483 | waitTime: { | |
484 | aggregate: false, | |
485 | average: false, | |
486 | median: false | |
487 | }, | |
5df69fab | 488 | elu: { |
9adcefab JB |
489 | aggregate: true, |
490 | average: true, | |
5df69fab JB |
491 | median: false |
492 | } | |
86bf340d | 493 | }) |
9adcefab JB |
494 | pool.setWorkerChoiceStrategyOptions({ |
495 | runTime: { median: true }, | |
496 | elu: { median: true } | |
497 | }) | |
a20f0ba5 | 498 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ |
8c0b113f | 499 | retries: 6, |
9adcefab | 500 | runTime: { median: true }, |
8990357d JB |
501 | waitTime: { median: false }, |
502 | elu: { median: true } | |
503 | }) | |
504 | expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ | |
8c0b113f | 505 | retries: 6, |
8990357d JB |
506 | runTime: { median: true }, |
507 | waitTime: { median: false }, | |
9adcefab | 508 | elu: { median: true } |
a20f0ba5 JB |
509 | }) |
510 | for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext | |
511 | .workerChoiceStrategies) { | |
932fc8be | 512 | expect(workerChoiceStrategy.opts).toStrictEqual({ |
8c0b113f | 513 | retries: 6, |
9adcefab | 514 | runTime: { median: true }, |
8990357d | 515 | waitTime: { median: false }, |
9adcefab | 516 | elu: { median: true } |
932fc8be | 517 | }) |
a20f0ba5 | 518 | } |
87de9ff5 JB |
519 | expect( |
520 | pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() | |
521 | ).toStrictEqual({ | |
932fc8be JB |
522 | runTime: { |
523 | aggregate: true, | |
524 | average: false, | |
525 | median: true | |
526 | }, | |
527 | waitTime: { | |
528 | aggregate: false, | |
529 | average: false, | |
530 | median: false | |
531 | }, | |
5df69fab | 532 | elu: { |
9adcefab | 533 | aggregate: true, |
5df69fab | 534 | average: false, |
9adcefab | 535 | median: true |
5df69fab | 536 | } |
86bf340d | 537 | }) |
9adcefab JB |
538 | pool.setWorkerChoiceStrategyOptions({ |
539 | runTime: { median: false }, | |
540 | elu: { median: false } | |
541 | }) | |
a20f0ba5 | 542 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ |
8c0b113f | 543 | retries: 6, |
8990357d JB |
544 | runTime: { median: false }, |
545 | waitTime: { median: false }, | |
546 | elu: { median: false } | |
547 | }) | |
548 | expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ | |
8c0b113f | 549 | retries: 6, |
9adcefab | 550 | runTime: { median: false }, |
8990357d | 551 | waitTime: { median: false }, |
9adcefab | 552 | elu: { median: false } |
a20f0ba5 JB |
553 | }) |
554 | for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext | |
555 | .workerChoiceStrategies) { | |
932fc8be | 556 | expect(workerChoiceStrategy.opts).toStrictEqual({ |
8c0b113f | 557 | retries: 6, |
9adcefab | 558 | runTime: { median: false }, |
8990357d | 559 | waitTime: { median: false }, |
9adcefab | 560 | elu: { median: false } |
932fc8be | 561 | }) |
a20f0ba5 | 562 | } |
87de9ff5 JB |
563 | expect( |
564 | pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() | |
565 | ).toStrictEqual({ | |
932fc8be JB |
566 | runTime: { |
567 | aggregate: true, | |
568 | average: true, | |
569 | median: false | |
570 | }, | |
571 | waitTime: { | |
572 | aggregate: false, | |
573 | average: false, | |
574 | median: false | |
575 | }, | |
5df69fab | 576 | elu: { |
9adcefab JB |
577 | aggregate: true, |
578 | average: true, | |
5df69fab JB |
579 | median: false |
580 | } | |
86bf340d | 581 | }) |
1f95d544 JB |
582 | expect(() => |
583 | pool.setWorkerChoiceStrategyOptions('invalidWorkerChoiceStrategyOptions') | |
584 | ).toThrowError( | |
8735b4e5 JB |
585 | new TypeError( |
586 | 'Invalid worker choice strategy options: must be a plain object' | |
587 | ) | |
1f95d544 | 588 | ) |
7e653ee0 JB |
589 | expect(() => |
590 | pool.setWorkerChoiceStrategyOptions({ | |
8c0b113f | 591 | retries: 'invalidChoiceRetries' |
7e653ee0 JB |
592 | }) |
593 | ).toThrowError( | |
594 | new TypeError( | |
8c0b113f | 595 | 'Invalid worker choice strategy options: retries must be an integer' |
7e653ee0 JB |
596 | ) |
597 | ) | |
598 | expect(() => | |
8c0b113f | 599 | pool.setWorkerChoiceStrategyOptions({ retries: -1 }) |
7e653ee0 JB |
600 | ).toThrowError( |
601 | new RangeError( | |
8c0b113f | 602 | "Invalid worker choice strategy options: retries '-1' must be greater or equal than zero" |
7e653ee0 JB |
603 | ) |
604 | ) | |
1f95d544 JB |
605 | expect(() => |
606 | pool.setWorkerChoiceStrategyOptions({ weights: {} }) | |
607 | ).toThrowError( | |
8735b4e5 JB |
608 | new Error( |
609 | 'Invalid worker choice strategy options: must have a weight for each worker node' | |
610 | ) | |
1f95d544 JB |
611 | ) |
612 | expect(() => | |
613 | pool.setWorkerChoiceStrategyOptions({ measurement: 'invalidMeasurement' }) | |
614 | ).toThrowError( | |
8735b4e5 JB |
615 | new Error( |
616 | "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'" | |
617 | ) | |
1f95d544 | 618 | ) |
a20f0ba5 JB |
619 | await pool.destroy() |
620 | }) | |
621 | ||
2431bdb4 | 622 | it('Verify that pool tasks queue can be enabled/disabled', async () => { |
a20f0ba5 JB |
623 | const pool = new FixedThreadPool( |
624 | numberOfWorkers, | |
625 | './tests/worker-files/thread/testWorker.js' | |
626 | ) | |
627 | expect(pool.opts.enableTasksQueue).toBe(false) | |
628 | expect(pool.opts.tasksQueueOptions).toBeUndefined() | |
629 | pool.enableTasksQueue(true) | |
630 | expect(pool.opts.enableTasksQueue).toBe(true) | |
20c6f652 JB |
631 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ |
632 | concurrency: 1, | |
ff3f866a | 633 | size: 4 |
20c6f652 | 634 | }) |
a20f0ba5 JB |
635 | pool.enableTasksQueue(true, { concurrency: 2 }) |
636 | expect(pool.opts.enableTasksQueue).toBe(true) | |
20c6f652 JB |
637 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ |
638 | concurrency: 2, | |
ff3f866a | 639 | size: 4 |
20c6f652 | 640 | }) |
a20f0ba5 JB |
641 | pool.enableTasksQueue(false) |
642 | expect(pool.opts.enableTasksQueue).toBe(false) | |
643 | expect(pool.opts.tasksQueueOptions).toBeUndefined() | |
644 | await pool.destroy() | |
645 | }) | |
646 | ||
2431bdb4 | 647 | it('Verify that pool tasks queue options can be set', async () => { |
a20f0ba5 JB |
648 | const pool = new FixedThreadPool( |
649 | numberOfWorkers, | |
650 | './tests/worker-files/thread/testWorker.js', | |
651 | { enableTasksQueue: true } | |
652 | ) | |
20c6f652 JB |
653 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ |
654 | concurrency: 1, | |
ff3f866a | 655 | size: 4 |
20c6f652 | 656 | }) |
a20f0ba5 | 657 | pool.setTasksQueueOptions({ concurrency: 2 }) |
20c6f652 JB |
658 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ |
659 | concurrency: 2, | |
ff3f866a | 660 | size: 4 |
20c6f652 | 661 | }) |
f0d7f803 JB |
662 | expect(() => |
663 | pool.setTasksQueueOptions('invalidTasksQueueOptions') | |
8735b4e5 JB |
664 | ).toThrowError( |
665 | new TypeError('Invalid tasks queue options: must be a plain object') | |
666 | ) | |
a20f0ba5 | 667 | expect(() => pool.setTasksQueueOptions({ concurrency: 0 })).toThrowError( |
e695d66f | 668 | new RangeError( |
20c6f652 | 669 | 'Invalid worker node tasks concurrency: 0 is a negative integer or zero' |
8735b4e5 JB |
670 | ) |
671 | ) | |
672 | expect(() => pool.setTasksQueueOptions({ concurrency: -1 })).toThrowError( | |
e695d66f | 673 | new RangeError( |
20c6f652 | 674 | 'Invalid worker node tasks concurrency: -1 is a negative integer or zero' |
8735b4e5 | 675 | ) |
a20f0ba5 | 676 | ) |
f0d7f803 | 677 | expect(() => pool.setTasksQueueOptions({ concurrency: 0.2 })).toThrowError( |
20c6f652 JB |
678 | new TypeError('Invalid worker node tasks concurrency: must be an integer') |
679 | ) | |
68dbcdc0 JB |
680 | expect(() => pool.setTasksQueueOptions({ queueMaxSize: 2 })).toThrowError( |
681 | new Error( | |
682 | 'Invalid tasks queue options: queueMaxSize is deprecated, please use size instead' | |
683 | ) | |
684 | ) | |
ff3f866a | 685 | expect(() => pool.setTasksQueueOptions({ size: 0 })).toThrowError( |
20c6f652 | 686 | new RangeError( |
68dbcdc0 | 687 | 'Invalid worker node tasks queue size: 0 is a negative integer or zero' |
20c6f652 JB |
688 | ) |
689 | ) | |
ff3f866a | 690 | expect(() => pool.setTasksQueueOptions({ size: -1 })).toThrowError( |
20c6f652 | 691 | new RangeError( |
68dbcdc0 | 692 | 'Invalid worker node tasks queue size: -1 is a negative integer or zero' |
20c6f652 JB |
693 | ) |
694 | ) | |
ff3f866a | 695 | expect(() => pool.setTasksQueueOptions({ size: 0.2 })).toThrowError( |
68dbcdc0 | 696 | new TypeError('Invalid worker node tasks queue size: must be an integer') |
f0d7f803 | 697 | ) |
a20f0ba5 JB |
698 | await pool.destroy() |
699 | }) | |
700 | ||
6b27d407 JB |
701 | it('Verify that pool info is set', async () => { |
702 | let pool = new FixedThreadPool( | |
703 | numberOfWorkers, | |
704 | './tests/worker-files/thread/testWorker.js' | |
705 | ) | |
2dca6cab JB |
706 | expect(pool.info).toStrictEqual({ |
707 | version, | |
708 | type: PoolTypes.fixed, | |
709 | worker: WorkerTypes.thread, | |
710 | ready: true, | |
711 | strategy: WorkerChoiceStrategies.ROUND_ROBIN, | |
712 | minSize: numberOfWorkers, | |
713 | maxSize: numberOfWorkers, | |
714 | workerNodes: numberOfWorkers, | |
715 | idleWorkerNodes: numberOfWorkers, | |
716 | busyWorkerNodes: 0, | |
717 | executedTasks: 0, | |
718 | executingTasks: 0, | |
2dca6cab JB |
719 | failedTasks: 0 |
720 | }) | |
6b27d407 JB |
721 | await pool.destroy() |
722 | pool = new DynamicClusterPool( | |
2431bdb4 | 723 | Math.floor(numberOfWorkers / 2), |
6b27d407 | 724 | numberOfWorkers, |
ecdfbdc0 | 725 | './tests/worker-files/cluster/testWorker.js' |
6b27d407 | 726 | ) |
2dca6cab JB |
727 | expect(pool.info).toStrictEqual({ |
728 | version, | |
729 | type: PoolTypes.dynamic, | |
730 | worker: WorkerTypes.cluster, | |
731 | ready: true, | |
732 | strategy: WorkerChoiceStrategies.ROUND_ROBIN, | |
733 | minSize: Math.floor(numberOfWorkers / 2), | |
734 | maxSize: numberOfWorkers, | |
735 | workerNodes: Math.floor(numberOfWorkers / 2), | |
736 | idleWorkerNodes: Math.floor(numberOfWorkers / 2), | |
737 | busyWorkerNodes: 0, | |
738 | executedTasks: 0, | |
739 | executingTasks: 0, | |
2dca6cab JB |
740 | failedTasks: 0 |
741 | }) | |
6b27d407 JB |
742 | await pool.destroy() |
743 | }) | |
744 | ||
2431bdb4 | 745 | it('Verify that pool worker tasks usage are initialized', async () => { |
bf9549ae JB |
746 | const pool = new FixedClusterPool( |
747 | numberOfWorkers, | |
748 | './tests/worker-files/cluster/testWorker.js' | |
749 | ) | |
f06e48d8 | 750 | for (const workerNode of pool.workerNodes) { |
465b2940 | 751 | expect(workerNode.usage).toStrictEqual({ |
a4e07f72 JB |
752 | tasks: { |
753 | executed: 0, | |
754 | executing: 0, | |
755 | queued: 0, | |
df593701 | 756 | maxQueued: 0, |
68cbdc84 | 757 | stolen: 0, |
a4e07f72 JB |
758 | failed: 0 |
759 | }, | |
760 | runTime: { | |
a4e07f72 JB |
761 | history: expect.any(CircularArray) |
762 | }, | |
763 | waitTime: { | |
a4e07f72 JB |
764 | history: expect.any(CircularArray) |
765 | }, | |
5df69fab JB |
766 | elu: { |
767 | idle: { | |
5df69fab JB |
768 | history: expect.any(CircularArray) |
769 | }, | |
770 | active: { | |
5df69fab | 771 | history: expect.any(CircularArray) |
f7510105 | 772 | } |
5df69fab | 773 | } |
86bf340d | 774 | }) |
f06e48d8 JB |
775 | } |
776 | await pool.destroy() | |
777 | }) | |
778 | ||
2431bdb4 JB |
779 | it('Verify that pool worker tasks queue are initialized', async () => { |
780 | let pool = new FixedClusterPool( | |
f06e48d8 JB |
781 | numberOfWorkers, |
782 | './tests/worker-files/cluster/testWorker.js' | |
783 | ) | |
784 | for (const workerNode of pool.workerNodes) { | |
785 | expect(workerNode.tasksQueue).toBeDefined() | |
574b351d | 786 | expect(workerNode.tasksQueue).toBeInstanceOf(Deque) |
4d8bf9e4 | 787 | expect(workerNode.tasksQueue.size).toBe(0) |
9c16fb4b | 788 | expect(workerNode.tasksQueue.maxSize).toBe(0) |
bf9549ae | 789 | } |
fd7ebd49 | 790 | await pool.destroy() |
2431bdb4 JB |
791 | pool = new DynamicThreadPool( |
792 | Math.floor(numberOfWorkers / 2), | |
793 | numberOfWorkers, | |
794 | './tests/worker-files/thread/testWorker.js' | |
795 | ) | |
796 | for (const workerNode of pool.workerNodes) { | |
797 | expect(workerNode.tasksQueue).toBeDefined() | |
574b351d | 798 | expect(workerNode.tasksQueue).toBeInstanceOf(Deque) |
2431bdb4 JB |
799 | expect(workerNode.tasksQueue.size).toBe(0) |
800 | expect(workerNode.tasksQueue.maxSize).toBe(0) | |
801 | } | |
213cbac6 | 802 | await pool.destroy() |
2431bdb4 JB |
803 | }) |
804 | ||
805 | it('Verify that pool worker info are initialized', async () => { | |
806 | let pool = new FixedClusterPool( | |
807 | numberOfWorkers, | |
808 | './tests/worker-files/cluster/testWorker.js' | |
809 | ) | |
2dca6cab JB |
810 | for (const workerNode of pool.workerNodes) { |
811 | expect(workerNode.info).toStrictEqual({ | |
812 | id: expect.any(Number), | |
813 | type: WorkerTypes.cluster, | |
814 | dynamic: false, | |
815 | ready: true | |
816 | }) | |
817 | } | |
2431bdb4 JB |
818 | await pool.destroy() |
819 | pool = new DynamicThreadPool( | |
820 | Math.floor(numberOfWorkers / 2), | |
821 | numberOfWorkers, | |
822 | './tests/worker-files/thread/testWorker.js' | |
823 | ) | |
2dca6cab JB |
824 | for (const workerNode of pool.workerNodes) { |
825 | expect(workerNode.info).toStrictEqual({ | |
826 | id: expect.any(Number), | |
827 | type: WorkerTypes.thread, | |
828 | dynamic: false, | |
7884d183 | 829 | ready: true |
2dca6cab JB |
830 | }) |
831 | } | |
213cbac6 | 832 | await pool.destroy() |
bf9549ae JB |
833 | }) |
834 | ||
9d2d0da1 JB |
835 | it('Verify that pool execute() arguments are checked', async () => { |
836 | const pool = new FixedClusterPool( | |
837 | numberOfWorkers, | |
838 | './tests/worker-files/cluster/testWorker.js' | |
839 | ) | |
840 | await expect(pool.execute(undefined, 0)).rejects.toThrowError( | |
841 | new TypeError('name argument must be a string') | |
842 | ) | |
843 | await expect(pool.execute(undefined, '')).rejects.toThrowError( | |
844 | new TypeError('name argument must not be an empty string') | |
845 | ) | |
846 | await expect(pool.execute(undefined, undefined, {})).rejects.toThrowError( | |
847 | new TypeError('transferList argument must be an array') | |
848 | ) | |
849 | await expect(pool.execute(undefined, 'unknown')).rejects.toBe( | |
850 | "Task function 'unknown' not found" | |
851 | ) | |
852 | await pool.destroy() | |
853 | await expect(pool.execute(undefined, undefined, {})).rejects.toThrowError( | |
854 | new Error('Cannot execute a task on destroyed pool') | |
855 | ) | |
856 | }) | |
857 | ||
2431bdb4 | 858 | it('Verify that pool worker tasks usage are computed', async () => { |
bf9549ae JB |
859 | const pool = new FixedClusterPool( |
860 | numberOfWorkers, | |
861 | './tests/worker-files/cluster/testWorker.js' | |
862 | ) | |
09c2d0d3 | 863 | const promises = new Set() |
fc027381 JB |
864 | const maxMultiplier = 2 |
865 | for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) { | |
09c2d0d3 | 866 | promises.add(pool.execute()) |
bf9549ae | 867 | } |
f06e48d8 | 868 | for (const workerNode of pool.workerNodes) { |
465b2940 | 869 | expect(workerNode.usage).toStrictEqual({ |
a4e07f72 JB |
870 | tasks: { |
871 | executed: 0, | |
872 | executing: maxMultiplier, | |
873 | queued: 0, | |
df593701 | 874 | maxQueued: 0, |
68cbdc84 | 875 | stolen: 0, |
a4e07f72 JB |
876 | failed: 0 |
877 | }, | |
878 | runTime: { | |
a4e07f72 JB |
879 | history: expect.any(CircularArray) |
880 | }, | |
881 | waitTime: { | |
a4e07f72 JB |
882 | history: expect.any(CircularArray) |
883 | }, | |
5df69fab JB |
884 | elu: { |
885 | idle: { | |
5df69fab JB |
886 | history: expect.any(CircularArray) |
887 | }, | |
888 | active: { | |
5df69fab | 889 | history: expect.any(CircularArray) |
f7510105 | 890 | } |
5df69fab | 891 | } |
86bf340d | 892 | }) |
bf9549ae JB |
893 | } |
894 | await Promise.all(promises) | |
f06e48d8 | 895 | for (const workerNode of pool.workerNodes) { |
465b2940 | 896 | expect(workerNode.usage).toStrictEqual({ |
a4e07f72 JB |
897 | tasks: { |
898 | executed: maxMultiplier, | |
899 | executing: 0, | |
900 | queued: 0, | |
df593701 | 901 | maxQueued: 0, |
68cbdc84 | 902 | stolen: 0, |
a4e07f72 JB |
903 | failed: 0 |
904 | }, | |
905 | runTime: { | |
a4e07f72 JB |
906 | history: expect.any(CircularArray) |
907 | }, | |
908 | waitTime: { | |
a4e07f72 JB |
909 | history: expect.any(CircularArray) |
910 | }, | |
5df69fab JB |
911 | elu: { |
912 | idle: { | |
5df69fab JB |
913 | history: expect.any(CircularArray) |
914 | }, | |
915 | active: { | |
5df69fab | 916 | history: expect.any(CircularArray) |
f7510105 | 917 | } |
5df69fab | 918 | } |
86bf340d | 919 | }) |
bf9549ae | 920 | } |
fd7ebd49 | 921 | await pool.destroy() |
bf9549ae JB |
922 | }) |
923 | ||
2431bdb4 | 924 | it('Verify that pool worker tasks usage are reset at worker choice strategy change', async () => { |
7fd82a1c | 925 | const pool = new DynamicThreadPool( |
2431bdb4 | 926 | Math.floor(numberOfWorkers / 2), |
8f4878b7 | 927 | numberOfWorkers, |
9e619829 JB |
928 | './tests/worker-files/thread/testWorker.js' |
929 | ) | |
09c2d0d3 | 930 | const promises = new Set() |
ee9f5295 JB |
931 | const maxMultiplier = 2 |
932 | for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) { | |
09c2d0d3 | 933 | promises.add(pool.execute()) |
9e619829 JB |
934 | } |
935 | await Promise.all(promises) | |
f06e48d8 | 936 | for (const workerNode of pool.workerNodes) { |
465b2940 | 937 | expect(workerNode.usage).toStrictEqual({ |
a4e07f72 JB |
938 | tasks: { |
939 | executed: expect.any(Number), | |
940 | executing: 0, | |
941 | queued: 0, | |
df593701 | 942 | maxQueued: 0, |
68cbdc84 | 943 | stolen: 0, |
a4e07f72 JB |
944 | failed: 0 |
945 | }, | |
946 | runTime: { | |
a4e07f72 JB |
947 | history: expect.any(CircularArray) |
948 | }, | |
949 | waitTime: { | |
a4e07f72 JB |
950 | history: expect.any(CircularArray) |
951 | }, | |
5df69fab JB |
952 | elu: { |
953 | idle: { | |
5df69fab JB |
954 | history: expect.any(CircularArray) |
955 | }, | |
956 | active: { | |
5df69fab | 957 | history: expect.any(CircularArray) |
f7510105 | 958 | } |
5df69fab | 959 | } |
86bf340d | 960 | }) |
465b2940 | 961 | expect(workerNode.usage.tasks.executed).toBeGreaterThan(0) |
94407def JB |
962 | expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual( |
963 | numberOfWorkers * maxMultiplier | |
964 | ) | |
b97d82d8 JB |
965 | expect(workerNode.usage.runTime.history.length).toBe(0) |
966 | expect(workerNode.usage.waitTime.history.length).toBe(0) | |
967 | expect(workerNode.usage.elu.idle.history.length).toBe(0) | |
968 | expect(workerNode.usage.elu.active.history.length).toBe(0) | |
9e619829 JB |
969 | } |
970 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) | |
f06e48d8 | 971 | for (const workerNode of pool.workerNodes) { |
465b2940 | 972 | expect(workerNode.usage).toStrictEqual({ |
a4e07f72 JB |
973 | tasks: { |
974 | executed: 0, | |
975 | executing: 0, | |
976 | queued: 0, | |
df593701 | 977 | maxQueued: 0, |
68cbdc84 | 978 | stolen: 0, |
a4e07f72 JB |
979 | failed: 0 |
980 | }, | |
981 | runTime: { | |
a4e07f72 JB |
982 | history: expect.any(CircularArray) |
983 | }, | |
984 | waitTime: { | |
a4e07f72 JB |
985 | history: expect.any(CircularArray) |
986 | }, | |
5df69fab JB |
987 | elu: { |
988 | idle: { | |
5df69fab JB |
989 | history: expect.any(CircularArray) |
990 | }, | |
991 | active: { | |
5df69fab | 992 | history: expect.any(CircularArray) |
f7510105 | 993 | } |
5df69fab | 994 | } |
86bf340d | 995 | }) |
465b2940 JB |
996 | expect(workerNode.usage.runTime.history.length).toBe(0) |
997 | expect(workerNode.usage.waitTime.history.length).toBe(0) | |
b97d82d8 JB |
998 | expect(workerNode.usage.elu.idle.history.length).toBe(0) |
999 | expect(workerNode.usage.elu.active.history.length).toBe(0) | |
ee11a4a2 | 1000 | } |
fd7ebd49 | 1001 | await pool.destroy() |
ee11a4a2 JB |
1002 | }) |
1003 | ||
a1763c54 JB |
1004 | it("Verify that pool event emitter 'ready' event can register a callback", async () => { |
1005 | const pool = new DynamicClusterPool( | |
2431bdb4 | 1006 | Math.floor(numberOfWorkers / 2), |
164d950a | 1007 | numberOfWorkers, |
a1763c54 | 1008 | './tests/worker-files/cluster/testWorker.js' |
164d950a | 1009 | ) |
d46660cd | 1010 | let poolInfo |
a1763c54 JB |
1011 | let poolReady = 0 |
1012 | pool.emitter.on(PoolEvents.ready, (info) => { | |
1013 | ++poolReady | |
d46660cd JB |
1014 | poolInfo = info |
1015 | }) | |
a1763c54 JB |
1016 | await waitPoolEvents(pool, PoolEvents.ready, 1) |
1017 | expect(poolReady).toBe(1) | |
d46660cd | 1018 | expect(poolInfo).toStrictEqual({ |
23ccf9d7 | 1019 | version, |
d46660cd | 1020 | type: PoolTypes.dynamic, |
a1763c54 JB |
1021 | worker: WorkerTypes.cluster, |
1022 | ready: true, | |
2431bdb4 JB |
1023 | strategy: WorkerChoiceStrategies.ROUND_ROBIN, |
1024 | minSize: expect.any(Number), | |
1025 | maxSize: expect.any(Number), | |
1026 | workerNodes: expect.any(Number), | |
1027 | idleWorkerNodes: expect.any(Number), | |
1028 | busyWorkerNodes: expect.any(Number), | |
1029 | executedTasks: expect.any(Number), | |
1030 | executingTasks: expect.any(Number), | |
2431bdb4 JB |
1031 | failedTasks: expect.any(Number) |
1032 | }) | |
1033 | await pool.destroy() | |
1034 | }) | |
1035 | ||
a1763c54 JB |
1036 | it("Verify that pool event emitter 'busy' event can register a callback", async () => { |
1037 | const pool = new FixedThreadPool( | |
2431bdb4 | 1038 | numberOfWorkers, |
a1763c54 | 1039 | './tests/worker-files/thread/testWorker.js' |
2431bdb4 | 1040 | ) |
a1763c54 JB |
1041 | const promises = new Set() |
1042 | let poolBusy = 0 | |
2431bdb4 | 1043 | let poolInfo |
a1763c54 JB |
1044 | pool.emitter.on(PoolEvents.busy, (info) => { |
1045 | ++poolBusy | |
2431bdb4 JB |
1046 | poolInfo = info |
1047 | }) | |
a1763c54 JB |
1048 | for (let i = 0; i < numberOfWorkers * 2; i++) { |
1049 | promises.add(pool.execute()) | |
1050 | } | |
1051 | await Promise.all(promises) | |
1052 | // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers. | |
1053 | // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool. | |
1054 | expect(poolBusy).toBe(numberOfWorkers + 1) | |
2431bdb4 JB |
1055 | expect(poolInfo).toStrictEqual({ |
1056 | version, | |
a1763c54 JB |
1057 | type: PoolTypes.fixed, |
1058 | worker: WorkerTypes.thread, | |
1059 | ready: expect.any(Boolean), | |
2431bdb4 | 1060 | strategy: WorkerChoiceStrategies.ROUND_ROBIN, |
d46660cd JB |
1061 | minSize: expect.any(Number), |
1062 | maxSize: expect.any(Number), | |
1063 | workerNodes: expect.any(Number), | |
1064 | idleWorkerNodes: expect.any(Number), | |
1065 | busyWorkerNodes: expect.any(Number), | |
a4e07f72 JB |
1066 | executedTasks: expect.any(Number), |
1067 | executingTasks: expect.any(Number), | |
a4e07f72 | 1068 | failedTasks: expect.any(Number) |
d46660cd | 1069 | }) |
164d950a JB |
1070 | await pool.destroy() |
1071 | }) | |
1072 | ||
a1763c54 JB |
1073 | it("Verify that pool event emitter 'full' event can register a callback", async () => { |
1074 | const pool = new DynamicThreadPool( | |
1075 | Math.floor(numberOfWorkers / 2), | |
7c0ba920 JB |
1076 | numberOfWorkers, |
1077 | './tests/worker-files/thread/testWorker.js' | |
1078 | ) | |
09c2d0d3 | 1079 | const promises = new Set() |
a1763c54 | 1080 | let poolFull = 0 |
d46660cd | 1081 | let poolInfo |
a1763c54 JB |
1082 | pool.emitter.on(PoolEvents.full, (info) => { |
1083 | ++poolFull | |
d46660cd JB |
1084 | poolInfo = info |
1085 | }) | |
7c0ba920 | 1086 | for (let i = 0; i < numberOfWorkers * 2; i++) { |
f5d14e90 | 1087 | promises.add(pool.execute()) |
7c0ba920 | 1088 | } |
cf597bc5 | 1089 | await Promise.all(promises) |
33e6bb4c | 1090 | expect(poolFull).toBe(1) |
d46660cd | 1091 | expect(poolInfo).toStrictEqual({ |
23ccf9d7 | 1092 | version, |
a1763c54 | 1093 | type: PoolTypes.dynamic, |
d46660cd | 1094 | worker: WorkerTypes.thread, |
2431bdb4 JB |
1095 | ready: expect.any(Boolean), |
1096 | strategy: WorkerChoiceStrategies.ROUND_ROBIN, | |
8735b4e5 JB |
1097 | minSize: expect.any(Number), |
1098 | maxSize: expect.any(Number), | |
1099 | workerNodes: expect.any(Number), | |
1100 | idleWorkerNodes: expect.any(Number), | |
1101 | busyWorkerNodes: expect.any(Number), | |
1102 | executedTasks: expect.any(Number), | |
1103 | executingTasks: expect.any(Number), | |
1104 | failedTasks: expect.any(Number) | |
1105 | }) | |
1106 | await pool.destroy() | |
1107 | }) | |
1108 | ||
3e8611a8 | 1109 | it("Verify that pool event emitter 'backPressure' event can register a callback", async () => { |
b1aae695 | 1110 | const pool = new FixedThreadPool( |
8735b4e5 JB |
1111 | numberOfWorkers, |
1112 | './tests/worker-files/thread/testWorker.js', | |
1113 | { | |
1114 | enableTasksQueue: true | |
1115 | } | |
1116 | ) | |
3e8611a8 | 1117 | sinon.stub(pool, 'hasBackPressure').returns(true) |
8735b4e5 JB |
1118 | const promises = new Set() |
1119 | let poolBackPressure = 0 | |
1120 | let poolInfo | |
1121 | pool.emitter.on(PoolEvents.backPressure, (info) => { | |
1122 | ++poolBackPressure | |
1123 | poolInfo = info | |
1124 | }) | |
033f1776 | 1125 | for (let i = 0; i < numberOfWorkers + 1; i++) { |
8735b4e5 JB |
1126 | promises.add(pool.execute()) |
1127 | } | |
1128 | await Promise.all(promises) | |
033f1776 | 1129 | expect(poolBackPressure).toBe(1) |
8735b4e5 JB |
1130 | expect(poolInfo).toStrictEqual({ |
1131 | version, | |
3e8611a8 | 1132 | type: PoolTypes.fixed, |
8735b4e5 JB |
1133 | worker: WorkerTypes.thread, |
1134 | ready: expect.any(Boolean), | |
1135 | strategy: WorkerChoiceStrategies.ROUND_ROBIN, | |
d46660cd JB |
1136 | minSize: expect.any(Number), |
1137 | maxSize: expect.any(Number), | |
1138 | workerNodes: expect.any(Number), | |
1139 | idleWorkerNodes: expect.any(Number), | |
1140 | busyWorkerNodes: expect.any(Number), | |
a4e07f72 JB |
1141 | executedTasks: expect.any(Number), |
1142 | executingTasks: expect.any(Number), | |
3e8611a8 JB |
1143 | maxQueuedTasks: expect.any(Number), |
1144 | queuedTasks: expect.any(Number), | |
1145 | backPressure: true, | |
68cbdc84 | 1146 | stolenTasks: expect.any(Number), |
a4e07f72 | 1147 | failedTasks: expect.any(Number) |
d46660cd | 1148 | }) |
3e8611a8 | 1149 | expect(pool.hasBackPressure.called).toBe(true) |
fd7ebd49 | 1150 | await pool.destroy() |
7c0ba920 | 1151 | }) |
70a4f5ea | 1152 | |
90d7d101 JB |
1153 | it('Verify that listTaskFunctions() is working', async () => { |
1154 | const dynamicThreadPool = new DynamicThreadPool( | |
1155 | Math.floor(numberOfWorkers / 2), | |
1156 | numberOfWorkers, | |
1157 | './tests/worker-files/thread/testMultipleTaskFunctionsWorker.js' | |
1158 | ) | |
1159 | await waitPoolEvents(dynamicThreadPool, PoolEvents.ready, 1) | |
1160 | expect(dynamicThreadPool.listTaskFunctions()).toStrictEqual([ | |
6cd5248f | 1161 | DEFAULT_TASK_NAME, |
90d7d101 JB |
1162 | 'jsonIntegerSerialization', |
1163 | 'factorial', | |
1164 | 'fibonacci' | |
1165 | ]) | |
1166 | const fixedClusterPool = new FixedClusterPool( | |
1167 | numberOfWorkers, | |
1168 | './tests/worker-files/cluster/testMultipleTaskFunctionsWorker.js' | |
1169 | ) | |
1170 | await waitPoolEvents(fixedClusterPool, PoolEvents.ready, 1) | |
1171 | expect(fixedClusterPool.listTaskFunctions()).toStrictEqual([ | |
6cd5248f | 1172 | DEFAULT_TASK_NAME, |
90d7d101 JB |
1173 | 'jsonIntegerSerialization', |
1174 | 'factorial', | |
1175 | 'fibonacci' | |
1176 | ]) | |
0fe39c97 JB |
1177 | await dynamicThreadPool.destroy() |
1178 | await fixedClusterPool.destroy() | |
90d7d101 JB |
1179 | }) |
1180 | ||
1181 | it('Verify that multiple task functions worker is working', async () => { | |
70a4f5ea | 1182 | const pool = new DynamicClusterPool( |
2431bdb4 | 1183 | Math.floor(numberOfWorkers / 2), |
70a4f5ea | 1184 | numberOfWorkers, |
90d7d101 | 1185 | './tests/worker-files/cluster/testMultipleTaskFunctionsWorker.js' |
70a4f5ea JB |
1186 | ) |
1187 | const data = { n: 10 } | |
82888165 | 1188 | const result0 = await pool.execute(data) |
30b963d4 | 1189 | expect(result0).toStrictEqual({ ok: 1 }) |
70a4f5ea | 1190 | const result1 = await pool.execute(data, 'jsonIntegerSerialization') |
30b963d4 | 1191 | expect(result1).toStrictEqual({ ok: 1 }) |
70a4f5ea JB |
1192 | const result2 = await pool.execute(data, 'factorial') |
1193 | expect(result2).toBe(3628800) | |
1194 | const result3 = await pool.execute(data, 'fibonacci') | |
024daf59 | 1195 | expect(result3).toBe(55) |
5bb5be17 JB |
1196 | expect(pool.info.executingTasks).toBe(0) |
1197 | expect(pool.info.executedTasks).toBe(4) | |
b414b84c JB |
1198 | for (const workerNode of pool.workerNodes) { |
1199 | expect(workerNode.info.taskFunctions).toStrictEqual([ | |
6cd5248f | 1200 | DEFAULT_TASK_NAME, |
b414b84c JB |
1201 | 'jsonIntegerSerialization', |
1202 | 'factorial', | |
1203 | 'fibonacci' | |
1204 | ]) | |
1205 | expect(workerNode.taskFunctionsUsage.size).toBe(3) | |
1206 | for (const name of pool.listTaskFunctions()) { | |
5bb5be17 JB |
1207 | expect(workerNode.getTaskFunctionWorkerUsage(name)).toStrictEqual({ |
1208 | tasks: { | |
1209 | executed: expect.any(Number), | |
1210 | executing: expect.any(Number), | |
1211 | failed: 0, | |
68cbdc84 JB |
1212 | queued: 0, |
1213 | stolen: 0 | |
5bb5be17 JB |
1214 | }, |
1215 | runTime: { | |
1216 | history: expect.any(CircularArray) | |
1217 | }, | |
1218 | waitTime: { | |
1219 | history: expect.any(CircularArray) | |
1220 | }, | |
1221 | elu: { | |
1222 | idle: { | |
1223 | history: expect.any(CircularArray) | |
1224 | }, | |
1225 | active: { | |
1226 | history: expect.any(CircularArray) | |
1227 | } | |
1228 | } | |
1229 | }) | |
1230 | expect( | |
1231 | workerNode.getTaskFunctionWorkerUsage(name).tasks.executing | |
1232 | ).toBeGreaterThanOrEqual(0) | |
1233 | } | |
1234 | } | |
0fe39c97 | 1235 | await pool.destroy() |
70a4f5ea | 1236 | }) |
3ec964d6 | 1237 | }) |