From 9aa85ed6f24325f48830e2000feda7922e7379b8 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 23:35:54 -0700 Subject: [PATCH 1/3] chore(quest): claim quest/m1/ietf-tracks-refuse.md Quest-Claim: 7809020f8f09e540 From 060229d0dc3682bd2ce66d53c337b01ce3fe4886 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 23:41:28 -0700 Subject: [PATCH 2/3] test(net): checkpoint inbound track refusal coverage Co-Authored-By: GPT-6.1 Sol --- js/net/src/ietf/connection.test.ts | 59 +++++++++++++++--------------- quest/m1/ietf-tracks-refuse.md | 5 +++ 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/js/net/src/ietf/connection.test.ts b/js/net/src/ietf/connection.test.ts index 6c7e6385996..0a3b4a0e572 100644 --- a/js/net/src/ietf/connection.test.ts +++ b/js/net/src/ietf/connection.test.ts @@ -119,37 +119,38 @@ for (const [version, alpn] of [ } } -/** A cancelled listing leaves the session usable for a new listing of the same prefix. */ -test("SUBSCRIBE_TRACKS accepts an empty origin", async () => { - const version = Version.DRAFT_22; - const pair = createMockTransportPair(ALPN.DRAFT_22); - const control = await Stream.open(pair.server, { version }); - const connection = new Connection({ - url: new URL("https://example.com"), - quic: pair.server, - control, - version, - client: false, - }); +/** Refusing repeated listings leaves the session usable and still spends their request IDs. */ +for (const version of [Version.DRAFT_18, Version.DRAFT_22]) { + test(`${version}: SUBSCRIBE_TRACKS is refused on its own stream`, async () => { + const pair = createMockTransportPair(ALPN.DRAFT_22); + const control = await Stream.open(pair.server, { version }); + const connection = new Connection({ + url: new URL("https://example.com"), + quic: pair.server, + control, + version, + client: false, + }); - try { - // A disjoint second listing proves the first left the session usable. - for (let i = 0; i < 2; i++) { - const stream = await Stream.open(pair.client, { version }); - await stream.writer.u62(BigInt(SUBSCRIBE_TRACKS_ID)); - await Message.encode(stream.writer, async (writer) => { - await writer.u62(BigInt(i * 2)); - await Namespace.encode(writer, Path.from(`prefix-${i}`)); - await writer.u53(0); - }); - expect(await stream.reader.u53()).toBe(RequestOk.id); - await RequestOk.decode(stream.reader, version); - stream.abort(new StreamError(StreamCode.Cancel)); + try { + // Reusing the prefix must still be refused, without retaining an overlapping listing. + for (let i = 0; i < 2; i++) { + const stream = await Stream.open(pair.client, { version }); + await stream.writer.u62(BigInt(SUBSCRIBE_TRACKS_ID)); + await Message.encode(stream.writer, async (writer) => { + await writer.u62(BigInt(i * 2)); + await Namespace.encode(writer, Path.from("prefix")); + await writer.u53(0); + }); + expect(await stream.reader.u53()).toBe(RequestError.id); + expect((await RequestError.decode(stream.reader, version)).errorCode).toBe(0x9); + expect(await stream.reader.done()).toBe(true); + } + } finally { + connection.abort(); } - } finally { - connection.abort(); - } -}); + }); +} /** A SUBSCRIBE_NAMESPACE always asks for namespaces, so it gets the ones we publish. */ test("SUBSCRIBE_NAMESPACE gets the published namespaces", async () => { diff --git a/quest/m1/ietf-tracks-refuse.md b/quest/m1/ietf-tracks-refuse.md index 2919883525f..a3f698922ba 100644 --- a/quest/m1/ietf-tracks-refuse.md +++ b/quest/m1/ietf-tracks-refuse.md @@ -11,6 +11,11 @@ listing, and the archive that uses it, still learns an IETF peer's tracks. ## Plan +Checkpoint 2026-10-29: the draft contains only an initial JS refusal test. +Both production publishers remain unchanged, so the Goal is not implemented. +The agent stopped when its selected model reached capacity; finish the +implementation and validate the test before marking the PR ready. + Decided with the maintainer on 2026-10-28 (`/quest-plan`, "what's not worth supporting in IETF"). From de6634606d52bfcc476a5bb8226e934bb7d2322c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 22:17:35 -0700 Subject: [PATCH 3/3] feat(net)!: refuse inbound IETF track listings Our moq-transport publishers refuse a peer's SUBSCRIBE_TRACKS with NOT_SUPPORTED again, on its own stream, and the session stays up. Rust still sends SUBSCRIBE_TRACKS upstream for a TRACKS listing. Deletes the Rust and JS answerers, their tests, and the subscribe-tracks JS bench, trims the SUBSCRIBE_TRACKS codec to what our own request carries, and covers outbound listing coalescing with a unit test on its state. Finishes quest/m1/ietf-tracks-refuse.md. Co-Authored-By: Claude Opus 5.5 --- doc/concept/standard.md | 13 +- doc/concept/stats.md | 5 +- js/justfile | 2 +- js/net/bench/subscribe-tracks.ts | 71 - js/net/src/ietf/connection.test.ts | 17 +- js/net/src/ietf/parameters.ts | 9 - js/net/src/ietf/peer-violations.test.ts | 43 - js/net/src/ietf/publisher.ts | 663 +---- js/net/src/ietf/subscribe_tracks.test.ts | 673 ----- js/net/src/ietf/subscribe_tracks.ts | 28 +- js/net/src/integration.test.ts | 16 +- quest/m1/README.md | 1 - quest/m1/archive/README.md | 4 +- quest/m1/ietf-tracks-refuse.md | 48 - rs/moq-net/src/ietf/error.rs | 3 - rs/moq-net/src/ietf/publisher.rs | 50 +- rs/moq-net/src/ietf/publisher/tracks.rs | 2562 ----------------- rs/moq-net/src/ietf/request_stream.rs | 33 +- rs/moq-net/src/ietf/subscribe_tracks.rs | 85 +- rs/moq-net/src/ietf/subscriber/tracks.rs | 34 +- .../tests/integration/subscribe_tracks.rs | 62 +- rs/moq-net/tests/integration/tracks_ietf.rs | 377 +-- sh/gh/nightly.sh | 2 +- 23 files changed, 168 insertions(+), 4633 deletions(-) delete mode 100644 js/net/bench/subscribe-tracks.ts delete mode 100644 js/net/src/ietf/subscribe_tracks.test.ts delete mode 100644 quest/m1/ietf-tracks-refuse.md delete mode 100644 rs/moq-net/src/ietf/publisher/tracks.rs diff --git a/doc/concept/standard.md b/doc/concept/standard.md index 0093964bf9d..6a5091ec944 100644 --- a/doc/concept/standard.md +++ b/doc/concept/standard.md @@ -25,16 +25,16 @@ maps everything else to "not supported" or a harmless equivalent. The [moq-lite page](/concept/moq-lite#what-moq-lite-leaves-out) lists what the subset drops. What a peer actually observes against this implementation: -- **Pull.** Subscribers ask. Single-track `PUBLISH` offers are declined, unless they answer our `SUBSCRIBE_TRACKS` (below); announce a namespace and serve the resulting subscriptions. Announcements go out unsolicited. A client asks only for the prefixes its readers watch, and a server or relay link for every prefix it may discover. We never hold two overlapping requests at a peer that did not declare solicit: a broader one replaces the narrower ones once they are closed. The solicit `SETUP` option makes us wait to be asked. Without it, a peer also gets each match as a `NAMESPACE` on its `SUBSCRIBE_NAMESPACE` stream, so it hears the namespace twice. We send `PUBLISH` only to answer a `SUBSCRIBE_TRACKS` (below). -- **`SUBSCRIBE_TRACKS` lists, it never pushes.** It lists every track of every broadcast under its prefix, later ones included. With `FORWARD=0`, each track gets a paused `PUBLISH` carrying its Track Properties, and a `PUBLISH_DONE` once the track is no longer listed: `TRACK_ENDED` if it finished, even just before its broadcast ended, and an error otherwise, since the track may go on elsewhere. A `REQUEST_UPDATE` with `FORWARD=1` resumes one, served from the cache like a `SUBSCRIBE` with the update's filter and answered with `LARGEST_OBJECT`; `FORWARD=0` pauses it again. Each paused `PUBLISH` opens only as the peer's stream credit admits it, so a peer that grants none holds none. A listing that omits `FORWARD` or sets it to 1 asks for every track's objects: on draft 22 each track gets `PUBLISH_SKIPPED`, and the subscriber `SUBSCRIBE`s to what it wants; draft 18 has no such skip, so the request is refused with `NOT_SUPPORTED`. A prefix overlapping another listing gets `PREFIX_OVERLAP`, a `TRACK_PROPERTY_FILTER` gets `NOT_SUPPORTED`, and a `REQUEST_UPDATE` on the listing itself ends it. On draft 18, a `SUBSCRIBE` to a track we `PUBLISH`ed gets `DUPLICATE_SUBSCRIPTION`. Cancelling the listing stops new `PUBLISH`es, while those sent live on. A broadcast is listed only while our publish grant covers it, from scratch once it does. Rust sends it upstream for a listing (below); JavaScript does not. -- **A Rust TRACKS listing sends `SUBSCRIBE_TRACKS` upstream, only on demand.** Listing a broadcast from a moq-transport peer sends `SUBSCRIBE_TRACKS` with `FORWARD=0` while someone lists it, never on connect, announce, or subscribe, so a peer that refuses, resets, or ignores it affects only that listing. Each listed track's paused `PUBLISH` is its upstream copy, which a reader resumes rather than subscribing again. Only a `PUBLISH_DONE` with `TRACK_ENDED` removes a listed track, and it lists as finished; any other end, including the error our own answerer sends for a track that left unfinished, fails the listing, and readers list again. +- **Pull.** Subscribers ask. Single-track `PUBLISH` offers are declined, unless they answer our `SUBSCRIBE_TRACKS` (below); announce a namespace and serve the resulting subscriptions. Announcements go out unsolicited. A client asks only for the prefixes its readers watch, and a server or relay link for every prefix it may discover. We never hold two overlapping requests at a peer that did not declare solicit: a broader one replaces the narrower ones once they are closed. The solicit `SETUP` option makes us wait to be asked. Without it, a peer also gets each match as a `NAMESPACE` on its `SUBSCRIBE_NAMESPACE` stream, so it hears the namespace twice. We never send `PUBLISH`. +- **We list no tracks by `PUBLISH`.** Rust and JavaScript refuse each `SUBSCRIBE_TRACKS` with `NOT_SUPPORTED`, and the session stays up. Our own peers list tracks over moq-lite instead. +- **A Rust TRACKS listing sends `SUBSCRIBE_TRACKS` upstream, only on demand.** Listing a broadcast from a moq-transport peer sends `SUBSCRIBE_TRACKS` with `FORWARD=0` while someone lists it, never on connect, announce, or subscribe, so a peer that refuses, resets, or ignores it affects only that listing. Each listed track's paused `PUBLISH` is its upstream copy, which a reader resumes rather than subscribing again. Only a `PUBLISH_DONE` with `TRACK_ENDED` removes a listed track, and it lists as finished; any other end fails the listing, and readers list again. Our own moq-transport publishers refuse it, so a Rust listing of one fails. - **History is served one group at a time.** Our moq-transport publishers answer a `FETCH` only within one group, a draft-18 joining `FETCH` for the subscription's current group head, and a draft-22 fill for the largest group's head. Whole-group fetches wait for an open group to finish. Both implementations accept and ignore `FILL_TIMEOUT`, including zero. A JavaScript subscription joins at the current group: on draft 18 it fetches the group's head with a Relative Joining `FETCH`, and on draft 22 asks for it with a fill. Datagrams are never fetchable. Subscribers still request ranges upstream, one per run of missing groups; a third-party publisher can answer those ranges and mark their gaps. A Rust reader that only fetches learns the track from `TRACK_STATUS`, so a finished track stays fetchable; a reader whose publisher refuses `TRACK_STATUS` as `NOT_SUPPORTED` still subscribes. - **Compression.** Rust peers exchange compressed tracks when both offer the [Flate extension](/draft/moq-flate). Other peers receive one shared inflated copy. A publisher that seals the compression declaration makes that fallback unavailable: an incompatible peer is refused, preserving the sealed metadata. JavaScript does not offer the IETF extension. - **Timing.** A track whose `SUBSCRIBE_OK` declares no `TIMESCALE` is untimed. A Rust reader that never subscribed takes the units from `TRACK_STATUS`. - **Strict SETUP and parameters.** Repeated unknown `SETUP` options, GREASE included, are accepted if well-formed; a repeated known option is rejected. A `GROUP_ORDER` other than Ascending (1) or Descending (2) closes the session. An unknown message parameter, or one defined only for another message, closes the session. Other malformed control input closes the session with the draft's code too, such as a server `SETUP` carrying `PATH` or `AUTHORITY`, a namespace field ending in `/` (it would map to a path with an empty segment), a request cut short by FIN, or a second `GOAWAY` on one stream; a first `GOAWAY` on a request stream is legal. - **One `SETUP` credential**, forwarded to the [auth server](/bin/relay/auth#the-contract) unverified. A second `SETUP` token closes the session. We advertise no token cache, so an alias reference in `SETUP` closes it with `PROTOCOL_VIOLATION`, one on a request with `UNKNOWN_AUTH_TOKEN_ALIAS`, and a registration on a request with `AUTH_TOKEN_CACHE_OVERFLOW`. Where the Rust side authorizes the peer or verifies tokens, a token on a `SUBSCRIBE` or `PUBLISH_NAMESPACE` is verified like an in-band session token and authorizes that one request alone, even where that authorization already covers it; it never widens the session. It is refused with `NOT_SUPPORTED` when nothing verifies it, when it is empty, or when its Token Type is anything but 0 (out of band). A side that does neither, JavaScript included, ignores the token. Rust refuses a request carrying more than one distinct token with `NOT_SUPPORTED`, since it verifies one; JavaScript, which verifies none, ignores them all. A token-authorized `SUBSCRIBE` still stays within the grant the peer gave us. A token-authorized `PUBLISH_NAMESPACE` needs a grant covering everything beneath the namespace, and the tracks we subscribe under it rely on that announce instead of our limit, ending with it. An update renews the token, and a refused renewal ends only that request. Rust and JavaScript send the token a route names (`Route::auth`) on its `PUBLISH_NAMESPACE`, and swapping it renews the request with `REQUEST_UPDATE`; the inline `NAMESPACE` that answers a solicited `SUBSCRIBE_NAMESPACE` carries no token. A token added to a reconnecting connection's auth handle rides every session it makes, so the announcement keeps it across a reconnect; one added to a single session's handle ends with that session. A token on any other request is decoded and grants nothing, and a request repeating one is refused with `MALFORMED_AUTH_TOKEN`. The negotiated [MoQ Auth extension](/concept/moq-lite#authorization) accepts additional session tokens in band. - **Pause delivery.** `FORWARD=0` on a `SUBSCRIBE` or its update drops the subscription, closing its data streams and holding nothing upstream, while the request and its slot stay. `FORWARD=1` subscribes again as a new `SUBSCRIBE` with the same filter would, so nothing carries across the pause: its `REQUEST_OK` names the live edge it joined at. A paused update may change only priority and max age. -- **Refused, not fatal.** A legal request this stack does not serve is rejected on its own and the session stays up: multi-group `FETCH` and joining requests reaching before the joined group get `NOT_SUPPORTED`. Range Filters get `INVALID_FILTER` on draft 22 because the advertised filter limit is zero. On draft 22, namespaces whose first tuple field is exactly `.` or `.session` get `DOES_NOT_EXIST` before application lookup; other dot prefixes still reach the application. Subscription updates accept priority, negotiated subscriber max age, and delivery timeouts. Rust also carries range changes, keeps bounded subscriptions updatable, renews request tokens, and judges `NEW_GROUP_REQUEST` as a join (see dynamic groups below). On draft 22, a wider or historical fill fails on its separate fetch stream while its subscription stays open; a largest-group fill hands its head to the live subgroup tail. Other supported-but-unimplemented preferences end that subscription; a `TRACK_PROPERTY_FILTER` there closes the session with `PROTOCOL_VIOLATION`, since it belongs only to `SUBSCRIBE_TRACKS`. A namespace update may change its prefix; suffixes after the acknowledgement are relative to that new prefix. +- **Refused, not fatal.** A legal request this stack does not serve is rejected on its own and the session stays up: `SUBSCRIBE_TRACKS`, multi-group `FETCH`, and joining requests reaching before the joined group get `NOT_SUPPORTED`. Range Filters get `INVALID_FILTER` on draft 22 because the advertised filter limit is zero. On draft 22, namespaces whose first tuple field is exactly `.` or `.session` get `DOES_NOT_EXIST` before application lookup; other dot prefixes still reach the application. Subscription updates accept priority, negotiated subscriber max age, and delivery timeouts. Rust also carries range changes, keeps bounded subscriptions updatable, renews request tokens, and judges `NEW_GROUP_REQUEST` as a join (see dynamic groups below). On draft 22, a wider or historical fill fails on its separate fetch stream while its subscription stays open; a largest-group fill hands its head to the live subgroup tail. Other supported-but-unimplemented preferences end that subscription; a `TRACK_PROPERTY_FILTER` there closes the session with `PROTOCOL_VIOLATION`, since it belongs only to `SUBSCRIBE_TRACKS`. A namespace update may change its prefix; suffixes after the acknowledgement are relative to that new prefix. - **Objects and datagrams.** Only subgroup 0 is accepted: a stream for any other subgroup, whether its header gives the ID or takes it from the first Object ID, is stopped and the session stays up. Rust relays preserve Object IDs, gaps, object properties, FIRST\_OBJECT, and END\_OF\_GROUP status objects between moq-transport peers for subgroup 0, including across a moq-lite-07 hop between two relays that negotiate the [ietf](/draft/moq-ietf) extension. Datagrams retain their IDs, properties, priority, and status. Plain Rust readers and moq-lite peers without the extension see consecutive frames and only normal datagram payloads at Object ID 0; other datagram objects and statuses are dropped on those surfaces. JavaScript supports only normal datagrams at Object ID 0. A malformed datagram closes the session; a relayed one the downstream draft cannot carry is dropped. A relayed Timestamp leaves in the units the downstream response declares, and is dropped where it declares none, as for a plain frame; an immutable one that would change or vanish is refused. On a timed track the frame holds each object's time, so a mutable Timestamp that only repeats the immutable one is not restated; a status object has no frame, so it keeps its Timestamp among its properties. Only property 0x10 is read as a Timestamp. Property 0x06 stays opaque and keeps its value regardless of timestamp units; it is dropped between drafts 18 and 22, or refused when immutable, because draft 22 assigns it to SUBGROUP\_DELIVERY\_TIMEOUT. A status object carrying properties is refused. A `FETCH` has no status objects, so its response ends at the last frame. A group cannot resume across route changes by frame ordinal once Object IDs skip ahead of the ordinals on either route; recovery refuses it instead of splicing different objects. - **Dynamic groups.** A track advertises `DYNAMIC_GROUPS` when its publisher sets a refresh ratio, and a track received with `DYNAMIC_GROUPS` gets a ratio of 0. A `NEW_GROUP_REQUEST` counts as one more subscriber joining, which a `SUBSCRIBE` already is: it asks for a new group only when the newest group's bytes after its first frame exceed the ratio times the first frame. Joins coalesce until the asked-for group arrives. A Rust relay sends `NEW_GROUP_REQUEST` upstream, on the `SUBSCRIBE` or in a `REQUEST_UPDATE`, once a join wants a group past the newest it holds; a relay holding nothing yet asks for none. On a track without a ratio, an update carrying one ends the subscription with `UPDATE_FAILED`. A moq-lite-07 hop (`moq-lite-07-wip`, opt-in) carries the same demand natively: the ratio in `TRACK_INFO` and the asked-for group in `SUBSCRIBE_UPDATE`. Earlier moq-lite versions carry neither, so a track received over one never refreshes. - **Priority.** Higher is served first. This stack's priorities run from -128 to 127 with 0 unset, and go out on the IETF wire, which ranks lower first, as `127 - priority`: an unset priority is IETF 127. An omitted subscriber or publisher priority reads as the drafts' default of IETF 128, one step below unset, so a relay forwards it unchanged. A relay's upstream subscription carries the highest priority its subscribers ask for and updates it as they come and go; its group order stays as the `SUBSCRIBE` asked, since no draft lets an update change it. @@ -106,8 +106,9 @@ choices. They follow the product model. termination code, so we close the session with it. - **Deduplicated subscriptions and discovery.** Shared subscriptions and overlapping - namespace prefixes are coalesced internally. Only `SUBSCRIBE_TRACKS`, described - above, sends `PREFIX_OVERLAP`, and on draft 18 `DUPLICATE_SUBSCRIPTION`. + namespace prefixes are coalesced internally, so we never send `PREFIX_OVERLAP`. Only + a redundant draft-18 `PUBLISH` answering our own `SUBSCRIBE_TRACKS` gets + `DUPLICATE_SUBSCRIPTION`. - **Subscriber max age is negotiated.** The [Timestamp extension](/draft/moq-timestamp) carries a subscriber's `max_delay` in `SUBSCRIBE` and `REQUEST_UPDATE` only diff --git a/doc/concept/stats.md b/doc/concept/stats.md index bd1c35b8373..bf82149756a 100644 --- a/doc/concept/stats.md +++ b/doc/concept/stats.md @@ -210,14 +210,15 @@ depth 0) carrying all of the group's tiers, both directions, and its sessions, so a reader holds one subscription per group. A reader discovers the live groups from the broadcast's track listing (TRACKS -in moq-lite 07, `SUBSCRIBE_TRACKS` in moq-transport draft 18 and later). At +in moq-lite 07). At depth 0 the one group's track is listed for the producer's life. At depth 1 or more, a group's track is listed from its first entry until it has lingered empty (five minutes by default), then unlisted and ended. A group that returns within the linger keeps its track and epoch, and its totals continue. The node's announcement never changes, so group churn costs no announce traffic across a cluster. Over a hop that cannot carry a listing (moq-lite 06 and -older, or moq-transport before draft 18) the listing is empty or refused; a +older, or moq-transport, whose `SUBSCRIBE_TRACKS` our relay refuses) the listing +is empty or refused; a reader that knows a group's name can still subscribe to it while it is listed, and is refused otherwise. diff --git a/js/justfile b/js/justfile index 00e472c7231..b2907d3d695 100644 --- a/js/justfile +++ b/js/justfile @@ -57,7 +57,7 @@ test-changed list: coverage package="": python3 ../sh/js/coverage.py '{{ package }}' -# Run one net benchmark: broadcasts, forward, requests, fills, reader, frames, track, resume, observed, drift, ledger, varint, lite-varint, priority, subscribe-drop, subscribe-tracks, refresh, or compression. +# Run one net benchmark: broadcasts, forward, requests, fills, reader, frames, track, resume, observed, drift, ledger, varint, lite-varint, priority, subscribe-drop, refresh, or compression. bench name: bun install --frozen-lockfile bun net/bench/{{ name }}.ts diff --git a/js/net/bench/subscribe-tracks.ts b/js/net/bench/subscribe-tracks.ts deleted file mode 100644 index ea7bfe87cfd..00000000000 --- a/js/net/bench/subscribe-tracks.ts +++ /dev/null @@ -1,71 +0,0 @@ -/** Sweep origin size and subscribers while listing one newly added track. */ -import { StreamCode, StreamError } from "../src/error.ts"; -import { Publisher } from "../src/ietf/publisher.ts"; -import { RequestOk } from "../src/ietf/request.ts"; -import { NativeSession } from "../src/ietf/session.ts"; -import { PublishSkipped, SubscribeTracks } from "../src/ietf/subscribe_tracks.ts"; -import { ALPN, Version } from "../src/ietf/version.ts"; -import { createMockTransportPair } from "../src/mock.ts"; -import { Producer } from "../src/origin.ts"; -import * as Path from "../src/path.ts"; -import { Stream } from "../src/stream.ts"; - -const version = Version.DRAFT_22; -console.log("broadcasts,subscribers,track_added_us"); -for (const broadcasts of [1, 16, 128]) { - for (const subscribers of [1, 4, 16]) { - const origin = new Producer(); - const sources = Array.from({ length: broadcasts }, (_, i) => { - const sourcePublication = origin.createBroadcast(Path.from(`room/${i}`)); - const source = sourcePublication.broadcast; - sourcePublication.announce(); - source.createTrack("initial"); - return source; - }); - const clients = await Promise.all( - Array.from({ length: subscribers }, async () => { - const pair = createMockTransportPair(ALPN.DRAFT_22); - const publisher = new Publisher({ - quic: pair.server, - session: new NativeSession(pair.server, version, false), - publish: origin.consume(), - requiresSolicitation: true, - }); - const client = await Stream.open(pair.client, { version }); - const server = await Stream.accept(pair.server, version); - if (!server) throw new Error("missing stream"); - const task = publisher.runSubscribeTracks(new SubscribeTracks(0n, Path.from("room")), server); - if ((await client.reader.u53()) !== RequestOk.id) throw new Error("missing REQUEST_OK"); - await RequestOk.decode(client.reader, version); - for (let i = 0; i < broadcasts; i++) { - if ((await client.reader.u53()) !== PublishSkipped.id) throw new Error("missing PUBLISH_SKIPPED"); - await PublishSkipped.decode(client.reader, version); - } - return { pair, publisher, client, task }; - }), - ); - const samples = []; - for (let sample = 0; sample < 21; sample++) { - const start = performance.now(); - const track = sources[0].createTrack(`added-${sample}`); - await Promise.all( - clients.map(async ({ client }) => { - if ((await client.reader.u53()) !== PublishSkipped.id) throw new Error("missing added track"); - if ((await PublishSkipped.decode(client.reader, version)).trackName !== track.name) - throw new Error("wrong track"); - }), - ); - if (sample) samples.push((performance.now() - start) * 1000); - track.abort(new Error("benchmark finished")); - } - samples.sort((a, b) => a - b); - console.log(`${broadcasts},${subscribers},${samples[10].toFixed(2)}`); - for (const { client } of clients) client.abort(new StreamError(StreamCode.Cancel)); - await Promise.all(clients.map(({ task }) => task)); - for (const { pair, publisher } of clients) { - pair.server.close(); - publisher.close(); - } - origin.close(); - } -} diff --git a/js/net/src/ietf/connection.test.ts b/js/net/src/ietf/connection.test.ts index 0a3b4a0e572..2ecf23a14bf 100644 --- a/js/net/src/ietf/connection.test.ts +++ b/js/net/src/ietf/connection.test.ts @@ -1,7 +1,7 @@ import { expect, spyOn, test } from "bun:test"; import { offered } from "../connection/extensions.ts"; import { exchangeSetup } from "../connection/handshake.ts"; -import { SessionCode, StreamCode, StreamError } from "../error.ts"; +import { SessionCode } from "../error.ts"; import { createMockTransportPair } from "../mock.ts"; import { Producer as OriginProducer } from "../origin.ts"; import * as Path from "../path.ts"; @@ -119,10 +119,13 @@ for (const [version, alpn] of [ } } -/** Refusing repeated listings leaves the session usable and still spends their request IDs. */ -for (const version of [Version.DRAFT_18, Version.DRAFT_22]) { - test(`${version}: SUBSCRIBE_TRACKS is refused on its own stream`, async () => { - const pair = createMockTransportPair(ALPN.DRAFT_22); +/** We list no tracks by PUBLISH: each SUBSCRIBE_TRACKS gets NOT_SUPPORTED, and the session stays usable. */ +for (const [version, alpn] of [ + [Version.DRAFT_18, ALPN.DRAFT_18], + [Version.DRAFT_22, ALPN.DRAFT_22], +] as const) { + test(`SUBSCRIBE_TRACKS is refused on its own stream on ${alpn}`, async () => { + const pair = createMockTransportPair(alpn); const control = await Stream.open(pair.server, { version }); const connection = new Connection({ url: new URL("https://example.com"), @@ -133,7 +136,7 @@ for (const version of [Version.DRAFT_18, Version.DRAFT_22]) { }); try { - // Reusing the prefix must still be refused, without retaining an overlapping listing. + // A second listing of the same prefix is refused the same way, each spending its Request ID. for (let i = 0; i < 2; i++) { const stream = await Stream.open(pair.client, { version }); await stream.writer.u62(BigInt(SUBSCRIBE_TRACKS_ID)); @@ -143,7 +146,7 @@ for (const version of [Version.DRAFT_18, Version.DRAFT_22]) { await writer.u53(0); }); expect(await stream.reader.u53()).toBe(RequestError.id); - expect((await RequestError.decode(stream.reader, version)).errorCode).toBe(0x9); + expect((await RequestError.decode(stream.reader, version)).errorCode).toBe(0x3); expect(await stream.reader.done()).toBe(true); } } finally { diff --git a/js/net/src/ietf/parameters.ts b/js/net/src/ietf/parameters.ts index 1e08070f457..8517e232c0c 100644 --- a/js/net/src/ietf/parameters.ts +++ b/js/net/src/ietf/parameters.ts @@ -54,8 +54,6 @@ type ControlMessage = | "subscribe-update" | "subscribe-namespace" | "subscribe-tracks" - | "tracks-update" - | "publish-ok" | "publish" | "publish-namespace" | "fetch" @@ -102,13 +100,6 @@ function allows(message: ControlMessage, version: IetfVersion, id: bigint): bool newGroup || include)) ); - case "tracks-update": - return auth || id === 0x34n || forward || priority || filter || fill || range || trackRange || newGroup; - case "publish-ok": - return ( - expires || - (!from22 && (forward || priority || filter || order || objectTimeout || subgroupTimeout || newGroup)) - ); case "subscribe": return ( id === MSG_PARAM_MAX_AGE || diff --git a/js/net/src/ietf/peer-violations.test.ts b/js/net/src/ietf/peer-violations.test.ts index 9fa5667fb11..e2e770d4498 100644 --- a/js/net/src/ietf/peer-violations.test.ts +++ b/js/net/src/ietf/peer-violations.test.ts @@ -2,19 +2,15 @@ import { expect, spyOn, test } from "bun:test"; import { SessionCode, StreamCode, StreamError } from "../error.ts"; import { HopSchema } from "../hop.ts"; import { createMockTransportPair } from "../mock.ts"; -import { Producer as OriginProducer } from "../origin.ts"; import * as Path from "../path.ts"; import { Stream, Writer } from "../stream.ts"; import { Slots } from "../util/limits.ts"; import { Connection } from "./connection.ts"; -import { Parameters } from "./parameters.ts"; import { PublishNamespace } from "./publish_namespace.ts"; -import { Publisher } from "./publisher.ts"; import { RequestOk } from "./request.ts"; import { NativeSession } from "./session.ts"; import { Subscribe, SubscribeOk } from "./subscribe.ts"; import { SubscribeNamespaceEntry, SubscribeNamespaceEntryDone } from "./subscribe_namespace.ts"; -import { SubscribeTracks } from "./subscribe_tracks.ts"; import { Subscriber } from "./subscriber.ts"; import { ALPN, Version } from "./version.ts"; @@ -161,45 +157,6 @@ for (const [version, alpn] of versions) { } }); - test(`${alpn}: a reset track listing observes a later transport rejection`, async () => { - const pair = createMockTransportPair(alpn); - const ended = Promise.withResolvers(); - Object.defineProperty(pair.server, "closed", { value: ended.promise }); - const origin = new OriginProducer(); - const publisher = new Publisher({ - session: new NativeSession(pair.server, version, false), - quic: pair.server, - publish: origin.consume(), - requiresSolicitation: true, - ready: new Promise(() => {}), - }); - const unhandled: unknown[] = []; - const onUnhandled = (err: unknown) => unhandled.push(err); - process.on("unhandledRejection", onUnhandled); - try { - const client = await Stream.open(pair.client, { version }); - const server = await Stream.accept(pair.server, version); - if (!server) throw new Error("missing listing stream"); - const parameters = new Parameters(); - parameters.forward = false; - const running = publisher.runSubscribeTracks(new SubscribeTracks(0n, Path.empty(), parameters), server); - expect(await client.reader.u53()).toBe(RequestOk.id); - await RequestOk.decode(client.reader, version); - client.abort(new StreamError(StreamCode.Cancel)); - await running; - ended.reject(new Error("transport died after the listing")); - // Unhandled rejection delivery happens on the next event-loop turn, not a clock deadline. - await new Promise((resolve) => setImmediate(resolve)); - expect(unhandled).toEqual([]); - } finally { - process.off("unhandledRejection", onUnhandled); - publisher.close(); - origin.close(); - pair.client.close(); - pair.server.close(); - } - }); - test.each(["done", "end"] as const)( `${alpn}: %s releases a slot after route reflection and reattachment`, async (ending) => { diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index fe8eaa09b9a..3fd68e074c4 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -47,9 +47,9 @@ import * as Message from "./message.ts"; import * as Namespace from "./namespace.ts"; import { FetchFrame, Frame, Group as GroupMessage } from "./object.ts"; import { Parameters } from "./parameters.ts"; -import { DEFAULT_WIRE, fromWire, toWire } from "./priority.ts"; +import { fromWire, toWire } from "./priority.ts"; import * as Properties from "./properties.ts"; -import { Publish, PublishDone, PublishDoneStatus } from "./publish.ts"; +import { PublishDone, PublishDoneStatus } from "./publish.ts"; import { type AdvertParams, PublishNamespace, PublishNamespaceUpdate } from "./publish_namespace.ts"; import { RequestError, RequestOk } from "./request.ts"; import { @@ -62,16 +62,16 @@ import { refuseUpdate, } from "./request_stream.ts"; import type { Session } from "./session.ts"; -import { GroupOrder, Subscribe, SubscribeOk } from "./subscribe.ts"; +import { GroupOrder, type Subscribe, SubscribeOk } from "./subscribe.ts"; import { type SubscribeNamespace, SubscribeNamespaceEntry, SubscribeNamespaceEntryDone, } from "./subscribe_namespace.ts"; -import { PublishSkipped, type SubscribeTracks } from "./subscribe_tracks.ts"; +import type { SubscribeTracks } from "./subscribe_tracks.ts"; import { MalformedAuthToken } from "./token.ts"; import { TrackStatusOk, type TrackStatusRequest } from "./track.ts"; -import { type IetfVersion, Version } from "./version.ts"; +import type { IetfVersion } from "./version.ts"; /** First wait before re-offering a namespace the peer refused or we couldn't open for. */ const RETRY_BASE = 100; @@ -260,31 +260,6 @@ interface RunGroup { /** How many data streams a subscription opened, fill streams included. */ type StreamCount = { opened: number; terminalSent?: boolean }; -/** A PUBLISH's state across every pause and resume. */ -interface Published { - streams: StreamCount; - unlisted: Once; - answer?: { requestId: bigint }; -} - -/** How a listed track left the listing: it finished, or it left unfinished. */ -type Unlisted = "finished" | "removed"; - -/** The next listed track to PUBLISH, ready but for its stream. */ -interface Staged { - name: string; - info: Info; - /** On draft-18, claims the track's one subscription from now on. */ - claim?: string; -} - -/** What a broadcast's listing woke for. */ -type Woken = - | { kind: "track"; event: broadcast.TracksEvent | undefined } - | { kind: "opened"; stream: Stream } - | { kind: "failed"; err: unknown } - | { kind: "changed" }; - /** Stop forwarding without ending the subscription. */ class Paused extends Error { /** Whether an update asked for the pause, which is answered once forwarding stopped. */ @@ -346,8 +321,6 @@ export class Publisher { #quic: WebTransport; #session: Session; #requiresSolicitation: boolean; - #listings = new Set(); - #trackClaims = new Map(); // What a joining FETCH needs from each draft-18 SUBSCRIBE, by its request ID, while it lasts: // pending from its arrival until it resolves. Request streams race, so a joining FETCH can // arrive before the subscription it names has. @@ -482,36 +455,15 @@ export class Publisher { // Draft-22 replaced joining FETCH with fills. const joinable = !Filter.isDraft22(this.#session.version); if (joinable) this.#setJoin(msg.requestId, "pending"); - const key = JSON.stringify([msg.trackNamespace, msg.trackName]); - const claimed = this.#session.version === Version.DRAFT_18; - let held = false; try { if (Namespace.reserved(msg.trackNamespace, this.#session.version)) { await refuseReserved(request); return; } - if (claimed && this.#trackClaims.get(key) === "publish") { - await Message.write( - request.writer, - RequestError.id, - new RequestError({ errorCode: 0x19, reasonPhrase: "track already PUBLISHed" }), - this.#session.version, - ); - return; - } - if (claimed) { - this.#trackClaims.set(key, ((this.#trackClaims.get(key) as number | undefined) ?? 0) + 1); - held = true; - } await this.#serveSubscribe(msg, request); } finally { // A joining FETCH arriving after the subscription ended has no edge to end at. if (joinable) this.#setJoin(msg.requestId, undefined); - if (held) { - const count = (this.#trackClaims.get(key) as number) - 1; - if (count) this.#trackClaims.set(key, count); - else this.#trackClaims.delete(key); - } } } @@ -722,15 +674,13 @@ export class Publisher { request, track, info, - published, - streams = published?.streams ?? { opened: 0 }, + streams = { opened: 0 }, answer, }: { msg: Subscribe; request: RequestStream; track: TrackSubscriber; info: Awaited>; - published?: Published; /** The data streams opened over every time the subscription forwarded. */ streams?: StreamCount; /** The update a resumed SUBSCRIBE answers with REQUEST_OK instead of SUBSCRIBE_OK. */ @@ -798,7 +748,7 @@ export class Publisher { // Draft-22 replaced joining FETCH with fills. Older drafts save the Largest Object the // subscription starts after, again on every resume, so a joining FETCH ends exactly // where the subscription begins. Saved before the answer names it. - if (!published && !Filter.isDraft22(version)) { + if (!Filter.isDraft22(version)) { this.#setJoin( msg.requestId, msg.filter.kind === "nextObject" @@ -815,14 +765,7 @@ export class Publisher { ); } - if (published) { - if (published.answer) { - const parameters = new Parameters(); - if (edge.largest) - parameters.largest = { groupId: edge.largest.group, objectId: edge.largest.object }; - await Message.write(request.writer, RequestOk.id, new RequestOk({ parameters }), version); - } - } else if (answer) { + if (answer) { // The resuming update learns where the subscription now joins. const parameters = new Parameters(); if (edge.largest) parameters.largest = { groupId: edge.largest.group, objectId: edge.largest.object }; @@ -874,8 +817,7 @@ export class Publisher { streams, }); }; - if (fill && cache) - startFill(answer?.requestId ?? published?.answer?.requestId ?? msg.requestId, fill, cache); + if (fill && cache) startFill(answer?.requestId ?? msg.requestId, fill, cache); // Every group started, until its stream finishes or resets, and the data streams // opened for PUBLISH_DONE to report. @@ -1035,20 +977,17 @@ export class Publisher { }), requestEnded, revoked, - ...(published ? [published.unlisted] : []), ]); - if (end === "removed") throw new NotFound("track no longer listed"); if (end === "revoked") { console.info(`subscription no longer authorized: broadcast=${name} track=${track.name}`); unauthorized = true; unsubscribe(); } - // A track that finished leaves the listing with TRACK_ENDED, like one served to its end. - ended = end === served || end === "finished"; + ended = end === served; } catch (err: unknown) { publishError = error(err); // Stop forwarding at once: a pause answers only after every data stream closed. - if (published || publishError instanceof Paused) { + if (publishError instanceof Paused) { unsubscribe(); track.close(); } @@ -2245,581 +2184,17 @@ export class Publisher { } } - /** Answer a request to discover tracks under a namespace prefix. @internal */ + /** Refuse a request to discover tracks under a namespace prefix: we list none by PUBLISH. @internal */ runSubscribeTracks(msg: SubscribeTracks, stream: Stream): Promise { const request = new RequestStream(stream, this.#session, this.#quic); - return request.serve(this.#runSubscribeTracks(msg, request)); - } - - async #runSubscribeTracks(msg: SubscribeTracks, request: RequestStream) { - const version = this.#session.version; - if (Namespace.reserved(msg.prefix, version)) return refuseReserved(request); - const params = msg.parameters; - if ( - params.rangeFilters || - params.trackPropertyFilter || - (version === Version.DRAFT_18 && params.forward !== false) - ) { - return request.refuse("subscribe_namespace", { - condition: params.rangeFilters ? "invalid_filter" : "not_supported", - reasonPhrase: params.rangeFilters - ? "Range Filters exceed MAX_FILTER_RANGES=0" - : "SUBSCRIBE_TRACKS parameters not supported", - }); - } - if ( - [...this.#listings].some( - (prefix) => Path.hasPrefix(prefix, msg.prefix) || Path.hasPrefix(msg.prefix, prefix), - ) - ) { - await Message.write( - request.writer, - RequestError.id, - new RequestError({ errorCode: 0x30, reasonPhrase: "overlapping SUBSCRIBE_TRACKS prefix" }), - version, - ); - return; - } - this.#listings.add(msg.prefix); - const adding = new Signal(true); - const skips = new Map(); - const changed = new Signal(0); - const tasks = new Set>(); - const routes = new Map(); - const stop = () => { - if (!adding.peek()) return; - adding.set(false); - this.#listings.delete(msg.prefix); - skips.clear(); - changed.update((n) => n + 1); - }; - void request.ended.then(stop); - let refusal: Refusal | undefined; - const closed = this.#quic.closed.then( - () => "closed" as const, - () => "closed" as const, + return request.serve( + Namespace.reserved(msg.prefix, this.#session.version) + ? refuseReserved(request) + : request.refuse("subscribe_namespace", { + condition: "not_supported", + reasonPhrase: "SUBSCRIBE_TRACKS is not supported", + }), ); - let updates: Promise | undefined; - let writing: Promise | undefined; - try { - await Message.write(request.writer, RequestOk.id, new RequestOk({}), version); - updates = (async () => { - const framed = await request.next(); - if (!framed) return request.ended; - if (framed.type !== 0x02) throw new ProtocolViolation("expected SUBSCRIBE_TRACKS update"); - await Message.decode(framed.reader, async (body) => { - this.#session.checkRequestId(await body.u62()); - await Parameters.decode(body, version, "tracks-update"); - }); - refusal = { condition: "not_supported", reasonPhrase: "SUBSCRIBE_TRACKS updates are not supported" }; - stop(); - })().catch((err) => { - if (err instanceof MalformedAuthToken) { - refusal = { condition: "malformed_auth_token", reasonPhrase: err.message }; - stop(); - } else request.abort(malformed(err, "SUBSCRIBE_TRACKS update")); - }); - writing = (async () => { - while (adding.peek()) { - const first = skips.entries().next().value; - if (!first) { - await Signal.race(changed, adding); - continue; - } - skips.delete(first[0]); - // A FORWARD=1 listing waits for this to read its next track. - changed.update((n) => n + 1); - await Message.write(request.writer, PublishSkipped.id, first[1], version); - } - if (refusal) await request.refuse("subscribe_namespace", refusal); - else request.close(); - })().catch((err) => { - request.abort(err); - }); - await race([this.#ready, request.ended]); - for (;;) { - const table = this.#advertised.peek(); - const visible = - table && - presented( - msg.prefix, - table, - (path) => this.#gate.allows(path) && (!this.#hidden || !hiddenBelow(msg.prefix, path)), - ); - for (const [suffix, route] of routes) { - const next = visible?.get(suffix); - if (!next || !sameInstance(route.snapshot, next)) { - route.hold.abort(); - routes.delete(suffix); - } - } - if (adding.peek() && visible) - for (const [suffix, snapshot] of visible) { - if (routes.has(suffix)) continue; - const hold = new AbortController(); - const task = this.#listBroadcast({ - msg, - suffix, - epoch: snapshot.route.epoch, - hold, - adding, - skips, - changed, - }) - .catch((err) => { - if (err instanceof ProtocolViolation) request.abort(err); - }) - .finally(() => { - tasks.delete(task); - changed.update((n) => n + 1); - }); - tasks.add(task); - routes.set(suffix, { snapshot, hold }); - } - if (!adding.peek()) { - // Sent PUBLISHes own their subscriptions, but still follow route withdrawal. - if (!tasks.size) break; - if ((await race([Signal.race(this.#advertised, this.#grant, changed), closed])) === "closed") break; - } else { - if ((await race([Signal.race(this.#advertised, this.#grant, adding), closed])) === "closed") break; - } - if (!table) break; - } - } finally { - stop(); - for (const route of routes.values()) route.hold.abort(); - await writing; - request.close(); - await Promise.all(tasks); - await updates; - } - } - - /** - * List one broadcast under a SUBSCRIBE_TRACKS: PUBLISH or skip each of its tracks until its - * route goes or the broadcast ends, then let its PUBLISHes run to their end. - * - * A PUBLISH starts only once the peer's stream credit opens its stream, one staged at a time, - * so the PUBLISHes a peer holds are bounded by the credit it grants. The listing is still read - * meanwhile, since an unlisted track's PUBLISH_DONE is what returns that credit; the tracks - * read but not yet PUBLISHed wait in `pending`. A FORWARD=1 listing has nothing to unlist, so - * it reads its next track only once the request stream has taken the last skip. - */ - async #listBroadcast({ - msg, - suffix, - epoch, - hold, - adding, - skips, - changed, - }: { - msg: SubscribeTracks; - suffix: Path.Valid; - epoch?: Epoch.Valid; - hold: AbortController; - adding: Signal; - skips: Map; - changed: Signal; - }) { - const forward = msg.parameters.forward !== false; - const namespace = Path.join(msg.prefix, suffix); - // The tracks waiting their turn for a PUBLISH, in listing order. - const pending = new Map(); - // The next track to PUBLISH, waiting on the peer's stream credit. - let staged: Staged | undefined; - // The stream the next PUBLISH goes out on, once the peer's credit admits it. - let opening: Promise | undefined; - // The listing's next event, read while anything else is awaited. - let reading: Promise | undefined; - // Each sent PUBLISH's entry: setting it says the track is no longer listed. - const listed = new Map>(); - // The tracks with a skip queued, so the broadcast's end cancels only its own. - const skipped = new Map(); - const tasks = new Set>(); - const skip = (name: string) => { - const key = Symbol(); - skipped.set(name, key); - skips.set(key, new PublishSkipped(suffix, name)); - changed.update((n) => n + 1); - }; - const unstage = () => { - if (staged?.claim) this.#trackClaims.delete(staged.claim); - staged = undefined; - }; - // The PUBLISHes already sent still end when their track is unlisted, so the listing is - // still read once cancelled, adding nothing. - const cancel = adding.subscribe((active) => { - if (active) return; - pending.clear(); - unstage(); - if (!tasks.size) hold.abort(); - }); - let listing: broadcast.Tracks | undefined; - try { - if (!adding.peek()) return; - const source = await this.#resolve(namespace, hold.signal, epoch); - listing = source.tracks(); - for (;;) { - // Stage the next pending track that will be PUBLISHed, skipping those that will - // not, so a stream is opened only for a PUBLISH that goes out. - for (const [name, info] of pending) { - if (staged || !adding.peek()) break; - pending.delete(name); - // Draft-18 allows one subscription per track (section 5.1), so a track the peer - // is subscribed to, by its SUBSCRIBE or a PUBLISH still alive, is skipped. - // Claimed from now, so a SUBSCRIBE cannot slip in before the PUBLISH goes out. - const claim = - this.#session.version === Version.DRAFT_18 ? JSON.stringify([namespace, name]) : undefined; - if (claim !== undefined) { - if (this.#trackClaims.has(claim)) { - skip(name); - continue; - } - this.#trackClaims.set(claim, "publish"); - } - staged = { name, info, claim }; - } - if (staged && !opening) - opening = Promise.resolve(this.#session.openBi({ timeout: null })).then( - (stream): Woken => ({ kind: "opened", stream }), - (err: unknown): Woken => ({ kind: "failed", err }), - ); - const waits: PromiseLike[] = []; - if (opening) waits.push(opening); - // A FORWARD=1 listing has nothing to unlist, so it reads its next track only once - // the request stream has taken the last skip. - if (forward && skips.size) waits.push(Signal.race(changed).then((): Woken => ({ kind: "changed" }))); - else { - reading ??= listing.next().then((event): Woken => ({ kind: "track", event })); - waits.push(reading); - } - let woken: Woken; - try { - woken = await untilAborted(race(waits), hold.signal); - } catch (err) { - // A closed broadcast's listing has already ended, after the tracks that left - // before it, so it is read to that end whatever withdrew the broadcast: a - // PUBLISH whose track finished ends with TRACK_ENDED. Nothing more goes out. - if (source.closed.peek() === undefined) throw err; - pending.clear(); - unstage(); - reading ??= listing.next().then((event): Woken => ({ kind: "track", event })); - woken = await reading; - } - if (woken.kind === "changed") continue; - if (woken.kind === "failed") { - console.debug(`listed broadcast=${namespace}: PUBLISH stream did not open`, woken.err); - break; - } - if (woken.kind === "opened") { - opening = undefined; - const next = staged; - if (!next || !adding.peek()) { - woken.stream.abort(new StreamError(StreamCode.Cancel, { message: "listing ended" })); - continue; - } - staged = undefined; - const unlisted = new Once(); - listed.set(next.name, unlisted); - const task = this.#runPublish({ - msg, - namespace, - source, - stream: woken.stream, - name: next.name, - info: next.info, - unlisted, - adding, - }) - .catch((err) => { - if (err instanceof ProtocolViolation) throw err; - }) - .finally(() => { - if (next.claim) this.#trackClaims.delete(next.claim); - tasks.delete(task); - if (!adding.peek() && !tasks.size) hold.abort(); - }); - tasks.add(task); - void task.catch(() => hold.abort()); - continue; - } - reading = undefined; - const event = woken.event; - if (!event) break; - if (event.kind === "synced") continue; - if (event.kind === "removed" || event.kind === "finished") { - if (staged?.name === event.name) unstage(); - pending.delete(event.name); - listed.get(event.name)?.set(event.kind); - listed.delete(event.name); - const key = skipped.get(event.name); - if (key) skips.delete(key); - skipped.delete(event.name); - continue; - } - if (!adding.peek()) continue; - if (forward) skip(event.name); - else pending.set(event.name, event.info); - } - } finally { - cancel(); - listing?.close(); - pending.clear(); - unstage(); - // A stream that opens from now on carries nothing. - void opening?.then((woken) => { - if (woken.kind === "opened") - woken.stream.abort(new StreamError(StreamCode.Cancel, { message: "listing ended" })); - }); - for (const entry of listed.values()) entry.set("removed"); - for (const key of skipped.values()) skips.delete(key); - await Promise.all(tasks); - } - } - - async #runPublish({ - msg, - namespace, - source, - stream, - name, - info, - unlisted, - adding, - }: { - msg: SubscribeTracks; - namespace: Path.Valid; - source: broadcast.Consumer; - /** The PUBLISH's stream, opened once the peer's credit admitted it. */ - stream: Stream; - name: string; - info: Info; - unlisted: Once; - adding: Signal; - }) { - const version = this.#session.version; - const hold = new AbortController(); - const stop = adding.subscribe((active) => { - if (!active) hold.abort(); - }); - // The peer lists the track once its PUBLISH arrives, which may be before the write - // returns, so a track that finishes from then on sees the PUBLISH through to its answer - // and ends with TRACK_ENDED; a reset would fail the peer's listing. - let publishing = false; - const removed = unlisted.subscribe((how) => { - if (how === "removed" || (how === "finished" && !publishing)) hold.abort(); - }); - const request = new RequestStream(stream, this.#session, this.#quic); - const cancelled = () => request.abort(new StreamError(StreamCode.Cancel, { message: "listing ended" })); - hold.signal.addEventListener("abort", cancelled); - try { - let peerEnded = false; - const ended = request.ended.then(() => { - peerEnded = true; - hold.abort(); - return undefined; - }); - const id = await this.#session.nextRequestId(hold.signal); - if (id === undefined) return; - const defaults = msg.parameters; - const subscription = new Subscribe({ - requestId: id, - trackNamespace: namespace, - trackName: name, - subscriberPriority: defaults.subscriberPriority ?? DEFAULT_WIRE, - groupOrder: defaults.groupOrder === GroupOrder.Ascending ? "ascending" : "descending", - filter: defaults.subscriptionFilter, - propertiesWanted: defaults.includeProperties ?? true, - }); - const parameters = new Parameters(); - if (Filter.isDraft22(version)) { - parameters.subscriberPriority = subscription.subscriberPriority; - parameters.groupOrder = - subscription.groupOrder === "ascending" ? GroupOrder.Ascending : GroupOrder.Descending; - parameters.subscriptionFilter = subscription.filter; - } - const largest = wireOf(source).largest(name); - publishing = true; - await Message.write( - request.writer, - Publish.id, - new Publish({ - requestId: id, - trackNamespace: namespace, - trackName: name, - trackAlias: id, - groupOrder: GroupOrder.Descending, - contentExists: !!largest, - largest: largest && { groupId: BigInt(largest.group), objectId: BigInt(largest.frame) }, - forward: false, - parameters, - properties: subscription.propertiesWanted - ? { - priority: toWire(info.priority), - groupOrder: Properties.DESCENDING, - timescale: info.timescale ?? undefined, - maxCacheDuration: info.maxAge === undefined ? undefined : BigInt(info.maxAge), - } - : {}, - }), - version, - ); - hold.signal.removeEventListener("abort", cancelled); - stop(); - // Once sent, the PUBLISH survives cancellation of its listing. - const state: Published = { streams: { opened: 0 }, unlisted }; - const answer = await untilAborted(request.next(), hold.signal); - publishing = false; - if (!answer) throw new ProtocolViolation("PUBLISH ended without an answer"); - if (answer.type === RequestError.id) { - await RequestError.decode(answer.reader, version).catch((err) => { - throw malformed(err, "REQUEST_ERROR"); - }); - return; - } - if (answer.type !== RequestOk.id && !(version === Version.DRAFT_18 && answer.type === 0x1e)) - throw new ProtocolViolation("expected PUBLISH_OK"); - const params = await Message.decode(answer.reader, (body) => - Parameters.decode(body, version, "publish-ok"), - ).catch((err) => { - throw malformed(err, "PUBLISH_OK"); - }); - if (params.vars.has(0x32n)) { - await this.#publishFailed(request, state.streams); - return; - } - this.#applyPublished(subscription, params); - let forward = !Filter.isDraft22(version) && params.forward !== false; - for (;;) { - if (!this.#gate.allows(namespace) || unlisted.peek() !== undefined) { - await Message.write( - request.writer, - PublishDone.id, - new PublishDone({ - statusCode: !this.#gate.allows(namespace) - ? PublishDoneStatus.UNAUTHORIZED - : unlisted.peek() === "finished" - ? PublishDoneStatus.TRACK_ENDED - : PublishDoneStatus.INTERNAL_ERROR, - streamCount: BigInt(state.streams.opened), - reasonPhrase: "track no longer listed or granted", - }), - version, - ); - return; - } - if (forward) { - const fixed = subscribeRange(subscription, {}, version); - const track = wireOf(source).subscribe(name, { - priority: fromWire(subscription.subscriberPriority), - order: subscription.groupOrder, - maxDelay: Milli(subscription.maxAge ?? Varint.MAX_U53), - groups: fixed.start ? rangeGroups(fixed) : undefined, - }); - let ready: Info; - try { - ready = await untilAborted(track.info(), hold.signal); - } catch (err) { - track.close(); - if (peerEnded) return; - if (state.answer) { - await refuseUpdate(request.writer, version, "subscribe", this.#failed(err)); - await this.#publishFailed(request, state.streams); - } else { - await Message.write( - request.writer, - PublishDone.id, - new PublishDone({ - statusCode: PublishDoneStatus.INTERNAL_ERROR, - streamCount: BigInt(state.streams.opened), - reasonPhrase: "resume failed", - }), - version, - ); - } - return; - } - const paused = await this.#forward({ - msg: subscription, - request, - track, - info: ready, - published: state, - }); - if (!paused) return; - await Message.write(request.writer, RequestOk.id, new RequestOk({}), version); - forward = false; - } - const revoked = new Once(); - const gate = this.#gate.watch(namespace, () => revoked.set(true)); - let next: Framed | undefined | true | Unlisted; - try { - next = await race([request.next(), unlisted, revoked, ended]); - } finally { - gate.dispose(); - } - if (next === true || next === "finished" || next === "removed") { - await Message.write( - request.writer, - PublishDone.id, - new PublishDone({ - statusCode: revoked.peek() - ? PublishDoneStatus.UNAUTHORIZED - : next === "finished" - ? PublishDoneStatus.TRACK_ENDED - : PublishDoneStatus.INTERNAL_ERROR, - streamCount: BigInt(state.streams.opened), - reasonPhrase: "track no longer listed or granted", - }), - version, - ); - return; - } - if (!next) return request.ended; - if (next.type !== 0x02) throw new ProtocolViolation("expected PUBLISH REQUEST_UPDATE"); - let update: Parameters; - let updateId: bigint; - try { - ({ params: update, requestId: updateId } = await this.#subscriptionUpdate(next)); - } catch (err) { - if (!(err instanceof MalformedAuthToken)) throw malformed(err, "REQUEST_UPDATE"); - await refuseUpdate(request.writer, version, "subscribe", { - condition: "malformed_auth_token", - reasonPhrase: err.message, - }); - await this.#publishFailed(request, state.streams); - return; - } - if ( - update.rangeFilters || - update.vars.has(0x32n) || - (update.fillParameters && update.forward !== true) - ) { - await refuseUpdate(request.writer, version, "subscribe", { - condition: update.rangeFilters ? "invalid_filter" : "not_supported", - reasonPhrase: "PUBLISH update parameters not supported", - }); - await this.#publishFailed(request, state.streams); - return; - } - this.#applyPublished(subscription, update); - forward = update.forward === true; - state.answer = forward ? { requestId: updateId } : undefined; - if (!forward) { - await Message.write(request.writer, RequestOk.id, new RequestOk({}), version); - } - } - } catch (err) { - request.abort(err); - throw err; - } finally { - stop(); - removed(); - hold.signal.removeEventListener("abort", cancelled); - hold.abort(); - request.close(); - } } async #subscriptionUpdate(framed: Framed): Promise<{ params: Parameters; requestId: bigint }> { diff --git a/js/net/src/ietf/subscribe_tracks.test.ts b/js/net/src/ietf/subscribe_tracks.test.ts deleted file mode 100644 index 25f75a01e70..00000000000 --- a/js/net/src/ietf/subscribe_tracks.test.ts +++ /dev/null @@ -1,673 +0,0 @@ -import { expect, onTestFinished, spyOn, test } from "bun:test"; -import { Manual, Nano } from "@moq/clock"; -import Qmux from "@moq/qmux"; -import { Signal } from "@moq/signals"; -import type { Grant } from "../auth.ts"; -import { Tracks } from "../broadcast.ts"; -import { StreamCode, StreamError } from "../error.ts"; -import { createMockTransportPair } from "../mock.ts"; -import { Producer as Origin } from "../origin.ts"; -import * as Path from "../path.ts"; -import { Reader, Stream, Writer } from "../stream.ts"; -import * as Message from "./message.ts"; -import { Group } from "./object.ts"; -import { Parameters } from "./parameters.ts"; -import { Publish, PublishDone, PublishDoneStatus } from "./publish.ts"; -import { Publisher } from "./publisher.ts"; -import { RequestError, RequestOk } from "./request.ts"; -import { NativeSession } from "./session.ts"; -import { Subscribe } from "./subscribe.ts"; -import { PublishSkipped, SubscribeTracks } from "./subscribe_tracks.ts"; -import { ALPN, type IetfVersion, Version } from "./version.ts"; - -const drafts = [Version.DRAFT_18, Version.DRAFT_22]; - -/** A client and server WebTransport, connected to each other. */ -type Pair = { client: WebTransport; server: WebTransport }; - -/** - * A qmux session pair over an in-memory socket, which, unlike the mock transport, holds each - * bidi stream against the receiver's stream credit until both its halves end. The client - * grants the server `credit` such streams at once. - */ -function qmuxPair(credit: bigint): Pair { - const socket = (readable: ReadableStream, writable: WritableStream) => { - const closed = Promise.withResolvers<{ closeCode: number; reason: string }>(); - return { - url: "", - opened: Promise.resolve({ readable, writable, extensions: "", protocol: "qmux-01" }), - closed: closed.promise, - close: () => closed.resolve({ closeCode: 1000, reason: "" }), - }; - }; - // Copied, since a sender may reuse its buffer once a write resolves. - const pipe = () => - new TransformStream({ transform: (chunk, c) => c.enqueue(chunk.slice()) }); - const up = pipe(); - const down = pipe(); - const client = new Qmux(socket(down.readable, up.writable), { - config: { idleTimeout: null, maxStreamsBidi: credit }, - }); - const server = Qmux.accept(socket(up.readable, down.writable), { config: { idleTimeout: null } }); - return { client, server }; -} - -function fixture( - version: IetfVersion, - { - grant = new Signal(undefined), - pair = createMockTransportPair(ALPN.DRAFT_22), - }: { grant?: Signal; pair?: Pair } = {}, -) { - const origin = new Origin(); - const session = new NativeSession(pair.server, version, false); - const publisher = new Publisher({ - quic: pair.server, - session, - publish: origin.consume(), - requiresSolicitation: true, - grant, - }); - const tasks: Promise[] = []; - const streams: Stream[] = []; - onTestFinished(async () => { - for (const stream of streams) stream.abort(new StreamError(StreamCode.Cancel, { message: "test ended" })); - origin.close(); - pair.server.close(); - pair.client.close(); - publisher.close(); - await Promise.all(tasks); - }); - return { - pair, - origin, - publisher, - session, - tasks, - streams, - async list(prefix = "room", forward = false, parameters = new Parameters()) { - const client = await Stream.open(pair.client, { version }); - streams.push(client); - parameters.forward = forward; - // Sent, since a transport that opens a stream lazily shows the peer none until then. - await Message.write( - client.writer, - SubscribeTracks.id, - new SubscribeTracks(0n, Path.from(prefix), parameters), - version, - ); - const server = await Stream.accept(pair.server, version); - if (!server) throw new Error("missing listing stream"); - expect(await server.reader.u53()).toBe(SubscribeTracks.id); - tasks.push(publisher.runSubscribeTracks(await SubscribeTracks.decode(server.reader, version), server)); - return client; - }, - /** The next PUBLISH, answered with a paused PUBLISH_OK unless `answer` is false. */ - async offered(answer = true) { - const stream = await Stream.accept(pair.client, version); - if (!stream) throw new Error("missing PUBLISH"); - streams.push(stream); - expect(await stream.reader.u53()).toBe(Publish.id); - const publish = await Publish.decode(stream.reader, version); - expect(publish.forward).toBe(false); - if (answer) await publishOk(stream, version); - return { stream, publish }; - }, - }; -} - -async function publishOk(stream: Stream, version: IetfVersion) { - const parameters = new Parameters(); - if (version < Version.DRAFT_22) parameters.forward = false; - await stream.writer.u53(RequestOk.id); - await new RequestOk({ parameters }).encode(stream.writer, version); -} - -async function ok(stream: Stream, version: IetfVersion) { - expect(await stream.reader.u53()).toBe(RequestOk.id); - await RequestOk.decode(stream.reader, version); -} - -async function update(stream: Stream, version: IetfVersion, id: bigint, forward: boolean) { - await stream.writer.u53(0x02); - await Message.encode(stream.writer, async (body) => { - await body.u62(id); - const parameters = new Parameters(); - parameters.forward = forward; - await parameters.encode(body, version); - }); - await ok(stream, version); -} - -for (const version of drafts) { - test(`track listing pauses, resumes, survives listing cancellation and ends on withdrawal (${version})`, async () => { - const clock = new Manual(); - const installed = clock.install(); - using _installed = { [Symbol.dispose]: () => installed.close() }; - const fx = fixture(version); - const broadcastPublication = fx.origin.createBroadcast(Path.from("room/live")); - const broadcast = broadcastPublication.broadcast; - broadcastPublication.announce(); - const track = broadcast.createTrack("video"); - const group = track.appendGroup(); - group.writeFrame({ payload: new Uint8Array([1]) }); - group.finish(); - const listing = await fx.list(); - await ok(listing, version); - const { stream, publish } = await fx.offered(); - expect(publish.trackNamespace).toBe(Path.from("room/live")); - expect(publish.trackName).toBe("video"); - const uni = fx.pair.client.incomingUnidirectionalStreams.getReader(); - let delivered = false; - const first = uni.read(); - void first.then(() => { - delivered = true; - }); - await clock.advance(Nano.zero); - expect(delivered).toBe(false); - await update(stream, version, 2n, true); - const next = await first; - if (!next.value) throw new Error("missing group"); - const reader = new Reader(next.value, undefined, version); - expect((await Group.decode(reader, version)).trackAlias).toBe(publish.trackAlias); - await reader.readAll(); - await update(stream, version, 4n, false); - listing.abort(new StreamError(StreamCode.Cancel, { message: "listing cancelled" })); - await clock.advance(Nano.zero); - await update(stream, version, 6n, true); - const resumed = await uni.read(); - if (!resumed.value) throw new Error("missing resumed group"); - await new Reader(resumed.value, undefined, version).readAll(); - await update(stream, version, 8n, false); - broadcast.close(); - expect(await stream.reader.u53()).toBe(PublishDone.id); - const done = await PublishDone.decode(stream.reader, version); - expect(done.statusCode).toBe(PublishDoneStatus.INTERNAL_ERROR); - expect(done.streamCount).toBe(2n); - uni.releaseLock(); - }); - - // The PUBLISH ends as its track left the listing: TRACK_ENDED once it finished, in error - // otherwise, since a track removed unfinished may go on elsewhere. - for (const finished of [true, false]) { - test(`an unlisted track's PUBLISH ends ${finished ? "with TRACK_ENDED" : "in error"} (${version})`, async () => { - const fx = fixture(version); - const broadcastPublication = fx.origin.createBroadcast(Path.from("room/live")); - const broadcast = broadcastPublication.broadcast; - broadcastPublication.announce(); - const track = broadcast.createTrack("video"); - const listing = await fx.list(); - await ok(listing, version); - const { stream } = await fx.offered(); - if (finished) track.finish(); - if (finished) track.close(); - broadcast.removeTrack("video"); - expect(await stream.reader.u53()).toBe(PublishDone.id); - const done = await PublishDone.decode(stream.reader, version); - expect(done.statusCode).toBe(finished ? PublishDoneStatus.TRACK_ENDED : PublishDoneStatus.INTERNAL_ERROR); - }); - } - - // The peer listed the track on its PUBLISH, so one that finishes before the answer still - // ends with TRACK_ENDED once answered, rather than a reset that fails the peer's listing. - test(`a track finished before its PUBLISH is answered ends with TRACK_ENDED (${version})`, async () => { - const clock = new Manual(); - const installed = clock.install(); - using _installed = { [Symbol.dispose]: () => installed.close() }; - const fx = fixture(version); - const broadcastPublication = fx.origin.createBroadcast(Path.from("room/live")); - const broadcast = broadcastPublication.broadcast; - broadcastPublication.announce(); - const track = broadcast.createTrack("video"); - const listing = await fx.list(); - await ok(listing, version); - const { stream } = await fx.offered(false); - track.finish(); - track.close(); - broadcast.removeTrack("video"); - await clock.advance(Nano.zero); - await publishOk(stream, version); - expect(await stream.reader.u53()).toBe(PublishDone.id); - const done = await PublishDone.decode(stream.reader, version); - expect(done.statusCode).toBe(PublishDoneStatus.TRACK_ENDED); - }); - - // The PUBLISH may reach the peer before its write returns, as when the write budget yields - // after the transport took the bytes. A track that finishes then is listed already. - test(`a track finished as its PUBLISH goes out ends with TRACK_ENDED (${version})`, async () => { - const clock = new Manual(); - const installed = clock.install(); - using _installed = { [Symbol.dispose]: () => installed.close() }; - // Spied before listing, since the PUBLISH may go out before the listing's OK is read. - // Restored only once the test is done: `Message.write` reads `writer.write` before - // awaiting its frame, so restoring mid-write would drop a concurrent write on the stale spy. - const resume = Promise.withResolvers(); - let holding = false; - const write = Writer.prototype.write; - const held = spyOn(Writer.prototype, "write").mockImplementation(async function (this: Writer, chunk) { - await write.call(this, chunk); - if (holding || chunk[0] !== Publish.id) return; - holding = true; - await resume.promise; - }); - using _held = { [Symbol.dispose]: () => held.mockRestore() }; - const fx = fixture(version); - const broadcastPublication = fx.origin.createBroadcast(Path.from("room/live")); - const broadcast = broadcastPublication.broadcast; - broadcastPublication.announce(); - const track = broadcast.createTrack("video"); - const listing = await fx.list(); - await ok(listing, version); - const { stream } = await fx.offered(false); - track.finish(); - track.close(); - broadcast.removeTrack("video"); - await clock.advance(Nano.zero); - expect(holding).toBe(true); - resume.resolve(); - await publishOk(stream, version); - expect(await stream.reader.u53()).toBe(PublishDone.id); - const done = await PublishDone.decode(stream.reader, version); - expect(done.statusCode).toBe(PublishDoneStatus.TRACK_ENDED); - }); - - // A broadcast closing right after a track finished ends that track's PUBLISH with TRACK_ENDED, - // removed first or not and answered or not, while a track left unfinished still ends in error: - // PUBLISH_DONE once answered, a reset before. - for (const remove of [false, true]) { - for (const answer of [true, false]) { - const name = `remove=${remove} answer=${answer}`; - test(`a clean end keeps its finishes, ${name} (${version})`, async () => { - const clock = new Manual(); - const installed = clock.install(); - using _installed = { [Symbol.dispose]: () => installed.close() }; - const fx = fixture(version); - const broadcastPublication = fx.origin.createBroadcast(Path.from("room/live")); - const broadcast = broadcastPublication.broadcast; - broadcastPublication.announce(); - const video = broadcast.createTrack("video"); - broadcast.createTrack("audio"); - const listing = await fx.list(); - await ok(listing, version); - const published = new Map(); - for (let i = 0; i < 2; i++) { - const { stream, publish } = await fx.offered(answer); - published.set(publish.trackName, stream); - } - await clock.advance(Nano.zero); - video.finish(); - video.close(); - if (remove) broadcast.removeTrack("video"); - broadcast.close(); - await clock.advance(Nano.zero); - const videoStream = published.get("video"); - const audioStream = published.get("audio"); - if (!videoStream || !audioStream) throw new Error("missing PUBLISH"); - if (!answer) await publishOk(videoStream, version); - expect(await videoStream.reader.u53()).toBe(PublishDone.id); - expect((await PublishDone.decode(videoStream.reader, version)).statusCode).toBe( - PublishDoneStatus.TRACK_ENDED, - ); - if (!answer) { - await expect(audioStream.reader.u53()).rejects.toThrow(); - return; - } - expect(await audioStream.reader.u53()).toBe(PublishDone.id); - expect((await PublishDone.decode(audioStream.reader, version)).statusCode).toBe( - PublishDoneStatus.INTERNAL_ERROR, - ); - }); - } - } - - test(`track listing rejects overlapping prefixes (${version})`, async () => { - const fx = fixture(version); - const broadcastPublication = fx.origin.createBroadcast(Path.from("room/live")); - const broadcast = broadcastPublication.broadcast; - broadcastPublication.announce(); - broadcast.createTrack("video"); - const listing = await fx.list(); - await ok(listing, version); - await fx.offered(); - for (const prefix of ["room/live", "", "room"]) { - const overlap = await fx.list(prefix); - expect(await overlap.reader.u53()).toBe(RequestError.id); - expect((await RequestError.decode(overlap.reader, version)).errorCode).toBe(0x30); - } - }); - - test(`forwarding listings are skipped or refused according to the draft (${version})`, async () => { - const fx = fixture(version); - const broadcastPublication = fx.origin.createBroadcast(Path.from("room/live")); - const broadcast = broadcastPublication.broadcast; - broadcastPublication.announce(); - broadcast.createTrack("video"); - const listing = await fx.list("room", true); - if (version === Version.DRAFT_18) { - expect(await listing.reader.u53()).toBe(RequestError.id); - expect((await RequestError.decode(listing.reader, version)).errorCode).toBe(3); - } else { - await ok(listing, version); - expect(await listing.reader.u53()).toBe(PublishSkipped.id); - const skipped = await PublishSkipped.decode(listing.reader, version); - expect(skipped.suffix).toBe(Path.from("live")); - expect(skipped.trackName).toBe("video"); - } - }); -} - -test("draft-18 refuses SUBSCRIBE to a PUBLISHed track", async () => { - const version = Version.DRAFT_18; - const fx = fixture(version); - const broadcastPublication = fx.origin.createBroadcast(Path.from("room/live")); - const broadcast = broadcastPublication.broadcast; - broadcastPublication.announce(); - broadcast.createTrack("video"); - const listing = await fx.list(); - await ok(listing, version); - await fx.offered(); - const client = await Stream.open(fx.pair.client, { version }); - const server = await Stream.accept(fx.pair.server, version); - if (!server) throw new Error("missing subscribe"); - fx.streams.push(client); - fx.tasks.push( - fx.publisher.runSubscribe( - new Subscribe({ - requestId: 2n, - trackNamespace: Path.from("room/live"), - trackName: "video", - subscriberPriority: 127, - }), - server, - ), - ); - expect(await client.reader.u53()).toBe(RequestError.id); - expect((await RequestError.decode(client.reader, version)).errorCode).toBe(0x19); -}); - -test("a cancelled listing cannot release its replacement's prefix", async () => { - const version = Version.DRAFT_22; - const clock = new Manual(); - const installed = clock.install(); - using _installed = { [Symbol.dispose]: () => installed.close() }; - const fx = fixture(version); - const broadcastPublication = fx.origin.createBroadcast(Path.from("room/live")); - const broadcast = broadcastPublication.broadcast; - broadcastPublication.announce(); - broadcast.createTrack("video"); - const first = await fx.list(); - await ok(first, version); - const old = await fx.offered(); - first.abort(new StreamError(StreamCode.Cancel)); - await clock.advance(Nano.zero); - const second = await fx.list(); - await ok(second, version); - await fx.offered(); - old.stream.abort(new StreamError(StreamCode.Cancel)); - await fx.tasks[0]; - const overlap = await fx.list(); - expect(await overlap.reader.u53()).toBe(RequestError.id); - expect((await RequestError.decode(overlap.reader, version)).errorCode).toBe(0x30); -}); - -test("grants hide tracks, revoke PUBLISHes and relist when broadened", async () => { - const version = Version.DRAFT_22; - const empty = (): Grant => ({ publish: new Path.Patterns([]), subscribe: new Path.Patterns([]) }); - const grant = new Signal(empty()); - const clock = new Manual(); - const installed = clock.install(); - using _installed = { [Symbol.dispose]: () => installed.close() }; - const fx = fixture(version, { grant }); - const listing = await fx.list(); - await ok(listing, version); - const broadcastPublication = fx.origin.createBroadcast(Path.from("room/live")); - const broadcast = broadcastPublication.broadcast; - broadcastPublication.announce(); - broadcast.createTrack("video"); - let delivered = false; - const offered = fx.offered(); - void offered.then(() => { - delivered = true; - }); - await clock.advance(Nano.zero); - expect(delivered).toBe(false); - grant.set(undefined); - const first = await offered; - grant.set(empty()); - expect(await first.stream.reader.u53()).toBe(PublishDone.id); - expect((await PublishDone.decode(first.stream.reader, version)).statusCode).toBe(PublishDoneStatus.UNAUTHORIZED); - grant.set(undefined); - expect((await fx.offered()).publish.trackName).toBe("video"); -}); - -test("PUBLISH carries listing defaults and honors INCLUDE_PROPERTIES", async () => { - const version = Version.DRAFT_22; - const fx = fixture(version); - const parameters = new Parameters(); - parameters.subscriberPriority = 42; - parameters.groupOrder = 1; - parameters.subscriptionFilter = { kind: "relative", groups: 2n }; - parameters.includeProperties = false; - const listing = await fx.list("room", false, parameters); - await ok(listing, version); - const broadcastPublication = fx.origin.createBroadcast(Path.from("room/live")); - const broadcast = broadcastPublication.broadcast; - broadcastPublication.announce(); - broadcast.createTrack("later"); - const { publish } = await fx.offered(); - expect(publish.properties).toEqual({}); - expect(publish.parameters?.subscriberPriority).toBe(42); - expect(publish.parameters?.groupOrder).toBe(1); - expect(publish.parameters?.subscriptionFilter).toEqual({ kind: "relative", groups: 2n }); -}); - -// An omitted SUBSCRIBER_PRIORITY is the drafts' 128, so the PUBLISH restates 128, not the unset 127. -test("PUBLISH restates an omitted listing priority as 128", async () => { - const version = Version.DRAFT_22; - const fx = fixture(version); - const listing = await fx.list("room", false, new Parameters()); - await ok(listing, version); - const broadcastPublication = fx.origin.createBroadcast(Path.from("room/live")); - const broadcast = broadcastPublication.broadcast; - broadcastPublication.announce(); - broadcast.createTrack("later"); - const { publish } = await fx.offered(); - expect(publish.parameters?.subscriberPriority).toBe(128); -}); - -test("an update ends only its listing and frees its prefix", async () => { - const version = Version.DRAFT_22; - const fx = fixture(version); - const listing = await fx.list(); - await ok(listing, version); - await listing.writer.u53(2); - await Message.encode(listing.writer, async (body) => { - await body.u62(2n); - await new Parameters().encode(body, version); - }); - expect(await listing.reader.u53()).toBe(RequestError.id); - expect((await RequestError.decode(listing.reader, version)).errorCode).toBe(3); - await fx.tasks[0]; - const replacement = await fx.list(); - await ok(replacement, version); -}); - -test("session shutdown releases listing tasks while the local origin stays open", async () => { - const version = Version.DRAFT_22; - const fx = fixture(version); - const broadcastPublication = fx.origin.createBroadcast(Path.from("room/live")); - const broadcast = broadcastPublication.broadcast; - broadcastPublication.announce(); - broadcast.createTrack("video"); - const listing = await fx.list(); - await ok(listing, version); - await fx.offered(); - fx.pair.server.close(); - await Promise.all(fx.tasks); - expect(broadcast.createTrack("still-local").name).toBe("still-local"); -}); - -for (const version of [Version.DRAFT_22]) { - test(`a filter applied while paused or while pausing is preserved on resume (${version})`, async () => { - const fx = fixture(version); - const broadcastPublication = fx.origin.createBroadcast(Path.from("room/live")); - const broadcast = broadcastPublication.broadcast; - broadcastPublication.announce(); - const track = broadcast.createTrack("video"); - for (let i = 0; i < 2; i++) { - const group = track.appendGroup(); - group.writeFrame({ payload: new Uint8Array([i]) }); - group.finish(); - } - await ok(await fx.list(), version); - const { stream } = await fx.offered(); - const filter = async (id: bigint, startGroup: bigint) => { - await stream.writer.u53(0x02); - await Message.encode(stream.writer, async (body) => { - await body.u62(id); - const params = new Parameters(); - params.forward = false; - params.subscriptionFilter = { kind: "absolute", startGroup, startObject: 0n }; - await params.encode(body, version); - }); - }; - await filter(2n, 2n); - await ok(stream, version); - await update(stream, version, 4n, true); - const fresh = track.appendGroup(); - fresh.writeFrame({ payload: new Uint8Array([2]) }); - fresh.finish(); - const uni = fx.pair.client.incomingUnidirectionalStreams.getReader(); - const next = await uni.read(); - if (!next.value) throw new Error("missing filtered group"); - const reader = new Reader(next.value, undefined, version); - expect((await Group.decode(reader, version)).groupId).toBe(2); - await reader.readAll(); - - // An active pause applies the same filter atomically before its acknowledgement. - await filter(6n, 3n); - await ok(stream, version); - while (track.subscription.peek() !== undefined) await track.subscription.changed(); - const paused = track.appendGroup(); - paused.writeFrame({ payload: new Uint8Array([3]) }); - paused.finish(); - await update(stream, version, 8n, true); - const resumed = (await uni.read()).value; - if (!resumed) throw new Error("missing group selected while paused"); - const resumedReader = new Reader(resumed, undefined, version); - expect((await Group.decode(resumedReader, version)).groupId).toBe(3); - await resumedReader.readAll(); - uni.releaseLock(); - }); -} - -/** Listed broadcast sizes to sweep: what a listing holds must not grow with them. */ -const CATALOGS = [4, 64, 512]; - -/** A broadcast under `room` listing `catalog` tracks. */ -function catalogOf(fx: ReturnType, catalog: number) { - const broadcastPublication = fx.origin.createBroadcast(Path.from("room/live")); - const broadcast = broadcastPublication.broadcast; - broadcastPublication.announce(); - for (let i = 0; i < catalog; i++) broadcast.createTrack(`t${i}`); -} - -// Each PUBLISH opens only as the peer's stream credit admits it: a peer that withholds credit and -// reads nothing leaves one stream open waiting, however many tracks are listed, and each track is -// PUBLISHed once credit returns. -for (const version of drafts) { - test(`a listing publishes only as credit admits (${version})`, async () => { - const credit = 2; - const clock = new Manual(); - const installed = clock.install(); - using _installed = { [Symbol.dispose]: () => installed.close() }; - for (const catalog of CATALOGS) { - const pair = qmuxPair(BigInt(credit)); - const open = pair.server.createBidirectionalStream.bind(pair.server); - let waiting = 0; - spyOn(pair.server, "createBidirectionalStream").mockImplementation((options) => { - waiting += 1; - const opened = open(options); - const done = () => { - waiting -= 1; - }; - opened.then(done, done); - return opened; - }); - const fx = fixture(version, { pair }); - catalogOf(fx, catalog); - const listing = await fx.list(); - await ok(listing, version); - const held = []; - for (let i = 0; i < credit; i++) held.push(await fx.offered(false)); - await clock.advance(Nano.zero); - expect(waiting, `catalog=${catalog}`).toBeLessThanOrEqual(1); - - // Refusing each PUBLISH returns its credit, which opens the next. - const published = new Set(held.map(({ publish }) => publish.trackName)); - for (const { stream } of held) stream.abort(new StreamError(StreamCode.Cancel)); - while (published.size < catalog) { - const { stream, publish } = await fx.offered(false); - expect(published.has(publish.trackName), "PUBLISHed a track twice").toBe(false); - published.add(publish.trackName); - stream.abort(new StreamError(StreamCode.Cancel)); - expect(waiting, `catalog=${catalog}`).toBeLessThanOrEqual(1); - } - } - }); -} - -// A FORWARD=1 listing reads its next track only once the request stream has taken the last -// skip, so a peer that stops reading leaves one skip queued, however many tracks are listed. -for (const version of [Version.DRAFT_22]) { - test(`a forwarding listing reads only as skips go out (${version})`, async () => { - const clock = new Manual(); - const installed = clock.install(); - using _installed = { [Symbol.dispose]: () => installed.close() }; - // Hold each skip's write until the test lets it out, as a peer that stops reading would. - const gates: (() => void)[] = []; - let arrived = Promise.withResolvers(); - const write = Message.write; - const writes = spyOn(Message, "write").mockImplementation(async (writer, id, msg, version) => { - if (id === PublishSkipped.id) - await new Promise((resolve) => { - gates.push(resolve); - arrived.resolve(); - }); - return write(writer, id, msg, version); - }); - const gate = async () => { - while (!gates.length) { - await arrived.promise; - arrived = Promise.withResolvers(); - } - return gates.shift() as () => void; - }; - const reads = spyOn(Tracks.prototype, "next"); - onTestFinished(() => { - writes.mockRestore(); - reads.mockRestore(); - }); - for (const catalog of CATALOGS) { - reads.mockClear(); - const fx = fixture(version); - catalogOf(fx, catalog); - const listing = await fx.list("room", true); - await ok(listing, version); - const skipped = new Set(); - while (skipped.size < catalog) { - const release = await gate(); - await clock.advance(Nano.zero); - // The skip being written, one queued, and the listing's SYNCED or next read. - expect(reads.mock.calls.length, `catalog=${catalog}`).toBeLessThanOrEqual(skipped.size + 3); - release(); - expect(await listing.reader.u53()).toBe(PublishSkipped.id); - const { trackName } = await PublishSkipped.decode(listing.reader, version); - expect(skipped.has(trackName), "skipped a track twice").toBe(false); - skipped.add(trackName); - } - await clock.advance(Nano.zero); - expect(gates.length, "skipped past the listing").toBe(0); - } - }); -} diff --git a/js/net/src/ietf/subscribe_tracks.ts b/js/net/src/ietf/subscribe_tracks.ts index d5022f6cb02..17a8346d285 100644 --- a/js/net/src/ietf/subscribe_tracks.ts +++ b/js/net/src/ietf/subscribe_tracks.ts @@ -6,7 +6,7 @@ import * as Namespace from "./namespace.ts"; import { Parameters } from "./parameters.ts"; import { type IetfVersion, Version } from "./version.ts"; -/** A request to list every track under a namespace prefix. @internal */ +/** A request to list every track under a namespace prefix, which we refuse. @internal */ export class SubscribeTracks { static readonly id = 0x51; readonly requestId: bigint; @@ -38,29 +38,3 @@ export class SubscribeTracks { }); } } - -/** A listed track whose objects the peer may request with SUBSCRIBE. @internal */ -export class PublishSkipped { - static readonly id = 0x0f; - readonly suffix: Path.Valid; - readonly trackName: string; - constructor(suffix: Path.Valid, trackName: string) { - this.suffix = suffix; - this.trackName = trackName; - } - - async encode(writer: Writer, version: IetfVersion): Promise { - if (version < Version.DRAFT_18) throw new Error("PUBLISH_SKIPPED before draft-18"); - await Message.encode(writer, async (body) => { - await Namespace.encode(body, this.suffix); - await body.string(this.trackName); - }); - } - - static async decode(reader: Reader, _version: IetfVersion): Promise { - return Message.decode( - reader, - async (body) => new PublishSkipped(await Namespace.decode(body), await body.string()), - ); - } -} diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index fb2c7a6f533..c81c85b4880 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -14,9 +14,8 @@ import { import * as Epoch from "./epoch.ts"; import { SessionCode, SessionError, StreamCode, StreamError, TooFarBehind } from "./error.ts"; import * as Ietf from "./ietf/index.ts"; -import { Parameters } from "./ietf/parameters.ts"; import { Publisher as IetfPublisher } from "./ietf/publisher.ts"; -import { RequestOk } from "./ietf/request.ts"; +import { RequestError } from "./ietf/request.ts"; import { refuse as refuseRequest } from "./ietf/request_stream.ts"; import { SubscribeTracks } from "./ietf/subscribe_tracks.ts"; import { hooks } from "./internal.ts"; @@ -1291,7 +1290,9 @@ test("integration: ietf fetch group is unsupported", async () => { server.abort(); }); -test("integration: cancelling an ietf track listing preserves an existing subscription", async () => { +// We list no tracks by PUBLISH, so a SUBSCRIBE_TRACKS is refused NOT_SUPPORTED on its own stream, and +// the subscription already on the session keeps going. +test("integration: ietf SUBSCRIBE_TRACKS is refused per request", async () => { for (const [protocol, version] of [ [Ietf.ALPN.DRAFT_18, Ietf.Version.DRAFT_18], [Ietf.ALPN.DRAFT_22, Ietf.Version.DRAFT_22], @@ -1322,13 +1323,10 @@ test("integration: cancelling an ietf track listing preserves an existing subscr const bidi = await pair.client.createBidirectionalStream(); const listing = new Stream({ ...bidi, version }); - const params = new Parameters(); - params.forward = false; await listing.writer.u53(SubscribeTracks.id); - await new SubscribeTracks(62n, Path.from("room"), params).encode(listing.writer, version); - expect(await listing.reader.u53()).toBe(RequestOk.id); - await RequestOk.decode(listing.reader, version); - listing.abort(new StreamError(StreamCode.Cancel)); + await new SubscribeTracks(62n, Path.from("room")).encode(listing.writer, version); + expect(await listing.reader.u53()).toBe(RequestError.id); + expect((await RequestError.decode(listing.reader, version)).errorCode).toBe(0x3); for (const producer of served) producer.writeFrame(textFrame("after")); expect(await track.readString()).toBe("after"); diff --git a/quest/m1/README.md b/quest/m1/README.md index aaebf63bfb1..388279d81c9 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -148,7 +148,6 @@ removed). - [Moxygen pin bump](/quest/m1/moxygen-pin-bump.md) - waiting on moxygen#264, then the TRACK_STATUS patch is dropped - [A smaller, safer API than release](/quest/m1/api-audit/README.md) - every surface added since release has a consumer or is gone, overlapping operations are collapsed, names mirror across Rust, JS, and the bindings, and every break says how to migrate - [moq-transport 18 and 22 only](/quest/m1/ietf-drop/README.md) - drafts 14-17 and 19-21 are deleted from Rust, JS, tests, interop lanes, and docs, removing the legacy control-stream adapter and its request limit -- [Our moq-transport publishers refuse SUBSCRIBE_TRACKS](/quest/m1/ietf-tracks-refuse.md) - a peer's listing gets NOT_SUPPORTED, while Rust still lists an IETF peer's tracks upstream for the archive - [A moq-transport request token is one auth per distinct token](/quest/m1/ietf-token-auth.md) - requests naming the same token share one verification and grant, on any request type, ending with the last such request - [moq-transport carries no epoch](/quest/m1/ietf-epoch-cut.md) - Rust and JS moq-transport sessions stop negotiating MoQ Epoch, so every moq-transport hop is epochless; the draft stays - [moq-quic's API improves on quinn's](/quest/m1/quic-api/README.md) - moq-quic and both QUIC drivers ship one mirrored API: `send::`/`recv::` namespaces, one error per direction, consuming terminal operations, plain configs, no quinn baggage diff --git a/quest/m1/archive/README.md b/quest/m1/archive/README.md index 00fd8bc4600..ff1f77deb66 100644 --- a/quest/m1/archive/README.md +++ b/quest/m1/archive/README.md @@ -32,8 +32,8 @@ archive (per-track `.timeline.z` records named by the catalog's root `broadcast.Consumer.tracks()` in JS). Over moq-transport, Rust learns an IETF peer's tracks with a FORWARD=0 SUBSCRIBE_TRACKS whose held PUBLISHes resume for data. Our publishers - refuse a peer's SUBSCRIBE_TRACKS - ([decided 2026-10-28](/quest/m1/ietf-tracks-refuse.md)), so an archive + refuse a peer's SUBSCRIBE_TRACKS (decided 2026-10-28, + [moq-dev/moq#7236](https://github.com/moq-dev/moq/pull/7236)), so an archive reading through a moq-transport hop of ours gets no listing. Rejected: SUBSCRIBE_ALL, whose relay had to merge pushed feeds into pulled copies ([moq-dev/moq#5931](https://github.com/moq-dev/moq/pull/5931)). The archive diff --git a/quest/m1/ietf-tracks-refuse.md b/quest/m1/ietf-tracks-refuse.md deleted file mode 100644 index 172b45b6a28..00000000000 --- a/quest/m1/ietf-tracks-refuse.md +++ /dev/null @@ -1,48 +0,0 @@ -# [M] Our moq-transport publishers refuse SUBSCRIBE_TRACKS - -## Goal - -A peer's SUBSCRIBE_TRACKS is refused with NOT_SUPPORTED by the Rust and JS -moq-transport publishers, as before -[moq-dev/moq#6167](https://github.com/moq-dev/moq/pull/6167), and the session -stays up. Rust keeps sending SUBSCRIBE_TRACKS upstream -([moq-dev/moq#6280](https://github.com/moq-dev/moq/pull/6280)), so a TRACKS -listing, and the archive that uses it, still learns an IETF peer's tracks. - -## Plan - -Checkpoint 2026-10-29: the draft contains only an initial JS refusal test. -Both production publishers remain unchanged, so the Goal is not implemented. -The agent stopped when its selected model reached capacity; finish the -implementation and validate the test before marking the PR ready. - -Decided with the maintainer on 2026-10-28 (`/quest-plan`, "what's not worth -supporting in IETF"). - -Decision prompt: ✅ cut inbound only; cut both; keep both. - -Why: answering takes `rs/moq-net/src/ietf/publisher/tracks.rs` (2,558 -lines) and its JS mirror in `js/net/src/ietf/publisher.ts`. That covers a -paused PUBLISH per track, FORWARD toggles, resume through REQUEST_UPDATE, -PUBLISH_SKIPPED, PREFIX_OVERLAP, and credit-gated opens, which took -[moq-dev/moq#6641](https://github.com/moq-dev/moq/pull/6641), -[moq-dev/moq#6663](https://github.com/moq-dev/moq/pull/6663), and -[moq-dev/moq#6577](https://github.com/moq-dev/moq/pull/6577). No known third -party sends it. The interop runner's SUBSCRIBE_TRACKS cases are already -listed as unsupported in -[the interop audit](/quest/m2/audit-interop.md), and our own peers list over -moq-lite. The outbound half stays because the -[archive](/quest/m1/archive/README.md) finds an IETF peer's tracks only -through it, having no catalog. - -Delete `publisher/tracks.rs`, the JS answerer, their tests, and the -`subscribe-tracks` JS bench, plus any of `rs/moq-net/benches/tracks.rs` -that measures only the IETF answerer. Keep the decoder, so a refusal still -admits the Request ID first. Change `test/interop/tracks.ts` to assert the -refusal where it drove our publisher, and keep any case that drives Rust's -outbound listing against a third party. Update the `SUBSCRIBE_TRACKS` -bullets of `doc/concept/standard.md`. Add a planned break if a compat cell -covers it. - -Public API: none. Wire: our publishers refuse SUBSCRIBE_TRACKS with -NOT_SUPPORTED. diff --git a/rs/moq-net/src/ietf/error.rs b/rs/moq-net/src/ietf/error.rs index 9a7c86e362e..e94a03d4a99 100644 --- a/rs/moq-net/src/ietf/error.rs +++ b/rs/moq-net/src/ietf/error.rs @@ -137,9 +137,6 @@ pub(crate) mod request { /// The endpoint is overloaded and cannot process the request. pub(crate) const EXCESSIVE_LOAD: u64 = 0x9; - /// A SUBSCRIBE_NAMESPACE or SUBSCRIBE_TRACKS prefix overlapping an established one. - pub(crate) const PREFIX_OVERLAP: u64 = 0x30; - /// A GOAWAY is draining the session, so no new request is accepted. const GOING_AWAY: u64 = 0x6; diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 5fa5b1099c0..c2d952f0940 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -19,7 +19,7 @@ use crate::transport::poll::{GroupOf, SendOptions}; use crate::{ DecodeError, Error, Timescale, Timestamp, coding::{Encode as _, Encoder, Stream, Writer}, - ietf::{self, Control, EndLocation, FetchHeader, FetchType, Filter, GroupOrder, Location, RequestId}, + ietf::{self, Control, EndLocation, FetchHeader, FetchType, Filter, Location, RequestId}, priority::{Priority, PriorityHandle, PriorityQueue}, track::{Order, Subscription}, util::{MaybeBoxedExt, MaybeSendBox}, @@ -27,8 +27,6 @@ use crate::{ use super::{Message, Version, cluster, error::request, peer, request_update}; -mod tracks; - /// Preserve retained groups when the subscriber supplies no age budget. fn serving_subscription(subscriber_priority: u8) -> Subscription { Subscription { @@ -800,8 +798,6 @@ pub(super) struct Publisher { pub(super) subscriptions: crate::session::Slots, // Ranks every subscription's group streams against each other for the transport. priority: PriorityQueue, - // The peer's SUBSCRIBE_TRACKS requests and the PUBLISHes they made. - listings: kio::Shared, } /// Authorization selected by dispatch: a standalone path or a live parent subscription. @@ -884,7 +880,14 @@ impl Publisher { owed: Default::default(), subscriptions: Default::default(), priority: Default::default(), - listings: Default::default(), + } + } + + /// Mark a paused subscription for joining FETCHes: draft-18 section 5.1 refuses one + /// unless the subscription forwards. + fn pause_join(&self, join: Option<&Join>>) { + if let Some(join) = join { + join.joins.lock().insert(join.request_id, Some(Joined::Empty)); } } @@ -1139,9 +1142,17 @@ impl Publisher { return Err(Error::WrongSize); } tracing::debug!(message = ?msg, "received subscribe_tracks"); + // We list no tracks by PUBLISH, so each listing is refused on its own and the + // session stays up. Decoded first, so a refusal still admits its Request ID. + let error = + super::namespace::refusal(&msg.prefix, this.version).unwrap_or_else(|| ietf::RequestError { + error_code: request::NOT_SUPPORTED, + reason_phrase: "SUBSCRIBE_TRACKS is not supported".into(), + retry_interval: 0, + }); async move { - if let Err(err) = this.serve_subscribe_tracks_stream(stream, msg).await { - tracing::debug!(%err, "subscribe_tracks stream error"); + if let Err(err) = super::request_stream::reject(stream, error).await { + tracing::debug!(%err, "subscribe_tracks refusal failed"); } } .maybe_boxed() @@ -1233,25 +1244,6 @@ impl Publisher { if let Some(error) = super::namespace::refusal(&msg.track_namespace, self.version) { return super::request_stream::reject(stream, error).await; } - // Draft-18 allows one subscription per track and role (section 5.1), so a track a - // SUBSCRIBE_TRACKS PUBLISHed is subscribed already, and one subscribed here is not - // PUBLISHed. Later drafts dropped the rule. - let _subscribed = match self.version { - Version::Draft18 => { - match tracks::Subscribed::subscribe(&self.listings, &msg.track_namespace, &msg.track_name) { - Some(subscribed) => Some(subscribed), - None => { - let error = ietf::RequestError { - error_code: request::DUPLICATE_SUBSCRIPTION, - reason_phrase: "the track is PUBLISHed to this session".into(), - retry_interval: 0, - }; - return super::request_stream::reject(stream, error).await; - } - } - } - _ => None, - }; let request_id = msg.request_id; let track_name = msg.track_name.clone(); // Logged beside the root rather than joined, so a namespace too long under it is @@ -6756,6 +6748,7 @@ mod subscribe_cursor_test { mod serve_tests { use super::*; use crate::coding::{Decode, Decoder}; + use crate::ietf::GroupOrder; use crate::lite::test_transport::{Log, ScriptedSession, Sent, SinkSession}; use crate::model::ProduceTest; @@ -14043,6 +14036,7 @@ mod serve_tests { mod tests { use super::*; use crate::coding::Decode; + use crate::ietf::GroupOrder; use crate::lite::test_transport::SinkSession; use crate::model::ProduceTest; use futures::FutureExt; @@ -16867,6 +16861,7 @@ fn fill_range(fill: ietf::Fill, subscription: Filter, largest: Option) mod range_tests { use super::*; use crate::ietf::EndLocation; + use crate::ietf::GroupOrder; fn subscribe(filter: Filter) -> ietf::Subscribe<'static> { ietf::Subscribe { @@ -17203,6 +17198,7 @@ mod range_tests { #[cfg(test)] mod fill_range_tests { use super::*; + use crate::ietf::GroupOrder; /// Objects 0 through 4 of group 100 are published. const LARGEST: Option = Some(Location { group: 100, object: 4 }); diff --git a/rs/moq-net/src/ietf/publisher/tracks.rs b/rs/moq-net/src/ietf/publisher/tracks.rs deleted file mode 100644 index 416d5fe5331..00000000000 --- a/rs/moq-net/src/ietf/publisher/tracks.rs +++ /dev/null @@ -1,2562 +0,0 @@ -//! Answers a moq-transport SUBSCRIBE_TRACKS (draft-18+) through the TRACKS hook. -//! -//! Every track of every broadcast under the request's prefix is listed, later tracks and -//! broadcasts included, but no track's data is pushed unasked. A FORWARD=0 request gets a -//! paused PUBLISH per track, which carries the track's properties and an error PUBLISH_DONE -//! once the track is no longer listed; the subscriber resumes one with REQUEST_UPDATE to get its -//! data, served from the cache as a SUBSCRIBE would be. A FORWARD=1 request asks for every -//! track's data, which no draft lets us downgrade, so from draft-22 each track is answered -//! with PUBLISH_SKIPPED and the subscriber SUBSCRIBEs to what it wants. Draft-18 has no such -//! skip, so there the request is refused. - -use crate::driver::Transport; -use std::collections::HashSet; - -use hashlink::LinkedHashMap; - -use super::*; -use crate::util::TaskSet; - -/// The session's SUBSCRIBE_TRACKS bookkeeping, shared by its requests. -#[derive(Default)] -pub(super) struct Listings { - /// The prefix of every established SUBSCRIBE_TRACKS, by a key its guard removes. - prefixes: HashMap, - next: u64, - /// On draft-18, the tracks a PUBLISH of ours subscribes the peer to, and how many - /// SUBSCRIBEs of the peer's each track has: draft-18 allows the peer one subscription - /// per track (section 5.1), so a PUBLISH joins neither kind. - published: HashMap<(crate::Path, Arc), usize>, - subscribed: HashMap<(crate::Path, Arc), usize>, -} - -/// Holds a SUBSCRIBE_TRACKS prefix as established until dropped. -struct Established { - listings: kio::Shared, - key: u64, -} - -impl Established { - /// Establish `prefix`, unless it overlaps an established request (section 10.19): two - /// would match the same namespaces, and keeping them apart is also what lists each - /// broadcast at most once per session. - fn new(listings: &kio::Shared, prefix: &crate::Path) -> Option { - let mut state = listings.lock(); - if state - .prefixes - .values() - .any(|other| other.has_prefix(prefix) || prefix.has_prefix(other)) - { - return None; - } - let key = state.next; - state.next += 1; - state.prefixes.insert(key, prefix.clone()); - Some(Self { - listings: listings.clone(), - key, - }) - } -} - -impl Drop for Established { - fn drop(&mut self) { - self.listings.lock().prefixes.remove(&self.key); - } -} - -/// Counts one subscription to a track, a PUBLISH of ours or a SUBSCRIBE of the peer's, -/// until dropped. -pub(super) struct Subscribed { - listings: kio::Shared, - key: (crate::Path, Arc), - /// A SUBSCRIBE of the peer's, rather than a PUBLISH of ours. - direct: bool, -} - -impl Subscribed { - /// Claim `namespace`'s `track` for a PUBLISH of ours, unless the peer is subscribed to - /// it already, by its SUBSCRIBE or another PUBLISH. - fn publish(listings: &kio::Shared, namespace: crate::Path, track: Arc) -> Option { - Self::claim(listings, (namespace, track), false) - } - - /// Count the peer's SUBSCRIBE to `namespace`'s `track`, unless a PUBLISH of ours - /// subscribes it already. - pub(super) fn subscribe(listings: &kio::Shared, namespace: &crate::Path, track: &str) -> Option { - Self::claim(listings, (namespace.clone(), Arc::from(track)), true) - } - - fn claim(listings: &kio::Shared, key: (crate::Path, Arc), direct: bool) -> Option { - let mut locked = listings.lock(); - let Listings { - published, subscribed, .. - } = &mut *locked; - let (mine, other) = if direct { - (subscribed, published) - } else { - (published, subscribed) - }; - if other.contains_key(&key) || (!direct && mine.contains_key(&key)) { - return None; - } - *mine.entry(key.clone()).or_default() += 1; - Some(Self { - listings: listings.clone(), - key, - direct, - }) - } -} - -impl Drop for Subscribed { - fn drop(&mut self) { - let mut listings = self.listings.lock(); - let counts = if self.direct { - &mut listings.subscribed - } else { - &mut listings.published - }; - if let Some(count) = counts.get_mut(&self.key) { - *count -= 1; - if *count == 0 { - counts.remove(&self.key); - } - } - } -} - -/// The subscription parameters a PUBLISH subscription has, starting from those of the -/// SUBSCRIBE_TRACKS that made it (draft-22 section 3.6.2) and moved by each update. -#[derive(Clone, Copy)] -struct Params { - priority: u8, - order: Option, - filter: Filter, - max_age: Option, - /// NEW_GROUP_REQUEST, applied on the first resume. - new_group: Option, -} - -/// The listed tracks a request answers with PUBLISH_SKIPPED and has not sent yet, keyed by -/// (suffix, name) in listing order. A track removed before its skip went out cancels it. -/// A FORWARD=1 listing adds one only once this is empty, so a peer that stops reading -/// leaves the rest unread in each broadcast's listing. -type Skips = kio::Shared), ()>>; - -/// What a request's listing shares with the tasks listing each broadcast. -#[derive(Clone)] -struct Request { - origin: origin::Consumer, - /// The request's prefix, which every PUBLISH namespace starts with. - prefix: crate::Path, - /// Whether the request asked for every track's objects, which we answer with skips. - forward: bool, - params: Params, - properties_wanted: bool, - skips: Skips, - /// Closes when the request is cancelled: nothing more is PUBLISHed or skipped, but the - /// PUBLISHes already sent live on. - live: kio::Consumer<()>, -} - -/// Why a listed broadcast's route closed. -#[derive(Clone, Copy, PartialEq, Eq)] -enum Route { - /// Another instance took the path: the PUBLISHes of this one end now. - Replaced, - /// Nothing serves the path any more, so its broadcast ends soon too: its listing is read - /// to that end, which says which tracks finished. - Withdrawn, -} - -/// One listed broadcast's tracks, by how far each got toward the peer. -#[derive(Default)] -struct Entries { - /// The tracks waiting their turn for a PUBLISH, in listing order. - pending: LinkedHashMap, track::Info>, - /// The next track to PUBLISH, waiting on the peer's stream credit. - staged: Option, - /// Each sent PUBLISH's entry: dropping it says the track is no longer listed. - listed: HashMap, kio::Producer>, - /// The tracks with a skip queued, so the broadcast's end cancels only its own. - skipped: HashSet>, -} - -impl Entries { - fn skip(&mut self, skips: &Skips, suffix: &crate::Path, name: Arc) { - skips.lock().insert((suffix.clone(), name.clone()), ()); - self.skipped.insert(name); - } - - /// `name` is no longer listed: drop whatever it got to, ending its PUBLISH with - /// TRACK_ENDED if it `finished`. - fn remove(&mut self, skips: &Skips, suffix: &crate::Path, name: Arc, finished: bool) { - if self.staged.as_ref().is_some_and(|staged| staged.name == name) { - self.staged = None; - } - self.pending.remove(&name); - if self.skipped.remove(&name) { - skips.lock().remove(&(suffix.clone(), name.clone())); - } - if let Some(entry) = self.listed.remove(&name) - && finished - && let Ok(mut done) = entry.write() - { - *done = true; - } - } - - /// Nothing more is PUBLISHed or skipped, while the PUBLISHes already sent stay listed. - fn halt(&mut self, skips: &Skips, suffix: &crate::Path) { - self.pending.clear(); - self.staged = None; - let mut skips = skips.lock(); - for name in self.skipped.drain() { - skips.remove(&(suffix.clone(), name)); - } - } - - /// Every track is no longer listed. - fn unlist(&mut self, skips: &Skips, suffix: &crate::Path) { - self.halt(skips, suffix); - self.listed.clear(); - } -} - -/// The next track to PUBLISH, ready but for its stream. -struct Staged { - name: Arc, - track: track::Consumer, - properties: ietf::Properties, - /// On draft-18, claims the track's one subscription from now on. - subscribed: Option, -} - -/// One listed track, handed to its PUBLISH task. -struct Listed { - /// The PUBLISH's stream, opened once the peer's credit admitted it. - stream: Stream, - track: track::Consumer, - /// The track's full namespace, as the PUBLISH names it. - namespace: crate::Path, - name: Arc, - properties: ietf::Properties, - /// Closes once the track is no longer listed, holding whether it finished. - listed: kio::Consumer, - /// Closes once the request is cancelled, which a PUBLISH not yet sent heeds. - live: kio::Consumer<()>, - /// Counts the PUBLISH among the session's subscriptions for as long as it lives. - slot: crate::session::Slot, - /// On draft-18, claims the track's one subscription for as long as the PUBLISH lives. - subscribed: Option, - params: Params, -} - -/// How a paused PUBLISH subscription resumes forwarding. -struct Resume { - /// FILL_PARAMETERS (draft-22) for the objects before the join. - fill: Option, - /// NEW_GROUP_REQUEST, counted as a join as on a SUBSCRIBE. - new_group: Option, - /// Whether a REQUEST_UPDATE asked for it, which the join's REQUEST_OK answers. - answer: bool, -} - -/// What ended a paused PUBLISH subscription's wait. -enum Wait { - /// The subscriber's REQUEST_UPDATE. - Update(super::super::request_stream::Update), - /// The track is no longer listed. - Removed, - /// Our grant stopped covering the track. - Revoked, - /// The subscriber cancelled, or the session ended. - Cancelled, - /// The request stream failed. - Failed(Error), -} - -impl Publisher { - /// Answer a SUBSCRIBE_TRACKS: list every track under its prefix until the subscriber - /// cancels or the session ends, then let the PUBLISHes it made run to their own end. - pub(super) async fn serve_subscribe_tracks_stream( - self, - mut stream: Stream, - msg: ietf::SubscribeTracks, - ) -> Result<(), Error> { - if let Some(error) = super::super::namespace::refusal(&msg.prefix, self.version) { - return super::super::request_stream::reject(stream, error).await; - } - let refuse = |error_code, reason: &'static str| ietf::RequestError { - error_code, - reason_phrase: reason.into(), - retry_interval: 0, - }; - // Filters we do not implement are refused rather than ignored, which would hand the - // subscriber tracks or objects it excluded. - if msg.range_filters { - let error = refuse(request::INVALID_FILTER, "Range Filters exceed MAX_FILTER_RANGES=0"); - return super::super::request_stream::reject(stream, error).await; - } - if msg.track_filter { - let error = refuse(request::NOT_SUPPORTED, "TRACK_PROPERTY_FILTER is not supported"); - return super::super::request_stream::reject(stream, error).await; - } - // We never push a track's objects unasked, and draft-18 has no PUBLISH_SKIPPED to - // list a track without them: its PUBLISH_BLOCKED only reports running out of streams. - if msg.forward && self.version == Version::Draft18 { - let error = refuse(request::NOT_SUPPORTED, "SUBSCRIBE_TRACKS needs FORWARD=0"); - return super::super::request_stream::reject(stream, error).await; - } - let prefix = msg.prefix.clone(); - let Some(established) = Established::new(&self.listings, &prefix) else { - let error = refuse(request::PREFIX_OVERLAP, "overlaps another SUBSCRIBE_TRACKS"); - return super::super::request_stream::reject(stream, error).await; - }; - - tracing::debug!(prefix = %crate::path::Rooted(self.origin.root(), &prefix), forward = msg.forward, "subscribe_tracks stream"); - - // The view SUBSCRIBE_NAMESPACE discovers through: rooted at the prefix so every - // broadcast arrives named by its suffix, never handing back what flowed through the - // subscriber, and without hidden broadcasts unless the peer opted in. - let scope = crate::Pattern::subtree(prefix.as_str()) - .map(|subtree| subtree.rebase(prefix.as_str())) - .unwrap_or_default(); - let peer = self.peer().await; - let declared = self.peer_setup.get().await; - // With MoQ Auth, list nothing until the credential we presented at setup is answered, - // so no track is listed before our grant says which ones the peer may see. The wait - // lives in the listing loop so a cancel or a closed session still frees the prefix. - let mut answered = !declared.auth; - let origin = self - .origin - .scope(&prefix, &scope) - .unwrap_or_else(|_| self.origin.empty()) - .excluding(self.exclude(&peer)) - .discovery(!declared.hidden || self.origin.includes_hidden()); - let mut announced = origin.announced(); - - stream - .writer - .encode_message(&ietf::RequestOk { - active: None, - largest: None, - }) - .await?; - - // Dropped when the request is cancelled, which tells every broadcast to stop. - let live = kio::Producer::new(()); - let request = Request { - origin, - prefix: prefix.clone(), - forward: msg.forward, - params: Params { - priority: msg.subscriber_priority.unwrap_or(crate::ietf::priority::DEFAULT_WIRE), - order: msg.group_order, - filter: msg.filter.unwrap_or(Filter::Unfiltered), - max_age: None, - new_group: msg.new_group, - }, - properties_wanted: msg.properties_wanted, - skips: Skips::default(), - live: live.consume(), - }; - // Each listed broadcast's route, closed once its announcement goes; see [`Route`]. - let mut routes: HashMap> = HashMap::new(); - let withdraw = |routes: &mut HashMap>, prefix: &crate::Path| { - if let Some(route) = routes.remove(prefix) - && let Ok(mut route) = route.write() - { - *route = Route::Withdrawn; - } - }; - let mut broadcasts = TaskSet::owned(); - let session = self.session.clone(); - let mut finished = false; - let mut goaways = super::super::request_stream::Messages::new(self.session.clone(), self.control.client()); - - enum Event { - Announce(crate::announce::Event), - /// The subscriber ended the request, or the stream failed. - Cancelled(Result<(), Error>), - Closed, - } - - let cancelled = loop { - let event = broadcasts - .drive(|waiter| { - if session.poll_closed(waiter).is_ready() { - return Poll::Ready(Event::Closed); - } - if let Poll::Ready(res) = self.poll_listing_cancel(&mut stream, &mut finished, &mut goaways, waiter) - { - return Poll::Ready(Event::Cancelled(res)); - } - if let Err(err) = Self::poll_write_skips(&mut stream, &request.skips, waiter) { - return Poll::Ready(Event::Cancelled(Err(err))); - } - if !answered { - ready!(self.auth.poll_setup_answered(waiter)); - answered = true; - } - match announced.poll_next(waiter) { - Poll::Ready(Some(event)) => Poll::Ready(Event::Announce(event)), - _ => Poll::Pending, - } - }) - .await; - - let announce = match event { - Event::Closed => return Ok(()), - Event::Cancelled(res) => break res, - Event::Announce( - crate::announce::Event::Start(announce) | crate::announce::Event::Restart(announce), - ) => announce, - Event::Announce(crate::announce::Event::End(announce)) => { - withdraw(&mut routes, &announce.prefix); - continue; - } - // A new hop or cost changes nothing listed. - Event::Announce(crate::announce::Event::Update(_)) => continue, - }; - // A restart is a new instance: the old one's listing ends with its route. - let route = kio::Producer::new(Route::Replaced); - let task = self.clone().run_listed_broadcast( - request.clone(), - announce.prefix.clone(), - announce.route.epoch.clone(), - route.consume(), - ); - routes.insert(announce.prefix, route); - broadcasts.push(task); - }; - - tracing::debug!(prefix = %crate::path::Rooted(self.origin.root(), &prefix), ?cancelled, "subscribe_tracks ended"); - // The request no longer stands: another may take its prefix, nothing more is - // PUBLISHed, and what was never sent is dropped. Its routes stay, so a PUBLISH - // already sent still ends when its track does. - drop(established); - drop(live); - request.skips.lock().clear(); - // A refused update left its REQUEST_ERROR buffered, so a cancel flushes before the FIN. - let mut closing = match cancelled { - Ok(()) => Some(stream.writer), - Err(err) => { - stream.writer.abort(&err); - None - } - }; - let mut fin = false; - - // The PUBLISHes already sent are subscriptions of their own, on their own streams, - // each ending once its broadcast does, even while the peer is slow to take the close. - loop { - let event = kio::wait(|waiter| { - if let Some(writer) = closing.as_mut() { - let closed = 'close: { - if !fin { - match writer.poll_flush(waiter) { - Poll::Pending => break 'close Poll::Pending, - Poll::Ready(res) => { - if res.and_then(|()| writer.finish()).is_err() { - break 'close Poll::Ready(()); - } - } - } - fin = true; - } - writer.poll_close(waiter).map(|_| ()) - }; - if closed.is_ready() { - closing = None; - } - } - if session.poll_closed(waiter).is_ready() { - return Poll::Ready(None); - } - if broadcasts.poll(waiter).is_ready() && closing.is_none() { - return Poll::Ready(None); - } - announced.poll_next(waiter) - }) - .await; - match event { - Some(crate::announce::Event::End(announce)) => withdraw(&mut routes, &announce.prefix), - Some(crate::announce::Event::Restart(announce)) => { - routes.remove(&announce.prefix); - } - Some(_) => {} - None => return Ok(()), - } - } - } - - /// Poll for the subscriber ending a SUBSCRIBE_TRACKS: `Ok` for a cancel, or the error - /// ending it, such as a REQUEST_UPDATE we refuse. - fn poll_listing_cancel( - &self, - stream: &mut Stream, - finished: &mut bool, - goaways: &mut super::super::request_stream::Messages, - waiter: &kio::Waiter, - ) -> Poll> { - use super::super::request_stream::{NamespaceUpdate, Next}; - if stream.writer.poll_closed(waiter).is_ready() { - return Poll::Ready(Ok(())); - } - while !*finished { - match ready!(stream.reader.poll_decode_maybe::>>(waiter)) { - Ok(Some(Next::Message(update))) => { - // Its Request ID is spent like any other: a bad one is the session's error, - // and one past our GOAWAY is refused as such. - let going_away = match self.control.accept(update.request_id) { - Err(Error::GoingAway) => true, - Err(err) => { - self.session.clone().close( - super::super::error::to_session_code(&crate::SessionError::from(&err)), - "invalid request ID", - ); - return Poll::Ready(Err(err)); - } - Ok(_) => false, - }; - // A new prefix or FORWARD would change which later tracks are listed and - // how, which we do not support. Refusing it ends the request (section 10.9.1). - let (err, reason) = match going_away { - true => (Error::GoingAway, "going away"), - false => ( - update.refused.unwrap_or(Error::Unsupported), - "REQUEST_UPDATE on SUBSCRIBE_TRACKS is not supported", - ), - }; - let code = request::to_code(&err, request::Kind::SubscribeNamespace, self.version); - stream.writer.buffer_message( - ietf::RequestError::ID, - &ietf::RequestError { - error_code: code, - reason_phrase: reason.into(), - retry_interval: 0, - }, - )?; - return Poll::Ready(Ok(())); - } - Ok(Some(Next::GoAway { redirect })) => goaways.receive(redirect)?, - Ok(None) if !super::super::request_stream::fin_cancels(self.version) => *finished = true, - Ok(None) => return Poll::Ready(Ok(())), - Err(err) => return Poll::Ready(Err(goaways.failed(err))), - } - } - Poll::Pending - } - - /// Write the pending PUBLISH_SKIPPEDs, buffering the next only once the last is out, so - /// a peer that stops reading leaves the rest collapsing in `skips`. - fn poll_write_skips( - stream: &mut Stream, - skips: &Skips, - waiter: &kio::Waiter, - ) -> Result<(), Error> { - while stream.writer.poll_flush(waiter)?.is_ready() { - let Poll::Ready(mut pending) = skips.poll(waiter, |skips| match skips.is_empty() { - true => Poll::Pending, - false => Poll::Ready(()), - }) else { - break; - }; - let Some(((suffix, name), ())) = pending.pop_front() else { - break; - }; - drop(pending); - stream.writer.buffer_message( - ietf::PublishSkipped::ID, - &ietf::PublishSkipped { - suffix, - track_name: name.as_ref().into(), - }, - )?; - } - Ok(()) - } - - /// List one broadcast under a SUBSCRIBE_TRACKS: PUBLISH or skip each of its tracks until - /// its route goes or the broadcast ends, then let its PUBLISHes run to their end. - /// - /// A PUBLISH starts only once the peer's stream credit opens its stream, one staged at a - /// time, so the PUBLISHes a peer holds are bounded by the credit it grants. The listing is - /// still read meanwhile, since an unlisted track's PUBLISH_DONE is what returns that - /// credit; the tracks read but not yet PUBLISHed wait in `pending`, moved off the - /// listing's own queue rather than copied. A FORWARD=1 listing has nothing to unlist, so - /// it reads its next track only once the request stream has taken the last skip. - async fn run_listed_broadcast( - self, - request: Request, - suffix: crate::Path, - epoch: Option, - route: kio::Consumer, - ) { - let namespace = request.prefix.rejoin(&suffix); - let absolute = self.origin.root().rejoin(&namespace); - let flate = self.peer_setup.get().await.flate; - - // Resolving may wait on a route for the broadcast, which its going ends. - let resolved = { - let mut requesting = std::pin::pin!(request.origin.request(&suffix, epoch)); - kio::wait(|waiter| { - if route.poll_closed(waiter).is_ready() || request.live.poll_closed(waiter).is_ready() { - return Poll::Ready(None); - } - waiter.poll_future(requesting.as_mut()).map(Some) - }) - .await - }; - let broadcast = match resolved { - Some(Ok(broadcast)) => broadcast, - Some(Err(err)) => { - tracing::debug!(broadcast = %absolute, %err, "listed broadcast did not resolve"); - return; - } - None => return, - }; - let mut entries = Entries::default(); - let mut publishes = TaskSet::owned(); - let session = self.session.clone(); - // The listing ended, the request did, or the route was withdrawn: nothing more to add. - let mut adding = true; - let mut withdrawn = false; - // The broadcast's listing, held only while our publish permit covers it: a track - // it does not cover is not revealed at all, and one it comes to cover is listed - // from scratch. - let (permit, mut auth_epoch) = self.permit_now(None); - let mut tracks = None; - if permit.matches(namespace.as_str()) { - match broadcast.tracks() { - Ok(listing) => tracks = Some(listing), - Err(err) => { - tracing::debug!(broadcast = %absolute, %err, "listed broadcast ended"); - return; - } - } - } - - enum Event { - Track(crate::broadcast::TracksEvent), - /// The staged track's PUBLISH stream opened. - Opened(Result, Error>), - /// Our publish permit may have changed. - Regrant(crate::auth::Permit), - /// The broadcast ended or another instance replaced it: every track it listed is gone. - Ended, - /// The route was withdrawn: nothing more is listed, and the broadcast ends soon. - Withdrawn, - /// The request was cancelled. - Cancelled, - } - - loop { - // Stage the next pending track that will be PUBLISHed, skipping those that will - // not, so a stream is opened only for a PUBLISH that goes out. - while adding - && entries.staged.is_none() - && let Some((name, info)) = entries.pending.pop_front() - { - // A track this draft cannot describe is skipped rather than PUBLISHed with - // its properties rewritten. - let Ok(properties) = track_properties(&info, request.properties_wanted, self.version, flate) else { - entries.skip(&request.skips, &suffix, name); - continue; - }; - let Ok(track) = broadcast.track(&name) else { - continue; - }; - // Draft-18 allows one subscription per track (section 5.1), so a track the - // peer is subscribed to, by its SUBSCRIBE or a PUBLISH still alive, is - // skipped. Held from now, so a SUBSCRIBE cannot slip in before the PUBLISH - // goes out. - let subscribed = match self.version { - Version::Draft18 => match Subscribed::publish(&self.listings, namespace.clone(), name.clone()) { - Some(subscribed) => Some(subscribed), - None => { - entries.skip(&request.skips, &suffix, name); - continue; - } - }, - _ => None, - }; - entries.staged = Some(Staged { - name, - track, - properties, - subscribed, - }); - } - - let event = kio::wait(|waiter| { - let drained = publishes.poll(waiter).is_ready(); - if !adding && drained { - return Poll::Ready(None); - } - if adding && request.live.poll_closed(waiter).is_ready() { - return Poll::Ready(Some(Event::Cancelled)); - } - if !withdrawn && route.poll_closed(waiter).is_ready() { - return Poll::Ready(Some(match *route.read() { - Route::Replaced => Event::Ended, - Route::Withdrawn => Event::Withdrawn, - })); - } - if let Poll::Ready(permit) = - self.auth - .poll_permit(crate::auth::Direction::Publish, &mut auth_epoch, waiter) - { - return Poll::Ready(Some(Event::Regrant(permit))); - } - if entries.staged.is_some() - && let Poll::Ready(opened) = Stream::poll_open(&session, self.version, waiter) - { - return Poll::Ready(Some(Event::Opened(opened))); - } - let Some(tracks) = tracks.as_mut() else { - return Poll::Pending; - }; - if request.forward - && request - .skips - .poll(waiter, |skips| match skips.is_empty() { - true => Poll::Ready(()), - false => Poll::Pending, - }) - .is_pending() - { - return Poll::Pending; - } - match tracks.poll_next(waiter) { - Poll::Ready(Ok(Some(event))) => Poll::Ready(Some(Event::Track(event))), - Poll::Ready(Ok(None)) => Poll::Ready(Some(Event::Ended)), - Poll::Ready(Err(err)) => { - tracing::debug!(broadcast = %absolute, %err, "listing failed"); - Poll::Ready(Some(Event::Ended)) - } - Poll::Pending => Poll::Pending, - } - }) - .await; - - let Some(event) = event else { - return; - }; - match event { - Event::Ended => { - entries.unlist(&request.skips, &suffix); - // What it already PUBLISHed ends now; wait for that to go out. - kio::wait(|waiter| publishes.poll(waiter)).await; - return; - } - Event::Regrant(permit) => match (permit.matches(namespace.as_str()), tracks.is_some()) { - // Each PUBLISH ends on its own gate as well; dropping the listing also - // cancels the skips not yet sent. - (false, true) => { - tracks = None; - entries.unlist(&request.skips, &suffix); - } - (true, false) if adding => match broadcast.tracks() { - Ok(listing) => tracks = Some(listing), - Err(err) => { - tracing::debug!(broadcast = %absolute, %err, "listed broadcast ended"); - entries.unlist(&request.skips, &suffix); - kio::wait(|waiter| publishes.poll(waiter)).await; - return; - } - }, - _ => {} - }, - // The PUBLISHes already sent still end when their track is unlisted, so keep - // reading the listing for them, adding nothing. - Event::Cancelled => { - adding = false; - entries.pending.clear(); - entries.staged = None; - } - // Its listing ends once the front serving it sees the route go, after the - // tracks its source finished last, so the PUBLISHes already sent end by it. - // The tracks not yet PUBLISHed or skipped are no longer announced. - Event::Withdrawn => { - withdrawn = true; - adding = false; - entries.halt(&request.skips, &suffix); - } - Event::Opened(Err(err)) => { - tracing::debug!(broadcast = %absolute, %err, "publish stream did not open"); - entries.unlist(&request.skips, &suffix); - kio::wait(|waiter| publishes.poll(waiter)).await; - return; - } - Event::Opened(Ok(stream)) => { - let Some(Staged { - name, - track, - properties, - subscribed, - }) = entries.staged.take() - else { - continue; - }; - let entry = kio::Producer::new(false); - let task = Listed { - stream, - track, - namespace: namespace.clone(), - name: name.clone(), - properties, - listed: entry.consume(), - live: request.live.clone(), - slot: self.subscriptions.hold(), - subscribed, - params: request.params, - }; - entries.listed.insert(name, entry); - let this = self.clone(); - publishes.push(async move { - if let Err(err) = this.run_publish(task).await { - tracing::debug!(%err, "publish stream error"); - } - }); - } - Event::Track(crate::broadcast::TracksEvent::Synced) => {} - Event::Track(crate::broadcast::TracksEvent::Removed { name }) => { - entries.remove(&request.skips, &suffix, name, false) - } - // Its PUBLISH ends with TRACK_ENDED rather than in error. - Event::Track(crate::broadcast::TracksEvent::Finished { name }) => { - entries.remove(&request.skips, &suffix, name, true) - } - Event::Track(crate::broadcast::TracksEvent::Added { .. }) if !adding => {} - Event::Track(crate::broadcast::TracksEvent::Added { name, .. }) if request.forward => { - entries.skip(&request.skips, &suffix, name); - } - Event::Track(crate::broadcast::TracksEvent::Added { name, info }) => { - entries.pending.insert(name, info); - } - } - } - } - - /// Run one paused PUBLISH subscription until it ends: the track is unlisted, the - /// subscriber cancels or refuses it, or, once resumed, the track ends. - async fn run_publish(self, task: Listed) -> Result<(), Error> { - let Listed { - stream, - track, - namespace, - name, - properties, - listed, - live, - slot, - subscribed, - mut params, - } = task; - let _slot = slot; - let _subscribed = subscribed; - // An update may ask for a new group only if the PUBLISH sent DYNAMIC_GROUPS, which - // INCLUDE_PROPERTIES=0 leaves out. - let dynamic = properties.dynamic_groups; - let absolute = self.origin.root().rejoin(&namespace); - - // Every wait on the peer for the PUBLISH's bytes lasts only while the track is listed - // and the request stands. - let session = self.session.clone(); - let mut stream = stream.with_stats(self.stats()); - // Dropped unwritten, the stream is reset before its header, which the peer ignores. - let noop = kio::Waiter::noop(); - if listed.poll_closed(&noop).is_ready() || live.poll_closed(&noop).is_ready() { - return Ok(()); - } - let request_id = self.control.next_request_id(); - - // A joining FETCH needs the subscription forwarding. - let join = (!Filter::is_draft22(self.version)).then(|| self.register_join(request_id)); - self.pause_join(join.as_ref()); - - tracing::debug!(id = %request_id, broadcast = %absolute, track = %name, "publish started"); - let publish = ietf::Publish { - request_id, - track_namespace: namespace.clone(), - track_name: name.as_ref().into(), - track_alias: request_id.0, - largest_location: live_edge(&track).largest, - forward: false, - properties, - // Draft-22 and later state the listing's subscription parameters on each PUBLISH; - // earlier drafts carry them on PUBLISH_OK. - subscriber_priority: Some(params.priority), - group_order: params.order, - filter: Some(params.filter), - }; - let written = { - let mut writing = std::pin::pin!(stream.writer.encode_message(&publish)); - kio::wait(|waiter| { - if listed.poll_closed(waiter).is_ready() || live.poll_closed(waiter).is_ready() { - return Poll::Ready(None); - } - waiter.poll_future(writing.as_mut()).map(Some) - }) - .await - }; - // Cut short, the stream is reset before its header is whole, which the peer ignores. - let Some(written) = written else { - return Ok(()); - }; - written?; - - let mut gate = crate::auth::Gate::new(self.auth.clone(), namespace.clone(), crate::auth::Direction::Publish); - let mut finished = false; - let mut goaways = super::super::request_stream::Messages::new(self.session.clone(), self.control.client()); - - // PUBLISH_OK or REQUEST_ERROR. A track that left the listing unfinished before the - // answer has nothing to end yet, so the request is reset. One that finished waits for - // the answer, then ends with TRACK_ENDED, since the peer listed it on the PUBLISH. - let answer = kio::wait(|waiter| { - if session.poll_closed(waiter).is_ready() || (listed.poll_closed(waiter).is_ready() && !*listed.read()) { - return Poll::Ready(None); - } - goaways.poll_next(&mut stream.reader, waiter).map(Some) - }) - .await; - // A NEW_GROUP_REQUEST on a PUBLISH_OK that leaves the subscription paused waits for - // the resume. - let mut new_group: Option; - // A PUBLISH_OK breaking a rule ends the PUBLISH, as a refused update does. - let mut refused = None; - let resume = match answer { - None => { - stream.writer.abort(&Error::Cancel); - return Ok(()); - } - Some(Err(err)) => return Err(err), - Some(Ok(None)) => return Err(goaways.failed(Error::ProtocolViolation)), - Some(Ok(Some(super::super::request_stream::Framed(id, body)))) => match id { - ietf::RequestError::ID => { - let refused = body - .message::(self.version) - .map_err(|err| goaways.failed(err.into()))?; - tracing::debug!(id = %request_id, track = %name, code = refused.error_code, reason = %refused.reason_phrase, "publish refused"); - return Ok(()); - } - // PUBLISH_OK is a REQUEST_OK from draft-18, which still lists 0x1E for it. - _ if id == ietf::RequestOk::ID || (id == ietf::PublishOk::ID && self.version == Version::Draft18) => { - let ok = body - .message::(self.version) - .map_err(|err| goaways.failed(err.into()))?; - // Through draft-18 the answer sets the Forward State and the parameters it - // carries, an absent FORWARD meaning 1; an absent parameter keeps the one the - // SUBSCRIBE_TRACKS set. Draft-22 moved them off PUBLISH_OK, leaving it paused. - if let Some(priority) = ok.subscriber_priority { - params.priority = priority; - } - params.order = ok.group_order.or(params.order); - params.filter = ok.filter.unwrap_or(params.filter); - // The answer may ask for a new group only if the PUBLISH sent DYNAMIC_GROUPS - // (draft-18 section 10.2.13). The listing's own asked without knowing, as a - // SUBSCRIBE may, so it is ignored where it does not apply. - if ok.new_group.is_some() && !dynamic { - refused = Some(Served::UpdateFailed(Error::Unsupported)); - } - new_group = ok.new_group.or(params.new_group.filter(|_| dynamic)); - (ok.forward && !Filter::is_draft22(self.version)).then(|| Resume { - fill: None, - new_group: new_group.take(), - answer: false, - }) - } - _ => return Err(goaways.failed(Error::UnexpectedMessage)), - }, - }; - - if let Some(done) = refused { - drop(join); - return self.write_publish_done(stream, done, 0, None).await; - } - - // Group streams opened over every time the subscription forwarded, which - // PUBLISH_DONE counts. - let mut opened = 0; - // What to resume with, and whether an update asked for it: a forwarding PUBLISH_OK - // resumes at once, with no update to answer. - let mut resume: Option = resume; - // Each turn answers a REQUEST_UPDATE, so a peer streaming them must not hold the - // session's other tasks. - let mut budget = kio::coop::Budget::new(32); - let done = loop { - kio::wait(|waiter| budget.poll_yield(waiter)).await; - if let Some(how) = resume.take() { - match self - .resume_publish( - &mut stream, - &track, - &namespace, - request_id, - &mut params, - how, - dynamic, - join.as_ref(), - &listed, - &mut gate, - &mut finished, - &mut goaways, - &mut opened, - ) - .await - { - Ok(Served::Paused(_)) => { - self.pause_join(join.as_ref()); - self.write_request_ok(&mut stream.writer, None).await?; - // Unlisted while it forwarded: it ends now that it is idle. - if listed.is_closed() { - break unlisted(&listed); - } - continue; - } - Ok(served) => break served, - // Cancelled by the subscriber, or the session ended. - Err(()) => return Ok(()), - } - } - - let wait = kio::wait(|waiter| { - if session.poll_closed(waiter).is_ready() || stream.writer.poll_closed(waiter).is_ready() { - return Poll::Ready(Wait::Cancelled); - } - if gate.poll_denied(waiter).is_ready() { - return Poll::Ready(Wait::Revoked); - } - if listed.poll_closed(waiter).is_ready() { - return Poll::Ready(Wait::Removed); - } - match ready!(self.poll_update(&mut stream, &mut finished, &mut goaways, waiter)) { - UpdateRead::Update(update) => Poll::Ready(Wait::Update(update)), - UpdateRead::Cancelled => Poll::Ready(Wait::Cancelled), - UpdateRead::Malformed(err) => { - if super::super::subscriber::is_protocol_violation(&err) { - self.session.clone().close( - super::super::error::to_session_code(&crate::SessionError::from(&err)), - &err.to_string(), - ); - } - Poll::Ready(Wait::Failed(err)) - } - UpdateRead::Failed(err) => Poll::Ready(Wait::Failed(err)), - } - }) - .await; - - let update = match wait { - Wait::Update(update) => update, - Wait::Removed => break unlisted(&listed), - Wait::Revoked => { - tracing::info!(broadcast = %absolute, track = %name, "publish no longer authorized"); - break Served::Track(Err(Error::Unauthorized)); - } - Wait::Cancelled => return Ok(()), - Wait::Failed(err) => break Served::Track(Err(err)), - }; - // Refused like an update to any subscription: it fails, ending this one. - let refusal = match update.refused { - Some(Error::Decode(err)) => Some((super::super::token::reason(&err), Error::Decode(err))), - Some(Error::GoingAway) => Some(("going away", Error::GoingAway)), - Some(err) => Some(("REQUEST_UPDATE parameters not supported", err)), - None if update.authorization_token.is_some() - && self.auth.token_decides(crate::auth::Direction::Publish) => - { - Some(( - "AUTHORIZATION TOKEN on a request admitted without one", - Error::Unsupported, - )) - } - None if update.new_group.is_some() && !dynamic => Some(( - "NEW_GROUP_REQUEST on a track without DYNAMIC_GROUPS", - Error::Unsupported, - )), - None if update.fill.is_some() && update.forward != Some(true) => { - Some(("FILL_PARAMETERS without resuming", Error::Unsupported)) - } - // A paused subscription opens no groups, so a new one is asked for only as it - // resumes. - None if update.new_group.is_some() && update.forward != Some(true) => { - Some(("NEW_GROUP_REQUEST without resuming", Error::Unsupported)) - } - None => None, - }; - if let Some((reason, err)) = refusal { - self.write_subscribe_error(&mut stream.writer, &err, reason).await?; - break Served::UpdateFailed(Error::Unsupported); - } - if let Some(priority) = update.priority { - params.priority = priority; - } - if let Some(filter) = update.filter { - params.filter = filter; - } - if let Some(max_age) = update.max_age { - params.max_age = Some(max_age); - } - match update.forward { - Some(true) => { - resume = Some(Resume { - fill: update.fill, - new_group: update.new_group.or(new_group.take()), - answer: true, - }) - } - _ => self.write_request_ok(&mut stream.writer, None).await?, - } - }; - - drop(join); - self.write_publish_done(stream, done, opened, None).await - } - - /// Mark a paused subscription for joining FETCHes: draft-18 section 5.1 refuses one - /// unless the subscription forwards. - pub(super) fn pause_join(&self, join: Option<&Join>>) { - if let Some(join) = join { - join.joins.lock().insert(join.request_id, Some(Joined::Empty)); - } - } - - /// End a PUBLISH whose resume failed with `err` before it joined. A REQUEST_UPDATE that - /// asked for it is owed an answer, so it is refused, which fails the update (draft-22 - /// section 9.5). `Err(())` when the subscriber cancelled. - async fn resume_failed( - &self, - stream: &mut Stream, - answer: bool, - err: Error, - ) -> Result { - if !answer { - return Ok(Served::Track(Err(err))); - } - self.write_subscribe_error(&mut stream.writer, &err, "resume failed") - .await - .map_err(|_| ())?; - Ok(Served::UpdateFailed(err)) - } - - /// Forward a paused PUBLISH subscription from the cache with `params`, as a SUBSCRIBE - /// with them would be served, until it pauses again or ends. Answers the resuming update - /// with where the subscription now joins, when `answer` says one asked. `Err(())` when the - /// subscriber cancelled. - #[expect( - clippy::too_many_arguments, - reason = "private plumbing; a single-use argument struct would only rename the parameters" - )] - async fn resume_publish( - &self, - stream: &mut Stream, - track: &track::Consumer, - namespace: &crate::Path, - request_id: RequestId, - params: &mut Params, - how: Resume, - dynamic: bool, - join: Option<&Join>>, - listed: &kio::Consumer, - gate: &mut crate::auth::Gate, - finished: &mut bool, - goaways: &mut super::super::request_stream::Messages, - opened: &mut u64, - ) -> Result { - let mut subscription = serving_subscription(params.priority); - if let Some(order) = params.order { - subscription = subscription.with_order(order.to_order()); - } - if let Some(max_age) = params.max_age { - subscription.max_delay = Some(Duration::from_millis(max_age)); - } - // A start the live edge does not decide is no join; see `serve_subscribe_stream`. - let fixed = subscribe_range(params.filter, LiveEdge::default(), self.version); - if let Some(start) = fixed.start { - ServeRange { - start: Some(Location { object: 0, ..start }), - ..fixed - } - .apply(&mut subscription); - } - // Setting up lasts only while the track is listed and the subscriber stays: a cold - // route may take a while to say where its live edge is. - let mut subscribing = std::pin::pin!(track.subscribe(subscription.clone())); - let subscribed = kio::wait(|waiter| { - if stream.writer.poll_closed(waiter).is_ready() { - return Poll::Ready(Err(())); - } - if listed.poll_closed(waiter).is_ready() { - return Poll::Ready(Ok(None)); - } - waiter - .poll_future(subscribing.as_mut()) - .map(|subscribed| Ok(Some(subscribed))) - }) - .await?; - let mut subscriber = match subscribed { - Some(Ok(subscriber)) => subscriber, - Some(Err(err)) => return self.resume_failed(stream, how.answer, err).await, - None => { - return self.resume_failed(stream, how.answer, Error::NotFound).await; - } - }; - let live = kio::wait(|waiter| { - if stream.writer.poll_closed(waiter).is_ready() { - return Poll::Ready(Err(())); - } - if listed.poll_closed(waiter).is_ready() { - return Poll::Ready(Ok(false)); - } - subscriber.poll_live(waiter).map(|_| Ok(true)) - }) - .await?; - if !live { - return self.resume_failed(stream, how.answer, Error::NotFound).await; - } - - let edge = live_edge(track); - let range = subscribe_range(params.filter, edge, self.version); - range.apply(&mut subscription); - let _ = subscriber.update(subscription); - let timescale = subscriber.info().timescale; - let mut serve = TrackServe::new( - self.session.clone(), - subscriber, - request_id, - self.version, - range, - timescale, - ) - .compression(self.peer_setup.get().await.flate) - .ranked(self.priority.clone()); - serve.dynamic = dynamic; - serve.filter = params.filter; - if let Some(value) = how.new_group { - serve.request_new_group(value); - } - - if let Some(join) = join { - let joined = match (params.filter, edge.largest) { - (Filter::NextObject, Some(largest)) => Joined::Group { - end: Location { - group: largest.group, - object: largest.object + 1, - }, - cache: track.clone(), - namespace: namespace.clone(), - grant: None, - ranking: serve.ranking.clone(), - }, - (Filter::NextObject, None) => Joined::Empty, - _ => Joined::Unsupported, - }; - join.joins.lock().insert(join.request_id, Some(joined)); - } - let fill = how - .fill - .filter(|_| Filter::is_draft22(self.version)) - .map(|fill| fill_range(fill, params.filter, edge.largest)); - - if how.answer && self.write_request_ok(&mut stream.writer, edge.largest).await.is_err() { - return Err(()); - } - - let served = { - let ranking = serve.ranking.clone(); - let streams = serve.opened.clone(); - let fill = async { - if let Some(fill) = fill { - self.run_fill(fill, &ranking, track.clone(), timescale, &streams, request_id) - .await; - } - }; - let serving = self.run_subscription(stream, &mut serve, finished, goaways, fill, None); - // A track no longer listed ends its PUBLISH even while the track itself goes on, - // as when its broadcast's route is withdrawn. - crate::auth::until(serving, |waiter| { - if gate.poll_denied(waiter).is_ready() { - return Poll::Ready(Error::Unauthorized.into()); - } - match listed.poll_closed(waiter) { - Poll::Ready(()) => Poll::Ready(Error::Cancel.into()), - Poll::Pending => Poll::Pending, - } - }) - .await - }; - // What an update changed while forwarding outlives a pause. - let applied = serve.track.subscription(); - params.priority = crate::ietf::priority::to_wire(applied.priority); - params.max_age = applied - .max_delay - .and_then(|max_delay| max_delay.as_millis().try_into().ok()); - let served = match served { - Ok(Some(served)) => served, - Ok(None) => { - serve.close_groups(); - *opened += serve.opened(); - return Err(()); - } - // Unlisted while it forwarded. - Err(refusal) if matches!(refusal.error, Error::Cancel) => unlisted(listed), - Err(refusal) => Served::Track(Err(refusal.error)), - }; - if matches!(served, Served::Track(Ok(()))) { - self.write_end_of_track(stream, &mut serve, finished, goaways).await; - } - // Forwarding stops here: PUBLISH_DONE, or the answer to the pause, follows the close - // of every data stream. - serve.close_groups(); - *opened += serve.opened(); - Ok(served) - } -} - -/// How a PUBLISH whose track is no longer listed ends: TRACK_ENDED if it finished, otherwise -/// in error. A track that leaves the listing unfinished may go on elsewhere, as when its -/// broadcast's route is withdrawn, and a clean end would tell the subscriber it is over. -fn unlisted(listed: &kio::Consumer) -> Served { - match *listed.read() { - true => Served::Track(Ok(())), - false => Served::Track(Err(Error::NotFound)), - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::model::ProduceTest as _; - use crate::transport::memory; - - const TIMEOUT: Duration = Duration::from_secs(10); - - const DRAFTS: [Version; 2] = [Version::Draft18, Version::Draft22]; - - /// A publisher whose origin holds broadcast `room/cam` listing `video` and `audio`, and - /// the peer's end of its session. - struct Harness { - version: Version, - publisher: Publisher, - session: memory::Session, - peer: memory::Session, - broadcast: crate::origin::Publication, - tracks: HashMap<&'static str, track::Producer>, - _origin: origin::Producer, - } - - fn harness(version: Version) -> Harness { - harness_with(version, memory::Config::default(), peer::Peer::default()) - } - - fn harness_with(version: Version, config: memory::Config, declared: peer::Peer) -> Harness { - let origin = crate::origin::Config::new(crate::Hop::new(1).unwrap()).produce(); - let broadcast = origin.publish("room/cam", crate::origin::Route::default()).unwrap(); - let tracks = ["video", "audio"] - .into_iter() - .map(|name| (name, broadcast.create_track(name, crate::track::Info::timed()).unwrap())) - .collect(); - let (session, peer) = memory::pair(config); - let setup = peer::PeerSetup::default(); - setup.set(declared); - let publisher = Publisher::new( - crate::time::sim(), - crate::driver::Transport::new(session.clone()), - origin.consume(), - Control::new(false), - None, - setup, - version, - ); - Harness { - version, - publisher, - session, - peer, - broadcast, - tracks, - _origin: origin, - } - } - - fn listing(forward: bool) -> ietf::SubscribeTracks { - ietf::SubscribeTracks { - request_id: RequestId(0), - prefix: crate::Path::from_static("room"), - forward, - subscriber_priority: None, - group_order: None, - filter: None, - properties_wanted: true, - new_group: None, - range_filters: false, - track_filter: false, - authorization_token: None, - } - } - - impl Harness { - /// Send `msg` on a new request stream, dispatched as the session would. - async fn request<'m, M: Message<'m>>(&mut self, msg: &M) -> Stream { - let mut stream = Stream::open(&mut crate::driver::Transport::new(self.peer.clone()), self.version) - .await - .unwrap(); - stream.writer.varint(M::ID).await.unwrap(); - stream.writer.encode(msg).await.unwrap(); - - let mut accepted = timeout(Stream::accept( - &mut crate::driver::Transport::new(self.session.clone()), - self.version, - )) - .await - .unwrap(); - let id = accepted.reader.varint().await.unwrap(); - let body: ietf::Body = accepted.reader.decode().await.unwrap(); - let task = self.publisher.handle_stream(id, body, accepted, false).unwrap(); - moq_clock::sim::spawn(task); - stream - } - - /// The next PUBLISH the publisher opens: its stream, namespace, track and FORWARD. - async fn publish(&mut self) -> (Stream, String, String, bool) { - let mut stream = timeout(Stream::accept( - &mut crate::driver::Transport::new(self.peer.clone()), - self.version, - )) - .await - .unwrap(); - assert_eq!(stream.reader.varint().await.unwrap(), ietf::Publish::ID); - let body: ietf::Body = stream.reader.decode().await.unwrap(); - let publish: ietf::Publish = body.message(self.version).unwrap(); - let namespace = publish.track_namespace.as_str().to_string(); - let name = publish.track_name.to_string(); - (stream, namespace, name, publish.forward) - } - } - - async fn timeout(future: F) -> F::Output { - moq_clock::timeout(TIMEOUT, future).await.expect("timed out") - } - - /// The next message on a request stream: its type and body. - async fn next(stream: &mut Stream) -> (u64, ietf::Body) { - let id = timeout(stream.reader.varint()).await.unwrap(); - let body = stream.reader.decode().await.unwrap(); - (id, body) - } - - async fn send<'m, M: Message<'m>>(stream: &mut Stream, msg: &M) { - stream.writer.varint(M::ID).await.unwrap(); - stream.writer.encode(msg).await.unwrap(); - } - - fn request_error(body: &ietf::Body, version: Version) -> u64 { - body.message::(version).unwrap().error_code - } - - /// Answer a PUBLISH with a PUBLISH_OK leaving it paused, which draft-22 on cannot - /// change. From draft-18 it is sent as REQUEST_OK. - async fn publish_ok(stream: &mut Stream) { - stream.writer.varint(ietf::RequestOk::ID).await.unwrap(); - stream - .writer - .encode(&ietf::PublishOk { - forward: false, - subscriber_priority: Some(128), - group_order: Some(GroupOrder::Ascending), - filter: Some(Filter::NextObject), - new_group: None, - }) - .await - .unwrap(); - } - - fn update(request_id: u64, forward: bool) -> ietf::SubscribeUpdate { - ietf::SubscribeUpdate { - request_id: RequestId(request_id), - subscriber_priority: None, - forward: Some(forward), - // Join at the live edge: draft-22 moved the filter off PUBLISH_OK. - filter: forward.then_some(Filter::NextObject), - authorization_token: None, - new_group: None, - max_age: None, - fill: None, - } - } - - /// A paused listing PUBLISHes every track under the prefix, later ones included, each - /// paused, and ends a track's PUBLISH with PUBLISH_DONE once it is no longer listed. - #[moq_clock::test] - async fn a_paused_listing_publishes_every_track() { - for version in DRAFTS { - let mut h = harness(version); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - - let mut published = HashMap::new(); - for _ in 0..2 { - let (stream, namespace, name, forward) = h.publish().await; - assert_eq!(namespace, "room/cam", "{version}"); - assert!(!forward, "{version}: {name} forwards"); - published.insert(name, stream); - } - assert!( - published.contains_key("video") && published.contains_key("audio"), - "{version}" - ); - - let _late = h - .broadcast - .create_track("captions", crate::track::Info::timed()) - .unwrap(); - let (_stream, _, name, _) = h.publish().await; - assert_eq!(name, "captions", "{version}"); - - let mic = h._origin.publish("room/mic", crate::origin::Route::default()).unwrap(); - let _audio = mic.create_track("audio", crate::track::Info::timed()).unwrap(); - let (_stream, namespace, name, _) = h.publish().await; - assert_eq!((namespace.as_str(), name.as_str()), ("room/mic", "audio"), "{version}"); - - let mut audio = published.remove("audio").unwrap(); - publish_ok(&mut audio).await; - drop(h.tracks.remove("audio")); - let (id, body) = next(&mut audio).await; - assert_eq!(id, ietf::PublishDone::ID, "{version}"); - let done: ietf::PublishDone = body.message(version).unwrap(); - assert_eq!(done.stream_count, 0, "{version}"); - } - } - - /// A track that finishes while its PUBLISH awaits the answer still ends with TRACK_ENDED - /// once answered, since the peer listed it on the PUBLISH; a reset would fail its listing. - #[moq_clock::test] - async fn a_track_finished_before_the_answer_ends_cleanly() { - for version in DRAFTS { - let mut h = harness(version); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let mut published = HashMap::new(); - for _ in 0..2 { - let (stream, _, name, _) = h.publish().await; - published.insert(name, stream); - } - - let mut video = published.remove("video").unwrap(); - let track = h.tracks.remove("video").unwrap(); - track.finish().unwrap(); - drop(track); - moq_clock::sleep(Duration::from_millis(10)).await; - publish_ok(&mut video).await; - let (id, body) = next(&mut video).await; - assert_eq!(id, ietf::PublishDone::ID, "{version}"); - let done: ietf::PublishDone = body.message(version).unwrap(); - assert_eq!( - done.status_code, - ietf::PublishDoneStatus::TrackEnded.code(), - "{version}" - ); - } - } - - /// A broadcast closing right after a track finished ends that track's PUBLISH with - /// TRACK_ENDED, whether its producer is still held or not, while a track left unfinished - /// still ends in error. - #[moq_clock::test] - async fn a_clean_end_keeps_its_finishes() { - for version in DRAFTS { - for held in [false, true] { - let case = format!("{version}: held={held}"); - let mut h = harness(version); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{case}"); - let mut published = HashMap::new(); - for _ in 0..2 { - let (mut stream, _, name, _) = h.publish().await; - publish_ok(&mut stream).await; - published.insert(name, stream); - } - moq_clock::sleep(Duration::from_millis(10)).await; - - let video = h.tracks.remove("video").unwrap(); - video.finish().unwrap(); - let video = held.then_some(video); - h.broadcast.close(); - for (name, status) in [ - ("video", ietf::PublishDoneStatus::TrackEnded), - ("audio", ietf::PublishDoneStatus::InternalError), - ] { - let (id, body) = next(published.get_mut(name).unwrap()).await; - assert_eq!(id, ietf::PublishDone::ID, "{case}: {name}"); - let done: ietf::PublishDone = body.message(version).unwrap(); - assert_eq!(done.status_code, status.code(), "{case}: {name}"); - } - drop(video); - } - } - } - - /// A REQUEST_UPDATE with FORWARD=1 resumes a paused PUBLISH: its answer names where the - /// subscription joins, and new groups flow. FORWARD=0 pauses it again. - #[moq_clock::test] - async fn an_update_resumes_and_pauses_a_publish() { - for version in DRAFTS { - let mut h = harness(version); - let video = h.tracks.remove("video").unwrap(); - let mut group = video.append_group().unwrap(); - group.write_frame(crate::Timestamp::ZERO, b"cached".as_slice()).unwrap(); - group.finish().unwrap(); - - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let mut stream = loop { - let (stream, _, name, _) = h.publish().await; - if name == "video" { - break stream; - } - }; - publish_ok(&mut stream).await; - send(&mut stream, &update(2, true)).await; - let (id, body) = next(&mut stream).await; - assert_eq!(id, ietf::RequestOk::ID, "{version}"); - let ok: ietf::RequestOk = body.message(version).unwrap(); - assert_eq!(ok.largest, Some(Location { group: 0, object: 0 }), "{version}"); - - let mut group = video.append_group().unwrap(); - group.write_frame(crate::Timestamp::ZERO, b"live".as_slice()).unwrap(); - group.finish().unwrap(); - // The joined group's remainder, then the live group. - let mut opened = 0; - loop { - let recv = timeout(kio::wait(|waiter| h.peer.poll_accept_uni(waiter))) - .await - .unwrap(); - opened += 1; - let mut reader = crate::coding::Reader::new(crate::transport::boxed::RecvStream::new(recv), version); - let mut bytes = Vec::new(); - while let Some(chunk) = timeout(kio::wait(|waiter| reader.poll_read_chunk(waiter, 1024))) - .await - .unwrap() - { - bytes.extend_from_slice(&chunk); - } - assert!(!bytes.windows(6).any(|w| w == b"cached"), "{version}: resent the join"); - if bytes.windows(4).any(|w| w == b"live") { - break; - } - } - - send(&mut stream, &update(4, false)).await; - assert_eq!(next(&mut stream).await.0, ietf::RequestOk::ID, "{version}"); - let mut group = video.append_group().unwrap(); - group.write_frame(crate::Timestamp::ZERO, b"paused".as_slice()).unwrap(); - group.finish().unwrap(); - moq_clock::sleep(Duration::from_millis(10)).await; - let more = futures::poll!(kio::wait(|waiter| h.peer.poll_accept_uni(waiter))); - assert!(more.is_pending(), "{version}: a paused PUBLISH forwarded"); - - // Ending the track while paused ends the PUBLISH, counting the streams it opened. - drop(video); - let (id, body) = next(&mut stream).await; - assert_eq!(id, ietf::PublishDone::ID, "{version}"); - let done: ietf::PublishDone = body.message(version).unwrap(); - assert_eq!(done.stream_count, opened, "{version}"); - } - } - - /// A listed compressed track is declared and served compressed only to a peer that - /// negotiated FLATE; any other peer gets the plain copy with no declaration. - #[moq_clock::test] - async fn a_publish_compresses_only_when_negotiated() { - for version in DRAFTS { - for flate in [false, true] { - let declared = peer::Peer { - flate, - ..Default::default() - }; - let mut h = harness_with(version, memory::Config::default(), declared); - let data = h - .broadcast - .create_track("data", crate::track::Info::timed().with_compress(true)) - .unwrap(); - - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let mut stream = loop { - let mut stream = timeout(Stream::accept( - &mut crate::driver::Transport::new(h.peer.clone()), - version, - )) - .await - .unwrap(); - assert_eq!(stream.reader.varint().await.unwrap(), ietf::Publish::ID); - let body: ietf::Body = stream.reader.decode().await.unwrap(); - let publish: ietf::Publish = body.message(version).unwrap(); - if &*publish.track_name == "data" { - assert_eq!(publish.properties.compress, flate, "{version} flate={flate}"); - break stream; - } - }; - publish_ok(&mut stream).await; - send(&mut stream, &update(2, true)).await; - assert_eq!(next(&mut stream).await.0, ietf::RequestOk::ID, "{version}"); - - let payload = vec![b'a'; 1024]; - let mut group = data.append_group().unwrap(); - group.write_frame(crate::Timestamp::ZERO, payload.clone()).unwrap(); - group.finish().unwrap(); - let recv = timeout(kio::wait(|waiter| h.peer.poll_accept_uni(waiter))) - .await - .unwrap(); - let mut reader = crate::coding::Reader::new(crate::transport::boxed::RecvStream::new(recv), version); - let mut bytes = Vec::new(); - while let Some(chunk) = timeout(kio::wait(|waiter| reader.poll_read_chunk(waiter, 1024))) - .await - .unwrap() - { - bytes.extend_from_slice(&chunk); - } - let plain = bytes.windows(payload.len()).any(|w| w == payload); - assert_eq!(plain, !flate, "{version} flate={flate}: {} bytes", bytes.len()); - } - } - } - - /// FORWARD=1 asks for every track's objects, which we never push unasked: from - /// draft-22 each track is skipped, and draft-18, with no way to skip, refuses it. - #[moq_clock::test] - async fn a_forwarding_listing_skips_every_track() { - for version in DRAFTS { - let mut h = harness(version); - let mut request = h.request(&listing(true)).await; - let (id, body) = next(&mut request).await; - if version == Version::Draft18 { - assert_eq!(id, ietf::RequestError::ID); - assert_eq!(request_error(&body, version), request::NOT_SUPPORTED); - continue; - } - assert_eq!(id, ietf::RequestOk::ID, "{version}"); - let mut skipped = Vec::new(); - for _ in 0..2 { - let (id, body) = next(&mut request).await; - assert_eq!(id, ietf::PublishSkipped::ID, "{version}"); - let skip: ietf::PublishSkipped = body.message(version).unwrap(); - assert_eq!(skip.suffix.as_str(), "cam", "{version}"); - skipped.push(skip.track_name.to_string()); - } - skipped.sort(); - assert_eq!(skipped, ["audio", "video"], "{version}"); - let opened = futures::poll!(std::pin::pin!(Stream::accept( - &mut crate::driver::Transport::new(h.peer.clone()), - version - ))); - assert!(opened.is_pending(), "{version}: a skipped track was PUBLISHed"); - } - } - - /// A prefix overlapping an established listing is refused, and freed by its cancel. - #[moq_clock::test] - async fn an_overlapping_prefix_is_refused() { - for version in DRAFTS { - let mut h = harness(version); - let mut first = h.request(&listing(false)).await; - assert_eq!(next(&mut first).await.0, ietf::RequestOk::ID, "{version}"); - - let mut nested = listing(false); - nested.request_id = RequestId(2); - nested.prefix = crate::Path::from_static("room/cam"); - let mut second = h.request(&nested).await; - let (id, body) = next(&mut second).await; - assert_eq!(id, ietf::RequestError::ID, "{version}"); - assert_eq!(request_error(&body, version), request::PREFIX_OVERLAP, "{version}"); - - first.writer.abort(&Error::Cancel); - moq_clock::sleep(Duration::from_millis(10)).await; - nested.request_id = RequestId(4); - let mut third = h.request(&nested).await; - assert_eq!(next(&mut third).await.0, ietf::RequestOk::ID, "{version}"); - } - } - - /// Draft-18 allows one subscription per track: a SUBSCRIBE to a PUBLISHed track is a - /// duplicate. Later drafts serve it. - #[moq_clock::test] - async fn a_subscribe_to_a_published_track() { - for version in DRAFTS { - let mut h = harness(version); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let _publishes = [h.publish().await, h.publish().await]; - - let subscribe = ietf::Subscribe { - request_id: RequestId(2), - track_namespace: crate::Path::from_static("room/cam"), - track_name: "video".into(), - subscriber_priority: 128, - group_order: GroupOrder::Descending, - filter: Filter::NextObject, - fill: None, - properties_wanted: true, - forward: true, - rendezvous: false, - range_filters: false, - authorization_token: None, - epoch: None, - max_age: None, - new_group: None, - }; - let mut stream = h.request(&subscribe).await; - let (id, body) = next(&mut stream).await; - match version { - Version::Draft18 => { - assert_eq!(id, ietf::RequestError::ID); - assert_eq!(request_error(&body, version), request::DUPLICATE_SUBSCRIPTION); - } - _ => assert_eq!(id, ietf::SubscribeOk::ID, "{version}"), - } - } - } - - /// Listed broadcast sizes to sweep: what a listing holds must not grow with them. - const CATALOGS: [usize; 3] = [4, 64, 512]; - - /// Each PUBLISH opens only as the peer's stream credit admits it: a peer that withholds - /// credit and reads nothing holds as many PUBLISHes as it grants, however many tracks are - /// listed, and each track is PUBLISHed once credit returns. - #[moq_clock::test] - async fn a_listing_publishes_only_as_credit_admits() { - const CREDIT: usize = 2; - for version in DRAFTS { - for catalog in CATALOGS { - let config = memory::Config { - max_concurrent_bi_streams: Some(CREDIT), - ..Default::default() - }; - let mut h = harness_with(version, config, peer::Peer::default()); - let _extra: Vec<_> = (h.tracks.len()..catalog) - .map(|i| { - h.broadcast - .create_track(format!("t{i}"), crate::track::Info::timed()) - .unwrap() - }) - .collect(); - let held = |h: &Harness| usize::MAX - h.publisher.subscriptions.available(); - - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - moq_clock::sleep(Duration::from_millis(10)).await; - assert_eq!(held(&h), CREDIT, "{version} catalog={catalog}"); - - // Refusing each PUBLISH returns its credit, which opens the next. - let mut published = HashSet::new(); - while published.len() < catalog { - let (stream, _, name, _) = h.publish().await; - assert!(published.insert(name), "{version}: PUBLISHed a track twice"); - drop(stream); - assert!(held(&h) <= CREDIT, "{version} catalog={catalog}: {} held", held(&h)); - } - } - } - } - - /// A FORWARD=1 listing reads its next track only once the request stream has taken the - /// last skip, so a peer that stops reading leaves one skip queued, however many tracks - /// are listed. - #[moq_clock::test] - async fn a_forwarding_listing_reads_only_as_skips_go_out() { - for version in [Version::Draft18, Version::Draft22] { - for catalog in CATALOGS { - let h = harness(version); - let _extra: Vec<_> = (h.tracks.len()..catalog) - .map(|i| { - h.broadcast - .create_track(format!("t{i}"), crate::track::Info::timed()) - .unwrap() - }) - .collect(); - let skips = Skips::default(); - let live = kio::Producer::new(()); - let route = kio::Producer::new(Route::Replaced); - let request = Request { - origin: h._origin.consume(), - prefix: crate::Path::from_static(""), - forward: true, - params: Params { - priority: crate::ietf::priority::DEFAULT_WIRE, - order: None, - filter: Filter::Unfiltered, - max_age: None, - new_group: None, - }, - properties_wanted: true, - skips: skips.clone(), - live: live.consume(), - }; - moq_clock::sim::spawn(h.publisher.clone().run_listed_broadcast( - request, - crate::Path::from_static("room/cam"), - None, - route.consume(), - )); - - let mut skipped = HashSet::new(); - while skipped.len() < catalog { - moq_clock::sleep(Duration::from_millis(10)).await; - let mut queued = skips.lock(); - assert_eq!(queued.len(), 1, "{version} catalog={catalog}"); - let ((_, name), ()) = queued.pop_front().unwrap(); - assert!(skipped.insert(name), "{version}: skipped a track twice"); - } - moq_clock::sleep(Duration::from_millis(10)).await; - assert!(skips.read().is_empty(), "{version}: skipped past the listing"); - } - } - } - - /// Cancelling the listing stops new PUBLISHes, while those already sent live on until - /// their broadcast ends, which ends each with PUBLISH_DONE. - #[moq_clock::test] - async fn a_cancelled_listing_keeps_its_publishes() { - for version in DRAFTS { - let mut h = harness(version); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let (mut first, ..) = h.publish().await; - let (mut second, ..) = h.publish().await; - publish_ok(&mut first).await; - publish_ok(&mut second).await; - - request.writer.abort(&Error::Cancel); - moq_clock::sleep(Duration::from_millis(10)).await; - let _late = h - .broadcast - .create_track("captions", crate::track::Info::timed()) - .unwrap(); - moq_clock::sleep(Duration::from_millis(10)).await; - let opened = futures::poll!(std::pin::pin!(Stream::accept( - &mut crate::driver::Transport::new(h.peer.clone()), - version - ))); - assert!(opened.is_pending(), "{version}: PUBLISHed after the cancel"); - - drop(h.broadcast); - for stream in [&mut first, &mut second] { - assert_eq!(next(stream).await.0, ietf::PublishDone::ID, "{version}"); - } - } - } - - /// On draft-18 a replacement listing skips the tracks whose PUBLISHes outlive the one it - /// replaced, since each is still the peer's one subscription; later drafts PUBLISH them - /// again. - #[moq_clock::test] - async fn a_relisting_skips_tracks_still_published_on_draft_18() { - let version = Version::Draft18; - let mut h = harness(version); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let (mut first, ..) = h.publish().await; - let (mut second, ..) = h.publish().await; - publish_ok(&mut first).await; - publish_ok(&mut second).await; - request.writer.abort(&Error::Cancel); - moq_clock::sleep(Duration::from_millis(10)).await; - - let relisting = ietf::SubscribeTracks { - request_id: RequestId(2), - ..listing(false) - }; - let mut relisting = h.request(&relisting).await; - assert_eq!(next(&mut relisting).await.0, ietf::RequestOk::ID, "{version}"); - if version == Version::Draft18 { - for _ in 0..2 { - assert_eq!(next(&mut relisting).await.0, ietf::PublishSkipped::ID); - } - let opened = futures::poll!(std::pin::pin!(Stream::accept( - &mut crate::driver::Transport::new(h.peer.clone()), - version - ))); - assert!(opened.is_pending(), "PUBLISHed a track twice"); - } else { - let _publishes = [h.publish().await, h.publish().await]; - } - } - - /// On draft 18 a PUBLISH_OK may ask for a new group only if the PUBLISH sent - /// DYNAMIC_GROUPS; one that asks anyway ends the PUBLISH in error. - #[moq_clock::test] - async fn a_publish_ok_new_group_needs_dynamic_groups() { - let version = Version::Draft18; - let mut h = harness(version); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let (mut stream, ..) = h.publish().await; - stream.writer.varint(ietf::RequestOk::ID).await.unwrap(); - stream - .writer - .encode(&ietf::PublishOk { - forward: true, - subscriber_priority: None, - group_order: None, - filter: Some(Filter::NextObject), - new_group: Some(0), - }) - .await - .unwrap(); - let (id, body) = next(&mut stream).await; - assert_eq!(id, ietf::PublishDone::ID, "{version}"); - let done: ietf::PublishDone = body.message(version).unwrap(); - assert!(done.end(version).is_err(), "{version}: ended cleanly"); - } - - /// Draft-18 lets PUBLISH_OK forward at once, with no update to answer. - #[moq_clock::test] - async fn a_forwarding_publish_ok_resumes() { - for version in [Version::Draft18] { - let mut h = harness(version); - let video = h.tracks.remove("video").unwrap(); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let mut stream = loop { - let (stream, _, name, _) = h.publish().await; - if name == "video" { - break stream; - } - }; - stream.writer.varint(ietf::RequestOk::ID).await.unwrap(); - stream - .writer - .encode(&ietf::PublishOk { - forward: true, - subscriber_priority: Some(128), - group_order: Some(GroupOrder::Ascending), - filter: Some(Filter::NextObject), - new_group: None, - }) - .await - .unwrap(); - - let mut group = video.append_group().unwrap(); - group.write_frame(crate::Timestamp::ZERO, b"live".as_slice()).unwrap(); - group.finish().unwrap(); - timeout(kio::wait(|waiter| h.peer.poll_accept_uni(waiter))) - .await - .unwrap(); - drop(video); - let (id, body) = next(&mut stream).await; - assert_eq!(id, ietf::PublishDone::ID, "{version}: answered no update"); - let done: ietf::PublishDone = body.message(version).unwrap(); - assert_eq!(done.stream_count, 1, "{version}"); - } - } - - /// A REQUEST_UPDATE on the listing admits its Request ID like any other: one of the - /// wrong parity closes the session rather than only refusing the update. - #[moq_clock::test] - async fn a_listing_update_admits_its_request_id() { - for version in DRAFTS { - let mut h = harness(version); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - // An odd Request ID is ours to allocate, never the client peer's. - let bare = ietf::SubscribeUpdate { - forward: None, - filter: None, - ..update(1, false) - }; - send(&mut request, &bare).await; - let closed = timeout(kio::wait(|waiter| h.peer.poll_closed(waiter))).await; - assert!( - matches!(&closed, memory::Error::Session { reason, .. } if reason == "invalid request ID"), - "{version}: {closed:?}" - ); - } - } - - /// A REQUEST_UPDATE on the listing is answered with REQUEST_ERROR. From draft-22 it may - /// carry FORWARD, which is refused the same way rather than closing the session. - #[moq_clock::test] - async fn a_listing_update_is_refused() { - for version in DRAFTS { - let mut h = harness(version); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let refused = ietf::SubscribeUpdate { - filter: None, - forward: Filter::is_draft22(version).then_some(false), - ..update(2, false) - }; - send(&mut request, &refused).await; - let (id, body) = next(&mut request).await; - assert_eq!(id, ietf::RequestError::ID, "{version}"); - assert_eq!(request_error(&body, version), request::NOT_SUPPORTED, "{version}"); - } - } - - /// A listing update past our GOAWAY is refused as GOING_AWAY, not as unsupported. - #[moq_clock::test] - async fn a_listing_update_after_goaway_is_going_away() { - for version in DRAFTS { - let mut h = harness(version); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - h.publisher.control.goaway().unwrap(); - let refused = ietf::SubscribeUpdate { - filter: None, - forward: None, - ..update(2, false) - }; - send(&mut request, &refused).await; - let (id, body) = next(&mut request).await; - assert_eq!(id, ietf::RequestError::ID, "{version}"); - let going_away = request::to_code(&Error::GoingAway, request::Kind::SubscribeNamespace, version); - assert_eq!(request_error(&body, version), going_away, "{version}"); - } - } - - /// NEW_GROUP_REQUEST resuming a paused PUBLISH is a join under the track's refresh - /// ratio, as on a SUBSCRIBE, even when the filter's fixed start is not. - #[moq_clock::test] - async fn a_resume_asks_for_a_new_group() { - let version = Version::Draft22; - let mut h = harness(version); - let live = h - .broadcast - .create_track("live", crate::track::Info::timed().with_refresh(Some(1))) - .unwrap(); - let mut group = live.create_group(group::Info { sequence: 4 }).unwrap(); - group.write_frame(crate::Timestamp::ZERO, b"key".as_slice()).unwrap(); - group.write_frame(crate::Timestamp::ZERO, b"delta".as_slice()).unwrap(); - - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID); - let mut stream = loop { - let (stream, _, name, _) = h.publish().await; - if name == "live" { - break stream; - } - }; - publish_ok(&mut stream).await; - send( - &mut stream, - &ietf::SubscribeUpdate { - filter: Some(Filter::Absolute { - start: Location { group: 4, object: 0 }, - end: None, - }), - new_group: Some(0), - ..update(2, true) - }, - ) - .await; - assert_eq!(next(&mut stream).await.0, ietf::RequestOk::ID); - assert_eq!(live.demand().wanted(), Some(5), "the join never reached the track"); - } - - /// On draft-22 an update may ask for a new group only if its PUBLISH sent - /// DYNAMIC_GROUPS, which INCLUDE_PROPERTIES=0 leaves out even for a dynamic track. - #[moq_clock::test] - async fn a_new_group_needs_the_advertised_dynamic_groups() { - for version in [Version::Draft22] { - let mut h = harness(version); - let live = h - .broadcast - .create_track("live", crate::track::Info::timed().with_refresh(Some(1))) - .unwrap(); - let request = ietf::SubscribeTracks { - properties_wanted: false, - ..listing(false) - }; - let mut request = h.request(&request).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let mut stream = loop { - let (stream, _, name, _) = h.publish().await; - if name == "live" { - break stream; - } - }; - publish_ok(&mut stream).await; - send( - &mut stream, - &ietf::SubscribeUpdate { - new_group: Some(0), - ..update(2, true) - }, - ) - .await; - let (id, body) = next(&mut stream).await; - assert_eq!(id, ietf::RequestError::ID, "{version}"); - assert_eq!(request_error(&body, version), request::NOT_SUPPORTED, "{version}"); - assert_eq!( - live.demand().wanted(), - None, - "{version}: the refused join reached the track" - ); - } - } - - /// While forwarding, an update's NEW_GROUP_REQUEST also needs the advertised - /// DYNAMIC_GROUPS, which INCLUDE_PROPERTIES=0 leaves out. - #[moq_clock::test] - async fn a_forwarding_new_group_needs_the_advertised_dynamic_groups() { - let version = Version::Draft22; - let mut h = harness(version); - let _live = h - .broadcast - .create_track("live", crate::track::Info::timed().with_refresh(Some(1))) - .unwrap(); - let request = ietf::SubscribeTracks { - properties_wanted: false, - ..listing(false) - }; - let mut request = h.request(&request).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID); - let mut stream = loop { - let (stream, _, name, _) = h.publish().await; - if name == "live" { - break stream; - } - }; - publish_ok(&mut stream).await; - send(&mut stream, &update(2, true)).await; - assert_eq!(next(&mut stream).await.0, ietf::RequestOk::ID); - let ask = ietf::SubscribeUpdate { - forward: None, - filter: None, - new_group: Some(0), - ..update(4, true) - }; - send(&mut stream, &ask).await; - let (id, body) = next(&mut stream).await; - assert_eq!(id, ietf::RequestError::ID); - assert_eq!(request_error(&body, version), request::NOT_SUPPORTED); - } - - /// A paused subscription opens no groups, so an update that pauses may not ask for one, - /// while forwarding as while paused. - #[moq_clock::test] - async fn a_pausing_new_group_is_refused() { - for version in [Version::Draft18, Version::Draft22] { - let mut h = harness(version); - let _live = h - .broadcast - .create_track("live", crate::track::Info::timed().with_refresh(Some(1))) - .unwrap(); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let mut stream = loop { - let (stream, _, name, _) = h.publish().await; - if name == "live" { - break stream; - } - }; - publish_ok(&mut stream).await; - send(&mut stream, &update(2, true)).await; - assert_eq!(next(&mut stream).await.0, ietf::RequestOk::ID, "{version}"); - let ask = ietf::SubscribeUpdate { - new_group: Some(0), - ..update(4, false) - }; - send(&mut stream, &ask).await; - let (id, body) = next(&mut stream).await; - assert_eq!(id, ietf::RequestError::ID, "{version}"); - assert_eq!(request_error(&body, version), request::NOT_SUPPORTED, "{version}"); - } - } - - /// An update taken while forwarding outlives a pause: the next resume keeps it. - #[moq_clock::test] - async fn a_forwarding_update_outlives_a_pause() { - for version in [Version::Draft18, Version::Draft22] { - let mut h = harness(version); - let video = h.tracks.remove("video").unwrap(); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let mut stream = loop { - let (stream, _, name, _) = h.publish().await; - if name == "video" { - break stream; - } - }; - publish_ok(&mut stream).await; - send(&mut stream, &update(2, true)).await; - assert_eq!(next(&mut stream).await.0, ietf::RequestOk::ID, "{version}"); - let reprice = ietf::SubscribeUpdate { - subscriber_priority: Some(7), - filter: None, - ..update(4, false) - }; - send(&mut stream, &reprice).await; - assert_eq!(next(&mut stream).await.0, ietf::RequestOk::ID, "{version}"); - send(&mut stream, &update(6, true)).await; - assert_eq!(next(&mut stream).await.0, ietf::RequestOk::ID, "{version}"); - let priority = video.subscription().map(|subscription| subscription.priority); - assert_eq!(priority, Some(crate::ietf::priority::from_wire(7)), "{version}"); - } - } - - /// On draft-18 a track the peer already SUBSCRIBEd to is skipped rather than PUBLISHed - /// a second time; later drafts PUBLISH it. - #[moq_clock::test] - async fn a_subscribed_track_is_skipped_on_draft_18() { - let version = Version::Draft18; - let mut h = harness(version); - let subscribe = ietf::Subscribe { - request_id: RequestId(0), - track_namespace: crate::Path::from_static("room/cam"), - track_name: "video".into(), - subscriber_priority: 128, - group_order: GroupOrder::Descending, - filter: Filter::NextObject, - fill: None, - properties_wanted: true, - forward: true, - rendezvous: false, - range_filters: false, - authorization_token: None, - epoch: None, - max_age: None, - new_group: None, - }; - let mut subscription = h.request(&subscribe).await; - assert_eq!(next(&mut subscription).await.0, ietf::SubscribeOk::ID, "{version}"); - - let request = ietf::SubscribeTracks { - request_id: RequestId(2), - ..listing(false) - }; - let mut request = h.request(&request).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let (_audio, _, name, _) = h.publish().await; - if version == Version::Draft18 { - assert_eq!(name, "audio"); - let (id, body) = next(&mut request).await; - assert_eq!(id, ietf::PublishSkipped::ID); - let skip: ietf::PublishSkipped = body.message(version).unwrap(); - assert_eq!(skip.track_name, "video"); - } else { - let (_, _, other, _) = h.publish().await; - assert!([name, other].contains(&"video".to_string()), "{version}"); - } - } - - /// From draft-22 each PUBLISH states the listing's subscription parameters, which the - /// subscriber otherwise takes as the defaults; earlier drafts carry them on PUBLISH_OK. - /// Draft-22 sends none of them on SUBSCRIBE_TRACKS. - #[moq_clock::test] - async fn a_listing_states_its_parameters_on_each_publish() { - for version in DRAFTS.into_iter().filter(|version| *version != Version::Draft22) { - let mut h = harness(version); - let request = ietf::SubscribeTracks { - subscriber_priority: Some(7), - group_order: Some(GroupOrder::Ascending), - filter: Some(Filter::NextObject), - ..listing(false) - }; - let mut request = h.request(&request).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let mut stream = timeout(Stream::accept( - &mut crate::driver::Transport::new(h.peer.clone()), - version, - )) - .await - .unwrap(); - assert_eq!(stream.reader.varint().await.unwrap(), ietf::Publish::ID); - let body: ietf::Body = stream.reader.decode().await.unwrap(); - let publish: ietf::Publish = body.message(version).unwrap(); - let stated = Filter::is_draft22(version); - assert_eq!(publish.subscriber_priority, stated.then_some(7), "{version}"); - assert_eq!( - publish.group_order, - stated.then_some(GroupOrder::Ascending), - "{version}" - ); - assert_eq!(publish.filter, stated.then_some(Filter::NextObject), "{version}"); - } - } - - /// An omitted SUBSCRIBER_PRIORITY is the drafts' 128, so each PUBLISH restates 128 rather - /// than this stack's unset 127. - #[moq_clock::test] - async fn a_listing_without_a_priority_states_the_default() { - for version in DRAFTS.into_iter().filter(|version| Filter::is_draft22(*version)) { - let mut h = harness(version); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let mut stream = timeout(Stream::accept(&mut h.peer, version)).await.unwrap(); - assert_eq!(stream.reader.varint().await.unwrap(), ietf::Publish::ID); - let body: ietf::Body = stream.reader.decode().await.unwrap(); - let publish: ietf::Publish = body.message(version).unwrap(); - assert_eq!(publish.subscriber_priority, Some(128), "{version}"); - } - } - - /// A forwarding PUBLISH ends with PUBLISH_DONE once its track is no longer listed, here - /// because its broadcast's route went, even while the track itself goes on. The track - /// has not ended, so neither does the PUBLISH cleanly. - #[moq_clock::test] - async fn an_unlisted_forwarding_publish_ends() { - for version in [Version::Draft18, Version::Draft22] { - let mut h = harness(version); - let video = h.tracks.remove("video").unwrap(); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let mut stream = loop { - let (stream, _, name, _) = h.publish().await; - if name == "video" { - break stream; - } - }; - publish_ok(&mut stream).await; - send(&mut stream, &update(2, true)).await; - assert_eq!(next(&mut stream).await.0, ietf::RequestOk::ID, "{version}"); - - drop(h.broadcast); - let mut group = video.append_group().unwrap(); - group.write_frame(crate::Timestamp::ZERO, b"after".as_slice()).unwrap(); - group.finish().unwrap(); - let (id, body) = next(&mut stream).await; - assert_eq!(id, ietf::PublishDone::ID, "{version}"); - let done: ietf::PublishDone = body.message(version).unwrap(); - assert!(done.end(version).is_err(), "{version}: ended cleanly"); - } - } - - /// A paused PUBLISH ends in error once its track is unlisted, here because its - /// broadcast's route went, never with the clean end that says the track is over. - #[moq_clock::test] - async fn an_unlisted_paused_publish_ends_in_error() { - for version in DRAFTS { - let mut h = harness(version); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let (mut stream, ..) = h.publish().await; - publish_ok(&mut stream).await; - - drop(h.broadcast); - let (id, body) = next(&mut stream).await; - assert_eq!(id, ietf::PublishDone::ID, "{version}"); - let done: ietf::PublishDone = body.message(version).unwrap(); - assert!(done.end(version).is_err(), "{version}: ended cleanly"); - } - } - - /// A listing ending through a refused update still unlists its tracks while the peer - /// withholds the credit its REQUEST_ERROR needs. - #[moq_clock::test] - async fn a_closing_listing_still_unlists() { - for version in [Version::Draft18, Version::Draft22] { - let config = memory::Config { - stream_buffer: 32, - ..Default::default() - }; - let mut h = harness_with(version, config, peer::Peer::default()); - // The REQUEST_OK stays unread, leaving no room for the REQUEST_ERROR. - let mut request = h.request(&listing(false)).await; - let (mut stream, ..) = h.publish().await; - publish_ok(&mut stream).await; - let refused = ietf::SubscribeUpdate { - filter: None, - forward: None, - ..update(2, false) - }; - send(&mut request, &refused).await; - moq_clock::sleep(Duration::from_millis(10)).await; - - // A new instance takes the path while the old one goes on: only its route ends. - let _restarted = h._origin.publish("room/cam", crate::origin::Route::default()).unwrap(); - let (id, body) = next(&mut stream).await; - assert_eq!(id, ietf::PublishDone::ID, "{version}"); - let done: ietf::PublishDone = body.message(version).unwrap(); - assert!(done.end(version).is_err(), "{version}: ended cleanly"); - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - assert_eq!(next(&mut request).await.0, ietf::RequestError::ID, "{version}"); - } - } - - /// With MoQ Auth, no track is listed until the peer answers our setup credential, whose - /// grant may narrow what the listing shows, and a listing cancelled while waiting frees - /// its prefix. - #[moq_clock::test] - async fn a_listing_waits_for_the_setup_answer() { - for version in [Version::Draft18, Version::Draft22] { - let mut h = harness(version); - let auth = crate::auth::Link::new(true); - let _cred = auth.present(bytes::Bytes::from_static(b"cred"), true).unwrap(); - let setup = peer::PeerSetup::default(); - setup.set(peer::Peer { - auth: true, - ..Default::default() - }); - h.publisher = Publisher::new( - crate::time::sim(), - crate::driver::Transport::new(h.session.clone()), - h._origin.consume(), - Control::new(false), - None, - setup, - version, - ) - .with_auth(auth.clone()); - - let first = h.request(&listing(false)).await; - moq_clock::sleep(Duration::from_millis(10)).await; - let opened = futures::poll!(std::pin::pin!(Stream::accept( - &mut crate::driver::Transport::new(h.peer.clone()), - version - ))); - assert!(opened.is_pending(), "{version}: listed a track before the setup answer"); - - first.writer.abort(&Error::Cancel); - moq_clock::sleep(Duration::from_millis(10)).await; - let mut again = listing(false); - again.request_id = RequestId(2); - let mut request = h.request(&again).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - - auth.granted(0, crate::auth::Grant::all()); - let (mut stream, ..) = h.publish().await; - publish_ok(&mut stream).await; - } - } - - /// Tracks under a namespace our grant does not cover are not listed until it does, and - /// a grant that stops covering them ends their PUBLISHes. - #[moq_clock::test] - async fn a_listing_follows_the_publish_grant() { - let nothing = || crate::auth::Grant { - publish: crate::Patterns::new(), - subscribe: crate::Patterns::new(), - expires: None, - }; - for version in [Version::Draft18, Version::Draft22] { - let mut h = harness(version); - let auth = crate::auth::Link::new(false); - auth.handle().authorize(¬hing()); - h.publisher = h.publisher.clone().with_auth(auth.clone()); - - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - moq_clock::sleep(Duration::from_millis(10)).await; - let opened = futures::poll!(std::pin::pin!(Stream::accept( - &mut crate::driver::Transport::new(h.peer.clone()), - version - ))); - assert!(opened.is_pending(), "{version}: listed a track the grant denies"); - - auth.handle().authorize(&crate::auth::Grant::all()); - let (mut first, ..) = h.publish().await; - let (mut second, ..) = h.publish().await; - publish_ok(&mut first).await; - publish_ok(&mut second).await; - - auth.handle().authorize(¬hing()); - for stream in [&mut first, &mut second] { - assert_eq!(next(stream).await.0, ietf::PublishDone::ID, "{version}"); - } - } - } -} diff --git a/rs/moq-net/src/ietf/request_stream.rs b/rs/moq-net/src/ietf/request_stream.rs index f154559aaad..fa81643e09e 100644 --- a/rs/moq-net/src/ietf/request_stream.rs +++ b/rs/moq-net/src/ietf/request_stream.rs @@ -327,11 +327,9 @@ impl<'a> Decode<'a, Version> for Update { } } -/// The namespace-only parameters of a REQUEST_UPDATE. `TRACKS` marks one on a -/// SUBSCRIBE_TRACKS, which from draft-22 may also carry TRACK_PROPERTY_FILTER and -/// FORWARD. +/// The namespace-only parameters of a REQUEST_UPDATE. #[derive(Debug)] -pub(super) struct NamespaceUpdate { +pub(super) struct NamespaceUpdate { pub(super) request_id: super::RequestId, pub(super) prefix: Option, pub(super) refused: Option, @@ -349,7 +347,7 @@ impl super::parameters::Param for NamespacePrefix { } } -impl<'a, const TRACKS: bool> Decode<'a, Version> for NamespaceUpdate { +impl<'a> Decode<'a, Version> for NamespaceUpdate { fn decode(r: &mut Decoder<'a>, version: Version) -> Result { if r.varint()? != super::SubscribeUpdate::ID { return Err(DecodeError::InvalidValue); @@ -361,8 +359,6 @@ impl<'a, const TRACKS: bool> Decode<'a, Version> for NamespaceUpdate { let request_id = super::RequestId::decode(&mut data, version)?; decode_params!(&mut data, version, 0x03 => token: Vec, - 0x10 => _forward: Option where TRACKS && super::Filter::is_draft22(version), - 0x29 => _track_filter: Vec where TRACKS && super::subscribe::has_range_filters(version), 0x34 => prefix: Option, ); if !data.is_empty() { @@ -488,24 +484,17 @@ mod tests { ); } - /// From draft-22 a SUBSCRIBE_TRACKS update may carry TRACK_PROPERTY_FILTER (0x29), so it - /// decodes there, to be refused per request, while a SUBSCRIBE_NAMESPACE's still closes. + /// TRACK_PROPERTY_FILTER (0x29) belongs only to SUBSCRIBE_TRACKS updates, so on a + /// SUBSCRIBE_NAMESPACE's it closes the session. #[test] - fn track_property_filter_on_listing_update_decodes() { + fn track_property_filter_on_namespace_update_closes() { // REQUEST_UPDATE, 6 bytes: Request ID 2, one parameter: 0x29 with a two-byte value. const FRAME: &[u8] = &[0x02, 0x00, 0x06, 2, 1, 0x29, 2, 0, 0]; - let version = Version::Draft22; - let mut buf = bytes::Bytes::from_static(FRAME); - let update = - crate::coding::decode_buf(&mut buf, version, |r, v| NamespaceUpdate::::decode(r, v)).unwrap(); - assert!(update.refused.is_none(), "{version}: {:?}", update.refused); - let mut buf = bytes::Bytes::from_static(FRAME); - assert!(crate::coding::decode_buf(&mut buf, version, |r, v| NamespaceUpdate::::decode(r, v)).is_err()); - let mut buf = bytes::Bytes::from_static(FRAME); - assert!( - crate::coding::decode_buf(&mut buf, Version::Draft18, |r, v| NamespaceUpdate::::decode(r, v)) - .is_err() - ); + for version in [Version::Draft18, Version::Draft22] { + let mut buf = bytes::Bytes::from_static(FRAME); + let decoded = crate::coding::decode_buf(&mut buf, version, |r, v| NamespaceUpdate::decode(r, v)); + assert!(decoded.is_err(), "{version}"); + } } /// TRACK_NAMESPACE_PREFIX (0x34) updates a SUBSCRIBE_NAMESPACE or SUBSCRIBE_TRACKS only, diff --git a/rs/moq-net/src/ietf/subscribe_tracks.rs b/rs/moq-net/src/ietf/subscribe_tracks.rs index 25772e046cb..bffb78a98f1 100644 --- a/rs/moq-net/src/ietf/subscribe_tracks.rs +++ b/rs/moq-net/src/ietf/subscribe_tracks.rs @@ -17,29 +17,18 @@ use super::token::RequestToken; /// SUBSCRIBE_TRACKS (0x51, draft-18+): list every track under a namespace prefix, each /// answered with a PUBLISH (or a [`PublishSkipped`]) as it appears. /// -/// Its subscription parameters are the defaults of each PUBLISH it produces. +/// We send it to list an IETF peer's tracks, and refuse one we receive, so only what our +/// request carries is kept; the rest is read to validate it and dropped. #[derive(Clone, Debug)] pub struct SubscribeTracks { pub request_id: RequestId, pub prefix: Path, /// FORWARD: absent means 1, which asks each PUBLISH to push its objects at once. pub forward: bool, - /// The default subscriber priority of each PUBLISH subscription. - pub subscriber_priority: Option, /// The default group order of each PUBLISH subscription. pub group_order: Option, - /// The default Location Filter of each PUBLISH subscription. - pub filter: Option, /// INCLUDE_PROPERTIES (draft-22): whether each PUBLISH carries Track Properties. pub properties_wanted: bool, - /// NEW_GROUP_REQUEST, which each PUBLISH applies as it first forwards. - pub new_group: Option, - /// Whether a Range Filter (0x25-0x28) narrows each subscription. We advertise no - /// MAX_FILTER_RANGES, so the request is refused. Never encoded. - pub range_filters: bool, - /// Whether TRACK_PROPERTY_FILTER (0x29) narrows which tracks are listed, which we - /// refuse rather than list tracks the subscriber excluded. Never encoded. - pub track_filter: bool, /// The `AUTHORIZATION TOKEN` (0x03) this request presents. pub authorization_token: Option, } @@ -79,28 +68,20 @@ impl<'a> Message<'a> for SubscribeTracks { 0x04 => _rendezvous_timeout: Option where defaults, 0x06 => _subgroup_delivery_timeout: Option where defaults, 0x10 => forward: Option, - 0x20 => subscriber_priority: Option where defaults, - 0x21 => filter: Option where defaults, + 0x20 => _subscriber_priority: Option where defaults, + 0x21 => _filter: Option where defaults, 0x22 => group_order: Option where defaults, // Each PUBLISH starts paused, where a fill opens nothing, even once resumed // (draft-22 section 3.4.1), so FILL_PARAMETERS is read and dropped. 0x23 => _fill: Option where Filter::is_draft22(version), - 0x25 => subgroup_filter: Vec where has_range_filters(version), - 0x26 => object_id_filter: Vec where has_range_filters(version), - 0x27 => priority_filter: Vec where has_range_filters(version), - 0x28 => object_property_filter: Vec where has_range_filters(version), - 0x29 => track_property_filter: Vec where has_range_filters(version), - 0x32 => new_group: Option where defaults, + 0x25 => _subgroup_filter: Vec where has_range_filters(version), + 0x26 => _object_id_filter: Vec where has_range_filters(version), + 0x27 => _priority_filter: Vec where has_range_filters(version), + 0x28 => _object_property_filter: Vec where has_range_filters(version), + 0x29 => _track_property_filter: Vec where has_range_filters(version), + 0x32 => _new_group: Option where defaults, 0x35 => include_properties: Option where Filter::is_draft22(version), ); - let range_filters = [ - subgroup_filter, - object_id_filter, - priority_filter, - object_property_filter, - ] - .iter() - .any(|filter| !filter.is_empty()); // Last, so a malformed field closes the session before a token refusal. let authorization_token = super::token::single(authorization_token)?; @@ -108,13 +89,8 @@ impl<'a> Message<'a> for SubscribeTracks { request_id, prefix, forward: forward.unwrap_or(true), - subscriber_priority, group_order, - filter, properties_wanted: include_properties.unwrap_or(true), - new_group, - range_filters, - track_filter: !track_property_filter.is_empty(), authorization_token, }) } @@ -169,13 +145,8 @@ mod tests { request_id: RequestId(4), prefix: Path::from_static("room"), forward: false, - subscriber_priority: None, group_order: None, - filter: None, properties_wanted: true, - new_group: None, - range_filters: false, - track_filter: false, authorization_token: None, } } @@ -199,14 +170,12 @@ mod tests { assert!(decoded.forward); } - /// TRACK_PROPERTY_FILTER (draft-22) is read so the request can be refused, not served - /// with tracks the subscriber excluded. + /// TRACK_PROPERTY_FILTER (draft-22) is legal, so it is read. #[test] - fn a_track_property_filter_is_noted() { + fn a_track_property_filter_is_read() { // Request ID 4, prefix "room", one parameter: 0x29 with a two-byte opaque value. let bytes = [0x04, 0x01, 0x04, b'r', b'o', b'o', b'm', 0x01, 0x29, 0x02, 0xaa, 0xbb]; - let decoded: SubscribeTracks = decode(&bytes, Version::Draft22).unwrap(); - assert!(decoded.track_filter); + assert!(decode::(&bytes, Version::Draft22).is_ok()); // Draft-18 has no such parameter, and an unknown even type is malformed there. assert!(decode::(&bytes, Version::Draft18).is_err()); } @@ -216,10 +185,7 @@ mod tests { #[test] fn draft_18_carries_no_subscription_parameters() { let listing = SubscribeTracks { - subscriber_priority: Some(7), group_order: Some(GroupOrder::Ascending), - filter: Some(Filter::NextObject), - new_group: Some(0), ..paused() }; assert_eq!(encode(&listing, Version::Draft18), encode(&paused(), Version::Draft18)); @@ -228,32 +194,21 @@ mod tests { assert!(decode::(&bytes, Version::Draft18).is_err()); } - /// Draft-22 section 9.18 no longer lists SUBSCRIBER_PRIORITY, LOCATION_FILTER, - /// FILL_PARAMETERS, or NEW_GROUP_REQUEST, so none is sent there; GROUP_ORDER still is. - /// Section 3.6.2 still allows any subscription parameter, so one received is read. + /// Draft-22 section 9.18 still lists GROUP_ORDER, so it is sent there. Section 3.6.2 + /// allows any subscription parameter, so one received is read. #[test] - fn draft_22_sends_only_its_listed_parameters() { - let listing = SubscribeTracks { - subscriber_priority: Some(7), - filter: Some(Filter::NextObject), - new_group: Some(0), - ..paused() - }; + fn draft_22_sends_its_group_order() { let ordered = SubscribeTracks { group_order: Some(GroupOrder::Ascending), ..paused() }; - let with_order = SubscribeTracks { - group_order: Some(GroupOrder::Ascending), - ..listing.clone() - }; let version = Version::Draft22; - assert_eq!(encode(&listing, version), encode(&paused(), version)); - assert_eq!(encode(&with_order, version), encode(&ordered, version)); + assert_ne!(encode(&ordered, version), encode(&paused(), version)); + let decoded: SubscribeTracks = decode(&encode(&ordered, version), version).unwrap(); + assert_eq!(decoded.group_order, Some(GroupOrder::Ascending)); // Request ID 4, prefix "room", one parameter: SUBSCRIBER_PRIORITY 7. let bytes = [0x04, 0x01, 0x04, b'r', b'o', b'o', b'm', 0x01, 0x20, 0x07]; - let decoded: SubscribeTracks = decode(&bytes, version).unwrap(); - assert_eq!(decoded.subscriber_priority, Some(7)); + assert!(decode::(&bytes, version).is_ok()); } #[test] diff --git a/rs/moq-net/src/ietf/subscriber/tracks.rs b/rs/moq-net/src/ietf/subscriber/tracks.rs index 9aa4a9d9f82..7efd77b81c9 100644 --- a/rs/moq-net/src/ietf/subscriber/tracks.rs +++ b/rs/moq-net/src/ietf/subscriber/tracks.rs @@ -433,13 +433,8 @@ impl Subscriber { request_id, prefix: prefix.clone(), forward: false, - subscriber_priority: None, group_order: None, - filter: None, properties_wanted: true, - new_group: None, - range_filters: false, - track_filter: false, authorization_token: None, }, )?; @@ -1522,6 +1517,35 @@ mod tests { } } + /// One SUBSCRIBE_TRACKS covers each nesting chain, since the peer refuses an overlapping + /// prefix: a nested listing reuses the broader request, and a broader one cancels the + /// nested requests and goes out only once they have left the wire. + #[test] + fn overlapping_listings_share_one_request() { + let broadcasts: Vec<_> = (0..5).map(|_| crate::broadcast::Info::default().produce()).collect(); + let lister = |i: usize| Arc::new(broadcasts[i].requests().lister().unwrap()); + let live = |listings: &Listings| listings.requests.values().filter(|r| r.live.is_some()).count(); + + let mut listings = Listings::default(); + let (_, _, outer) = listings.listen(Path::from_static("a"), lister(0)); + assert!( + outer.is_some_and(|outer| outer.blockers.is_empty()), + "the first listing waits" + ); + let (_, _, nested) = listings.listen(Path::from_static("a/b"), lister(1)); + assert!(nested.is_none(), "a nested listing asked again"); + assert_eq!(live(&listings), 1); + + let mut listings = Listings::default(); + for (i, path) in ["a/b", "a/c"].into_iter().enumerate() { + let (_, _, nested) = listings.listen(Path::new(path).unwrap(), lister(2 + i)); + assert!(nested.is_some_and(|nested| nested.blockers.is_empty()), "{path}"); + } + let (_, _, outer) = listings.listen(Path::from_static("a"), lister(4)); + assert_eq!(outer.expect("the broader listing asks").blockers.len(), 2); + assert_eq!(live(&listings), 1, "the nested requests stand beside it"); + } + /// The error code a refused PUBLISH answered with. fn refusal(id: u64, body: &ietf::Body, version: Version) -> u64 { assert_eq!(id, ietf::RequestError::ID, "accepted"); diff --git a/rs/moq-net/tests/integration/subscribe_tracks.rs b/rs/moq-net/tests/integration/subscribe_tracks.rs index e33a0341b0f..4f4fafa5542 100644 --- a/rs/moq-net/tests/integration/subscribe_tracks.rs +++ b/rs/moq-net/tests/integration/subscribe_tracks.rs @@ -1,7 +1,6 @@ #![cfg(feature = "async")] -//! A SUBSCRIBE_TRACKS asking for every track's objects is answered on its own stream, and -//! the session keeps delivering. +//! A SUBSCRIBE_TRACKS is refused on its own stream, and the session keeps delivering. //! //! Deterministic: paused time and a single-threaded runtime over the mock transport. @@ -100,9 +99,9 @@ async fn deliver(setup: &mut Setup, payload: &'static [u8]) { assert_eq!(got, payload); } -/// Send SUBSCRIBE_TRACKS from the server to the publishing client, returning the first -/// `len` bytes of the reply, or all of it if the client ends the stream first. -async fn subscribe_tracks(pair: &MockPair, len: usize) -> Vec { +/// Send SUBSCRIBE_TRACKS from the server to the publishing client, returning every byte +/// of the reply once the client ends the stream. +async fn subscribe_tracks(pair: &MockPair) -> Vec { let (mut send, mut recv) = pair .server_transport .clone() @@ -112,50 +111,29 @@ async fn subscribe_tracks(pair: &MockPair, len: usize) -> Vec { send.write(SUBSCRIBE_TRACKS).await.expect("write"); let mut reply = Vec::new(); - while reply.len() < len - && let Some(chunk) = moq_clock::timeout(TIMEOUT, recv.read_chunk(len - reply.len())) - .await - .expect("reply timed out") - .expect("read") + while let Some(chunk) = moq_clock::timeout(TIMEOUT, recv.read_chunk(usize::MAX)) + .await + .expect("reply timed out") + .expect("read") { reply.extend_from_slice(&chunk); } reply } -/// An omitted FORWARD asks for every track's objects, which we never push unasked: on -/// draft-22 each track is answered with PUBLISH_SKIPPED, and the subscription already on -/// the session keeps going. +/// We list no tracks by PUBLISH, so a SUBSCRIBE_TRACKS gets NOT_SUPPORTED on its own +/// stream, and the subscription already on the session keeps going. #[moq_clock::test] -async fn subscribe_tracks_skips_every_track() { - let version = "moq-transport-22"; - let mut setup = setup(version.parse().unwrap()).await; - deliver(&mut setup, b"before").await; - - #[rustfmt::skip] - let expected = [ - 0x07, 0x00, 0x01, 0x00, // REQUEST_OK, no parameters - 0x0f, 0x00, 0x07, // PUBLISH_SKIPPED - 0x00, // The broadcast is the prefix itself: an empty suffix. - 0x05, b'v', b'i', b'd', b'e', b'o', - ]; - let reply = subscribe_tracks(&setup.pair, expected.len()).await; - assert_eq!(reply, expected, "{version}"); - - deliver(&mut setup, b"after").await; -} +async fn subscribe_tracks_is_refused_per_request() { + for version in ["moq-transport-18", "moq-transport-22"] { + let mut setup = setup(version.parse().unwrap()).await; + deliver(&mut setup, b"before").await; -/// Draft-18 has no PUBLISH_SKIPPED, so a listing that must forward is refused on its own -/// stream with NOT_SUPPORTED, and the session keeps delivering. -#[moq_clock::test] -async fn subscribe_tracks_needs_forward_0_on_draft_18() { - let mut setup = setup("moq-transport-18".parse().unwrap()).await; - deliver(&mut setup, b"before").await; + let reply = subscribe_tracks(&setup.pair).await; + // REQUEST_ERROR (0x05), a two-byte length, then the error code: NOT_SUPPORTED (0x3). + assert_eq!(reply.first(), Some(&0x05), "{version}: not a REQUEST_ERROR: {reply:?}"); + assert_eq!(reply.get(3), Some(&0x03), "{version}: not NOT_SUPPORTED: {reply:?}"); - let reply = subscribe_tracks(&setup.pair, usize::MAX).await; - // REQUEST_ERROR (0x05), a two-byte length, then the error code: NOT_SUPPORTED (0x3). - assert_eq!(reply.first(), Some(&0x05), "not a REQUEST_ERROR: {reply:?}"); - assert_eq!(reply.get(3), Some(&0x03), "not NOT_SUPPORTED: {reply:?}"); - - deliver(&mut setup, b"after").await; + deliver(&mut setup, b"after").await; + } } diff --git a/rs/moq-net/tests/integration/tracks_ietf.rs b/rs/moq-net/tests/integration/tracks_ietf.rs index 0d6c60c7c91..28e7f63b8df 100644 --- a/rs/moq-net/tests/integration/tracks_ietf.rs +++ b/rs/moq-net/tests/integration/tracks_ietf.rs @@ -1,8 +1,9 @@ #![cfg(feature = "async")] -//! A TRACKS listing over a moq-transport session (draft-18+): the subscriber sends -//! SUBSCRIBE_TRACKS with FORWARD=0 and holds each PUBLISH paused as its track's entry, which -//! a reader resumes for data instead of subscribing again. +//! A TRACKS listing over a moq-transport session (draft-18+) sends SUBSCRIBE_TRACKS with +//! FORWARD=0, only on demand. Our own publisher refuses it, so a listing of our peer fails +//! alone: the session and its SUBSCRIBEs carry on. The listing's handling of a peer that +//! answers is covered against a scripted peer in `ietf::subscriber::tracks`. //! //! Deterministic: paused time and a single-threaded runtime over the mock transport. @@ -46,13 +47,6 @@ async fn next(tracks: &mut moq_net::broadcast::Tracks) -> String { } } -/// Whether the listing has nothing more to report for a while. -async fn quiet(tracks: &mut moq_net::broadcast::Tracks) -> bool { - moq_clock::timeout(Duration::from_millis(500), tracks.next()) - .await - .is_err() -} - /// `publisher` served to a fresh origin over one session; returns that origin's copy. async fn connect(publisher: &moq_net::origin::Producer, version: &str) -> (moq_net::origin::Producer, MockPair) { let subscriber = produce_origin(2); @@ -97,31 +91,11 @@ async fn read(sub: &mut moq_net::track::Subscriber) -> Option<(u64, Vec)> { Some((group.sequence, frame.payload.to_vec())) } -/// The listing syncs at once, then lists the peer's tracks as they appear. +/// SUBSCRIBE_TRACKS goes out only for a listing, since many peers do not answer it: +/// connecting, an announce, and a reader's SUBSCRIBE send none. Our own publisher refuses +/// it, which fails only that listing, and the SUBSCRIBE keeps delivering. #[moq_clock::test] -async fn an_ietf_session_lists_its_peers_tracks() { - for version in DRAFTS { - let publisher = produce_origin(1); - let broadcast = publisher.create_broadcast("bcast").unwrap(); - let _a = broadcast.create_track("a", None).unwrap(); - broadcast.announce(Default::default()).unwrap(); - - let (subscriber, _pair) = connect(&publisher, version).await; - let remote = resolve(&subscriber, "bcast").await; - let mut tracks = remote.tracks().unwrap(); - assert_eq!(next(&mut tracks).await, "synced", "{version}"); - assert_eq!(next(&mut tracks).await, "+a", "{version}"); - - let _b = broadcast.create_track("b", None).unwrap(); - assert_eq!(next(&mut tracks).await, "+b", "{version}"); - assert!(quiet(&mut tracks).await, "{version}"); - } -} - -/// SUBSCRIBE_TRACKS goes out only for a listing, since many peers do not answer it yet: -/// connecting, an announce, and a reader's SUBSCRIBE send none. -#[moq_clock::test] -async fn only_a_listing_sends_subscribe_tracks() { +async fn a_listing_of_our_own_peer_fails_alone() { for version in DRAFTS { let publisher = produce_origin(1); let broadcast = publisher.create_broadcast("bcast").unwrap(); @@ -139,344 +113,21 @@ async fn only_a_listing_sends_subscribe_tracks() { .expect("never subscribed") .unwrap(); let mut group = video.append_group().unwrap(); - group.write_frame(None, &b"frame"[..]).unwrap(); + group.write_frame(None, &b"before"[..]).unwrap(); group.finish().unwrap(); - assert_eq!(read(&mut sub).await, Some((0, b"frame".to_vec())), "{version}"); - assert_eq!(subscribes(&pair), 1, "{version}"); + assert_eq!(read(&mut sub).await, Some((0, b"before".to_vec())), "{version}"); assert_eq!(opened(&pair, SUBSCRIBE_TRACKS), 0, "{version}: listed unasked"); let mut tracks = remote.tracks().unwrap(); assert_eq!(next(&mut tracks).await, "synced", "{version}"); - assert_eq!(next(&mut tracks).await, "+video", "{version}"); + let failed = next(&mut tracks).await; + assert!(failed.starts_with("error:"), "{version}: {failed}"); assert_eq!(opened(&pair, SUBSCRIBE_TRACKS), 1, "{version}"); - } -} - -/// A listed track carries the info its PUBLISH declared. -#[moq_clock::test] -async fn a_listing_carries_track_info() { - let publisher = produce_origin(1); - let broadcast = publisher.create_broadcast("bcast").unwrap(); - let info = moq_net::track::Info::default() - .with_timescale(moq_net::Timescale::MILLI) - .with_priority(9); - let _a = broadcast.create_track("a", info).unwrap(); - broadcast.announce(Default::default()).unwrap(); - - let (subscriber, _pair) = connect(&publisher, "moq-transport-22").await; - let mut tracks = resolve(&subscriber, "bcast").await.tracks().unwrap(); - assert_eq!(next(&mut tracks).await, "synced"); - let event = moq_clock::timeout(TIMEOUT, tracks.next()) - .await - .expect("listing stalled") - .unwrap(); - let Some(TracksEvent::Added { name, info }) = event else { - panic!("expected an add, got {event:?}"); - }; - assert_eq!(&*name, "a"); - assert_eq!(info.timescale, Some(moq_net::Timescale::MILLI)); - assert_eq!(info.priority, 9); -} - -/// A reader of a listed track resumes its held PUBLISH, so its data arrives with no -/// SUBSCRIBE at all; draft-18 would refuse one as a second subscription to the track. -/// Once the reader leaves, the PUBLISH pauses and stays listed, and the next reader -/// resumes it again. -#[moq_clock::test] -async fn a_reader_resumes_the_held_publish() { - for version in DRAFTS { - let publisher = produce_origin(1); - let broadcast = publisher.create_broadcast("bcast").unwrap(); - let video = broadcast.create_track("video", None).unwrap(); - broadcast.announce(Default::default()).unwrap(); - - let (subscriber, pair) = connect(&publisher, version).await; - let remote = resolve(&subscriber, "bcast").await; - let mut tracks = remote.tracks().unwrap(); - assert_eq!(next(&mut tracks).await, "synced", "{version}"); - assert_eq!(next(&mut tracks).await, "+video", "{version}"); - let mut sub = moq_clock::timeout(TIMEOUT, remote.track("video").unwrap().subscribe(None)) - .await - .expect("subscribe timeout") - .expect("subscribe"); - moq_clock::timeout(TIMEOUT, support::wanted(video.demand())) - .await - .expect("never resumed") - .unwrap(); let mut group = video.append_group().unwrap(); - group.write_frame(None, &b"first"[..]).unwrap(); + group.write_frame(None, &b"after"[..]).unwrap(); group.finish().unwrap(); - assert_eq!(read(&mut sub).await, Some((0, b"first".to_vec())), "{version}"); - assert_eq!(subscribes(&pair), 0, "{version}: subscribed beside the held PUBLISH"); - - // The last reader leaving pauses the PUBLISH upstream, and the track stays listed. - drop(sub); - moq_clock::timeout(TIMEOUT, support::unwanted(video.demand())) - .await - .expect("never paused") - .unwrap(); - assert!(quiet(&mut tracks).await, "{version}"); - - let mut sub = moq_clock::timeout(TIMEOUT, remote.track("video").unwrap().subscribe(None)) - .await - .expect("subscribe timeout") - .expect("subscribe"); - moq_clock::timeout(TIMEOUT, support::wanted(video.demand())) - .await - .expect("never resumed again") - .unwrap(); - let mut group = video.append_group().unwrap(); - group.write_frame(None, &b"second"[..]).unwrap(); - group.finish().unwrap(); - let got = loop { - match read(&mut sub).await { - Some((sequence, _)) if sequence < 1 => continue, - got => break got, - } - }; - assert_eq!(got, Some((1, b"second".to_vec())), "{version}"); - assert_eq!(subscribes(&pair), 0, "{version}"); - } -} - -/// A reader joining a listed track mid-group gets the objects already published in it: on -/// draft 18, whose filters deliver nothing already published, through a Joining -/// FETCH on the held PUBLISH's Request ID, and from draft-22 through the resume's fill. -#[moq_clock::test] -async fn a_join_gets_the_current_groups_head() { - for version in DRAFTS { - let publisher = produce_origin(1); - let broadcast = publisher.create_broadcast("bcast").unwrap(); - let video = broadcast.create_track("video", None).unwrap(); - broadcast.announce(Default::default()).unwrap(); - - let (subscriber, pair) = connect(&publisher, version).await; - let remote = resolve(&subscriber, "bcast").await; - let mut tracks = remote.tracks().unwrap(); - assert_eq!(next(&mut tracks).await, "synced", "{version}"); - assert_eq!(next(&mut tracks).await, "+video", "{version}"); - - let mut group = video.create_group(moq_net::group::Info { sequence: 5 }).unwrap(); - group.write_frame(None, &b"head"[..]).unwrap(); - - let mut sub = moq_clock::timeout(TIMEOUT, remote.track("video").unwrap().subscribe(None)) - .await - .expect("subscribe timeout") - .expect("subscribe"); - moq_clock::timeout(TIMEOUT, support::wanted(video.demand())) - .await - .expect("never resumed") - .unwrap(); - moq_clock::sleep(Duration::from_millis(10)).await; - group.write_frame(None, &b"tail"[..]).unwrap(); - group.finish().unwrap(); - - let mut joined = moq_clock::timeout(TIMEOUT, sub.recv_group()) - .await - .expect("group timeout") - .unwrap() - .expect("the joined group"); - assert_eq!(joined.sequence, 5, "{version}"); - let mut frames = Vec::new(); - while let Some(frame) = moq_clock::timeout(TIMEOUT, joined.read_frame()) - .await - .expect("frame timeout") - .unwrap() - { - frames.push(frame.payload.to_vec()); - } - assert_eq!(frames, [b"head".to_vec(), b"tail".to_vec()], "{version}"); - assert_eq!(subscribes(&pair), 0, "{version}"); - } -} - -/// A nested listing reuses the broader SUBSCRIBE_TRACKS and filters locally: the peer -/// would refuse an overlapping prefix. -#[moq_clock::test] -async fn a_nested_listing_reuses_the_broader_one() { - for version in DRAFTS { - let publisher = produce_origin(1); - let outer = publisher.create_broadcast("a").unwrap(); - let _x = outer.create_track("x", None).unwrap(); - outer.announce(Default::default()).unwrap(); - let inner = publisher.create_broadcast("a/b").unwrap(); - let _y = inner.create_track("y", None).unwrap(); - inner.announce(Default::default()).unwrap(); - - let (subscriber, _pair) = connect(&publisher, version).await; - let mut outer_tracks = resolve(&subscriber, "a").await.tracks().unwrap(); - assert_eq!(next(&mut outer_tracks).await, "synced", "{version}"); - assert_eq!(next(&mut outer_tracks).await, "+x", "{version}"); - // The broader request holds `a/b`'s tracks already, so they list before syncing. - let mut inner_tracks = resolve(&subscriber, "a/b").await.tracks().unwrap(); - assert_eq!(next(&mut inner_tracks).await, "+y", "{version}"); - assert_eq!(next(&mut inner_tracks).await, "synced", "{version}"); - - let _z = inner.create_track("z", None).unwrap(); - assert_eq!(next(&mut inner_tracks).await, "+z", "{version}"); - assert!( - quiet(&mut outer_tracks).await, - "{version}: a nested track listed in its parent" - ); - } -} - -/// A broader listing replaces the nested requests only once the peer has closed them, so it -/// is never refused as overlapping, and the nested listings keep what they listed. -#[moq_clock::test] -async fn a_broader_listing_replaces_the_nested_ones() { - for version in DRAFTS { - let publisher = produce_origin(1); - let mut held = Vec::new(); - let mut broadcasts = Vec::new(); - for path in ["a/b", "a/c", "a"] { - let broadcast = publisher.create_broadcast(path).unwrap(); - held.push(broadcast.create_track("t", None).unwrap()); - broadcast.announce(Default::default()).unwrap(); - broadcasts.push(broadcast); - } - - let (subscriber, _pair) = connect(&publisher, version).await; - let mut nested = Vec::new(); - for path in ["a/b", "a/c"] { - let mut tracks = resolve(&subscriber, path).await.tracks().unwrap(); - assert_eq!(next(&mut tracks).await, "synced", "{version}"); - assert_eq!(next(&mut tracks).await, "+t", "{version}: {path}"); - nested.push(tracks); - } - - let mut outer = resolve(&subscriber, "a").await.tracks().unwrap(); - assert_eq!(next(&mut outer).await, "synced", "{version}"); - assert_eq!(next(&mut outer).await, "+t", "{version}"); - for tracks in &mut nested { - assert!(quiet(tracks).await, "{version}: a nested listing changed"); - } - - // The broader request lists what follows in the nested broadcasts too. - held.push(broadcasts[0].create_track("u", None).unwrap()); - assert_eq!(next(&mut nested[0]).await, "+u", "{version}"); - } -} - -/// A PUBLISH for a track we already SUBSCRIBEd is redundant: the SUBSCRIBE lists the track -/// and keeps carrying its data. Draft-18 skips it instead, which lists it the same way. -#[moq_clock::test] -async fn a_subscribed_track_is_listed_through_its_subscribe() { - for version in DRAFTS { - let publisher = produce_origin(1); - let broadcast = publisher.create_broadcast("bcast").unwrap(); - let video = broadcast.create_track("video", None).unwrap(); - broadcast.announce(Default::default()).unwrap(); - - let (subscriber, pair) = connect(&publisher, version).await; - let remote = resolve(&subscriber, "bcast").await; - let mut sub = moq_clock::timeout(TIMEOUT, remote.track("video").unwrap().subscribe(None)) - .await - .expect("subscribe timeout") - .expect("subscribe"); - moq_clock::timeout(TIMEOUT, support::wanted(video.demand())) - .await - .expect("never subscribed") - .unwrap(); + assert_eq!(read(&mut sub).await, Some((1, b"after".to_vec())), "{version}"); assert_eq!(subscribes(&pair), 1, "{version}"); - - let mut tracks = remote.tracks().unwrap(); - assert_eq!(next(&mut tracks).await, "synced", "{version}"); - assert_eq!(next(&mut tracks).await, "+video", "{version}"); - - let mut group = video.append_group().unwrap(); - group.write_frame(None, &b"live"[..]).unwrap(); - group.finish().unwrap(); - assert_eq!(read(&mut sub).await, Some((0, b"live".to_vec())), "{version}"); - assert!(quiet(&mut tracks).await, "{version}"); - assert_eq!(subscribes(&pair), 1, "{version}"); - } -} - -/// Each held PUBLISH counts against the session's `announces` cap, as a lite listed track -/// does: past it, the PUBLISH is refused and the listing reset rather than left short, and -/// the session stays. -#[moq_clock::test] -async fn a_listing_past_the_announces_cap_is_reset() { - for version in DRAFTS { - let publisher = produce_origin(1); - let broadcast = publisher.create_broadcast("bcast").unwrap(); - const CAP: usize = 10; - let _tracks: Vec<_> = (0..CAP) - .map(|i| broadcast.create_track(format!("t{i}"), None).unwrap()) - .collect(); - broadcast.announce(Default::default()).unwrap(); - - let mut limits = moq_net::session::Limits::default(); - limits.announces = CAP; - let subscriber = produce_origin(2); - let mut options = MockConnectOptions::new(version.parse::().unwrap()); - options.client_publish = Some(publisher.consume()); - options.server_subscribe = Some(subscriber.clone()); - options.server_limits = Some(limits); - let pair = connect_mock(options).await; - - let remote = resolve(&subscriber, "bcast").await; - let mut listing = remote.tracks().unwrap(); - assert_eq!(next(&mut listing).await, "synced", "{version}"); - // The announcement holds a slot too, so not every track fits. - let mut added = 0; - let end = loop { - match next(&mut listing).await { - event if event.starts_with('+') => added += 1, - event => break event, - } - }; - assert!(added < CAP, "{version}: listed {added} tracks under a cap of {CAP}"); - assert_eq!(end, format!("error: {}", moq_net::Error::TooManyRequests), "{version}"); - - assert!( - moq_clock::timeout(Duration::from_millis(100), pair.server.closed()) - .await - .is_err(), - "{version}: the session closed" - ); - } -} - -/// Only TRACK_ENDED removes a listed track, and our answerer sends it only for a track that -/// finished: that one leaves the listing quietly. One that left unfinished may go on -/// elsewhere, so its PUBLISH ends in error, which fails the listing loudly, and a reader -/// listing again sees what is still listed. -#[moq_clock::test] -async fn a_track_leaves_the_listing_as_it_ended() { - for finish in [true, false] { - for version in DRAFTS { - let publisher = produce_origin(1); - let broadcast = publisher.create_broadcast("bcast").unwrap(); - let a = broadcast.create_track("a", None).unwrap(); - let _b = broadcast.create_track("b", None).unwrap(); - broadcast.announce(Default::default()).unwrap(); - - let (subscriber, _pair) = connect(&publisher, version).await; - let remote = resolve(&subscriber, "bcast").await; - let mut tracks = remote.tracks().unwrap(); - assert_eq!(next(&mut tracks).await, "synced", "{version}"); - let mut listed = vec![next(&mut tracks).await, next(&mut tracks).await]; - listed.sort(); - assert_eq!(listed, ["+a", "+b"], "{version}"); - - if finish { - a.finish().unwrap(); - drop(a); - assert_eq!(next(&mut tracks).await, "~a", "{version}"); - assert!(quiet(&mut tracks).await, "{version}: the listing stands"); - continue; - } - let _ = a.abort(moq_net::Error::Cancel); - let failed = next(&mut tracks).await; - assert!(failed.starts_with("error:"), "{version}: {failed}"); - - let mut again = remote.tracks().unwrap(); - assert_eq!(next(&mut again).await, "synced", "{version}"); - assert_eq!(next(&mut again).await, "+b", "{version}"); - assert!(quiet(&mut again).await, "{version}"); - } } } diff --git a/sh/gh/nightly.sh b/sh/gh/nightly.sh index 7f8bc0d4698..ada816becf5 100755 --- a/sh/gh/nightly.sh +++ b/sh/gh/nightly.sh @@ -136,7 +136,7 @@ case "$1" in done ;; js) - for benchmark in broadcasts requests fills forward reader frames drift ledger varint lite-varint track resume priority subscribe-drop subscribe-tracks refresh observed compression; do + for benchmark in broadcasts requests fills forward reader frames drift ledger varint lite-varint track resume priority subscribe-drop refresh observed compression; do javascript "$benchmark" just js bench "$benchmark" done javascript catalog just js catalog-bench