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