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