88 lines
1.9 KiB
TypeScript
88 lines
1.9 KiB
TypeScript
/// <reference lib="deno.ns" />
|
|
import { assertEquals } from "jsr:@std/assert";
|
|
import { startServer } from "./deno.ts";
|
|
|
|
const hostname = "0.0.0.0";
|
|
const port = 8042;
|
|
const href = `ws://${hostname}:${port}`;
|
|
|
|
function connect(url: string): Promise<[WebSocket, string[]]> {
|
|
return new Promise((resolve, reject) => {
|
|
const messages: string[] = [];
|
|
const socket = new WebSocket(url);
|
|
|
|
socket.onopen = () => resolve([socket, messages]);
|
|
socket.onerror = (error) => reject(error);
|
|
socket.onmessage = ({ data }) => {
|
|
messages.push(data);
|
|
};
|
|
});
|
|
}
|
|
|
|
function close(socket: WebSocket): Promise<void> {
|
|
return new Promise((resolve) => {
|
|
if (socket.readyState === WebSocket.CLOSED) {
|
|
return resolve();
|
|
}
|
|
socket.onclose = () => resolve();
|
|
socket.close();
|
|
});
|
|
}
|
|
|
|
function setup(
|
|
fn: (ctx: Deno.TestContext) => Promise<void>,
|
|
): (ctx: Deno.TestContext) => Promise<void> {
|
|
return async (ctx) => {
|
|
const shutdown = startServer(hostname, port);
|
|
try {
|
|
await fn(ctx);
|
|
} finally {
|
|
await shutdown();
|
|
}
|
|
};
|
|
}
|
|
|
|
function flush(): Promise<void> {
|
|
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
}
|
|
|
|
Deno.test(
|
|
"broadcasts a message on connect to all clients",
|
|
setup(async () => {
|
|
const [s1, m1] = await connect(href);
|
|
const [s2, m2] = await connect(href);
|
|
|
|
await flush();
|
|
|
|
assertEquals(m1, ["1 has joined!", "2 has joined!"]);
|
|
assertEquals(m2, ["2 has joined!"]);
|
|
|
|
await close(s2);
|
|
await close(s1);
|
|
}),
|
|
);
|
|
|
|
Deno.test(
|
|
"removes a client from broadcast after it disconnects",
|
|
setup(async () => {
|
|
const [s1, m1] = await connect(href);
|
|
const [s2, m2] = await connect(href);
|
|
const [s3, m3] = await connect(href);
|
|
|
|
await flush();
|
|
|
|
m1.length = 0;
|
|
m2.length = 0;
|
|
m3.length = 0;
|
|
|
|
await close(s3);
|
|
|
|
assertEquals(m1, ["3 has left!"]);
|
|
assertEquals(m2, ["3 has left!"]);
|
|
assertEquals(m3, []);
|
|
|
|
await close(s2);
|
|
await close(s1);
|
|
}),
|
|
);
|