575e065132b889ad4b96ba7f1440ab5db23b2759
[poolifier.git] / tests / worker / abstract-worker.test.mjs
1 import { expect } from 'expect'
2 import { restore, stub } from 'sinon'
3 import { ClusterWorker, KillBehaviors, ThreadWorker } from '../../lib/index.js'
4 import { DEFAULT_TASK_NAME, EMPTY_FUNCTION } from '../../lib/utils.js'
5
6 describe('Abstract worker test suite', () => {
7 class StubWorkerWithMainWorker extends ThreadWorker {
8 constructor (fn, opts) {
9 super(fn, opts)
10 delete this.mainWorker
11 }
12 }
13
14 afterEach(() => {
15 restore()
16 })
17
18 it('Verify worker options default values', () => {
19 const worker = new ThreadWorker(() => {})
20 expect(worker.opts).toStrictEqual({
21 killBehavior: KillBehaviors.SOFT,
22 maxInactiveTime: 60000,
23 killHandler: EMPTY_FUNCTION
24 })
25 })
26
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 })
64
65 it('Verify that worker options are set at worker creation', () => {
66 const killHandler = () => {
67 console.info('Worker received kill message')
68 }
69 const worker = new ClusterWorker(() => {}, {
70 killBehavior: KillBehaviors.HARD,
71 maxInactiveTime: 6000,
72 killHandler
73 })
74 expect(worker.opts).toStrictEqual({
75 killBehavior: KillBehaviors.HARD,
76 maxInactiveTime: 6000,
77 killHandler
78 })
79 })
80
81 it('Verify that taskFunctions parameter is mandatory', () => {
82 expect(() => new ClusterWorker()).toThrowError(
83 new Error('taskFunctions parameter is mandatory')
84 )
85 })
86
87 it('Verify that taskFunctions parameter is a function or a plain object', () => {
88 expect(() => new ClusterWorker(0)).toThrowError(
89 new TypeError(
90 'taskFunctions parameter is not a function or a plain object'
91 )
92 )
93 expect(() => new ClusterWorker('')).toThrowError(
94 new TypeError(
95 'taskFunctions parameter is not a function or a plain object'
96 )
97 )
98 expect(() => new ClusterWorker(true)).toThrowError(
99 new TypeError(
100 'taskFunctions parameter is not a function or a plain object'
101 )
102 )
103 expect(() => new ClusterWorker([])).toThrowError(
104 new TypeError(
105 'taskFunctions parameter is not a function or a plain object'
106 )
107 )
108 expect(() => new ClusterWorker(new Map())).toThrowError(
109 new TypeError(
110 'taskFunctions parameter is not a function or a plain object'
111 )
112 )
113 expect(() => new ClusterWorker(new Set())).toThrowError(
114 new TypeError(
115 'taskFunctions parameter is not a function or a plain object'
116 )
117 )
118 expect(() => new ClusterWorker(new WeakMap())).toThrowError(
119 new TypeError(
120 'taskFunctions parameter is not a function or a plain object'
121 )
122 )
123 expect(() => new ClusterWorker(new WeakSet())).toThrowError(
124 new TypeError(
125 'taskFunctions parameter is not a function or a plain object'
126 )
127 )
128 })
129
130 it('Verify that taskFunctions parameter is not an empty object', () => {
131 expect(() => new ClusterWorker({})).toThrowError(
132 new Error('taskFunctions parameter object is empty')
133 )
134 })
135
136 it('Verify that taskFunctions parameter with unique function is taken', () => {
137 const worker = new ThreadWorker(() => {})
138 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
139 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
140 expect(worker.taskFunctions.size).toBe(2)
141 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
142 worker.taskFunctions.get('fn1')
143 )
144 })
145
146 it('Verify that taskFunctions parameter with multiple task functions is checked', () => {
147 const fn1 = () => {
148 return 1
149 }
150 const fn2 = ''
151 expect(() => new ThreadWorker({ '': fn1 })).toThrowError(
152 new TypeError('A taskFunctions parameter object key is an empty string')
153 )
154 expect(() => new ThreadWorker({ fn1, fn2 })).toThrowError(
155 new TypeError('A taskFunctions parameter object value is not a function')
156 )
157 })
158
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 })
167 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
168 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
169 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
170 expect(worker.taskFunctions.size).toBe(3)
171 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
172 worker.taskFunctions.get('fn1')
173 )
174 })
175
176 it('Verify that async kill handler is called when worker is killed', () => {
177 const killHandlerStub = stub().returns()
178 const worker = new ClusterWorker(() => {}, {
179 killHandler: async () => await Promise.resolve(killHandlerStub())
180 })
181 worker.isMain = false
182 worker.handleKillMessage()
183 expect(killHandlerStub.calledOnce).toBe(true)
184 })
185
186 it('Verify that getMainWorker() throw error if main worker is not set', () => {
187 expect(() =>
188 new StubWorkerWithMainWorker(() => {}).getMainWorker()
189 ).toThrowError('Main worker not set')
190 })
191
192 it('Verify that hasTaskFunction() is working', () => {
193 const fn1 = () => {
194 return 1
195 }
196 const fn2 = () => {
197 return 2
198 }
199 const worker = new ClusterWorker({ fn1, fn2 })
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 })
214 })
215
216 it('Verify that addTaskFunction() is working', () => {
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)
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 })
239 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
240 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
241 expect(worker.taskFunctions.size).toBe(2)
242 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
243 worker.taskFunctions.get('fn1')
244 )
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 })
251 worker.addTaskFunction('fn2', fn2)
252 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
253 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
254 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
255 expect(worker.taskFunctions.size).toBe(3)
256 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
257 worker.taskFunctions.get('fn1')
258 )
259 worker.addTaskFunction('fn1', fn1Replacement)
260 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
261 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
262 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
263 expect(worker.taskFunctions.size).toBe(3)
264 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
265 worker.taskFunctions.get('fn1')
266 )
267 })
268
269 it('Verify that listTaskFunctionNames() is working', () => {
270 const fn1 = () => {
271 return 1
272 }
273 const fn2 = () => {
274 return 2
275 }
276 const worker = new ClusterWorker({ fn1, fn2 })
277 expect(worker.listTaskFunctionNames()).toStrictEqual([
278 DEFAULT_TASK_NAME,
279 'fn1',
280 'fn2'
281 ])
282 })
283
284 it('Verify that setDefaultTaskFunction() is working', () => {
285 const fn1 = () => {
286 return 1
287 }
288 const fn2 = () => {
289 return 2
290 }
291 const worker = new ThreadWorker({ fn1, fn2 })
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 })
300 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
301 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
302 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
303 expect(worker.taskFunctions.size).toBe(3)
304 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
305 worker.taskFunctions.get('fn1')
306 )
307 expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
308 status: false,
309 error: new Error(
310 'Cannot set the default task function reserved name as the default task function'
311 )
312 })
313 expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({
314 status: false,
315 error: new Error(
316 'Cannot set the default task function to a non-existing task function'
317 )
318 })
319 worker.setDefaultTaskFunction('fn1')
320 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
321 worker.taskFunctions.get('fn1')
322 )
323 worker.setDefaultTaskFunction('fn2')
324 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
325 worker.taskFunctions.get('fn2')
326 )
327 })
328 })