Skip to content

Commit

Permalink
sync: use tracing-mock to test mutex spans
Browse files Browse the repository at this point in the history
  • Loading branch information
xuorig committed Oct 26, 2023
1 parent 5a74dd0 commit f20a43f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 68 deletions.
10 changes: 8 additions & 2 deletions tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
94 changes: 28 additions & 66 deletions tokio/tests/sync_mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]
Expand Down Expand Up @@ -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<HashMap<u64, (&'static Metadata<'static>, bool)>>,
pub follows_from: std::sync::Mutex<HashMap<Id, Id>>,
current_id: std::sync::Mutex<u64>,
}

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[&current_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();
}

0 comments on commit f20a43f

Please sign in to comment.