-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
112 lines (91 loc) · 2.88 KB
/
index.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
import { noise } from "@chainsafe/libp2p-noise";
import { yamux } from "@chainsafe/libp2p-yamux";
import { tcp } from "@libp2p/tcp";
import { webSockets } from "@libp2p/websockets";
import { createLibp2p } from "libp2p";
import { describe, it } from "vitest";
export const delay = (ms: number) => {
return new Promise<void>((res, rej) => {
setTimeout(() => {
res();
}, ms);
});
};
async function createNode(ws?: boolean) {
let getTransports = () => [tcp()];
let listen = ["/ip4/127.0.0.1/tcp/0"];
if (ws === true) {
getTransports = () => [webSockets()];
listen[0] = listen[0] + "/ws";
}
const node = await createLibp2p<any>({
transports: getTransports(),
connectionEncrypters: [noise()],
streamMuxers: [yamux()],
addresses: {
listen,
},
});
return node;
}
describe("delay5", () => {
it("passes with 0 delay", async () => {
console.info("start");
const [node1, node2] = await Promise.all([createNode(), createNode()]);
console.info("dial");
node2.dial(node1.getMultiaddrs()).catch((e) => {}); // dont wait here
await delay(0);
console.info("stop");
await node1.stop();
console.log("start again");
await node1.start();
console.log("stop all");
await Promise.all([node1, node2].map((x) => x.stop()));
console.log("done");
});
it("passes with 5 delay", async () => {
console.info("start");
const [node1, node2] = await Promise.all([createNode(), createNode()]);
console.info("dial");
node2.dial(node1.getMultiaddrs()).catch((e) => {}); // dont wait here
await delay(5); // but put a little bit of delay
console.info("stop");
await node1.stop();
console.log("start again");
await node1.start();
console.log("stop all");
await Promise.all([node1, node2].map((x) => x.stop()));
console.log("done");
});
it("passes with 5 delay (webSockets)", async () => {
console.info("start");
const [node1, node2] = await Promise.all([
createNode(true),
createNode(true),
]);
console.info("dial");
node2.dial(node1.getMultiaddrs()).catch((e) => {}); // dont wait here
await delay(5); // but put a little bit of delay
console.info("stop");
await node1.stop();
console.log("start again");
await node1.start();
console.log("stop all");
await Promise.all([node1, node2].map((x) => x.stop()));
console.log("done");
});
it("passes with 100 delay", async () => {
console.info("start");
const [node1, node2] = await Promise.all([createNode(), createNode()]);
console.info("dial");
node2.dial(node1.getMultiaddrs()).catch((e) => {}); // dont wait here
await delay(100);
console.info("stop");
await node1.stop();
console.log("start again");
await node1.start();
console.log("stop all");
await Promise.all([node1, node2].map((x) => x.stop()));
console.log("done");
});
});