From 2abf4b93a8ce710f27dd431dfd400ace83ec97a2 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Wed, 24 Jun 2020 11:30:20 -0700 Subject: [PATCH] consolidate test init functions into zebra-test (#541) * consolidate test init logic into one place * rustfmt Co-authored-by: Jane Lusby --- Cargo.lock | 10 ++++------ tower-batch/Cargo.toml | 3 +-- tower-batch/tests/ed25519.rs | 26 ++------------------------ zebra-consensus/Cargo.toml | 2 -- zebra-consensus/src/verify.rs | 19 +------------------ zebra-state/Cargo.toml | 4 +--- zebra-state/tests/basic.rs | 31 +++---------------------------- zebra-test/Cargo.toml | 5 ++++- zebra-test/src/lib.rs | 24 ++++++++++++++++++++++++ zebra-test/tests/transcript.rs | 2 +- 10 files changed, 41 insertions(+), 85 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 421dc534..a6a250a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1955,9 +1955,8 @@ dependencies = [ "tokio", "tower", "tracing", - "tracing-error", "tracing-futures", - "tracing-subscriber", + "zebra-test", ] [[package]] @@ -2334,8 +2333,6 @@ dependencies = [ "tokio", "tower", "tracing", - "tracing-error", - "tracing-subscriber", "zebra-chain", "zebra-state", "zebra-test", @@ -2393,9 +2390,7 @@ dependencies = [ "tokio", "tower", "tracing", - "tracing-error", "tracing-futures", - "tracing-subscriber", "zebra-chain", "zebra-test", ] @@ -2410,6 +2405,9 @@ dependencies = [ "lazy_static", "tokio", "tower", + "tracing", + "tracing-error", + "tracing-subscriber", ] [[package]] diff --git a/tower-batch/Cargo.toml b/tower-batch/Cargo.toml index b274b3a9..879efdca 100644 --- a/tower-batch/Cargo.toml +++ b/tower-batch/Cargo.toml @@ -18,7 +18,6 @@ futures = "0.3.5" ed25519-zebra = "0.4" rand = "0.7" tokio = { version = "0.2", features = ["full"]} -tracing-error = "0.1.2" -tracing-subscriber = "0.2.6" tracing = "0.1.15" color-eyre = "0.5" +zebra-test = { path = "../zebra-test/" } diff --git a/tower-batch/tests/ed25519.rs b/tower-batch/tests/ed25519.rs index 23d3136a..f1b44ff9 100644 --- a/tower-batch/tests/ed25519.rs +++ b/tower-batch/tests/ed25519.rs @@ -2,7 +2,6 @@ use std::{ future::Future, mem, pin::Pin, - sync::Once, task::{Context, Poll}, time::Duration, }; @@ -86,27 +85,6 @@ impl Drop for Ed25519Verifier { // =============== testing code ======== -static LOGGER_INIT: Once = Once::new(); - -fn install_tracing() { - use tracing_error::ErrorLayer; - use tracing_subscriber::prelude::*; - use tracing_subscriber::{fmt, EnvFilter}; - - 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(); - }) -} - async fn sign_and_verify(mut verifier: V, n: usize) -> Result<(), V::Error> where V: Service, @@ -133,7 +111,7 @@ where #[tokio::test] async fn batch_flushes_on_max_items() -> Result<()> { use tokio::time::timeout; - install_tracing(); + zebra_test::init(); // Use a very long max_latency and a short timeout to check that // flushing is happening based on hitting max_items. @@ -144,7 +122,7 @@ async fn batch_flushes_on_max_items() -> Result<()> { #[tokio::test] async fn batch_flushes_on_max_latency() -> Result<()> { use tokio::time::timeout; - install_tracing(); + zebra_test::init(); // Use a very high max_items and a short timeout to check that // flushing is happening based on hitting max_latency. diff --git a/zebra-consensus/Cargo.toml b/zebra-consensus/Cargo.toml index 061fdc75..ac2fbc75 100644 --- a/zebra-consensus/Cargo.toml +++ b/zebra-consensus/Cargo.toml @@ -19,6 +19,4 @@ zebra-test = { path = "../zebra-test/" } spandoc = "0.1" tokio = { version = "0.2.21", features = ["full"] } tracing = "0.1.15" -tracing-error = "0.1.2" -tracing-subscriber = "0.2.6" color-eyre = "0.5" diff --git a/zebra-consensus/src/verify.rs b/zebra-consensus/src/verify.rs index 17673a5f..e8f983f5 100644 --- a/zebra-consensus/src/verify.rs +++ b/zebra-consensus/src/verify.rs @@ -129,23 +129,6 @@ mod tests { use tower::{util::ServiceExt, Service}; use zebra_chain::serialization::ZcashDeserialize; - 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> { @@ -218,7 +201,7 @@ mod tests { #[tokio::test] #[spandoc::spandoc] async fn verify_fail_add_block() -> Result<(), Report> { - install_tracing(); + zebra_test::init(); let block = Arc::::zcash_deserialize(&zebra_test::vectors::BLOCK_MAINNET_415000_BYTES[..])?; diff --git a/zebra-state/Cargo.toml b/zebra-state/Cargo.toml index 0e55baaa..ca47790f 100644 --- a/zebra-state/Cargo.toml +++ b/zebra-state/Cargo.toml @@ -18,12 +18,10 @@ serde = { version = "1", features = ["serde_derive"] } [dev-dependencies] tokio = { version = "0.2.21", features = ["full"] } -zebra-test = { path = "../zebra-test" } +zebra-test = { path = "../zebra-test/" } spandoc = "0.1" tracing = "0.1.15" tracing-futures = "0.2.4" -tracing-error = "0.1.2" -tracing-subscriber = "0.2.6" tempdir = "0.3.7" color-eyre = "0.5" once_cell = "1.4" diff --git a/zebra-state/tests/basic.rs b/zebra-state/tests/basic.rs index dfe0ecac..d3deedda 100644 --- a/zebra-state/tests/basic.rs +++ b/zebra-state/tests/basic.rs @@ -1,37 +1,12 @@ use color_eyre::eyre::Report; -use color_eyre::eyre::{bail, ensure, eyre}; use once_cell::sync::Lazy; -use std::sync::{Arc, Once}; +use std::sync::Arc; use tempdir::TempDir; -use tower::Service; -use tracing_error::ErrorLayer; -use tracing_subscriber::prelude::*; -use tracing_subscriber::{fmt, EnvFilter}; -use zebra_chain::{ - block::{Block, BlockHeaderHash}, - serialization::ZcashDeserialize, -}; +use zebra_chain::{block::Block, serialization::ZcashDeserialize}; use zebra_test::transcript::Transcript; use zebra_state::*; -static LOGGER_INIT: Once = Once::new(); - -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(); - }) -} - static ADD_BLOCK_TRANSCRIPT: Lazy> = Lazy::new(|| { let block: Arc<_> = Block::zcash_deserialize(&zebra_test::vectors::BLOCK_MAINNET_415000_BYTES[..]) @@ -75,7 +50,7 @@ static GET_TIP_TRANSCRIPT: Lazy> = Lazy::new(|| { #[tokio::test] async fn check_transcripts() -> Result<(), Report> { - install_tracing(); + zebra_test::init(); for transcript_data in &[&ADD_BLOCK_TRANSCRIPT, &GET_TIP_TRANSCRIPT] { let service = in_memory::init(); diff --git a/zebra-test/Cargo.toml b/zebra-test/Cargo.toml index dd9d6dc9..a8f0b7a7 100644 --- a/zebra-test/Cargo.toml +++ b/zebra-test/Cargo.toml @@ -13,6 +13,9 @@ lazy_static = "1.4.0" tower = "0.3.1" futures = "0.3.5" color-eyre = "0.5" +tracing = "0.1.15" +tracing-subscriber = "0.2.6" +tracing-error = "0.1.2" [dev-dependencies] -tokio = { version = "0.2", features = ["full"] } \ No newline at end of file +tokio = { version = "0.2", features = ["full"] } diff --git a/zebra-test/src/lib.rs b/zebra-test/src/lib.rs index 1a157d56..b35ce342 100644 --- a/zebra-test/src/lib.rs +++ b/zebra-test/src/lib.rs @@ -1,4 +1,28 @@ //! Miscellaneous test code for Zebra. +use std::sync::Once; +use tracing_error::ErrorLayer; +use tracing_subscriber::{fmt, prelude::*, EnvFilter}; + +static INIT: Once = Once::new(); + +/// Initialize globals for tests such as the tracing subscriber and panic / error +/// reporting hooks +pub fn init() { + 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(); + + color_eyre::install().unwrap(); + }) +} pub mod transcript; pub mod vectors; diff --git a/zebra-test/tests/transcript.rs b/zebra-test/tests/transcript.rs index 687ecd5c..9ebf1af4 100644 --- a/zebra-test/tests/transcript.rs +++ b/zebra-test/tests/transcript.rs @@ -2,7 +2,7 @@ use tower::{Service, ServiceExt}; use zebra_test::transcript::Transcript; -const TRANSCRIPT_DATA: [(&'static str, &'static str); 4] = [ +const TRANSCRIPT_DATA: [(&str, &str); 4] = [ ("req1", "rsp1"), ("req2", "rsp2"), ("req3", "rsp3"),