Skip to content

Commit 58fb231

Browse files
committed
Auto merge of #13099 - weihanglo:running-binary, r=epage
test(install): use TCP connection instead of thread sleep
2 parents f023d05 + 297b827 commit 58fb231

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

tests/testsuite/install.rs

+33-15
Original file line numberDiff line numberDiff line change
@@ -2522,28 +2522,32 @@ fn assert_tracker_noexistence(key: &str) {
25222522

25232523
#[cargo_test]
25242524
fn uninstall_running_binary() {
2525+
use std::io::Write;
2526+
25252527
Package::new("foo", "0.0.1")
2526-
.file("src/lib.rs", "")
25272528
.file(
25282529
"Cargo.toml",
25292530
r#"
2530-
[package]
2531-
name = "foo"
2532-
version = "0.0.1"
2533-
2534-
[[bin]]
2535-
name = "foo"
2536-
path = "src/main.rs"
2537-
"#,
2531+
[package]
2532+
name = "foo"
2533+
version = "0.0.1"
2534+
"#,
25382535
)
25392536
.file(
25402537
"src/main.rs",
25412538
r#"
2542-
use std::{{thread, time}};
2543-
fn main() {
2544-
thread::sleep(time::Duration::from_secs(3));
2545-
}
2546-
"#,
2539+
use std::net::TcpStream;
2540+
use std::env::var;
2541+
use std::io::Read;
2542+
fn main() {
2543+
for i in 0..2 {
2544+
TcpStream::connect(&var("__ADDR__").unwrap()[..])
2545+
.unwrap()
2546+
.read_to_end(&mut Vec::new())
2547+
.unwrap();
2548+
}
2549+
}
2550+
"#,
25472551
)
25482552
.publish();
25492553

@@ -2565,15 +2569,26 @@ fn uninstall_running_binary() {
25652569
assert_has_installed_exe(cargo_home(), "foo");
25662570

25672571
let foo_bin = cargo_home().join("bin").join(exe("foo"));
2568-
let t = thread::spawn(|| ProcessBuilder::new(foo_bin).exec().unwrap());
2572+
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
2573+
let addr = l.local_addr().unwrap().to_string();
2574+
let t = thread::spawn(move || {
2575+
ProcessBuilder::new(foo_bin)
2576+
.env("__ADDR__", addr)
2577+
.exec()
2578+
.unwrap();
2579+
});
25692580
let key = "foo 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)";
25702581

25712582
#[cfg(windows)]
25722583
{
2584+
// Ensure foo is running before the first `cargo uninstall` call
2585+
l.accept().unwrap().0.write_all(&[1]).unwrap();
25732586
cargo_process("uninstall foo")
25742587
.with_status(101)
25752588
.with_stderr_contains("[ERROR] failed to remove file `[CWD]/home/.cargo/bin/foo[EXE]`")
25762589
.run();
2590+
// Ensure foo is stopped before the second `cargo uninstall` call
2591+
l.accept().unwrap().0.write_all(&[1]).unwrap();
25772592
t.join().unwrap();
25782593
cargo_process("uninstall foo")
25792594
.with_stderr("[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]")
@@ -2582,9 +2597,12 @@ fn uninstall_running_binary() {
25822597

25832598
#[cfg(not(windows))]
25842599
{
2600+
// Ensure foo is running before the first `cargo uninstall` call
2601+
l.accept().unwrap().0.write_all(&[1]).unwrap();
25852602
cargo_process("uninstall foo")
25862603
.with_stderr("[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]")
25872604
.run();
2605+
l.accept().unwrap().0.write_all(&[1]).unwrap();
25882606
t.join().unwrap();
25892607
};
25902608

0 commit comments

Comments
 (0)