a5790db075f400b3ff4ff93dd6c0a01a3beb62fd
[poolifier.git] / tests / pools / cluster / fixed.test.js
1 const { expect } = require('expect')
2 const { FixedClusterPool, PoolEvents } = require('../../../lib')
3 const { TaskFunctions } = require('../../test-types')
4 const { waitPoolEvents, waitWorkerEvents } = require('../../test-utils')
5
6 describe('Fixed cluster pool test suite', () => {
7 const numberOfWorkers = 6
8 const tasksConcurrency = 2
9 const pool = new FixedClusterPool(
10 numberOfWorkers,
11 './tests/worker-files/cluster/testWorker.js',
12 {
13 errorHandler: (e) => console.error(e)
14 }
15 )
16 const queuePool = new FixedClusterPool(
17 numberOfWorkers,
18 './tests/worker-files/cluster/testWorker.js',
19 {
20 enableTasksQueue: true,
21 tasksQueueOptions: {
22 concurrency: tasksConcurrency
23 },
24 errorHandler: (e) => console.error(e)
25 }
26 )
27 const emptyPool = new FixedClusterPool(
28 numberOfWorkers,
29 './tests/worker-files/cluster/emptyWorker.js',
30 { exitHandler: () => console.info('empty pool worker exited') }
31 )
32 const echoPool = new FixedClusterPool(
33 numberOfWorkers,
34 './tests/worker-files/cluster/echoWorker.js'
35 )
36 const errorPool = new FixedClusterPool(
37 numberOfWorkers,
38 './tests/worker-files/cluster/errorWorker.js',
39 {
40 errorHandler: (e) => console.error(e)
41 }
42 )
43 const asyncErrorPool = new FixedClusterPool(
44 numberOfWorkers,
45 './tests/worker-files/cluster/asyncErrorWorker.js',
46 {
47 errorHandler: (e) => console.error(e)
48 }
49 )
50 const asyncPool = new FixedClusterPool(
51 numberOfWorkers,
52 './tests/worker-files/cluster/asyncWorker.js'
53 )
54
55 after('Destroy all pools', async () => {
56 // We need to clean up the resources after our test
57 await echoPool.destroy()
58 await asyncPool.destroy()
59 await errorPool.destroy()
60 await asyncErrorPool.destroy()
61 await emptyPool.destroy()
62 await queuePool.destroy()
63 })
64
65 it('Verify that the function is executed in a worker cluster', async () => {
66 let result = await pool.execute({
67 function: TaskFunctions.fibonacci
68 })
69 expect(result).toBe(75025)
70 result = await pool.execute({
71 function: TaskFunctions.factorial
72 })
73 expect(result).toBe(9.33262154439441e157)
74 })
75
76 it('Verify that is possible to invoke the execute() method without input', async () => {
77 const result = await pool.execute()
78 expect(result).toStrictEqual({ ok: 1 })
79 })
80
81 it("Verify that 'ready' event is emitted", async () => {
82 const pool1 = new FixedClusterPool(
83 numberOfWorkers,
84 './tests/worker-files/cluster/testWorker.js',
85 {
86 errorHandler: (e) => console.error(e)
87 }
88 )
89 let poolReady = 0
90 pool1.emitter.on(PoolEvents.ready, () => ++poolReady)
91 await waitPoolEvents(pool1, PoolEvents.ready, 1)
92 expect(poolReady).toBe(1)
93 })
94
95 it("Verify that 'busy' event is emitted", async () => {
96 const promises = new Set()
97 let poolBusy = 0
98 pool.emitter.on(PoolEvents.busy, () => ++poolBusy)
99 for (let i = 0; i < numberOfWorkers * 2; i++) {
100 promises.add(pool.execute())
101 }
102 await Promise.all(promises)
103 // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
104 // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool.
105 expect(poolBusy).toBe(numberOfWorkers + 1)
106 })
107
108 it('Verify that tasks queuing is working', async () => {
109 const promises = new Set()
110 const maxMultiplier = 3 // Must be greater than tasksConcurrency
111 for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) {
112 promises.add(queuePool.execute())
113 }
114 expect(promises.size).toBe(numberOfWorkers * maxMultiplier)
115 for (const workerNode of queuePool.workerNodes) {
116 expect(workerNode.usage.tasks.executing).toBeGreaterThanOrEqual(0)
117 expect(workerNode.usage.tasks.executing).toBeLessThanOrEqual(
118 queuePool.opts.tasksQueueOptions.concurrency
119 )
120 expect(workerNode.usage.tasks.executed).toBe(0)
121 expect(workerNode.usage.tasks.queued).toBe(
122 maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
123 )
124 expect(workerNode.usage.tasks.maxQueued).toBe(
125 maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
126 )
127 }
128 expect(queuePool.info.executingTasks).toBe(
129 numberOfWorkers * queuePool.opts.tasksQueueOptions.concurrency
130 )
131 expect(queuePool.info.queuedTasks).toBe(
132 numberOfWorkers *
133 (maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency)
134 )
135 expect(queuePool.info.maxQueuedTasks).toBe(
136 numberOfWorkers *
137 (maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency)
138 )
139 expect(queuePool.info.backPressure).toBe(false)
140 await Promise.all(promises)
141 for (const workerNode of queuePool.workerNodes) {
142 expect(workerNode.usage.tasks.executing).toBe(0)
143 expect(workerNode.usage.tasks.executed).toBe(maxMultiplier)
144 expect(workerNode.usage.tasks.queued).toBe(0)
145 expect(workerNode.usage.tasks.maxQueued).toBe(
146 maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
147 )
148 }
149 })
150
151 it('Verify that is possible to have a worker that return undefined', async () => {
152 const result = await emptyPool.execute()
153 expect(result).toBeUndefined()
154 })
155
156 it('Verify that data are sent to the worker correctly', async () => {
157 const data = { f: 10 }
158 const result = await echoPool.execute(data)
159 expect(result).toStrictEqual(data)
160 })
161
162 it('Verify that error handling is working properly:sync', async () => {
163 const data = { f: 10 }
164 let taskError
165 errorPool.emitter.on(PoolEvents.taskError, (e) => {
166 taskError = e
167 })
168 let inError
169 try {
170 await errorPool.execute(data)
171 } catch (e) {
172 inError = e
173 }
174 expect(inError).toBeDefined()
175 expect(typeof inError === 'string').toBe(true)
176 expect(inError).toBe('Error Message from ClusterWorker')
177 expect(taskError).toStrictEqual({
178 name: 'default',
179 message: 'Error Message from ClusterWorker',
180 data
181 })
182 expect(
183 errorPool.workerNodes.some(
184 (workerNode) => workerNode.usage.tasks.failed === 1
185 )
186 ).toBe(true)
187 })
188
189 it('Verify that error handling is working properly:async', async () => {
190 const data = { f: 10 }
191 let taskError
192 asyncErrorPool.emitter.on(PoolEvents.taskError, (e) => {
193 taskError = e
194 })
195 let inError
196 try {
197 await asyncErrorPool.execute(data)
198 } catch (e) {
199 inError = e
200 }
201 expect(inError).toBeDefined()
202 expect(typeof inError === 'string').toBe(true)
203 expect(inError).toBe('Error Message from ClusterWorker:async')
204 expect(taskError).toStrictEqual({
205 name: 'default',
206 message: 'Error Message from ClusterWorker:async',
207 data
208 })
209 expect(
210 asyncErrorPool.workerNodes.some(
211 (workerNode) => workerNode.usage.tasks.failed === 1
212 )
213 ).toBe(true)
214 })
215
216 it('Verify that async function is working properly', async () => {
217 const data = { f: 10 }
218 const startTime = performance.now()
219 const result = await asyncPool.execute(data)
220 const usedTime = performance.now() - startTime
221 expect(result).toStrictEqual(data)
222 expect(usedTime).toBeGreaterThanOrEqual(2000)
223 })
224
225 it('Shutdown test', async () => {
226 const exitPromise = waitWorkerEvents(pool, 'exit', numberOfWorkers)
227 let poolDestroy = 0
228 pool.emitter.on(PoolEvents.destroy, () => ++poolDestroy)
229 await pool.destroy()
230 const numberOfExitEvents = await exitPromise
231 expect(numberOfExitEvents).toBe(numberOfWorkers)
232 expect(poolDestroy).toBe(1)
233 })
234
235 it('Verify that cluster pool options are checked', async () => {
236 const workerFilePath = './tests/worker-files/cluster/testWorker.js'
237 let pool1 = new FixedClusterPool(numberOfWorkers, workerFilePath)
238 expect(pool1.opts.env).toBeUndefined()
239 expect(pool1.opts.settings).toBeUndefined()
240 await pool1.destroy()
241 pool1 = new FixedClusterPool(numberOfWorkers, workerFilePath, {
242 env: { TEST: 'test' },
243 settings: { args: ['--use', 'http'], silent: true }
244 })
245 expect(pool1.opts.env).toStrictEqual({ TEST: 'test' })
246 expect(pool1.opts.settings).toStrictEqual({
247 args: ['--use', 'http'],
248 silent: true
249 })
250 expect({ ...pool1.opts.settings, exec: workerFilePath }).toStrictEqual({
251 args: ['--use', 'http'],
252 silent: true,
253 exec: workerFilePath
254 })
255 await pool1.destroy()
256 })
257
258 it('Should work even without opts in input', async () => {
259 const pool1 = new FixedClusterPool(
260 numberOfWorkers,
261 './tests/worker-files/cluster/testWorker.js'
262 )
263 const res = await pool1.execute()
264 expect(res).toStrictEqual({ ok: 1 })
265 // We need to clean up the resources after our test
266 await pool1.destroy()
267 })
268
269 it('Verify that a pool with zero worker fails', async () => {
270 expect(
271 () =>
272 new FixedClusterPool(0, './tests/worker-files/cluster/testWorker.js')
273 ).toThrowError('Cannot instantiate a fixed pool with zero worker')
274 })
275 })