Lines
98.11 %
Functions
100 %
Branches
//! The browser harness leaves nothing behind in the temp directory.
use std::time::{Duration, Instant};
use anyhow::{Result, ensure};
use tempfile::TempDir;
use super::driver::Driver;
use super::harness::{chromedriver, descendants};
const DEADLINE: Duration = Duration::from_secs(10);
/// The pids of `pids` that are alive and not zombies.
fn alive(pids: &[u32]) -> Vec<u32> {
pids.iter()
.copied()
.filter(|pid| {
std::fs::read_to_string(format!("/proc/{pid}/stat"))
.ok()
.and_then(|stat| {
stat.rsplit_once(')')
.and_then(|(_, rest)| rest.split_whitespace().next().map(|s| s != "Z"))
})
.unwrap_or(false)
.collect()
}
/// Waits for the kill to take effect: delivery and teardown are asynchronous.
fn wait_for_exit(pids: &[u32]) -> Result<()> {
let deadline = Instant::now() + DEADLINE;
loop {
let survivors = alive(pids);
if survivors.is_empty() {
return Ok(());
ensure!(
Instant::now() < deadline,
"processes survived {DEADLINE:?}: {survivors:?}"
);
std::thread::sleep(Duration::from_millis(50));
#[tokio::test]
async fn chromedriver_and_chrome_leave_no_temp_entries() -> Result<()> {
let outer = TempDir::new()?;
let private = TempDir::new_in(outer.path())?;
let (process, endpoint) = chromedriver(private.path()).await?;
let driver = Driver::connect(&endpoint).await?;
let used = std::fs::read_dir(private.path())?.count();
assert!(used > 0, "chromedriver wrote nothing to its private TMPDIR");
// A failed scenario never calls `quit`; dropping must still clean up.
let mut family = descendants(process.id());
family.push(process.id());
assert!(family.len() > 1, "chromedriver started no Chrome");
drop(driver);
drop(process);
wait_for_exit(&family)?;
drop(private);
assert_eq!(std::fs::read_dir(outer.path())?.count(), 0);
Ok(())
async fn a_panicking_scenario_leaves_no_chrome_behind() -> Result<()> {
let tmp = TempDir::new()?;
let (process, endpoint) = chromedriver(tmp.path()).await?;
let scenario = tokio::spawn(async move {
let _held = (process, driver);
std::panic::panic_any("scenario failed before quit");
});
assert!(scenario.await.is_err_and(|e| e.is_panic()));
wait_for_exit(&family)