diff --git a/tracing-core/src/callsite.rs b/tracing-core/src/callsite.rs index 77efb3fbf7..7098adefad 100644 --- a/tracing-core/src/callsite.rs +++ b/tracing-core/src/callsite.rs @@ -276,23 +276,11 @@ impl LinkedList { mod tests { use super::*; - #[derive(Eq, PartialEq)] - struct Cs1; - static CS1: Cs1 = Cs1; - static REG1: Registration = Registration::new(&CS1); + struct TestCallsite; + static CS1: TestCallsite = TestCallsite; + static CS2: TestCallsite = TestCallsite; - impl Callsite for Cs1 { - fn set_interest(&self, _interest: Interest) {} - fn metadata(&self) -> &Metadata<'_> { - unimplemented!("not needed for this test") - } - } - - struct Cs2; - static CS2: Cs2 = Cs2; - static REG2: Registration = Registration::new(&CS2); - - impl Callsite for Cs2 { + impl Callsite for TestCallsite { fn set_interest(&self, _interest: Interest) {} fn metadata(&self) -> &Metadata<'_> { unimplemented!("not needed for this test") @@ -301,6 +289,9 @@ mod tests { #[test] fn linked_list_push() { + static REG1: Registration = Registration::new(&CS1); + static REG2: Registration = Registration::new(&CS2); + let linked_list = LinkedList::new(); linked_list.push(®1); @@ -325,9 +316,69 @@ mod tests { }); } + #[test] + fn linked_list_push_several() { + static REG1: Registration = Registration::new(&CS1); + static REG2: Registration = Registration::new(&CS2); + static REG3: Registration = Registration::new(&CS1); + static REG4: Registration = Registration::new(&CS2); + + let linked_list = LinkedList::new(); + + fn expect<'a>( + callsites: &'a mut impl Iterator, + ) -> impl FnMut(&'static Registration) + 'a { + move |reg: &'static Registration| { + let ptr = callsites + .next() + .expect("list contained more than the expected number of registrations!"); + + assert!( + ptr::eq(reg, ptr), + "Registration pointers need to match ({:?} != {:?})", + reg, + ptr + ); + } + } + + linked_list.push(®1); + linked_list.push(®2); + let regs = [®2, ®1]; + let mut callsites = regs.iter().copied(); + linked_list.for_each(expect(&mut callsites)); + assert!( + callsites.next().is_none(), + "some registrations were expected but not present: {:?}", + callsites + ); + + linked_list.push(®3); + let regs = [®3, ®2, ®1]; + let mut callsites = regs.iter().copied(); + linked_list.for_each(expect(&mut callsites)); + assert!( + callsites.next().is_none(), + "some registrations were expected but not present: {:?}", + callsites + ); + + linked_list.push(®4); + let regs = [®4, ®3, ®2, ®1]; + let mut callsites = regs.iter().copied(); + linked_list.for_each(expect(&mut callsites)); + assert!( + callsites.next().is_none(), + "some registrations were expected but not present: {:?}", + callsites + ); + } + #[test] #[should_panic] fn linked_list_repeated() { + static REG1: Registration = Registration::new(&CS1); + let linked_list = LinkedList::new(); linked_list.push(®1);