summaryrefslogtreecommitdiffstats
path: root/vendor/wait-timeout/tests/smoke.rs
blob: 233259b71c3ad1843ef7f295403b2401be2ec9ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
extern crate wait_timeout;

use std::env;
use std::process::{Command, Child, Stdio};
use std::time::{Duration, Instant};

use wait_timeout::ChildExt;

macro_rules! t {
    ($e:expr) => (match $e {
        Ok(e) => e,
        Err(e) => panic!("{} failed with {}", stringify!($e), e),
    })
}

fn sleeper(ms: u32) -> Child {
    let mut me = env::current_exe().unwrap();
    me.pop();
    if me.ends_with("deps") {
        me.pop();
    }
    me.push("sleep");
    t!(Command::new(me).arg(ms.to_string()).spawn())
}

fn exit(code: u32) -> Child {
    let mut me = env::current_exe().unwrap();
    me.pop();
    if me.ends_with("deps") {
        me.pop();
    }
    me.push("exit");
    t!(Command::new(me).arg(code.to_string()).spawn())
}

fn reader() -> Child {
    let mut me = env::current_exe().unwrap();
    me.pop();
    if me.ends_with("deps") {
        me.pop();
    }
    me.push("reader");
    t!(Command::new(me).stdin(Stdio::piped()).spawn())
}

#[test]
fn smoke_insta_timeout() {
    let mut child = sleeper(1_000);
    assert_eq!(t!(child.wait_timeout_ms(0)), None);

    t!(child.kill());
    let status = t!(child.wait());
    assert!(!status.success());
}

#[test]
fn smoke_success() {
    let start = Instant::now();
    let mut child = sleeper(0);
    let status = t!(child.wait_timeout_ms(1_000)).expect("should have succeeded");
    assert!(status.success());

    assert!(start.elapsed() < Duration::from_millis(500));
}

#[test]
fn smoke_timeout() {
    let mut child = sleeper(1_000_000);
    let start = Instant::now();
    assert_eq!(t!(child.wait_timeout_ms(100)), None);
    assert!(start.elapsed() > Duration::from_millis(80));

    t!(child.kill());
    let status = t!(child.wait());
    assert!(!status.success());
}

#[test]
fn smoke_reader() {
    let mut child = reader();
    let dur = Duration::from_millis(100);
    let status = t!(child.wait_timeout(dur)).unwrap();
    assert!(status.success());
}

#[test]
fn exit_codes() {
    let mut child = exit(0);
    let status = t!(child.wait_timeout_ms(1_000)).unwrap();
    assert_eq!(status.code(), Some(0));

    let mut child = exit(1);
    let status = t!(child.wait_timeout_ms(1_000)).unwrap();
    assert_eq!(status.code(), Some(1));

    // check STILL_ACTIVE on windows, on unix this ends up just getting
    // truncated so don't bother with it.
    if cfg!(windows) {
        let mut child = exit(259);
        let status = t!(child.wait_timeout_ms(1_000)).unwrap();
        assert_eq!(status.code(), Some(259));
    }
}