diff --git a/doc/lib/js/clock.md b/doc/lib/js/clock.md index 3e4e89b5623..830324f3b56 100644 --- a/doc/lib/js/clock.md +++ b/doc/lib/js/clock.md @@ -46,7 +46,10 @@ Use it for dates, rather than measuring elapsed time. A Rust wall-time boundary must convert its units explicitly. A new `Manual` starts at instant zero and 2025-01-01 UTC. `await advance()` moves both readings through each due timer deadline, draining tasks between deadlines, including timers those tasks -re-arm before the target. `jump()` moves straight to the target and fires due +re-arm before the target. `yieldNow()` waits for a later host task, giving the +host's other work a turn without ordering it; a `Manual` clock moves time only +once every pending yield has resumed, so a loop that yields mid-work never sees +a later time. `jump()` moves straight to the target and fires due timers late without draining, modeling a stalled event loop. `stepWall()` changes only wall time. `fire()` waits for a timer to be armed, even by a later host task such as I/O, then advances exactly to the diff --git a/js/clock/api/index.api.md b/js/clock/api/index.api.md index 44cef16c6d1..a235e7946b6 100644 --- a/js/clock/api/index.api.md +++ b/js/clock/api/index.api.md @@ -135,4 +135,6 @@ export function sleepUntil(at: Instant, options?: { export function timeout(duration: Nano, operation: PromiseLike): Promise; export function wall(): Wall; + +export function yieldNow(): Promise; ``` diff --git a/js/clock/src/backend.ts b/js/clock/src/backend.ts index ba328dfccb0..896abde7f67 100644 --- a/js/clock/src/backend.ts +++ b/js/clock/src/backend.ts @@ -6,4 +6,7 @@ export type Backend = { origin(): number; // `ref: false` must not keep the host process alive. arm(at: number, ready: () => void, ref: boolean): () => void; + // Call `ready` from a later host task, after every already-queued microtask. Tasks from + // other sources, such as I/O, may still run after it. + task(ready: () => void): void; }; diff --git a/js/clock/src/host.ts b/js/clock/src/host.ts index 840afcfba54..83f14415d31 100644 --- a/js/clock/src/host.ts +++ b/js/clock/src/host.ts @@ -27,18 +27,20 @@ export const host: Backend = { if (timer !== undefined) clearTimeout(timer); }; }, -}; - -// A host task drains every already-queued microtask, including promise chains; -// counting Promise.resolve() turns would impose an arbitrary depth limit. -export function drain(): Promise { - return new Promise((resolve) => { + task(ready) { + // Message ports schedule a task without the nested timer's minimum delay. const channel = new MessageChannel(); channel.port1.onmessage = () => { channel.port1.close(); channel.port2.close(); - resolve(); + ready(); }; channel.port2.postMessage(null); - }); + }, +}; + +// A host task drains every already-queued microtask, including promise chains; +// counting Promise.resolve() turns would impose an arbitrary depth limit. +export function drain(): Promise { + return new Promise((resolve) => host.task(resolve)); } diff --git a/js/clock/src/index.test.ts b/js/clock/src/index.test.ts index 00e5e06f08a..e69854897fd 100644 --- a/js/clock/src/index.test.ts +++ b/js/clock/src/index.test.ts @@ -18,6 +18,7 @@ import { timeout, Wall, wall, + yieldNow, } from "./index.ts"; const installs: Installed[] = []; @@ -112,6 +113,21 @@ test("advance drains arbitrarily deep microtasks and newly due timers", async () expect(done).toBe(true); }); +for (const driver of ["advance", "fire"] as const) { + test(`${driver} waits out a chain of host-task yields before moving time`, async () => { + const clock = manual(); + void sleep(Nano(10)); + const seen: number[] = []; + void (async () => { + for (let i = 0; i < 3; i++) await yieldNow(); + seen.push(now()); + })(); + if (driver === "advance") await clock.advance(Nano(10)); + else expect(await clock.fire()).toBe(10); + expect(seen).toEqual([0]); + }); +} + test("a sleep stays on its creation clock and abort releases it", async () => { const first = manual(); let done = false; diff --git a/js/clock/src/index.ts b/js/clock/src/index.ts index 47f93c41334..832720d1ced 100644 --- a/js/clock/src/index.ts +++ b/js/clock/src/index.ts @@ -10,7 +10,7 @@ import { Wall } from "./types.ts"; export type { Installed } from "./ambient.ts"; export { Manual } from "./manual.ts"; -export { Elapsed, Interval, interval, MissedTickBehavior, now, sleep, sleepUntil, timeout } from "./timer.ts"; +export { Elapsed, Interval, interval, MissedTickBehavior, now, sleep, sleepUntil, timeout, yieldNow } from "./timer.ts"; export { Instant, Micro, Milli, Nano, Second, Wall } from "./types.ts"; /** Read the installed clock's wall time in Unix milliseconds. */ diff --git a/js/clock/src/manual.ts b/js/clock/src/manual.ts index 7df6931d0bb..e8bc3d78f19 100644 --- a/js/clock/src/manual.ts +++ b/js/clock/src/manual.ts @@ -1,6 +1,6 @@ import { type Installed, install } from "./ambient.ts"; import type { Backend } from "./backend.ts"; -import { drain } from "./host.ts"; +import { drain, host } from "./host.ts"; import { Instant, Nano, Wall } from "./types.ts"; type Timer = { at: number; order: number; index: number; ready: () => void }; @@ -20,6 +20,7 @@ export class Manual { #order = 0; #revision = 0; #fired = 0; + #tasks = 0; #driving = false; #advancing = false; #armed?: () => void; @@ -38,6 +39,15 @@ export class Manual { this.#remove(timer); }; }, + // Yields stay real host tasks, so the host's other work still gets a turn, but the clock counts them. + task: (ready) => { + this.#tasks++; + this.#revision++; + host.task(() => { + this.#tasks--; + ready(); + }); + }, }; /** Install this clock in the current realm until the returned handle closes. */ @@ -67,7 +77,7 @@ export class Manual { } } - /** Advance through each due deadline, draining tasks before continuing to the target. */ + /** Advance through each due deadline, draining tasks and pending yields before continuing to the target. */ async advance(duration: Nano): Promise { if (this.#driving) throw new Error("time driver already pending"); this.#driving = true; @@ -131,6 +141,8 @@ export class Manual { for (;;) { const revision = this.#revision; await drain(); + // A yield armed while draining is still queued: time must not pass a yielding loop. + if (this.#tasks > 0) continue; if (target === undefined) { if (stop?.()) return; // Select fire's target in the same turn that readies its first timer. diff --git a/js/clock/src/timer.ts b/js/clock/src/timer.ts index e3009febbb7..96695107259 100644 --- a/js/clock/src/timer.ts +++ b/js/clock/src/timer.ts @@ -49,6 +49,12 @@ export function sleep(duration: Nano, options?: { signal?: AbortSignal; ref?: bo return wait(clock, Instant.add(Instant(clock.now()), duration), options).promise; } +/** Complete in a later host task, giving the host's other work a turn; a `Manual` clock won't advance past it. */ +export function yieldNow(): Promise { + const clock = backend(); + return new Promise((resolve) => clock.task(resolve)); +} + /** A timeout expired before its operation completed. */ export class Elapsed extends Error { /** Build a deadline-elapsed error. */ diff --git a/js/net/src/auth.test.ts b/js/net/src/auth.test.ts index 06ca66a4a98..fe4a01c35dc 100644 --- a/js/net/src/auth.test.ts +++ b/js/net/src/auth.test.ts @@ -1,5 +1,5 @@ import { describe, expect, spyOn, test } from "bun:test"; -import { Manual, Milli, Nano } from "@moq/clock"; +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"; @@ -16,7 +16,6 @@ import * as Path from "./path.ts"; import { Stream, Writer } from "./stream.ts"; import { abandoned } from "./util/demand.ts"; import { withTimeout } from "./util/timeout.ts"; -import { nextTask } from "./util/yield.ts"; import { wireOf } from "./wire.ts"; const url = new URL("https://localhost:4443/test"); @@ -54,7 +53,7 @@ function waited(logged: { mock: { calls: unknown[][] } }, path: string): boolean /** Yield until `done` holds, failing if it never does. */ async function settles(done: () => boolean): Promise { - for (let i = 0; i < 50 && !done(); i++) await nextTask(); + for (let i = 0; i < 50 && !done(); i++) await yieldNow(); expect(done()).toBe(true); } @@ -112,7 +111,7 @@ describe.each([Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_22])("%s", (protocol) => { void token.closed.then(() => { ended = true; }); - await nextTask(); + await yieldNow(); expect(ended).toBe(false); const second = await connect({ publish: new OriginProducer(), protocol, auth: handle }); @@ -162,7 +161,7 @@ describe.each([Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_22])("%s", (protocol) => { streams[0]?.abort(new StreamError(StreamCode.Internal, { message: "lost" })); await waitFor(token.grant, (g) => g === undefined); await clock.advance(Nano.fromMilli(Milli(999))); - for (let i = 0; i < 10; i++) await nextTask(); + for (let i = 0; i < 10; i++) await yieldNow(); expect(streams.length).toBe(1); await clock.advance(Nano.fromMilli(Milli(1))); await waitFor(token.grant, (g) => g?.publish.equals(patterns("a")) === true); @@ -335,9 +334,9 @@ describe.each([Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_22])("%s", (protocol) => { if (!unanswered) throw new Error("missing token"); const withdrawn = origin.createBroadcast(Path.from("foo/bar")); withdrawn.announce(); - await nextTask(); + await yieldNow(); withdrawn.unannounce(); - await nextTask(); + await yieldNow(); const issued = unanswered.accept(grant(["other"], [])); await adding; // Still enforcing: the next uncovered broadcast is the one named. @@ -477,7 +476,7 @@ describe.each([Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_22])("%s", (protocol) => { const unanswered = await requests.next(); if (!unanswered) throw new Error("missing token"); const track = wireOf(client).consume(Path.from("room/x")).track("video").subscribe(); - await nextTask(); + await yieldNow(); expect(track.closed.peek()).toBeUndefined(); clock.jump(Nano.fromMilli(Milli(7))); const firstGrant = unanswered.accept(grant([], ["room"])); @@ -499,7 +498,7 @@ describe.each([Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_22])("%s", (protocol) => { const renewedGrant = renewal.accept(grant([], ["room"])); const renewed = await renewing; first.close(); - await nextTask(); + await yieldNow(); expect(track.closed.peek()).toBeUndefined(); published.appendGroup().writeFrame({ payload: new Uint8Array([2]) }); expect(await track.recvGroup()).toBeDefined(); @@ -537,7 +536,7 @@ describe.each([Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_22])("%s", (protocol) => { void ready.then(() => { settled = true; }); - await nextTask(); + await yieldNow(); expect(settled).toBe(false); reader.close(); expect(await ready).toBe(false); @@ -568,13 +567,13 @@ describe.each([Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_22])("%s", (protocol) => { const unanswered = await requests.next(); if (!unanswered) throw new Error("missing token"); const track = wireOf(client).consume(Path.from("room/x")).track("video").subscribe(); - await nextTask(); + await yieldNow(); track.close(); await settles(() => waited(logged, "room/x")); const opened = transport.sendStreams.bidi.length; const issued = unanswered.accept(grant([], ["room"])); await adding; - for (let i = 0; i < 10; i++) await nextTask(); + for (let i = 0; i < 10; i++) await yieldNow(); expect(transport.sendStreams.bidi.length).toBe(opened); issued.close(); } finally { @@ -609,7 +608,7 @@ describe.each([Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_22])("%s", (protocol) => { if (!unanswered) throw new Error("missing token"); const track = wireOf(client).consume(Path.from("room/x")).track("video"); const first = track.subscribe(); - await nextTask(); + await yieldNow(); first.close(); for (let i = 0; i < gap; i++) await Promise.resolve(); const second = track.subscribe(); @@ -646,7 +645,7 @@ describe.each([Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_22])("%s", (protocol) => { // reaches the subscription; force that order. wireOf(client).auth.close(); expect(wireOf(client).auth.grant.peek()?.subscribe.size).toBe(0); - await nextTask(); + await yieldNow(); expect(track.closed.peek()).toBeUndefined(); client.abort(); @@ -709,7 +708,7 @@ test("closing a session settles its unanswered setup token", async () => { .then(() => { settled = true; }); - for (let i = 0; i < 10; i++) await nextTask(); + for (let i = 0; i < 10; i++) await yieldNow(); expect(settled).toBe(false); client.abort(); await settles(() => settled); @@ -747,7 +746,7 @@ test("lite-07 presents a transiently refused token again after a backoff", async return request; }); await clock.advance(Nano.fromMilli(Milli(backoff - 1))); - for (let i = 0; i < 10; i++) await nextTask(); + for (let i = 0; i < 10; i++) await yieldNow(); expect(presented).toBe(false); expect(added).toBe(false); await clock.advance(Nano.fromMilli(Milli(1))); @@ -820,7 +819,7 @@ test("lite-07 presents a shared token again when its retry stream is reset", asy ); await clock.advance(Nano.fromMilli(Milli(1000))); await settles(() => logged("auth token ended") > 0); - for (let i = 0; i < 10; i++) await nextTask(); + for (let i = 0; i < 10; i++) await yieldNow(); expect(rejected).toBeUndefined(); await clock.advance(Nano.fromMilli(Milli(2000))); @@ -865,7 +864,7 @@ test("lite-07 presents a token added after a GOAWAY only on the successor", asyn answer(successor.server.auth.requests(), (token) => (token ? grant(["second"], []) : grant([], []))); await late; await waitFor(handle.grant, (g) => g?.publish.equals(patterns("second")) === true); - for (let i = 0; i < 10; i++) await nextTask(); + for (let i = 0; i < 10; i++) await yieldNow(); expect(seen).not.toContain("late"); client.abort(); @@ -929,7 +928,7 @@ test("lite-07 ignores a draining session's late refusal once its successor grant unanswered?.reject(SessionCode.Unauthorized, "stale"); try { await settles(() => warned.mock.calls.some(([line]) => line === "auth token refused")); - for (let i = 0; i < 10; i++) await nextTask(); + for (let i = 0; i < 10; i++) await yieldNow(); expect(ended).toBe(false); expect(token.grant.peek()?.publish.equals(patterns("second"))).toBe(true); } finally { @@ -994,7 +993,7 @@ test("lite-07 ignores a draining session's late refusal while its successor retr const stale = refusals(); unanswered?.reject(SessionCode.Unauthorized, "stale"); await settles(() => refusals() > stale); - for (let i = 0; i < 10; i++) await nextTask(); + for (let i = 0; i < 10; i++) await yieldNow(); expect(ended).toBe(false); const retried = successorRequests.next(); @@ -1041,14 +1040,14 @@ test("a fetch abandoned while its token waits never reaches the wire", async () .consume(Path.from("room/x")) .track("video") .fetchGroup(0, { signal: abort.signal }); - await nextTask(); + await yieldNow(); abort.abort(new Error("left")); await expect(fetched).rejects.toThrow(); await settles(() => waited(logged, "room/x")); const opened = transport.sendStreams.bidi.length; const issued = unanswered.accept(grant([], ["room"])); await adding; - for (let i = 0; i < 10; i++) await nextTask(); + for (let i = 0; i < 10; i++) await yieldNow(); expect(transport.sendStreams.bidi.length).toBe(opened); issued.close(); } finally { @@ -1079,7 +1078,7 @@ test("a listing waits for its token before reaching the wire", async () => { if (!unanswered) throw new Error("missing token"); const opened = transport.sendStreams.bidi.length; const tracks = wireOf(client).consume(Path.from("room/x")).tracks(); - for (let i = 0; i < 10; i++) await nextTask(); + for (let i = 0; i < 10; i++) await yieldNow(); expect(transport.sendStreams.bidi.length).toBe(opened); const issued = unanswered.accept(grant([], ["room"])); await adding; @@ -1121,7 +1120,7 @@ test.each(Array.from({ length: 17 }, (_, gap) => gap))( const track = wireOf(client).consume(Path.from("room/x")).track("video"); const abort = new AbortController(); const first = track.fetchGroup(0, { signal: abort.signal }); - await nextTask(); + await yieldNow(); // The last caller leaves and another coalesces before the wait observes it. abort.abort(new Error("left")); for (let i = 0; i < gap; i++) await Promise.resolve(); @@ -1236,7 +1235,7 @@ test("a GOING_AWAY refusal leaves a shared token for the next session on moq-tra }); try { await settles(() => warned.mock.calls.some(([line]) => line === "auth token refused")); - for (let i = 0; i < 10; i++) await nextTask(); + for (let i = 0; i < 10; i++) await yieldNow(); expect(rejected).toBeUndefined(); } finally { read.mockRestore(); diff --git a/js/net/src/auth_session.test.ts b/js/net/src/auth_session.test.ts index f7ea919b827..db54086676f 100644 --- a/js/net/src/auth_session.test.ts +++ b/js/net/src/auth_session.test.ts @@ -1,9 +1,9 @@ import { expect, test } from "bun:test"; +import { yieldNow } from "@moq/clock"; import { type Getter, Signal } from "@moq/signals"; import type { Grant } from "./auth.ts"; import { type AuthSession, enforceGrant } from "./auth_session.ts"; import * as Path from "./path.ts"; -import { nextTask } from "./util/yield.ts"; /** How many reactions `run` attached to each promise, by `then` on it. */ async function reactions(run: () => Promise): Promise { @@ -34,7 +34,7 @@ test.each([2, 64])("churn over %i live broadcasts leaves no reaction behind per // A live table that keeps changing: one broadcast swaps out per update. for (let i = 0; i < 1_000; i++) { advertised.set(new Map(Array.from({ length: live }, (_, j) => [Path.from(`live/${i + j}`), i]))); - await nextTask(); + await yieldNow(); } close(); await enforcing; @@ -75,11 +75,11 @@ test("a session that closes during an authorization wait ends enforcement withou const enforcing = enforceGrant({ quic, advertised, grant, setupAnswered: Promise.resolve(), auth }).then(() => { done = true; }); - for (let i = 0; i < 10 && waiting === 0; i++) await nextTask(); + for (let i = 0; i < 10 && waiting === 0; i++) await yieldNow(); expect(waiting).toBe(1); close(); - for (let i = 0; i < 10 && !done; i++) await nextTask(); + for (let i = 0; i < 10 && !done; i++) await yieldNow(); expect(done).toBe(true); expect(closes).toBe(0); await enforcing; diff --git a/js/net/src/auth_session.ts b/js/net/src/auth_session.ts index 8fda004f5f9..8df4df55918 100644 --- a/js/net/src/auth_session.ts +++ b/js/net/src/auth_session.ts @@ -1,10 +1,9 @@ -import { Instant, Milli, Nano, now, sleep, wall } from "@moq/clock"; +import { Instant, Milli, Nano, now, sleep, wall, yieldNow } from "@moq/clock"; import { type Dispose, type Getter, Once, race, Signal } from "@moq/signals"; import type { Credential, Grant, Issued, Request, Requests } from "./auth.ts"; import { closeReason, error, ProtocolViolation, SessionCode, SessionError, StreamCode, StreamError } from "./error.ts"; import * as Path from "./path.ts"; import type { Reader, Stream, Writer } from "./stream.ts"; -import { nextTask } from "./util/yield.ts"; /** Write a grant's patterns as a count and each one's canonical text, the encoding both wires' AUTH_OK share. @internal */ export async function encodePatterns(w: Writer, patterns: Path.Patterns) { @@ -585,7 +584,7 @@ export class AuthSession { // Whoever answers the peer's tokens is decided once, after the task that established // the session: an app that calls requests() as soon as connect/accept resolves always // wins, however quickly the peer's first token arrives. - #decided = nextTask(); + #decided = yieldNow(); /** Throws {@link Duplicate} while `handle` backs another active session, or the handle's end. */ constructor({ wire, requests, peerGrant, handle, draining }: AuthSessionProps) { diff --git a/js/net/src/connection/connect.test.ts b/js/net/src/connection/connect.test.ts index 5ccd92cb3bd..2458bb81de2 100644 --- a/js/net/src/connection/connect.test.ts +++ b/js/net/src/connection/connect.test.ts @@ -466,6 +466,8 @@ test("a client refuses an IETF version selected in the moql SETUP", async () => test("a server refuses a client offering only IETF versions in the moql SETUP", async () => { const pair = createMockTransportPair(""); const accepting = accept({ url, transport: pair.server }); + // Handled now, since the refusal can land while the write below yields. + accepting.catch(() => {}); const stream = await Stream.open(pair.client, { version: Lite.DRAFT_02 }); await new ClientSetup([DRAFT_14]).encode(stream.writer); await expect(accepting).rejects.toThrow("no common version found; client offered: ff00000e"); diff --git a/js/net/src/connection/migrate.test.ts b/js/net/src/connection/migrate.test.ts index 59162933992..58e99e612ae 100644 --- a/js/net/src/connection/migrate.test.ts +++ b/js/net/src/connection/migrate.test.ts @@ -1,12 +1,11 @@ import { afterEach, expect, spyOn, test } from "bun:test"; -import { Instant, Manual, Milli, Nano } from "@moq/clock"; +import { Instant, Manual, Milli, Nano, yieldNow } from "@moq/clock"; import { RefusedRedirect } from "../error.ts"; import * as Lite from "../lite/index.ts"; import { createMockTransportPair } from "../mock.ts"; import { Producer as OriginProducer } from "../origin.ts"; import * as Path from "../path.ts"; import { Stream } from "../stream.ts"; -import { nextTask } from "../util/yield.ts"; import { accept } from "./accept.ts"; import { Connection, resetShared } from "./pool.ts"; import { Redirects, Reload } from "./reload.ts"; @@ -298,7 +297,7 @@ test("the connection's tokens ride every session across a GOAWAY", async () => { // so the reconnect's delay passes long before the draining session's handover. const until = async (done: () => boolean) => { for (let i = 0; i < 100 && !done(); i++) { - await nextTask(); + await yieldNow(); await clock.advance(Nano.fromMilli(Milli(1))); } expect(done()).toBe(true); @@ -351,13 +350,13 @@ test("the connection's tokens ride every session across a GOAWAY", async () => { // session keeps presenting it beside its successor. await until(() => relays[1]?.tokens.includes("token") === true); await until(() => token.grant.peek() !== undefined && reload.established.peek() !== undefined); - await nextTask(); + await yieldNow(); expect(first.withdrawn).not.toContain("token"); let ended = false; void token.closed.then(() => { ended = true; }); - await nextTask(); + await yieldNow(); expect(ended).toBe(false); // The tokens end with the loop. diff --git a/js/net/src/connection/pool.test.ts b/js/net/src/connection/pool.test.ts index 9479bd6d6a5..f849baf5145 100644 --- a/js/net/src/connection/pool.test.ts +++ b/js/net/src/connection/pool.test.ts @@ -1,5 +1,5 @@ -import { afterEach, beforeEach, expect, jest, test } from "bun:test"; -import { Milli } from "@moq/clock"; +import { afterEach, beforeEach, expect, test } from "bun:test"; +import { type Installed, Manual, Milli, Nano } from "@moq/clock"; import { Signal } from "@moq/signals"; import { SessionCode, SessionError } from "../error.ts"; import * as Lite from "../lite/index.ts"; @@ -26,15 +26,11 @@ function accept(transport: WebTransport, url: URL, props: Omit { + return clock.advance(Nano.fromMilli(Milli(ms))); } const linger = Milli(2000); @@ -42,7 +38,10 @@ const linger = Milli(2000); const original = globalThis.WebTransport; const originalWebSocket = globalThis.WebSocket; -beforeEach(() => jest.useFakeTimers()); +beforeEach(() => { + clock = new Manual(); + installed = clock.install(); +}); afterEach(async () => { resetShared(); @@ -50,7 +49,7 @@ afterEach(async () => { await advance(linger); globalThis.WebTransport = original; globalThis.WebSocket = originalWebSocket; - jest.useRealTimers(); + installed.close(); }); /** Hand out a live mock transport per dial, counting how many were opened. */ @@ -570,40 +569,26 @@ test("pooled probe readers share demand and carry it through reconnects", async return pair.client; }; globalThis.WebTransport = stub as unknown as typeof WebTransport; - const settle = () => - new Promise((resolve) => { - const channel = new MessageChannel(); - channel.port1.onmessage = () => { - channel.port1.close(); - channel.port2.close(); - resolve(); - }; - channel.port2.postMessage(null); - }); const first = new Connection({ url }); const second = new Connection({ url }); let stopFirst = () => {}; let stopSecond = () => {}; try { await advance(); - await settle(); expect(pairs[0].client.sendStreams.bidi).toHaveLength(0); stopFirst = first.probe.subscribe(() => {}); stopSecond = second.probe.subscribe(() => {}); await advance(); - await settle(); expect(pairs[0].client.sendStreams.bidi).toHaveLength(1); stopFirst(); await advance(100); expect(pairs[0].client.sendStreams.bidi).toHaveLength(1); pairs[0].server.close({ closeCode: SessionCode.Internal, reason: "reconnect" }); await advance(2_000); - await settle(); expect(pairs).toHaveLength(2); expect(pairs[1].client.sendStreams.bidi).toHaveLength(1); stopSecond(); await advance(); - await settle(); const before = statsCalls[1]; await advance(500); expect(statsCalls[1]).toBe(before); @@ -613,7 +598,6 @@ test("pooled probe readers share demand and carry it through reconnects", async first.close(); second.close(); await advance(linger); - await settle(); } }); diff --git a/js/net/src/connection/reload.test.ts b/js/net/src/connection/reload.test.ts index e4a27ed3f9d..071d8d427a5 100644 --- a/js/net/src/connection/reload.test.ts +++ b/js/net/src/connection/reload.test.ts @@ -1,5 +1,5 @@ import { expect, spyOn, test } from "bun:test"; -import { Instant, Manual, Milli, Nano } from "@moq/clock"; +import { Instant, Manual, Milli, Nano, yieldNow } from "@moq/clock"; import { Effect, Signal } from "@moq/signals"; import * as Announce from "../announced.ts"; import type { Producer as BroadcastProducer } from "../broadcast.ts"; @@ -13,7 +13,6 @@ import * as Path from "../path.ts"; import { Stream } from "../stream.ts"; import { Timescale } from "../time.ts"; import { closeUnwanted } from "../util/announce.ts"; -import { nextTask } from "../util/yield.ts"; import { wireOf } from "../wire.ts"; import type { Established } from "./established.ts"; import { type AcceptProps, accept as acceptSession } from "./index.ts"; @@ -269,7 +268,7 @@ test("an ended auth handle gives up instead of retrying", async () => { // so a reconnect, if the loop tried one, would be dialed well within the bound. const until = async (done: () => boolean) => { for (let i = 0; i < 100 && !done(); i++) { - await nextTask(); + await yieldNow(); await clock.advance(Nano.fromMilli(Milli(1))); } expect(done()).toBe(true); diff --git a/js/net/src/connection/stats.test.ts b/js/net/src/connection/stats.test.ts index 6e7b1155eff..8b9fe1abeec 100644 --- a/js/net/src/connection/stats.test.ts +++ b/js/net/src/connection/stats.test.ts @@ -1,5 +1,5 @@ -import { expect, jest, spyOn, test } from "bun:test"; -import { Milli } from "@moq/clock"; +import { expect, spyOn, test } from "bun:test"; +import { Manual, Milli, Nano } from "@moq/clock"; import { Effect } from "@moq/signals"; import * as Ietf from "../ietf/index.ts"; import * as Lite from "../lite/index.ts"; @@ -135,20 +135,10 @@ test("Reload exposes its established session stats and probe only while connecte } }); -function drain(): Promise { - return new Promise((resolve) => { - const channel = new MessageChannel(); - channel.port1.onmessage = () => { - channel.port1.close(); - channel.port2.close(); - resolve(); - }; - channel.port2.postMessage(null); - }); -} - test("a lite connection probes only while its estimate is observed", async () => { - jest.useFakeTimers(); + const clock = new Manual(); + const installed = clock.install(); + const drain = () => clock.advance(Nano.zero); const pair = createMockTransportPair(Lite.ALPN_06, { stats: { estimatedSendRate: 1_000_000 } }); const open = pair.client.createBidirectionalStream.bind(pair.client); let finished = 0; @@ -183,8 +173,7 @@ test("a lite connection probes only while its estimate is observed", async () => values.push(effect.get(client.probe)); }); await drain(); - jest.advanceTimersByTime(100); - await drain(); + await clock.advance(Nano.fromMilli(Milli(100))); expect(values.at(-1)).toMatchObject({ estimatedRecvRate: 1_000_000 }); // A reactive one-shot listener reattaches without dropping demand between updates. expect(pair.client.sendStreams.bidi).toHaveLength(1); @@ -205,6 +194,6 @@ test("a lite connection probes only while its estimate is observed", async () => client.abort(); server.abort(); opening.mockRestore(); - jest.useRealTimers(); + installed.close(); } }); diff --git a/js/net/src/ietf/subscriber.test.ts b/js/net/src/ietf/subscriber.test.ts index 2b328e42979..d2557c4c676 100644 --- a/js/net/src/ietf/subscriber.test.ts +++ b/js/net/src/ietf/subscriber.test.ts @@ -762,6 +762,35 @@ async function subscribeTrack({ return { subscriber, track }; } +// A refusal already ended the request: a reset back would tell the publisher we failed. +test("a refused SUBSCRIBE or SUBSCRIBE_NAMESPACE ends its stream cleanly", async () => { + const pair = createMockTransportPair(ALPN.DRAFT_18); + const subscriber = new Subscriber({ session: new NativeSession(pair.server, VERSION, true) }); + const refuse = async (peer: Stream) => { + await peer.writer.u53(RequestError.id); + await new RequestError({ errorCode: 0x10, reasonPhrase: "not here" }).encode(peer.writer, VERSION); + peer.writer.close(); + }; + + const announced = subscriber.announced(); + const namespace = await nextStream(pair.client); + if (!namespace) throw new Error("no SUBSCRIBE_NAMESPACE was sent"); + expect(await namespace.reader.u53()).toBe(SubscribeNamespace.id); + await SubscribeNamespace.decode(namespace.reader, VERSION); + await refuse(namespace); + expect(await namespace.reader.done()).toBe(true); + announced.close(); + + const track = subscriber.consume(Path.from("room")).track("video").subscribe(); + const subscribe = await nextStream(pair.client); + if (!subscribe) throw new Error("no SUBSCRIBE was sent"); + expect(await subscribe.reader.u53()).toBe(Subscribe.id); + await Subscribe.decode(subscribe.reader, VERSION); + await refuse(subscribe); + expect(await subscribe.reader.done()).toBe(true); + track.close(); +}); + /** One object carrying `properties` as its raw extension bytes, then a one-byte payload. */ function encodeStamped(properties: number[]): Uint8Array { // Every field here is under 64, so each is a one-byte varint. diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index 9e7aa82e3ba..827954c3496 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -553,6 +553,12 @@ export class Subscriber { } return count ?? 0; } + // A well-formed refusal already ended the request, so end ours cleanly before + // failing it. Anything else leaves the stream for the reset. + if (framed.type === RequestError.id) { + await RequestError.decode(framed.reader, version); + request.close(); + } throw new Error(`SubscribeNamespace rejected: typeId=0x${framed.type.toString(16)}`); })(); // A consumer that leaves before the peer answers ends the request now, not on the answer. @@ -842,7 +848,10 @@ export class Subscriber { if (!state.sent && !afterSetup) return; torn = true; - state.stream.abort(e); + // A refusal already ended the request, so end ours cleanly: a reset would tell the + // publisher we failed. + if (state.rejected) state.stream.close(); + else state.stream.abort(e); }; // Tear down what is already open rather than waiting on `setup`. A peer that @@ -1138,11 +1147,12 @@ export class Subscriber { if (response.type === RequestError.id) { const err = await RequestError.decode(response.reader, version); reasonPhrase = requestReason(err.errorCode, err.reasonPhrase, "subscribe", version); + // Only a well-formed refusal ended the request; cleanup resets anything else. + state.rejected = true; } } catch { // Decoding error response failed, use default message } - state.rejected = true; throw new Error(`SUBSCRIBE error: ${reasonPhrase}`); } diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index e634a42e56c..7298b8a31b1 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -1550,13 +1550,17 @@ test("lite draft-06: a newer update wins before a buffered group backlog drains" startGroup: 0, gated: true, }); + // Encoded up front, so the second update arrives whole the way a peer's would. + const capped = await Writer.collect(Version.DRAFT_06, (w) => + replayUpdate({ priority: 0, endGroup: 1 }).encode(w, Version.DRAFT_06), + ); let second!: Promise; const withRange = TrackSubscriber.prototype.withRange; const ranges = spyOn(TrackSubscriber.prototype, "withRange").mockImplementation(function ( this: TrackSubscriber, groups, ) { - second ??= replayUpdate({ priority: 0, endGroup: 1 }).encode(sub.client.writer, Version.DRAFT_06); + second ??= sub.client.writer.write(capped); return withRange.call(this, groups); }); diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 79246cdcaec..1e75de690c3 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -1,4 +1,4 @@ -import { Instant, Milli, Nano, now, sleep } from "@moq/clock"; +import { Instant, Milli, Nano, now, sleep, yieldNow } from "@moq/clock"; import { type Dispose, type Getter, Once, race, Signal } from "@moq/signals"; import type { Grant } from "../auth.ts"; import { Gate } from "../auth_gate.ts"; @@ -21,7 +21,6 @@ import type * as track from "../track.ts"; import { untilAborted } from "../util/abort.ts"; import { nextChange } from "../util/change.ts"; import { untilGivenUp } from "../util/given_up.ts"; -import { nextTask } from "../util/yield.ts"; import { type Advertised, type Advertisements, sameInstance, wireOf } from "../wire.ts"; import { AnnounceInit, AnnounceOk, type AnnounceRequest, encodeAnnounceBroadcast } from "./announce.ts"; import { Datagram as DatagramMessage } from "./datagram.ts"; @@ -1270,10 +1269,11 @@ export class Publisher { moveCursor(); // A microtask is too short: decoding one framed update crosses several // awaits, each of which can requeue behind the serving continuation. A - // task boundary lets the decoder finish whatever the transport already - // delivered before the next group pop. Updates are rare, so groups do - // not pay this scheduling cost on the normal path. - await nextTask(); + // task boundary usually lets the decoder finish whatever the transport + // already delivered before the next group pop. It orders nothing: an update + // decoded later applies from the pop after it. Updates are rare, so groups + // do not pay this scheduling cost on the normal path. + await yieldNow(); continue; } } diff --git a/js/net/src/lite/tracks.test.ts b/js/net/src/lite/tracks.test.ts index 036b8180c94..d1b25cc9078 100644 --- a/js/net/src/lite/tracks.test.ts +++ b/js/net/src/lite/tracks.test.ts @@ -226,17 +226,15 @@ test("a listing past the announces cap is reset, and the session stays", async ( const cap = 3; const { subscriber, listed, closing } = handPlayed({ limits: { announces: cap } }); const tracks = subscriber.consume(Path.from("room")).tracks(); + expect(await next(tracks)).toBe("synced"); const peer = await listed(); - for (let i = 0; i <= cap; i++) await peer.send({ kind: "added", name: `t${i}`, info }); - let added = 0; - let end: string; - for (;;) { - end = await next(tracks); - if (end.startsWith("+")) added++; - else if (end !== "synced") break; + // Read in step: the reset withdraws whatever adds are still unread. + for (let i = 0; i < cap; i++) { + await peer.send({ kind: "added", name: `t${i}`, info }); + expect(await next(tracks)).toBe(`+t${i}`); } - expect(added).toBe(cap); - expect(end).toMatch(/^error: /); + await peer.send({ kind: "added", name: `t${cap}`, info }); + expect(await next(tracks)).toMatch(/^error: /); await settle(); expect(closing).not.toHaveBeenCalled(); // The reset released every slot: another listing lists the whole cap again. diff --git a/js/net/src/net-cleanups.test.ts b/js/net/src/net-cleanups.test.ts index 45f9e20ff4c..43c64be8b41 100644 --- a/js/net/src/net-cleanups.test.ts +++ b/js/net/src/net-cleanups.test.ts @@ -84,13 +84,8 @@ for (const [version, alpn] of [ const server = new LiteConnection({ ...props, quic: pair.server }); const dispose = client.probe.subscribe(() => {}); try { - // The publisher samples on its 100ms PROBE cadence, and a shared write budget may - // yield a host task mid-delivery, so drive time until the estimate actually lands. - await clock.drive( - (async () => { - while (client.probe.peek().estimatedRecvRate === undefined) await client.probe.changed(); - })(), - ); + // The publisher samples on its 100ms PROBE cadence. + await clock.advance(Nano.fromMilli(Milli(100))); expect(client.probe.peek().estimatedRecvRate).toBe(8_000_000); expect(client.probe.peek().rtt).toBe(version === Version.DRAFT_03 ? undefined : Milli(12)); } finally { diff --git a/js/net/src/subscribe_priority.test.ts b/js/net/src/subscribe_priority.test.ts index 00787e37275..73dbcb78ba0 100644 --- a/js/net/src/subscribe_priority.test.ts +++ b/js/net/src/subscribe_priority.test.ts @@ -3,7 +3,7 @@ // relay case lives with the Rust relay (rs/moq-net/tests/integration/subscribe_priority.rs). import { expect, spyOn, test } from "bun:test"; -import { Manual } from "@moq/clock"; +import { Manual, yieldNow } from "@moq/clock"; import { accept, connect, type Established } from "./connection/index.ts"; import * as Ietf from "./ietf/index.ts"; import { SubscribeUpdate } from "./ietf/subscribe.ts"; @@ -14,7 +14,6 @@ import * as Path from "./path.ts"; import { Writer } from "./stream.ts"; import { Timescale } from "./time.ts"; import type { Producer as TrackProducer } from "./track.ts"; -import { nextTask } from "./util/yield.ts"; import { wireOf } from "./wire.ts"; const url = new URL("https://localhost:4443/test"); @@ -98,7 +97,7 @@ test.each(Object.values(Ietf.ALPN))("%s ends a subscription mid-update without a const write = Writer.prototype.write; const yielding = spyOn(Writer.prototype, "write").mockImplementation(async function (this: Writer, chunk) { await write.call(this, chunk); - await nextTask(); + await yieldNow(); }); const origin = new OriginProducer(); const broadcastPublication = origin.createBroadcast(path); @@ -122,7 +121,7 @@ test.each(Object.values(Ietf.ALPN))("%s ends a subscription mid-update without a low.close(); while (video.subscription.peek() !== undefined) await video.subscription.changed(); // Each hop is a task or two away, so this lets the subscriber read all the publisher wrote. - for (let i = 0; i < 20; i++) await nextTask(); + for (let i = 0; i < 20; i++) await yieldNow(); expect(closed).toBeUndefined(); remote.close(); } finally { diff --git a/js/net/src/util/yield.ts b/js/net/src/util/yield.ts index 350dd39b2db..36cf7e2b5ba 100644 --- a/js/net/src/util/yield.ts +++ b/js/net/src/util/yield.ts @@ -1,14 +1,4 @@ -/** - * Resolve after the host's next task, so I/O the host already queued runs first. - * - * Scheduling, not time: a manual clock must not hold it back, so it uses the host's timer directly. - * - * @internal - */ -export function nextTask(): Promise { - // biome-ignore lint/style/noRestrictedGlobals: a zero-delay task boundary, not a clock read. - return new Promise((resolve) => setTimeout(resolve, 0)); -} +import { yieldNow } from "@moq/clock"; // The slice measures real CPU time spent without yielding, which a manual clock never advances: // on one, the budget would never yield and a backlog could starve the host's own tasks. @@ -27,17 +17,9 @@ export class Budget { poll(): Promise | undefined { if (this.#pending !== undefined) return this.#pending; if (++this.#checks % 32 !== 0 || host.now() - this.#start < 4) return; - this.#pending = new Promise((resolve) => { - // Message ports schedule a task without the nested timer's minimum delay. - const channel = new MessageChannel(); - channel.port1.onmessage = () => { - channel.port1.close(); - channel.port2.close(); - this.#start = host.now(); - this.#pending = undefined; - resolve(); - }; - channel.port2.postMessage(null); + this.#pending = yieldNow().then(() => { + this.#start = host.now(); + this.#pending = undefined; }); return this.#pending; } diff --git a/quest/m0/test-flakes-2/README.md b/quest/m0/test-flakes-2/README.md index c76d5928ec6..3b77b0ecd92 100644 --- a/quest/m0/test-flakes-2/README.md +++ b/quest/m0/test-flakes-2/README.md @@ -51,4 +51,3 @@ Public API: none. Wire: none. - [FFI publisher audio gaps](/quest/m0/test-flakes-2/ffi-audio-gap.md) - interop tone checks stop finding gaps from FFI publishers - [IETF22 fallback](/quest/m0/test-flakes-2/ietf22-fallback.md) - an IETF22 browser viewer follows a fallback within the media lane's bound, keeping its session and its audio - [Swapped Rust to JS Opus](/quest/m0/test-flakes-2/swapped-js-opus.md) - the `rust-swapped -> js` interop cell stops timing out under load -- [JS host-task yields follow the clock](/quest/m0/test-flakes-2/js-host-yields.md) - `Budget` and `nextTask` yield through the injected clock, so `Manual` tests stop racing them diff --git a/quest/m0/test-flakes-2/js-host-yields.md b/quest/m0/test-flakes-2/js-host-yields.md deleted file mode 100644 index e32768abcc0..00000000000 --- a/quest/m0/test-flakes-2/js-host-yields.md +++ /dev/null @@ -1,34 +0,0 @@ -# [M] JS host-task yields follow the injected clock - -## Goal - -`Budget` and `nextTask` (`js/net/src/util/yield.ts`) yield through the -injected clock, so a `Manual` clock (`js/clock/src/manual.ts`) drives those -yields like its timers. Tests that advance a `Manual` clock pass whether or -not a `Budget` yields, and production keeps real host-task yields. - -## Plan - -Found while landing [moq-dev/moq#6905](https://github.com/moq-dev/moq/pull/6905), -whose new test raced the clock and was fixed per test with `clock.drive(...)`. -`Manual.advance` cannot see a host-task yield, so a test that advances past a -yielding loop observes state before the loop resumes. - -Decided 2026-10-27 (maintainer): the yields go through the clock, not -`Manual.advance` draining host tasks, which slows every advance and still -cannot tell when a yield chain has finished. - -Reproduce by forcing `Budget.poll` to always yield. These tests on main then -fail and are the acceptance set: - -- pooled probe readers share demand -- handle announcements sync with shared origin -- lite connection probes only while estimate observed -- listing past announces cap is reset -- integration: ietf blind handle picks up a publisher that arrives late (seen - once under a full `just check`) - -Once the clock owns the yields, drop per-test workarounds such as #6905's -`clock.drive(...)` where plain advances now suffice. - -Public API: `@moq/clock` gains whatever yield hook the clock needs. Wire: none.