-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
redis_test.ts
240 lines (214 loc) · 6.58 KB
/
redis_test.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
import { connect, Redis } from "./redis.ts";
import {
assertEquals,
assertThrowsAsync,
assertArrayContains
} from "./vendor/https/deno.land/std/testing/asserts.ts";
const { test } = Deno;
// can be substituted with env variable
const addr = {
hostname: "127.0.0.1",
port: 6379
};
let redis: Redis;
test(async function beforeAll() {
redis = await connect(addr);
await redis.del(
"incr",
"incrby",
"decr",
"decryby",
"get",
"getset",
"del1",
"del2",
"spop",
"spopWithCount",
"zrange",
"zrangeWithScores",
"zrevrange",
"zrevrangeWithScores",
"zrangebyscore",
"zrangebyscoreWithScores",
"zrevrangebyscore",
"zrevrangebyscoreWithScores"
);
});
test(async function testExists() {
const none = await redis.exists("none", "none2");
assertEquals(none, 0);
await redis.set("exists", "aaa");
const exists = await redis.exists("exists", "none");
assertEquals(exists, 1);
});
test(async function testGetWhenNil() {
const hoge = await redis.get("none");
assertEquals(hoge, undefined);
});
test(async function testSet() {
const s = await redis.set("get", "fuga你好こんにちは");
assertEquals(s, "OK");
const fuga = await redis.get("get");
assertEquals(fuga, "fuga你好こんにちは");
});
test(async function testGetSet() {
await redis.set("getset", "val");
const v = await redis.getset("getset", "lav");
assertEquals(v, "val");
assertEquals(await redis.get("getset"), "lav");
});
test(async function testMget() {
await redis.set("mget1", "val1");
await redis.set("mget2", "val2");
await redis.set("mget3", "val3");
const v = await redis.mget("mget1", "mget2", "mget3");
assertEquals(v, ["val1", "val2", "val3"]);
});
test(async function testDel() {
let s = await redis.set("del1", "fuga");
assertEquals(s, "OK");
s = await redis.set("del2", "fugaaa");
assertEquals(s, "OK");
const deleted = await redis.del("del1", "del2");
assertEquals(deleted, 2);
});
test(async function testIncr() {
const rep = await redis.incr("incr");
assertEquals(rep, 1);
assertEquals(await redis.get("incr"), "1");
});
test(async function testIncrby() {
const rep = await redis.incrby("incrby", 101);
assertEquals(rep, 101);
assertEquals(await redis.get("incrby"), "101");
});
test(async function testDecr() {
const rep = await redis.decr("decr");
assertEquals(rep, -1);
assertEquals(await redis.get("decr"), "-1");
});
test(async function testDecrby() {
const rep = await redis.decrby("decryby", 101);
assertEquals(rep, -101);
assertEquals(await redis.get("decryby"), "-101");
});
test(async function testSpop() {
await redis.sadd("spop", "a");
const v = await redis.spop("spop");
assertEquals(v, "a");
});
test(async function testSpopWithCount() {
await redis.sadd("spopWithCount", "a", "b");
const v = await redis.spop("spopWithCount", 2);
assertArrayContains(v, ["a", "b"]);
});
test(async function testZrange() {
redis.zadd("zrange", 1, "one");
redis.zadd("zrange", 2, "two");
redis.zadd("zrange", 3, "three");
const v = await redis.zrange("zrange", 0, 1);
assertEquals(v, ["one", "two"]);
});
test(async function testZrangeWithScores() {
redis.zadd("zrangeWithScores", 1, "one");
redis.zadd("zrangeWithScores", 2, "two");
redis.zadd("zrangeWithScores", 3, "three");
const v = await redis.zrange("zrangeWithScores", 0, 1, { withScore: true });
assertEquals(v, ["one", "1", "two", "2"]);
});
test(async function testZrevrange() {
redis.zadd("zrevrange", 1, "one");
redis.zadd("zrevrange", 2, "two");
redis.zadd("zrevrange", 3, "three");
const v = await redis.zrevrange("zrevrange", 0, 1);
assertEquals(v, ["three", "two"]);
});
test(async function testZrevrangeWithScores() {
redis.zadd("zrevrangeWithScores", 1, "one");
redis.zadd("zrevrangeWithScores", 2, "two");
redis.zadd("zrevrangeWithScores", 3, "three");
const v = await redis.zrevrange("zrevrangeWithScores", 0, 1, {
withScore: true
});
assertEquals(v, ["three", "3", "two", "2"]);
});
test(async function testZrangebyscore() {
redis.zadd("zrangebyscore", 2, "m1");
redis.zadd("zrangebyscore", 5, "m2");
redis.zadd("zrangebyscore", 8, "m3");
redis.zadd("zrangebyscore", 10, "m4");
const v = await redis.zrangebyscore("zrangebyscore", 3, 9);
assertEquals(v, ["m2", "m3"]);
});
test(async function testZrangebyscoreWithScores() {
redis.zadd("zrangebyscoreWithScores", 2, "m1");
redis.zadd("zrangebyscoreWithScores", 5, "m2");
redis.zadd("zrangebyscoreWithScores", 8, "m3");
redis.zadd("zrangebyscoreWithScores", 10, "m4");
const v = await redis.zrangebyscore("zrangebyscoreWithScores", 3, 9, {
withScore: true
});
assertEquals(v, ["m2", "5", "m3", "8"]);
});
test(async function testZrevrangebyscore() {
redis.zadd("zrevrangebyscore", 2, "m1");
redis.zadd("zrevrangebyscore", 5, "m2");
redis.zadd("zrevrangebyscore", 8, "m3");
redis.zadd("zrevrangebyscore", 10, "m4");
const v = await redis.zrevrangebyscore("zrevrangebyscore", 9, 4);
assertEquals(v, ["m3", "m2"]);
});
test(async function testZrevrangebyscore() {
redis.zadd("zrevrangebyscoreWithScores", 2, "m1");
redis.zadd("zrevrangebyscoreWithScores", 5, "m2");
redis.zadd("zrevrangebyscoreWithScores", 8, "m3");
redis.zadd("zrevrangebyscoreWithScores", 10, "m4");
const v = await redis.zrevrangebyscore("zrevrangebyscoreWithScores", 9, 4, {
withScore: true
});
assertEquals(v, ["m3", "8", "m2", "5"]);
});
test(async function testConcurrent() {
let promises: Promise<any>[] = [];
for (const key of ["a", "b", "c"]) {
promises.push(redis.set(key, key));
}
await Promise.all(promises);
promises = [];
for (const key of ["a", "b", "c"]) {
promises.push(redis.get(key));
}
const [a, b, c] = await Promise.all(promises);
assertEquals(a, "a");
assertEquals(b, "b");
assertEquals(c, "c");
});
test(async function testDb0Option() {
const key = "exists";
await redis.set(key, "aaa");
const exists1 = await redis.exists(key);
assertEquals(exists1, 1);
const client2 = await connect({ ...addr, db: 0 });
const exists2 = await client2.exists(key);
assertEquals(exists2, 1);
});
test(async function testDb1Option() {
const key = "exists";
await redis.set(key, "aaa");
const exists1 = await redis.exists(key);
assertEquals(exists1, 1);
const client2 = await connect({ ...addr, db: 1 });
const exists2 = await client2.exists(key);
assertEquals(exists2, 0);
});
[Infinity, NaN, "", "port"].forEach(v => {
test(`invalid port: ${v}`, () => {
assertThrowsAsync(
async () => {
await connect({ hostname: "127.0.0.1", port: v });
},
Error,
"invalid"
);
});
});