Commit | Line | Data |
---|---|---|
506c2a14 | 1 | const expect = require('expect') |
325f50bc | 2 | const { DynamicClusterPool } = require('../../../lib/index') |
85a3f8a7 | 3 | const TestUtils = require('../../test-utils') |
506c2a14 | 4 | const min = 1 |
c719859c | 5 | const max = 3 |
325f50bc S |
6 | const pool = new DynamicClusterPool( |
7 | min, | |
8 | max, | |
76b1e974 | 9 | './tests/worker-files/cluster/testWorker.js', |
325f50bc | 10 | { |
e5177d86 | 11 | errorHandler: e => console.error(e) |
325f50bc S |
12 | } |
13 | ) | |
506c2a14 | 14 | |
a35560ba | 15 | describe('Dynamic cluster pool test suite', () => { |
325f50bc | 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 = [] | |
7c0ba920 JB |
24 | let poolBusy = 0 |
25 | pool.emitter.on('busy', () => poolBusy++) | |
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) | |
8620fb25 | 31 | expect(poolBusy).toBe(max + 1) |
85a3f8a7 APA |
32 | const numberOfExitEvents = await TestUtils.waitExits(pool, max - min) |
33 | expect(numberOfExitEvents).toBe(max - min) | |
506c2a14 | 34 | }) |
35 | ||
325f50bc | 36 | it('Verify scale worker up and down is working', async () => { |
bcf04003 | 37 | expect(pool.workers.length).toBe(min) |
38 | for (let i = 0; i < max * 10; i++) { | |
39 | pool.execute({ test: 'test' }) | |
40 | } | |
325f50bc | 41 | expect(pool.workers.length).toBeGreaterThan(min) |
85a3f8a7 | 42 | await TestUtils.waitExits(pool, max - min) |
bcf04003 | 43 | expect(pool.workers.length).toBe(min) |
44 | for (let i = 0; i < max * 10; i++) { | |
45 | pool.execute({ test: 'test' }) | |
46 | } | |
325f50bc | 47 | expect(pool.workers.length).toBeGreaterThan(min) |
85a3f8a7 | 48 | await TestUtils.waitExits(pool, max - min) |
bcf04003 | 49 | expect(pool.workers.length).toBe(min) |
50 | }) | |
506c2a14 | 51 | |
85a3f8a7 APA |
52 | it('Shutdown test', async () => { |
53 | const exitPromise = TestUtils.waitExits(pool, min) | |
54 | await pool.destroy() | |
55 | const res = await exitPromise | |
56 | expect(res).toBe(min) | |
506c2a14 | 57 | }) |
58 | ||
8d3782fa JB |
59 | it('Validation of inputs test', () => { |
60 | expect(() => new DynamicClusterPool(min)).toThrowError( | |
61 | new Error('Please specify a file with a worker implementation') | |
62 | ) | |
63 | }) | |
64 | ||
506c2a14 | 65 | it('Should work even without opts in input', async () => { |
325f50bc S |
66 | const pool1 = new DynamicClusterPool( |
67 | 1, | |
68 | 1, | |
76b1e974 | 69 | './tests/worker-files/cluster/testWorker.js' |
325f50bc | 70 | ) |
8bc77620 | 71 | const result = await pool1.execute({ test: 'test' }) |
8620fb25 | 72 | expect(result).toBeDefined() |
8bc77620 APA |
73 | expect(result).toBeFalsy() |
74 | // We need to clean up the resources after our test | |
75 | await pool1.destroy() | |
506c2a14 | 76 | }) |
e826bd34 | 77 | |
4c35177b | 78 | it('Verify scale processes up and down is working when long running task is used:hard', async () => { |
c01733f1 | 79 | const longRunningPool = new DynamicClusterPool( |
80 | min, | |
81 | max, | |
292ad316 JB |
82 | './tests/worker-files/cluster/longRunningWorkerHardBehavior.js', |
83 | { | |
84 | errorHandler: e => console.error(e), | |
85 | onlineHandler: () => console.log('long running worker is online'), | |
86 | exitHandler: () => console.log('long running worker exited') | |
87 | } | |
4c35177b | 88 | ) |
89 | expect(longRunningPool.workers.length).toBe(min) | |
90 | for (let i = 0; i < max * 10; i++) { | |
91 | longRunningPool.execute({ test: 'test' }) | |
92 | } | |
93 | expect(longRunningPool.workers.length).toBe(max) | |
85a3f8a7 | 94 | await TestUtils.waitExits(longRunningPool, max - min) |
4c35177b | 95 | // Here we expect the workers to be at the max size since that the task is still running |
96 | expect(longRunningPool.workers.length).toBe(min) | |
8bc77620 APA |
97 | // We need to clean up the resources after our test |
98 | await longRunningPool.destroy() | |
4c35177b | 99 | }) |
100 | ||
101 | it('Verify scale processes up and down is working when long running task is used:soft', async () => { | |
102 | const longRunningPool = new DynamicClusterPool( | |
103 | min, | |
104 | max, | |
292ad316 JB |
105 | './tests/worker-files/cluster/longRunningWorkerSoftBehavior.js', |
106 | { | |
107 | errorHandler: e => console.error(e), | |
108 | onlineHandler: () => console.log('long running worker is online'), | |
109 | exitHandler: () => console.log('long running worker exited') | |
110 | } | |
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) | |
85a3f8a7 | 117 | await TestUtils.sleep(1500) |
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) | |
8bc77620 APA |
120 | // We need to clean up the resources after our test |
121 | await longRunningPool.destroy() | |
c01733f1 | 122 | }) |
8d3782fa JB |
123 | |
124 | it('Verify that a pool with zero worker can be instantiated', async () => { | |
125 | const pool = new DynamicClusterPool( | |
126 | 0, | |
127 | max, | |
128 | './tests/worker-files/cluster/testWorker.js' | |
129 | ) | |
130 | expect(pool).toBeInstanceOf(DynamicClusterPool) | |
131 | // We need to clean up the resources after our test | |
132 | await pool.destroy() | |
133 | }) | |
506c2a14 | 134 | }) |