diff --git a/js/clock/src/manual.ts b/js/clock/src/manual.ts index e8bc3d78f19..f742edf2b2e 100644 --- a/js/clock/src/manual.ts +++ b/js/clock/src/manual.ts @@ -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(work: PromiseLike): Promise { if (this.#driving) throw new Error("time driver already pending"); diff --git a/js/net/src/auth.test.ts b/js/net/src/auth.test.ts index 5b6c885fc95..ae1314dd423 100644 --- a/js/net/src/auth.test.ts +++ b/js/net/src/auth.test.ts @@ -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"; @@ -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"; @@ -38,12 +37,6 @@ function grant(publish: string[], subscribe: string[]): Grant { return { publish: patterns(...publish), subscribe: patterns(...subscribe) }; } -async function waitFor(getter: Getter, ready: (value: T) => boolean): Promise { - 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]) => diff --git a/js/net/src/connection/connect.test.ts b/js/net/src/connection/connect.test.ts index 0f516a9ed4f..6d6d4ad93ed 100644 --- a/js/net/src/connection/connect.test.ts +++ b/js/net/src/connection/connect.test.ts @@ -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"; @@ -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 { @@ -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(); diff --git a/js/net/src/connection/reload.test.ts b/js/net/src/connection/reload.test.ts index aa71649dc19..fb144cd4a93 100644 --- a/js/net/src/connection/reload.test.ts +++ b/js/net/src/connection/reload.test.ts @@ -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"; @@ -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. @@ -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> { + const paths = new Signal>(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 { const deadline = Instant.add(now(), Nano.fromMilli(Milli(1000))); @@ -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(); @@ -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) { - const dialed: (string | null)[] = []; + const dialed = new Signal([]); 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; @@ -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(); @@ -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(); @@ -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(""); @@ -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; @@ -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; @@ -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> }[] + >([]); const stub = function StubWebTransport() { const pair = createMockTransportPair(Lite.ALPN_05); const saw = new OriginProducer(); @@ -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; }; @@ -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(); @@ -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(); diff --git a/js/net/src/ietf/tail.test.ts b/js/net/src/ietf/tail.test.ts index ded72ec1d22..958c2e43323 100644 --- a/js/net/src/ietf/tail.test.ts +++ b/js/net/src/ietf/tail.test.ts @@ -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"; @@ -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); }); diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index c870da42db7..7106b0f7882 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -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 { @@ -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"; @@ -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(signal: Getter, pred: (value: T) => boolean): Promise { - 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) { diff --git a/js/net/src/lite/datagram-tracks.test.ts b/js/net/src/lite/datagram-tracks.test.ts index e6567c638ce..e8c81d01445 100644 --- a/js/net/src/lite/datagram-tracks.test.ts +++ b/js/net/src/lite/datagram-tracks.test.ts @@ -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"; @@ -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(); @@ -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)); } @@ -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(); @@ -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(); diff --git a/js/net/src/mock.test.ts b/js/net/src/mock.test.ts index 2d143fe3be1..e3a661ce49e 100644 --- a/js/net/src/mock.test.ts +++ b/js/net/src/mock.test.ts @@ -1,6 +1,9 @@ import { expect, test } from "bun:test"; +import { Elapsed, Instant, Milli, Nano, now, sleep, timeout } from "@moq/clock"; import { StreamCode, toTransport } from "./error.ts"; -import { createMockTransportPair } from "./mock.ts"; +import { createMockTransportPair, manual } from "./mock.ts"; + +const ms = (n: number) => Nano.fromMilli(Milli(n)); async function pair() { const { client, server } = createMockTransportPair(); @@ -53,3 +56,59 @@ test("a write parked on a full peer gives up when the peer stops", async () => { const last = await writes[writes.length - 1]; expect(last).toMatchObject({ source: "stream", streamErrorCode: StreamCode.Cancel }); }); + +test("drive delivers a queued round trip before its deadline and stops at the deadline the work then meets", async () => { + using clock = manual(); + const { local, remote } = await pair(); + let late = false; + void sleep(ms(60_000)).then(() => { + late = true; + }); + // The peer answers only once the request reaches it, so the response is a second delivery. + void (async () => { + const { value } = await remote.readable.getReader().read(); + if (value) await remote.writable.getWriter().write(value); + })(); + const work = (async () => { + await local.writable.getWriter().write(Uint8Array.of(7)); + const { value } = await timeout(ms(1000), local.readable.getReader().read()); + await sleep(ms(10)); + return { value, at: now() }; + })(); + expect(await clock.drive(work)).toEqual({ value: Uint8Array.of(7), at: Instant(ms(10)) }); + expect(late).toBe(false); +}); + +test("drive times out a read with nothing in flight at its deadline", async () => { + using clock = manual(); + const { local } = await pair(); + const read = timeout(ms(1000), local.readable.getReader().read()); + expect(await clock.drive(read.catch((e: unknown) => e))).toBeInstanceOf(Elapsed); + expect(clock.now()).toBe(Instant(ms(1000))); +}); + +test("drive reaches a deadline while deliveries keep arriving", async () => { + using clock = manual(); + const { local, remote } = await pair(); + // A peer that refills the stream's queue on every host turn, so a delivery is always pending. + let delivered = 0; + const reader = remote.readable.getReader(); + void (async () => { + while (!(await reader.read()).done) delivered++; + })(); + const writer = local.writable.getWriter(); + let sending = true; + const send = () => { + if (!sending) return; + while ((writer.desiredSize ?? 0) > 0) void writer.write(Uint8Array.of(1)); + setImmediate(send); + }; + send(); + try { + await clock.drive(sleep(ms(1000))); + expect(clock.now()).toBe(Instant(ms(1000))); + expect(delivered).toBeGreaterThan(0); + } finally { + sending = false; + } +}); diff --git a/js/net/src/mock.ts b/js/net/src/mock.ts index 7e9e91f4491..a505fbb13b9 100644 --- a/js/net/src/mock.ts +++ b/js/net/src/mock.ts @@ -4,7 +4,8 @@ * Creates paired client/server transports connected by in-memory streams. */ -import { Manual, Milli, Nano, yieldNow } from "@moq/clock"; +import { Manual, Milli, Nano } from "@moq/clock"; +import type { Getter } from "@moq/signals"; import { Consumer as BroadcastConsumer } from "./broadcast.ts"; import type { TransportStats } from "./connection/stats.ts"; import type { Frame } from "./group.ts"; @@ -38,30 +39,11 @@ export async function settlesWithin( return settled; } -/** - * Moves `clock` a millisecond at a time, each step after a host turn, until `until` holds or settles. - * - * `Manual.drive` fires the next deadline after one drain, which skips past work waiting on a host - * task the clock can't see, such as a read through the host's streams or the IETF handshake's - * dynamic import. A regression fails within ten mocked seconds. - */ -export async function driveUntil(clock: Manual, until: (() => boolean) | PromiseLike): Promise { - let done: () => boolean; - if (typeof until === "function") { - done = until; - } else { - let settled = false; - const stop = () => { - settled = true; - }; - Promise.resolve(until).then(stop, stop); - done = () => settled; - } - for (let i = 0; i < 10_000 && !done(); i++) { - await yieldNow(); - await clock.advance(Nano.fromMilli(Milli(1))); - } - if (!done()) throw new Error("not done within ten mocked seconds"); +/** Resolves with `getter`'s value once `ready` holds for it, for a test awaiting a state change. */ +export async function waitFor(getter: Getter, ready: (value: T) => boolean): Promise { + let value = getter.peek(); + while (!ready(value)) value = await getter.changed(); + return value; } /** diff --git a/js/net/src/tail.test.ts b/js/net/src/tail.test.ts index a450f8df459..76396bf244e 100644 --- a/js/net/src/tail.test.ts +++ b/js/net/src/tail.test.ts @@ -5,7 +5,7 @@ import { accept, connect } from "./connection/index.ts"; import * as Ietf from "./ietf/index.ts"; import { GRACE as TAIL_GRACE_MS } from "./ledger.ts"; import * as Lite from "./lite/index.ts"; -import { createMockTransportPair, driveUntil, manual, textFrame } from "./mock.ts"; +import { createMockTransportPair, manual, textFrame } from "./mock.ts"; import { Producer as OriginProducer } from "./origin.ts"; import * as Path from "./path.ts"; import { Tail } from "./tail.ts"; @@ -101,7 +101,7 @@ test.each([Ietf.ALPN.DRAFT_22])("%s: END_OF_TRACK carries the declared end", asy video.close(); const read = readAll(reader); - await driveUntil(clock, reader.closed); + await clock.drive(reader.closed); // Every counted stream arrived, so nothing waited out the grace. expect(Nano.toMilli(Instant.durationSince(clock.now(), ended))).toBeLessThan(TAIL_GRACE_MS); expect(await read).toEqual(["1", "2"]); diff --git a/quest/m1/mock-clock-tests/README.md b/quest/m1/mock-clock-tests/README.md index 092bc3fb18d..92beb185a5e 100644 --- a/quest/m1/mock-clock-tests/README.md +++ b/quest/m1/mock-clock-tests/README.md @@ -39,7 +39,3 @@ Rules every child keeps: The counts come from a heuristic scan on 2026-11-06; re-scan each area before starting, since tests land and move. - -## Required - -- [Manual.drive waits for mocked I/O the work is blocked on](/quest/m1/mock-clock-tests/manual-drive-host-io.md) - `drive` stops at the deadline the work meets, and `js/net`'s `driveUntil` workaround is deleted diff --git a/quest/m1/mock-clock-tests/manual-drive-host-io.md b/quest/m1/mock-clock-tests/manual-drive-host-io.md deleted file mode 100644 index 75a4b92ce81..00000000000 --- a/quest/m1/mock-clock-tests/manual-drive-host-io.md +++ /dev/null @@ -1,44 +0,0 @@ -# [M] Manual.drive waits for mocked I/O the work is blocked on - -## Goal - -`Manual.drive` (`js/clock/src/manual.ts`) stops time exactly at the deadline -the driven work meets, even when the work first waits on a mocked transport -delivery. `js/net`'s `driveUntil` test helper is deleted: every caller -passes `drive` a promise, a predicate caller one that resolves on the event -its predicate reads. - -## Plan - -Planned 2026-11-07 (maintainer, /quest-complete) from -[moq-dev/moq#8165](https://github.com/moq-dev/moq/pull/8165), which adds -`driveUntil` to `js/net/src/mock.ts` as a workaround: it alternates -`yieldNow()` with 1 ms `advance` steps for up to ten mocked seconds. - -`drive` advances once a `drain()` (one host task) leaves no counted yield -queued, so work parked on a mock delivery that resolves on a later, -uncounted host task looks idle, and `drive` fires the next deadline early. - -Decided at the design checkpoint (maintainer, 2026-11-07): ✅ this quest -states outcomes and required tests, and the mechanism is chosen in its PR. -Not chosen: prescribing one here, which took three review rounds without -settling. Required tests: - -- a read whose delivery is queued resolves before the clock passes its - deadline, and the clock stops at the deadline the work then meets; -- a read with nothing in flight times out when its deadline comes; -- continuous background deliveries never starve a deadline. - -Also settled: - -- A predicate caller (most of them, such as the ~19 in - `js/net/src/connection/reload.test.ts`) passes `drive` a promise that - resolves on the signal or event its predicate reads, per the epic's - "await the event it means" rule, so a timer the event depends on still - fires. `drive` stays promise-only. -- A dynamic `import()` is out of scope: JavaScript exposes no "host queue - empty" signal, so a clock-side quiescence check would be a quiet window, - which the epic rules out. A test whose work imports a module loads it - before driving. - -Public API: none beyond `@moq/clock`'s test helpers, if any. Wire: none.