Skip to content

Commit

Permalink
test: process vectored writes
Browse files Browse the repository at this point in the history
  • Loading branch information
koivunej committed Nov 21, 2022
1 parent 9c88def commit 47c6e69
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests-integration/Cargo.toml
Expand Up @@ -58,3 +58,4 @@ tokio = { path = "../tokio" }
tokio-test = { path = "../tokio-test", optional = true }
doc-comment = "0.3.1"
futures = { version = "0.3.0", features = ["async-await"] }
bytes = "1.0.0"
51 changes: 51 additions & 0 deletions tests-integration/tests/process_stdio.rs
Expand Up @@ -190,3 +190,54 @@ async fn pipe_from_one_command_to_another() {
assert!(second_status.expect("second status").success());
assert!(third_status.expect("third status").success());
}

#[tokio::test]
async fn vectored_writes() {
use bytes::{Buf, Bytes};
use std::{io::IoSlice, pin::Pin};
use tokio::io::AsyncWrite;

let mut cat = cat().spawn().unwrap();
let mut stdin = cat.stdin.take().unwrap();
let mut stdout = cat.stdout.take().unwrap();

let write = async {
let mut input = Bytes::from_static(b"hello\n").chain(Bytes::from_static(b"world!\n"));
let mut writes_completed = 0;

futures::future::poll_fn(|cx| loop {
let mut slices = [IoSlice::new(&[]); 2];
let vectored = input.chunks_vectored(&mut slices);
if vectored == 0 {
return std::task::Poll::Ready(std::io::Result::Ok(()));
}
let n = futures::ready!(Pin::new(&mut stdin).poll_write_vectored(cx, &slices))?;
writes_completed += 1;
input.advance(n);
})
.await?;

drop(stdin);

std::io::Result::Ok(writes_completed)
};

let read = async {
let mut buffer = Vec::with_capacity(6 + 7);
stdout.read_to_end(&mut buffer).await?;
std::io::Result::Ok(buffer)
};

let (write, read, status) = future::join3(write, read, cat.wait()).await;

assert!(status.unwrap().success());

let writes_completed = write.unwrap();
// on unix our small payload should always fit in whatever default sized pipe with a single
// syscall. if multiple are used, then the forwarding does not work, or we are on a platform
// for which the `std` does not support vectored writes.
#[cfg(target_family = "unix")]
assert_eq!(writes_completed, 1);

assert_eq!(&read.unwrap(), b"hello\nworld!\n");
}

0 comments on commit 47c6e69

Please sign in to comment.