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