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