fix: fix task stealing
[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, 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 < numberOfThreads * 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 numberOfThreads + 1 times for a loop submitting up to numberOfThreads * 2 tasks to the fixed pool.
105 expect(poolBusy).toBe(numberOfThreads + 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 < numberOfThreads * maxMultiplier; i++) {
112 promises.add(queuePool.execute())
113 }
114 expect(promises.size).toBe(numberOfThreads * 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 numberOfThreads * queuePool.opts.tasksQueueOptions.concurrency
130 )
131 expect(queuePool.info.queuedTasks).toBe(
132 numberOfThreads *
133 (maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency)
134 )
135 expect(queuePool.info.maxQueuedTasks).toBe(
136 numberOfThreads *
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).toBeGreaterThanOrEqual(0)
143 expect(workerNode.usage.tasks.executing).toBeLessThanOrEqual(
144 numberOfThreads * maxMultiplier
145 )
146 expect(workerNode.usage.tasks.executed).toBe(maxMultiplier)
147 expect(workerNode.usage.tasks.queued).toBe(0)
148 expect(workerNode.usage.tasks.maxQueued).toBe(
149 maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
150 )
151 }
152 })
153
154 it('Verify that is possible to have a worker that return undefined', async () => {
155 const result = await emptyPool.execute()
156 expect(result).toBeUndefined()
157 })
158
159 it('Verify that data are sent to the worker correctly', async () => {
160 const data = { f: 10 }
161 const result = await echoPool.execute(data)
162 expect(result).toStrictEqual(data)
163 })
164
165 it('Verify that transferable objects are sent to the worker correctly', async () => {
166 let error
167 let result
168 try {
169 result = await pool.execute(undefined, undefined, [
170 new ArrayBuffer(16),
171 new MessageChannel().port1
172 ])
173 } catch (e) {
174 error = e
175 }
176 expect(result).toStrictEqual({ ok: 1 })
177 expect(error).toBeUndefined()
178 try {
179 result = await pool.execute(undefined, undefined, [
180 new SharedArrayBuffer(16)
181 ])
182 } catch (e) {
183 error = e
184 }
185 expect(result).toStrictEqual({ ok: 1 })
186 expect(error).toStrictEqual(
187 new TypeError('Found invalid object in transferList')
188 )
189 })
190
191 it('Verify that error handling is working properly:sync', async () => {
192 const data = { f: 10 }
193 let taskError
194 errorPool.emitter.on(PoolEvents.taskError, (e) => {
195 taskError = e
196 })
197 let inError
198 try {
199 await errorPool.execute(data)
200 } catch (e) {
201 inError = e
202 }
203 expect(inError).toBeDefined()
204 expect(inError).toBeInstanceOf(Error)
205 expect(inError.message).toBeDefined()
206 expect(typeof inError.message === 'string').toBe(true)
207 expect(inError.message).toBe('Error Message from ThreadWorker')
208 expect(taskError).toStrictEqual({
209 name: 'default',
210 message: new Error('Error Message from ThreadWorker'),
211 data
212 })
213 expect(
214 errorPool.workerNodes.some(
215 (workerNode) => workerNode.usage.tasks.failed === 1
216 )
217 ).toBe(true)
218 })
219
220 it('Verify that error handling is working properly:async', async () => {
221 const data = { f: 10 }
222 let taskError
223 asyncErrorPool.emitter.on(PoolEvents.taskError, (e) => {
224 taskError = e
225 })
226 let inError
227 try {
228 await asyncErrorPool.execute(data)
229 } catch (e) {
230 inError = e
231 }
232 expect(inError).toBeDefined()
233 expect(inError).toBeInstanceOf(Error)
234 expect(inError.message).toBeDefined()
235 expect(typeof inError.message === 'string').toBe(true)
236 expect(inError.message).toBe('Error Message from ThreadWorker:async')
237 expect(taskError).toStrictEqual({
238 name: 'default',
239 message: new Error('Error Message from ThreadWorker:async'),
240 data
241 })
242 expect(
243 asyncErrorPool.workerNodes.some(
244 (workerNode) => workerNode.usage.tasks.failed === 1
245 )
246 ).toBe(true)
247 })
248
249 it('Verify that async function is working properly', async () => {
250 const data = { f: 10 }
251 const startTime = performance.now()
252 const result = await asyncPool.execute(data)
253 const usedTime = performance.now() - startTime
254 expect(result).toStrictEqual(data)
255 expect(usedTime).toBeGreaterThanOrEqual(2000)
256 })
257
258 it('Shutdown test', async () => {
259 const exitPromise = waitWorkerEvents(pool, 'exit', numberOfThreads)
260 let poolDestroy = 0
261 pool.emitter.on(PoolEvents.destroy, () => ++poolDestroy)
262 await pool.destroy()
263 const numberOfExitEvents = await exitPromise
264 expect(numberOfExitEvents).toBe(numberOfThreads)
265 expect(poolDestroy).toBe(1)
266 })
267
268 it('Verify that thread pool options are checked', async () => {
269 const workerFilePath = './tests/worker-files/thread/testWorker.js'
270 let pool1 = new FixedThreadPool(numberOfThreads, workerFilePath)
271 expect(pool1.opts.workerOptions).toBeUndefined()
272 await pool1.destroy()
273 pool1 = new FixedThreadPool(numberOfThreads, workerFilePath, {
274 workerOptions: {
275 env: { TEST: 'test' },
276 name: 'test'
277 }
278 })
279 expect(pool1.opts.workerOptions).toStrictEqual({
280 env: { TEST: 'test' },
281 name: 'test'
282 })
283 await pool1.destroy()
284 })
285
286 it('Should work even without opts in input', async () => {
287 const pool1 = new FixedThreadPool(
288 numberOfThreads,
289 './tests/worker-files/thread/testWorker.js'
290 )
291 const res = await pool1.execute()
292 expect(res).toStrictEqual({ ok: 1 })
293 // We need to clean up the resources after our test
294 await pool1.destroy()
295 })
296
297 it('Verify that a pool with zero worker fails', async () => {
298 expect(
299 () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js')
300 ).toThrowError('Cannot instantiate a fixed pool with zero worker')
301 })
302 })