/
redis.ts
173 lines (147 loc) 路 5.55 KB
/
redis.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
import * as sinon from "sinon";
import { expect } from "chai";
import Redis from "../../lib/Redis";
describe("Redis", () => {
describe("constructor", () => {
it("should parse options correctly", () => {
const stub = sinon
.stub(Redis.prototype, "connect")
.returns(Promise.resolve());
let option;
try {
option = getOption();
expect(option).to.have.property("port", 6379);
expect(option).to.have.property("host", "localhost");
expect(option).to.have.property("family", 4);
option = getOption(6380);
expect(option).to.have.property("port", 6380);
expect(option).to.have.property("host", "localhost");
option = getOption("6380");
expect(option).to.have.property("port", 6380);
option = getOption(6381, "192.168.1.1");
expect(option).to.have.property("port", 6381);
expect(option).to.have.property("host", "192.168.1.1");
option = getOption(6381, "192.168.1.1", {
password: "123",
db: 2,
});
expect(option).to.have.property("port", 6381);
expect(option).to.have.property("host", "192.168.1.1");
expect(option).to.have.property("password", "123");
expect(option).to.have.property("db", 2);
option = getOption("redis://:authpassword@127.0.0.1:6380/4");
expect(option).to.have.property("port", 6380);
expect(option).to.have.property("host", "127.0.0.1");
expect(option).to.have.property("password", "authpassword");
expect(option).to.have.property("db", 4);
option = getOption("redis://:1+1@127.0.0.1:6380");
expect(option).to.have.property("password", "1+1");
option = getOption(
`redis://127.0.0.1:6380/?password=${encodeURIComponent("1+1")}`
);
expect(option).to.have.property("password", "1+1");
option = getOption("redis://127.0.0.1/");
expect(option).to.have.property("db", 0);
option = getOption("/tmp/redis.sock");
expect(option).to.have.property("path", "/tmp/redis.sock");
option = getOption({
port: 6380,
host: "192.168.1.1",
});
expect(option).to.have.property("port", 6380);
expect(option).to.have.property("host", "192.168.1.1");
option = getOption({
path: "/tmp/redis.sock",
});
expect(option).to.have.property("path", "/tmp/redis.sock");
option = getOption({
port: "6380",
});
expect(option).to.have.property("port", 6380);
option = getOption({
showFriendlyErrorStack: true,
});
expect(option).to.have.property("showFriendlyErrorStack", true);
option = getOption(6380, {
host: "192.168.1.1",
});
expect(option).to.have.property("port", 6380);
expect(option).to.have.property("host", "192.168.1.1");
option = getOption("6380", {
host: "192.168.1.1",
});
expect(option).to.have.property("port", 6380);
option = getOption("rediss://host");
expect(option).to.have.property("tls", true);
option = getOption("rediss://example.test", {
tls: { hostname: "example.test" },
});
expect(option.tls).to.deep.equal({ hostname: "example.test" });
option = getOption("redis://localhost?family=6");
expect(option).to.have.property("family", 6);
} catch (err) {
stub.restore();
throw err;
}
stub.restore();
function getOption(...args) {
// @ts-expect-error
const redis = new Redis(...args);
return redis.options;
}
});
it("should throw when arguments is invalid", () => {
expect(() => {
// @ts-expect-error
new Redis(() => {});
}).to.throw(Error);
});
});
describe(".createClient", () => {
it("should redirect to constructor", () => {
const redis = Redis.createClient({ name: "pass", lazyConnect: true });
expect(redis.options).to.have.property("name", "pass");
expect(redis.options).to.have.property("lazyConnect", true);
});
});
describe("#end", () => {
it("should redirect to #disconnect", (done) => {
const redis = new Redis({ lazyConnect: true });
const stub = sinon.stub(redis, "disconnect").callsFake(() => {
stub.restore();
done();
});
redis.end();
});
});
describe("#flushQueue", () => {
it("should flush all queues by default", () => {
const flushQueue = Redis.prototype.flushQueue;
const redis = {
offlineQueue: [{ command: { reject: () => {} } }],
commandQueue: [{ command: { reject: () => {} } }],
};
const offline = sinon.mock(redis.offlineQueue[0].command);
const command = sinon.mock(redis.commandQueue[0].command);
offline.expects("reject").once();
command.expects("reject").once();
flushQueue.call(redis);
offline.verify();
command.verify();
});
it("should be able to ignore a queue", () => {
const flushQueue = Redis.prototype.flushQueue;
const redis = {
offlineQueue: [{ command: { reject: () => {} } }],
commandQueue: [{ command: { reject: () => {} } }],
};
const offline = sinon.mock(redis.offlineQueue[0].command);
const command = sinon.mock(redis.commandQueue[0].command);
offline.expects("reject").once();
command.expects("reject").never();
flushQueue.call(redis, new Error(), { commandQueue: false });
offline.verify();
command.verify();
});
});
});