-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_delay.ts
289 lines (236 loc) · 8.1 KB
/
test_delay.ts
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
import { Queue, Job, Worker, QueueEvents, QueueScheduler } from '../src/classes';
import { describe, beforeEach, it } from 'mocha';
import { expect } from 'chai';
import * as IORedis from 'ioredis';
import { v4 } from 'uuid';
import { removeAllQueueData } from '../src/utils';
describe('Delayed jobs', function() {
this.timeout(15000);
let queue: Queue;
let queueName: string;
beforeEach(async function() {
queueName = `test-${v4()}`;
queue = new Queue(queueName);
});
afterEach(async function() {
await queue.close();
await removeAllQueueData(new IORedis(), queueName);
});
it('should process a delayed job only after delayed time', async function() {
const delay = 1000;
const queueScheduler = new QueueScheduler(queueName);
await queueScheduler.waitUntilReady();
const queueEvents = new QueueEvents(queueName);
await queueEvents.waitUntilReady();
const worker = new Worker(queueName, async job => {});
const timestamp = Date.now();
let publishHappened = false;
const delayed = new Promise<void>((resolve, reject) => {
queueEvents.on('delayed', () => {
publishHappened = true;
resolve();
});
});
const completed = new Promise<void>((resolve, reject) => {
queueEvents.on('completed', async function() {
try {
expect(Date.now() > timestamp + delay);
const jobs = await queue.getWaiting();
expect(jobs.length).to.be.equal(0);
const delayedJobs = await queue.getDelayed();
expect(delayedJobs.length).to.be.equal(0);
expect(publishHappened).to.be.eql(true);
await worker.close();
resolve();
} catch (err) {
reject(err);
}
});
});
const job = await queue.add('test', { delayed: 'foobar' }, { delay });
expect(job.id).to.be.ok;
expect(job.data.delayed).to.be.eql('foobar');
expect(job.opts.delay).to.be.eql(delay);
await delayed;
await completed;
await queueScheduler.close();
await queueEvents.close();
await worker.close();
});
describe('when autorun option is provided as false', function() {
it('should process a delayed job only after delayed time', async function() {
const delay = 1000;
const queueScheduler = new QueueScheduler(queueName, { autorun: false });
await queueScheduler.waitUntilReady();
const queueEvents = new QueueEvents(queueName);
await queueEvents.waitUntilReady();
const worker = new Worker(queueName, async job => {});
const timestamp = Date.now();
let publishHappened = false;
const delayed = new Promise<void>((resolve, reject) => {
queueEvents.on('delayed', () => {
publishHappened = true;
resolve();
});
});
const completed = new Promise<void>((resolve, reject) => {
queueEvents.on('completed', async function() {
try {
expect(Date.now() > timestamp + delay);
const jobs = await queue.getWaiting();
expect(jobs.length).to.be.equal(0);
const delayedJobs = await queue.getDelayed();
expect(delayedJobs.length).to.be.equal(0);
expect(publishHappened).to.be.eql(true);
resolve();
} catch (err) {
reject(err);
}
});
});
const job = await queue.add('test', { delayed: 'foobar' }, { delay });
expect(job.id).to.be.ok;
expect(job.data.delayed).to.be.eql('foobar');
expect(job.opts.delay).to.be.eql(delay);
queueScheduler.run();
await delayed;
await completed;
await queueScheduler.close();
await queueEvents.close();
await worker.close();
});
describe('when run method is called when queue-scheduler is running', function() {
it('throws an error', async function() {
const delay = 1000;
const maxJobs = 10;
const queueScheduler = new QueueScheduler(queueName, {
autorun: false,
});
await queueScheduler.waitUntilReady();
const worker = new Worker(queueName, async job => {});
queueScheduler.run();
for (let i = 1; i <= maxJobs; i++) {
await queue.add('test', { foo: 'bar', num: i }, { delay });
}
await expect(queueScheduler.run()).to.be.rejectedWith(
'Queue Scheduler is already running.',
);
await queueScheduler.close();
await worker.close();
});
});
});
it('should process delayed jobs in correct order', async function() {
this.timeout(2000);
let order = 0;
const queueScheduler = new QueueScheduler(queueName);
await queueScheduler.waitUntilReady();
let processor;
const processing = new Promise<void>((resolve, reject) => {
processor = async (job: Job) => {
order++;
try {
expect(order).to.be.equal(job.data.order);
if (order === 10) {
resolve();
}
} catch (err) {
reject(err);
}
};
});
const worker = new Worker(queueName, processor);
worker.on('failed', function(job, err) {});
await Promise.all([
queue.add('test', { order: 1 }, { delay: 100 }),
queue.add('test', { order: 6 }, { delay: 600 }),
queue.add('test', { order: 10 }, { delay: 1000 }),
queue.add('test', { order: 2 }, { delay: 200 }),
queue.add('test', { order: 9 }, { delay: 900 }),
queue.add('test', { order: 5 }, { delay: 500 }),
queue.add('test', { order: 3 }, { delay: 300 }),
queue.add('test', { order: 7 }, { delay: 700 }),
queue.add('test', { order: 4 }, { delay: 400 }),
queue.add('test', { order: 8 }, { delay: 800 }),
]);
await processing;
await queueScheduler.close();
await worker.close();
});
it('should process delayed jobs in correct order even in case of restart', async function() {
this.timeout(5000);
let worker: Worker;
let order = 1;
let secondQueueScheduler: QueueScheduler;
const firstQueueScheduler = new QueueScheduler(queueName);
await firstQueueScheduler.waitUntilReady();
queue = new Queue(queueName);
const processing = new Promise<void>((resolve, reject) => {
worker = new Worker(queueName, async (job: Job) => {
try {
expect(order).to.be.equal(job.data.order);
if (order === 1) {
await firstQueueScheduler.close();
secondQueueScheduler = new QueueScheduler(queueName);
await secondQueueScheduler.waitUntilReady();
}
if (order === 4) {
resolve();
}
order++;
} catch (err) {
reject(err);
}
});
});
await Promise.all([
queue.add('test', { order: 2 }, { delay: 500 }),
queue.add('test', { order: 4 }, { delay: 1500 }),
queue.add('test', { order: 1 }, { delay: 200 }),
queue.add('test', { order: 3 }, { delay: 800 }),
]);
await processing;
await queue.close();
worker && (await worker.close());
secondQueueScheduler && (await secondQueueScheduler.close());
});
it('should process delayed jobs with exact same timestamps in correct order (FIFO)', async function() {
let order = 1;
const queueScheduler = new QueueScheduler(queueName);
await queueScheduler.waitUntilReady();
let worker: Worker;
const processing = new Promise((resolve, reject) => {
worker = new Worker(queueName, async (job: Job) => {
try {
expect(order).to.be.equal(job.data.order);
if (order === 12) {
resolve(worker.close());
}
} catch (err) {
reject(err);
}
order++;
});
});
worker.on('failed', function(job, err) {});
const now = Date.now();
const promises = [];
let i = 1;
for (i; i <= 12; i++) {
promises.push(
queue.add(
'test',
{ order: i },
{
delay: 1000,
timestamp: now,
},
),
);
}
await Promise.all(promises);
await processing;
await queueScheduler.close();
await worker.close();
});
});