Skip to content

Commit

Permalink
Replace lazy_static dependency with std::sync::OnceCell (#36)
Browse files Browse the repository at this point in the history
OnceCell was added to std in Rust 1.70, and replaces our use case of lazy_static.
  • Loading branch information
threema-danilo authored Feb 16, 2024
1 parent 49fa2c4 commit 9b8aa60
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
1 change: 0 additions & 1 deletion tracing-test-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ categories = ["development-tools::testing"]
proc-macro = true

[dependencies]
lazy_static = "1.4"
quote = "1"
syn = { version = "1", features = ["full"] }

Expand Down
26 changes: 13 additions & 13 deletions tracing-test-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@
//! [tracing-test]: https://docs.rs/tracing-test
extern crate proc_macro;

use std::sync::Mutex;
use std::sync::{Mutex, OnceLock};

use lazy_static::lazy_static;
use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use syn::{parse, ItemFn, Stmt};

lazy_static! {
/// Registered scopes.
///
/// By default, every traced test registers a span with the function name.
/// However, since multiple tests can share the same function name, in case
/// of conflict, a counter is appended.
///
/// This vector is used to store all already registered scopes.
static ref REGISTERED_SCOPES: Mutex<Vec<String>> = Mutex::new(vec![]);
/// Registered scopes.
///
/// By default, every traced test registers a span with the function name.
/// However, since multiple tests can share the same function name, in case
/// of conflict, a counter is appended.
///
/// This vector is used to store all already registered scopes.
fn registered_scopes() -> &'static Mutex<Vec<String>> {
static REGISTERED_SCOPES: OnceLock<Mutex<Vec<String>>> = OnceLock::new();
REGISTERED_SCOPES.get_or_init(|| Mutex::new(vec![]))
}

/// Check whether this test function name is already taken as scope. If yes, a
/// counter is appended to make it unique. In the end, a unique scope is returned.
fn get_free_scope(mut test_fn_name: String) -> String {
let mut vec = REGISTERED_SCOPES.lock().unwrap();
let mut vec = registered_scopes().lock().unwrap();
let mut counter = 1;
let len = test_fn_name.len();
while vec.contains(&test_fn_name) {
Expand Down Expand Up @@ -77,7 +77,7 @@ pub fn traced_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
.to_string();
format!("{}=trace", crate_name)
};
let mock_writer = tracing_test::internal::MockWriter::new(&tracing_test::internal::GLOBAL_BUF);
let mock_writer = tracing_test::internal::MockWriter::new(&tracing_test::internal::global_buf());
let subscriber = tracing_test::internal::get_subscriber(mock_writer, &env_filter);
tracing::dispatcher::set_global_default(subscriber)
.expect("Could not set global tracing subscriber");
Expand Down
1 change: 0 additions & 1 deletion tracing-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ readme = "../README.md"
categories = ["development-tools::testing"]

[dependencies]
lazy_static = "1.4"
tracing-core = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-test-macro = { path = "../tracing-test-macro", version = "0.2.4" }
Expand Down
17 changes: 8 additions & 9 deletions tracing-test/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@
//!
//! These functions should usually not be accessed from user code. The stability of these functions
//! is not guaranteed, the API may change even in patch releases.
use std::sync::{Mutex, Once};

use lazy_static::lazy_static;
use std::sync::{Mutex, Once, OnceLock};

pub use crate::subscriber::{get_subscriber, MockWriter};

/// Static variable to ensure that logging is only initialized once.
pub static INITIALIZED: Once = Once::new();

lazy_static! {
/// The global log output buffer used in tests.
#[doc(hidden)]
pub static ref GLOBAL_BUF: Mutex<Vec<u8>> = Mutex::new(vec![]);
/// The global log output buffer used in tests.
#[doc(hidden)]
pub fn global_buf() -> &'static Mutex<Vec<u8>> {
static GLOBAL_BUF: OnceLock<Mutex<Vec<u8>>> = OnceLock::new();
GLOBAL_BUF.get_or_init(|| Mutex::new(vec![]))
}

/// Return whether the logs with the specified scope contain the specified value.
///
/// This function should usually not be used directly, instead use the `logs_contain(val: &str)`
/// function injected by the [`#[traced_test]`](attr.traced_test.html) macro.
pub fn logs_with_scope_contain(scope: &str, val: &str) -> bool {
let logs = String::from_utf8(GLOBAL_BUF.lock().unwrap().to_vec()).unwrap();
let logs = String::from_utf8(global_buf().lock().unwrap().to_vec()).unwrap();
for line in logs.split('\n') {
if line.contains(&format!(" {}:", scope)) && line.contains(val) {
return true;
Expand All @@ -41,7 +40,7 @@ pub fn logs_assert<F>(scope: &str, f: F) -> std::result::Result<(), String>
where
F: Fn(&[&str]) -> std::result::Result<(), String>,
{
let buf = GLOBAL_BUF.lock().unwrap();
let buf = global_buf().lock().unwrap();
let logs: Vec<&str> = std::str::from_utf8(&buf)
.expect("Logs contain invalid UTF8")
.lines()
Expand Down

0 comments on commit 9b8aa60

Please sign in to comment.