Retry transient 400s with body logging; garage reserve default 27
All checks were successful
build / build-test (push) Successful in 1m47s

This commit is contained in:
Jan-Peter von Hunnius 2026-09-29 09:25:47 +02:00
parent 94127bec46
commit 0b1a710ffa
4 changed files with 103 additions and 14 deletions

View file

@ -69,6 +69,57 @@ async fn test_get_file_server_error_retries() {
m_ok.assert();
}
#[tokio::test]
async fn test_get_file_bad_request_retries_then_recovers() {
let mut server = Server::new_async().await;
// The records API occasionally serves transient 400s — return 400 three
// times, then 200. Three requests must hit the 400 mock.
let m_fail = server
.mock("GET", "/data.json")
.with_status(400)
.with_body("upstream overloaded")
.expect(3)
.create();
let m_ok = mock_success(&mut server, "recovered");
let dir = tempfile::tempdir().unwrap();
let out_path = dir.path().join("output.txt");
let url = format!("{}/data.json", server.url());
get_file(&url, out_path.to_str().unwrap()).await.unwrap();
assert_eq!(fs::read_to_string(&out_path).unwrap(), "recovered");
m_fail.assert();
m_ok.assert();
}
#[tokio::test]
async fn test_get_file_persistent_bad_request_fails_after_retries() {
let mut server = Server::new_async().await;
// 400 every time — should retry 3 times then fail with the body in the message
let m = server
.mock("GET", "/data.json")
.with_status(400)
.with_body("upstream overloaded")
.expect(4) // 1 initial + 3 retries
.create();
let dir = tempfile::tempdir().unwrap();
let out_path = dir.path().join("output.txt");
let url = format!("{}/data.json", server.url());
let result = get_file(&url, out_path.to_str().unwrap()).await;
assert!(result.is_err(), "Persistent 400 should fail after retries");
let err_msg = format!("{}", result.unwrap_err());
assert!(err_msg.contains("400"), "Error should mention HTTP 400: {err_msg}");
assert!(
err_msg.contains("upstream overloaded"),
"Error should carry the response body: {err_msg}"
);
assert!(!out_path.exists(), "No file should be written on persistent failure");
m.assert();
}
#[tokio::test]
async fn test_get_file_client_error_fails_immediately() {
let mut server = Server::new_async().await;