1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! Test-only helpers shared across modules.
use ;
/// Global mutex to serialize tests that mutate process-global state.
///
/// In particular, many tests change the current working directory via
/// `std::env::set_current_dir`, which is process-global and will race when
/// Rust tests run in parallel.
/// Global mutex to serialize tests that install a capturing tracing subscriber.
///
/// `tracing::subscriber::set_default` installs the subscriber thread-locally,
/// but the *max-level hint* it publishes is process-global: while a WARN-only
/// capture guard is held on any thread, `info!` callsites are disabled on every
/// thread. Two capture tests that overlap therefore silently steal each other's
/// records — a WARN-scoped capture blanks the INFO events another test is
/// asserting on, with no error and no ordering the test itself can see.
///
/// Every test that installs a capturing subscriber holds this lock for the
/// lifetime of its guard, so at most one is ever live. It is a
/// [`tokio::sync::Mutex`] because those guards are held across `.await` points,
/// and it is runtime-independent so tests on separate `#[tokio::test]` runtimes
/// still exclude each other.
/// A process-global subscriber that records nothing but decides nothing either.
///
/// Its whole purpose is `register_callsite`: returning [`Interest::sometimes`]
/// keeps every callsite in the "ask each time" state, so a thread-local capture
/// subscriber installed later still gets consulted.
;
/// Make thread-local tracing capture reliable under a parallel test suite.
///
/// Callsite *interest* is cached process-globally the first time a callsite is
/// reached, using whatever subscriber that thread happened to have — and a
/// thread with no subscriber at all answers [`Interest::never`], which disables
/// that callsite permanently, on every thread. With hundreds of tests exercising
/// the same code concurrently, an `info!` a capture test asserts on can be
/// silently switched off before that test ever starts. The symptom is an
/// assertion that passes in isolation and fails in the full suite purely on
/// ordering.
///
/// Two things together make it deterministic:
///
/// 1. a global [`AlwaysAskSubscriber`], so a bare thread can never cache
/// `never`; and
/// 2. a cache rebuild against *this* test's subscriber, which clears any
/// `never` a WARN-scoped capture cached earlier.
///
/// Call it immediately after installing a capturing subscriber, while holding
/// [`tracing_capture_lock`].