use super::*;
#[tokio::test(flavor = "current_thread")]
async fn round_trip_frame() {
let stream = MemoryStream::default();
let producer = stream.clone();
let push = tokio::spawn(async move {
tokio::task::yield_now().await;
producer.push_inbound(b"hello".to_vec());
});
let body = stream.read_message().await.unwrap();
assert_eq!(body, b"hello");
push.await.unwrap();
stream.write_all(b"out").await.unwrap();
assert_eq!(stream.captured(), b"out");
stream.hang_up();
let err = stream.read_message().await.unwrap_err();
assert!(
matches!(err, Error::Io(ref e) if e.kind() == std::io::ErrorKind::UnexpectedEof),
"unexpected error: {err:?}"
);
}
#[tokio::test(flavor = "current_thread")]
async fn trait_close_wakes_a_pending_reader() {
let stream = MemoryStream::default();
let reading = stream.clone();
let read = tokio::spawn(async move { reading.read_message().await });
tokio::task::yield_now().await;
stream.close().await;
let result = tokio::time::timeout(Duration::from_secs(1), read)
.await
.unwrap_or_else(|_| panic!("transport close did not wake {stream:?}"))
.unwrap();
assert!(matches!(result, Err(Error::Shutdown)), "got {result:?}");
}
#[tokio::test]
async fn trait_close_is_terminal() {
let stream = MemoryStream::default();
stream.close().await;
stream.reopen();
stream.push_inbound(b"late".to_vec());
assert!(matches!(stream.read_message().await, Err(Error::Shutdown)));
assert!(matches!(stream.write_all(b"late").await, Err(Error::Shutdown)));
assert!(matches!(stream.reconnect().await, Err(Error::Shutdown)));
assert!(stream.captured().is_empty());
}