|
| 1 | +//! Tests for task instrumentation. |
| 2 | +//! |
| 3 | +//! These tests ensure that the instrumentation for task spawning and task |
| 4 | +//! lifecycles is correct. |
| 5 | +
|
| 6 | +use tokio::task; |
| 7 | +use tracing_mock::{expect, span::NewSpan, subscriber}; |
| 8 | + |
| 9 | +#[tokio::test] |
| 10 | +async fn task_spawn_creates_span() { |
| 11 | + let task_span = expect::span() |
| 12 | + .named("runtime.spawn") |
| 13 | + .with_target("tokio::task"); |
| 14 | + |
| 15 | + let (subscriber, handle) = subscriber::mock() |
| 16 | + .new_span(task_span.clone()) |
| 17 | + .enter(task_span.clone()) |
| 18 | + .exit(task_span.clone()) |
| 19 | + // The task span is entered once more when it gets dropped |
| 20 | + .enter(task_span.clone()) |
| 21 | + .exit(task_span.clone()) |
| 22 | + .drop_span(task_span) |
| 23 | + .run_with_handle(); |
| 24 | + |
| 25 | + { |
| 26 | + let _guard = tracing::subscriber::set_default(subscriber); |
| 27 | + tokio::spawn(futures::future::ready(())) |
| 28 | + .await |
| 29 | + .expect("failed to await join handle"); |
| 30 | + } |
| 31 | + |
| 32 | + handle.assert_finished(); |
| 33 | +} |
| 34 | + |
| 35 | +#[tokio::test] |
| 36 | +async fn task_spawn_loc_file_recorded() { |
| 37 | + let task_span = expect::span() |
| 38 | + .named("runtime.spawn") |
| 39 | + .with_target("tokio::task") |
| 40 | + .with_field(expect::field("loc.file").with_value(&file!())); |
| 41 | + |
| 42 | + let (subscriber, handle) = subscriber::mock().new_span(task_span).run_with_handle(); |
| 43 | + |
| 44 | + { |
| 45 | + let _guard = tracing::subscriber::set_default(subscriber); |
| 46 | + |
| 47 | + tokio::spawn(futures::future::ready(())) |
| 48 | + .await |
| 49 | + .expect("failed to await join handle"); |
| 50 | + } |
| 51 | + |
| 52 | + handle.assert_finished(); |
| 53 | +} |
| 54 | + |
| 55 | +#[tokio::test] |
| 56 | +async fn task_builder_name_recorded() { |
| 57 | + let task_span = expect_task_named("test-task"); |
| 58 | + |
| 59 | + let (subscriber, handle) = subscriber::mock().new_span(task_span).run_with_handle(); |
| 60 | + |
| 61 | + { |
| 62 | + let _guard = tracing::subscriber::set_default(subscriber); |
| 63 | + task::Builder::new() |
| 64 | + .name("test-task") |
| 65 | + .spawn(futures::future::ready(())) |
| 66 | + .unwrap() |
| 67 | + .await |
| 68 | + .expect("failed to await join handle"); |
| 69 | + } |
| 70 | + |
| 71 | + handle.assert_finished(); |
| 72 | +} |
| 73 | + |
| 74 | +#[tokio::test] |
| 75 | +async fn task_builder_loc_file_recorded() { |
| 76 | + let task_span = expect::span() |
| 77 | + .named("runtime.spawn") |
| 78 | + .with_target("tokio::task") |
| 79 | + .with_field(expect::field("loc.file").with_value(&file!())); |
| 80 | + |
| 81 | + let (subscriber, handle) = subscriber::mock().new_span(task_span).run_with_handle(); |
| 82 | + |
| 83 | + { |
| 84 | + let _guard = tracing::subscriber::set_default(subscriber); |
| 85 | + |
| 86 | + task::Builder::new() |
| 87 | + .spawn(futures::future::ready(())) |
| 88 | + .unwrap() |
| 89 | + .await |
| 90 | + .expect("failed to await join handle"); |
| 91 | + } |
| 92 | + |
| 93 | + handle.assert_finished(); |
| 94 | +} |
| 95 | + |
| 96 | +/// Expect a task with name |
| 97 | +/// |
| 98 | +/// This is a convenience function to create the expectation for a new task |
| 99 | +/// with the `task.name` field set to the provided name. |
| 100 | +fn expect_task_named(name: &str) -> NewSpan { |
| 101 | + expect::span() |
| 102 | + .named("runtime.spawn") |
| 103 | + .with_target("tokio::task") |
| 104 | + .with_field( |
| 105 | + expect::field("task.name").with_value(&tracing::field::debug(format_args!("{}", name))), |
| 106 | + ) |
| 107 | +} |
0 commit comments