extern crate futures; extern crate futures_mio; use std::net::{TcpListener, TcpStream}; use std::sync::mpsc::channel; use std::thread; use futures::Future; use futures::stream::Stream; macro_rules! t { ($e:expr) => (match $e { Ok(e) => e, Err(e) => panic!("{} failed with {:?}", stringify!($e), e), }) } #[test] fn connect() { let mut l = t!(futures_mio::Loop::new()); let srv = t!(TcpListener::bind("127.0.0.1:0")); let addr = t!(srv.local_addr()); let t = thread::spawn(move || { t!(srv.accept()).0 }); let stream = l.handle().tcp_connect(&addr); let mine = t!(l.run(stream)); let theirs = t.join().unwrap(); assert_eq!(t!(mine.local_addr()), t!(theirs.peer_addr())); assert_eq!(t!(theirs.local_addr()), t!(mine.peer_addr())); } #[test] fn accept() { let mut l = t!(futures_mio::Loop::new()); let srv = l.handle().tcp_listen(&"127.0.0.1:0".parse().unwrap()); let srv = t!(l.run(srv)); let addr = t!(srv.local_addr()); let (tx, rx) = channel(); let client = srv.incoming().map(move |t| { tx.send(()).unwrap(); t.0 }).into_future().map_err(|e| e.0); assert!(rx.try_recv().is_err()); let t = thread::spawn(move || { TcpStream::connect(&addr).unwrap() }); let (mine, _remaining) = t!(l.run(client)); let mine = mine.unwrap(); let theirs = t.join().unwrap(); assert_eq!(t!(mine.local_addr()), t!(theirs.peer_addr())); assert_eq!(t!(theirs.local_addr()), t!(mine.peer_addr())); } #[test] fn accept2() { let mut l = t!(futures_mio::Loop::new()); let srv = l.handle().tcp_listen(&"127.0.0.1:0".parse().unwrap()); let srv = t!(l.run(srv)); let addr = t!(srv.local_addr()); let t = thread::spawn(move || { TcpStream::connect(&addr).unwrap() }); let (tx, rx) = channel(); let client = srv.incoming().map(move |t| { tx.send(()).unwrap(); t.0 }).into_future().map_err(|e| e.0); assert!(rx.try_recv().is_err()); let (mine, _remaining) = t!(l.run(client)); mine.unwrap(); t.join().unwrap(); }