Commit | Line | Data |
---|---|---|
a61a0724 | 1 | const { expect } = require('expect') |
e843b904 | 2 | const { |
70a4f5ea | 3 | DynamicClusterPool, |
9e619829 | 4 | DynamicThreadPool, |
aee46736 | 5 | FixedClusterPool, |
e843b904 | 6 | FixedThreadPool, |
aee46736 | 7 | PoolEvents, |
6b27d407 | 8 | WorkerChoiceStrategies, |
184855e6 JB |
9 | PoolTypes, |
10 | WorkerTypes | |
cdace0e5 | 11 | } = require('../../../lib') |
78099a15 | 12 | const { CircularArray } = require('../../../lib/circular-array') |
29ee7e9a | 13 | const { Queue } = require('../../../lib/queue') |
e1ffb94f JB |
14 | |
15 | describe('Abstract pool test suite', () => { | |
fc027381 | 16 | const numberOfWorkers = 2 |
a8884ffd | 17 | class StubPoolWithRemoveAllWorker extends FixedThreadPool { |
e1ffb94f | 18 | removeAllWorker () { |
d4aeae5a | 19 | this.workerNodes = [] |
c923ce56 | 20 | this.promiseResponseMap.clear() |
e1ffb94f | 21 | } |
3ec964d6 | 22 | } |
a8884ffd | 23 | class StubPoolWithIsMain extends FixedThreadPool { |
e1ffb94f JB |
24 | isMain () { |
25 | return false | |
26 | } | |
3ec964d6 | 27 | } |
3ec964d6 | 28 | |
3ec964d6 | 29 | it('Simulate pool creation from a non main thread/process', () => { |
8d3782fa JB |
30 | expect( |
31 | () => | |
a8884ffd | 32 | new StubPoolWithIsMain( |
7c0ba920 | 33 | numberOfWorkers, |
8d3782fa JB |
34 | './tests/worker-files/thread/testWorker.js', |
35 | { | |
36 | errorHandler: e => console.error(e) | |
37 | } | |
38 | ) | |
d4aeae5a | 39 | ).toThrowError('Cannot start a pool from a worker!') |
3ec964d6 | 40 | }) |
c510fea7 APA |
41 | |
42 | it('Verify that filePath is checked', () => { | |
292ad316 JB |
43 | const expectedError = new Error( |
44 | 'Please specify a file with a worker implementation' | |
45 | ) | |
7c0ba920 | 46 | expect(() => new FixedThreadPool(numberOfWorkers)).toThrowError( |
292ad316 | 47 | expectedError |
8d3782fa | 48 | ) |
7c0ba920 | 49 | expect(() => new FixedThreadPool(numberOfWorkers, '')).toThrowError( |
292ad316 | 50 | expectedError |
8d3782fa JB |
51 | ) |
52 | }) | |
53 | ||
54 | it('Verify that numberOfWorkers is checked', () => { | |
55 | expect(() => new FixedThreadPool()).toThrowError( | |
d4aeae5a | 56 | 'Cannot instantiate a pool without specifying the number of workers' |
8d3782fa JB |
57 | ) |
58 | }) | |
59 | ||
60 | it('Verify that a negative number of workers is checked', () => { | |
61 | expect( | |
62 | () => | |
63 | new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js') | |
64 | ).toThrowError( | |
473c717a JB |
65 | new RangeError( |
66 | 'Cannot instantiate a pool with a negative number of workers' | |
67 | ) | |
8d3782fa JB |
68 | ) |
69 | }) | |
70 | ||
71 | it('Verify that a non integer number of workers is checked', () => { | |
72 | expect( | |
73 | () => | |
74 | new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js') | |
75 | ).toThrowError( | |
473c717a | 76 | new TypeError( |
0d80593b | 77 | 'Cannot instantiate a pool with a non safe integer number of workers' |
8d3782fa JB |
78 | ) |
79 | ) | |
c510fea7 | 80 | }) |
7c0ba920 | 81 | |
fd7ebd49 | 82 | it('Verify that pool options are checked', async () => { |
7c0ba920 JB |
83 | let pool = new FixedThreadPool( |
84 | numberOfWorkers, | |
85 | './tests/worker-files/thread/testWorker.js' | |
86 | ) | |
7c0ba920 | 87 | expect(pool.emitter).toBeDefined() |
1f68cede JB |
88 | expect(pool.opts.enableEvents).toBe(true) |
89 | expect(pool.opts.restartWorkerOnError).toBe(true) | |
ff733df7 | 90 | expect(pool.opts.enableTasksQueue).toBe(false) |
d4aeae5a | 91 | expect(pool.opts.tasksQueueOptions).toBeUndefined() |
e843b904 JB |
92 | expect(pool.opts.workerChoiceStrategy).toBe( |
93 | WorkerChoiceStrategies.ROUND_ROBIN | |
94 | ) | |
da309861 | 95 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ |
932fc8be | 96 | runTime: { median: false }, |
5df69fab JB |
97 | waitTime: { median: false }, |
98 | elu: { median: false } | |
da309861 | 99 | }) |
35cf1c03 JB |
100 | expect(pool.opts.messageHandler).toBeUndefined() |
101 | expect(pool.opts.errorHandler).toBeUndefined() | |
102 | expect(pool.opts.onlineHandler).toBeUndefined() | |
103 | expect(pool.opts.exitHandler).toBeUndefined() | |
fd7ebd49 | 104 | await pool.destroy() |
35cf1c03 | 105 | const testHandler = () => console.log('test handler executed') |
7c0ba920 JB |
106 | pool = new FixedThreadPool( |
107 | numberOfWorkers, | |
108 | './tests/worker-files/thread/testWorker.js', | |
109 | { | |
e4543b14 | 110 | workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED, |
49be33fe | 111 | workerChoiceStrategyOptions: { |
932fc8be | 112 | runTime: { median: true }, |
fc027381 | 113 | weights: { 0: 300, 1: 200 } |
49be33fe | 114 | }, |
35cf1c03 | 115 | enableEvents: false, |
1f68cede | 116 | restartWorkerOnError: false, |
ff733df7 | 117 | enableTasksQueue: true, |
d4aeae5a | 118 | tasksQueueOptions: { concurrency: 2 }, |
35cf1c03 JB |
119 | messageHandler: testHandler, |
120 | errorHandler: testHandler, | |
121 | onlineHandler: testHandler, | |
122 | exitHandler: testHandler | |
7c0ba920 JB |
123 | } |
124 | ) | |
7c0ba920 | 125 | expect(pool.emitter).toBeUndefined() |
1f68cede JB |
126 | expect(pool.opts.enableEvents).toBe(false) |
127 | expect(pool.opts.restartWorkerOnError).toBe(false) | |
ff733df7 | 128 | expect(pool.opts.enableTasksQueue).toBe(true) |
d4aeae5a | 129 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 }) |
e843b904 | 130 | expect(pool.opts.workerChoiceStrategy).toBe( |
e4543b14 | 131 | WorkerChoiceStrategies.LEAST_USED |
e843b904 | 132 | ) |
da309861 | 133 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ |
932fc8be | 134 | runTime: { median: true }, |
fc027381 | 135 | weights: { 0: 300, 1: 200 } |
da309861 | 136 | }) |
35cf1c03 JB |
137 | expect(pool.opts.messageHandler).toStrictEqual(testHandler) |
138 | expect(pool.opts.errorHandler).toStrictEqual(testHandler) | |
139 | expect(pool.opts.onlineHandler).toStrictEqual(testHandler) | |
140 | expect(pool.opts.exitHandler).toStrictEqual(testHandler) | |
fd7ebd49 | 141 | await pool.destroy() |
7c0ba920 JB |
142 | }) |
143 | ||
a20f0ba5 | 144 | it('Verify that pool options are validated', async () => { |
d4aeae5a JB |
145 | expect( |
146 | () => | |
147 | new FixedThreadPool( | |
148 | numberOfWorkers, | |
149 | './tests/worker-files/thread/testWorker.js', | |
150 | { | |
f0d7f803 | 151 | workerChoiceStrategy: 'invalidStrategy' |
d4aeae5a JB |
152 | } |
153 | ) | |
f0d7f803 | 154 | ).toThrowError("Invalid worker choice strategy 'invalidStrategy'") |
d4aeae5a JB |
155 | expect( |
156 | () => | |
157 | new FixedThreadPool( | |
158 | numberOfWorkers, | |
159 | './tests/worker-files/thread/testWorker.js', | |
160 | { | |
f0d7f803 | 161 | workerChoiceStrategyOptions: 'invalidOptions' |
d4aeae5a JB |
162 | } |
163 | ) | |
f0d7f803 JB |
164 | ).toThrowError( |
165 | 'Invalid worker choice strategy options: must be a plain object' | |
166 | ) | |
49be33fe JB |
167 | expect( |
168 | () => | |
169 | new FixedThreadPool( | |
170 | numberOfWorkers, | |
171 | './tests/worker-files/thread/testWorker.js', | |
172 | { | |
173 | workerChoiceStrategyOptions: { weights: {} } | |
174 | } | |
175 | ) | |
176 | ).toThrowError( | |
177 | 'Invalid worker choice strategy options: must have a weight for each worker node' | |
178 | ) | |
f0d7f803 JB |
179 | expect( |
180 | () => | |
181 | new FixedThreadPool( | |
182 | numberOfWorkers, | |
183 | './tests/worker-files/thread/testWorker.js', | |
184 | { | |
185 | workerChoiceStrategyOptions: { measurement: 'invalidMeasurement' } | |
186 | } | |
187 | ) | |
188 | ).toThrowError( | |
189 | "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'" | |
190 | ) | |
191 | expect( | |
192 | () => | |
193 | new FixedThreadPool( | |
194 | numberOfWorkers, | |
195 | './tests/worker-files/thread/testWorker.js', | |
196 | { | |
197 | enableTasksQueue: true, | |
198 | tasksQueueOptions: { concurrency: 0 } | |
199 | } | |
200 | ) | |
201 | ).toThrowError("Invalid worker tasks concurrency '0'") | |
202 | expect( | |
203 | () => | |
204 | new FixedThreadPool( | |
205 | numberOfWorkers, | |
206 | './tests/worker-files/thread/testWorker.js', | |
207 | { | |
208 | enableTasksQueue: true, | |
209 | tasksQueueOptions: 'invalidTasksQueueOptions' | |
210 | } | |
211 | ) | |
212 | ).toThrowError('Invalid tasks queue options: must be a plain object') | |
213 | expect( | |
214 | () => | |
215 | new FixedThreadPool( | |
216 | numberOfWorkers, | |
217 | './tests/worker-files/thread/testWorker.js', | |
218 | { | |
219 | enableTasksQueue: true, | |
220 | tasksQueueOptions: { concurrency: 0.2 } | |
221 | } | |
222 | ) | |
223 | ).toThrowError('Invalid worker tasks concurrency: must be an integer') | |
d4aeae5a JB |
224 | }) |
225 | ||
a20f0ba5 JB |
226 | it('Verify that worker choice strategy options can be set', async () => { |
227 | const pool = new FixedThreadPool( | |
228 | numberOfWorkers, | |
229 | './tests/worker-files/thread/testWorker.js', | |
230 | { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } | |
231 | ) | |
232 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ | |
932fc8be | 233 | runTime: { median: false }, |
5df69fab JB |
234 | waitTime: { median: false }, |
235 | elu: { median: false } | |
a20f0ba5 JB |
236 | }) |
237 | for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext | |
238 | .workerChoiceStrategies) { | |
86bf340d | 239 | expect(workerChoiceStrategy.opts).toStrictEqual({ |
932fc8be | 240 | runTime: { median: false }, |
5df69fab JB |
241 | waitTime: { median: false }, |
242 | elu: { median: false } | |
86bf340d | 243 | }) |
a20f0ba5 | 244 | } |
87de9ff5 JB |
245 | expect( |
246 | pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() | |
247 | ).toStrictEqual({ | |
932fc8be JB |
248 | runTime: { |
249 | aggregate: true, | |
250 | average: true, | |
251 | median: false | |
252 | }, | |
253 | waitTime: { | |
254 | aggregate: false, | |
255 | average: false, | |
256 | median: false | |
257 | }, | |
5df69fab | 258 | elu: { |
9adcefab JB |
259 | aggregate: true, |
260 | average: true, | |
5df69fab JB |
261 | median: false |
262 | } | |
86bf340d | 263 | }) |
9adcefab JB |
264 | pool.setWorkerChoiceStrategyOptions({ |
265 | runTime: { median: true }, | |
266 | elu: { median: true } | |
267 | }) | |
a20f0ba5 | 268 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ |
9adcefab JB |
269 | runTime: { median: true }, |
270 | elu: { median: true } | |
a20f0ba5 JB |
271 | }) |
272 | for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext | |
273 | .workerChoiceStrategies) { | |
932fc8be | 274 | expect(workerChoiceStrategy.opts).toStrictEqual({ |
9adcefab JB |
275 | runTime: { median: true }, |
276 | elu: { median: true } | |
932fc8be | 277 | }) |
a20f0ba5 | 278 | } |
87de9ff5 JB |
279 | expect( |
280 | pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() | |
281 | ).toStrictEqual({ | |
932fc8be JB |
282 | runTime: { |
283 | aggregate: true, | |
284 | average: false, | |
285 | median: true | |
286 | }, | |
287 | waitTime: { | |
288 | aggregate: false, | |
289 | average: false, | |
290 | median: false | |
291 | }, | |
5df69fab | 292 | elu: { |
9adcefab | 293 | aggregate: true, |
5df69fab | 294 | average: false, |
9adcefab | 295 | median: true |
5df69fab | 296 | } |
86bf340d | 297 | }) |
9adcefab JB |
298 | pool.setWorkerChoiceStrategyOptions({ |
299 | runTime: { median: false }, | |
300 | elu: { median: false } | |
301 | }) | |
a20f0ba5 | 302 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ |
9adcefab JB |
303 | runTime: { median: false }, |
304 | elu: { median: false } | |
a20f0ba5 JB |
305 | }) |
306 | for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext | |
307 | .workerChoiceStrategies) { | |
932fc8be | 308 | expect(workerChoiceStrategy.opts).toStrictEqual({ |
9adcefab JB |
309 | runTime: { median: false }, |
310 | elu: { median: false } | |
932fc8be | 311 | }) |
a20f0ba5 | 312 | } |
87de9ff5 JB |
313 | expect( |
314 | pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() | |
315 | ).toStrictEqual({ | |
932fc8be JB |
316 | runTime: { |
317 | aggregate: true, | |
318 | average: true, | |
319 | median: false | |
320 | }, | |
321 | waitTime: { | |
322 | aggregate: false, | |
323 | average: false, | |
324 | median: false | |
325 | }, | |
5df69fab | 326 | elu: { |
9adcefab JB |
327 | aggregate: true, |
328 | average: true, | |
5df69fab JB |
329 | median: false |
330 | } | |
86bf340d | 331 | }) |
1f95d544 JB |
332 | expect(() => |
333 | pool.setWorkerChoiceStrategyOptions('invalidWorkerChoiceStrategyOptions') | |
334 | ).toThrowError( | |
335 | 'Invalid worker choice strategy options: must be a plain object' | |
336 | ) | |
337 | expect(() => | |
338 | pool.setWorkerChoiceStrategyOptions({ weights: {} }) | |
339 | ).toThrowError( | |
340 | 'Invalid worker choice strategy options: must have a weight for each worker node' | |
341 | ) | |
342 | expect(() => | |
343 | pool.setWorkerChoiceStrategyOptions({ measurement: 'invalidMeasurement' }) | |
344 | ).toThrowError( | |
345 | "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'" | |
346 | ) | |
a20f0ba5 JB |
347 | await pool.destroy() |
348 | }) | |
349 | ||
350 | it('Verify that tasks queue can be enabled/disabled', async () => { | |
351 | const pool = new FixedThreadPool( | |
352 | numberOfWorkers, | |
353 | './tests/worker-files/thread/testWorker.js' | |
354 | ) | |
355 | expect(pool.opts.enableTasksQueue).toBe(false) | |
356 | expect(pool.opts.tasksQueueOptions).toBeUndefined() | |
357 | pool.enableTasksQueue(true) | |
358 | expect(pool.opts.enableTasksQueue).toBe(true) | |
359 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 1 }) | |
360 | pool.enableTasksQueue(true, { concurrency: 2 }) | |
361 | expect(pool.opts.enableTasksQueue).toBe(true) | |
362 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 }) | |
363 | pool.enableTasksQueue(false) | |
364 | expect(pool.opts.enableTasksQueue).toBe(false) | |
365 | expect(pool.opts.tasksQueueOptions).toBeUndefined() | |
366 | await pool.destroy() | |
367 | }) | |
368 | ||
369 | it('Verify that tasks queue options can be set', async () => { | |
370 | const pool = new FixedThreadPool( | |
371 | numberOfWorkers, | |
372 | './tests/worker-files/thread/testWorker.js', | |
373 | { enableTasksQueue: true } | |
374 | ) | |
375 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 1 }) | |
376 | pool.setTasksQueueOptions({ concurrency: 2 }) | |
377 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 }) | |
f0d7f803 JB |
378 | expect(() => |
379 | pool.setTasksQueueOptions('invalidTasksQueueOptions') | |
380 | ).toThrowError('Invalid tasks queue options: must be a plain object') | |
a20f0ba5 JB |
381 | expect(() => pool.setTasksQueueOptions({ concurrency: 0 })).toThrowError( |
382 | "Invalid worker tasks concurrency '0'" | |
383 | ) | |
f0d7f803 JB |
384 | expect(() => pool.setTasksQueueOptions({ concurrency: 0.2 })).toThrowError( |
385 | 'Invalid worker tasks concurrency: must be an integer' | |
386 | ) | |
a20f0ba5 JB |
387 | await pool.destroy() |
388 | }) | |
389 | ||
6b27d407 JB |
390 | it('Verify that pool info is set', async () => { |
391 | let pool = new FixedThreadPool( | |
392 | numberOfWorkers, | |
393 | './tests/worker-files/thread/testWorker.js' | |
394 | ) | |
395 | expect(pool.info).toStrictEqual({ | |
396 | type: PoolTypes.fixed, | |
184855e6 | 397 | worker: WorkerTypes.thread, |
6b27d407 JB |
398 | minSize: numberOfWorkers, |
399 | maxSize: numberOfWorkers, | |
400 | workerNodes: numberOfWorkers, | |
401 | idleWorkerNodes: numberOfWorkers, | |
402 | busyWorkerNodes: 0, | |
a4e07f72 JB |
403 | executedTasks: 0, |
404 | executingTasks: 0, | |
6b27d407 | 405 | queuedTasks: 0, |
a4e07f72 JB |
406 | maxQueuedTasks: 0, |
407 | failedTasks: 0 | |
6b27d407 | 408 | }) |
f59e1027 JB |
409 | for (const workerNode of pool.workerNodes) { |
410 | console.log('thread:workerNode.info', workerNode.info) | |
411 | } | |
6b27d407 JB |
412 | await pool.destroy() |
413 | pool = new DynamicClusterPool( | |
414 | numberOfWorkers, | |
415 | numberOfWorkers * 2, | |
f59e1027 | 416 | './tests/worker-files/cluster/testWorker.js' |
6b27d407 JB |
417 | ) |
418 | expect(pool.info).toStrictEqual({ | |
419 | type: PoolTypes.dynamic, | |
184855e6 | 420 | worker: WorkerTypes.cluster, |
6b27d407 JB |
421 | minSize: numberOfWorkers, |
422 | maxSize: numberOfWorkers * 2, | |
423 | workerNodes: numberOfWorkers, | |
424 | idleWorkerNodes: numberOfWorkers, | |
425 | busyWorkerNodes: 0, | |
a4e07f72 JB |
426 | executedTasks: 0, |
427 | executingTasks: 0, | |
6b27d407 | 428 | queuedTasks: 0, |
a4e07f72 JB |
429 | maxQueuedTasks: 0, |
430 | failedTasks: 0 | |
6b27d407 | 431 | }) |
f59e1027 JB |
432 | for (const workerNode of pool.workerNodes) { |
433 | console.log('cluster:workerNode.info', workerNode.info) | |
434 | } | |
6b27d407 JB |
435 | await pool.destroy() |
436 | }) | |
437 | ||
9d16d33e | 438 | it('Simulate worker not found', async () => { |
a8884ffd | 439 | const pool = new StubPoolWithRemoveAllWorker( |
10fcfaf4 | 440 | numberOfWorkers, |
f59e1027 | 441 | './tests/worker-files/thread/testWorker.js', |
10fcfaf4 | 442 | { |
10fcfaf4 JB |
443 | errorHandler: e => console.error(e) |
444 | } | |
445 | ) | |
d4aeae5a | 446 | expect(pool.workerNodes.length).toBe(numberOfWorkers) |
10fcfaf4 JB |
447 | // Simulate worker not found. |
448 | pool.removeAllWorker() | |
d4aeae5a | 449 | expect(pool.workerNodes.length).toBe(0) |
fd7ebd49 | 450 | await pool.destroy() |
bf9549ae JB |
451 | }) |
452 | ||
fd7ebd49 | 453 | it('Verify that worker pool tasks usage are initialized', async () => { |
bf9549ae JB |
454 | const pool = new FixedClusterPool( |
455 | numberOfWorkers, | |
456 | './tests/worker-files/cluster/testWorker.js' | |
457 | ) | |
f06e48d8 | 458 | for (const workerNode of pool.workerNodes) { |
f59e1027 | 459 | expect(workerNode.usage).toStrictEqual({ |
a4e07f72 JB |
460 | tasks: { |
461 | executed: 0, | |
462 | executing: 0, | |
463 | queued: 0, | |
df593701 | 464 | maxQueued: 0, |
a4e07f72 JB |
465 | failed: 0 |
466 | }, | |
467 | runTime: { | |
932fc8be | 468 | aggregate: 0, |
a4e07f72 JB |
469 | average: 0, |
470 | median: 0, | |
471 | history: expect.any(CircularArray) | |
472 | }, | |
473 | waitTime: { | |
932fc8be | 474 | aggregate: 0, |
a4e07f72 JB |
475 | average: 0, |
476 | median: 0, | |
477 | history: expect.any(CircularArray) | |
478 | }, | |
5df69fab JB |
479 | elu: { |
480 | idle: { | |
481 | aggregate: 0, | |
482 | average: 0, | |
483 | median: 0, | |
484 | history: expect.any(CircularArray) | |
485 | }, | |
486 | active: { | |
487 | aggregate: 0, | |
488 | average: 0, | |
489 | median: 0, | |
490 | history: expect.any(CircularArray) | |
491 | }, | |
492 | utilization: 0 | |
493 | } | |
86bf340d | 494 | }) |
f06e48d8 JB |
495 | } |
496 | await pool.destroy() | |
497 | }) | |
498 | ||
499 | it('Verify that worker pool tasks queue are initialized', async () => { | |
500 | const pool = new FixedClusterPool( | |
501 | numberOfWorkers, | |
502 | './tests/worker-files/cluster/testWorker.js' | |
503 | ) | |
504 | for (const workerNode of pool.workerNodes) { | |
505 | expect(workerNode.tasksQueue).toBeDefined() | |
29ee7e9a | 506 | expect(workerNode.tasksQueue).toBeInstanceOf(Queue) |
4d8bf9e4 | 507 | expect(workerNode.tasksQueue.size).toBe(0) |
9c16fb4b | 508 | expect(workerNode.tasksQueue.maxSize).toBe(0) |
bf9549ae | 509 | } |
fd7ebd49 | 510 | await pool.destroy() |
bf9549ae JB |
511 | }) |
512 | ||
513 | it('Verify that worker pool tasks usage are computed', async () => { | |
514 | const pool = new FixedClusterPool( | |
515 | numberOfWorkers, | |
516 | './tests/worker-files/cluster/testWorker.js' | |
517 | ) | |
09c2d0d3 | 518 | const promises = new Set() |
fc027381 JB |
519 | const maxMultiplier = 2 |
520 | for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) { | |
09c2d0d3 | 521 | promises.add(pool.execute()) |
bf9549ae | 522 | } |
f06e48d8 | 523 | for (const workerNode of pool.workerNodes) { |
f59e1027 | 524 | expect(workerNode.usage).toStrictEqual({ |
a4e07f72 JB |
525 | tasks: { |
526 | executed: 0, | |
527 | executing: maxMultiplier, | |
528 | queued: 0, | |
df593701 | 529 | maxQueued: 0, |
a4e07f72 JB |
530 | failed: 0 |
531 | }, | |
532 | runTime: { | |
932fc8be | 533 | aggregate: 0, |
a4e07f72 JB |
534 | average: 0, |
535 | median: 0, | |
536 | history: expect.any(CircularArray) | |
537 | }, | |
538 | waitTime: { | |
932fc8be | 539 | aggregate: 0, |
a4e07f72 JB |
540 | average: 0, |
541 | median: 0, | |
542 | history: expect.any(CircularArray) | |
543 | }, | |
5df69fab JB |
544 | elu: { |
545 | idle: { | |
546 | aggregate: 0, | |
547 | average: 0, | |
548 | median: 0, | |
549 | history: expect.any(CircularArray) | |
550 | }, | |
551 | active: { | |
552 | aggregate: 0, | |
553 | average: 0, | |
554 | median: 0, | |
555 | history: expect.any(CircularArray) | |
556 | }, | |
557 | utilization: 0 | |
558 | } | |
86bf340d | 559 | }) |
bf9549ae JB |
560 | } |
561 | await Promise.all(promises) | |
f06e48d8 | 562 | for (const workerNode of pool.workerNodes) { |
f59e1027 | 563 | expect(workerNode.usage).toStrictEqual({ |
a4e07f72 JB |
564 | tasks: { |
565 | executed: maxMultiplier, | |
566 | executing: 0, | |
567 | queued: 0, | |
df593701 | 568 | maxQueued: 0, |
a4e07f72 JB |
569 | failed: 0 |
570 | }, | |
571 | runTime: { | |
932fc8be | 572 | aggregate: 0, |
a4e07f72 JB |
573 | average: 0, |
574 | median: 0, | |
575 | history: expect.any(CircularArray) | |
576 | }, | |
577 | waitTime: { | |
932fc8be | 578 | aggregate: 0, |
a4e07f72 JB |
579 | average: 0, |
580 | median: 0, | |
581 | history: expect.any(CircularArray) | |
582 | }, | |
5df69fab JB |
583 | elu: { |
584 | idle: { | |
585 | aggregate: 0, | |
586 | average: 0, | |
587 | median: 0, | |
588 | history: expect.any(CircularArray) | |
589 | }, | |
590 | active: { | |
591 | aggregate: 0, | |
592 | average: 0, | |
593 | median: 0, | |
594 | history: expect.any(CircularArray) | |
595 | }, | |
596 | utilization: 0 | |
597 | } | |
86bf340d | 598 | }) |
bf9549ae | 599 | } |
fd7ebd49 | 600 | await pool.destroy() |
bf9549ae JB |
601 | }) |
602 | ||
ee11a4a2 | 603 | it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => { |
7fd82a1c | 604 | const pool = new DynamicThreadPool( |
9e619829 | 605 | numberOfWorkers, |
8f4878b7 | 606 | numberOfWorkers, |
9e619829 JB |
607 | './tests/worker-files/thread/testWorker.js' |
608 | ) | |
09c2d0d3 | 609 | const promises = new Set() |
ee9f5295 JB |
610 | const maxMultiplier = 2 |
611 | for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) { | |
09c2d0d3 | 612 | promises.add(pool.execute()) |
9e619829 JB |
613 | } |
614 | await Promise.all(promises) | |
f06e48d8 | 615 | for (const workerNode of pool.workerNodes) { |
f59e1027 | 616 | expect(workerNode.usage).toStrictEqual({ |
a4e07f72 JB |
617 | tasks: { |
618 | executed: expect.any(Number), | |
619 | executing: 0, | |
620 | queued: 0, | |
df593701 | 621 | maxQueued: 0, |
a4e07f72 JB |
622 | failed: 0 |
623 | }, | |
624 | runTime: { | |
932fc8be | 625 | aggregate: 0, |
a4e07f72 JB |
626 | average: 0, |
627 | median: 0, | |
628 | history: expect.any(CircularArray) | |
629 | }, | |
630 | waitTime: { | |
932fc8be | 631 | aggregate: 0, |
a4e07f72 JB |
632 | average: 0, |
633 | median: 0, | |
634 | history: expect.any(CircularArray) | |
635 | }, | |
5df69fab JB |
636 | elu: { |
637 | idle: { | |
638 | aggregate: 0, | |
639 | average: 0, | |
640 | median: 0, | |
641 | history: expect.any(CircularArray) | |
642 | }, | |
643 | active: { | |
644 | aggregate: 0, | |
645 | average: 0, | |
646 | median: 0, | |
647 | history: expect.any(CircularArray) | |
648 | }, | |
649 | utilization: 0 | |
650 | } | |
86bf340d | 651 | }) |
f59e1027 JB |
652 | expect(workerNode.usage.tasks.executed).toBeGreaterThan(0) |
653 | expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(maxMultiplier) | |
9e619829 JB |
654 | } |
655 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) | |
f06e48d8 | 656 | for (const workerNode of pool.workerNodes) { |
f59e1027 | 657 | expect(workerNode.usage).toStrictEqual({ |
a4e07f72 JB |
658 | tasks: { |
659 | executed: 0, | |
660 | executing: 0, | |
661 | queued: 0, | |
df593701 | 662 | maxQueued: 0, |
a4e07f72 JB |
663 | failed: 0 |
664 | }, | |
665 | runTime: { | |
932fc8be | 666 | aggregate: 0, |
a4e07f72 JB |
667 | average: 0, |
668 | median: 0, | |
669 | history: expect.any(CircularArray) | |
670 | }, | |
671 | waitTime: { | |
932fc8be | 672 | aggregate: 0, |
a4e07f72 JB |
673 | average: 0, |
674 | median: 0, | |
675 | history: expect.any(CircularArray) | |
676 | }, | |
5df69fab JB |
677 | elu: { |
678 | idle: { | |
679 | aggregate: 0, | |
680 | average: 0, | |
681 | median: 0, | |
682 | history: expect.any(CircularArray) | |
683 | }, | |
684 | active: { | |
685 | aggregate: 0, | |
686 | average: 0, | |
687 | median: 0, | |
688 | history: expect.any(CircularArray) | |
689 | }, | |
690 | utilization: 0 | |
691 | } | |
86bf340d | 692 | }) |
f59e1027 JB |
693 | expect(workerNode.usage.runTime.history.length).toBe(0) |
694 | expect(workerNode.usage.waitTime.history.length).toBe(0) | |
ee11a4a2 | 695 | } |
fd7ebd49 | 696 | await pool.destroy() |
ee11a4a2 JB |
697 | }) |
698 | ||
164d950a JB |
699 | it("Verify that pool event emitter 'full' event can register a callback", async () => { |
700 | const pool = new DynamicThreadPool( | |
701 | numberOfWorkers, | |
702 | numberOfWorkers, | |
703 | './tests/worker-files/thread/testWorker.js' | |
704 | ) | |
09c2d0d3 | 705 | const promises = new Set() |
164d950a | 706 | let poolFull = 0 |
d46660cd JB |
707 | let poolInfo |
708 | pool.emitter.on(PoolEvents.full, info => { | |
709 | ++poolFull | |
710 | poolInfo = info | |
711 | }) | |
164d950a | 712 | for (let i = 0; i < numberOfWorkers * 2; i++) { |
f5d14e90 | 713 | promises.add(pool.execute()) |
164d950a JB |
714 | } |
715 | await Promise.all(promises) | |
594bfb84 | 716 | // The `full` event is triggered when the number of submitted tasks at once reach the max number of workers in the dynamic pool. |
fc027381 JB |
717 | // So in total numberOfWorkers * 2 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool with min = max = numberOfWorkers. |
718 | expect(poolFull).toBe(numberOfWorkers * 2) | |
d46660cd JB |
719 | expect(poolInfo).toStrictEqual({ |
720 | type: PoolTypes.dynamic, | |
721 | worker: WorkerTypes.thread, | |
722 | minSize: expect.any(Number), | |
723 | maxSize: expect.any(Number), | |
724 | workerNodes: expect.any(Number), | |
725 | idleWorkerNodes: expect.any(Number), | |
726 | busyWorkerNodes: expect.any(Number), | |
a4e07f72 JB |
727 | executedTasks: expect.any(Number), |
728 | executingTasks: expect.any(Number), | |
d46660cd | 729 | queuedTasks: expect.any(Number), |
a4e07f72 JB |
730 | maxQueuedTasks: expect.any(Number), |
731 | failedTasks: expect.any(Number) | |
d46660cd | 732 | }) |
164d950a JB |
733 | await pool.destroy() |
734 | }) | |
735 | ||
cf597bc5 | 736 | it("Verify that pool event emitter 'busy' event can register a callback", async () => { |
7c0ba920 JB |
737 | const pool = new FixedThreadPool( |
738 | numberOfWorkers, | |
739 | './tests/worker-files/thread/testWorker.js' | |
740 | ) | |
09c2d0d3 | 741 | const promises = new Set() |
7c0ba920 | 742 | let poolBusy = 0 |
d46660cd JB |
743 | let poolInfo |
744 | pool.emitter.on(PoolEvents.busy, info => { | |
745 | ++poolBusy | |
746 | poolInfo = info | |
747 | }) | |
7c0ba920 | 748 | for (let i = 0; i < numberOfWorkers * 2; i++) { |
f5d14e90 | 749 | promises.add(pool.execute()) |
7c0ba920 | 750 | } |
cf597bc5 | 751 | await Promise.all(promises) |
14916bf9 JB |
752 | // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers. |
753 | // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool. | |
754 | expect(poolBusy).toBe(numberOfWorkers + 1) | |
d46660cd JB |
755 | expect(poolInfo).toStrictEqual({ |
756 | type: PoolTypes.fixed, | |
757 | worker: WorkerTypes.thread, | |
758 | minSize: expect.any(Number), | |
759 | maxSize: expect.any(Number), | |
760 | workerNodes: expect.any(Number), | |
761 | idleWorkerNodes: expect.any(Number), | |
762 | busyWorkerNodes: expect.any(Number), | |
a4e07f72 JB |
763 | executedTasks: expect.any(Number), |
764 | executingTasks: expect.any(Number), | |
d46660cd | 765 | queuedTasks: expect.any(Number), |
a4e07f72 JB |
766 | maxQueuedTasks: expect.any(Number), |
767 | failedTasks: expect.any(Number) | |
d46660cd | 768 | }) |
fd7ebd49 | 769 | await pool.destroy() |
7c0ba920 | 770 | }) |
70a4f5ea JB |
771 | |
772 | it('Verify that multiple tasks worker is working', async () => { | |
773 | const pool = new DynamicClusterPool( | |
774 | numberOfWorkers, | |
775 | numberOfWorkers * 2, | |
776 | './tests/worker-files/cluster/testMultiTasksWorker.js' | |
777 | ) | |
778 | const data = { n: 10 } | |
82888165 JB |
779 | const result0 = await pool.execute(data) |
780 | expect(result0).toBe(false) | |
70a4f5ea JB |
781 | const result1 = await pool.execute(data, 'jsonIntegerSerialization') |
782 | expect(result1).toBe(false) | |
783 | const result2 = await pool.execute(data, 'factorial') | |
784 | expect(result2).toBe(3628800) | |
785 | const result3 = await pool.execute(data, 'fibonacci') | |
024daf59 | 786 | expect(result3).toBe(55) |
70a4f5ea | 787 | }) |
3ec964d6 | 788 | }) |