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