mirror of
https://github.com/oven-sh/bun
synced 2026-02-16 22:01:47 +00:00
* WebSocketServer wrapper + socket.io initial support * fix up backpressure * fix up backpressure * fix http address * add socket.io tests * add closing tests * add connection state recovery tests for socket.io * add handshake test * add middeware tests for socket.io * added socket.io socket middleware tests * add more socket.io test comment/skip hang tests * add pending package for tests * add server attachment servers for socket.io * add utility-methods tests for socket.io * rename * rename * add messaging-many socket.io tests * add namespaces tests to socket.io * skip some tests * fmt * add packages to general package.json
138 lines
3.6 KiB
TypeScript
138 lines
3.6 KiB
TypeScript
// TODO: uncomment when Blob bug in isBinary is fixed
|
|
|
|
import { Server } from "socket.io";
|
|
import { describe, it, expect } from "bun:test";
|
|
|
|
import { success, fail, createClient } from "./support/util.ts";
|
|
|
|
describe("timeout", () => {
|
|
it.skip("should timeout if the client does not acknowledge the event", done => {
|
|
const io = new Server(0);
|
|
const client = createClient(io, "/");
|
|
try {
|
|
const timeout = setTimeout(() => {
|
|
fail(done, io, new Error("timeout"), client);
|
|
}, 200);
|
|
|
|
io.on("connection", socket => {
|
|
socket.timeout(50).emit("unknown", err => {
|
|
clearTimeout(timeout);
|
|
try {
|
|
expect(err).toBeInstanceOf(Error);
|
|
success(done, io, client);
|
|
} catch (err) {
|
|
fail(done, io, err, client);
|
|
}
|
|
});
|
|
});
|
|
} catch (err) {
|
|
fail(done, io, err, client);
|
|
}
|
|
});
|
|
|
|
it.skip("should timeout if the client does not acknowledge the event in time", done => {
|
|
const io = new Server(0);
|
|
const client = createClient(io, "/");
|
|
const timeout = setTimeout(() => {
|
|
fail(done, io, new Error("timeout"), client);
|
|
}, 500);
|
|
|
|
client.on("echo", (arg, cb) => {
|
|
cb(arg);
|
|
});
|
|
|
|
let count = 0;
|
|
|
|
io.on("connection", socket => {
|
|
socket.timeout(0).emit("echo", 42, err => {
|
|
try {
|
|
expect(err).toBeInstanceOf(Error);
|
|
count++;
|
|
} catch (err) {
|
|
clearTimeout(timeout);
|
|
fail(done, io, err, client);
|
|
}
|
|
});
|
|
});
|
|
|
|
setTimeout(() => {
|
|
clearTimeout(timeout);
|
|
try {
|
|
expect(count).toBe(1);
|
|
success(done, io, client);
|
|
} catch (err) {
|
|
fail(done, io, err, client);
|
|
}
|
|
}, 200);
|
|
});
|
|
|
|
it.skip("should not timeout if the client does acknowledge the event", done => {
|
|
const io = new Server(0);
|
|
const client = createClient(io, "/");
|
|
const timeout = setTimeout(() => {
|
|
fail(done, io, new Error("timeout"), client);
|
|
}, 200);
|
|
|
|
client.on("echo", (arg, cb) => {
|
|
cb(arg);
|
|
});
|
|
|
|
io.on("connection", socket => {
|
|
socket.timeout(50).emit("echo", 42, (err, value) => {
|
|
clearTimeout(timeout);
|
|
try {
|
|
expect(err).toBe(null);
|
|
expect(value).toBe(42);
|
|
success(done, io, client);
|
|
} catch (err) {
|
|
fail(done, io, err, client);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
it.skip("should timeout if the client does not acknowledge the event (promise)", done => {
|
|
const io = new Server(0);
|
|
const client = createClient(io, "/");
|
|
const timeout = setTimeout(() => {
|
|
fail(done, io, new Error("timeout"), client);
|
|
}, 200);
|
|
|
|
io.on("connection", async socket => {
|
|
try {
|
|
await socket.timeout(50).emitWithAck("unknown");
|
|
clearTimeout(timeout);
|
|
fail(done, io, new Error("timeout"), client);
|
|
} catch (err) {
|
|
clearTimeout(timeout);
|
|
expect(err).toBeInstanceOf(Error);
|
|
success(done, io, client);
|
|
}
|
|
});
|
|
});
|
|
|
|
it.skip("should not timeout if the client does acknowledge the event (promise)", done => {
|
|
const io = new Server(0);
|
|
const client = createClient(io, "/");
|
|
const timeout = setTimeout(() => {
|
|
fail(done, io, new Error("timeout"), client);
|
|
}, 200);
|
|
|
|
client.on("echo", (arg, cb) => {
|
|
cb(arg);
|
|
});
|
|
|
|
io.on("connection", async socket => {
|
|
try {
|
|
const value = await socket.timeout(50).emitWithAck("echo", 42);
|
|
clearTimeout(timeout);
|
|
expect(value).toBe(42);
|
|
success(done, io, client);
|
|
} catch (err) {
|
|
clearTimeout(timeout);
|
|
fail(done, io, err, client);
|
|
}
|
|
});
|
|
});
|
|
});
|