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 set at worker creation', () => {
28 const killHandler
= () => {
29 console
.info('Worker received kill message')
31 const worker
= new ClusterWorker(() => {}, {
32 killBehavior
: KillBehaviors
.HARD
,
33 maxInactiveTime
: 6000,
37 expect(worker
.opts
).toStrictEqual({
38 killBehavior
: KillBehaviors
.HARD
,
39 maxInactiveTime
: 6000,
44 it('Verify that taskFunctions parameter is mandatory', () => {
45 expect(() => new ClusterWorker()).toThrowError(
46 'taskFunctions parameter is mandatory'
50 it('Verify that taskFunctions parameter is a function or a plain object', () => {
51 expect(() => new ClusterWorker(0)).toThrowError(
53 'taskFunctions parameter is not a function or a plain object'
56 expect(() => new ClusterWorker('')).toThrowError(
58 'taskFunctions parameter is not a function or a plain object'
61 expect(() => new ClusterWorker(true)).toThrowError(
63 'taskFunctions parameter is not a function or a plain object'
66 expect(() => new ClusterWorker([])).toThrowError(
68 'taskFunctions parameter is not a function or a plain object'
71 expect(() => new ClusterWorker(new Map())).toThrowError(
73 'taskFunctions parameter is not a function or a plain object'
76 expect(() => new ClusterWorker(new Set())).toThrowError(
78 'taskFunctions parameter is not a function or a plain object'
81 expect(() => new ClusterWorker(new WeakMap())).toThrowError(
83 'taskFunctions parameter is not a function or a plain object'
86 expect(() => new ClusterWorker(new WeakSet())).toThrowError(
88 'taskFunctions parameter is not a function or a plain object'
93 it('Verify that taskFunctions parameter is not an empty object', () => {
94 expect(() => new ClusterWorker({})).toThrowError(
95 new Error('taskFunctions parameter object is empty')
99 it('Verify that taskFunctions parameter with unique function is taken', () => {
100 const worker
= new ThreadWorker(() => {})
101 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
102 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
103 expect(worker
.taskFunctions
.size
).toBe(2)
104 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
105 worker
.taskFunctions
.get('fn1')
109 it('Verify that taskFunctions parameter with multiple task functions is checked', () => {
114 expect(() => new ThreadWorker({ '': fn1
})).toThrowError(
115 new TypeError('A taskFunctions parameter object key is an empty string')
117 expect(() => new ThreadWorker({ fn1
, fn2
})).toThrowError(
118 new TypeError('A taskFunctions parameter object value is not a function')
122 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
129 const worker
= new ClusterWorker({ fn1
, fn2
})
130 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
131 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
132 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
133 expect(worker
.taskFunctions
.size
).toBe(3)
134 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
135 worker
.taskFunctions
.get('fn1')
139 it('Verify that sync kill handler is called when worker is killed', () => {
140 const worker
= new ClusterWorker(() => {}, {
141 killHandler
: sinon
.stub().returns()
143 worker
.isMain
= false
144 worker
.getMainWorker
= sinon
.stub().returns({
146 send
: sinon
.stub().returns()
148 worker
.handleKillMessage()
149 expect(worker
.getMainWorker().send
.calledOnce
).toBe(true)
150 expect(worker
.opts
.killHandler
.calledOnce
).toBe(true)
153 it('Verify that async kill handler is called when worker is killed', () => {
154 const killHandlerStub
= sinon
.stub().returns()
155 const worker
= new ClusterWorker(() => {}, {
156 killHandler
: async () => Promise
.resolve(killHandlerStub())
158 worker
.isMain
= false
159 worker
.handleKillMessage()
160 expect(killHandlerStub
.calledOnce
).toBe(true)
163 it('Verify that handleError() method works properly', () => {
164 const error
= new Error('Error as an error')
165 const worker
= new ClusterWorker(() => {})
166 expect(worker
.handleError(error
)).not
.toBeInstanceOf(Error
)
167 expect(worker
.handleError(error
)).toStrictEqual(error
.message
)
168 const errorMessage
= 'Error as a string'
169 expect(worker
.handleError(errorMessage
)).toStrictEqual(errorMessage
)
172 it('Verify that getMainWorker() throw error if main worker is not set', () => {
174 new StubWorkerWithMainWorker(() => {}).getMainWorker()
175 ).toThrowError('Main worker not set')
178 it('Verify that hasTaskFunction() works', () => {
185 const worker
= new ClusterWorker({ fn1
, fn2
})
186 expect(worker
.hasTaskFunction(0)).toStrictEqual({
188 error
: new TypeError('name parameter is not a string')
190 expect(worker
.hasTaskFunction('')).toStrictEqual({
192 error
: new TypeError('name parameter is an empty string')
194 expect(worker
.hasTaskFunction(DEFAULT_TASK_NAME
)).toStrictEqual({
197 expect(worker
.hasTaskFunction('fn1')).toStrictEqual({ status
: true })
198 expect(worker
.hasTaskFunction('fn2')).toStrictEqual({ status
: true })
199 expect(worker
.hasTaskFunction('fn3')).toStrictEqual({ status
: false })
202 it('Verify that addTaskFunction() works', () => {
209 const fn1Replacement
= () => {
212 const worker
= new ThreadWorker(fn1
)
213 expect(worker
.addTaskFunction(0, fn1
)).toStrictEqual({
215 error
: new TypeError('name parameter is not a string')
217 expect(worker
.addTaskFunction('', fn1
)).toStrictEqual({
219 error
: new TypeError('name parameter is an empty string')
221 expect(worker
.addTaskFunction('fn3', '')).toStrictEqual({
223 error
: new TypeError('fn parameter is not a function')
225 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
226 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
227 expect(worker
.taskFunctions
.size
).toBe(2)
228 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
229 worker
.taskFunctions
.get('fn1')
231 expect(worker
.addTaskFunction(DEFAULT_TASK_NAME
, fn2
)).toStrictEqual({
234 'Cannot add a task function with the default reserved name'
237 worker
.addTaskFunction('fn2', fn2
)
238 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
239 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
240 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
241 expect(worker
.taskFunctions
.size
).toBe(3)
242 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
243 worker
.taskFunctions
.get('fn1')
245 worker
.addTaskFunction('fn1', fn1Replacement
)
246 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
247 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
248 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
249 expect(worker
.taskFunctions
.size
).toBe(3)
250 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
251 worker
.taskFunctions
.get('fn1')
255 it('Verify that removeTaskFunction() works', () => {
262 const worker
= new ClusterWorker({ fn1
, fn2
})
263 expect(worker
.removeTaskFunction(0, fn1
)).toStrictEqual({
265 error
: new TypeError('name parameter is not a string')
267 expect(worker
.removeTaskFunction('', fn1
)).toStrictEqual({
269 error
: new TypeError('name parameter is an empty string')
271 worker
.getMainWorker
= sinon
.stub().returns({
273 send
: sinon
.stub().returns()
275 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
276 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
277 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
278 expect(worker
.taskFunctions
.size
).toBe(3)
279 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
280 worker
.taskFunctions
.get('fn1')
282 expect(worker
.removeTaskFunction(DEFAULT_TASK_NAME
)).toStrictEqual({
285 'Cannot remove the task function with the default reserved name'
288 expect(worker
.removeTaskFunction('fn1')).toStrictEqual({
291 'Cannot remove the task function used as the default task function'
294 worker
.removeTaskFunction('fn2')
295 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
296 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
297 expect(worker
.taskFunctions
.get('fn2')).toBeUndefined()
298 expect(worker
.taskFunctions
.size
).toBe(2)
299 expect(worker
.getMainWorker().send
.calledOnce
).toBe(true)
302 it('Verify that listTaskFunctions() works', () => {
309 const worker
= new ClusterWorker({ fn1
, fn2
})
310 expect(worker
.listTaskFunctionNames()).toStrictEqual([
317 it('Verify that setDefaultTaskFunction() works', () => {
324 const worker
= new ThreadWorker({ fn1
, fn2
})
325 expect(worker
.setDefaultTaskFunction(0, fn1
)).toStrictEqual({
327 error
: new TypeError('name parameter is not a string')
329 expect(worker
.setDefaultTaskFunction('', fn1
)).toStrictEqual({
331 error
: new TypeError('name parameter is an empty string')
333 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
334 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
335 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
336 expect(worker
.taskFunctions
.size
).toBe(3)
337 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
338 worker
.taskFunctions
.get('fn1')
340 expect(worker
.setDefaultTaskFunction(DEFAULT_TASK_NAME
)).toStrictEqual({
343 'Cannot set the default task function reserved name as the default task function'
346 expect(worker
.setDefaultTaskFunction('fn3')).toStrictEqual({
349 'Cannot set the default task function to a non-existing task function'
352 worker
.setDefaultTaskFunction('fn1')
353 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
354 worker
.taskFunctions
.get('fn1')
356 worker
.setDefaultTaskFunction('fn2')
357 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
358 worker
.taskFunctions
.get('fn2')