Skip to content

Commit

Permalink
Allow for retries when checking if the emulator is running (#1459)
Browse files Browse the repository at this point in the history
The reason we were experiencing intermittent failures with these tests is
because when checking if the emulator was fully up and running,
sometimes the container wouldn't even be started yet, and would panic in
the `wait_for_emulator_start_text` fn. This change adds a retry with
exponential backoff so if the container isn't up yet, and the request
fails, it will try again after waiting.
  • Loading branch information
elizabethengelman authored Jul 16, 2024
1 parent 7ba2513 commit d19fc13
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions cmd/crates/stellar-ledger/tests/test/emulator_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,30 +343,52 @@ fn get_http_transport(host: &str, port: u16) -> Result<impl Exchange, Error> {
}

async fn wait_for_emulator_start_text(ui_host_port: u16) {
sleep(Duration::from_secs(1)).await;

let mut ready = false;
while !ready {
let events = get_emulator_events(ui_host_port).await;
let events = get_emulator_events_with_retries(ui_host_port, 5).await;

if events.iter().any(|event| event.text == "is ready") {
ready = true;
}
sleep(Duration::from_secs(1)).await;
}
}

async fn get_emulator_events(ui_host_port: u16) -> Vec<EmulatorEvent> {
// Allowing for less retries here because presumably the emulator should be up and running since we waited
// for the "is ready" text via wait_for_emulator_start_text
get_emulator_events_with_retries(ui_host_port, 1).await
}

async fn get_emulator_events_with_retries(
ui_host_port: u16,
max_retries: u16,
) -> Vec<EmulatorEvent> {
let client = reqwest::Client::new();
let resp = client
.get(format!("http://localhost:{ui_host_port}/events"))
.send()
.await
.unwrap()
.json::<EventsResponse>()
.await
.unwrap();
resp.events
let mut retries = 0;

let mut wait_time = Duration::from_secs(1);

loop {
match client
.get(format!("http://localhost:{ui_host_port}/events"))
.send()
.await
{
Ok(req) => {
let resp = req.json::<EventsResponse>().await.unwrap();
return resp.events;
}
Err(e) => {
println!("this many retries: {retries}");
retries += 1;
if retries >= max_retries {
panic!("Failed to get emulator events: {e}");
}
sleep(wait_time).await;
wait_time *= 2;
}
}
}
}

async fn approve_tx_hash_signature(ui_host_port: u16, device_model: String) {
Expand Down

0 comments on commit d19fc13

Please sign in to comment.