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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions js/net/src/ietf/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const INVALID_RANGE = 0x11;
const INVALID_JOINING_REQUEST_ID = 0x32;

/** A publisher serving the `room` broadcast, with its `video` track and the client's uni streams. */
function serve(version: IetfVersion, grant?: Signal<Grant | undefined>) {
function serve(version: IetfVersion, grant?: Signal<Grant | undefined>, priority?: number) {
const pair = createMockTransportPair(ALPNS[version]);
const origin = new OriginProducer();
const pub = new Publisher({
Expand All @@ -48,7 +48,7 @@ function serve(version: IetfVersion, grant?: Signal<Grant | undefined>) {
});
const publication = origin.createBroadcast(Path.from("room"));
publication.announce();
const track = publication.broadcast.createTrack("video", { timescale: Timescale.MILLI });
const track = publication.broadcast.createTrack("video", { timescale: Timescale.MILLI, priority });
const uni = pair.client.incomingUnidirectionalStreams.getReader() as ReadableStreamDefaultReader<
ReadableStream<Uint8Array>
>;
Expand Down Expand Up @@ -326,6 +326,37 @@ test("a FETCH spanning groups is refused before FETCH_OK", async () => {
}
});

/** The Publisher Priority each object on a fetch stream states, absent where it inherits. */
async function priorities(raw: Uint8Array, version: IetfVersion): Promise<(number | undefined)[]> {
const reader = new Reader(undefined, raw, version);
expect(await reader.u53()).toBe(FetchHeader.type);
await FetchHeader.decode(reader, version);
const out: (number | undefined)[] = [];
while (!(await reader.done())) {
const flags = await reader.u53();
if (flags & 0x08) await reader.u53();
if (flags & 0x04) await reader.u53();
out.push(flags & 0x10 ? await reader.u8() : undefined);
if (flags & 0x20) await reader.read(await reader.u53());
await reader.read(await reader.u53());
}
return out;
}

// A fetch stream's first object states the track's Publisher Priority, as a subgroup header
// does, and the rest inherit it. Model priority 72 is wire 55, lower first.
test("a FETCH states the track's priority on its first object", async () => {
for (const version of VERSIONS) {
using fx = serve(version, undefined, 72);
publishPairs(fx.track, 2);
const type = range(version, at(1, 0), at(1, 0));
const answer = served(await fetch(fx, new Fetch({ requestId: FETCH_ID, type })));
expect(answer.objects).toEqual(pairs([1]));
expect(answer.ok.properties.priority).toBe(55);
expect(await priorities(answer.raw, version)).toEqual([55, undefined]);
}
});

// Only a finished track's final object ends it, wherever the range starts or ends.
test("a FETCH through the final object reports the end of track", async () => {
for (const version of VERSIONS) {
Expand Down Expand Up @@ -560,7 +591,8 @@ test("draft-18: a joining FETCH serves the head its subscription starts after",
await reader.u53();
await FetchHeader.decode(reader, D18);
expect(await reader.u53()).toBe(0x3c);
expect([await reader.u53(), await reader.u53(), await reader.u8()]).toEqual([5, 0, 0]);
// The priority is the track's, as FETCH_OK and every subgroup header state it.
expect([await reader.u53(), await reader.u53(), await reader.u8()]).toEqual([5, 0, 127]);
await reader.read(await reader.u53());
await reader.read(await reader.u53());
expect(await reader.u53()).toBe(0x20);
Expand Down
19 changes: 12 additions & 7 deletions js/net/src/ietf/ietf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,17 +814,18 @@ test("Frame reads only the current LOC Timestamp ID on every draft", async () =>

// A fetch stream's first object is the only one carrying absolute ids, so a wrong flag byte
// there silently renumbers every object after it. This codec is the sole serialization path
// for a served fill.
// for a served fill. Its priority is the track's, as a subgroup header's is: model priority
// 72 is wire 55, lower first.
test("FetchFrame: the first object carries its absolute ids and the rest inherit them", async () => {
const timestamp = new Timestamp(96_000, Timescale.MILLI);
const frame = new FetchFrame({ payload: new Uint8Array([0xaa, 0xbb]), timestamp });
const frame = new FetchFrame({ payload: new Uint8Array([0xaa, 0xbb]), timestamp, priority: 55 });

const first = await encodeFetchFrameVersioned(frame, { group: 7n, object: 3, first: true }, Version.DRAFT_22);
expect(Array.from(first)).toEqual([
0x3c, // GROUP_ID | OBJECT_ID | PRIORITY | PROPERTIES
7, // group id
3, // object id
0, // publisher priority
55, // publisher priority
0x04, // properties length
0x10, // TIMESTAMP
0xc1,
Expand Down Expand Up @@ -879,7 +880,7 @@ test("FetchEndOfRange: absolute ids and nothing after them", async () => {
// The first object after an End of Range has no prior object's priority to inherit, but its
// group is still a delta from the marker's.
test("FetchFrame: the first object after an End of Range", async () => {
const frame = new FetchFrame({ payload: new Uint8Array([0x01]) });
const frame = new FetchFrame({ payload: new Uint8Array([0x01]), priority: 0 });
const encoded = await encodeFetchFrameVersioned(
frame,
{ group: 9n, object: 0, first: true, previousGroup: 7n },
Expand All @@ -892,7 +893,11 @@ test("FetchFrame: the first object after an End of Range", async () => {
// A fetch object has no status field, unlike a subgroup object: a zero payload length is
// simply an empty object, and writing a status after it would desync the stream.
test("FetchFrame: an empty payload is a zero length, with no status byte", async () => {
const frame = new FetchFrame({ payload: new Uint8Array(), timestamp: new Timestamp(0, Timescale.MILLI) });
const frame = new FetchFrame({
payload: new Uint8Array(),
timestamp: new Timestamp(0, Timescale.MILLI),
priority: 0,
});
const encoded = await encodeFetchFrameVersioned(frame, { group: 1n, object: 0, first: true }, Version.DRAFT_22);

expect(Array.from(encoded)).toEqual([
Expand All @@ -911,7 +916,7 @@ test("FetchFrame: an empty payload is a zero length, with no status byte", async
// omitted rather than carrying a value in units the peer was never told. This is what a
// subscriber that sent INCLUDE_PROPERTIES=0 gets.
test("FetchFrame: an unstamped object omits the properties field", async () => {
const frame = new FetchFrame({ payload: new Uint8Array([0x01]) });
const frame = new FetchFrame({ payload: new Uint8Array([0x01]), priority: 0 });
const encoded = await encodeFetchFrameVersioned(frame, { group: 0n, object: 0, first: false }, Version.DRAFT_22);

// Flags 0 (subgroup zero, no fields present), then the payload.
Expand All @@ -920,7 +925,7 @@ test("FetchFrame: an unstamped object omits the properties field", async () => {

// The first object still carries its absolute ids and priority; only the properties go.
test("FetchFrame: an unstamped first object keeps its ids", async () => {
const frame = new FetchFrame({ payload: new Uint8Array([0x01]) });
const frame = new FetchFrame({ payload: new Uint8Array([0x01]), priority: 0 });
const encoded = await encodeFetchFrameVersioned(frame, { group: 4n, object: 2, first: true }, Version.DRAFT_22);

expect(Array.from(encoded)).toEqual([
Expand Down
12 changes: 6 additions & 6 deletions js/net/src/ietf/join.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async function head(requestId: bigint, payloads: string[], group = GROUP, versio
const bytes = await Writer.collect(version, async (w) => {
await w.u62(requestId);
for (const [object, payload] of payloads.entries()) {
await new FetchFrame({ payload: text(payload) }).encode(
await new FetchFrame({ payload: text(payload), priority: 0 }).encode(
w,
{ group, object, first: object === 0 },
undefined,
Expand Down Expand Up @@ -394,7 +394,7 @@ async function deltaHead(requestId: bigint, payloads: string[], delta: number) {
await w.u62(requestId);
for (const [object, payload] of payloads.entries()) {
if (object === 0) {
await new FetchFrame({ payload: text(payload) }).encode(
await new FetchFrame({ payload: text(payload), priority: 0 }).encode(
w,
{ group: GROUP, object, first: true },
undefined,
Expand Down Expand Up @@ -455,13 +455,13 @@ test("a joining FETCH spanning another group drops the head", async () => {

const bytes = await Writer.collect(VERSION, async (w) => {
await w.u62(fetch.requestId);
await new FetchFrame({ payload: text("h0") }).encode(
await new FetchFrame({ payload: text("h0"), priority: 0 }).encode(
w,
{ group: GROUP, object: 0, first: true },
undefined,
VERSION,
);
await new FetchFrame({ payload: text("x0") }).encode(
await new FetchFrame({ payload: text("x0"), priority: 0 }).encode(
w,
{ group: GROUP + 1n, object: 0, first: false, previousGroup: GROUP },
undefined,
Expand Down Expand Up @@ -550,13 +550,13 @@ test("a clean end finishes a head nothing claimed", () => {

test("fetch objects decode as FetchFrame wrote them", async () => {
const bytes = await Writer.collect(VERSION, async (w) => {
await new FetchFrame({ payload: text("a") }).encode(
await new FetchFrame({ payload: text("a"), priority: 0 }).encode(
w,
{ group: 5n, object: 0, first: true },
undefined,
VERSION,
);
await new FetchFrame({ payload: new Uint8Array() }).encode(
await new FetchFrame({ payload: new Uint8Array(), priority: 0 }).encode(
w,
{ group: 5n, object: 1, first: false },
undefined,
Expand Down
10 changes: 8 additions & 2 deletions js/net/src/ietf/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,16 @@ export class FetchFrame {
payload: Uint8Array;
/** The presentation timestamp carried in object properties, when present. */
timestamp?: Timestamp;
/**
* The track's Publisher Priority on the wire, which the stream's first object states for
* every object after it, as a subgroup header does.
*/
priority: number;

constructor({ payload, timestamp }: { payload: Uint8Array; timestamp?: Timestamp }) {
constructor({ payload, timestamp, priority }: { payload: Uint8Array; timestamp?: Timestamp; priority: number }) {
this.payload = payload;
this.timestamp = timestamp;
this.priority = priority;
}

/**
Expand Down Expand Up @@ -459,7 +465,7 @@ export class FetchFrame {
),
encodeVarint(position.object, version),
);
if (position.first) header.push(Uint8Array.of(0));
if (position.first) header.push(Uint8Array.of(this.priority));
} else {
// Same group and priority; the Object ID is the prior one plus one.
header.push(encodeVarint(stamped ? FETCH_PROPERTIES : 0, version));
Expand Down
82 changes: 82 additions & 0 deletions js/net/src/ietf/publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4188,6 +4188,88 @@ test("draft-22: cancellation releases a fill parked on flow control", async () =
}
});

// A fill reads the cache through forks of its own, ranked at the converted subscriber
// priority. Copied unconverted, wire 100 would raise the track's aggregate from 27 to 100,
// and wire 191 is outside the model's -128..=127, so the fork throws and the fill never runs.
test("draft-22: a SUBSCRIBE's fill reads the cache at the converted subscriber priority", async () => {
const fx = fixture();
const track = fx.broadcast.createTrack("video", { timescale: Timescale.MILLI });
writeGroup(track, 2);
const { client } = await runSubscribe(
fx,
new Subscribe({
requestId: 0n,
trackNamespace: Path.from("test"),
trackName: "video",
subscriberPriority: 191,
filter: { kind: "absolute", startGroup: 10n, startObject: 0n },
fill: { filter: { kind: "unfiltered" }, rangeFilters: false },
}),
);
try {
expect(track.subscription.peek()?.priority).toBe(-64);
const stream = await nextUni(fx.uni);
if (!stream) throw new Error("missing fill stream");
const served = await readFill(new Reader(stream, undefined, V22), "ascending");
expect(served.reset).toBeUndefined();
expect(served.objects).toEqual([0, 1].map((id) => ({ group: 0, id, payload: `0.${id}` })));
} finally {
client.close();
track.finish();
track.close();
fx.close();
}
});

test("draft-22: an update's fill reads the cache at the converted subscriber priority", async () => {
for (const [wire, model] of [
[100, 27],
[191, -64],
]) {
const fx = fixture();
const parked = Promise.withResolvers<void>();
const stalled = new WritableStream<Uint8Array>({
write() {
parked.resolve();
return new Promise<void>(() => {});
},
});
const track = fx.broadcast.createTrack("video", { timescale: Timescale.MILLI });
writeGroup(track, 1);
const { client } = await runSubscribe(
fx,
new Subscribe({
requestId: 0n,
trackNamespace: Path.from("test"),
trackName: "video",
subscriberPriority: wire,
filter: { kind: "absolute", startGroup: 10n, startObject: 0n },
}),
);
try {
expect(track.subscription.peek()?.priority).toBe(model);
spyOn(fx.pair.server, "createUnidirectionalStream").mockImplementationOnce(async () => stalled);
const parameters = new Parameters();
parameters.fillParameters = Filter.encodeFill(
{ filter: { kind: "unfiltered" }, groupOrder: "ascending", rangeFilters: false },
fx.version,
);
await client.writer.u53(SubscribeUpdate.id);
await new SubscribeUpdate({ requestId: 2n, parameters }).encode(client.writer, fx.version);
expect(await client.reader.u53()).toBe(RequestOk.id);
await RequestOk.decode(client.reader, fx.version);
// The fill is parked writing its header, so the fork it reads through is still live.
await parked.promise;
expect(track.subscription.peek()?.priority, `wire ${wire}`).toBe(model);
} finally {
client.close();
track.finish();
track.close();
fx.close();
}
}
});

test("draft-22: narrowing gives up an excluded group's open stream", async () => {
const fx = fixture();
const track = fx.broadcast.createTrack("video", { timescale: Timescale.MILLI });
Expand Down
Loading
Loading