-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_pause.ts
353 lines (285 loc) · 9.18 KB
/
test_pause.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
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
import { expect } from 'chai';
import * as IORedis from 'ioredis';
import { beforeEach, describe, it } from 'mocha';
import { v4 } from 'uuid';
import {
Job,
Queue,
QueueEvents,
QueueScheduler,
Worker,
} from '../src/classes';
import { delay, removeAllQueueData } from '../src/utils';
describe('Pause', function () {
let queue: Queue;
let queueName: string;
let queueEvents: QueueEvents;
const connection = { host: 'localhost' };
beforeEach(async function () {
queueName = `test-${v4()}`;
queue = new Queue(queueName, { connection });
queueEvents = new QueueEvents(queueName, { connection });
await queueEvents.waitUntilReady();
});
afterEach(async function () {
await queue.close();
await queueEvents.close();
await removeAllQueueData(new IORedis(), queueName);
});
// Skipped since some side effect makes this test fail
it.skip('should not processed delayed jobs', async function () {
this.timeout(5000);
const queueScheduler = new QueueScheduler(queueName);
await queueScheduler.waitUntilReady();
let processed = false;
const worker = new Worker(
queueName,
async () => {
processed = true;
},
{ connection },
);
await worker.waitUntilReady();
await queue.pause();
await queue.add('test', {}, { delay: 200 });
const counts = await queue.getJobCounts('waiting', 'delayed');
expect(counts).to.have.property('waiting', 0);
expect(counts).to.have.property('delayed', 1);
await delay(1000);
if (processed) {
throw new Error('should not process delayed jobs in paused queue.');
}
const counts2 = await queue.getJobCounts('waiting', 'paused', 'delayed');
expect(counts2).to.have.property('waiting', 0);
expect(counts2).to.have.property('paused', 1);
expect(counts2).to.have.property('delayed', 0);
await queueScheduler.close();
await worker.close();
});
it('should pause a queue until resumed', async () => {
let process;
let isPaused = false;
let counter = 2;
const processPromise = new Promise<void>(resolve => {
process = async (job: Job) => {
expect(isPaused).to.be.eql(false);
expect(job.data.foo).to.be.equal('paused');
counter--;
if (counter === 0) {
resolve();
}
};
});
const worker = new Worker(queueName, process, { connection });
await worker.waitUntilReady();
await queue.pause();
isPaused = true;
await queue.add('test', { foo: 'paused' });
await queue.add('test', { foo: 'paused' });
isPaused = false;
await queue.resume();
await processPromise;
return worker.close();
});
it('should be able to pause a running queue and emit relevant events', async () => {
let process;
let isPaused = false,
isResumed = true,
first = true;
const processPromise = new Promise<void>((resolve, reject) => {
process = async (job: Job) => {
try {
expect(isPaused).to.be.eql(false);
expect(job.data.foo).to.be.equal('paused');
if (first) {
first = false;
isPaused = true;
return queue.pause();
} else {
expect(isResumed).to.be.eql(true);
await queue.close();
resolve();
}
} catch (err) {
reject(err);
}
};
});
new Worker(queueName, process, { connection });
queueEvents.on('paused', async () => {
isPaused = false;
await queue.resume();
});
queueEvents.on('resumed', () => {
isResumed = true;
});
await queue.add('test', { foo: 'paused' });
await queue.add('test', { foo: 'paused' });
return processPromise;
});
it('should pause the queue locally', async () => {
let worker: Worker;
let counter = 2;
let process;
const processPromise = new Promise<void>(resolve => {
process = async (job: Job) => {
expect(worker.isPaused()).to.be.eql(false);
counter--;
if (counter === 0) {
await queue.close();
resolve();
}
};
});
worker = new Worker(queueName, process, { connection });
await worker.waitUntilReady();
await worker.pause();
// Add the worker after the queue is in paused mode since the normal behavior is to pause
// it after the current lock expires. This way, we can ensure there isn't a lock already
// to test that pausing behavior works.
await queue.add('test', { foo: 'paused' });
await queue.add('test', { foo: 'paused' });
expect(counter).to.be.eql(2);
expect(worker.isPaused()).to.be.eql(true);
await worker.resume();
return processPromise;
});
it('should wait until active jobs are finished before resolving pause', async () => {
let process;
const startProcessing = new Promise<void>(resolve => {
process = async () => {
resolve();
return delay(1000);
};
});
const worker = new Worker(queueName, process, { connection });
await worker.waitUntilReady();
const jobs: Promise<Job | void>[] = [];
for (let i = 0; i < 10; i++) {
jobs.push(queue.add('test', i));
}
//
// Add start processing so that we can test that pause waits for this job to be completed.
//
jobs.push(startProcessing);
await Promise.all(jobs);
await worker.pause();
let active = await queue.getJobCountByTypes('active');
expect(active).to.be.eql(0);
expect(worker.isPaused()).to.be.eql(true);
// One job from the 10 posted above will be processed, so we expect 9 jobs pending
let paused = await queue.getJobCountByTypes('delayed', 'waiting');
expect(paused).to.be.eql(9);
await queue.add('test', {});
active = await queue.getJobCountByTypes('active');
expect(active).to.be.eql(0);
paused = await queue.getJobCountByTypes('paused', 'waiting', 'delayed');
expect(paused).to.be.eql(10);
await worker.close();
});
it('should pause the queue locally when more than one worker is active', async () => {
let process1, process2;
const startProcessing1 = new Promise<void>(resolve => {
process1 = async () => {
resolve();
return delay(200);
};
});
const startProcessing2 = new Promise<void>(resolve => {
process2 = async () => {
resolve();
return delay(200);
};
});
const worker1 = new Worker(queueName, process1, { connection });
await worker1.waitUntilReady();
const worker2 = new Worker(queueName, process2, { connection });
await worker2.waitUntilReady();
await Promise.all([
queue.add('test', 1),
queue.add('test', 2),
queue.add('test', 3),
queue.add('test', 4),
]);
await Promise.all([startProcessing1, startProcessing2]);
await Promise.all([worker1.pause(), worker2.pause()]);
const count = await queue.getJobCounts('active', 'waiting', 'completed');
expect(count.active).to.be.eql(0);
expect(count.waiting).to.be.eql(2);
expect(count.completed).to.be.eql(2);
return Promise.all([worker1.close(), worker2.close()]);
});
it('should wait for blocking job retrieval to complete before pausing locally', async () => {
let process;
const startProcessing = new Promise<void>(resolve => {
process = async () => {
resolve();
return delay(200);
};
});
const worker = new Worker(queueName, process, { connection });
await worker.waitUntilReady();
await queue.add('test', 1);
await startProcessing;
await worker.pause();
await queue.add('test', 2);
const count = await queue.getJobCounts('active', 'waiting', 'completed');
expect(count.active).to.be.eql(0);
expect(count.waiting).to.be.eql(1);
expect(count.completed).to.be.eql(1);
return worker.close();
});
it('pauses fast when queue is drained', async function () {
const worker = new Worker(
queueName,
async () => {
},
{
connection,
},
);
await worker.waitUntilReady();
await queue.add('test', {});
return new Promise((resolve, reject) => {
queueEvents.on('drained', async () => {
try {
const start = new Date().getTime();
await queue.pause();
const finish = new Date().getTime();
expect(finish - start).to.be.lt(1000);
} catch (err) {
reject(err);
} finally {
await worker.close();
}
resolve();
});
});
});
it('gets the right response from isPaused', async () => {
await queue.pause();
const isPausedQueuePaused = await queue.isPaused();
expect(isPausedQueuePaused).to.be.true;
await queue.resume();
const isResumedQueuePaused = await queue.isPaused();
expect(isResumedQueuePaused).to.be.false;
});
it('should pause and resume worker without error', async function () {
const worker = new Worker(
queueName,
async job => {
await delay(100);
},
{ connection },
);
await worker.waitUntilReady();
await delay(10);
await worker.pause();
await delay(10);
worker.resume();
await delay(10);
await worker.pause();
await delay(10);
return worker.close();
});
});