mirror of
https://github.com/actix/actix-web.git
synced 2024-12-30 12:00:38 +00:00
Add helper function for executing futures test::block_fn()
This commit is contained in:
parent
29a841529f
commit
f4b4875cb1
3 changed files with 30 additions and 6 deletions
|
@ -1,5 +1,10 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* Add helper function for executing futures `test::block_fn()`
|
||||||
|
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
* Allow to construct `Data` instances to avoid double `Arc` for `Send + Sync` types.
|
* Allow to construct `Data` instances to avoid double `Arc` for `Send + Sync` types.
|
||||||
|
|
|
@ -445,7 +445,9 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::http::{header, HeaderValue, Method, StatusCode};
|
use crate::http::{header, HeaderValue, Method, StatusCode};
|
||||||
use crate::service::{ServiceRequest, ServiceResponse};
|
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};
|
use crate::{web, Error, HttpRequest, HttpResponse};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -454,7 +456,7 @@ mod tests {
|
||||||
App::new().service(web::resource("/test").to(|| HttpResponse::Ok())),
|
App::new().service(web::resource("/test").to(|| HttpResponse::Ok())),
|
||||||
);
|
);
|
||||||
let req = TestRequest::with_uri("/test").to_request();
|
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);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/blah").to_request();
|
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_server_config::ServerConfig;
|
||||||
use actix_service::{FnService, IntoNewService, NewService, Service};
|
use actix_service::{FnService, IntoNewService, NewService, Service};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures::{
|
use futures::future::{lazy, ok, Future, IntoFuture};
|
||||||
future::{lazy, ok, Future},
|
use futures::Stream;
|
||||||
stream::Stream,
|
|
||||||
};
|
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
|
||||||
|
@ -52,6 +50,25 @@ where
|
||||||
RT.with(move |rt| rt.borrow_mut().block_on(f))
|
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.
|
/// Runs the provided function, with runtime enabled.
|
||||||
///
|
///
|
||||||
/// Note that this function is intended to be used only for testing purpose.
|
/// Note that this function is intended to be used only for testing purpose.
|
||||||
|
|
Loading…
Reference in a new issue