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