diff --git a/packages/cultmesh-ts/test/realtime-quic-consumer.test.ts b/packages/cultmesh-ts/test/realtime-quic-consumer.test.ts index 6864d764..97be7891 100644 --- a/packages/cultmesh-ts/test/realtime-quic-consumer.test.ts +++ b/packages/cultmesh-ts/test/realtime-quic-consumer.test.ts @@ -1034,17 +1034,29 @@ test("P8: a reliable send still queued behind reliableSendTail rejects instead o }; const connector = new CultMeshQuicRealtimeConnector(); const transport = await connector.connect(candidate, target); - // `sendFrame` chains onto `reliableSendTail`, which resolves as a - // microtask; calling `dispose()` synchronously right after, in the same - // turn, guarantees `cleanup()` has already set `disposed = true` by the - // time that microtask runs `sendOnStream`. Without the `this.disposed` - // guard `sendOnStream` registers (P8's target), this send would sit in - // `pendingSends` forever: `cleanup()`'s reject sweep already ran once - // before this send was ever registered, and no later native event can - // reach it either, since `offConnectionEvent` already ran too. - const send = transport.sendFrame(testFrame({ delivery: "reliable-ordered" })); - transport.dispose(); - await assert.rejects(send, /disposed/i); + try { + // Prime the reliable outbound stream and let it fully settle, so its + // id is already cached: a second reliable send then chains straight + // onto `sendOnStream` with no `streamOpen` call of its own in between + // (a `streamOpen` against an already-shut-down connection fails with + // its own native error, which would mask the guard this test targets). + await transport.sendFrame(testFrame({ delivery: "reliable-ordered", sequence: 1n })); + + // `sendFrame` chains onto `reliableSendTail`, which resolves as a + // microtask; calling `dispose()` synchronously right after, in the + // same turn, guarantees `cleanup()` has already set `disposed = true` + // by the time that microtask runs `sendOnStream`. Without the + // `this.disposed` guard `sendOnStream` registers (P8's target), this + // send would sit in `pendingSends` forever: `cleanup()`'s reject sweep + // already ran once before this send was ever registered, and no later + // native event can reach it either, since `offConnectionEvent` already + // ran too. + const send = transport.sendFrame(testFrame({ delivery: "reliable-ordered", sequence: 2n })); + transport.dispose(); + await assert.rejects(send, /disposed/i); + } finally { + transport.dispose(); + } } finally { await listener.close(); } diff --git a/packages/cultmesh-ts/test/realtime-quic-native.test.ts b/packages/cultmesh-ts/test/realtime-quic-native.test.ts index 00c47741..f5761daf 100644 --- a/packages/cultmesh-ts/test/realtime-quic-native.test.ts +++ b/packages/cultmesh-ts/test/realtime-quic-native.test.ts @@ -198,9 +198,28 @@ test( // `onFault` (P10b's target), that throw would escape the pump's // dispatch loop and take every other connection down with it instead // of staying scoped to connection A. - const acceptedA = waitFor((resolve) => { + // + // The server-side connection listener is registered synchronously, + // inside the LISTENER_NEW_CONNECTION callback itself, rather than + // after an `await`: the native bridge can dispatch a connection's next + // event (CERTIFICATE_RECEIVED, or CONNECTED) before a later `await`'s + // continuation ever runs, and an event with no listener registered for + // its connection id is silently dropped (`dispatch`'s own + // `if (!entry) return`) — the same async-registration gap fix 1 closed + // for the provider's own accept path. + const serverAFaulted = waitFor((resolve) => { runtime.onListenerEvent(listenerId, (event) => { - if (event.type === CULTMESH_QUIC_EVENT_LISTENER_NEW_CONNECTION) resolve(event.connectionId); + if (event.type !== CULTMESH_QUIC_EVENT_LISTENER_NEW_CONNECTION) return; + runtime.onConnectionEvent( + event.connectionId, + () => { + throw new Error("P10b: connection A's listener always throws."); + }, + () => { + resolve(); + throw new Error("P10b: connection A's onFault also throws."); + }, + ); }); }); const clientA = runtime.connectionOpen("127.0.0.1", boundPort); @@ -209,24 +228,18 @@ test( runtime.connectionCertificateComplete(clientA, true); } }); - const serverAId = await acceptedA; - runtime.onConnectionEvent( - serverAId, - () => { - throw new Error("P10b: connection A's listener always throws."); - }, - () => { - throw new Error("P10b: connection A's onFault also throws."); - }, - ); - // Give A's handshake a moment to reach the throwing listener and fault. - await new Promise((resolve) => setTimeout(resolve, 300)); + await serverAFaulted; // Connection B, opened only after A faulted: if the throw above ever // escaped the pump, this connection would never reach CONNECTED at all. - const acceptedB = waitFor((resolve) => { + let serverBConnected = false; + const serverBAccepted = waitFor((resolve) => { runtime.onListenerEvent(listenerId, (event) => { - if (event.type === CULTMESH_QUIC_EVENT_LISTENER_NEW_CONNECTION) resolve(event.connectionId); + if (event.type !== CULTMESH_QUIC_EVENT_LISTENER_NEW_CONNECTION) return; + runtime.onConnectionEvent(event.connectionId, (connectionEvent) => { + if (connectionEvent.type === CULTMESH_QUIC_EVENT_CONNECTION_CONNECTED) serverBConnected = true; + }); + resolve(); }); }); const clientB = runtime.connectionOpen("127.0.0.1", boundPort); @@ -239,17 +252,12 @@ test( if (event.type === CULTMESH_QUIC_EVENT_CONNECTION_CONNECTED) resolve(); }); }); - const serverBId = await acceptedB; - let serverBConnected = false; - runtime.onConnectionEvent(serverBId, (event) => { - if (event.type === CULTMESH_QUIC_EVENT_CONNECTION_CONNECTED) serverBConnected = true; - }); + await serverBAccepted; await clientBConnected; assert.ok(serverBConnected, "connection B's handler must still run after A's onFault threw"); runtime.connectionShutdown(clientA, 0n); runtime.connectionShutdown(clientB, 0n); - runtime.connectionShutdown(serverBId, 0n); runtime.listenerClose(listenerId); } finally { await runtime.release(); diff --git a/packages/cultmesh-ts/test/realtime-quic-provider.test.ts b/packages/cultmesh-ts/test/realtime-quic-provider.test.ts index 9d088f06..ea723b64 100644 --- a/packages/cultmesh-ts/test/realtime-quic-provider.test.ts +++ b/packages/cultmesh-ts/test/realtime-quic-provider.test.ts @@ -661,8 +661,12 @@ test("fix 3: an IPv6 bind advertises a bracketed literal that round-trips throug provider.advertisedEndpoint, /^cultmesh-state\+quic:\/\/\[::1\]:\d+\?cert-sha256=[0-9A-F]{64}$/, ); + // `URL`'s own `hostname` keeps the brackets for an IPv6 literal (unlike + // the unbracketed form `formatEndpointHost` accepted as input); the + // round trip's job is proving the endpoint parses at all, not stripping + // brackets the WHATWG URL parser itself never strips. const parsed = parseQuicRealtimeEndpoint(provider.advertisedEndpoint); - assert.equal(parsed.host, "::1"); + assert.equal(parsed.host, "[::1]"); } finally { provider.dispose(); }