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