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
4 changes: 2 additions & 2 deletions c/moq/test/probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,10 @@ int main(void)
moq_track_consumer *consumer = await(&subscribed, "subscribe_track");
moq_task_free(task);

/* Freeing a pending task cancels the read, and its callback never runs. */
/* Freeing a pending task cancels the read, and its callback never runs, even while the
* next read below pumps the loop. */
result cancelled = {0};
task = moq_track_consumer_read_frame(consumer, on_frame, &cancelled);
CHECK(!pump(&cancelled.done, 50));
moq_task_free(task);

/* The consumer survives the cancelled read and delivers the next frame. */
Expand Down
4 changes: 2 additions & 2 deletions cpp/moq/test/probe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ int main()

// Cancel a read that has nothing to deliver yet.
auto pending = consumer->read_frame();
CHECK(pending.wait_for(50ms) == std::future_status::timeout);
CHECK(pending.wait_for(0ms) == std::future_status::timeout);
CHECK(pending.valid());
pending.cancel();
CHECK(!pending.valid());
Expand Down Expand Up @@ -396,7 +396,7 @@ int main()
Latch done;
std::optional<moq::Frame> received;
auto abandoned = read_one(consumer, received, done);
CHECK(!done.wait_for(50ms));
CHECK(!done.wait_for(0ms));
abandoned.handle.destroy();
}

Expand Down
46 changes: 25 additions & 21 deletions demo/carla/sim/tests/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,10 +611,21 @@ def test_session_announces_after_populating_tracks() -> None:
assert calls == ["status", "announce"]


def test_a_driver_keeps_the_world_stepping_with_nobody_watching() -> None:
async def expect_held(session: runtime.TruckSession, held: set[str]) -> None:
"""Let the session's tasks run until the world's holds are `held`."""
for _ in range(10):
if session._clock.held == held:
return
await asyncio.sleep(0)
pytest.fail(f"holds never became {held}, still {session._clock.held}")


def test_a_driver_keeps_the_world_stepping_with_nobody_watching(monkeypatch: pytest.MonkeyPatch) -> None:
"""Video demand and the lease are independent; the gate must hold for both."""

async def scenario() -> None:
# Time stands still, so only the watchdog command below ends the lease.
monkeypatch.setattr(asyncio.get_running_loop(), "time", lambda: 0.0)
commands = asyncio.Queue()
commands.put_nowait(
Command(kind="drive", driver_id="holder", session="holder", sequence=1, state=DriveState(throttle=0.5))
Expand All @@ -631,17 +642,17 @@ async def scenario() -> None:
session._actuate = lambda: None
session._publish = lambda: None

with contextlib.suppress(asyncio.TimeoutError):
await asyncio.wait_for(session._loop(), timeout=0.1)

assert session._clock.held == {"drive:truck1"}

# The watchdog hands the truck back, and the hold goes with the lease.
session._commands.put_nowait(Command(kind="watchdog_expired"))
with contextlib.suppress(asyncio.TimeoutError):
await asyncio.wait_for(session._loop(), timeout=0.1)
task = asyncio.create_task(session._loop())
try:
await expect_held(session, {"drive:truck1"})

assert session._clock.held == set()
# The watchdog hands the truck back, and the hold goes with the lease.
session._commands.put_nowait(Command(kind="watchdog_expired"))
await expect_held(session, set())
finally:
task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await task

asyncio.run(scenario())

Expand Down Expand Up @@ -690,20 +701,13 @@ async def scenario() -> None:
session._status = DemandPublisher()
watcher = asyncio.create_task(session._watch_status_demand())

async def expect_held(held: set[str]) -> None:
for _ in range(10):
if session._clock.held == held:
return
await asyncio.sleep(0)
pytest.fail(f"holds never became {held}, still {session._clock.held}")

try:
session._status.use.set()
await expect_held({"status:truck1"})
await expect_held(session, {"status:truck1"})
session._status.unuse.set()
await expect_held(set())
await expect_held(session, set())
session._status.use.set()
await expect_held({"status:truck1"})
await expect_held(session, {"status:truck1"})
finally:
watcher.cancel()
with contextlib.suppress(asyncio.CancelledError):
Expand Down
54 changes: 38 additions & 16 deletions quest/m1/mock-clock-tests/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# [XL] Tests run on a mocked clock
# [L] Tests run on a mocked clock

## Goal

No test proves that nothing happened by waiting, sleeps to let things
settle, or asserts on elapsed real time. Each one runs on a paused,
simulated, or `Manual` clock, or awaits the event it means. About 300 tests
do one of these today, most of them behind a dozen shared helpers. Tests
that only wait for progress under a generous timeout, and tests on real
sockets, devices, or processes, stay on the real clock.
simulated, or `Manual` clock, or awaits the event it means. Tests that only
wait for progress under a generous timeout, and tests on real sockets,
devices, or processes, stay on the real clock.

## Plan

Expand All @@ -21,21 +20,44 @@ test's clock):
- ✅ m1, while the guards that stop new ones (the runtime guards and the
`REVIEW.md` rule) landed in m0.

Rules every child keeps:
Rules the remaining work keeps:

- Fix the shared helper first, then its callers. A helper that resolves
`undefined` when a timeout runs out becomes one that advances a mocked
clock past the deadline, or awaits the event.
- Never replace one real wait with a shorter or longer one, or with a
quiet window.
- Fix a test and its siblings in other languages together.
- Each JS child removes the bare `hostTime();` opt-outs in its area, so the
`bun test` guard (`js/clock/src/preload.ts`) covers the converted tests. An
opt-out with a trailing reason comment is real I/O and stays.
- Each Rust child removes the `kio::clock::host_time()` calls in the
tests it converts, so `just rs test`'s host-timer guard covers them. A test
over a real QUIC endpoint or ICE agent is exempt without one, since those
transports allow host timers themselves.

The counts come from a heuristic scan on 2026-11-06; re-scan each area
before starting, since tests land and move.
- An opt-out with a trailing reason comment (`hostTime(); // a real HTTP
server`, `kio::clock::host_time(); // real SRT sockets`) is real I/O and
stays. A bare one marks a test still to convert: remove it with the
conversion, so the `bun test` guard (`js/clock/src/preload.ts`) or
`just rs test`'s host-timer guard covers the test. A test over a real QUIC
endpoint or ICE agent needs none, since those transports allow host timers
themselves.

What remains, from a scan on 2026-11-07; re-scan before starting, since
tests land and move:

- **moq-relay's TCP-only tests**, the seven bare `kio::clock::host_time()`
calls in `tests/integration/auth_lifetime.rs` (4), `session_revalidate.rs`
(2), and `socket_auth.rs` (1). They wait out real re-check cadences and the
handshake deadline over TCP, bound a close by a real budget ("well inside
the cadence"), and poll listings on kio sleeps. The relay runs on the test
runtime, so an installed `kio::clock::Manual` can own its timers while the
sockets stay real: a push then proves its close with the clock standing
still, and a cadence passes by `advance`. `wait_listed` and `assert_closed`
need awaited events first. `socket_auth`'s test waits only for progress,
so find which relay timer fires there.
- **moq-ffi's `video_encoder_follows_a_shrinking_grant`**, which polls
`applied_bitrate()` every 10 ms while the follow thread retunes the
encoder. The applied target lives in `VideoProducer` under the rendition's
one lock, so the test needs an event from the follower, not a second
channel beside that lock.
- **The OBS plugin tests** in `cpp/obs/test`, which neither runtime guard
sees. `moq-output-test.cpp` and `moq-source-test.cpp` sleep 50 to 100 ms,
then check that no further signal or frame arrived; `obs-moq-test.cpp`
proves a parked `accept` by waiting 50 ms; `moq-dock-stop-test.cpp`
asserts `20ms <= elapsed < 400ms` around a worker that sleeps 30 ms. Each
needs an event from the plugin, such as its worker joining or the bridge
observing the close, instead of a window.
15 changes: 11 additions & 4 deletions rs/moq-auth/src/mesh/contract_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,18 @@ async fn refresh_issues_a_new_token_until_revoked() {

#[tokio::test]
async fn renew_refreshes_before_expiry() {
kio::clock::host_time();
let mesh = Mesh::with_ttl(Duration::from_secs(3)).await;
// A clock only the test moves, which the coordinator reads too; the HTTP between them is real.
let clock = kio::clock::Manual::new().install(&tokio::runtime::Handle::current());
let ttl = Duration::from_secs(3);
let mesh = Mesh::with_ttl(ttl).await;
let device = mesh.device();
let enrollment = device.enroll(&mesh.key(true), None).await.unwrap();
let renewed = device.renew(&enrollment).await.unwrap();
let mut renew = std::pin::pin!(device.renew(&enrollment));
// The first poll arms the wait for two thirds of the token's life, before any request.
let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
assert!(std::future::Future::poll(renew.as_mut(), &mut cx).is_pending());
clock.advance(ttl * 2 / 3);
let renewed = renew.await.unwrap();
assert!(kio::clock::wall() < enrollment.expires);
assert!(renewed.expires > enrollment.expires);
}
Expand Down Expand Up @@ -218,7 +225,7 @@ async fn only_an_issued_challenge_is_accepted() {

#[tokio::test]
async fn renew_retries_a_transient_refusal() {
kio::clock::host_time();
kio::clock::host_time(); // real HTTP, and the backoff is armed only after its refusal
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

Expand Down
6 changes: 2 additions & 4 deletions rs/moq-cli/src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,8 @@ mod tests {

/// `export archive` records each broadcast announced under its prefix into its own
/// `<path>/@<instance>`, and leaves out what is announced elsewhere.
#[tokio::test]
#[tokio::test(start_paused = true)]
async fn each_broadcast_records_under_its_own_key_prefix() {
kio::clock::host_time();
let dir = tempfile::tempdir().unwrap();
let url = Url::from_directory_path(dir.path()).unwrap();

Expand Down Expand Up @@ -482,9 +481,8 @@ mod tests {

/// `import archive` republishes a recording `export archive` wrote under the given name and
/// instance, serving its stored groups.
#[tokio::test]
#[tokio::test(start_paused = true)]
async fn an_exported_broadcast_imports() {
kio::clock::host_time();
let dir = tempfile::tempdir().unwrap();
let url = Url::from_directory_path(dir.path()).unwrap();

Expand Down
3 changes: 1 addition & 2 deletions rs/moq-cli/src/hls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,8 @@ mod test {

/// An announced import that fails, here on a timestamp reset, aborts its tracks and catalog
/// with the cause, so subscribers see the failure rather than a dropped producer.
#[tokio::test]
#[tokio::test(start_paused = true)]
async fn a_failed_import_aborts_its_tracks() {
kio::clock::host_time();
use moq_mux::container::{Frame, fmp4::Muxer};

let dir = tempfile::tempdir().unwrap();
Expand Down
1 change: 0 additions & 1 deletion rs/moq-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,6 @@ mod tests {
/// A raw TCP bind is a complete server side even when no QUIC bind is set.
#[tokio::test(start_paused = true)]
async fn tcp_only_moq_side_starts_a_server() {
kio::clock::host_time();
let invocation = Invocation::try_parse_from([
"moq",
"--listen-tcp-bind",
Expand Down
3 changes: 1 addition & 2 deletions rs/moq-cli/src/play/headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,8 @@ mod tests {

/// A run that ends still writes its final sample, with nothing it could not
/// measure filled in.
#[tokio::test]
#[tokio::test(start_paused = true)]
async fn metrics_write_a_final_sample() {
kio::clock::host_time();
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("play.ndjson");
let producer = moq_tokio::origin::spawn();
Expand Down
4 changes: 0 additions & 4 deletions rs/moq-ffi/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3336,7 +3336,6 @@ fn audio_rejects_bad_init_bytes() {
/// the announcement.
#[tokio::test]
async fn a_hidden_route_is_invisible_until_announced() {
kio::clock::host_time();
let origin = MoqOriginProducer::new(MoqOriginConfig::default());
let consumer = origin.consume();
let route = origin.route("live".into(), hidden()).unwrap();
Expand Down Expand Up @@ -6282,13 +6281,11 @@ async fn follow_hands_out_a_takeover_once(dynamic_at: &str) {

#[tokio::test]
async fn follow_hands_out_a_takeover_once_while_resolving() {
kio::clock::host_time();
follow_hands_out_a_takeover_once("").await;
}

#[tokio::test]
async fn follow_hands_out_a_held_takeover_once() {
kio::clock::host_time();
follow_hands_out_a_takeover_once("live").await;
}

Expand Down Expand Up @@ -6420,7 +6417,6 @@ async fn an_instance_pins_request_and_follow() {
/// clock that nothing comes past the origin's update hold either.
#[tokio::test]
async fn follow_waits_for_another_instance_after_a_dropped_takeover() {
kio::clock::host_time();
let origin = MoqOriginProducer::new(MoqOriginConfig::default());
let dynamic = serve(&origin, "live");
let followed = origin.consume().follow("live".into(), None).unwrap();
Expand Down
Loading
Loading