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