1 #[path = "../../tracing-futures/tests/support.rs"]
2 // we don't use some of the test support functions, but `tracing-futures` does.
7 use tracing
::subscriber
::with_default
;
8 use tracing_attributes
::instrument
;
11 async
fn test_async_fn(polls
: usize) -> Result
<(), ()> {
12 let future
= PollN
::new_ok(polls
);
13 tracing
::trace
!(awaiting
= true);
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"))
29 with_default(subscriber
, || {
30 block_on_future(async { test_async_fn(2).await }
).unwrap();
32 handle
.assert_finished();
36 fn async_fn_nested() {
38 async
fn test_async_fns_nested() {
39 test_async_fns_nested_other().await
43 async
fn test_async_fns_nested_other() {
44 tracing
::trace
!(nested
= true);
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())
52 .new_span(span2
.clone())
54 .event(event
::mock().with_fields(field
::mock("nested").with_value(&true)))
62 with_default(subscriber
, || {
63 block_on_future(async { test_async_fns_nested().await }
);
66 handle
.assert_finished();
70 fn async_fn_with_async_trait() {
71 use async_trait
::async_trait
;
73 // test the correctness of the metadata obtained by #[instrument]
74 // (function name, functions parameters) when async-trait is used
77 async
fn foo(&mut self, v
: usize);
80 // test nesting of async fns with aync-trait
86 // test skip(self) with async-await
93 struct TestImpl(usize);
96 impl TestA
for TestImpl
{
98 async
fn foo(&mut self, v
: usize) {
106 impl TestB
for TestImpl
{
108 async
fn bar(&self) {
109 tracing
::trace
!(val
= self.0);
114 impl TestC
for TestImpl
{
115 #[instrument(skip(self))]
116 async
fn baz(&self) {
117 tracing
::trace
!(val
= self.0);
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()
127 .with_field(field
::mock("self"))
128 .with_field(field
::mock("v")),
131 .new_span(span3
.clone())
132 .enter(span3
.clone())
133 .event(event
::mock().with_fields(field
::mock("val").with_value(&2u64)))
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)))
146 with_default(subscriber
, || {
147 let mut test
= TestImpl(2);
148 block_on_future(async { test.foo(5).await }
);
151 handle
.assert_finished();
155 fn async_fn_with_async_trait_and_fields_expressions() {
156 use async_trait
::async_trait
;
160 async
fn call(&mut self, v
: usize);
163 #[derive(Clone, Debug)]
167 fn foo(&self) -> usize {
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) {}
179 let span
= span
::mock().named("call");
180 let (subscriber
, handle
) = subscriber
::mock()
182 span
.clone().with_field(
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)),
195 with_default(subscriber
, || {
196 block_on_future(async { TestImpl.call(5).await }
);
199 handle
.assert_finished();
203 fn async_fn_with_async_trait_and_fields_expressions_with_generic_parameter() {
204 use async_trait
::async_trait
;
209 async
fn call_with_self(&self);
210 async
fn call_with_mut_self(&mut self);
213 #[derive(Clone, Debug)]
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>()))]
222 #[instrument(fields(Self=std::any::type_name::<Self>()))]
223 async
fn call_with_self(&self) {}
225 #[instrument(fields(Self=std::any::type_name::<Self>()))]
226 async
fn call_with_mut_self(self: &mut Self) {}
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()
235 field::mock("Self").with_value(&"TestImpler")))
242 .with_field(field
::mock("Self").with_value(&std
::any
::type_name
::<TestImpl
>())),
244 .enter(span2
.clone())
250 .with_field(field
::mock("Self").with_value(&std
::any
::type_name
::<TestImpl
>())),
252 .enter(span3
.clone())
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
266 handle
.assert_finished();