-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
220 lines (178 loc) · 6.69 KB
/
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
import { assertEquals, assertRejects } from "@std/assert";
import { Buffer } from "@std/io/buffer";
import { type Command, RedisClient, type Reply } from "./mod.ts";
const encoder = new TextEncoder();
async function readReplyTest(output: string, expected: Reply, raw = false) {
const redisClient = new RedisClient(new Buffer(encoder.encode(output)));
const { value } = await redisClient.readReplies(raw).next();
assertEquals(value, expected);
}
function readReplyRejectTest(output: string, expected: string) {
const redisClient = new RedisClient(new Buffer(encoder.encode(output)));
return assertRejects(
() => redisClient.readReplies().next(),
expected,
);
}
Deno.test("readReply() - mixed array", () =>
readReplyTest("*3\r\n$5\r\nstring\r\n:123\r\n$-1", [
"string",
123,
null,
]));
Deno.test("readReply() - empty array", () => readReplyTest("*0\r\n", []));
Deno.test("readReply() - null array", () => readReplyTest("*-1\r\n", null));
Deno.test("readReply() - nested array", () =>
readReplyTest("*2\r\n*3\r\n:1\r\n$5\r\nhello\r\n:2\r\n#f\r\n", [[
1,
"hello",
2,
], false]));
Deno.test("readReply() - attribute", async () => {
await readReplyTest(
"|1\r\n+key-popularity\r\n%2\r\n$1\r\na\r\n,0.1923\r\n$1\r\nb\r\n,0.0012\r\n*2\r\n:2039123\r\n:9543892\r\n",
[2039123, 9543892],
);
await readReplyTest("*3\r\n:1\r\n:2\r\n|1\r\n+ttl\r\n:3600\r\n:3\r\n", [
1,
2,
3,
]);
});
Deno.test("readReply() - positive big number", () =>
readReplyTest(
"(3492890328409238509324850943850943825024385\r\n",
3492890328409238509324850943850943825024385n,
));
Deno.test("readReply() - negative big number", () =>
readReplyTest(
"(-3492890328409238509324850943850943825024385\r\n",
-3492890328409238509324850943850943825024385n,
));
Deno.test("readReply() - true boolean", () => readReplyTest("#t\r\n", true));
Deno.test("readReply() - false boolean", () => readReplyTest("#f\r\n", false));
Deno.test("readReply() - integer", () => readReplyTest(":42\r\n", 42));
Deno.test("readReply() - bulk string", () =>
readReplyTest("$5\r\nhello\r\n", "hello"));
Deno.test("readReply() - emtpy bulk string", () =>
readReplyTest("$0\r\n\r\n", ""));
Deno.test("readReply() - emtpy raw bulk string", () =>
readReplyTest("$0\r\n\r\n", new Uint8Array(), true));
Deno.test("readReply() - null bulk string", () =>
readReplyTest("$-1\r\n", null));
Deno.test("readReply() - blob error", async () => {
await readReplyRejectTest(
"!21\r\nSYNTAX invalid syntax\r\n",
"SYNTAX invalid syntax",
);
});
Deno.test("readReply() - error", async () => {
await readReplyRejectTest(
"-ERR this is the error description\r\n",
"ERR this is the error description",
);
});
Deno.test("readReply() - double", () => readReplyTest(",1.23\r\n", 1.23));
Deno.test("readReply() - positive infinity double", () =>
readReplyTest(",inf\r\n", Infinity));
Deno.test("readReply() - negative infinity double", () =>
readReplyTest(",-inf\r\n", -Infinity));
Deno.test("readReply() - map", () =>
readReplyTest("%2\r\n+first\r\n:1\r\n+second\r\n:2\r\n", {
first: 1,
second: 2,
}));
Deno.test("readReply() - null", () => readReplyTest("_\r\n", null));
Deno.test("readReply() - push", () =>
readReplyTest(
">4\r\n+pubsub\r\n+message\r\n+somechannel\r\n+this is the message\r\n",
["pubsub", "message", "somechannel", "this is the message"],
));
Deno.test("readReply() - set", () =>
readReplyTest(
"~5\r\n+orange\r\n+apple\r\n#t\r\n:100\r\n:999\r\n",
new Set(["orange", "apple", true, 100, 999]),
));
Deno.test("readReply() - simple string", () => readReplyTest("+OK\r\n", "OK"));
Deno.test("readReply() - verbatim string", () =>
readReplyTest("=15\r\ntxt:Some string\r\n", "txt:Some string"));
Deno.test("readReply() - large reply", async () => {
const reply = "a".repeat(4096 * 2);
await readReplyTest(`$${reply.length}\r\n${reply}\r\n`, reply);
});
const redisConn = await Deno.connect({ port: 6379 });
const redisClient = new RedisClient(redisConn);
await redisClient.sendCommand(["FLUSHALL"]);
async function sendCommandTest(command: Command, expected: Reply) {
assertEquals(await redisClient.sendCommand(command), expected);
}
Deno.test("RedisClient.sendCommand() - transactions", async () => {
await sendCommandTest(["MULTI"], "OK");
await sendCommandTest(["INCR", "FOO"], "QUEUED");
await sendCommandTest(["INCR", "BAR"], "QUEUED");
await sendCommandTest(["EXEC"], [1, 1]);
});
Deno.test("RedisClient.sendCommand() - raw data", async () => {
const data = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
assertEquals(await redisClient.sendCommand(["SET", "binary", data]), "OK");
assertEquals(await redisClient.sendCommand(["GET", "binary"], true), data);
});
Deno.test("RedisClient.sendCommand() - eval script", () =>
sendCommandTest(["EVAL", "return ARGV[1]", 0, "hello"], "hello"));
Deno.test("RedisClient.sendCommand() - Lua script", async () => {
await sendCommandTest([
"FUNCTION",
"LOAD",
"#!lua name=mylib\nredis.register_function('knockknock', function() return 'Who\\'s there?' end)",
], "mylib");
await sendCommandTest(["FCALL", "knockknock", 0], "Who's there?");
});
Deno.test("RedisClient.sendCommand() - RESP3", async () => {
await redisClient.sendCommand(["HELLO", 3]);
await sendCommandTest(["HSET", "hash3", "foo", 1, "bar", 2], 2);
await sendCommandTest(["HGETALL", "hash3"], {
foo: "1",
bar: "2",
});
});
Deno.test("RedisClient.sendCommand() - race condition (#146)", async () => {
await Promise.all(Array.from({ length: 20 }, async () => {
const key = crypto.randomUUID();
const value = crypto.randomUUID();
await redisClient.sendCommand(["SET", key, value]);
const result = await redisClient.sendCommand(["GET", key]);
assertEquals(result, value);
}));
});
Deno.test("RedisClient.pipelineCommands()", async () => {
assertEquals(
await redisClient.pipelineCommands([
["INCR", "X"],
["INCR", "X"],
["INCR", "X"],
["INCR", "X"],
]),
[1, 2, 3, 4],
);
});
Deno.test("RedisClient.writeCommand() + RedisClient.readReplies()", async () => {
await redisClient.writeCommand(["SUBSCRIBE", "mychannel"]);
const iterator = redisClient.readReplies();
assertEquals(await iterator.next(), {
value: ["subscribe", "mychannel", 1],
done: false,
});
await redisClient.writeCommand(["UNSUBSCRIBE"]);
assertEquals(await iterator.next(), {
value: ["unsubscribe", "mychannel", 0],
done: false,
});
});
Deno.test("RedisClient.sendCommand() - no reply", async () => {
await assertRejects(
async () => await redisClient.sendCommand(["SHUTDOWN"]),
TypeError,
"No reply received",
);
});
addEventListener("unload", () => redisConn.close());