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