Let a listener's teardown run last and answer the sends the tests left hanging

This commit is contained in:
2026-08-30 22:03:23 +02:00
parent 427565ed3e
commit 685495f534
7 changed files with 41 additions and 29 deletions
+10 -4
View File
@@ -9,13 +9,19 @@ export function closeAfter(t: TestContext, closable: Closable): void {
t.after(() => closable.close({ signal: AbortSignal.abort() }));
}
/** A listener with a socket still on it never closes, so the sockets go first. */
export function endListenerAfter(t: TestContext, listener: Server, sockets: Socket[]): void {
/**
* Stops a listener, given every socket it accepted — one still open and it never closes. The wait is
* bounded because a caller that misses one must not hang the net itself.
*/
export function closeListenerAfter(t: TestContext, listener: Server, accepted: Socket[]): void {
t.after(async () => {
for (const sock of sockets) {
for (const sock of accepted) {
sock.destroy();
}
await new Promise<void>(resolve => { listener.close(() => { resolve(); }); });
await Promise.race([
new Promise<void>(resolve => { listener.close(() => { resolve(); }); }),
new Promise<void>(resolve => { setTimeout(resolve, 1000).unref(); }),
]);
});
}