window-warmup: the fake gateway's reset no longer logs "error: 连接中断" (#902)

The "响应中途连接重置" test's fake gateway broke the connection with
controller.error(new Error("连接中断")) on a served ReadableStream. Bun.serve
prints such a stream error as an unhandled error (its error() callback is
never called), so every green run of the package showed "error: 连接中断"
with a stack, which read as a failing test. It was not the bun version, HOME
or a port: a 20-line probe on bun 1.4.2 printed the same for any
controller.error or throwing pull() in a served stream, with an error()
handler set and seeing nothing.

The reset is now a forced close: the fake records the request, then
server.stop(true) closes the connection before any answer, which the client
sees as ECONNRESET (the same path as before), and nothing is logged. The
truncate case keeps its half body and clean close. Test-only: no version
bump.

Run on mote (bun 1.4.2, HOME=~/runs/home-902):
- origin/main 36141e38, bun test tests/gateway.test.ts, 3 of 3: 63 pass,
  0 fail, one "error: 连接中断" logged each run.
- this commit, same, 3 of 3: 63 pass, 0 fail, nothing logged; whole
  package 279 pass, 1 skip, 0 fail, nothing logged.
- Broken by hand: with the ECONNRESET/EPIPE branch of send() removed, the
  reset test is red (62 pass, 1 fail), so it still reaches that path.
Not run yet: the full root bun test at this commit, queued under
~/suite.lock behind other sessions' suites; its result goes on #902.
This commit is contained in:
yetone
2026-11-09 18:54:29 +08:00
parent 36141e381e
commit 951ac55c94
+11 -3
View File
@@ -13,7 +13,12 @@ export interface FakeChatReply {
body?: unknown;
/** 延迟应答毫秒数,用于模拟超时与外部取消 */
delayMs?: number;
/** 模拟 2xx 响应体不完整:truncate=半截 JSON;reset=流中途断开(客户端收到 ECONNRESET) */
/**
* 模拟 2xx 响应体不完整:truncate=半截 JSON;reset=请求已收到、应答前网关掉线
* (连接被强制关闭,客户端收到 ECONNRESET;此后假网关不再应答)。
* reset 不用 ReadableStream 的 controller.error:Bun.serve 会把它当未处理错误打印
* (error 回调收不到),测试全绿时日志里也出现「error: 连接中断」(#902)。
*/
breakBody?: "truncate" | "reset";
}
@@ -105,12 +110,15 @@ export function fakeGateway(opts: FakeGatewayOpts = {}): FakeGateway {
const conf =
typeof opts.chat === "function" ? await opts.chat(entry.body, req) : (opts.chat ?? {});
if (conf.delayMs) await Bun.sleep(conf.delayMs);
if (conf.breakBody === "reset") {
queueMicrotask(() => server.stop(true));
return new Promise<Response>(() => {});
}
if (conf.breakBody) {
const stream = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(new TextEncoder().encode('{"id":"chatcmpl-fake"'));
if (conf.breakBody === "reset") controller.error(new Error("连接中断"));
else controller.close();
controller.close();
},
});
return new Response(stream, {