Merge pull request #161 from pioardi/issue-70
[poolifier.git] / tests / pools / cluster / dynamic.test.js
1 const expect = require('expect')
2 const { DynamicClusterPool } = require('../../../lib/index')
3 const min = 1
4 const max = 3
5 const pool = new DynamicClusterPool(
6 min,
7 max,
8 './tests/worker/cluster/testWorker.js',
9 {
10 errorHandler: e => console.error(e)
11 }
12 )
13
14 describe('Dynamic cluster pool test suite ', () => {
15 it('Verify that the function is executed in a worker cluster', async () => {
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 = []
23 let closedWorkers = 0
24 let fullPool = 0
25 pool.emitter.on('FullPool', () => fullPool++)
26 for (let i = 0; i < max * 2; i++) {
27 promises.push(pool.execute({ test: 'test' }))
28 }
29 expect(pool.workers.length).toBeLessThanOrEqual(max)
30 expect(pool.workers.length).toBeGreaterThan(min)
31 pool.workers.forEach(w => {
32 w.on('exit', () => {
33 closedWorkers++
34 })
35 })
36 expect(fullPool > 1).toBeTruthy()
37 await new Promise(resolve => setTimeout(resolve, 5000))
38 expect(closedWorkers).toBe(max - min)
39 })
40
41 it('Verify scale worker up and down is working', async () => {
42 expect(pool.workers.length).toBe(min)
43 for (let i = 0; i < max * 10; i++) {
44 pool.execute({ test: 'test' })
45 }
46 expect(pool.workers.length).toBeGreaterThan(min)
47 await new Promise(resolve => setTimeout(resolve, 3000))
48 expect(pool.workers.length).toBe(min)
49 for (let i = 0; i < max * 10; i++) {
50 pool.execute({ test: 'test' })
51 }
52 expect(pool.workers.length).toBeGreaterThan(min)
53 await new Promise(resolve => setTimeout(resolve, 3000))
54 expect(pool.workers.length).toBe(min)
55 })
56 it('Shutdown test', async () => {
57 let closedWorkers = 0
58 pool.workers.forEach(w => {
59 w.on('exit', () => {
60 closedWorkers++
61 })
62 })
63 pool.destroy()
64 await new Promise(resolve => setTimeout(resolve, 2000))
65 expect(closedWorkers).toBe(min)
66 })
67
68 it('Validations test', () => {
69 let error
70 try {
71 const pool1 = new DynamicClusterPool()
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 () => {
81 const pool1 = new DynamicClusterPool(
82 1,
83 1,
84 './tests/worker/cluster/testWorker.js'
85 )
86 const res = await pool1.execute({ test: 'test' })
87 expect(res).toBeFalsy()
88 })
89
90 it('Verify scale processes up and down is working when long running task is used:hard', async () => {
91 const longRunningPool = new DynamicClusterPool(
92 min,
93 max,
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'
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)
117 await new Promise(resolve => setTimeout(resolve, 3000))
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 })
121 })