• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // These tests require the thread-local scoped dispatcher, which only works when
2 // we have a standard library. The behaviour being tested should be the same
3 // with the standard lib disabled.
4 #![cfg(feature = "std")]
5 
6 use std::{future::Future, pin::Pin, task};
7 
8 use futures::FutureExt as _;
9 use tracing::{subscriber::with_default, Instrument as _, Level};
10 use tracing_mock::*;
11 
12 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
13 #[test]
span_on_drop()14 fn span_on_drop() {
15     #[derive(Clone, Debug)]
16     struct AssertSpanOnDrop;
17 
18     impl Drop for AssertSpanOnDrop {
19         fn drop(&mut self) {
20             tracing::info!("Drop");
21         }
22     }
23 
24     #[allow(dead_code)] // Field not used, but logs on `Drop`
25     struct Fut(Option<AssertSpanOnDrop>);
26 
27     impl Future for Fut {
28         type Output = ();
29 
30         fn poll(mut self: Pin<&mut Self>, _: &mut task::Context<'_>) -> task::Poll<Self::Output> {
31             self.set(Fut(None));
32             task::Poll::Ready(())
33         }
34     }
35 
36     let subscriber = subscriber::mock()
37         .enter(expect::span().named("foo"))
38         .event(
39             expect::event()
40                 .with_ancestry(expect::has_contextual_parent("foo"))
41                 .at_level(Level::INFO),
42         )
43         .exit(expect::span().named("foo"))
44         .enter(expect::span().named("foo"))
45         .exit(expect::span().named("foo"))
46         .drop_span(expect::span().named("foo"))
47         .enter(expect::span().named("bar"))
48         .event(
49             expect::event()
50                 .with_ancestry(expect::has_contextual_parent("bar"))
51                 .at_level(Level::INFO),
52         )
53         .exit(expect::span().named("bar"))
54         .drop_span(expect::span().named("bar"))
55         .only()
56         .run();
57 
58     with_default(subscriber, || {
59         // polled once
60         Fut(Some(AssertSpanOnDrop))
61             .instrument(tracing::span!(Level::TRACE, "foo"))
62             .now_or_never()
63             .unwrap();
64 
65         // never polled
66         drop(Fut(Some(AssertSpanOnDrop)).instrument(tracing::span!(Level::TRACE, "bar")));
67     });
68 }
69