mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-23 19:51:06 +01:00
add test and main macros
This commit is contained in:
parent
1fddd1e75b
commit
4ceac79f2c
@ -4,6 +4,7 @@ members = [
|
||||
"actix-connect",
|
||||
"actix-ioframe",
|
||||
"actix-rt",
|
||||
"actix-macros",
|
||||
"actix-service",
|
||||
"actix-server",
|
||||
"actix-server-config",
|
||||
@ -18,6 +19,7 @@ actix-codec = { path = "actix-codec" }
|
||||
actix-connect = { path = "actix-connect" }
|
||||
actix-ioframe = { path = "actix-ioframe" }
|
||||
actix-rt = { path = "actix-rt" }
|
||||
actix-macros = { path = "actix-macros" }
|
||||
actix-server = { path = "actix-server" }
|
||||
actix-server-config = { path = "actix-server-config" }
|
||||
actix-service = { path = "actix-service" }
|
||||
|
@ -3,7 +3,7 @@ use std::io;
|
||||
use actix_codec::{BytesCodec, Framed};
|
||||
use actix_server_config::Io;
|
||||
use actix_service::{service_fn, Service, ServiceFactory};
|
||||
use actix_testing::{self as test, TestServer};
|
||||
use actix_testing::TestServer;
|
||||
use bytes::Bytes;
|
||||
use futures::SinkExt;
|
||||
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
||||
@ -11,8 +11,8 @@ use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
||||
use actix_connect::Connect;
|
||||
|
||||
#[cfg(feature = "openssl")]
|
||||
#[test]
|
||||
fn test_string() {
|
||||
#[actix_rt::test]
|
||||
async fn test_string() {
|
||||
let srv = TestServer::with(|| {
|
||||
service_fn(|io: Io<tokio_net::tcp::TcpStream>| {
|
||||
async {
|
||||
@ -25,13 +25,13 @@ fn test_string() {
|
||||
|
||||
let mut conn = actix_connect::default_connector();
|
||||
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());
|
||||
}
|
||||
|
||||
#[cfg(feature = "rustls")]
|
||||
#[test]
|
||||
fn test_rustls_string() {
|
||||
#[actix_rt::test]
|
||||
async fn test_rustls_string() {
|
||||
let srv = TestServer::with(|| {
|
||||
service_fn(|io: Io<tokio_net::tcp::TcpStream>| {
|
||||
async {
|
||||
@ -44,7 +44,7 @@ fn test_rustls_string() {
|
||||
|
||||
let mut conn = actix_connect::default_connector();
|
||||
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());
|
||||
}
|
||||
|
||||
@ -72,8 +72,8 @@ async fn test_static_str() {
|
||||
assert!(con.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_service() {
|
||||
#[actix_rt::test]
|
||||
async fn test_new_service() {
|
||||
let srv = TestServer::with(|| {
|
||||
service_fn(|io: Io<tokio_net::tcp::TcpStream>| {
|
||||
async {
|
||||
@ -84,19 +84,19 @@ fn test_new_service() {
|
||||
})
|
||||
});
|
||||
|
||||
let resolver = test::block_on(async {
|
||||
actix_connect::start_resolver(ResolverConfig::default(), ResolverOpts::default())
|
||||
});
|
||||
let factory = test::block_on(async { actix_connect::new_connector_factory(resolver) });
|
||||
let resolver =
|
||||
actix_connect::start_resolver(ResolverConfig::default(), ResolverOpts::default());
|
||||
|
||||
let mut conn = test::block_on(factory.new_service(&())).unwrap();
|
||||
let con = test::block_on(conn.call(Connect::with("10", srv.addr()))).unwrap();
|
||||
let factory = actix_connect::new_connector_factory(resolver);
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
#[cfg(feature = "openssl")]
|
||||
#[test]
|
||||
fn test_uri() {
|
||||
#[actix_rt::test]
|
||||
async fn test_uri() {
|
||||
use http::HttpTryFrom;
|
||||
|
||||
let srv = TestServer::with(|| {
|
||||
@ -111,13 +111,13 @@ fn test_uri() {
|
||||
|
||||
let mut conn = actix_connect::default_connector();
|
||||
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());
|
||||
}
|
||||
|
||||
#[cfg(feature = "rustls")]
|
||||
#[test]
|
||||
fn test_rustls_uri() {
|
||||
#[actix_rt::test]
|
||||
async fn test_rustls_uri() {
|
||||
use http::HttpTryFrom;
|
||||
|
||||
let srv = TestServer::with(|| {
|
||||
@ -132,6 +132,6 @@ fn test_rustls_uri() {
|
||||
|
||||
let mut conn = actix_connect::default_connector();
|
||||
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());
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ use std::time::Duration;
|
||||
use actix_codec::BytesCodec;
|
||||
use actix_server_config::Io;
|
||||
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 tokio_net::tcp::TcpStream;
|
||||
use tokio_timer::delay_for;
|
||||
@ -14,8 +14,8 @@ use actix_ioframe::{Builder, Connect};
|
||||
|
||||
struct State;
|
||||
|
||||
#[test]
|
||||
fn test_disconnect() -> std::io::Result<()> {
|
||||
#[actix_rt::test]
|
||||
async fn test_disconnect() -> std::io::Result<()> {
|
||||
let disconnect = Arc::new(AtomicBool::new(false));
|
||||
let disconnect1 = disconnect.clone();
|
||||
|
||||
@ -43,14 +43,13 @@ fn test_disconnect() -> std::io::Result<()> {
|
||||
})
|
||||
.finish(service_fn(|_t| ok(None)));
|
||||
|
||||
let conn = test::block_on(
|
||||
actix_connect::default_connector()
|
||||
.call(actix_connect::Connect::with(String::new(), srv.addr())),
|
||||
)
|
||||
.unwrap();
|
||||
let conn = actix_connect::default_connector()
|
||||
.call(actix_connect::Connect::with(String::new(), srv.addr()))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
test::block_on(client.call(conn.into_parts().0)).unwrap();
|
||||
let _ = test::block_on(delay_for(Duration::from_millis(100)));
|
||||
client.call(conn.into_parts().0).await.unwrap();
|
||||
let _ = delay_for(Duration::from_millis(100)).await;
|
||||
assert!(disconnect.load(Ordering::Relaxed));
|
||||
|
||||
Ok(())
|
||||
|
20
actix-macros/Cargo.toml
Normal file
20
actix-macros/Cargo.toml
Normal 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
96
actix-macros/src/lib.rs
Normal 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()
|
||||
}
|
@ -1,5 +1,19 @@
|
||||
# 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
|
||||
|
||||
### Fixed
|
||||
|
@ -18,13 +18,13 @@ name = "actix_rt"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
actix-macros = "0.1.0-alpha.1"
|
||||
actix-threadpool = "0.2"
|
||||
futures = "0.3.1"
|
||||
|
||||
# TODO: Replace this with dependency on tokio-runtime once it is ready
|
||||
tokio = { version = "0.2.0-alpha.6" }
|
||||
tokio-timer = "=0.3.0-alpha.6"
|
||||
tokio = "=0.2.0-alpha.6"
|
||||
tokio-executor = "=0.2.0-alpha.6"
|
||||
tokio-net = "=0.2.0-alpha.6"
|
||||
tokio-timer = "=0.3.0-alpha.6"
|
||||
|
||||
copyless = "0.1.4"
|
||||
|
@ -9,7 +9,7 @@ use std::{fmt, thread};
|
||||
use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
|
||||
use futures::channel::oneshot::{channel, Canceled, Sender};
|
||||
use futures::{future, Future, FutureExt, Stream};
|
||||
use tokio::runtime::current_thread::spawn;
|
||||
use tokio_executor::current_thread::spawn;
|
||||
|
||||
use crate::builder::Builder;
|
||||
use crate::system::System;
|
||||
|
@ -3,17 +3,16 @@ use std::io;
|
||||
|
||||
use futures::channel::mpsc::unbounded;
|
||||
use futures::channel::oneshot::{channel, Receiver};
|
||||
use futures::future::{lazy, Future};
|
||||
use futures::{future, FutureExt};
|
||||
use futures::future::{lazy, Future, FutureExt};
|
||||
|
||||
use tokio::runtime::current_thread::Handle;
|
||||
use tokio_executor::current_thread::CurrentThread;
|
||||
use tokio_net::driver::Reactor;
|
||||
use tokio_timer::{clock::Clock, timer::Timer};
|
||||
|
||||
use crate::arbiter::{Arbiter, SystemArbiter};
|
||||
use crate::runtime::Runtime;
|
||||
use crate::system::System;
|
||||
use tokio_executor::current_thread::CurrentThread;
|
||||
|
||||
/// Builder struct for a actix runtime.
|
||||
///
|
||||
@ -163,7 +162,7 @@ impl AsyncSystemRunner {
|
||||
let AsyncSystemRunner { stop, .. } = self;
|
||||
|
||||
// run loop
|
||||
future::lazy(|_| {
|
||||
lazy(|_| {
|
||||
Arbiter::run_system();
|
||||
async {
|
||||
let res = match stop.await {
|
||||
|
@ -1,5 +1,7 @@
|
||||
//! A runtime implementation that runs everything on the current thread.
|
||||
|
||||
pub use actix_macros::{main, test};
|
||||
|
||||
mod arbiter;
|
||||
mod builder;
|
||||
mod runtime;
|
||||
|
@ -1,9 +1,9 @@
|
||||
use std::cell::RefCell;
|
||||
use std::future::Future;
|
||||
use std::io;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
use futures::channel::mpsc::UnboundedSender;
|
||||
use futures::Future;
|
||||
use tokio::runtime::current_thread::Handle;
|
||||
|
||||
use crate::arbiter::{Arbiter, SystemCommand};
|
||||
|
@ -24,8 +24,7 @@ path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
futures = "0.3.1"
|
||||
pin-project = "0.4.5"
|
||||
pin-project = "0.4.6"
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = "0.2.0-alpha.6"
|
||||
# actix-rt = "1.0.0-alpha.1"
|
||||
actix-rt = "1.0.0-alpha.1"
|
||||
|
@ -292,7 +292,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[actix_rt::test]
|
||||
async fn test_poll_ready() {
|
||||
let cnt = Rc::new(Cell::new(0));
|
||||
let mut srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt.clone()));
|
||||
@ -301,7 +301,7 @@ mod tests {
|
||||
assert_eq!(cnt.get(), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[actix_rt::test]
|
||||
async fn test_call() {
|
||||
let cnt = Rc::new(Cell::new(0));
|
||||
let mut srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt));
|
||||
@ -310,7 +310,7 @@ mod tests {
|
||||
assert_eq!(res.unwrap(), (("srv1", "srv2")));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[actix_rt::test]
|
||||
async fn test_new_service() {
|
||||
let cnt = Rc::new(Cell::new(0));
|
||||
let cnt2 = cnt.clone();
|
||||
|
@ -196,7 +196,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[actix_rt::test]
|
||||
async fn test_call() {
|
||||
let mut srv = pipeline(apply_fn(Srv, |req: &'static str, srv| {
|
||||
let fut = srv.call(());
|
||||
@ -213,7 +213,7 @@ mod tests {
|
||||
assert_eq!(res.unwrap(), (("srv", ())));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[actix_rt::test]
|
||||
async fn test_new_service() {
|
||||
let new_srv = pipeline_factory(apply_fn_factory(
|
||||
|| ok::<_, ()>(Srv),
|
||||
|
@ -219,14 +219,14 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[actix_rt::test]
|
||||
async fn test_poll_ready() {
|
||||
let mut srv = Srv.map(|_| "ok");
|
||||
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
||||
assert_eq!(res, Poll::Ready(Ok(())));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[actix_rt::test]
|
||||
async fn test_call() {
|
||||
let mut srv = Srv.map(|_| "ok");
|
||||
let res = srv.call(()).await;
|
||||
@ -234,7 +234,7 @@ mod tests {
|
||||
assert_eq!(res.unwrap(), "ok");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[actix_rt::test]
|
||||
async fn test_new_service() {
|
||||
let new_srv = (|| ok::<_, ()>(Srv)).into_factory().map(|_| "ok");
|
||||
let mut srv = new_srv.new_service(&()).await.unwrap();
|
||||
|
@ -221,14 +221,14 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[actix_rt::test]
|
||||
async fn test_poll_ready() {
|
||||
let mut srv = Srv.map_err(|_| "error");
|
||||
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
||||
assert_eq!(res, Poll::Ready(Err("error")));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[actix_rt::test]
|
||||
async fn test_call() {
|
||||
let mut srv = Srv.map_err(|_| "error");
|
||||
let res = srv.call(()).await;
|
||||
@ -236,7 +236,7 @@ mod tests {
|
||||
assert_eq!(res.err().unwrap(), "error");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[actix_rt::test]
|
||||
async fn test_new_service() {
|
||||
let new_srv = (|| ok::<_, ()>(Srv)).into_factory().map_err(|_| "error");
|
||||
let mut srv = new_srv.new_service(&()).await.unwrap();
|
||||
|
@ -307,7 +307,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[actix_rt::test]
|
||||
async fn test_poll_ready() {
|
||||
let cnt = Rc::new(Cell::new(0));
|
||||
let mut srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt.clone()));
|
||||
@ -316,7 +316,7 @@ mod tests {
|
||||
assert_eq!(cnt.get(), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[actix_rt::test]
|
||||
async fn test_call() {
|
||||
let cnt = Rc::new(Cell::new(0));
|
||||
let mut srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt));
|
||||
@ -330,7 +330,7 @@ mod tests {
|
||||
assert_eq!(res.unwrap(), (("srv2", "err")));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[actix_rt::test]
|
||||
async fn test_factory() {
|
||||
let cnt = Rc::new(Cell::new(0));
|
||||
let cnt2 = cnt.clone();
|
||||
|
@ -1,5 +1,14 @@
|
||||
# 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
|
||||
|
||||
* Upgrade actix-server and actix-server-config deps
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-testing"
|
||||
version = "0.3.0-alpha.1"
|
||||
version = "0.3.0-alpha.2"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix testing utils"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
@ -18,6 +18,7 @@ path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
actix-rt = "1.0.0-alpha.1"
|
||||
actix-macros = "0.1.0-alpha.1"
|
||||
actix-server = "0.8.0-alpha.1"
|
||||
actix-server-config = "0.3.0-alpha.1"
|
||||
actix-service = "1.0.0-alpha.1"
|
||||
@ -25,5 +26,4 @@ actix-service = "1.0.0-alpha.1"
|
||||
log = "0.4"
|
||||
net2 = "0.2"
|
||||
futures = "0.3.1"
|
||||
tokio = "0.2.0-alpha.6"
|
||||
tokio-net = { version = "0.2.0-alpha.6" }
|
||||
|
@ -10,8 +10,7 @@ use net2::TcpBuilder;
|
||||
use tokio_net::driver::Handle;
|
||||
use tokio_net::tcp::TcpStream;
|
||||
|
||||
mod rt;
|
||||
pub use self::rt::*;
|
||||
pub use actix_macros::test;
|
||||
|
||||
/// The `TestServer` type.
|
||||
///
|
||||
|
@ -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()
|
||||
}
|
@ -137,36 +137,33 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transform() {
|
||||
#[actix_rt::test]
|
||||
async fn test_transform() {
|
||||
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(());
|
||||
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Pending);
|
||||
let mut srv = InFlightService::new(1, SleepService(wait_time));
|
||||
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
||||
|
||||
let _ = res.await;
|
||||
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
||||
});
|
||||
let res = srv.call(());
|
||||
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]
|
||||
fn test_newtransform() {
|
||||
#[actix_rt::test]
|
||||
async fn test_newtransform() {
|
||||
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();
|
||||
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
||||
let mut srv = srv.new_service(&()).await.unwrap();
|
||||
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
||||
|
||||
let res = srv.call(());
|
||||
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Pending);
|
||||
let res = srv.call(());
|
||||
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(())));
|
||||
});
|
||||
let _ = res.await;
|
||||
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
||||
}
|
||||
}
|
||||
|
@ -230,8 +230,8 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_inorder() {
|
||||
#[actix_rt::test]
|
||||
async fn test_inorder() {
|
||||
let (tx1, rx1) = oneshot::channel();
|
||||
let (tx2, rx2) = oneshot::channel();
|
||||
let (tx3, rx3) = oneshot::channel();
|
||||
@ -269,7 +269,7 @@ mod tests {
|
||||
let _ = tx2.send(2);
|
||||
let _ = tx1.send(1);
|
||||
|
||||
let _ = actix_rt::System::new("test").block_on(rx_stop);
|
||||
let _ = rx_stop.await;
|
||||
let _ = h.join();
|
||||
}
|
||||
}
|
||||
|
@ -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`.
|
||||
///
|
||||
/// Expected Behavior: Two back-to-back calls of `SystemTimeService::now()` return the same value.
|
||||
#[test]
|
||||
fn system_time_service_time_does_not_immediately_change() {
|
||||
#[actix_rt::test]
|
||||
async fn system_time_service_time_does_not_immediately_change() {
|
||||
let resolution = Duration::from_millis(50);
|
||||
|
||||
let _ = actix_rt::System::new("test").block_on(async {
|
||||
let time_service = SystemTimeService::with(resolution);
|
||||
|
||||
assert_eq!(time_service.now(), time_service.now());
|
||||
});
|
||||
let time_service = SystemTimeService::with(resolution);
|
||||
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`.
|
||||
///
|
||||
/// Expected Behavior: Two back-to-back calls of `LowResTimeService::now()` return the same value.
|
||||
#[test]
|
||||
fn lowres_time_service_time_does_not_immediately_change() {
|
||||
#[actix_rt::test]
|
||||
async fn lowres_time_service_time_does_not_immediately_change() {
|
||||
let resolution = Duration::from_millis(50);
|
||||
|
||||
let _ = actix_rt::System::new("test").block_on(async {
|
||||
let time_service = LowResTimeService::with(resolution);
|
||||
|
||||
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.
|
||||
///
|
||||
/// 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.
|
||||
#[test]
|
||||
fn system_time_service_time_updates_after_resolution_interval() {
|
||||
#[actix_rt::test]
|
||||
async fn system_time_service_time_updates_after_resolution_interval() {
|
||||
let resolution = Duration::from_millis(100);
|
||||
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
|
||||
.now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap();
|
||||
let first_time = time_service
|
||||
.now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap();
|
||||
|
||||
delay_for(wait_time).await;
|
||||
delay_for(wait_time).await;
|
||||
|
||||
let second_time = time_service
|
||||
.now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap();
|
||||
let second_time = time_service
|
||||
.now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.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.
|
||||
///
|
||||
/// 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.
|
||||
#[test]
|
||||
fn lowres_time_service_time_updates_after_resolution_interval() {
|
||||
#[actix_rt::test]
|
||||
async fn lowres_time_service_time_updates_after_resolution_interval() {
|
||||
let resolution = Duration::from_millis(100);
|
||||
let wait_time = Duration::from_millis(150);
|
||||
let time_service = LowResTimeService::with(resolution);
|
||||
|
||||
let _ = actix_rt::System::new("test").block_on(async {
|
||||
let time_service = LowResTimeService::with(resolution);
|
||||
let first_time = time_service.now();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -204,44 +204,35 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_success() {
|
||||
#[actix_rt::test]
|
||||
async fn test_success() {
|
||||
let resolution = Duration::from_millis(100);
|
||||
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));
|
||||
timeout.call(()).await
|
||||
});
|
||||
assert_eq!(res, Ok(()));
|
||||
let mut timeout = TimeoutService::new(resolution, SleepService(wait_time));
|
||||
assert_eq!(timeout.call(()).await, Ok(()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_timeout() {
|
||||
#[actix_rt::test]
|
||||
async fn test_timeout() {
|
||||
let resolution = Duration::from_millis(100);
|
||||
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));
|
||||
timeout.call(()).await
|
||||
});
|
||||
assert_eq!(res, Err(TimeoutError::Timeout));
|
||||
let mut timeout = TimeoutService::new(resolution, SleepService(wait_time));
|
||||
assert_eq!(timeout.call(()).await, Err(TimeoutError::Timeout));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_timeout_newservice() {
|
||||
#[actix_rt::test]
|
||||
async fn test_timeout_newservice() {
|
||||
let resolution = Duration::from_millis(100);
|
||||
let wait_time = Duration::from_millis(150);
|
||||
|
||||
let res = actix_rt::System::new("test").block_on(async {
|
||||
let timeout = apply(
|
||||
Timeout::new(resolution),
|
||||
factory_fn(|| ok::<_, ()>(SleepService(wait_time))),
|
||||
);
|
||||
let mut srv = timeout.new_service(&()).await.unwrap();
|
||||
let timeout = apply(
|
||||
Timeout::new(resolution),
|
||||
factory_fn(|| ok::<_, ()>(SleepService(wait_time))),
|
||||
);
|
||||
let mut srv = timeout.new_service(&()).await.unwrap();
|
||||
|
||||
srv.call(()).await
|
||||
});
|
||||
assert_eq!(res, Err(TimeoutError::Timeout));
|
||||
assert_eq!(srv.call(()).await, Err(TimeoutError::Timeout));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user