1515
1616use crate :: config:: HealthStatus ;
1717use crate :: logging:: make_request_span;
18- use crate :: logging:: TraceParent ;
1918use crate :: metrics:: MetricsRegistry ;
2019use crate :: traits:: DistributedRuntimeProvider ;
21- use axum:: { body , http:: StatusCode , response:: IntoResponse , routing:: get, Router } ;
20+ use axum:: { http:: StatusCode , response:: IntoResponse , routing:: get, Router } ;
2221use serde_json:: json;
23- use std:: collections:: HashMap ;
24- use std:: sync:: Arc ;
25- use std:: sync:: OnceLock ;
22+ use std:: sync:: { Arc , OnceLock } ;
2623use std:: time:: Instant ;
2724use tokio:: { net:: TcpListener , task:: JoinHandle } ;
2825use tokio_util:: sync:: CancellationToken ;
29- use tower_http:: trace:: DefaultMakeSpan ;
3026use tower_http:: trace:: TraceLayer ;
3127
3228/// System status server information containing socket address and handle
@@ -296,61 +292,10 @@ async fn metrics_handler(state: Arc<SystemStatusState>) -> impl IntoResponse {
296292}
297293
298294// Regular tests: cargo test system_status_server --lib
299- // Integration tests: cargo test system_status_server --lib --features integration
300-
301- #[ cfg( test) ]
302- /// Helper function to create a DRT instance for basic unit tests
303- /// Uses from_current to leverage existing tokio runtime without environment configuration
304- async fn create_test_drt_async ( ) -> crate :: DistributedRuntime {
305- let rt = crate :: Runtime :: from_current ( ) . unwrap ( ) ;
306- crate :: DistributedRuntime :: from_settings_without_discovery ( rt)
307- . await
308- . unwrap ( )
309- }
310-
311- #[ cfg( test) ]
312- /// Helper function to create a DRT instance for integration tests
313- /// Uses spawn_blocking to create runtime safely without ownership issues
314- /// Enables system status server for integration testing
315- /// Note: This function uses environment variables to configure and create the DistributedRuntime.
316- async fn create_test_drt_with_settings_async ( ) -> crate :: DistributedRuntime {
317- // Create runtime in blocking context where it can be safely dropped
318- let handle = tokio:: task:: spawn_blocking ( || {
319- // Load configuration from environment/settings
320- let config = crate :: config:: RuntimeConfig :: from_settings ( ) . unwrap ( ) ;
321-
322- // Create runtime with the configuration and extract handle
323- let runtime = config. create_runtime ( ) . unwrap ( ) ;
324- let handle = runtime. handle ( ) . clone ( ) ;
325-
326- // Runtime will be automatically dropped when it goes out of scope
327- handle
328- } )
329- . await
330- . unwrap ( ) ;
331-
332- // Create Runtime using external handle (no ownership)
333- let rt = crate :: Runtime :: from_handle ( handle) . unwrap ( ) ;
334- crate :: DistributedRuntime :: from_settings_without_discovery ( rt)
335- . await
336- . unwrap ( )
337- }
338-
339295#[ cfg( test) ]
340296mod tests {
341297 use super :: * ;
342- use crate :: logging:: tests:: load_log;
343- use crate :: metrics:: MetricsRegistry ;
344- use anyhow:: { anyhow, Result } ;
345- use chrono:: { DateTime , Utc } ;
346- use jsonschema:: { Draft , JSONSchema } ;
347- use rstest:: rstest;
348- use serde_json:: Value ;
349- use std:: fs:: File ;
350- use std:: io:: { BufRead , BufReader } ;
351- use std:: sync:: Arc ;
352- use stdio_override:: * ;
353- use tokio:: time:: { sleep, Duration } ;
298+ use tokio:: time:: Duration ;
354299
355300 // This is a basic test to verify the HTTP server is working before testing other more complicated tests
356301 #[ tokio:: test]
@@ -381,8 +326,19 @@ mod tests {
381326 "HTTP server should shut down when cancel token is cancelled"
382327 ) ;
383328 }
329+ }
330+
331+ // Integration tests: cargo test system_status_server --lib --features integration
332+ #[ cfg( all( test, feature = "integration" ) ) ]
333+ mod integration_tests {
334+ use super :: * ;
335+ use crate :: distributed:: test_helpers:: create_test_drt_async;
336+ use crate :: metrics:: MetricsRegistry ;
337+ use anyhow:: Result ;
338+ use rstest:: rstest;
339+ use std:: sync:: Arc ;
340+ use tokio:: time:: Duration ;
384341
385- #[ cfg( feature = "integration" ) ]
386342 #[ tokio:: test]
387343 async fn test_uptime_without_initialization ( ) {
388344 // Test that uptime returns an error if start time is not initialized
@@ -398,7 +354,6 @@ mod tests {
398354 . await ;
399355 }
400356
401- #[ cfg( feature = "integration" ) ]
402357 #[ tokio:: test]
403358 async fn test_runtime_metrics_initialization_and_namespace ( ) {
404359 // Test that metrics have correct namespace
@@ -439,7 +394,6 @@ mod tests {
439394 . await ;
440395 }
441396
442- #[ cfg( feature = "integration" ) ]
443397 #[ tokio:: test]
444398 async fn test_start_time_initialization ( ) {
445399 // Test that start time can only be initialized once
@@ -516,7 +470,7 @@ mod tests {
516470 ( "DYN_SYSTEM_LIVE_PATH" , custom_live_path) ,
517471 ] ,
518472 ( async || {
519- let drt = Arc :: new ( create_test_drt_with_settings_async ( ) . await ) ;
473+ let drt = Arc :: new ( create_test_drt_async ( ) . await ) ;
520474
521475 // Get system status server info from DRT (instead of manually spawning)
522476 let system_info = drt
@@ -575,7 +529,6 @@ mod tests {
575529 }
576530
577531 #[ tokio:: test]
578- #[ cfg( feature = "integration" ) ]
579532 async fn test_health_endpoint_tracing ( ) -> Result < ( ) > {
580533 use std:: sync:: Arc ;
581534
@@ -596,7 +549,7 @@ mod tests {
596549
597550 crate :: logging:: init ( ) ;
598551
599- let drt = Arc :: new ( create_test_drt_with_settings_async ( ) . await ) ;
552+ let drt = Arc :: new ( create_test_drt_async ( ) . await ) ;
600553
601554 // Get system status server info from DRT (instead of manually spawning)
602555 let system_info = drt
@@ -631,7 +584,6 @@ mod tests {
631584 Ok ( ( ) )
632585 }
633586
634- #[ cfg( feature = "integration" ) ]
635587 #[ tokio:: test]
636588 async fn test_health_endpoint_with_changing_health_status ( ) {
637589 // Test health endpoint starts in not ready status, then becomes ready
@@ -646,7 +598,7 @@ mod tests {
646598 ( "DYN_SYSTEM_USE_ENDPOINT_HEALTH_STATUS" , Some ( ENDPOINT_HEALTH_CONFIG ) ) ,
647599 ] ,
648600 async {
649- let drt = Arc :: new ( create_test_drt_with_settings_async ( ) . await ) ;
601+ let drt = Arc :: new ( create_test_drt_async ( ) . await ) ;
650602
651603 // Check if system status server was started
652604 let system_info_opt = drt. system_status_server_info ( ) ;
@@ -745,7 +697,6 @@ mod tests {
745697 . await ;
746698 }
747699
748- #[ cfg( feature = "integration" ) ]
749700 #[ tokio:: test]
750701 async fn test_spawn_system_status_server_endpoints ( ) {
751702 // use reqwest for HTTP requests
@@ -756,7 +707,7 @@ mod tests {
756707 ( "DYN_SYSTEM_STARTING_HEALTH_STATUS" , Some ( "ready" ) ) ,
757708 ] ,
758709 async {
759- let drt = Arc :: new ( create_test_drt_with_settings_async ( ) . await ) ;
710+ let drt = Arc :: new ( create_test_drt_async ( ) . await ) ;
760711
761712 // Get system status server info from DRT (instead of manually spawning)
762713 let system_info = drt
0 commit comments