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