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(() => {}, '')).toThrowError(
29 new TypeError('opts worker options parameter is not a plain object')
32 () => new ClusterWorker(() => {}, { killBehavior: '' })
33 ).toThrowError(new TypeError("killBehavior option '' is not valid"))
34 expect(() => new ClusterWorker(() => {}, { killBehavior: 0 })).toThrowError(
35 new TypeError("killBehavior option '0' is not valid")
38 () => new ThreadWorker(() => {}, { maxInactiveTime: '' })
39 ).toThrowError(new TypeError('maxInactiveTime option is not an integer'))
41 () => new ThreadWorker(() => {}, { maxInactiveTime: 0.5 })
42 ).toThrowError(new TypeError('maxInactiveTime option is not an integer'))
44 () => new ThreadWorker(() => {}, { maxInactiveTime: 0 })
47 'maxInactiveTime option is not a positive integer greater or equal than 5'
51 () => new ThreadWorker(() => {}, { maxInactiveTime: 4 })
54 'maxInactiveTime option is not a positive integer greater or equal than 5'
57 expect(() => new ThreadWorker(() => {}, { killHandler: '' })).toThrowError(
58 new TypeError('killHandler option is not a function')
60 expect(() => new ThreadWorker(() => {}, { killHandler: 0 })).toThrowError(
61 new TypeError('killHandler option is not a function')
65 it('Verify that worker options are set at worker creation', () => {
66 const killHandler = () => {
67 console.info('Worker received kill message')
69 const worker = new ClusterWorker(() => {}, {
70 killBehavior: KillBehaviors.HARD,
71 maxInactiveTime: 6000,
74 expect(worker.opts).toStrictEqual({
75 killBehavior: KillBehaviors.HARD,
76 maxInactiveTime: 6000,
81 it('Verify that taskFunctions parameter is mandatory', () => {
82 expect(() => new ClusterWorker()).toThrowError(
83 new Error('taskFunctions parameter is mandatory')
87 it('Verify that taskFunctions parameter is a function or a plain object', () => {
88 expect(() => new ClusterWorker(0)).toThrowError(
90 'taskFunctions parameter is not a function or a plain object'
93 expect(() => new ClusterWorker('')).toThrowError(
95 'taskFunctions parameter is not a function or a plain object'
98 expect(() => new ClusterWorker(true)).toThrowError(
100 'taskFunctions parameter is not a function or a plain object'
103 expect(() => new ClusterWorker([])).toThrowError(
105 'taskFunctions parameter is not a function or a plain object'
108 expect(() => new ClusterWorker(new Map())).toThrowError(
110 'taskFunctions parameter is not a function or a plain object'
113 expect(() => new ClusterWorker(new Set())).toThrowError(
115 'taskFunctions parameter is not a function or a plain object'
118 expect(() => new ClusterWorker(new WeakMap())).toThrowError(
120 'taskFunctions parameter is not a function or a plain object'
123 expect(() => new ClusterWorker(new WeakSet())).toThrowError(
125 'taskFunctions parameter is not a function or a plain object'
130 it('Verify that taskFunctions parameter is not an empty object', () => {
131 expect(() => new ClusterWorker({})).toThrowError(
132 new Error('taskFunctions parameter object is empty')
136 it('Verify that taskFunctions parameter with unique function is taken', () => {
137 const worker = new ThreadWorker(() => {})
138 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
139 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
140 expect(worker.taskFunctions.size).toBe(2)
141 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
142 worker.taskFunctions.get('fn1')
146 it('Verify that taskFunctions parameter with multiple task functions is checked', () => {
151 expect(() => new ThreadWorker({ '': fn1 })).toThrowError(
152 new TypeError('A taskFunctions parameter object key is an empty string')
154 expect(() => new ThreadWorker({ fn1, fn2 })).toThrowError(
155 new TypeError('A taskFunctions parameter object value is not a function')
159 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
166 const worker = new ClusterWorker({ fn1, fn2 })
167 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
168 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
169 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
170 expect(worker.taskFunctions.size).toBe(3)
171 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
172 worker.taskFunctions.get('fn1')
176 it('Verify that async kill handler is called when worker is killed', () => {
177 const killHandlerStub = stub().returns()
178 const worker = new ClusterWorker(() => {}, {
179 killHandler: async () => await Promise.resolve(killHandlerStub())
181 worker.isMain = false
182 worker.handleKillMessage()
183 expect(killHandlerStub.calledOnce).toBe(true)
186 it('Verify that getMainWorker() throw error if main worker is not set', () => {
188 new StubWorkerWithMainWorker(() => {}).getMainWorker()
189 ).toThrowError('Main worker not set')
192 it('Verify that hasTaskFunction() is working', () => {
199 const worker = new ClusterWorker({ fn1, fn2 })
200 expect(worker.hasTaskFunction(0)).toStrictEqual({
202 error: new TypeError('name parameter is not a string')
204 expect(worker.hasTaskFunction('')).toStrictEqual({
206 error: new TypeError('name parameter is an empty string')
208 expect(worker.hasTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
211 expect(worker.hasTaskFunction('fn1')).toStrictEqual({ status: true })
212 expect(worker.hasTaskFunction('fn2')).toStrictEqual({ status: true })
213 expect(worker.hasTaskFunction('fn3')).toStrictEqual({ status: false })
216 it('Verify that addTaskFunction() is working', () => {
223 const fn1Replacement = () => {
226 const worker = new ThreadWorker(fn1)
227 expect(worker.addTaskFunction(0, fn1)).toStrictEqual({
229 error: new TypeError('name parameter is not a string')
231 expect(worker.addTaskFunction('', fn1)).toStrictEqual({
233 error: new TypeError('name parameter is an empty string')
235 expect(worker.addTaskFunction('fn3', '')).toStrictEqual({
237 error: new TypeError('fn parameter is not a function')
239 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
240 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
241 expect(worker.taskFunctions.size).toBe(2)
242 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
243 worker.taskFunctions.get('fn1')
245 expect(worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toStrictEqual({
248 'Cannot add a task function with the default reserved name'
251 worker.addTaskFunction('fn2', fn2)
252 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
253 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
254 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
255 expect(worker.taskFunctions.size).toBe(3)
256 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
257 worker.taskFunctions.get('fn1')
259 worker.addTaskFunction('fn1', fn1Replacement)
260 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
261 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
262 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
263 expect(worker.taskFunctions.size).toBe(3)
264 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
265 worker.taskFunctions.get('fn1')
269 it('Verify that listTaskFunctionNames() is working', () => {
276 const worker = new ClusterWorker({ fn1, fn2 })
277 expect(worker.listTaskFunctionNames()).toStrictEqual([
284 it('Verify that setDefaultTaskFunction() is working', () => {
291 const worker = new ThreadWorker({ fn1, fn2 })
292 expect(worker.setDefaultTaskFunction(0, fn1)).toStrictEqual({
294 error: new TypeError('name parameter is not a string')
296 expect(worker.setDefaultTaskFunction('', fn1)).toStrictEqual({
298 error: new TypeError('name parameter is an empty string')
300 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
301 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
302 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
303 expect(worker.taskFunctions.size).toBe(3)
304 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
305 worker.taskFunctions.get('fn1')
307 expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
310 'Cannot set the default task function reserved name as the default task function'
313 expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({
316 'Cannot set the default task function to a non-existing task function'
319 worker.setDefaultTaskFunction('fn1')
320 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
321 worker.taskFunctions.get('fn1')
323 worker.setDefaultTaskFunction('fn2')
324 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
325 worker.taskFunctions.get('fn2')