-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.ts
More file actions
268 lines (223 loc) · 8.22 KB
/
test.ts
File metadata and controls
268 lines (223 loc) · 8.22 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
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
/// <reference lib="deno.ns" />
import { assertEquals } from "@std/assert/equals";
import { assertRejects } from "@std/assert/rejects";
import { type Command, RedisClient, RedisError, type Reply } from "./mod.ts";
const redisConn = await Deno.connect({ port: 6379 });
const redisClient = new RedisClient(redisConn);
function createConn(output: string) {
return {
readable: ReadableStream.from([output]).pipeThrough(
new TextEncoderStream(),
),
writable: new WritableStream(),
};
}
async function assertSendCommandEquals(command: Command, expected: Reply) {
const actual = await redisClient.sendCommand(command);
assertEquals(actual, expected);
}
async function assertReadReplyEquals<T extends Reply>(
output: string,
expected: T,
raw = false,
) {
const redisClient = new RedisClient(createConn(output));
const { value } = await redisClient.readReplies<T>(raw).next();
assertEquals<T>(value, expected);
}
function assertReadReplyRejects(output: string, expectedMsg: string) {
const redisClient = new RedisClient(createConn(output));
return assertRejects(
() => redisClient.readReplies().next(),
RedisError,
expectedMsg,
);
}
Deno.test("readReply() - mixed array", () =>
assertReadReplyEquals("*3\r\n$5\r\nstring\r\n:123\r\n$-1\r\n", [
"string",
123,
null,
]));
Deno.test("readReply() - empty array", () =>
assertReadReplyEquals("*0\r\n", []));
Deno.test("readReply() - null array", () =>
assertReadReplyEquals("*-1\r\n", null));
Deno.test("readReply() - nested array", () =>
assertReadReplyEquals("*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 assertReadReplyEquals(
"|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 assertReadReplyEquals(
"*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", () =>
assertReadReplyEquals(
"(3492890328409238509324850943850943825024385\r\n",
3492890328409238509324850943850943825024385n,
));
Deno.test("readReply() - negative big number", () =>
assertReadReplyEquals(
"(-3492890328409238509324850943850943825024385\r\n",
-3492890328409238509324850943850943825024385n,
));
Deno.test("readReply() - true boolean", () =>
assertReadReplyEquals("#t\r\n", true));
Deno.test("readReply() - false boolean", () =>
assertReadReplyEquals("#f\r\n", false));
Deno.test("readReply() - integer", () => assertReadReplyEquals(":42\r\n", 42));
Deno.test("readReply() - bulk string", () =>
assertReadReplyEquals("$5\r\nhello\r\n", "hello"));
Deno.test("readReply() - bulk string containing CRLF (known issue)", () =>
assertReadReplyEquals("$7\r\nhello\r\n\r\n", "hello"));
Deno.test("readReply() - emtpy bulk string", () =>
assertReadReplyEquals(
"%2\r\n$5\r\nempty\r\n$0\r\n\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",
{ empty: "", foo: "bar" },
));
Deno.test("readReply() - emtpy raw bulk string", () =>
assertReadReplyEquals("$0\r\n\r\n", new Uint8Array(), true));
Deno.test("readReply() - null bulk string", () =>
assertReadReplyEquals("$-1\r\n", null));
Deno.test("readReply() - blob error", async () => {
await assertReadReplyRejects(
"!21\r\nSYNTAX invalid syntax\r\n",
"SYNTAX invalid syntax",
);
});
Deno.test("readReply() - error", async () => {
await assertReadReplyRejects(
"-ERR this is the error description\r\n",
"ERR this is the error description",
);
});
Deno.test("readReply() - double", () =>
assertReadReplyEquals(",1.23\r\n", 1.23));
Deno.test("readReply() - positive infinity double", () =>
assertReadReplyEquals(",inf\r\n", Infinity));
Deno.test("readReply() - negative infinity double", () =>
assertReadReplyEquals(",-inf\r\n", -Infinity));
Deno.test("readReply() - map", () =>
assertReadReplyEquals("%2\r\n+first\r\n:1\r\n+second\r\n:2\r\n", {
first: 1,
second: 2,
}));
Deno.test("readReply() - null", () => assertReadReplyEquals("_\r\n", null));
Deno.test("readReply() - push", () =>
assertReadReplyEquals(
">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", () =>
assertReadReplyEquals(
"~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", () =>
assertReadReplyEquals("+OK\r\n", "OK"));
Deno.test("readReply() - verbatim string", () =>
assertReadReplyEquals("=15\r\ntxt:Some string\r\n", "txt:Some string"));
Deno.test("readReply() - large reply", async () => {
const reply = "a".repeat(4096 * 2);
await assertReadReplyEquals(`$${reply.length}\r\n${reply}\r\n`, reply);
});
Deno.test("RedisClient.sendCommand() - transactions", async () => {
await assertSendCommandEquals(["MULTI"], "OK");
await assertSendCommandEquals(["INCR", "FOO"], "QUEUED");
await assertSendCommandEquals(["INCR", "BAR"], "QUEUED");
await assertSendCommandEquals(["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", () =>
assertSendCommandEquals(["EVAL", "return ARGV[1]", 0, "hello"], "hello"));
Deno.test("RedisClient.sendCommand() - Lua script", async () => {
await assertSendCommandEquals([
"FUNCTION",
"LOAD",
"#!lua name=mylib\nredis.register_function('knockknock', function() return 'Who\\'s there?' end)",
], "mylib");
await assertSendCommandEquals(["FCALL", "knockknock", 0], "Who's there?");
});
Deno.test("RedisClient.sendCommand() - RESP3", async () => {
await redisClient.sendCommand(["HELLO", 3]);
await assertSendCommandEquals(["HSET", "hash3", "foo", 1, "bar", 2], 2);
await assertSendCommandEquals(["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() - error recovery", async () => {
// Send an invalid command that will cause an error
await assertRejects(
() => redisClient.sendCommand(["INVALIDCOMMAND", "arg1"]),
RedisError,
"ERR unknown command 'INVALIDCOMMAND', with args beginning with: 'arg1' ",
);
// Subsequent commands should still work
await assertSendCommandEquals(["SET", "test-key", "test-value"], "OK");
await assertSendCommandEquals(["GET", "test-key"], "test-value");
// Another error should also be handled correctly
await assertRejects(
() => redisClient.sendCommand(["YETANOTHERBADCMD"]),
RedisError,
"ERR unknown command 'YETANOTHERBADCMD', with args beginning with: ",
);
// And subsequent commands should still work
await assertSendCommandEquals(["DEL", "test-key"], 1);
});
Deno.test("RedisError", () => {
const message = crypto.randomUUID();
const cause = crypto.randomUUID();
const error = new RedisError(message, { cause });
assertEquals(error.message, message);
assertEquals(error.cause, cause);
});