1 import { expect } from 'expect'
2 import { restore, stub } from 'sinon'
4 import { ClusterWorker, KillBehaviors, ThreadWorker } from '../../lib/index.cjs'
5 import { DEFAULT_TASK_NAME, EMPTY_FUNCTION } from '../../lib/utils.cjs'
7 describe('Abstract worker test suite', () => {
8 class StubWorkerWithMainWorker extends ThreadWorker {
9 constructor (fn, opts) {
11 delete this.mainWorker
19 it('Verify worker options default values', () => {
20 const worker = new ThreadWorker(() => {})
21 expect(worker.opts).toStrictEqual({
22 killBehavior: KillBehaviors.SOFT,
23 maxInactiveTime: 60000,
24 killHandler: EMPTY_FUNCTION
28 it('Verify that worker options are checked at worker creation', () => {
29 expect(() => new ClusterWorker(() => {}, '')).toThrow(
30 new TypeError('opts worker options parameter is not a plain object')
32 expect(() => new ClusterWorker(() => {}, { killBehavior: '' })).toThrow(
33 new TypeError("killBehavior option '' is not valid")
35 expect(() => new ClusterWorker(() => {}, { killBehavior: 0 })).toThrow(
36 new TypeError("killBehavior option '0' is not valid")
38 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: '' })).toThrow(
39 new TypeError('maxInactiveTime option is not an integer')
41 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 0.5 })).toThrow(
42 new TypeError('maxInactiveTime option is not an integer')
44 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 0 })).toThrow(
46 'maxInactiveTime option is not a positive integer greater or equal than 5'
49 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 4 })).toThrow(
51 'maxInactiveTime option is not a positive integer greater or equal than 5'
54 expect(() => new ThreadWorker(() => {}, { killHandler: '' })).toThrow(
55 new TypeError('killHandler option is not a function')
57 expect(() => new ThreadWorker(() => {}, { killHandler: 0 })).toThrow(
58 new TypeError('killHandler option is not a function')
62 it('Verify that worker options are set at worker creation', () => {
63 const killHandler = () => {
64 console.info('Worker received kill message')
66 const worker = new ClusterWorker(() => {}, {
67 killBehavior: KillBehaviors.HARD,
68 maxInactiveTime: 6000,
71 expect(worker.opts).toStrictEqual({
72 killBehavior: KillBehaviors.HARD,
73 maxInactiveTime: 6000,
78 it('Verify that taskFunctions parameter is mandatory', () => {
79 expect(() => new ClusterWorker()).toThrow(
80 new Error('taskFunctions parameter is mandatory')
84 it('Verify that taskFunctions parameter is a function or a plain object', () => {
85 expect(() => new ClusterWorker(0)).toThrow(
87 'taskFunctions parameter is not a function or a plain object'
90 expect(() => new ClusterWorker('')).toThrow(
92 'taskFunctions parameter is not a function or a plain object'
95 expect(() => new ClusterWorker(true)).toThrow(
97 'taskFunctions parameter is not a function or a plain object'
100 expect(() => new ClusterWorker([])).toThrow(
102 'taskFunctions parameter is not a function or a plain object'
105 expect(() => new ClusterWorker(new Map())).toThrow(
107 'taskFunctions parameter is not a function or a plain object'
110 expect(() => new ClusterWorker(new Set())).toThrow(
112 'taskFunctions parameter is not a function or a plain object'
115 expect(() => new ClusterWorker(new WeakMap())).toThrow(
117 'taskFunctions parameter is not a function or a plain object'
120 expect(() => new ClusterWorker(new WeakSet())).toThrow(
122 'taskFunctions parameter is not a function or a plain object'
127 it('Verify that taskFunctions parameter is not an empty object', () => {
128 expect(() => new ClusterWorker({})).toThrow(
129 new Error('taskFunctions parameter object is empty')
133 it('Verify that taskFunctions parameter with unique function is taken', () => {
134 const worker = new ThreadWorker(() => {})
135 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object)
136 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object)
137 expect(worker.taskFunctions.size).toBe(2)
138 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
139 worker.taskFunctions.get('fn1')
143 it('Verify that taskFunctions parameter with multiple task functions is checked', () => {
148 expect(() => new ThreadWorker({ '': fn1 })).toThrow(
149 new TypeError('A taskFunctions parameter object key is an empty string')
151 expect(() => new ThreadWorker({ fn1, fn2 })).toThrow(
153 "taskFunction object 'taskFunction' property 'undefined' is not a function"
158 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
165 const worker = new ClusterWorker({ fn1, fn2 })
166 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object)
167 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object)
168 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Object)
169 expect(worker.taskFunctions.size).toBe(3)
170 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
171 worker.taskFunctions.get('fn1')
175 it('Verify that async kill handler is called when worker is killed', () => {
176 const killHandlerStub = stub().returns()
177 const worker = new ClusterWorker(() => {}, {
178 killHandler: async () => await Promise.resolve(killHandlerStub())
180 worker.isMain = false
181 worker.handleKillMessage()
182 expect(killHandlerStub.calledOnce).toBe(true)
185 it('Verify that getMainWorker() throw error if main worker is not set', () => {
187 new StubWorkerWithMainWorker(() => {}).getMainWorker()
188 ).toThrow('Main worker not set')
191 it('Verify that hasTaskFunction() is working', () => {
198 const worker = new ClusterWorker({ fn1, fn2 })
199 expect(worker.hasTaskFunction(0)).toStrictEqual({
201 error: new TypeError('name parameter is not a string')
203 expect(worker.hasTaskFunction('')).toStrictEqual({
205 error: new TypeError('name parameter is an empty string')
207 expect(worker.hasTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
210 expect(worker.hasTaskFunction('fn1')).toStrictEqual({ status: true })
211 expect(worker.hasTaskFunction('fn2')).toStrictEqual({ status: true })
212 expect(worker.hasTaskFunction('fn3')).toStrictEqual({ status: false })
215 it('Verify that addTaskFunction() is working', () => {
222 const fn1Replacement = () => {
225 const worker = new ThreadWorker(fn1)
226 expect(worker.addTaskFunction(0, fn1)).toStrictEqual({
228 error: new TypeError('name parameter is not a string')
230 expect(worker.addTaskFunction('', fn1)).toStrictEqual({
232 error: new TypeError('name parameter is an empty string')
234 expect(worker.addTaskFunction('fn3', '')).toStrictEqual({
236 error: new TypeError(
237 "taskFunction object 'taskFunction' property 'undefined' is not a function"
240 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object)
241 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object)
242 expect(worker.taskFunctions.size).toBe(2)
243 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
244 worker.taskFunctions.get('fn1')
246 expect(worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toStrictEqual({
249 'Cannot add a task function with the default reserved name'
252 worker.addTaskFunction('fn2', fn2)
253 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object)
254 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object)
255 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Object)
256 expect(worker.taskFunctions.size).toBe(3)
257 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
258 worker.taskFunctions.get('fn1')
260 worker.addTaskFunction('fn1', fn1Replacement)
261 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object)
262 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object)
263 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Object)
264 expect(worker.taskFunctions.size).toBe(3)
265 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
266 worker.taskFunctions.get('fn1')
270 it('Verify that listTaskFunctionNames() is working', () => {
277 const worker = new ClusterWorker({ fn1, fn2 })
278 expect(worker.listTaskFunctionsProperties()).toStrictEqual([
279 { name: DEFAULT_TASK_NAME },
285 it('Verify that setDefaultTaskFunction() is working', () => {
292 const worker = new ThreadWorker({ fn1, fn2 })
293 expect(worker.setDefaultTaskFunction(0, fn1)).toStrictEqual({
295 error: new TypeError('name parameter is not a string')
297 expect(worker.setDefaultTaskFunction('', fn1)).toStrictEqual({
299 error: new TypeError('name parameter is an empty string')
301 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Object)
302 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Object)
303 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Object)
304 expect(worker.taskFunctions.size).toBe(3)
305 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
306 worker.taskFunctions.get('fn1')
308 expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
311 'Cannot set the default task function reserved name as the default task function'
314 expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({
317 'Cannot set the default task function to a non-existing task function'
320 worker.setDefaultTaskFunction('fn1')
321 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
322 worker.taskFunctions.get('fn1')
324 worker.setDefaultTaskFunction('fn2')
325 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
326 worker.taskFunctions.get('fn2')