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