1 const { expect
} = require('expect')
2 const { DynamicClusterPool
, PoolEvents
} = require('../../../lib')
3 const { TaskFunctions
} = require('../../test-types')
4 const { sleep
, waitWorkerEvents
} = require('../../test-utils')
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
)
61 pool
.emitter
.on(PoolEvents
.destroy
, () => ++poolDestroy
)
63 const numberOfExitEvents
= await exitPromise
64 expect(numberOfExitEvents
).toBe(min
)
65 expect(poolDestroy
).toBe(1)
68 it('Validation of inputs test', () => {
69 expect(() => new DynamicClusterPool(min
)).toThrowError(
70 'Please specify a file with a worker implementation'
74 it('Should work even without opts in input', async () => {
75 const pool
= new DynamicClusterPool(
78 './tests/worker-files/cluster/testWorker.js'
80 const result
= await pool
.execute()
81 expect(result
).toStrictEqual({ ok
: 1 })
82 // We need to clean up the resources after our test
86 it('Verify scale processes up and down is working when long executing task is used:hard', async () => {
87 const longRunningPool
= new DynamicClusterPool(
90 './tests/worker-files/cluster/longRunningWorkerHardBehavior.js',
92 errorHandler
: (e
) => console
.error(e
),
93 onlineHandler
: () => console
.info('long executing worker is online'),
94 exitHandler
: () => console
.info('long executing worker exited')
97 expect(longRunningPool
.workerNodes
.length
).toBe(min
)
98 for (let i
= 0; i
< max
* 2; i
++) {
99 longRunningPool
.execute()
101 expect(longRunningPool
.workerNodes
.length
).toBe(max
)
102 await
waitWorkerEvents(longRunningPool
, 'exit', max
- min
)
103 expect(longRunningPool
.workerNodes
.length
).toBe(min
)
105 longRunningPool
.workerChoiceStrategyContext
.workerChoiceStrategies
.get(
106 longRunningPool
.workerChoiceStrategyContext
.workerChoiceStrategy
108 ).toBeLessThan(longRunningPool
.workerNodes
.length
)
109 // We need to clean up the resources after our test
110 await longRunningPool
.destroy()
113 it('Verify scale processes up and down is working when long executing task is used:soft', async () => {
114 const longRunningPool
= new DynamicClusterPool(
117 './tests/worker-files/cluster/longRunningWorkerSoftBehavior.js',
119 errorHandler
: (e
) => console
.error(e
),
120 onlineHandler
: () => console
.info('long executing worker is online'),
121 exitHandler
: () => console
.info('long executing worker exited')
124 expect(longRunningPool
.workerNodes
.length
).toBe(min
)
125 for (let i
= 0; i
< max
* 2; i
++) {
126 longRunningPool
.execute()
128 expect(longRunningPool
.workerNodes
.length
).toBe(max
)
130 // Here we expect the workerNodes to be at the max size since the task is still executing
131 expect(longRunningPool
.workerNodes
.length
).toBe(max
)
132 // We need to clean up the resources after our test
133 await longRunningPool
.destroy()
136 it('Verify that a pool with zero worker can be instantiated', async () => {
137 const pool
= new DynamicClusterPool(
140 './tests/worker-files/cluster/testWorker.js'
142 expect(pool
).toBeInstanceOf(DynamicClusterPool
)
143 // We need to clean up the resources after our test