1#![warn(rust_2018_idioms)]
2#![cfg(feature = "full")]
3#![cfg(unix)]
4
5use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
6use tokio::net::UnixStream;
7
8/// Checks that `UnixStream` can be split into a read half and a write half using
9/// `UnixStream::split` and `UnixStream::split_mut`.
10///
11/// Verifies that the implementation of `AsyncWrite::poll_shutdown` shutdowns the stream for
12/// writing by reading to the end of stream on the other side of the connection.
13#[tokio::test]
14async fn split() -> std::io::Result<()> {
15 let (mut a, mut b) = UnixStream::pair()?;
16
17 let (mut a_read, mut a_write) = a.split();
18 let (mut b_read, mut b_write) = b.split();
19
20 let (a_response, b_response) = futures::future::try_join(
21 send_recv_all(&mut a_read, &mut a_write, b"A"),
22 send_recv_all(&mut b_read, &mut b_write, b"B"),
23 )
24 .await?;
25
26 assert_eq!(a_response, b"B");
27 assert_eq!(b_response, b"A");
28
29 Ok(())
30}
31
32async fn send_recv_all(
33 read: &mut (dyn AsyncRead + Unpin),
34 write: &mut (dyn AsyncWrite + Unpin),
35 input: &[u8],
36) -> std::io::Result<Vec<u8>> {
37 write.write_all(input).await?;
38 write.shutdown().await?;
39
40 let mut output = Vec::new();
41 read.read_to_end(&mut output).await?;
42 Ok(output)
43}
44