Files
tokio/tokio-net/tests/process_stdio.rs
T

148 lines
4.0 KiB
Rust
Raw Normal View History

#![cfg(feature = "process")]
#![warn(rust_2018_idioms)]
2019-07-29 21:36:11 -04:00
2016-12-04 18:47:48 +01:00
#[macro_use]
2019-08-27 17:53:57 -07:00
extern crate tracing;
2016-11-18 20:57:31 +01:00
use std::env;
use std::io;
use std::process::{ExitStatus, Stdio};
2016-11-18 20:57:31 +01:00
2019-07-29 21:36:11 -04:00
use futures_util::future;
use futures_util::future::FutureExt;
2019-08-07 10:38:45 -07:00
use futures_util::stream::StreamExt;
use tokio::codec::{FramedRead, LinesCodec};
use tokio::io::AsyncWriteExt;
use tokio_net::process::{Child, Command};
2016-11-18 20:57:31 +01:00
2016-12-18 22:48:18 -08:00
mod support;
use support::*;
2016-12-18 22:48:18 -08:00
fn cat() -> Command {
let mut me = env::current_exe().unwrap();
me.pop();
if me.ends_with("deps") {
me.pop();
}
me.push("test-cat");
let mut cmd = Command::new(me);
2019-06-24 17:19:16 -07:00
cmd.stdin(Stdio::piped()).stdout(Stdio::piped());
2016-11-18 20:57:31 +01:00
cmd
}
2019-08-07 10:38:45 -07:00
async fn feed_cat(mut cat: Child, n: usize) -> io::Result<ExitStatus> {
let mut stdin = cat.stdin().take().unwrap();
2016-11-18 20:57:31 +01:00
let stdout = cat.stdout().take().unwrap();
// Produce n lines on the child's stdout.
2019-08-07 10:38:45 -07:00
let write = async {
debug!("starting to feed");
for i in 0..n {
debug!("sending line {} to child", i);
let bytes = format!("line {}\n", i).into_bytes();
stdin.write_all(&bytes).await.unwrap();
}
drop(stdin);
};
let read = async {
let mut reader = FramedRead::new(stdout, LinesCodec::new());
let mut num_lines = 0;
// Try to read `n + 1` lines, ensuring the last one is empty
// (i.e. EOF is reached after `n` lines.
loop {
2019-07-29 21:36:11 -04:00
debug!("starting read from child");
2019-08-07 10:38:45 -07:00
let data = reader
.next()
2019-07-29 21:36:11 -04:00
.await
2019-08-07 10:38:45 -07:00
.unwrap_or_else(|| Ok(String::new()))
.expect("failed to read line");
let num_read = data.len();
let done = num_lines >= n;
2019-06-24 17:19:16 -07:00
debug!(
"read line {} from child ({} bytes, done: {})",
2019-08-07 10:38:45 -07:00
num_lines, num_read, done
2019-06-24 17:19:16 -07:00
);
2019-08-07 10:38:45 -07:00
match (done, num_read) {
(false, 0) => panic!("broken pipe"),
(true, n) if n != 0 => panic!("extraneous data"),
2016-11-18 20:57:31 +01:00
_ => {
2019-08-07 10:38:45 -07:00
let expected = format!("line {}", num_lines);
assert_eq!(expected, data);
2016-11-18 20:57:31 +01:00
}
2019-08-07 10:38:45 -07:00
};
num_lines += 1;
if num_lines >= n {
break;
2016-11-18 20:57:31 +01:00
}
2019-08-07 10:38:45 -07:00
}
};
2016-11-18 20:57:31 +01:00
// Compose reading and writing concurrently.
2019-08-07 10:38:45 -07:00
future::join3(write, read, cat)
.map(|(_, _, status)| status)
.await
2016-11-18 20:57:31 +01:00
}
/// Check for the following properties when feeding stdin and
/// consuming stdout of a cat-like process:
///
/// - A number of lines that amounts to a number of bytes exceeding a
/// typical OS buffer size can be fed to the child without
/// deadlock. This tests that we also consume the stdout
/// concurrently; otherwise this would deadlock.
///
/// - We read the same lines from the child that we fed it.
///
2016-11-18 20:57:31 +01:00
/// - The child does produce EOF on stdout after the last line.
2019-08-07 10:38:45 -07:00
#[tokio::test]
async fn feed_a_lot() {
let child = cat().spawn().unwrap();
let status = with_timeout(feed_cat(child, 10000)).await.unwrap();
2016-11-18 20:57:31 +01:00
assert_eq!(status.code(), Some(0));
}
2019-08-07 10:38:45 -07:00
#[tokio::test]
async fn wait_with_output_captures() {
let mut child = cat().spawn().unwrap();
2019-07-29 21:36:11 -04:00
let mut stdin = child.stdin().take().unwrap();
let write_bytes = b"1234";
let future = async {
2019-08-07 10:38:45 -07:00
stdin.write_all(write_bytes).await?;
2019-07-29 21:36:11 -04:00
drop(stdin);
let out = child.wait_with_output();
out.await
};
2016-12-18 22:40:14 -08:00
let output = with_timeout(future).await.unwrap();
2016-12-18 22:40:14 -08:00
assert!(output.status.success());
2019-07-29 21:36:11 -04:00
assert_eq!(output.stdout, write_bytes);
2016-12-18 22:40:14 -08:00
assert_eq!(output.stderr.len(), 0);
}
2019-08-07 10:38:45 -07:00
#[tokio::test]
async fn status_closes_any_pipes() {
// Cat will open a pipe between the parent and child.
// If `status_async` doesn't ensure the handles are closed,
// we would end up blocking forever (and time out).
let child = cat().status();
2018-05-13 14:56:30 -07:00
with_timeout(child)
2019-08-07 10:38:45 -07:00
.await
2018-05-13 14:56:30 -07:00
.expect("time out exceeded! did we get stuck waiting on the child?");
}