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