2016-08-15 16:22:09 -07:00
|
|
|
extern crate futures;
|
2016-08-26 14:30:46 -07:00
|
|
|
extern crate tokio_core;
|
2016-08-15 16:22:09 -07:00
|
|
|
extern crate env_logger;
|
|
|
|
|
|
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;
|
2016-09-07 13:53:18 -07:00
|
|
|
use tokio_core::io::{Io, copy};
|
2016-09-02 11:07:52 -07:00
|
|
|
use tokio_core::net::TcpListener;
|
|
|
|
|
use tokio_core::reactor::Core;
|
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());
|
|
|
|
|
|
2016-09-02 11:07:52 -07:00
|
|
|
let mut l = t!(Core::new());
|
2016-09-07 16:11:19 -07:00
|
|
|
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &l.handle()));
|
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()
|
2016-09-07 13:53:18 -07:00
|
|
|
.map(|s| s.0.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();
|
|
|
|
|
|
|
|
|
|
t!(l.run(future));
|
|
|
|
|
|
|
|
|
|
t.join().unwrap();
|
|
|
|
|
}
|