aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--test/bun.js/websocket-subprocess.ts12
-rw-r--r--test/bun.js/websocket.test.js53
2 files changed, 64 insertions, 1 deletions
diff --git a/test/bun.js/websocket-subprocess.ts b/test/bun.js/websocket-subprocess.ts
new file mode 100644
index 000000000..4700f75b7
--- /dev/null
+++ b/test/bun.js/websocket-subprocess.ts
@@ -0,0 +1,12 @@
+const hostname = process.argv[2];
+const port = process.argv[3];
+
+const host = port ? `http://${hostname}:${port}` : hostname;
+
+const ws = new WebSocket(host);
+
+ws.onmessage = (message) => {
+ if (message.data == "hello websocket") {
+ ws.send("hello");
+ }
+};
diff --git a/test/bun.js/websocket.test.js b/test/bun.js/websocket.test.js
index ff5362050..0d4a0ce06 100644
--- a/test/bun.js/websocket.test.js
+++ b/test/bun.js/websocket.test.js
@@ -1,5 +1,7 @@
import { describe, it, expect } from "bun:test";
-import { unsafe } from "bun";
+import { unsafe, spawn, readableStreamToText } from "bun";
+import { bunExe } from "bunExe";
+
import { gc } from "./gc";
const TEST_WEBSOCKET_HOST =
@@ -131,3 +133,52 @@ describe("WebSocket", () => {
gc(true);
});
});
+
+describe("websocket in subprocess", () => {
+ it("should exit", async () => {
+ let messageReceived = false;
+ const server = Bun.serve({
+ port: 8765,
+ fetch(req, server) {
+ if (server.upgrade(req)) {
+ return;
+ }
+
+ return new Response("http response");
+ },
+ websocket: {
+ open(ws) {
+ ws.send("hello websocket");
+ },
+ message(ws) {
+ messageReceived = true;
+ ws.close();
+ },
+ close(ws) {},
+ },
+ });
+ const subprocess = Bun.spawn({
+ cmd: [bunExe(), "websocket-subprocess.ts", server.hostname, server.port],
+ stderr: "pipe",
+ stdin: "pipe",
+ stdout: "pipe",
+ });
+
+ expect(await subprocess.exited).toBe(0);
+ expect(messageReceived).toBe(true);
+ server.stop();
+ });
+
+ it("should exit after killed", async () => {
+ const subprocess = Bun.spawn({
+ cmd: [bunExe(), "websocket-subprocess.ts", TEST_WEBSOCKET_HOST],
+ stderr: "pipe",
+ stdin: "pipe",
+ stdout: "pipe",
+ });
+
+ subprocess.kill();
+
+ expect(await subprocess.exited).toBe("SIGHUP");
+ });
+});