1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#![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;
async fn 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();
let (c1, c2) = tokio::join!(connector, acceptor);
(c1.unwrap(), c2.unwrap().0)
}
async fn block_write(s: &mut TcpStream) -> usize {
static BUF: [u8; 2048] = [0; 2048];
let mut copied = 0;
loop {
tokio::select! {
result = s.write(&BUF) => {
copied += result.expect("write error")
},
_ = tokio::time::sleep(Duration::from_millis(10)) => {
break;
}
}
}
copied
}
async fn 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(async move { 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(async move { copy_bidirectional(&mut b1, &mut a1).await });
cb(handle, b, a).await;
}
#[tokio::test]
#[cfg_attr(miri, ignore)] // No `socket` in miri.
async fn test_basic_transfer() {
symmetric(|_handle, mut a, mut b| async move {
a.write_all(b"test").await.unwrap();
let mut tmp = [0; 4];
b.read_exact(&mut tmp).await.unwrap();
assert_eq!(&tmp[..], b"test");
})
.await
}
#[tokio::test]
#[cfg_attr(miri, ignore)] // No `socket` in miri.
async fn test_transfer_after_close() {
symmetric(|handle, mut a, mut b| async move {
AsyncWriteExt::shutdown(&mut a).await.unwrap();
b.read_to_end(&mut Vec::new()).await.unwrap();
b.write_all(b"quux").await.unwrap();
let mut tmp = [0; 4];
a.read_exact(&mut tmp).await.unwrap();
assert_eq!(&tmp[..], b"quux");
// Once both are closed, we should have our handle back
drop(b);
assert_eq!(handle.await.unwrap().unwrap(), (0, 4));
})
.await
}
#[tokio::test]
#[cfg_attr(miri, ignore)] // No `socket` in miri.
async fn blocking_one_side_does_not_block_other() {
symmetric(|handle, mut a, mut b| async move {
block_write(&mut a).await;
b.write_all(b"quux").await.unwrap();
let mut tmp = [0; 4];
a.read_exact(&mut tmp).await.unwrap();
assert_eq!(&tmp[..], b"quux");
AsyncWriteExt::shutdown(&mut a).await.unwrap();
let mut buf = Vec::new();
b.read_to_end(&mut buf).await.unwrap();
drop(b);
assert_eq!(handle.await.unwrap().unwrap(), (buf.len() as u64, 4));
})
.await
}
#[tokio::test]
async fn immediate_exit_on_write_error() {
let payload = b"here, take this";
let error = || io::Error::new(io::ErrorKind::Other, "no thanks!");
let mut a = tokio_test::io::Builder::new()
.read(payload)
.write_error(error())
.build();
let mut b = tokio_test::io::Builder::new()
.read(payload)
.write_error(error())
.build();
assert!(copy_bidirectional(&mut a, &mut b).await.is_err());
}
#[tokio::test]
async fn immediate_exit_on_read_error() {
let error = || io::Error::new(io::ErrorKind::Other, "got nothing!");
let mut a = tokio_test::io::Builder::new().read_error(error()).build();
let mut b = tokio_test::io::Builder::new().read_error(error()).build();
assert!(copy_bidirectional(&mut a, &mut b).await.is_err());
}
#[tokio::test]
async fn copy_bidirectional_is_cooperative() {
tokio::select! {
biased;
_ = async {
loop {
let payload = b"here, take this";
let mut a = tokio_test::io::Builder::new()
.read(payload)
.write(payload)
.build();
let mut b = tokio_test::io::Builder::new()
.read(payload)
.write(payload)
.build();
let _ = copy_bidirectional(&mut a, &mut b).await;
}
} => {},
_ = tokio::task::yield_now() => {}
}
}
|