From 20a5f8ace4bc230d180c1379dbf50e822e4847fd Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 22:02:32 -0800 Subject: [PATCH 1/4] chore(quest): claim quest/m1/mock-clock-tests/README.md Quest-Claim: 65a3839a6161da0b From c9bb116edaef9259cb1129ceb9224d8936de6744 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 22:37:24 -0800 Subject: [PATCH 2/4] test: in-process tests drop their host-clock opt-outs The kio host-timer guard's burn-down list: in-process tests move to paused tokio or an awaited event, stale opt-outs go, and real-I/O opt-outs say why. The demo/carla settle window and the C/C++ probes' 50 ms pending proofs go too. Co-Authored-By: Claude Opus 5.5 --- c/moq/test/probe.c | 4 +- cpp/moq/test/probe.cpp | 4 +- demo/carla/sim/tests/test_runtime.py | 46 +++++++++-------- quest/m1/mock-clock-tests/README.md | 49 +++++++++++++------ rs/moq-auth/src/mesh/contract_tests.rs | 15 ++++-- rs/moq-cli/src/archive.rs | 6 +-- rs/moq-cli/src/hls.rs | 3 +- rs/moq-cli/src/main.rs | 1 - rs/moq-cli/src/play/headless.rs | 3 +- rs/moq-ffi/src/test.rs | 35 ++++++------- rs/moq-ffi/src/video.rs | 29 +++++++---- rs/moq-hls/src/export/mod.rs | 16 ++---- rs/moq-hls/src/import.rs | 1 - rs/moq-relay/src/web.rs | 2 +- .../tests/integration/shutdown_signal.rs | 10 ++-- rs/moq-rtmp/src/server.rs | 2 +- rs/moq-rtsp/src/client.rs | 2 +- rs/moq-srt/src/server.rs | 2 +- rs/moq-tokio/src/server.rs | 2 +- rs/moq-tokio/src/tcp.rs | 2 +- rs/moq-tokio/src/websocket.rs | 10 ++-- rs/moq-transcode/src/lib.rs | 1 - rs/moq-video/src/decode/consumer.rs | 9 ++-- rs/moq-video/src/encode/producer.rs | 3 -- 24 files changed, 136 insertions(+), 121 deletions(-) diff --git a/c/moq/test/probe.c b/c/moq/test/probe.c index 6ca21f6ee1f..db6d9316443 100644 --- a/c/moq/test/probe.c +++ b/c/moq/test/probe.c @@ -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. */ diff --git a/cpp/moq/test/probe.cpp b/cpp/moq/test/probe.cpp index e4f325ebade..f5fc8e0343a 100644 --- a/cpp/moq/test/probe.cpp +++ b/cpp/moq/test/probe.cpp @@ -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()); @@ -396,7 +396,7 @@ int main() Latch done; std::optional received; auto abandoned = read_one(consumer, received, done); - CHECK(!done.wait_for(50ms)); + CHECK(!done.wait_for(0ms)); abandoned.handle.destroy(); } diff --git a/demo/carla/sim/tests/test_runtime.py b/demo/carla/sim/tests/test_runtime.py index 2280f25b990..e6d30ed9d7f 100644 --- a/demo/carla/sim/tests/test_runtime.py +++ b/demo/carla/sim/tests/test_runtime.py @@ -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)) @@ -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()) @@ -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): diff --git a/quest/m1/mock-clock-tests/README.md b/quest/m1/mock-clock-tests/README.md index 92beb185a5e..003419151ef 100644 --- a/quest/m1/mock-clock-tests/README.md +++ b/quest/m1/mock-clock-tests/README.md @@ -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 @@ -21,7 +20,7 @@ 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 @@ -29,13 +28,31 @@ Rules every child keeps: - 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. +- **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. diff --git a/rs/moq-auth/src/mesh/contract_tests.rs b/rs/moq-auth/src/mesh/contract_tests.rs index 569d63b6d48..6f3f043fc5b 100644 --- a/rs/moq-auth/src/mesh/contract_tests.rs +++ b/rs/moq-auth/src/mesh/contract_tests.rs @@ -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); } @@ -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}; diff --git a/rs/moq-cli/src/archive.rs b/rs/moq-cli/src/archive.rs index a728a8c0b77..33b76a8e644 100644 --- a/rs/moq-cli/src/archive.rs +++ b/rs/moq-cli/src/archive.rs @@ -385,9 +385,8 @@ mod tests { /// `export archive` records each broadcast announced under its prefix into its own /// `/@`, 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(); @@ -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(); diff --git a/rs/moq-cli/src/hls.rs b/rs/moq-cli/src/hls.rs index fee57d0b649..6217977b335 100644 --- a/rs/moq-cli/src/hls.rs +++ b/rs/moq-cli/src/hls.rs @@ -486,9 +486,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(); diff --git a/rs/moq-cli/src/main.rs b/rs/moq-cli/src/main.rs index 0a8a4d2590b..b46fab21382 100644 --- a/rs/moq-cli/src/main.rs +++ b/rs/moq-cli/src/main.rs @@ -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", diff --git a/rs/moq-cli/src/play/headless.rs b/rs/moq-cli/src/play/headless.rs index b8bd1c2e9c5..3c3fb17f425 100644 --- a/rs/moq-cli/src/play/headless.rs +++ b/rs/moq-cli/src/play/headless.rs @@ -151,9 +151,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(); diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index 269284b3613..a4bff0a21c9 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -3297,7 +3297,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(); @@ -6239,13 +6238,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; } @@ -6377,7 +6374,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(); @@ -7496,7 +7492,6 @@ async fn cancelled_state_wait_does_not_swallow_the_next_transition() { #[cfg(feature = "video")] #[tokio::test] async fn video_encoder_follows_a_shrinking_grant() { - kio::clock::host_time(); use crate::bandwidth::MoqAllocator; use crate::video::*; @@ -7547,18 +7542,20 @@ async fn video_encoder_follows_a_shrinking_grant() { .unwrap(); assert_eq!(reservation.rate(), Some(1_000_000)); - let deadline = kio::clock::Instant::now() + TIMEOUT; - loop { - if video.applied_bitrate() == 1_000_000 { - break; - } - assert!( - kio::clock::Instant::now() < deadline, - "encoder did not follow the shrinking grant, last applied {}", - video.applied_bitrate() - ); - kio::clock::sleep(Duration::from_millis(10)).await; - } + // The follow thread retunes the encoder, so wait for it to record the grant. + let applied = video.applied_bitrate(); + let followed = kio::wait(|waiter| { + applied + .poll(waiter, |bitrate| match **bitrate { + 1_000_000 => std::task::Poll::Ready(()), + _ => std::task::Poll::Pending, + }) + .map(|followed| followed.is_ok()) + }); + let open = kio::clock::timeout(TIMEOUT, followed) + .await + .unwrap_or_else(|_| panic!("encoder did not follow the shrinking grant, last applied {}", *applied.read())); + assert!(open, "the applied bitrate closed"); video.finish().unwrap(); broadcast.close().unwrap(); @@ -7651,13 +7648,13 @@ async fn set_bitrate_caps_a_later_bandwidth_grant() { video.set_bitrate(1_000_000).unwrap(); assert_eq!(reservation.rate(), Some(1_000_000)); - assert_eq!(video.applied_bitrate(), 1_000_000); + assert_eq!(*video.applied_bitrate().read(), 1_000_000); estimate .set(Some(moq_net::bandwidth::Rate::from_bps(3_000_000))) .unwrap(); assert_eq!(reservation.rate(), Some(1_000_000)); - assert_eq!(video.applied_bitrate(), 1_000_000); + assert_eq!(*video.applied_bitrate().read(), 1_000_000); video.finish().unwrap(); broadcast.close().unwrap(); diff --git a/rs/moq-ffi/src/video.rs b/rs/moq-ffi/src/video.rs index 2a6945dd17a..a4ff96bd4b3 100644 --- a/rs/moq-ffi/src/video.rs +++ b/rs/moq-ffi/src/video.rs @@ -11,7 +11,7 @@ //! timestamp. use std::sync::Arc; -use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::atomic::{AtomicBool, Ordering}; use tokio::sync::oneshot; @@ -489,8 +489,9 @@ pub struct MoqVideoProducer { inner: Arc>>, reservation: std::sync::Mutex>>, follow: std::sync::Mutex>, - /// Last bitrate applied by the follow loop or [`set_bitrate`](Self::set_bitrate). - applied: Arc, + /// Last bitrate applied by the follow loop or [`set_bitrate`](Self::set_bitrate). A + /// channel, so a test can await the follow thread's retune. + applied: kio::Producer, /// The next lower rendition's ceiling, kept to follow a new track with. lower: Option>, /// Held so the reservation's registry outlives extra bandwidth handles, and to reserve @@ -570,8 +571,8 @@ impl MoqVideoProducer { } #[cfg(test)] - pub(crate) fn applied_bitrate(&self) -> u64 { - self.applied.load(Ordering::SeqCst) + pub(crate) fn applied_bitrate(&self) -> kio::Consumer { + self.applied.consume() } } @@ -671,7 +672,7 @@ impl MoqVideoProducer { if let Some(reservation) = self.reservation.lock().unwrap().as_ref() { reservation.update(bitrate); } - self.applied.store(bitrate, Ordering::SeqCst); + store(&self.applied, bitrate); Ok(()) } @@ -816,7 +817,7 @@ impl MoqVideoProducer { ceiling: kio::Producer::new(ceiling), priority, }))); - let applied = Arc::new(AtomicU64::new(ceiling.as_bps())); + let applied = kio::Producer::new(ceiling.as_bps()); let reserved = bandwidth.as_ref().map(|bandwidth| { let demand = inner .lock() @@ -875,7 +876,15 @@ struct Followed { lower: Option>, consumer: moq_net::bandwidth::Consumer, ceiling: moq_net::bandwidth::Rate, - applied: Arc, + applied: kio::Producer, +} + +/// Record `bitrate` as the last one applied. +fn store(applied: &kio::Producer, bitrate: u64) { + let Ok(mut applied) = applied.write() else { + unreachable!("nothing closes the applied bitrate") + }; + *applied = bitrate; } fn spawn_follow(followed: Followed) -> Follow { @@ -974,8 +983,8 @@ async fn follow_reservation(followed: Followed) { }; let policy = video_policy(*producer.ceiling.read(), below); match retune(&mut follower, policy, target, |target| producer.apply(target)) { - Ok(Some(moq_mux::rate::Target::Enabled(bitrate))) => applied.store(bitrate.as_bps(), Ordering::SeqCst), - Ok(Some(moq_mux::rate::Target::Disabled)) => applied.store(0, Ordering::SeqCst), + Ok(Some(moq_mux::rate::Target::Enabled(bitrate))) => store(&applied, bitrate.as_bps()), + Ok(Some(moq_mux::rate::Target::Disabled)) => store(&applied, 0), Ok(None) => {} Err(_) => return, } diff --git a/rs/moq-hls/src/export/mod.rs b/rs/moq-hls/src/export/mod.rs index 143f1d1028a..9454719f91d 100644 --- a/rs/moq-hls/src/export/mod.rs +++ b/rs/moq-hls/src/export/mod.rs @@ -947,7 +947,6 @@ mod tests { // nothing numbers their segments. Once it does, every rendition is added. #[tokio::test(start_paused = true)] async fn renditions_wait_for_the_join_interval() { - kio::clock::host_time(); let origin = produce_origin(); let broadcast = origin .route("live", moq_net::origin::RouteOptions::default()) @@ -1032,7 +1031,6 @@ mod tests { // and makes every recording cursor see a Removed followed by an Added. #[tokio::test(start_paused = true)] async fn estimate_only_catalog_update_keeps_the_rendition() { - kio::clock::host_time(); let origin = produce_origin(); let _broadcast = origin .route("live", moq_net::origin::RouteOptions::default()) @@ -1103,7 +1101,6 @@ mod tests { // (its timescale derives from the framerate) and churn every recording cursor mid-stream. #[tokio::test(start_paused = true)] async fn estimated_framerate_keeps_the_rendition() { - kio::clock::host_time(); let origin = produce_origin(); let _broadcast = origin .route("live", moq_net::origin::RouteOptions::default()) @@ -1168,7 +1165,6 @@ mod tests { // longer matches. #[tokio::test(start_paused = true)] async fn decoder_config_change_rebuilds_the_rendition() { - kio::clock::host_time(); let origin = produce_origin(); let _broadcast = origin .route("live", moq_net::origin::RouteOptions::default()) @@ -1232,9 +1228,8 @@ mod tests { // The whole fetch-on-demand path in process: a broadcast publishes media through the // catalog (which declares its join interval), the Broadcaster renders playlists from the // groups' timestamps, and a segment request fetches and transmuxes exactly its groups. - #[tokio::test] + #[tokio::test(start_paused = true)] async fn master_lists_only_playable_renditions() { - kio::clock::host_time(); let origin = produce_origin(); let broadcast = origin .route("live", moq_net::origin::RouteOptions::default()) @@ -1873,9 +1868,8 @@ mod tests { // A reconfigure that changes the init bytes changes the init URL, so a cache can never hand a // player the previous init under the new one. The old URL keeps naming the old bytes while the // playlist still lists the segments muxed under them. - #[tokio::test] + #[tokio::test(start_paused = true)] async fn a_reconfigure_versions_the_init_url() { - kio::clock::host_time(); let origin = produce_origin(); let broadcast = origin .route("live", moq_net::origin::RouteOptions::default()) @@ -2166,9 +2160,8 @@ mod tests { /// Indexing reads every group of the media, but a stats session on the source's origin /// counts only the catalog and what a client fetches. - #[tokio::test] + #[tokio::test(start_paused = true)] async fn a_metered_source_counts_only_what_clients_fetch() { - kio::clock::host_time(); let origin = produce_origin(); let broadcast = origin .route("live", moq_net::origin::RouteOptions::default()) @@ -2678,9 +2671,8 @@ mod tests { } // A finished broadcast renders a static presentation. - #[tokio::test] + #[tokio::test(start_paused = true)] async fn dash_manifest_turns_static_when_finished() { - kio::clock::host_time(); let origin = produce_origin(); let broadcast = origin .route("live", moq_net::origin::RouteOptions::default()) diff --git a/rs/moq-hls/src/import.rs b/rs/moq-hls/src/import.rs index 88678186379..a2de5e98c69 100644 --- a/rs/moq-hls/src/import.rs +++ b/rs/moq-hls/src/import.rs @@ -2530,7 +2530,6 @@ mod tests { /// since the first placed the clock. A fresh offset would shift it by its own arrival. #[tokio::test(start_paused = true)] async fn a_replacement_importer_keeps_the_offset() { - kio::clock::host_time(); let (init, fragments) = fmp4_parts(2); let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); diff --git a/rs/moq-relay/src/web.rs b/rs/moq-relay/src/web.rs index 88f3f576eca..1e783d5fc99 100644 --- a/rs/moq-relay/src/web.rs +++ b/rs/moq-relay/src/web.rs @@ -2443,7 +2443,7 @@ mod tests { /// sockets, so this runs on the wall clock and bounds the answer from below. #[tokio::test] async fn an_unanswered_http_admission_is_refused_at_the_timeout() { - kio::clock::host_time(); + kio::clock::host_time(); // real HTTP sockets use tokio::io::{AsyncReadExt, AsyncWriteExt}; let timeout = std::time::Duration::from_millis(200); diff --git a/rs/moq-relay/tests/integration/shutdown_signal.rs b/rs/moq-relay/tests/integration/shutdown_signal.rs index 4127f328288..a1c505c62ba 100644 --- a/rs/moq-relay/tests/integration/shutdown_signal.rs +++ b/rs/moq-relay/tests/integration/shutdown_signal.rs @@ -70,7 +70,7 @@ fn run_test + 'static>(test: fn() -> F) { #[test] fn sigint_drains_sessions_before_exiting() { - kio::clock::host_time(); + kio::clock::host_time(); // a real relay on real sockets let _sigint = SIGINT.lock().unwrap_or_else(|e| e.into_inner()); run_test(sigint_drains_sessions_before_exiting_inner); } @@ -110,7 +110,7 @@ fn a_failed_startup_leaves_the_signals_alone() { /// drains on its own trigger. #[test] fn an_embedder_owns_the_signals() { - kio::clock::host_time(); + kio::clock::host_time(); // a real relay on real sockets const CHILD: &str = "MOQ_RELAY_EMBEDDER_SIGNALS_CHILD"; if std::env::var_os(CHILD).is_some() { run_test(a_relay_without_signals_leaves_sigint_alone_inner); @@ -132,7 +132,7 @@ fn an_embedder_owns_the_signals() { #[test] fn a_session_arriving_mid_drain_gets_what_is_left() { - kio::clock::host_time(); + kio::clock::host_time(); // a real relay on real sockets run_test(a_session_arriving_mid_drain_gets_what_is_left_inner); } @@ -143,13 +143,13 @@ fn a_drain_ends_once_every_session_leaves() { #[test] fn a_trigger_before_run_keeps_the_deadline() { - kio::clock::host_time(); + kio::clock::host_time(); // a real relay on real sockets run_test(a_trigger_before_run_keeps_the_deadline_inner); } #[test] fn a_reconnecting_client_rides_out_a_restart() { - kio::clock::host_time(); + kio::clock::host_time(); // a real relay on real sockets run_test(a_reconnecting_client_rides_out_a_restart_inner); } diff --git a/rs/moq-rtmp/src/server.rs b/rs/moq-rtmp/src/server.rs index ffc4990937d..87c948fae69 100644 --- a/rs/moq-rtmp/src/server.rs +++ b/rs/moq-rtmp/src/server.rs @@ -2800,7 +2800,7 @@ mod tests { /// until it leaves. #[tokio::test] async fn a_reconnect_replaces_the_stale_publish() { - kio::clock::host_time(); + kio::clock::host_time(); // real RTMP over TCP let mut server = Server::bind("127.0.0.1:0".parse().unwrap()).await.unwrap(); let addr = server.local_addr().unwrap(); let origin = moq_tokio::origin::spawn(); diff --git a/rs/moq-rtsp/src/client.rs b/rs/moq-rtsp/src/client.rs index b194e6eca82..2ea8bb7318a 100644 --- a/rs/moq-rtsp/src/client.rs +++ b/rs/moq-rtsp/src/client.rs @@ -1046,7 +1046,7 @@ mod tests { /// far ahead as it paused, which continues the timeline rather than jumping it. #[tokio::test] async fn a_paused_stream_is_not_a_jump() { - kio::clock::host_time(); + kio::clock::host_time(); // real RTSP sockets, and the pause is real time let server = Server::bind(vec![Media::H264 { sprop: true }]).await; let (broadcast, catalog) = broadcast(); let mut client = Client::new(server.url()).unwrap(); diff --git a/rs/moq-srt/src/server.rs b/rs/moq-srt/src/server.rs index 8c008bd2b2c..836253deddd 100644 --- a/rs/moq-srt/src/server.rs +++ b/rs/moq-srt/src/server.rs @@ -1198,7 +1198,7 @@ mod tests { /// task and its socket until the follow runs out. #[tokio::test] async fn a_caller_hanging_up_during_the_follow_ends_the_egress() { - kio::clock::host_time(); + kio::clock::host_time(); // real SRT sockets let (origin, driver) = moq_net::origin::Producer::new(moq_net::origin::Config::default()); tokio::spawn(moq_net::time::run(driver)); let mut live = Instance::publish(&origin, "a.aac"); diff --git a/rs/moq-tokio/src/server.rs b/rs/moq-tokio/src/server.rs index cdcb51a9a6a..d794fb5dfa0 100644 --- a/rs/moq-tokio/src/server.rs +++ b/rs/moq-tokio/src/server.rs @@ -2709,7 +2709,7 @@ mod tests { #[cfg(all(feature = "uds", unix))] #[tokio::test] async fn unix_listener_serves_the_configured_publisher() { - kio::clock::host_time(); + kio::clock::host_time(); // a real Unix socket use rand::RngExt; // macOS caps AF_UNIX paths near 104 bytes and the system temp dir is long, diff --git a/rs/moq-tokio/src/tcp.rs b/rs/moq-tokio/src/tcp.rs index d5f634c14c1..fe9d68488d9 100644 --- a/rs/moq-tokio/src/tcp.rs +++ b/rs/moq-tokio/src/tcp.rs @@ -663,7 +663,7 @@ mod tests { /// through to the loopback listener within the stagger delay. #[tokio::test] async fn failover_recovers_from_blackhole_candidate() { - kio::clock::host_time(); + kio::clock::host_time(); // a real dial that blackholes let listener = Listener::bind("127.0.0.1:0".parse().unwrap()) .await .expect("bind listener") diff --git a/rs/moq-tokio/src/websocket.rs b/rs/moq-tokio/src/websocket.rs index 1670aba1fb0..3e26926ab96 100644 --- a/rs/moq-tokio/src/websocket.rs +++ b/rs/moq-tokio/src/websocket.rs @@ -874,31 +874,31 @@ mod tests { #[tokio::test] async fn tls_host_name_override_dials_url_address() { - kio::clock::host_time(); + kio::clock::host_time(); // real TLS over loopback check_tls_authority("127.0.0.1", false, Some("relay.example")).await; } #[tokio::test] async fn fixed_addresses_keep_tls_name_and_request_host() { - kio::clock::host_time(); + kio::clock::host_time(); // real TLS over loopback check_tls_authority("relay.example", true, None).await; } #[tokio::test] async fn ipv6_literal() { - kio::clock::host_time(); + kio::clock::host_time(); // real TLS over loopback check_tls_authority("[::1]", false, None).await; } #[tokio::test] async fn ipv6_literal_fixed_addresses() { - kio::clock::host_time(); + kio::clock::host_time(); // real TLS over loopback check_tls_authority("[::1]", true, None).await; } #[tokio::test] async fn ipv6_tls_host_name_override() { - kio::clock::host_time(); + kio::clock::host_time(); // real TLS over loopback check_tls_authority("[::1]", false, Some("::1")).await; } diff --git a/rs/moq-transcode/src/lib.rs b/rs/moq-transcode/src/lib.rs index 759436718ac..d62ab2fa77b 100644 --- a/rs/moq-transcode/src/lib.rs +++ b/rs/moq-transcode/src/lib.rs @@ -1019,7 +1019,6 @@ mod tests { /// do have to keep serving, and the passthrough entry has to follow. #[tokio::test] async fn ladder_follows_a_source_resize() { - kio::clock::host_time(); let mut source = source_catalog(640, 360); let config = Config { diff --git a/rs/moq-video/src/decode/consumer.rs b/rs/moq-video/src/decode/consumer.rs index 7b81c5566d7..5165cd1205a 100644 --- a/rs/moq-video/src/decode/consumer.rs +++ b/rs/moq-video/src/decode/consumer.rs @@ -985,7 +985,6 @@ mod tests { #[cfg(not(apple))] #[tokio::test] async fn a_cancelled_read_resumes_the_flush_in_flight() { - kio::clock::host_time(); probe::prepare_blocking_flush(); let broadcast = moq_net::broadcast::Info::new().produce(); let track = broadcast @@ -1227,7 +1226,6 @@ mod tests { /// then the max-delay decoder's once it has decoded the sweep before them. #[tokio::test] async fn a_cold_join_hands_over_to_the_max_delay_decoder() { - kio::clock::host_time(); let (broadcast, mut track) = live_track(); let published = track.clone(); group(&mut track, 0, &[0, 1, 2, 3]).finish().unwrap(); @@ -1260,7 +1258,6 @@ mod tests { /// max-delay decoder still starts at the group before it, and takes over. #[tokio::test] async fn a_cold_join_whose_live_edge_arrives_first_starts_a_group_earlier() { - kio::clock::host_time(); let (broadcast, mut track) = live_track(); let mut live = group(&mut track, 1, &[4, 5]); @@ -1306,7 +1303,6 @@ mod tests { /// never alternating in between. #[tokio::test] async fn a_short_stall_skips_only_the_min_delay_decoder() { - kio::clock::host_time(); let (broadcast, mut track) = live_track(); let mut consumer = dual( &broadcast, @@ -1334,11 +1330,15 @@ mod tests { // leading by more than a frame, so it is shown at once. let mut live = group(&mut track, 2, &[8]); shown.push(read(&mut consumer).await); + // Mocked time, so the timeout fires only once every task has run, well inside the + // sweep; no decode runs while the max-delay decoder waits on the stalled group. + tokio::time::pause(); assert!( kio::clock::timeout(std::time::Duration::from_millis(100), consumer.next_frame()) .await .is_err() ); + tokio::time::resume(); assert_eq!( consumer.selector.source(), Lane::Min, @@ -1372,7 +1372,6 @@ mod tests { /// late frames are never shown, and the max-delay decoder still takes over. #[tokio::test] async fn a_long_stall_skips_both_decoders() { - kio::clock::host_time(); let (broadcast, mut track) = live_track(); let mut consumer = dual( &broadcast, diff --git a/rs/moq-video/src/encode/producer.rs b/rs/moq-video/src/encode/producer.rs index fb21f0dd466..c0900f58191 100644 --- a/rs/moq-video/src/encode/producer.rs +++ b/rs/moq-video/src/encode/producer.rs @@ -1376,7 +1376,6 @@ mod tests { /// acquisition: not zero, and not the later instant the loop dequeued it. #[tokio::test] async fn a_late_first_frame_publishes_its_acquisition() { - kio::clock::host_time(); tokio::task::LocalSet::new() .run_until(async { let fixture = Fixture::start(Duration::from_secs(5), kio::clock::wall()).await; @@ -1455,7 +1454,6 @@ mod tests { /// broadcast forward with the device's spacing instead of rewinding it. #[tokio::test] async fn a_device_clock_restart_continues_forward() { - kio::clock::host_time(); tokio::task::LocalSet::new() .run_until(async { let fixture = Fixture::start(Duration::from_secs(1), kio::clock::wall()).await; @@ -1499,7 +1497,6 @@ mod tests { /// frame after a resume lands after the idle gap, at its own acquisition. #[tokio::test] async fn a_restart_after_idle_keeps_the_gap() { - kio::clock::host_time(); tokio::task::LocalSet::new() .run_until(async { let idle = Duration::from_millis(300); From 35e8436dd8f01cc63d45d2124bbc18dd28283322 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 22:44:29 -0800 Subject: [PATCH 3/4] test(ffi): format the applied-bitrate wait Co-Authored-By: Claude Opus 5.5 --- rs/moq-ffi/src/test.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index a4bff0a21c9..cbe6500de9b 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -7552,9 +7552,12 @@ async fn video_encoder_follows_a_shrinking_grant() { }) .map(|followed| followed.is_ok()) }); - let open = kio::clock::timeout(TIMEOUT, followed) - .await - .unwrap_or_else(|_| panic!("encoder did not follow the shrinking grant, last applied {}", *applied.read())); + let open = kio::clock::timeout(TIMEOUT, followed).await.unwrap_or_else(|_| { + panic!( + "encoder did not follow the shrinking grant, last applied {}", + *applied.read() + ) + }); assert!(open, "the applied bitrate closed"); video.finish().unwrap(); From 8f08404a2c0347bbe070e5aa7c256eefd9ccd55c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 8 Nov 2026 15:02:04 -0800 Subject: [PATCH 4/4] test(hls): paused waits park instead of yielding, so their timeouts fire A yield_now loop keeps a paused runtime busy, so tokio never advances the clock and the surrounding timeout can't expire. master_lists_only_playable_renditions now awaits the rendition's Added event; the disabled-rendition wait and until_finished park on a kio sleep like their siblings. Co-Authored-By: Claude Opus 5.5 --- rs/moq-hls/src/export/mod.rs | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/rs/moq-hls/src/export/mod.rs b/rs/moq-hls/src/export/mod.rs index 9454719f91d..3a898e25dce 100644 --- a/rs/moq-hls/src/export/mod.rs +++ b/rs/moq-hls/src/export/mod.rs @@ -1239,16 +1239,12 @@ mod tests { let mut media = video(&catalog, broadcast.broadcast(), "video0", video_config()); let broadcaster = Broadcaster::spawned(crate::resolved(&origin, "live").await, Config::default()); - let rendition = kio::clock::timeout(Duration::from_secs(5), async { - loop { - if let Some(rendition) = broadcaster.rendition(Kind::Video, "video0") { - break rendition; - } - tokio::task::yield_now().await; - } - }) - .await - .expect("rendition discovered from the catalog"); + let added = kio::clock::timeout(Duration::from_secs(5), broadcaster.renditions().next()) + .await + .expect("rendition discovered from the catalog"); + let Ok(Some(renditions::Event::Added(rendition))) = added else { + panic!("expected an Added event"); + }; assert!( rendition.media_playlist(None).unwrap().is_none(), @@ -1365,7 +1361,7 @@ mod tests { media.set(disabled).unwrap(); kio::clock::timeout(Duration::from_secs(5), async { while listed() { - tokio::task::yield_now().await; + kio::clock::sleep(Duration::from_millis(10)).await; } }) .await @@ -3185,14 +3181,13 @@ mod tests { } async fn until_finished(rendition: &Rendition) { - let deadline = kio::clock::now() + Duration::from_secs(5); - loop { - if rendition.snapshot().finished { - return; + kio::clock::timeout(Duration::from_secs(5), async { + while !rendition.snapshot().finished { + kio::clock::sleep(Duration::from_millis(10)).await; } - assert!(kio::clock::now() < deadline, "the playlist did not end"); - tokio::task::yield_now().await; - } + }) + .await + .expect("the playlist did not end"); } /// A sibling-media fixture: the catalog broadcast `live` lists `video0`, and its media lives in