Skip to content

Commit c4a6629

Browse files
committed
test(install): use TCP connection instead of thread sleep
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
1 parent bbd2dcd commit c4a6629

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

tests/testsuite/install.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,27 +2523,22 @@ fn assert_tracker_noexistence(key: &str) {
25232523
#[cargo_test]
25242524
fn uninstall_running_binary() {
25252525
Package::new("foo", "0.0.1")
2526-
.file("src/lib.rs", "")
25272526
.file(
25282527
"Cargo.toml",
25292528
r#"
2530-
[package]
2531-
name = "foo"
2532-
version = "0.0.1"
2533-
2534-
[[bin]]
2535-
name = "foo"
2536-
path = "src/main.rs"
2537-
"#,
2529+
[package]
2530+
name = "foo"
2531+
version = "0.0.1"
2532+
"#,
25382533
)
25392534
.file(
25402535
"src/main.rs",
25412536
r#"
2542-
use std::{{thread, time}};
2543-
fn main() {
2544-
thread::sleep(time::Duration::from_secs(3));
2545-
}
2546-
"#,
2537+
fn main() {
2538+
std::net::TcpStream::connect(&std::env::var("ADDR").unwrap()[..]).unwrap();
2539+
std::net::TcpStream::connect(&std::env::var("ADDR").unwrap()[..]).unwrap();
2540+
}
2541+
"#,
25472542
)
25482543
.publish();
25492544

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

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

25712573
#[cfg(windows)]
25722574
{
2575+
// Ensure foo is running before the first `cargo uninstall` call
2576+
l.accept().unwrap();
25732577
cargo_process("uninstall foo")
25742578
.with_status(101)
25752579
.with_stderr_contains("[ERROR] failed to remove file `[CWD]/home/.cargo/bin/foo[EXE]`")
25762580
.run();
2581+
// Ensure foo is stopped before the second `cargo uninstall` call
2582+
l.accept().unwrap();
25772583
t.join().unwrap();
25782584
cargo_process("uninstall foo")
25792585
.with_stderr("[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]")
@@ -2582,9 +2588,13 @@ fn uninstall_running_binary() {
25822588

25832589
#[cfg(not(windows))]
25842590
{
2591+
// Ensure foo is running before the first `cargo uninstall` call
2592+
l.accept().unwrap();
25852593
cargo_process("uninstall foo")
25862594
.with_stderr("[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]")
25872595
.run();
2596+
// Ensure foo is stopped before the second `cargo uninstall` call
2597+
l.accept().unwrap();
25882598
t.join().unwrap();
25892599
};
25902600

0 commit comments

Comments
 (0)