From 0ae70f01fea5acf49d97aab33e1c6b825cf79204 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 14 Mar 2025 12:39:39 +0500 Subject: [PATCH] Check test server availability before using it --- .github/workflows/linux.yml | 6 ++-- ntex-macros/CHANGES.md | 4 +++ ntex-macros/Cargo.toml | 2 +- ntex-macros/src/lib.rs | 2 ++ ntex-server/src/net/test.rs | 5 +++ ntex/CHANGES.md | 2 ++ ntex/Cargo.toml | 2 +- ntex/src/http/test.rs | 5 +++ ntex/src/lib.rs | 10 ++++++ ntex/src/web/test.rs | 5 +++ ntex/tests/http_openssl.rs | 38 +--------------------- ntex/tests/web_server.rs | 64 ------------------------------------- 12 files changed, 39 insertions(+), 106 deletions(-) diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index f634bdff..e178d634 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -52,17 +52,17 @@ jobs: - name: Run tests (compio) timeout-minutes: 40 run: | - RUST_LOG=trace cargo test --all --no-default-features --features="ntex/compio,ntex/cookie,ntex/url,ntex/compress,ntex/openssl,ntex/rustls,ntex/ws,ntex/brotli" + cargo test --all --no-default-features --features="ntex/compio,ntex/cookie,ntex/url,ntex/compress,ntex/openssl,ntex/rustls,ntex/ws,ntex/brotli" - name: Run tests (neon) timeout-minutes: 40 run: | - RUST_LOG=trace cargo test --all --no-default-features --features="ntex/neon,ntex/cookie,ntex/url,ntex/compress,ntex/openssl,ntex/rustls,ntex/ws,ntex/brotli" + cargo test --all --no-default-features --features="ntex/neon,ntex/cookie,ntex/url,ntex/compress,ntex/openssl,ntex/rustls,ntex/ws,ntex/brotli" - name: Run tests (neon-uring) timeout-minutes: 40 run: | - RUST_LOG=trace cargo test --all --no-default-features --features="ntex/neon-uring,ntex/cookie,ntex/url,ntex/compress,ntex/openssl,ntex/rustls,ntex/ws,ntex/brotli" + cargo test --all --no-default-features --features="ntex/neon-uring,ntex/cookie,ntex/url,ntex/compress,ntex/openssl,ntex/rustls,ntex/ws,ntex/brotli" - name: Install cargo-cache continue-on-error: true 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..2dea7a91 100644 --- a/ntex-server/src/net/test.rs +++ b/ntex-server/src/net/test.rs @@ -67,6 +67,11 @@ where let (system, addr, server) = rx.recv().unwrap(); + // wait for server + while net::TcpStream::connect(addr).is_err() { + thread::sleep(std::time::Duration::from_millis(25)); + } + 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..7b0e3e76 100644 --- a/ntex/src/http/test.rs +++ b/ntex/src/http/test.rs @@ -251,6 +251,11 @@ where let (system, server, addr) = rx.recv().unwrap(); + // wait for server + while net::TcpStream::connect(addr).is_err() { + thread::sleep(std::time::Duration::from_millis(25)); + } + 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..1664ea8b 100644 --- a/ntex/src/web/test.rs +++ b/ntex/src/web/test.rs @@ -704,6 +704,11 @@ where let (system, server, addr) = rx.recv().unwrap(); + // wait for server + while net::TcpStream::connect(addr).is_err() { + thread::sleep(std::time::Duration::from_millis(25)); + } + let client = { let connector = { #[cfg(feature = "openssl")] diff --git a/ntex/tests/http_openssl.rs b/ntex/tests/http_openssl.rs index b321eaa7..4be3aa51 100644 --- a/ntex/tests/http_openssl.rs +++ b/ntex/tests/http_openssl.rs @@ -1,5 +1,5 @@ #![cfg(feature = "openssl")] -use std::{io, sync::atomic::AtomicUsize, sync::atomic::Ordering, sync::Arc}; +use std::{env, io, sync::atomic::AtomicUsize, sync::atomic::Ordering, sync::Arc}; use futures_util::stream::{once, Stream, StreamExt}; use tls_openssl::ssl::{AlpnError, SslAcceptor, SslFiletype, SslMethod}; @@ -58,8 +58,6 @@ fn ssl_acceptor() -> SslAcceptor { #[ntex::test] async fn test_h2() -> io::Result<()> { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test_server(move || { HttpService::build() .h2(|_| Ready::Ok::<_, io::Error>(Response::Ok().finish())) @@ -74,8 +72,6 @@ async fn test_h2() -> io::Result<()> { #[ntex::test] async fn test_h1() -> io::Result<()> { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test_server(move || { let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap(); builder @@ -98,8 +94,6 @@ async fn test_h1() -> io::Result<()> { #[ntex::test] async fn test_h2_1() -> io::Result<()> { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test_server(move || { HttpService::build() .finish(|req: Request| { @@ -118,8 +112,6 @@ async fn test_h2_1() -> io::Result<()> { #[ntex::test] async fn test_h2_body() -> io::Result<()> { - let _ = env_logger::builder().is_test(true).try_init(); - let data = "HELLOWORLD".to_owned().repeat(64 * 1024); let mut srv = test_server(move || { HttpService::build() @@ -147,8 +139,6 @@ async fn test_h2_body() -> io::Result<()> { #[ntex::test] async fn test_h2_content_length() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test_server(move || { HttpService::build() .h2(|req: Request| async move { @@ -192,8 +182,6 @@ async fn test_h2_content_length() { #[ntex::test] async fn test_h2_headers() { - let _ = env_logger::builder().is_test(true).try_init(); - let data = STR.repeat(10); let data2 = data.clone(); @@ -257,8 +245,6 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \ #[ntex::test] async fn test_h2_body2() { - let _ = env_logger::builder().is_test(true).try_init(); - let mut srv = test_server(move || { HttpService::build() .h2(|_| async { Ok::<_, io::Error>(Response::Ok().body(STR)) }) @@ -276,8 +262,6 @@ async fn test_h2_body2() { #[ntex::test] async fn test_h2_head_empty() { - let _ = env_logger::builder().is_test(true).try_init(); - let mut srv = test_server(move || { HttpService::build() .finish(|_| async { Ok::<_, io::Error>(Response::Ok().body(STR)) }) @@ -301,8 +285,6 @@ async fn test_h2_head_empty() { #[ntex::test] async fn test_h2_head_binary() { - let _ = env_logger::builder().is_test(true).try_init(); - let mut srv = test_server(move || { HttpService::build() .h2(|_| async { @@ -330,8 +312,6 @@ async fn test_h2_head_binary() { /// Server must send content-length, but no payload #[ntex::test] async fn test_h2_head_binary2() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test_server(move || { HttpService::build() .h2(|_| async { Ok::<_, io::Error>(Response::Ok().body(STR)) }) @@ -350,8 +330,6 @@ async fn test_h2_head_binary2() { #[ntex::test] async fn test_h2_body_length() { - let _ = env_logger::builder().is_test(true).try_init(); - let mut srv = test_server(move || { HttpService::build() .h2(|_| async { @@ -374,8 +352,6 @@ async fn test_h2_body_length() { #[ntex::test] async fn test_h2_body_chunked_explicit() { - let _ = env_logger::builder().is_test(true).try_init(); - let mut srv = test_server(move || { HttpService::build() .h2(|_| { @@ -404,8 +380,6 @@ async fn test_h2_body_chunked_explicit() { #[ntex::test] async fn test_h2_response_http_error_handling() { - let _ = env_logger::builder().is_test(true).try_init(); - let mut srv = test_server(move || { HttpService::build() .h2(fn_service(|_| { @@ -430,8 +404,6 @@ async fn test_h2_response_http_error_handling() { #[ntex::test] async fn test_h2_service_error() { - let _ = env_logger::builder().is_test(true).try_init(); - let mut srv = test_server(move || { HttpService::build() .h2(|_| { @@ -462,8 +434,6 @@ impl Drop for SetOnDrop { #[ntex::test] async fn test_h2_client_drop() -> io::Result<()> { - let _ = env_logger::builder().is_test(true).try_init(); - let count = Arc::new(AtomicUsize::new(0)); let count2 = count.clone(); @@ -493,8 +463,6 @@ async fn test_h2_client_drop() -> io::Result<()> { #[ntex::test] async fn test_ssl_handshake_timeout() { - let _ = env_logger::builder().is_test(true).try_init(); - use std::io::Read; let srv = test_server(move || { @@ -513,8 +481,6 @@ async fn test_ssl_handshake_timeout() { #[ntex::test] async fn test_ws_transport() { - let _ = env_logger::builder().is_test(true).try_init(); - let mut srv = test_server(|| { HttpService::build() .h1_control(|req: h1::Control<_, _>| async move { @@ -571,8 +537,6 @@ async fn test_ws_transport() { #[ntex::test] async fn test_h2_graceful_shutdown() -> io::Result<()> { - let _ = env_logger::builder().is_test(true).try_init(); - let count = Arc::new(AtomicUsize::new(0)); let count2 = count.clone(); diff --git a/ntex/tests/web_server.rs b/ntex/tests/web_server.rs index a70922d0..b7bf1b75 100644 --- a/ntex/tests/web_server.rs +++ b/ntex/tests/web_server.rs @@ -81,8 +81,6 @@ impl Stream for TestBody { #[ntex::test] async fn test_body() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test::server(|| { App::new().service( web::resource("/").route(web::to(|| async { HttpResponse::Ok().body(STR) })), @@ -99,8 +97,6 @@ async fn test_body() { #[ntex::test] async fn test_body_gzip() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test::server_with(test::config().h1(), || { App::new() .wrap(Compress::new(ContentEncoding::Gzip)) @@ -131,8 +127,6 @@ async fn test_body_gzip() { #[ntex::test] async fn test_body_gzip2() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test::server_with(test::config().h1(), || { App::new() .wrap(Compress::new(ContentEncoding::Gzip)) @@ -162,8 +156,6 @@ async fn test_body_gzip2() { #[ntex::test] async fn test_body_encoding_override() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test::server_with(test::config().h1(), || { App::new() .wrap(Compress::new(ContentEncoding::Gzip)) @@ -223,8 +215,6 @@ async fn test_body_encoding_override() { #[ntex::test] async fn test_body_gzip_large() { - let _ = env_logger::builder().is_test(true).try_init(); - let data = STR.repeat(10); let srv_data = data.clone(); @@ -258,8 +248,6 @@ async fn test_body_gzip_large() { #[ntex::test] async fn test_body_gzip_large_random() { - let _ = env_logger::builder().is_test(true).try_init(); - let data = rand::thread_rng() .sample_iter(&Alphanumeric) .take(70_000) @@ -298,8 +286,6 @@ async fn test_body_gzip_large_random() { #[ntex::test] async fn test_body_chunked_implicit() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test::server_with(test::config().h1(), || { App::new() .wrap(Compress::new(ContentEncoding::Gzip)) @@ -334,8 +320,6 @@ async fn test_body_chunked_implicit() { #[ntex::test] async fn test_body_br_streaming() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test::server_with(test::config().h1(), || { App::new().wrap(Compress::new(ContentEncoding::Br)).service( web::resource("/").route(web::to(move || async { @@ -366,8 +350,6 @@ async fn test_body_br_streaming() { #[ntex::test] async fn test_head_binary() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test::server_with(test::config().h1(), || { App::new().service( web::resource("/") @@ -392,8 +374,6 @@ async fn test_head_binary() { #[ntex::test] async fn test_no_chunking() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test::server_with(test::config().h1(), || { App::new().service(web::resource("/").route(web::to(move || async { HttpResponse::Ok() @@ -414,8 +394,6 @@ async fn test_no_chunking() { #[ntex::test] async fn test_body_deflate() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test::server_with(test::config().h1(), || { App::new() .wrap(Compress::new(ContentEncoding::Deflate)) @@ -446,8 +424,6 @@ async fn test_body_deflate() { #[ntex::test] async fn test_body_brotli() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test::server_with(test::config().h1(), || { App::new().wrap(Compress::new(ContentEncoding::Br)).service( web::resource("/") @@ -477,8 +453,6 @@ async fn test_body_brotli() { #[ntex::test] async fn test_encoding() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test::server_with(test::config().h1(), || { App::new() .wrap(Compress::default()) @@ -506,8 +480,6 @@ async fn test_encoding() { #[ntex::test] async fn test_gzip_encoding() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test::server_with(test::config().h1(), || { App::new().service(web::resource("/").route(web::to(move |body: Bytes| async { HttpResponse::Ok().body(body) @@ -533,8 +505,6 @@ async fn test_gzip_encoding() { #[ntex::test] async fn test_gzip_encoding_large() { - let _ = env_logger::builder().is_test(true).try_init(); - let data = STR.repeat(10); let srv = test::server_with(test::config().h1(), || { App::new().service(web::resource("/").route(web::to(move |body: Bytes| async { @@ -561,8 +531,6 @@ async fn test_gzip_encoding_large() { #[ntex::test] async fn test_reading_gzip_encoding_large_random() { - let _ = env_logger::builder().is_test(true).try_init(); - let data = rand::thread_rng() .sample_iter(&Alphanumeric) .take(60_000) @@ -595,8 +563,6 @@ async fn test_reading_gzip_encoding_large_random() { #[ntex::test] async fn test_reading_deflate_encoding() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test::server_with(test::config().h1(), || { App::new().service(web::resource("/").route(web::to(move |body: Bytes| async { HttpResponse::Ok().body(body) @@ -622,8 +588,6 @@ async fn test_reading_deflate_encoding() { #[ntex::test] async fn test_reading_deflate_encoding_large() { - let _ = env_logger::builder().is_test(true).try_init(); - let data = STR.repeat(10); let srv = test::server_with(test::config().h1(), || { App::new().service(web::resource("/").route(web::to(move |body: Bytes| async { @@ -650,8 +614,6 @@ async fn test_reading_deflate_encoding_large() { #[ntex::test] async fn test_reading_deflate_encoding_large_random() { - let _ = env_logger::builder().is_test(true).try_init(); - let data = rand::thread_rng() .sample_iter(&Alphanumeric) .take(160_000) @@ -684,8 +646,6 @@ async fn test_reading_deflate_encoding_large_random() { #[ntex::test] async fn test_brotli_encoding() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test::server_with(test::config().h1(), || { App::new().service(web::resource("/").route(web::to(move |body: Bytes| async { HttpResponse::Ok().body(body) @@ -711,8 +671,6 @@ async fn test_brotli_encoding() { #[ntex::test] async fn test_brotli_encoding_large() { - let _ = env_logger::builder().is_test(true).try_init(); - let data = rand::thread_rng() .sample_iter(&Alphanumeric) .take(320_000) @@ -749,8 +707,6 @@ async fn test_brotli_encoding_large() { #[cfg(feature = "openssl")] #[ntex::test] async fn test_brotli_encoding_large_openssl() { - let _ = env_logger::builder().is_test(true).try_init(); - // load ssl keys use tls_openssl::ssl::{SslAcceptor, SslFiletype, SslMethod}; @@ -793,8 +749,6 @@ async fn test_brotli_encoding_large_openssl() { #[cfg(feature = "openssl")] #[ntex::test] async fn test_brotli_encoding_large_openssl_h1() { - let _ = env_logger::builder().is_test(true).try_init(); - // load ssl keys use tls_openssl::ssl::{SslAcceptor, SslFiletype, SslMethod}; @@ -837,8 +791,6 @@ async fn test_brotli_encoding_large_openssl_h1() { #[cfg(feature = "openssl")] #[ntex::test] async fn test_brotli_encoding_large_openssl_h2() { - let _ = env_logger::builder().is_test(true).try_init(); - // load ssl keys use tls_openssl::ssl::{AlpnError, SslAcceptor, SslFiletype, SslMethod}; @@ -893,8 +845,6 @@ async fn test_brotli_encoding_large_openssl_h2() { #[cfg(all(feature = "rustls", feature = "openssl"))] #[ntex::test] async fn test_reading_deflate_encoding_large_random_rustls() { - let _ = env_logger::builder().is_test(true).try_init(); - let data = rand::thread_rng() .sample_iter(&Alphanumeric) .take(160_000) @@ -934,8 +884,6 @@ async fn test_reading_deflate_encoding_large_random_rustls() { #[cfg(all(feature = "rustls", feature = "openssl"))] #[ntex::test] async fn test_reading_deflate_encoding_large_random_rustls_h1() { - let _ = env_logger::builder().is_test(true).try_init(); - let data = rand::thread_rng() .sample_iter(&Alphanumeric) .take(160_000) @@ -977,8 +925,6 @@ async fn test_reading_deflate_encoding_large_random_rustls_h1() { #[cfg(all(feature = "rustls", feature = "openssl"))] #[ntex::test] async fn test_reading_deflate_encoding_large_random_rustls_h2() { - let _ = env_logger::builder().is_test(true).try_init(); - let data = rand::thread_rng() .sample_iter(&Alphanumeric) .take(160_000) @@ -1019,8 +965,6 @@ async fn test_reading_deflate_encoding_large_random_rustls_h2() { #[ntex::test] async fn test_server_cookies() { - let _ = env_logger::builder().is_test(true).try_init(); - use ntex::http::header::SET_COOKIE; use ntex::http::HttpMessage; @@ -1068,8 +1012,6 @@ async fn test_server_cookies() { #[ntex::test] async fn test_slow_request() { - let _ = env_logger::builder().is_test(true).try_init(); - use std::net; let srv = test::server_with(test::config().client_timeout(Seconds(1)), || { @@ -1091,8 +1033,6 @@ async fn test_slow_request() { #[ntex::test] async fn test_custom_error() { - let _ = env_logger::builder().is_test(true).try_init(); - #[derive(Error, Debug)] #[error("TestError")] struct TestError; @@ -1163,8 +1103,6 @@ async fn test_custom_error() { #[ntex::test] async fn test_web_server() { - let _ = env_logger::builder().is_test(true).try_init(); - let (tx, rx) = std::sync::mpsc::channel(); std::thread::spawn(move || { @@ -1207,8 +1145,6 @@ async fn test_web_server() { /// Websocket connection, no ws handler and response contains payload #[ntex::test] async fn web_no_ws_with_response_payload() { - let _ = env_logger::builder().is_test(true).try_init(); - let srv = test::server_with(test::config().h1(), || { App::new() .service(web::resource("/").route(web::get().to(move || async {