Add tests for the handshake-timer fix, encode-once broadcast, and P3/P8/P10b

realtime-quic-provider.test.ts: a peer stays connected and keeps
receiving well past handshakeTimeoutMs (must fail on the pre-fix code);
an invalid reliable-ordered or latest-only frame throws from broadcast()
without evicting any peer; advertisedHost is used in place of the bind
address and round-trips through the module's own parser; an IPv6 bind
advertises a bracketed literal; receive() with an already-aborted signal
rejects immediately; and a latest-only send failure evicts the peer
(P3 — the outbox's own send-failure path, not covered by the existing
reliable-ordered eviction test).

realtime-quic-consumer.test.ts: a reliable send still queued behind
reliableSendTail rejects instead of hanging when disposal wins the race
(P8 — sendOnStream's this.disposed guard).

realtime-quic-native.test.ts: a throwing onFault handler is isolated to
its own connection; the pump survives and a second connection's handler
still runs (P10b).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-23 19:16:06 +02:00
commit 0bb0fcba82
3 changed files with 267 additions and 0 deletions

View file

@ -1020,3 +1020,32 @@ test("fix 6: a stream shutdown prunes its entry so streamKinds does not grow wit
await listener.close();
}
});
test("P8: a reliable send still queued behind reliableSendTail rejects instead of hanging when disposal wins the race", async (t) => {
if (!nativeBridgeAvailable()) return void t.skip("no native bridge available");
const listener = await RawQuicListener.open();
try {
const target: CultMeshRealtimeTarget = { verseId: "aetheria", authorityRuntimeId: "service:aetheria.daemon" };
const candidate: CultMeshRealtimeCandidate = {
endpoint: `cultmesh-state+quic://127.0.0.1:${listener.boundPort}?cert-sha256=${fixturePinHex()}`,
authorityRuntimeId: target.authorityRuntimeId,
priority: 0,
generation: "gen-1",
};
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);
} finally {
await listener.close();
}
});

View file

@ -180,6 +180,83 @@ test("F7: a 64-bit event code round-trips exactly through the struct decode", as
}
});
test(
"P10b: a throwing onFault handler is isolated — the pump survives and other connections' handlers still run",
{ timeout: 10_000 },
async (t) => {
if (!nativeBridgeAvailable()) {
t.skip("CULTMESH_QUIC_NATIVE_DIR is not set to a built native bridge.");
return;
}
const runtime = await CultMeshQuicNativeRuntime.open();
try {
const pkcs12 = readFileSync(FIXTURE_P12);
const { listenerId, boundPort } = runtime.listenerOpen("127.0.0.1", 0, pkcs12, "");
// Connection A: both its listener and its onFault handler throw.
// Without `faultConnection`'s own try/catch isolating a throwing
// `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<bigint>((resolve) => {
runtime.onListenerEvent(listenerId, (event) => {
if (event.type === CULTMESH_QUIC_EVENT_LISTENER_NEW_CONNECTION) resolve(event.connectionId);
});
});
const clientA = runtime.connectionOpen("127.0.0.1", boundPort);
runtime.onConnectionEvent(clientA, (event) => {
if (event.type === CULTMESH_QUIC_EVENT_CONNECTION_CERTIFICATE_RECEIVED) {
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));
// 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<bigint>((resolve) => {
runtime.onListenerEvent(listenerId, (event) => {
if (event.type === CULTMESH_QUIC_EVENT_LISTENER_NEW_CONNECTION) resolve(event.connectionId);
});
});
const clientB = runtime.connectionOpen("127.0.0.1", boundPort);
const clientBConnected = waitFor<void>((resolve) => {
runtime.onConnectionEvent(clientB, (event) => {
if (event.type === CULTMESH_QUIC_EVENT_CONNECTION_CERTIFICATE_RECEIVED) {
runtime.connectionCertificateComplete(clientB, true);
return;
}
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 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();
}
},
);
test("fix 2: release() refuses a call with no outstanding reference", async (t) => {
if (!nativeBridgeAvailable()) {
t.skip("CULTMESH_QUIC_NATIVE_DIR is not set to a built native bridge.");

View file

@ -19,6 +19,7 @@ import {
import {
CultMeshQuicRealtimeConnector,
CultMeshQuicRealtimeProvider,
parseQuicRealtimeEndpoint,
type CultMeshRealtimeCandidate,
type CultMeshRealtimeTarget,
type CultMeshRealtimeTransport,
@ -541,3 +542,163 @@ test("a reliable-ordered frame is never retained and is not delivered to a late
provider.dispose();
}
});
test(
"fix 1: a peer stays connected and keeps receiving well past handshakeTimeoutMs",
{ timeout: 10_000 },
async (t) => {
if (!nativeBridgeAvailable()) return void t.skip("no native bridge available");
// Must fail on the pre-fix code: the buffering-handler/attachPeer swap
// there loses `state.connected` to whichever handler happens to be
// registered when CONNECTED actually arrives, and the handshake timer
// evicts a fully healthy peer at this deadline regardless.
const handshakeTimeoutMs = 500;
const provider = await CultMeshQuicRealtimeProvider.listen({
host: "127.0.0.1",
port: 0,
serverCertificate: { pkcs12: readFileSync(FIXTURE_P12), password: "" },
handshakeTimeoutMs,
});
try {
const consumer = await dialProvider(provider);
try {
await waitUntil(() => provider.connectionCount === 1);
await new Promise((resolve) => setTimeout(resolve, handshakeTimeoutMs * 3));
assert.equal(provider.connectionCount, 1, "a connected peer must not be evicted by the handshake timer");
await provider.broadcast(testFrame({ delivery: "reliable-ordered", sequence: 1n }));
const received = await consumer.receiveFrame();
assert.equal(received.sequence, 1n, "a peer past the handshake deadline must still receive frames");
} finally {
consumer.dispose();
}
} finally {
provider.dispose();
}
},
);
test("fix 2: an invalid frame throws from broadcast, and connectionCount is unchanged", async (t) => {
if (!nativeBridgeAvailable()) return void t.skip("no native bridge available");
const provider = await startProvider();
try {
const consumer = await dialProvider(provider);
try {
await waitUntil(() => provider.connectionCount === 1);
// A 70,000-byte channelId exceeds the wire's 0xffff identity limit:
// `encodeRealtimeFrame` throws. Before fix 2, encoding happened once
// per peer inside `sendFrame`, so the same failure evicted every peer
// while `broadcast()` itself reported success.
await assert.rejects(
provider.broadcast(testFrame({ delivery: "reliable-ordered", channelId: "x".repeat(70_000) })),
/exceeds the QUIC wire limit/i,
);
assert.equal(provider.connectionCount, 1, "an invalid frame must not evict any peer");
// The peer must still be usable afterward: prove the connection itself
// survived, not merely that the count field wasn't decremented.
await provider.broadcast(testFrame({ delivery: "reliable-ordered", sequence: 42n }));
assert.equal((await consumer.receiveFrame()).sequence, 42n);
} finally {
consumer.dispose();
}
} finally {
provider.dispose();
}
});
test("fix 2: a latest-only invalid frame also throws from broadcast without evicting any peer", async (t) => {
if (!nativeBridgeAvailable()) return void t.skip("no native bridge available");
const provider = await startProvider();
try {
const consumer = await dialProvider(provider);
try {
await waitUntil(() => provider.connectionCount === 1);
await assert.rejects(
provider.broadcast(testFrame({ delivery: "latest-only", channelId: "x".repeat(70_000) })),
/exceeds the QUIC wire limit/i,
);
assert.equal(provider.connectionCount, 1, "an invalid latest-only frame must not evict any peer");
} finally {
consumer.dispose();
}
} finally {
provider.dispose();
}
});
test("fix 3: advertisedHost is used in place of the bind address, and round-trips through the module's parser", async (t) => {
if (!nativeBridgeAvailable()) return void t.skip("no native bridge available");
const provider = await CultMeshQuicRealtimeProvider.listen({
host: "0.0.0.0",
port: 0,
serverCertificate: { pkcs12: readFileSync(FIXTURE_P12), password: "" },
advertisedHost: "streampixels.gamecult.org",
});
try {
assert.match(
provider.advertisedEndpoint,
/^cultmesh-state\+quic:\/\/streampixels\.gamecult\.org:\d+\?cert-sha256=[0-9A-F]{64}$/,
);
const parsed = parseQuicRealtimeEndpoint(provider.advertisedEndpoint);
assert.equal(parsed.host, "streampixels.gamecult.org");
assert.equal(parsed.port, Number(new URL(provider.advertisedEndpoint).port));
assert.ok(parsed.certificateSha256);
} finally {
provider.dispose();
}
});
test("fix 3: an IPv6 bind advertises a bracketed literal that round-trips through the module's parser", async (t) => {
if (!nativeBridgeAvailable()) return void t.skip("no native bridge available");
const provider = await CultMeshQuicRealtimeProvider.listen({
host: "::1",
port: 0,
serverCertificate: { pkcs12: readFileSync(FIXTURE_P12), password: "" },
});
try {
assert.match(
provider.advertisedEndpoint,
/^cultmesh-state\+quic:\/\/\[::1\]:\d+\?cert-sha256=[0-9A-F]{64}$/,
);
const parsed = parseQuicRealtimeEndpoint(provider.advertisedEndpoint);
assert.equal(parsed.host, "::1");
} finally {
provider.dispose();
}
});
test("fix 4: receive() with an already-aborted signal rejects immediately instead of hanging", async (t) => {
if (!nativeBridgeAvailable()) return void t.skip("no native bridge available");
const provider = await startProvider();
try {
const controller = new AbortController();
controller.abort();
await assert.rejects(provider.receive(controller.signal), /aborted/i);
} finally {
provider.dispose();
}
});
test("P3: a latest-only send failure evicts the peer without broadcast() throwing to the caller", async (t) => {
if (!nativeBridgeAvailable()) return void t.skip("no native bridge available");
const provider = await startProvider();
try {
const consumer = await dialProvider(provider);
await waitUntil(() => provider.connectionCount === 1);
consumer.dispose();
// Mirrors the existing reliable-ordered eviction test, but for the
// outbox's own send failure path (`CultMeshQuicRealtimeProviderOutbox.pump`'s
// catch, which disposes the peer and reports failure through
// `onSendFailure`) rather than `broadcast()`'s direct per-peer catch.
const deadline = Date.now() + 2_000;
let sequence = 0n;
while (Date.now() < deadline && provider.connectionCount > 0) {
sequence += 1n;
await provider.broadcast(testFrame({ delivery: "latest-only", sequence, bodyId: `body:${sequence}` }));
}
assert.equal(provider.connectionCount, 0, "the dead peer must be evicted, not counted forever");
} finally {
provider.dispose();
}
});