Commit | Line | Data |
---|---|---|
a61a0724 | 1 | const { expect } = require('expect') |
e843b904 | 2 | const { |
9e619829 | 3 | DynamicThreadPool, |
aee46736 | 4 | FixedClusterPool, |
e843b904 | 5 | FixedThreadPool, |
aee46736 | 6 | PoolEvents, |
e843b904 JB |
7 | WorkerChoiceStrategies |
8 | } = require('../../../lib/index') | |
78099a15 | 9 | const { CircularArray } = require('../../../lib/circular-array') |
e1ffb94f JB |
10 | |
11 | describe('Abstract pool test suite', () => { | |
12 | const numberOfWorkers = 1 | |
3032893a | 13 | const workerNotFoundInPoolError = new Error( |
f06e48d8 | 14 | 'Worker could not be found in the pool worker nodes' |
e1ffb94f | 15 | ) |
a8884ffd | 16 | class StubPoolWithRemoveAllWorker extends FixedThreadPool { |
e1ffb94f | 17 | removeAllWorker () { |
e65c6cd9 | 18 | this.workers = [] |
c923ce56 | 19 | this.promiseResponseMap.clear() |
e1ffb94f | 20 | } |
3ec964d6 | 21 | } |
a8884ffd | 22 | class StubPoolWithIsMain extends FixedThreadPool { |
e1ffb94f JB |
23 | isMain () { |
24 | return false | |
25 | } | |
3ec964d6 | 26 | } |
3ec964d6 | 27 | |
3ec964d6 | 28 | it('Simulate pool creation from a non main thread/process', () => { |
8d3782fa JB |
29 | expect( |
30 | () => | |
a8884ffd | 31 | new StubPoolWithIsMain( |
7c0ba920 | 32 | numberOfWorkers, |
8d3782fa JB |
33 | './tests/worker-files/thread/testWorker.js', |
34 | { | |
35 | errorHandler: e => console.error(e) | |
36 | } | |
37 | ) | |
38 | ).toThrowError(new Error('Cannot start a pool from a worker!')) | |
3ec964d6 | 39 | }) |
c510fea7 APA |
40 | |
41 | it('Verify that filePath is checked', () => { | |
292ad316 JB |
42 | const expectedError = new Error( |
43 | 'Please specify a file with a worker implementation' | |
44 | ) | |
7c0ba920 | 45 | expect(() => new FixedThreadPool(numberOfWorkers)).toThrowError( |
292ad316 | 46 | expectedError |
8d3782fa | 47 | ) |
7c0ba920 | 48 | expect(() => new FixedThreadPool(numberOfWorkers, '')).toThrowError( |
292ad316 | 49 | expectedError |
8d3782fa JB |
50 | ) |
51 | }) | |
52 | ||
53 | it('Verify that numberOfWorkers is checked', () => { | |
54 | expect(() => new FixedThreadPool()).toThrowError( | |
55 | new Error( | |
56 | 'Cannot instantiate a pool without specifying the number of workers' | |
57 | ) | |
58 | ) | |
59 | }) | |
60 | ||
61 | it('Verify that a negative number of workers is checked', () => { | |
62 | expect( | |
63 | () => | |
64 | new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js') | |
65 | ).toThrowError( | |
473c717a JB |
66 | new RangeError( |
67 | 'Cannot instantiate a pool with a negative number of workers' | |
68 | ) | |
8d3782fa JB |
69 | ) |
70 | }) | |
71 | ||
72 | it('Verify that a non integer number of workers is checked', () => { | |
73 | expect( | |
74 | () => | |
75 | new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js') | |
76 | ).toThrowError( | |
473c717a | 77 | new TypeError( |
8d3782fa JB |
78 | 'Cannot instantiate a pool with a non integer number of workers' |
79 | ) | |
80 | ) | |
c510fea7 | 81 | }) |
7c0ba920 | 82 | |
fd7ebd49 | 83 | it('Verify that pool options are checked', async () => { |
7c0ba920 JB |
84 | let pool = new FixedThreadPool( |
85 | numberOfWorkers, | |
86 | './tests/worker-files/thread/testWorker.js' | |
87 | ) | |
8620fb25 | 88 | expect(pool.opts.enableEvents).toBe(true) |
7c0ba920 | 89 | expect(pool.emitter).toBeDefined() |
ff733df7 | 90 | expect(pool.opts.enableTasksQueue).toBe(false) |
e843b904 JB |
91 | expect(pool.opts.workerChoiceStrategy).toBe( |
92 | WorkerChoiceStrategies.ROUND_ROBIN | |
93 | ) | |
da309861 JB |
94 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ |
95 | medRunTime: false | |
96 | }) | |
35cf1c03 JB |
97 | expect(pool.opts.messageHandler).toBeUndefined() |
98 | expect(pool.opts.errorHandler).toBeUndefined() | |
99 | expect(pool.opts.onlineHandler).toBeUndefined() | |
100 | expect(pool.opts.exitHandler).toBeUndefined() | |
fd7ebd49 | 101 | await pool.destroy() |
35cf1c03 | 102 | const testHandler = () => console.log('test handler executed') |
7c0ba920 JB |
103 | pool = new FixedThreadPool( |
104 | numberOfWorkers, | |
105 | './tests/worker-files/thread/testWorker.js', | |
106 | { | |
737c6d97 | 107 | workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED, |
da309861 | 108 | workerChoiceStrategyOptions: { medRunTime: true }, |
35cf1c03 | 109 | enableEvents: false, |
ff733df7 | 110 | enableTasksQueue: true, |
35cf1c03 JB |
111 | messageHandler: testHandler, |
112 | errorHandler: testHandler, | |
113 | onlineHandler: testHandler, | |
114 | exitHandler: testHandler | |
7c0ba920 JB |
115 | } |
116 | ) | |
8620fb25 | 117 | expect(pool.opts.enableEvents).toBe(false) |
7c0ba920 | 118 | expect(pool.emitter).toBeUndefined() |
ff733df7 | 119 | expect(pool.opts.enableTasksQueue).toBe(true) |
e843b904 | 120 | expect(pool.opts.workerChoiceStrategy).toBe( |
737c6d97 | 121 | WorkerChoiceStrategies.LESS_USED |
e843b904 | 122 | ) |
da309861 JB |
123 | expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ |
124 | medRunTime: true | |
125 | }) | |
35cf1c03 JB |
126 | expect(pool.opts.messageHandler).toStrictEqual(testHandler) |
127 | expect(pool.opts.errorHandler).toStrictEqual(testHandler) | |
128 | expect(pool.opts.onlineHandler).toStrictEqual(testHandler) | |
129 | expect(pool.opts.exitHandler).toStrictEqual(testHandler) | |
fd7ebd49 | 130 | await pool.destroy() |
7c0ba920 JB |
131 | }) |
132 | ||
3032893a | 133 | it('Simulate worker not found during getWorkerTasksUsage', async () => { |
a8884ffd | 134 | const pool = new StubPoolWithRemoveAllWorker( |
10fcfaf4 JB |
135 | numberOfWorkers, |
136 | './tests/worker-files/cluster/testWorker.js', | |
137 | { | |
10fcfaf4 JB |
138 | errorHandler: e => console.error(e) |
139 | } | |
140 | ) | |
141 | // Simulate worker not found. | |
142 | pool.removeAllWorker() | |
3032893a JB |
143 | expect(() => pool.getWorkerTasksUsage()).toThrowError( |
144 | workerNotFoundInPoolError | |
bf9549ae | 145 | ) |
fd7ebd49 | 146 | await pool.destroy() |
bf9549ae JB |
147 | }) |
148 | ||
fd7ebd49 | 149 | it('Verify that worker pool tasks usage are initialized', async () => { |
bf9549ae JB |
150 | const pool = new FixedClusterPool( |
151 | numberOfWorkers, | |
152 | './tests/worker-files/cluster/testWorker.js' | |
153 | ) | |
f06e48d8 JB |
154 | for (const workerNode of pool.workerNodes) { |
155 | expect(workerNode.tasksUsage).toBeDefined() | |
156 | expect(workerNode.tasksUsage.run).toBe(0) | |
157 | expect(workerNode.tasksUsage.running).toBe(0) | |
158 | expect(workerNode.tasksUsage.runTime).toBe(0) | |
159 | expect(workerNode.tasksUsage.runTimeHistory).toBeInstanceOf(CircularArray) | |
160 | expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0) | |
161 | expect(workerNode.tasksUsage.avgRunTime).toBe(0) | |
162 | expect(workerNode.tasksUsage.medRunTime).toBe(0) | |
163 | expect(workerNode.tasksUsage.error).toBe(0) | |
164 | } | |
165 | await pool.destroy() | |
166 | }) | |
167 | ||
168 | it('Verify that worker pool tasks queue are initialized', async () => { | |
169 | const pool = new FixedClusterPool( | |
170 | numberOfWorkers, | |
171 | './tests/worker-files/cluster/testWorker.js' | |
172 | ) | |
173 | for (const workerNode of pool.workerNodes) { | |
174 | expect(workerNode.tasksQueue).toBeDefined() | |
175 | expect(workerNode.tasksQueue).toBeInstanceOf(Array) | |
176 | expect(workerNode.tasksQueue.length).toBe(0) | |
bf9549ae | 177 | } |
fd7ebd49 | 178 | await pool.destroy() |
bf9549ae JB |
179 | }) |
180 | ||
181 | it('Verify that worker pool tasks usage are computed', async () => { | |
182 | const pool = new FixedClusterPool( | |
183 | numberOfWorkers, | |
184 | './tests/worker-files/cluster/testWorker.js' | |
185 | ) | |
186 | const promises = [] | |
187 | for (let i = 0; i < numberOfWorkers * 2; i++) { | |
6db75ad9 | 188 | promises.push(pool.execute()) |
bf9549ae | 189 | } |
f06e48d8 JB |
190 | for (const workerNode of pool.workerNodes) { |
191 | expect(workerNode.tasksUsage).toBeDefined() | |
192 | expect(workerNode.tasksUsage.run).toBe(0) | |
193 | expect(workerNode.tasksUsage.running).toBe(numberOfWorkers * 2) | |
194 | expect(workerNode.tasksUsage.runTime).toBe(0) | |
195 | expect(workerNode.tasksUsage.runTimeHistory).toBeInstanceOf(CircularArray) | |
196 | expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0) | |
197 | expect(workerNode.tasksUsage.avgRunTime).toBe(0) | |
198 | expect(workerNode.tasksUsage.medRunTime).toBe(0) | |
199 | expect(workerNode.tasksUsage.error).toBe(0) | |
bf9549ae JB |
200 | } |
201 | await Promise.all(promises) | |
f06e48d8 JB |
202 | for (const workerNode of pool.workerNodes) { |
203 | expect(workerNode.tasksUsage).toBeDefined() | |
204 | expect(workerNode.tasksUsage.run).toBe(numberOfWorkers * 2) | |
205 | expect(workerNode.tasksUsage.running).toBe(0) | |
206 | expect(workerNode.tasksUsage.runTime).toBeGreaterThanOrEqual(0) | |
207 | expect(workerNode.tasksUsage.runTimeHistory).toBeInstanceOf(CircularArray) | |
208 | expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0) | |
209 | expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) | |
210 | expect(workerNode.tasksUsage.medRunTime).toBe(0) | |
211 | expect(workerNode.tasksUsage.error).toBe(0) | |
bf9549ae | 212 | } |
fd7ebd49 | 213 | await pool.destroy() |
bf9549ae JB |
214 | }) |
215 | ||
ee11a4a2 | 216 | it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => { |
7fd82a1c | 217 | const pool = new DynamicThreadPool( |
9e619829 JB |
218 | numberOfWorkers, |
219 | numberOfWorkers, | |
220 | './tests/worker-files/thread/testWorker.js' | |
221 | ) | |
7fd82a1c | 222 | const promises = [] |
9e619829 JB |
223 | for (let i = 0; i < numberOfWorkers * 2; i++) { |
224 | promises.push(pool.execute()) | |
225 | } | |
226 | await Promise.all(promises) | |
f06e48d8 JB |
227 | for (const workerNode of pool.workerNodes) { |
228 | expect(workerNode.tasksUsage).toBeDefined() | |
229 | expect(workerNode.tasksUsage.run).toBe(numberOfWorkers * 2) | |
230 | expect(workerNode.tasksUsage.running).toBe(0) | |
231 | expect(workerNode.tasksUsage.runTime).toBeGreaterThanOrEqual(0) | |
232 | expect(workerNode.tasksUsage.runTimeHistory).toBeInstanceOf(CircularArray) | |
233 | expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0) | |
234 | expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) | |
235 | expect(workerNode.tasksUsage.medRunTime).toBe(0) | |
236 | expect(workerNode.tasksUsage.error).toBe(0) | |
9e619829 JB |
237 | } |
238 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) | |
f06e48d8 JB |
239 | for (const workerNode of pool.workerNodes) { |
240 | expect(workerNode.tasksUsage).toBeDefined() | |
241 | expect(workerNode.tasksUsage.run).toBe(0) | |
242 | expect(workerNode.tasksUsage.running).toBe(0) | |
243 | expect(workerNode.tasksUsage.runTime).toBe(0) | |
244 | expect(workerNode.tasksUsage.runTimeHistory).toBeInstanceOf(CircularArray) | |
245 | expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0) | |
246 | expect(workerNode.tasksUsage.avgRunTime).toBe(0) | |
247 | expect(workerNode.tasksUsage.medRunTime).toBe(0) | |
248 | expect(workerNode.tasksUsage.error).toBe(0) | |
ee11a4a2 | 249 | } |
fd7ebd49 | 250 | await pool.destroy() |
ee11a4a2 JB |
251 | }) |
252 | ||
164d950a JB |
253 | it("Verify that pool event emitter 'full' event can register a callback", async () => { |
254 | const pool = new DynamicThreadPool( | |
255 | numberOfWorkers, | |
256 | numberOfWorkers, | |
257 | './tests/worker-files/thread/testWorker.js' | |
258 | ) | |
259 | const promises = [] | |
260 | let poolFull = 0 | |
aee46736 | 261 | pool.emitter.on(PoolEvents.full, () => ++poolFull) |
164d950a JB |
262 | for (let i = 0; i < numberOfWorkers * 2; i++) { |
263 | promises.push(pool.execute()) | |
264 | } | |
265 | await Promise.all(promises) | |
266 | // The `full` event is triggered when the number of submitted tasks at once reach the number of dynamic pool workers. | |
267 | // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool. | |
268 | expect(poolFull).toBe(numberOfWorkers + 1) | |
269 | await pool.destroy() | |
270 | }) | |
271 | ||
cf597bc5 | 272 | it("Verify that pool event emitter 'busy' event can register a callback", async () => { |
7c0ba920 JB |
273 | const pool = new FixedThreadPool( |
274 | numberOfWorkers, | |
275 | './tests/worker-files/thread/testWorker.js' | |
276 | ) | |
277 | const promises = [] | |
278 | let poolBusy = 0 | |
aee46736 | 279 | pool.emitter.on(PoolEvents.busy, () => ++poolBusy) |
7c0ba920 | 280 | for (let i = 0; i < numberOfWorkers * 2; i++) { |
6db75ad9 | 281 | promises.push(pool.execute()) |
7c0ba920 | 282 | } |
cf597bc5 | 283 | await Promise.all(promises) |
14916bf9 JB |
284 | // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers. |
285 | // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool. | |
286 | expect(poolBusy).toBe(numberOfWorkers + 1) | |
fd7ebd49 | 287 | await pool.destroy() |
7c0ba920 | 288 | }) |
3ec964d6 | 289 | }) |