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({ |
86bf340d JB |
96 | medRunTime: false, |
97 | medWaitTime: false | |
da309861 | 98 | }) |
35cf1c03 JB |
99 | expect(pool.opts.messageHandler).toBeUndefined() |
100 | expect(pool.opts.errorHandler).toBeUndefined() | |
101 | expect(pool.opts.onlineHandler).toBeUndefined() | |
102 | expect(pool.opts.exitHandler).toBeUndefined() | |
fd7ebd49 | 103 | await pool.destroy() |
35cf1c03 | 104 | const testHandler = () => console.log('test handler executed') |
7c0ba920 JB |
105 | pool = new FixedThreadPool( |
106 | numberOfWorkers, | |
107 | './tests/worker-files/thread/testWorker.js', | |
108 | { | |
e4543b14 | 109 | workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED, |
49be33fe JB |
110 | workerChoiceStrategyOptions: { |
111 | medRunTime: true, | |
fc027381 | 112 | weights: { 0: 300, 1: 200 } |
49be33fe | 113 | }, |
35cf1c03 | 114 | enableEvents: false, |
1f68cede | 115 | restartWorkerOnError: false, |
ff733df7 | 116 | enableTasksQueue: true, |
d4aeae5a | 117 | tasksQueueOptions: { concurrency: 2 }, |
35cf1c03 JB |
118 | messageHandler: testHandler, |
119 | errorHandler: testHandler, | |
120 | onlineHandler: testHandler, | |
121 | exitHandler: testHandler | |
7c0ba920 JB |
122 | } |
123 | ) | |
7c0ba920 | 124 | expect(pool.emitter).toBeUndefined() |
1f68cede JB |
125 | expect(pool.opts.enableEvents).toBe(false) |
126 | expect(pool.opts.restartWorkerOnError).toBe(false) | |
ff733df7 | 127 | expect(pool.opts.enableTasksQueue).toBe(true) |
d4aeae5a | 128 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 }) |
e843b904 | 129 | expect(pool.opts.workerChoiceStrategy).toBe( |
e4543b14 | 130 | WorkerChoiceStrategies.LEAST_USED |
e843b904 | 131 | ) |
da309861 | 132 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ |
49be33fe | 133 | medRunTime: true, |
fc027381 | 134 | weights: { 0: 300, 1: 200 } |
da309861 | 135 | }) |
35cf1c03 JB |
136 | expect(pool.opts.messageHandler).toStrictEqual(testHandler) |
137 | expect(pool.opts.errorHandler).toStrictEqual(testHandler) | |
138 | expect(pool.opts.onlineHandler).toStrictEqual(testHandler) | |
139 | expect(pool.opts.exitHandler).toStrictEqual(testHandler) | |
fd7ebd49 | 140 | await pool.destroy() |
7c0ba920 JB |
141 | }) |
142 | ||
a20f0ba5 | 143 | it('Verify that pool options are validated', async () => { |
d4aeae5a JB |
144 | expect( |
145 | () => | |
146 | new FixedThreadPool( | |
147 | numberOfWorkers, | |
148 | './tests/worker-files/thread/testWorker.js', | |
149 | { | |
150 | enableTasksQueue: true, | |
151 | tasksQueueOptions: { concurrency: 0 } | |
152 | } | |
153 | ) | |
154 | ).toThrowError("Invalid worker tasks concurrency '0'") | |
155 | expect( | |
156 | () => | |
157 | new FixedThreadPool( | |
158 | numberOfWorkers, | |
159 | './tests/worker-files/thread/testWorker.js', | |
160 | { | |
161 | workerChoiceStrategy: 'invalidStrategy' | |
162 | } | |
163 | ) | |
164 | ).toThrowError("Invalid worker choice strategy 'invalidStrategy'") | |
49be33fe JB |
165 | expect( |
166 | () => | |
167 | new FixedThreadPool( | |
168 | numberOfWorkers, | |
169 | './tests/worker-files/thread/testWorker.js', | |
170 | { | |
171 | workerChoiceStrategyOptions: { weights: {} } | |
172 | } | |
173 | ) | |
174 | ).toThrowError( | |
175 | 'Invalid worker choice strategy options: must have a weight for each worker node' | |
176 | ) | |
d4aeae5a JB |
177 | }) |
178 | ||
a20f0ba5 JB |
179 | it('Verify that worker choice strategy options can be set', async () => { |
180 | const pool = new FixedThreadPool( | |
181 | numberOfWorkers, | |
182 | './tests/worker-files/thread/testWorker.js', | |
183 | { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } | |
184 | ) | |
185 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ | |
86bf340d JB |
186 | medRunTime: false, |
187 | medWaitTime: false | |
a20f0ba5 JB |
188 | }) |
189 | for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext | |
190 | .workerChoiceStrategies) { | |
86bf340d JB |
191 | expect(workerChoiceStrategy.opts).toStrictEqual({ |
192 | medRunTime: false, | |
193 | medWaitTime: false | |
194 | }) | |
a20f0ba5 | 195 | } |
87de9ff5 JB |
196 | expect( |
197 | pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() | |
198 | ).toStrictEqual({ | |
86bf340d JB |
199 | runTime: true, |
200 | avgRunTime: true, | |
201 | medRunTime: false, | |
202 | waitTime: false, | |
203 | avgWaitTime: false, | |
d44d5953 JB |
204 | medWaitTime: false, |
205 | elu: false | |
86bf340d | 206 | }) |
a20f0ba5 JB |
207 | pool.setWorkerChoiceStrategyOptions({ medRunTime: true }) |
208 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ | |
209 | medRunTime: true | |
210 | }) | |
211 | for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext | |
212 | .workerChoiceStrategies) { | |
213 | expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: true }) | |
214 | } | |
87de9ff5 JB |
215 | expect( |
216 | pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() | |
217 | ).toStrictEqual({ | |
86bf340d JB |
218 | runTime: true, |
219 | avgRunTime: false, | |
220 | medRunTime: true, | |
221 | waitTime: false, | |
222 | avgWaitTime: false, | |
d44d5953 JB |
223 | medWaitTime: false, |
224 | elu: false | |
86bf340d | 225 | }) |
a20f0ba5 JB |
226 | pool.setWorkerChoiceStrategyOptions({ medRunTime: false }) |
227 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ | |
228 | medRunTime: false | |
229 | }) | |
230 | for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext | |
231 | .workerChoiceStrategies) { | |
232 | expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: false }) | |
233 | } | |
87de9ff5 JB |
234 | expect( |
235 | pool.workerChoiceStrategyContext.getTaskStatisticsRequirements() | |
236 | ).toStrictEqual({ | |
86bf340d JB |
237 | runTime: true, |
238 | avgRunTime: true, | |
239 | medRunTime: false, | |
240 | waitTime: false, | |
241 | avgWaitTime: false, | |
d44d5953 JB |
242 | medWaitTime: false, |
243 | elu: false | |
86bf340d | 244 | }) |
a20f0ba5 JB |
245 | await pool.destroy() |
246 | }) | |
247 | ||
248 | it('Verify that tasks queue can be enabled/disabled', async () => { | |
249 | const pool = new FixedThreadPool( | |
250 | numberOfWorkers, | |
251 | './tests/worker-files/thread/testWorker.js' | |
252 | ) | |
253 | expect(pool.opts.enableTasksQueue).toBe(false) | |
254 | expect(pool.opts.tasksQueueOptions).toBeUndefined() | |
255 | pool.enableTasksQueue(true) | |
256 | expect(pool.opts.enableTasksQueue).toBe(true) | |
257 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 1 }) | |
258 | pool.enableTasksQueue(true, { concurrency: 2 }) | |
259 | expect(pool.opts.enableTasksQueue).toBe(true) | |
260 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 }) | |
261 | pool.enableTasksQueue(false) | |
262 | expect(pool.opts.enableTasksQueue).toBe(false) | |
263 | expect(pool.opts.tasksQueueOptions).toBeUndefined() | |
264 | await pool.destroy() | |
265 | }) | |
266 | ||
267 | it('Verify that tasks queue options can be set', async () => { | |
268 | const pool = new FixedThreadPool( | |
269 | numberOfWorkers, | |
270 | './tests/worker-files/thread/testWorker.js', | |
271 | { enableTasksQueue: true } | |
272 | ) | |
273 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 1 }) | |
274 | pool.setTasksQueueOptions({ concurrency: 2 }) | |
275 | expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 }) | |
276 | expect(() => pool.setTasksQueueOptions({ concurrency: 0 })).toThrowError( | |
277 | "Invalid worker tasks concurrency '0'" | |
278 | ) | |
279 | await pool.destroy() | |
280 | }) | |
281 | ||
6b27d407 JB |
282 | it('Verify that pool info is set', async () => { |
283 | let pool = new FixedThreadPool( | |
284 | numberOfWorkers, | |
285 | './tests/worker-files/thread/testWorker.js' | |
286 | ) | |
287 | expect(pool.info).toStrictEqual({ | |
288 | type: PoolTypes.fixed, | |
184855e6 | 289 | worker: WorkerTypes.thread, |
6b27d407 JB |
290 | minSize: numberOfWorkers, |
291 | maxSize: numberOfWorkers, | |
292 | workerNodes: numberOfWorkers, | |
293 | idleWorkerNodes: numberOfWorkers, | |
294 | busyWorkerNodes: 0, | |
a4e07f72 JB |
295 | executedTasks: 0, |
296 | executingTasks: 0, | |
6b27d407 | 297 | queuedTasks: 0, |
a4e07f72 JB |
298 | maxQueuedTasks: 0, |
299 | failedTasks: 0 | |
6b27d407 JB |
300 | }) |
301 | await pool.destroy() | |
302 | pool = new DynamicClusterPool( | |
303 | numberOfWorkers, | |
304 | numberOfWorkers * 2, | |
305 | './tests/worker-files/thread/testWorker.js' | |
306 | ) | |
307 | expect(pool.info).toStrictEqual({ | |
308 | type: PoolTypes.dynamic, | |
184855e6 | 309 | worker: WorkerTypes.cluster, |
6b27d407 JB |
310 | minSize: numberOfWorkers, |
311 | maxSize: numberOfWorkers * 2, | |
312 | workerNodes: numberOfWorkers, | |
313 | idleWorkerNodes: numberOfWorkers, | |
314 | busyWorkerNodes: 0, | |
a4e07f72 JB |
315 | executedTasks: 0, |
316 | executingTasks: 0, | |
6b27d407 | 317 | queuedTasks: 0, |
a4e07f72 JB |
318 | maxQueuedTasks: 0, |
319 | failedTasks: 0 | |
6b27d407 JB |
320 | }) |
321 | await pool.destroy() | |
322 | }) | |
323 | ||
9d16d33e | 324 | it('Simulate worker not found', async () => { |
a8884ffd | 325 | const pool = new StubPoolWithRemoveAllWorker( |
10fcfaf4 JB |
326 | numberOfWorkers, |
327 | './tests/worker-files/cluster/testWorker.js', | |
328 | { | |
10fcfaf4 JB |
329 | errorHandler: e => console.error(e) |
330 | } | |
331 | ) | |
d4aeae5a | 332 | expect(pool.workerNodes.length).toBe(numberOfWorkers) |
10fcfaf4 JB |
333 | // Simulate worker not found. |
334 | pool.removeAllWorker() | |
d4aeae5a | 335 | expect(pool.workerNodes.length).toBe(0) |
fd7ebd49 | 336 | await pool.destroy() |
bf9549ae JB |
337 | }) |
338 | ||
fd7ebd49 | 339 | it('Verify that worker pool tasks usage are initialized', async () => { |
bf9549ae JB |
340 | const pool = new FixedClusterPool( |
341 | numberOfWorkers, | |
342 | './tests/worker-files/cluster/testWorker.js' | |
343 | ) | |
f06e48d8 | 344 | for (const workerNode of pool.workerNodes) { |
a4e07f72 JB |
345 | expect(workerNode.workerUsage).toStrictEqual({ |
346 | tasks: { | |
347 | executed: 0, | |
348 | executing: 0, | |
349 | queued: 0, | |
350 | failed: 0 | |
351 | }, | |
352 | runTime: { | |
353 | aggregation: 0, | |
354 | average: 0, | |
355 | median: 0, | |
356 | history: expect.any(CircularArray) | |
357 | }, | |
358 | waitTime: { | |
359 | aggregation: 0, | |
360 | average: 0, | |
361 | median: 0, | |
362 | history: expect.any(CircularArray) | |
363 | }, | |
d44d5953 | 364 | elu: undefined |
86bf340d | 365 | }) |
f06e48d8 JB |
366 | } |
367 | await pool.destroy() | |
368 | }) | |
369 | ||
370 | it('Verify that worker pool tasks queue are initialized', async () => { | |
371 | const pool = new FixedClusterPool( | |
372 | numberOfWorkers, | |
373 | './tests/worker-files/cluster/testWorker.js' | |
374 | ) | |
375 | for (const workerNode of pool.workerNodes) { | |
376 | expect(workerNode.tasksQueue).toBeDefined() | |
29ee7e9a | 377 | expect(workerNode.tasksQueue).toBeInstanceOf(Queue) |
4d8bf9e4 | 378 | expect(workerNode.tasksQueue.size).toBe(0) |
bf9549ae | 379 | } |
fd7ebd49 | 380 | await pool.destroy() |
bf9549ae JB |
381 | }) |
382 | ||
383 | it('Verify that worker pool tasks usage are computed', async () => { | |
384 | const pool = new FixedClusterPool( | |
385 | numberOfWorkers, | |
386 | './tests/worker-files/cluster/testWorker.js' | |
387 | ) | |
09c2d0d3 | 388 | const promises = new Set() |
fc027381 JB |
389 | const maxMultiplier = 2 |
390 | for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) { | |
09c2d0d3 | 391 | promises.add(pool.execute()) |
bf9549ae | 392 | } |
f06e48d8 | 393 | for (const workerNode of pool.workerNodes) { |
a4e07f72 JB |
394 | expect(workerNode.workerUsage).toStrictEqual({ |
395 | tasks: { | |
396 | executed: 0, | |
397 | executing: maxMultiplier, | |
398 | queued: 0, | |
399 | failed: 0 | |
400 | }, | |
401 | runTime: { | |
402 | aggregation: 0, | |
403 | average: 0, | |
404 | median: 0, | |
405 | history: expect.any(CircularArray) | |
406 | }, | |
407 | waitTime: { | |
408 | aggregation: 0, | |
409 | average: 0, | |
410 | median: 0, | |
411 | history: expect.any(CircularArray) | |
412 | }, | |
d44d5953 | 413 | elu: undefined |
86bf340d | 414 | }) |
bf9549ae JB |
415 | } |
416 | await Promise.all(promises) | |
f06e48d8 | 417 | for (const workerNode of pool.workerNodes) { |
a4e07f72 JB |
418 | expect(workerNode.workerUsage).toStrictEqual({ |
419 | tasks: { | |
420 | executed: maxMultiplier, | |
421 | executing: 0, | |
422 | queued: 0, | |
423 | failed: 0 | |
424 | }, | |
425 | runTime: { | |
426 | aggregation: 0, | |
427 | average: 0, | |
428 | median: 0, | |
429 | history: expect.any(CircularArray) | |
430 | }, | |
431 | waitTime: { | |
432 | aggregation: 0, | |
433 | average: 0, | |
434 | median: 0, | |
435 | history: expect.any(CircularArray) | |
436 | }, | |
d44d5953 | 437 | elu: undefined |
86bf340d | 438 | }) |
bf9549ae | 439 | } |
fd7ebd49 | 440 | await pool.destroy() |
bf9549ae JB |
441 | }) |
442 | ||
ee11a4a2 | 443 | it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => { |
7fd82a1c | 444 | const pool = new DynamicThreadPool( |
9e619829 | 445 | numberOfWorkers, |
8f4878b7 | 446 | numberOfWorkers, |
9e619829 JB |
447 | './tests/worker-files/thread/testWorker.js' |
448 | ) | |
09c2d0d3 | 449 | const promises = new Set() |
ee9f5295 JB |
450 | const maxMultiplier = 2 |
451 | for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) { | |
09c2d0d3 | 452 | promises.add(pool.execute()) |
9e619829 JB |
453 | } |
454 | await Promise.all(promises) | |
f06e48d8 | 455 | for (const workerNode of pool.workerNodes) { |
a4e07f72 JB |
456 | expect(workerNode.workerUsage).toStrictEqual({ |
457 | tasks: { | |
458 | executed: expect.any(Number), | |
459 | executing: 0, | |
460 | queued: 0, | |
461 | failed: 0 | |
462 | }, | |
463 | runTime: { | |
464 | aggregation: 0, | |
465 | average: 0, | |
466 | median: 0, | |
467 | history: expect.any(CircularArray) | |
468 | }, | |
469 | waitTime: { | |
470 | aggregation: 0, | |
471 | average: 0, | |
472 | median: 0, | |
473 | history: expect.any(CircularArray) | |
474 | }, | |
d44d5953 | 475 | elu: undefined |
86bf340d | 476 | }) |
a4e07f72 JB |
477 | expect(workerNode.workerUsage.tasks.executed).toBeGreaterThan(0) |
478 | expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual( | |
479 | maxMultiplier | |
480 | ) | |
9e619829 JB |
481 | } |
482 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) | |
f06e48d8 | 483 | for (const workerNode of pool.workerNodes) { |
a4e07f72 JB |
484 | expect(workerNode.workerUsage).toStrictEqual({ |
485 | tasks: { | |
486 | executed: 0, | |
487 | executing: 0, | |
488 | queued: 0, | |
489 | failed: 0 | |
490 | }, | |
491 | runTime: { | |
492 | aggregation: 0, | |
493 | average: 0, | |
494 | median: 0, | |
495 | history: expect.any(CircularArray) | |
496 | }, | |
497 | waitTime: { | |
498 | aggregation: 0, | |
499 | average: 0, | |
500 | median: 0, | |
501 | history: expect.any(CircularArray) | |
502 | }, | |
d44d5953 | 503 | elu: undefined |
86bf340d | 504 | }) |
a4e07f72 JB |
505 | expect(workerNode.workerUsage.runTime.history.length).toBe(0) |
506 | expect(workerNode.workerUsage.waitTime.history.length).toBe(0) | |
ee11a4a2 | 507 | } |
fd7ebd49 | 508 | await pool.destroy() |
ee11a4a2 JB |
509 | }) |
510 | ||
164d950a JB |
511 | it("Verify that pool event emitter 'full' event can register a callback", async () => { |
512 | const pool = new DynamicThreadPool( | |
513 | numberOfWorkers, | |
514 | numberOfWorkers, | |
515 | './tests/worker-files/thread/testWorker.js' | |
516 | ) | |
09c2d0d3 | 517 | const promises = new Set() |
164d950a | 518 | let poolFull = 0 |
d46660cd JB |
519 | let poolInfo |
520 | pool.emitter.on(PoolEvents.full, info => { | |
521 | ++poolFull | |
522 | poolInfo = info | |
523 | }) | |
164d950a | 524 | for (let i = 0; i < numberOfWorkers * 2; i++) { |
f5d14e90 | 525 | promises.add(pool.execute()) |
164d950a JB |
526 | } |
527 | await Promise.all(promises) | |
594bfb84 | 528 | // 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 |
529 | // So in total numberOfWorkers * 2 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool with min = max = numberOfWorkers. |
530 | expect(poolFull).toBe(numberOfWorkers * 2) | |
d46660cd JB |
531 | expect(poolInfo).toStrictEqual({ |
532 | type: PoolTypes.dynamic, | |
533 | worker: WorkerTypes.thread, | |
534 | minSize: expect.any(Number), | |
535 | maxSize: expect.any(Number), | |
536 | workerNodes: expect.any(Number), | |
537 | idleWorkerNodes: expect.any(Number), | |
538 | busyWorkerNodes: expect.any(Number), | |
a4e07f72 JB |
539 | executedTasks: expect.any(Number), |
540 | executingTasks: expect.any(Number), | |
d46660cd | 541 | queuedTasks: expect.any(Number), |
a4e07f72 JB |
542 | maxQueuedTasks: expect.any(Number), |
543 | failedTasks: expect.any(Number) | |
d46660cd | 544 | }) |
164d950a JB |
545 | await pool.destroy() |
546 | }) | |
547 | ||
cf597bc5 | 548 | it("Verify that pool event emitter 'busy' event can register a callback", async () => { |
7c0ba920 JB |
549 | const pool = new FixedThreadPool( |
550 | numberOfWorkers, | |
551 | './tests/worker-files/thread/testWorker.js' | |
552 | ) | |
09c2d0d3 | 553 | const promises = new Set() |
7c0ba920 | 554 | let poolBusy = 0 |
d46660cd JB |
555 | let poolInfo |
556 | pool.emitter.on(PoolEvents.busy, info => { | |
557 | ++poolBusy | |
558 | poolInfo = info | |
559 | }) | |
7c0ba920 | 560 | for (let i = 0; i < numberOfWorkers * 2; i++) { |
f5d14e90 | 561 | promises.add(pool.execute()) |
7c0ba920 | 562 | } |
cf597bc5 | 563 | await Promise.all(promises) |
14916bf9 JB |
564 | // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers. |
565 | // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool. | |
566 | expect(poolBusy).toBe(numberOfWorkers + 1) | |
d46660cd JB |
567 | expect(poolInfo).toStrictEqual({ |
568 | type: PoolTypes.fixed, | |
569 | worker: WorkerTypes.thread, | |
570 | minSize: expect.any(Number), | |
571 | maxSize: expect.any(Number), | |
572 | workerNodes: expect.any(Number), | |
573 | idleWorkerNodes: expect.any(Number), | |
574 | busyWorkerNodes: expect.any(Number), | |
a4e07f72 JB |
575 | executedTasks: expect.any(Number), |
576 | executingTasks: expect.any(Number), | |
d46660cd | 577 | queuedTasks: expect.any(Number), |
a4e07f72 JB |
578 | maxQueuedTasks: expect.any(Number), |
579 | failedTasks: expect.any(Number) | |
d46660cd | 580 | }) |
fd7ebd49 | 581 | await pool.destroy() |
7c0ba920 | 582 | }) |
70a4f5ea JB |
583 | |
584 | it('Verify that multiple tasks worker is working', async () => { | |
585 | const pool = new DynamicClusterPool( | |
586 | numberOfWorkers, | |
587 | numberOfWorkers * 2, | |
588 | './tests/worker-files/cluster/testMultiTasksWorker.js' | |
589 | ) | |
590 | const data = { n: 10 } | |
82888165 JB |
591 | const result0 = await pool.execute(data) |
592 | expect(result0).toBe(false) | |
70a4f5ea JB |
593 | const result1 = await pool.execute(data, 'jsonIntegerSerialization') |
594 | expect(result1).toBe(false) | |
595 | const result2 = await pool.execute(data, 'factorial') | |
596 | expect(result2).toBe(3628800) | |
597 | const result3 = await pool.execute(data, 'fibonacci') | |
598 | expect(result3).toBe(89) | |
599 | }) | |
3ec964d6 | 600 | }) |