refactor: silence sonar code smells
[poolifier.git] / tests / worker / abstract-worker.test.js
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')
5
6 describe('Abstract worker test suite', () => {
7 class StubWorkerWithMainWorker extends ThreadWorker {
8 constructor (fn, opts) {
9 super(fn, opts)
10 this.mainWorker = undefined
11 }
12 }
13
14 afterEach(() => {
15 sinon.restore()
16 })
17
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)
24 })
25
26 it('Verify that worker options are set at worker creation', () => {
27 const killHandler = () => {
28 console.info('Worker received kill message')
29 }
30 const worker = new ClusterWorker(() => {}, {
31 maxInactiveTime: 6000,
32 killBehavior: KillBehaviors.HARD,
33 killHandler,
34 async: true
35 })
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)
40 })
41
42 it('Verify that taskFunctions parameter is mandatory', () => {
43 expect(() => new ClusterWorker()).toThrowError(
44 'taskFunctions parameter is mandatory'
45 )
46 })
47
48 it('Verify that taskFunctions parameter is a function or a plain object', () => {
49 expect(() => new ClusterWorker(0)).toThrowError(
50 new TypeError(
51 'taskFunctions parameter is not a function or a plain object'
52 )
53 )
54 expect(() => new ClusterWorker('')).toThrowError(
55 new TypeError(
56 'taskFunctions parameter is not a function or a plain object'
57 )
58 )
59 expect(() => new ClusterWorker(true)).toThrowError(
60 new TypeError(
61 'taskFunctions parameter is not a function or a plain object'
62 )
63 )
64 expect(() => new ClusterWorker([])).toThrowError(
65 new TypeError(
66 'taskFunctions parameter is not a function or a plain object'
67 )
68 )
69 expect(() => new ClusterWorker(new Map())).toThrowError(
70 new TypeError(
71 'taskFunctions parameter is not a function or a plain object'
72 )
73 )
74 expect(() => new ClusterWorker(new Set())).toThrowError(
75 new TypeError(
76 'taskFunctions parameter is not a function or a plain object'
77 )
78 )
79 expect(() => new ClusterWorker(new WeakMap())).toThrowError(
80 new TypeError(
81 'taskFunctions parameter is not a function or a plain object'
82 )
83 )
84 expect(() => new ClusterWorker(new WeakSet())).toThrowError(
85 new TypeError(
86 'taskFunctions parameter is not a function or a plain object'
87 )
88 )
89 })
90
91 it('Verify that taskFunctions parameter is not an empty object', () => {
92 expect(() => new ClusterWorker({})).toThrowError(
93 new Error('taskFunctions parameter object is empty')
94 )
95 })
96
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')
104 )
105 })
106
107 it('Verify that taskFunctions parameter with multiple task functions is checked', () => {
108 const fn1 = () => {
109 return 1
110 }
111 const fn2 = ''
112 expect(() => new ThreadWorker({ '': fn1 })).toThrowError(
113 new TypeError('A taskFunctions parameter object key is an empty string')
114 )
115 expect(() => new ThreadWorker({ fn1, fn2 })).toThrowError(
116 new TypeError('A taskFunctions parameter object value is not a function')
117 )
118 })
119
120 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
121 const fn1 = () => {
122 return 1
123 }
124 const fn2 = () => {
125 return 2
126 }
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')
134 )
135 })
136
137 it('Verify that sync kill handler is called when worker is killed', () => {
138 const worker = new ClusterWorker(() => {}, {
139 killHandler: sinon.stub().returns()
140 })
141 worker.isMain = false
142 worker.getMainWorker = sinon.stub().returns({
143 id: 1,
144 send: sinon.stub().returns()
145 })
146 worker.handleKillMessage()
147 expect(worker.getMainWorker().send.calledOnce).toBe(true)
148 expect(worker.opts.killHandler.calledOnce).toBe(true)
149 })
150
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())
155 })
156 worker.isMain = false
157 worker.handleKillMessage()
158 expect(killHandlerStub.calledOnce).toBe(true)
159 })
160
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)
168 })
169
170 it('Verify that getMainWorker() throw error if main worker is not set', () => {
171 expect(() =>
172 new StubWorkerWithMainWorker(() => {}).getMainWorker()
173 ).toThrowError('Main worker not set')
174 })
175
176 it('Verify that hasTaskFunction() works', () => {
177 const fn1 = () => {
178 return 1
179 }
180 const fn2 = () => {
181 return 2
182 }
183 const worker = new ClusterWorker({ fn1, fn2 })
184 expect(() => worker.hasTaskFunction(0)).toThrowError(
185 new TypeError('name parameter is not a string')
186 )
187 expect(() => worker.hasTaskFunction('')).toThrowError(
188 new TypeError('name parameter is an empty string')
189 )
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)
194 })
195
196 it('Verify that addTaskFunction() works', () => {
197 const fn1 = () => {
198 return 1
199 }
200 const fn2 = () => {
201 return 2
202 }
203 const fn1Replacement = () => {
204 return 3
205 }
206 const worker = new ThreadWorker(fn1)
207 expect(() => worker.addTaskFunction(0, fn1)).toThrowError(
208 new TypeError('name parameter is not a string')
209 )
210 expect(() => worker.addTaskFunction('', fn1)).toThrowError(
211 new TypeError('name parameter is an empty string')
212 )
213 expect(() => worker.addTaskFunction('fn3', '')).toThrowError(
214 new TypeError('fn parameter is not a function')
215 )
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')
221 )
222 expect(() => worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toThrowError(
223 new Error('Cannot add a task function with the default reserved name')
224 )
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')
232 )
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')
240 )
241 })
242
243 it('Verify that removeTaskFunction() works', () => {
244 const fn1 = () => {
245 return 1
246 }
247 const fn2 = () => {
248 return 2
249 }
250 const worker = new ClusterWorker({ fn1, fn2 })
251 expect(() => worker.removeTaskFunction(0, fn1)).toThrowError(
252 new TypeError('name parameter is not a string')
253 )
254 expect(() => worker.removeTaskFunction('', fn1)).toThrowError(
255 new TypeError('name parameter is an empty string')
256 )
257 worker.getMainWorker = sinon.stub().returns({
258 id: 1,
259 send: sinon.stub().returns()
260 })
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')
267 )
268 expect(() => worker.removeTaskFunction(DEFAULT_TASK_NAME)).toThrowError(
269 new Error(
270 'Cannot remove the task function with the default reserved name'
271 )
272 )
273 expect(() => worker.removeTaskFunction('fn1')).toThrowError(
274 new Error(
275 'Cannot remove the task function used as the default task function'
276 )
277 )
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)
284 })
285
286 it('Verify that listTaskFunctions() works', () => {
287 const fn1 = () => {
288 return 1
289 }
290 const fn2 = () => {
291 return 2
292 }
293 const worker = new ClusterWorker({ fn1, fn2 })
294 expect(worker.listTaskFunctions()).toStrictEqual([
295 DEFAULT_TASK_NAME,
296 'fn1',
297 'fn2'
298 ])
299 })
300
301 it('Verify that setDefaultTaskFunction() works', () => {
302 const fn1 = () => {
303 return 1
304 }
305 const fn2 = () => {
306 return 2
307 }
308 const worker = new ThreadWorker({ fn1, fn2 })
309 expect(() => worker.setDefaultTaskFunction(0, fn1)).toThrowError(
310 new TypeError('name parameter is not a string')
311 )
312 expect(() => worker.setDefaultTaskFunction('', fn1)).toThrowError(
313 new TypeError('name parameter is an empty string')
314 )
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')
321 )
322 expect(() => worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toThrowError(
323 new Error(
324 'Cannot set the default task function reserved name as the default task function'
325 )
326 )
327 expect(() => worker.setDefaultTaskFunction('fn3')).toThrowError(
328 new Error(
329 'Cannot set the default task function to a non-existing task function'
330 )
331 )
332 worker.setDefaultTaskFunction('fn1')
333 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
334 worker.taskFunctions.get('fn1')
335 )
336 worker.setDefaultTaskFunction('fn2')
337 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
338 worker.taskFunctions.get('fn2')
339 )
340 })
341 })