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