1 import { expect } from 'expect'
2 import { restore, stub } from 'sinon'
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 maxInactiveTime: 60000,
29 killHandler: EMPTY_FUNCTION
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,
73 maxInactiveTime: 6000,
76 expect(worker.opts).toStrictEqual({
77 killBehavior: KillBehaviors.HARD,
78 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(EMPTY_FUNCTION)
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: { taskFunction: fn1, priority: '' } })
177 ).toThrow(new TypeError("Invalid property 'priority': ''"))
179 () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: -21 } })
180 ).toThrow(new TypeError("Property 'priority' must be between -20 and 19"))
182 () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: 20 } })
183 ).toThrow(new RangeError("Property 'priority' must be between -20 and 19"))
187 fn1: { taskFunction: fn1, strategy: 'invalidStrategy' }
190 new RangeError("Invalid worker choice strategy 'invalidStrategy'")
194 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
201 const worker = new ClusterWorker({ fn1, fn2 })
202 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
203 taskFunction: expect.any(Function)
205 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
206 taskFunction: expect.any(Function)
208 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
209 taskFunction: expect.any(Function)
211 expect(worker.taskFunctions.size).toBe(3)
212 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
213 worker.taskFunctions.get('fn1')
217 it('Verify that taskFunctions parameter with multiple task functions object is taken', () => {
219 taskFunction: () => {
225 taskFunction: () => {
229 strategy: WorkerChoiceStrategies.LESS_BUSY
231 const worker = new ThreadWorker({
235 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(fn1Obj)
236 expect(worker.taskFunctions.get('fn1')).toStrictEqual(fn1Obj)
237 expect(worker.taskFunctions.get('fn2')).toStrictEqual(fn2Obj)
238 expect(worker.taskFunctions.size).toBe(3)
239 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
240 worker.taskFunctions.get('fn1')
244 it('Verify that async kill handler is called when worker is killed', () => {
245 const killHandlerStub = stub().returns()
246 const worker = new ClusterWorker(() => {}, {
247 killHandler: async () => await Promise.resolve(killHandlerStub())
249 worker.isMain = false
250 worker.handleKillMessage()
251 expect(killHandlerStub.calledOnce).toBe(true)
254 it('Verify that getMainWorker() throw error if main worker is not set', () => {
256 new StubWorkerWithMainWorker(() => {}).getMainWorker()
257 ).toThrow('Main worker not set')
260 it('Verify that hasTaskFunction() is working', () => {
267 const worker = new ClusterWorker({ fn1, fn2 })
268 expect(worker.hasTaskFunction(0)).toStrictEqual({
270 error: new TypeError('name parameter is not a string')
272 expect(worker.hasTaskFunction('')).toStrictEqual({
274 error: new TypeError('name parameter is an empty string')
276 expect(worker.hasTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
279 expect(worker.hasTaskFunction('fn1')).toStrictEqual({ status: true })
280 expect(worker.hasTaskFunction('fn2')).toStrictEqual({ status: true })
281 expect(worker.hasTaskFunction('fn3')).toStrictEqual({ status: false })
284 it('Verify that addTaskFunction() is working', () => {
291 const fn1Replacement = () => {
294 const worker = new ThreadWorker(fn1)
295 expect(worker.addTaskFunction(0, fn1)).toStrictEqual({
297 error: new TypeError('name parameter is not a string')
299 expect(worker.addTaskFunction('', fn1)).toStrictEqual({
301 error: new TypeError('name parameter is an empty string')
303 expect(worker.addTaskFunction('fn3', '')).toStrictEqual({
305 error: new TypeError(
306 "taskFunction object 'taskFunction' property 'undefined' is not a function"
309 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
310 taskFunction: expect.any(Function)
312 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
313 taskFunction: expect.any(Function)
315 expect(worker.taskFunctions.size).toBe(2)
316 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
317 worker.taskFunctions.get('fn1')
319 expect(worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toStrictEqual({
322 'Cannot add a task function with the default reserved name'
325 worker.addTaskFunction('fn2', fn2)
326 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
327 taskFunction: expect.any(Function)
329 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
330 taskFunction: expect.any(Function)
332 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
333 taskFunction: expect.any(Function)
335 expect(worker.taskFunctions.size).toBe(3)
336 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
337 worker.taskFunctions.get('fn1')
339 worker.addTaskFunction('fn1', fn1Replacement)
340 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
341 taskFunction: expect.any(Function)
343 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
344 taskFunction: expect.any(Function)
346 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
347 taskFunction: expect.any(Function)
349 expect(worker.taskFunctions.size).toBe(3)
350 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
351 worker.taskFunctions.get('fn1')
355 it('Verify that listTaskFunctionsProperties() is working', () => {
362 const worker = new ClusterWorker({ fn1, fn2 })
363 expect(worker.listTaskFunctionsProperties()).toStrictEqual([
364 { name: DEFAULT_TASK_NAME },
370 it('Verify that setDefaultTaskFunction() is working', () => {
377 const worker = new ThreadWorker({ fn1, fn2 })
378 expect(worker.setDefaultTaskFunction(0, fn1)).toStrictEqual({
380 error: new TypeError('name parameter is not a string')
382 expect(worker.setDefaultTaskFunction('', fn1)).toStrictEqual({
384 error: new TypeError('name parameter is an empty string')
386 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
387 taskFunction: expect.any(Function)
389 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
390 taskFunction: expect.any(Function)
392 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
393 taskFunction: expect.any(Function)
395 expect(worker.taskFunctions.size).toBe(3)
396 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
397 worker.taskFunctions.get('fn1')
399 expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
402 'Cannot set the default task function reserved name as the default task function'
405 expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({
408 'Cannot set the default task function to a non-existing task function'
411 worker.setDefaultTaskFunction('fn1')
412 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
413 worker.taskFunctions.get('fn1')
415 worker.setDefaultTaskFunction('fn2')
416 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
417 worker.taskFunctions.get('fn2')