Calls the runtime's private `nextEvent` directly (0 ms timeout, no listener/connection open, so each round trip returns immediately with status 0) 200,000 times after a warm-up, then asserts RSS growth across the run stays under 5 MiB with `global.gc()` forced before and after. The old per-call `koffi.alloc` code allocates a ~64 B event struct plus a 4 B int32 every iteration and never frees either, so it clears this bound by roughly 2x; the fixed code, which allocates nothing per call, stays flat. Skips when no native bridge is built or when Node wasn't started with `--expose-gc`; `npm test` now passes `--expose-gc` so the check runs by default instead of always skipping. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
268 lines
11 KiB
TypeScript
268 lines
11 KiB
TypeScript
// The Cut 3 FFI smoke, now run against `CultMeshQuicNativeRuntime` instead of
|
|
// raw koffi calls: open a runtime, open a listener with a real credential,
|
|
// connect a real loopback connection, exchange one frame's worth of raw
|
|
// bytes, and close everything down. This proves the binding's declared ABI
|
|
// matches the shipped bridge; frame semantics (delivery, generation) are
|
|
// `realtime-quic-consumer.test.ts`'s job.
|
|
//
|
|
// Skips instead of failing when no native bridge is available: set
|
|
// CULTMESH_QUIC_NATIVE_DIR to a directory `scripts/build-quic-native.ps1` or
|
|
// `scripts/build-quic-native.sh` built, or run from the committed
|
|
// `packages/cultmesh-ts/native/<platform>` tree once a later cut lands it.
|
|
|
|
import assert from "node:assert/strict";
|
|
import { readFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import test from "node:test";
|
|
|
|
import {
|
|
CultMeshQuicNativeRuntime,
|
|
CULTMESH_QUIC_EVENT_LISTENER_NEW_CONNECTION,
|
|
CULTMESH_QUIC_EVENT_CONNECTION_CERTIFICATE_RECEIVED,
|
|
CULTMESH_QUIC_EVENT_CONNECTION_CONNECTED,
|
|
CULTMESH_QUIC_EVENT_CONNECTION_SHUTDOWN,
|
|
CULTMESH_QUIC_EVENT_STREAM_STARTED,
|
|
CULTMESH_QUIC_EVENT_STREAM_FRAME,
|
|
CULTMESH_QUIC_EVENT_STREAM_SEND_COMPLETE,
|
|
CULTMESH_QUIC_STREAM_RELIABLE,
|
|
} from "../src/realtime-quic-native";
|
|
import { nativeBridgeAvailable } from "./support/native-bridge";
|
|
|
|
// __dirname at runtime is dist-test/test; fixtures are binary and are never
|
|
// compiled/copied there, so they are read from their source location, two
|
|
// levels up.
|
|
const FIXTURE_P12 = join(__dirname, "..", "..", "test", "fixtures", "quic-test.p12");
|
|
|
|
async function waitFor<T>(collect: (resolve: (value: T) => void) => void, timeoutMs = 5_000): Promise<T> {
|
|
return await new Promise<T>((resolve, reject) => {
|
|
const timer = setTimeout(() => reject(new Error("timed out waiting for a native QUIC event")), timeoutMs);
|
|
collect((value) => {
|
|
clearTimeout(timer);
|
|
resolve(value);
|
|
});
|
|
});
|
|
}
|
|
|
|
test("CultMeshQuicNativeRuntime opens and closes with no listener or connection", async (t) => {
|
|
if (!nativeBridgeAvailable()) {
|
|
t.skip("CULTMESH_QUIC_NATIVE_DIR is not set to a built native bridge.");
|
|
return;
|
|
}
|
|
const runtime = await CultMeshQuicNativeRuntime.open();
|
|
await runtime.release();
|
|
});
|
|
|
|
test("CultMeshQuicNativeRuntime opens and closes a listener with a real PKCS12 credential", 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, "");
|
|
assert.ok(boundPort > 0);
|
|
runtime.listenerClose(listenerId);
|
|
} finally {
|
|
await runtime.release();
|
|
}
|
|
});
|
|
|
|
test("a full loopback connection delivers one raw frame end to end", 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, "");
|
|
|
|
const accepted = waitFor<bigint>((resolve) => {
|
|
runtime.onListenerEvent(listenerId, (event) => {
|
|
if (event.type === CULTMESH_QUIC_EVENT_LISTENER_NEW_CONNECTION) resolve(event.connectionId);
|
|
});
|
|
});
|
|
|
|
const clientConnectionId = runtime.connectionOpen("127.0.0.1", boundPort);
|
|
const clientConnected = waitFor<void>((resolve) => {
|
|
runtime.onConnectionEvent(clientConnectionId, (event) => {
|
|
if (event.type === CULTMESH_QUIC_EVENT_CONNECTION_CERTIFICATE_RECEIVED) {
|
|
runtime.connectionCertificateComplete(clientConnectionId, true);
|
|
return;
|
|
}
|
|
if (event.type === CULTMESH_QUIC_EVENT_CONNECTION_CONNECTED) resolve();
|
|
});
|
|
});
|
|
|
|
const serverConnectionId = await accepted;
|
|
const serverFrame = waitFor<Uint8Array>((resolve) => {
|
|
runtime.onConnectionEvent(serverConnectionId, (event) => {
|
|
if (event.type === CULTMESH_QUIC_EVENT_STREAM_STARTED) {
|
|
assert.equal(event.streamKind, CULTMESH_QUIC_STREAM_RELIABLE);
|
|
return;
|
|
}
|
|
if (event.type === CULTMESH_QUIC_EVENT_STREAM_FRAME) resolve(event.payload);
|
|
});
|
|
});
|
|
|
|
await clientConnected;
|
|
const streamId = runtime.streamOpen(clientConnectionId, CULTMESH_QUIC_STREAM_RELIABLE);
|
|
const sendComplete = waitFor<void>((resolve) => {
|
|
runtime.onConnectionEvent(clientConnectionId, (event) => {
|
|
if (event.type === CULTMESH_QUIC_EVENT_STREAM_SEND_COMPLETE) resolve();
|
|
});
|
|
});
|
|
runtime.streamSendFrame(streamId, new Uint8Array([1, 2, 3, 4]), true);
|
|
await sendComplete;
|
|
|
|
const payload = await serverFrame;
|
|
assert.deepEqual(Array.from(payload), [1, 2, 3, 4]);
|
|
|
|
runtime.connectionShutdown(clientConnectionId, 0n);
|
|
runtime.connectionShutdown(serverConnectionId, 0n);
|
|
runtime.listenerClose(listenerId);
|
|
} finally {
|
|
await runtime.release();
|
|
}
|
|
});
|
|
|
|
test("F7: a 64-bit event code round-trips exactly through the struct decode", 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, "");
|
|
|
|
// `code` (a `uint64_t`, the same field `CULTMESH_QUIC_SEND_CANCELED` is
|
|
// compared against on the stream-send path) is exercised here through
|
|
// CONNECTION_SHUTDOWN instead, whose code is an application-chosen value
|
|
// this test fully controls: two values above 2^53 (where koffi's decode
|
|
// stops being a plain safe-integer `number`) prove the whole
|
|
// encode-native-decode-coerce path is exact, deterministically, with no
|
|
// dependency on MsQuic's own send-completion timing.
|
|
for (const code of [(1n << 53n) + 1n, (1n << 62n) - 1n]) {
|
|
const accepted = waitFor<bigint>((resolve) => {
|
|
runtime.onListenerEvent(listenerId, (event) => {
|
|
if (event.type === CULTMESH_QUIC_EVENT_LISTENER_NEW_CONNECTION) resolve(event.connectionId);
|
|
});
|
|
});
|
|
const clientConnectionId = runtime.connectionOpen("127.0.0.1", boundPort);
|
|
const clientConnected = waitFor<void>((resolve) => {
|
|
runtime.onConnectionEvent(clientConnectionId, (event) => {
|
|
if (event.type === CULTMESH_QUIC_EVENT_CONNECTION_CERTIFICATE_RECEIVED) {
|
|
runtime.connectionCertificateComplete(clientConnectionId, true);
|
|
return;
|
|
}
|
|
if (event.type === CULTMESH_QUIC_EVENT_CONNECTION_CONNECTED) resolve();
|
|
});
|
|
});
|
|
const serverConnectionId = await accepted;
|
|
await clientConnected;
|
|
|
|
const seenOnClient = waitFor<bigint>((resolve) => {
|
|
runtime.onConnectionEvent(clientConnectionId, (event) => {
|
|
if (event.type === CULTMESH_QUIC_EVENT_CONNECTION_SHUTDOWN) resolve(event.code);
|
|
});
|
|
});
|
|
runtime.connectionShutdown(serverConnectionId, code);
|
|
const seen = await seenOnClient;
|
|
assert.equal(typeof seen, "bigint", "a decoded event code must always be bigint, never number");
|
|
assert.equal(seen, code, `shutdown code ${code} must round-trip exactly through the struct decode`);
|
|
}
|
|
|
|
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.");
|
|
return;
|
|
}
|
|
const runtime = await CultMeshQuicNativeRuntime.open();
|
|
await runtime.release();
|
|
await assert.rejects(runtime.release(), /no outstanding reference/i);
|
|
});
|
|
|
|
test("negative grep: no koffi callbacks, .async only on nextEvent", () => {
|
|
// __dirname at runtime is dist-test/test; the source lives two levels up,
|
|
// in src/, since only .js output is mirrored under dist-test.
|
|
const source = readFileSync(join(__dirname, "..", "..", "src", "realtime-quic-native.ts"), "utf8");
|
|
// `.async` is referenced (not necessarily called with a literal "(" at the
|
|
// reference site: it is handed to `promisify`, which calls it) only for
|
|
// `nextEvent`, the ABI's single blocking export.
|
|
const asyncUses = [...source.matchAll(/\.async\b/g)];
|
|
assert.ok(asyncUses.length > 0, "expected at least one .async use for nextEvent");
|
|
for (const match of asyncUses) {
|
|
const before = source.slice(Math.max(0, match.index! - 40), match.index!);
|
|
assert.match(before, /nextEvent/i, "the only .async reference must be nextEvent");
|
|
}
|
|
assert.doesNotMatch(source, /koffi\.register/, "no koffi callbacks");
|
|
});
|
|
|
|
test(
|
|
"memory: sustained nextEvent calls do not leak koffi-allocated out-parameter buffers",
|
|
{ timeout: 60_000 },
|
|
async (t) => {
|
|
if (!nativeBridgeAvailable()) {
|
|
t.skip("CULTMESH_QUIC_NATIVE_DIR is not set to a built native bridge.");
|
|
return;
|
|
}
|
|
if (typeof global.gc !== "function") {
|
|
t.skip("run with --expose-gc to measure RSS growth deterministically.");
|
|
return;
|
|
}
|
|
|
|
const runtime = await CultMeshQuicNativeRuntime.open();
|
|
try {
|
|
// `nextEvent` is private: the pump loop is the only production caller,
|
|
// and it never has more than one call in flight at a time, which is
|
|
// exactly the property this test needs to call it directly in a tight
|
|
// loop instead of waiting on the pump's 250 ms idle poll. A 0 ms
|
|
// timeout with no listener/connection open returns immediately with
|
|
// status 0 (no event), so each iteration is a fast round trip through
|
|
// the real native ABI, not a synthetic stand-in.
|
|
const nextEvent = (
|
|
runtime as unknown as {
|
|
nextEvent(timeoutMs: number, payload: Uint8Array | null, payloadCapacity: number): Promise<unknown>;
|
|
}
|
|
).nextEvent.bind(runtime);
|
|
|
|
// Warm up: let koffi's own one-time bookkeeping (module init, JIT,
|
|
// thread pool spin-up) happen before the baseline measurement.
|
|
for (let i = 0; i < 2_000; i += 1) {
|
|
await nextEvent(0, null, 0);
|
|
}
|
|
global.gc();
|
|
const before = process.memoryUsage().rss;
|
|
|
|
// Sized so the old per-call `koffi.alloc` code (~64 B event struct +
|
|
// ~4 B int32, plus allocator bookkeeping, well over 53 B/call) clearly
|
|
// exceeds the bound below, while the fixed code, which allocates
|
|
// nothing per call, stays flat.
|
|
const ITERATIONS = 200_000;
|
|
for (let i = 0; i < ITERATIONS; i += 1) {
|
|
await nextEvent(0, null, 0);
|
|
}
|
|
global.gc();
|
|
const after = process.memoryUsage().rss;
|
|
|
|
const growthBytes = after - before;
|
|
const BOUND_BYTES = 5 * 1024 * 1024;
|
|
assert.ok(
|
|
growthBytes < BOUND_BYTES,
|
|
`RSS grew by ${growthBytes} bytes (${(growthBytes / 1024 / 1024).toFixed(2)} MiB) over ` +
|
|
`${ITERATIONS} nextEvent calls; expected it to stay under ${BOUND_BYTES / 1024 / 1024} MiB ` +
|
|
"once out-parameter buffers are reused instead of allocated per call.",
|
|
);
|
|
} finally {
|
|
await runtime.release();
|
|
}
|
|
},
|
|
);
|