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