|
| 1 | +//! Tests for time resource instrumentation. |
| 2 | +//! |
| 3 | +//! These tests ensure that the instrumentation for tokio |
| 4 | +//! synchronization primitives is correct. |
| 5 | +use std::time::Duration; |
| 6 | + |
| 7 | +use tracing_mock::{expect, subscriber}; |
| 8 | + |
| 9 | +#[tokio::test] |
| 10 | +async fn test_sleep_creates_span() { |
| 11 | + let sleep_span = expect::span() |
| 12 | + .named("runtime.resource") |
| 13 | + .with_target("tokio::time::sleep"); |
| 14 | + |
| 15 | + let state_update = expect::event() |
| 16 | + .with_target("runtime::resource::state_update") |
| 17 | + .with_fields( |
| 18 | + expect::field("duration") |
| 19 | + .with_value(&(7_u64 + 1)) |
| 20 | + .and(expect::field("duration.op").with_value(&"override")), |
| 21 | + ); |
| 22 | + |
| 23 | + let async_op_span = expect::span() |
| 24 | + .named("runtime.resource.async_op") |
| 25 | + .with_target("tokio::time::sleep"); |
| 26 | + |
| 27 | + let async_op_poll_span = expect::span() |
| 28 | + .named("runtime.resource.async_op.poll") |
| 29 | + .with_target("tokio::time::sleep"); |
| 30 | + |
| 31 | + let (subscriber, handle) = subscriber::mock() |
| 32 | + .new_span(sleep_span.clone().with_explicit_parent(None)) |
| 33 | + .enter(sleep_span.clone()) |
| 34 | + .event(state_update) |
| 35 | + .new_span( |
| 36 | + async_op_span |
| 37 | + .clone() |
| 38 | + .with_contextual_parent(Some("runtime.resource")) |
| 39 | + .with_field(expect::field("source").with_value(&"Sleep::new_timeout")), |
| 40 | + ) |
| 41 | + .exit(sleep_span.clone()) |
| 42 | + .enter(async_op_span.clone()) |
| 43 | + .new_span( |
| 44 | + async_op_poll_span |
| 45 | + .clone() |
| 46 | + .with_contextual_parent(Some("runtime.resource.async_op")), |
| 47 | + ) |
| 48 | + .exit(async_op_span.clone()) |
| 49 | + .drop_span(async_op_span) |
| 50 | + .drop_span(async_op_poll_span) |
| 51 | + .drop_span(sleep_span) |
| 52 | + .run_with_handle(); |
| 53 | + |
| 54 | + { |
| 55 | + let _guard = tracing::subscriber::set_default(subscriber); |
| 56 | + |
| 57 | + _ = tokio::time::sleep(Duration::from_millis(7)); |
| 58 | + } |
| 59 | + |
| 60 | + handle.assert_finished(); |
| 61 | +} |
0 commit comments