fix stdout issue with test framework for cached data tests

This commit is contained in:
Jane Lusby 2020-10-30 12:36:20 -07:00 committed by Deirdre Connolly
parent 0f51891359
commit 17fdbe941b
3 changed files with 34 additions and 7 deletions

View File

@ -1,3 +1,5 @@
use std::fmt;
#[cfg(any(test, feature = "proptest-impl"))]
use proptest_derive::Arbitrary;
@ -11,6 +13,15 @@ pub enum Network {
Testnet,
}
impl fmt::Display for Network {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Network::Mainnet => f.write_str("Mainnet"),
Network::Testnet => f.write_str("Testnet"),
}
}
}
impl Network {
/// Get the default port associated to this network.
pub fn default_port(&self) -> u16 {

View File

@ -86,6 +86,7 @@ impl CommandExt for Command {
dir,
deadline: None,
stdout: None,
bypass_test_stdout: false,
})
}
}
@ -152,6 +153,7 @@ pub struct TestChild<T> {
pub child: Child,
pub stdout: Option<Lines<BufReader<ChildStdout>>>,
pub deadline: Option<Instant>,
bypass_test_stdout: bool,
}
impl<T> TestChild<T> {
@ -190,6 +192,13 @@ impl<T> TestChild<T> {
self
}
/// Configures testrunner to forward stdout to the true stdout rather than
/// fakestdout used by cargo tests.
pub fn bypass_test_stdout(mut self, cond: bool) -> Self {
self.bypass_test_stdout = cond;
self
}
/// Checks each line of the child's stdout against `regex`, and returns matching lines.
///
/// Kills the child after the configured timeout has elapsed.
@ -221,7 +230,13 @@ impl<T> TestChild<T> {
// since we're about to discard this line write it to stdout so our
// test runner can capture it and display if the test fails, may
// cause weird reordering for stdout / stderr
println!("{}", line);
if !self.bypass_test_stdout {
println!("{}", line);
} else {
use std::io::Write;
#[allow(clippy::explicit_write)]
writeln!(std::io::stdout(), "{}", line).unwrap();
}
if re.is_match(&line) {
self.stdout = Some(lines);

View File

@ -652,7 +652,6 @@ fn cached_sapling_test_config() -> Result<ZebradConfig> {
config.consensus.checkpoint_sync = true;
config.state.cache_dir = "/zebrad-cache".into();
config.state.memory_cache_bytes = 52428800;
config.tracing.endpoint_addr = Some("0.0.0.0:3000".parse().unwrap());
Ok(config)
}
@ -666,15 +665,17 @@ fn create_cached_database_height(network: Network, height: Height) -> Result<()>
// TODO: add convenience methods?
config.network.network = network;
config.state.debug_stop_at_height = Some(height.0);
let dir = PathBuf::from("/");
let dir = PathBuf::from("/zebrad-cache");
fs::File::create(dir.join("zebrad.toml"))?.write_all(toml::to_string(&config)?.as_bytes())?;
let mut child = dir.spawn_child(&["start"])?.with_timeout(timeout);
let mut child = dir
.spawn_child(&["start"])?
.with_timeout(timeout)
.bypass_test_stdout(true);
// TODO: is there a way to check for testnet or mainnet here?
// For example: "network=Mainnet" or "network=Testnet"
child.expect_stdout("network: Mainnet,")?;
let network = format!("network: {},", network);
child.expect_stdout(&network)?;
child.expect_stdout(STOP_AT_HEIGHT_REGEX)?;
child.kill()?;