refactor: cleanup eslint configuration
[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(Function)
136 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
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('A taskFunctions parameter object value is not a function')
153 )
154 })
155
156 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
157 const fn1 = () => {
158 return 1
159 }
160 const fn2 = () => {
161 return 2
162 }
163 const worker = new ClusterWorker({ fn1, fn2 })
164 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
165 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
166 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
167 expect(worker.taskFunctions.size).toBe(3)
168 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
169 worker.taskFunctions.get('fn1')
170 )
171 })
172
173 it('Verify that async kill handler is called when worker is killed', () => {
174 const killHandlerStub = stub().returns()
175 const worker = new ClusterWorker(() => {}, {
176 killHandler: async () => await Promise.resolve(killHandlerStub())
177 })
178 worker.isMain = false
179 worker.handleKillMessage()
180 expect(killHandlerStub.calledOnce).toBe(true)
181 })
182
183 it('Verify that getMainWorker() throw error if main worker is not set', () => {
184 expect(() =>
185 new StubWorkerWithMainWorker(() => {}).getMainWorker()
186 ).toThrow('Main worker not set')
187 })
188
189 it('Verify that hasTaskFunction() is working', () => {
190 const fn1 = () => {
191 return 1
192 }
193 const fn2 = () => {
194 return 2
195 }
196 const worker = new ClusterWorker({ fn1, fn2 })
197 expect(worker.hasTaskFunction(0)).toStrictEqual({
198 status: false,
199 error: new TypeError('name parameter is not a string')
200 })
201 expect(worker.hasTaskFunction('')).toStrictEqual({
202 status: false,
203 error: new TypeError('name parameter is an empty string')
204 })
205 expect(worker.hasTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
206 status: true
207 })
208 expect(worker.hasTaskFunction('fn1')).toStrictEqual({ status: true })
209 expect(worker.hasTaskFunction('fn2')).toStrictEqual({ status: true })
210 expect(worker.hasTaskFunction('fn3')).toStrictEqual({ status: false })
211 })
212
213 it('Verify that addTaskFunction() is working', () => {
214 const fn1 = () => {
215 return 1
216 }
217 const fn2 = () => {
218 return 2
219 }
220 const fn1Replacement = () => {
221 return 3
222 }
223 const worker = new ThreadWorker(fn1)
224 expect(worker.addTaskFunction(0, fn1)).toStrictEqual({
225 status: false,
226 error: new TypeError('name parameter is not a string')
227 })
228 expect(worker.addTaskFunction('', fn1)).toStrictEqual({
229 status: false,
230 error: new TypeError('name parameter is an empty string')
231 })
232 expect(worker.addTaskFunction('fn3', '')).toStrictEqual({
233 status: false,
234 error: new TypeError('fn parameter is not a function')
235 })
236 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
237 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
238 expect(worker.taskFunctions.size).toBe(2)
239 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
240 worker.taskFunctions.get('fn1')
241 )
242 expect(worker.addTaskFunction(DEFAULT_TASK_NAME, fn2)).toStrictEqual({
243 status: false,
244 error: new Error(
245 'Cannot add a task function with the default reserved name'
246 )
247 })
248 worker.addTaskFunction('fn2', fn2)
249 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
250 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
251 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
252 expect(worker.taskFunctions.size).toBe(3)
253 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
254 worker.taskFunctions.get('fn1')
255 )
256 worker.addTaskFunction('fn1', fn1Replacement)
257 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
258 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
259 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
260 expect(worker.taskFunctions.size).toBe(3)
261 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
262 worker.taskFunctions.get('fn1')
263 )
264 })
265
266 it('Verify that listTaskFunctionNames() is working', () => {
267 const fn1 = () => {
268 return 1
269 }
270 const fn2 = () => {
271 return 2
272 }
273 const worker = new ClusterWorker({ fn1, fn2 })
274 expect(worker.listTaskFunctionNames()).toStrictEqual([
275 DEFAULT_TASK_NAME,
276 'fn1',
277 'fn2'
278 ])
279 })
280
281 it('Verify that setDefaultTaskFunction() is working', () => {
282 const fn1 = () => {
283 return 1
284 }
285 const fn2 = () => {
286 return 2
287 }
288 const worker = new ThreadWorker({ fn1, fn2 })
289 expect(worker.setDefaultTaskFunction(0, fn1)).toStrictEqual({
290 status: false,
291 error: new TypeError('name parameter is not a string')
292 })
293 expect(worker.setDefaultTaskFunction('', fn1)).toStrictEqual({
294 status: false,
295 error: new TypeError('name parameter is an empty string')
296 })
297 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
298 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
299 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
300 expect(worker.taskFunctions.size).toBe(3)
301 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
302 worker.taskFunctions.get('fn1')
303 )
304 expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({
305 status: false,
306 error: new Error(
307 'Cannot set the default task function reserved name as the default task function'
308 )
309 })
310 expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({
311 status: false,
312 error: new Error(
313 'Cannot set the default task function to a non-existing task function'
314 )
315 })
316 worker.setDefaultTaskFunction('fn1')
317 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
318 worker.taskFunctions.get('fn1')
319 )
320 worker.setDefaultTaskFunction('fn2')
321 expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
322 worker.taskFunctions.get('fn2')
323 )
324 })
325 })