Skip to content

Commit

Permalink
chore(core-p2p): terminate on ws.ping() ws.pong() (#4194)
Browse files Browse the repository at this point in the history
  • Loading branch information
air1one authored Nov 24, 2020
1 parent 21eb71a commit 9f9178d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
33 changes: 33 additions & 0 deletions __tests__/unit/core-p2p/hapi-nes/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1004,5 +1004,38 @@ describe("Client", () => {
await server.stop();
});
});

describe("ping / pong", () => {
it.each([["ping"], ["pong"]])("terminates when receiving a ws.%s", async (method) => {
const server = await createServerWithPlugin({}, {}, true);

server.route({
method: "POST",
path: "/",
handler: async (request) => {
request.server.plugins.nes._listener._sockets._forEach((socket) => {
setTimeout(() => socket._ws[method](), 100);
});

return "hello";
},
});

await server.start();
const client = new Client("http://localhost:" + server.info.port);

await client.connect();

await client.request("/");

await Hoek.wait(500);

//@ts-ignore
expect(client._ws).toBeNull(); // null because _cleanup() in reconnect() method

await client.disconnect();
await server.stop();
});
})
});
});
29 changes: 29 additions & 0 deletions __tests__/unit/core-p2p/hapi-nes/socket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,35 @@ describe("Socket", () => {
client.close();
await server.stop();
});

it.each([["ping"], ["pong"]])("terminates on ws.%s", async (method) => {
const server = Hapi.server();
await server.register({ plugin: plugin, options: {} });

server.route({
method: "POST",
path: "/",
handler: () => "hello",
});

await server.start();
const client = new Ws("http://localhost:" + server.info.port);
client.onerror = Hoek.ignore;

const sendPingOrPong = async () => new Promise((resolve, reject) => {
client.on("open", () => {
client[method]("", true, () => resolve());
});
})

await sendPingOrPong();
await delay(1000);

expect(client.readyState).toEqual(client.CLOSED);

client.close();
await server.stop();
});
});

describe("_processRequest()", () => {
Expand Down
12 changes: 10 additions & 2 deletions packages/core-p2p/src/hapi-nes/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,12 @@ export class Client {
ws.onmessage = (message) => {
return this._onMessage(message);
};

ws.on("ping", () => this._disconnect(() => {}, true, true));
ws.on("pong", () => this._disconnect(() => {}, true, true));
}

private _disconnect(next, isInternal) {
private _disconnect(next, isInternal, terminate = false) {
this._reconnection = null;
clearTimeout(this._reconnectionTimer);
this._reconnectionTimer = null;
Expand All @@ -302,7 +305,12 @@ export class Client {

this._disconnectRequested = requested;
this._disconnectListeners = [next];
this._ws.close();

if (terminate) {
this._ws.terminate();
} else {
this._ws.close();
}
}

private _cleanup() {
Expand Down
2 changes: 2 additions & 0 deletions packages/core-p2p/src/hapi-nes/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export class Socket {
};

this._ws.on("message", (message) => this._onMessage(message));
this._ws.on("ping", () => this.terminate());
this._ws.on("pong", () => this.terminate());
}

public disconnect() {
Expand Down

0 comments on commit 9f9178d

Please sign in to comment.