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

52 lines
1.2 KiB
Rust
Raw Normal View History

2018-05-14 14:48:32 -07:00
#![cfg(unix)]
2019-05-14 10:27:36 -07:00
#![deny(warnings, rust_2018_idioms)]
2018-05-14 14:48:32 -07:00
use futures::sync::oneshot;
2019-02-21 11:56:15 -08:00
use futures::{Future, Stream};
2018-08-10 21:37:45 +02:00
use tempfile::Builder;
2019-05-14 10:27:36 -07:00
use tokio::io;
use tokio::runtime::current_thread::Runtime;
use tokio_uds::*;
2018-05-14 14:48:32 -07:00
macro_rules! t {
2019-02-21 11:56:15 -08:00
($e:expr) => {
match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {:?}", stringify!($e), e),
}
};
2018-05-14 14:48:32 -07:00
}
#[test]
fn echo() {
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");
let mut rt = Runtime::new().unwrap();
let server = t!(UnixListener::bind(&sock_path));
let (tx, rx) = oneshot::channel();
rt.spawn({
2019-02-21 11:56:15 -08:00
server
.incoming()
2018-05-14 14:48:32 -07:00
.into_future()
.and_then(move |(sock, _)| {
tx.send(sock.unwrap()).unwrap();
Ok(())
})
.map_err(|e| panic!("err={:?}", e))
});
let client = rt.block_on(UnixStream::connect(&sock_path)).unwrap();
let server = rt.block_on(rx).unwrap();
// Write to the client
rt.block_on(io::write_all(client, b"hello")).unwrap();
// Read from the server
let (_, buf) = rt.block_on(io::read_to_end(server, vec![])).unwrap();
assert_eq!(buf, b"hello");
}