mirror of
https://github.com/tokio-rs/tracing.git
synced 2025-10-01 15:00:33 +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 FILTER2_CALLS: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
fn filter1<N>(_: &Metadata, _: &span::Context<N>) -> bool {
|
||||
FILTER1_CALLS.fetch_add(1, Ordering::Relaxed);
|
||||
true
|
||||
enum Filter {
|
||||
One,
|
||||
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 {
|
||||
FILTER2_CALLS.fetch_add(1, Ordering::Relaxed);
|
||||
true
|
||||
fn enabled(&self, _: &Metadata, _: &Context<N>) -> bool {
|
||||
match self {
|
||||
Filter::One => FILTER1_CALLS.fetch_add(1, Ordering::Relaxed),
|
||||
Filter::Two => FILTER2_CALLS.fetch_add(1, Ordering::Relaxed),
|
||||
};
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fn event() {
|
||||
trace!("my event");
|
||||
}
|
||||
|
||||
let subscriber = FmtSubscriber::builder()
|
||||
.compact()
|
||||
.with_filter(filter1 as fn(&Metadata, &span::Context<_>) -> bool)
|
||||
.with_filter(Filter::One)
|
||||
.with_filter_reloading();
|
||||
let handle = subscriber.reload_handle();
|
||||
let subscriber = Dispatch::new(subscriber.finish());
|
||||
@ -193,9 +200,7 @@ mod test {
|
||||
assert_eq!(FILTER1_CALLS.load(Ordering::Relaxed), 1);
|
||||
assert_eq!(FILTER2_CALLS.load(Ordering::Relaxed), 0);
|
||||
|
||||
handle
|
||||
.reload(filter2 as fn(&Metadata, &span::Context<_>) -> bool)
|
||||
.expect("should reload");
|
||||
handle.reload(Filter::Two).expect("should reload");
|
||||
|
||||
event();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user