diff --git a/Cargo.toml b/Cargo.toml index f489c5c3..b7646adb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/actix-connect/tests/test_connect.rs b/actix-connect/tests/test_connect.rs index f3bd5818..fec644b3 100644 --- a/actix-connect/tests/test_connect.rs +++ b/actix-connect/tests/test_connect.rs @@ -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| { 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| { 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| { 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()); } diff --git a/actix-ioframe/tests/test_server.rs b/actix-ioframe/tests/test_server.rs index f0e0d697..8b92c878 100644 --- a/actix-ioframe/tests/test_server.rs +++ b/actix-ioframe/tests/test_server.rs @@ -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(()) diff --git a/actix-macros/Cargo.toml b/actix-macros/Cargo.toml new file mode 100644 index 00000000..9052fa66 --- /dev/null +++ b/actix-macros/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "actix-macros" +version = "0.1.0-alpha.1" +authors = ["Nikolay Kim "] +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" } diff --git a/actix-macros/src/lib.rs b/actix-macros/src/lib.rs new file mode 100644 index 00000000..8d628e01 --- /dev/null +++ b/actix-macros/src/lib.rs @@ -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() +} diff --git a/actix-rt/CHANGES.md b/actix-rt/CHANGES.md index 35f5c19e..787a0b0b 100644 --- a/actix-rt/CHANGES.md +++ b/actix-rt/CHANGES.md @@ -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 diff --git a/actix-rt/Cargo.toml b/actix-rt/Cargo.toml index f134bd84..6638316a 100644 --- a/actix-rt/Cargo.toml +++ b/actix-rt/Cargo.toml @@ -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" diff --git a/actix-rt/src/arbiter.rs b/actix-rt/src/arbiter.rs index 31690efc..0b0ae659 100644 --- a/actix-rt/src/arbiter.rs +++ b/actix-rt/src/arbiter.rs @@ -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; diff --git a/actix-rt/src/builder.rs b/actix-rt/src/builder.rs index b7d5b466..8814decf 100644 --- a/actix-rt/src/builder.rs +++ b/actix-rt/src/builder.rs @@ -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 { diff --git a/actix-rt/src/lib.rs b/actix-rt/src/lib.rs index eeb768b2..d9c6c58a 100644 --- a/actix-rt/src/lib.rs +++ b/actix-rt/src/lib.rs @@ -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; diff --git a/actix-rt/src/system.rs b/actix-rt/src/system.rs index c2eb9f37..7f643095 100644 --- a/actix-rt/src/system.rs +++ b/actix-rt/src/system.rs @@ -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}; diff --git a/actix-service/Cargo.toml b/actix-service/Cargo.toml index c4204299..d8b50ce3 100644 --- a/actix-service/Cargo.toml +++ b/actix-service/Cargo.toml @@ -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" diff --git a/actix-service/src/and_then.rs b/actix-service/src/and_then.rs index d2be3012..3e148040 100644 --- a/actix-service/src/and_then.rs +++ b/actix-service/src/and_then.rs @@ -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(); diff --git a/actix-service/src/apply.rs b/actix-service/src/apply.rs index 3eefae27..fdb6aac0 100644 --- a/actix-service/src/apply.rs +++ b/actix-service/src/apply.rs @@ -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), diff --git a/actix-service/src/map.rs b/actix-service/src/map.rs index 6e533c84..2f55a38a 100644 --- a/actix-service/src/map.rs +++ b/actix-service/src/map.rs @@ -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(); diff --git a/actix-service/src/map_err.rs b/actix-service/src/map_err.rs index 1b858d80..87ccd32f 100644 --- a/actix-service/src/map_err.rs +++ b/actix-service/src/map_err.rs @@ -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(); diff --git a/actix-service/src/then.rs b/actix-service/src/then.rs index 9e5e7917..b32bd168 100644 --- a/actix-service/src/then.rs +++ b/actix-service/src/then.rs @@ -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(); diff --git a/actix-testing/CHANGES.md b/actix-testing/CHANGES.md index 7896c18e..098fc581 100644 --- a/actix-testing/CHANGES.md +++ b/actix-testing/CHANGES.md @@ -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 diff --git a/actix-testing/Cargo.toml b/actix-testing/Cargo.toml index a4257fbd..e199d568 100644 --- a/actix-testing/Cargo.toml +++ b/actix-testing/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-testing" -version = "0.3.0-alpha.1" +version = "0.3.0-alpha.2" authors = ["Nikolay Kim "] 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" } diff --git a/actix-testing/src/lib.rs b/actix-testing/src/lib.rs index a4631eda..ed831eec 100644 --- a/actix-testing/src/lib.rs +++ b/actix-testing/src/lib.rs @@ -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. /// diff --git a/actix-testing/src/rt.rs b/actix-testing/src/rt.rs deleted file mode 100644 index 163a8c34..00000000 --- a/actix-testing/src/rt.rs +++ /dev/null @@ -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 = { - RefCell::new(Inner(Some(System::builder().build()))) - }; -} - -struct Inner(Option); - -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::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: 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(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: 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(app: &mut S, req: R) -> S::Response -where - S: Service, - S::Error: std::fmt::Debug, -{ - block_on(run_on(move || app.call(req))).unwrap() -} diff --git a/actix-utils/src/inflight.rs b/actix-utils/src/inflight.rs index 8beff7e7..5e425823 100644 --- a/actix-utils/src/inflight.rs +++ b/actix-utils/src/inflight.rs @@ -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(()))); } } diff --git a/actix-utils/src/order.rs b/actix-utils/src/order.rs index 6938ca41..476a4a5b 100644 --- a/actix-utils/src/order.rs +++ b/actix-utils/src/order.rs @@ -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(); } } diff --git a/actix-utils/src/time.rs b/actix-utils/src/time.rs index 2c50d9dc..bd9d0638 100644 --- a/actix-utils/src/time.rs +++ b/actix-utils/src/time.rs @@ -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); } } diff --git a/actix-utils/src/timeout.rs b/actix-utils/src/timeout.rs index b2fcdbd9..0a39b456 100644 --- a/actix-utils/src/timeout.rs +++ b/actix-utils/src/timeout.rs @@ -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)); } }