2017-02-05 17:06:57 -08:00
|
|
|
extern crate env_logger;
|
2016-08-15 16:22:09 -07:00
|
|
|
extern crate futures;
|
2017-10-24 16:30:16 -07:00
|
|
|
extern crate tokio;
|
2017-02-05 17:06:57 -08:00
|
|
|
extern crate tokio_io;
|
2016-08-15 16:22:09 -07:00
|
|
|
|
2016-08-17 11:23:32 -07:00
|
|
|
use std::io::{Read, Write};
|
|
|
|
|
use std::net::TcpStream;
|
2016-08-15 16:22:09 -07:00
|
|
|
use std::thread;
|
|
|
|
|
|
|
|
|
|
use futures::Future;
|
|
|
|
|
use futures::stream::Stream;
|
2017-02-05 17:06:57 -08:00
|
|
|
use tokio_io::io::copy;
|
|
|
|
|
use tokio_io::AsyncRead;
|
2017-10-24 16:30:16 -07:00
|
|
|
use tokio::net::TcpListener;
|
2016-08-15 16:22:09 -07:00
|
|
|
|
|
|
|
|
macro_rules! t {
|
|
|
|
|
($e:expr) => (match $e {
|
|
|
|
|
Ok(e) => e,
|
|
|
|
|
Err(e) => panic!("{} failed with {:?}", stringify!($e), e),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn echo_server() {
|
|
|
|
|
drop(env_logger::init());
|
|
|
|
|
|
2017-12-12 18:32:50 -06:00
|
|
|
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse())));
|
2016-08-15 16:22:09 -07:00
|
|
|
let addr = t!(srv.local_addr());
|
|
|
|
|
|
|
|
|
|
let t = thread::spawn(move || {
|
|
|
|
|
let mut s1 = t!(TcpStream::connect(&addr));
|
|
|
|
|
let mut s2 = t!(TcpStream::connect(&addr));
|
|
|
|
|
|
|
|
|
|
let msg = b"foo";
|
|
|
|
|
assert_eq!(t!(s1.write(msg)), msg.len());
|
|
|
|
|
assert_eq!(t!(s2.write(msg)), msg.len());
|
|
|
|
|
let mut buf = [0; 1024];
|
|
|
|
|
assert_eq!(t!(s1.read(&mut buf)), msg.len());
|
|
|
|
|
assert_eq!(&buf[..msg.len()], msg);
|
|
|
|
|
assert_eq!(t!(s2.read(&mut buf)), msg.len());
|
|
|
|
|
assert_eq!(&buf[..msg.len()], msg);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let future = srv.incoming()
|
2018-01-30 13:01:34 -08:00
|
|
|
.map(|s| s.split())
|
2016-08-17 09:29:05 -07:00
|
|
|
.map(|(a, b)| copy(a, b).map(|_| ()))
|
2016-08-15 16:22:09 -07:00
|
|
|
.buffered(10)
|
|
|
|
|
.take(2)
|
|
|
|
|
.collect();
|
|
|
|
|
|
2018-02-06 07:26:21 -08:00
|
|
|
t!(future.wait());
|
2016-08-15 16:22:09 -07:00
|
|
|
|
|
|
|
|
t.join().unwrap();
|
|
|
|
|
}
|