]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/try-wait.rs
Rollup merge of #39604 - est31:i128_tests, r=alexcrichton
[rust.git] / src / test / run-pass / try-wait.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // ignore-emscripten
12
13 #![feature(process_try_wait)]
14
15 use std::env;
16 use std::process::Command;
17 use std::thread;
18 use std::time::Duration;
19
20 fn main() {
21     let args = env::args().collect::<Vec<_>>();
22     if args.len() != 1 {
23         match &args[1][..] {
24             "sleep" => thread::sleep(Duration::new(1_000, 0)),
25             _ => {}
26         }
27         return
28     }
29
30     let mut me = Command::new(env::current_exe().unwrap())
31                          .arg("sleep")
32                          .spawn()
33                          .unwrap();
34     let maybe_status = me.try_wait().unwrap();
35     assert!(maybe_status.is_none());
36     let maybe_status = me.try_wait().unwrap();
37     assert!(maybe_status.is_none());
38
39     me.kill().unwrap();
40     me.wait().unwrap();
41
42     let status = me.try_wait().unwrap().unwrap();
43     assert!(!status.success());
44     let status = me.try_wait().unwrap().unwrap();
45     assert!(!status.success());
46
47     let mut me = Command::new(env::current_exe().unwrap())
48                          .arg("return-quickly")
49                          .spawn()
50                          .unwrap();
51     loop {
52         match me.try_wait() {
53             Ok(Some(res)) => {
54                 assert!(res.success());
55                 break
56             }
57             Ok(None) => {
58                 thread::sleep(Duration::from_millis(1));
59             }
60             Err(e) => panic!("error in try_wait: {}", e),
61         }
62     }
63
64     let status = me.try_wait().unwrap().unwrap();
65     assert!(status.success());
66 }