1 import { expect } from '@std/expect'
2 import { restore, stub } from 'sinon'
8 WorkerChoiceStrategies,
9 } from '../../lib/index.cjs'
10 import { DEFAULT_TASK_NAME, EMPTY_FUNCTION } from '../../lib/utils.cjs'
12 describe('Abstract worker test suite', () => {
13 class StubWorkerWithMainWorker extends ThreadWorker {
14 constructor (fn, opts) {
16 delete this.mainWorker
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,
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')
37 expect(() => new ClusterWorker(() => {}, { killBehavior: '' })).toThrow(
38 new TypeError("killBehavior option '' is not valid")
40 expect(() => new ClusterWorker(() => {}, { killBehavior: 0 })).toThrow(
41 new TypeError("killBehavior option '0' is not valid")
43 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: '' })).toThrow(
44 new TypeError('maxInactiveTime option is not an integer')
46 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 0.5 })).toThrow(
47 new TypeError('maxInactiveTime option is not an integer')
49 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 0 })).toThrow(
51 'maxInactiveTime option is not a positive integer greater or equal than 5'
54 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 4 })).toThrow(
56 'maxInactiveTime option is not a positive integer greater or equal than 5'
59 expect(() => new ThreadWorker(() => {}, { killHandler: '' })).toThrow(
60 new TypeError('killHandler option is not a function')
62 expect(() => new ThreadWorker(() => {}, { killHandler: 0 })).toThrow(
63 new TypeError('killHandler option is not a function')
67 it('Verify that worker options are set at worker creation', () => {
68 const killHandler = () => {
69 console.info('Worker received kill message')
71 const worker = new ClusterWorker(() => {}, {
72 killBehavior: KillBehaviors.HARD,
74 maxInactiveTime: 6000,
76 expect(worker.opts).toStrictEqual({
77 killBehavior: KillBehaviors.HARD,
79 maxInactiveTime: 6000,
83 it('Verify that taskFunctions parameter is mandatory', () => {
84 expect(() => new ClusterWorker()).toThrow(
85 new Error('taskFunctions parameter is mandatory')
89 it('Verify that taskFunctions parameter is a function or a plain object', () => {
90 expect(() => new ClusterWorker(0)).toThrow(
92 'taskFunctions parameter is not a function or a plain object'
95 expect(() => new ClusterWorker('')).toThrow(
97 'taskFunctions parameter is not a function or a plain object'
100 expect(() => new ClusterWorker(true)).toThrow(
102 'taskFunctions parameter is not a function or a plain object'
105 expect(() => new ClusterWorker([])).toThrow(
107 'taskFunctions parameter is not a function or a plain object'
110 expect(() => new ClusterWorker(new Map())).toThrow(
112 'taskFunctions parameter is not a function or a plain object'
115 expect(() => new ClusterWorker(new Set())).toThrow(
117 'taskFunctions parameter is not a function or a plain object'
120 expect(() => new ClusterWorker(new WeakMap())).toThrow(
122 'taskFunctions parameter is not a function or a plain object'
125 expect(() => new ClusterWorker(new WeakSet())).toThrow(
127 'taskFunctions parameter is not a function or a plain object'
132 it('Verify that taskFunctions parameter is not an empty object', () => {
133 expect(() => new ClusterWorker({})).toThrow(
134 new Error('taskFunctions parameter object is empty')
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),
143 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
144 taskFunction: expect.any(Function),
146 expect(worker.taskFunctions.size).toBe(2)
147 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
148 worker.taskFunctions.get('fn1')
152 it('Verify that taskFunctions parameter with multiple task functions is checked', () => {
157 expect(() => new ThreadWorker({ '': fn1 })).toThrow(
158 new TypeError('A taskFunctions parameter object key is an empty string')
160 expect(() => new ThreadWorker({ fn1, fn2 })).toThrow(
162 "taskFunction object 'taskFunction' property 'undefined' is not a function"
165 expect(() => new ThreadWorker({ fn1: { fn1 } })).toThrow(
167 "taskFunction object 'taskFunction' property 'undefined' is not a function"
170 expect(() => new ThreadWorker({ fn2: { taskFunction: fn2 } })).toThrow(
172 "taskFunction object 'taskFunction' property '' is not a function"
176 () => new ThreadWorker({ fn1: { priority: '', taskFunction: fn1 } })
177 ).toThrow(new TypeError("Invalid property 'priority': ''"))
179 () => new ThreadWorker({ fn1: { priority: -21, taskFunction: fn1 } })
180 ).toThrow(new RangeError("Property 'priority' must be between -20 and 19"))
182 () => new ThreadWorker({ fn1: { priority: 20, taskFunction: fn1 } })
183 ).toThrow(new RangeError("Property 'priority' must be between -20 and 19"))
187 fn1: { strategy: 'invalidStrategy', taskFunction: fn1 },
189 ).toThrow(new Error("Invalid worker choice strategy 'invalidStrategy'"))
192 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
199 const worker = new ClusterWorker({ fn1, fn2 })
200 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
201 taskFunction: expect.any(Function),
203 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
204 taskFunction: expect.any(Function),
206 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
207 taskFunction: expect.any(Function),
209 expect(worker.taskFunctions.size).toBe(3)
210 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
211 worker.taskFunctions.get('fn1')
215 it('Verify that taskFunctions parameter with multiple task functions object is taken', () => {
218 taskFunction: () => {
224 strategy: WorkerChoiceStrategies.LESS_BUSY,
225 taskFunction: () => {
229 const worker = new ThreadWorker({
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')
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()),
247 worker.isMain = false
248 worker.handleKillMessage()
249 expect(killHandlerStub.calledOnce).toBe(true)
252 it('Verify that getMainWorker() throw error if main worker is not set', () => {
254 new StubWorkerWithMainWorker(() => {}).getMainWorker()
255 ).toThrow('Main worker not set')
258 it('Verify that hasTaskFunction() is working', () => {
265 const worker = new ClusterWorker({ fn1, fn2 })
266 expect(worker.hasTaskFunction(0)).toStrictEqual({
267 error: new TypeError('name parameter is not a string'),
270 expect(worker.hasTaskFunction('')).toStrictEqual({
271 error: new TypeError('name parameter is an empty string'),
274 expect(worker.hasTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
277 expect(worker.hasTaskFunction('fn1')).toStrictEqual({ status: true })
278 expect(worker.hasTaskFunction('fn2')).toStrictEqual({ status: true })
279 expect(worker.hasTaskFunction('fn3')).toStrictEqual({ status: false })
282 it('Verify that addTaskFunction() is working', () => {
289 const fn1Replacement = () => {
292 const worker = new ThreadWorker(fn1)
293 expect(worker.addTaskFunction(0, fn1)).toStrictEqual({
294 error: new TypeError('name parameter is not a string'),
297 expect(worker.addTaskFunction('', fn1)).toStrictEqual({
298 error: new TypeError('name parameter is an empty string'),
301 expect(worker.addTaskFunction('fn2', 0)).toStrictEqual({
302 error: new TypeError(
303 "taskFunction object 'taskFunction' property 'undefined' is not a function"
307 expect(worker.addTaskFunction('fn3', '')).toStrictEqual({
308 error: new TypeError(
309 "taskFunction object 'taskFunction' property 'undefined' is not a function"
313 expect(worker.addTaskFunction('fn2', { taskFunction: 0 })).toStrictEqual({
314 error: new TypeError(
315 "taskFunction object 'taskFunction' property '0' is not a function"
319 expect(worker.addTaskFunction('fn3', { taskFunction: '' })).toStrictEqual({
320 error: new TypeError(
321 "taskFunction object 'taskFunction' property '' is not a function"
326 worker.addTaskFunction('fn2', { priority: -21, taskFunction: () => {} })
328 error: new RangeError("Property 'priority' must be between -20 and 19"),
332 worker.addTaskFunction('fn3', { priority: 20, taskFunction: () => {} })
334 error: new RangeError("Property 'priority' must be between -20 and 19"),
338 worker.addTaskFunction('fn2', {
339 strategy: 'invalidStrategy',
340 taskFunction: () => {},
343 error: new Error("Invalid worker choice strategy 'invalidStrategy'"),
346 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
347 taskFunction: expect.any(Function),
349 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
350 taskFunction: expect.any(Function),
352 expect(worker.taskFunctions.size).toBe(2)
353 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
354 worker.taskFunctions.get('fn1')
356 expect(worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toStrictEqual({
358 'Cannot add a task function with the default reserved name'
362 worker.addTaskFunction('fn2', fn2)
363 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
364 taskFunction: expect.any(Function),
366 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
367 taskFunction: expect.any(Function),
369 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
370 taskFunction: expect.any(Function),
372 expect(worker.taskFunctions.size).toBe(3)
373 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
374 worker.taskFunctions.get('fn1')
376 worker.addTaskFunction('fn1', fn1Replacement)
377 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
378 taskFunction: expect.any(Function),
380 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
381 taskFunction: expect.any(Function),
383 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
384 taskFunction: expect.any(Function),
386 expect(worker.taskFunctions.size).toBe(3)
387 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
388 worker.taskFunctions.get('fn1')
392 it('Verify that listTaskFunctionsProperties() is working', () => {
399 const worker = new ClusterWorker({ fn1, fn2 })
400 expect(worker.listTaskFunctionsProperties()).toStrictEqual([
401 { name: DEFAULT_TASK_NAME },
407 it('Verify that setDefaultTaskFunction() is working', () => {
414 const worker = new ThreadWorker({ fn1, fn2 })
415 expect(worker.setDefaultTaskFunction(0, fn1)).toStrictEqual({
416 error: new TypeError('name parameter is not a string'),
419 expect(worker.setDefaultTaskFunction('', fn1)).toStrictEqual({
420 error: new TypeError('name parameter is an empty string'),
423 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
424 taskFunction: expect.any(Function),
426 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
427 taskFunction: expect.any(Function),
429 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
430 taskFunction: expect.any(Function),
432 expect(worker.taskFunctions.size).toBe(3)
433 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
434 worker.taskFunctions.get('fn1')
436 expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
438 'Cannot set the default task function reserved name as the default task function'
442 expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({
444 'Cannot set the default task function to a non-existing task function'
448 worker.setDefaultTaskFunction('fn1')
449 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
450 worker.taskFunctions.get('fn1')
452 worker.setDefaultTaskFunction('fn2')
453 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
454 worker.taskFunctions.get('fn2')