F: make the multi-address tcp test independent of /etc/hosts

This commit is contained in:
ari committed 2026-10-20 15:16:56 -04:00
1 parent 4f388ccc92
commit 2af03308ba
1 file changed
+48 -37
+48 -37
View File
@@ -525,6 +525,34 @@ impl Stream for CappedStream {
}
}
/// Dial the addresses in order until one accepts. A peer name may resolve to
/// an address family the peer does not listen on, so a refusal on the first
/// address must not decide the check.
async fn connect_any(
addrs: &[SocketAddr],
deadline: tokio::time::Instant,
) -> Result<Duration, PeerError> {
let mut left = addrs.len();
let mut last = PeerError::Unreachable;
for &addr in addrs {
// Each address gets its share of what remains, so a blackholed first
// address cannot eat the deadline the others need.
let now = tokio::time::Instant::now();
let share = deadline.saturating_duration_since(now) / left as u32;
left -= 1;
let started = tokio::time::Instant::now();
match tokio::time::timeout(share, tokio::net::TcpStream::connect(addr)).await {
Ok(Ok(stream)) => {
drop(stream);
return Ok(started.elapsed());
}
Ok(Err(err)) => last = map_io(err),
Err(_) => last = PeerError::Timeout,
}
}
Err(last)
}
#[async_trait]
impl PeerTransport for ReqwestTransport {
async fn tcp_check(&self, peer: &PeerConfig, timeout: Duration) -> Result<Duration, PeerError> {
@@ -532,25 +560,7 @@ impl PeerTransport for ReqwestTransport {
within(deadline, async {
let deny = self.deny_list();
let (_, addrs) = resolve_target(peer, &deny).await?;
let mut left = addrs.len();
let mut last = PeerError::Unreachable;
for addr in addrs {
// Each address gets its share of what remains, so a blackholed
// first address cannot eat the deadline the others need.
let now = tokio::time::Instant::now();
let share = deadline.saturating_duration_since(now) / left as u32;
left -= 1;
let started = tokio::time::Instant::now();
match tokio::time::timeout(share, tokio::net::TcpStream::connect(addr)).await {
Ok(Ok(stream)) => {
drop(stream);
return Ok(started.elapsed());
}
Ok(Err(err)) => last = map_io(err),
Err(_) => last = PeerError::Timeout,
}
}
Err(last)
connect_any(&addrs, deadline).await
})
.await
}
@@ -1245,16 +1255,16 @@ mod tests {
}
/// A peer declared by name must be reached on the family it listens on:
/// the listener takes one family only, so whichever family the resolver
/// puts first, the other one has to be tried as well.
/// the listener takes one family only, so an address of the other family
/// has to be tried and refused before the reachable one. Driven with an
/// explicit address list, because what `localhost` resolves to depends on
/// /etc/hosts.
#[tokio::test]
async fn tcp_check_tries_every_resolved_address() {
let token = write_token("t", 0o600);
let transport = ReqwestTransport::new(&limits(4, 8, 1024)).unwrap();
let mut tried = Vec::new();
let mut served = Vec::new();
for bind in ["127.0.0.1", "[::1]"] {
let Ok(listener) = TcpListener::bind(format!("{bind}:0")).await else {
continue; // this environment has no IPv6 loopback
continue; // this environment has no loopback of that family
};
let port = listener.local_addr().unwrap().port();
tokio::spawn(async move {
@@ -1267,19 +1277,20 @@ mod tests {
.await;
}
});
let mut p = peer(0, &token, None);
p.url = url(&format!("http://localhost:{port}"));
assert!(
transport
.tcp_check(&p, Duration::from_secs(2))
.await
.is_ok(),
"{bind}"
);
tried.push(bind);
let live: SocketAddr = format!("{bind}:{port}").parse().unwrap();
// Nothing listens on that port on any other loopback, so a check
// that gave up on the first address would fail here.
let dead: Vec<SocketAddr> = ["127.0.0.1", "[::1]"]
.iter()
.filter(|other| **other != bind)
.filter_map(|other| format!("{other}:{port}").parse().ok())
.collect();
let addrs: Vec<SocketAddr> = dead.into_iter().chain([live]).collect();
let deadline = tokio::time::Instant::now() + Duration::from_secs(2);
assert!(connect_any(&addrs, deadline).await.is_ok(), "{addrs:?}");
served.push(bind);
}
assert!(!tried.is_empty(), "no loopback family available");
let _ = std::fs::remove_file(&token);
assert!(!served.is_empty(), "no loopback address to listen on");
}
/// The request timeout bounds preparation, not a follow stream: chunks