Skip to content

Commit

Permalink
Add test for behavior if tool is killed mid-run
Browse files Browse the repository at this point in the history
This is the codified version of #36, just with a SIGKILL instead of the
mentioned SIGING (or SIGTERM). It is unclear, if the tool could even be-
have sane with SIGKILL, but this is the only thing available in the Rust
standard library. The test currently fails and thus shows that the issue
is reproducible.
  • Loading branch information
jfrimmel committed Sep 20, 2024
1 parent 3084430 commit 40d3951
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/corpus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ path = "issue-74.rs"
name = "issue-20"
path = "issue-20.rs"

[[bin]]
name = "issue-36"
path = "issue-36.rs"

[[bin]]
name = "issue-70"
path = "issue-70.rs"
6 changes: 6 additions & 0 deletions tests/corpus/issue-36.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
for n in 1..=10 {
std::thread::sleep(std::time::Duration::from_secs(1));
println!("waited {n} seconds");
}
}
49 changes: 49 additions & 0 deletions tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,52 @@ fn environment_variables_are_passed_to_program_under_test() {
.assert()
.stdout(predicates::str::contains("RUST_LOG=debug"));
}

/// Issue: [#36]: make sure, that an interrupted `cargo valgrind` invocation
/// kills the running program, so that it does not run in the background.
///
/// [#36]: https://github.com/jfrimmel/cargo-valgrind/issues/36
#[test]
fn interrupted_program_execution() {
use assert_cmd::cargo::CommandCargoExt;
use std::{io::Read, process, thread, time::Duration};

// pre-build the crate to run, so that it does not need to be built later on
cargo_valgrind()
.arg("build")
.args(TARGET_CRATE)
.arg("--bin=issue-36");

// We need the raw `std::process::Command` in order to send the kill signal
// to it. Therefore this does not use the `cargo_valgrind()` helper as all
// other tests.
let mut cargo_valgrind = process::Command::cargo_bin("cargo-valgrind")
.unwrap()
.arg("valgrind")
.arg("run")
.arg("-q") // silence cargo output
.args(TARGET_CRATE)
.arg("--bin=issue-36")
.stderr(process::Stdio::piped())
.stdout(process::Stdio::piped())
.spawn()
.unwrap();

// wait until program is certainly started
thread::sleep(Duration::from_millis(500));

// kill `cargo valgrind`, which should kill the run program as well.
cargo_valgrind.kill().unwrap();
cargo_valgrind.wait().unwrap();

// Check, what the helper program printed. The run program prints one line
// every second. Since this test should have killed the program before the
// first second is elapsed, there should be no output.
let mut stdout = String::new();
cargo_valgrind
.stdout
.unwrap()
.read_to_string(&mut stdout)
.unwrap();
assert_eq!("", stdout, "Program must end before the first print");
}

0 comments on commit 40d3951

Please sign in to comment.