1
0
mirror of https://github.com/coder/code-server.git synced 2024-12-05 07:13:06 +08:00
code-server/packages/protocol/test/net.test.ts
Asher dc2253e718 Refactor evaluations (#285)
* Replace evaluations with proxies and messages

* Return proxies synchronously

Otherwise events can be lost.

* Ensure events cannot be missed

* Refactor remaining fills

* Use more up-to-date version of util

For callbackify.

* Wait for dispose to come back before removing

This prevents issues with the "done" event not always being the last
event fired. For example a socket might close and then end, but only
if the caller called end.

* Remove old node-pty tests

* Fix emitting events twice on duplex streams

* Preserve environment when spawning processes

* Throw a better error if the proxy doesn't exist

* Remove rimraf dependency from ide

* Update net.Server.listening

* Use exit event instead of killed

Doesn't look like killed is even a thing.

* Add response timeout to server

* Fix trash

* Require node-pty & spdlog after they get unpackaged

This fixes an error when running in the binary.

* Fix errors in down emitter preventing reconnecting

* Fix disposing proxies when nothing listens to "error" event

* Refactor event tests to use jest.fn()

* Reject proxy call when disconnected

Otherwise it'll wait for the timeout which is a waste of time since we
already know the connection is dead.

* Use nbin for binary packaging

* Remove additional module requires

* Attempt to remove require for local bootstrap-fork

* Externalize fsevents
2019-03-26 13:01:25 -05:00

146 lines
3.3 KiB
TypeScript

import * as nativeNet from "net";
import { Module } from "../src/common/proxy";
import { createClient, Helper } from "./helpers";
describe("net", () => {
const client = createClient();
const net = client.modules[Module.Net];
const helper = new Helper("net");
beforeAll(async () => {
await helper.prepare();
});
describe("Socket", () => {
const socketPath = helper.tmpFile();
let server: nativeNet.Server;
beforeAll(async () => {
await new Promise((r): void => {
server = nativeNet.createServer().listen(socketPath, r);
});
});
afterAll(() => {
server.close();
});
it("should fail to connect", async () => {
const socket = new net.Socket();
const fn = jest.fn();
socket.on("error", fn);
socket.connect("/tmp/t/e/s/t/d/o/e/s/n/o/t/e/x/i/s/t");
await new Promise((r): nativeNet.Socket => socket.on("close", r));
expect(fn).toHaveBeenCalledTimes(1);
});
it("should connect", async () => {
await new Promise((resolve): void => {
const socket = net.createConnection(socketPath, () => {
socket.end();
socket.addListener("close", () => {
resolve();
});
});
});
await new Promise((resolve): void => {
const socket = new net.Socket();
socket.connect(socketPath, () => {
socket.end();
socket.addListener("close", () => {
resolve();
});
});
});
});
it("should get data", (done) => {
server.once("connection", (socket: nativeNet.Socket) => {
socket.write("hi how r u");
});
const socket = net.createConnection(socketPath);
socket.addListener("data", (data) => {
expect(data.toString()).toEqual("hi how r u");
socket.end();
socket.addListener("close", () => {
done();
});
});
});
it("should send data", (done) => {
const clientSocket = net.createConnection(socketPath);
clientSocket.write(Buffer.from("bananas"));
server.once("connection", (socket: nativeNet.Socket) => {
socket.addListener("data", (data) => {
expect(data.toString()).toEqual("bananas");
socket.end();
clientSocket.addListener("end", () => {
done();
});
});
});
});
});
describe("Server", () => {
it("should listen", (done) => {
const s = net.createServer();
s.on("listening", () => s.close());
s.on("close", () => done());
s.listen(helper.tmpFile());
});
it("should get connection", async () => {
let constructorListener: (() => void) | undefined;
const s = net.createServer(() => {
if (constructorListener) {
constructorListener();
}
});
const socketPath = helper.tmpFile();
s.listen(socketPath);
await new Promise((resolve): void => {
s.on("listening", resolve);
});
const makeConnection = async (): Promise<void> => {
net.createConnection(socketPath);
await Promise.all([
new Promise((resolve): void => {
constructorListener = resolve;
}),
new Promise((resolve): void => {
s.once("connection", (socket) => {
socket.destroy();
resolve();
});
}),
]);
};
await makeConnection();
await makeConnection();
s.close();
await new Promise((r): nativeNet.Server => s.on("close", r));
});
});
it("should dispose", (done) => {
setTimeout(() => {
client.dispose();
done();
}, 100);
});
});