test: improve addTaskFunction() tests
[poolifier.git] / tests / worker / abstract-worker.test.mjs
CommitLineData
a074ffee
JB
1import { expect } from 'expect'
2import { restore, stub } from 'sinon'
ded253e2 3
d0bd5062
JB
4import {
5 ClusterWorker,
6 KillBehaviors,
7 ThreadWorker,
8 WorkerChoiceStrategies
9} from '../../lib/index.cjs'
d35e5717 10import { DEFAULT_TASK_NAME, EMPTY_FUNCTION } from '../../lib/utils.cjs'
7fc5cce6 11
e1ffb94f 12describe('Abstract worker test suite', () => {
1f68cede 13 class StubWorkerWithMainWorker extends ThreadWorker {
e1ffb94f
JB
14 constructor (fn, opts) {
15 super(fn, opts)
41072404 16 delete this.mainWorker
e1ffb94f 17 }
7fc5cce6 18 }
c510fea7 19
dc021bcc 20 afterEach(() => {
a074ffee 21 restore()
dc021bcc
JB
22 })
23
e088a00c 24 it('Verify worker options default values', () => {
8620fb25 25 const worker = new ThreadWorker(() => {})
cca3bb1a
JB
26 expect(worker.opts).toStrictEqual({
27 killBehavior: KillBehaviors.SOFT,
28 maxInactiveTime: 60000,
29 killHandler: EMPTY_FUNCTION
30 })
8620fb25
JB
31 })
32
c20084b6 33 it('Verify that worker options are checked at worker creation', () => {
948faff7 34 expect(() => new ClusterWorker(() => {}, '')).toThrow(
c20084b6
JB
35 new TypeError('opts worker options parameter is not a plain object')
36 )
948faff7
JB
37 expect(() => new ClusterWorker(() => {}, { killBehavior: '' })).toThrow(
38 new TypeError("killBehavior option '' is not valid")
39 )
40 expect(() => new ClusterWorker(() => {}, { killBehavior: 0 })).toThrow(
c20084b6
JB
41 new TypeError("killBehavior option '0' is not valid")
42 )
948faff7
JB
43 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: '' })).toThrow(
44 new TypeError('maxInactiveTime option is not an integer')
45 )
46 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 0.5 })).toThrow(
47 new TypeError('maxInactiveTime option is not an integer')
48 )
49 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 0 })).toThrow(
c20084b6
JB
50 new TypeError(
51 'maxInactiveTime option is not a positive integer greater or equal than 5'
52 )
53 )
948faff7 54 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 4 })).toThrow(
c20084b6
JB
55 new TypeError(
56 'maxInactiveTime option is not a positive integer greater or equal than 5'
57 )
58 )
948faff7 59 expect(() => new ThreadWorker(() => {}, { killHandler: '' })).toThrow(
c20084b6
JB
60 new TypeError('killHandler option is not a function')
61 )
948faff7 62 expect(() => new ThreadWorker(() => {}, { killHandler: 0 })).toThrow(
c20084b6
JB
63 new TypeError('killHandler option is not a function')
64 )
c20084b6
JB
65 })
66
8620fb25 67 it('Verify that worker options are set at worker creation', () => {
df9aaf20
JB
68 const killHandler = () => {
69 console.info('Worker received kill message')
70 }
8620fb25 71 const worker = new ClusterWorker(() => {}, {
df9aaf20 72 killBehavior: KillBehaviors.HARD,
cca3bb1a 73 maxInactiveTime: 6000,
c20084b6 74 killHandler
8620fb25 75 })
cca3bb1a
JB
76 expect(worker.opts).toStrictEqual({
77 killBehavior: KillBehaviors.HARD,
78 maxInactiveTime: 6000,
79 killHandler
80 })
8620fb25
JB
81 })
82
a86b6df1 83 it('Verify that taskFunctions parameter is mandatory', () => {
948faff7 84 expect(() => new ClusterWorker()).toThrow(
c20084b6 85 new Error('taskFunctions parameter is mandatory')
a86b6df1 86 )
d4aeae5a
JB
87 })
88
f34fdabe 89 it('Verify that taskFunctions parameter is a function or a plain object', () => {
948faff7 90 expect(() => new ClusterWorker(0)).toThrow(
f34fdabe
JB
91 new TypeError(
92 'taskFunctions parameter is not a function or a plain object'
93 )
d4aeae5a 94 )
948faff7 95 expect(() => new ClusterWorker('')).toThrow(
f34fdabe
JB
96 new TypeError(
97 'taskFunctions parameter is not a function or a plain object'
98 )
a86b6df1 99 )
948faff7 100 expect(() => new ClusterWorker(true)).toThrow(
f34fdabe
JB
101 new TypeError(
102 'taskFunctions parameter is not a function or a plain object'
103 )
d4aeae5a 104 )
948faff7 105 expect(() => new ClusterWorker([])).toThrow(
f34fdabe
JB
106 new TypeError(
107 'taskFunctions parameter is not a function or a plain object'
108 )
a86b6df1 109 )
948faff7 110 expect(() => new ClusterWorker(new Map())).toThrow(
f34fdabe
JB
111 new TypeError(
112 'taskFunctions parameter is not a function or a plain object'
113 )
a86b6df1 114 )
948faff7 115 expect(() => new ClusterWorker(new Set())).toThrow(
f34fdabe
JB
116 new TypeError(
117 'taskFunctions parameter is not a function or a plain object'
118 )
a86b6df1 119 )
948faff7 120 expect(() => new ClusterWorker(new WeakMap())).toThrow(
f34fdabe
JB
121 new TypeError(
122 'taskFunctions parameter is not a function or a plain object'
123 )
d4aeae5a 124 )
948faff7 125 expect(() => new ClusterWorker(new WeakSet())).toThrow(
f34fdabe
JB
126 new TypeError(
127 'taskFunctions parameter is not a function or a plain object'
128 )
a86b6df1 129 )
f34fdabe
JB
130 })
131
132 it('Verify that taskFunctions parameter is not an empty object', () => {
948faff7 133 expect(() => new ClusterWorker({})).toThrow(
0d80593b 134 new Error('taskFunctions parameter object is empty')
630f0acf 135 )
a86b6df1
JB
136 })
137
2a69b8c5 138 it('Verify that taskFunctions parameter with unique function is taken', () => {
f509d987 139 const worker = new ThreadWorker(() => {})
915040cc
JB
140 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
141 taskFunction: expect.any(Function)
142 })
143 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
144 taskFunction: expect.any(Function)
145 })
2a69b8c5 146 expect(worker.taskFunctions.size).toBe(2)
6cd5248f 147 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
148 worker.taskFunctions.get('fn1')
149 )
150 })
151
6934964f 152 it('Verify that taskFunctions parameter with multiple task functions is checked', () => {
f34fdabe
JB
153 const fn1 = () => {
154 return 1
155 }
156 const fn2 = ''
948faff7 157 expect(() => new ThreadWorker({ '': fn1 })).toThrow(
6934964f
JB
158 new TypeError('A taskFunctions parameter object key is an empty string')
159 )
948faff7 160 expect(() => new ThreadWorker({ fn1, fn2 })).toThrow(
31847469
JB
161 new TypeError(
162 "taskFunction object 'taskFunction' property 'undefined' is not a function"
163 )
f34fdabe 164 )
d0bd5062
JB
165 expect(() => new ThreadWorker({ fn1: { fn1 } })).toThrow(
166 new TypeError(
167 "taskFunction object 'taskFunction' property 'undefined' is not a function"
168 )
169 )
170 expect(() => new ThreadWorker({ fn2: { taskFunction: fn2 } })).toThrow(
171 new TypeError(
172 "taskFunction object 'taskFunction' property '' is not a function"
173 )
174 )
175 expect(
176 () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: '' } })
85bbc7ab 177 ).toThrow(new TypeError("Invalid property 'priority': ''"))
d0bd5062
JB
178 expect(
179 () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: -21 } })
19c42d90 180 ).toThrow(new RangeError("Property 'priority' must be between -20 and 19"))
d0bd5062
JB
181 expect(
182 () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: 20 } })
85bbc7ab 183 ).toThrow(new RangeError("Property 'priority' must be between -20 and 19"))
d0bd5062
JB
184 expect(
185 () =>
186 new ThreadWorker({
187 fn1: { taskFunction: fn1, strategy: 'invalidStrategy' }
188 })
19c42d90 189 ).toThrow(new Error("Invalid worker choice strategy 'invalidStrategy'"))
f34fdabe
JB
190 })
191
a86b6df1
JB
192 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
193 const fn1 = () => {
194 return 1
195 }
196 const fn2 = () => {
197 return 2
198 }
199 const worker = new ClusterWorker({ fn1, fn2 })
915040cc
JB
200 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
201 taskFunction: expect.any(Function)
202 })
203 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
204 taskFunction: expect.any(Function)
205 })
206 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
207 taskFunction: expect.any(Function)
208 })
2a69b8c5 209 expect(worker.taskFunctions.size).toBe(3)
6cd5248f 210 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
211 worker.taskFunctions.get('fn1')
212 )
d4aeae5a
JB
213 })
214
d0bd5062
JB
215 it('Verify that taskFunctions parameter with multiple task functions object is taken', () => {
216 const fn1Obj = {
217 taskFunction: () => {
218 return 1
219 },
220 priority: 5
221 }
222 const fn2Obj = {
223 taskFunction: () => {
224 return 2
225 },
226 priority: 6,
227 strategy: WorkerChoiceStrategies.LESS_BUSY
228 }
229 const worker = new ThreadWorker({
230 fn1: fn1Obj,
231 fn2: fn2Obj
232 })
233 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(fn1Obj)
234 expect(worker.taskFunctions.get('fn1')).toStrictEqual(fn1Obj)
235 expect(worker.taskFunctions.get('fn2')).toStrictEqual(fn2Obj)
236 expect(worker.taskFunctions.size).toBe(3)
237 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
238 worker.taskFunctions.get('fn1')
239 )
240 })
241
07588f30 242 it('Verify that async kill handler is called when worker is killed', () => {
a074ffee 243 const killHandlerStub = stub().returns()
07588f30 244 const worker = new ClusterWorker(() => {}, {
1cc6e9ef 245 killHandler: async () => await Promise.resolve(killHandlerStub())
07588f30
JB
246 })
247 worker.isMain = false
248 worker.handleKillMessage()
249 expect(killHandlerStub.calledOnce).toBe(true)
250 })
df9aaf20 251
318d4156 252 it('Verify that getMainWorker() throw error if main worker is not set', () => {
7fc5cce6 253 expect(() =>
1f68cede 254 new StubWorkerWithMainWorker(() => {}).getMainWorker()
948faff7 255 ).toThrow('Main worker not set')
7fc5cce6 256 })
2a69b8c5 257
9eae3c69 258 it('Verify that hasTaskFunction() is working', () => {
2a69b8c5
JB
259 const fn1 = () => {
260 return 1
261 }
262 const fn2 = () => {
263 return 2
264 }
265 const worker = new ClusterWorker({ fn1, fn2 })
66979634
JB
266 expect(worker.hasTaskFunction(0)).toStrictEqual({
267 status: false,
268 error: new TypeError('name parameter is not a string')
269 })
270 expect(worker.hasTaskFunction('')).toStrictEqual({
271 status: false,
272 error: new TypeError('name parameter is an empty string')
273 })
274 expect(worker.hasTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
275 status: true
276 })
277 expect(worker.hasTaskFunction('fn1')).toStrictEqual({ status: true })
278 expect(worker.hasTaskFunction('fn2')).toStrictEqual({ status: true })
279 expect(worker.hasTaskFunction('fn3')).toStrictEqual({ status: false })
2a69b8c5
JB
280 })
281
9eae3c69 282 it('Verify that addTaskFunction() is working', () => {
2a69b8c5
JB
283 const fn1 = () => {
284 return 1
285 }
286 const fn2 = () => {
287 return 2
288 }
289 const fn1Replacement = () => {
290 return 3
291 }
292 const worker = new ThreadWorker(fn1)
66979634
JB
293 expect(worker.addTaskFunction(0, fn1)).toStrictEqual({
294 status: false,
295 error: new TypeError('name parameter is not a string')
296 })
297 expect(worker.addTaskFunction('', fn1)).toStrictEqual({
298 status: false,
299 error: new TypeError('name parameter is an empty string')
300 })
19c42d90
JB
301 expect(worker.addTaskFunction('fn2', 0)).toStrictEqual({
302 status: false,
303 error: new TypeError(
304 "taskFunction object 'taskFunction' property 'undefined' is not a function"
305 )
306 })
66979634
JB
307 expect(worker.addTaskFunction('fn3', '')).toStrictEqual({
308 status: false,
31847469
JB
309 error: new TypeError(
310 "taskFunction object 'taskFunction' property 'undefined' is not a function"
311 )
66979634 312 })
19c42d90
JB
313 expect(worker.addTaskFunction('fn2', { taskFunction: 0 })).toStrictEqual({
314 status: false,
315 error: new TypeError(
316 "taskFunction object 'taskFunction' property '0' is not a function"
317 )
318 })
319 expect(worker.addTaskFunction('fn3', { taskFunction: '' })).toStrictEqual({
320 status: false,
321 error: new TypeError(
322 "taskFunction object 'taskFunction' property '' is not a function"
323 )
324 })
325 expect(
326 worker.addTaskFunction('fn2', { taskFunction: () => {}, priority: -21 })
327 ).toStrictEqual({
328 status: false,
329 error: new RangeError("Property 'priority' must be between -20 and 19")
330 })
331 expect(
332 worker.addTaskFunction('fn3', { taskFunction: () => {}, priority: 20 })
333 ).toStrictEqual({
334 status: false,
335 error: new RangeError("Property 'priority' must be between -20 and 19")
336 })
337 expect(
338 worker.addTaskFunction('fn2', {
339 taskFunction: () => {},
340 strategy: 'invalidStrategy'
341 })
342 ).toStrictEqual({
343 status: false,
344 error: new Error("Invalid worker choice strategy 'invalidStrategy'")
345 })
915040cc
JB
346 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
347 taskFunction: expect.any(Function)
348 })
349 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
350 taskFunction: expect.any(Function)
351 })
2a69b8c5 352 expect(worker.taskFunctions.size).toBe(2)
6cd5248f 353 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
354 worker.taskFunctions.get('fn1')
355 )
66979634
JB
356 expect(worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toStrictEqual({
357 status: false,
358 error: new Error(
359 'Cannot add a task function with the default reserved name'
360 )
361 })
2a69b8c5 362 worker.addTaskFunction('fn2', fn2)
915040cc
JB
363 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
364 taskFunction: expect.any(Function)
365 })
366 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
367 taskFunction: expect.any(Function)
368 })
369 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
370 taskFunction: expect.any(Function)
371 })
2a69b8c5 372 expect(worker.taskFunctions.size).toBe(3)
6cd5248f 373 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
374 worker.taskFunctions.get('fn1')
375 )
376 worker.addTaskFunction('fn1', fn1Replacement)
915040cc
JB
377 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
378 taskFunction: expect.any(Function)
379 })
380 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
381 taskFunction: expect.any(Function)
382 })
383 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
384 taskFunction: expect.any(Function)
385 })
2a69b8c5 386 expect(worker.taskFunctions.size).toBe(3)
6cd5248f 387 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
388 worker.taskFunctions.get('fn1')
389 )
390 })
391
f7a08a34 392 it('Verify that listTaskFunctionsProperties() is working', () => {
c50b93fb
JB
393 const fn1 = () => {
394 return 1
395 }
396 const fn2 = () => {
397 return 2
398 }
399 const worker = new ClusterWorker({ fn1, fn2 })
31847469
JB
400 expect(worker.listTaskFunctionsProperties()).toStrictEqual([
401 { name: DEFAULT_TASK_NAME },
402 { name: 'fn1' },
403 { name: 'fn2' }
6cd5248f 404 ])
c50b93fb
JB
405 })
406
9eae3c69 407 it('Verify that setDefaultTaskFunction() is working', () => {
2a69b8c5
JB
408 const fn1 = () => {
409 return 1
410 }
411 const fn2 = () => {
412 return 2
413 }
414 const worker = new ThreadWorker({ fn1, fn2 })
66979634
JB
415 expect(worker.setDefaultTaskFunction(0, fn1)).toStrictEqual({
416 status: false,
417 error: new TypeError('name parameter is not a string')
418 })
419 expect(worker.setDefaultTaskFunction('', fn1)).toStrictEqual({
420 status: false,
421 error: new TypeError('name parameter is an empty string')
422 })
915040cc
JB
423 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
424 taskFunction: expect.any(Function)
425 })
426 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
427 taskFunction: expect.any(Function)
428 })
429 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
430 taskFunction: expect.any(Function)
431 })
2a69b8c5 432 expect(worker.taskFunctions.size).toBe(3)
6cd5248f 433 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
434 worker.taskFunctions.get('fn1')
435 )
66979634
JB
436 expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
437 status: false,
438 error: new Error(
2a69b8c5
JB
439 'Cannot set the default task function reserved name as the default task function'
440 )
66979634
JB
441 })
442 expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({
443 status: false,
444 error: new Error(
6934964f
JB
445 'Cannot set the default task function to a non-existing task function'
446 )
66979634 447 })
2a69b8c5 448 worker.setDefaultTaskFunction('fn1')
6cd5248f 449 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
450 worker.taskFunctions.get('fn1')
451 )
452 worker.setDefaultTaskFunction('fn2')
6cd5248f 453 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
454 worker.taskFunctions.get('fn2')
455 )
456 })
c510fea7 457})