-
Notifications
You must be signed in to change notification settings - Fork 5
/
mock.mjs
92 lines (83 loc) · 2.5 KB
/
mock.mjs
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
import WebSocket from "ws";
const logs = {};
const log = (testCase, message, extra) => {
if (!logs[testCase]) {
logs[testCase] = [];
}
logs[testCase].push([Date.now(), message, extra]);
};
const testCaseSetup = {};
const reqCount = {};
const wss = new WebSocket.Server({
port: 4752,
host: "0.0.0.0",
verifyClient: (info, done) => {
if (info.req.url === "/supervisor") {
return done(true);
}
const testCase = info.req.url.slice(1);
log(testCase, "connect");
const setup = testCaseSetup[testCase] && testCaseSetup[testCase][reqCount[testCase] || 0];
if (setup) {
if (setup.delay) {
setTimeout(() => {
done(!setup.fail);
}, setup.delay);
} else {
done(!setup.fail);
}
if (reqCount[testCase]) {
reqCount[testCase] = reqCount[testCase] + 1;
} else {
reqCount[testCase] = 1;
}
} else {
done(true);
}
},
});
wss.on('connection', (ws, request) => {
if (request.url === "/supervisor") {
ws.on('message', (message) => {
let data = JSON.parse(message);
const method = data.method;
if (method === "logs") {
ws.send(JSON.stringify({
rpc: data["rpc"],
data: logs[data.args.testCase]
}));
}
else if (method === "setup") {
testCaseSetup[data.args.testCase] = data.args.setup;
ws.send(JSON.stringify({
rpc: data["rpc"]
}));
}
});
} else {
const testCase = request.url.slice(1);
ws.on('close', (code, reason) => {
log(testCase, "close", [code, reason]);
});
ws.on('message', (message) => {
log(testCase, message);
if (message === "ping") {
try {
ws.send("pong");
} catch (e) {
}
} else if (message === "disconnect") {
try {
setTimeout(() => ws.terminate(), 100);
} catch (e) {
}
} else if (message.substr(4) === "ping") {
ws.send(message.replace("ping", "pong"));
}
try {
ws.send("hello");
} catch (e) {
}
});
}
});