b2aa996dfaa8764f6fd27c693d708807e9d790fc
[poolifier.git] / tests / worker / abstract-worker.test.mjs
1 import { expect } from 'expect'
2 import { restore, stub } from 'sinon'
3
4 import {
5 ClusterWorker,
6 KillBehaviors,
7 ThreadWorker,
8 WorkerChoiceStrategies
9 } from '../../lib/index.cjs'
10 import { DEFAULT_TASK_NAME, EMPTY_FUNCTION } from '../../lib/utils.cjs'
11
12 describe('Abstract worker test suite', () => {
13 class StubWorkerWithMainWorker extends ThreadWorker {
14 constructor (fn, opts) {
15 super(fn, opts)
16 delete this.mainWorker
17 }
18 }
19
20 afterEach(() => {
21 restore()
22 })
23
24 it('Verify worker options default values', () => {
25 const worker = new ThreadWorker(() => {})
26 expect(worker.opts).toStrictEqual({
27 killBehavior: KillBehaviors.SOFT,
28 maxInactiveTime: 60000,
29 killHandler: EMPTY_FUNCTION
30 })
31 })
32
33 it('Verify that worker options are checked at worker creation', () => {
34 expect(() => new ClusterWorker(() => {}, '')).toThrow(
35 new TypeError('opts worker options parameter is not a plain object')
36 )
37 expect(() => new ClusterWorker(() => {}, { killBehavior: '' })).toThrow(
38 new TypeError("killBehavior option '' is not valid")
39 )
40 expect(() => new ClusterWorker(() => {}, { killBehavior: 0 })).toThrow(
41 new TypeError("killBehavior option '0' is not valid")
42 )
43 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: '' })).toThrow(
44 new TypeError('maxInactiveTime option is not an integer')
45 )
46 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 0.5 })).toThrow(
47 new TypeError('maxInactiveTime option is not an integer')
48 )
49 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 0 })).toThrow(
50 new TypeError(
51 'maxInactiveTime option is not a positive integer greater or equal than 5'
52 )
53 )
54 expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 4 })).toThrow(
55 new TypeError(
56 'maxInactiveTime option is not a positive integer greater or equal than 5'
57 )
58 )
59 expect(() => new ThreadWorker(() => {}, { killHandler: '' })).toThrow(
60 new TypeError('killHandler option is not a function')
61 )
62 expect(() => new ThreadWorker(() => {}, { killHandler: 0 })).toThrow(
63 new TypeError('killHandler option is not a function')
64 )
65 })
66
67 it('Verify that worker options are set at worker creation', () => {
68 const killHandler = () => {
69 console.info('Worker received kill message')
70 }
71 const worker = new ClusterWorker(() => {}, {
72 killBehavior: KillBehaviors.HARD,
73 maxInactiveTime: 6000,
74 killHandler
75 })
76 expect(worker.opts).toStrictEqual({
77 killBehavior: KillBehaviors.HARD,
78 maxInactiveTime: 6000,
79 killHandler
80 })
81 })
82
83 it('Verify that taskFunctions parameter is mandatory', () => {
84 expect(() => new ClusterWorker()).toThrow(
85 new Error('taskFunctions parameter is mandatory')
86 )
87 })
88
89 it('Verify that taskFunctions parameter is a function or a plain object', () => {
90 expect(() => new ClusterWorker(0)).toThrow(
91 new TypeError(
92 'taskFunctions parameter is not a function or a plain object'
93 )
94 )
95 expect(() => new ClusterWorker('')).toThrow(
96 new TypeError(
97 'taskFunctions parameter is not a function or a plain object'
98 )
99 )
100 expect(() => new ClusterWorker(true)).toThrow(
101 new TypeError(
102 'taskFunctions parameter is not a function or a plain object'
103 )
104 )
105 expect(() => new ClusterWorker([])).toThrow(
106 new TypeError(
107 'taskFunctions parameter is not a function or a plain object'
108 )
109 )
110 expect(() => new ClusterWorker(new Map())).toThrow(
111 new TypeError(
112 'taskFunctions parameter is not a function or a plain object'
113 )
114 )
115 expect(() => new ClusterWorker(new Set())).toThrow(
116 new TypeError(
117 'taskFunctions parameter is not a function or a plain object'
118 )
119 )
120 expect(() => new ClusterWorker(new WeakMap())).toThrow(
121 new TypeError(
122 'taskFunctions parameter is not a function or a plain object'
123 )
124 )
125 expect(() => new ClusterWorker(new WeakSet())).toThrow(
126 new TypeError(
127 'taskFunctions parameter is not a function or a plain object'
128 )
129 )
130 })
131
132 it('Verify that taskFunctions parameter is not an empty object', () => {
133 expect(() => new ClusterWorker({})).toThrow(
134 new Error('taskFunctions parameter object is empty')
135 )
136 })
137
138 it('Verify that taskFunctions parameter with unique function is taken', () => {
139 const worker = new ThreadWorker(EMPTY_FUNCTION)
140 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
141 taskFunction: expect.any(Function)
142 })
143 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
144 taskFunction: expect.any(Function)
145 })
146 expect(worker.taskFunctions.size).toBe(2)
147 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
148 worker.taskFunctions.get('fn1')
149 )
150 })
151
152 it('Verify that taskFunctions parameter with multiple task functions is checked', () => {
153 const fn1 = () => {
154 return 1
155 }
156 const fn2 = ''
157 expect(() => new ThreadWorker({ '': fn1 })).toThrow(
158 new TypeError('A taskFunctions parameter object key is an empty string')
159 )
160 expect(() => new ThreadWorker({ fn1, fn2 })).toThrow(
161 new TypeError(
162 "taskFunction object 'taskFunction' property 'undefined' is not a function"
163 )
164 )
165 expect(() => new ThreadWorker({ fn1: { fn1 } })).toThrow(
166 new TypeError(
167 "taskFunction object 'taskFunction' property 'undefined' is not a function"
168 )
169 )
170 expect(() => new ThreadWorker({ fn2: { taskFunction: fn2 } })).toThrow(
171 new TypeError(
172 "taskFunction object 'taskFunction' property '' is not a function"
173 )
174 )
175 expect(
176 () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: '' } })
177 ).toThrow(new TypeError("Invalid property 'priority': ''"))
178 expect(
179 () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: -21 } })
180 ).toThrow(new TypeError("Property 'priority' must be between -20 and 19"))
181 expect(
182 () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: 20 } })
183 ).toThrow(new RangeError("Property 'priority' must be between -20 and 19"))
184 expect(
185 () =>
186 new ThreadWorker({
187 fn1: { taskFunction: fn1, strategy: 'invalidStrategy' }
188 })
189 ).toThrow(
190 new RangeError("Invalid worker choice strategy 'invalidStrategy'")
191 )
192 })
193
194 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
195 const fn1 = () => {
196 return 1
197 }
198 const fn2 = () => {
199 return 2
200 }
201 const worker = new ClusterWorker({ fn1, fn2 })
202 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
203 taskFunction: expect.any(Function)
204 })
205 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
206 taskFunction: expect.any(Function)
207 })
208 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
209 taskFunction: expect.any(Function)
210 })
211 expect(worker.taskFunctions.size).toBe(3)
212 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
213 worker.taskFunctions.get('fn1')
214 )
215 })
216
217 it('Verify that taskFunctions parameter with multiple task functions object is taken', () => {
218 const fn1Obj = {
219 taskFunction: () => {
220 return 1
221 },
222 priority: 5
223 }
224 const fn2Obj = {
225 taskFunction: () => {
226 return 2
227 },
228 priority: 6,
229 strategy: WorkerChoiceStrategies.LESS_BUSY
230 }
231 const worker = new ThreadWorker({
232 fn1: fn1Obj,
233 fn2: fn2Obj
234 })
235 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(fn1Obj)
236 expect(worker.taskFunctions.get('fn1')).toStrictEqual(fn1Obj)
237 expect(worker.taskFunctions.get('fn2')).toStrictEqual(fn2Obj)
238 expect(worker.taskFunctions.size).toBe(3)
239 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
240 worker.taskFunctions.get('fn1')
241 )
242 })
243
244 it('Verify that async kill handler is called when worker is killed', () => {
245 const killHandlerStub = stub().returns()
246 const worker = new ClusterWorker(() => {}, {
247 killHandler: async () => await Promise.resolve(killHandlerStub())
248 })
249 worker.isMain = false
250 worker.handleKillMessage()
251 expect(killHandlerStub.calledOnce).toBe(true)
252 })
253
254 it('Verify that getMainWorker() throw error if main worker is not set', () => {
255 expect(() =>
256 new StubWorkerWithMainWorker(() => {}).getMainWorker()
257 ).toThrow('Main worker not set')
258 })
259
260 it('Verify that hasTaskFunction() is working', () => {
261 const fn1 = () => {
262 return 1
263 }
264 const fn2 = () => {
265 return 2
266 }
267 const worker = new ClusterWorker({ fn1, fn2 })
268 expect(worker.hasTaskFunction(0)).toStrictEqual({
269 status: false,
270 error: new TypeError('name parameter is not a string')
271 })
272 expect(worker.hasTaskFunction('')).toStrictEqual({
273 status: false,
274 error: new TypeError('name parameter is an empty string')
275 })
276 expect(worker.hasTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
277 status: true
278 })
279 expect(worker.hasTaskFunction('fn1')).toStrictEqual({ status: true })
280 expect(worker.hasTaskFunction('fn2')).toStrictEqual({ status: true })
281 expect(worker.hasTaskFunction('fn3')).toStrictEqual({ status: false })
282 })
283
284 it('Verify that addTaskFunction() is working', () => {
285 const fn1 = () => {
286 return 1
287 }
288 const fn2 = () => {
289 return 2
290 }
291 const fn1Replacement = () => {
292 return 3
293 }
294 const worker = new ThreadWorker(fn1)
295 expect(worker.addTaskFunction(0, fn1)).toStrictEqual({
296 status: false,
297 error: new TypeError('name parameter is not a string')
298 })
299 expect(worker.addTaskFunction('', fn1)).toStrictEqual({
300 status: false,
301 error: new TypeError('name parameter is an empty string')
302 })
303 expect(worker.addTaskFunction('fn3', '')).toStrictEqual({
304 status: false,
305 error: new TypeError(
306 "taskFunction object 'taskFunction' property 'undefined' is not a function"
307 )
308 })
309 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
310 taskFunction: expect.any(Function)
311 })
312 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
313 taskFunction: expect.any(Function)
314 })
315 expect(worker.taskFunctions.size).toBe(2)
316 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
317 worker.taskFunctions.get('fn1')
318 )
319 expect(worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toStrictEqual({
320 status: false,
321 error: new Error(
322 'Cannot add a task function with the default reserved name'
323 )
324 })
325 worker.addTaskFunction('fn2', fn2)
326 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
327 taskFunction: expect.any(Function)
328 })
329 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
330 taskFunction: expect.any(Function)
331 })
332 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
333 taskFunction: expect.any(Function)
334 })
335 expect(worker.taskFunctions.size).toBe(3)
336 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
337 worker.taskFunctions.get('fn1')
338 )
339 worker.addTaskFunction('fn1', fn1Replacement)
340 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
341 taskFunction: expect.any(Function)
342 })
343 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
344 taskFunction: expect.any(Function)
345 })
346 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
347 taskFunction: expect.any(Function)
348 })
349 expect(worker.taskFunctions.size).toBe(3)
350 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
351 worker.taskFunctions.get('fn1')
352 )
353 })
354
355 it('Verify that listTaskFunctionsProperties() is working', () => {
356 const fn1 = () => {
357 return 1
358 }
359 const fn2 = () => {
360 return 2
361 }
362 const worker = new ClusterWorker({ fn1, fn2 })
363 expect(worker.listTaskFunctionsProperties()).toStrictEqual([
364 { name: DEFAULT_TASK_NAME },
365 { name: 'fn1' },
366 { name: 'fn2' }
367 ])
368 })
369
370 it('Verify that setDefaultTaskFunction() is working', () => {
371 const fn1 = () => {
372 return 1
373 }
374 const fn2 = () => {
375 return 2
376 }
377 const worker = new ThreadWorker({ fn1, fn2 })
378 expect(worker.setDefaultTaskFunction(0, fn1)).toStrictEqual({
379 status: false,
380 error: new TypeError('name parameter is not a string')
381 })
382 expect(worker.setDefaultTaskFunction('', fn1)).toStrictEqual({
383 status: false,
384 error: new TypeError('name parameter is an empty string')
385 })
386 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({
387 taskFunction: expect.any(Function)
388 })
389 expect(worker.taskFunctions.get('fn1')).toStrictEqual({
390 taskFunction: expect.any(Function)
391 })
392 expect(worker.taskFunctions.get('fn2')).toStrictEqual({
393 taskFunction: expect.any(Function)
394 })
395 expect(worker.taskFunctions.size).toBe(3)
396 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
397 worker.taskFunctions.get('fn1')
398 )
399 expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
400 status: false,
401 error: new Error(
402 'Cannot set the default task function reserved name as the default task function'
403 )
404 })
405 expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({
406 status: false,
407 error: new Error(
408 'Cannot set the default task function to a non-existing task function'
409 )
410 })
411 worker.setDefaultTaskFunction('fn1')
412 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
413 worker.taskFunctions.get('fn1')
414 )
415 worker.setDefaultTaskFunction('fn2')
416 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
417 worker.taskFunctions.get('fn2')
418 )
419 })
420 })