forked from OptimalBits/bull
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_connection.js
144 lines (125 loc) · 3.25 KB
/
test_connection.js
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
'use strict';
const expect = require('expect.js');
const utils = require('./utils');
const redis = require('ioredis');
describe('connection', () => {
let queue;
let client;
beforeEach(() => {
client = new redis();
return client.flushdb().then(() => {
queue = utils.buildQueue();
return queue;
});
});
afterEach(() => {
return client.quit();
});
it('should recover from a connection loss', done => {
queue.on('error', () => {
// error event has to be observed or the exception will bubble up
});
queue
.process((job, jobDone) => {
expect(job.data.foo).to.be.equal('bar');
jobDone();
queue.close();
})
.then(() => {
done();
})
.catch(done);
// Simulate disconnect
queue.isReady().then(() => {
queue.client.stream.end();
queue.client.emit('error', new Error('ECONNRESET'));
// add something to the queue
queue.add({ foo: 'bar' });
});
});
it('should handle jobs added before and after a redis disconnect', done => {
let count = 0;
queue
.process((job, jobDone) => {
if (count == 0) {
expect(job.data.foo).to.be.equal('bar');
jobDone();
} else {
jobDone();
queue.close().then(done, done);
}
count++;
})
.catch(done);
queue.on('completed', () => {
if (count === 1) {
queue.client.stream.end();
queue.client.emit('error', new Error('ECONNRESET'));
}
});
queue.isReady().then(() => {
queue.add({ foo: 'bar' });
});
queue.on('error', (/*err*/) => {
if (count === 1) {
queue.add({ foo: 'bar' });
}
});
});
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 new Promise(resolve => {
if (subscriber.status === 'ready') {
return resolve();
}
subscriber.once('ready', resolve);
})
.then(() => {
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', done => {
queue = utils.buildQueue('connection fail', {
redis: {
host: 'localhost',
port: 1234
}
});
queue.isReady().then(
() => {
done(new Error('Did not fail connecting to invalid redis instance'));
},
err => {
expect(err.code).to.be.eql('ECONNREFUSED');
queue.close().then(done, done);
}
);
});
});