Files
tokio/tokio-tcp/tests/echo.rs
T

52 lines
1.3 KiB
Rust
Raw Normal View History

2019-05-14 10:27:36 -07:00
#![deny(warnings, rust_2018_idioms)]
#![cfg(feature = "broken")]
2016-07-30 17:53:12 -07:00
2019-05-14 10:27:36 -07:00
use env_logger;
use futures::stream::Stream;
use futures::Future;
2016-08-17 11:23:32 -07:00
use std::io::{Read, Write};
use std::net::TcpStream;
2016-07-30 17:53:12 -07:00
use std::thread;
2017-02-05 17:06:57 -08:00
use tokio_io::io::copy;
2019-02-21 11:56:15 -08:00
use tokio_io::AsyncRead;
use tokio_tcp::TcpListener;
2016-07-30 17:53:12 -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),
}
};
2016-07-30 17:53:12 -07:00
}
#[test]
fn echo_server() {
2018-08-10 21:37:45 +02:00
drop(env_logger::try_init());
2016-08-16 15:56:57 -07:00
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse())));
2016-07-30 17:53:12 -07:00
let addr = t!(srv.local_addr());
let msg = "foo bar baz";
let t = thread::spawn(move || {
let mut s = TcpStream::connect(&addr).unwrap();
for _i in 0..1024 {
assert_eq!(t!(s.write(msg.as_bytes())), msg.len());
let mut buf = [0; 1024];
assert_eq!(t!(s.read(&mut buf)), msg.len());
assert_eq!(&buf[..msg.len()], msg.as_bytes());
}
});
let clients = srv.incoming();
let client = clients.into_future().map(|e| e.0.unwrap()).map_err(|e| e.0);
let halves = client.map(|s| s.split());
2016-07-30 17:53:12 -07:00
let copied = halves.and_then(|(a, b)| copy(a, b));
let (amt, _, _) = t!(copied.wait());
2016-07-30 17:53:12 -07:00
t.join().unwrap();
assert_eq!(amt, msg.len() as u64 * 1024);
}