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     struct Fut(Option<AssertSpanOnDrop>);
25 
26     impl Future for Fut {
27         type Output = ();
28 
29         fn poll(mut self: Pin<&mut Self>, _: &mut task::Context<'_>) -> task::Poll<Self::Output> {
30             self.set(Fut(None));
31             task::Poll::Ready(())
32         }
33     }
34 
35     let subscriber = subscriber::mock()
36         .enter(expect::span().named("foo"))
37         .event(expect::event().at_level(Level::INFO))
38         .exit(expect::span().named("foo"))
39         .enter(expect::span().named("foo"))
40         .exit(expect::span().named("foo"))
41         .drop_span(expect::span().named("foo"))
42         .enter(expect::span().named("bar"))
43         .event(expect::event().at_level(Level::INFO))
44         .exit(expect::span().named("bar"))
45         .drop_span(expect::span().named("bar"))
46         .only()
47         .run();
48 
49     with_default(subscriber, || {
50         // polled once
51         Fut(Some(AssertSpanOnDrop))
52             .instrument(tracing::span!(Level::TRACE, "foo"))
53             .now_or_never()
54             .unwrap();
55 
56         // never polled
57         drop(Fut(Some(AssertSpanOnDrop)).instrument(tracing::span!(Level::TRACE, "bar")));
58     });
59 }
60