-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
120 lines (103 loc) · 3.17 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
import { assertEquals, assertRejects } from "./deps.ts";
import {
type Command,
listenReplies,
pipelineCommands,
type Reply,
sendCommand,
sendCommandRawReply,
writeCommand,
} from "./mod.ts";
/** Default port for Redis. */
export const PORT = 6379;
const redisConn = await Deno.connect({ port: PORT });
async function sendCommandTest(
command: Command,
expected: Reply,
): Promise<void> {
assertEquals(await sendCommand(redisConn, command), expected);
}
Deno.test("sendCommand parses simple string", async () => {
await sendCommandTest(["PING"], "PONG");
});
Deno.test("sendCommand parses error", async () => {
await assertRejects(async () => await sendCommand(redisConn, ["helloworld"]));
});
Deno.test("sendCommand parses integer", async () => {
await sendCommandTest(["INCR", "integer"], 1);
await sendCommandTest(["INCR", "integer"], 2);
});
Deno.test("sendCommand parses bulk string", async () => {
await sendCommand(redisConn, ["SET", "big ups", "west side massive"]);
await sendCommandTest(["GET", "big ups"], "west side massive");
});
Deno.test("sendCommand parses null", async () => {
await sendCommandTest(["GET", "nonexistant"], null);
});
Deno.test("sendCommand parses array", async () => {
await sendCommand(redisConn, [
"HSET",
"hash",
"hello",
"world",
"integer",
13,
]);
await sendCommandTest([
"HMGET",
"hash",
"hello",
"integer",
"nonexistant",
], ["world", "13", null]);
});
Deno.test("sendCommand parses empty array", async () => {
await sendCommandTest(["HGETALL", "nonexistant"], []);
});
Deno.test("sendCommand parses null array", async () => {
await sendCommandTest(["BLPOP", "list", 1], null);
});
Deno.test("sendCommand works with transactions", async () => {
await sendCommandTest(["MULTI"], "OK");
await sendCommandTest(["INCR", "FOO"], "QUEUED");
await sendCommandTest(["INCR", "BAR"], "QUEUED");
await sendCommandTest(["EXEC"], [1, 1]);
});
Deno.test("sendCommandRawReply works", async () => {
const value = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
await sendCommandTest(["SET", "binary", value], "OK");
assertEquals(await sendCommandRawReply(redisConn, ["GET", "binary"]), value);
});
Deno.test("sendCommandRawReply throws on non-bulk-string reply", async () => {
await assertRejects(async () =>
await sendCommandRawReply(redisConn, ["PING"])
);
});
Deno.test("pipelineCommands works", async () => {
assertEquals(
await pipelineCommands(redisConn, [
["INCR", "X"],
["INCR", "X"],
["INCR", "X"],
["INCR", "X"],
]),
[1, 2, 3, 4],
);
});
Deno.test("listenReples works", async () => {
await writeCommand(redisConn, ["SUBSCRIBE", "mychannel"]);
const iterator = listenReplies(redisConn);
assertEquals(await iterator.next(), {
value: ["subscribe", "mychannel", 1],
done: false,
});
await writeCommand(redisConn, ["UNSUBSCRIBE"]);
assertEquals(await iterator.next(), {
value: ["unsubscribe", "mychannel", 0],
done: false,
});
});
Deno.test("any fn throws if no reply", async () => {
await assertRejects(async () => await sendCommand(redisConn, ["SHUTDOWN"]));
});
addEventListener("unload", () => redisConn.close());