Better handling for maxInactiveTime on dynamic pools
[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,
8 './tests/worker/cluster/testWorker.js',
9 {
10 errorHandler: e => console.error(e),
11 onlineHandler: () => console.log('worker is online')
12 }
13)
506c2a14 14
325f50bc
S
15describe('Dynamic cluster pool test suite ', () => {
16 it('Verify that the function is executed in a worker cluster', async () => {
506c2a14 17 const result = await pool.execute({ test: 'test' })
18 expect(result).toBeDefined()
19 expect(result).toBeFalsy()
20 })
21
22 it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
23 const promises = []
325f50bc 24 let closedWorkers = 0
b755d5be 25 let fullPool = 0
26 pool.emitter.on('FullPool', () => fullPool++)
cf9aa6c3 27 for (let i = 0; i < max * 2; i++) {
506c2a14 28 promises.push(pool.execute({ test: 'test' }))
29 }
325f50bc
S
30 expect(pool.workers.length).toBeLessThanOrEqual(max)
31 expect(pool.workers.length).toBeGreaterThan(min)
506c2a14 32 pool.workers.forEach(w => {
33 w.on('exit', () => {
325f50bc 34 closedWorkers++
506c2a14 35 })
36 })
b755d5be 37 expect(fullPool > 1).toBeTruthy()
325f50bc
S
38 await new Promise(resolve => setTimeout(resolve, 5000))
39 expect(closedWorkers).toBe(max - min)
506c2a14 40 })
41
325f50bc 42 it('Verify scale worker up and down is working', async () => {
bcf04003 43 expect(pool.workers.length).toBe(min)
44 for (let i = 0; i < max * 10; i++) {
45 pool.execute({ test: 'test' })
46 }
325f50bc
S
47 expect(pool.workers.length).toBeGreaterThan(min)
48 await new Promise(resolve => setTimeout(resolve, 3000))
bcf04003 49 expect(pool.workers.length).toBe(min)
50 for (let i = 0; i < max * 10; i++) {
51 pool.execute({ test: 'test' })
52 }
325f50bc
S
53 expect(pool.workers.length).toBeGreaterThan(min)
54 await new Promise(resolve => setTimeout(resolve, 2000))
bcf04003 55 expect(pool.workers.length).toBe(min)
56 })
506c2a14 57 it('Shutdown test', async () => {
325f50bc 58 let closedWorkers = 0
506c2a14 59 pool.workers.forEach(w => {
60 w.on('exit', () => {
325f50bc 61 closedWorkers++
506c2a14 62 })
63 })
325f50bc
S
64 pool.destroy()
65 await new Promise(resolve => setTimeout(resolve, 1000))
66 expect(closedWorkers).toBe(min)
506c2a14 67 })
68
69 it('Validations test', () => {
70 let error
71 try {
325f50bc 72 const pool1 = new DynamicClusterPool()
506c2a14 73 console.log(pool1)
74 } catch (e) {
75 error = e
76 }
77 expect(error).toBeTruthy()
78 expect(error.message).toBeTruthy()
79 })
80
81 it('Should work even without opts in input', async () => {
325f50bc
S
82 const pool1 = new DynamicClusterPool(
83 1,
84 1,
85 './tests/worker/cluster/testWorker.js'
86 )
506c2a14 87 const res = await pool1.execute({ test: 'test' })
88 expect(res).toBeFalsy()
89 })
c01733f1 90 it('Verify scale processes up and down is working when long running task is used', async () => {
91 const longRunningPool = new DynamicClusterPool(
92 min,
93 max,
94 './tests/worker/thread/longRunningWorker.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, 1000))
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(max)
104 })
506c2a14 105})