1
use super::*;
2
use std::process::Command;
3
use std::time::{Duration, Instant};
4

            
5
#[test]
6
1
fn a_stat_line_is_read_past_a_command_name_with_spaces_and_parens() {
7
1
    let line = "4242 (a (b) c) R 17 4242 4242 0 -1 4194304 1 0 0 0 250 50 7 3 20 0 1 0";
8
1
    let stat = parse(line).expect("a well-formed line parses");
9
1
    assert_eq!((stat.pid, stat.ppid, stat.ticks), (4242, 17, 310));
10
1
}
11

            
12
#[test]
13
1
fn a_truncated_stat_line_is_skipped() {
14
1
    assert!(parse("4242 (sh) S 17 4242").is_none());
15
1
    assert!(parse("garbage").is_none());
16
1
}
17

            
18
#[test]
19
1
fn a_busy_child_is_counted_in_its_parents_tree() {
20
1
    let _processes = crate::test_support::processes();
21
1
    let me = std::process::id();
22
1
    let before = tree_ticks(me);
23
1
    let mut child = Command::new("bash")
24
1
        .args(["-c", "while :; do :; done"])
25
1
        .spawn()
26
1
        .expect("bash runs");
27
    // A loaded or CPU-throttled runner gives the child less than a core, so
28
    // wait for the ticks instead of sampling a fixed window.
29
1
    let started = Instant::now();
30
1
    let mut seen = 0;
31
7
    while seen < TICKS_PER_SECOND / 2 && started.elapsed() < Duration::from_secs(30) {
32
6
        std::thread::sleep(Duration::from_millis(100));
33
6
        seen = seen.max(tree_ticks(me).saturating_sub(before));
34
    }
35
1
    child.kill().expect("the spinning child is killed");
36
1
    child.wait().expect("the killed child is reaped");
37
1
    assert!(
38
1
        seen >= TICKS_PER_SECOND / 2,
39
        "a spinning child must show up as CPU work, saw {seen} ticks"
40
    );
41
1
}
42

            
43
#[test]
44
1
fn a_reaped_childs_cpu_stays_in_the_live_parents_total() {
45
    use nix::sys::signal::{Signal, kill};
46
    use nix::unistd::Pid;
47
    use std::io::{BufRead, BufReader};
48
    use std::process::Stdio;
49

            
50
1
    let _processes = crate::test_support::processes();
51
1
    let mut parent = Command::new("sh")
52
1
        .args(["-c", "while :; do :; done & echo $!; wait; exec sleep 30"])
53
1
        .stdout(Stdio::piped())
54
1
        .spawn()
55
1
        .expect("sh runs");
56
1
    let mut line = String::new();
57
1
    let burner = parent
58
1
        .stdout
59
1
        .take()
60
1
        .map(BufReader::new)
61
1
        .and_then(|mut out| out.read_line(&mut line).ok())
62
1
        .and_then(|_| line.trim().parse::<i32>().ok());
63
1
    let started = Instant::now();
64
7
    while tree_ticks(parent.id()) < TICKS_PER_SECOND / 4
65
6
        && started.elapsed() < Duration::from_secs(30)
66
    {
67
6
        std::thread::sleep(Duration::from_millis(50));
68
    }
69
1
    let before = tree_ticks(parent.id());
70
1
    let killed = burner.map(|pid| kill(Pid::from_raw(pid), Signal::SIGKILL));
71
1
    let mut after = before;
72
1
    let sampled = Instant::now();
73
19
    while sampled.elapsed() < Duration::from_secs(1) {
74
18
        std::thread::sleep(Duration::from_millis(50));
75
18
        after = after.min(tree_ticks(parent.id()));
76
    }
77
1
    let _ = parent.kill();
78
1
    let _ = parent.wait();
79
1
    assert!(matches!(killed, Some(Ok(()))), "the burner was not killed");
80
1
    assert!(
81
1
        before >= TICKS_PER_SECOND / 4,
82
        "the burner never showed CPU work, saw {before} ticks"
83
    );
84
1
    assert!(
85
1
        after >= before,
86
        "the tree total fell from {before} to {after} after the child was reaped"
87
    );
88
1
}
89

            
90
#[test]
91
1
fn an_unknown_root_has_no_ticks() {
92
1
    assert_eq!(tree_ticks(u32::MAX), 0);
93
1
}