refactor: cleanup cluster ESM issue workaround
[poolifier.git] / tests / worker / abstract-worker.test.js
CommitLineData
a61a0724 1const { expect } = require('expect')
8620fb25 2const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib')
7fc5cce6 3
e1ffb94f 4describe('Abstract worker test suite', () => {
1f68cede 5 class StubWorkerWithMainWorker extends ThreadWorker {
e1ffb94f
JB
6 constructor (fn, opts) {
7 super(fn, opts)
78cea37e 8 this.mainWorker = undefined
e1ffb94f 9 }
7fc5cce6 10 }
c510fea7 11
e088a00c 12 it('Verify worker options default values', () => {
8620fb25 13 const worker = new ThreadWorker(() => {})
978aad6f 14 expect(worker.opts.maxInactiveTime).toStrictEqual(60000)
e088a00c 15 expect(worker.opts.killBehavior).toBe(KillBehaviors.SOFT)
571227f4 16 expect(worker.opts.async).toBe(undefined)
8620fb25
JB
17 })
18
19 it('Verify that worker options are set at worker creation', () => {
20 const worker = new ClusterWorker(() => {}, {
21 maxInactiveTime: 6000,
22 async: true,
23 killBehavior: KillBehaviors.HARD
24 })
978aad6f 25 expect(worker.opts.maxInactiveTime).toStrictEqual(6000)
e088a00c 26 expect(worker.opts.killBehavior).toBe(KillBehaviors.HARD)
571227f4 27 expect(worker.opts.async).toBe(undefined)
8620fb25
JB
28 })
29
a86b6df1
JB
30 it('Verify that taskFunctions parameter is mandatory', () => {
31 expect(() => new ClusterWorker()).toThrowError(
32 'taskFunctions parameter is mandatory'
33 )
d4aeae5a
JB
34 })
35
f34fdabe 36 it('Verify that taskFunctions parameter is a function or a plain object', () => {
a86b6df1 37 expect(() => new ClusterWorker(0)).toThrowError(
f34fdabe
JB
38 new TypeError(
39 'taskFunctions parameter is not a function or a plain object'
40 )
d4aeae5a
JB
41 )
42 expect(() => new ClusterWorker('')).toThrowError(
f34fdabe
JB
43 new TypeError(
44 'taskFunctions parameter is not a function or a plain object'
45 )
a86b6df1
JB
46 )
47 expect(() => new ClusterWorker(true)).toThrowError(
f34fdabe
JB
48 new TypeError(
49 'taskFunctions parameter is not a function or a plain object'
50 )
d4aeae5a 51 )
a86b6df1 52 expect(() => new ClusterWorker([])).toThrowError(
f34fdabe
JB
53 new TypeError(
54 'taskFunctions parameter is not a function or a plain object'
55 )
a86b6df1
JB
56 )
57 expect(() => new ClusterWorker(new Map())).toThrowError(
f34fdabe
JB
58 new TypeError(
59 'taskFunctions parameter is not a function or a plain object'
60 )
a86b6df1
JB
61 )
62 expect(() => new ClusterWorker(new Set())).toThrowError(
f34fdabe
JB
63 new TypeError(
64 'taskFunctions parameter is not a function or a plain object'
65 )
a86b6df1
JB
66 )
67 expect(() => new ClusterWorker(new WeakMap())).toThrowError(
f34fdabe
JB
68 new TypeError(
69 'taskFunctions parameter is not a function or a plain object'
70 )
d4aeae5a 71 )
a86b6df1 72 expect(() => new ClusterWorker(new WeakSet())).toThrowError(
f34fdabe
JB
73 new TypeError(
74 'taskFunctions parameter is not a function or a plain object'
75 )
a86b6df1 76 )
f34fdabe
JB
77 })
78
79 it('Verify that taskFunctions parameter is not an empty object', () => {
630f0acf 80 expect(() => new ClusterWorker({})).toThrowError(
0d80593b 81 new Error('taskFunctions parameter object is empty')
630f0acf 82 )
a86b6df1
JB
83 })
84
2a69b8c5
JB
85 it('Verify that taskFunctions parameter with unique function is taken', () => {
86 const worker = new ThreadWorker(() => {})
87 expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
88 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
89 expect(worker.taskFunctions.size).toBe(2)
90 expect(worker.taskFunctions.get('default')).toStrictEqual(
91 worker.taskFunctions.get('fn1')
92 )
93 })
94
f34fdabe
JB
95 it('Verify that taskFunctions parameter with multiple task functions contains function', () => {
96 const fn1 = () => {
97 return 1
98 }
99 const fn2 = ''
100 expect(() => new ThreadWorker({ fn1, fn2 })).toThrowError(
101 new TypeError('A taskFunctions parameter object value is not a function')
102 )
103 })
104
a86b6df1
JB
105 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
106 const fn1 = () => {
107 return 1
108 }
109 const fn2 = () => {
110 return 2
111 }
112 const worker = new ClusterWorker({ fn1, fn2 })
2a69b8c5
JB
113 expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
114 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
115 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
116 expect(worker.taskFunctions.size).toBe(3)
117 expect(worker.taskFunctions.get('default')).toStrictEqual(
118 worker.taskFunctions.get('fn1')
119 )
d4aeae5a
JB
120 })
121
2431bdb4
JB
122 it('Verify that handleError() method works properly', () => {
123 const error = new Error('Error as an error')
124 const worker = new ClusterWorker(() => {})
125 expect(worker.handleError(error)).not.toBeInstanceOf(Error)
126 expect(worker.handleError(error)).toStrictEqual(error.message)
127 const errorMessage = 'Error as a string'
128 expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage)
7fc5cce6
APA
129 })
130
318d4156 131 it('Verify that getMainWorker() throw error if main worker is not set', () => {
7fc5cce6 132 expect(() =>
1f68cede 133 new StubWorkerWithMainWorker(() => {}).getMainWorker()
e102732c 134 ).toThrowError('Main worker not set')
7fc5cce6 135 })
2a69b8c5
JB
136
137 it('Verify that hasTaskFunction() works', () => {
138 const fn1 = () => {
139 return 1
140 }
141 const fn2 = () => {
142 return 2
143 }
144 const worker = new ClusterWorker({ fn1, fn2 })
145 expect(worker.hasTaskFunction('default')).toBe(true)
146 expect(worker.hasTaskFunction('fn1')).toBe(true)
147 expect(worker.hasTaskFunction('fn2')).toBe(true)
148 expect(worker.hasTaskFunction('fn3')).toBe(false)
149 })
150
151 it('Verify that addTaskFunction() works', () => {
152 const fn1 = () => {
153 return 1
154 }
155 const fn2 = () => {
156 return 2
157 }
158 const fn1Replacement = () => {
159 return 3
160 }
161 const worker = new ThreadWorker(fn1)
162 expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
163 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
164 expect(worker.taskFunctions.size).toBe(2)
165 expect(worker.taskFunctions.get('default')).toStrictEqual(
166 worker.taskFunctions.get('fn1')
167 )
168 expect(() => worker.addTaskFunction('default', fn2)).toThrowError(
169 new Error('Cannot add a task function with the default reserved name')
170 )
171 worker.addTaskFunction('fn2', fn2)
172 expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
173 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
174 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
175 expect(worker.taskFunctions.size).toBe(3)
176 expect(worker.taskFunctions.get('default')).toStrictEqual(
177 worker.taskFunctions.get('fn1')
178 )
179 worker.addTaskFunction('fn1', fn1Replacement)
180 expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
181 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
182 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
183 expect(worker.taskFunctions.size).toBe(3)
184 expect(worker.taskFunctions.get('default')).toStrictEqual(
185 worker.taskFunctions.get('fn1')
186 )
187 })
188
189 it('Verify that removeTaskFunction() works', () => {
190 const fn1 = () => {
191 return 1
192 }
193 const fn2 = () => {
194 return 2
195 }
135f2a9f 196 const worker = new ClusterWorker({ fn1, fn2 })
2a69b8c5
JB
197 expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
198 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
199 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
200 expect(worker.taskFunctions.size).toBe(3)
201 expect(worker.taskFunctions.get('default')).toStrictEqual(
202 worker.taskFunctions.get('fn1')
203 )
204 expect(() => worker.removeTaskFunction('default')).toThrowError(
205 new Error(
206 'Cannot remove the task function with the default reserved name'
207 )
208 )
209 expect(() => worker.removeTaskFunction('fn1')).toThrowError(
210 new Error(
211 'Cannot remove the task function used as the default task function'
212 )
213 )
214 worker.removeTaskFunction('fn2')
215 expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
216 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
217 expect(worker.taskFunctions.get('fn2')).toBeUndefined()
218 expect(worker.taskFunctions.size).toBe(2)
219 })
220
c50b93fb
JB
221 it('Verify that listTaskFunctions() works', () => {
222 const fn1 = () => {
223 return 1
224 }
225 const fn2 = () => {
226 return 2
227 }
228 const worker = new ClusterWorker({ fn1, fn2 })
229 expect(worker.listTaskFunctions()).toStrictEqual(['default', 'fn1', 'fn2'])
230 })
231
2a69b8c5
JB
232 it('Verify that setDefaultTaskFunction() works', () => {
233 const fn1 = () => {
234 return 1
235 }
236 const fn2 = () => {
237 return 2
238 }
239 const worker = new ThreadWorker({ fn1, fn2 })
240 expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
241 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
242 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
243 expect(worker.taskFunctions.size).toBe(3)
244 expect(worker.taskFunctions.get('default')).toStrictEqual(
245 worker.taskFunctions.get('fn1')
246 )
247 expect(() => worker.setDefaultTaskFunction('default')).toThrowError(
248 new Error(
249 'Cannot set the default task function reserved name as the default task function'
250 )
251 )
252 worker.setDefaultTaskFunction('fn1')
253 expect(worker.taskFunctions.get('default')).toStrictEqual(
254 worker.taskFunctions.get('fn1')
255 )
256 worker.setDefaultTaskFunction('fn2')
257 expect(worker.taskFunctions.get('default')).toStrictEqual(
258 worker.taskFunctions.get('fn2')
259 )
260 })
c510fea7 261})