From d19fc13be16d67f1e01a858ecc9d1486c8e6cb55 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Tue, 16 Jul 2024 10:19:42 -0400 Subject: [PATCH] Allow for retries when checking if the emulator is running (#1459) 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. --- .../tests/test/emulator_tests.rs | 48 ++++++++++++++----- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/cmd/crates/stellar-ledger/tests/test/emulator_tests.rs b/cmd/crates/stellar-ledger/tests/test/emulator_tests.rs index a1243befe..d22830089 100644 --- a/cmd/crates/stellar-ledger/tests/test/emulator_tests.rs +++ b/cmd/crates/stellar-ledger/tests/test/emulator_tests.rs @@ -343,30 +343,52 @@ fn get_http_transport(host: &str, port: u16) -> Result { } 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 { + // 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 { let client = reqwest::Client::new(); - let resp = client - .get(format!("http://localhost:{ui_host_port}/events")) - .send() - .await - .unwrap() - .json::() - .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::().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) {