1 import { expect } from 'expect'
2 import { DynamicClusterPool, PoolEvents } from '../../../lib/index.js'
3 import { TaskFunctions } from '../../test-types.js'
4 import { sleep, waitWorkerEvents } from '../../test-utils.js'
6 describe('Dynamic cluster pool test suite', () => {
9 const pool = new DynamicClusterPool(
12 './tests/worker-files/cluster/testWorker.js',
14 errorHandler: e => console.error(e)
18 it('Verify that the function is executed in a worker cluster', async () => {
19 let result = await pool.execute({
20 function: TaskFunctions.fibonacci
22 expect(result).toBe(75025)
23 result = await pool.execute({
24 function: TaskFunctions.factorial
26 expect(result).toBe(9.33262154439441e157)
29 it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
31 pool.emitter.on(PoolEvents.busy, () => ++poolBusy)
32 for (let i = 0; i < max * 2; i++) {
35 expect(pool.workerNodes.length).toBeLessThanOrEqual(max)
36 expect(pool.workerNodes.length).toBeGreaterThan(min)
37 expect(poolBusy).toBe(1)
38 const numberOfExitEvents = await waitWorkerEvents(pool, 'exit', max - min)
39 expect(numberOfExitEvents).toBe(max - min)
42 it('Verify scale worker up and down is working', async () => {
43 expect(pool.workerNodes.length).toBe(min)
44 for (let i = 0; i < max * 2; i++) {
47 expect(pool.workerNodes.length).toBeGreaterThan(min)
48 await waitWorkerEvents(pool, 'exit', max - min)
49 expect(pool.workerNodes.length).toBe(min)
50 for (let i = 0; i < max * 2; i++) {
53 expect(pool.workerNodes.length).toBeGreaterThan(min)
54 await waitWorkerEvents(pool, 'exit', max - min)
55 expect(pool.workerNodes.length).toBe(min)
58 it('Shutdown test', async () => {
59 const exitPromise = waitWorkerEvents(pool, 'exit', min)
60 expect(pool.emitter.eventNames()).toStrictEqual([PoolEvents.busy])
62 pool.emitter.on(PoolEvents.destroy, () => ++poolDestroy)
63 expect(pool.emitter.eventNames()).toStrictEqual([
68 const numberOfExitEvents = await exitPromise
69 expect(pool.started).toBe(false)
70 expect(pool.workerNodes.length).toBe(0)
71 expect(numberOfExitEvents).toBe(min)
72 expect(poolDestroy).toBe(1)
75 it('Validation of inputs test', () => {
76 expect(() => new DynamicClusterPool(min)).toThrow(
77 "Cannot find the worker file 'undefined'"
81 it('Should work even without opts in input', async () => {
82 const pool = new DynamicClusterPool(
85 './tests/worker-files/cluster/testWorker.js'
87 const result = await pool.execute()
88 expect(result).toStrictEqual({ ok: 1 })
89 // We need to clean up the resources after our test
93 it('Verify scale processes up and down is working when long executing task is used:hard', async () => {
94 const longRunningPool = new DynamicClusterPool(
97 './tests/worker-files/cluster/longRunningWorkerHardBehavior.js',
99 errorHandler: e => console.error(e),
100 onlineHandler: () => console.info('long executing worker is online'),
101 exitHandler: () => console.info('long executing worker exited')
104 expect(longRunningPool.workerNodes.length).toBe(min)
105 for (let i = 0; i < max * 2; i++) {
106 longRunningPool.execute()
108 expect(longRunningPool.workerNodes.length).toBe(max)
109 await waitWorkerEvents(longRunningPool, 'exit', max - min)
110 expect(longRunningPool.workerNodes.length).toBe(min)
112 longRunningPool.workerChoiceStrategyContext.workerChoiceStrategies.get(
113 longRunningPool.workerChoiceStrategyContext.workerChoiceStrategy
115 ).toBeLessThan(longRunningPool.workerNodes.length)
116 // We need to clean up the resources after our test
117 await longRunningPool.destroy()
120 it('Verify scale processes up and down is working when long executing task is used:soft', async () => {
121 const longRunningPool = new DynamicClusterPool(
124 './tests/worker-files/cluster/longRunningWorkerSoftBehavior.js',
126 errorHandler: e => console.error(e),
127 onlineHandler: () => console.info('long executing worker is online'),
128 exitHandler: () => console.info('long executing worker exited')
131 expect(longRunningPool.workerNodes.length).toBe(min)
132 for (let i = 0; i < max * 2; i++) {
133 longRunningPool.execute()
135 expect(longRunningPool.workerNodes.length).toBe(max)
137 // Here we expect the workerNodes to be at the max size since the task is still executing
138 expect(longRunningPool.workerNodes.length).toBe(max)
139 // We need to clean up the resources after our test
140 await longRunningPool.destroy()
143 it('Verify that a pool with zero worker can be instantiated', async () => {
144 const pool = new DynamicClusterPool(
147 './tests/worker-files/cluster/testWorker.js'
149 expect(pool).toBeInstanceOf(DynamicClusterPool)
150 // We need to clean up the resources after our test