Commit | Line | Data |
---|---|---|
a61a0724 | 1 | const { expect } = require('expect') |
cdace0e5 | 2 | const { FixedThreadPool, PoolEvents } = require('../../../lib') |
dbca3be9 | 3 | const { TaskFunctions } = require('../../test-types') |
1dcbfefc | 4 | const { waitPoolEvents, waitWorkerEvents } = require('../../test-utils') |
506c2a14 | 5 | |
079de991 | 6 | describe('Fixed thread pool test suite', () => { |
e1ffb94f | 7 | const numberOfThreads = 6 |
4e377863 | 8 | const tasksConcurrency = 2 |
e1ffb94f JB |
9 | const pool = new FixedThreadPool( |
10 | numberOfThreads, | |
11 | './tests/worker-files/thread/testWorker.js', | |
12 | { | |
13 | errorHandler: e => console.error(e) | |
14 | } | |
15 | ) | |
594bfb84 JB |
16 | const queuePool = new FixedThreadPool( |
17 | numberOfThreads, | |
18 | './tests/worker-files/thread/testWorker.js', | |
19 | { | |
20 | enableTasksQueue: true, | |
21 | tasksQueueOptions: { | |
4e377863 | 22 | concurrency: tasksConcurrency |
594bfb84 JB |
23 | }, |
24 | errorHandler: e => console.error(e) | |
25 | } | |
26 | ) | |
e1ffb94f JB |
27 | const emptyPool = new FixedThreadPool( |
28 | numberOfThreads, | |
29 | './tests/worker-files/thread/emptyWorker.js', | |
73bfd59d | 30 | { exitHandler: () => console.info('empty pool worker exited') } |
e1ffb94f JB |
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 | ||
0e2503fc JB |
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() | |
7c0ba920 | 60 | await asyncErrorPool.destroy() |
0e2503fc | 61 | await emptyPool.destroy() |
594bfb84 | 62 | await queuePool.destroy() |
0e2503fc JB |
63 | }) |
64 | ||
506c2a14 | 65 | it('Verify that the function is executed in a worker thread', async () => { |
6db75ad9 | 66 | let result = await pool.execute({ |
dbca3be9 | 67 | function: TaskFunctions.fibonacci |
6db75ad9 | 68 | }) |
024daf59 | 69 | expect(result).toBe(75025) |
6db75ad9 | 70 | result = await pool.execute({ |
dbca3be9 | 71 | function: TaskFunctions.factorial |
6db75ad9 | 72 | }) |
70a4f5ea | 73 | expect(result).toBe(9.33262154439441e157) |
506c2a14 | 74 | }) |
75 | ||
318d4156 | 76 | it('Verify that is possible to invoke the execute() method without input', async () => { |
106744f7 | 77 | const result = await pool.execute() |
30b963d4 | 78 | expect(result).toStrictEqual({ ok: 1 }) |
106744f7 | 79 | }) |
80 | ||
1dcbfefc | 81 | it("Verify that 'ready' event is emitted", async () => { |
079de991 JB |
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 | |
66293e7d | 90 | pool1.emitter.on(PoolEvents.ready, () => ++poolReady) |
1dcbfefc | 91 | await waitPoolEvents(pool1, 'ready', 1) |
216541b6 JB |
92 | expect(poolReady).toBe(1) |
93 | }) | |
94 | ||
1dcbfefc | 95 | it("Verify that 'busy' event is emitted", () => { |
7c0ba920 | 96 | let poolBusy = 0 |
aee46736 | 97 | pool.emitter.on(PoolEvents.busy, () => ++poolBusy) |
7c0ba920 | 98 | for (let i = 0; i < numberOfThreads * 2; i++) { |
8cbb82eb | 99 | pool.execute() |
7c0ba920 | 100 | } |
14916bf9 JB |
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) | |
7c0ba920 JB |
104 | }) |
105 | ||
594bfb84 | 106 | it('Verify that tasks queuing is working', async () => { |
d3d4b67d | 107 | const promises = new Set() |
4e377863 | 108 | const maxMultiplier = 3 // Must be greater than tasksConcurrency |
594bfb84 | 109 | for (let i = 0; i < numberOfThreads * maxMultiplier; i++) { |
d3d4b67d | 110 | promises.add(queuePool.execute()) |
594bfb84 | 111 | } |
d3d4b67d | 112 | expect(promises.size).toBe(numberOfThreads * maxMultiplier) |
594bfb84 | 113 | for (const workerNode of queuePool.workerNodes) { |
f59e1027 | 114 | expect(workerNode.usage.tasks.executing).toBeLessThanOrEqual( |
594bfb84 JB |
115 | queuePool.opts.tasksQueueOptions.concurrency |
116 | ) | |
f59e1027 | 117 | expect(workerNode.usage.tasks.executed).toBe(0) |
4e377863 JB |
118 | expect(workerNode.usage.tasks.queued).toBe( |
119 | maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency | |
120 | ) | |
121 | expect(workerNode.usage.tasks.maxQueued).toBe( | |
122 | maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency | |
123 | ) | |
594bfb84 | 124 | } |
4e377863 JB |
125 | expect(queuePool.info.executingTasks).toBe( |
126 | numberOfThreads * queuePool.opts.tasksQueueOptions.concurrency | |
127 | ) | |
6b27d407 | 128 | expect(queuePool.info.queuedTasks).toBe( |
4e377863 JB |
129 | numberOfThreads * |
130 | (maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency) | |
6b27d407 JB |
131 | ) |
132 | expect(queuePool.info.maxQueuedTasks).toBe( | |
4e377863 JB |
133 | numberOfThreads * |
134 | (maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency) | |
d3d4b67d | 135 | ) |
594bfb84 JB |
136 | await Promise.all(promises) |
137 | for (const workerNode of queuePool.workerNodes) { | |
f59e1027 | 138 | expect(workerNode.usage.tasks.executing).toBe(0) |
4e377863 | 139 | expect(workerNode.usage.tasks.executed).toBe(maxMultiplier) |
f59e1027 | 140 | expect(workerNode.usage.tasks.queued).toBe(0) |
4e377863 JB |
141 | expect(workerNode.usage.tasks.maxQueued).toBe( |
142 | maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency | |
143 | ) | |
594bfb84 JB |
144 | } |
145 | }) | |
146 | ||
106744f7 | 147 | it('Verify that is possible to have a worker that return undefined', async () => { |
148 | const result = await emptyPool.execute() | |
6db75ad9 | 149 | expect(result).toBeUndefined() |
106744f7 | 150 | }) |
151 | ||
152 | it('Verify that data are sent to the worker correctly', async () => { | |
153 | const data = { f: 10 } | |
154 | const result = await echoPool.execute(data) | |
e1ffb94f | 155 | expect(result).toStrictEqual(data) |
106744f7 | 156 | }) |
157 | ||
7c0ba920 | 158 | it('Verify that error handling is working properly:sync', async () => { |
106744f7 | 159 | const data = { f: 10 } |
d46660cd JB |
160 | let taskError |
161 | errorPool.emitter.on(PoolEvents.taskError, e => { | |
162 | taskError = e | |
163 | }) | |
106744f7 | 164 | let inError |
165 | try { | |
166 | await errorPool.execute(data) | |
167 | } catch (e) { | |
168 | inError = e | |
169 | } | |
7c0ba920 JB |
170 | expect(inError).toBeDefined() |
171 | expect(inError).toBeInstanceOf(Error) | |
172 | expect(inError.message).toBeDefined() | |
8620fb25 | 173 | expect(typeof inError.message === 'string').toBe(true) |
985d0e79 JB |
174 | expect(inError.message).toBe('Error Message from ThreadWorker') |
175 | expect(taskError).toStrictEqual({ | |
ff128cc9 | 176 | name: 'default', |
985d0e79 JB |
177 | message: new Error('Error Message from ThreadWorker'), |
178 | data | |
179 | }) | |
18482cec JB |
180 | expect( |
181 | errorPool.workerNodes.some( | |
f59e1027 | 182 | workerNode => workerNode.usage.tasks.failed === 1 |
18482cec JB |
183 | ) |
184 | ).toBe(true) | |
7c0ba920 JB |
185 | }) |
186 | ||
187 | it('Verify that error handling is working properly:async', async () => { | |
188 | const data = { f: 10 } | |
4f0b85b3 JB |
189 | let taskError |
190 | asyncErrorPool.emitter.on(PoolEvents.taskError, e => { | |
191 | taskError = e | |
192 | }) | |
7c0ba920 JB |
193 | let inError |
194 | try { | |
195 | await asyncErrorPool.execute(data) | |
196 | } catch (e) { | |
197 | inError = e | |
198 | } | |
199 | expect(inError).toBeDefined() | |
200 | expect(inError).toBeInstanceOf(Error) | |
201 | expect(inError.message).toBeDefined() | |
8620fb25 | 202 | expect(typeof inError.message === 'string').toBe(true) |
985d0e79 JB |
203 | expect(inError.message).toBe('Error Message from ThreadWorker:async') |
204 | expect(taskError).toStrictEqual({ | |
ff128cc9 | 205 | name: 'default', |
985d0e79 JB |
206 | message: new Error('Error Message from ThreadWorker:async'), |
207 | data | |
208 | }) | |
18482cec JB |
209 | expect( |
210 | asyncErrorPool.workerNodes.some( | |
f59e1027 | 211 | workerNode => workerNode.usage.tasks.failed === 1 |
18482cec JB |
212 | ) |
213 | ).toBe(true) | |
106744f7 | 214 | }) |
215 | ||
7784f548 | 216 | it('Verify that async function is working properly', async () => { |
217 | const data = { f: 10 } | |
15e5141f | 218 | const startTime = performance.now() |
7784f548 | 219 | const result = await asyncPool.execute(data) |
15e5141f | 220 | const usedTime = performance.now() - startTime |
e1ffb94f | 221 | expect(result).toStrictEqual(data) |
32d490eb | 222 | expect(usedTime).toBeGreaterThanOrEqual(2000) |
7784f548 | 223 | }) |
224 | ||
506c2a14 | 225 | it('Shutdown test', async () => { |
bac873bd | 226 | const exitPromise = waitWorkerEvents(pool, 'exit', numberOfThreads) |
1f9a5a44 | 227 | await pool.destroy() |
bdacc2d2 JB |
228 | const numberOfExitEvents = await exitPromise |
229 | expect(numberOfExitEvents).toBe(numberOfThreads) | |
506c2a14 | 230 | }) |
231 | ||
90082c8c | 232 | it('Verify that thread pool options are checked', async () => { |
f59e1027 | 233 | const workerFilePath = './tests/worker-files/thread/testWorker.js' |
90082c8c JB |
234 | let pool1 = new FixedThreadPool(numberOfThreads, workerFilePath) |
235 | expect(pool1.opts.workerOptions).toBeUndefined() | |
236 | await pool1.destroy() | |
237 | pool1 = new FixedThreadPool(numberOfThreads, workerFilePath, { | |
238 | workerOptions: { | |
239 | env: { TEST: 'test' }, | |
240 | name: 'test' | |
241 | } | |
242 | }) | |
243 | expect(pool1.opts.workerOptions).toStrictEqual({ | |
244 | env: { TEST: 'test' }, | |
245 | name: 'test' | |
246 | }) | |
247 | await pool1.destroy() | |
248 | }) | |
249 | ||
506c2a14 | 250 | it('Should work even without opts in input', async () => { |
76b1e974 | 251 | const pool1 = new FixedThreadPool( |
e1ffb94f | 252 | numberOfThreads, |
76b1e974 S |
253 | './tests/worker-files/thread/testWorker.js' |
254 | ) | |
6db75ad9 | 255 | const res = await pool1.execute() |
30b963d4 | 256 | expect(res).toStrictEqual({ ok: 1 }) |
0e2503fc JB |
257 | // We need to clean up the resources after our test |
258 | await pool1.destroy() | |
506c2a14 | 259 | }) |
8d3782fa JB |
260 | |
261 | it('Verify that a pool with zero worker fails', async () => { | |
262 | expect( | |
263 | () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js') | |
2431bdb4 | 264 | ).toThrowError('Cannot instantiate a fixed pool with zero worker') |
8d3782fa | 265 | }) |
506c2a14 | 266 | }) |