1 const { expect
} = require('expect')
2 const sinon
= require('sinon')
3 const { ClusterWorker
, KillBehaviors
, ThreadWorker
} = require('../../lib')
4 const { DEFAULT_TASK_NAME
, EMPTY_FUNCTION
} = require('../../lib/utils')
6 describe('Abstract worker test suite', () => {
7 class StubWorkerWithMainWorker
extends ThreadWorker
{
8 constructor (fn
, opts
) {
10 this.mainWorker
= undefined
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')
63 expect(() => new ThreadWorker(() => {}, { async
: true })).toThrowError(
64 new TypeError('async option is deprecated')
68 it('Verify that worker options are set at worker creation', () => {
69 const killHandler
= () => {
70 console
.info('Worker received kill message')
72 const worker
= new ClusterWorker(() => {}, {
73 killBehavior
: KillBehaviors
.HARD
,
74 maxInactiveTime
: 6000,
77 expect(worker
.opts
).toStrictEqual({
78 killBehavior
: KillBehaviors
.HARD
,
79 maxInactiveTime
: 6000,
84 it('Verify that taskFunctions parameter is mandatory', () => {
85 expect(() => new ClusterWorker()).toThrowError(
86 new Error('taskFunctions parameter is mandatory')
90 it('Verify that taskFunctions parameter is a function or a plain object', () => {
91 expect(() => new ClusterWorker(0)).toThrowError(
93 'taskFunctions parameter is not a function or a plain object'
96 expect(() => new ClusterWorker('')).toThrowError(
98 'taskFunctions parameter is not a function or a plain object'
101 expect(() => new ClusterWorker(true)).toThrowError(
103 'taskFunctions parameter is not a function or a plain object'
106 expect(() => new ClusterWorker([])).toThrowError(
108 'taskFunctions parameter is not a function or a plain object'
111 expect(() => new ClusterWorker(new Map())).toThrowError(
113 'taskFunctions parameter is not a function or a plain object'
116 expect(() => new ClusterWorker(new Set())).toThrowError(
118 'taskFunctions parameter is not a function or a plain object'
121 expect(() => new ClusterWorker(new WeakMap())).toThrowError(
123 'taskFunctions parameter is not a function or a plain object'
126 expect(() => new ClusterWorker(new WeakSet())).toThrowError(
128 'taskFunctions parameter is not a function or a plain object'
133 it('Verify that taskFunctions parameter is not an empty object', () => {
134 expect(() => new ClusterWorker({})).toThrowError(
135 new Error('taskFunctions parameter object is empty')
139 it('Verify that taskFunctions parameter with unique function is taken', () => {
140 const worker
= new ThreadWorker(() => {})
141 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
142 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
143 expect(worker
.taskFunctions
.size
).toBe(2)
144 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
145 worker
.taskFunctions
.get('fn1')
149 it('Verify that taskFunctions parameter with multiple task functions is checked', () => {
154 expect(() => new ThreadWorker({ '': fn1
})).toThrowError(
155 new TypeError('A taskFunctions parameter object key is an empty string')
157 expect(() => new ThreadWorker({ fn1
, fn2
})).toThrowError(
158 new TypeError('A taskFunctions parameter object value is not a function')
162 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
169 const worker
= new ClusterWorker({ fn1
, fn2
})
170 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
171 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
172 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
173 expect(worker
.taskFunctions
.size
).toBe(3)
174 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
175 worker
.taskFunctions
.get('fn1')
179 it('Verify that sync kill handler is called when worker is killed', () => {
180 const worker
= new ClusterWorker(() => {}, {
181 killHandler
: sinon
.stub().returns()
183 worker
.isMain
= false
184 worker
.getMainWorker
= sinon
.stub().returns({
186 send
: sinon
.stub().returns()
188 worker
.handleKillMessage()
189 expect(worker
.getMainWorker().send
.calledOnce
).toBe(true)
190 expect(worker
.opts
.killHandler
.calledOnce
).toBe(true)
193 it('Verify that async kill handler is called when worker is killed', () => {
194 const killHandlerStub
= sinon
.stub().returns()
195 const worker
= new ClusterWorker(() => {}, {
196 killHandler
: async () => Promise
.resolve(killHandlerStub())
198 worker
.isMain
= false
199 worker
.handleKillMessage()
200 expect(killHandlerStub
.calledOnce
).toBe(true)
203 it('Verify that handleError() method works properly', () => {
204 const error
= new Error('Error as an error')
205 const worker
= new ClusterWorker(() => {})
206 expect(worker
.handleError(error
)).not
.toBeInstanceOf(Error
)
207 expect(worker
.handleError(error
)).toStrictEqual(error
.message
)
208 const errorMessage
= 'Error as a string'
209 expect(worker
.handleError(errorMessage
)).toStrictEqual(errorMessage
)
212 it('Verify that getMainWorker() throw error if main worker is not set', () => {
214 new StubWorkerWithMainWorker(() => {}).getMainWorker()
215 ).toThrowError('Main worker not set')
218 it('Verify that hasTaskFunction() works', () => {
225 const worker
= new ClusterWorker({ fn1
, fn2
})
226 expect(() => worker
.hasTaskFunction(0)).toThrowError(
227 new TypeError('name parameter is not a string')
229 expect(() => worker
.hasTaskFunction('')).toThrowError(
230 new TypeError('name parameter is an empty string')
232 expect(worker
.hasTaskFunction(DEFAULT_TASK_NAME
)).toBe(true)
233 expect(worker
.hasTaskFunction('fn1')).toBe(true)
234 expect(worker
.hasTaskFunction('fn2')).toBe(true)
235 expect(worker
.hasTaskFunction('fn3')).toBe(false)
238 it('Verify that addTaskFunction() works', () => {
245 const fn1Replacement
= () => {
248 const worker
= new ThreadWorker(fn1
)
249 expect(() => worker
.addTaskFunction(0, fn1
)).toThrowError(
250 new TypeError('name parameter is not a string')
252 expect(() => worker
.addTaskFunction('', fn1
)).toThrowError(
253 new TypeError('name parameter is an empty string')
255 expect(() => worker
.addTaskFunction('fn3', '')).toThrowError(
256 new TypeError('fn parameter is not a function')
258 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
259 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
260 expect(worker
.taskFunctions
.size
).toBe(2)
261 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
262 worker
.taskFunctions
.get('fn1')
264 expect(() => worker
.addTaskFunction(DEFAULT_TASK_NAME
, fn2
)).toThrowError(
265 new Error('Cannot add a task function with the default reserved name')
267 worker
.addTaskFunction('fn2', fn2
)
268 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
269 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
270 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
271 expect(worker
.taskFunctions
.size
).toBe(3)
272 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
273 worker
.taskFunctions
.get('fn1')
275 worker
.addTaskFunction('fn1', fn1Replacement
)
276 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
277 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
278 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
279 expect(worker
.taskFunctions
.size
).toBe(3)
280 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
281 worker
.taskFunctions
.get('fn1')
285 it('Verify that removeTaskFunction() works', () => {
292 const worker
= new ClusterWorker({ fn1
, fn2
})
293 expect(() => worker
.removeTaskFunction(0, fn1
)).toThrowError(
294 new TypeError('name parameter is not a string')
296 expect(() => worker
.removeTaskFunction('', fn1
)).toThrowError(
297 new TypeError('name parameter is an empty string')
299 worker
.getMainWorker
= sinon
.stub().returns({
301 send
: sinon
.stub().returns()
303 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
304 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
305 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
306 expect(worker
.taskFunctions
.size
).toBe(3)
307 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
308 worker
.taskFunctions
.get('fn1')
310 expect(() => worker
.removeTaskFunction(DEFAULT_TASK_NAME
)).toThrowError(
312 'Cannot remove the task function with the default reserved name'
315 expect(() => worker
.removeTaskFunction('fn1')).toThrowError(
317 'Cannot remove the task function used as the default task function'
320 worker
.removeTaskFunction('fn2')
321 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
322 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
323 expect(worker
.taskFunctions
.get('fn2')).toBeUndefined()
324 expect(worker
.taskFunctions
.size
).toBe(2)
325 expect(worker
.getMainWorker().send
.calledOnce
).toBe(true)
328 it('Verify that listTaskFunctions() works', () => {
335 const worker
= new ClusterWorker({ fn1
, fn2
})
336 expect(worker
.listTaskFunctions()).toStrictEqual([
343 it('Verify that setDefaultTaskFunction() works', () => {
350 const worker
= new ThreadWorker({ fn1
, fn2
})
351 expect(() => worker
.setDefaultTaskFunction(0, fn1
)).toThrowError(
352 new TypeError('name parameter is not a string')
354 expect(() => worker
.setDefaultTaskFunction('', fn1
)).toThrowError(
355 new TypeError('name parameter is an empty string')
357 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
358 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
359 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
360 expect(worker
.taskFunctions
.size
).toBe(3)
361 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
362 worker
.taskFunctions
.get('fn1')
364 expect(() => worker
.setDefaultTaskFunction(DEFAULT_TASK_NAME
)).toThrowError(
366 'Cannot set the default task function reserved name as the default task function'
369 expect(() => worker
.setDefaultTaskFunction('fn3')).toThrowError(
371 'Cannot set the default task function to a non-existing task function'
374 worker
.setDefaultTaskFunction('fn1')
375 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
376 worker
.taskFunctions
.get('fn1')
378 worker
.setDefaultTaskFunction('fn2')
379 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
380 worker
.taskFunctions
.get('fn2')