Commit | Line | Data |
---|---|---|
a61a0724 | 1 | const { expect } = require('expect') |
df9aaf20 | 2 | const sinon = require('sinon') |
8620fb25 | 3 | const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib') |
6cd5248f | 4 | const { DEFAULT_TASK_NAME, EMPTY_FUNCTION } = require('../../lib/utils') |
7fc5cce6 | 5 | |
e1ffb94f | 6 | describe('Abstract worker test suite', () => { |
1f68cede | 7 | class StubWorkerWithMainWorker extends ThreadWorker { |
e1ffb94f JB |
8 | constructor (fn, opts) { |
9 | super(fn, opts) | |
78cea37e | 10 | this.mainWorker = undefined |
e1ffb94f | 11 | } |
7fc5cce6 | 12 | } |
c510fea7 | 13 | |
dc021bcc JB |
14 | afterEach(() => { |
15 | sinon.restore() | |
16 | }) | |
17 | ||
e088a00c | 18 | it('Verify worker options default values', () => { |
8620fb25 | 19 | const worker = new ThreadWorker(() => {}) |
cca3bb1a JB |
20 | expect(worker.opts).toStrictEqual({ |
21 | killBehavior: KillBehaviors.SOFT, | |
22 | maxInactiveTime: 60000, | |
23 | killHandler: EMPTY_FUNCTION | |
24 | }) | |
8620fb25 JB |
25 | }) |
26 | ||
27 | it('Verify that worker options are set at worker creation', () => { | |
df9aaf20 JB |
28 | const killHandler = () => { |
29 | console.info('Worker received kill message') | |
30 | } | |
8620fb25 | 31 | const worker = new ClusterWorker(() => {}, { |
df9aaf20 | 32 | killBehavior: KillBehaviors.HARD, |
cca3bb1a | 33 | maxInactiveTime: 6000, |
df9aaf20 JB |
34 | killHandler, |
35 | async: true | |
8620fb25 | 36 | }) |
cca3bb1a JB |
37 | expect(worker.opts).toStrictEqual({ |
38 | killBehavior: KillBehaviors.HARD, | |
39 | maxInactiveTime: 6000, | |
40 | killHandler | |
41 | }) | |
8620fb25 JB |
42 | }) |
43 | ||
a86b6df1 JB |
44 | it('Verify that taskFunctions parameter is mandatory', () => { |
45 | expect(() => new ClusterWorker()).toThrowError( | |
46 | 'taskFunctions parameter is mandatory' | |
47 | ) | |
d4aeae5a JB |
48 | }) |
49 | ||
f34fdabe | 50 | it('Verify that taskFunctions parameter is a function or a plain object', () => { |
a86b6df1 | 51 | expect(() => new ClusterWorker(0)).toThrowError( |
f34fdabe JB |
52 | new TypeError( |
53 | 'taskFunctions parameter is not a function or a plain object' | |
54 | ) | |
d4aeae5a JB |
55 | ) |
56 | expect(() => new ClusterWorker('')).toThrowError( | |
f34fdabe JB |
57 | new TypeError( |
58 | 'taskFunctions parameter is not a function or a plain object' | |
59 | ) | |
a86b6df1 JB |
60 | ) |
61 | expect(() => new ClusterWorker(true)).toThrowError( | |
f34fdabe JB |
62 | new TypeError( |
63 | 'taskFunctions parameter is not a function or a plain object' | |
64 | ) | |
d4aeae5a | 65 | ) |
a86b6df1 | 66 | expect(() => new ClusterWorker([])).toThrowError( |
f34fdabe JB |
67 | new TypeError( |
68 | 'taskFunctions parameter is not a function or a plain object' | |
69 | ) | |
a86b6df1 JB |
70 | ) |
71 | expect(() => new ClusterWorker(new Map())).toThrowError( | |
f34fdabe JB |
72 | new TypeError( |
73 | 'taskFunctions parameter is not a function or a plain object' | |
74 | ) | |
a86b6df1 JB |
75 | ) |
76 | expect(() => new ClusterWorker(new Set())).toThrowError( | |
f34fdabe JB |
77 | new TypeError( |
78 | 'taskFunctions parameter is not a function or a plain object' | |
79 | ) | |
a86b6df1 JB |
80 | ) |
81 | expect(() => new ClusterWorker(new WeakMap())).toThrowError( | |
f34fdabe JB |
82 | new TypeError( |
83 | 'taskFunctions parameter is not a function or a plain object' | |
84 | ) | |
d4aeae5a | 85 | ) |
a86b6df1 | 86 | expect(() => new ClusterWorker(new WeakSet())).toThrowError( |
f34fdabe JB |
87 | new TypeError( |
88 | 'taskFunctions parameter is not a function or a plain object' | |
89 | ) | |
a86b6df1 | 90 | ) |
f34fdabe JB |
91 | }) |
92 | ||
93 | it('Verify that taskFunctions parameter is not an empty object', () => { | |
630f0acf | 94 | expect(() => new ClusterWorker({})).toThrowError( |
0d80593b | 95 | new Error('taskFunctions parameter object is empty') |
630f0acf | 96 | ) |
a86b6df1 JB |
97 | }) |
98 | ||
2a69b8c5 JB |
99 | it('Verify that taskFunctions parameter with unique function is taken', () => { |
100 | const worker = new ThreadWorker(() => {}) | |
6cd5248f | 101 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
2a69b8c5 JB |
102 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
103 | expect(worker.taskFunctions.size).toBe(2) | |
6cd5248f | 104 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
105 | worker.taskFunctions.get('fn1') |
106 | ) | |
107 | }) | |
108 | ||
6934964f | 109 | it('Verify that taskFunctions parameter with multiple task functions is checked', () => { |
f34fdabe JB |
110 | const fn1 = () => { |
111 | return 1 | |
112 | } | |
113 | const fn2 = '' | |
6934964f JB |
114 | expect(() => new ThreadWorker({ '': fn1 })).toThrowError( |
115 | new TypeError('A taskFunctions parameter object key is an empty string') | |
116 | ) | |
f34fdabe JB |
117 | expect(() => new ThreadWorker({ fn1, fn2 })).toThrowError( |
118 | new TypeError('A taskFunctions parameter object value is not a function') | |
119 | ) | |
120 | }) | |
121 | ||
a86b6df1 JB |
122 | it('Verify that taskFunctions parameter with multiple task functions is taken', () => { |
123 | const fn1 = () => { | |
124 | return 1 | |
125 | } | |
126 | const fn2 = () => { | |
127 | return 2 | |
128 | } | |
129 | const worker = new ClusterWorker({ fn1, fn2 }) | |
6cd5248f | 130 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
2a69b8c5 JB |
131 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
132 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) | |
133 | expect(worker.taskFunctions.size).toBe(3) | |
6cd5248f | 134 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
135 | worker.taskFunctions.get('fn1') |
136 | ) | |
d4aeae5a JB |
137 | }) |
138 | ||
df9aaf20 JB |
139 | it('Verify that sync kill handler is called when worker is killed', () => { |
140 | const worker = new ClusterWorker(() => {}, { | |
141 | killHandler: sinon.stub().returns() | |
142 | }) | |
143 | worker.isMain = false | |
1e3214b6 JB |
144 | worker.getMainWorker = sinon.stub().returns({ |
145 | id: 1, | |
146 | send: sinon.stub().returns() | |
147 | }) | |
df9aaf20 | 148 | worker.handleKillMessage() |
1e3214b6 | 149 | expect(worker.getMainWorker().send.calledOnce).toBe(true) |
df9aaf20 JB |
150 | expect(worker.opts.killHandler.calledOnce).toBe(true) |
151 | }) | |
152 | ||
07588f30 JB |
153 | it('Verify that async kill handler is called when worker is killed', () => { |
154 | const killHandlerStub = sinon.stub().returns() | |
155 | const worker = new ClusterWorker(() => {}, { | |
156 | killHandler: async () => Promise.resolve(killHandlerStub()) | |
157 | }) | |
158 | worker.isMain = false | |
159 | worker.handleKillMessage() | |
160 | expect(killHandlerStub.calledOnce).toBe(true) | |
161 | }) | |
df9aaf20 | 162 | |
2431bdb4 JB |
163 | it('Verify that handleError() method works properly', () => { |
164 | const error = new Error('Error as an error') | |
165 | const worker = new ClusterWorker(() => {}) | |
166 | expect(worker.handleError(error)).not.toBeInstanceOf(Error) | |
167 | expect(worker.handleError(error)).toStrictEqual(error.message) | |
168 | const errorMessage = 'Error as a string' | |
169 | expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage) | |
7fc5cce6 APA |
170 | }) |
171 | ||
318d4156 | 172 | it('Verify that getMainWorker() throw error if main worker is not set', () => { |
7fc5cce6 | 173 | expect(() => |
1f68cede | 174 | new StubWorkerWithMainWorker(() => {}).getMainWorker() |
e102732c | 175 | ).toThrowError('Main worker not set') |
7fc5cce6 | 176 | }) |
2a69b8c5 JB |
177 | |
178 | it('Verify that hasTaskFunction() works', () => { | |
179 | const fn1 = () => { | |
180 | return 1 | |
181 | } | |
182 | const fn2 = () => { | |
183 | return 2 | |
184 | } | |
185 | const worker = new ClusterWorker({ fn1, fn2 }) | |
6934964f JB |
186 | expect(() => worker.hasTaskFunction(0)).toThrowError( |
187 | new TypeError('name parameter is not a string') | |
188 | ) | |
189 | expect(() => worker.hasTaskFunction('')).toThrowError( | |
190 | new TypeError('name parameter is an empty string') | |
191 | ) | |
6cd5248f | 192 | expect(worker.hasTaskFunction(DEFAULT_TASK_NAME)).toBe(true) |
2a69b8c5 JB |
193 | expect(worker.hasTaskFunction('fn1')).toBe(true) |
194 | expect(worker.hasTaskFunction('fn2')).toBe(true) | |
195 | expect(worker.hasTaskFunction('fn3')).toBe(false) | |
196 | }) | |
197 | ||
198 | it('Verify that addTaskFunction() works', () => { | |
199 | const fn1 = () => { | |
200 | return 1 | |
201 | } | |
202 | const fn2 = () => { | |
203 | return 2 | |
204 | } | |
205 | const fn1Replacement = () => { | |
206 | return 3 | |
207 | } | |
208 | const worker = new ThreadWorker(fn1) | |
6934964f JB |
209 | expect(() => worker.addTaskFunction(0, fn1)).toThrowError( |
210 | new TypeError('name parameter is not a string') | |
211 | ) | |
212 | expect(() => worker.addTaskFunction('', fn1)).toThrowError( | |
213 | new TypeError('name parameter is an empty string') | |
214 | ) | |
215 | expect(() => worker.addTaskFunction('fn3', '')).toThrowError( | |
216 | new TypeError('fn parameter is not a function') | |
217 | ) | |
6cd5248f | 218 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
2a69b8c5 JB |
219 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
220 | expect(worker.taskFunctions.size).toBe(2) | |
6cd5248f | 221 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
222 | worker.taskFunctions.get('fn1') |
223 | ) | |
6cd5248f | 224 | expect(() => worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toThrowError( |
2a69b8c5 JB |
225 | new Error('Cannot add a task function with the default reserved name') |
226 | ) | |
227 | worker.addTaskFunction('fn2', fn2) | |
6cd5248f | 228 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
2a69b8c5 JB |
229 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
230 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) | |
231 | expect(worker.taskFunctions.size).toBe(3) | |
6cd5248f | 232 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
233 | worker.taskFunctions.get('fn1') |
234 | ) | |
235 | worker.addTaskFunction('fn1', fn1Replacement) | |
6cd5248f | 236 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
2a69b8c5 JB |
237 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
238 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) | |
239 | expect(worker.taskFunctions.size).toBe(3) | |
6cd5248f | 240 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
241 | worker.taskFunctions.get('fn1') |
242 | ) | |
243 | }) | |
244 | ||
245 | it('Verify that removeTaskFunction() works', () => { | |
246 | const fn1 = () => { | |
247 | return 1 | |
248 | } | |
249 | const fn2 = () => { | |
250 | return 2 | |
251 | } | |
135f2a9f | 252 | const worker = new ClusterWorker({ fn1, fn2 }) |
6934964f JB |
253 | expect(() => worker.removeTaskFunction(0, fn1)).toThrowError( |
254 | new TypeError('name parameter is not a string') | |
255 | ) | |
256 | expect(() => worker.removeTaskFunction('', fn1)).toThrowError( | |
257 | new TypeError('name parameter is an empty string') | |
258 | ) | |
90d7d101 JB |
259 | worker.getMainWorker = sinon.stub().returns({ |
260 | id: 1, | |
261 | send: sinon.stub().returns() | |
262 | }) | |
6cd5248f | 263 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
2a69b8c5 JB |
264 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
265 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) | |
266 | expect(worker.taskFunctions.size).toBe(3) | |
6cd5248f | 267 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
268 | worker.taskFunctions.get('fn1') |
269 | ) | |
6cd5248f | 270 | expect(() => worker.removeTaskFunction(DEFAULT_TASK_NAME)).toThrowError( |
2a69b8c5 JB |
271 | new Error( |
272 | 'Cannot remove the task function with the default reserved name' | |
273 | ) | |
274 | ) | |
275 | expect(() => worker.removeTaskFunction('fn1')).toThrowError( | |
276 | new Error( | |
277 | 'Cannot remove the task function used as the default task function' | |
278 | ) | |
279 | ) | |
280 | worker.removeTaskFunction('fn2') | |
6cd5248f | 281 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
2a69b8c5 JB |
282 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
283 | expect(worker.taskFunctions.get('fn2')).toBeUndefined() | |
284 | expect(worker.taskFunctions.size).toBe(2) | |
90d7d101 | 285 | expect(worker.getMainWorker().send.calledOnce).toBe(true) |
2a69b8c5 JB |
286 | }) |
287 | ||
c50b93fb JB |
288 | it('Verify that listTaskFunctions() works', () => { |
289 | const fn1 = () => { | |
290 | return 1 | |
291 | } | |
292 | const fn2 = () => { | |
293 | return 2 | |
294 | } | |
295 | const worker = new ClusterWorker({ fn1, fn2 }) | |
6cd5248f JB |
296 | expect(worker.listTaskFunctions()).toStrictEqual([ |
297 | DEFAULT_TASK_NAME, | |
298 | 'fn1', | |
299 | 'fn2' | |
300 | ]) | |
c50b93fb JB |
301 | }) |
302 | ||
2a69b8c5 JB |
303 | it('Verify that setDefaultTaskFunction() works', () => { |
304 | const fn1 = () => { | |
305 | return 1 | |
306 | } | |
307 | const fn2 = () => { | |
308 | return 2 | |
309 | } | |
310 | const worker = new ThreadWorker({ fn1, fn2 }) | |
6934964f JB |
311 | expect(() => worker.setDefaultTaskFunction(0, fn1)).toThrowError( |
312 | new TypeError('name parameter is not a string') | |
313 | ) | |
314 | expect(() => worker.setDefaultTaskFunction('', fn1)).toThrowError( | |
315 | new TypeError('name parameter is an empty string') | |
316 | ) | |
6cd5248f | 317 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
2a69b8c5 JB |
318 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
319 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) | |
320 | expect(worker.taskFunctions.size).toBe(3) | |
6cd5248f | 321 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
322 | worker.taskFunctions.get('fn1') |
323 | ) | |
6cd5248f | 324 | expect(() => worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toThrowError( |
2a69b8c5 JB |
325 | new Error( |
326 | 'Cannot set the default task function reserved name as the default task function' | |
327 | ) | |
328 | ) | |
6934964f JB |
329 | expect(() => worker.setDefaultTaskFunction('fn3')).toThrowError( |
330 | new Error( | |
331 | 'Cannot set the default task function to a non-existing task function' | |
332 | ) | |
333 | ) | |
2a69b8c5 | 334 | worker.setDefaultTaskFunction('fn1') |
6cd5248f | 335 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
336 | worker.taskFunctions.get('fn1') |
337 | ) | |
338 | worker.setDefaultTaskFunction('fn2') | |
6cd5248f | 339 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
340 | worker.taskFunctions.get('fn2') |
341 | ) | |
342 | }) | |
c510fea7 | 343 | }) |