Skip to content

Commit

Permalink
Merge pull request #2134 from rajatjindal/retry-errors
Browse files Browse the repository at this point in the history
retry http assertions a few times before failing
  • Loading branch information
rajatjindal authored Dec 8, 2023
2 parents d98ec4e + ea8b2d1 commit 0405da7
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions crates/e2e-testing/src/http_asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,24 @@ use crate::ensure_eq;
use anyhow::Result;
use reqwest::{Method, Request, Response};
use std::str;
use std::thread::sleep;
use std::time::Duration;

pub async fn assert_status(url: &str, expected: u16) -> Result<()> {
for _ in 0..5 {
let result = assert_status_once(url, expected).await;
if result.is_ok() {
return Ok(());
}

println!("assert_status error is {:?}", result.err());
sleep(Duration::from_secs(2))
}

panic!("failed assert_status after 5 retries")
}

pub async fn assert_status_once(url: &str, expected: u16) -> Result<()> {
let resp = make_request(Method::GET, url, "").await?;
let status = resp.status();

Expand All @@ -22,6 +38,35 @@ pub async fn assert_http_response(
expected: u16,
expected_headers: &[(&str, &str)],
expected_body: Option<&str>,
) -> Result<()> {
for _ in 0..5 {
let result = assert_http_response_once(
url,
method.clone(),
body,
expected,
expected_headers,
expected_body,
)
.await;
if result.is_ok() {
return Ok(());
}

println!("assert_http_response error is {:?}", result.err());
sleep(Duration::from_secs(2))
}

panic!("failed assert_http_response after 5 retries")
}

pub async fn assert_http_response_once(
url: &str,
method: Method,
body: &str,
expected: u16,
expected_headers: &[(&str, &str)],
expected_body: Option<&str>,
) -> Result<()> {
let res = make_request(method, url, body).await?;

Expand Down

0 comments on commit 0405da7

Please sign in to comment.