1
0
mirror of https://git.tuxpa.in/a/code-server.git synced 2024-12-28 21:25:25 +00:00
code-server-2/packages/protocol/test/child_process.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

99 lines
2.5 KiB
TypeScript

import { ChildProcess } from "child_process";
import * as path from "path";
import { Readable } from "stream";
import * as util from "util";
import { createClient } from "@coder/protocol/test";
import { Module } from "../src/common/proxy";
describe("child_process", () => {
const client = createClient();
const cp = client.modules[Module.ChildProcess];
const getStdout = async (proc: ChildProcess): Promise<string> => {
return new Promise((r): Readable => proc.stdout.on("data", r))
.then((s) => s.toString());
};
describe("exec", () => {
it("should get exec stdout", async () => {
await expect(util.promisify(cp.exec)("echo test", { encoding: "utf8" }))
.resolves.toEqual({
stdout: "test\n",
stderr: "",
});
});
});
describe("spawn", () => {
it("should get spawn stdout", async () => {
const proc = cp.spawn("echo", ["test"]);
await expect(Promise.all([
getStdout(proc),
new Promise((r): ChildProcess => proc.on("exit", r)),
]).then((values) => values[0])).resolves.toEqual("test\n");
});
it("should cat", async () => {
const proc = cp.spawn("cat", []);
expect(proc.pid).toBe(-1);
proc.stdin.write("banana");
await expect(getStdout(proc)).resolves.toBe("banana");
proc.stdin.end();
proc.kill();
expect(proc.pid).toBeGreaterThan(-1);
await new Promise((r): ChildProcess => proc.on("exit", r));
});
it("should print env", async () => {
const proc = cp.spawn("env", [], {
env: { hi: "donkey" },
});
await expect(getStdout(proc)).resolves.toContain("hi=donkey\n");
});
});
describe("fork", () => {
it("should echo messages", async () => {
const proc = cp.fork(path.join(__dirname, "forker.js"));
proc.send({ bananas: true });
await expect(new Promise((r): ChildProcess => proc.on("message", r)))
.resolves.toMatchObject({
bananas: true,
});
proc.kill();
await new Promise((r): ChildProcess => proc.on("exit", r));
});
});
it("should dispose", (done) => {
setTimeout(() => {
client.dispose();
done();
}, 100);
});
it("should disconnect", async () => {
const client = createClient();
const cp = client.modules[Module.ChildProcess];
const proc = cp.fork(path.join(__dirname, "forker.js"));
const fn = jest.fn();
proc.on("error", fn);
proc.send({ bananas: true });
await expect(new Promise((r): ChildProcess => proc.on("message", r)))
.resolves.toMatchObject({
bananas: true,
});
client.dispose();
expect(fn).toHaveBeenCalledWith(new Error("disconnected"));
});
});