Skip to content

Commit 372c8be

Browse files
committed
Implement re-try logic for TestOrchestratorServer port binding
Signed-off-by: Mateus Devino <mdevino@ibm.com>
1 parent 19be26e commit 372c8be

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

tests/common/orchestrator.rs

+28-14
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ use futures::{
3333
},
3434
};
3535
use mocktail::server::MockServer;
36-
use rand::Rng;
36+
use rand::{Rng, SeedableRng, rngs::SmallRng};
3737
use rustls::crypto::ring;
3838
use serde::{Serialize, de::DeserializeOwned};
39-
use tokio::task::JoinHandle;
39+
use tracing::info;
4040
use url::Url;
4141

4242
// Default orchestrator configuration file for integration tests.
@@ -149,7 +149,6 @@ pub struct TestOrchestratorServer {
149149
base_url: Url,
150150
health_url: Url,
151151
client: reqwest::Client,
152-
_handle: Option<JoinHandle<Result<(), anyhow::Error>>>,
153152
}
154153

155154
impl TestOrchestratorServer {
@@ -164,7 +163,6 @@ impl TestOrchestratorServer {
164163
base_url,
165164
health_url,
166165
client,
167-
_handle: None,
168166
}
169167
}
170168

@@ -174,23 +172,39 @@ impl TestOrchestratorServer {
174172

175173
/// Starts the orchestrator server.
176174
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(
183184
http_addr,
184185
health_http_addr,
185186
None,
186187
None,
187188
None,
188189
orchestrator,
189190
)
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+
}
194208

195209
// Give the server time to become ready.
196210
tokio::time::sleep(Duration::from_millis(10)).await;

0 commit comments

Comments
 (0)