chore: v3.1.22
[poolifier.git] / tests / pools / cluster / dynamic.test.mjs
CommitLineData
a074ffee 1import { expect } from 'expect'
ded253e2 2
8e8d9101
JB
3import {
4 DynamicClusterPool,
5 PoolEvents,
6 WorkerChoiceStrategies
7} from '../../../lib/index.cjs'
d35e5717 8import { TaskFunctions } from '../../test-types.cjs'
8e8d9101 9import { sleep, waitPoolEvents, waitWorkerEvents } from '../../test-utils.cjs'
506c2a14 10
a35560ba 11describe('Dynamic cluster pool test suite', () => {
e1ffb94f
JB
12 const min = 1
13 const max = 3
14 const pool = new DynamicClusterPool(
15 min,
16 max,
d35e5717 17 './tests/worker-files/cluster/testWorker.cjs',
e1ffb94f 18 {
041dc05b 19 errorHandler: e => console.error(e)
e1ffb94f
JB
20 }
21 )
22
325f50bc 23 it('Verify that the function is executed in a worker cluster', async () => {
6db75ad9 24 let result = await pool.execute({
dbca3be9 25 function: TaskFunctions.fibonacci
6db75ad9 26 })
024daf59 27 expect(result).toBe(75025)
6db75ad9 28 result = await pool.execute({
dbca3be9 29 function: TaskFunctions.factorial
6db75ad9 30 })
70a4f5ea 31 expect(result).toBe(9.33262154439441e157)
506c2a14 32 })
33
34 it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
7c0ba920 35 let poolBusy = 0
aee46736 36 pool.emitter.on(PoolEvents.busy, () => ++poolBusy)
cf9aa6c3 37 for (let i = 0; i < max * 2; i++) {
8cbb82eb 38 pool.execute()
506c2a14 39 }
f06e48d8
JB
40 expect(pool.workerNodes.length).toBeLessThanOrEqual(max)
41 expect(pool.workerNodes.length).toBeGreaterThan(min)
94407def 42 expect(poolBusy).toBe(1)
bac873bd 43 const numberOfExitEvents = await waitWorkerEvents(pool, 'exit', max - min)
85a3f8a7 44 expect(numberOfExitEvents).toBe(max - min)
9b5c72ff 45 expect(pool.workerNodes.length).toBe(min)
506c2a14 46 })
47
325f50bc 48 it('Verify scale worker up and down is working', async () => {
e211bc18 49 for (let i = 0; i < max * 2; i++) {
6db75ad9 50 pool.execute()
bcf04003 51 }
f06e48d8 52 expect(pool.workerNodes.length).toBeGreaterThan(min)
bac873bd 53 await waitWorkerEvents(pool, 'exit', max - min)
f06e48d8 54 expect(pool.workerNodes.length).toBe(min)
e211bc18 55 for (let i = 0; i < max * 2; i++) {
6db75ad9 56 pool.execute()
bcf04003 57 }
f06e48d8 58 expect(pool.workerNodes.length).toBeGreaterThan(min)
bac873bd 59 await waitWorkerEvents(pool, 'exit', max - min)
f06e48d8 60 expect(pool.workerNodes.length).toBe(min)
bcf04003 61 })
506c2a14 62
85a3f8a7 63 it('Shutdown test', async () => {
bac873bd 64 const exitPromise = waitWorkerEvents(pool, 'exit', min)
c726f66c 65 expect(pool.emitter.eventNames()).toStrictEqual([PoolEvents.busy])
ef3891a3
JB
66 let poolDestroy = 0
67 pool.emitter.on(PoolEvents.destroy, () => ++poolDestroy)
c726f66c
JB
68 expect(pool.emitter.eventNames()).toStrictEqual([
69 PoolEvents.busy,
70 PoolEvents.destroy
71 ])
85a3f8a7 72 await pool.destroy()
bdacc2d2 73 const numberOfExitEvents = await exitPromise
bb9423b7 74 expect(pool.started).toBe(false)
d67bed32 75 expect(pool.emitter.eventNames()).toStrictEqual(['busy', 'destroy'])
55082af9 76 expect(pool.readyEventEmitted).toBe(false)
bb9423b7 77 expect(pool.workerNodes.length).toBe(0)
bdacc2d2 78 expect(numberOfExitEvents).toBe(min)
ef3891a3 79 expect(poolDestroy).toBe(1)
506c2a14 80 })
81
8d3782fa 82 it('Validation of inputs test', () => {
948faff7 83 expect(() => new DynamicClusterPool(min)).toThrow(
c3719753 84 'The worker file path must be specified'
8d3782fa
JB
85 )
86 })
87
506c2a14 88 it('Should work even without opts in input', async () => {
0fe39c97 89 const pool = new DynamicClusterPool(
e1ffb94f
JB
90 min,
91 max,
d35e5717 92 './tests/worker-files/cluster/testWorker.cjs'
325f50bc 93 )
0fe39c97 94 const result = await pool.execute()
30b963d4 95 expect(result).toStrictEqual({ ok: 1 })
8bc77620 96 // We need to clean up the resources after our test
0fe39c97 97 await pool.destroy()
506c2a14 98 })
e826bd34 99
1c6fe997 100 it('Verify scale processes up and down is working when long executing task is used:hard', async () => {
c01733f1 101 const longRunningPool = new DynamicClusterPool(
102 min,
103 max,
d35e5717 104 './tests/worker-files/cluster/longRunningWorkerHardBehavior.cjs',
292ad316 105 {
041dc05b 106 errorHandler: e => console.error(e),
73bfd59d
JB
107 onlineHandler: () => console.info('long executing worker is online'),
108 exitHandler: () => console.info('long executing worker exited')
292ad316 109 }
4c35177b 110 )
f06e48d8 111 expect(longRunningPool.workerNodes.length).toBe(min)
e211bc18 112 for (let i = 0; i < max * 2; i++) {
6db75ad9 113 longRunningPool.execute()
4c35177b 114 }
f06e48d8 115 expect(longRunningPool.workerNodes.length).toBe(max)
bac873bd 116 await waitWorkerEvents(longRunningPool, 'exit', max - min)
f06e48d8 117 expect(longRunningPool.workerNodes.length).toBe(min)
d710242d
JB
118 expect(
119 longRunningPool.workerChoiceStrategyContext.workerChoiceStrategies.get(
120 longRunningPool.workerChoiceStrategyContext.workerChoiceStrategy
9b106837 121 ).nextWorkerNodeKey
d710242d 122 ).toBeLessThan(longRunningPool.workerNodes.length)
8bc77620
APA
123 // We need to clean up the resources after our test
124 await longRunningPool.destroy()
4c35177b 125 })
126
1c6fe997 127 it('Verify scale processes up and down is working when long executing task is used:soft', async () => {
4c35177b 128 const longRunningPool = new DynamicClusterPool(
129 min,
130 max,
d35e5717 131 './tests/worker-files/cluster/longRunningWorkerSoftBehavior.cjs',
292ad316 132 {
041dc05b 133 errorHandler: e => console.error(e),
73bfd59d
JB
134 onlineHandler: () => console.info('long executing worker is online'),
135 exitHandler: () => console.info('long executing worker exited')
292ad316 136 }
c01733f1 137 )
f06e48d8 138 expect(longRunningPool.workerNodes.length).toBe(min)
e211bc18 139 for (let i = 0; i < max * 2; i++) {
6db75ad9 140 longRunningPool.execute()
c01733f1 141 }
f06e48d8 142 expect(longRunningPool.workerNodes.length).toBe(max)
920278a2 143 await sleep(1000)
1c6fe997 144 // Here we expect the workerNodes to be at the max size since the task is still executing
f06e48d8 145 expect(longRunningPool.workerNodes.length).toBe(max)
8bc77620
APA
146 // We need to clean up the resources after our test
147 await longRunningPool.destroy()
c01733f1 148 })
8d3782fa
JB
149
150 it('Verify that a pool with zero worker can be instantiated', async () => {
151 const pool = new DynamicClusterPool(
152 0,
153 max,
d35e5717 154 './tests/worker-files/cluster/testWorker.cjs'
8d3782fa
JB
155 )
156 expect(pool).toBeInstanceOf(DynamicClusterPool)
157 // We need to clean up the resources after our test
158 await pool.destroy()
159 })
e44639e9 160
6d7beb8c 161 it('Verify that a pool with zero worker works', async () => {
8e8d9101 162 for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) {
6d7beb8c
JB
163 const pool = new DynamicClusterPool(
164 0,
165 max,
166 './tests/worker-files/cluster/testWorker.cjs',
167 {
168 workerChoiceStrategy
169 }
170 )
171 expect(pool.starting).toBe(false)
8e8d9101
JB
172 expect(pool.readyEventEmitted).toBe(false)
173 for (let run = 0; run < 2; run++) {
174 run % 2 !== 0 && pool.enableTasksQueue(true)
175 const maxMultiplier = 4
176 const promises = new Set()
177 expect(pool.workerNodes.length).toBe(pool.info.minSize)
178 for (let i = 0; i < max * maxMultiplier; i++) {
179 promises.add(pool.execute())
180 }
181 await Promise.all(promises)
182 expect(pool.readyEventEmitted).toBe(true)
183 expect(pool.workerNodes.length).toBeGreaterThan(pool.info.minSize)
184 expect(pool.workerNodes.length).toBeLessThanOrEqual(pool.info.maxSize)
185 await waitPoolEvents(pool, PoolEvents.empty, 1)
186 expect(pool.readyEventEmitted).toBe(false)
187 expect(pool.workerNodes.length).toBe(pool.info.minSize)
28881126 188 }
6d7beb8c
JB
189 // We need to clean up the resources after our test
190 await pool.destroy()
a2283a19 191 }
e44639e9 192 })
506c2a14 193})