refactor(lib): convert to futures 0.2.0-beta (#1470)

This commit is contained in:
Sam Rijs
2018-03-30 07:32:44 +11:00
committed by Sean McArthur
parent 5db85316a1
commit a12f7beed9
34 changed files with 1366 additions and 1347 deletions

View File

@@ -5,7 +5,6 @@ extern crate futures;
extern crate futures_timer;
extern crate net2;
extern crate tokio;
extern crate tokio_io;
extern crate pretty_env_logger;
use std::io::{self, Read, Write};
@@ -15,8 +14,9 @@ use std::time::Duration;
use hyper::{Body, Client, Method, Request, StatusCode};
use futures::{Future, Stream};
use futures::sync::oneshot;
use futures::{FutureExt, StreamExt};
use futures::channel::oneshot;
use futures::executor::block_on;
use tokio::reactor::Handle;
use tokio::runtime::Runtime;
use tokio::net::{ConnectFuture, TcpStream};
@@ -111,11 +111,7 @@ macro_rules! test {
assert_eq!(res.headers()[$response_header_name], $response_header_val);
)*
let body = res
.into_body()
.into_stream()
.concat2()
.wait()
let body = block_on(res.into_body().into_stream().concat())
.expect("body concat wait");
let expected_res_body = Option::<&[u8]>::from($response_body)
@@ -186,7 +182,7 @@ macro_rules! test {
if !$set_host {
config = config.set_host(false);
}
let client = config.build_with_executor(&runtime.handle(), runtime.executor());
let client = config.build(&runtime.handle());
let body = if let Some(body) = $request_body {
let body: &'static str = body;
@@ -203,7 +199,7 @@ macro_rules! test {
.body(body)
.expect("request builder");
let res = client.request(req);
let res = client.request(req).with_executor(runtime.executor());
let (tx, rx) = oneshot::channel();
@@ -230,7 +226,7 @@ macro_rules! test {
let rx = rx.map_err(|_| hyper::Error::Io(io::Error::new(io::ErrorKind::Other, "thread panicked")));
res.join(rx).map(|r| r.0).wait()
block_on(res.join(rx).map(|r| r.0))
});
}
@@ -642,12 +638,15 @@ mod dispatch_impl {
use std::thread;
use std::time::Duration;
use futures::{self, Future};
use futures::sync::{mpsc, oneshot};
use futures::{self, Future, FutureExt, Stream, StreamExt};
use futures::channel::{mpsc, oneshot};
use futures::io::{AsyncRead, AsyncWrite};
use futures::future::lazy;
use futures::executor::block_on;
use futures::task;
use futures_timer::Delay;
use tokio::net::TcpStream;
use tokio::runtime::Runtime;
use tokio_io::{AsyncRead, AsyncWrite};
use hyper::client::connect::{Connect, Connected, Destination, HttpConnector};
use hyper::Client;
@@ -666,7 +665,7 @@ mod dispatch_impl {
let (closes_tx, closes) = mpsc::channel(10);
let client = Client::configure()
.connector(DebugConnector::with_http_and_closes(HttpConnector::new(1, &runtime.handle()), closes_tx))
.executor(runtime.executor());
.build();
let (tx1, rx1) = oneshot::channel();
@@ -686,15 +685,15 @@ mod dispatch_impl {
.uri(&*format!("http://{}/a", addr))
.body(Body::empty())
.unwrap();
let res = client.request(req).and_then(move |res| {
let res = client.request(req).with_executor(runtime.executor()).and_then(move |res| {
assert_eq!(res.status(), hyper::StatusCode::OK);
Delay::new(Duration::from_secs(1))
.from_err()
.err_into()
});
let rx = rx1.map_err(|_| hyper::Error::Io(io::Error::new(io::ErrorKind::Other, "thread panicked")));
res.join(rx).map(|r| r.0).wait().unwrap();
block_on(res.join(rx).map(|r| r.0)).unwrap();
closes.into_future().wait().unwrap().0.expect("closes");
block_on(closes.next()).unwrap().0.expect("closes");
}
#[test]
@@ -725,25 +724,25 @@ mod dispatch_impl {
let res = {
let client = Client::configure()
.connector(DebugConnector::with_http_and_closes(HttpConnector::new(1, &handle), closes_tx))
.executor(runtime.executor());
.build();
let req = Request::builder()
.uri(&*format!("http://{}/a", addr))
.body(Body::empty())
.unwrap();
client.request(req).and_then(move |res| {
client.request(req).with_executor(runtime.executor()).and_then(move |res| {
assert_eq!(res.status(), hyper::StatusCode::OK);
res.into_body().into_stream().concat2()
res.into_body().into_stream().concat()
}).and_then(|_| {
Delay::new(Duration::from_secs(1))
.from_err()
.err_into()
})
};
// client is dropped
let rx = rx1.map_err(|_| hyper::Error::Io(io::Error::new(io::ErrorKind::Other, "thread panicked")));
res.join(rx).map(|r| r.0).wait().unwrap();
block_on(res.join(rx).map(|r| r.0)).unwrap();
closes.into_future().wait().unwrap().0.expect("closes");
block_on(closes.next()).unwrap().0.expect("closes");
}
@@ -773,41 +772,41 @@ mod dispatch_impl {
// prevent this thread from closing until end of test, so the connection
// stays open and idle until Client is dropped
let _ = client_drop_rx.wait();
let _ = block_on(client_drop_rx);
});
let client = Client::configure()
.connector(DebugConnector::with_http_and_closes(HttpConnector::new(1, &handle), closes_tx))
.executor(runtime.executor());
.build();
let req = Request::builder()
.uri(&*format!("http://{}/a", addr))
.body(Body::empty())
.unwrap();
let res = client.request(req).and_then(move |res| {
let res = client.request(req).with_executor(runtime.executor()).and_then(move |res| {
assert_eq!(res.status(), hyper::StatusCode::OK);
res.into_body().into_stream().concat2()
res.into_body().into_stream().concat()
});
let rx = rx1.map_err(|_| hyper::Error::Io(io::Error::new(io::ErrorKind::Other, "thread panicked")));
res.join(rx).map(|r| r.0).wait().unwrap();
block_on(res.join(rx).map(|r| r.0)).unwrap();
// not closed yet, just idle
{
futures::future::poll_fn(|| {
assert!(closes.poll()?.is_not_ready());
Ok::<_, ()>(().into())
}).wait().unwrap();
block_on(lazy(|cx| {
assert!(closes.poll_next(cx).unwrap().is_pending());
Ok::<_, ()>(())
})).unwrap();
}
drop(client);
let t = Delay::new(Duration::from_millis(100))
.map(|_| panic!("time out"));
let close = closes.into_future()
let close = closes.next()
.map(|(opt, _)| {
opt.expect("closes");
})
.map_err(|_| panic!("closes dropped"));
let _ = t.select(close).wait();
let _ = block_on(t.select(close));
}
@@ -836,32 +835,32 @@ mod dispatch_impl {
// prevent this thread from closing until end of test, so the connection
// stays open and idle until Client is dropped
let _ = client_drop_rx.wait();
let _ = block_on(client_drop_rx);
});
let res = {
let client = Client::configure()
.connector(DebugConnector::with_http_and_closes(HttpConnector::new(1, &handle), closes_tx))
.executor(runtime.executor());
.build();
let req = Request::builder()
.uri(&*format!("http://{}/a", addr))
.body(Body::empty())
.unwrap();
client.request(req)
client.request(req).with_executor(runtime.executor())
};
//let rx = rx1.map_err(|_| hyper::Error::Io(io::Error::new(io::ErrorKind::Other, "thread panicked")));
res.select2(rx1).wait().unwrap();
block_on(res.select(rx1)).unwrap();
// res now dropped
let t = Delay::new(Duration::from_millis(100))
.map(|_| panic!("time out"));
let close = closes.into_future()
let close = closes.next()
.map(|(opt, _)| {
opt.expect("closes");
})
.map_err(|_| panic!("closes dropped"));
let _ = t.select(close).wait();
let _ = block_on(t.select(close));
}
#[test]
@@ -888,33 +887,33 @@ mod dispatch_impl {
// prevent this thread from closing until end of test, so the connection
// stays open and idle until Client is dropped
let _ = client_drop_rx.wait();
let _ = block_on(client_drop_rx);
});
let res = {
let client = Client::configure()
.connector(DebugConnector::with_http_and_closes(HttpConnector::new(1, &handle), closes_tx))
.executor(runtime.executor());
.build();
let req = Request::builder()
.uri(&*format!("http://{}/a", addr))
.body(Body::empty())
.unwrap();
// notably, havent read body yet
client.request(req)
client.request(req).with_executor(runtime.executor())
};
let rx = rx1.map_err(|_| hyper::Error::Io(io::Error::new(io::ErrorKind::Other, "thread panicked")));
res.join(rx).map(|r| r.0).wait().unwrap();
block_on(res.join(rx).map(|r| r.0)).unwrap();
let t = Delay::new(Duration::from_millis(100))
.map(|_| panic!("time out"));
let close = closes.into_future()
let close = closes.next()
.map(|(opt, _)| {
opt.expect("closes");
})
.map_err(|_| panic!("closes dropped"));
let _ = t.select(close).wait();
let _ = block_on(t.select(close));
}
#[test]
@@ -939,33 +938,33 @@ mod dispatch_impl {
sock.read(&mut buf).expect("read 1");
sock.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n").unwrap();
let _ = tx1.send(());
let _ = rx2.wait();
let _ = block_on(rx2);
});
let client = Client::configure()
.connector(DebugConnector::with_http_and_closes(HttpConnector::new(1, &handle), closes_tx))
.keep_alive(false)
.executor(runtime.executor());
.build();
let req = Request::builder()
.uri(&*format!("http://{}/a", addr))
.body(Body::empty())
.unwrap();
let res = client.request(req).and_then(move |res| {
let res = client.request(req).with_executor(runtime.executor()).and_then(move |res| {
assert_eq!(res.status(), hyper::StatusCode::OK);
res.into_body().into_stream().concat2()
res.into_body().into_stream().concat()
});
let rx = rx1.map_err(|_| hyper::Error::Io(io::Error::new(io::ErrorKind::Other, "thread panicked")));
res.join(rx).map(|r| r.0).wait().unwrap();
block_on(res.join(rx).map(|r| r.0)).unwrap();
let t = Delay::new(Duration::from_millis(100))
.map(|_| panic!("time out"));
let close = closes.into_future()
let close = closes.next()
.map(|(opt, _)| {
opt.expect("closes");
})
.map_err(|_| panic!("closes dropped"));
let _ = t.select(close).wait();
let _ = block_on(t.select(close));
}
#[test]
@@ -993,28 +992,28 @@ mod dispatch_impl {
let client = Client::configure()
.connector(DebugConnector::with_http_and_closes(HttpConnector::new(1, &handle), closes_tx))
.executor(runtime.executor());
.build();
let req = Request::builder()
.uri(&*format!("http://{}/a", addr))
.body(Body::empty())
.unwrap();
let res = client.request(req).and_then(move |res| {
let res = client.request(req).with_executor(runtime.executor()).and_then(move |res| {
assert_eq!(res.status(), hyper::StatusCode::OK);
res.into_body().into_stream().concat2()
res.into_body().into_stream().concat()
});
let rx = rx1.map_err(|_| hyper::Error::Io(io::Error::new(io::ErrorKind::Other, "thread panicked")));
res.join(rx).map(|r| r.0).wait().unwrap();
block_on(res.join(rx).map(|r| r.0)).unwrap();
let t = Delay::new(Duration::from_millis(100))
.map(|_| panic!("time out"));
let close = closes.into_future()
let close = closes.next()
.map(|(opt, _)| {
opt.expect("closes");
})
.map_err(|_| panic!("closes dropped"));
let _ = t.select(close).wait();
let _ = block_on(t.select(close));
}
#[test]
@@ -1026,11 +1025,13 @@ mod dispatch_impl {
// See https://github.com/hyperium/hyper/issues/1429
use std::error::Error;
use tokio::prelude::Future;
let _ = pretty_env_logger::try_init();
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let runtime = Runtime::new().unwrap();
let executor = runtime.executor();
let handle = runtime.handle().clone();
let (tx1, rx1) = oneshot::channel();
@@ -1042,27 +1043,27 @@ mod dispatch_impl {
let mut buf = [0; 4096];
sock.read(&mut buf).expect("read 1");
sock.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n").unwrap();
sock.read(&mut buf).expect("read 2");
//sock.read(&mut buf).expect("read 2");
let _ = tx1.send(());
});
let uri = format!("http://{}/a", addr).parse::<hyper::Uri>().unwrap();
let client = Client::configure().build_with_executor(&handle, runtime.executor());
let client = Client::configure().build(&handle);
let req = Request::builder()
.uri(uri.clone())
.body(Body::empty())
.unwrap();
let res = client.request(req).and_then(move |res| {
let res = client.request(req).with_executor(executor.clone()).and_then(move |res| {
assert_eq!(res.status(), hyper::StatusCode::OK);
res.into_body().into_stream().concat2()
res.into_body().into_stream().concat()
});
res.wait().unwrap();
block_on(res).unwrap();
// drop previous runtime
drop(runtime);
// shut down runtime
runtime.shutdown_now().wait().unwrap();
let timeout = Delay::new(Duration::from_millis(200));
let rx = rx1.map_err(|_| hyper::Error::Io(io::Error::new(io::ErrorKind::Other, "thread panicked")));
let rx = rx.and_then(move |_| timeout.map_err(|e| e.into()));
@@ -1071,10 +1072,10 @@ mod dispatch_impl {
.uri(uri)
.body(Body::empty())
.unwrap();
let res = client.request(req);
let res = client.request(req).with_executor(executor);
// this does trigger an 'event loop gone' error, but before, it would
// panic internally on a `SendError`, which is what we're testing against.
let err = res.join(rx).map(|r| r.0).wait().unwrap_err();
let err = block_on(res.join(rx).map(|r| r.0)).unwrap_err();
assert_eq!(err.description(), "event loop gone");
}
@@ -1100,31 +1101,31 @@ mod dispatch_impl {
let client = Client::configure()
.connector(DebugConnector::with_http_and_closes(HttpConnector::new(1, &handle), closes_tx))
.executor(runtime.executor());
.build();
let req = Request::builder()
.uri(&*format!("http://{}/a", addr))
.body(Body::empty())
.unwrap();
let res = client.request(req).and_then(move |res| {
let res = client.request(req).with_executor(runtime.executor()).and_then(move |res| {
assert_eq!(res.status(), hyper::StatusCode::OK);
res.into_body().into_stream().concat2()
res.into_body().into_stream().concat()
});
let rx = rx1.map_err(|_| hyper::Error::Io(io::Error::new(io::ErrorKind::Other, "thread panicked")));
let timeout = Delay::new(Duration::from_millis(200));
let rx = rx.and_then(move |_| timeout.map_err(|e| e.into()));
res.join(rx).map(|r| r.0).wait().unwrap();
block_on(res.join(rx).map(|r| r.0)).unwrap();
let t = Delay::new(Duration::from_millis(100))
.map(|_| panic!("time out"));
let close = closes.into_future()
let close = closes.next()
.map(|(opt, _)| {
opt.expect("closes");
})
.map_err(|_| panic!("closes dropped"));
let _ = t.select(close).wait();
let _ = block_on(t.select(close));
}
#[test]
@@ -1140,14 +1141,14 @@ mod dispatch_impl {
let client = Client::configure()
.connector(connector)
.executor(runtime.executor());
.build();
assert_eq!(connects.load(Ordering::Relaxed), 0);
let req = Request::builder()
.uri("http://hyper.local/a")
.body(Default::default())
.unwrap();
let _fut = client.request(req);
let _fut = client.request(req).with_executor(runtime.executor());
// internal Connect::connect should have been lazy, and not
// triggered an actual connect yet.
assert_eq!(connects.load(Ordering::Relaxed), 0);
@@ -1165,7 +1166,7 @@ mod dispatch_impl {
let client = Client::configure()
.connector(connector)
.executor(runtime.executor());
.build();
let (tx1, rx1) = oneshot::channel();
let (tx2, rx2) = oneshot::channel();
@@ -1195,8 +1196,8 @@ mod dispatch_impl {
.uri(&*format!("http://{}/a", addr))
.body(Body::empty())
.unwrap();
let res = client.request(req);
res.join(rx).map(|r| r.0).wait().unwrap();
let res = client.request(req).with_executor(runtime.executor());
block_on(res.join(rx).map(|r| r.0)).unwrap();
assert_eq!(connects.load(Ordering::SeqCst), 1);
@@ -1209,8 +1210,8 @@ mod dispatch_impl {
.uri(&*format!("http://{}/b", addr))
.body(Body::empty())
.unwrap();
let res = client.request(req);
res.join(rx).map(|r| r.0).wait().unwrap();
let res = client.request(req).with_executor(runtime.executor());
block_on(res.join(rx).map(|r| r.0)).unwrap();
assert_eq!(connects.load(Ordering::SeqCst), 1, "second request should still only have 1 connect");
}
@@ -1228,7 +1229,7 @@ mod dispatch_impl {
let client = Client::configure()
.connector(connector)
.executor(runtime.executor());
.build();
let (tx1, rx1) = oneshot::channel();
let (tx2, rx2) = oneshot::channel();
@@ -1261,8 +1262,8 @@ mod dispatch_impl {
.uri(&*format!("http://{}/a", addr))
.body(Body::empty())
.unwrap();
let res = client.request(req);
res.join(rx).map(|r| r.0).wait().unwrap();
let res = client.request(req).with_executor(runtime.executor());
block_on(res.join(rx).map(|r| r.0)).unwrap();
assert_eq!(connects.load(Ordering::Relaxed), 1);
@@ -1271,8 +1272,8 @@ mod dispatch_impl {
.uri(&*format!("http://{}/b", addr))
.body(Body::empty())
.unwrap();
let res = client.request(req);
res.join(rx).map(|r| r.0).wait().unwrap();
let res = client.request(req).with_executor(runtime.executor());
block_on(res.join(rx).map(|r| r.0)).unwrap();
assert_eq!(connects.load(Ordering::Relaxed), 2);
}
@@ -1289,7 +1290,7 @@ mod dispatch_impl {
let client = Client::configure()
.connector(connector)
.executor(runtime.executor());
.build();
let (tx1, rx1) = oneshot::channel();
thread::spawn(move || {
@@ -1312,8 +1313,8 @@ mod dispatch_impl {
.uri(&*format!("http://{}/foo/bar", addr))
.body(Body::empty())
.unwrap();
let res = client.request(req);
res.join(rx).map(|r| r.0).wait().unwrap();
let res = client.request(req).with_executor(runtime.executor());
block_on(res.join(rx).map(|r| r.0)).unwrap();
}
@@ -1369,33 +1370,25 @@ mod dispatch_impl {
}
}
impl Write for DebugStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
}
impl AsyncWrite for DebugStream {
fn shutdown(&mut self) -> futures::Poll<(), io::Error> {
AsyncWrite::shutdown(&mut self.0)
fn poll_write(&mut self, cx: &mut task::Context, buf: &[u8]) -> futures::Poll<usize, io::Error> {
self.0.poll_write(cx, buf)
}
fn write_buf<B: ::bytes::Buf>(&mut self, buf: &mut B) -> futures::Poll<usize, io::Error> {
self.0.write_buf(buf)
fn poll_flush(&mut self, cx: &mut task::Context) -> futures::Poll<(), io::Error> {
self.0.poll_flush(cx)
}
fn poll_close(&mut self, cx: &mut task::Context) -> futures::Poll<(), io::Error> {
self.0.poll_close(cx)
}
}
impl Read for DebugStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
impl AsyncRead for DebugStream {
fn poll_read(&mut self, cx: &mut task::Context, buf: &mut [u8]) -> futures::Poll<usize, io::Error> {
self.0.poll_read(cx, buf)
}
}
impl AsyncRead for DebugStream {}
}
mod conn {
@@ -1404,13 +1397,15 @@ mod conn {
use std::thread;
use std::time::Duration;
use futures::{Async, Future, Poll, Stream};
use futures::future::poll_fn;
use futures::sync::oneshot;
use futures::{FutureExt, Poll, StreamExt};
use futures::future::{poll_fn, lazy};
use futures::channel::oneshot;
use futures::task;
use futures::io::{AsyncRead, AsyncWrite};
use futures::executor::block_on;
use futures_timer::Delay;
use tokio::runtime::Runtime;
use tokio::net::TcpStream;
use tokio_io::{AsyncRead, AsyncWrite};
use hyper::{self, Request};
use hyper::client::conn;
@@ -1442,11 +1437,11 @@ mod conn {
let _ = tx1.send(());
});
let tcp = tcp_connect(&addr).wait().unwrap();
let tcp = block_on(tcp_connect(&addr)).unwrap();
let (mut client, conn) = conn::handshake(tcp).wait().unwrap();
let (mut client, conn) = block_on(conn::handshake(tcp)).unwrap();
runtime.spawn(conn.map(|_| ()).map_err(|e| panic!("conn error: {}", e)));
runtime.spawn2(conn.map(|_| ()).map_err(|e| panic!("conn error: {}", e)));
let req = Request::builder()
.uri("/a")
@@ -1454,13 +1449,13 @@ mod conn {
.unwrap();
let res = client.send_request(req).and_then(move |res| {
assert_eq!(res.status(), hyper::StatusCode::OK);
res.into_body().into_stream().concat2()
res.into_body().into_stream().concat()
});
let rx = rx1.map_err(|_| hyper::Error::Io(io::Error::new(io::ErrorKind::Other, "thread panicked")));
let timeout = Delay::new(Duration::from_millis(200));
let rx = rx.and_then(move |_| timeout.map_err(|e| e.into()));
res.join(rx).map(|r| r.0).wait().unwrap();
block_on(res.join(rx).map(|r| r.0)).unwrap();
}
#[test]
@@ -1487,11 +1482,11 @@ mod conn {
let _ = tx1.send(());
});
let tcp = tcp_connect(&addr).wait().unwrap();
let tcp = block_on(tcp_connect(&addr)).unwrap();
let (mut client, conn) = conn::handshake(tcp).wait().unwrap();
let (mut client, conn) = block_on(conn::handshake(tcp)).unwrap();
runtime.spawn(conn.map(|_| ()).map_err(|e| panic!("conn error: {}", e)));
runtime.spawn2(conn.map(|_| ()).map_err(|e| panic!("conn error: {}", e)));
let req = Request::builder()
.uri("http://hyper.local/a")
@@ -1500,13 +1495,13 @@ mod conn {
let res = client.send_request(req).and_then(move |res| {
assert_eq!(res.status(), hyper::StatusCode::OK);
res.into_body().into_stream().concat2()
res.into_body().into_stream().concat()
});
let rx = rx1.map_err(|_| hyper::Error::Io(io::Error::new(io::ErrorKind::Other, "thread panicked")));
let timeout = Delay::new(Duration::from_millis(200));
let rx = rx.and_then(move |_| timeout.map_err(|e| e.into()));
res.join(rx).map(|r| r.0).wait().unwrap();
block_on(res.join(rx).map(|r| r.0)).unwrap();
}
#[test]
@@ -1528,11 +1523,11 @@ mod conn {
let _ = tx1.send(());
});
let tcp = tcp_connect(&addr).wait().unwrap();
let tcp = block_on(tcp_connect(&addr)).unwrap();
let (mut client, conn) = conn::handshake(tcp).wait().unwrap();
let (mut client, conn) = block_on(conn::handshake(tcp)).unwrap();
runtime.spawn(conn.map(|_| ()).map_err(|e| panic!("conn error: {}", e)));
runtime.spawn2(conn.map(|_| ()).map_err(|e| panic!("conn error: {}", e)));
let req = Request::builder()
.uri("/a")
@@ -1540,10 +1535,10 @@ mod conn {
.unwrap();
let res1 = client.send_request(req).and_then(move |res| {
assert_eq!(res.status(), hyper::StatusCode::OK);
res.into_body().into_stream().concat2()
res.into_body().into_stream().concat()
});
// pipelined request will hit NotReady, and thus should return an Error::Cancel
// pipelined request will hit Pending, and thus should return an Error::Cancel
let req = Request::builder()
.uri("/b")
.body(Default::default())
@@ -1562,12 +1557,12 @@ mod conn {
let timeout = Delay::new(Duration::from_millis(200));
let rx = rx.and_then(move |_| timeout.map_err(|e| e.into()));
res1.join(res2).join(rx).map(|r| r.0).wait().unwrap();
block_on(res1.join(res2).join(rx).map(|r| r.0)).unwrap();
}
#[test]
fn upgrade() {
use tokio_io::io::{read_to_end, write_all};
use futures::io::{AsyncReadExt, AsyncWriteExt};
let _ = ::pretty_env_logger::try_init();
let server = TcpListener::bind("127.0.0.1:0").unwrap();
@@ -1595,18 +1590,18 @@ mod conn {
sock.write_all(b"bar=foo").expect("write 2");
});
let tcp = tcp_connect(&addr).wait().unwrap();
let tcp = block_on(tcp_connect(&addr)).unwrap();
let io = DebugStream {
tcp: tcp,
shutdown_called: false,
};
let (mut client, mut conn) = conn::handshake(io).wait().unwrap();
let (mut client, mut conn) = block_on(conn::handshake(io)).unwrap();
{
let until_upgrade = poll_fn(|| {
conn.poll_without_shutdown()
let until_upgrade = poll_fn(|cx| {
conn.poll_without_shutdown(cx)
});
let req = Request::builder()
@@ -1616,20 +1611,20 @@ mod conn {
let res = client.send_request(req).and_then(move |res| {
assert_eq!(res.status(), hyper::StatusCode::SWITCHING_PROTOCOLS);
assert_eq!(res.headers()["Upgrade"], "foobar");
res.into_body().into_stream().concat2()
res.into_body().into_stream().concat()
});
let rx = rx1.map_err(|_| hyper::Error::Io(io::Error::new(io::ErrorKind::Other, "thread panicked")));
let timeout = Delay::new(Duration::from_millis(200));
let rx = rx.and_then(move |_| timeout.map_err(|e| e.into()));
until_upgrade.join(res).join(rx).map(|r| r.0).wait().unwrap();
block_on(until_upgrade.join(res).join(rx).map(|r| r.0)).unwrap();
// should not be ready now
poll_fn(|| {
assert!(client.poll_ready().unwrap().is_not_ready());
Ok::<_, ()>(Async::Ready(()))
}).wait().unwrap();
block_on(lazy(|cx| {
assert!(client.poll_ready(cx).unwrap().is_pending());
Ok::<_, ()>(())
})).unwrap();
}
let parts = conn.into_parts();
@@ -1638,16 +1633,16 @@ mod conn {
assert_eq!(buf, b"foobar=ready"[..]);
assert!(!io.shutdown_called, "upgrade shouldn't shutdown AsyncWrite");
assert!(client.poll_ready().is_err());
assert!(block_on(poll_fn(|cx| client.poll_ready(cx))).is_err());
let io = write_all(io, b"foo=bar").wait().unwrap().0;
let vec = read_to_end(io, vec![]).wait().unwrap().1;
let io = block_on(io.write_all(b"foo=bar")).unwrap().0;
let vec = block_on(io.read_to_end(vec![])).unwrap().1;
assert_eq!(vec, b"bar=foo");
}
#[test]
fn connect_method() {
use tokio_io::io::{read_to_end, write_all};
use futures::io::{AsyncReadExt, AsyncWriteExt};
let _ = ::pretty_env_logger::try_init();
let server = TcpListener::bind("127.0.0.1:0").unwrap();
@@ -1674,18 +1669,18 @@ mod conn {
sock.write_all(b"bar=foo").expect("write 2");
});
let tcp = tcp_connect(&addr).wait().unwrap();
let tcp = block_on(tcp_connect(&addr)).unwrap();
let io = DebugStream {
tcp: tcp,
shutdown_called: false,
};
let (mut client, mut conn) = conn::handshake(io).wait().unwrap();
let (mut client, mut conn) = block_on(conn::handshake(io)).unwrap();
{
let until_tunneled = poll_fn(|| {
conn.poll_without_shutdown()
let until_tunneled = poll_fn(|cx| {
conn.poll_without_shutdown(cx)
});
let req = Request::builder()
@@ -1696,7 +1691,7 @@ mod conn {
let res = client.send_request(req)
.and_then(move |res| {
assert_eq!(res.status(), hyper::StatusCode::OK);
res.into_body().into_stream().concat2()
res.into_body().into_stream().concat()
})
.map(|body| {
assert_eq!(body.as_ref(), b"");
@@ -1706,13 +1701,13 @@ mod conn {
let timeout = Delay::new(Duration::from_millis(200));
let rx = rx.and_then(move |_| timeout.map_err(|e| e.into()));
until_tunneled.join(res).join(rx).map(|r| r.0).wait().unwrap();
block_on(until_tunneled.join(res).join(rx).map(|r| r.0)).unwrap();
// should not be ready now
poll_fn(|| {
assert!(client.poll_ready().unwrap().is_not_ready());
Ok::<_, ()>(Async::Ready(()))
}).wait().unwrap();
block_on(lazy(|cx| {
assert!(client.poll_ready(cx).unwrap().is_pending());
Ok::<_, ()>(())
})).unwrap();
}
let parts = conn.into_parts();
@@ -1721,10 +1716,10 @@ mod conn {
assert_eq!(buf, b"foobar=ready"[..]);
assert!(!io.shutdown_called, "tunnel shouldn't shutdown AsyncWrite");
assert!(client.poll_ready().is_err());
assert!(block_on(poll_fn(|cx| client.poll_ready(cx))).is_err());
let io = write_all(io, b"foo=bar").wait().unwrap().0;
let vec = read_to_end(io, vec![]).wait().unwrap().1;
let io = block_on(io.write_all(b"foo=bar")).unwrap().0;
let vec = block_on(io.read_to_end(vec![])).unwrap().1;
assert_eq!(vec, b"bar=foo");
}
@@ -1733,28 +1728,24 @@ mod conn {
shutdown_called: bool,
}
impl Write for DebugStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.tcp.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.tcp.flush()
}
}
impl AsyncWrite for DebugStream {
fn shutdown(&mut self) -> Poll<(), io::Error> {
fn poll_write(&mut self, cx: &mut task::Context, buf: &[u8]) -> Poll<usize, io::Error> {
self.tcp.poll_write(cx, buf)
}
fn poll_flush(&mut self, cx: &mut task::Context) -> Poll<(), io::Error> {
self.tcp.poll_flush(cx)
}
fn poll_close(&mut self, cx: &mut task::Context) -> Poll<(), io::Error> {
self.shutdown_called = true;
AsyncWrite::shutdown(&mut self.tcp)
self.tcp.poll_close(cx)
}
}
impl Read for DebugStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.tcp.read(buf)
impl AsyncRead for DebugStream {
fn poll_read(&mut self, cx: &mut task::Context, buf: &mut [u8]) -> Poll<usize, io::Error> {
self.tcp.poll_read(cx, buf)
}
}
impl AsyncRead for DebugStream {}
}