1
//! The browser harness leaves nothing behind in the temp directory.
2

            
3
use std::time::{Duration, Instant};
4

            
5
use anyhow::{Result, ensure};
6
use tempfile::TempDir;
7

            
8
use super::driver::Driver;
9
use super::harness::{chromedriver, descendants};
10

            
11
const DEADLINE: Duration = Duration::from_secs(10);
12

            
13
/// The pids of `pids` that are alive and not zombies.
14
4
fn alive(pids: &[u32]) -> Vec<u32> {
15
4
    pids.iter()
16
4
        .copied()
17
40
        .filter(|pid| {
18
40
            std::fs::read_to_string(format!("/proc/{pid}/stat"))
19
40
                .ok()
20
15
                .and_then(|stat| {
21
15
                    stat.rsplit_once(')')
22
15
                        .and_then(|(_, rest)| rest.split_whitespace().next().map(|s| s != "Z"))
23
15
                })
24
40
                .unwrap_or(false)
25
40
        })
26
        .collect()
27
4
}
28

            
29
/// Waits for the kill to take effect: delivery and teardown are asynchronous.
30
2
fn wait_for_exit(pids: &[u32]) -> Result<()> {
31
2
    let deadline = Instant::now() + DEADLINE;
32
    loop {
33
4
        let survivors = alive(pids);
34
4
        if survivors.is_empty() {
35
2
            return Ok(());
36
2
        }
37
2
        ensure!(
38
2
            Instant::now() < deadline,
39
            "processes survived {DEADLINE:?}: {survivors:?}"
40
        );
41
2
        std::thread::sleep(Duration::from_millis(50));
42
    }
43
2
}
44

            
45
#[tokio::test]
46
1
async fn chromedriver_and_chrome_leave_no_temp_entries() -> Result<()> {
47
1
    let outer = TempDir::new()?;
48
1
    let private = TempDir::new_in(outer.path())?;
49
1
    let (process, endpoint) = chromedriver(private.path()).await?;
50
1
    let driver = Driver::connect(&endpoint).await?;
51
1
    let used = std::fs::read_dir(private.path())?.count();
52
1
    assert!(used > 0, "chromedriver wrote nothing to its private TMPDIR");
53

            
54
    // A failed scenario never calls `quit`; dropping must still clean up.
55
1
    let mut family = descendants(process.id());
56
1
    family.push(process.id());
57
1
    assert!(family.len() > 1, "chromedriver started no Chrome");
58
1
    drop(driver);
59
1
    drop(process);
60
1
    wait_for_exit(&family)?;
61
1
    drop(private);
62

            
63
1
    assert_eq!(std::fs::read_dir(outer.path())?.count(), 0);
64
2
    Ok(())
65
1
}
66

            
67
#[tokio::test]
68
1
async fn a_panicking_scenario_leaves_no_chrome_behind() -> Result<()> {
69
1
    let tmp = TempDir::new()?;
70
1
    let (process, endpoint) = chromedriver(tmp.path()).await?;
71
1
    let driver = Driver::connect(&endpoint).await?;
72
1
    let mut family = descendants(process.id());
73
1
    family.push(process.id());
74

            
75
1
    let scenario = tokio::spawn(async move {
76
1
        let _held = (process, driver);
77
1
        std::panic::panic_any("scenario failed before quit");
78
    });
79
1
    assert!(scenario.await.is_err_and(|e| e.is_panic()));
80

            
81
2
    wait_for_exit(&family)
82
1
}