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