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