chore(deps-dev): bump tatami-ng to 0.6.0
[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,
3a502712 8 WorkerChoiceStrategies,
d0bd5062 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,
3a502712 28 killHandler: EMPTY_FUNCTION,
97231086 29 maxInactiveTime: 60000,
cca3bb1a 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,
3a502712 73 killHandler,
97231086 74 maxInactiveTime: 6000,
8620fb25 75 })
cca3bb1a
JB
76 expect(worker.opts).toStrictEqual({
77 killBehavior: KillBehaviors.HARD,
3a502712 78 killHandler,
97231086 79 maxInactiveTime: 6000,
cca3bb1a 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 140 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
3a502712 141 taskFunction: expect.any(Function),
915040cc
JB
142 })
143 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
3a502712 144 taskFunction: expect.any(Function),
915040cc 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(
97231086 176 () => new ThreadWorker({ fn1: { priority: '', taskFunction: fn1 } })
85bbc7ab 177 ).toThrow(new TypeError("Invalid property 'priority': ''"))
d0bd5062 178 expect(
97231086 179 () => new ThreadWorker({ fn1: { priority: -21, taskFunction: fn1 } })
19c42d90 180 ).toThrow(new RangeError("Property 'priority' must be between -20 and 19"))
d0bd5062 181 expect(
97231086 182 () => new ThreadWorker({ fn1: { priority: 20, taskFunction: fn1 } })
85bbc7ab 183 ).toThrow(new RangeError("Property 'priority' must be between -20 and 19"))
d0bd5062
JB
184 expect(
185 () =>
186 new ThreadWorker({
97231086 187 fn1: { strategy: 'invalidStrategy', taskFunction: fn1 },
d0bd5062 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 200 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
3a502712 201 taskFunction: expect.any(Function),
915040cc
JB
202 })
203 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
3a502712 204 taskFunction: expect.any(Function),
915040cc
JB
205 })
206 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
3a502712 207 taskFunction: expect.any(Function),
915040cc 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 = {
97231086 217 priority: 5,
d0bd5062
JB
218 taskFunction: () => {
219 return 1
220 },
d0bd5062
JB
221 }
222 const fn2Obj = {
97231086
JB
223 priority: 6,
224 strategy: WorkerChoiceStrategies.LESS_BUSY,
d0bd5062
JB
225 taskFunction: () => {
226 return 2
227 },
d0bd5062
JB
228 }
229 const worker = new ThreadWorker({
230 fn1: fn1Obj,
3a502712 231 fn2: fn2Obj,
d0bd5062
JB
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(() => {}, {
3a502712 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 266 expect(worker.hasTaskFunction(0)).toStrictEqual({
3a502712 267 error: new TypeError('name parameter is not a string'),
97231086 268 status: false,
66979634
JB
269 })
270 expect(worker.hasTaskFunction('')).toStrictEqual({
3a502712 271 error: new TypeError('name parameter is an empty string'),
97231086 272 status: false,
66979634
JB
273 })
274 expect(worker.hasTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
3a502712 275 status: true,
66979634
JB
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 293 expect(worker.addTaskFunction(0, fn1)).toStrictEqual({
3a502712 294 error: new TypeError('name parameter is not a string'),
97231086 295 status: false,
66979634
JB
296 })
297 expect(worker.addTaskFunction('', fn1)).toStrictEqual({
3a502712 298 error: new TypeError('name parameter is an empty string'),
97231086 299 status: false,
66979634 300 })
19c42d90 301 expect(worker.addTaskFunction('fn2', 0)).toStrictEqual({
19c42d90
JB
302 error: new TypeError(
303 "taskFunction object 'taskFunction' property 'undefined' is not a function"
3a502712 304 ),
97231086 305 status: false,
19c42d90 306 })
66979634 307 expect(worker.addTaskFunction('fn3', '')).toStrictEqual({
31847469
JB
308 error: new TypeError(
309 "taskFunction object 'taskFunction' property 'undefined' is not a function"
3a502712 310 ),
97231086 311 status: false,
66979634 312 })
19c42d90 313 expect(worker.addTaskFunction('fn2', { taskFunction: 0 })).toStrictEqual({
19c42d90
JB
314 error: new TypeError(
315 "taskFunction object 'taskFunction' property '0' is not a function"
3a502712 316 ),
97231086 317 status: false,
19c42d90
JB
318 })
319 expect(worker.addTaskFunction('fn3', { taskFunction: '' })).toStrictEqual({
19c42d90
JB
320 error: new TypeError(
321 "taskFunction object 'taskFunction' property '' is not a function"
3a502712 322 ),
97231086 323 status: false,
19c42d90
JB
324 })
325 expect(
97231086 326 worker.addTaskFunction('fn2', { priority: -21, taskFunction: () => {} })
19c42d90 327 ).toStrictEqual({
3a502712 328 error: new RangeError("Property 'priority' must be between -20 and 19"),
97231086 329 status: false,
19c42d90
JB
330 })
331 expect(
97231086 332 worker.addTaskFunction('fn3', { priority: 20, taskFunction: () => {} })
19c42d90 333 ).toStrictEqual({
3a502712 334 error: new RangeError("Property 'priority' must be between -20 and 19"),
97231086 335 status: false,
19c42d90
JB
336 })
337 expect(
338 worker.addTaskFunction('fn2', {
3a502712 339 strategy: 'invalidStrategy',
97231086 340 taskFunction: () => {},
19c42d90
JB
341 })
342 ).toStrictEqual({
3a502712 343 error: new Error("Invalid worker choice strategy 'invalidStrategy'"),
97231086 344 status: false,
19c42d90 345 })
915040cc 346 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
3a502712 347 taskFunction: expect.any(Function),
915040cc
JB
348 })
349 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
3a502712 350 taskFunction: expect.any(Function),
915040cc 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 356 expect(worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toStrictEqual({
66979634
JB
357 error: new Error(
358 'Cannot add a task function with the default reserved name'
3a502712 359 ),
97231086 360 status: false,
66979634 361 })
2a69b8c5 362 worker.addTaskFunction('fn2', fn2)
915040cc 363 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
3a502712 364 taskFunction: expect.any(Function),
915040cc
JB
365 })
366 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
3a502712 367 taskFunction: expect.any(Function),
915040cc
JB
368 })
369 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
3a502712 370 taskFunction: expect.any(Function),
915040cc 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 377 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
3a502712 378 taskFunction: expect.any(Function),
915040cc
JB
379 })
380 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
3a502712 381 taskFunction: expect.any(Function),
915040cc
JB
382 })
383 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
3a502712 384 taskFunction: expect.any(Function),
915040cc 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' },
3a502712 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 415 expect(worker.setDefaultTaskFunction(0, fn1)).toStrictEqual({
3a502712 416 error: new TypeError('name parameter is not a string'),
97231086 417 status: false,
66979634
JB
418 })
419 expect(worker.setDefaultTaskFunction('', fn1)).toStrictEqual({
3a502712 420 error: new TypeError('name parameter is an empty string'),
97231086 421 status: false,
66979634 422 })
915040cc 423 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
3a502712 424 taskFunction: expect.any(Function),
915040cc
JB
425 })
426 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
3a502712 427 taskFunction: expect.any(Function),
915040cc
JB
428 })
429 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
3a502712 430 taskFunction: expect.any(Function),
915040cc 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 436 expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
66979634 437 error: new Error(
2a69b8c5 438 'Cannot set the default task function reserved name as the default task function'
3a502712 439 ),
97231086 440 status: false,
66979634
JB
441 })
442 expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({
66979634 443 error: new Error(
6934964f 444 'Cannot set the default task function to a non-existing task function'
3a502712 445 ),
97231086 446 status: false,
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})