aboutsummaryrefslogtreecommitdiff
path: root/tests/filters_are_not_reevaluated_for_the_same_span.rs
blob: e9b2529b8f6f49ba3f6a839dabbbb2882f9b6882 (plain)
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
// Tests that depend on a count of the number of times their filter is evaluated
// cant exist in the same file with other tests that add subscribers to the
// registry. The registry was changed so that each time a new dispatcher is
// added all filters are re-evaluated. The tests being run only in separate
// threads with shared global state lets them interfere with each other
#[cfg(not(feature = "std"))]
extern crate std;

use tracing::{span, Level};
use tracing_mock::*;

use std::sync::{
    atomic::{AtomicUsize, Ordering},
    Arc,
};

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn filters_are_not_reevaluated_for_the_same_span() {
    // Asserts that the `span!` macro caches the result of calling
    // `Subscriber::enabled` for each span.
    let alice_count = Arc::new(AtomicUsize::new(0));
    let bob_count = Arc::new(AtomicUsize::new(0));
    let alice_count2 = alice_count.clone();
    let bob_count2 = bob_count.clone();

    let (subscriber, handle) = subscriber::mock()
        .with_filter(move |meta| match meta.name() {
            "alice" => {
                alice_count2.fetch_add(1, Ordering::Relaxed);
                false
            }
            "bob" => {
                bob_count2.fetch_add(1, Ordering::Relaxed);
                true
            }
            _ => false,
        })
        .run_with_handle();

    // Since this test is in its own file anyway, we can do this. Thus, this
    // test will work even with no-std.
    tracing::subscriber::set_global_default(subscriber).unwrap();

    // Enter "alice" and then "bob". The dispatcher expects to see "bob" but
    // not "alice."
    let alice = span!(Level::TRACE, "alice");
    let bob = alice.in_scope(|| {
        let bob = span!(Level::TRACE, "bob");
        bob.in_scope(|| ());
        bob
    });

    // The filter should have seen each span a single time.
    assert_eq!(alice_count.load(Ordering::Relaxed), 1);
    assert_eq!(bob_count.load(Ordering::Relaxed), 1);

    alice.in_scope(|| bob.in_scope(|| {}));

    // The subscriber should see "bob" again, but the filter should not have
    // been called.
    assert_eq!(alice_count.load(Ordering::Relaxed), 1);
    assert_eq!(bob_count.load(Ordering::Relaxed), 1);

    bob.in_scope(|| {});
    assert_eq!(alice_count.load(Ordering::Relaxed), 1);
    assert_eq!(bob_count.load(Ordering::Relaxed), 1);

    handle.assert_finished();
}