1 import { expect } from 'expect'
2 import { restore, stub } from 'sinon'
3 import { ClusterWorker, KillBehaviors, ThreadWorker } from '../../lib/index.js'
4 import { DEFAULT_TASK_NAME, EMPTY_FUNCTION } from '../../lib/utils.js'
6 describe('Abstract worker test suite', () => {
7 class StubWorkerWithMainWorker extends ThreadWorker {
8 constructor (fn, opts) {
10 delete this.mainWorker
18 it('Verify worker options default values', () => {
19 const worker = new ThreadWorker(() => {})
20 expect(worker.opts).toStrictEqual({
21 killBehavior: KillBehaviors.SOFT,
22 maxInactiveTime: 60000,
23 killHandler: EMPTY_FUNCTION
27 it('Verify that worker options are checked at worker creation', () => {
28 expect(() => new ClusterWorker(() => {}, '')).toThrow(
29 new TypeError('opts worker options parameter is not a plain object')
31 expect(() => new ClusterWorker(() => {}, { killBehavior: '' })).toThrow(
32 new TypeError("killBehavior option '' is not valid")
34 expect(() => new ClusterWorker(() => {}, { killBehavior: 0 })).toThrow(
35 new TypeError("killBehavior option '0' is not valid")
37 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: '' })).toThrow(
38 new TypeError('maxInactiveTime option is not an integer')
40 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 0.5 })).toThrow(
41 new TypeError('maxInactiveTime option is not an integer')
43 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 0 })).toThrow(
45 'maxInactiveTime option is not a positive integer greater or equal than 5'
48 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 4 })).toThrow(
50 'maxInactiveTime option is not a positive integer greater or equal than 5'
53 expect(() => new ThreadWorker(() => {}, { killHandler: '' })).toThrow(
54 new TypeError('killHandler option is not a function')
56 expect(() => new ThreadWorker(() => {}, { killHandler: 0 })).toThrow(
57 new TypeError('killHandler option is not a function')
61 it('Verify that worker options are set at worker creation', () => {
62 const killHandler = () => {
63 console.info('Worker received kill message')
65 const worker = new ClusterWorker(() => {}, {
66 killBehavior: KillBehaviors.HARD,
67 maxInactiveTime: 6000,
70 expect(worker.opts).toStrictEqual({
71 killBehavior: KillBehaviors.HARD,
72 maxInactiveTime: 6000,
77 it('Verify that taskFunctions parameter is mandatory', () => {
78 expect(() => new ClusterWorker()).toThrow(
79 new Error('taskFunctions parameter is mandatory')
83 it('Verify that taskFunctions parameter is a function or a plain object', () => {
84 expect(() => new ClusterWorker(0)).toThrow(
86 'taskFunctions parameter is not a function or a plain object'
89 expect(() => new ClusterWorker('')).toThrow(
91 'taskFunctions parameter is not a function or a plain object'
94 expect(() => new ClusterWorker(true)).toThrow(
96 'taskFunctions parameter is not a function or a plain object'
99 expect(() => new ClusterWorker([])).toThrow(
101 'taskFunctions parameter is not a function or a plain object'
104 expect(() => new ClusterWorker(new Map())).toThrow(
106 'taskFunctions parameter is not a function or a plain object'
109 expect(() => new ClusterWorker(new Set())).toThrow(
111 'taskFunctions parameter is not a function or a plain object'
114 expect(() => new ClusterWorker(new WeakMap())).toThrow(
116 'taskFunctions parameter is not a function or a plain object'
119 expect(() => new ClusterWorker(new WeakSet())).toThrow(
121 'taskFunctions parameter is not a function or a plain object'
126 it('Verify that taskFunctions parameter is not an empty object', () => {
127 expect(() => new ClusterWorker({})).toThrow(
128 new Error('taskFunctions parameter object is empty')
132 it('Verify that taskFunctions parameter with unique function is taken', () => {
133 const worker = new ThreadWorker(() => {})
134 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
135 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
136 expect(worker.taskFunctions.size).toBe(2)
137 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
138 worker.taskFunctions.get('fn1')
142 it('Verify that taskFunctions parameter with multiple task functions is checked', () => {
147 expect(() => new ThreadWorker({ '': fn1 })).toThrow(
148 new TypeError('A taskFunctions parameter object key is an empty string')
150 expect(() => new ThreadWorker({ fn1, fn2 })).toThrow(
151 new TypeError('A taskFunctions parameter object value is not a function')
155 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
162 const worker = new ClusterWorker({ fn1, fn2 })
163 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
164 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
165 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
166 expect(worker.taskFunctions.size).toBe(3)
167 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
168 worker.taskFunctions.get('fn1')
172 it('Verify that async kill handler is called when worker is killed', () => {
173 const killHandlerStub = stub().returns()
174 const worker = new ClusterWorker(() => {}, {
175 killHandler: async () => await Promise.resolve(killHandlerStub())
177 worker.isMain = false
178 worker.handleKillMessage()
179 expect(killHandlerStub.calledOnce).toBe(true)
182 it('Verify that getMainWorker() throw error if main worker is not set', () => {
184 new StubWorkerWithMainWorker(() => {}).getMainWorker()
185 ).toThrow('Main worker not set')
188 it('Verify that hasTaskFunction() is working', () => {
195 const worker = new ClusterWorker({ fn1, fn2 })
196 expect(worker.hasTaskFunction(0)).toStrictEqual({
198 error: new TypeError('name parameter is not a string')
200 expect(worker.hasTaskFunction('')).toStrictEqual({
202 error: new TypeError('name parameter is an empty string')
204 expect(worker.hasTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
207 expect(worker.hasTaskFunction('fn1')).toStrictEqual({ status: true })
208 expect(worker.hasTaskFunction('fn2')).toStrictEqual({ status: true })
209 expect(worker.hasTaskFunction('fn3')).toStrictEqual({ status: false })
212 it('Verify that addTaskFunction() is working', () => {
219 const fn1Replacement = () => {
222 const worker = new ThreadWorker(fn1)
223 expect(worker.addTaskFunction(0, fn1)).toStrictEqual({
225 error: new TypeError('name parameter is not a string')
227 expect(worker.addTaskFunction('', fn1)).toStrictEqual({
229 error: new TypeError('name parameter is an empty string')
231 expect(worker.addTaskFunction('fn3', '')).toStrictEqual({
233 error: new TypeError('fn parameter is not a function')
235 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
236 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
237 expect(worker.taskFunctions.size).toBe(2)
238 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
239 worker.taskFunctions.get('fn1')
241 expect(worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toStrictEqual({
244 'Cannot add a task function with the default reserved name'
247 worker.addTaskFunction('fn2', fn2)
248 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
249 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
250 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
251 expect(worker.taskFunctions.size).toBe(3)
252 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
253 worker.taskFunctions.get('fn1')
255 worker.addTaskFunction('fn1', fn1Replacement)
256 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
257 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
258 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
259 expect(worker.taskFunctions.size).toBe(3)
260 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
261 worker.taskFunctions.get('fn1')
265 it('Verify that listTaskFunctionNames() is working', () => {
272 const worker = new ClusterWorker({ fn1, fn2 })
273 expect(worker.listTaskFunctionNames()).toStrictEqual([
280 it('Verify that setDefaultTaskFunction() is working', () => {
287 const worker = new ThreadWorker({ fn1, fn2 })
288 expect(worker.setDefaultTaskFunction(0, fn1)).toStrictEqual({
290 error: new TypeError('name parameter is not a string')
292 expect(worker.setDefaultTaskFunction('', fn1)).toStrictEqual({
294 error: new TypeError('name parameter is an empty string')
296 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
297 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
298 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
299 expect(worker.taskFunctions.size).toBe(3)
300 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
301 worker.taskFunctions.get('fn1')
303 expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
306 'Cannot set the default task function reserved name as the default task function'
309 expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({
312 'Cannot set the default task function to a non-existing task function'
315 worker.setDefaultTaskFunction('fn1')
316 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
317 worker.taskFunctions.get('fn1')
319 worker.setDefaultTaskFunction('fn2')
320 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
321 worker.taskFunctions.get('fn2')