From b175f61fda3594875f925df49931ba3044fa9c6b Mon Sep 17 00:00:00 2001 From: yoriswind Date: Fri, 20 Nov 2026 09:26:00 -0800 Subject: [PATCH] test(control): hold a non-listening port reservation --- Cargo.lock | 1 + crates/control/Cargo.toml | 4 ++ crates/control/src/client_tests.rs | 63 +++++++++++------------------- 3 files changed, 27 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6223511b3a..8f913e195c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1720,6 +1720,7 @@ dependencies = [ "proptest", "serde", "serde_json", + "socket2", "tempfile", ] diff --git a/crates/control/Cargo.toml b/crates/control/Cargo.toml index 3e756aff75..cc5f7b10a5 100644 --- a/crates/control/Cargo.toml +++ b/crates/control/Cargo.toml @@ -36,6 +36,10 @@ log = { workspace = true, optional = true } proptest = { workspace = true } tempfile = "3" +[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] +# Reserve an unused control port without opening a listener, including between assertions. +socket2 = "0.6.5" + [[test]] name = "agents" required-features = ["agents"] diff --git a/crates/control/src/client_tests.rs b/crates/control/src/client_tests.rs index ea41cd56fc..e19aa62302 100644 --- a/crates/control/src/client_tests.rs +++ b/crates/control/src/client_tests.rs @@ -1,6 +1,7 @@ //! The client against the shared server ([`crate::server::Hub`]) on an ephemeral loopback port. use super::*; +use socket2::{Domain, Protocol, Socket, Type}; use crate::ControlRequest; use crate::server::{Hub, Protocol, Stop, Summary, Waits as ServerWaits}; use std::ffi::OsString; @@ -78,36 +79,14 @@ fn addr(port: u16) -> String { format!("127.0.0.1:{port}") } -/// A candidate port: another process may bind it after this listener is dropped. -fn free_port() -> u16 { - TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port() -} - -/// Check a closed port, trying another candidate only when a successful TCP connection proves -/// that a listener took this one. A wrong error for a port we never reached still fails. -fn assert_not_running_on_unused_port(mut next_port: impl FnMut() -> u16) -> (u16, Vec) { - let mut occupied = Vec::new(); - for _ in 0..10 { - let port = next_port(); - let mut c = Connection::new(APP, &addr(port), Some(TOKEN)); - let result = c.call(Request::new("document.inspect", json!({}))); - if c.serial() != 0 { - occupied.push(port); - continue; - } - let e = result.unwrap_err(); - let CallError::NotRunning(m) = &e else { panic!("{e:?}") }; - assert!(m.starts_with(&format!("no Probe app is listening at 127.0.0.1:{port}")), "{m}"); - assert!(m.contains(&format!("`probe --control {port}`")), "{m}"); - let connected = c.try_connect(); - if c.serial() != 0 { - occupied.push(port); - continue; - } - assert_eq!(connected.unwrap_err().kind(), std::io::ErrorKind::NotConnected); - return (port, occupied); - } - panic!("another listener took every candidate port"); +/// Keep this port bound without listening, so another normal test listener cannot take it. +fn reserve_non_listening_port() -> (Socket, u16) { + let reserved = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP)).unwrap(); + let address = std::net::SocketAddr::from(([127, 0, 0, 1], 0)); + reserved.bind(&address.into()).unwrap(); + let port = reserved.local_addr().unwrap().as_socket().unwrap().port(); + assert_ne!(port, 0); + (reserved, port) } #[test] @@ -142,20 +121,22 @@ fn a_wrong_token_or_none_is_refused() { #[test] fn an_app_not_running_says_how_to_start_it() { - assert_not_running_on_unused_port(free_port); + let (reserved, port) = reserve_non_listening_port(); + let mut c = Connection::new(APP, &addr(port), Some(TOKEN)); + let e = c.call(Request::new("document.inspect", json!({}))).unwrap_err(); + let CallError::NotRunning(m) = &e else { panic!("{e:?}") }; + assert!(m.starts_with(&format!("no Probe app is listening at 127.0.0.1:{port}")), "{m}"); + assert!(m.contains(&format!("`probe --control {port}`")), "{m}"); + assert_eq!(c.try_connect().unwrap_err().kind(), std::io::ErrorKind::NotConnected); + drop(reserved); } #[test] -fn the_not_running_fixture_retries_a_port_taken_by_another_server() { - let rival = serve(0, Duration::ZERO, Duration::from_secs(10)); - let mut candidates = 0; - let (port, occupied) = assert_not_running_on_unused_port(|| { - candidates += 1; - if candidates == 1 { rival.port() } else { free_port() } - }); - assert!(candidates >= 2); - assert_eq!(occupied.first(), Some(&rival.port())); - assert_ne!(port, rival.port()); +fn the_not_running_fixture_keeps_its_port_from_another_listener() { + let (reserved, port) = reserve_non_listening_port(); + let rival = TcpListener::bind(("127.0.0.1", port)); + assert!(rival.is_err(), "another listener acquired the reserved port"); + drop(reserved); } /// An app that goes away during a call is lost, not "not running"; the next call says it is not