Commit | Line | Data |
---|---|---|
325f50bc S |
1 | const expect = require('expect') |
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 | } | |
8620fb25 | 77 | expect(poolBusy).toBe(numberOfWorkers) |
7c0ba920 JB |
78 | }) |
79 | ||
325f50bc S |
80 | it('Verify that is possible to have a worker that return undefined', async () => { |
81 | const result = await emptyPool.execute() | |
82 | expect(result).toBeFalsy() | |
83 | }) | |
84 | ||
85 | it('Verify that data are sent to the worker correctly', async () => { | |
86 | const data = { f: 10 } | |
87 | const result = await echoPool.execute(data) | |
88 | expect(result).toBeTruthy() | |
89 | expect(result.f).toBe(data.f) | |
90 | }) | |
91 | ||
92 | it('Verify that error handling is working properly:sync', async () => { | |
93 | const data = { f: 10 } | |
94 | let inError | |
95 | try { | |
96 | await errorPool.execute(data) | |
97 | } catch (e) { | |
98 | inError = e | |
99 | } | |
100 | expect(inError).toBeDefined() | |
8620fb25 | 101 | expect(typeof inError === 'string').toBe(true) |
325f50bc S |
102 | expect(inError).toBe('Error Message from ClusterWorker') |
103 | }) | |
104 | ||
105 | it('Verify that error handling is working properly:async', async () => { | |
106 | const data = { f: 10 } | |
107 | let inError | |
108 | try { | |
109 | await asyncErrorPool.execute(data) | |
110 | } catch (e) { | |
111 | inError = e | |
112 | } | |
113 | expect(inError).toBeDefined() | |
8620fb25 | 114 | expect(typeof inError === 'string').toBe(true) |
325f50bc S |
115 | expect(inError).toBe('Error Message from ClusterWorker:async') |
116 | }) | |
117 | ||
118 | it('Verify that async function is working properly', async () => { | |
119 | const data = { f: 10 } | |
120 | const startTime = new Date().getTime() | |
121 | const result = await asyncPool.execute(data) | |
122 | const usedTime = new Date().getTime() - startTime | |
123 | expect(result).toBeTruthy() | |
124 | expect(result.f).toBe(data.f) | |
125 | expect(usedTime).toBeGreaterThanOrEqual(2000) | |
126 | }) | |
127 | ||
128 | it('Shutdown test', async () => { | |
85a3f8a7 | 129 | const exitPromise = TestUtils.waitExits(pool, numberOfWorkers) |
45dbbb14 | 130 | await pool.destroy() |
85a3f8a7 APA |
131 | const res = await exitPromise |
132 | expect(res).toBe(numberOfWorkers) | |
325f50bc S |
133 | }) |
134 | ||
135 | it('Should work even without opts in input', async () => { | |
136 | const pool1 = new FixedClusterPool( | |
137 | 1, | |
76b1e974 | 138 | './tests/worker-files/cluster/testWorker.js' |
325f50bc S |
139 | ) |
140 | const res = await pool1.execute({ test: 'test' }) | |
141 | expect(res).toBeFalsy() | |
8bc77620 APA |
142 | // We need to clean up the resources after our test |
143 | await pool1.destroy() | |
325f50bc | 144 | }) |
8d3782fa JB |
145 | |
146 | it('Verify that a pool with zero worker fails', async () => { | |
147 | expect( | |
148 | () => | |
149 | new FixedClusterPool(0, './tests/worker-files/cluster/testWorker.js') | |
150 | ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker')) | |
151 | }) | |
325f50bc | 152 | }) |