From 7bdf6f6fe8de1ea092c4cfea4ae44c7da2001aa4 Mon Sep 17 00:00:00 2001 From: teor Date: Wed, 24 Jun 2020 18:18:43 +1000 Subject: [PATCH] consensus: Only initialise tracing once for the block tests Part of #428. --- zebra-consensus/src/lib.rs | 29 +++++++++++++++++++++++++++++ zebra-consensus/src/verify/block.rs | 24 +++++++----------------- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/zebra-consensus/src/lib.rs b/zebra-consensus/src/lib.rs index b0d513729b4..bf894937699 100644 --- a/zebra-consensus/src/lib.rs +++ b/zebra-consensus/src/lib.rs @@ -16,3 +16,32 @@ pub mod checkpoint; pub mod mempool; pub mod verify; + +/// Test utility functions +/// +/// Submodules have their own specific tests. +#[cfg(test)] +mod tests { + use std::sync::Once; + use tracing_error::ErrorLayer; + use tracing_subscriber::prelude::*; + use tracing_subscriber::{fmt, EnvFilter}; + + static LOGGER_INIT: Once = Once::new(); + + // TODO(jlusby): Refactor into the zebra-test crate (#515) + pub(crate) fn install_tracing() { + LOGGER_INIT.call_once(|| { + let fmt_layer = fmt::layer().with_target(false); + let filter_layer = EnvFilter::try_from_default_env() + .or_else(|_| EnvFilter::try_new("info")) + .unwrap(); + + tracing_subscriber::registry() + .with(filter_layer) + .with(fmt_layer) + .with(ErrorLayer::default()) + .init(); + }) + } +} diff --git a/zebra-consensus/src/verify/block.rs b/zebra-consensus/src/verify/block.rs index 17d609476a9..322bdfe3564 100644 --- a/zebra-consensus/src/verify/block.rs +++ b/zebra-consensus/src/verify/block.rs @@ -147,6 +147,7 @@ where #[cfg(test)] mod tests { use super::*; + use crate::tests::install_tracing; use chrono::offset::{LocalResult, TimeZone}; use chrono::{Duration, Utc}; @@ -288,26 +289,11 @@ mod tests { } } - fn install_tracing() { - use tracing_error::ErrorLayer; - use tracing_subscriber::prelude::*; - use tracing_subscriber::{fmt, EnvFilter}; - - let fmt_layer = fmt::layer().with_target(false); - let filter_layer = EnvFilter::try_from_default_env() - .or_else(|_| EnvFilter::try_new("info")) - .unwrap(); - - tracing_subscriber::registry() - .with(filter_layer) - .with(fmt_layer) - .with(ErrorLayer::default()) - .init(); - } - #[tokio::test] #[spandoc::spandoc] async fn verify() -> Result<(), Report> { + install_tracing(); + let block = Arc::::zcash_deserialize(&zebra_test::vectors::BLOCK_MAINNET_415000_BYTES[..])?; let hash: BlockHeaderHash = block.as_ref().into(); @@ -331,6 +317,8 @@ mod tests { #[tokio::test] #[spandoc::spandoc] async fn round_trip() -> Result<(), Report> { + install_tracing(); + let block = Arc::::zcash_deserialize(&zebra_test::vectors::BLOCK_MAINNET_415000_BYTES[..])?; let hash: BlockHeaderHash = block.as_ref().into(); @@ -440,6 +428,8 @@ mod tests { #[tokio::test] #[spandoc::spandoc] async fn verify_fail_future_time() -> Result<(), Report> { + install_tracing(); + let mut block = ::zcash_deserialize(&zebra_test::vectors::BLOCK_MAINNET_415000_BYTES[..])?;