Files
tokio/tests/stream-buffered.rs
T
Alex Crichton 6c045d31ac Reorganize the entire crate:
Renamed APIs

* Loop => reactor::Core
* LoopHandle => reactor::Handle
* LoopPin => reactor::Pinned
* TcpStream => net::TcpStream
* TcpListener => net::TcpListener
* UdpSocket => net::UdpSocket
* Sender => channel::Sender
* Receiver => channel::Receiver
* Timeout => reactor::Timeout
* ReadinessStream => reactor::PollEvented
* All `LoopHandle` methods to construct objects are now free functions on the
  associated types, e.g. `LoopHandle::tcp_listen` is now `TcpListener::bind`
* All APIs taking a `Handle` now take a `Handle` as the last argument
* All future-returning APIs now return concrete types instead of trait objects

Added APIs

* io::Io trait -- Read + Write + ability to poll

Removed without replacement:

* AddSource
* AddTimeout
* IoToken
* TimeoutToken

Closes #3
Closes #6
2016-09-07 22:12:14 -07:00

56 lines
1.4 KiB
Rust

extern crate futures;
extern crate tokio_core;
extern crate env_logger;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::thread;
use futures::Future;
use futures::stream::Stream;
use tokio_core::io::{copy, TaskIo};
use tokio_core::net::TcpListener;
use tokio_core::reactor::Core;
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());
let mut l = t!(Core::new());
let srv = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &l.handle());
let srv = t!(l.run(srv));
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()
.map(|s| TaskIo::new(s.0).split())
.map(|(a, b)| copy(a, b).map(|_| ()))
.buffered(10)
.take(2)
.collect();
t!(l.run(future));
t.join().unwrap();
}