-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathtest_connection.ts
413 lines (333 loc) · 11.3 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import { expect } from 'chai';
import { default as IORedis, RedisOptions } from 'ioredis';
import { v4 } from 'uuid';
import { Queue, Job, Worker, QueueBase } from '../src/classes';
import { removeAllQueueData } from '../src/utils';
import {
before,
describe,
it,
beforeEach,
afterEach,
after as afterAll,
} from 'mocha';
describe('connection', () => {
const redisHost = process.env.REDIS_HOST || 'localhost';
const prefix = process.env.BULLMQ_TEST_PREFIX || 'bull';
let queue: Queue;
let queueName: string;
let connection;
before(async function () {
connection = new IORedis(redisHost, { maxRetriesPerRequest: null });
});
beforeEach(async function () {
queueName = `test-${v4()}`;
queue = new Queue(queueName, { connection, prefix });
});
afterEach(async function () {
await queue.close();
await removeAllQueueData(new IORedis(redisHost), queueName);
});
afterAll(async function () {
await connection.quit();
});
describe('establish ioredis connection', () => {
it('should connect with host:port', async () => {
const queue = new Queue('valid-host-port', {
connection: {
host: 'localhost',
port: 6379,
retryStrategy: () => null,
},
});
const client = await queue.waitUntilReady();
expect(client.status).to.be.eql('ready');
await queue.close();
});
it('should fail with invalid host:port', async () => {
const queue = new Queue('invalid-host-port', {
connection: {
host: 'localhost',
port: 9000,
retryStrategy: () => null,
},
});
await expect(queue.waitUntilReady()).to.be.eventually.rejectedWith(
'connect ECONNREFUSED 127.0.0.1:9000',
);
});
it('should connect with connection URL', async () => {
const queue = new Queue('valid-url', {
connection: {
url: 'redis://localhost:6379',
// Make sure defaults are not being used
host: '1.1.1.1',
port: 2222,
retryStrategy: () => null,
},
});
const client = await queue.waitUntilReady();
expect(client.status).to.be.eql('ready');
await queue.close();
});
it('should fail with invalid connection URL', async () => {
const queue = new Queue('invalid-url', {
connection: {
url: 'redis://localhost:9001',
// Make sure defaults are not being used
host: '1.1.1.1',
port: 2222,
retryStrategy: () => null,
},
});
await expect(queue.waitUntilReady()).to.be.eventually.rejectedWith(
'connect ECONNREFUSED 127.0.0.1:9001',
);
});
});
describe('prefix', () => {
it('should throw exception if using prefix with ioredis', async () => {
const connection = new IORedis({
host: redisHost,
keyPrefix: 'bullmq',
});
expect(() => new QueueBase(queueName, { connection })).to.throw(
'BullMQ: ioredis does not support ioredis prefixes, use the prefix option instead.',
);
await connection.disconnect();
});
it('should throw exception if using prefix with ioredis in cluster mode', async () => {
const connection = new IORedis.Cluster(
[{ host: '10.0.6.161', port: 7379 }],
{
keyPrefix: 'bullmq',
natMap: {},
},
);
expect(() => new QueueBase(queueName, { connection })).to.throw(
'BullMQ: ioredis does not support ioredis prefixes, use the prefix option instead.',
);
await connection.disconnect();
});
});
describe('blocking', () => {
it('should override maxRetriesPerRequest: null as redis options', async () => {
if (redisHost === 'localhost') {
// We cannot currently test this behaviour for remote redis servers
const queue = new QueueBase(queueName, {
connection: { host: 'localhost' },
});
const options = connection.options;
expect(options.maxRetriesPerRequest).to.be.equal(null);
await queue.close();
}
});
});
describe('non-blocking', () => {
it('should not override any redis options', async () => {
const connection2 = new IORedis(redisHost, { maxRetriesPerRequest: 20 });
const queue = new Queue(queueName, {
connection: connection2,
});
const options = <RedisOptions>(await queue.client).options;
expect(options.maxRetriesPerRequest).to.be.equal(20);
await queue.close();
await connection2.quit();
});
});
describe('when maxmemory-policy is different than noeviction in Redis', () => {
it.skip('throws an error', async () => {
const opts = {
connection: {
host: 'localhost',
},
};
const queue = new QueueBase(queueName, opts);
const client = await queue.client;
await client.config('SET', 'maxmemory-policy', 'volatile-lru');
const queue2 = new QueueBase(`${queueName}2`, opts);
await expect(queue2.client).to.be.eventually.rejectedWith(
'Eviction policy is volatile-lru. It should be "noeviction"',
);
await client.config('SET', 'maxmemory-policy', 'noeviction');
await queue.close();
await queue2.close();
});
});
describe('when instantiating with a clustered ioredis connection', () => {
it('should not fail when using dsn strings', async () => {
const connection = new IORedis.Cluster(['redis://10.0.6.161:7379'], {
natMap: {},
});
const queue = new Queue('myqueue', { connection });
connection.disconnect();
});
});
it('should close worker even if redis is down', async () => {
const connection = new IORedis('badhost', { maxRetriesPerRequest: null });
connection.on('error', () => {});
const worker = new Worker('test', async () => {}, { connection, prefix });
worker.on('error', err => {});
await worker.close();
});
it('should close underlying redis connection when closing fast', async () => {
const queue = new Queue('CALLS_JOB_QUEUE_NAME', {
connection: {
host: 'localhost',
port: 6379,
},
});
const client = queue['connection']['_client'];
await queue.close();
expect(client.status).to.be.eql('end');
});
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, { connection, prefix });
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' }, { delay: 2000 });
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, { connection, prefix });
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.waitUntilReady()).to.be.eventually.rejectedWith(
'connect ECONNREFUSED 127.0.0.1:1234',
);
await expect(queueFail.close()).to.be.eventually.equal(undefined);
});
});