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