@@ -33,10 +33,10 @@ use futures::{
33
33
} ,
34
34
} ;
35
35
use mocktail:: server:: MockServer ;
36
- use rand:: Rng ;
36
+ use rand:: { Rng , SeedableRng , rngs :: SmallRng } ;
37
37
use rustls:: crypto:: ring;
38
38
use serde:: { Serialize , de:: DeserializeOwned } ;
39
- use tokio :: task :: JoinHandle ;
39
+ use tracing :: info ;
40
40
use url:: Url ;
41
41
42
42
// Default orchestrator configuration file for integration tests.
@@ -149,7 +149,6 @@ pub struct TestOrchestratorServer {
149
149
base_url : Url ,
150
150
health_url : Url ,
151
151
client : reqwest:: Client ,
152
- _handle : Option < JoinHandle < Result < ( ) , anyhow:: Error > > > ,
153
152
}
154
153
155
154
impl TestOrchestratorServer {
@@ -164,7 +163,6 @@ impl TestOrchestratorServer {
164
163
base_url,
165
164
health_url,
166
165
client,
167
- _handle : None ,
168
166
}
169
167
}
170
168
@@ -174,23 +172,39 @@ impl TestOrchestratorServer {
174
172
175
173
/// Starts the orchestrator server.
176
174
pub async fn start ( & mut self ) -> Result < ( ) , anyhow:: Error > {
177
- let orchestrator = Orchestrator :: new ( self . config . clone ( ) , false ) . await ?;
178
- let http_addr = SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 0 , 0 , 0 , 0 ) ) , self . port ) ;
179
- let health_http_addr: SocketAddr =
180
- SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 0 , 0 , 0 , 0 ) ) , self . health_port ) ;
181
- let handle = tokio:: spawn ( async move {
182
- fms_guardrails_orchestr8:: server:: run (
175
+ let mut rng = SmallRng :: from_os_rng ( ) ;
176
+ loop {
177
+ let port = rng. random_range ( 10000 ..60000 ) ;
178
+ let health_port = rng. random_range ( 10000 ..60000 ) ;
179
+ let orchestrator = Orchestrator :: new ( self . config . clone ( ) , false ) . await ?;
180
+ let http_addr = SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 0 , 0 , 0 , 0 ) ) , port) ;
181
+ let health_http_addr: SocketAddr =
182
+ SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 0 , 0 , 0 , 0 ) ) , health_port) ;
183
+ let handle = fms_guardrails_orchestr8:: server:: run (
183
184
http_addr,
184
185
health_http_addr,
185
186
None ,
186
187
None ,
187
188
None ,
188
189
orchestrator,
189
190
)
190
- . await ?;
191
- Ok :: < ( ) , anyhow:: Error > ( ( ) )
192
- } ) ;
193
- self . _handle = Some ( handle) ;
191
+ . await ;
192
+
193
+ if handle. is_ok ( ) {
194
+ self . port = port;
195
+ self . health_port = health_port;
196
+ self . base_url = Url :: parse ( & format ! ( "http://0.0.0.0:{port}" ) ) . unwrap ( ) ;
197
+ self . health_url =
198
+ Url :: parse ( & format ! ( "http://0.0.0.0:{health_port}/health" ) ) . unwrap ( ) ;
199
+ info ! (
200
+ "TestOrchestratorServer started successfully on ports {port} (guardrails) and {health_port} (health)"
201
+ ) ;
202
+ break ;
203
+ }
204
+ info ! (
205
+ "Failed to bind TestOrchestratorServer to ports {port} (guardrails) and {health_port} (health). Trying again using different ports..."
206
+ ) ;
207
+ }
194
208
195
209
// Give the server time to become ready.
196
210
tokio:: time:: sleep ( Duration :: from_millis ( 10 ) ) . await ;
0 commit comments