#![warn(rust_2018_idioms)] #![cfg(all(feature = "full", not(target_os = "wasi")))] // Wasi does not support bind()
use std::time::Duration; use tokio::io::{self, copy_bidirectional, AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpStream; use tokio::task::JoinHandle;
asyncfn make_socketpair() -> (TcpStream, TcpStream) { let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let addr = listener.local_addr().unwrap(); let connector = TcpStream::connect(addr); let acceptor = listener.accept();
asyncfn symmetric<F, Fut>(mut cb: F) where
F: FnMut(JoinHandle<io::Result<(u64, u64)>>, TcpStream, TcpStream) -> Fut,
Fut: std::future::Future<Output = ()>,
{ // We run the test twice, with streams passed to copy_bidirectional in // different orders, in order to ensure that the two arguments are // interchangeable.
let (a, mut a1) = make_socketpair().await; let (b, mut b1) = make_socketpair().await;
let handle = tokio::spawn(asyncmove { copy_bidirectional(&mut a1, &mut b1).await });
cb(handle, a, b).await;
let (a, mut a1) = make_socketpair().await; let (b, mut b1) = make_socketpair().await;
let handle = tokio::spawn(asyncmove { copy_bidirectional(&mut b1, &mut a1).await });
assert_eq!(handle.await.unwrap().unwrap(), (buf.len() as u64, 4));
})
.await
}
#[tokio::test] asyncfn immediate_exit_on_write_error() { let payload = b"here, take this"; let error = || io::Error::new(io::ErrorKind::Other, "no thanks!");
letmut a = tokio_test::io::Builder::new()
.read(payload)
.write_error(error())
.build();
letmut b = tokio_test::io::Builder::new()
.read(payload)
.write_error(error())
.build();
assert!(copy_bidirectional(&mut a, &mut b).await.is_err());
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.