feat(lib): convert to use tokio 0.1
BREAKING CHANGE: All uses of `Handle` now need to be new-tokio `Handle`. Co-authored-by: Sean McArthur <sean@seanmonstar.com>
This commit is contained in:
142
tests/server.rs
142
tests/server.rs
@@ -3,9 +3,11 @@ extern crate http;
|
||||
extern crate hyper;
|
||||
#[macro_use]
|
||||
extern crate futures;
|
||||
extern crate futures_timer;
|
||||
extern crate net2;
|
||||
extern crate spmc;
|
||||
extern crate pretty_env_logger;
|
||||
extern crate tokio_core;
|
||||
extern crate tokio;
|
||||
extern crate tokio_io;
|
||||
|
||||
use std::net::{TcpStream, Shutdown, SocketAddr};
|
||||
@@ -13,21 +15,29 @@ use std::io::{self, Read, Write};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::mpsc;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::net::{TcpListener as StdTcpListener};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use futures::{Future, Stream};
|
||||
use futures::future::{self, FutureResult, Either};
|
||||
use futures::sync::oneshot;
|
||||
use futures_timer::Delay;
|
||||
use http::header::{HeaderName, HeaderValue};
|
||||
use tokio_core::net::TcpListener;
|
||||
use tokio_core::reactor::{Core, Timeout};
|
||||
//use net2::TcpBuilder;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::runtime::Runtime;
|
||||
use tokio::reactor::Handle;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
|
||||
|
||||
use hyper::{Body, Request, Response, StatusCode};
|
||||
use hyper::server::{Http, Service, NewService, service_fn};
|
||||
|
||||
fn tcp_bind(addr: &SocketAddr, handle: &Handle) -> ::tokio::io::Result<TcpListener> {
|
||||
let std_listener = StdTcpListener::bind(addr).unwrap();
|
||||
TcpListener::from_std(std_listener, handle)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_should_ignore_body() {
|
||||
@@ -67,8 +77,8 @@ fn get_with_body() {
|
||||
#[test]
|
||||
fn get_implicitly_empty() {
|
||||
// See https://github.com/hyperium/hyper/issues/1373
|
||||
let mut core = Core::new().unwrap();
|
||||
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &core.handle()).unwrap();
|
||||
let runtime = Runtime::new().unwrap();
|
||||
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap(), &runtime.handle()).unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
|
||||
thread::spawn(move || {
|
||||
@@ -84,11 +94,11 @@ fn get_implicitly_empty() {
|
||||
.into_future()
|
||||
.map_err(|_| unreachable!())
|
||||
.and_then(|(item, _incoming)| {
|
||||
let (socket, _) = item.unwrap();
|
||||
let socket = item.unwrap();
|
||||
Http::<hyper::Chunk>::new().serve_connection(socket, GetImplicitlyEmpty)
|
||||
});
|
||||
|
||||
core.run(fut).unwrap();
|
||||
fut.wait().unwrap();
|
||||
|
||||
struct GetImplicitlyEmpty;
|
||||
|
||||
@@ -96,7 +106,7 @@ fn get_implicitly_empty() {
|
||||
type Request = Request<Body>;
|
||||
type Response = Response<Body>;
|
||||
type Error = hyper::Error;
|
||||
type Future = Box<Future<Item=Self::Response, Error=Self::Error>>;
|
||||
type Future = Box<Future<Item=Self::Response, Error=Self::Error> + Send>;
|
||||
|
||||
fn call(&self, req: Request<Body>) -> Self::Future {
|
||||
Box::new(req.into_body()
|
||||
@@ -744,9 +754,8 @@ fn http_10_request_receives_http_10_response() {
|
||||
|
||||
#[test]
|
||||
fn disable_keep_alive_mid_request() {
|
||||
|
||||
let mut core = Core::new().unwrap();
|
||||
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &core.handle()).unwrap();
|
||||
let runtime = Runtime::new().unwrap();
|
||||
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap(), &runtime.handle()).unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
|
||||
let (tx1, rx1) = oneshot::channel();
|
||||
@@ -766,7 +775,7 @@ fn disable_keep_alive_mid_request() {
|
||||
.into_future()
|
||||
.map_err(|_| unreachable!())
|
||||
.and_then(|(item, _incoming)| {
|
||||
let (socket, _) = item.unwrap();
|
||||
let socket = item.unwrap();
|
||||
Http::<hyper::Chunk>::new().serve_connection(socket, HelloWorld)
|
||||
.select2(rx1)
|
||||
.then(|r| {
|
||||
@@ -783,15 +792,15 @@ fn disable_keep_alive_mid_request() {
|
||||
})
|
||||
});
|
||||
|
||||
core.run(fut).unwrap();
|
||||
fut.wait().unwrap();
|
||||
child.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disable_keep_alive_post_request() {
|
||||
let _ = pretty_env_logger::try_init();
|
||||
let mut core = Core::new().unwrap();
|
||||
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &core.handle()).unwrap();
|
||||
let runtime = Runtime::new().unwrap();
|
||||
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap(), &runtime.handle()).unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
|
||||
let (tx1, rx1) = oneshot::channel();
|
||||
@@ -827,7 +836,7 @@ fn disable_keep_alive_post_request() {
|
||||
.into_future()
|
||||
.map_err(|_| unreachable!())
|
||||
.and_then(|(item, _incoming)| {
|
||||
let (socket, _) = item.expect("accepted socket");
|
||||
let socket = item.expect("accepted socket");
|
||||
let transport = DebugStream {
|
||||
stream: socket,
|
||||
_debug: dropped2,
|
||||
@@ -848,21 +857,21 @@ fn disable_keep_alive_post_request() {
|
||||
});
|
||||
|
||||
assert!(!dropped.load());
|
||||
core.run(fut).unwrap();
|
||||
fut.wait().unwrap();
|
||||
// we must poll the Core one more time in order for Windows to drop
|
||||
// the read-blocked socket.
|
||||
//
|
||||
// See https://github.com/carllerche/mio/issues/776
|
||||
let timeout = Timeout::new(Duration::from_millis(10), &core.handle()).unwrap();
|
||||
core.run(timeout).unwrap();
|
||||
let timeout = Delay::new(Duration::from_millis(10));
|
||||
timeout.wait().unwrap();
|
||||
assert!(dropped.load());
|
||||
child.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_parse_eof_does_not_return_error() {
|
||||
let mut core = Core::new().unwrap();
|
||||
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &core.handle()).unwrap();
|
||||
let runtime = Runtime::new().unwrap();
|
||||
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap(), &runtime.handle()).unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
|
||||
thread::spawn(move || {
|
||||
@@ -873,17 +882,17 @@ fn empty_parse_eof_does_not_return_error() {
|
||||
.into_future()
|
||||
.map_err(|_| unreachable!())
|
||||
.and_then(|(item, _incoming)| {
|
||||
let (socket, _) = item.unwrap();
|
||||
let socket = item.unwrap();
|
||||
Http::<hyper::Chunk>::new().serve_connection(socket, HelloWorld)
|
||||
});
|
||||
|
||||
core.run(fut).unwrap();
|
||||
fut.wait().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nonempty_parse_eof_returns_error() {
|
||||
let mut core = Core::new().unwrap();
|
||||
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &core.handle()).unwrap();
|
||||
let runtime = Runtime::new().unwrap();
|
||||
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap(), &runtime.handle()).unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
|
||||
thread::spawn(move || {
|
||||
@@ -895,18 +904,18 @@ fn nonempty_parse_eof_returns_error() {
|
||||
.into_future()
|
||||
.map_err(|_| unreachable!())
|
||||
.and_then(|(item, _incoming)| {
|
||||
let (socket, _) = item.unwrap();
|
||||
let socket = item.unwrap();
|
||||
Http::<hyper::Chunk>::new().serve_connection(socket, HelloWorld)
|
||||
.map(|_| ())
|
||||
});
|
||||
|
||||
core.run(fut).unwrap_err();
|
||||
fut.wait().unwrap_err();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn returning_1xx_response_is_error() {
|
||||
let mut core = Core::new().unwrap();
|
||||
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &core.handle()).unwrap();
|
||||
let runtime = Runtime::new().unwrap();
|
||||
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap(), &runtime.handle()).unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
|
||||
thread::spawn(move || {
|
||||
@@ -923,7 +932,7 @@ fn returning_1xx_response_is_error() {
|
||||
.into_future()
|
||||
.map_err(|_| unreachable!())
|
||||
.and_then(|(item, _incoming)| {
|
||||
let (socket, _) = item.unwrap();
|
||||
let socket = item.unwrap();
|
||||
Http::<hyper::Chunk>::new()
|
||||
.serve_connection(socket, service_fn(|_| {
|
||||
Ok(Response::builder()
|
||||
@@ -934,15 +943,15 @@ fn returning_1xx_response_is_error() {
|
||||
.map(|_| ())
|
||||
});
|
||||
|
||||
core.run(fut).unwrap_err();
|
||||
fut.wait().unwrap_err();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn upgrades() {
|
||||
use tokio_io::io::{read_to_end, write_all};
|
||||
let _ = pretty_env_logger::try_init();
|
||||
let mut core = Core::new().unwrap();
|
||||
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &core.handle()).unwrap();
|
||||
let runtime = Runtime::new().unwrap();
|
||||
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap(), &runtime.handle()).unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
@@ -971,7 +980,7 @@ fn upgrades() {
|
||||
.into_future()
|
||||
.map_err(|_| -> hyper::Error { unreachable!() })
|
||||
.and_then(|(item, _incoming)| {
|
||||
let (socket, _) = item.unwrap();
|
||||
let socket = item.unwrap();
|
||||
let conn = Http::<hyper::Chunk>::new()
|
||||
.serve_connection(socket, service_fn(|_| {
|
||||
let res = Response::builder()
|
||||
@@ -990,24 +999,24 @@ fn upgrades() {
|
||||
})
|
||||
});
|
||||
|
||||
let conn = core.run(fut).unwrap();
|
||||
let conn = fut.wait().unwrap();
|
||||
|
||||
// wait so that we don't write until other side saw 101 response
|
||||
core.run(rx).unwrap();
|
||||
rx.wait().unwrap();
|
||||
|
||||
let parts = conn.into_parts();
|
||||
let io = parts.io;
|
||||
assert_eq!(parts.read_buf, "eagerly optimistic");
|
||||
|
||||
let io = core.run(write_all(io, b"foo=bar")).unwrap().0;
|
||||
let vec = core.run(read_to_end(io, vec![])).unwrap().1;
|
||||
let io = write_all(io, b"foo=bar").wait().unwrap().0;
|
||||
let vec = read_to_end(io, vec![]).wait().unwrap().1;
|
||||
assert_eq!(vec, b"bar=foo");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_errors_send_4xx_response() {
|
||||
let mut core = Core::new().unwrap();
|
||||
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &core.handle()).unwrap();
|
||||
let runtime = Runtime::new().unwrap();
|
||||
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap(), &runtime.handle()).unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
|
||||
thread::spawn(move || {
|
||||
@@ -1024,19 +1033,19 @@ fn parse_errors_send_4xx_response() {
|
||||
.into_future()
|
||||
.map_err(|_| unreachable!())
|
||||
.and_then(|(item, _incoming)| {
|
||||
let (socket, _) = item.unwrap();
|
||||
let socket = item.unwrap();
|
||||
Http::<hyper::Chunk>::new()
|
||||
.serve_connection(socket, HelloWorld)
|
||||
.map(|_| ())
|
||||
});
|
||||
|
||||
core.run(fut).unwrap_err();
|
||||
fut.wait().unwrap_err();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn illegal_request_length_returns_400_response() {
|
||||
let mut core = Core::new().unwrap();
|
||||
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &core.handle()).unwrap();
|
||||
let runtime = Runtime::new().unwrap();
|
||||
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap(), &runtime.handle()).unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
|
||||
thread::spawn(move || {
|
||||
@@ -1053,20 +1062,20 @@ fn illegal_request_length_returns_400_response() {
|
||||
.into_future()
|
||||
.map_err(|_| unreachable!())
|
||||
.and_then(|(item, _incoming)| {
|
||||
let (socket, _) = item.unwrap();
|
||||
let socket = item.unwrap();
|
||||
Http::<hyper::Chunk>::new()
|
||||
.serve_connection(socket, HelloWorld)
|
||||
.map(|_| ())
|
||||
});
|
||||
|
||||
core.run(fut).unwrap_err();
|
||||
fut.wait().unwrap_err();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn max_buf_size() {
|
||||
let _ = pretty_env_logger::try_init();
|
||||
let mut core = Core::new().unwrap();
|
||||
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &core.handle()).unwrap();
|
||||
let runtime = Runtime::new().unwrap();
|
||||
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap(), &runtime.handle()).unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
|
||||
const MAX: usize = 16_000;
|
||||
@@ -1086,21 +1095,21 @@ fn max_buf_size() {
|
||||
.into_future()
|
||||
.map_err(|_| unreachable!())
|
||||
.and_then(|(item, _incoming)| {
|
||||
let (socket, _) = item.unwrap();
|
||||
let socket = item.unwrap();
|
||||
Http::<hyper::Chunk>::new()
|
||||
.max_buf_size(MAX)
|
||||
.serve_connection(socket, HelloWorld)
|
||||
.map(|_| ())
|
||||
});
|
||||
|
||||
core.run(fut).unwrap_err();
|
||||
fut.wait().unwrap_err();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn streaming_body() {
|
||||
let _ = pretty_env_logger::try_init();
|
||||
let mut core = Core::new().unwrap();
|
||||
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &core.handle()).unwrap();
|
||||
let runtime = Runtime::new().unwrap();
|
||||
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap(), &runtime.handle()).unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
@@ -1130,7 +1139,7 @@ fn streaming_body() {
|
||||
.into_future()
|
||||
.map_err(|_| unreachable!())
|
||||
.and_then(|(item, _incoming)| {
|
||||
let (socket, _) = item.unwrap();
|
||||
let socket = item.unwrap();
|
||||
Http::<hyper::Chunk>::new()
|
||||
.keep_alive(false)
|
||||
.serve_connection(socket, service_fn(|_| {
|
||||
@@ -1143,7 +1152,7 @@ fn streaming_body() {
|
||||
.map(|_| ())
|
||||
});
|
||||
|
||||
core.run(fut.join(rx)).unwrap();
|
||||
fut.join(rx).wait().unwrap();
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
@@ -1272,7 +1281,7 @@ impl Service for TestService {
|
||||
type Request = Request<Body>;
|
||||
type Response = Response<Body>;
|
||||
type Error = hyper::Error;
|
||||
type Future = Box<Future<Item=Response<Body>, Error=hyper::Error>>;
|
||||
type Future = Box<Future<Item=Response<Body>, Error=hyper::Error> + Send>;
|
||||
fn call(&self, req: Request<Body>) -> Self::Future {
|
||||
let tx1 = self.tx.clone();
|
||||
let tx2 = self.tx.clone();
|
||||
@@ -1370,16 +1379,19 @@ fn serve_with_options(options: ServeOptions) -> Serve {
|
||||
|
||||
let thread_name = format!("test-server-{:?}", dur);
|
||||
let thread = thread::Builder::new().name(thread_name).spawn(move || {
|
||||
let srv = Http::new()
|
||||
.keep_alive(keep_alive)
|
||||
.pipeline(pipeline)
|
||||
.bind(&addr, TestService {
|
||||
tx: Arc::new(Mutex::new(msg_tx.clone())),
|
||||
_timeout: dur,
|
||||
reply: reply_rx,
|
||||
}).unwrap();
|
||||
addr_tx.send(srv.local_addr().unwrap()).unwrap();
|
||||
srv.run_until(shutdown_rx.then(|_| Ok(()))).unwrap();
|
||||
tokio::run(::futures::future::lazy(move || {
|
||||
let srv = Http::new()
|
||||
.keep_alive(keep_alive)
|
||||
.pipeline(pipeline)
|
||||
.bind(&addr, TestService {
|
||||
tx: Arc::new(Mutex::new(msg_tx.clone())),
|
||||
_timeout: dur,
|
||||
reply: reply_rx,
|
||||
}).unwrap();
|
||||
addr_tx.send(srv.local_addr().unwrap()).unwrap();
|
||||
srv.run_until(shutdown_rx.then(|_| Ok(())))
|
||||
.map_err(|err| println!("error {}", err))
|
||||
}))
|
||||
}).unwrap();
|
||||
|
||||
let addr = addr_rx.recv().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user