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
56 changes: 34 additions & 22 deletions js/hang/src/container/consumer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ function settle(ms = 20): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

// Keep transport drift filtering out of container-consumer tests. These cases
// exercise the consumer's own latency policy against a complete retained track.
function subscribeAll(track: Track.Producer): Track.Subscriber {
return track.subscribe({ latencyMax: 30_000 });
}

// --- LegacyFormat ---

test("LegacyFormat decodes a valid frame", () => {
Expand Down Expand Up @@ -183,7 +189,7 @@ async function drainFrames(

test("Consumer delivers frames from a single group", async () => {
const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), { format: new LegacyFormat(), latency: 500 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), { format: new LegacyFormat(), latency: 500 as Time.Milli });

writeGroupWithLegacyFrames(track, 0, [0 as Time.Micro, 33_000 as Time.Micro]);
track.close();
Expand All @@ -197,7 +203,7 @@ test("Consumer delivers frames from a single group", async () => {

test("Consumer forces keyframe true at index 0", async () => {
const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), { format: new LegacyFormat(), latency: 500 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), { format: new LegacyFormat(), latency: 500 as Time.Milli });

writeGroupWithLegacyFrames(track, 0, [0 as Time.Micro, 33_000 as Time.Micro]);
track.close();
Expand All @@ -221,7 +227,7 @@ test("Consumer index spans MoQ frames for keyframe detection", async () => {
};

const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), { format: multiFormat, latency: 500 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), { format: multiFormat, latency: 500 as Time.Milli });

const group = new Group.Producer(0);
group.writeFrame({ payload: new Uint8Array([0x01]), timestamp: Time.Timestamp.now() }); // first MoQ frame → 3 samples
Expand Down Expand Up @@ -249,7 +255,7 @@ test("Consumer keeps frames decoded before an error (truncated GoP)", async () =
};

const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), { format: truncatingFormat, latency: 500 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), { format: truncatingFormat, latency: 500 as Time.Milli });

// Group.Producer 0: 2 valid frames then a tail-truncating error.
const g0 = new Group.Producer(0);
Expand All @@ -276,7 +282,7 @@ test("Consumer keeps frames decoded before an error (truncated GoP)", async () =

test("Consumer close returns undefined from next()", async () => {
const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), { format: new LegacyFormat(), latency: 500 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), { format: new LegacyFormat(), latency: 500 as Time.Milli });

const promise = consumer.next();
consumer.close();
Expand All @@ -287,7 +293,7 @@ test("Consumer close returns undefined from next()", async () => {

test("Consumer throws on concurrent next() calls", async () => {
const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), { format: new LegacyFormat(), latency: 500 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), { format: new LegacyFormat(), latency: 500 as Time.Milli });

// First call blocks waiting for data
void consumer.next();
Expand All @@ -300,7 +306,7 @@ test("Consumer throws on concurrent next() calls", async () => {
test("Consumer skips groups via PTS-span when over latency", async () => {
const track = new Track.Producer("test");
// Zero latency = skip everything that's not the latest
const consumer = new Consumer(track.subscribe(), { format: new LegacyFormat(), latency: 0 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), { format: new LegacyFormat(), latency: 0 as Time.Milli });

// Write groups with increasing timestamps. With 0 latency, any PTS span > 0 triggers skip.
writeGroupWithLegacyFrames(track, 0, [0 as Time.Micro]);
Expand All @@ -319,7 +325,7 @@ test("Consumer skips groups via PTS-span when over latency", async () => {

test("Consumer delivers groups in sequence order regardless of arrival order", async () => {
const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), { format: new LegacyFormat(), latency: 500 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), { format: new LegacyFormat(), latency: 500 as Time.Milli });

writeGroupWithLegacyFrames(track, 2, [60_000 as Time.Micro]);
writeGroupWithLegacyFrames(track, 0, [0 as Time.Micro]);
Expand All @@ -338,7 +344,7 @@ test("Consumer delivers groups in sequence order regardless of arrival order", a

test("Consumer rejects stale groups", async () => {
const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), { format: new LegacyFormat(), latency: 500 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), { format: new LegacyFormat(), latency: 500 as Time.Milli });

// Group.Producer 5 arrives first (sets active = 5)
writeGroupWithLegacyFrames(track, 5, [0 as Time.Micro]);
Expand All @@ -363,7 +369,7 @@ test("Consumer rejects stale groups", async () => {

test("Consumer next() returns group-done signals", async () => {
const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), { format: new LegacyFormat(), latency: 500 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), { format: new LegacyFormat(), latency: 500 as Time.Milli });

writeGroupWithLegacyFrames(track, 0, [0 as Time.Micro, 33_000 as Time.Micro]);
writeGroupWithLegacyFrames(track, 1, [66_000 as Time.Micro]);
Expand Down Expand Up @@ -394,7 +400,7 @@ test("Consumer next() returns group-done signals", async () => {

test("Consumer buffered signal updates as frames arrive", async () => {
const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), { format: new LegacyFormat(), latency: 500 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), { format: new LegacyFormat(), latency: 500 as Time.Milli });

expect(consumer.buffered.peek()).toEqual([]);

Expand All @@ -416,7 +422,7 @@ test("Consumer buffered signal updates as frames arrive", async () => {

test("Consumer recovers from gap in group sequence numbers", async () => {
const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), { format: new LegacyFormat(), latency: 100 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), { format: new LegacyFormat(), latency: 100 as Time.Milli });

writeGroupWithLegacyFrames(track, 0, [0 as Time.Micro, 20_000 as Time.Micro]);
writeGroupWithLegacyFrames(track, 1, [40_000 as Time.Micro, 60_000 as Time.Micro]);
Expand Down Expand Up @@ -446,7 +452,7 @@ test("Consumer handles empty decode result without deadlock", async () => {
};

const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), { format: emptyThenValid, latency: 500 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), { format: emptyThenValid, latency: 500 as Time.Milli });

const group = new Group.Producer(0);
group.writeFrame({ payload: new Uint8Array([0x01]), timestamp: Time.Timestamp.now() }); // empty decode
Expand All @@ -469,7 +475,7 @@ test("Consumer handles empty decode result without deadlock", async () => {

test("Consumer with CmafFormat delivers correct timestamps", async () => {
const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), {
const consumer = new Consumer(subscribeAll(track), {
format: new CmafFormat(TEST_INIT),
latency: 500 as Time.Milli,
});
Expand Down Expand Up @@ -542,7 +548,7 @@ const durationFormat: ContainerFormat = {
test("Consumer duration-skips a stalled group once it is covered", async () => {
const track = new Track.Producer("test");
// Latency dwarfs the gap, so only duration coverage can trigger the skip.
const consumer = new Consumer(track.subscribe(), { format: durationFormat, latency: 10_000 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), { format: durationFormat, latency: 10_000 as Time.Milli });

// Group.Producer 0: one frame at ts=0 lasting 33ms, never closed (stalled).
const g0 = new Group.Producer(0);
Expand Down Expand Up @@ -579,7 +585,7 @@ test("Consumer does not duration-skip when the gap is not covered", async () =>
};

const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), { format: shortFormat, latency: 10_000 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), { format: shortFormat, latency: 10_000 as Time.Milli });

// Group.Producer 0 stays open and later receives a second frame; nothing covers the gap,
// so that late frame must survive rather than being skipped.
Expand Down Expand Up @@ -612,7 +618,10 @@ test("Consumer does not duration-skip when the gap is not covered", async () =>
// surface immediately, even while still open.
test("Consumer delivers a PTS-contiguous next group whose sequence jumped (CMAF)", async () => {
const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), { format: new CmafFormat(TEST_INIT), latency: 500 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), {
format: new CmafFormat(TEST_INIT),
latency: 500 as Time.Milli,
});

// Group A (seq 1_000_000): one 3000-tick sample, so its content ends at 33_333µs (3000/90000 * 1e6).
const a = new Group.Producer(1_000_000);
Expand Down Expand Up @@ -668,7 +677,7 @@ test("Consumer delivers a PTS-contiguous next group whose sequence jumped (CMAF)
test("Consumer waits on a PTS gap instead of skipping to a later buffered group (CMAF)", async () => {
const track = new Track.Producer("test");
// Large latency so the gap can't be latency-skipped during the test window.
const consumer = new Consumer(track.subscribe(), {
const consumer = new Consumer(subscribeAll(track), {
format: new CmafFormat(TEST_INIT),
latency: 10_000 as Time.Milli,
});
Expand Down Expand Up @@ -742,7 +751,7 @@ test("Consumer waits on a PTS gap instead of skipping to a later buffered group
// blocks every later contiguous group.
test("Consumer delivers a contiguous group after one that completed out of order (CMAF)", async () => {
const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), {
const consumer = new Consumer(subscribeAll(track), {
format: new CmafFormat(TEST_INIT),
latency: 10_000 as Time.Milli,
});
Expand Down Expand Up @@ -790,7 +799,10 @@ test("Consumer delivers a contiguous group after one that completed out of order
// that check is the only thing left that can break the stall.
test("Consumer latency-skips a waited-out gap when only the head receives frames (CMAF)", async () => {
const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), { format: new CmafFormat(TEST_INIT), latency: 100 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), {
format: new CmafFormat(TEST_INIT),
latency: 100 as Time.Milli,
});

// A (seq 1000): one frame, ends at 3000 ticks (33_333µs).
const a = new Group.Producer(1000);
Expand Down Expand Up @@ -840,7 +852,7 @@ test("Consumer latency-skips a waited-out gap when only the head receives frames
// sequential, and adjacency doesn't rule out a group the latency check dropped on the way past.
test("Consumer reports continuity while nothing is dropped", async () => {
const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), { format: new LegacyFormat(), latency: 100 as Time.Milli });
const consumer = new Consumer(subscribeAll(track), { format: new LegacyFormat(), latency: 100 as Time.Milli });

writeGroupWithLegacyFrames(track, 0, [0 as Time.Micro, 33_000 as Time.Micro]);
await settle();
Expand All @@ -859,7 +871,7 @@ test("Consumer reports continuity while nothing is dropped", async () => {
// The case the group-number heuristic got backwards: ids jump, but the PTS timeline is unbroken.
test("Consumer reports continuity across a PTS-contiguous group id jump (CMAF)", async () => {
const track = new Track.Producer("test");
const consumer = new Consumer(track.subscribe(), {
const consumer = new Consumer(subscribeAll(track), {
format: new CmafFormat(TEST_INIT),
latency: 10_000 as Time.Milli,
});
Expand Down
5 changes: 3 additions & 2 deletions js/json/src/snapshot/compression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Value = Record<string, unknown>;

const enc = new TextEncoder();
const dec = new TextDecoder();
const REPLAY_LATENCY = 30_000;

// Reconstruct every value a compressed consumer yields, in order.
async function drainCompressed(track: Track.Subscriber): Promise<Value[]> {
Expand Down Expand Up @@ -44,7 +45,7 @@ test("compressed snapshot per group round-trips", async () => {
producer.finish();

// Deltas off: one compressed snapshot per group, reconstructed in order.
expect(await drainCompressed(track.subscribe())).toEqual([{ a: 1 }, { a: 2 }]);
expect(await drainCompressed(track.subscribe({ latencyMax: REPLAY_LATENCY }))).toEqual([{ a: 1 }, { a: 2 }]);
});

test("compressed live consumer sees each update in order", async () => {
Expand Down Expand Up @@ -139,7 +140,7 @@ test("compressed deltas roll on the compressed budget", async () => {
};

const layout = new Track.Producer("layout");
const layoutSub = layout.subscribe();
const layoutSub = layout.subscribe({ latencyMax: REPLAY_LATENCY });
fill(layout);
expect(await groupCount(layoutSub)).toBeGreaterThan(1);

Expand Down
14 changes: 9 additions & 5 deletions js/json/src/snapshot/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { Producer } from "./producer.ts";

type Value = Record<string, unknown>;

// These tests inspect complete finished timelines, so request a replay window
// instead of the transport's live-edge default.
const REPLAY_LATENCY = 30_000;

// Reconstruct every value a consumer yields, in order.
async function drain(track: Track.Subscriber): Promise<Value[]> {
const out: Value[] = [];
Expand Down Expand Up @@ -35,7 +39,7 @@ test("deltas off: a snapshot group per change", async () => {
producer.finish();

// Two changes => two single-frame snapshot groups, reconstructed in order.
expect(await drain(track.subscribe())).toEqual([{ a: 1 }, { a: 2 }]);
expect(await drain(track.subscribe({ latencyMax: REPLAY_LATENCY }))).toEqual([{ a: 1 }, { a: 2 }]);
});

test("deltaRatio 0 disables deltas, like off", async () => {
Expand All @@ -47,7 +51,7 @@ test("deltaRatio 0 disables deltas, like off", async () => {

// `0` is treated as off, not a degenerate "enabled" value that keeps the group open: each change
// is its own single-frame snapshot group.
expect(await structure(track.subscribe())).toEqual([1, 1]);
expect(await structure(track.subscribe({ latencyMax: REPLAY_LATENCY }))).toEqual([1, 1]);
});

test("live consumer sees each update", async () => {
Expand Down Expand Up @@ -161,7 +165,7 @@ test("tight ratio rolls snapshots", async () => {
producer.update({ a: 4 }); // budget already exceeded, rolls group 1
producer.finish();

expect(await structure(track.subscribe())).toEqual([3, 1]);
expect(await structure(track.subscribe({ latencyMax: REPLAY_LATENCY }))).toEqual([3, 1]);
});

test("deltas stay within ratio times snapshot", async () => {
Expand All @@ -175,7 +179,7 @@ test("deltas stay within ratio times snapshot", async () => {
for (let n = 0; n <= 10; n++) producer.update({ n });
producer.finish();

expect(await structure(track.subscribe())).toEqual([10, 1]);
expect(await structure(track.subscribe({ latencyMax: REPLAY_LATENCY }))).toEqual([10, 1]);
});

test("array change is a wholesale delta", async () => {
Expand Down Expand Up @@ -212,5 +216,5 @@ test("frame cap rolls snapshot", async () => {
}
producer.finish();

expect(await structure(track.subscribe())).toEqual([256, 1]);
expect(await structure(track.subscribe({ latencyMax: REPLAY_LATENCY }))).toEqual([256, 1]);
});
6 changes: 3 additions & 3 deletions js/net/src/broadcast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ test("two subscribers to one inserted track each get a full copy", async () => {
const broadcast = new BroadcastProducer();
const producer = broadcast.createTrack("video");

const a = broadcast.track("video").subscribe();
const b = broadcast.track("video").subscribe();
const a = broadcast.track("video").subscribe({ latencyMax: 5000 });
const b = broadcast.track("video").subscribe({ latencyMax: 5000 });

producer.writeString("hello");
producer.writeString("world");
Expand Down Expand Up @@ -189,7 +189,7 @@ test("a late subscriber replays the cached window", async () => {
test("a read throws Lagged on a gap, then resyncs to the next group", async () => {
const broadcast = new BroadcastProducer();
const producer = broadcast.createTrack("video");
const sub = broadcast.track("video").subscribe();
const sub = broadcast.track("video").subscribe({ latencyMax: 5000 });

// Group 0 overflows its frame cap without being read, evicting the front: a gap.
const g0 = producer.appendGroup();
Expand Down
1 change: 1 addition & 0 deletions js/net/src/broadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ async function fetchGroup(
options: track.FetchGroupOptions = {},
): Promise<GroupConsumer> {
const subscriber = subscribe(state, name, { priority: options.priority });
hooks.ignoreLatency(subscriber);
try {
for (;;) {
const group = await subscriber.recvGroup();
Expand Down
Loading
Loading