From f20a43f495fa4bfa545fe462b722fbe847abced4 Mon Sep 17 00:00:00 2001 From: mgiroux Date: Thu, 26 Oct 2023 09:21:22 -0400 Subject: [PATCH] sync: use tracing-mock to test mutex spans --- tokio/Cargo.toml | 10 ++++- tokio/tests/sync_mutex.rs | 94 ++++++++++++--------------------------- 2 files changed, 36 insertions(+), 68 deletions(-) diff --git a/tokio/Cargo.toml b/tokio/Cargo.toml index ad876ce7810..92680b81ff7 100644 --- a/tokio/Cargo.toml +++ b/tokio/Cargo.toml @@ -105,10 +105,16 @@ socket2 = { version = "0.5.3", optional = true, features = [ "all" ] } # Currently unstable. The API exposed by these features may be broken at any time. # Requires `--cfg tokio_unstable` to enable. [target.'cfg(tokio_unstable)'.dependencies] -tracing = { version = "0.1.25", default-features = false, features = ["std"], optional = true } # Not in full +tracing = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x", features = ["std"], optional = true } [target.'cfg(tokio_unstable)'.dev-dependencies] -tracing-core = { version = "0.1.25", default-features = false } +tracing = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x" } +tracing-core = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x" } +tracing-mock = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x" } + +[target.'cfg(tokio_unstable)'.patch.crates-io] +tracing = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x" } +tracing-core = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x" } # Currently unstable. The API exposed by these features may be broken at any time. # Requires `--cfg tokio_unstable` to enable. diff --git a/tokio/tests/sync_mutex.rs b/tokio/tests/sync_mutex.rs index c45632993a8..e03f47ad272 100644 --- a/tokio/tests/sync_mutex.rs +++ b/tokio/tests/sync_mutex.rs @@ -7,14 +7,9 @@ use wasm_bindgen_test::wasm_bindgen_test as test; use wasm_bindgen_test::wasm_bindgen_test as maybe_tokio_test; #[cfg(all(tokio_unstable, feature = "tracing"))] -use tracing::{ - span::{Attributes, Record}, - subscriber::Subscriber, - Event, Id, Metadata, -}; - +use tracing::subscriber::with_default; #[cfg(all(tokio_unstable, feature = "tracing"))] -use tracing_core::span::Current; +use tracing_mock::{expect, subscriber}; #[cfg(not(all(target_family = "wasm", not(target_os = "wasi"))))] use tokio::test as maybe_tokio_test; @@ -23,8 +18,6 @@ use tokio::sync::Mutex; use tokio_test::task::spawn; use tokio_test::{assert_pending, assert_ready}; -#[cfg(all(tokio_unstable, feature = "tracing"))] -use std::collections::HashMap; use std::sync::Arc; #[test] @@ -192,66 +185,35 @@ async fn mutex_debug() { #[tokio::test] #[cfg(all(tokio_unstable, feature = "tracing"))] async fn mutex_new_resource_span_follows_from_current_span() { - #[derive(Default, Debug)] - struct MockSubscriber { - pub spans: std::sync::Mutex, bool)>>, - pub follows_from: std::sync::Mutex>, - current_id: std::sync::Mutex, - } - - impl Subscriber for MockSubscriber { - fn enabled(&self, _metadata: &Metadata<'_>) -> bool { - true - } - - fn new_span(&self, span: &Attributes<'_>) -> Id { - let mut spans = self.spans.lock().unwrap(); - let mut current_id = self.current_id.lock().unwrap(); - *current_id += 1; - spans.insert(*current_id, (span.metadata(), span.is_root())); - Id::from_u64(*current_id) - } - - fn enter(&self, _span: &Id) {} - - fn exit(&self, _span: &Id) {} - - fn event(&self, _event: &Event<'_>) {} - - fn record_follows_from(&self, span: &Id, follows: &Id) { - let mut follows_from = self.follows_from.lock().unwrap(); - follows_from.insert(span.clone(), follows.clone()); - } - - fn record(&self, _span: &Id, _values: &Record<'_>) {} - - fn current_span(&self) -> Current { - let spans = self.spans.lock().unwrap(); - let current_id = self.current_id.lock().unwrap(); - let metadata = spans[¤t_id].0; - Current::new(Id::from_u64(*current_id), &metadata) - } - } - - let mock_subscriber = Arc::new(MockSubscriber::default()); - - tracing::subscriber::with_default(mock_subscriber.clone(), || { + let expected_parent = expect::span().named("parent"); + let expected_mutex = expect::span() + .named("runtime.resource") + .with_target("tokio::sync::mutex"); + let expected_semaphore = expect::span() + .named("runtime.resource") + .with_target("tokio::sync::batch_semaphore"); + + let (subscriber, handle) = subscriber::mock() + .new_span(expected_parent.clone()) + .enter(expected_parent.clone()) + .new_span(expected_mutex.clone()) + .follows_from(expected_mutex.clone(), expected_parent.clone()) + .enter(expected_mutex.clone()) + .event(expect::event().with_target("runtime::resource::state_update")) + .follows_from(expected_semaphore.clone(), expected_mutex.clone()) + .enter(expected_semaphore.clone()) + .event(expect::event().with_target("runtime::resource::state_update")) + .exit(expected_semaphore.clone()) + .exit(expected_mutex.clone()) + .exit(expected_parent.clone()) + .only() + .run_with_handle(); + + with_default(subscriber, || { let parent = tracing::info_span!("parent"); let _guard = parent.enter(); let _m = Mutex::new(0); }); - let spans = mock_subscriber.spans.lock().unwrap(); - let follows = mock_subscriber.follows_from.lock().unwrap(); - - // Parent span created first, not a root - assert_eq!("parent", spans[&1].0.name()); - assert_eq!(false, spans[&1].1); - - // Mutex span created second, has no parent - assert_eq!("runtime.resource", spans[&2].0.name()); - assert_eq!(true, spans[&2].1); - - // ID 2 (mutex runtime.resource) follow from ID 1 (parent) - assert_eq!(Id::from_u64(1), follows[&Id::from_u64(2)]); + handle.assert_finished(); }