Commit | Line | Data |
---|---|---|
a61a0724 | 1 | const { expect } = require('expect') |
325f50bc | 2 | const { FixedClusterPool } = require('../../../lib/index') |
85a3f8a7 | 3 | const TestUtils = require('../../test-utils') |
5c5a1fb7 | 4 | const numberOfWorkers = 10 |
325f50bc | 5 | const pool = new FixedClusterPool( |
5c5a1fb7 | 6 | numberOfWorkers, |
76b1e974 | 7 | './tests/worker-files/cluster/testWorker.js', |
325f50bc | 8 | { |
e5177d86 | 9 | errorHandler: e => console.error(e) |
325f50bc S |
10 | } |
11 | ) | |
12 | const emptyPool = new FixedClusterPool( | |
13 | 1, | |
7c0ba920 JB |
14 | './tests/worker-files/cluster/emptyWorker.js', |
15 | { exitHandler: () => console.log('empty pool worker exited') } | |
76b1e974 S |
16 | ) |
17 | const echoPool = new FixedClusterPool( | |
18 | 1, | |
19 | './tests/worker-files/cluster/echoWorker.js' | |
325f50bc | 20 | ) |
325f50bc S |
21 | const errorPool = new FixedClusterPool( |
22 | 1, | |
76b1e974 | 23 | './tests/worker-files/cluster/errorWorker.js', |
325f50bc | 24 | { |
e5177d86 | 25 | errorHandler: e => console.error(e) |
325f50bc S |
26 | } |
27 | ) | |
325f50bc S |
28 | const asyncErrorPool = new FixedClusterPool( |
29 | 1, | |
76b1e974 | 30 | './tests/worker-files/cluster/asyncErrorWorker.js', |
325f50bc | 31 | { |
292ad316 | 32 | errorHandler: e => console.error(e) |
325f50bc S |
33 | } |
34 | ) | |
35 | const asyncPool = new FixedClusterPool( | |
36 | 1, | |
1927ee67 | 37 | './tests/worker-files/cluster/asyncWorker.js' |
325f50bc S |
38 | ) |
39 | ||
a35560ba | 40 | describe('Fixed cluster pool test suite', () => { |
8bc77620 APA |
41 | after('Destroy all pools', async () => { |
42 | // We need to clean up the resources after our test | |
43 | await echoPool.destroy() | |
44 | await asyncPool.destroy() | |
45 | await errorPool.destroy() | |
46 | await asyncErrorPool.destroy() | |
47 | await emptyPool.destroy() | |
48 | }) | |
49 | ||
325f50bc S |
50 | it('Choose worker round robin test', async () => { |
51 | const results = new Set() | |
5c5a1fb7 | 52 | for (let i = 0; i < numberOfWorkers; i++) { |
325f50bc S |
53 | results.add(pool.chooseWorker().id) |
54 | } | |
5c5a1fb7 | 55 | expect(results.size).toBe(numberOfWorkers) |
325f50bc S |
56 | }) |
57 | ||
58 | it('Verify that the function is executed in a worker cluster', async () => { | |
59 | const result = await pool.execute({ test: 'test' }) | |
60 | expect(result).toBeDefined() | |
61 | expect(result).toBeFalsy() | |
62 | }) | |
63 | ||
64 | it('Verify that is possible to invoke the execute method without input', async () => { | |
65 | const result = await pool.execute() | |
66 | expect(result).toBeDefined() | |
67 | expect(result).toBeFalsy() | |
68 | }) | |
69 | ||
7c0ba920 JB |
70 | it('Verify that busy event is emitted', async () => { |
71 | const promises = [] | |
72 | let poolBusy = 0 | |
73 | pool.emitter.on('busy', () => poolBusy++) | |
74 | for (let i = 0; i < numberOfWorkers * 2; i++) { | |
75 | promises.push(pool.execute({ test: 'test' })) | |
76 | } | |
42f051a8 | 77 | await Promise.all(promises) |
14916bf9 JB |
78 | // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers. |
79 | // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool. | |
80 | expect(poolBusy).toBe(numberOfWorkers + 1) | |
7c0ba920 JB |
81 | }) |
82 | ||
325f50bc S |
83 | it('Verify that is possible to have a worker that return undefined', async () => { |
84 | const result = await emptyPool.execute() | |
85 | expect(result).toBeFalsy() | |
86 | }) | |
87 | ||
88 | it('Verify that data are sent to the worker correctly', async () => { | |
89 | const data = { f: 10 } | |
90 | const result = await echoPool.execute(data) | |
91 | expect(result).toBeTruthy() | |
92 | expect(result.f).toBe(data.f) | |
93 | }) | |
94 | ||
95 | it('Verify that error handling is working properly:sync', async () => { | |
96 | const data = { f: 10 } | |
97 | let inError | |
98 | try { | |
99 | await errorPool.execute(data) | |
100 | } catch (e) { | |
101 | inError = e | |
102 | } | |
103 | expect(inError).toBeDefined() | |
8620fb25 | 104 | expect(typeof inError === 'string').toBe(true) |
325f50bc S |
105 | expect(inError).toBe('Error Message from ClusterWorker') |
106 | }) | |
107 | ||
108 | it('Verify that error handling is working properly:async', async () => { | |
109 | const data = { f: 10 } | |
110 | let inError | |
111 | try { | |
112 | await asyncErrorPool.execute(data) | |
113 | } catch (e) { | |
114 | inError = e | |
115 | } | |
116 | expect(inError).toBeDefined() | |
8620fb25 | 117 | expect(typeof inError === 'string').toBe(true) |
325f50bc S |
118 | expect(inError).toBe('Error Message from ClusterWorker:async') |
119 | }) | |
120 | ||
121 | it('Verify that async function is working properly', async () => { | |
122 | const data = { f: 10 } | |
123 | const startTime = new Date().getTime() | |
124 | const result = await asyncPool.execute(data) | |
125 | const usedTime = new Date().getTime() - startTime | |
126 | expect(result).toBeTruthy() | |
127 | expect(result.f).toBe(data.f) | |
128 | expect(usedTime).toBeGreaterThanOrEqual(2000) | |
129 | }) | |
130 | ||
131 | it('Shutdown test', async () => { | |
85a3f8a7 | 132 | const exitPromise = TestUtils.waitExits(pool, numberOfWorkers) |
45dbbb14 | 133 | await pool.destroy() |
bdacc2d2 JB |
134 | const numberOfExitEvents = await exitPromise |
135 | expect(numberOfExitEvents).toBe(numberOfWorkers) | |
325f50bc S |
136 | }) |
137 | ||
138 | it('Should work even without opts in input', async () => { | |
139 | const pool1 = new FixedClusterPool( | |
140 | 1, | |
76b1e974 | 141 | './tests/worker-files/cluster/testWorker.js' |
325f50bc S |
142 | ) |
143 | const res = await pool1.execute({ test: 'test' }) | |
144 | expect(res).toBeFalsy() | |
8bc77620 APA |
145 | // We need to clean up the resources after our test |
146 | await pool1.destroy() | |
325f50bc | 147 | }) |
8d3782fa JB |
148 | |
149 | it('Verify that a pool with zero worker fails', async () => { | |
150 | expect( | |
151 | () => | |
152 | new FixedClusterPool(0, './tests/worker-files/cluster/testWorker.js') | |
153 | ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker')) | |
154 | }) | |
325f50bc | 155 | }) |