@@ -9,7 +9,10 @@ use futures::{Future, FutureExt};
99use std:: { pin:: Pin , sync:: Arc , time:: Duration } ;
1010use tokio:: net:: TcpListener ;
1111
12- use super :: super :: client:: { AppApiTokenLayer , TonicClient } ;
12+ use super :: super :: client:: {
13+ AppApiTokenLayer , TonicClient ,
14+ config:: { DAPR_GRPC_PORT_ENV , DEFAULT_DAPR_GRPC_PORT } ,
15+ } ;
1316use super :: actor:: runtime:: { ActorRuntime , ActorTypeRegistration } ;
1417
1518/// The Dapr HTTP server.
@@ -92,11 +95,7 @@ impl DaprHttpServer {
9295 /// For a non-panicking version that allows you to handle any errors yourself, see:
9396 /// [DaprHttpServer::try_new_with_dapr_port]
9497 pub async fn new ( ) -> Self {
95- let dapr_port: u16 = std:: env:: var ( "DAPR_GRPC_PORT" )
96- . unwrap_or ( "3501" . into ( ) )
97- . parse ( )
98- . unwrap ( ) ;
99- Self :: with_dapr_port ( dapr_port) . await
98+ Self :: with_dapr_port ( dapr_grpc_port_from_env ( ) ) . await
10099 }
101100
102101 /// Creates a new instance of the Dapr HTTP server that connects to the Dapr sidecar on the
@@ -385,3 +384,52 @@ struct ReminderPayload {
385384struct TimerPayload {
386385 data : Option < String > ,
387386}
387+
388+ fn dapr_grpc_port_from_env ( ) -> u16 {
389+ std:: env:: var ( DAPR_GRPC_PORT_ENV )
390+ . unwrap_or_else ( |_| DEFAULT_DAPR_GRPC_PORT . to_string ( ) )
391+ . parse ( )
392+ . unwrap ( )
393+ }
394+
395+ #[ cfg( test) ]
396+ mod tests {
397+ use super :: * ;
398+ use std:: sync:: Mutex ;
399+
400+ static ENV_LOCK : Mutex < ( ) > = Mutex :: new ( ( ) ) ;
401+
402+ fn with_env < F : FnOnce ( ) > ( key : & str , value : Option < & str > , f : F ) {
403+ let _guard = ENV_LOCK . lock ( ) . unwrap ( ) ;
404+ let prev = std:: env:: var ( key) . ok ( ) ;
405+ match value {
406+ Some ( value) => unsafe { std:: env:: set_var ( key, value) } ,
407+ None => unsafe { std:: env:: remove_var ( key) } ,
408+ }
409+
410+ let result = std:: panic:: catch_unwind ( std:: panic:: AssertUnwindSafe ( f) ) ;
411+
412+ match prev {
413+ Some ( value) => unsafe { std:: env:: set_var ( key, value) } ,
414+ None => unsafe { std:: env:: remove_var ( key) } ,
415+ }
416+
417+ if let Err ( err) = result {
418+ std:: panic:: resume_unwind ( err) ;
419+ }
420+ }
421+
422+ #[ test]
423+ fn http_server_defaults_to_standard_dapr_grpc_port ( ) {
424+ with_env ( DAPR_GRPC_PORT_ENV , None , || {
425+ assert_eq ! ( dapr_grpc_port_from_env( ) , DEFAULT_DAPR_GRPC_PORT ) ;
426+ } ) ;
427+ }
428+
429+ #[ test]
430+ fn http_server_honors_dapr_grpc_port_override ( ) {
431+ with_env ( DAPR_GRPC_PORT_ENV , Some ( "12345" ) , || {
432+ assert_eq ! ( dapr_grpc_port_from_env( ) , 12345 ) ;
433+ } ) ;
434+ }
435+ }
0 commit comments