Commit | Line | Data |
---|---|---|
a074ffee JB |
1 | import { expect } from 'expect' |
2 | import { restore, stub } from 'sinon' | |
ded253e2 | 3 | |
d35e5717 JB |
4 | import { ClusterWorker, KillBehaviors, ThreadWorker } from '../../lib/index.cjs' |
5 | import { DEFAULT_TASK_NAME, EMPTY_FUNCTION } from '../../lib/utils.cjs' | |
7fc5cce6 | 6 | |
e1ffb94f | 7 | describe('Abstract worker test suite', () => { |
1f68cede | 8 | class StubWorkerWithMainWorker extends ThreadWorker { |
e1ffb94f JB |
9 | constructor (fn, opts) { |
10 | super(fn, opts) | |
41072404 | 11 | delete this.mainWorker |
e1ffb94f | 12 | } |
7fc5cce6 | 13 | } |
c510fea7 | 14 | |
dc021bcc | 15 | afterEach(() => { |
a074ffee | 16 | restore() |
dc021bcc JB |
17 | }) |
18 | ||
e088a00c | 19 | it('Verify worker options default values', () => { |
8620fb25 | 20 | const worker = new ThreadWorker(() => {}) |
cca3bb1a JB |
21 | expect(worker.opts).toStrictEqual({ |
22 | killBehavior: KillBehaviors.SOFT, | |
23 | maxInactiveTime: 60000, | |
24 | killHandler: EMPTY_FUNCTION | |
25 | }) | |
8620fb25 JB |
26 | }) |
27 | ||
c20084b6 | 28 | it('Verify that worker options are checked at worker creation', () => { |
948faff7 | 29 | expect(() => new ClusterWorker(() => {}, '')).toThrow( |
c20084b6 JB |
30 | new TypeError('opts worker options parameter is not a plain object') |
31 | ) | |
948faff7 JB |
32 | expect(() => new ClusterWorker(() => {}, { killBehavior: '' })).toThrow( |
33 | new TypeError("killBehavior option '' is not valid") | |
34 | ) | |
35 | expect(() => new ClusterWorker(() => {}, { killBehavior: 0 })).toThrow( | |
c20084b6 JB |
36 | new TypeError("killBehavior option '0' is not valid") |
37 | ) | |
948faff7 JB |
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( | |
c20084b6 JB |
45 | new TypeError( |
46 | 'maxInactiveTime option is not a positive integer greater or equal than 5' | |
47 | ) | |
48 | ) | |
948faff7 | 49 | expect(() => new ThreadWorker(() => {}, { maxInactiveTime: 4 })).toThrow( |
c20084b6 JB |
50 | new TypeError( |
51 | 'maxInactiveTime option is not a positive integer greater or equal than 5' | |
52 | ) | |
53 | ) | |
948faff7 | 54 | expect(() => new ThreadWorker(() => {}, { killHandler: '' })).toThrow( |
c20084b6 JB |
55 | new TypeError('killHandler option is not a function') |
56 | ) | |
948faff7 | 57 | expect(() => new ThreadWorker(() => {}, { killHandler: 0 })).toThrow( |
c20084b6 JB |
58 | new TypeError('killHandler option is not a function') |
59 | ) | |
c20084b6 JB |
60 | }) |
61 | ||
8620fb25 | 62 | it('Verify that worker options are set at worker creation', () => { |
df9aaf20 JB |
63 | const killHandler = () => { |
64 | console.info('Worker received kill message') | |
65 | } | |
8620fb25 | 66 | const worker = new ClusterWorker(() => {}, { |
df9aaf20 | 67 | killBehavior: KillBehaviors.HARD, |
cca3bb1a | 68 | maxInactiveTime: 6000, |
c20084b6 | 69 | killHandler |
8620fb25 | 70 | }) |
cca3bb1a JB |
71 | expect(worker.opts).toStrictEqual({ |
72 | killBehavior: KillBehaviors.HARD, | |
73 | maxInactiveTime: 6000, | |
74 | killHandler | |
75 | }) | |
8620fb25 JB |
76 | }) |
77 | ||
a86b6df1 | 78 | it('Verify that taskFunctions parameter is mandatory', () => { |
948faff7 | 79 | expect(() => new ClusterWorker()).toThrow( |
c20084b6 | 80 | new Error('taskFunctions parameter is mandatory') |
a86b6df1 | 81 | ) |
d4aeae5a JB |
82 | }) |
83 | ||
f34fdabe | 84 | it('Verify that taskFunctions parameter is a function or a plain object', () => { |
948faff7 | 85 | expect(() => new ClusterWorker(0)).toThrow( |
f34fdabe JB |
86 | new TypeError( |
87 | 'taskFunctions parameter is not a function or a plain object' | |
88 | ) | |
d4aeae5a | 89 | ) |
948faff7 | 90 | expect(() => new ClusterWorker('')).toThrow( |
f34fdabe JB |
91 | new TypeError( |
92 | 'taskFunctions parameter is not a function or a plain object' | |
93 | ) | |
a86b6df1 | 94 | ) |
948faff7 | 95 | expect(() => new ClusterWorker(true)).toThrow( |
f34fdabe JB |
96 | new TypeError( |
97 | 'taskFunctions parameter is not a function or a plain object' | |
98 | ) | |
d4aeae5a | 99 | ) |
948faff7 | 100 | expect(() => new ClusterWorker([])).toThrow( |
f34fdabe JB |
101 | new TypeError( |
102 | 'taskFunctions parameter is not a function or a plain object' | |
103 | ) | |
a86b6df1 | 104 | ) |
948faff7 | 105 | expect(() => new ClusterWorker(new Map())).toThrow( |
f34fdabe JB |
106 | new TypeError( |
107 | 'taskFunctions parameter is not a function or a plain object' | |
108 | ) | |
a86b6df1 | 109 | ) |
948faff7 | 110 | expect(() => new ClusterWorker(new Set())).toThrow( |
f34fdabe JB |
111 | new TypeError( |
112 | 'taskFunctions parameter is not a function or a plain object' | |
113 | ) | |
a86b6df1 | 114 | ) |
948faff7 | 115 | expect(() => new ClusterWorker(new WeakMap())).toThrow( |
f34fdabe JB |
116 | new TypeError( |
117 | 'taskFunctions parameter is not a function or a plain object' | |
118 | ) | |
d4aeae5a | 119 | ) |
948faff7 | 120 | expect(() => new ClusterWorker(new WeakSet())).toThrow( |
f34fdabe JB |
121 | new TypeError( |
122 | 'taskFunctions parameter is not a function or a plain object' | |
123 | ) | |
a86b6df1 | 124 | ) |
f34fdabe JB |
125 | }) |
126 | ||
127 | it('Verify that taskFunctions parameter is not an empty object', () => { | |
948faff7 | 128 | expect(() => new ClusterWorker({})).toThrow( |
0d80593b | 129 | new Error('taskFunctions parameter object is empty') |
630f0acf | 130 | ) |
a86b6df1 JB |
131 | }) |
132 | ||
2a69b8c5 JB |
133 | it('Verify that taskFunctions parameter with unique function is taken', () => { |
134 | const worker = new ThreadWorker(() => {}) | |
6cd5248f | 135 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
2a69b8c5 JB |
136 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
137 | expect(worker.taskFunctions.size).toBe(2) | |
6cd5248f | 138 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
139 | worker.taskFunctions.get('fn1') |
140 | ) | |
141 | }) | |
142 | ||
6934964f | 143 | it('Verify that taskFunctions parameter with multiple task functions is checked', () => { |
f34fdabe JB |
144 | const fn1 = () => { |
145 | return 1 | |
146 | } | |
147 | const fn2 = '' | |
948faff7 | 148 | expect(() => new ThreadWorker({ '': fn1 })).toThrow( |
6934964f JB |
149 | new TypeError('A taskFunctions parameter object key is an empty string') |
150 | ) | |
948faff7 | 151 | expect(() => new ThreadWorker({ fn1, fn2 })).toThrow( |
f34fdabe JB |
152 | new TypeError('A taskFunctions parameter object value is not a function') |
153 | ) | |
154 | }) | |
155 | ||
a86b6df1 JB |
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 }) | |
6cd5248f | 164 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
2a69b8c5 JB |
165 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
166 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) | |
167 | expect(worker.taskFunctions.size).toBe(3) | |
6cd5248f | 168 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
169 | worker.taskFunctions.get('fn1') |
170 | ) | |
d4aeae5a JB |
171 | }) |
172 | ||
07588f30 | 173 | it('Verify that async kill handler is called when worker is killed', () => { |
a074ffee | 174 | const killHandlerStub = stub().returns() |
07588f30 | 175 | const worker = new ClusterWorker(() => {}, { |
1cc6e9ef | 176 | killHandler: async () => await Promise.resolve(killHandlerStub()) |
07588f30 JB |
177 | }) |
178 | worker.isMain = false | |
179 | worker.handleKillMessage() | |
180 | expect(killHandlerStub.calledOnce).toBe(true) | |
181 | }) | |
df9aaf20 | 182 | |
318d4156 | 183 | it('Verify that getMainWorker() throw error if main worker is not set', () => { |
7fc5cce6 | 184 | expect(() => |
1f68cede | 185 | new StubWorkerWithMainWorker(() => {}).getMainWorker() |
948faff7 | 186 | ).toThrow('Main worker not set') |
7fc5cce6 | 187 | }) |
2a69b8c5 | 188 | |
9eae3c69 | 189 | it('Verify that hasTaskFunction() is working', () => { |
2a69b8c5 JB |
190 | const fn1 = () => { |
191 | return 1 | |
192 | } | |
193 | const fn2 = () => { | |
194 | return 2 | |
195 | } | |
196 | const worker = new ClusterWorker({ fn1, fn2 }) | |
66979634 JB |
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 }) | |
2a69b8c5 JB |
211 | }) |
212 | ||
9eae3c69 | 213 | it('Verify that addTaskFunction() is working', () => { |
2a69b8c5 JB |
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) | |
66979634 JB |
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 | }) | |
6cd5248f | 236 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
2a69b8c5 JB |
237 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
238 | expect(worker.taskFunctions.size).toBe(2) | |
6cd5248f | 239 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
240 | worker.taskFunctions.get('fn1') |
241 | ) | |
66979634 JB |
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 | }) | |
2a69b8c5 | 248 | worker.addTaskFunction('fn2', fn2) |
6cd5248f | 249 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
2a69b8c5 JB |
250 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
251 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) | |
252 | expect(worker.taskFunctions.size).toBe(3) | |
6cd5248f | 253 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
254 | worker.taskFunctions.get('fn1') |
255 | ) | |
256 | worker.addTaskFunction('fn1', fn1Replacement) | |
6cd5248f | 257 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
2a69b8c5 JB |
258 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
259 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) | |
260 | expect(worker.taskFunctions.size).toBe(3) | |
6cd5248f | 261 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
262 | worker.taskFunctions.get('fn1') |
263 | ) | |
264 | }) | |
265 | ||
9eae3c69 | 266 | it('Verify that listTaskFunctionNames() is working', () => { |
c50b93fb JB |
267 | const fn1 = () => { |
268 | return 1 | |
269 | } | |
270 | const fn2 = () => { | |
271 | return 2 | |
272 | } | |
273 | const worker = new ClusterWorker({ fn1, fn2 }) | |
66979634 | 274 | expect(worker.listTaskFunctionNames()).toStrictEqual([ |
6cd5248f JB |
275 | DEFAULT_TASK_NAME, |
276 | 'fn1', | |
277 | 'fn2' | |
278 | ]) | |
c50b93fb JB |
279 | }) |
280 | ||
9eae3c69 | 281 | it('Verify that setDefaultTaskFunction() is working', () => { |
2a69b8c5 JB |
282 | const fn1 = () => { |
283 | return 1 | |
284 | } | |
285 | const fn2 = () => { | |
286 | return 2 | |
287 | } | |
288 | const worker = new ThreadWorker({ fn1, fn2 }) | |
66979634 JB |
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 | }) | |
6cd5248f | 297 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function) |
2a69b8c5 JB |
298 | expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function) |
299 | expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function) | |
300 | expect(worker.taskFunctions.size).toBe(3) | |
6cd5248f | 301 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
302 | worker.taskFunctions.get('fn1') |
303 | ) | |
66979634 JB |
304 | expect(worker.setDefaultTaskFunction(DEFAULT_TASK_NAME)).toStrictEqual({ |
305 | status: false, | |
306 | error: new Error( | |
2a69b8c5 JB |
307 | 'Cannot set the default task function reserved name as the default task function' |
308 | ) | |
66979634 JB |
309 | }) |
310 | expect(worker.setDefaultTaskFunction('fn3')).toStrictEqual({ | |
311 | status: false, | |
312 | error: new Error( | |
6934964f JB |
313 | 'Cannot set the default task function to a non-existing task function' |
314 | ) | |
66979634 | 315 | }) |
2a69b8c5 | 316 | worker.setDefaultTaskFunction('fn1') |
6cd5248f | 317 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
318 | worker.taskFunctions.get('fn1') |
319 | ) | |
320 | worker.setDefaultTaskFunction('fn2') | |
6cd5248f | 321 | expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual( |
2a69b8c5 JB |
322 | worker.taskFunctions.get('fn2') |
323 | ) | |
324 | }) | |
c510fea7 | 325 | }) |