• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![cfg(all(unix, feature = "process"))]
2 #![warn(rust_2018_idioms)]
3 
4 use std::process::Stdio;
5 use std::time::Duration;
6 use tokio::io::AsyncReadExt;
7 use tokio::process::Command;
8 use tokio::time::sleep;
9 use tokio_test::assert_ok;
10 
11 #[tokio::test]
kill_on_drop()12 async fn kill_on_drop() {
13     let mut cmd = Command::new("bash");
14     cmd.args(&[
15         "-c",
16         "
17        # Fork another child that won't get killed
18        sh -c 'sleep 1; echo child ran' &
19        disown -a
20 
21        # Await our death
22        sleep 5
23        echo hello from beyond the grave
24     ",
25     ]);
26 
27     let mut child = cmd
28         .kill_on_drop(true)
29         .stdout(Stdio::piped())
30         .spawn()
31         .unwrap();
32 
33     sleep(Duration::from_secs(2)).await;
34 
35     let mut out = child.stdout.take().unwrap();
36     drop(child);
37 
38     let mut msg = String::new();
39     assert_ok!(out.read_to_string(&mut msg).await);
40 
41     assert_eq!("child ran\n", msg);
42 }
43