Skip to content

Commit

Permalink
consensus: Only initialise tracing once for the block tests
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Jun 24, 2020
1 parent e2e47d1 commit 7bdf6f6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
29 changes: 29 additions & 0 deletions zebra-consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})
}
}
24 changes: 7 additions & 17 deletions zebra-consensus/src/verify/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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::<Block>::zcash_deserialize(&zebra_test::vectors::BLOCK_MAINNET_415000_BYTES[..])?;
let hash: BlockHeaderHash = block.as_ref().into();
Expand All @@ -331,6 +317,8 @@ mod tests {
#[tokio::test]
#[spandoc::spandoc]
async fn round_trip() -> Result<(), Report> {
install_tracing();

let block =
Arc::<Block>::zcash_deserialize(&zebra_test::vectors::BLOCK_MAINNET_415000_BYTES[..])?;
let hash: BlockHeaderHash = block.as_ref().into();
Expand Down Expand Up @@ -440,6 +428,8 @@ mod tests {
#[tokio::test]
#[spandoc::spandoc]
async fn verify_fail_future_time() -> Result<(), Report> {
install_tracing();

let mut block =
<Block>::zcash_deserialize(&zebra_test::vectors::BLOCK_MAINNET_415000_BYTES[..])?;

Expand Down

0 comments on commit 7bdf6f6

Please sign in to comment.