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