mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-10-01 12:20:39 +00:00

In an effort to reach API stability, the `tokio` crate is shedding its _public_ dependencies on crates that are either a) do not provide a stable (1.0+) release with longevity guarantees or b) match the `tokio` release cadence. Of course, implementing `std` traits fits the requirements. The on exception, for now, is the `Stream` trait found in `futures_core`. It is expected that this trait will not change much and be moved into `std. Since Tokio is not yet going reaching 1.0, I feel that it is acceptable to maintain a dependency on this trait given how foundational it is. Since the `Stream` implementation is optional, types that are logically streams provide `async fn next_*` functions to obtain the next value. Avoiding the `next()` name prevents fn conflicts with `StreamExt::next()`. Additionally, some misc cleanup is also done: - `tokio::io::io` -> `tokio::io::util`. - `delay` -> `delay_until`. - `Timeout::new` -> `timeout(...)`. - `signal::ctrl_c()` returns a future instead of a stream. - `{tcp,unix}::Incoming` is removed (due to lack of `Stream` trait). - `time::Throttle` is removed (due to lack of `Stream` trait). - Fix: `mpsc::UnboundedSender::send(&self)` (no more conflict with `Sink` fns).
127 lines
3.4 KiB
Rust
127 lines
3.4 KiB
Rust
#![warn(rust_2018_idioms)]
|
|
|
|
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
|
use tokio::process::{Child, Command};
|
|
use tokio_test::assert_ok;
|
|
|
|
use futures::future::{self, FutureExt};
|
|
use std::env;
|
|
use std::io;
|
|
use std::process::{ExitStatus, Stdio};
|
|
|
|
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);
|
|
cmd.stdin(Stdio::piped()).stdout(Stdio::piped());
|
|
cmd
|
|
}
|
|
|
|
async fn feed_cat(mut cat: Child, n: usize) -> io::Result<ExitStatus> {
|
|
let mut stdin = cat.stdin().take().unwrap();
|
|
let stdout = cat.stdout().take().unwrap();
|
|
|
|
// Produce n lines on the child's stdout.
|
|
let write = async {
|
|
for i in 0..n {
|
|
let bytes = format!("line {}\n", i).into_bytes();
|
|
stdin.write_all(&bytes).await.unwrap();
|
|
}
|
|
|
|
drop(stdin);
|
|
};
|
|
|
|
let read = async {
|
|
let mut reader = BufReader::new(stdout).lines();
|
|
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 {
|
|
let data = reader
|
|
.next_line()
|
|
.await
|
|
.unwrap_or_else(|_| Some(String::new()))
|
|
.expect("failed to read line");
|
|
|
|
let num_read = data.len();
|
|
let done = num_lines >= n;
|
|
|
|
match (done, num_read) {
|
|
(false, 0) => panic!("broken pipe"),
|
|
(true, n) if n != 0 => panic!("extraneous data"),
|
|
_ => {
|
|
let expected = format!("line {}", num_lines);
|
|
assert_eq!(expected, data);
|
|
}
|
|
};
|
|
|
|
num_lines += 1;
|
|
if num_lines >= n {
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
|
|
// Compose reading and writing concurrently.
|
|
future::join3(write, read, cat)
|
|
.map(|(_, _, status)| status)
|
|
.await
|
|
}
|
|
|
|
/// 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.
|
|
///
|
|
/// - The child does produce EOF on stdout after the last line.
|
|
#[tokio::test]
|
|
async fn feed_a_lot() {
|
|
let child = cat().spawn().unwrap();
|
|
let status = feed_cat(child, 10000).await.unwrap();
|
|
assert_eq!(status.code(), Some(0));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn wait_with_output_captures() {
|
|
let mut child = cat().spawn().unwrap();
|
|
let mut stdin = child.stdin().take().unwrap();
|
|
|
|
let write_bytes = b"1234";
|
|
|
|
let future = async {
|
|
stdin.write_all(write_bytes).await?;
|
|
drop(stdin);
|
|
let out = child.wait_with_output();
|
|
out.await
|
|
};
|
|
|
|
let output = future.await.unwrap();
|
|
|
|
assert!(output.status.success());
|
|
assert_eq!(output.stdout, write_bytes);
|
|
assert_eq!(output.stderr.len(), 0);
|
|
}
|
|
|
|
#[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();
|
|
|
|
assert_ok!(child.await);
|
|
}
|