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') | |
e1ffb94f JB |
9 | |
10 | describe('Abstract pool test suite', () => { | |
11 | const numberOfWorkers = 1 | |
3032893a | 12 | const workerNotFoundInPoolError = new Error( |
ffcbbad8 | 13 | 'Worker could not be found in the pool' |
e1ffb94f | 14 | ) |
a8884ffd | 15 | class StubPoolWithRemoveAllWorker extends FixedThreadPool { |
e1ffb94f | 16 | removeAllWorker () { |
e65c6cd9 | 17 | this.workers = [] |
c923ce56 | 18 | this.promiseResponseMap.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 | { | |
737c6d97 | 102 | workerChoiceStrategy: WorkerChoiceStrategies.LESS_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 | 112 | expect(pool.opts.workerChoiceStrategy).toBe( |
737c6d97 | 113 | WorkerChoiceStrategies.LESS_USED |
e843b904 | 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 | ||
3032893a | 122 | it('Simulate worker not found during getWorkerTasksUsage', async () => { |
a8884ffd | 123 | const pool = new StubPoolWithRemoveAllWorker( |
10fcfaf4 JB |
124 | numberOfWorkers, |
125 | './tests/worker-files/cluster/testWorker.js', | |
126 | { | |
10fcfaf4 JB |
127 | errorHandler: e => console.error(e) |
128 | } | |
129 | ) | |
130 | // Simulate worker not found. | |
131 | pool.removeAllWorker() | |
3032893a JB |
132 | expect(() => pool.getWorkerTasksUsage()).toThrowError( |
133 | workerNotFoundInPoolError | |
bf9549ae | 134 | ) |
fd7ebd49 | 135 | await pool.destroy() |
bf9549ae JB |
136 | }) |
137 | ||
fd7ebd49 | 138 | it('Verify that worker pool tasks usage are initialized', async () => { |
bf9549ae JB |
139 | const pool = new FixedClusterPool( |
140 | numberOfWorkers, | |
141 | './tests/worker-files/cluster/testWorker.js' | |
142 | ) | |
e65c6cd9 JB |
143 | for (const workerItem of pool.workers) { |
144 | expect(workerItem.tasksUsage).toBeDefined() | |
145 | expect(workerItem.tasksUsage.run).toBe(0) | |
146 | expect(workerItem.tasksUsage.running).toBe(0) | |
147 | expect(workerItem.tasksUsage.runTime).toBe(0) | |
148 | expect(workerItem.tasksUsage.avgRunTime).toBe(0) | |
c923ce56 | 149 | expect(workerItem.tasksUsage.error).toBe(0) |
bf9549ae | 150 | } |
fd7ebd49 | 151 | await pool.destroy() |
bf9549ae JB |
152 | }) |
153 | ||
154 | it('Verify that worker pool tasks usage are computed', async () => { | |
155 | const pool = new FixedClusterPool( | |
156 | numberOfWorkers, | |
157 | './tests/worker-files/cluster/testWorker.js' | |
158 | ) | |
159 | const promises = [] | |
160 | for (let i = 0; i < numberOfWorkers * 2; i++) { | |
6db75ad9 | 161 | promises.push(pool.execute()) |
bf9549ae | 162 | } |
e65c6cd9 JB |
163 | for (const workerItem of pool.workers) { |
164 | expect(workerItem.tasksUsage).toBeDefined() | |
165 | expect(workerItem.tasksUsage.run).toBe(0) | |
166 | expect(workerItem.tasksUsage.running).toBe(numberOfWorkers * 2) | |
167 | expect(workerItem.tasksUsage.runTime).toBe(0) | |
168 | expect(workerItem.tasksUsage.avgRunTime).toBe(0) | |
c923ce56 | 169 | expect(workerItem.tasksUsage.error).toBe(0) |
bf9549ae JB |
170 | } |
171 | await Promise.all(promises) | |
e65c6cd9 JB |
172 | for (const workerItem of pool.workers) { |
173 | expect(workerItem.tasksUsage).toBeDefined() | |
174 | expect(workerItem.tasksUsage.run).toBe(numberOfWorkers * 2) | |
175 | expect(workerItem.tasksUsage.running).toBe(0) | |
176 | expect(workerItem.tasksUsage.runTime).toBeGreaterThanOrEqual(0) | |
177 | expect(workerItem.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) | |
c923ce56 | 178 | expect(workerItem.tasksUsage.error).toBe(0) |
bf9549ae | 179 | } |
fd7ebd49 | 180 | await pool.destroy() |
bf9549ae JB |
181 | }) |
182 | ||
ee11a4a2 | 183 | it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => { |
7fd82a1c | 184 | const pool = new DynamicThreadPool( |
9e619829 JB |
185 | numberOfWorkers, |
186 | numberOfWorkers, | |
187 | './tests/worker-files/thread/testWorker.js' | |
188 | ) | |
7fd82a1c | 189 | const promises = [] |
9e619829 JB |
190 | for (let i = 0; i < numberOfWorkers * 2; i++) { |
191 | promises.push(pool.execute()) | |
192 | } | |
193 | await Promise.all(promises) | |
e65c6cd9 JB |
194 | for (const workerItem of pool.workers) { |
195 | expect(workerItem.tasksUsage).toBeDefined() | |
196 | expect(workerItem.tasksUsage.run).toBe(numberOfWorkers * 2) | |
197 | expect(workerItem.tasksUsage.running).toBe(0) | |
198 | expect(workerItem.tasksUsage.runTime).toBeGreaterThanOrEqual(0) | |
199 | expect(workerItem.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) | |
c923ce56 | 200 | expect(workerItem.tasksUsage.error).toBe(0) |
9e619829 JB |
201 | } |
202 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) | |
e65c6cd9 JB |
203 | for (const workerItem of pool.workers) { |
204 | expect(workerItem.tasksUsage).toBeDefined() | |
205 | expect(workerItem.tasksUsage.run).toBe(0) | |
206 | expect(workerItem.tasksUsage.running).toBe(0) | |
207 | expect(workerItem.tasksUsage.runTime).toBe(0) | |
208 | expect(workerItem.tasksUsage.avgRunTime).toBe(0) | |
c923ce56 | 209 | expect(workerItem.tasksUsage.error).toBe(0) |
ee11a4a2 | 210 | } |
fd7ebd49 | 211 | await pool.destroy() |
ee11a4a2 JB |
212 | }) |
213 | ||
164d950a JB |
214 | it("Verify that pool event emitter 'full' event can register a callback", async () => { |
215 | const pool = new DynamicThreadPool( | |
216 | numberOfWorkers, | |
217 | numberOfWorkers, | |
218 | './tests/worker-files/thread/testWorker.js' | |
219 | ) | |
220 | const promises = [] | |
221 | let poolFull = 0 | |
aee46736 | 222 | pool.emitter.on(PoolEvents.full, () => ++poolFull) |
164d950a JB |
223 | for (let i = 0; i < numberOfWorkers * 2; i++) { |
224 | promises.push(pool.execute()) | |
225 | } | |
226 | await Promise.all(promises) | |
227 | // The `full` event is triggered when the number of submitted tasks at once reach the number of dynamic pool workers. | |
228 | // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool. | |
229 | expect(poolFull).toBe(numberOfWorkers + 1) | |
230 | await pool.destroy() | |
231 | }) | |
232 | ||
cf597bc5 | 233 | it("Verify that pool event emitter 'busy' event can register a callback", async () => { |
7c0ba920 JB |
234 | const pool = new FixedThreadPool( |
235 | numberOfWorkers, | |
236 | './tests/worker-files/thread/testWorker.js' | |
237 | ) | |
238 | const promises = [] | |
239 | let poolBusy = 0 | |
aee46736 | 240 | pool.emitter.on(PoolEvents.busy, () => ++poolBusy) |
7c0ba920 | 241 | for (let i = 0; i < numberOfWorkers * 2; i++) { |
6db75ad9 | 242 | promises.push(pool.execute()) |
7c0ba920 | 243 | } |
cf597bc5 | 244 | await Promise.all(promises) |
14916bf9 JB |
245 | // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers. |
246 | // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool. | |
247 | expect(poolBusy).toBe(numberOfWorkers + 1) | |
fd7ebd49 | 248 | await pool.destroy() |
7c0ba920 | 249 | }) |
3ec964d6 | 250 | }) |