chore(deps-dev): apply updates
[poolifier.git] / tests / worker / abstract-worker.test.mjs
1 import { expect } from '@std/expect'
2 import { restore, stub } from 'sinon'
3
4 import {
5 ClusterWorker,
6 KillBehaviors,
7 ThreadWorker,
8 WorkerChoiceStrategies,
9 } from '../../lib/index.cjs'
10 import { DEFAULT_TASK_NAME, EMPTY_FUNCTION } from '../../lib/utils.cjs'
11
12 describe('Abstract worker test suite', () => {
13 class StubWorkerWithMainWorker extends ThreadWorker {
14 constructor (fn, opts) {
15 super(fn, opts)
16 delete this.mainWorker
17 }
18 }
19
20 afterEach(() => {
21 restore()
22 })
23
24 it('Verify worker options default values', () => {
25 const worker = new ThreadWorker(() => {})
26 expect(worker.opts).toStrictEqual({
27 killBehavior: KillBehaviors.SOFT,
28 killHandler: EMPTY_FUNCTION,
29 maxInactiveTime: 60000,
30 })
31 })
32
33 it('Verify that worker options are checked at worker creation', () => {
34 expect(() => new ClusterWorker(() => {}, '')).toThrow(
35 new TypeError('opts worker options parameter is not a plain object')
36 )
37 expect(() => new ClusterWorker(() => {}, { killBehavior: '' })).toThrow(
38 new TypeError("killBehavior option '' is not valid")
39 )
40 expect(() => new ClusterWorker(() => {}, { killBehavior: 0 })).toThrow(
41 new TypeError("killBehavior option '0' is not valid")
42 )
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(
50 new TypeError(
51 'maxInactiveTime option is not a positive integer greater or equal than 5'
52 )
53 )
54 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 4 })).toThrow(
55 new TypeError(
56 'maxInactiveTime option is not a positive integer greater or equal than 5'
57 )
58 )
59 expect(() => new ThreadWorker(() => {}, { killHandler: '' })).toThrow(
60 new TypeError('killHandler option is not a function')
61 )
62 expect(() => new ThreadWorker(() => {}, { killHandler: 0 })).toThrow(
63 new TypeError('killHandler option is not a function')
64 )
65 })
66
67 it('Verify that worker options are set at worker creation', () => {
68 const killHandler = () => {
69 console.info('Worker received kill message')
70 }
71 const worker = new ClusterWorker(() => {}, {
72 killBehavior: KillBehaviors.HARD,
73 killHandler,
74 maxInactiveTime: 6000,
75 })
76 expect(worker.opts).toStrictEqual({
77 killBehavior: KillBehaviors.HARD,
78 killHandler,
79 maxInactiveTime: 6000,
80 })
81 })
82
83 it('Verify that taskFunctions parameter is mandatory', () => {
84 expect(() => new ClusterWorker()).toThrow(
85 new Error('taskFunctions parameter is mandatory')
86 )
87 })
88
89 it('Verify that taskFunctions parameter is a function or a plain object', () => {
90 expect(() => new ClusterWorker(0)).toThrow(
91 new TypeError(
92 'taskFunctions parameter is not a function or a plain object'
93 )
94 )
95 expect(() => new ClusterWorker('')).toThrow(
96 new TypeError(
97 'taskFunctions parameter is not a function or a plain object'
98 )
99 )
100 expect(() => new ClusterWorker(true)).toThrow(
101 new TypeError(
102 'taskFunctions parameter is not a function or a plain object'
103 )
104 )
105 expect(() => new ClusterWorker([])).toThrow(
106 new TypeError(
107 'taskFunctions parameter is not a function or a plain object'
108 )
109 )
110 expect(() => new ClusterWorker(new Map())).toThrow(
111 new TypeError(
112 'taskFunctions parameter is not a function or a plain object'
113 )
114 )
115 expect(() => new ClusterWorker(new Set())).toThrow(
116 new TypeError(
117 'taskFunctions parameter is not a function or a plain object'
118 )
119 )
120 expect(() => new ClusterWorker(new WeakMap())).toThrow(
121 new TypeError(
122 'taskFunctions parameter is not a function or a plain object'
123 )
124 )
125 expect(() => new ClusterWorker(new WeakSet())).toThrow(
126 new TypeError(
127 'taskFunctions parameter is not a function or a plain object'
128 )
129 )
130 })
131
132 it('Verify that taskFunctions parameter is not an empty object', () => {
133 expect(() => new ClusterWorker({})).toThrow(
134 new Error('taskFunctions parameter object is empty')
135 )
136 })
137
138 it('Verify that taskFunctions parameter with unique function is taken', () => {
139 const worker = new ThreadWorker(() => {})
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 })
146 expect(worker.taskFunctions.size).toBe(2)
147 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
148 worker.taskFunctions.get('fn1')
149 )
150 })
151
152 it('Verify that taskFunctions parameter with multiple task functions is checked', () => {
153 const fn1 = () => {
154 return 1
155 }
156 const fn2 = ''
157 expect(() => new ThreadWorker({ '': fn1 })).toThrow(
158 new TypeError('A taskFunctions parameter object key is an empty string')
159 )
160 expect(() => new ThreadWorker({ fn1, fn2 })).toThrow(
161 new TypeError(
162 "taskFunction object 'taskFunction' property 'undefined' is not a function"
163 )
164 )
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: { priority: '', taskFunction: fn1 } })
177 ).toThrow(new TypeError("Invalid property 'priority': ''"))
178 expect(
179 () => new ThreadWorker({ fn1: { priority: -21, taskFunction: fn1 } })
180 ).toThrow(new RangeError("Property 'priority' must be between -20 and 19"))
181 expect(
182 () => new ThreadWorker({ fn1: { priority: 20, taskFunction: fn1 } })
183 ).toThrow(new RangeError("Property 'priority' must be between -20 and 19"))
184 expect(
185 () =>
186 new ThreadWorker({
187 fn1: { strategy: 'invalidStrategy', taskFunction: fn1 },
188 })
189 ).toThrow(new Error("Invalid worker choice strategy 'invalidStrategy'"))
190 })
191
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 })
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 })
209 expect(worker.taskFunctions.size).toBe(3)
210 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
211 worker.taskFunctions.get('fn1')
212 )
213 })
214
215 it('Verify that taskFunctions parameter with multiple task functions object is taken', () => {
216 const fn1Obj = {
217 priority: 5,
218 taskFunction: () => {
219 return 1
220 },
221 }
222 const fn2Obj = {
223 priority: 6,
224 strategy: WorkerChoiceStrategies.LESS_BUSY,
225 taskFunction: () => {
226 return 2
227 },
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
242 it('Verify that async kill handler is called when worker is killed', () => {
243 const killHandlerStub = stub().returns()
244 const worker = new ClusterWorker(() => {}, {
245 killHandler: async () => await Promise.resolve(killHandlerStub()),
246 })
247 worker.isMain = false
248 worker.handleKillMessage()
249 expect(killHandlerStub.calledOnce).toBe(true)
250 })
251
252 it('Verify that getMainWorker() throw error if main worker is not set', () => {
253 expect(() =>
254 new StubWorkerWithMainWorker(() => {}).getMainWorker()
255 ).toThrow('Main worker not set')
256 })
257
258 it('Verify that hasTaskFunction() is working', () => {
259 const fn1 = () => {
260 return 1
261 }
262 const fn2 = () => {
263 return 2
264 }
265 const worker = new ClusterWorker({ fn1, fn2 })
266 expect(worker.hasTaskFunction(0)).toStrictEqual({
267 error: new TypeError('name parameter is not a string'),
268 status: false,
269 })
270 expect(worker.hasTaskFunction('')).toStrictEqual({
271 error: new TypeError('name parameter is an empty string'),
272 status: false,
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 })
280 })
281
282 it('Verify that addTaskFunction() is working', () => {
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)
293 expect(worker.addTaskFunction(0, fn1)).toStrictEqual({
294 error: new TypeError('name parameter is not a string'),
295 status: false,
296 })
297 expect(worker.addTaskFunction('', fn1)).toStrictEqual({
298 error: new TypeError('name parameter is an empty string'),
299 status: false,
300 })
301 expect(worker.addTaskFunction('fn2', 0)).toStrictEqual({
302 error: new TypeError(
303 "taskFunction object 'taskFunction' property 'undefined' is not a function"
304 ),
305 status: false,
306 })
307 expect(worker.addTaskFunction('fn3', '')).toStrictEqual({
308 error: new TypeError(
309 "taskFunction object 'taskFunction' property 'undefined' is not a function"
310 ),
311 status: false,
312 })
313 expect(worker.addTaskFunction('fn2', { taskFunction: 0 })).toStrictEqual({
314 error: new TypeError(
315 "taskFunction object 'taskFunction' property '0' is not a function"
316 ),
317 status: false,
318 })
319 expect(worker.addTaskFunction('fn3', { taskFunction: '' })).toStrictEqual({
320 error: new TypeError(
321 "taskFunction object 'taskFunction' property '' is not a function"
322 ),
323 status: false,
324 })
325 expect(
326 worker.addTaskFunction('fn2', { priority: -21, taskFunction: () => {} })
327 ).toStrictEqual({
328 error: new RangeError("Property 'priority' must be between -20 and 19"),
329 status: false,
330 })
331 expect(
332 worker.addTaskFunction('fn3', { priority: 20, taskFunction: () => {} })
333 ).toStrictEqual({
334 error: new RangeError("Property 'priority' must be between -20 and 19"),
335 status: false,
336 })
337 expect(
338 worker.addTaskFunction('fn2', {
339 strategy: 'invalidStrategy',
340 taskFunction: () => {},
341 })
342 ).toStrictEqual({
343 error: new Error("Invalid worker choice strategy 'invalidStrategy'"),
344 status: false,
345 })
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 })
352 expect(worker.taskFunctions.size).toBe(2)
353 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
354 worker.taskFunctions.get('fn1')
355 )
356 expect(worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toStrictEqual({
357 error: new Error(
358 'Cannot add a task function with the default reserved name'
359 ),
360 status: false,
361 })
362 worker.addTaskFunction('fn2', fn2)
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 })
372 expect(worker.taskFunctions.size).toBe(3)
373 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
374 worker.taskFunctions.get('fn1')
375 )
376 worker.addTaskFunction('fn1', fn1Replacement)
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 })
386 expect(worker.taskFunctions.size).toBe(3)
387 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
388 worker.taskFunctions.get('fn1')
389 )
390 })
391
392 it('Verify that listTaskFunctionsProperties() is working', () => {
393 const fn1 = () => {
394 return 1
395 }
396 const fn2 = () => {
397 return 2
398 }
399 const worker = new ClusterWorker({ fn1, fn2 })
400 expect(worker.listTaskFunctionsProperties()).toStrictEqual([
401 { name: DEFAULT_TASK_NAME },
402 { name: 'fn1' },
403 { name: 'fn2' },
404 ])
405 })
406
407 it('Verify that setDefaultTaskFunction() is working', () => {
408 const fn1 = () => {
409 return 1
410 }
411 const fn2 = () => {
412 return 2
413 }
414 const worker = new ThreadWorker({ fn1, fn2 })
415 expect(worker.setDefaultTaskFunction(0, fn1)).toStrictEqual({
416 error: new TypeError('name parameter is not a string'),
417 status: false,
418 })
419 expect(worker.setDefaultTaskFunction('', fn1)).toStrictEqual({
420 error: new TypeError('name parameter is an empty string'),
421 status: false,
422 })
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 })
432 expect(worker.taskFunctions.size).toBe(3)
433 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
434 worker.taskFunctions.get('fn1')
435 )
436 expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
437 error: new Error(
438 'Cannot set the default task function reserved name as the default task function'
439 ),
440 status: false,
441 })
442 expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({
443 error: new Error(
444 'Cannot set the default task function to a non-existing task function'
445 ),
446 status: false,
447 })
448 worker.setDefaultTaskFunction('fn1')
449 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
450 worker.taskFunctions.get('fn1')
451 )
452 worker.setDefaultTaskFunction('fn2')
453 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
454 worker.taskFunctions.get('fn2')
455 )
456 })
457 })