test(blocklist): decouple retry tests from RETRY_DELAYS_SECS length

Derive both the flaky-server drop count and the zero-delay schedule
from RETRY_DELAYS_SECS.len() so the tests keep exercising their
intended invariants — "succeeds on final attempt" and "gives up after
all attempts fail" — if the production retry schedule ever changes.

Also: rename fail_first → drop_first_n to match drop(sock); swap the
giveup test's empty body for an "unreachable" sentinel so a regression
that accidentally served couldn't silently match Some("").
This commit is contained in:
Razvan Dimescu
2026-04-20 19:11:53 +03:00
parent 5b1642c6dc
commit 8bed7c4649

View File

@@ -437,11 +437,11 @@ mod retry_tests {
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener; use tokio::net::TcpListener;
async fn flaky_http_server(fail_first: usize, body: &'static str) -> SocketAddr { async fn flaky_http_server(drop_first_n: usize, body: &'static str) -> SocketAddr {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap(); let addr = listener.local_addr().unwrap();
tokio::spawn(async move { tokio::spawn(async move {
for _ in 0..fail_first { for _ in 0..drop_first_n {
if let Ok((sock, _)) = listener.accept().await { if let Ok((sock, _)) = listener.accept().await {
drop(sock); drop(sock);
} }
@@ -466,22 +466,28 @@ mod retry_tests {
addr addr
} }
fn zero_delays() -> Vec<u64> {
vec![0; RETRY_DELAYS_SECS.len()]
}
#[tokio::test] #[tokio::test]
async fn retry_succeeds_after_transient_failure() { async fn retry_succeeds_on_final_attempt() {
let body = "ads.example.com\ntracker.example.net\n"; let body = "ads.example.com\ntracker.example.net\n";
let addr = flaky_http_server(2, body).await; let delays = zero_delays();
let addr = flaky_http_server(delays.len(), body).await;
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let url = format!("http://{addr}/"); let url = format!("http://{addr}/");
let result = fetch_with_retry_delays(&client, &url, &[0, 0, 0]).await; let result = fetch_with_retry_delays(&client, &url, &delays).await;
assert_eq!(result.as_deref(), Some(body)); assert_eq!(result.as_deref(), Some(body));
} }
#[tokio::test] #[tokio::test]
async fn retry_gives_up_when_all_attempts_fail() { async fn retry_gives_up_when_all_attempts_fail() {
let addr = flaky_http_server(10, "").await; let delays = zero_delays();
let addr = flaky_http_server(delays.len() + 2, "unreachable").await;
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let url = format!("http://{addr}/"); let url = format!("http://{addr}/");
let result = fetch_with_retry_delays(&client, &url, &[0, 0, 0]).await; let result = fetch_with_retry_delays(&client, &url, &delays).await;
assert_eq!(result, None); assert_eq!(result, None);
} }
} }