Skip to content

Commit

Permalink
Fix a timeout bug in zebra_test::command
Browse files Browse the repository at this point in the history
And add tests for the command functionality.

Also document some remaining bugs (see ZcashFoundation#1140).
  • Loading branch information
teor2345 committed Oct 9, 2020
1 parent 5d374eb commit 4a74289
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions zebra-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ thiserror = "1.0.21"
pretty_assertions = "0.6.1"
owo-colors = "1.1.3"
proptest = "0.10.1"
tempdir = "0.3.7"

[dev-dependencies]
tokio = { version = "0.2", features = ["full"] }
16 changes: 15 additions & 1 deletion zebra-test/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub struct TestChild<T> {
}

impl<T> TestChild<T> {
/// Kill the child process.
#[spandoc::spandoc]
pub fn kill(&mut self) -> Result<()> {
/// SPANDOC: Killing child process
Expand All @@ -131,6 +132,9 @@ impl<T> TestChild<T> {
Ok(())
}

/// Waits for the child process to exit, then returns its output.
///
/// Ignores any configured timeouts.
#[spandoc::spandoc]
pub fn wait_with_output(self) -> Result<TestOutput<T>> {
/// SPANDOC: waiting for command to exit
Expand All @@ -146,11 +150,18 @@ impl<T> TestChild<T> {
})
}

/// Set a timeout for `expect_stdout`.
///
/// Does not apply to `wait_with_output`.
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.deadline = Some(Instant::now() + timeout);
self
}

/// Checks each line of the child's stdout against `regex`, and returns matching lines.
///
/// Kills the child after the configured timeout has elapsed.
/// Note: the timeout is only checked after each line.
#[instrument(skip(self))]
pub fn expect_stdout(&mut self, regex: &str) -> Result<&mut Self> {
if self.stdout.is_none() {
Expand Down Expand Up @@ -186,7 +197,10 @@ impl<T> TestChild<T> {
}
}

if self.past_deadline() && !self.is_running() {
if self.past_deadline() && self.is_running() {
// If the process exits between is_running and kill, we will see
// spurious errors here. If that happens, ignore "no such process"
// errors from kill.
self.kill()?;
}

Expand Down
11 changes: 6 additions & 5 deletions zebrad/tests/acceptance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,22 @@ fn testdir() -> Result<TempDir> {
}

/// Extension trait for methods on `tempdir::TempDir` for using it as a test
/// directory.
trait TestDirExt
/// directory for `zebrad`.
trait ZebradTestDirExt
where
Self: Borrow<TempDir> + Sized,
{
/// Spawn the zebrad as a child process in this test directory, potentially
/// taking ownership of the tempdir for the duration of the child process.
/// Spawn `zebrad` with `args` as a child process in this test directory,
/// potentially taking ownership of the tempdir for the duration of the
/// child process.
fn spawn_child(self, args: &[&str]) -> Result<TestChild<Self>>;

/// Add the given config to the test directory and use it for all
/// subsequently spawned processes.
fn with_config(self, config: ZebradConfig) -> Result<Self>;
}

impl<T> TestDirExt for T
impl<T> ZebradTestDirExt for T
where
Self: Borrow<TempDir> + Sized,
{
Expand Down

0 comments on commit 4a74289

Please sign in to comment.