6582961febd4fe699dc1b1a4977604f5023edf68
1 const { expect
} = require('expect')
2 const sinon
= require('sinon')
3 const { ClusterWorker
, KillBehaviors
, ThreadWorker
} = require('../../lib')
4 const { EMPTY_FUNCTION
} = require('../../lib/utils')
6 describe('Abstract worker test suite', () => {
7 class StubWorkerWithMainWorker
extends ThreadWorker
{
8 constructor (fn
, opts
) {
10 this.mainWorker
= undefined
14 it('Verify worker options default values', () => {
15 const worker
= new ThreadWorker(() => {})
16 expect(worker
.opts
.maxInactiveTime
).toStrictEqual(60000)
17 expect(worker
.opts
.killBehavior
).toBe(KillBehaviors
.SOFT
)
18 expect(worker
.opts
.killHandler
).toStrictEqual(EMPTY_FUNCTION
)
19 expect(worker
.opts
.async
).toBe(undefined)
22 it('Verify that worker options are set at worker creation', () => {
23 const killHandler
= () => {
24 console
.info('Worker received kill message')
26 const worker
= new ClusterWorker(() => {}, {
27 maxInactiveTime
: 6000,
28 killBehavior
: KillBehaviors
.HARD
,
32 expect(worker
.opts
.maxInactiveTime
).toStrictEqual(6000)
33 expect(worker
.opts
.killBehavior
).toBe(KillBehaviors
.HARD
)
34 expect(worker
.opts
.killHandler
).toStrictEqual(killHandler
)
35 expect(worker
.opts
.async
).toBe(undefined)
38 it('Verify that taskFunctions parameter is mandatory', () => {
39 expect(() => new ClusterWorker()).toThrowError(
40 'taskFunctions parameter is mandatory'
44 it('Verify that taskFunctions parameter is a function or a plain object', () => {
45 expect(() => new ClusterWorker(0)).toThrowError(
47 'taskFunctions parameter is not a function or a plain object'
50 expect(() => new ClusterWorker('')).toThrowError(
52 'taskFunctions parameter is not a function or a plain object'
55 expect(() => new ClusterWorker(true)).toThrowError(
57 'taskFunctions parameter is not a function or a plain object'
60 expect(() => new ClusterWorker([])).toThrowError(
62 'taskFunctions parameter is not a function or a plain object'
65 expect(() => new ClusterWorker(new Map())).toThrowError(
67 'taskFunctions parameter is not a function or a plain object'
70 expect(() => new ClusterWorker(new Set())).toThrowError(
72 'taskFunctions parameter is not a function or a plain object'
75 expect(() => new ClusterWorker(new WeakMap())).toThrowError(
77 'taskFunctions parameter is not a function or a plain object'
80 expect(() => new ClusterWorker(new WeakSet())).toThrowError(
82 'taskFunctions parameter is not a function or a plain object'
87 it('Verify that taskFunctions parameter is not an empty object', () => {
88 expect(() => new ClusterWorker({})).toThrowError(
89 new Error('taskFunctions parameter object is empty')
93 it('Verify that taskFunctions parameter with unique function is taken', () => {
94 const worker
= new ThreadWorker(() => {})
95 expect(worker
.taskFunctions
.get('default')).toBeInstanceOf(Function
)
96 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
97 expect(worker
.taskFunctions
.size
).toBe(2)
98 expect(worker
.taskFunctions
.get('default')).toStrictEqual(
99 worker
.taskFunctions
.get('fn1')
103 it('Verify that taskFunctions parameter with multiple task functions contains function', () => {
108 expect(() => new ThreadWorker({ fn1
, fn2
})).toThrowError(
109 new TypeError('A taskFunctions parameter object value is not a function')
113 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
120 const worker
= new ClusterWorker({ fn1
, fn2
})
121 expect(worker
.taskFunctions
.get('default')).toBeInstanceOf(Function
)
122 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
123 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
124 expect(worker
.taskFunctions
.size
).toBe(3)
125 expect(worker
.taskFunctions
.get('default')).toStrictEqual(
126 worker
.taskFunctions
.get('fn1')
130 it('Verify that sync kill handler is called when worker is killed', () => {
131 const worker
= new ClusterWorker(() => {}, {
132 killHandler
: sinon
.stub().returns()
134 worker
.isMain
= false
135 worker
.handleKillMessage()
136 expect(worker
.opts
.killHandler
.calledOnce
).toBe(true)
139 // it('Verify that async kill handler is called when worker is killed', () => {
140 // const worker = new ClusterWorker(() => {}, {
141 // killHandler: sinon.stub().resolves()
143 // worker.isMain = false
144 // worker.handleKillMessage()
145 // expect(worker.opts.killHandler.calledOnce).toBe(true)
148 it('Verify that handleError() method works properly', () => {
149 const error
= new Error('Error as an error')
150 const worker
= new ClusterWorker(() => {})
151 expect(worker
.handleError(error
)).not
.toBeInstanceOf(Error
)
152 expect(worker
.handleError(error
)).toStrictEqual(error
.message
)
153 const errorMessage
= 'Error as a string'
154 expect(worker
.handleError(errorMessage
)).toStrictEqual(errorMessage
)
157 it('Verify that getMainWorker() throw error if main worker is not set', () => {
159 new StubWorkerWithMainWorker(() => {}).getMainWorker()
160 ).toThrowError('Main worker not set')
163 it('Verify that hasTaskFunction() works', () => {
170 const worker
= new ClusterWorker({ fn1
, fn2
})
171 expect(worker
.hasTaskFunction('default')).toBe(true)
172 expect(worker
.hasTaskFunction('fn1')).toBe(true)
173 expect(worker
.hasTaskFunction('fn2')).toBe(true)
174 expect(worker
.hasTaskFunction('fn3')).toBe(false)
177 it('Verify that addTaskFunction() works', () => {
184 const fn1Replacement
= () => {
187 const worker
= new ThreadWorker(fn1
)
188 expect(worker
.taskFunctions
.get('default')).toBeInstanceOf(Function
)
189 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
190 expect(worker
.taskFunctions
.size
).toBe(2)
191 expect(worker
.taskFunctions
.get('default')).toStrictEqual(
192 worker
.taskFunctions
.get('fn1')
194 expect(() => worker
.addTaskFunction('default', fn2
)).toThrowError(
195 new Error('Cannot add a task function with the default reserved name')
197 worker
.addTaskFunction('fn2', fn2
)
198 expect(worker
.taskFunctions
.get('default')).toBeInstanceOf(Function
)
199 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
200 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
201 expect(worker
.taskFunctions
.size
).toBe(3)
202 expect(worker
.taskFunctions
.get('default')).toStrictEqual(
203 worker
.taskFunctions
.get('fn1')
205 worker
.addTaskFunction('fn1', fn1Replacement
)
206 expect(worker
.taskFunctions
.get('default')).toBeInstanceOf(Function
)
207 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
208 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
209 expect(worker
.taskFunctions
.size
).toBe(3)
210 expect(worker
.taskFunctions
.get('default')).toStrictEqual(
211 worker
.taskFunctions
.get('fn1')
215 it('Verify that removeTaskFunction() works', () => {
222 const worker
= new ClusterWorker({ fn1
, fn2
})
223 expect(worker
.taskFunctions
.get('default')).toBeInstanceOf(Function
)
224 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
225 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
226 expect(worker
.taskFunctions
.size
).toBe(3)
227 expect(worker
.taskFunctions
.get('default')).toStrictEqual(
228 worker
.taskFunctions
.get('fn1')
230 expect(() => worker
.removeTaskFunction('default')).toThrowError(
232 'Cannot remove the task function with the default reserved name'
235 expect(() => worker
.removeTaskFunction('fn1')).toThrowError(
237 'Cannot remove the task function used as the default task function'
240 worker
.removeTaskFunction('fn2')
241 expect(worker
.taskFunctions
.get('default')).toBeInstanceOf(Function
)
242 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
243 expect(worker
.taskFunctions
.get('fn2')).toBeUndefined()
244 expect(worker
.taskFunctions
.size
).toBe(2)
247 it('Verify that listTaskFunctions() works', () => {
254 const worker
= new ClusterWorker({ fn1
, fn2
})
255 expect(worker
.listTaskFunctions()).toStrictEqual(['default', 'fn1', 'fn2'])
258 it('Verify that setDefaultTaskFunction() works', () => {
265 const worker
= new ThreadWorker({ fn1
, fn2
})
266 expect(worker
.taskFunctions
.get('default')).toBeInstanceOf(Function
)
267 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
268 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
269 expect(worker
.taskFunctions
.size
).toBe(3)
270 expect(worker
.taskFunctions
.get('default')).toStrictEqual(
271 worker
.taskFunctions
.get('fn1')
273 expect(() => worker
.setDefaultTaskFunction('default')).toThrowError(
275 'Cannot set the default task function reserved name as the default task function'
278 worker
.setDefaultTaskFunction('fn1')
279 expect(worker
.taskFunctions
.get('default')).toStrictEqual(
280 worker
.taskFunctions
.get('fn1')
282 worker
.setDefaultTaskFunction('fn2')
283 expect(worker
.taskFunctions
.get('default')).toStrictEqual(
284 worker
.taskFunctions
.get('fn2')