1 extern crate futures;
2 extern crate tokio_tcp;
3 extern crate tokio_io;
4 
5 use std::net::TcpStream;
6 use std::thread;
7 use std::io::{Write, Read};
8 
9 use futures::Future;
10 use futures::stream::Stream;
11 use tokio_io::io::read_to_end;
12 use tokio_tcp::TcpListener;
13 
14 macro_rules! t {
15     ($e:expr) => (match $e {
16         Ok(e) => e,
17         Err(e) => panic!("{} failed with {:?}", stringify!($e), e),
18     })
19 }
20 
21 #[test]
chain_clients()22 fn chain_clients() {
23     let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse())));
24     let addr = t!(srv.local_addr());
25 
26     let t = thread::spawn(move || {
27         let mut s1 = TcpStream::connect(&addr).unwrap();
28         s1.write_all(b"foo ").unwrap();
29         let mut s2 = TcpStream::connect(&addr).unwrap();
30         s2.write_all(b"bar ").unwrap();
31         let mut s3 = TcpStream::connect(&addr).unwrap();
32         s3.write_all(b"baz").unwrap();
33     });
34 
35     let clients = srv.incoming().take(3);
36     let copied = clients.collect().and_then(|clients| {
37         let mut clients = clients.into_iter();
38         let a = clients.next().unwrap();
39         let b = clients.next().unwrap();
40         let c = clients.next().unwrap();
41 
42         read_to_end(a.chain(b).chain(c), Vec::new())
43     });
44 
45     let (_, data) = t!(copied.wait());
46     t.join().unwrap();
47 
48     assert_eq!(data, b"foo bar baz");
49 }
50