1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-23 22:51:07 +01:00

add test and main macros

This commit is contained in:
Nikolay Kim 2019-11-25 21:49:11 +06:00
parent 1fddd1e75b
commit 4ceac79f2c
25 changed files with 265 additions and 267 deletions

View File

@ -4,6 +4,7 @@ members = [
"actix-connect", "actix-connect",
"actix-ioframe", "actix-ioframe",
"actix-rt", "actix-rt",
"actix-macros",
"actix-service", "actix-service",
"actix-server", "actix-server",
"actix-server-config", "actix-server-config",
@ -18,6 +19,7 @@ actix-codec = { path = "actix-codec" }
actix-connect = { path = "actix-connect" } actix-connect = { path = "actix-connect" }
actix-ioframe = { path = "actix-ioframe" } actix-ioframe = { path = "actix-ioframe" }
actix-rt = { path = "actix-rt" } actix-rt = { path = "actix-rt" }
actix-macros = { path = "actix-macros" }
actix-server = { path = "actix-server" } actix-server = { path = "actix-server" }
actix-server-config = { path = "actix-server-config" } actix-server-config = { path = "actix-server-config" }
actix-service = { path = "actix-service" } actix-service = { path = "actix-service" }

View File

@ -3,7 +3,7 @@ use std::io;
use actix_codec::{BytesCodec, Framed}; use actix_codec::{BytesCodec, Framed};
use actix_server_config::Io; use actix_server_config::Io;
use actix_service::{service_fn, Service, ServiceFactory}; use actix_service::{service_fn, Service, ServiceFactory};
use actix_testing::{self as test, TestServer}; use actix_testing::TestServer;
use bytes::Bytes; use bytes::Bytes;
use futures::SinkExt; use futures::SinkExt;
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts}; use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
@ -11,8 +11,8 @@ use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
use actix_connect::Connect; use actix_connect::Connect;
#[cfg(feature = "openssl")] #[cfg(feature = "openssl")]
#[test] #[actix_rt::test]
fn test_string() { async fn test_string() {
let srv = TestServer::with(|| { let srv = TestServer::with(|| {
service_fn(|io: Io<tokio_net::tcp::TcpStream>| { service_fn(|io: Io<tokio_net::tcp::TcpStream>| {
async { async {
@ -25,13 +25,13 @@ fn test_string() {
let mut conn = actix_connect::default_connector(); let mut conn = actix_connect::default_connector();
let addr = format!("localhost:{}", srv.port()); let addr = format!("localhost:{}", srv.port());
let con = test::call_service(&mut conn, addr.into()); let con = conn.call(addr.into()).await.unwrap();
assert_eq!(con.peer_addr().unwrap(), srv.addr()); assert_eq!(con.peer_addr().unwrap(), srv.addr());
} }
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
#[test] #[actix_rt::test]
fn test_rustls_string() { async fn test_rustls_string() {
let srv = TestServer::with(|| { let srv = TestServer::with(|| {
service_fn(|io: Io<tokio_net::tcp::TcpStream>| { service_fn(|io: Io<tokio_net::tcp::TcpStream>| {
async { async {
@ -44,7 +44,7 @@ fn test_rustls_string() {
let mut conn = actix_connect::default_connector(); let mut conn = actix_connect::default_connector();
let addr = format!("localhost:{}", srv.port()); let addr = format!("localhost:{}", srv.port());
let con = test::call_service(&mut conn, addr.into()); let con = conn.call(addr.into()).await.unwrap();
assert_eq!(con.peer_addr().unwrap(), srv.addr()); assert_eq!(con.peer_addr().unwrap(), srv.addr());
} }
@ -72,8 +72,8 @@ async fn test_static_str() {
assert!(con.is_err()); assert!(con.is_err());
} }
#[test] #[actix_rt::test]
fn test_new_service() { async fn test_new_service() {
let srv = TestServer::with(|| { let srv = TestServer::with(|| {
service_fn(|io: Io<tokio_net::tcp::TcpStream>| { service_fn(|io: Io<tokio_net::tcp::TcpStream>| {
async { async {
@ -84,19 +84,19 @@ fn test_new_service() {
}) })
}); });
let resolver = test::block_on(async { let resolver =
actix_connect::start_resolver(ResolverConfig::default(), ResolverOpts::default()) actix_connect::start_resolver(ResolverConfig::default(), ResolverOpts::default());
});
let factory = test::block_on(async { actix_connect::new_connector_factory(resolver) });
let mut conn = test::block_on(factory.new_service(&())).unwrap(); let factory = actix_connect::new_connector_factory(resolver);
let con = test::block_on(conn.call(Connect::with("10", srv.addr()))).unwrap();
let mut conn = factory.new_service(&()).await.unwrap();
let con = conn.call(Connect::with("10", srv.addr())).await.unwrap();
assert_eq!(con.peer_addr().unwrap(), srv.addr()); assert_eq!(con.peer_addr().unwrap(), srv.addr());
} }
#[cfg(feature = "openssl")] #[cfg(feature = "openssl")]
#[test] #[actix_rt::test]
fn test_uri() { async fn test_uri() {
use http::HttpTryFrom; use http::HttpTryFrom;
let srv = TestServer::with(|| { let srv = TestServer::with(|| {
@ -111,13 +111,13 @@ fn test_uri() {
let mut conn = actix_connect::default_connector(); let mut conn = actix_connect::default_connector();
let addr = http::Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap(); let addr = http::Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap();
let con = test::call_service(&mut conn, addr.into()); let con = conn.call(addr.into()).await.unwrap();
assert_eq!(con.peer_addr().unwrap(), srv.addr()); assert_eq!(con.peer_addr().unwrap(), srv.addr());
} }
#[cfg(feature = "rustls")] #[cfg(feature = "rustls")]
#[test] #[actix_rt::test]
fn test_rustls_uri() { async fn test_rustls_uri() {
use http::HttpTryFrom; use http::HttpTryFrom;
let srv = TestServer::with(|| { let srv = TestServer::with(|| {
@ -132,6 +132,6 @@ fn test_rustls_uri() {
let mut conn = actix_connect::default_connector(); let mut conn = actix_connect::default_connector();
let addr = http::Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap(); let addr = http::Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap();
let con = test::call_service(&mut conn, addr.into()); let con = conn.call(addr.into()).await.unwrap();
assert_eq!(con.peer_addr().unwrap(), srv.addr()); assert_eq!(con.peer_addr().unwrap(), srv.addr());
} }

View File

@ -5,7 +5,7 @@ use std::time::Duration;
use actix_codec::BytesCodec; use actix_codec::BytesCodec;
use actix_server_config::Io; use actix_server_config::Io;
use actix_service::{apply_fn_factory, service_fn, Service}; use actix_service::{apply_fn_factory, service_fn, Service};
use actix_testing::{self as test, TestServer}; use actix_testing::TestServer;
use futures::future::ok; use futures::future::ok;
use tokio_net::tcp::TcpStream; use tokio_net::tcp::TcpStream;
use tokio_timer::delay_for; use tokio_timer::delay_for;
@ -14,8 +14,8 @@ use actix_ioframe::{Builder, Connect};
struct State; struct State;
#[test] #[actix_rt::test]
fn test_disconnect() -> std::io::Result<()> { async fn test_disconnect() -> std::io::Result<()> {
let disconnect = Arc::new(AtomicBool::new(false)); let disconnect = Arc::new(AtomicBool::new(false));
let disconnect1 = disconnect.clone(); let disconnect1 = disconnect.clone();
@ -43,14 +43,13 @@ fn test_disconnect() -> std::io::Result<()> {
}) })
.finish(service_fn(|_t| ok(None))); .finish(service_fn(|_t| ok(None)));
let conn = test::block_on( let conn = actix_connect::default_connector()
actix_connect::default_connector() .call(actix_connect::Connect::with(String::new(), srv.addr()))
.call(actix_connect::Connect::with(String::new(), srv.addr())), .await
) .unwrap();
.unwrap();
test::block_on(client.call(conn.into_parts().0)).unwrap(); client.call(conn.into_parts().0).await.unwrap();
let _ = test::block_on(delay_for(Duration::from_millis(100))); let _ = delay_for(Duration::from_millis(100)).await;
assert!(disconnect.load(Ordering::Relaxed)); assert!(disconnect.load(Ordering::Relaxed));
Ok(()) Ok(())

20
actix-macros/Cargo.toml Normal file
View File

@ -0,0 +1,20 @@
[package]
name = "actix-macros"
version = "0.1.0-alpha.1"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
repository = "https://github.com/actix/actix-net"
documentation = "https://docs.rs/actix-macros/"
categories = ["network-programming", "asynchronous"]
license = "MIT/Apache-2.0"
edition = "2018"
workspace = ".."
[lib]
proc-macro = true
[dependencies]
quote = "^1"
syn = { version = "^1", features = ["full"] }
[dev-dependencies]
actix-rt = { version = "1.0.0-alpha.1" }

96
actix-macros/src/lib.rs Normal file
View File

@ -0,0 +1,96 @@
//! Macros for use with Tokio
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
/// Marks async function to be executed by actix system.
///
/// ## Usage
///
/// ```rust
/// #[actix_rt::main]
/// async fn main() {
/// println!("Hello world");
/// }
/// ```
#[proc_macro_attribute]
#[cfg(not(test))] // Work around for rust-lang/rust#62127
pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(item as syn::ItemFn);
let ret = &input.sig.output;
let name = &input.sig.ident;
let inputs = &input.sig.inputs;
let body = &input.block;
let attrs = &input.attrs;
if input.sig.asyncness.is_none() {
return syn::Error::new_spanned(input.sig.fn_token, "only async fn is supported")
.to_compile_error()
.into();
}
(quote! {
#(#attrs)*
fn #name(#inputs) #ret {
actix_rt::System::new("main")
.block_on(async { #body })
}
})
.into()
}
/// Marks async test function to be executed by actix runtime.
///
/// ## Usage
///
/// ```no_run
/// #[actix_rt::test]
/// async fn my_test() {
/// assert!(true);
/// }
/// ```
#[proc_macro_attribute]
pub fn test(_: TokenStream, item: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(item as syn::ItemFn);
let ret = &input.sig.output;
let name = &input.sig.ident;
let body = &input.block;
let attrs = &input.attrs;
let mut has_test_attr = false;
for attr in attrs {
if attr.path.is_ident("test") {
has_test_attr = true;
}
}
if input.sig.asyncness.is_none() {
return syn::Error::new_spanned(input.sig.fn_token, "only async fn is supported")
.to_compile_error()
.into();
}
let result = if has_test_attr {
quote! {
#(#attrs)*
fn #name() #ret {
actix_rt::System::new("test")
.block_on(async { #body })
}
}
} else {
quote! {
#[test]
#(#attrs)*
fn #name() #ret {
actix_rt::System::new("test")
.block_on(async { #body })
}
}
};
result.into()
}

View File

@ -1,5 +1,19 @@
# Changes # Changes
## [1.0.0-alpha.2] - 2019-11-xx
Added
* Export `main` and `test` attribute macros
## [1.0.0-alpha.1] - 2019-11-22
### Changed
* Migrate to std::future and tokio 0.2
## [0.2.6] - 2019-11-14 ## [0.2.6] - 2019-11-14
### Fixed ### Fixed

View File

@ -18,13 +18,13 @@ name = "actix_rt"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
actix-macros = "0.1.0-alpha.1"
actix-threadpool = "0.2" actix-threadpool = "0.2"
futures = "0.3.1" futures = "0.3.1"
# TODO: Replace this with dependency on tokio-runtime once it is ready tokio = "=0.2.0-alpha.6"
tokio = { version = "0.2.0-alpha.6" }
tokio-timer = "=0.3.0-alpha.6"
tokio-executor = "=0.2.0-alpha.6" tokio-executor = "=0.2.0-alpha.6"
tokio-net = "=0.2.0-alpha.6" tokio-net = "=0.2.0-alpha.6"
tokio-timer = "=0.3.0-alpha.6"
copyless = "0.1.4" copyless = "0.1.4"

View File

@ -9,7 +9,7 @@ use std::{fmt, thread};
use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender}; use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
use futures::channel::oneshot::{channel, Canceled, Sender}; use futures::channel::oneshot::{channel, Canceled, Sender};
use futures::{future, Future, FutureExt, Stream}; use futures::{future, Future, FutureExt, Stream};
use tokio::runtime::current_thread::spawn; use tokio_executor::current_thread::spawn;
use crate::builder::Builder; use crate::builder::Builder;
use crate::system::System; use crate::system::System;

View File

@ -3,17 +3,16 @@ use std::io;
use futures::channel::mpsc::unbounded; use futures::channel::mpsc::unbounded;
use futures::channel::oneshot::{channel, Receiver}; use futures::channel::oneshot::{channel, Receiver};
use futures::future::{lazy, Future}; use futures::future::{lazy, Future, FutureExt};
use futures::{future, FutureExt};
use tokio::runtime::current_thread::Handle; use tokio::runtime::current_thread::Handle;
use tokio_executor::current_thread::CurrentThread;
use tokio_net::driver::Reactor; use tokio_net::driver::Reactor;
use tokio_timer::{clock::Clock, timer::Timer}; use tokio_timer::{clock::Clock, timer::Timer};
use crate::arbiter::{Arbiter, SystemArbiter}; use crate::arbiter::{Arbiter, SystemArbiter};
use crate::runtime::Runtime; use crate::runtime::Runtime;
use crate::system::System; use crate::system::System;
use tokio_executor::current_thread::CurrentThread;
/// Builder struct for a actix runtime. /// Builder struct for a actix runtime.
/// ///
@ -163,7 +162,7 @@ impl AsyncSystemRunner {
let AsyncSystemRunner { stop, .. } = self; let AsyncSystemRunner { stop, .. } = self;
// run loop // run loop
future::lazy(|_| { lazy(|_| {
Arbiter::run_system(); Arbiter::run_system();
async { async {
let res = match stop.await { let res = match stop.await {

View File

@ -1,5 +1,7 @@
//! A runtime implementation that runs everything on the current thread. //! A runtime implementation that runs everything on the current thread.
pub use actix_macros::{main, test};
mod arbiter; mod arbiter;
mod builder; mod builder;
mod runtime; mod runtime;

View File

@ -1,9 +1,9 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::future::Future;
use std::io; use std::io;
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
use futures::channel::mpsc::UnboundedSender; use futures::channel::mpsc::UnboundedSender;
use futures::Future;
use tokio::runtime::current_thread::Handle; use tokio::runtime::current_thread::Handle;
use crate::arbiter::{Arbiter, SystemCommand}; use crate::arbiter::{Arbiter, SystemCommand};

View File

@ -24,8 +24,7 @@ path = "src/lib.rs"
[dependencies] [dependencies]
futures = "0.3.1" futures = "0.3.1"
pin-project = "0.4.5" pin-project = "0.4.6"
[dev-dependencies] [dev-dependencies]
tokio = "0.2.0-alpha.6" actix-rt = "1.0.0-alpha.1"
# actix-rt = "1.0.0-alpha.1"

View File

@ -292,7 +292,7 @@ mod tests {
} }
} }
#[tokio::test] #[actix_rt::test]
async fn test_poll_ready() { async fn test_poll_ready() {
let cnt = Rc::new(Cell::new(0)); let cnt = Rc::new(Cell::new(0));
let mut srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt.clone())); let mut srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt.clone()));
@ -301,7 +301,7 @@ mod tests {
assert_eq!(cnt.get(), 2); assert_eq!(cnt.get(), 2);
} }
#[tokio::test] #[actix_rt::test]
async fn test_call() { async fn test_call() {
let cnt = Rc::new(Cell::new(0)); let cnt = Rc::new(Cell::new(0));
let mut srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt)); let mut srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt));
@ -310,7 +310,7 @@ mod tests {
assert_eq!(res.unwrap(), (("srv1", "srv2"))); assert_eq!(res.unwrap(), (("srv1", "srv2")));
} }
#[tokio::test] #[actix_rt::test]
async fn test_new_service() { async fn test_new_service() {
let cnt = Rc::new(Cell::new(0)); let cnt = Rc::new(Cell::new(0));
let cnt2 = cnt.clone(); let cnt2 = cnt.clone();

View File

@ -196,7 +196,7 @@ mod tests {
} }
} }
#[tokio::test] #[actix_rt::test]
async fn test_call() { async fn test_call() {
let mut srv = pipeline(apply_fn(Srv, |req: &'static str, srv| { let mut srv = pipeline(apply_fn(Srv, |req: &'static str, srv| {
let fut = srv.call(()); let fut = srv.call(());
@ -213,7 +213,7 @@ mod tests {
assert_eq!(res.unwrap(), (("srv", ()))); assert_eq!(res.unwrap(), (("srv", ())));
} }
#[tokio::test] #[actix_rt::test]
async fn test_new_service() { async fn test_new_service() {
let new_srv = pipeline_factory(apply_fn_factory( let new_srv = pipeline_factory(apply_fn_factory(
|| ok::<_, ()>(Srv), || ok::<_, ()>(Srv),

View File

@ -219,14 +219,14 @@ mod tests {
} }
} }
#[tokio::test] #[actix_rt::test]
async fn test_poll_ready() { async fn test_poll_ready() {
let mut srv = Srv.map(|_| "ok"); let mut srv = Srv.map(|_| "ok");
let res = lazy(|cx| srv.poll_ready(cx)).await; let res = lazy(|cx| srv.poll_ready(cx)).await;
assert_eq!(res, Poll::Ready(Ok(()))); assert_eq!(res, Poll::Ready(Ok(())));
} }
#[tokio::test] #[actix_rt::test]
async fn test_call() { async fn test_call() {
let mut srv = Srv.map(|_| "ok"); let mut srv = Srv.map(|_| "ok");
let res = srv.call(()).await; let res = srv.call(()).await;
@ -234,7 +234,7 @@ mod tests {
assert_eq!(res.unwrap(), "ok"); assert_eq!(res.unwrap(), "ok");
} }
#[tokio::test] #[actix_rt::test]
async fn test_new_service() { async fn test_new_service() {
let new_srv = (|| ok::<_, ()>(Srv)).into_factory().map(|_| "ok"); let new_srv = (|| ok::<_, ()>(Srv)).into_factory().map(|_| "ok");
let mut srv = new_srv.new_service(&()).await.unwrap(); let mut srv = new_srv.new_service(&()).await.unwrap();

View File

@ -221,14 +221,14 @@ mod tests {
} }
} }
#[tokio::test] #[actix_rt::test]
async fn test_poll_ready() { async fn test_poll_ready() {
let mut srv = Srv.map_err(|_| "error"); let mut srv = Srv.map_err(|_| "error");
let res = lazy(|cx| srv.poll_ready(cx)).await; let res = lazy(|cx| srv.poll_ready(cx)).await;
assert_eq!(res, Poll::Ready(Err("error"))); assert_eq!(res, Poll::Ready(Err("error")));
} }
#[tokio::test] #[actix_rt::test]
async fn test_call() { async fn test_call() {
let mut srv = Srv.map_err(|_| "error"); let mut srv = Srv.map_err(|_| "error");
let res = srv.call(()).await; let res = srv.call(()).await;
@ -236,7 +236,7 @@ mod tests {
assert_eq!(res.err().unwrap(), "error"); assert_eq!(res.err().unwrap(), "error");
} }
#[tokio::test] #[actix_rt::test]
async fn test_new_service() { async fn test_new_service() {
let new_srv = (|| ok::<_, ()>(Srv)).into_factory().map_err(|_| "error"); let new_srv = (|| ok::<_, ()>(Srv)).into_factory().map_err(|_| "error");
let mut srv = new_srv.new_service(&()).await.unwrap(); let mut srv = new_srv.new_service(&()).await.unwrap();

View File

@ -307,7 +307,7 @@ mod tests {
} }
} }
#[tokio::test] #[actix_rt::test]
async fn test_poll_ready() { async fn test_poll_ready() {
let cnt = Rc::new(Cell::new(0)); let cnt = Rc::new(Cell::new(0));
let mut srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt.clone())); let mut srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt.clone()));
@ -316,7 +316,7 @@ mod tests {
assert_eq!(cnt.get(), 2); assert_eq!(cnt.get(), 2);
} }
#[tokio::test] #[actix_rt::test]
async fn test_call() { async fn test_call() {
let cnt = Rc::new(Cell::new(0)); let cnt = Rc::new(Cell::new(0));
let mut srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt)); let mut srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt));
@ -330,7 +330,7 @@ mod tests {
assert_eq!(res.unwrap(), (("srv2", "err"))); assert_eq!(res.unwrap(), (("srv2", "err")));
} }
#[tokio::test] #[actix_rt::test]
async fn test_factory() { async fn test_factory() {
let cnt = Rc::new(Cell::new(0)); let cnt = Rc::new(Cell::new(0));
let cnt2 = cnt.clone(); let cnt2 = cnt.clone();

View File

@ -1,5 +1,14 @@
# Changes # Changes
## [0.3.0-alpha.2] - 2019-11-xx
* Re-export `test` attribute macros
## [0.3.0-alpha.1] - 2019-11-22
* Migrate to std::future
## [0.2.0] - 2019-10-14 ## [0.2.0] - 2019-10-14
* Upgrade actix-server and actix-server-config deps * Upgrade actix-server and actix-server-config deps

View File

@ -1,6 +1,6 @@
[package] [package]
name = "actix-testing" name = "actix-testing"
version = "0.3.0-alpha.1" version = "0.3.0-alpha.2"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix testing utils" description = "Actix testing utils"
keywords = ["network", "framework", "async", "futures"] keywords = ["network", "framework", "async", "futures"]
@ -18,6 +18,7 @@ path = "src/lib.rs"
[dependencies] [dependencies]
actix-rt = "1.0.0-alpha.1" actix-rt = "1.0.0-alpha.1"
actix-macros = "0.1.0-alpha.1"
actix-server = "0.8.0-alpha.1" actix-server = "0.8.0-alpha.1"
actix-server-config = "0.3.0-alpha.1" actix-server-config = "0.3.0-alpha.1"
actix-service = "1.0.0-alpha.1" actix-service = "1.0.0-alpha.1"
@ -25,5 +26,4 @@ actix-service = "1.0.0-alpha.1"
log = "0.4" log = "0.4"
net2 = "0.2" net2 = "0.2"
futures = "0.3.1" futures = "0.3.1"
tokio = "0.2.0-alpha.6"
tokio-net = { version = "0.2.0-alpha.6" } tokio-net = { version = "0.2.0-alpha.6" }

View File

@ -10,8 +10,7 @@ use net2::TcpBuilder;
use tokio_net::driver::Handle; use tokio_net::driver::Handle;
use tokio_net::tcp::TcpStream; use tokio_net::tcp::TcpStream;
mod rt; pub use actix_macros::test;
pub use self::rt::*;
/// The `TestServer` type. /// The `TestServer` type.
/// ///

View File

@ -1,117 +0,0 @@
//! Various helpers for Actix applications to use during testing.
use std::cell::RefCell;
use std::future::Future;
use actix_rt::{System, SystemRunner};
use actix_service::Service;
use futures::future::{lazy, FutureExt};
// use futures_util::future::FutureExt;
thread_local! {
static RT: RefCell<Inner> = {
RefCell::new(Inner(Some(System::builder().build())))
};
}
struct Inner(Option<SystemRunner>);
impl Inner {
fn get_mut(&mut self) -> &mut SystemRunner {
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) -> F::Output
where
F: Future,
{
RT.with(move |rt| rt.borrow_mut().get_mut().block_on(f))
}
/// Runs the provided function, blocking the current thread until the result
/// 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) -> R::Output
where
F: FnOnce() -> R,
R: Future,
{
RT.with(move |rt| {
let mut rt = rt.borrow_mut();
let fut = rt.get_mut().block_on(lazy(|_| f()));
rt.get_mut().block_on(fut)
})
}
/// Spawn future to the current test runtime.
pub fn spawn<F>(fut: F)
where
F: Future + 'static,
{
run_on(move || {
actix_rt::spawn(fut.map(|_| ()));
});
}
/// Runs the provided function, with runtime enabled.
///
/// Note that this function is intended to be used only for testing purpose.
/// This function panics on nested call.
pub fn run_on<F, R>(f: F) -> R
where
F: FnOnce() -> R,
{
RT.with(move |rt| rt.borrow_mut().get_mut().block_on(lazy(|_| f())))
}
/// Calls service and waits for response future completion.
///
/// ```rust,ignore
/// use actix_web::{test, App, HttpResponse, http::StatusCode};
/// use actix_service::Service;
///
/// #[test]
/// fn test_response() {
/// let mut app = test::init_service(
/// App::new()
/// .service(web::resource("/test").to(|| HttpResponse::Ok()))
/// );
///
/// // Create request object
/// let req = test::TestRequest::with_uri("/test").to_request();
///
/// // Call application
/// let resp = test::call_service(&mut app, req);
/// assert_eq!(resp.status(), StatusCode::OK);
/// }
/// ```
pub fn call_service<S, R>(app: &mut S, req: R) -> S::Response
where
S: Service<Request = R>,
S::Error: std::fmt::Debug,
{
block_on(run_on(move || app.call(req))).unwrap()
}

View File

@ -137,36 +137,33 @@ mod tests {
} }
} }
#[test] #[actix_rt::test]
fn test_transform() { async fn test_transform() {
let wait_time = Duration::from_millis(50); let wait_time = Duration::from_millis(50);
let _ = actix_rt::System::new("test").block_on(async {
let mut srv = InFlightService::new(1, SleepService(wait_time));
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
let res = srv.call(()); let mut srv = InFlightService::new(1, SleepService(wait_time));
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Pending); assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
let _ = res.await; let res = srv.call(());
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(()))); assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Pending);
});
let _ = res.await;
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
} }
#[test] #[actix_rt::test]
fn test_newtransform() { async fn test_newtransform() {
let wait_time = Duration::from_millis(50); let wait_time = Duration::from_millis(50);
actix_rt::System::new("test").block_on(async { let srv = apply(InFlight::new(1), factory_fn(|| ok(SleepService(wait_time))));
let srv = apply(InFlight::new(1), factory_fn(|| ok(SleepService(wait_time))));
let mut srv = srv.new_service(&()).await.unwrap(); let mut srv = srv.new_service(&()).await.unwrap();
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(()))); assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
let res = srv.call(()); let res = srv.call(());
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Pending); assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Pending);
let _ = res.await; let _ = res.await;
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(()))); assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
});
} }
} }

View File

@ -230,8 +230,8 @@ mod tests {
} }
} }
#[test] #[actix_rt::test]
fn test_inorder() { async fn test_inorder() {
let (tx1, rx1) = oneshot::channel(); let (tx1, rx1) = oneshot::channel();
let (tx2, rx2) = oneshot::channel(); let (tx2, rx2) = oneshot::channel();
let (tx3, rx3) = oneshot::channel(); let (tx3, rx3) = oneshot::channel();
@ -269,7 +269,7 @@ mod tests {
let _ = tx2.send(2); let _ = tx2.send(2);
let _ = tx1.send(1); let _ = tx1.send(1);
let _ = actix_rt::System::new("test").block_on(rx_stop); let _ = rx_stop.await;
let _ = h.join(); let _ = h.join();
} }
} }

View File

@ -161,77 +161,65 @@ mod tests {
/// State Under Test: Two calls of `SystemTimeService::now()` return the same value if they are done within resolution interval of `SystemTimeService`. /// State Under Test: Two calls of `SystemTimeService::now()` return the same value if they are done within resolution interval of `SystemTimeService`.
/// ///
/// Expected Behavior: Two back-to-back calls of `SystemTimeService::now()` return the same value. /// Expected Behavior: Two back-to-back calls of `SystemTimeService::now()` return the same value.
#[test] #[actix_rt::test]
fn system_time_service_time_does_not_immediately_change() { async fn system_time_service_time_does_not_immediately_change() {
let resolution = Duration::from_millis(50); let resolution = Duration::from_millis(50);
let _ = actix_rt::System::new("test").block_on(async { let time_service = SystemTimeService::with(resolution);
let time_service = SystemTimeService::with(resolution); assert_eq!(time_service.now(), time_service.now());
assert_eq!(time_service.now(), time_service.now());
});
} }
/// State Under Test: Two calls of `LowResTimeService::now()` return the same value if they are done within resolution interval of `SystemTimeService`. /// State Under Test: Two calls of `LowResTimeService::now()` return the same value if they are done within resolution interval of `SystemTimeService`.
/// ///
/// Expected Behavior: Two back-to-back calls of `LowResTimeService::now()` return the same value. /// Expected Behavior: Two back-to-back calls of `LowResTimeService::now()` return the same value.
#[test] #[actix_rt::test]
fn lowres_time_service_time_does_not_immediately_change() { async fn lowres_time_service_time_does_not_immediately_change() {
let resolution = Duration::from_millis(50); let resolution = Duration::from_millis(50);
let time_service = LowResTimeService::with(resolution);
let _ = actix_rt::System::new("test").block_on(async { assert_eq!(time_service.now(), time_service.now());
let time_service = LowResTimeService::with(resolution);
assert_eq!(time_service.now(), time_service.now());
});
} }
/// State Under Test: `SystemTimeService::now()` updates returned value every resolution period. /// State Under Test: `SystemTimeService::now()` updates returned value every resolution period.
/// ///
/// Expected Behavior: Two calls of `LowResTimeService::now()` made in subsequent resolution interval return different values /// Expected Behavior: Two calls of `LowResTimeService::now()` made in subsequent resolution interval return different values
/// and second value is greater than the first one at least by a resolution interval. /// and second value is greater than the first one at least by a resolution interval.
#[test] #[actix_rt::test]
fn system_time_service_time_updates_after_resolution_interval() { async fn system_time_service_time_updates_after_resolution_interval() {
let resolution = Duration::from_millis(100); let resolution = Duration::from_millis(100);
let wait_time = Duration::from_millis(150); let wait_time = Duration::from_millis(150);
actix_rt::System::new("test").block_on(async { let time_service = SystemTimeService::with(resolution);
let time_service = SystemTimeService::with(resolution);
let first_time = time_service let first_time = time_service
.now() .now()
.duration_since(SystemTime::UNIX_EPOCH) .duration_since(SystemTime::UNIX_EPOCH)
.unwrap(); .unwrap();
delay_for(wait_time).await; delay_for(wait_time).await;
let second_time = time_service let second_time = time_service
.now() .now()
.duration_since(SystemTime::UNIX_EPOCH) .duration_since(SystemTime::UNIX_EPOCH)
.unwrap(); .unwrap();
assert!(second_time - first_time >= wait_time); assert!(second_time - first_time >= wait_time);
});
} }
/// State Under Test: `LowResTimeService::now()` updates returned value every resolution period. /// State Under Test: `LowResTimeService::now()` updates returned value every resolution period.
/// ///
/// Expected Behavior: Two calls of `LowResTimeService::now()` made in subsequent resolution interval return different values /// Expected Behavior: Two calls of `LowResTimeService::now()` made in subsequent resolution interval return different values
/// and second value is greater than the first one at least by a resolution interval. /// and second value is greater than the first one at least by a resolution interval.
#[test] #[actix_rt::test]
fn lowres_time_service_time_updates_after_resolution_interval() { async fn lowres_time_service_time_updates_after_resolution_interval() {
let resolution = Duration::from_millis(100); let resolution = Duration::from_millis(100);
let wait_time = Duration::from_millis(150); let wait_time = Duration::from_millis(150);
let time_service = LowResTimeService::with(resolution);
let _ = actix_rt::System::new("test").block_on(async { let first_time = time_service.now();
let time_service = LowResTimeService::with(resolution);
let first_time = time_service.now(); delay_for(wait_time).await;
delay_for(wait_time).await; let second_time = time_service.now();
assert!(second_time - first_time >= wait_time);
let second_time = time_service.now();
assert!(second_time - first_time >= wait_time);
});
} }
} }

View File

@ -204,44 +204,35 @@ mod tests {
} }
} }
#[test] #[actix_rt::test]
fn test_success() { async fn test_success() {
let resolution = Duration::from_millis(100); let resolution = Duration::from_millis(100);
let wait_time = Duration::from_millis(50); let wait_time = Duration::from_millis(50);
let res = actix_rt::System::new("test").block_on(async { let mut timeout = TimeoutService::new(resolution, SleepService(wait_time));
let mut timeout = TimeoutService::new(resolution, SleepService(wait_time)); assert_eq!(timeout.call(()).await, Ok(()));
timeout.call(()).await
});
assert_eq!(res, Ok(()));
} }
#[test] #[actix_rt::test]
fn test_timeout() { async fn test_timeout() {
let resolution = Duration::from_millis(100); let resolution = Duration::from_millis(100);
let wait_time = Duration::from_millis(150); let wait_time = Duration::from_millis(150);
let res = actix_rt::System::new("test").block_on(async { let mut timeout = TimeoutService::new(resolution, SleepService(wait_time));
let mut timeout = TimeoutService::new(resolution, SleepService(wait_time)); assert_eq!(timeout.call(()).await, Err(TimeoutError::Timeout));
timeout.call(()).await
});
assert_eq!(res, Err(TimeoutError::Timeout));
} }
#[test] #[actix_rt::test]
fn test_timeout_newservice() { async fn test_timeout_newservice() {
let resolution = Duration::from_millis(100); let resolution = Duration::from_millis(100);
let wait_time = Duration::from_millis(150); let wait_time = Duration::from_millis(150);
let res = actix_rt::System::new("test").block_on(async { let timeout = apply(
let timeout = apply( Timeout::new(resolution),
Timeout::new(resolution), factory_fn(|| ok::<_, ()>(SleepService(wait_time))),
factory_fn(|| ok::<_, ()>(SleepService(wait_time))), );
); let mut srv = timeout.new_service(&()).await.unwrap();
let mut srv = timeout.new_service(&()).await.unwrap();
srv.call(()).await assert_eq!(srv.call(()).await, Err(TimeoutError::Timeout));
});
assert_eq!(res, Err(TimeoutError::Timeout));
} }
} }