-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathpubsub_test.ts
73 lines (68 loc) · 1.85 KB
/
pubsub_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
import { test } from "https://deno.land/[email protected]/testing/mod.ts";
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { connect } from "./redis.ts";
import { RedisPubSubMessage } from "./pubsub.ts";
const addr = "127.0.0.1:6379";
async function wait(duration) {
return new Promise(resolve => {
setTimeout(resolve, duration);
});
}
test(async function testSubscribe() {
const redis = await connect(addr);
const sub = await redis.subscribe("subsc");
//const hoge = await redis.get("hoge");
const unsub = await sub.unsubscribe("subsc");
await sub.close();
assertEquals(sub.isClosed, true);
redis.close();
});
test(async function testSubscribe2() {
const redis = await connect(addr);
const pub = await connect(addr);
const sub = await redis.subscribe("subsc2");
let message: RedisPubSubMessage;
const p = (async function() {
const it = sub.receive();
message = (await it.next()).value;
})();
await pub.publish("subsc2", "wayway");
await p;
assertEquals(message, {
channel: "subsc2",
message: "wayway"
});
await sub.close();
const a = await redis.get("aaa");
assertEquals(a, void 0);
pub.close();
redis.close();
});
test(async function testPsubscribe() {
const redis = await connect(addr);
const pub = await connect(addr);
const sub = await redis.psubscribe("ps*");
let message1;
let message2;
const it = sub.receive();
const p = (async function() {
message1 = (await it.next()).value;
message2 = (await it.next()).value;
})();
await pub.publish("psub", "wayway");
await pub.publish("psubs", "heyhey");
await p;
assertEquals(message1, {
pattern: "ps*",
channel: "psub",
message: "wayway"
});
assertEquals(message2, {
pattern: "ps*",
channel: "psubs",
message: "heyhey"
});
await sub.close();
pub.close();
redis.close();
});