consolidate test init functions into zebra-test (#541)

* consolidate test init logic into one place

* rustfmt

Co-authored-by: Jane Lusby <jane@zfnd.org>
This commit is contained in:
Jane Lusby 2020-06-24 11:30:20 -07:00 committed by Henry de Valence
parent 87a8d328d5
commit 2abf4b93a8
10 changed files with 41 additions and 85 deletions

10
Cargo.lock generated
View File

@ -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]]

View File

@ -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/" }

View File

@ -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<V>(mut verifier: V, n: usize) -> Result<(), V::Error>
where
V: Service<Ed25519Item, Response = ()>,
@ -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.

View File

@ -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"

View File

@ -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::<Block>::zcash_deserialize(&zebra_test::vectors::BLOCK_MAINNET_415000_BYTES[..])?;

View File

@ -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"

View File

@ -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<Vec<(Request, Response)>> = Lazy::new(|| {
let block: Arc<_> =
Block::zcash_deserialize(&zebra_test::vectors::BLOCK_MAINNET_415000_BYTES[..])
@ -75,7 +50,7 @@ static GET_TIP_TRANSCRIPT: Lazy<Vec<(Request, Response)>> = 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();

View File

@ -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"] }
tokio = { version = "0.2", features = ["full"] }

View File

@ -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;

View File

@ -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"),