use super::Context;
use core::time::Duration;
use std::io::ErrorKind;
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
time::timeout,
};
#[tokio::test]
async fn zero_write_test() {
let context = Context::new().await;
let (mut client, server) = context.pair().await;
client.write_all(&[]).await.unwrap();
drop(server);
}
#[tokio::test]
async fn zero_write_reset_test() {
let context = Context::new().await;
let (mut client, server) = context.pair().await;
drop(server);
tokio::time::sleep(Duration::from_millis(1)).await;
client.write_all(&[]).await.unwrap();
}
#[tokio::test]
async fn unresponsive_shutdown_test() {
let context = Context::new().await;
let (mut client, server) = context.pair().await;
client.write_all(b"hello!").await.unwrap();
client.shutdown().await.unwrap();
drop(server);
}
#[tokio::test]
async fn write_after_shutdown_test() {
let context = Context::new().await;
let (mut client, server) = context.pair().await;
client.shutdown().await.unwrap();
let err = client.write_all(b"hello!").await.unwrap_err();
assert_eq!(err.kind(), ErrorKind::BrokenPipe);
drop(server);
}
#[tokio::test]
async fn empty_write_after_shutdown_test() {
let context = Context::new().await;
let (mut client, server) = context.pair().await;
client.shutdown().await.unwrap();
client.write_all(&[]).await.unwrap();
drop(server);
}
#[cfg(not(target_os = "macos"))]
#[tokio::test]
async fn multiple_shutdown_test() {
let context = Context::new().await;
let (mut client, server) = context.pair().await;
client.shutdown().await.unwrap();
drop(server);
}
#[tokio::test]
async fn addr_after_half_close_test() {
let context = Context::new().await;
let (mut client, mut server) = context.pair().await;
client.shutdown().await.unwrap();
let _ = server.read(&mut []).await;
client.local_addr().unwrap();
client.peer_addr().unwrap();
server.local_addr().unwrap();
server.peer_addr().unwrap();
}
#[tokio::test]
async fn addr_after_full_shutdown_test() {
let context = Context::new().await;
let (mut client, mut server) = context.pair().await;
if context.protocol().is_udp() {
let _ = timeout(Duration::from_millis(5), server.read(&mut [])).await;
let _ = timeout(Duration::from_millis(5), client.read(&mut [])).await;
}
client.shutdown().await.unwrap();
server.shutdown().await.unwrap();
let _ = client.read(&mut []).await;
let _ = server.read(&mut []).await;
let expected_err = if cfg!(target_os = "macos") {
ErrorKind::InvalidInput
} else {
ErrorKind::NotConnected
};
client.local_addr().unwrap();
let err = client.peer_addr().unwrap_err();
assert_eq!(err.kind(), expected_err);
if !context.protocol().is_udp() {
server.local_addr().unwrap();
let err = server.peer_addr().unwrap_err();
assert_eq!(err.kind(), expected_err);
}
}
#[tokio::test]
async fn half_close_read_test() {
let context = Context::new().await;
let (mut client, mut server) = context.pair().await;
client.shutdown().await.unwrap();
let mut buffer = vec![];
server.read_to_end(&mut buffer).await.unwrap();
assert!(buffer.is_empty());
server.write_all(b"hello!").await.unwrap();
buffer.resize(10, 0);
let len = client.read(&mut buffer).await.unwrap();
assert_eq!(&buffer[..len], b"hello!");
server.shutdown().await.unwrap();
let len = client.read(&mut buffer).await.unwrap();
assert_eq!(len, 0);
}