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
104 changes: 104 additions & 0 deletions js/hang/src/container/consumer.holes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { expect, test } from "bun:test";
import { Manual, Micro, Milli, Nano } from "@moq/clock";
import { Group, Time, Track } from "@moq/net";
import { Consumer, type Read } from "./consumer.ts";
import { encodeFrame, Format } from "./legacy.ts";

const flush = () => new Promise<void>((resolve) => setImmediate(resolve));

test.each([0, 100])("a sequence hole before a paused data marker expires at %sms", async (budget) => {
const clock = new Manual();
const restore = clock.install();
const track = new Track.Producer("data");
const consumer = new Consumer(track.subscribe({ maxDelay: Milli(30_000) }), {
format: new Format("data"),
maxDelay: Milli(budget),
});
const results: Read[] = [];
const reading = (async () => {
for (;;) {
const read = await consumer.next();
if (!read) return;
results.push(read);
}
})();
try {
const first = new Group.Producer(0);
track.writeGroup(first);
first.writeFrame({ payload: encodeFrame(new Uint8Array([1]), Micro(10_000)), timestamp: Time.Timestamp.now() });
first.close();
await flush();
expect(results).toHaveLength(1);
const marker = new Group.Producer(2);
track.writeGroup(marker);
marker.writeFrame({ payload: new Uint8Array(), timestamp: Time.Timestamp.now() });
marker.close();
await flush();
if (budget > 0) {
expect(results).toHaveLength(1);
await clock.advanceAsync(Nano.fromMilli(Milli(budget - 1)));
expect(results).toHaveLength(1);
await clock.advanceAsync(Nano.fromMilli(Milli(1)));
}
expect(results.slice(1)).toEqual([
{ kind: "gap", timestamp: Micro(10_000), duration: Micro.zero },
{ kind: "end", group: 2, end: Micro(10_000) },
]);
} finally {
consumer.close();
track.close();
await reading;
restore();
}
});

test("a filled hole cancels its timer and the next hole gets a fresh budget", async () => {
const clock = new Manual();
const restore = clock.install();
const track = new Track.Producer("data");
const consumer = new Consumer(track.subscribe({ maxDelay: Milli(30_000) }), {
format: new Format("data"),
maxDelay: Milli(100),
});
const results: Read[] = [];
const reading = (async () => {
for (;;) {
const read = await consumer.next();
if (!read) return;
results.push(read);
}
})();
const write = (sequence: number, timestamp?: number) => {
const group = new Group.Producer(sequence);
track.writeGroup(group);
group.writeFrame({
payload: timestamp === undefined ? new Uint8Array() : encodeFrame(new Uint8Array([1]), Micro(timestamp)),
timestamp: Time.Timestamp.fromMicros(timestamp ?? 20_000),
});
group.close();
};
try {
write(0, 0);
await flush();
write(2, 20_000);
await flush();
await clock.advanceAsync(Nano.fromMilli(Milli(50)));
write(1, 10_000);
await flush();
expect(results.map((read) => read.kind)).toEqual(["frame", "frame", "frame"]);
write(4);
await flush();
await clock.advanceAsync(Nano.fromMilli(Milli(99)));
expect(results).toHaveLength(3);
await clock.advanceAsync(Nano.fromMilli(Milli(1)));
expect(results.slice(3)).toEqual([
{ kind: "gap", timestamp: Micro(20_000), duration: Micro.zero },
{ kind: "end", group: 4, end: Micro(20_000) },
]);
} finally {
consumer.close();
track.close();
await reading;
restore();
}
});
41 changes: 35 additions & 6 deletions js/hang/src/container/consumer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Micro, Milli, Nano, now } from "@moq/clock";
import { Instant, Micro, Milli, Nano, now } from "@moq/clock";
import * as Moq from "@moq/net";
import { Effect, type Getter, type GetterInit, getter, Once, Signal } from "@moq/signals";
import type { Format } from "./format";
Expand All @@ -13,7 +13,9 @@ export interface ConsumerProps {
* How stale a group may get before it is skipped, in milliseconds (default: 0).
*
* Measured as the span from the oldest buffered frame to the newest, so it bounds how long a
* late or missing group is waited for. The local half of the subscription's
* late group is waited for. A missing sequence also expires after this much monotonic
* time blocked on it, even when the next group is a marker on a paused track.
* The local half of the subscription's
* `maxDelay`; both measure the same budget, one on the wire and one as frames are read.
*/
// Read-only: a Getter (e.g. another component's output) is accepted directly.
Expand Down Expand Up @@ -103,6 +105,7 @@ export class Consumer {
#maxDelay: Getter<Milli>;
#groups: Group[] = [];
#active?: number; // the active group sequence number
#hole?: { sequence: number; since: Instant };
// Presentation end (max PTS + duration) of the group we most recently advanced past, so next()'s
// promotion guard can tell a timeline-continuous next group from one sitting after a gap.
// Maintained only via #recordPresented; see its comment for the invariant.
Expand Down Expand Up @@ -501,6 +504,9 @@ export class Consumer {
if (this.#error) throw this.#error;

const ended = this.#closed.peek();
if (this.#groups[0]?.consumer.sequence === this.#active || this.#groups.length === 0) {
this.#hole = undefined;
}
Comment thread
kixelated marked this conversation as resolved.
Resolved
if (this.#groups.length === 0) {
if (ended !== undefined) {
if (ended instanceof Error) throw ended;
Expand All @@ -520,11 +526,14 @@ export class Consumer {
// max delay, so anything still missing in between would arrive too old to play. Proving it
// here matters: #checkMaxDelay would instead drop the head, the very group to play next.
// After track termination no missing group can arrive, so drain across any remaining gap.
// Otherwise wait: #checkMaxDelay skips once the budget is spent, and #tryDurationSkip once
// the duration covers it.
// A missing sequence also expires on elapsed monotonic time, so timestamp-free markers
// behind a hole can end a paused track without more arrivals.
if (this.#active !== undefined && this.#groups.length > 0) {
const head = this.#groups[0];
if (head.consumer.sequence > this.#active) {
if (this.#hole?.sequence !== this.#active) {
this.#hole = { sequence: this.#active, since: now() };
}
const startup = this.#start === undefined;
const contiguous = ptsContiguous(this.#presentedEnd, head.frames.at(0)?.timestamp);
const empty = head.empty && head.consumer.done;
Expand All @@ -535,9 +544,18 @@ export class Consumer {
(this.#presentedEnd !== undefined &&
head.latest !== undefined &&
head.latest - this.#presentedEnd > maxDelay));
if (startup || empty || contiguous || skipHole || ended !== undefined) {
if (!contiguous && !empty) this.#lost = true;
const elapsed = Instant.durationSince(now(), this.#hole.since) >= Nano.fromMicro(maxDelay);
if (startup || empty || contiguous || skipHole || elapsed || ended !== undefined) {
if (!contiguous && !empty) {
this.#lost = true;
if (!startup) {
console.warn(
`skipping missing groups: track=${this.#track.name} ${this.#active} -> ${head.consumer.sequence}`,
);
}
}
this.#active = head.consumer.sequence;
this.#hole = undefined;
}
}
}
Expand Down Expand Up @@ -621,6 +639,16 @@ export class Consumer {

const abort = this.#signals.abort;
if (abort.aborted) return undefined;
let timer: Effect | undefined;
if (this.#hole) {
timer = new Effect();
const since = this.#hole.since;
timer.run((effect) => {
const budget = Nano.fromMilli(effect.get(this.#maxDelay));
const elapsed = Instant.durationSince(now(), since);
effect.timer(() => this.#notify?.(), Nano(Math.max(0, budget - elapsed)));
});
}

const aborted = await new Promise<boolean>((resolve) => {
const onAbort = () => resolve(true);
Expand All @@ -630,6 +658,7 @@ export class Consumer {
resolve(false);
};
});
timer?.close();

this.#notify = undefined;
if (aborted) return undefined;
Expand Down
1 change: 0 additions & 1 deletion quest/m1/ts-reliability/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ remain with their current quests.
## Required

- [Plan: semantic fidelity](/quest/m1/ts-reliability/fidelity.md) - explicit profiles and PMT lifecycle contracts produce implementation quests
- [Sequence holes get a time budget](/quest/m1/ts-reliability/hole-budget.md) - a hole gives up on max(wall, pts), so a break marker behind it on a paused track is delivered
- [Loss and recovery tests](/quest/m1/ts-reliability/recovery.md) - actual MoQ omissions exercise reacquisition and unaffected tracks

## Related
Expand Down
44 changes: 0 additions & 44 deletions quest/m1/ts-reliability/hole-budget.md

This file was deleted.

Loading
Loading