-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_clean.ts
167 lines (133 loc) · 4.75 KB
/
test_clean.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
import { expect } from 'chai';
import * as IORedis from 'ioredis';
import { after } from 'lodash';
import { beforeEach, describe, it } from 'mocha';
import { v4 } from 'uuid';
import { Queue, QueueEvents, Worker } from '../src/classes';
import { delay, removeAllQueueData } from '../src/utils';
describe('Cleaner', () => {
let queue: Queue;
let queueEvents: QueueEvents;
let queueName: string;
beforeEach(async () => {
queueName = `test-${v4()}`;
queue = new Queue(queueName);
queueEvents = new QueueEvents(queueName);
await queueEvents.waitUntilReady();
});
afterEach(async function() {
await queue.close();
await queueEvents.close();
await removeAllQueueData(new IORedis(), queueName);
});
it('should clean an empty queue', async () => {
await queue.waitUntilReady();
const waitCleaned = new Promise<void>(resolve => {
queue.on('cleaned', (jobs, type) => {
expect(type).to.be.eql('completed');
expect(jobs.length).to.be.eql(0);
resolve();
});
});
const jobs = await queue.clean(0, 0);
expect(jobs.length).to.be.eql(0);
await waitCleaned;
});
it('should clean two jobs from the queue', async () => {
await queue.add('test', { some: 'data' });
await queue.add('test', { some: 'data' });
const worker = new Worker(queueName, async job => {});
await worker.waitUntilReady();
queue.on(
'completed',
after(2, async () => {
const jobs = await queue.clean(0, 0);
expect(jobs.length).to.be.eql(2);
}),
);
await worker.close();
});
it('should only remove a job outside of the grace period', async () => {
const worker = new Worker(queueName, async job => {});
await worker.waitUntilReady();
await queue.add('test', { some: 'data' });
await queue.add('test', { some: 'data' });
await delay(200);
await queue.add('test', { some: 'data' });
await queue.clean(100, 100);
await delay(100);
const jobs = await queue.getCompleted();
expect(jobs.length).to.be.eql(1);
await worker.close();
});
it('should not clean anything if all jobs are in grace period', async () => {
await queue.add('test', { some: 'data' });
await queue.add('test', { some: 'data' });
const count1 = await queue.count();
expect(count1).to.be.eql(2);
const cleaned = await queue.clean(5000, 2, 'wait');
expect(cleaned.length).to.be.eql(0);
const cleaned2 = await queue.clean(5000, 2, 'wait');
expect(cleaned2.length).to.be.eql(0);
const count2 = await queue.count();
expect(count2).to.be.eql(2);
});
it('should clean all failed jobs', async () => {
const worker = new Worker(queueName, async job => {
throw new Error('It failed');
});
await worker.waitUntilReady();
await queue.add('test', { some: 'data' });
await queue.add('test', { some: 'data' });
await delay(100);
const jobs = await queue.clean(0, 0, 'failed');
expect(jobs.length).to.be.eql(2);
const count = await queue.count();
expect(count).to.be.eql(0);
await worker.close();
});
it('should clean all waiting jobs', async () => {
await queue.add('test', { some: 'data' });
await queue.add('test', { some: 'data' });
await delay(100);
const jobs = await queue.clean(0, 0, 'wait');
expect(jobs.length).to.be.eql(2);
const count = await queue.count();
expect(count).to.be.eql(0);
});
it('should clean all delayed jobs', async () => {
await queue.add('test', { some: 'data' }, { delay: 5000 });
await queue.add('test', { some: 'data' }, { delay: 5000 });
await delay(100);
const jobs = await queue.clean(0, 0, 'delayed');
expect(jobs.length).to.be.eql(2);
const count = await queue.count();
expect(count).to.be.eql(0);
});
it('should clean the number of jobs requested', async () => {
await queue.add('test', { some: 'data' });
await queue.add('test', { some: 'data' });
await queue.add('test', { some: 'data' });
await delay(100);
const jobs = await queue.clean(0, 1, 'wait');
expect(jobs.length).to.be.eql(1);
const count = await queue.count();
expect(count).to.be.eql(2);
});
it('should clean a job without a timestamp', async () => {
const worker = new Worker(queueName, async job => {
throw new Error('It failed');
});
await worker.waitUntilReady();
const client = new IORedis();
await queue.add('test', { some: 'data' });
await queue.add('test', { some: 'data' });
await delay(100);
await client.hdel(`bull:${queueName}:1`, 'timestamp');
const jobs = await queue.clean(0, 0, 'failed');
expect(jobs.length).to.be.eql(2);
const failed = await queue.getFailed();
expect(failed.length).to.be.eql(0);
await worker.close();
});
});