Skip to content

Commit c7c7000

Browse files
Seulgi Kimmergify[bot]
authored andcommitted
Add tests for IPC
1 parent 4a772ce commit c7c7000

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

test/src/e2e/ipc.test.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2019 Kodebox, Inc.
2+
// This file is part of CodeChain.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as
6+
// published by the Free Software Foundation, either version 3 of the
7+
// License, or (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
import { expect } from "chai";
18+
import * as _ from "lodash";
19+
import "mocha";
20+
import * as net from "net";
21+
import CodeChain from "../helper/spawn";
22+
23+
describe("ipc", function() {
24+
let node: CodeChain;
25+
beforeEach(async function() {
26+
node = new CodeChain();
27+
await node.start({ disableIpc: false });
28+
});
29+
30+
it("ping", async function() {
31+
const id = 1;
32+
const socket = await createConnection(id, node.ipcPath);
33+
sendPing(socket, id);
34+
await waitPong(socket, id);
35+
});
36+
37+
it("multiple connections at the same time in the random order", async function() {
38+
const ids = _.shuffle([1, 2, 3, 4, 5]);
39+
40+
const socket0 = await createConnection(ids[0], node.ipcPath);
41+
const socket1 = await createConnection(ids[1], node.ipcPath);
42+
const socket2 = await createConnection(ids[2], node.ipcPath);
43+
const socket3 = await createConnection(ids[3], node.ipcPath);
44+
const socket4 = await createConnection(ids[4], node.ipcPath);
45+
46+
const sockets: [net.Socket, number][] = [
47+
[socket0, ids[0]],
48+
[socket1, ids[1]],
49+
[socket2, ids[2]],
50+
[socket3, ids[3]],
51+
[socket4, ids[4]]
52+
];
53+
for (const [socket, id] of _.shuffle(sockets)) {
54+
sendPing(socket, id);
55+
}
56+
await Promise.all(
57+
_.shuffle(sockets).map(([socket, id]) => waitPong(socket, id))
58+
);
59+
});
60+
61+
afterEach(async function() {
62+
if (this.currentTest!.state === "failed") {
63+
node.testFailed(this.currentTest!.fullTitle());
64+
}
65+
await node.clean();
66+
});
67+
});
68+
69+
function createConnection(id: number, ipcPath: string): Promise<net.Socket> {
70+
return new Promise((resolve, reject) => {
71+
const socket = net.createConnection(ipcPath).on("error", reject);
72+
socket.on("connect", () => resolve(socket));
73+
});
74+
}
75+
76+
const jsonrpc = "2.0";
77+
78+
function sendPing(socket: net.Socket, id: number): void {
79+
const req = { jsonrpc, method: "ping", params: [], id };
80+
socket.write(JSON.stringify(req));
81+
}
82+
83+
function waitPong(socket: net.Socket, id: number): Promise<void> {
84+
return new Promise((resolve, reject) => {
85+
socket.on("data", data => {
86+
const s = data.toString();
87+
let response: { id: number; jsonrpc: string; result: any };
88+
try {
89+
response = JSON.parse(s.toString());
90+
} catch (err) {
91+
reject(Error(s.toString()));
92+
return;
93+
}
94+
expect(response.id).be.equal(id);
95+
expect(response.jsonrpc).be.equal(jsonrpc);
96+
expect(response.result).be.equal("pong");
97+
resolve();
98+
});
99+
});
100+
}

0 commit comments

Comments
 (0)