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