Skip to content
Merged
5 changes: 3 additions & 2 deletions js/hang/src/container/consumer.backpressure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,11 +461,12 @@ test("Consumer raises a malformed group an age skip passes after the track finis
expect(await within(consumer.next())).toMatchObject({ kind: "frame", group: 0n });
await flush();

// Group 1 holds a valid frame then a malformed one, and group 2 starts past the max delay, so the
// next read's decode skips group 1 for its age.
// Group 1 holds a valid frame then a malformed one, and group 2 starts a whole max delay behind
// its own newest frame, so the next read's decode skips group 1 for its age.
write(malformed, 10_000);
malformed.writeFrame({ payload: new Uint8Array([0x80]), timestamp: Time.Timestamp.now() });
write(later, 2_000_000);
write(later, 2_500_000);
await expect(within(consumer.next())).rejects.toThrow("buffer too short");
} finally {
consumer.close();
Expand Down
101 changes: 87 additions & 14 deletions js/hang/src/container/consumer.holes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ test.each([0, 100])("a sequence hole before a paused data marker expires at %sms
consumer.close();
track.finish();
track.close();
await reading;
installed.close();
try {
await reading;
} finally {
installed.close();
}
}
});

Expand Down Expand Up @@ -97,8 +100,11 @@ test("a filled hole cancels its timer and the next hole gets a fresh budget", as
consumer.close();
track.finish();
track.close();
await reading;
installed.close();
try {
await reading;
} finally {
installed.close();
}
}
});

Expand Down Expand Up @@ -138,8 +144,11 @@ test("the holes a stalled source left behind share one budget", async () => {
consumer.close();
track.finish();
track.close();
await reading;
installed.close();
try {
await reading;
} finally {
installed.close();
}
}
});

Expand Down Expand Up @@ -182,8 +191,11 @@ test("a hole is late from the earliest arrival past it", async () => {
consumer.close();
track.finish();
track.close();
await reading;
installed.close();
try {
await reading;
} finally {
installed.close();
}
}
});

Expand Down Expand Up @@ -234,8 +246,11 @@ test("a hole in a drained backlog is late from the earliest arrival past it, not
consumer.close();
track.finish();
track.close();
await reading;
installed.close();
try {
await reading;
} finally {
installed.close();
}
}
});

Expand Down Expand Up @@ -286,12 +301,13 @@ test.each([true, false])("the head after a declared end is waited for (arrives:
write(2n, [60_000_000]);
await flush();
expect(results.slice(3)).toEqual([frame(2n, 60_000_000), frame(3n, 60_060_000)]);
// Resumed, a later hole past the budget is proven again without a wait.
write(5n, [61_000_000]);
// Resumed, a later hole whose head starts a budget behind the newest is proven without a wait.
write(5n, [61_000_000], [61_300_000]);
await flush();
expect(results.slice(5)).toEqual([
{ kind: "gap", timestamp: Micro(60_060_000), duration: Micro(940_000) },
frame(5n, 61_000_000),
frame(5n, 61_300_000),
]);
} else {
await clock.advance(Nano.fromMilli(Milli(1)));
Expand All @@ -304,7 +320,64 @@ test.each([true, false])("the head after a declared end is waited for (arrives:
consumer.close();
track.finish();
track.close();
await reading;
installed.close();
try {
await reading;
} finally {
installed.close();
}
}
});

// Groups go out newest first, so a flush's newest group lands just ahead of the rest. Where
// presentation left off is a budget behind it, but the missing groups end where it starts, so they
// may still play: skipping the hole would drop them on arrival, below the cursor.
test("a newest-first flush past a hole delivers the groups landing behind its newest", async () => {
// Time stands still while the flush lands, so only the timestamps can prove the hole.
const clock = new Manual();
const installed = clock.install();
const track = new Track.Producer("audio");
const consumer = new Consumer(track.subscribe({ delay: { max: Milli(30_000) } }), {
format: new Format("audio"),
delay: { max: Milli(80) },
});
const results: Read[] = [];
const reading = (async () => {
for (;;) {
const read = await consumer.next();
if (!read) return;
results.push(read);
}
})();
// One 20ms frame per group, as a voice publisher writes them, all sent at once.
const sent = Time.Timestamp.now();
const write = (sequence: number) => {
const group = track.createGroup(BigInt(sequence));
const timestamp = Micro(sequence * 20_000);
group.writeFrame({ payload: encodeFrame(new Uint8Array([1]), timestamp), timestamp: sent });
group.finish();
};
try {
for (const sequence of [0, 1, 2]) write(sequence);
await flush();
write(7);
await flush();
for (const sequence of [3, 4, 5, 6]) write(sequence);
await flush();
expect(results).toEqual(
[0, 1, 2, 3, 4, 5, 6, 7].map((sequence) => ({
kind: "frame",
group: BigInt(sequence),
frame: expect.objectContaining({ timestamp: Micro(sequence * 20_000) }),
})),
);
} finally {
consumer.close();
track.finish();
track.close();
try {
await reading;
} finally {
installed.close();
}
}
});
79 changes: 75 additions & 4 deletions js/hang/src/container/consumer.skips.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Signal } from "@moq/signals";
import { Consumer } from "./consumer.ts";
import { Format } from "./legacy.ts";

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

for (const count of [1, 16, 256]) {
test(`Consumer summarizes ${count} skipped groups once per catch-up`, async () => {
const warn = spyOn(console, "warn").mockImplementation(() => {});
Expand Down Expand Up @@ -165,8 +167,8 @@ test("Consumer measures each skip's span over every group left, not just the new
] as const)
write(sequence, timestamp);
await buffered(12);
// Reading the first frame warms the reader, so the age skip runs on the max delay alone.
expect(await consumer.next()).toMatchObject({ kind: "frame", group: 0n });
// Before the first read, the span alone ages a group out: once reading began, its successor's
// start bounds it instead, which the span always reaches past.
delay.set({ max: Milli(5) });
write(4n, 13_000);
await buffered(13);
Expand All @@ -175,8 +177,7 @@ test("Consumer measures each skip's span over every group left, not just the new
{ start: Milli(12), end: Milli(12) },
{ start: Milli(13), end: Milli(13) },
]);
// The skipped groups held media past the first frame, so their span reads as a gap first.
expect(await consumer.next()).toMatchObject({ kind: "gap", timestamp: 0, duration: 12_000 });
// Joining mid-track is not a gap: nothing was read before it.
expect(await consumer.next()).toMatchObject({ kind: "frame", group: 3n });
} finally {
consumer.close();
Expand All @@ -185,3 +186,73 @@ test("Consumer measures each skip's span over every group left, not just the new
warn.mockRestore();
}
});

// An open group blocks until its successor starts a whole budget behind the newest content, not until
// its own first frame does: the skip moves the cursor past the groups still in flight between, which
// a group arriving below the cursor never delivers.
test("a blocking group is kept until its successor starts a budget behind the newest", async () => {
const warn = spyOn(console, "warn").mockImplementation(() => {});
const track = new Track.Producer("tone");
const consumer = new Consumer(track.subscribe({ delay: { max: Milli(30_000) } }), {
format: new Format("data"),
delay: { max: Milli(150) },
});
const sent = Time.Timestamp.now();
const write = (sequence: bigint, timestamp: number) => {
const group = track.createGroup(sequence);
group.writeFrame({ payload: Varint.encode(timestamp), timestamp: sent });
return group;
};
try {
const blocking = write(0n, 0);
expect(await consumer.next()).toMatchObject({ kind: "frame", group: 0n });
// Group 3's stream wins the race, 160ms past group 0's first frame.
write(3n, 160_000).finish();
await flush();
write(1n, 100_000).finish();
write(2n, 130_000).finish();
blocking.finish();
const reads = [await consumer.next(), await consumer.next(), await consumer.next()];
expect(reads).toMatchObject([
{ kind: "frame", group: 1n },
{ kind: "frame", group: 2n },
{ kind: "frame", group: 3n },
]);
expect(warn).not.toHaveBeenCalled();
} finally {
consumer.close();
track.finish();
track.close();
warn.mockRestore();
}
});

// A group with no frame says nothing about the timeline, so the age bound is the first stamped group
// past the head, and the unstamped one between ages out with it.
test("a blocking group ages out past an unstamped successor", async () => {
const warn = spyOn(console, "warn").mockImplementation(() => {});
const track = new Track.Producer("tone");
const consumer = new Consumer(track.subscribe({ delay: { max: Milli(30_000) } }), {
format: new Format("data"),
delay: { max: Milli(150) },
});
const sent = Time.Timestamp.now();
try {
const blocking = track.createGroup(0n);
blocking.writeFrame({ payload: Varint.encode(0), timestamp: sent });
expect(await consumer.next()).toMatchObject({ kind: "frame", group: 0n });
track.createGroup(1n).finish();
const later = track.createGroup(2n);
for (const timestamp of [1_000_000, 2_000_000])
later.writeFrame({ payload: Varint.encode(timestamp), timestamp: sent });
await flush();
expect(await consumer.next()).toMatchObject({ kind: "gap" });
expect(await consumer.next()).toMatchObject({ kind: "frame", group: 2n });
blocking.finish();
} finally {
consumer.close();
track.finish();
track.close();
warn.mockRestore();
}
});
18 changes: 12 additions & 6 deletions js/hang/src/container/consumer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1355,8 +1355,9 @@ test("Consumer reads a gap when retiring an open group", async () => {
await flush();
expect(frameOf(await consumer.next()).timestamp).toBe(0 as Micro);

// Group 1 starts a whole budget behind the newest content, so the open group is retired.
writeGroupWithLegacyFrames(track, 1n, [100_000 as Micro]);
writeGroupWithLegacyFrames(track, 2n, [200_000 as Micro]);
writeGroupWithLegacyFrames(track, 2n, [250_000 as Micro]);
await flush();

// Legacy carries no duration, so the gap starts at the last frame read.
Expand Down Expand Up @@ -1733,9 +1734,9 @@ test("Consumer delivers a contiguous group after one that completed out of order
consumer.close();
});

// A resubscribe can replay a stale group, lose the group after it, then carry on live. Once the live
// head reaches past where presentation left off by more than the budget, whatever is missing would
// arrive too old to play, so the head is the group to play next and must not be dropped as "slow".
// A resubscribe can replay a stale group, lose the group after it, then carry on live. Once the head
// past the hole starts a whole budget behind the newest content, whatever is missing would arrive too
// old to play, so the head is the group to play next and must not be dropped as "slow".
test("Consumer plays the head once a waited-out gap exceeds the budget (Legacy)", async () => {
const track = new Track.Producer("test");
const consumer = new Consumer(replay(track), {
Expand All @@ -1752,10 +1753,15 @@ test("Consumer plays the head once a waited-out gap exceeds the budget (Legacy)"
// The reader is parked on the missing 1001, as a decoder is while it waits for the next group.
const pending = consumer.next();

// B (seq 2000) starts at 90_000 ticks (1_000_000µs), a second past A's end: the missing group
// could only ever arrive far beyond the 100ms budget, so B plays from its first frame.
// B (seq 2000) starts at 90_000 ticks (1_000_000µs), a second past A's end. The missing groups end
// where B starts, so B alone proves nothing: one may hold media just before it.
const b = track.createGroup(2000n);
b.writeFrame(timedFrame(0x02, 90_000, 1));
expect(await poll(pending)).toBe("pending");

// C (seq 2001) carries on live 200ms past B's start, so anything missing below B is too old to play.
const c = track.createGroup(2001n);
c.writeFrame(timedFrame(0x03, 108_000, 2));

// The missing group's span was never delivered, so a gap from A's end to B comes first.
const result = await poll(pending);
Expand Down
Loading
Loading