fix: ensure task function ops sync worker choice strategies
[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 138 it('Verify that taskFunctions parameter with unique function is taken', () => {
915040cc
JB
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 })
2a69b8c5 146 expect(worker.taskFunctions.size).toBe(2)
6cd5248f 147 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
148 worker.taskFunctions.get('fn1')
149 )
150 })
151
6934964f 152 it('Verify that taskFunctions parameter with multiple task functions is checked', () => {
f34fdabe
JB
153 const fn1 = () => {
154 return 1
155 }
156 const fn2 = ''
948faff7 157 expect(() => new ThreadWorker({ '': fn1 })).toThrow(
6934964f
JB
158 new TypeError('A taskFunctions parameter object key is an empty string')
159 )
948faff7 160 expect(() => new ThreadWorker({ fn1, fn2 })).toThrow(
31847469
JB
161 new TypeError(
162 "taskFunction object 'taskFunction' property 'undefined' is not a function"
163 )
f34fdabe 164 )
d0bd5062
JB
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: '' } })
85bbc7ab 177 ).toThrow(new TypeError("Invalid property 'priority': ''"))
d0bd5062
JB
178 expect(
179 () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: -21 } })
85bbc7ab 180 ).toThrow(new TypeError("Property 'priority' must be between -20 and 19"))
d0bd5062
JB
181 expect(
182 () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: 20 } })
85bbc7ab 183 ).toThrow(new RangeError("Property 'priority' must be between -20 and 19"))
d0bd5062
JB
184 expect(
185 () =>
186 new ThreadWorker({
187 fn1: { taskFunction: fn1, strategy: 'invalidStrategy' }
188 })
189 ).toThrow(
190 new RangeError("Invalid worker choice strategy 'invalidStrategy'")
191 )
f34fdabe
JB
192 })
193
a86b6df1
JB
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 })
915040cc
JB
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 })
2a69b8c5 211 expect(worker.taskFunctions.size).toBe(3)
6cd5248f 212 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
213 worker.taskFunctions.get('fn1')
214 )
d4aeae5a
JB
215 })
216
d0bd5062
JB
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
07588f30 244 it('Verify that async kill handler is called when worker is killed', () => {
a074ffee 245 const killHandlerStub = stub().returns()
07588f30 246 const worker = new ClusterWorker(() => {}, {
1cc6e9ef 247 killHandler: async () => await Promise.resolve(killHandlerStub())
07588f30
JB
248 })
249 worker.isMain = false
250 worker.handleKillMessage()
251 expect(killHandlerStub.calledOnce).toBe(true)
252 })
df9aaf20 253
318d4156 254 it('Verify that getMainWorker() throw error if main worker is not set', () => {
7fc5cce6 255 expect(() =>
1f68cede 256 new StubWorkerWithMainWorker(() => {}).getMainWorker()
948faff7 257 ).toThrow('Main worker not set')
7fc5cce6 258 })
2a69b8c5 259
9eae3c69 260 it('Verify that hasTaskFunction() is working', () => {
2a69b8c5
JB
261 const fn1 = () => {
262 return 1
263 }
264 const fn2 = () => {
265 return 2
266 }
267 const worker = new ClusterWorker({ fn1, fn2 })
66979634
JB
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 })
2a69b8c5
JB
282 })
283
9eae3c69 284 it('Verify that addTaskFunction() is working', () => {
2a69b8c5
JB
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)
66979634
JB
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,
31847469
JB
305 error: new TypeError(
306 "taskFunction object 'taskFunction' property 'undefined' is not a function"
307 )
66979634 308 })
915040cc
JB
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 })
2a69b8c5 315 expect(worker.taskFunctions.size).toBe(2)
6cd5248f 316 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
317 worker.taskFunctions.get('fn1')
318 )
66979634
JB
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 })
2a69b8c5 325 worker.addTaskFunction('fn2', fn2)
915040cc
JB
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 })
2a69b8c5 335 expect(worker.taskFunctions.size).toBe(3)
6cd5248f 336 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
337 worker.taskFunctions.get('fn1')
338 )
339 worker.addTaskFunction('fn1', fn1Replacement)
915040cc
JB
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 })
2a69b8c5 349 expect(worker.taskFunctions.size).toBe(3)
6cd5248f 350 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
351 worker.taskFunctions.get('fn1')
352 )
353 })
354
9eae3c69 355 it('Verify that listTaskFunctionNames() is working', () => {
c50b93fb
JB
356 const fn1 = () => {
357 return 1
358 }
359 const fn2 = () => {
360 return 2
361 }
362 const worker = new ClusterWorker({ fn1, fn2 })
31847469
JB
363 expect(worker.listTaskFunctionsProperties()).toStrictEqual([
364 { name: DEFAULT_TASK_NAME },
365 { name: 'fn1' },
366 { name: 'fn2' }
6cd5248f 367 ])
c50b93fb
JB
368 })
369
9eae3c69 370 it('Verify that setDefaultTaskFunction() is working', () => {
2a69b8c5
JB
371 const fn1 = () => {
372 return 1
373 }
374 const fn2 = () => {
375 return 2
376 }
377 const worker = new ThreadWorker({ fn1, fn2 })
66979634
JB
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 })
915040cc
JB
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 })
2a69b8c5 395 expect(worker.taskFunctions.size).toBe(3)
6cd5248f 396 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
397 worker.taskFunctions.get('fn1')
398 )
66979634
JB
399 expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
400 status: false,
401 error: new Error(
2a69b8c5
JB
402 'Cannot set the default task function reserved name as the default task function'
403 )
66979634
JB
404 })
405 expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({
406 status: false,
407 error: new Error(
6934964f
JB
408 'Cannot set the default task function to a non-existing task function'
409 )
66979634 410 })
2a69b8c5 411 worker.setDefaultTaskFunction('fn1')
6cd5248f 412 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
413 worker.taskFunctions.get('fn1')
414 )
415 worker.setDefaultTaskFunction('fn2')
6cd5248f 416 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
417 worker.taskFunctions.get('fn2')
418 )
419 })
c510fea7 420})