Skip to content

Commit

Permalink
test(install): use TCP connection instead of thread sleep
Browse files Browse the repository at this point in the history
This is more robust than `thread::sleep`, ensuring

* foo is running before the first `cargo uninstall` call
* foo is stopped before the second `cargo uninstall` call
  • Loading branch information
weihanglo committed Dec 2, 2023
1 parent bbd2dcd commit c4a6629
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2523,27 +2523,22 @@ fn assert_tracker_noexistence(key: &str) {
#[cargo_test]
fn uninstall_running_binary() {
Package::new("foo", "0.0.1")
.file("src/lib.rs", "")
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
[[bin]]
name = "foo"
path = "src/main.rs"
"#,
[package]
name = "foo"
version = "0.0.1"
"#,
)
.file(
"src/main.rs",
r#"
use std::{{thread, time}};
fn main() {
thread::sleep(time::Duration::from_secs(3));
}
"#,
fn main() {
std::net::TcpStream::connect(&std::env::var("ADDR").unwrap()[..]).unwrap();
std::net::TcpStream::connect(&std::env::var("ADDR").unwrap()[..]).unwrap();
}
"#,
)
.publish();

Expand All @@ -2565,15 +2560,26 @@ fn uninstall_running_binary() {
assert_has_installed_exe(cargo_home(), "foo");

let foo_bin = cargo_home().join("bin").join(exe("foo"));
let t = thread::spawn(|| ProcessBuilder::new(foo_bin).exec().unwrap());
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = l.local_addr().unwrap().to_string();
let t = thread::spawn(move || {
ProcessBuilder::new(foo_bin)
.env("ADDR", addr)
.exec()
.unwrap();
});
let key = "foo 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)";

#[cfg(windows)]
{
// Ensure foo is running before the first `cargo uninstall` call
l.accept().unwrap();
cargo_process("uninstall foo")
.with_status(101)
.with_stderr_contains("[ERROR] failed to remove file `[CWD]/home/.cargo/bin/foo[EXE]`")
.run();
// Ensure foo is stopped before the second `cargo uninstall` call
l.accept().unwrap();
t.join().unwrap();
cargo_process("uninstall foo")
.with_stderr("[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]")
Expand All @@ -2582,9 +2588,13 @@ fn uninstall_running_binary() {

#[cfg(not(windows))]
{
// Ensure foo is running before the first `cargo uninstall` call
l.accept().unwrap();
cargo_process("uninstall foo")
.with_stderr("[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]")
.run();
// Ensure foo is stopped before the second `cargo uninstall` call
l.accept().unwrap();
t.join().unwrap();
};

Expand Down

0 comments on commit c4a6629

Please sign in to comment.