From 9a8a2b321663d64fb1a29e7a3739404fa1a9b56c Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 14 Mar 2025 14:08:43 +0500 Subject: [PATCH] Check test server availability before using it (#522) --- ntex-macros/CHANGES.md | 4 ++++ ntex-macros/Cargo.toml | 2 +- ntex-macros/src/lib.rs | 2 ++ ntex-server/src/net/test.rs | 3 +++ ntex/CHANGES.md | 2 ++ ntex/Cargo.toml | 2 +- ntex/src/http/test.rs | 3 +++ ntex/src/lib.rs | 10 ++++++++++ ntex/src/web/test.rs | 3 +++ 9 files changed, 29 insertions(+), 2 deletions(-) diff --git a/ntex-macros/CHANGES.md b/ntex-macros/CHANGES.md index 8f29a797..8d884c08 100644 --- a/ntex-macros/CHANGES.md +++ b/ntex-macros/CHANGES.md @@ -1,5 +1,9 @@ # Changes +## [0.1.4] - 2025-03-14 + +* Enable env_logger for test macro + ## [0.1.2] - 2021-02-25 * Export runtime from ntex crate diff --git a/ntex-macros/Cargo.toml b/ntex-macros/Cargo.toml index 512b4501..f6cad0e2 100644 --- a/ntex-macros/Cargo.toml +++ b/ntex-macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ntex-macros" -version = "0.1.3" +version = "0.1.4" description = "ntex proc macros" readme = "README.md" authors = ["ntex contributors "] diff --git a/ntex-macros/src/lib.rs b/ntex-macros/src/lib.rs index 8c6c29d2..3179296a 100644 --- a/ntex-macros/src/lib.rs +++ b/ntex-macros/src/lib.rs @@ -262,6 +262,7 @@ pub fn rt_test(_: TokenStream, item: TokenStream) -> TokenStream { quote! { #(#attrs)* fn #name() #ret { + ntex::util::enable_test_logging(); ntex::rt::System::new("test") .block_on(async { #body }) } @@ -271,6 +272,7 @@ pub fn rt_test(_: TokenStream, item: TokenStream) -> TokenStream { #[test] #(#attrs)* fn #name() #ret { + ntex::util::enable_test_logging(); ntex::rt::System::new("test") .block_on(async { #body }) } diff --git a/ntex-server/src/net/test.rs b/ntex-server/src/net/test.rs index 80441628..2ddae445 100644 --- a/ntex-server/src/net/test.rs +++ b/ntex-server/src/net/test.rs @@ -67,6 +67,9 @@ where let (system, addr, server) = rx.recv().unwrap(); + // wait for server + thread::sleep(std::time::Duration::from_millis(50)); + TestServer { addr, server, diff --git a/ntex/CHANGES.md b/ntex/CHANGES.md index 539557c1..dc61776a 100644 --- a/ntex/CHANGES.md +++ b/ntex/CHANGES.md @@ -4,6 +4,8 @@ * Add neon runtime support +* Check test server availability before using it + * Drop glommio support * Drop async-std support diff --git a/ntex/Cargo.toml b/ntex/Cargo.toml index 0ead2c63..41c418de 100644 --- a/ntex/Cargo.toml +++ b/ntex/Cargo.toml @@ -83,6 +83,7 @@ pin-project-lite = "0.2" regex = { version = "1.11", default-features = false, features = ["std"] } serde = { version = "1", features = ["derive"] } sha-1 = { version = "0.10", optional = true } +env_logger = { version = "0.11", default-features = false } thiserror = "1" nanorand = { version = "0.7", default-features = false, features = [ "std", @@ -112,7 +113,6 @@ brotli2 = { version = "0.3.2", optional = true } flate2 = { version = "1.0", optional = true } [dev-dependencies] -env_logger = "0.11" rand = "0.8" time = "0.3" futures-util = "0.3" diff --git a/ntex/src/http/test.rs b/ntex/src/http/test.rs index a64df994..01ad7f4c 100644 --- a/ntex/src/http/test.rs +++ b/ntex/src/http/test.rs @@ -251,6 +251,9 @@ where let (system, server, addr) = rx.recv().unwrap(); + // wait for server + thread::sleep(std::time::Duration::from_millis(50)); + TestServer { addr, system, diff --git a/ntex/src/lib.rs b/ntex/src/lib.rs index 6d208d9e..93220f15 100644 --- a/ntex/src/lib.rs +++ b/ntex/src/lib.rs @@ -123,4 +123,14 @@ pub mod util { #[doc(hidden)] #[deprecated] pub use std::task::ready; + + #[doc(hidden)] + pub fn enable_test_logging() { + if std::env::var("NTEX_NO_TEST_LOG").is_err() { + if std::env::var("RUST_LOG").is_err() { + std::env::set_var("RUST_LOG", "trace"); + } + let _ = env_logger::builder().is_test(true).try_init(); + } + } } diff --git a/ntex/src/web/test.rs b/ntex/src/web/test.rs index 6a0bcabc..1d431b2f 100644 --- a/ntex/src/web/test.rs +++ b/ntex/src/web/test.rs @@ -704,6 +704,9 @@ where let (system, server, addr) = rx.recv().unwrap(); + // wait for server + thread::sleep(std::time::Duration::from_millis(50)); + let client = { let connector = { #[cfg(feature = "openssl")]