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
1 change: 0 additions & 1 deletion quest/m1/test-flakes-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- [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
- [moq-mux live import clock](/quest/m1/test-flakes-2/mux-live-import-clock.md) - the live import restart tests advance a paused clock instead of sleeping
Expand Down
14 changes: 0 additions & 14 deletions quest/m1/test-flakes-2/warn-capture.md

This file was deleted.

2 changes: 1 addition & 1 deletion rs/moq-net/src/model/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
140 changes: 89 additions & 51 deletions rs/moq-net/src/model/test_tracing.rs
Original file line number Diff line number Diff line change
@@ -1,80 +1,118 @@
//! 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<Option<Capture>> = 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<AtomicUsize>,
expected: String,
}
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();

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);
}
}
2 changes: 1 addition & 1 deletion rs/moq-net/src/model/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading