mirror of
https://github.com/tokio-rs/tracing.git
synced 2025-10-02 07:20:35 +00:00
fmt: fix flakey test (#111)
## Motivation The `reload_handle` test in `tracing-fmt` is flakey. This is because reloading a filter now triggers a callsite registry rebuild; and using functions as filters means that the call counters are incremented when the registry is rebuilt. ## Solution This branch replaces the functions with an enum implementing `Filter`, which doesn't increment the call counters when `register_callsite` is called. This should make the tests no longer be flakey. Signed-off-by: Eliza Weisman <eliza@buoyant.io>
This commit is contained in:
parent
94d87ae072
commit
26a4317003
@ -163,23 +163,30 @@ mod test {
|
|||||||
static FILTER1_CALLS: AtomicUsize = AtomicUsize::new(0);
|
static FILTER1_CALLS: AtomicUsize = AtomicUsize::new(0);
|
||||||
static FILTER2_CALLS: AtomicUsize = AtomicUsize::new(0);
|
static FILTER2_CALLS: AtomicUsize = AtomicUsize::new(0);
|
||||||
|
|
||||||
fn filter1<N>(_: &Metadata, _: &span::Context<N>) -> bool {
|
enum Filter {
|
||||||
FILTER1_CALLS.fetch_add(1, Ordering::Relaxed);
|
One,
|
||||||
true
|
Two,
|
||||||
|
}
|
||||||
|
impl<N> filter::Filter<N> for Filter {
|
||||||
|
fn callsite_enabled(&self, _: &Metadata, _: &Context<N>) -> Interest {
|
||||||
|
Interest::sometimes()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn filter2<N>(_: &Metadata, _: &span::Context<N>) -> bool {
|
fn enabled(&self, _: &Metadata, _: &Context<N>) -> bool {
|
||||||
FILTER2_CALLS.fetch_add(1, Ordering::Relaxed);
|
match self {
|
||||||
|
Filter::One => FILTER1_CALLS.fetch_add(1, Ordering::Relaxed),
|
||||||
|
Filter::Two => FILTER2_CALLS.fetch_add(1, Ordering::Relaxed),
|
||||||
|
};
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
}
|
||||||
fn event() {
|
fn event() {
|
||||||
trace!("my event");
|
trace!("my event");
|
||||||
}
|
}
|
||||||
|
|
||||||
let subscriber = FmtSubscriber::builder()
|
let subscriber = FmtSubscriber::builder()
|
||||||
.compact()
|
.compact()
|
||||||
.with_filter(filter1 as fn(&Metadata, &span::Context<_>) -> bool)
|
.with_filter(Filter::One)
|
||||||
.with_filter_reloading();
|
.with_filter_reloading();
|
||||||
let handle = subscriber.reload_handle();
|
let handle = subscriber.reload_handle();
|
||||||
let subscriber = Dispatch::new(subscriber.finish());
|
let subscriber = Dispatch::new(subscriber.finish());
|
||||||
@ -193,9 +200,7 @@ mod test {
|
|||||||
assert_eq!(FILTER1_CALLS.load(Ordering::Relaxed), 1);
|
assert_eq!(FILTER1_CALLS.load(Ordering::Relaxed), 1);
|
||||||
assert_eq!(FILTER2_CALLS.load(Ordering::Relaxed), 0);
|
assert_eq!(FILTER2_CALLS.load(Ordering::Relaxed), 0);
|
||||||
|
|
||||||
handle
|
handle.reload(Filter::Two).expect("should reload");
|
||||||
.reload(filter2 as fn(&Metadata, &span::Context<_>) -> bool)
|
|
||||||
.expect("should reload");
|
|
||||||
|
|
||||||
event();
|
event();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user