forked from redis/ioredis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreconnect_on_error.ts
More file actions
166 lines (148 loc) · 3.68 KB
/
reconnect_on_error.ts
File metadata and controls
166 lines (148 loc) · 3.68 KB
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
import Redis from "../../lib/Redis";
import { expect } from "chai";
import * as sinon from "sinon";
describe("reconnectOnError", () => {
it("should pass the error as the first param", (done) => {
let pending = 2;
function assert(err) {
expect(err.name).to.eql("ReplyError");
expect(err.command.name).to.eql("set");
expect(err.command.args).to.eql(["foo"]);
if (!--pending) {
done();
}
}
const redis = new Redis({
reconnectOnError: function (err) {
assert(err);
return 1;
},
});
redis.set("foo", function (err) {
assert(err);
});
});
it("should not reconnect if reconnectOnError returns false", (done) => {
const redis = new Redis({
reconnectOnError: function (err) {
return false;
},
});
redis.disconnect = () => {
throw new Error("should not disconnect");
};
redis.set("foo", function (err) {
done();
});
});
it("should reconnect if reconnectOnError returns true or 1", (done) => {
const redis = new Redis({
reconnectOnError: () => {
return true;
},
});
redis.set("foo", () => {
redis.on("ready", () => {
done();
});
});
});
it("should reconnect and retry the command if reconnectOnError returns 2", (done) => {
const redis = new Redis({
reconnectOnError: () => {
redis.del("foo");
return 2;
},
});
redis.set("foo", "bar");
redis.sadd("foo", "a", function (err, res) {
expect(res).to.eql(1);
done();
});
});
it("should select the currect database", (done) => {
const redis = new Redis({
reconnectOnError: () => {
redis.select(3);
redis.del("foo");
redis.select(0);
return 2;
},
});
redis.select(3);
redis.set("foo", "bar");
redis.sadd("foo", "a", function (err, res) {
expect(res).to.eql(1);
redis.select(3);
redis.type("foo", function (err, type) {
expect(type).to.eql("set");
done();
});
});
});
it("should work with pipeline", (done) => {
const redis = new Redis({
reconnectOnError: () => {
redis.del("foo");
return 2;
},
});
redis.set("foo", "bar");
redis
.pipeline()
.get("foo")
.sadd("foo", "a")
.exec(function (err, res) {
expect(res).to.eql([
[null, "bar"],
[null, 1],
]);
done();
});
});
it("should work with pipelined multi", (done) => {
const redis = new Redis({
reconnectOnError: () => {
// deleting foo allows sadd below to succeed on the second try
redis.del("foo");
return 2;
},
});
const delSpy = sinon.spy(redis, "del");
redis.set("foo", "bar");
redis.set("i", 1);
redis
.pipeline()
.sadd("foo", "a") // trigger a WRONGTYPE error
.multi()
.get("foo")
.incr("i")
.exec()
.exec(function (err, res) {
expect(delSpy.calledOnce).to.eql(true);
expect(delSpy.firstCall.args[0]).to.eql("foo");
expect(err).to.be.null;
expect(res).to.eql([
[null, 1],
[null, "OK"],
[null, "QUEUED"],
[null, "QUEUED"],
[null, ["bar", 2]],
]);
done();
});
});
it("should not reconnect if reconnectOnError if ignored command fails", (done) => {
const redis = new Redis({
reconnectOnError: function (err) {
return true;
},
});
redis.disconnect = () => {
throw new Error("should not disconnect");
};
redis.client("some-wrong-arg", function (err) {
done();
});
});
});