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