-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_connection.ts
232 lines (186 loc) · 6.2 KB
/
test_connection.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
import { expect } from 'chai';
import * as IORedis from 'ioredis';
import { v4 } from 'uuid';
import { Queue, Job, Worker, QueueBase } from '../src/classes';
import { RedisClient } from '../src/interfaces';
import { removeAllQueueData } from '../src/utils';
describe('connection', () => {
let queue: Queue;
let queueName: string;
beforeEach(async function() {
queueName = `test-${v4()}`;
queue = new Queue(queueName, { connection: { host: 'localhost' } });
});
afterEach(async function() {
await queue.close();
await removeAllQueueData(new IORedis(), queueName);
});
it('should override maxRetriesPerRequest: null and enableReadyCheck: false as redis options', async () => {
const opts = {
connection: {
host: 'localhost',
maxRetriesPerRequest: 20,
enableReadyCheck: true,
},
};
function checkOptions(client: RedisClient) {
expect(
(<IORedis.RedisOptions>client.options).maxRetriesPerRequest,
).to.be.equal(null);
expect(
(<IORedis.RedisOptions>client.options).enableReadyCheck,
).to.be.equal(false);
}
const queue = new QueueBase(queueName, opts);
checkOptions(await queue.client);
});
it('should recover from a connection loss', async () => {
let processor;
const processing = new Promise<void>(resolve => {
processor = async (job: Job) => {
expect(job.data.foo).to.be.equal('bar');
resolve();
};
});
const worker = new Worker(queueName, processor);
worker.on('error', err => {
// error event has to be observed or the exception will bubble up
});
queue.on('error', (err: Error) => {
// error event has to be observed or the exception will bubble up
});
const workerClient = await worker.client;
const queueClient = await queue.client;
// Simulate disconnect
(<any>queueClient).stream.end();
queueClient.emit('error', new Error('ECONNRESET'));
(<any>workerClient).stream.end();
workerClient.emit('error', new Error('ECONNRESET'));
// add something to the queue
await queue.add('test', { foo: 'bar' });
await processing;
await worker.close();
});
it('should handle jobs added before and after a redis disconnect', async () => {
let count = 0;
let processor;
const processing = new Promise<void>((resolve, reject) => {
processor = async (job: Job) => {
try {
if (count == 0) {
expect(job.data.foo).to.be.equal('bar');
} else {
resolve();
}
count++;
} catch (err) {
reject(err);
}
};
});
const worker = new Worker(queueName, processor);
worker.on('error', err => {
// error event has to be observed or the exception will bubble up
});
queue.on('error', (err: Error) => {
// error event has to be observed or the exception will bubble up
});
await worker.waitUntilReady();
worker.on('completed', async () => {
if (count === 1) {
const workerClient = await worker.client;
const queueClient = await queue.client;
(<any>queueClient).stream.end();
queueClient.emit('error', new Error('ECONNRESET'));
(<any>workerClient).stream.end();
workerClient.emit('error', new Error('ECONNRESET'));
await queue.add('test', { foo: 'bar' });
}
});
await queue.waitUntilReady();
await queue.add('test', { foo: 'bar' });
await processing;
await worker.close();
});
/*
it('should not close external connections', () => {
const client = new redis();
const subscriber = new redis();
const opts = {
createClient(type) {
switch (type) {
case 'client':
return client;
case 'subscriber':
return subscriber;
default:
return new redis();
}
},
};
const testQueue = utils.buildQueue('external connections', opts);
return testQueue
.isReady()
.then(() => {
return testQueue.add({ foo: 'bar' });
})
.then(() => {
expect(testQueue.client).to.be.eql(client);
expect(testQueue.eclient).to.be.eql(subscriber);
return testQueue.close();
})
.then(() => {
expect(client.status).to.be.eql('ready');
expect(subscriber.status).to.be.eql('ready');
return Promise.all([client.quit(), subscriber.quit()]);
});
});
*/
it('should fail if redis connection fails', async () => {
const queueFail = new Queue('connection fail port', {
connection: { port: 1234, host: '127.0.0.1', retryStrategy: () => null },
});
await expect(queueFail.waitUntilReady()).to.be.eventually.rejectedWith(
'connect ECONNREFUSED 127.0.0.1:1234',
);
});
it('should emit error if redis connection fails', async () => {
const queueFail = new Queue('connection fail port', {
connection: { port: 1234, host: '127.0.0.1', retryStrategy: () => null },
});
const waitingErrorEvent = new Promise<void>((resolve, reject) => {
queueFail.on('error', (err: Error) => {
try {
expect(err.message).to.equal('connect ECONNREFUSED 127.0.0.1:1234');
resolve();
} catch (err) {
reject(err);
}
});
});
await waitingErrorEvent;
});
it('should close if connection has failed', async () => {
const queueFail = new Queue('connection fail port', {
connection: { port: 1234, host: '127.0.0.1', retryStrategy: () => null },
});
queueFail.on('error', () => {});
await expect(queueFail.waitUntilReady()).to.be.rejectedWith(
'connect ECONNREFUSED 127.0.0.1:1234',
);
await expect(queueFail.close()).to.be.eventually.equal(undefined);
});
it('should close if connection is failing', async () => {
const queueFail = new Queue('connection fail port', {
connection: {
port: 1234,
host: '127.0.0.1',
retryStrategy: times => (times === 0 ? 10 : null),
},
});
await expect(queueFail.close()).to.be.eventually.equal(undefined);
await expect(queueFail.waitUntilReady()).to.be.eventually.rejectedWith(
'connect ECONNREFUSED 127.0.0.1:1234',
);
});
});