Skip to content
Open
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
12 changes: 6 additions & 6 deletions js/e2ee/src/e2ee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ test("grouped authentication failure ends the track", async () => {
group.finish();

// A forged second group, written straight to the net track.
const bad = p.net.createGroup(1n);
const bad = defined(p.net.createGroup(1n));
bad.writeFrame({ timestamp: ms(2), payload: await sealed(p, GROUP, 1n, 0, "ok", true) });
bad.finish();

Expand All @@ -439,9 +439,9 @@ test("grouped failure wakes pending reads and releases demand", async () => {
expect(demand.wanted.peek()).toBe(true);

// One authentic frame, then silence. The group and the track stay open.
const open = p.net.createGroup(0n);
const open = defined(p.net.createGroup(0n));
open.writeFrame({ timestamp: ms(1), payload: await sealed(p, GROUP, 0n, 0, "ok") });
const bad = p.net.createGroup(1n);
const bad = defined(p.net.createGroup(1n));
const badDemand = bad.demand();
bad.writeFrame({ timestamp: ms(2), payload: await sealed(p, GROUP, 1n, 0, "nope", true) });

Expand Down Expand Up @@ -469,7 +469,7 @@ test("grouped failure wakes pending reads and releases demand", async () => {

test("a grouped failure leaves the independent datagram track readable", async () => {
const p = await pair("audio");
const bad = p.net.createGroup(0n);
const bad = defined(p.net.createGroup(0n));
bad.writeFrame({ timestamp: ms(1), payload: await sealed(p, GROUP, 0n, 0, "nope", true) });
p.dg.net.insert({ sequence: 0n, timestamp: ms(2), payload: await sealed(p, DATAGRAM, 0n, 0, "late") });
const forged = await recvGroup(p.consumer);
Expand Down Expand Up @@ -896,9 +896,9 @@ test("a timestamp out of range at the track's timescale spends nothing", async (

test("a grouped failure skips opens queued behind it", async () => {
const p = await pair("video");
const bad = p.net.createGroup(0n);
const bad = defined(p.net.createGroup(0n));
bad.writeFrame({ timestamp: ms(1), payload: await sealed(p, GROUP, 0n, 0, "nope", true) });
const good = p.net.createGroup(1n);
const good = defined(p.net.createGroup(1n));
good.writeFrame({ timestamp: ms(2), payload: await sealed(p, GROUP, 1n, 0, "ok") });
p.dg.net.insert({ sequence: 0n, timestamp: ms(3), payload: await sealed(p, DATAGRAM, 0n, 0, "late") });
const forged = await recvGroup(p.consumer);
Expand Down
7 changes: 4 additions & 3 deletions js/e2ee/src/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ export class Producer {
* Start a group at `start`, in the track's timescale, which must be at or above the next
* unallocated one, as {@link NetTrack.Producer.createGroup} does.
*
* Throws {@link Failure} `reuse` if `start` was already allocated, `identity` if it exceeds
* `2^53-1`, or a net error.
* Throws {@link Failure} `reuse` if `start` was already allocated or another group holds it,
* `identity` if it exceeds `2^53-1`, or a net error.
*/
createGroup(start: bigint): Group.Producer {
this.#writable();
this.#reserve(start);
this.#next = start + 1n;
const inner = this.#inner.createGroup(start);
if (inner === undefined) throw new Failure("reuse");
this.#next = start + 1n;
return Group.Producer.make(inner, this.#writer);
}

Expand Down
7 changes: 4 additions & 3 deletions js/hang/bench/buffered.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { type Group, Time, Track, Varint } from "@moq/net";
import { Signal } from "@moq/signals";
import { Consumer } from "../src/container/consumer.ts";
import { Format } from "../src/container/legacy.ts";
import { defined } from "../src/defined.test-util.ts";

// Far past every fixture's span, so only the burst case skips anything.
const HOLD = Duration.millis(1_000_000_000);
Expand Down Expand Up @@ -126,7 +127,7 @@ async function measure(
if (scenario === "steady") {
// The player reads each frame as it lands, so nothing builds up behind it.
for (const i of order) {
const sources = tracks.map((track) => track.createGroup(BigInt(i)));
const sources = tracks.map((track) => defined(track.createGroup(BigInt(i))));
for (let frame = 0; frame < frames; frame++) {
const reads = players.map((player) => player.consumer.next());
for (const source of sources) source.writeFrame(wire(i, frame));
Expand All @@ -141,7 +142,7 @@ async function measure(

// Everything arrives before the player reads: a stalled reader, or history after a join.
for (const i of order) {
const sources = tracks.map((track) => track.createGroup(BigInt(sequence(i))));
const sources = tracks.map((track) => defined(track.createGroup(BigInt(sequence(i)))));
for (let frame = 0; frame < frames; frame++) await arrive(sources, players, sequence(i), frame);
for (const source of sources) source.finish();
}
Expand All @@ -163,7 +164,7 @@ async function measure(
// The budget drops to nothing, so the next arrival makes every older group too stale to keep.
delay.set({ max: Duration.ZERO });
start = performance.now();
const sources = tracks.map((track) => track.createGroup(BigInt(groups)));
const sources = tracks.map((track) => defined(track.createGroup(BigInt(groups))));
await arrive(sources, players, groups, 0);
const elapsed = (performance.now() - start) * 1e6;
const newest = timestamp(groups, 0) / 1000;
Expand Down
3 changes: 2 additions & 1 deletion js/hang/bench/slow-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Time, Track, Varint } from "@moq/net";
import { Signal } from "@moq/signals";
import { Consumer } from "../src/container/consumer.ts";
import { Format } from "../src/container/legacy.ts";
import { defined } from "../src/defined.test-util.ts";

console.log("groups,warnings,bytes,catchup_ms");
for (const count of [1, 16, 256, 1024]) {
Expand All @@ -15,7 +16,7 @@ for (const count of [1, 16, 256, 1024]) {
delay,
});
const write = (sequence: bigint) => {
const group = track.createGroup(sequence);
const group = defined(track.createGroup(sequence));
group.writeFrame({ payload: Varint.encode(Number(sequence) * 1000), timestamp: Time.Timestamp.now() });
};
const buffered = async (timestamp: number) => {
Expand Down
7 changes: 4 additions & 3 deletions js/hang/src/container/consumer.backlog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Duration, type Installed, Instant, now } from "@moq/clock";
import { Manual } from "@moq/clock/test";
import { Time, Track, Varint } from "@moq/net";
import { race } from "@moq/signals";
import { defined } from "../defined.test-util.ts";
import { Consumer } from "./consumer.ts";
import { Jitter } from "./jitter.ts";
import { Format } from "./legacy.ts";
Expand Down Expand Up @@ -48,7 +49,7 @@ async function play(
try {
for (const [at, sequence, media] of events) {
await clock.advance(Duration.millis(base + at).saturatingSub(now().since(Instant.ZERO)));
const group = track.createGroup(BigInt(sequence));
const group = defined(track.createGroup(BigInt(sequence)));
const payload = new Uint8Array([...Varint.encode(media * 1000), 1]);
group.writeFrame({ payload, timestamp: Time.Timestamp.fromMicros(media * 1000) });
group.finish();
Expand Down Expand Up @@ -160,7 +161,7 @@ function joining(from: bigint, largest: () => bigint | Promise<bigint>) {
largest: async () => ({ group: await largest(), frame: 0 }),
});
const write = (sequence: number, media: number) => {
const group = track.createGroup(BigInt(sequence));
const group = defined(track.createGroup(BigInt(sequence)));
const payload = new Uint8Array([...Varint.encode(media * 1000), 1]);
group.writeFrame({ payload, timestamp: Time.Timestamp.fromMicros(media * 1000) });
group.finish();
Expand Down Expand Up @@ -309,7 +310,7 @@ test("a range capped below the largest still delivers", async () => {
// largest: the consumer reports no backlog, but delivers what it can read.
const track = new Track.Producer("audio");
for (let sequence = 0; sequence <= 5; sequence++) {
const group = track.createGroup(BigInt(sequence));
const group = defined(track.createGroup(BigInt(sequence)));
const media = sequence * 20;
group.writeFrame({
payload: new Uint8Array([...Varint.encode(media * 1000), 1]),
Expand Down
69 changes: 35 additions & 34 deletions js/hang/src/container/consumer.backpressure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect, spyOn, test } from "bun:test";
import { Duration, Elapsed, Instant, timeout } from "@moq/clock";
import { Manual } from "@moq/clock/test";
import { Time, Track } from "@moq/net";
import { defined } from "../defined.test-util.ts";
import { Consumer, type Read } from "./consumer.ts";
import { encodeFrame, Format } from "./legacy.ts";
import type { Frame } from "./types.ts";
Expand Down Expand Up @@ -65,9 +66,9 @@ test("Consumer holds frames sharing a timestamp within the max buffer", async ()
// Frames packed into one timestamp have no span, so the age budget never skips the stalled
// group they queue behind. They fill the max buffer instead, which skips it once its one frame
// is read, so nothing decoded is dropped.
const stalled = track.createGroup(0n);
const stalled = defined(track.createGroup(0n));
write(stalled, 0);
const packed = track.createGroup(1n);
const packed = defined(track.createGroup(1n));
for (let i = 0; i < 2_000; i++) write(packed, 10_000);
packed.finish();
track.finish();
Expand Down Expand Up @@ -97,10 +98,10 @@ test("Consumer holds many groups within the max buffer and reaches the live edge
maxBuffer,
});
try {
const stalled = track.createGroup(0n);
const stalled = defined(track.createGroup(0n));
write(stalled, 0);
for (let g = 1n; g <= 500n; g++) {
const group = track.createGroup(g);
const group = defined(track.createGroup(g));
write(group, Number(g) * 1_000);
group.finish();
}
Expand Down Expand Up @@ -133,9 +134,9 @@ test("Consumer skips a stalled group once group overhead alone fills the max buf
maxBuffer: 512,
});
try {
const stalled = track.createGroup(0n);
const stalled = defined(track.createGroup(0n));
write(stalled, 0);
const next = track.createGroup(1n);
const next = defined(track.createGroup(1n));
write(next, 10_000);
next.finish();

Expand All @@ -160,10 +161,10 @@ test("Consumer reaches a readable group past a stalled successor once group over
maxBuffer: 768,
});
try {
const stalled = track.createGroup(0n);
const stalled = defined(track.createGroup(0n));
write(stalled, 0);
track.createGroup(1n);
const readable = track.createGroup(2n);
defined(track.createGroup(1n));
const readable = defined(track.createGroup(2n));
write(readable, 10_000);
write(readable, 20_000);
readable.finish();
Expand All @@ -190,7 +191,7 @@ test("Consumer admits reads completing together one at a time against the max bu
try {
// Ten open groups park on their first read with room to spare, then large frames land in all of
// them at once.
const groups = Array.from({ length: 10 }, (_, sequence) => track.createGroup(BigInt(sequence)));
const groups = Array.from({ length: 10 }, (_, sequence) => defined(track.createGroup(BigInt(sequence))));
await flush();
const large = new Uint8Array(2_000);
for (const group of groups) {
Expand Down Expand Up @@ -223,9 +224,9 @@ test("Consumer charges a small view of a large wire frame the wire frame", async
maxBuffer: 16 * 1024,
});
try {
const stalled = track.createGroup(0n);
const stalled = defined(track.createGroup(0n));
write(stalled, 0);
const padded = track.createGroup(1n);
const padded = defined(track.createGroup(1n));
const padding = new Uint8Array(60_000);
for (let i = 0; i < 8; i++) {
padded.writeFrame({
Expand Down Expand Up @@ -266,7 +267,7 @@ test("Consumer stamps frames the max buffer held back with when they landed, not
maxBuffer: 300,
});
try {
const group = track.createGroup(0n);
const group = defined(track.createGroup(0n));
write(group, 0);
await clock.advance(Duration.millis(250));
write(group, 10_000);
Expand Down Expand Up @@ -302,11 +303,11 @@ test("Consumer expires a hole behind a full max buffer from when the group past
});
try {
for (const sequence of [0n, 1n]) {
const group = track.createGroup(sequence);
const group = defined(track.createGroup(sequence));
write(group, Number(sequence) * 10_000);
group.finish();
}
const marker = track.createGroup(3n);
const marker = defined(track.createGroup(3n));
marker.writeFrame({ payload: new Uint8Array(), timestamp: Time.Timestamp.fromMicros(30_000) });
marker.finish();
// The reader is slow: the hole before group 3 expired before anything is read.
Expand Down Expand Up @@ -334,9 +335,9 @@ for (const maxBuffer of [0, 256]) {
maxBuffer,
});
try {
const stalled = track.createGroup(0n);
const stalled = defined(track.createGroup(0n));
write(stalled, 0);
const next = track.createGroup(1n);
const next = defined(track.createGroup(1n));
write(next, 10_000);
next.finish();

Expand Down Expand Up @@ -364,12 +365,12 @@ test("Consumer moves past a stalled backlog group below the cursor once the max
maxBuffer: 512,
});
try {
const active = track.createGroup(3n);
const active = defined(track.createGroup(3n));
write(active, 30_000);
expect(await consumer.next()).toMatchObject({ kind: "frame", group: 3n });

track.createGroup(1n);
const newer = track.createGroup(4n);
defined(track.createGroup(1n));
const newer = defined(track.createGroup(4n));
write(newer, 40_000);
newer.finish();

Expand All @@ -395,14 +396,14 @@ test("Consumer reads what a promoted head already holds before skipping it under
maxBuffer: 1024,
});
try {
const stalled = track.createGroup(0n);
const stalled = defined(track.createGroup(0n));
write(stalled, 0);
expect(await consumer.next()).toMatchObject({ kind: "frame", group: 0n });

// A later group's large frame fills the max buffer, then a frame lands in the group between,
// stamped on the wire before the later group's.
const between = track.createGroup(1n);
const later = track.createGroup(2n);
const between = defined(track.createGroup(1n));
const later = defined(track.createGroup(2n));
const early = Time.Timestamp.now();
later.writeFrame({
payload: encodeFrame(new Uint8Array(2_000), Duration.micros(20_000)),
Expand Down Expand Up @@ -444,13 +445,13 @@ test("Consumer raises a malformed frame in a head promoted under pressure", asyn
maxBuffer: 1024,
});
try {
const stalled = track.createGroup(0n);
const stalled = defined(track.createGroup(0n));
write(stalled, 0);
expect(await consumer.next()).toMatchObject({ kind: "frame", group: 0n });

// A later group's large frame fills the max buffer, then a malformed frame lands in the open group between.
const between = track.createGroup(1n);
const later = track.createGroup(2n);
const between = defined(track.createGroup(1n));
const later = defined(track.createGroup(2n));
const early = Time.Timestamp.now();
later.writeFrame({
payload: encodeFrame(new Uint8Array(2_000), Duration.micros(20_000)),
Expand All @@ -476,11 +477,11 @@ test("Consumer raises a malformed group an age skip passes after the track finis
delay: { max: Duration.millis(500) },
});
try {
const first = track.createGroup(0n);
const first = defined(track.createGroup(0n));
write(first, 0);
first.finish();
const malformed = track.createGroup(1n);
const later = track.createGroup(2n);
const malformed = defined(track.createGroup(1n));
const later = defined(track.createGroup(2n));
track.finish();
expect(await within(consumer.next())).toMatchObject({ kind: "frame", group: 0n });
await flush();
Expand Down Expand Up @@ -508,13 +509,13 @@ test("Consumer reads a finished marker group's end before skipping under pressur
maxBuffer: 1024,
});
try {
const stalled = track.createGroup(0n);
const stalled = defined(track.createGroup(0n));
write(stalled, 0);
expect(await consumer.next()).toMatchObject({ kind: "frame", group: 0n });

// A later group's large frame fills the max buffer, then the group between finishes as a data marker.
const between = track.createGroup(1n);
const later = track.createGroup(2n);
const between = defined(track.createGroup(1n));
const later = defined(track.createGroup(2n));
const early = Time.Timestamp.now();
later.writeFrame({
payload: encodeFrame(new Uint8Array(2_000), Duration.micros(20_000)),
Expand Down Expand Up @@ -547,10 +548,10 @@ test("Consumer reads a group whose frame fails to read as cut short while the re
delay: { max: Duration.millis(5_000) },
});
try {
const stalled = track.createGroup(0n);
const stalled = defined(track.createGroup(0n));
write(stalled, 0);
expect(await consumer.next()).toMatchObject({ kind: "frame", group: 0n });
const next = track.createGroup(1n);
const next = defined(track.createGroup(1n));
write(next, 10_000);
next.finish();

Expand Down
Loading