1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-08-31 17:07:01 +02:00

Add helper function for executing futures test::block_fn()

This commit is contained in:
Nikolay Kim
2019-04-29 09:34:14 -07:00
parent 29a841529f
commit f4b4875cb1
3 changed files with 30 additions and 6 deletions

View File

@@ -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.