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