Files
tokio/tokio-uds/tests/stream.rs
T

33 lines
1006 B
Rust
Raw Normal View History

2018-05-14 14:48:32 -07:00
#![cfg(unix)]
2019-07-09 05:58:40 +08:00
#![feature(async_await)]
2019-05-14 10:27:36 -07:00
#![deny(warnings, rust_2018_idioms)]
2018-05-14 14:48:32 -07:00
2019-07-09 05:58:40 +08:00
use futures::future::try_join;
2018-08-10 21:37:45 +02:00
use tempfile::Builder;
2019-07-09 05:58:40 +08:00
use tokio::io::{AsyncReadExt, AsyncWriteExt};
2019-05-14 10:27:36 -07:00
use tokio_uds::*;
2018-05-14 14:48:32 -07:00
2019-07-09 05:58:40 +08:00
#[tokio::test]
async fn accept_read_write() -> std::io::Result<()> {
2018-08-10 21:37:45 +02:00
let dir = Builder::new().prefix("tokio-uds-tests").tempdir().unwrap();
2018-05-14 14:48:32 -07:00
let sock_path = dir.path().join("connect.sock");
2019-07-09 05:58:40 +08:00
let mut listener = UnixListener::bind(&sock_path)?;
let accept = listener.accept();
let connect = UnixStream::connect(&sock_path);
let ((mut server, _), mut client) = try_join(accept, connect).await?;
// Write to the client. TODO: Switch to write_all.
let write_len = client.write(b"hello").await?;
assert_eq!(write_len, 5);
drop(client);
// Read from the server. TODO: Switch to read_to_end.
let mut buf = [0u8; 5];
server.read_exact(&mut buf).await?;
assert_eq!(&buf, b"hello");
let len = server.read(&mut buf).await?;
assert_eq!(len, 0);
Ok(())
2018-05-14 14:48:32 -07:00
}