From 7d7ff2252631aeabe22a1f2794c41c9d337100d6 Mon Sep 17 00:00:00 2001 From: "Philip (a-0)" <@ph:a-0.me> Date: Sun, 7 Jan 2024 22:09:00 +0100 Subject: [PATCH] Fixed `tracing` setup in tests --- ubisync-lib/src/lib.rs | 13 +++++++++++++ ubisync/src/state/api_state.rs | 9 ++++++--- ubisync/src/state/comm_state.rs | 11 +++++++---- ubisync/tests/api.rs | 8 +++----- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/ubisync-lib/src/lib.rs b/ubisync-lib/src/lib.rs index b06f1fb..0875afa 100644 --- a/ubisync-lib/src/lib.rs +++ b/ubisync-lib/src/lib.rs @@ -2,3 +2,16 @@ pub mod api; pub mod messages; pub mod peer; pub mod types; + + +#[macro_export] +macro_rules! tracing_setup { + ($level:expr) => { + let _tracing_default_guard = tracing::subscriber::set_default( + tracing_subscriber::fmt() + .pretty() + .with_max_level($level) + .finish(), + ); + }; +} diff --git a/ubisync/src/state/api_state.rs b/ubisync/src/state/api_state.rs index 4b33748..8e1486e 100644 --- a/ubisync/src/state/api_state.rs +++ b/ubisync/src/state/api_state.rs @@ -102,7 +102,8 @@ impl ApiState { #[cfg(test)] mod tests { - use ubisync_lib::types::ElementContent; + use tracing::Level; + use ubisync_lib::{types::ElementContent, tracing_setup}; use crate::state::State; @@ -111,7 +112,8 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn test_element_create() { - tracing_subscriber::fmt().pretty().init(); + tracing_setup!(Level::DEBUG); + let state = ApiState::new( State::new().await.unwrap(), "abcdabcdabcdabcdabcdabcdabcdabcd", @@ -129,7 +131,8 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn test_element_write() { - tracing_subscriber::fmt().pretty().init(); + tracing_setup!(Level::DEBUG); + let state = ApiState::new( State::new().await.unwrap(), "abcdabcdabcdabcdabcdabcdabcdabcd", diff --git a/ubisync/src/state/comm_state.rs b/ubisync/src/state/comm_state.rs index f91c436..3a2e9c2 100644 --- a/ubisync/src/state/comm_state.rs +++ b/ubisync/src/state/comm_state.rs @@ -64,7 +64,7 @@ impl CommState { pub fn set_peer(&self, id: &PeerId, name: &str) -> anyhow::Result<()> { queries::peers::put(self.db(), id, name)?; - debug!("Set peer {{{}}}", id.to_string()); + debug!("Set peer {} with address {}.", &name, id.to_string()); Ok(()) } @@ -84,12 +84,14 @@ mod tests { use super::CommState; - use ubisync_lib::types::{ElementContent, ElementId, MessageId}; + use tracing::Level; + use ubisync_lib::{types::{ElementContent, ElementId, MessageId}, tracing_setup}; #[tokio::test] #[serial_test::serial] async fn test_element_add() { - tracing_subscriber::fmt().pretty().init(); + tracing_setup!(Level::DEBUG); + let state = CommState::new(State::new().await.unwrap()); let id = ElementId::new(); state @@ -109,7 +111,8 @@ mod tests { #[tokio::test] #[serial_test::serial] async fn test_element_update() { - tracing_subscriber::fmt().pretty().init(); + tracing_setup!(Level::DEBUG); + let state = CommState::new(State::new().await.unwrap()); let id = ElementId::new(); state diff --git a/ubisync/tests/api.rs b/ubisync/tests/api.rs index 367d389..17accc0 100644 --- a/ubisync/tests/api.rs +++ b/ubisync/tests/api.rs @@ -4,16 +4,14 @@ use tracing::{debug, warn, Level}; use ubisync::{config::Config, Ubisync}; use ubisync_lib::{ api::element::{ElementCreateRequest, ElementGetRequest}, - types::{Element, ElementContent}, + types::{Element, ElementContent}, tracing_setup, }; use ubisync_sdk::UbisyncClient; + #[tokio::test(flavor = "multi_thread")] async fn two_nodes_element_creation() { - tracing_subscriber::fmt() - .pretty() - .with_max_level(Level::DEBUG) - .init(); + tracing_setup!(Level::DEBUG); // Two nodes need to bind to different ports let mut c2 = Config::default();