diff --git a/src/client/legacy/connect/http.rs b/src/client/legacy/connect/http.rs index d6c59b7e..cc3e4cee 100644 --- a/src/client/legacy/connect/http.rs +++ b/src/client/legacy/connect/http.rs @@ -1184,226 +1184,6 @@ mod tests { .await; } - #[test] - #[ignore] // TODO - #[cfg_attr(not(feature = "__internal_happy_eyeballs_tests"), ignore)] - fn client_happy_eyeballs() { - use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, TcpListener}; - use std::time::{Duration, Instant}; - - use super::ConnectingTcp; - use super::dns; - - let server4 = TcpListener::bind("127.0.0.1:0").unwrap(); - let addr = server4.local_addr().unwrap(); - let _server6 = TcpListener::bind(format!("[::1]:{}", addr.port())).unwrap(); - let rt = tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .unwrap(); - - let local_timeout = Duration::default(); - let unreachable_v4_timeout = measure_connect(unreachable_ipv4_addr()).1; - let unreachable_v6_timeout = measure_connect(unreachable_ipv6_addr()).1; - let fallback_timeout = std::cmp::max(unreachable_v4_timeout, unreachable_v6_timeout) - + Duration::from_millis(250); - - let scenarios = &[ - // Fast primary, without fallback. - (&[local_ipv4_addr()][..], 4, local_timeout, false), - (&[local_ipv6_addr()][..], 6, local_timeout, false), - // Fast primary, with (unused) fallback. - ( - &[local_ipv4_addr(), local_ipv6_addr()][..], - 4, - local_timeout, - false, - ), - ( - &[local_ipv6_addr(), local_ipv4_addr()][..], - 6, - local_timeout, - false, - ), - // Unreachable + fast primary, without fallback. - ( - &[unreachable_ipv4_addr(), local_ipv4_addr()][..], - 4, - unreachable_v4_timeout, - false, - ), - ( - &[unreachable_ipv6_addr(), local_ipv6_addr()][..], - 6, - unreachable_v6_timeout, - false, - ), - // Unreachable + fast primary, with (unused) fallback. - ( - &[ - unreachable_ipv4_addr(), - local_ipv4_addr(), - local_ipv6_addr(), - ][..], - 4, - unreachable_v4_timeout, - false, - ), - ( - &[ - unreachable_ipv6_addr(), - local_ipv6_addr(), - local_ipv4_addr(), - ][..], - 6, - unreachable_v6_timeout, - true, - ), - // Slow primary, with (used) fallback. - ( - &[slow_ipv4_addr(), local_ipv4_addr(), local_ipv6_addr()][..], - 6, - fallback_timeout, - false, - ), - ( - &[slow_ipv6_addr(), local_ipv6_addr(), local_ipv4_addr()][..], - 4, - fallback_timeout, - true, - ), - // Slow primary, with (used) unreachable + fast fallback. - ( - &[slow_ipv4_addr(), unreachable_ipv6_addr(), local_ipv6_addr()][..], - 6, - fallback_timeout + unreachable_v6_timeout, - false, - ), - ( - &[slow_ipv6_addr(), unreachable_ipv4_addr(), local_ipv4_addr()][..], - 4, - fallback_timeout + unreachable_v4_timeout, - true, - ), - ]; - - // Scenarios for IPv6 -> IPv4 fallback require that host can access IPv6 network. - // Otherwise, connection to "slow" IPv6 address will error-out immediately. - let ipv6_accessible = measure_connect(slow_ipv6_addr()).0; - - for &(hosts, family, timeout, needs_ipv6_access) in scenarios { - if needs_ipv6_access && !ipv6_accessible { - continue; - } - - let (start, stream) = rt - .block_on(async move { - let addrs = hosts - .iter() - .map(|host| (*host, addr.port()).into()) - .collect(); - let cfg = Config { - local_address_ipv4: None, - local_address_ipv6: None, - connect_timeout: None, - tcp_keepalive_config: TcpKeepaliveConfig::default(), - happy_eyeballs_timeout: Some(fallback_timeout), - nodelay: false, - reuse_address: false, - enforce_http: false, - send_buffer_size: None, - recv_buffer_size: None, - #[cfg(any( - target_os = "android", - target_os = "fuchsia", - target_os = "linux" - ))] - interface: None, - #[cfg(any( - target_os = "illumos", - target_os = "ios", - target_os = "macos", - target_os = "solaris", - target_os = "tvos", - target_os = "visionos", - target_os = "watchos", - ))] - interface: None, - #[cfg(any( - target_os = "android", - target_os = "fuchsia", - target_os = "linux" - ))] - mark: None, - #[cfg(any( - target_os = "android", - target_os = "fuchsia", - target_os = "linux" - ))] - tcp_user_timeout: None, - }; - let connecting_tcp = ConnectingTcp::new(dns::SocketAddrs::new(addrs), &cfg); - let start = Instant::now(); - Ok::<_, ConnectError>((start, ConnectingTcp::connect(connecting_tcp).await?)) - }) - .unwrap(); - let res = if stream.peer_addr().unwrap().is_ipv4() { - 4 - } else { - 6 - }; - let duration = start.elapsed(); - - // Allow actual duration to be +/- 150ms off. - let min_duration = if timeout >= Duration::from_millis(150) { - timeout - Duration::from_millis(150) - } else { - Duration::default() - }; - let max_duration = timeout + Duration::from_millis(150); - - assert_eq!(res, family); - assert!(duration >= min_duration); - assert!(duration <= max_duration); - } - - fn local_ipv4_addr() -> IpAddr { - Ipv4Addr::new(127, 0, 0, 1).into() - } - - fn local_ipv6_addr() -> IpAddr { - Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).into() - } - - fn unreachable_ipv4_addr() -> IpAddr { - Ipv4Addr::new(127, 0, 0, 2).into() - } - - fn unreachable_ipv6_addr() -> IpAddr { - Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2).into() - } - - fn slow_ipv4_addr() -> IpAddr { - // RFC 6890 reserved IPv4 address. - Ipv4Addr::new(198, 18, 0, 25).into() - } - - fn slow_ipv6_addr() -> IpAddr { - // RFC 6890 reserved IPv6 address. - Ipv6Addr::new(2001, 2, 0, 0, 0, 0, 0, 254).into() - } - - fn measure_connect(addr: IpAddr) -> (bool, Duration) { - let start = Instant::now(); - let result = - std::net::TcpStream::connect_timeout(&(addr, 80).into(), Duration::from_secs(1)); - - let reachable = result.is_ok() || result.unwrap_err().kind() == io::ErrorKind::TimedOut; - let duration = start.elapsed(); - (reachable, duration) - } - } - use std::time::Duration; #[test]