diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index 98d57de162d..5724b4c2a58 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -42,6 +42,7 @@ import { SubscribeOk, type SubscribeResponse, SubscribeStart, + SubscribeUpdate, } from "./subscribe.ts"; import { Minted, SUBSCRIBE_SETUP_TIMEOUT_MS, Subscriber } from "./subscriber.ts"; import { Track, TrackInfo } from "./track.ts"; @@ -1010,6 +1011,8 @@ test("an unknown announce type between starts keeps the stream", async () => { interface FakeStream { inbound: ReadableStreamDefaultController; + // The subscriber's writes as they arrive, so a decoder can wait for a whole message. + outbound: ReadableStream; // Resolves once the subscriber waits on a read the test has not answered. reading: Promise; aborted: Promise; @@ -1040,6 +1043,8 @@ function fakeSession(park: number[] = [], stall: number[] = []) { close: (info?: WebTransportCloseInfo) => void closes.push(info), createBidirectionalStream: () => { let inbound!: ReadableStreamDefaultController; + let sent!: ReadableStreamDefaultController; + const outbound = new ReadableStream({ start: (controller) => (sent = controller) }); let onRead!: () => void; let onAbort!: (reason: unknown) => void; let onFinish!: () => void; @@ -1068,7 +1073,9 @@ function fakeSession(park: number[] = [], stall: number[] = []) { stopped.refused(); return Promise.reject(stopped.reason); } - written.push(new Uint8Array(chunk)); + const copy = new Uint8Array(chunk); + written.push(copy); + sent.enqueue(copy); onWrite(); return new Promise((resolve) => { if (stalled) return; @@ -1082,8 +1089,14 @@ function fakeSession(park: number[] = [], stall: number[] = []) { channel.port2.postMessage(null); }); }, - close: () => onFinish(), - abort: (reason) => void onAbort(reason), + close: () => { + sent.close(); + onFinish(); + }, + abort: (reason) => { + sent.error(reason); + onAbort(reason); + }, }); const opened = new Promise((resolve) => (release = () => resolve({ readable, writable }))); if (!park.includes(streams.length)) release(); @@ -1091,7 +1104,7 @@ function fakeSession(park: number[] = [], stall: number[] = []) { new Promise((refused) => { stopped = { reason, refused }; }); - streams.push({ inbound, reading, aborted, finished, writing, written, release, stop }); + streams.push({ inbound, outbound, reading, aborted, finished, writing, written, release, stop }); count.set(streams.length); return opened; }, @@ -1301,47 +1314,51 @@ test("a lite subscribe whose SUBSCRIBE write stalls past the deadline closes its // On lite-05 and later a SUBSCRIBE goes out beside its TRACK, before the TRACK_INFO, and accepting // the info sends no SUBSCRIBE_UPDATE even when the publisher keeps groups fresh for less than the // subscriber waits. -test.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])( - "lite %s: a subscribe goes out with its track", - async (version) => { - const { quic, streams, opened } = fakeSession(); - const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); - const track = subscriber - .consume(Path.from("room")) - .track("video") - .subscribe({ priority: 3, delay: { max: Milli(10_000) } }); - try { - await opened(0); - await streams[0].reading; - await (await opened(1)).writing; - const sent = new Reader(undefined, new Uint8Array(Buffer.concat(streams[0].written)), version); - expect(await sent.u53()).toBe(StreamId.Track); - await drainReactions(); - const wire = () => new Reader(undefined, new Uint8Array(Buffer.concat(streams[1].written)), version); - let subscribe = wire(); - expect(await subscribe.u53()).toBe(StreamId.Subscribe); - const msg = await Subscribe.decode(subscribe, version); - expect([msg.id, msg.track, msg.priority, msg.maxDelay]).toEqual([0n, "video", 3, 10_000]); +test.each( + [Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07].flatMap((version) => + [false, true].map((forced) => ({ name: versionName(version), version, forced })), + ), +)("$name: a subscribe goes out with its track (forced yields: $forced)", async ({ version, forced }) => { + const yielding = forced ? spyOn(Budget.prototype, "poll").mockImplementation(() => yieldNow()) : undefined; + const { quic, streams, opened } = fakeSession(); + const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); + const track = subscriber + .consume(Path.from("room")) + .track("video") + .subscribe({ priority: 3, delay: { max: Milli(10_000) } }); + try { + await opened(0); + await streams[0].reading; + const sent = new Reader(undefined, new Uint8Array(Buffer.concat(streams[0].written)), version); + expect(await sent.u53()).toBe(StreamId.Track); + const request = await opened(1); + const subscribe = new Reader(request.outbound, undefined, version); + expect(await subscribe.u53()).toBe(StreamId.Subscribe); + // Reading the request waits for its body without needing TRACK_INFO first. + const msg = await Subscribe.decode(subscribe, version); + expect([msg.id, msg.track, msg.priority, msg.maxDelay]).toEqual([0n, "video", 3, 10_000]); - const chunks: Uint8Array[] = []; - const writer = new Writer( - new WritableStream({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }), - version, - ); - await new TrackInfo({ timescale: 1000, maxAge: 1000 }).encode(writer, version); - for (const chunk of chunks) streams[0].inbound.enqueue(chunk); - await track.info(); - await drainReactions(); - subscribe = wire(); - await subscribe.u53(); - await Subscribe.decode(subscribe, version); - expect(await subscribe.done()).toBe(true); - } finally { - track.close(); - subscriber.close(); - } - }, -); + const chunks: Uint8Array[] = []; + const writer = new Writer( + new WritableStream({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }), + version, + ); + await new TrackInfo({ timescale: 1000, maxAge: 1000 }).encode(writer, version); + for (const chunk of chunks) streams[0].inbound.enqueue(chunk); + await track.info(); + await request.reading; + // An explicit change orders after any update caused by TRACK_INFO, even if its encoding yields. + track.update({ priority: 4, delay: { max: Milli(10_000) } }); + const update = await SubscribeUpdate.decode(subscribe, version); + expect([update.priority, update.maxDelay]).toEqual([4, 10_000]); + track.close(); + expect(await subscribe.done()).toBe(true); + } finally { + track.close(); + subscriber.close(); + yielding?.mockRestore(); + } +}); // A GROUP that beats its subscription's TRACK_INFO waits unread for it, then is read with it. test("a group that beats its track info waits for it", async () => { diff --git a/quest/m0/test-flakes-2/README.md b/quest/m0/test-flakes-2/README.md index b07ed823fa0..69a865614f3 100644 --- a/quest/m0/test-flakes-2/README.md +++ b/quest/m0/test-flakes-2/README.md @@ -94,4 +94,3 @@ Public API: none. Wire: none. - [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 - [Browser buffering and Firefox audio-tone tests pass under load](/quest/m0/test-flakes-2/browser-media-flakes.md) - the two `harness.browser.ts` buffering tests and the Firefox player's audio-tone checks pass reliably in Interop and Nightly, each fixed at its cause under the epic's rules -- [@moq/net's lite subscribe tests wait for the whole request](/quest/m0/test-flakes-2/js-subscribe-fixture.md) - the three lite subscribe-with-track cases pass under forced yields diff --git a/quest/m0/test-flakes-2/js-subscribe-fixture.md b/quest/m0/test-flakes-2/js-subscribe-fixture.md deleted file mode 100644 index 15a883993d5..00000000000 --- a/quest/m0/test-flakes-2/js-subscribe-fixture.md +++ /dev/null @@ -1,31 +0,0 @@ -# [XS] @moq/net's lite subscribe tests wait for the whole request - -## Goal - -The three `lite %s: a subscribe goes out with its track` cases (lite-05, -06, and 07) in `js/net/src/lite/subscriber.test.ts` pass when every budget -poll is forced to yield. They decode the SUBSCRIBE only after the request -has been fully written. - -## Plan - -Decided 2026-11-03 (maintainer, /quest-plan of the follow-ups from -[moq-dev/moq#7663](https://github.com/moq-dev/moq/pull/7663)): milestone m0, -a child of the test-flakes epic. #7663 fixes the announce harness only and -leaves these tests alone. - -- The test (`subscriber.test.ts:1289`) waits on `(await - opened(1)).writing`, which resolves on the first written chunk. It then - decodes `streams[1].written`. The `fakeSession` fixture (`:1015`) copies - the bytes after one host turn, so under forced yields the SUBSCRIBE can - still be partial. -- Wait on a signal that the request is complete, such as a full-message - promise from the fixture, then decode. Don't wait on the subscriber reading - its response, as #7663 does for the announce harness: on lite-05 and later - the subscriber reads it only after TRACK_INFO, which the test sends after - decoding SUBSCRIBE. -- Verify under forced yields (#7663 shows how to force them) and at normal - speed. Check `fakeSession`'s other users for the same partial-write - wait. - -Public API: none. Wire: none.