mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 00:21:08 +01:00
Add helper function for executing futures test::block_fn()
This commit is contained in:
parent
29a841529f
commit
f4b4875cb1
@ -1,5 +1,10 @@
|
||||
# Changes
|
||||
|
||||
### Added
|
||||
|
||||
* Add helper function for executing futures `test::block_fn()`
|
||||
|
||||
|
||||
### Changed
|
||||
|
||||
* Allow to construct `Data` instances to avoid double `Arc` for `Send + Sync` types.
|
||||
|
@ -445,7 +445,9 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::http::{header, HeaderValue, Method, StatusCode};
|
||||
use crate::service::{ServiceRequest, ServiceResponse};
|
||||
use crate::test::{block_on, call_service, init_service, read_body, TestRequest};
|
||||
use crate::test::{
|
||||
block_fn, block_on, call_service, init_service, read_body, TestRequest,
|
||||
};
|
||||
use crate::{web, Error, HttpRequest, HttpResponse};
|
||||
|
||||
#[test]
|
||||
@ -454,7 +456,7 @@ mod tests {
|
||||
App::new().service(web::resource("/test").to(|| HttpResponse::Ok())),
|
||||
);
|
||||
let req = TestRequest::with_uri("/test").to_request();
|
||||
let resp = block_on(srv.call(req)).unwrap();
|
||||
let resp = block_fn(|| srv.call(req)).unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
let req = TestRequest::with_uri("/blah").to_request();
|
||||
|
25
src/test.rs
25
src/test.rs
@ -12,10 +12,8 @@ use actix_rt::Runtime;
|
||||
use actix_server_config::ServerConfig;
|
||||
use actix_service::{FnService, IntoNewService, NewService, Service};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use futures::{
|
||||
future::{lazy, ok, Future},
|
||||
stream::Stream,
|
||||
};
|
||||
use futures::future::{lazy, ok, Future, IntoFuture};
|
||||
use futures::Stream;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde_json;
|
||||
|
||||
@ -52,6 +50,25 @@ where
|
||||
RT.with(move |rt| rt.borrow_mut().block_on(f))
|
||||
}
|
||||
|
||||
/// Runs the provided function, blocking the current thread until the resul
|
||||
/// 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_fn<F, R>(f: F) -> Result<R::Item, R::Error>
|
||||
where
|
||||
F: FnOnce() -> R,
|
||||
R: IntoFuture,
|
||||
{
|
||||
RT.with(move |rt| rt.borrow_mut().block_on(f().into_future()))
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
/// Runs the provided function, with runtime enabled.
|
||||
///
|
||||
/// Note that this function is intended to be used only for testing purpose.
|
||||
|
Loading…
Reference in New Issue
Block a user