build(deps-dev): bump @rollup/plugin-typescript
[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 )
c20084b6
JB
63 })
64
8620fb25 65 it('Verify that worker options are set at worker creation', () => {
df9aaf20
JB
66 const killHandler = () => {
67 console.info('Worker received kill message')
68 }
8620fb25 69 const worker = new ClusterWorker(() => {}, {
df9aaf20 70 killBehavior: KillBehaviors.HARD,
cca3bb1a 71 maxInactiveTime: 6000,
c20084b6 72 killHandler
8620fb25 73 })
cca3bb1a
JB
74 expect(worker.opts).toStrictEqual({
75 killBehavior: KillBehaviors.HARD,
76 maxInactiveTime: 6000,
77 killHandler
78 })
8620fb25
JB
79 })
80
a86b6df1
JB
81 it('Verify that taskFunctions parameter is mandatory', () => {
82 expect(() => new ClusterWorker()).toThrowError(
c20084b6 83 new Error('taskFunctions parameter is mandatory')
a86b6df1 84 )
d4aeae5a
JB
85 })
86
f34fdabe 87 it('Verify that taskFunctions parameter is a function or a plain object', () => {
a86b6df1 88 expect(() => new ClusterWorker(0)).toThrowError(
f34fdabe
JB
89 new TypeError(
90 'taskFunctions parameter is not a function or a plain object'
91 )
d4aeae5a
JB
92 )
93 expect(() => new ClusterWorker('')).toThrowError(
f34fdabe
JB
94 new TypeError(
95 'taskFunctions parameter is not a function or a plain object'
96 )
a86b6df1
JB
97 )
98 expect(() => new ClusterWorker(true)).toThrowError(
f34fdabe
JB
99 new TypeError(
100 'taskFunctions parameter is not a function or a plain object'
101 )
d4aeae5a 102 )
a86b6df1 103 expect(() => new ClusterWorker([])).toThrowError(
f34fdabe
JB
104 new TypeError(
105 'taskFunctions parameter is not a function or a plain object'
106 )
a86b6df1
JB
107 )
108 expect(() => new ClusterWorker(new Map())).toThrowError(
f34fdabe
JB
109 new TypeError(
110 'taskFunctions parameter is not a function or a plain object'
111 )
a86b6df1
JB
112 )
113 expect(() => new ClusterWorker(new Set())).toThrowError(
f34fdabe
JB
114 new TypeError(
115 'taskFunctions parameter is not a function or a plain object'
116 )
a86b6df1
JB
117 )
118 expect(() => new ClusterWorker(new WeakMap())).toThrowError(
f34fdabe
JB
119 new TypeError(
120 'taskFunctions parameter is not a function or a plain object'
121 )
d4aeae5a 122 )
a86b6df1 123 expect(() => new ClusterWorker(new WeakSet())).toThrowError(
f34fdabe
JB
124 new TypeError(
125 'taskFunctions parameter is not a function or a plain object'
126 )
a86b6df1 127 )
f34fdabe
JB
128 })
129
130 it('Verify that taskFunctions parameter is not an empty object', () => {
630f0acf 131 expect(() => new ClusterWorker({})).toThrowError(
0d80593b 132 new Error('taskFunctions parameter object is empty')
630f0acf 133 )
a86b6df1
JB
134 })
135
2a69b8c5
JB
136 it('Verify that taskFunctions parameter with unique function is taken', () => {
137 const worker = new ThreadWorker(() => {})
6cd5248f 138 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
2a69b8c5
JB
139 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
140 expect(worker.taskFunctions.size).toBe(2)
6cd5248f 141 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
142 worker.taskFunctions.get('fn1')
143 )
144 })
145
6934964f 146 it('Verify that taskFunctions parameter with multiple task functions is checked', () => {
f34fdabe
JB
147 const fn1 = () => {
148 return 1
149 }
150 const fn2 = ''
6934964f
JB
151 expect(() => new ThreadWorker({ '': fn1 })).toThrowError(
152 new TypeError('A taskFunctions parameter object key is an empty string')
153 )
f34fdabe
JB
154 expect(() => new ThreadWorker({ fn1, fn2 })).toThrowError(
155 new TypeError('A taskFunctions parameter object value is not a function')
156 )
157 })
158
a86b6df1
JB
159 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
160 const fn1 = () => {
161 return 1
162 }
163 const fn2 = () => {
164 return 2
165 }
166 const worker = new ClusterWorker({ fn1, fn2 })
6cd5248f 167 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
2a69b8c5
JB
168 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
169 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
170 expect(worker.taskFunctions.size).toBe(3)
6cd5248f 171 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
172 worker.taskFunctions.get('fn1')
173 )
d4aeae5a
JB
174 })
175
07588f30 176 it('Verify that async kill handler is called when worker is killed', () => {
a074ffee 177 const killHandlerStub = stub().returns()
07588f30 178 const worker = new ClusterWorker(() => {}, {
1cc6e9ef 179 killHandler: async () => await Promise.resolve(killHandlerStub())
07588f30
JB
180 })
181 worker.isMain = false
182 worker.handleKillMessage()
183 expect(killHandlerStub.calledOnce).toBe(true)
184 })
df9aaf20 185
318d4156 186 it('Verify that getMainWorker() throw error if main worker is not set', () => {
7fc5cce6 187 expect(() =>
1f68cede 188 new StubWorkerWithMainWorker(() => {}).getMainWorker()
e102732c 189 ).toThrowError('Main worker not set')
7fc5cce6 190 })
2a69b8c5 191
9eae3c69 192 it('Verify that hasTaskFunction() is working', () => {
2a69b8c5
JB
193 const fn1 = () => {
194 return 1
195 }
196 const fn2 = () => {
197 return 2
198 }
199 const worker = new ClusterWorker({ fn1, fn2 })
66979634
JB
200 expect(worker.hasTaskFunction(0)).toStrictEqual({
201 status: false,
202 error: new TypeError('name parameter is not a string')
203 })
204 expect(worker.hasTaskFunction('')).toStrictEqual({
205 status: false,
206 error: new TypeError('name parameter is an empty string')
207 })
208 expect(worker.hasTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
209 status: true
210 })
211 expect(worker.hasTaskFunction('fn1')).toStrictEqual({ status: true })
212 expect(worker.hasTaskFunction('fn2')).toStrictEqual({ status: true })
213 expect(worker.hasTaskFunction('fn3')).toStrictEqual({ status: false })
2a69b8c5
JB
214 })
215
9eae3c69 216 it('Verify that addTaskFunction() is working', () => {
2a69b8c5
JB
217 const fn1 = () => {
218 return 1
219 }
220 const fn2 = () => {
221 return 2
222 }
223 const fn1Replacement = () => {
224 return 3
225 }
226 const worker = new ThreadWorker(fn1)
66979634
JB
227 expect(worker.addTaskFunction(0, fn1)).toStrictEqual({
228 status: false,
229 error: new TypeError('name parameter is not a string')
230 })
231 expect(worker.addTaskFunction('', fn1)).toStrictEqual({
232 status: false,
233 error: new TypeError('name parameter is an empty string')
234 })
235 expect(worker.addTaskFunction('fn3', '')).toStrictEqual({
236 status: false,
237 error: new TypeError('fn parameter is not a function')
238 })
6cd5248f 239 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
2a69b8c5
JB
240 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
241 expect(worker.taskFunctions.size).toBe(2)
6cd5248f 242 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
243 worker.taskFunctions.get('fn1')
244 )
66979634
JB
245 expect(worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toStrictEqual({
246 status: false,
247 error: new Error(
248 'Cannot add a task function with the default reserved name'
249 )
250 })
2a69b8c5 251 worker.addTaskFunction('fn2', fn2)
6cd5248f 252 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
2a69b8c5
JB
253 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
254 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
255 expect(worker.taskFunctions.size).toBe(3)
6cd5248f 256 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
257 worker.taskFunctions.get('fn1')
258 )
259 worker.addTaskFunction('fn1', fn1Replacement)
6cd5248f 260 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
2a69b8c5
JB
261 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
262 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
263 expect(worker.taskFunctions.size).toBe(3)
6cd5248f 264 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
265 worker.taskFunctions.get('fn1')
266 )
267 })
268
9eae3c69 269 it('Verify that listTaskFunctionNames() is working', () => {
c50b93fb
JB
270 const fn1 = () => {
271 return 1
272 }
273 const fn2 = () => {
274 return 2
275 }
276 const worker = new ClusterWorker({ fn1, fn2 })
66979634 277 expect(worker.listTaskFunctionNames()).toStrictEqual([
6cd5248f
JB
278 DEFAULT_TASK_NAME,
279 'fn1',
280 'fn2'
281 ])
c50b93fb
JB
282 })
283
9eae3c69 284 it('Verify that setDefaultTaskFunction() is working', () => {
2a69b8c5
JB
285 const fn1 = () => {
286 return 1
287 }
288 const fn2 = () => {
289 return 2
290 }
291 const worker = new ThreadWorker({ fn1, fn2 })
66979634
JB
292 expect(worker.setDefaultTaskFunction(0, fn1)).toStrictEqual({
293 status: false,
294 error: new TypeError('name parameter is not a string')
295 })
296 expect(worker.setDefaultTaskFunction('', fn1)).toStrictEqual({
297 status: false,
298 error: new TypeError('name parameter is an empty string')
299 })
6cd5248f 300 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
2a69b8c5
JB
301 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
302 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
303 expect(worker.taskFunctions.size).toBe(3)
6cd5248f 304 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
305 worker.taskFunctions.get('fn1')
306 )
66979634
JB
307 expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
308 status: false,
309 error: new Error(
2a69b8c5
JB
310 'Cannot set the default task function reserved name as the default task function'
311 )
66979634
JB
312 })
313 expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({
314 status: false,
315 error: new Error(
6934964f
JB
316 'Cannot set the default task function to a non-existing task function'
317 )
66979634 318 })
2a69b8c5 319 worker.setDefaultTaskFunction('fn1')
6cd5248f 320 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
321 worker.taskFunctions.get('fn1')
322 )
323 worker.setDefaultTaskFunction('fn2')
6cd5248f 324 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
2a69b8c5
JB
325 worker.taskFunctions.get('fn2')
326 )
327 })
c510fea7 328})