tests(client): make retryable request tests more reliable

This commit is contained in:
Sean McArthur
2018-03-05 15:05:24 -08:00
parent 49fcb0663c
commit 994bcd193c
4 changed files with 252 additions and 90 deletions

View File

@@ -584,72 +584,6 @@ fn client_keep_alive() {
core.run(res.join(rx).map(|r| r.0)).unwrap();
}
#[test]
fn client_keep_alive_connreset() {
use std::sync::mpsc;
extern crate pretty_env_logger;
let _ = pretty_env_logger::try_init();
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let mut core = Core::new().unwrap();
let handle = core.handle();
let client = client(&handle);
let (tx1, rx1) = oneshot::channel();
let (tx2, rx2) = mpsc::channel();
thread::spawn(move || {
let mut sock = server.accept().unwrap().0;
sock.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
sock.set_write_timeout(Some(Duration::from_secs(5))).unwrap();
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").expect("write 1");
// Wait for client to indicate it is done processing the first request
// This is what seem to trigger the race condition -- without it client notices
// connection is closed while processing the first request.
let _ = rx2.recv();
let _r = sock.shutdown(std::net::Shutdown::Both);
// Let client know it can try to reuse the connection
let _ = tx1.send(());
// use sock2 so that sock isn't dropped yet
let mut sock2 = server.accept().unwrap().0;
sock2.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
sock2.set_write_timeout(Some(Duration::from_secs(5))).unwrap();
let mut buf = [0; 4096];
sock2.read(&mut buf).expect("read 2");
sock2.write_all(b"HTTP/1.1 222 OK\r\nContent-Length: 0\r\n\r\n").expect("write 2");
});
let res = client.get(format!("http://{}/a", addr).parse().unwrap());
core.run(res).unwrap();
let _ = tx2.send(());
let rx = rx1.map_err(|_| hyper::Error::Io(io::Error::new(io::ErrorKind::Other, "thread panicked")));
core.run(rx).unwrap();
let t = Timeout::new(Duration::from_millis(100), &handle).unwrap();
let res = client.get(format!("http://{}/b", addr).parse().unwrap())
.map(|res| {
assert_eq!(res.status().as_u16(), 222);
});
let fut = res.select2(t).then(|result| match result {
Ok(Either::A((resp, _))) => Ok(resp),
Err(Either::A((err, _))) => Err(err),
Ok(Either::B(_)) |
Err(Either::B(_)) => Err(hyper::Error::Timeout),
});
core.run(fut).expect("req 2");
}
#[test]
fn client_keep_alive_extra_body() {
let _ = pretty_env_logger::try_init();