]> git.proxmox.com Git - rustc.git/blob - vendor/tracing-attributes/tests/async_fn.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / vendor / tracing-attributes / tests / async_fn.rs
1 #[path = "../../tracing-futures/tests/support.rs"]
2 // we don't use some of the test support functions, but `tracing-futures` does.
3 #[allow(dead_code)]
4 mod support;
5 use support::*;
6
7 use tracing::subscriber::with_default;
8 use tracing_attributes::instrument;
9
10 #[instrument]
11 async fn test_async_fn(polls: usize) -> Result<(), ()> {
12 let future = PollN::new_ok(polls);
13 tracing::trace!(awaiting = true);
14 future.await
15 }
16
17 #[test]
18 fn async_fn_only_enters_for_polls() {
19 let (subscriber, handle) = subscriber::mock()
20 .new_span(span::mock().named("test_async_fn"))
21 .enter(span::mock().named("test_async_fn"))
22 .event(event::mock().with_fields(field::mock("awaiting").with_value(&true)))
23 .exit(span::mock().named("test_async_fn"))
24 .enter(span::mock().named("test_async_fn"))
25 .exit(span::mock().named("test_async_fn"))
26 .drop_span(span::mock().named("test_async_fn"))
27 .done()
28 .run_with_handle();
29 with_default(subscriber, || {
30 block_on_future(async { test_async_fn(2).await }).unwrap();
31 });
32 handle.assert_finished();
33 }
34
35 #[test]
36 fn async_fn_nested() {
37 #[instrument]
38 async fn test_async_fns_nested() {
39 test_async_fns_nested_other().await
40 }
41
42 #[instrument]
43 async fn test_async_fns_nested_other() {
44 tracing::trace!(nested = true);
45 }
46
47 let span = span::mock().named("test_async_fns_nested");
48 let span2 = span::mock().named("test_async_fns_nested_other");
49 let (subscriber, handle) = subscriber::mock()
50 .new_span(span.clone())
51 .enter(span.clone())
52 .new_span(span2.clone())
53 .enter(span2.clone())
54 .event(event::mock().with_fields(field::mock("nested").with_value(&true)))
55 .exit(span2.clone())
56 .drop_span(span2)
57 .exit(span.clone())
58 .drop_span(span)
59 .done()
60 .run_with_handle();
61
62 with_default(subscriber, || {
63 block_on_future(async { test_async_fns_nested().await });
64 });
65
66 handle.assert_finished();
67 }
68
69 #[test]
70 fn async_fn_with_async_trait() {
71 use async_trait::async_trait;
72
73 // test the correctness of the metadata obtained by #[instrument]
74 // (function name, functions parameters) when async-trait is used
75 #[async_trait]
76 pub trait TestA {
77 async fn foo(&mut self, v: usize);
78 }
79
80 // test nesting of async fns with aync-trait
81 #[async_trait]
82 pub trait TestB {
83 async fn bar(&self);
84 }
85
86 // test skip(self) with async-await
87 #[async_trait]
88 pub trait TestC {
89 async fn baz(&self);
90 }
91
92 #[derive(Debug)]
93 struct TestImpl(usize);
94
95 #[async_trait]
96 impl TestA for TestImpl {
97 #[instrument]
98 async fn foo(&mut self, v: usize) {
99 self.baz().await;
100 self.0 = v;
101 self.bar().await
102 }
103 }
104
105 #[async_trait]
106 impl TestB for TestImpl {
107 #[instrument]
108 async fn bar(&self) {
109 tracing::trace!(val = self.0);
110 }
111 }
112
113 #[async_trait]
114 impl TestC for TestImpl {
115 #[instrument(skip(self))]
116 async fn baz(&self) {
117 tracing::trace!(val = self.0);
118 }
119 }
120
121 let span = span::mock().named("foo");
122 let span2 = span::mock().named("bar");
123 let span3 = span::mock().named("baz");
124 let (subscriber, handle) = subscriber::mock()
125 .new_span(
126 span.clone()
127 .with_field(field::mock("self"))
128 .with_field(field::mock("v")),
129 )
130 .enter(span.clone())
131 .new_span(span3.clone())
132 .enter(span3.clone())
133 .event(event::mock().with_fields(field::mock("val").with_value(&2u64)))
134 .exit(span3.clone())
135 .drop_span(span3)
136 .new_span(span2.clone().with_field(field::mock("self")))
137 .enter(span2.clone())
138 .event(event::mock().with_fields(field::mock("val").with_value(&5u64)))
139 .exit(span2.clone())
140 .drop_span(span2)
141 .exit(span.clone())
142 .drop_span(span)
143 .done()
144 .run_with_handle();
145
146 with_default(subscriber, || {
147 let mut test = TestImpl(2);
148 block_on_future(async { test.foo(5).await });
149 });
150
151 handle.assert_finished();
152 }
153
154 #[test]
155 fn async_fn_with_async_trait_and_fields_expressions() {
156 use async_trait::async_trait;
157
158 #[async_trait]
159 pub trait Test {
160 async fn call(&mut self, v: usize);
161 }
162
163 #[derive(Clone, Debug)]
164 struct TestImpl;
165
166 impl TestImpl {
167 fn foo(&self) -> usize {
168 42
169 }
170 }
171
172 #[async_trait]
173 impl Test for TestImpl {
174 // check that self is correctly handled, even when using async_trait
175 #[instrument(fields(val=self.foo(), test=%v+5))]
176 async fn call(&mut self, v: usize) {}
177 }
178
179 let span = span::mock().named("call");
180 let (subscriber, handle) = subscriber::mock()
181 .new_span(
182 span.clone().with_field(
183 field::mock("v")
184 .with_value(&tracing::field::debug(5))
185 .and(field::mock("test").with_value(&tracing::field::debug(10)))
186 .and(field::mock("val").with_value(&42u64)),
187 ),
188 )
189 .enter(span.clone())
190 .exit(span.clone())
191 .drop_span(span)
192 .done()
193 .run_with_handle();
194
195 with_default(subscriber, || {
196 block_on_future(async { TestImpl.call(5).await });
197 });
198
199 handle.assert_finished();
200 }
201
202 #[test]
203 fn async_fn_with_async_trait_and_fields_expressions_with_generic_parameter() {
204 use async_trait::async_trait;
205
206 #[async_trait]
207 pub trait Test {
208 async fn call();
209 async fn call_with_self(&self);
210 async fn call_with_mut_self(&mut self);
211 }
212
213 #[derive(Clone, Debug)]
214 struct TestImpl;
215
216 #[async_trait]
217 impl Test for TestImpl {
218 // instrumenting this is currently not possible, see https://github.com/tokio-rs/tracing/issues/864#issuecomment-667508801
219 //#[instrument(fields(Self=std::any::type_name::<Self>()))]
220 async fn call() {}
221
222 #[instrument(fields(Self=std::any::type_name::<Self>()))]
223 async fn call_with_self(&self) {}
224
225 #[instrument(fields(Self=std::any::type_name::<Self>()))]
226 async fn call_with_mut_self(&mut self) {}
227 }
228
229 //let span = span::mock().named("call");
230 let span2 = span::mock().named("call_with_self");
231 let span3 = span::mock().named("call_with_mut_self");
232 let (subscriber, handle) = subscriber::mock()
233 /*.new_span(span.clone()
234 .with_field(
235 field::mock("Self").with_value(&"TestImpler")))
236 .enter(span.clone())
237 .exit(span.clone())
238 .drop_span(span)*/
239 .new_span(
240 span2
241 .clone()
242 .with_field(field::mock("Self").with_value(&std::any::type_name::<TestImpl>())),
243 )
244 .enter(span2.clone())
245 .exit(span2.clone())
246 .drop_span(span2)
247 .new_span(
248 span3
249 .clone()
250 .with_field(field::mock("Self").with_value(&std::any::type_name::<TestImpl>())),
251 )
252 .enter(span3.clone())
253 .exit(span3.clone())
254 .drop_span(span3)
255 .done()
256 .run_with_handle();
257
258 with_default(subscriber, || {
259 block_on_future(async {
260 TestImpl::call().await;
261 TestImpl.call_with_self().await;
262 TestImpl.call_with_mut_self().await
263 });
264 });
265
266 handle.assert_finished();
267 }