forked from billmalarky/react-native-queue
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathWorker.test.js
More file actions
360 lines (273 loc) · 9.7 KB
/
Copy pathWorker.test.js
File metadata and controls
360 lines (273 loc) · 9.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Define globals for eslint.
/* global describe it */
// Load dependencies
import should from 'should'; // eslint-disable-line no-unused-vars
import Worker from '../Models/Worker';
describe('Models/Worker', function() {
it('#addWorker() should validate input', async () => {
const worker = new Worker();
try {
worker.addWorker(null, async () => {});
throw new Error('worker.addWorker() should throw error if no jobname supplied.');
} catch (error) {
error.should.deepEqual(new Error('Job name and associated worker function must be supplied.'));
}
try {
worker.addWorker('test-job-one', null);
throw new Error('worker.addWorker() should throw error if no worker function supplied.');
} catch (error) {
error.should.deepEqual(new Error('Job name and associated worker function must be supplied.'));
}
});
it('#addWorker() should work as expected', async () => {
const worker = new Worker();
worker.addWorker('test-job-one', async () => {});
const workerOptions = {
concurrency: 3,
onStart: async () => {}
};
worker.addWorker('test-job-two', async () => {}, workerOptions);
// first worker is added with default options.
Worker.workers['test-job-one'].should.be.a.Function();
Worker.workers['test-job-one'].options.should.deepEqual({
concurrency: 1,
onStart: null,
onSuccess: null,
onFailure: null,
onFailed: null,
onComplete: null
});
// second worker is added with new concurrency option.
Worker.workers['test-job-two'].should.be.a.Function();
Worker.workers['test-job-two'].options.should.deepEqual({
concurrency: workerOptions.concurrency,
onStart: workerOptions.onStart,
onSuccess: null,
onFailure: null,
onFailed: null,
onComplete: null
});
});
it('#removeWorker() should work as expected', async () => {
const worker = new Worker();
// Add workers.
worker.addWorker('test-job-one', async () => {});
const workerOptions = {
concurrency: 3
};
worker.addWorker('test-job-two', async () => {}, workerOptions);
worker.addWorker('test-job-three', async () => {});
Object.keys(Worker.workers).should.deepEqual(['test-job-one', 'test-job-two', 'test-job-three']);
worker.removeWorker('test-job-two');
Object.keys(Worker.workers).should.deepEqual(['test-job-one', 'test-job-three']);
});
it('#getConcurrency() should throw error if no worker assigned to passed in job name.', async () => {
const worker = new Worker();
const jobName = 'no-worker-exists';
try {
worker.getConcurrency(jobName);
throw new Error('getConcurrency() should have thrown an error due to no worker assigned to that job name.');
} catch (error) {
error.should.deepEqual(new Error('Job ' + jobName + ' does not have a worker assigned to it.'));
}
});
it('#getConcurrency() should return worker job concurrency', async () => {
const worker = new Worker();
// Add worker.
const workerOptions = {
concurrency: 36
};
worker.addWorker('test-job-one', async () => {}, workerOptions);
worker.getConcurrency('test-job-one').should.equal(36);
});
it('#executeJob() should error if worker not assigned to job yet.', async () => {
const worker = new Worker();
const jobName = 'this-worker-does-not-exist';
try {
await worker.executeJob({ name : jobName });
throw new Error('execute job should have thrown an error due to no worker assigned to that job name.');
} catch (error) {
error.should.deepEqual(new Error('Job ' + jobName + ' does not have a worker assigned to it.'));
}
});
it('#executeJob() timeout logic should work if timeout is set.', async () => {
const jobTimeout = 100;
const job = {
id: 'd21dca87-435c-4533-b0af-ed9844e6b827',
name: 'test-job-one',
payload: JSON.stringify({
key: 'value'
}),
data: JSON.stringify({
attempts: 1
}),
priority: 0,
active: false,
timeout: jobTimeout,
created: new Date(),
failed: null
};
const worker = new Worker();
worker.addWorker('test-job-one', async () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, 1000);
});
});
try {
await worker.executeJob(job);
throw new Error('execute job should have thrown an error due to timeout.');
} catch (error) {
error.should.deepEqual(new Error('TIMEOUT: Job id: ' + job.id + ' timed out in ' + jobTimeout + 'ms.'));
}
});
it('#executeJob() should execute a job correctly.', async () => {
let counter = 0;
const job = {
id: 'd21dca87-435c-4533-b0af-ed9844e6b827',
name: 'test-job-one',
payload: JSON.stringify({
key: 'value'
}),
data: JSON.stringify({
timeout: 0,
attempts: 1
}),
priority: 0,
active: false,
created: new Date(),
failed: null
};
const worker = new Worker();
worker.addWorker('test-job-one', async () => {
// Job increments counter.
counter++;
return new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, 500);
});
});
counter.should.equal(0);
await worker.executeJob(job);
counter.should.equal(1);
});
it('#executeJobLifecycleCallback() should execute a job lifecycle method correctly.', async () => {
let onStartCalled = false;
let testPassed = false;
const job = {
id: 'd21dca87-435c-4533-b0af-ed9844e6b827',
name: 'test-job-one',
payload: JSON.stringify({
key: 'value'
}),
data: JSON.stringify({
timeout: 0,
attempts: 1
}),
priority: 0,
active: false,
created: new Date(),
failed: null
};
const worker = new Worker();
worker.addWorker('test-job-one', async () => {}, {
onStart: (id, payload) => {
onStartCalled = true;
// Verify params passed correctly off job and payload JSON has been parsed.
id.should.equal(job.id);
payload.should.deepEqual({
key: 'value'
});
// Explicitly mark test as passed because the assertions
// directly above will be caught in the try/catch statement within
// executeJobLifecycleCallback() if they throw an error. While any thrown errors will be
// output to console, the test will still pass so won't be caught by CI testing.
testPassed = true;
}
});
onStartCalled.should.equal(false);
const payload = JSON.parse(job.payload); // Payload JSON is always parsed by Queue model before passing to executeJobLifecycleCallback();
await worker.executeJobLifecycleCallback('onStart', job.name, job.id, payload);
onStartCalled.should.equal(true);
testPassed.should.equal(true);
});
it('#executeJobLifecycleCallback() should throw an error on invalid job lifecycle name.', async () => {
let onStartCalled = false;
let testPassed = true;
const job = {
id: 'd21dca87-435c-4533-b0af-ed9844e6b827',
name: 'test-job-one',
payload: JSON.stringify({
key: 'value'
}),
data: JSON.stringify({
timeout: 0,
attempts: 1
}),
priority: 0,
active: false,
created: new Date(),
failed: null
};
const worker = new Worker();
worker.addWorker('test-job-one', async () => {}, {
onStart: () => {
testPassed = false;
throw new Error('Should not be called.');
}
});
onStartCalled.should.equal(false);
const payload = JSON.parse(job.payload); // Payload JSON is always parsed by Queue model before passing to executeJobLifecycleCallback();
try {
await worker.executeJobLifecycleCallback('onInvalidLifecycleName', job.name, job.id, payload);
} catch (error) {
error.should.deepEqual(new Error('Invalid job lifecycle callback name.'));
}
onStartCalled.should.equal(false);
testPassed.should.equal(true);
});
it('#executeJobLifecycleCallback() job lifecycle callbacks that error out should gracefully degrade to console error.', async () => {
let onStartCalled = false;
let consoleErrorCalled = false;
// Cache console error.
const consoleErrorCache = console.error; // eslint-disable-line no-console
// Overwrite console.error to make sure it gets called on job lifecycle
// callback error and is passed the error object.
console.error = (errorObject) => { // eslint-disable-line no-console
consoleErrorCalled = true;
errorObject.should.deepEqual(new Error('Something failed catastrophically!'));
};
const job = {
id: 'd21dca87-435c-4533-b0af-ed9844e6b827',
name: 'test-job-one',
payload: JSON.stringify({
key: 'value'
}),
data: JSON.stringify({
timeout: 0,
attempts: 1
}),
priority: 0,
active: false,
created: new Date(),
failed: null
};
const worker = new Worker();
worker.addWorker('test-job-one', async () => {}, {
onStart: () => {
onStartCalled = true;
throw new Error('Something failed catastrophically!');
}
});
onStartCalled.should.equal(false);
consoleErrorCalled.should.equal(false);
const payload = JSON.parse(job.payload); // Payload JSON is always parsed by Queue model before passing to executeJobLifecycleCallback();
await worker.executeJobLifecycleCallback('onStart', job.name, job.id, payload);
onStartCalled.should.equal(true);
consoleErrorCalled.should.equal(true);
// Re-apply console.error.
console.error = consoleErrorCache; // eslint-disable-line no-console
});
});