Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions js/clock/src/manual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ export class Manual {
/**
* Fire timers like repeated `fire` calls until `work` settles, then settle the same way.
* Time stops at the deadline being fired when `work` settles, and no wait outlives the call.
* Promise-based I/O such as a stream delivery lands before each deadline; a dynamic `import()`
* may not, so load modules first. Await it directly: bun's `expect(...).rejects` runs it nested,
* ahead of microtasks the caller already queued.
*/
async drive<T>(work: PromiseLike<T>): Promise<T> {
if (this.#driving) throw new Error("time driver already pending");
Expand Down
9 changes: 1 addition & 8 deletions js/net/src/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, spyOn, test } from "bun:test";
import { Manual, Milli, Nano, yieldNow } from "@moq/clock";
import type { Getter } from "@moq/signals";
import { Duplicate, type Grant, Handle, type Issued, type Request, type Requests, Unsupported } from "./auth.ts";
import { accept as acceptSession, connect as connectSession, type Established } from "./connection/index.ts";
import { SessionCode, SessionError, StreamCode, StreamError, toStreamCode } from "./error.ts";
Expand All @@ -10,7 +9,7 @@ import * as IetfMessage from "./ietf/message.ts";
import { AuthMessage as LiteAuthMessage, LiteAuthWire } from "./lite/auth.ts";
import * as Lite from "./lite/index.ts";
import * as LiteMessage from "./lite/message.ts";
import { createMockTransportPair, type MockTransport, manual, settlesWithin } from "./mock.ts";
import { createMockTransportPair, type MockTransport, manual, settlesWithin, waitFor } from "./mock.ts";
import { Producer as OriginProducer } from "./origin.ts";
import * as Path from "./path.ts";
import { Stream, Writer } from "./stream.ts";
Expand Down Expand Up @@ -38,12 +37,6 @@ function grant(publish: string[], subscribe: string[]): Grant {
return { publish: patterns(...publish), subscribe: patterns(...subscribe) };
}

async function waitFor<T>(getter: Getter<T>, ready: (value: T) => boolean): Promise<T> {
let value = getter.peek();
while (!ready(value)) value = await getter.changed();
return value;
}

/** Whether a request on `path` logged the end of its authorization wait. */
function waited(logged: { mock: { calls: unknown[][] } }, path: string): boolean {
return logged.mock.calls.some(([line]) =>
Expand Down
9 changes: 5 additions & 4 deletions js/net/src/connection/connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ConnectTimeout, SessionCode, SessionError } from "../error.ts";
import * as Ietf from "../ietf/index.ts";
import { ClientSetup, ServerSetup } from "../lite/compat.ts";
import { ALPN, ALPN_05, ALPN_06, ALPN_07_WIP, Version as Lite } from "../lite/version.ts";
import { createMockTransportPair, driveUntil, manual } from "../mock.ts";
import { createMockTransportPair, manual } from "../mock.ts";
import { Stream } from "../stream.ts";
import { accept } from "./accept.ts";
import { type ConnectProps, connect as connectSession } from "./connect.ts";
Expand Down Expand Up @@ -564,8 +564,9 @@ test("SETUP refuses a version the client did not name", async () => {
versions: ["moq-lite-07-wip", "moq-lite-06"],
websocket: { enabled: false },
});
// The refusal waits out the grace a peer's close code gets.
await driveUntil(clock, connecting);
// The refusal waits out the grace a peer's close code gets. Driven outside `expect`, as
// `Manual.drive` asks.
await clock.drive(connecting.catch(() => {}));
await expect(connecting).rejects.toThrow("unsupported server version: 0xff0dad05");
(await server)?.abort();
} finally {
Expand Down Expand Up @@ -601,7 +602,7 @@ test.each(IETF_VERSIONS)("a client refuses %s selected in the moql SETUP", async
await ClientSetup.decode(stream.reader);
await new ServerSetup(version).encode(stream.writer);
// The refusal waits out the grace a peer's close code gets.
await driveUntil(clock, connecting);
await clock.drive(connecting.catch(() => {}));
await expect(connecting).rejects.toThrow(`unsupported server version: 0x${version.toString(16)}`);
} finally {
restore();
Expand Down
85 changes: 53 additions & 32 deletions js/net/src/connection/reload.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { expect, spyOn, test } from "bun:test";
import { Instant, Manual, Milli, Nano, now, yieldNow } from "@moq/clock";
import { Effect, Signal } from "@moq/signals";
import { Effect, type Getter, Signal } from "@moq/signals";
import * as Announce from "../announced.ts";
import { Viewers } from "../bandwidth.ts";
import type { Producer as BroadcastProducer } from "../broadcast.ts";
import { ConnectTimeout, SessionCode, SessionError, StreamCode, toTransport } from "../error.ts";
import { WireRoute } from "../hop.ts";
// Loaded up front: a session imports it on first use, which takes host turns a Manual clock cannot wait out.
import "../ietf/connection.ts";
import { ALPN as IetfALPN } from "../ietf/version.ts";
import * as Lite from "../lite/index.ts";
import { createMockTransportPair, driveUntil, type MockTransport, manual, textFrame } from "../mock.ts";
import { createMockTransportPair, type MockTransport, manual, textFrame, waitFor } from "../mock.ts";
import { Producer as OriginProducer } from "../origin.ts";
import * as Path from "../path.ts";
import { Stream } from "../stream.ts";
Expand Down Expand Up @@ -154,7 +156,7 @@ test("a peer that severs immediately keeps escalating the backoff", async () =>
delay: { initial: Milli(1000), multiplier: 2, max: Milli(1000), timeout: Milli(1) },
});
try {
await driveUntil(clock, () => reload.error.peek() !== undefined);
await clock.drive(waitFor(reload.error, (error) => error !== undefined));
expect(reload.closed.peek()).toBeUndefined();

// Giving up this URL does not dispose the loop: a disable/re-enable starts another sequence.
Expand Down Expand Up @@ -205,6 +207,22 @@ test("an explicitly undefined delay field falls back to its default", async () =
}
});

// The paths `routes` has announced and not retracted since, read as they arrive.
function present(routes: Announce.Routes): Getter<ReadonlySet<Path.Valid>> {
const paths = new Signal<ReadonlySet<Path.Valid>>(new Set());
void (async () => {
for await (const event of routes) {
paths.update((previous) => {
const next = new Set(previous);
if (event.kind === "end") next.delete(event.prefix);
else next.add(event.prefix);
return next;
});
}
})();
return paths;
}

// Polls until `pred` holds, so a regression fails the test instead of hanging it.
async function waitUntil(pred: () => boolean): Promise<void> {
const deadline = Instant.add(now(), Nano.fromMilli(Milli(1000)));
Expand Down Expand Up @@ -246,12 +264,11 @@ test("a follower follows the reconnect on the same handle", async () => {
const watched = clientOrigin.follow(Path.from("late"));

try {
await driveUntil(clock, () => watched.active.peek() !== undefined);
const first = watched.active.peek();
const first = await clock.drive(waitFor(watched.active, (active) => active !== undefined));

// The session dies; the same request follows the next session's announcement.
sessions[0]?.abort();
await driveUntil(clock, () => watched.active.peek() !== undefined && watched.active.peek() !== first);
await clock.drive(waitFor(watched.active, (active) => active !== undefined && active !== first));
expect(watched.closed.peek()).toBeUndefined();
} finally {
watched.close();
Expand Down Expand Up @@ -420,10 +437,10 @@ for (const code of [SessionCode.Unauthorized, SessionCode.MalformedAuthToken, Se
* once `release` resolves, and accepts any other credential, recording each dialed jwt.
*/
function refusing(refused: string, release: Promise<void>) {
const dialed: (string | null)[] = [];
const dialed = new Signal<readonly (string | null)[]>([]);
const stub = function StubWebTransport(target: string | URL) {
const jwt = new URL(target).searchParams.get("jwt");
dialed.push(jwt);
dialed.update((jwts) => [...jwts, jwt]);
const pair = createMockTransportPair(Lite.ALPN_06);
void accept(pair.server, new URL(target)).then(async () => {
if (jwt !== refused) return;
Expand Down Expand Up @@ -451,12 +468,12 @@ test("a refused credential replaced since its dial redials with the new one", as
delay: { initial: Milli(1), multiplier: 2, max: Milli(1), timeout: Milli(0) },
});
try {
await driveUntil(clock, () => reload.established.peek() !== undefined);
await clock.drive(waitFor(reload.established, (established) => established !== undefined));
tokens.set(["new"]);
resolve();
await driveUntil(clock, () => dialed.includes("new"));
expect(dialed).toEqual(["old", "new"]);
await driveUntil(clock, () => reload.established.peek() !== undefined);
await clock.drive(waitFor(dialed, (jwts) => jwts.includes("new")));
expect(dialed.peek()).toEqual(["old", "new"]);
await clock.drive(waitFor(reload.established, (established) => established !== undefined));
expect(reload.error.peek()).toBeUndefined();
} finally {
reload.close();
Expand All @@ -479,11 +496,11 @@ test("new tokens redial a URL that stopped on refusing the old ones", async () =
delay: { initial: Milli(1), multiplier: 2, max: Milli(1), timeout: Milli(0) },
});
try {
await driveUntil(clock, () => reload.error.peek() !== undefined);
expect(dialed).toEqual(["old"]);
await clock.drive(waitFor(reload.error, (error) => error !== undefined));
expect(dialed.peek()).toEqual(["old"]);
tokens.set(["new"]);
await driveUntil(clock, () => dialed.includes("new"));
await driveUntil(clock, () => reload.established.peek() !== undefined);
await clock.drive(waitFor(dialed, (jwts) => jwts.includes("new")));
await clock.drive(waitFor(reload.established, (established) => established !== undefined));
expect(reload.error.peek()).toBeUndefined();
} finally {
reload.close();
Expand Down Expand Up @@ -547,7 +564,7 @@ test("a setup stream reset is not mistaken for an unauthorized session", async (
using clock = manual();
const original = globalThis.WebTransport;
const url = new URL("https://example.com/");
let attempts = 0;
const attempts = new Signal(0);
const stub = function StubWebTransport() {
// No ALPN, so the handshake exchanges SETUP over a bidi stream we can reset.
const pair = createMockTransportPair("");
Expand All @@ -557,7 +574,7 @@ test("a setup stream reset is not mistaken for an unauthorized session", async (
incoming.releaseLock();
if (accepted.done) return;

attempts++;
attempts.update((n) => n + 1);
await accepted.value.writable.abort(toTransport(StreamCode.DeliveryTimeout, "delivery timeout"));
})();
return pair.client;
Expand All @@ -574,7 +591,7 @@ test("a setup stream reset is not mistaken for an unauthorized session", async (
});

try {
await driveUntil(clock, () => attempts >= 2);
await clock.drive(waitFor(attempts, (n) => n >= 2));
} finally {
reload.close();
globalThis.WebTransport = original;
Expand All @@ -593,7 +610,9 @@ test("origins span reconnects: local re-announces, remote re-populates", async (

// Each connect attempt gets a fresh server session that publishes "remote" and records
// what the client announced to it.
const servers: { session: { abort: () => void }; saw: OriginProducer; watching: { close(): void } }[] = [];
const servers = new Signal<
readonly { session: { abort: () => void }; watching: Announce.Routes; saw: Getter<ReadonlySet<Path.Valid>> }[]
>([]);
const stub = function StubWebTransport() {
const pair = createMockTransportPair(Lite.ALPN_05);
const saw = new OriginProducer();
Expand All @@ -602,7 +621,7 @@ test("origins span reconnects: local re-announces, remote re-populates", async (
const serverOrigin = new OriginProducer();
void accept(pair.server, url, { publish: serverOrigin.consume(), consume: saw }).then((session) => {
publish(serverOrigin, Path.from("remote"));
servers.push({ session, saw, watching });
servers.update((list) => [...list, { session, watching, saw: present(watching) }]);
});
return pair.client;
};
Expand All @@ -619,24 +638,26 @@ test("origins span reconnects: local re-announces, remote re-populates", async (
const reader = subscribeOrigin.consume();
// A watch held across the reconnect: each session re-opens its announce request.
const watching = reader.routes();
const remote = present(watching);

try {
// First session: the server's broadcast lands in the client origin, and the client's
// publish lands in the server's.
await driveUntil(clock, () => wireOf(reader).routes(Path.from("remote")));
await driveUntil(clock, () => (servers[0] ? wireOf(servers[0].saw).routes(Path.from("mine")) : false));
await clock.drive(waitFor(remote, (paths) => paths.has(Path.from("remote"))));
const [first] = await clock.drive(waitFor(servers, (list) => list.length > 0));
await clock.drive(waitFor(first.saw, (paths) => paths.has(Path.from("mine"))));

// Kill the session: the remote entry retracts, the local publish stays put.
servers[0]?.session.abort();
await driveUntil(clock, () => !wireOf(reader).routes(Path.from("remote")));
first.session.abort();
await clock.drive(waitFor(remote, (paths) => !paths.has(Path.from("remote"))));

// The reconnect re-announces the (untouched) publish and re-populates the table.
await driveUntil(clock, () => servers.length > 1);
await driveUntil(clock, () => wireOf(reader).routes(Path.from("remote")));
await driveUntil(clock, () => (servers[1] ? wireOf(servers[1].saw).routes(Path.from("mine")) : false));
const [, second] = await clock.drive(waitFor(servers, (list) => list.length > 1));
await clock.drive(waitFor(remote, (paths) => paths.has(Path.from("remote"))));
await clock.drive(waitFor(second.saw, (paths) => paths.has(Path.from("mine"))));
} finally {
watching.close();
for (const server of servers) server.watching.close();
for (const server of servers.peek()) server.watching.close();
reload.close();
publishOrigin.close();
subscribeOrigin.close();
Expand Down Expand Up @@ -1114,12 +1135,12 @@ test("a watcher syncs with the server's initial set, and again after a reconnect
try {
// Nothing is connected yet, which is not the same as nothing being announced.
expect(watching.synced.peek()).toBe(false);
await driveUntil(clock, () => watching.synced.peek());
await clock.drive(waitFor(watching.synced, (synced) => synced));
expect([...seen].sort()).toEqual(["a", "b"]);

sessions[0]?.abort();
await driveUntil(clock, () => !watching.synced.peek());
await driveUntil(clock, () => sessions.length > 1 && watching.synced.peek());
await clock.drive(waitFor(watching.synced, (synced) => !synced));
await clock.drive(waitFor(watching.synced, (synced) => synced));
expect([...seen].sort()).toEqual(["a", "b"]);
} finally {
watching.close();
Expand Down
5 changes: 2 additions & 3 deletions js/net/src/ietf/tail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ProtocolViolation, StreamCode, StreamError } from "../error.ts";
import type { Consumer as GroupConsumer } from "../group.ts";
import { hooks } from "../internal.ts";
import { GRACE } from "../ledger.ts";
import { createMockTransportPair, driveUntil, manual } from "../mock.ts";
import { createMockTransportPair, manual } from "../mock.ts";
import * as Path from "../path.ts";
import { Reader, Stream } from "../stream.ts";
import { type GroupFlags, Group as GroupMessage } from "./object.ts";
Expand Down Expand Up @@ -116,8 +116,7 @@ test("a group stream that arrives after PUBLISH_DONE is delivered", async () =>
expect(await reader.recvGroup()).toBeUndefined();
expect(await reader.closed).toBeNull();
})();
await driveUntil(clock, read);
await read;
await clock.drive(read);
// The Stream Count was met, so nothing waited out the grace.
expect(Nano.toMilli(Instant.durationSince(clock.now(), started))).toBeLessThan(GRACE);
});
Expand Down
12 changes: 1 addition & 11 deletions js/net/src/integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { afterEach, expect, mock, onTestFinished, spyOn, test } from "bun:test";
import { Instant, Manual, Milli, Nano, now, yieldNow } from "@moq/clock";
import type { Getter } from "@moq/signals";
import type * as Announce from "./announced.ts";
import { type Consumer as BroadcastConsumer, Producer as BroadcastProducer } from "./broadcast.ts";
import {
Expand All @@ -21,7 +20,7 @@ import * as Instance from "./instance.ts";
import { hooks } from "./internal.ts";
import * as Lite from "./lite/index.ts";
import { Publisher as LitePublisher } from "./lite/publisher.ts";
import { createMockTransportPair, manual, settlesWithin, textFrame } from "./mock.ts";
import { createMockTransportPair, manual, settlesWithin, textFrame, waitFor } from "./mock.ts";
import type { Consumer as OriginConsumer } from "./origin.ts";
import { Producer as OriginProducer } from "./origin.ts";
import * as Path from "./path.ts";
Expand Down Expand Up @@ -1425,15 +1424,6 @@ async function runConsumeDedup(protocol: string, version?: number) {
server.abort();
}

/** Wait for `signal` to hold a value that passes `pred`, and return it. */
async function waitFor<T>(signal: Getter<T>, pred: (value: T) => boolean): Promise<T> {
for (;;) {
const value = signal.peek();
if (pred(value)) return value;
await signal.changed();
}
}

// Closing the last subscriber to a track tears the wire subscription down, so the publisher stops
// serving it (the muted-watch-tile case in #2355) instead of sending groups to a reader that left.
async function runSubscriberTeardown(protocol: string, version?: number) {
Expand Down
17 changes: 5 additions & 12 deletions js/net/src/lite/datagram-tracks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Signal } from "@moq/signals";
import { accept, connect } from "../connection/index.ts";
import { SessionCode } from "../error.ts";
import { hooks } from "../internal.ts";
import { createMockTransportPair, driveUntil, type MockTransport, manual } from "../mock.ts";
import { createMockTransportPair, type MockTransport, manual } from "../mock.ts";
import { Producer as Origin } from "../origin.ts";
import * as Path from "../path.ts";
import { sendOrder } from "../priority.ts";
Expand Down Expand Up @@ -62,9 +62,7 @@ for (const alpn of [ALPN_05, ALPN_06, ALPN_07_WIP]) {
datagrams.finish();
track?.finish();
// The end waits out the grace a late datagram gets.
const end = reader.recv();
await driveUntil(clock, end);
expect(await end).toBeUndefined();
expect(await clock.drive(reader.recv())).toBeUndefined();
} finally {
reader.close();
remote.close();
Expand Down Expand Up @@ -368,9 +366,7 @@ for (const alpn of [ALPN_05, ALPN_06, ALPN_07_WIP]) {
expect((await reader.recv())?.payload).toEqual(Uint8Array.of(1));
if (finish) {
// The end waits out the grace a late datagram gets.
const end = reader.recv();
await driveUntil(clock, end);
expect(await end).toBeUndefined();
expect(await clock.drive(reader.recv())).toBeUndefined();
} else {
expect((await reader.recv())?.payload).toEqual(Uint8Array.of(2));
}
Expand Down Expand Up @@ -410,9 +406,7 @@ for (const alpn of [ALPN_05, ALPN_06, ALPN_07_WIP]) {
datagrams.finish();
expect((await reader.recv())?.payload).toEqual(Uint8Array.of(1));
// The end waits out the grace a late datagram gets.
const end = reader.recv();
await driveUntil(clock, end);
expect(await end).toBeUndefined();
expect(await clock.drive(reader.recv())).toBeUndefined();
expect((await client.stats()).datagramsUnrouted).toBe(0);
} finally {
reader.close();
Expand Down Expand Up @@ -459,8 +453,7 @@ for (const alpn of [ALPN_05, ALPN_06, ALPN_07_WIP]) {
return count;
})();
// The end waits out the grace a late datagram gets.
await driveUntil(clock, counted);
expect(await counted).toBe(BURST);
expect(await clock.drive(counted)).toBe(BURST);
expect((await client.stats()).datagramsUnrouted).toBe(0);
} finally {
budget.mockRestore();
Expand Down
Loading