From b64851c5ec286a547c36bd00a5a720fb60500ffb Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 17 Apr 2019 10:28:27 -0700 Subject: [PATCH] enable runtime for test:: methods --- awc/src/request.rs | 4 +-- src/responder.rs | 4 +-- src/test.rs | 65 +++++++++++++++++++--------------------------- 3 files changed, 31 insertions(+), 42 deletions(-) diff --git a/awc/src/request.rs b/awc/src/request.rs index d6716cdcf..c868d052b 100644 --- a/awc/src/request.rs +++ b/awc/src/request.rs @@ -331,7 +331,7 @@ impl ClientRequest { /// This method calls provided closure with builder reference if /// value is `true`. - pub fn if_true(mut self, value: bool, f: F) -> Self + pub fn if_true(self, value: bool, f: F) -> Self where F: FnOnce(ClientRequest) -> ClientRequest, { @@ -344,7 +344,7 @@ impl ClientRequest { /// This method calls provided closure with builder reference if /// value is `Some`. - pub fn if_some(mut self, value: Option, f: F) -> Self + pub fn if_some(self, value: Option, f: F) -> Self where F: FnOnce(T, ClientRequest) -> ClientRequest, { diff --git a/src/responder.rs b/src/responder.rs index 3e0676289..103009e75 100644 --- a/src/responder.rs +++ b/src/responder.rs @@ -307,11 +307,11 @@ pub(crate) mod tests { ); let req = TestRequest::with_uri("/none").to_request(); - let resp = TestRequest::block_on(srv.call(req)).unwrap(); + let resp = block_on(srv.call(req)).unwrap(); assert_eq!(resp.status(), StatusCode::NOT_FOUND); let req = TestRequest::with_uri("/some").to_request(); - let resp = TestRequest::block_on(srv.call(req)).unwrap(); + let resp = block_on(srv.call(req)).unwrap(); assert_eq!(resp.status(), StatusCode::OK); match resp.response().body() { ResponseBody::Body(Body::Bytes(ref b)) => { diff --git a/src/test.rs b/src/test.rs index 89562c61a..ad40a0321 100644 --- a/src/test.rs +++ b/src/test.rs @@ -58,7 +58,7 @@ where /// This function panics on nested call. pub fn run_on(f: F) -> R where - F: Fn() -> R, + F: FnOnce() -> R, { RT.with(move |rt| rt.borrow_mut().block_on(lazy(|| Ok::<_, ()>(f())))) .unwrap() @@ -117,7 +117,9 @@ where S::InitError: std::fmt::Debug, { let cfg = ServerConfig::new("127.0.0.1:8080".parse().unwrap()); - block_on(app.into_new_service().new_service(&cfg)).unwrap() + let srv = app.into_new_service(); + let fut = run_on(move || srv.new_service(&cfg)); + block_on(fut).unwrap() } /// Calls service and waits for response future completion. @@ -146,7 +148,7 @@ where S: Service, Error = E>, E: std::fmt::Debug, { - block_on(app.call(req)).unwrap() + block_on(run_on(move || app.call(req))).unwrap() } /// Helper function that returns a response body of a TestRequest @@ -178,13 +180,15 @@ where S: Service, Error = Error>, B: MessageBody, { - block_on(app.call(req).and_then(|mut resp: ServiceResponse| { - resp.take_body() - .fold(BytesMut::new(), move |mut body, chunk| { - body.extend_from_slice(&chunk); - Ok::<_, Error>(body) - }) - .map(|body: BytesMut| body.freeze()) + block_on(run_on(move || { + app.call(req).and_then(|mut resp: ServiceResponse| { + resp.take_body() + .fold(BytesMut::new(), move |mut body, chunk| { + body.extend_from_slice(&chunk); + Ok::<_, Error>(body) + }) + .map(|body: BytesMut| body.freeze()) + }) })) .unwrap_or_else(|_| panic!("read_response failed at block_on unwrap")) } @@ -229,17 +233,19 @@ where B: MessageBody, T: DeserializeOwned, { - block_on(app.call(req).and_then(|mut resp: ServiceResponse| { - resp.take_body() - .fold(BytesMut::new(), move |mut body, chunk| { - body.extend_from_slice(&chunk); - Ok::<_, Error>(body) - }) - .and_then(|body: BytesMut| { - ok(serde_json::from_slice(&body).unwrap_or_else(|_| { - panic!("read_response_json failed during deserialization") - })) - }) + block_on(run_on(move || { + app.call(req).and_then(|mut resp: ServiceResponse| { + resp.take_body() + .fold(BytesMut::new(), move |mut body, chunk| { + body.extend_from_slice(&chunk); + Ok::<_, Error>(body) + }) + .and_then(|body: BytesMut| { + ok(serde_json::from_slice(&body).unwrap_or_else(|_| { + panic!("read_response_json failed during deserialization") + })) + }) + }) })) .unwrap_or_else(|_| panic!("read_response_json failed at block_on unwrap")) } @@ -460,23 +466,6 @@ impl TestRequest { req.set_route_data(Some(Rc::new(self.route_data))); (req, payload) } - - /// Runs the provided future, blocking the current thread until the future - /// completes. - /// - /// This function can be used to synchronously block the current thread - /// until the provided `future` has resolved either successfully or with an - /// error. The result of the future is then returned from this function - /// call. - /// - /// Note that this function is intended to be used only for testing purpose. - /// This function panics on nested call. - pub fn block_on(f: F) -> Result - where - F: Future, - { - block_on(f) - } } #[cfg(test)]