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