1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 22:49:21 +02:00

migrate awc and test-server to std::future

This commit is contained in:
Nikolay Kim
2019-11-19 09:55:17 +06:00
parent a6a2d2f444
commit 5ab29b2e62
12 changed files with 376 additions and 397 deletions

View File

@ -14,7 +14,7 @@ categories = ["network-programming", "asynchronous",
license = "MIT/Apache-2.0"
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
edition = "2018"
workspace = ".."
# workspace = ".."
[package.metadata.docs.rs]
features = []
@ -27,20 +27,22 @@ path = "src/lib.rs"
default = []
# openssl
ssl = ["openssl", "actix-server/ssl", "awc/ssl"]
openssl = ["open-ssl", "actix-server/openssl", "awc/openssl"]
[dependencies]
actix-codec = "0.1.2"
actix-rt = "0.2.2"
actix-service = "0.4.1"
actix-server = "0.6.0"
actix-utils = "0.4.1"
awc = "0.2.6"
actix-connect = "0.2.2"
actix-service = "1.0.0-alpha.1"
actix-codec = "0.2.0-alpha.1"
actix-connect = "1.0.0-alpha.1"
actix-utils = "0.5.0-alpha.1"
actix-rt = "1.0.0-alpha.1"
actix-server = "0.8.0-alpha.1"
actix-server-config = "0.3.0-alpha.1"
actix-testing = "0.3.0-alpha.1"
awc = "0.3.0-alpha.1"
base64 = "0.10"
bytes = "0.4"
futures = "0.1"
futures = "0.3.1"
http = "0.1.8"
log = "0.4"
env_logger = "0.6"
@ -51,10 +53,25 @@ sha1 = "0.6"
slab = "0.4"
serde_urlencoded = "0.6.1"
time = "0.1"
tokio-tcp = "0.1"
tokio-timer = "0.2"
openssl = { version="0.10", optional = true }
tokio-net = "0.2.0-alpha.6"
tokio-timer = "0.3.0-alpha.6"
open-ssl = { version="0.10", package="openssl", optional = true }
[dev-dependencies]
actix-web = "1.0.7"
actix-http = "0.2.9"
#actix-web = "1.0.7"
actix-http = "0.3.0-alpha.1"
[patch.crates-io]
actix-http = { path = "../actix-http" }
awc = { path = "../awc" }
actix-codec = { path = "../../actix-net/actix-codec" }
actix-connect = { path = "../../actix-net/actix-connect" }
actix-rt = { path = "../../actix-net/actix-rt" }
actix-server = { path = "../../actix-net/actix-server" }
actix-server-config = { path = "../../actix-net/actix-server-config" }
actix-service = { path = "../../actix-net/actix-service" }
actix-testing = { path = "../../actix-net/actix-testing" }
actix-threadpool = { path = "../../actix-net/actix-threadpool" }
actix-utils = { path = "../../actix-net/actix-utils" }

View File

@ -1,73 +1,18 @@
//! Various helpers for Actix applications to use during testing.
use std::cell::RefCell;
use std::sync::mpsc;
use std::{net, thread, time};
use actix_codec::{AsyncRead, AsyncWrite, Framed};
use actix_rt::{Runtime, System};
use actix_server::{Server, StreamServiceFactory};
use actix_rt::{System};
use actix_server::{Server, ServiceFactory};
use awc::{error::PayloadError, ws, Client, ClientRequest, ClientResponse, Connector};
use bytes::Bytes;
use futures::future::lazy;
use futures::{Future, IntoFuture, Stream};
use futures::{Stream, future::lazy};
use http::Method;
use net2::TcpBuilder;
use tokio_tcp::TcpStream;
use tokio_net::tcp::TcpStream;
thread_local! {
static RT: RefCell<Inner> = {
RefCell::new(Inner(Some(Runtime::new().unwrap())))
};
}
struct Inner(Option<Runtime>);
impl Inner {
fn get_mut(&mut self) -> &mut Runtime {
self.0.as_mut().unwrap()
}
}
impl Drop for Inner {
fn drop(&mut self) {
std::mem::forget(self.0.take().unwrap())
}
}
/// 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: F) -> Result<F::Item, F::Error>
where
F: IntoFuture,
{
RT.with(move |rt| rt.borrow_mut().get_mut().block_on(f.into_future()))
}
/// 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().get_mut().block_on(lazy(f)))
}
pub use actix_testing::*;
/// The `TestServer` type.
///
@ -110,7 +55,7 @@ pub struct TestServerRuntime {
impl TestServer {
#[allow(clippy::new_ret_no_self)]
/// Start new test server with application factory
pub fn new<F: StreamServiceFactory<TcpStream>>(factory: F) -> TestServerRuntime {
pub fn new<F: ServiceFactory<TcpStream>>(factory: F) -> TestServerRuntime {
let (tx, rx) = mpsc::channel();
// run server in separate thread
@ -131,7 +76,7 @@ impl TestServer {
let (system, addr) = rx.recv().unwrap();
let client = block_on(lazy(move || {
let client = block_on(lazy(move |_| {
let connector = {
#[cfg(feature = "ssl")]
{
@ -161,9 +106,9 @@ impl TestServer {
}))
.unwrap();
block_on(lazy(
|| Ok::<_, ()>(actix_connect::start_default_resolver()),
))
block_on(lazy(|_| {
Ok::<_, ()>(actix_connect::start_default_resolver())
}))
.unwrap();
TestServerRuntime {
@ -185,31 +130,6 @@ impl TestServer {
}
impl TestServerRuntime {
/// Execute future on current core
pub fn block_on<F, I, E>(&mut self, fut: F) -> Result<I, E>
where
F: Future<Item = I, Error = E>,
{
block_on(fut)
}
/// Execute future on current core
pub fn block_on_fn<F, R>(&mut self, f: F) -> Result<R::Item, R::Error>
where
F: FnOnce() -> R,
R: Future,
{
block_on(lazy(f))
}
/// Execute function on current core
pub fn execute<F, R>(&mut self, fut: F) -> R
where
F: FnOnce() -> R,
{
block_on(lazy(|| Ok::<_, ()>(fut()))).unwrap()
}
/// Construct test server url
pub fn addr(&self) -> net::SocketAddr {
self.addr
@ -313,9 +233,9 @@ impl TestServerRuntime {
mut response: ClientResponse<S>,
) -> Result<Bytes, PayloadError>
where
S: Stream<Item = Bytes, Error = PayloadError> + 'static,
S: Stream<Item = Result<Bytes, PayloadError>> + Unpin + 'static,
{
self.block_on(response.body().limit(10_485_760))
block_on(response.body().limit(10_485_760))
}
/// Connect to websocket server at a given path
@ -326,7 +246,9 @@ impl TestServerRuntime {
{
let url = self.url(path);
let connect = self.client.ws(url).connect();
block_on(lazy(move || connect.map(|(_, framed)| framed)))
block_on(async move {
connect.await.map(|(_, framed)| framed)
})
}
/// Connect to a websocket server