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
14 changes: 4 additions & 10 deletions js/net/src/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, spyOn, test } from "bun:test";
import { Manual, Milli, Nano, sleep, yieldNow } from "@moq/clock";
import { hostTime } from "@moq/clock/test";
import { Manual, Milli, Nano, yieldNow } from "@moq/clock";
import type { Getter } from "@moq/signals";
import { Duplicate, type Grant, Handle, type Issued, type Request, type Requests, Unsupported } from "./auth.ts";
import { accept as acceptSession, connect as connectSession, type Established } from "./connection/index.ts";
Expand All @@ -11,7 +10,7 @@ import * as IetfMessage from "./ietf/message.ts";
import { AuthMessage as LiteAuthMessage, LiteAuthWire } from "./lite/auth.ts";
import * as Lite from "./lite/index.ts";
import * as LiteMessage from "./lite/message.ts";
import { createMockTransportPair, type MockTransport } from "./mock.ts";
import { createMockTransportPair, type MockTransport, manual, settlesWithin } from "./mock.ts";
import { Producer as OriginProducer } from "./origin.ts";
import * as Path from "./path.ts";
import { Stream, Writer } from "./stream.ts";
Expand Down Expand Up @@ -354,7 +353,7 @@ describe.each([Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_22])("%s", (protocol) => {
});

test("a revoked grant withdraws its broadcasts without closing the session", async () => {
hostTime();
using clock = manual();
const origin = new OriginProducer();
const { client, server, transport } = await connect({ publish: origin, protocol });
const requests = server.auth.requests();
Expand Down Expand Up @@ -388,12 +387,7 @@ describe.each([Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_22])("%s", (protocol) => {
const third = await nextRoute(announced);
expect(third?.kind).toBe("start");

let closed = false;
void transport.closed.then(() => {
closed = true;
});
await sleep(Nano.fromMilli(Milli(10)));
expect(closed).toBe(false);
expect(await settlesWithin(transport.closed, clock, Milli(10))).toBe(false);

announced.close();
client.abort();
Expand Down
18 changes: 12 additions & 6 deletions js/net/src/connection/connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ConnectTimeout, SessionCode, SessionError } from "../error.ts";
import * as Ietf from "../ietf/index.ts";
import { ClientSetup, ServerSetup } from "../lite/compat.ts";
import { ALPN, ALPN_05, ALPN_06, ALPN_07_WIP, Version as Lite } from "../lite/version.ts";
import { createMockTransportPair } from "../mock.ts";
import { createMockTransportPair, driveUntil, manual } from "../mock.ts";
import { Stream } from "../stream.ts";
import { accept } from "./accept.ts";
import { type ConnectProps, connect as connectSession } from "./connect.ts";
Expand Down Expand Up @@ -555,14 +555,18 @@ for (const protocol of ["", undefined]) {
}

test("SETUP refuses a version the client did not name", async () => {
hostTime();
using clock = manual();
const pair = createMockTransportPair("");
const restore = stubWebTransport(pair.client);
try {
const server = accept({ url, transport: pair.server, version: Lite.DRAFT_05 }).catch(() => undefined);
await expect(
connect(url, { versions: ["moq-lite-07-wip", "moq-lite-06"], websocket: { enabled: false } }),
).rejects.toThrow("unsupported server version: 0xff0dad05");
const connecting = connect(url, {
versions: ["moq-lite-07-wip", "moq-lite-06"],
websocket: { enabled: false },
});
// The refusal waits out the grace a peer's close code gets.
await driveUntil(clock, connecting);
await expect(connecting).rejects.toThrow("unsupported server version: 0xff0dad05");
(await server)?.abort();
} finally {
restore();
Expand All @@ -585,7 +589,7 @@ test("a peer without an ALPN is refused when no named version negotiates in SETU
const IETF_VERSIONS = Object.values(Ietf.Version).map((v) => [Ietf.versionName(v), v] as const);

test.each(IETF_VERSIONS)("a client refuses %s selected in the moql SETUP", async (_, version) => {
hostTime();
using clock = manual();
const pair = createMockTransportPair("");
const restore = stubWebTransport(pair.client);
try {
Expand All @@ -596,6 +600,8 @@ test.each(IETF_VERSIONS)("a client refuses %s selected in the moql SETUP", async
if (!stream) throw new Error("no SETUP stream");
await ClientSetup.decode(stream.reader);
await new ServerSetup(version).encode(stream.writer);
// The refusal waits out the grace a peer's close code gets.
await driveUntil(clock, connecting);
await expect(connecting).rejects.toThrow(`unsupported server version: 0x${version.toString(16)}`);
} finally {
restore();
Expand Down
62 changes: 30 additions & 32 deletions js/net/src/connection/reload.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expect, spyOn, test } from "bun:test";
import { Instant, Manual, Milli, Nano, now, sleep, yieldNow } from "@moq/clock";
import { hostTime } from "@moq/clock/test";
import { Instant, Manual, Milli, Nano, now, yieldNow } from "@moq/clock";
import { Effect, Signal } from "@moq/signals";
import * as Announce from "../announced.ts";
import { Viewers } from "../bandwidth.ts";
Expand All @@ -9,7 +8,7 @@ import { ConnectTimeout, SessionCode, SessionError, StreamCode, toTransport } fr
import { WireRoute } from "../hop.ts";
import { ALPN as IetfALPN } from "../ietf/version.ts";
import * as Lite from "../lite/index.ts";
import { createMockTransportPair, type MockTransport, textFrame } from "../mock.ts";
import { createMockTransportPair, driveUntil, type MockTransport, manual, textFrame } from "../mock.ts";
import { Producer as OriginProducer } from "../origin.ts";
import * as Path from "../path.ts";
import { Stream } from "../stream.ts";
Expand Down Expand Up @@ -129,7 +128,7 @@ test("closing mid-connect aborts the pending attempt", async () => {
});

test("a peer that severs immediately keeps escalating the backoff", async () => {
hostTime();
using clock = manual();
const original = globalThis.WebTransport;
const url = new URL("https://example.com/");
let attempts = 0;
Expand All @@ -155,7 +154,7 @@ test("a peer that severs immediately keeps escalating the backoff", async () =>
delay: { initial: Milli(1000), multiplier: 2, max: Milli(1000), timeout: Milli(1) },
});
try {
await waitUntil(() => reload.error.peek() !== undefined);
await driveUntil(clock, () => reload.error.peek() !== undefined);
expect(reload.closed.peek()).toBeUndefined();

// Giving up this URL does not dispose the loop: a disable/re-enable starts another sequence.
Expand All @@ -175,7 +174,7 @@ test("a peer that severs immediately keeps escalating the backoff", async () =>
});

test("an explicitly undefined delay field falls back to its default", async () => {
hostTime();
using clock = manual();
const original = globalThis.WebTransport;
const url = new URL("https://example.com/");
let dials = 0;
Expand All @@ -197,8 +196,7 @@ test("an explicitly undefined delay field falls back to its default", async () =
delay: { initial: undefined, multiplier: undefined, max: undefined, timeout: Milli(0) },
});
try {
await waitUntil(() => dials > 0);
await sleep(Nano.fromMilli(Milli(100)));
await clock.advance(Nano.fromMilli(Milli(100)));
// The default initial delay is 1000ms, so the first retry is still pending.
expect(dials).toBe(1);
} finally {
Expand All @@ -218,7 +216,7 @@ async function waitUntil(pred: () => boolean): Promise<void> {
}

test("a follower follows the reconnect on the same handle", async () => {
hostTime();
using clock = manual();
const original = globalThis.WebTransport;
const url = new URL("https://example.com/");

Expand Down Expand Up @@ -248,12 +246,12 @@ test("a follower follows the reconnect on the same handle", async () => {
const watched = clientOrigin.follow(Path.from("late"));

try {
await waitUntil(() => watched.active.peek() !== undefined);
await driveUntil(clock, () => watched.active.peek() !== undefined);
const first = watched.active.peek();

// The session dies; the same request follows the next session's announcement.
sessions[0]?.abort();
await waitUntil(() => watched.active.peek() !== undefined && watched.active.peek() !== first);
await driveUntil(clock, () => watched.active.peek() !== undefined && watched.active.peek() !== first);
expect(watched.closed.peek()).toBeUndefined();
} finally {
watched.close();
Expand Down Expand Up @@ -438,7 +436,7 @@ function refusing(refused: string, release: Promise<void>) {
}

test("a refused credential replaced since its dial redials with the new one", async () => {
hostTime();
using clock = manual();
const original = globalThis.WebTransport;
const { promise: release, resolve } = Promise.withResolvers<void>();
const { dialed, stub } = refusing("old", release);
Expand All @@ -453,12 +451,12 @@ test("a refused credential replaced since its dial redials with the new one", as
delay: { initial: Milli(1), multiplier: 2, max: Milli(1), timeout: Milli(0) },
});
try {
await waitUntil(() => reload.established.peek() !== undefined);
await driveUntil(clock, () => reload.established.peek() !== undefined);
tokens.set(["new"]);
resolve();
await waitUntil(() => dialed.includes("new"));
await driveUntil(clock, () => dialed.includes("new"));
expect(dialed).toEqual(["old", "new"]);
await waitUntil(() => reload.established.peek() !== undefined);
await driveUntil(clock, () => reload.established.peek() !== undefined);
expect(reload.error.peek()).toBeUndefined();
} finally {
reload.close();
Expand All @@ -467,7 +465,7 @@ test("a refused credential replaced since its dial redials with the new one", as
});

test("new tokens redial a URL that stopped on refusing the old ones", async () => {
hostTime();
using clock = manual();
const original = globalThis.WebTransport;
const { dialed, stub } = refusing("old", Promise.resolve());
globalThis.WebTransport = stub;
Expand All @@ -481,11 +479,11 @@ test("new tokens redial a URL that stopped on refusing the old ones", async () =
delay: { initial: Milli(1), multiplier: 2, max: Milli(1), timeout: Milli(0) },
});
try {
await waitUntil(() => reload.error.peek() !== undefined);
await driveUntil(clock, () => reload.error.peek() !== undefined);
expect(dialed).toEqual(["old"]);
tokens.set(["new"]);
await waitUntil(() => dialed.includes("new"));
await waitUntil(() => reload.established.peek() !== undefined);
await driveUntil(clock, () => dialed.includes("new"));
await driveUntil(clock, () => reload.established.peek() !== undefined);
expect(reload.error.peek()).toBeUndefined();
} finally {
reload.close();
Expand Down Expand Up @@ -546,7 +544,7 @@ test("an unauthorized session close during setup stops retrying", async () => {
// disjoint. Reading a stream reset off the session table would call every SETUP-stream reset
// an auth rejection and suppress reconnect for the life of the page.
test("a setup stream reset is not mistaken for an unauthorized session", async () => {
hostTime();
using clock = manual();
const original = globalThis.WebTransport;
const url = new URL("https://example.com/");
let attempts = 0;
Expand Down Expand Up @@ -576,15 +574,15 @@ test("a setup stream reset is not mistaken for an unauthorized session", async (
});

try {
await waitUntil(() => attempts >= 2);
await driveUntil(clock, () => attempts >= 2);
} finally {
reload.close();
globalThis.WebTransport = original;
}
});

test("origins span reconnects: local re-announces, remote re-populates", async () => {
hostTime();
using clock = manual();
const original = globalThis.WebTransport;
const url = new URL("https://example.com/origins");

Expand Down Expand Up @@ -625,17 +623,17 @@ test("origins span reconnects: local re-announces, remote re-populates", async (
try {
// First session: the server's broadcast lands in the client origin, and the client's
// publish lands in the server's.
await waitUntil(() => wireOf(reader).routes(Path.from("remote")));
await waitUntil(() => (servers[0] ? wireOf(servers[0].saw).routes(Path.from("mine")) : false));
await driveUntil(clock, () => wireOf(reader).routes(Path.from("remote")));
await driveUntil(clock, () => (servers[0] ? wireOf(servers[0].saw).routes(Path.from("mine")) : false));

// Kill the session: the remote entry retracts, the local publish stays put.
servers[0]?.session.abort();
await waitUntil(() => !wireOf(reader).routes(Path.from("remote")));
await driveUntil(clock, () => !wireOf(reader).routes(Path.from("remote")));

// The reconnect re-announces the (untouched) publish and re-populates the table.
await waitUntil(() => servers.length > 1);
await waitUntil(() => wireOf(reader).routes(Path.from("remote")));
await waitUntil(() => (servers[1] ? wireOf(servers[1].saw).routes(Path.from("mine")) : false));
await driveUntil(clock, () => servers.length > 1);
await driveUntil(clock, () => wireOf(reader).routes(Path.from("remote")));
await driveUntil(clock, () => (servers[1] ? wireOf(servers[1].saw).routes(Path.from("mine")) : false));
} finally {
watching.close();
for (const server of servers) server.watching.close();
Expand Down Expand Up @@ -1072,7 +1070,7 @@ test("announced accepts a non-prefix scope before any session exists", () => {
* a reconnect takes it back until the next session's set lands.
*/
test("a watcher syncs with the server's initial set, and again after a reconnect", async () => {
hostTime();
using clock = manual();
// Through the consume origin, and through the handle's own stream without one.
for (const [alpn, viaOrigin] of [
[Lite.ALPN_06, true],
Expand Down Expand Up @@ -1116,12 +1114,12 @@ test("a watcher syncs with the server's initial set, and again after a reconnect
try {
// Nothing is connected yet, which is not the same as nothing being announced.
expect(watching.synced.peek()).toBe(false);
await waitUntil(() => watching.synced.peek());
await driveUntil(clock, () => watching.synced.peek());
expect([...seen].sort()).toEqual(["a", "b"]);

sessions[0]?.abort();
await waitUntil(() => !watching.synced.peek());
await waitUntil(() => sessions.length > 1 && watching.synced.peek());
await driveUntil(clock, () => !watching.synced.peek());
await driveUntil(clock, () => sessions.length > 1 && watching.synced.peek());
expect([...seen].sort()).toEqual(["a", "b"]);
} finally {
watching.close();
Expand Down
26 changes: 13 additions & 13 deletions js/net/src/ietf/datagram.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { expect, spyOn, test } from "bun:test";
import { Milli, Nano, sleep, yieldNow } from "@moq/clock";
import { hostTime } from "@moq/clock/test";
import { Milli, yieldNow } from "@moq/clock";
import { accept, connect } from "../connection/index.ts";
import { ProtocolViolation, SessionCode } from "../error.ts";
import { createMockTransportPair, type MockTransport } from "../mock.ts";
import { createMockTransportPair, type MockTransport, manual, settlesWithin } from "../mock.ts";
import { Producer as Origin } from "../origin.ts";
import * as Path from "../path.ts";
import { encodeVarint, Stream } from "../stream.ts";
Expand Down Expand Up @@ -106,7 +105,7 @@ test("an undefined object status closes the session", async () => {
});

test("a defined object status does not close the session", async () => {
hostTime();
using clock = manual();
const { pair, connection, version, writable } = await session();
try {
const bytes = new ObjectDatagram({
Expand All @@ -117,16 +116,14 @@ test("a defined object status does not close the session", async () => {
}).encode(version);
const writer = writable.getWriter();
await writer.write(bytes);
const open = Symbol("open");
const result = await Promise.race([pair.server.closed, sleep(Nano.fromMilli(Milli(50))).then(() => open)]);
expect(result).toBe(open);
expect(await settlesWithin(pair.server.closed, clock, Milli(50))).toBe(false);
} finally {
connection.abort();
}
});

test("a datagram track round-trips over moq-transport", async () => {
hostTime();
using clock = manual();
const source = new Origin();
const path = Path.from("live");
const publication = source.route(path, { hidden: true });
Expand Down Expand Up @@ -166,7 +163,8 @@ test("a datagram track round-trips over moq-transport", async () => {
expect(received?.timestamp?.asMillis()).toBe(9);
expect(received?.payload).toEqual(new Uint8Array([7]));
datagrams.finish();
expect(await reader.recv()).toBeUndefined();
// The end waits out the grace a late datagram gets.
expect(await clock.drive(reader.recv())).toBeUndefined();
} finally {
reader.close();
await peers[0].close();
Expand Down Expand Up @@ -291,7 +289,7 @@ for (const alpn of [ALPN.DRAFT_18, ALPN.DRAFT_20, ALPN.DRAFT_22]) {
[100, true],
] as const) {
test(`${alpn}: a datagram sent right behind its answer arrives (answer ${lag} microtasks late${finish ? ", then the end" : ""})`, async () => {
hostTime();
using clock = manual();
const source = new Origin();
const publication = source.route(Path.from("live"), { hidden: true });
const datagrams = publication.broadcast.createDatagrams("audio");
Expand All @@ -317,7 +315,8 @@ for (const alpn of [ALPN.DRAFT_18, ALPN.DRAFT_20, ALPN.DRAFT_22]) {
await reader.info();
if (!finish) datagrams.append(undefined, Uint8Array.of(2));
expect((await reader.recv())?.payload).toEqual(Uint8Array.of(1));
if (finish) expect(await reader.recv()).toBeUndefined();
// The end waits out the grace a late datagram gets.
if (finish) expect(await clock.drive(reader.recv())).toBeUndefined();
else expect((await reader.recv())?.payload).toEqual(Uint8Array.of(2));
} finally {
reader.close();
Expand All @@ -332,7 +331,7 @@ for (const alpn of [ALPN.DRAFT_18, ALPN.DRAFT_20, ALPN.DRAFT_22]) {

for (const alpn of [ALPN.DRAFT_18, ALPN.DRAFT_20, ALPN.DRAFT_22]) {
test(`${alpn}: a datagram burst reaches its reader before the end across a receive yield`, async () => {
hostTime();
using clock = manual();
const source = new Origin();
const publication = source.route(Path.from("live"), { hidden: true });
const datagrams = publication.broadcast.createDatagrams("audio");
Expand All @@ -356,7 +355,8 @@ for (const alpn of [ALPN.DRAFT_18, ALPN.DRAFT_20, ALPN.DRAFT_22]) {
for (let i = 0; i < 64; i++) datagrams.append(undefined, Uint8Array.of(i));
datagrams.finish();
for (let i = 0; i < 64; i++) expect((await reader.recv())?.payload).toEqual(Uint8Array.of(i));
expect(await reader.recv()).toBeUndefined();
// The end waits out the grace a late datagram gets.
expect(await clock.drive(reader.recv())).toBeUndefined();
} finally {
budget.mockRestore();
reader.close();
Expand Down
Loading
Loading