Remove the Reactor::run method (#58)

This commit removes the `Reactor::run` method which has previously been used to
execute futures and turn the reactor at the same time. The tests/examples made
heavy usage of this method but they have now all temporarily moved to `wait()`
until the futures dependency is upgraded. In the meantime this'll allow us to
further trim down the `Reactor` APIs to their final state.
This commit is contained in:
Alex Crichton
2017-12-11 21:29:18 -06:00
committed by Carl Lerche
parent 32f2750c2d
commit a577bfc033
23 changed files with 133 additions and 247 deletions
+4 -4
View File
@@ -11,7 +11,7 @@ use futures::Future;
use futures::stream::Stream;
use tokio_io::io::copy;
use tokio::net::TcpListener;
use tokio::reactor::Reactor;
use tokio::reactor::Handle;
macro_rules! t {
($e:expr) => (match $e {
@@ -25,8 +25,8 @@ fn echo_server() {
const N: usize = 1024;
drop(env_logger::init());
let mut l = t!(Reactor::new());
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &l.handle()));
let handle = Handle::default();
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &handle));
let addr = t!(srv.local_addr());
let msg = "foo bar baz";
@@ -56,7 +56,7 @@ fn echo_server() {
copy(a, b)
});
let (amt, _, _) = t!(l.run(copied));
let (amt, _, _) = t!(copied.wait());
let (expected, t2) = t.join().unwrap();
let actual = t2.join().unwrap();
+4 -4
View File
@@ -10,7 +10,7 @@ use futures::Future;
use futures::stream::Stream;
use tokio_io::io::read_to_end;
use tokio::net::TcpListener;
use tokio::reactor::Reactor;
use tokio::reactor::Handle;
macro_rules! t {
($e:expr) => (match $e {
@@ -21,8 +21,8 @@ macro_rules! t {
#[test]
fn chain_clients() {
let mut l = t!(Reactor::new());
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &l.handle()));
let handle = Handle::default();
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &handle));
let addr = t!(srv.local_addr());
let t = thread::spawn(move || {
@@ -44,7 +44,7 @@ fn chain_clients() {
read_to_end(a.chain(b).chain(c), Vec::new())
});
let (_, data) = t!(l.run(copied));
let (_, data) = t!(copied.wait());
t.join().unwrap();
assert_eq!(data, b"foo bar baz");
+4 -4
View File
@@ -10,7 +10,7 @@ use std::thread;
use futures::Future;
use futures::stream::Stream;
use tokio::net::TcpListener;
use tokio::reactor::Reactor;
use tokio::reactor::Handle;
use tokio_io::AsyncRead;
use tokio_io::io::copy;
@@ -25,8 +25,8 @@ macro_rules! t {
fn echo_server() {
drop(env_logger::init());
let mut l = t!(Reactor::new());
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &l.handle()));
let handle = Handle::default();
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &handle));
let addr = t!(srv.local_addr());
let msg = "foo bar baz";
@@ -46,7 +46,7 @@ fn echo_server() {
let halves = client.map(|s| s.0.split());
let copied = halves.and_then(|(a, b)| copy(a, b));
let (amt, _, _) = t!(l.run(copied));
let (amt, _, _) = t!(copied.wait());
t.join().unwrap();
assert_eq!(amt, msg.len() as u64 * 1024);
+4 -4
View File
@@ -10,7 +10,7 @@ use futures::Future;
use futures::stream::Stream;
use tokio_io::io::read_to_end;
use tokio::net::TcpListener;
use tokio::reactor::Reactor;
use tokio::reactor::Handle;
macro_rules! t {
($e:expr) => (match $e {
@@ -21,8 +21,8 @@ macro_rules! t {
#[test]
fn limit() {
let mut l = t!(Reactor::new());
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &l.handle()));
let handle = Handle::default();
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &handle));
let addr = t!(srv.local_addr());
let t = thread::spawn(move || {
@@ -38,7 +38,7 @@ fn limit() {
read_to_end(a.take(4), Vec::new())
});
let (_, data) = t!(l.run(copied));
let (_, data) = t!(copied.wait());
t.join().unwrap();
assert_eq!(data, b"foo ");
+9 -12
View File
@@ -13,7 +13,7 @@ use futures::{Future, Stream, Sink};
use futures::future::Executor;
use futures_cpupool::CpuPool;
use tokio::net::{TcpListener, TcpStream};
use tokio::reactor::Reactor;
use tokio::reactor::Handle;
use tokio_io::codec::{Encoder, Decoder};
use tokio_io::io::{write_all, read};
use tokio_io::AsyncRead;
@@ -55,10 +55,8 @@ impl Encoder for LineCodec {
fn echo() {
drop(env_logger::init());
let mut core = Reactor::new().unwrap();
let handle = core.handle();
let pool = CpuPool::new(1);
let handle = Handle::default();
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &handle).unwrap();
let addr = listener.local_addr().unwrap();
@@ -69,24 +67,23 @@ fn echo() {
Ok(())
});
let handle = core.handle();
pool.execute(srv.map_err(|e| panic!("srv error: {}", e))).unwrap();
let client = TcpStream::connect(&addr, &handle);
let client = core.run(client).unwrap();
let (client, _) = core.run(write_all(client, b"a\n")).unwrap();
let (client, buf, amt) = core.run(read(client, vec![0; 1024])).unwrap();
let client = client.wait().unwrap();
let (client, _) = write_all(client, b"a\n").wait().unwrap();
let (client, buf, amt) = read(client, vec![0; 1024]).wait().unwrap();
assert_eq!(amt, 2);
assert_eq!(&buf[..2], b"a\n");
let (client, _) = core.run(write_all(client, b"\n")).unwrap();
let (client, buf, amt) = core.run(read(client, buf)).unwrap();
let (client, _) = write_all(client, b"\n").wait().unwrap();
let (client, buf, amt) = read(client, buf).wait().unwrap();
assert_eq!(amt, 1);
assert_eq!(&buf[..1], b"\n");
let (client, _) = core.run(write_all(client, b"b")).unwrap();
let (client, _) = write_all(client, b"b").wait().unwrap();
client.shutdown(Shutdown::Write).unwrap();
let (_client, buf, amt) = core.run(read(client, buf)).unwrap();
let (_client, buf, amt) = read(client, buf).wait().unwrap();
assert_eq!(amt, 1);
assert_eq!(&buf[..1], b"b");
}
+6 -5
View File
@@ -13,10 +13,11 @@ use std::os::unix::io::{AsRawFd, FromRawFd};
use std::thread;
use std::time::Duration;
use futures::prelude::*;
use mio::event::Evented;
use mio::unix::{UnixReady, EventedFd};
use mio::{PollOpt, Ready, Token};
use mio::event::Evented;
use tokio::reactor::{Reactor, PollEvented};
use tokio::reactor::{Handle, PollEvented};
use tokio_io::io::read_to_end;
macro_rules! t {
@@ -64,7 +65,7 @@ impl Evented for MyFile {
fn hup() {
drop(env_logger::init());
let mut l = t!(Reactor::new());
let handle = Handle::default();
unsafe {
let mut pipes = [0; 2];
assert!(libc::pipe(pipes.as_mut_ptr()) != -1,
@@ -77,10 +78,10 @@ fn hup() {
thread::sleep(Duration::from_millis(100));
});
let source = PollEvented::new(MyFile::new(read), &l.handle()).unwrap();
let source = PollEvented::new(MyFile::new(read), &handle).unwrap();
let reader = read_to_end(source, Vec::new());
let (_, content) = t!(l.run(reader));
let (_, content) = t!(reader.wait());
assert_eq!(&b"Hello!\nGood bye!\n"[..], &content[..]);
t.join().unwrap();
}
+4 -4
View File
@@ -12,7 +12,7 @@ use futures::stream::Stream;
use tokio_io::io::copy;
use tokio_io::AsyncRead;
use tokio::net::TcpListener;
use tokio::reactor::Reactor;
use tokio::reactor::Handle;
macro_rules! t {
($e:expr) => (match $e {
@@ -25,8 +25,8 @@ macro_rules! t {
fn echo_server() {
drop(env_logger::init());
let mut l = t!(Reactor::new());
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &l.handle()));
let handle = Handle::default();
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &handle));
let addr = t!(srv.local_addr());
let t = thread::spawn(move || {
@@ -50,7 +50,7 @@ fn echo_server() {
.take(2)
.collect();
t!(l.run(future));
t!(future.wait());
t.join().unwrap();
}
+10 -10
View File
@@ -8,7 +8,7 @@ use std::thread;
use futures::Future;
use futures::stream::Stream;
use tokio::reactor::Reactor;
use tokio::reactor::Handle;
use tokio::net::{TcpListener, TcpStream};
macro_rules! t {
@@ -21,15 +21,15 @@ macro_rules! t {
#[test]
fn connect() {
drop(env_logger::init());
let mut l = t!(Reactor::new());
let handle = Handle::default();
let srv = t!(net::TcpListener::bind("127.0.0.1:0"));
let addr = t!(srv.local_addr());
let t = thread::spawn(move || {
t!(srv.accept()).0
});
let stream = TcpStream::connect(&addr, &l.handle());
let mine = t!(l.run(stream));
let stream = TcpStream::connect(&addr, &handle);
let mine = t!(stream.wait());
let theirs = t.join().unwrap();
assert_eq!(t!(mine.local_addr()), t!(theirs.peer_addr()));
@@ -39,8 +39,8 @@ fn connect() {
#[test]
fn accept() {
drop(env_logger::init());
let mut l = t!(Reactor::new());
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &l.handle()));
let handle = Handle::default();
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &handle));
let addr = t!(srv.local_addr());
let (tx, rx) = channel();
@@ -53,7 +53,7 @@ fn accept() {
net::TcpStream::connect(&addr).unwrap()
});
let (mine, _remaining) = t!(l.run(client));
let (mine, _remaining) = t!(client.wait());
let mine = mine.unwrap();
let theirs = t.join().unwrap();
@@ -64,8 +64,8 @@ fn accept() {
#[test]
fn accept2() {
drop(env_logger::init());
let mut l = t!(Reactor::new());
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &l.handle()));
let handle = Handle::default();
let srv = t!(TcpListener::bind(&t!("127.0.0.1:0".parse()), &handle));
let addr = t!(srv.local_addr());
let t = thread::spawn(move || {
@@ -79,7 +79,7 @@ fn accept2() {
}).into_future().map_err(|e| e.0);
assert!(rx.try_recv().is_err());
let (mine, _remaining) = t!(l.run(client));
let (mine, _remaining) = t!(client.wait());
mine.unwrap();
t.join().unwrap();
}
+16 -16
View File
@@ -8,7 +8,7 @@ use std::net::SocketAddr;
use futures::{Future, Poll, Stream, Sink};
use tokio::net::{UdpSocket, UdpCodec};
use tokio::reactor::Reactor;
use tokio::reactor::Handle;
macro_rules! t {
($e:expr) => (match $e {
@@ -18,16 +18,16 @@ macro_rules! t {
}
fn send_messages<S: SendFn + Clone, R: RecvFn + Clone>(send: S, recv: R) {
let mut l = t!(Reactor::new());
let mut a = t!(UdpSocket::bind(&([127, 0, 0, 1], 0).into(), &l.handle()));
let mut b = t!(UdpSocket::bind(&([127, 0, 0, 1], 0).into(), &l.handle()));
let handle = Handle::default();
let mut a = t!(UdpSocket::bind(&([127, 0, 0, 1], 0).into(), &handle));
let mut b = t!(UdpSocket::bind(&([127, 0, 0, 1], 0).into(), &handle));
let a_addr = t!(a.local_addr());
let b_addr = t!(b.local_addr());
{
let send = SendMessage::new(a, send.clone(), b_addr, b"1234");
let recv = RecvMessage::new(b, recv.clone(), a_addr, b"1234");
let (sendt, received) = t!(l.run(send.join(recv)));
let (sendt, received) = t!(send.join(recv).wait());
a = sendt;
b = received;
}
@@ -35,7 +35,7 @@ fn send_messages<S: SendFn + Clone, R: RecvFn + Clone>(send: S, recv: R) {
{
let send = SendMessage::new(a, send, b_addr, b"");
let recv = RecvMessage::new(b, recv, a_addr, b"");
t!(l.run(send.join(recv)));
t!(send.join(recv).wait());
}
}
@@ -166,16 +166,16 @@ impl<R: RecvFn> Future for RecvMessage<R> {
#[test]
fn send_dgrams() {
let mut l = t!(Reactor::new());
let mut a = t!(UdpSocket::bind(&t!("127.0.0.1:0".parse()), &l.handle()));
let mut b = t!(UdpSocket::bind(&t!("127.0.0.1:0".parse()), &l.handle()));
let handle = Handle::default();
let mut a = t!(UdpSocket::bind(&t!("127.0.0.1:0".parse()), &handle));
let mut b = t!(UdpSocket::bind(&t!("127.0.0.1:0".parse()), &handle));
let mut buf = [0u8; 50];
let b_addr = t!(b.local_addr());
{
let send = a.send_dgram(&b"4321"[..], b_addr);
let recv = b.recv_dgram(&mut buf[..]);
let (sendt, received) = t!(l.run(send.join(recv)));
let (sendt, received) = t!(send.join(recv).wait());
assert_eq!(received.2, 4);
assert_eq!(&received.1[..4], b"4321");
a = sendt.0;
@@ -185,7 +185,7 @@ fn send_dgrams() {
{
let send = a.send_dgram(&b""[..], b_addr);
let recv = b.recv_dgram(&mut buf[..]);
let received = t!(l.run(send.join(recv))).1;
let received = t!(send.join(recv).wait()).1;
assert_eq!(received.2, 0);
}
}
@@ -216,9 +216,9 @@ impl UdpCodec for Codec {
#[test]
fn send_framed() {
let mut l = t!(Reactor::new());
let mut a_soc = t!(UdpSocket::bind(&t!("127.0.0.1:0".parse()), &l.handle()));
let mut b_soc = t!(UdpSocket::bind(&t!("127.0.0.1:0".parse()), &l.handle()));
let handle = Handle::default();
let mut a_soc = t!(UdpSocket::bind(&t!("127.0.0.1:0".parse()), &handle));
let mut b_soc = t!(UdpSocket::bind(&t!("127.0.0.1:0".parse()), &handle));
let a_addr = t!(a_soc.local_addr());
let b_addr = t!(b_soc.local_addr());
@@ -228,7 +228,7 @@ fn send_framed() {
let send = a.send(&b"4567"[..]);
let recv = b.into_future().map_err(|e| e.0);
let (sendt, received) = t!(l.run(send.join(recv)));
let (sendt, received) = t!(send.join(recv).wait());
assert_eq!(received.0, Some(()));
a_soc = sendt.into_inner();
@@ -241,7 +241,7 @@ fn send_framed() {
let send = a.send(&b""[..]);
let recv = b.into_future().map_err(|e| e.0);
let received = t!(l.run(send.join(recv))).1;
let received = t!(send.join(recv).wait()).1;
assert_eq!(received.0, Some(()));
}
}