/
connect.ts
459 lines (409 loc) 路 12.7 KB
/
connect.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import MockServer from "../../helpers/mock_server";
import { expect } from "chai";
import { Cluster } from "../../../lib";
import * as sinon from "sinon";
describe("cluster:connect", function () {
it("should flush the queue when all startup nodes are unreachable", function (done) {
const cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }], {
clusterRetryStrategy: null,
});
cluster.get("foo", function (err) {
expect(err.message).to.match(/None of startup nodes is available/);
cluster.disconnect();
done();
});
});
it("should invoke clusterRetryStrategy when all startup nodes are unreachable", function (done) {
let t = 0;
const cluster = new Cluster(
[
{ host: "127.0.0.1", port: "30001" },
{ host: "127.0.0.1", port: "30002" },
],
{
clusterRetryStrategy: function (times) {
expect(times).to.eql(++t);
if (times === 3) {
return;
}
return 0;
},
}
);
cluster.get("foo", function (err) {
expect(t).to.eql(3);
expect(err.message).to.match(/None of startup nodes is available/);
cluster.disconnect();
done();
});
});
it("should invoke clusterRetryStrategy when none nodes are ready", function (done) {
const argvHandler = function (argv) {
if (argv[0] === "cluster") {
return new Error("CLUSTERDOWN");
}
};
new MockServer(30001, argvHandler);
new MockServer(30002, argvHandler);
let t = 0;
var cluster = new Cluster(
[
{ host: "127.0.0.1", port: "30001" },
{ host: "127.0.0.1", port: "30002" },
],
{
clusterRetryStrategy: function (times) {
expect(times).to.eql(++t);
if (times === 3) {
cluster.disconnect();
done();
return;
}
return 0;
},
}
);
});
it("should connect to cluster successfully", function (done) {
const node = new MockServer(30001);
const cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }]);
node.once("connect", function () {
cluster.disconnect();
done();
});
});
it("should wait for ready state before resolving", function (done) {
const slotTable = [[0, 16383, ["127.0.0.1", 30001]]];
const argvHandler = function (argv) {
if (argv[0] === "info") {
// return 'role:master'
}
if (argv[0] === "cluster" && argv[1] === "slots") {
return slotTable;
}
if (argv[0] === "cluster" && argv[1] === "info") {
return "cluster_state:ok";
}
};
new MockServer(30001, argvHandler);
const cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }], {
lazyConnect: true,
});
cluster.connect().then(function () {
expect(cluster.status).to.eql("ready");
cluster.disconnect();
done();
});
});
it("should support url schema", function (done) {
const node = new MockServer(30001);
const cluster = new Cluster(["redis://127.0.0.1:30001"]);
node.once("connect", function () {
cluster.disconnect();
done();
});
});
it("should support a single port", function (done) {
const node = new MockServer(30001);
const cluster = new Cluster([30001]);
node.once("connect", function () {
cluster.disconnect();
done();
});
});
it("should return a promise to be resolved when connected", function (done) {
const slotTable = [
[0, 5460, ["127.0.0.1", 30001]],
[5461, 10922, ["127.0.0.1", 30002]],
[10923, 16383, ["127.0.0.1", 30003]],
];
const argvHandler = function (argv) {
if (argv[0] === "cluster" && argv[1] === "slots") {
return slotTable;
}
};
new MockServer(30001, argvHandler);
new MockServer(30002, argvHandler);
new MockServer(30003, argvHandler);
sinon.stub(Cluster.prototype, "connect").callsFake(() => Promise.resolve());
const cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }], {
lazyConnect: false,
});
Cluster.prototype.connect.restore();
cluster.connect().then(function () {
cluster.disconnect();
done();
});
});
it("should return a promise to be rejected when closed", function (done) {
sinon.stub(Cluster.prototype, "connect").callsFake(() => Promise.resolve());
const cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }], {
lazyConnect: false,
});
Cluster.prototype.connect.restore();
cluster.connect().catch(function () {
cluster.disconnect();
done();
});
});
it("should stop reconnecting when disconnected", function (done) {
const cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }], {
clusterRetryStrategy: function () {
return 0;
},
});
cluster.on("close", function () {
cluster.disconnect();
const stub = sinon
.stub(Cluster.prototype, "connect")
.throws(new Error("`connect` should not be called"));
setTimeout(function () {
stub.restore();
done();
}, 1);
});
});
it("should discover other nodes automatically", function (done) {
const slotTable = [
[0, 5460, ["127.0.0.1", 30001]],
[5461, 10922, ["127.0.0.1", 30002]],
[10923, 16383, ["127.0.0.1", 30003]],
];
const argvHandler = function (argv) {
if (argv[0] === "cluster" && argv[1] === "slots") {
return slotTable;
}
};
const node1 = new MockServer(30001, argvHandler);
const node2 = new MockServer(30002, argvHandler);
const node3 = new MockServer(30003, argvHandler);
let pending = 3;
node1.once("connect", check);
node2.once("connect", check);
node3.once("connect", check);
const cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }], {
redisOptions: { lazyConnect: false },
});
function check() {
if (!--pending) {
cluster.disconnect();
done();
}
}
});
it("should send command to the correct node", function (done) {
new MockServer(30001, function (argv) {
if (argv[0] === "cluster" && argv[1] === "slots") {
return [
[0, 1, ["127.0.0.1", 30001]],
[2, 16383, ["127.0.0.1", 30002]],
];
}
});
new MockServer(30002, function (argv) {
if (argv[0] === "get" && argv[1] === "foo") {
process.nextTick(function () {
cluster.disconnect();
done();
});
}
});
var cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }], {
lazyConnect: false,
});
cluster.get("foo");
});
it("should emit errors when cluster cannot be connected", function (done) {
const errorMessage = "ERR This instance has cluster support disabled";
const argvHandler = function (argv) {
if (argv[0] === "cluster" && argv[1] === "slots") {
return new Error(errorMessage);
}
};
new MockServer(30001, argvHandler);
new MockServer(30002, argvHandler);
let pending = 2;
let retry: number | false = 0;
var cluster = new Cluster(
[
{ host: "127.0.0.1", port: "30001" },
{ host: "127.0.0.1", port: "30002" },
],
{
clusterRetryStrategy: function () {
cluster.once("error", function (err) {
retry = false;
expect(err.message).to.eql("Failed to refresh slots cache.");
expect(err.lastNodeError.message).to.eql(errorMessage);
checkDone();
});
return retry;
},
}
);
cluster.once("node error", function (err, key) {
expect(err.message).to.eql(errorMessage);
expect(["127.0.0.1:30001", "127.0.0.1:30002"]).to.include(key);
checkDone();
});
function checkDone() {
if (!--pending) {
cluster.disconnect();
done();
}
}
});
it("should using the specified password", function (done) {
let cluster;
const slotTable = [
[0, 5460, ["127.0.0.1", 30001]],
[5461, 10922, ["127.0.0.1", 30002]],
[10923, 16383, ["127.0.0.1", 30003]],
];
const argvHandler = function (port, argv) {
if (argv[0] === "cluster" && argv[1] === "slots") {
return slotTable;
}
if (argv[0] === "auth") {
const password = argv[1];
if (port === 30001) {
expect(password).to.eql("other password");
} else if (port === 30002) {
throw new Error("30002 got password");
} else if (port === 30003) {
expect(password).to.eql("default password");
cluster.disconnect();
done();
}
}
};
new MockServer(30001, argvHandler.bind(null, 30001));
new MockServer(30002, argvHandler.bind(null, 30002));
new MockServer(30003, argvHandler.bind(null, 30003));
cluster = new Cluster(
[
{ host: "127.0.0.1", port: "30001", password: "other password" },
{ host: "127.0.0.1", port: "30002", password: null },
],
{ redisOptions: { lazyConnect: false, password: "default password" } }
);
});
it("should discover other nodes automatically every slotsRefreshInterval", function (done) {
let times = 0;
let cluster;
const argvHandler = function (argv) {
if (argv[0] === "cluster" && argv[1] === "slots") {
times++;
if (times === 1) {
return [
[0, 5460, ["127.0.0.1", 30001]],
[5461, 10922, ["127.0.0.1", 30001]],
[10923, 16383, ["127.0.0.1", 30001]],
];
}
return [
[0, 5460, ["127.0.0.1", 30001]],
[5461, 10922, ["127.0.0.1", 30001]],
[10923, 16383, ["127.0.0.1", 30002]],
];
}
};
const node1 = new MockServer(30001, argvHandler);
const node2 = new MockServer(30002, argvHandler);
node1.once("connect", function () {
node2.once("connect", function () {
cluster.disconnect();
done();
});
});
cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }], {
slotsRefreshInterval: 100,
redisOptions: { lazyConnect: false },
});
});
it("throws when startupNodes is empty", (done) => {
const message = "`startupNodes` should contain at least one node.";
let pending = 2;
const cluster = new Cluster(null, {
lazyConnect: true,
clusterRetryStrategy(_, reason) {
expect(reason.message).to.eql(message);
if (!--pending) {
done();
}
return false;
},
});
cluster.connect().catch((err) => {
expect(err.message).to.eql(message);
cluster.disconnect();
if (!--pending) {
done();
}
});
});
describe("multiple reconnect", function () {
it("should reconnect after multiple consecutive disconnect(true) are called", function (done) {
new MockServer(30001);
const cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }], {
enableReadyCheck: false,
});
cluster.once("reconnecting", function () {
cluster.disconnect(true);
});
cluster.once("ready", function () {
cluster.disconnect(true);
const rejectTimeout = setTimeout(function () {
cluster.disconnect();
done(new Error("second disconnect(true) didn't reconnect redis"));
}, 1000);
process.nextTick(function () {
cluster.once("ready", function () {
clearTimeout(rejectTimeout);
cluster.disconnect();
done();
});
});
});
});
});
});
describe("cluster:disconnect", function () {
it("should clear the added script hashes interval when disconnecting", function (done) {
new MockServer(30001);
const cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }], {
enableReadyCheck: false,
});
cluster.once("ready", function () {
cluster.disconnect();
expect(cluster._addedScriptHashesCleanInterval).to.be.null;
done();
});
});
it("should clear the added script hashes interval when quitting", function (done) {
new MockServer(30001);
const cluster = new Cluster([{ host: "127.0.0.1", port: "30001" }], {
enableReadyCheck: false,
});
cluster.once("ready", function () {
cluster.quit();
expect(cluster._addedScriptHashesCleanInterval).to.be.null;
done();
});
});
it("should clear the added script hashes interval even when no connection succeeded", function (done) {
const cluster = new Cluster([{ host: "127.0.0.1", port: "0" }], {
enableReadyCheck: false,
});
let attempt = 0;
cluster.on("error", function () {
if(attempt < 5) {
attempt ++;
return
}
cluster.quit();
expect(cluster._addedScriptHashesCleanInterval).to.be.null;
done();
});
});
});