Fix tests with WRR worker choice strategy
[poolifier.git] / tests / pools / cluster / dynamic.test.js
CommitLineData
a61a0724 1const { expect } = require('expect')
325f50bc 2const { DynamicClusterPool } = require('../../../lib/index')
6db75ad9 3const WorkerFunctions = require('../../test-types')
85a3f8a7 4const TestUtils = require('../../test-utils')
506c2a14 5const min = 1
c719859c 6const max = 3
325f50bc
S
7const pool = new DynamicClusterPool(
8 min,
9 max,
76b1e974 10 './tests/worker-files/cluster/testWorker.js',
325f50bc 11 {
e5177d86 12 errorHandler: e => console.error(e)
325f50bc
S
13 }
14)
506c2a14 15
a35560ba 16describe('Dynamic cluster pool test suite', () => {
325f50bc 17 it('Verify that the function is executed in a worker cluster', async () => {
6db75ad9
JB
18 let result = await pool.execute({
19 function: WorkerFunctions.fibonacci
20 })
21 expect(result).toBe(false)
22 result = await pool.execute({
23 function: WorkerFunctions.factorial
24 })
25 expect(result).toBe(false)
506c2a14 26 })
27
28 it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
29 const promises = []
7c0ba920
JB
30 let poolBusy = 0
31 pool.emitter.on('busy', () => poolBusy++)
cf9aa6c3 32 for (let i = 0; i < max * 2; i++) {
6db75ad9 33 promises.push(pool.execute())
506c2a14 34 }
325f50bc
S
35 expect(pool.workers.length).toBeLessThanOrEqual(max)
36 expect(pool.workers.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)
85a3f8a7
APA
40 const numberOfExitEvents = await TestUtils.waitExits(pool, max - min)
41 expect(numberOfExitEvents).toBe(max - min)
506c2a14 42 })
43
325f50bc 44 it('Verify scale worker up and down is working', async () => {
bcf04003 45 expect(pool.workers.length).toBe(min)
46 for (let i = 0; i < max * 10; i++) {
6db75ad9 47 pool.execute()
bcf04003 48 }
325f50bc 49 expect(pool.workers.length).toBeGreaterThan(min)
85a3f8a7 50 await TestUtils.waitExits(pool, max - min)
bcf04003 51 expect(pool.workers.length).toBe(min)
52 for (let i = 0; i < max * 10; i++) {
6db75ad9 53 pool.execute()
bcf04003 54 }
325f50bc 55 expect(pool.workers.length).toBeGreaterThan(min)
85a3f8a7 56 await TestUtils.waitExits(pool, max - min)
bcf04003 57 expect(pool.workers.length).toBe(min)
58 })
506c2a14 59
85a3f8a7
APA
60 it('Shutdown test', async () => {
61 const exitPromise = TestUtils.waitExits(pool, min)
62 await pool.destroy()
bdacc2d2
JB
63 const numberOfExitEvents = await exitPromise
64 expect(numberOfExitEvents).toBe(min)
506c2a14 65 })
66
8d3782fa
JB
67 it('Validation of inputs test', () => {
68 expect(() => new DynamicClusterPool(min)).toThrowError(
69 new Error('Please specify a file with a worker implementation')
70 )
71 })
72
506c2a14 73 it('Should work even without opts in input', async () => {
325f50bc
S
74 const pool1 = new DynamicClusterPool(
75 1,
76 1,
76b1e974 77 './tests/worker-files/cluster/testWorker.js'
325f50bc 78 )
6db75ad9
JB
79 const result = await pool1.execute()
80 expect(result).toBe(false)
8bc77620
APA
81 // We need to clean up the resources after our test
82 await pool1.destroy()
506c2a14 83 })
e826bd34 84
4c35177b 85 it('Verify scale processes up and down is working when long running task is used:hard', async () => {
c01733f1 86 const longRunningPool = new DynamicClusterPool(
87 min,
88 max,
292ad316
JB
89 './tests/worker-files/cluster/longRunningWorkerHardBehavior.js',
90 {
91 errorHandler: e => console.error(e),
92 onlineHandler: () => console.log('long running worker is online'),
93 exitHandler: () => console.log('long running worker exited')
94 }
4c35177b 95 )
96 expect(longRunningPool.workers.length).toBe(min)
97 for (let i = 0; i < max * 10; i++) {
6db75ad9 98 longRunningPool.execute()
4c35177b 99 }
100 expect(longRunningPool.workers.length).toBe(max)
85a3f8a7 101 await TestUtils.waitExits(longRunningPool, max - min)
4c35177b 102 // Here we expect the workers to be at the max size since that the task is still running
103 expect(longRunningPool.workers.length).toBe(min)
8bc77620
APA
104 // We need to clean up the resources after our test
105 await longRunningPool.destroy()
4c35177b 106 })
107
108 it('Verify scale processes up and down is working when long running task is used:soft', async () => {
109 const longRunningPool = new DynamicClusterPool(
110 min,
111 max,
292ad316
JB
112 './tests/worker-files/cluster/longRunningWorkerSoftBehavior.js',
113 {
114 errorHandler: e => console.error(e),
115 onlineHandler: () => console.log('long running worker is online'),
116 exitHandler: () => console.log('long running worker exited')
117 }
c01733f1 118 )
119 expect(longRunningPool.workers.length).toBe(min)
120 for (let i = 0; i < max * 10; i++) {
6db75ad9 121 longRunningPool.execute()
c01733f1 122 }
123 expect(longRunningPool.workers.length).toBe(max)
85a3f8a7 124 await TestUtils.sleep(1500)
c01733f1 125 // Here we expect the workers to be at the max size since that the task is still running
126 expect(longRunningPool.workers.length).toBe(max)
8bc77620
APA
127 // We need to clean up the resources after our test
128 await longRunningPool.destroy()
c01733f1 129 })
8d3782fa
JB
130
131 it('Verify that a pool with zero worker can be instantiated', async () => {
132 const pool = new DynamicClusterPool(
133 0,
134 max,
135 './tests/worker-files/cluster/testWorker.js'
136 )
137 expect(pool).toBeInstanceOf(DynamicClusterPool)
138 // We need to clean up the resources after our test
139 await pool.destroy()
140 })
506c2a14 141})