fix(net): harden wire lifecycle races

This commit is contained in:
2026-09-05 04:13:58 +08:00
parent a8873b9a84
commit 58a7a2dc68
3 changed files with 367 additions and 19 deletions
@@ -11,10 +11,16 @@ export class FakeTransport implements Transport {
private closeCb?: () => void;
private pending: Array<() => void> = [];
private closed = false;
private connectFailure: unknown = null;
connect(url: string): void {
this.connectCalls++;
this.url = url;
if (this.connectFailure !== null) {
const error = this.connectFailure;
this.connectFailure = null;
throw error;
}
this.pending.push(() => this.openCb?.());
}
send(frame: string): void { if (!this.closed) this.sent.push(frame); }
@@ -28,6 +34,9 @@ export class FakeTransport implements Transport {
onMessage(cb: (f: string) => void): void { this.msgCb = cb; }
onClose(cb: () => void): void { this.closeCb = cb; }
/** 测试侧:令下一次 connect 同步失败。 */
failNextConnect(error: unknown): void { this.connectFailure = error; }
/** 测试侧:模拟服务器推一帧给客户端。 */
serverPush(frame: string): void { this.pending.push(() => this.msgCb?.(frame)); }