Skip to content

Commit

Permalink
fix blocking client to use request timeout for response body (#1395)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Nov 30, 2021
1 parent 8b37ae4 commit d73d961
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/blocking/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ impl ClientHandle {
Ok(Err(err)) => Err(err.with_url(url)),
Ok(Ok(res)) => Ok(Response::new(
res,
self.timeout.0,
timeout,
KeepCoreThreadAlive(Some(self.inner.clone())),
)),
Err(wait::Waited::TimedOut(e)) => Err(crate::error::request(e).with_url(url)),
Expand Down
37 changes: 37 additions & 0 deletions tests/timeouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,43 @@ fn timeout_blocking_request() {
assert_eq!(err.url().map(|u| u.as_str()), Some(url.as_str()));
}

#[cfg(feature = "blocking")]
#[test]
fn blocking_request_timeout_body() {
let _ = env_logger::try_init();

let client = reqwest::blocking::Client::builder()
// this should be overridden
.connect_timeout(Duration::from_millis(200))
// this should be overridden
.timeout(Duration::from_millis(200))
.build()
.unwrap();

let server = server::http(move |_req| {
async {
// immediate response, but delayed body
let body = hyper::Body::wrap_stream(futures_util::stream::once(async {
tokio::time::sleep(Duration::from_secs(1)).await;
Ok::<_, std::convert::Infallible>("Hello")
}));

http::Response::new(body)
}
});

let url = format!("http://{}/closes", server.addr());
let res = client
.get(&url)
// longer than client timeout
.timeout(Duration::from_secs(5))
.send()
.expect("get response");

let text = res.text().unwrap();
assert_eq!(text, "Hello");
}

#[cfg(feature = "blocking")]
#[test]
fn write_timeout_large_body() {
Expand Down

0 comments on commit d73d961

Please sign in to comment.