#[cfg(windows)]
use std::path::{Path, PathBuf};
#[cfg(windows)]
use std::process::{Command, Stdio};
#[cfg(windows)]
use std::time::Duration;
#[cfg(windows)]
fn system_ping() -> PathBuf {
std::env::var("WINDIR")
.map(|windows_dir| Path::new(&windows_dir).join("System32").join("ping.exe"))
.unwrap_or_else(|_| PathBuf::from(r"C:\Windows\System32\ping.exe"))
}
#[cfg(windows)]
fn spawn_ping_holder() -> std::process::Child {
Command::new("ping")
.args(["-n", "90", "127.0.0.1"])
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.expect("spawn the holder process")
}
#[cfg(windows)]
fn wait_for_instances(
exe: &Path,
deadline: Duration,
) -> Vec<shun::targets::instance::RunningInstance> {
let started = std::time::Instant::now();
loop {
match shun::targets::instance::running_instances(exe) {
Ok(instances) if !instances.is_empty() => return instances,
_ => {}
}
if started.elapsed() >= deadline {
return Vec::new();
}
std::thread::sleep(Duration::from_millis(100));
}
}
#[cfg(windows)]
#[test]
fn detects_and_stops_a_running_instance_by_exact_path() {
use shun::targets::instance::{running_instances, stop_instances};
if std::env::var("SHUN_PROBE_LIVE").as_deref() != Ok("1") {
eprintln!("SHUN_PROBE_LIVE unset — the live probe is skipped");
return;
}
let ping = system_ping();
let mut child = spawn_ping_holder();
let detected = wait_for_instances(&ping, Duration::from_secs(5));
assert!(
detected.iter().any(|i| i.pid == child.id()),
"the spawned holder is detected by path: {detected:?}"
);
let survivors = stop_instances(&ping).expect("the shutdown session opens");
assert!(
survivors.is_empty(),
"nothing survives the shutdown: {survivors:?}"
);
let _ = child.wait();
let detected = running_instances(&ping).expect("the post-shutdown session opens");
assert!(
!detected.iter().any(|i| i.pid == child.id()),
"the holder is gone: {detected:?}"
);
}