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(() => {})
140 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object)
141 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object)
142 expect(worker.taskFunctions.size).toBe(2)
143 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
144 worker.taskFunctions.get('fn1')
148 it('Verify that taskFunctions parameter with multiple task functions is checked', () => {
153 expect(() => new ThreadWorker({ '': fn1 })).toThrow(
154 new TypeError('A taskFunctions parameter object key is an empty string')
156 expect(() => new ThreadWorker({ fn1, fn2 })).toThrow(
158 "taskFunction object 'taskFunction' property 'undefined' is not a function"
161 expect(() => new ThreadWorker({ fn1: { fn1 } })).toThrow(
163 "taskFunction object 'taskFunction' property 'undefined' is not a function"
166 expect(() => new ThreadWorker({ fn2: { taskFunction: fn2 } })).toThrow(
168 "taskFunction object 'taskFunction' property '' is not a function"
172 () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: '' } })
173 ).toThrow(new TypeError("Invalid priority ''"))
175 () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: -21 } })
176 ).toThrow(new TypeError('Property priority must be between -20 and 19'))
178 () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: 20 } })
179 ).toThrow(new RangeError('Property priority must be between -20 and 19'))
183 fn1: { taskFunction: fn1, strategy: 'invalidStrategy' }
186 new RangeError("Invalid worker choice strategy 'invalidStrategy'")
190 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
197 const worker = new ClusterWorker({ fn1, fn2 })
198 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object)
199 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object)
200 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Object)
201 expect(worker.taskFunctions.size).toBe(3)
202 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
203 worker.taskFunctions.get('fn1')
207 it('Verify that taskFunctions parameter with multiple task functions object is taken', () => {
209 taskFunction: () => {
215 taskFunction: () => {
219 strategy: WorkerChoiceStrategies.LESS_BUSY
221 const worker = new ThreadWorker({
225 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(fn1Obj)
226 expect(worker.taskFunctions.get('fn1')).toStrictEqual(fn1Obj)
227 expect(worker.taskFunctions.get('fn2')).toStrictEqual(fn2Obj)
228 expect(worker.taskFunctions.size).toBe(3)
229 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
230 worker.taskFunctions.get('fn1')
234 it('Verify that async kill handler is called when worker is killed', () => {
235 const killHandlerStub = stub().returns()
236 const worker = new ClusterWorker(() => {}, {
237 killHandler: async () => await Promise.resolve(killHandlerStub())
239 worker.isMain = false
240 worker.handleKillMessage()
241 expect(killHandlerStub.calledOnce).toBe(true)
244 it('Verify that getMainWorker() throw error if main worker is not set', () => {
246 new StubWorkerWithMainWorker(() => {}).getMainWorker()
247 ).toThrow('Main worker not set')
250 it('Verify that hasTaskFunction() is working', () => {
257 const worker = new ClusterWorker({ fn1, fn2 })
258 expect(worker.hasTaskFunction(0)).toStrictEqual({
260 error: new TypeError('name parameter is not a string')
262 expect(worker.hasTaskFunction('')).toStrictEqual({
264 error: new TypeError('name parameter is an empty string')
266 expect(worker.hasTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
269 expect(worker.hasTaskFunction('fn1')).toStrictEqual({ status: true })
270 expect(worker.hasTaskFunction('fn2')).toStrictEqual({ status: true })
271 expect(worker.hasTaskFunction('fn3')).toStrictEqual({ status: false })
274 it('Verify that addTaskFunction() is working', () => {
281 const fn1Replacement = () => {
284 const worker = new ThreadWorker(fn1)
285 expect(worker.addTaskFunction(0, fn1)).toStrictEqual({
287 error: new TypeError('name parameter is not a string')
289 expect(worker.addTaskFunction('', fn1)).toStrictEqual({
291 error: new TypeError('name parameter is an empty string')
293 expect(worker.addTaskFunction('fn3', '')).toStrictEqual({
295 error: new TypeError(
296 "taskFunction object 'taskFunction' property 'undefined' is not a function"
299 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object)
300 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object)
301 expect(worker.taskFunctions.size).toBe(2)
302 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
303 worker.taskFunctions.get('fn1')
305 expect(worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toStrictEqual({
308 'Cannot add a task function with the default reserved name'
311 worker.addTaskFunction('fn2', fn2)
312 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object)
313 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object)
314 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Object)
315 expect(worker.taskFunctions.size).toBe(3)
316 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
317 worker.taskFunctions.get('fn1')
319 worker.addTaskFunction('fn1', fn1Replacement)
320 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object)
321 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object)
322 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Object)
323 expect(worker.taskFunctions.size).toBe(3)
324 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
325 worker.taskFunctions.get('fn1')
329 it('Verify that listTaskFunctionNames() is working', () => {
336 const worker = new ClusterWorker({ fn1, fn2 })
337 expect(worker.listTaskFunctionsProperties()).toStrictEqual([
338 { name: DEFAULT_TASK_NAME },
344 it('Verify that setDefaultTaskFunction() is working', () => {
351 const worker = new ThreadWorker({ fn1, fn2 })
352 expect(worker.setDefaultTaskFunction(0, fn1)).toStrictEqual({
354 error: new TypeError('name parameter is not a string')
356 expect(worker.setDefaultTaskFunction('', fn1)).toStrictEqual({
358 error: new TypeError('name parameter is an empty string')
360 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object)
361 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object)
362 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Object)
363 expect(worker.taskFunctions.size).toBe(3)
364 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
365 worker.taskFunctions.get('fn1')
367 expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
370 'Cannot set the default task function reserved name as the default task function'
373 expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({
376 'Cannot set the default task function to a non-existing task function'
379 worker.setDefaultTaskFunction('fn1')
380 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
381 worker.taskFunctions.get('fn1')
383 worker.setDefaultTaskFunction('fn2')
384 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
385 worker.taskFunctions.get('fn2')