test: improve INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy test
[poolifier.git] / tests / pools / cluster / dynamic.test.js
CommitLineData
a61a0724 1const { expect } = require('expect')
cdace0e5 2const { DynamicClusterPool, PoolEvents } = require('../../../lib')
dbca3be9 3const { TaskFunctions } = require('../../test-types')
bac873bd 4const { sleep, waitWorkerEvents } = require('../../test-utils')
506c2a14 5
a35560ba 6describe('Dynamic cluster pool test suite', () => {
e1ffb94f
JB
7 const min = 1
8 const max = 3
9 const pool = new DynamicClusterPool(
10 min,
11 max,
12 './tests/worker-files/cluster/testWorker.js',
13 {
8ebe6c30 14 errorHandler: (e) => console.error(e)
e1ffb94f
JB
15 }
16 )
17
325f50bc 18 it('Verify that the function is executed in a worker cluster', async () => {
6db75ad9 19 let result = await pool.execute({
dbca3be9 20 function: TaskFunctions.fibonacci
6db75ad9 21 })
024daf59 22 expect(result).toBe(75025)
6db75ad9 23 result = await pool.execute({
dbca3be9 24 function: TaskFunctions.factorial
6db75ad9 25 })
70a4f5ea 26 expect(result).toBe(9.33262154439441e157)
506c2a14 27 })
28
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 () => {
7c0ba920 30 let poolBusy = 0
aee46736 31 pool.emitter.on(PoolEvents.busy, () => ++poolBusy)
cf9aa6c3 32 for (let i = 0; i < max * 2; i++) {
8cbb82eb 33 pool.execute()
506c2a14 34 }
f06e48d8
JB
35 expect(pool.workerNodes.length).toBeLessThanOrEqual(max)
36 expect(pool.workerNodes.length).toBeGreaterThan(min)
14916bf9
JB
37 // The `busy` event is triggered when the number of submitted tasks at once reach the max number of workers in the dynamic pool.
38 // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool.
8620fb25 39 expect(poolBusy).toBe(max + 1)
bac873bd 40 const numberOfExitEvents = await waitWorkerEvents(pool, 'exit', max - min)
85a3f8a7 41 expect(numberOfExitEvents).toBe(max - min)
506c2a14 42 })
43
325f50bc 44 it('Verify scale worker up and down is working', async () => {
f06e48d8 45 expect(pool.workerNodes.length).toBe(min)
e211bc18 46 for (let i = 0; i < max * 2; i++) {
6db75ad9 47 pool.execute()
bcf04003 48 }
f06e48d8 49 expect(pool.workerNodes.length).toBeGreaterThan(min)
bac873bd 50 await waitWorkerEvents(pool, 'exit', max - min)
f06e48d8 51 expect(pool.workerNodes.length).toBe(min)
e211bc18 52 for (let i = 0; i < max * 2; i++) {
6db75ad9 53 pool.execute()
bcf04003 54 }
f06e48d8 55 expect(pool.workerNodes.length).toBeGreaterThan(min)
bac873bd 56 await waitWorkerEvents(pool, 'exit', max - min)
f06e48d8 57 expect(pool.workerNodes.length).toBe(min)
bcf04003 58 })
506c2a14 59
85a3f8a7 60 it('Shutdown test', async () => {
bac873bd 61 const exitPromise = waitWorkerEvents(pool, 'exit', min)
ef3891a3
JB
62 let poolDestroy = 0
63 pool.emitter.on(PoolEvents.destroy, () => ++poolDestroy)
85a3f8a7 64 await pool.destroy()
bdacc2d2
JB
65 const numberOfExitEvents = await exitPromise
66 expect(numberOfExitEvents).toBe(min)
ef3891a3 67 expect(poolDestroy).toBe(1)
506c2a14 68 })
69
8d3782fa
JB
70 it('Validation of inputs test', () => {
71 expect(() => new DynamicClusterPool(min)).toThrowError(
d4aeae5a 72 'Please specify a file with a worker implementation'
8d3782fa
JB
73 )
74 })
75
506c2a14 76 it('Should work even without opts in input', async () => {
325f50bc 77 const pool1 = new DynamicClusterPool(
e1ffb94f
JB
78 min,
79 max,
76b1e974 80 './tests/worker-files/cluster/testWorker.js'
325f50bc 81 )
6db75ad9 82 const result = await pool1.execute()
30b963d4 83 expect(result).toStrictEqual({ ok: 1 })
8bc77620
APA
84 // We need to clean up the resources after our test
85 await pool1.destroy()
506c2a14 86 })
e826bd34 87
1c6fe997 88 it('Verify scale processes up and down is working when long executing task is used:hard', async () => {
c01733f1 89 const longRunningPool = new DynamicClusterPool(
90 min,
91 max,
292ad316
JB
92 './tests/worker-files/cluster/longRunningWorkerHardBehavior.js',
93 {
8ebe6c30 94 errorHandler: (e) => console.error(e),
73bfd59d
JB
95 onlineHandler: () => console.info('long executing worker is online'),
96 exitHandler: () => console.info('long executing worker exited')
292ad316 97 }
4c35177b 98 )
f06e48d8 99 expect(longRunningPool.workerNodes.length).toBe(min)
e211bc18 100 for (let i = 0; i < max * 2; i++) {
6db75ad9 101 longRunningPool.execute()
4c35177b 102 }
f06e48d8 103 expect(longRunningPool.workerNodes.length).toBe(max)
bac873bd 104 await waitWorkerEvents(longRunningPool, 'exit', max - min)
f06e48d8 105 expect(longRunningPool.workerNodes.length).toBe(min)
d710242d
JB
106 expect(
107 longRunningPool.workerChoiceStrategyContext.workerChoiceStrategies.get(
108 longRunningPool.workerChoiceStrategyContext.workerChoiceStrategy
9b106837 109 ).nextWorkerNodeKey
d710242d 110 ).toBeLessThan(longRunningPool.workerNodes.length)
8bc77620
APA
111 // We need to clean up the resources after our test
112 await longRunningPool.destroy()
4c35177b 113 })
114
1c6fe997 115 it('Verify scale processes up and down is working when long executing task is used:soft', async () => {
4c35177b 116 const longRunningPool = new DynamicClusterPool(
117 min,
118 max,
292ad316
JB
119 './tests/worker-files/cluster/longRunningWorkerSoftBehavior.js',
120 {
8ebe6c30 121 errorHandler: (e) => console.error(e),
73bfd59d
JB
122 onlineHandler: () => console.info('long executing worker is online'),
123 exitHandler: () => console.info('long executing worker exited')
292ad316 124 }
c01733f1 125 )
f06e48d8 126 expect(longRunningPool.workerNodes.length).toBe(min)
e211bc18 127 for (let i = 0; i < max * 2; i++) {
6db75ad9 128 longRunningPool.execute()
c01733f1 129 }
f06e48d8 130 expect(longRunningPool.workerNodes.length).toBe(max)
920278a2 131 await sleep(1000)
1c6fe997 132 // Here we expect the workerNodes to be at the max size since the task is still executing
f06e48d8 133 expect(longRunningPool.workerNodes.length).toBe(max)
8bc77620
APA
134 // We need to clean up the resources after our test
135 await longRunningPool.destroy()
c01733f1 136 })
8d3782fa
JB
137
138 it('Verify that a pool with zero worker can be instantiated', async () => {
139 const pool = new DynamicClusterPool(
140 0,
141 max,
142 './tests/worker-files/cluster/testWorker.js'
143 )
144 expect(pool).toBeInstanceOf(DynamicClusterPool)
145 // We need to clean up the resources after our test
146 await pool.destroy()
147 })
506c2a14 148})