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
.maxInactiveTime
).toStrictEqual(60000)
21 expect(worker
.opts
.killBehavior
).toBe(KillBehaviors
.SOFT
)
22 expect(worker
.opts
.killHandler
).toStrictEqual(EMPTY_FUNCTION
)
23 expect(worker
.opts
.async
).toBe(undefined)
26 it('Verify that worker options are set at worker creation', () => {
27 const killHandler
= () => {
28 console
.info('Worker received kill message')
30 const worker
= new ClusterWorker(() => {}, {
31 maxInactiveTime
: 6000,
32 killBehavior
: KillBehaviors
.HARD
,
36 expect(worker
.opts
.maxInactiveTime
).toStrictEqual(6000)
37 expect(worker
.opts
.killBehavior
).toBe(KillBehaviors
.HARD
)
38 expect(worker
.opts
.killHandler
).toStrictEqual(killHandler
)
39 expect(worker
.opts
.async
).toBe(undefined)
42 it('Verify that taskFunctions parameter is mandatory', () => {
43 expect(() => new ClusterWorker()).toThrowError(
44 'taskFunctions parameter is mandatory'
48 it('Verify that taskFunctions parameter is a function or a plain object', () => {
49 expect(() => new ClusterWorker(0)).toThrowError(
51 'taskFunctions parameter is not a function or a plain object'
54 expect(() => new ClusterWorker('')).toThrowError(
56 'taskFunctions parameter is not a function or a plain object'
59 expect(() => new ClusterWorker(true)).toThrowError(
61 'taskFunctions parameter is not a function or a plain object'
64 expect(() => new ClusterWorker([])).toThrowError(
66 'taskFunctions parameter is not a function or a plain object'
69 expect(() => new ClusterWorker(new Map())).toThrowError(
71 'taskFunctions parameter is not a function or a plain object'
74 expect(() => new ClusterWorker(new Set())).toThrowError(
76 'taskFunctions parameter is not a function or a plain object'
79 expect(() => new ClusterWorker(new WeakMap())).toThrowError(
81 'taskFunctions parameter is not a function or a plain object'
84 expect(() => new ClusterWorker(new WeakSet())).toThrowError(
86 'taskFunctions parameter is not a function or a plain object'
91 it('Verify that taskFunctions parameter is not an empty object', () => {
92 expect(() => new ClusterWorker({})).toThrowError(
93 new Error('taskFunctions parameter object is empty')
97 it('Verify that taskFunctions parameter with unique function is taken', () => {
98 const worker
= new ThreadWorker(() => {})
99 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
100 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
101 expect(worker
.taskFunctions
.size
).toBe(2)
102 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
103 worker
.taskFunctions
.get('fn1')
107 it('Verify that taskFunctions parameter with multiple task functions is checked', () => {
112 expect(() => new ThreadWorker({ '': fn1
})).toThrowError(
113 new TypeError('A taskFunctions parameter object key is an empty string')
115 expect(() => new ThreadWorker({ fn1
, fn2
})).toThrowError(
116 new TypeError('A taskFunctions parameter object value is not a function')
120 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
127 const worker
= new ClusterWorker({ fn1
, fn2
})
128 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
129 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
130 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
131 expect(worker
.taskFunctions
.size
).toBe(3)
132 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
133 worker
.taskFunctions
.get('fn1')
137 it('Verify that sync kill handler is called when worker is killed', () => {
138 const worker
= new ClusterWorker(() => {}, {
139 killHandler
: sinon
.stub().returns()
141 worker
.isMain
= false
142 worker
.getMainWorker
= sinon
.stub().returns({
144 send
: sinon
.stub().returns()
146 worker
.handleKillMessage()
147 expect(worker
.getMainWorker().send
.calledOnce
).toBe(true)
148 expect(worker
.opts
.killHandler
.calledOnce
).toBe(true)
151 it('Verify that async kill handler is called when worker is killed', () => {
152 const killHandlerStub
= sinon
.stub().returns()
153 const worker
= new ClusterWorker(() => {}, {
154 killHandler
: async () => Promise
.resolve(killHandlerStub())
156 worker
.isMain
= false
157 worker
.handleKillMessage()
158 expect(killHandlerStub
.calledOnce
).toBe(true)
161 it('Verify that handleError() method works properly', () => {
162 const error
= new Error('Error as an error')
163 const worker
= new ClusterWorker(() => {})
164 expect(worker
.handleError(error
)).not
.toBeInstanceOf(Error
)
165 expect(worker
.handleError(error
)).toStrictEqual(error
.message
)
166 const errorMessage
= 'Error as a string'
167 expect(worker
.handleError(errorMessage
)).toStrictEqual(errorMessage
)
170 it('Verify that getMainWorker() throw error if main worker is not set', () => {
172 new StubWorkerWithMainWorker(() => {}).getMainWorker()
173 ).toThrowError('Main worker not set')
176 it('Verify that hasTaskFunction() works', () => {
183 const worker
= new ClusterWorker({ fn1
, fn2
})
184 expect(() => worker
.hasTaskFunction(0)).toThrowError(
185 new TypeError('name parameter is not a string')
187 expect(() => worker
.hasTaskFunction('')).toThrowError(
188 new TypeError('name parameter is an empty string')
190 expect(worker
.hasTaskFunction(DEFAULT_TASK_NAME
)).toBe(true)
191 expect(worker
.hasTaskFunction('fn1')).toBe(true)
192 expect(worker
.hasTaskFunction('fn2')).toBe(true)
193 expect(worker
.hasTaskFunction('fn3')).toBe(false)
196 it('Verify that addTaskFunction() works', () => {
203 const fn1Replacement
= () => {
206 const worker
= new ThreadWorker(fn1
)
207 expect(() => worker
.addTaskFunction(0, fn1
)).toThrowError(
208 new TypeError('name parameter is not a string')
210 expect(() => worker
.addTaskFunction('', fn1
)).toThrowError(
211 new TypeError('name parameter is an empty string')
213 expect(() => worker
.addTaskFunction('fn3', '')).toThrowError(
214 new TypeError('fn parameter is not a function')
216 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
217 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
218 expect(worker
.taskFunctions
.size
).toBe(2)
219 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
220 worker
.taskFunctions
.get('fn1')
222 expect(() => worker
.addTaskFunction(DEFAULT_TASK_NAME
, fn2
)).toThrowError(
223 new Error('Cannot add a task function with the default reserved name')
225 worker
.addTaskFunction('fn2', fn2
)
226 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
227 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
228 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
229 expect(worker
.taskFunctions
.size
).toBe(3)
230 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
231 worker
.taskFunctions
.get('fn1')
233 worker
.addTaskFunction('fn1', fn1Replacement
)
234 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
235 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
236 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
237 expect(worker
.taskFunctions
.size
).toBe(3)
238 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
239 worker
.taskFunctions
.get('fn1')
243 it('Verify that removeTaskFunction() works', () => {
250 const worker
= new ClusterWorker({ fn1
, fn2
})
251 expect(() => worker
.removeTaskFunction(0, fn1
)).toThrowError(
252 new TypeError('name parameter is not a string')
254 expect(() => worker
.removeTaskFunction('', fn1
)).toThrowError(
255 new TypeError('name parameter is an empty string')
257 worker
.getMainWorker
= sinon
.stub().returns({
259 send
: sinon
.stub().returns()
261 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
262 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
263 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
264 expect(worker
.taskFunctions
.size
).toBe(3)
265 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
266 worker
.taskFunctions
.get('fn1')
268 expect(() => worker
.removeTaskFunction(DEFAULT_TASK_NAME
)).toThrowError(
270 'Cannot remove the task function with the default reserved name'
273 expect(() => worker
.removeTaskFunction('fn1')).toThrowError(
275 'Cannot remove the task function used as the default task function'
278 worker
.removeTaskFunction('fn2')
279 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
280 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
281 expect(worker
.taskFunctions
.get('fn2')).toBeUndefined()
282 expect(worker
.taskFunctions
.size
).toBe(2)
283 expect(worker
.getMainWorker().send
.calledOnce
).toBe(true)
286 it('Verify that listTaskFunctions() works', () => {
293 const worker
= new ClusterWorker({ fn1
, fn2
})
294 expect(worker
.listTaskFunctions()).toStrictEqual([
301 it('Verify that setDefaultTaskFunction() works', () => {
308 const worker
= new ThreadWorker({ fn1
, fn2
})
309 expect(() => worker
.setDefaultTaskFunction(0, fn1
)).toThrowError(
310 new TypeError('name parameter is not a string')
312 expect(() => worker
.setDefaultTaskFunction('', fn1
)).toThrowError(
313 new TypeError('name parameter is an empty string')
315 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toBeInstanceOf(Function
)
316 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
317 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
318 expect(worker
.taskFunctions
.size
).toBe(3)
319 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
320 worker
.taskFunctions
.get('fn1')
322 expect(() => worker
.setDefaultTaskFunction(DEFAULT_TASK_NAME
)).toThrowError(
324 'Cannot set the default task function reserved name as the default task function'
327 expect(() => worker
.setDefaultTaskFunction('fn3')).toThrowError(
329 'Cannot set the default task function to a non-existing task function'
332 worker
.setDefaultTaskFunction('fn1')
333 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
334 worker
.taskFunctions
.get('fn1')
336 worker
.setDefaultTaskFunction('fn2')
337 expect(worker
.taskFunctions
.get(DEFAULT_TASK_NAME
)).toStrictEqual(
338 worker
.taskFunctions
.get('fn2')