| 1 |
use tracing::{subscriber::with_default, Id, Level}; |
| 2 |
use tracing_attributes::instrument; |
| 3 |
use tracing_mock::*; |
| 4 |
|
| 5 |
#[instrument] |
| 6 |
fn with_default_parent() {} |
| 7 |
|
| 8 |
#[instrument(parent = parent_span, skip(parent_span))] |
| 9 |
fn with_explicit_parent<P>(parent_span: P) |
| 10 |
where |
| 11 |
P: Into<Option<Id>>, |
| 12 |
{ |
| 13 |
} |
| 14 |
|
| 15 |
#[test] |
| 16 |
fn default_parent_test() { |
| 17 |
let contextual_parent = span::mock().named("contextual_parent"); |
| 18 |
let child = span::mock().named("with_default_parent"); |
| 19 |
|
| 20 |
let (subscriber, handle) = subscriber::mock() |
| 21 |
.new_span( |
| 22 |
contextual_parent |
| 23 |
.clone() |
| 24 |
.with_contextual_parent(None) |
| 25 |
.with_explicit_parent(None), |
| 26 |
) |
| 27 |
.new_span( |
| 28 |
child |
| 29 |
.clone() |
| 30 |
.with_contextual_parent(Some("contextual_parent")) |
| 31 |
.with_explicit_parent(None), |
| 32 |
) |
| 33 |
.enter(child.clone()) |
| 34 |
.exit(child.clone()) |
| 35 |
.enter(contextual_parent.clone()) |
| 36 |
.new_span( |
| 37 |
child |
| 38 |
.clone() |
| 39 |
.with_contextual_parent(Some("contextual_parent")) |
| 40 |
.with_explicit_parent(None), |
| 41 |
) |
| 42 |
.enter(child.clone()) |
| 43 |
.exit(child) |
| 44 |
.exit(contextual_parent) |
| 45 |
.done() |
| 46 |
.run_with_handle(); |
| 47 |
|
| 48 |
with_default(subscriber, || { |
| 49 |
let contextual_parent = tracing::span!(Level::TRACE, "contextual_parent"); |
| 50 |
|
| 51 |
with_default_parent(); |
| 52 |
|
| 53 |
contextual_parent.in_scope(|| { |
| 54 |
with_default_parent(); |
| 55 |
}); |
| 56 |
}); |
| 57 |
|
| 58 |
handle.assert_finished(); |
| 59 |
} |
| 60 |
|
| 61 |
#[test] |
| 62 |
fn explicit_parent_test() { |
| 63 |
let contextual_parent = span::mock().named("contextual_parent"); |
| 64 |
let explicit_parent = span::mock().named("explicit_parent"); |
| 65 |
let child = span::mock().named("with_explicit_parent"); |
| 66 |
|
| 67 |
let (subscriber, handle) = subscriber::mock() |
| 68 |
.new_span( |
| 69 |
contextual_parent |
| 70 |
.clone() |
| 71 |
.with_contextual_parent(None) |
| 72 |
.with_explicit_parent(None), |
| 73 |
) |
| 74 |
.new_span( |
| 75 |
explicit_parent |
| 76 |
.with_contextual_parent(None) |
| 77 |
.with_explicit_parent(None), |
| 78 |
) |
| 79 |
.enter(contextual_parent.clone()) |
| 80 |
.new_span( |
| 81 |
child |
| 82 |
.clone() |
| 83 |
.with_contextual_parent(Some("contextual_parent")) |
| 84 |
.with_explicit_parent(Some("explicit_parent")), |
| 85 |
) |
| 86 |
.enter(child.clone()) |
| 87 |
.exit(child) |
| 88 |
.exit(contextual_parent) |
| 89 |
.done() |
| 90 |
.run_with_handle(); |
| 91 |
|
| 92 |
with_default(subscriber, || { |
| 93 |
let contextual_parent = tracing::span!(Level::INFO, "contextual_parent"); |
| 94 |
let explicit_parent = tracing::span!(Level::INFO, "explicit_parent"); |
| 95 |
|
| 96 |
contextual_parent.in_scope(|| { |
| 97 |
with_explicit_parent(&explicit_parent); |
| 98 |
}); |
| 99 |
}); |
| 100 |
|
| 101 |
handle.assert_finished(); |
| 102 |
} |