Improve jsdoc comments (#175)
[poolifier.git] / tests / pools / cluster / dynamic.test.js
CommitLineData
506c2a14 1const expect = require('expect')
325f50bc 2const { DynamicClusterPool } = require('../../../lib/index')
506c2a14 3const min = 1
c719859c 4const max = 3
325f50bc
S
5const pool = new DynamicClusterPool(
6 min,
7 max,
76b1e974 8 './tests/worker-files/cluster/testWorker.js',
325f50bc 9 {
e5177d86 10 errorHandler: e => console.error(e)
325f50bc
S
11 }
12)
506c2a14 13
325f50bc
S
14describe('Dynamic cluster pool test suite ', () => {
15 it('Verify that the function is executed in a worker cluster', async () => {
506c2a14 16 const result = await pool.execute({ test: 'test' })
17 expect(result).toBeDefined()
18 expect(result).toBeFalsy()
19 })
20
21 it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
22 const promises = []
325f50bc 23 let closedWorkers = 0
b755d5be 24 let fullPool = 0
25 pool.emitter.on('FullPool', () => fullPool++)
cf9aa6c3 26 for (let i = 0; i < max * 2; i++) {
506c2a14 27 promises.push(pool.execute({ test: 'test' }))
28 }
325f50bc
S
29 expect(pool.workers.length).toBeLessThanOrEqual(max)
30 expect(pool.workers.length).toBeGreaterThan(min)
506c2a14 31 pool.workers.forEach(w => {
32 w.on('exit', () => {
325f50bc 33 closedWorkers++
506c2a14 34 })
35 })
b755d5be 36 expect(fullPool > 1).toBeTruthy()
325f50bc
S
37 await new Promise(resolve => setTimeout(resolve, 5000))
38 expect(closedWorkers).toBe(max - min)
506c2a14 39 })
40
325f50bc 41 it('Verify scale worker up and down is working', async () => {
bcf04003 42 expect(pool.workers.length).toBe(min)
43 for (let i = 0; i < max * 10; i++) {
44 pool.execute({ test: 'test' })
45 }
325f50bc
S
46 expect(pool.workers.length).toBeGreaterThan(min)
47 await new Promise(resolve => setTimeout(resolve, 3000))
bcf04003 48 expect(pool.workers.length).toBe(min)
49 for (let i = 0; i < max * 10; i++) {
50 pool.execute({ test: 'test' })
51 }
325f50bc 52 expect(pool.workers.length).toBeGreaterThan(min)
e5177d86 53 await new Promise(resolve => setTimeout(resolve, 3000))
bcf04003 54 expect(pool.workers.length).toBe(min)
55 })
506c2a14 56 it('Shutdown test', async () => {
325f50bc 57 let closedWorkers = 0
506c2a14 58 pool.workers.forEach(w => {
59 w.on('exit', () => {
325f50bc 60 closedWorkers++
506c2a14 61 })
62 })
325f50bc 63 pool.destroy()
e5177d86 64 await new Promise(resolve => setTimeout(resolve, 2000))
325f50bc 65 expect(closedWorkers).toBe(min)
506c2a14 66 })
67
68 it('Validations test', () => {
69 let error
70 try {
325f50bc 71 const pool1 = new DynamicClusterPool()
506c2a14 72 console.log(pool1)
73 } catch (e) {
74 error = e
75 }
76 expect(error).toBeTruthy()
77 expect(error.message).toBeTruthy()
78 })
79
80 it('Should work even without opts in input', async () => {
325f50bc
S
81 const pool1 = new DynamicClusterPool(
82 1,
83 1,
76b1e974 84 './tests/worker-files/cluster/testWorker.js'
325f50bc 85 )
506c2a14 86 const res = await pool1.execute({ test: 'test' })
87 expect(res).toBeFalsy()
88 })
e826bd34 89
4c35177b 90 it('Verify scale processes up and down is working when long running task is used:hard', async () => {
c01733f1 91 const longRunningPool = new DynamicClusterPool(
92 min,
93 max,
4c35177b 94 './tests/worker/cluster/longRunningWorkerHardBehavior.js'
95 )
96 expect(longRunningPool.workers.length).toBe(min)
97 for (let i = 0; i < max * 10; i++) {
98 longRunningPool.execute({ test: 'test' })
99 }
100 expect(longRunningPool.workers.length).toBe(max)
101 await new Promise(resolve => setTimeout(resolve, 3000))
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)
104 })
105
106 it('Verify scale processes up and down is working when long running task is used:soft', async () => {
107 const longRunningPool = new DynamicClusterPool(
108 min,
109 max,
110 './tests/worker/cluster/longRunningWorkerSoftBehavior.js'
c01733f1 111 )
112 expect(longRunningPool.workers.length).toBe(min)
113 for (let i = 0; i < max * 10; i++) {
114 longRunningPool.execute({ test: 'test' })
115 }
116 expect(longRunningPool.workers.length).toBe(max)
e5177d86 117 await new Promise(resolve => setTimeout(resolve, 3000))
c01733f1 118 // Here we expect the workers to be at the max size since that the task is still running
119 expect(longRunningPool.workers.length).toBe(max)
120 })
506c2a14 121})