1 #[path = "../../tracing-futures/tests/support.rs"]
2 // we don't use some of the test support functions, but `tracing-futures` does.
7 use std
::{future::Future, pin::Pin, sync::Arc}
;
8 use tracing
::subscriber
::with_default
;
9 use tracing_attributes
::instrument
;
12 async
fn test_async_fn(polls
: usize) -> Result
<(), ()> {
13 let future
= PollN
::new_ok(polls
);
14 tracing
::trace
!(awaiting
= true);
19 fn async_fn_only_enters_for_polls() {
20 let (subscriber
, handle
) = subscriber
::mock()
21 .new_span(span
::mock().named("test_async_fn"))
22 .enter(span
::mock().named("test_async_fn"))
23 .event(event
::mock().with_fields(field
::mock("awaiting").with_value(&true)))
24 .exit(span
::mock().named("test_async_fn"))
25 .enter(span
::mock().named("test_async_fn"))
26 .exit(span
::mock().named("test_async_fn"))
27 .drop_span(span
::mock().named("test_async_fn"))
30 with_default(subscriber
, || {
31 block_on_future(async { test_async_fn(2).await }
).unwrap();
33 handle
.assert_finished();
37 fn async_fn_nested() {
39 async
fn test_async_fns_nested() {
40 test_async_fns_nested_other().await
44 async
fn test_async_fns_nested_other() {
45 tracing
::trace
!(nested
= true);
48 let span
= span
::mock().named("test_async_fns_nested");
49 let span2
= span
::mock().named("test_async_fns_nested_other");
50 let (subscriber
, handle
) = subscriber
::mock()
51 .new_span(span
.clone())
53 .new_span(span2
.clone())
55 .event(event
::mock().with_fields(field
::mock("nested").with_value(&true)))
63 with_default(subscriber
, || {
64 block_on_future(async { test_async_fns_nested().await }
);
67 handle
.assert_finished();
71 fn async_fn_with_async_trait() {
72 use async_trait
::async_trait
;
74 // test the correctness of the metadata obtained by #[instrument]
75 // (function name, functions parameters) when async-trait is used
78 async
fn foo(&mut self, v
: usize);
81 // test nesting of async fns with aync-trait
87 // test skip(self) with async-await
94 struct TestImpl(usize);
97 impl TestA
for TestImpl
{
99 async
fn foo(&mut self, v
: usize) {
107 impl TestB
for TestImpl
{
109 async
fn bar(&self) {
110 tracing
::trace
!(val
= self.0);
115 impl TestC
for TestImpl
{
116 #[instrument(skip(self))]
117 async
fn baz(&self) {
118 tracing
::trace
!(val
= self.0);
122 let span
= span
::mock().named("foo");
123 let span2
= span
::mock().named("bar");
124 let span3
= span
::mock().named("baz");
125 let (subscriber
, handle
) = subscriber
::mock()
128 .with_field(field
::mock("self"))
129 .with_field(field
::mock("v")),
132 .new_span(span3
.clone())
133 .enter(span3
.clone())
134 .event(event
::mock().with_fields(field
::mock("val").with_value(&2u64)))
137 .new_span(span2
.clone().with_field(field
::mock("self")))
138 .enter(span2
.clone())
139 .event(event
::mock().with_fields(field
::mock("val").with_value(&5u64)))
147 with_default(subscriber
, || {
148 let mut test
= TestImpl(2);
149 block_on_future(async { test.foo(5).await }
);
152 handle
.assert_finished();
156 fn async_fn_with_async_trait_and_fields_expressions() {
157 use async_trait
::async_trait
;
161 async
fn call(&mut self, v
: usize);
164 #[derive(Clone, Debug)]
168 fn foo(&self) -> usize {
174 impl Test
for TestImpl
{
175 // check that self is correctly handled, even when using async_trait
176 #[instrument(fields(val=self.foo(), val2=Self::clone(self).foo(), test=%_v+5))]
177 async
fn call(&mut self, _v
: usize) {}
180 let span
= span
::mock().named("call");
181 let (subscriber
, handle
) = subscriber
::mock()
183 span
.clone().with_field(
186 .and(field
::mock("test").with_value(&tracing
::field
::debug(10)))
187 .and(field
::mock("val").with_value(&42u64))
188 .and(field
::mock("val2").with_value(&42u64)),
197 with_default(subscriber
, || {
198 block_on_future(async { TestImpl.call(5).await }
);
201 handle
.assert_finished();
205 fn async_fn_with_async_trait_and_fields_expressions_with_generic_parameter() {
206 use async_trait
::async_trait
;
211 async
fn call_with_self(&self);
212 async
fn call_with_mut_self(&mut self);
215 #[derive(Clone, Debug)]
218 // we also test sync functions that return futures, as they should be handled just like
219 // async-trait (>= 0.1.44) functions
221 #[instrument(fields(Self=std::any::type_name::<Self>()))]
222 fn sync_fun(&self) -> Pin
<Box
<dyn Future
<Output
= ()> + Send
+ '_
>> {
223 let val
= self.clone();
224 Box
::pin(async
move {
231 impl Test
for TestImpl
{
232 // instrumenting this is currently not possible, see https://github.com/tokio-rs/tracing/issues/864#issuecomment-667508801
233 //#[instrument(fields(Self=std::any::type_name::<Self>()))]
236 #[instrument(fields(Self=std::any::type_name::<Self>()))]
237 async
fn call_with_self(&self) {
238 self.sync_fun().await
;
241 #[instrument(fields(Self=std::any::type_name::<Self>()))]
242 async
fn call_with_mut_self(&mut self) {}
245 //let span = span::mock().named("call");
246 let span2
= span
::mock().named("call_with_self");
247 let span3
= span
::mock().named("call_with_mut_self");
248 let span4
= span
::mock().named("sync_fun");
249 let (subscriber
, handle
) = subscriber
::mock()
250 /*.new_span(span.clone()
252 field::mock("Self").with_value(&"TestImpler")))
259 .with_field(field
::mock("Self").with_value(&std
::any
::type_name
::<TestImpl
>())),
261 .enter(span2
.clone())
265 .with_field(field
::mock("Self").with_value(&std
::any
::type_name
::<TestImpl
>())),
267 .enter(span4
.clone())
274 .with_field(field
::mock("Self").with_value(&std
::any
::type_name
::<TestImpl
>())),
276 .enter(span3
.clone())
282 with_default(subscriber
, || {
283 block_on_future(async
{
284 TestImpl
::call().await
;
285 TestImpl
.call_with_self().await
;
286 TestImpl
.call_with_mut_self().await
290 handle
.assert_finished();
294 fn out_of_scope_fields() {
295 // Reproduces tokio-rs/tracing#1296
302 #[instrument(skip(self, _req), fields(app_id))]
303 fn call(&mut self, _req
: ()) -> Pin
<Box
<dyn Future
<Output
= Arc
<()>> + Send
+ Sync
>> {
305 let metrics
= self.metrics
.clone();
307 Box
::pin(async
move {
309 metrics
// cannot find value `metrics` in this scope
314 let span
= span
::mock().named("call");
315 let (subscriber
, handle
) = subscriber
::mock()
316 .new_span(span
.clone())
323 with_default(subscriber
, || {
324 block_on_future(async
{
325 let mut my_thing
= Thing
{
326 metrics
: Arc
::new(()),
328 my_thing
.call(()).await
;
332 handle
.assert_finished();