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(Function)
136 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
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(
152 new TypeError('A taskFunctions parameter object value is not a function')
156 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
163 const worker = new ClusterWorker({ fn1, fn2 })
164 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
165 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
166 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
167 expect(worker.taskFunctions.size).toBe(3)
168 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
169 worker.taskFunctions.get('fn1')
173 it('Verify that async kill handler is called when worker is killed', () => {
174 const killHandlerStub = stub().returns()
175 const worker = new ClusterWorker(() => {}, {
176 killHandler: async () => await Promise.resolve(killHandlerStub())
178 worker.isMain = false
179 worker.handleKillMessage()
180 expect(killHandlerStub.calledOnce).toBe(true)
183 it('Verify that getMainWorker() throw error if main worker is not set', () => {
185 new StubWorkerWithMainWorker(() => {}).getMainWorker()
186 ).toThrow('Main worker not set')
189 it('Verify that hasTaskFunction() is working', () => {
196 const worker = new ClusterWorker({ fn1, fn2 })
197 expect(worker.hasTaskFunction(0)).toStrictEqual({
199 error: new TypeError('name parameter is not a string')
201 expect(worker.hasTaskFunction('')).toStrictEqual({
203 error: new TypeError('name parameter is an empty string')
205 expect(worker.hasTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
208 expect(worker.hasTaskFunction('fn1')).toStrictEqual({ status: true })
209 expect(worker.hasTaskFunction('fn2')).toStrictEqual({ status: true })
210 expect(worker.hasTaskFunction('fn3')).toStrictEqual({ status: false })
213 it('Verify that addTaskFunction() is working', () => {
220 const fn1Replacement = () => {
223 const worker = new ThreadWorker(fn1)
224 expect(worker.addTaskFunction(0, fn1)).toStrictEqual({
226 error: new TypeError('name parameter is not a string')
228 expect(worker.addTaskFunction('', fn1)).toStrictEqual({
230 error: new TypeError('name parameter is an empty string')
232 expect(worker.addTaskFunction('fn3', '')).toStrictEqual({
234 error: new TypeError('fn parameter is not a function')
236 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
237 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
238 expect(worker.taskFunctions.size).toBe(2)
239 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
240 worker.taskFunctions.get('fn1')
242 expect(worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toStrictEqual({
245 'Cannot add a task function with the default reserved name'
248 worker.addTaskFunction('fn2', fn2)
249 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
250 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
251 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
252 expect(worker.taskFunctions.size).toBe(3)
253 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
254 worker.taskFunctions.get('fn1')
256 worker.addTaskFunction('fn1', fn1Replacement)
257 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
258 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
259 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
260 expect(worker.taskFunctions.size).toBe(3)
261 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
262 worker.taskFunctions.get('fn1')
266 it('Verify that listTaskFunctionNames() is working', () => {
273 const worker = new ClusterWorker({ fn1, fn2 })
274 expect(worker.listTaskFunctionNames()).toStrictEqual([
281 it('Verify that setDefaultTaskFunction() is working', () => {
288 const worker = new ThreadWorker({ fn1, fn2 })
289 expect(worker.setDefaultTaskFunction(0, fn1)).toStrictEqual({
291 error: new TypeError('name parameter is not a string')
293 expect(worker.setDefaultTaskFunction('', fn1)).toStrictEqual({
295 error: new TypeError('name parameter is an empty string')
297 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
298 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
299 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
300 expect(worker.taskFunctions.size).toBe(3)
301 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
302 worker.taskFunctions.get('fn1')
304 expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
307 'Cannot set the default task function reserved name as the default task function'
310 expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({
313 'Cannot set the default task function to a non-existing task function'
316 worker.setDefaultTaskFunction('fn1')
317 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
318 worker.taskFunctions.get('fn1')
320 worker.setDefaultTaskFunction('fn2')
321 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
322 worker.taskFunctions.get('fn2')