Commit | Line | Data |
---|---|---|
506c2a14 | 1 | const expect = require('expect') |
325f50bc | 2 | const { DynamicClusterPool } = require('../../../lib/index') |
506c2a14 | 3 | const min = 1 |
c719859c | 4 | const max = 3 |
325f50bc S |
5 | const pool = new DynamicClusterPool( |
6 | min, | |
7 | max, | |
8 | './tests/worker/cluster/testWorker.js', | |
9 | { | |
e5177d86 | 10 | errorHandler: e => console.error(e) |
325f50bc S |
11 | } |
12 | ) | |
506c2a14 | 13 | |
325f50bc S |
14 | describe('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, | |
84 | './tests/worker/cluster/testWorker.js' | |
85 | ) | |
506c2a14 | 86 | const res = await pool1.execute({ test: 'test' }) |
87 | expect(res).toBeFalsy() | |
88 | }) | |
c01733f1 | 89 | it('Verify scale processes up and down is working when long running task is used', async () => { |
90 | const longRunningPool = new DynamicClusterPool( | |
91 | min, | |
92 | max, | |
e5177d86 | 93 | './tests/worker/cluster/longRunningWorker.js' |
c01733f1 | 94 | ) |
95 | expect(longRunningPool.workers.length).toBe(min) | |
96 | for (let i = 0; i < max * 10; i++) { | |
97 | longRunningPool.execute({ test: 'test' }) | |
98 | } | |
99 | expect(longRunningPool.workers.length).toBe(max) | |
e5177d86 | 100 | await new Promise(resolve => setTimeout(resolve, 3000)) |
c01733f1 | 101 | // Here we expect the workers to be at the max size since that the task is still running |
102 | expect(longRunningPool.workers.length).toBe(max) | |
103 | }) | |
506c2a14 | 104 | }) |