From c63c01eba7520095f27e739d48c2645bc683190f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 1 Oct 2026 16:51:52 -0700 Subject: [PATCH 1/3] quest: claim test-flakes-2/warn-capture Co-Authored-By: Claude Opus 5.5 From c8db5fb6d4887f0fe5e38e44ca9f777a52779832 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 1 Oct 2026 17:12:27 -0700 Subject: [PATCH 2/3] moq-net: count drop WARNs through a global subscriber, per thread A scoped with_default subscriber let a parallel test reach the WARN callsite first on a thread with no subscriber, caching its interest as never for the whole process, so drop_unfinished_warns saw 0 WARNs. Co-Authored-By: Claude Opus 5.5 --- quest/m1/test-flakes-2/README.md | 1 - quest/m1/test-flakes-2/warn-capture.md | 14 --- rs/moq-net/src/model/group.rs | 2 +- rs/moq-net/src/model/test_tracing.rs | 136 +++++++++++++++---------- rs/moq-net/src/model/track.rs | 2 +- 5 files changed, 87 insertions(+), 68 deletions(-) delete mode 100644 quest/m1/test-flakes-2/warn-capture.md diff --git a/quest/m1/test-flakes-2/README.md b/quest/m1/test-flakes-2/README.md index 80843a51653..bfe1ae5b56a 100644 --- a/quest/m1/test-flakes-2/README.md +++ b/quest/m1/test-flakes-2/README.md @@ -33,7 +33,6 @@ Public API: none. Wire: none. - [moq-cli tests on a paused clock](/quest/m1/test-flakes-2/cli-paused-clock.md) - the fetch timeout and completion tests stop racing wall-clock budgets - [Subscription cut by disconnect](/quest/m1/test-flakes-2/subscription-cut.md) - a publisher disconnect never ends a subscription clean - [Media late join](/quest/m1/test-flakes-2/media-late-join.md) - `just test media` late join stays within one GOP, or the regression is fixed -- [Scoped WARN capture](/quest/m1/test-flakes-2/warn-capture.md) - the drop-unfinished tests count only their own WARNs - [moq-mux debounce clock](/quest/m1/test-flakes-2/mux-debounce-clock.md) - the TS export debounce test advances on the paused clock - [js/publish audio clock](/quest/m1/test-flakes-2/publish-audio-clock.md) - the audio encoder delay test runs on mock time - [WebSocket paused TLS dial](/quest/m1/test-flakes-2/websocket-paused-tls.md) - the fixed-address WebSocket tests stop pausing the clock over a real dial diff --git a/quest/m1/test-flakes-2/warn-capture.md b/quest/m1/test-flakes-2/warn-capture.md deleted file mode 100644 index d6f928e0c43..00000000000 --- a/quest/m1/test-flakes-2/warn-capture.md +++ /dev/null @@ -1,14 +0,0 @@ -# [XS] Scoped WARN capture - -## Goal - -moq-net `model::group::test::drop_unfinished_warns` and the `model::track` -test of the same name count only the WARNs their own code emits. - -## Plan - -Both count WARNs through a global tracing capture, so another test's WARN, -or a missed one, changes the count -([#4104](https://github.com/moq-dev/moq/pull/4104)). Capture per test, with a -scoped subscriber or a filter on the test's own span, instead of a -process-global count. diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index bb725dcba1d..5dfa179541e 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -2035,7 +2035,7 @@ mod test { drop(writer); drop(producer); }); - assert!(warns >= 1, "unfinished drop must emit unfinished-producer WARN"); + assert_eq!(warns, 1, "unfinished drop must emit one unfinished-producer WARN"); } #[test] diff --git a/rs/moq-net/src/model/test_tracing.rs b/rs/moq-net/src/model/test_tracing.rs index 63caa9d852c..37fcdaec490 100644 --- a/rs/moq-net/src/model/test_tracing.rs +++ b/rs/moq-net/src/model/test_tracing.rs @@ -1,80 +1,114 @@ //! Tracing helpers shared by model tests. -use std::sync::Arc; -use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering}; +use std::cell::RefCell; +use std::sync::Once; use tracing::field::{Field, Visit}; use tracing::span::{Attributes, Id, Record}; use tracing::{Event, Level, Metadata, Subscriber}; -/// Count WARN events whose `message` field contains `expected_message` while running `f`. +/// The capture active on this thread: the message to match and how many WARNs matched. +struct Capture { + expected: String, + hits: usize, +} + +thread_local! { + static CAPTURE: RefCell> = const { RefCell::new(None) }; +} + +/// Count WARN events emitted on this thread whose `message` field contains `expected_message` +/// while running `f`. /// -/// Uses only the existing `tracing` dependency (no tracing-subscriber). +/// The subscriber is process-global rather than scoped with `with_default`: tracing caches each +/// callsite's interest process-wide, so a parallel test reaching the WARN first on a thread +/// without a scoped subscriber disables it for every thread. Counting per thread keeps another +/// test's WARNs out. pub(crate) fn count_drop_warnings(expected_message: &str, f: impl FnOnce()) -> usize { - struct Count { - hits: Arc, - expected: String, - } + static INSTALL: Once = Once::new(); + INSTALL.call_once(|| tracing::subscriber::set_global_default(Warns).expect("no other global subscriber")); - struct Msg<'a> { - expected: &'a str, - matched: bool, - } + let capture = Capture { + expected: expected_message.to_owned(), + hits: 0, + }; + let prev = CAPTURE.replace(Some(capture)); + assert!(prev.is_none(), "count_drop_warnings does not nest"); - impl Visit for Msg<'_> { - fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) { - if field.name() == "message" { - let s = format!("{value:?}"); - if s.contains(self.expected) { - self.matched = true; - } - } - } + f(); - fn record_str(&mut self, field: &Field, value: &str) { - if field.name() == "message" && value.contains(self.expected) { - self.matched = true; - } - } - } + CAPTURE.take().expect("capture still installed").hits +} - impl Subscriber for Count { - fn enabled(&self, metadata: &Metadata<'_>) -> bool { - *metadata.level() == Level::WARN - } +/// Routes every WARN to the capture on the emitting thread, if any. +struct Warns; - fn new_span(&self, _span: &Attributes<'_>) -> Id { - Id::from_u64(1) - } +impl Subscriber for Warns { + fn enabled(&self, metadata: &Metadata<'_>) -> bool { + *metadata.level() == Level::WARN + } - fn record(&self, _span: &Id, _values: &Record<'_>) {} + fn new_span(&self, _span: &Attributes<'_>) -> Id { + Id::from_u64(1) + } + + fn record(&self, _span: &Id, _values: &Record<'_>) {} - fn record_follows_from(&self, _span: &Id, _follows: &Id) {} + fn record_follows_from(&self, _span: &Id, _follows: &Id) {} - fn event(&self, event: &Event<'_>) { + fn event(&self, event: &Event<'_>) { + CAPTURE.with_borrow_mut(|capture| { + let Some(capture) = capture else { return }; let mut msg = Msg { - expected: &self.expected, + expected: &capture.expected, matched: false, }; event.record(&mut msg); if msg.matched { - self.hits.fetch_add(1, AtomicOrdering::SeqCst); + capture.hits += 1; } + }); + } + + fn enter(&self, _span: &Id) {} + + fn exit(&self, _span: &Id) {} +} + +struct Msg<'a> { + expected: &'a str, + matched: bool, +} + +impl Visit for Msg<'_> { + fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) { + if field.name() == "message" && format!("{value:?}").contains(self.expected) { + self.matched = true; } + } - fn enter(&self, _span: &Id) {} + fn record_str(&mut self, field: &Field, value: &str) { + if field.name() == "message" && value.contains(self.expected) { + self.matched = true; + } + } +} + +mod tests { + use super::count_drop_warnings; - fn exit(&self, _span: &Id) {} + fn probe() { + tracing::warn!("test_tracing probe"); } - let hits = Arc::new(AtomicUsize::new(0)); - let expected = expected_message.to_owned(); - tracing::subscriber::with_default( - Count { - hits: hits.clone(), - expected, - }, - f, - ); - hits.load(AtomicOrdering::SeqCst) + /// Another thread without a capture reaching the WARN first must neither disable it for this + /// thread nor be counted here. + #[test] + fn counts_only_this_threads_warns() { + let warns = count_drop_warnings("test_tracing probe", || { + std::thread::spawn(probe).join().unwrap(); + probe(); + }); + assert_eq!(warns, 1); + } } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 097b162a4bc..3eb9bc8fa85 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -6841,7 +6841,7 @@ mod test { drop(writer); drop(producer); }); - assert!(warns >= 1, "unfinished drop must emit unfinished-producer WARN"); + assert_eq!(warns, 1, "unfinished drop must emit one unfinished-producer WARN"); } #[tokio::test] From 7a934933f201df8f8a4ae7371c903c1a4943898c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 1 Oct 2026 17:45:05 -0700 Subject: [PATCH 3/3] moq-net: rebuild callsite interest at each WARN capture Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/test_tracing.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rs/moq-net/src/model/test_tracing.rs b/rs/moq-net/src/model/test_tracing.rs index 37fcdaec490..c1d315f1032 100644 --- a/rs/moq-net/src/model/test_tracing.rs +++ b/rs/moq-net/src/model/test_tracing.rs @@ -27,6 +27,10 @@ thread_local! { pub(crate) fn count_drop_warnings(expected_message: &str, f: impl FnOnce()) -> usize { static INSTALL: Once = Once::new(); INSTALL.call_once(|| tracing::subscriber::set_global_default(Warns).expect("no other global subscriber")); + // A callsite first reached on another thread while the install above was still publishing + // sees no subscriber and is cached as disabled. Every capture starts after the install has + // completed, so rebuilding here re-enables it against the now-global subscriber. + tracing::callsite::rebuild_interest_cache(); let capture = Capture { expected: expected_message.to_owned(),