From cf73dec46678418ff1fb1753e570ecda0cbb2c43 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 20:55:51 -0800 Subject: [PATCH] automation: the control tests wait up to a minute for the server before failing as hung The socket tests in tests/it/control.rs bounded each read and each channel wait at 5 seconds (10 seconds for the capture client's requests). The server sends every reply, close and request there without waiting on a timer, so these bounds only guard against a hang, but a slow run could reach them: under heavy load an_oversized_reply_keeps_its_framing_and_id_and_the_connection_serves_on failed at its 5-second read timeout and passed when run again. - control.rs waits with one HANG constant of 60 seconds, the bound the agents tests already use as a hang guard (tests/it/agents.rs:53). - The client test that peeks for the server's refusal line waits up to 60 seconds too. The assertions do not change. --- crates/automation/src/client_tests.rs | 4 ++- crates/automation/tests/it/control.rs | 48 +++++++++++++++------------ 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/crates/automation/src/client_tests.rs b/crates/automation/src/client_tests.rs index 8a767ddd21..8aeee2d0bb 100644 --- a/crates/automation/src/client_tests.rs +++ b/crates/automation/src/client_tests.rs @@ -128,7 +128,9 @@ fn a_refusal_waiting_at_sign_in_is_the_reason_connect_gives() { reader.read_line(&mut line).ok().filter(|&read| read > 0).map(|_| line) }); let stream = std::net::TcpStream::connect(addr).unwrap(); - stream.set_read_timeout(Some(std::time::Duration::from_secs(5))).unwrap(); + // A hang guard only: the server thread writes the refusal without waiting on a timer, so a run that works never + // reaches the limit. + stream.set_read_timeout(Some(std::time::Duration::from_secs(60))).unwrap(); assert!(stream.peek(&mut [0]).unwrap() > 0, "the refusal is waiting before the sign-in"); let signed = ControlClient::over(stream).unwrap().sign_in(TOKEN); assert_eq!(signed.err(), Some(ConnectError::Refused("connection limit reached".into()))); diff --git a/crates/automation/tests/it/control.rs b/crates/automation/tests/it/control.rs index 6e1dc700fc..69498ee521 100644 --- a/crates/automation/tests/it/control.rs +++ b/crates/automation/tests/it/control.rs @@ -89,6 +89,10 @@ fn a_finished_job_answers_only_its_own_waiters() { const TOKEN: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; +/// How long a test waits for a reply, a close or a request before it fails as hung. A server that works sends each +/// one without waiting on a timer, so the result does not depend on this limit or on how fast the machine runs. +const HANG: Duration = Duration::from_secs(60); + fn read_json(reader: &mut impl BufRead) -> Option { let mut line = String::new(); (reader.read_line(&mut line).ok()? > 0).then(|| serde_json::from_str(&line).expect("a JSON line")) @@ -116,9 +120,9 @@ fn an_unauthenticated_connection_is_closed_at_the_deadline_whatever_it_sends() { let options = ServeOptions { auth_deadline: Duration::from_secs(10), clock, ended: Some(ended) }; let (rx, server) = server::serve_with(TcpListener::bind("127.0.0.1:0").expect("bound"), TOKEN.into(), host(), options).expect("started"); let mut stream = TcpStream::connect(server.addr).expect("connected"); - stream.set_read_timeout(Some(Duration::from_secs(5))).expect("timeout"); + stream.set_read_timeout(Some(HANG)).expect("timeout"); let mut reader = BufReader::new(stream.try_clone().expect("cloned")); - clock_read.recv_timeout(Duration::from_secs(5)).expect("the connection set its deadline"); + clock_read.recv_timeout(HANG).expect("the connection set its deadline"); // Blank lines and the start of a line, sent while the deadline is ahead: none is a frame the handshake refuses. stream.write_all(b"\n \n ").expect("written"); *at.lock().expect("the clock") = start + Duration::from_secs(10); @@ -176,9 +180,9 @@ fn serve_counting_ends() -> (Receiver, server::Listening, Receiv fn nothing_more_dispatched(rx: &Receiver, server: server::Listening, ended: &Receiver<()>, connections: usize) -> bool { drop(server); for _ in 0..connections { - ended.recv_timeout(Duration::from_secs(5)).expect("a connection ended"); + ended.recv_timeout(HANG).expect("a connection ended"); } - rx.recv_timeout(Duration::from_secs(5)).err() == Some(RecvTimeoutError::Disconnected) + rx.recv_timeout(HANG).err() == Some(RecvTimeoutError::Disconnected) } /// With `MAX_CONNECTIONS` connections open, the next one is told `connection limit reached` and closed; once one of @@ -188,7 +192,7 @@ fn connections_past_the_limit_are_refused_with_the_reason() { let (rx, server, ended) = serve_counting_ends(); let connect = || { let stream = TcpStream::connect(server.addr).expect("connected"); - stream.set_read_timeout(Some(Duration::from_secs(5))).expect("timeout"); + stream.set_read_timeout(Some(HANG)).expect("timeout"); let reader = BufReader::new(stream.try_clone().expect("cloned")); (stream, reader) }; @@ -202,7 +206,7 @@ fn connections_past_the_limit_are_refused_with_the_reason() { drop(open.pop()); // The server frees the place when it reads the close; a new connection is served once it has. - ended.recv_timeout(Duration::from_secs(5)).expect("the closed connection ended"); + ended.recv_timeout(HANG).expect("the closed connection ended"); let (mut stream, mut reader) = connect(); writeln!(stream, "{}", json!({"id": 1, "method": "auth", "params": {"token": TOKEN}})).expect("written"); let served = read_json(&mut reader).expect("a reply"); @@ -222,7 +226,7 @@ fn a_refused_first_frame_closes_the_connection() { for (first, id) in [("not json".to_owned(), Value::Null), (json!({"id": 4, "method": "auth", "params": {"token": wrong}}).to_string(), json!(4))] { let mut stream = TcpStream::connect(server.addr).expect("connected"); - stream.set_read_timeout(Some(Duration::from_secs(5))).expect("timeout"); + stream.set_read_timeout(Some(HANG)).expect("timeout"); let mut reader = BufReader::new(stream.try_clone().expect("cloned")); writeln!(stream, "{first}").expect("written"); assert_eq!(read_json(&mut reader).expect("a refusal"), json!({"id": id, "ok": false, "error": "authentication required"}), "{first}"); @@ -240,7 +244,7 @@ fn a_browser_post_never_reaches_the_app() { // The server's clock stands still, so the close comes from the refusal, not the deadline. let (rx, server, ended) = serve_counting_ends(); let mut stream = TcpStream::connect(server.addr).expect("connected"); - stream.set_read_timeout(Some(Duration::from_secs(5))).expect("timeout"); + stream.set_read_timeout(Some(HANG)).expect("timeout"); let mut reader = BufReader::new(stream.try_clone().expect("cloned")); let body = format!("{}\n{}\n", json!({"id": 1, "method": "auth", "params": {"token": TOKEN}}), json!({"id": 2, "method": "test.ping"})); write!(stream, "POST / HTTP/1.1\r\n").expect("written"); @@ -265,7 +269,7 @@ fn a_browser_post_never_reaches_the_app() { fn an_over_long_request_is_refused_and_closes_the_connection() { let (rx, server) = server::listen(0, TOKEN.into(), host()).expect("started"); let mut stream = TcpStream::connect(server.addr).expect("connected"); - stream.set_read_timeout(Some(Duration::from_secs(5))).expect("timeout"); + stream.set_read_timeout(Some(HANG)).expect("timeout"); let mut reader = BufReader::new(stream.try_clone().expect("cloned")); writeln!(stream, "{}", json!({"id": 1, "method": "auth", "params": {"token": TOKEN}})).expect("written"); assert_eq!(read_json(&mut reader).expect("a reply")["ok"], true); @@ -281,7 +285,7 @@ fn an_over_long_request_is_refused_and_closes_the_connection() { /// Answers the next request, which must be `test.ping`, with `pong`. fn answer_one_ping(rx: Receiver) -> std::thread::JoinHandle<()> { std::thread::spawn(move || { - let req = rx.recv_timeout(Duration::from_secs(5)).expect("a request"); + let req = rx.recv_timeout(HANG).expect("a request"); assert_eq!(req.method, "test.ping"); let pong: ControlResponse = json!({"ok": true, "result": "pong"}); req.reply.send(pong).expect("replied"); @@ -291,7 +295,7 @@ fn answer_one_ping(rx: Receiver) -> std::thread::JoinHandle<()> /// Authenticates with the one-frame token, sends `test.ping` and returns its reply. fn ping(addr: SocketAddr) -> Value { let mut stream = TcpStream::connect(addr).expect("connected"); - stream.set_read_timeout(Some(Duration::from_secs(5))).expect("timeout"); + stream.set_read_timeout(Some(HANG)).expect("timeout"); let mut reader = BufReader::new(stream.try_clone().expect("cloned")); writeln!(stream, "{}", json!({"id": 1, "method": "auth", "params": {"token": TOKEN}})).expect("written"); assert_eq!(read_json(&mut reader).expect("a reply")["ok"], true); @@ -326,7 +330,7 @@ fn a_challenge_authenticated_connection_is_served() { let (rx, server) = server::listen(0, TOKEN.into(), host()).expect("started"); let handler = answer_one_ping(rx); let mut stream = TcpStream::connect(server.addr).expect("connected"); - stream.set_read_timeout(Some(Duration::from_secs(5))).expect("timeout"); + stream.set_read_timeout(Some(HANG)).expect("timeout"); let mut reader = BufReader::new(stream.try_clone().expect("cloned")); let client_nonce = generate_nonce().expect("a nonce"); writeln!(stream, "{}", json!({"id": 1, "method": CHALLENGE_METHOD, "params": {"clientNonce": client_nonce}})).expect("written"); @@ -376,7 +380,7 @@ fn the_documented_capture_client_sends_setup_steps_and_capture() { let mut channel = ControlChannel::new(forwarded); let mut seen: Vec = Vec::new(); while seen.len() < 4 { - forward.send(requests.recv_timeout(Duration::from_secs(10)).expect("a request")).expect("forwarded"); + forward.send(requests.recv_timeout(HANG).expect("a request")).expect("forwarded"); let req = channel.next_request().expect("the forwarded request is dispatched"); seen.push(format!("{} {}", req.method, req.params)); let reply = match req.method.as_str() { @@ -401,7 +405,7 @@ fn the_documented_capture_client_sends_setup_steps_and_capture() { /// Connects and authenticates with the one-frame token. fn authenticated(addr: SocketAddr) -> (TcpStream, BufReader) { let mut stream = TcpStream::connect(addr).expect("connected"); - stream.set_read_timeout(Some(Duration::from_secs(5))).expect("timeout"); + stream.set_read_timeout(Some(HANG)).expect("timeout"); let mut reader = BufReader::new(stream.try_clone().expect("cloned")); writeln!(stream, "{}", json!({"id": "auth", "method": "auth", "params": {"token": TOKEN}})).expect("written"); assert_eq!(read_json(&mut reader).expect("a reply")["ok"], true); @@ -414,11 +418,11 @@ fn authenticated(addr: SocketAddr) -> (TcpStream, BufReader) { fn an_oversized_reply_keeps_its_framing_and_id_and_the_connection_serves_on() { let (rx, server) = server::listen(0, TOKEN.into(), host()).expect("started"); let handler = std::thread::spawn(move || { - let first = rx.recv_timeout(Duration::from_secs(5)).expect("a request"); + let first = rx.recv_timeout(HANG).expect("a request"); assert_eq!(first.method, "test.large"); assert!(first.deadline.is_some_and(|d| d > motion::now()), "native requests carry their reply deadline"); first.reply.send(json!({"ok": true, "result": "x".repeat(MAX_RESPONSE_BYTES)})).expect("replied"); - let second = rx.recv_timeout(Duration::from_secs(5)).expect("a request"); + let second = rx.recv_timeout(HANG).expect("a request"); assert_eq!(second.method, "test.small"); second.reply.send(json!({"ok": true, "result": "still serving"})).expect("replied"); }); @@ -441,7 +445,7 @@ fn an_oversized_reply_keeps_its_framing_and_id_and_the_connection_serves_on() { fn a_bad_json_request_gets_a_null_id_and_the_connection_serves_on() { let (rx, server) = server::listen(0, TOKEN.into(), host()).expect("started"); let handler = std::thread::spawn(move || { - let req = rx.recv_timeout(Duration::from_secs(5)).expect("a request"); + let req = rx.recv_timeout(HANG).expect("a request"); assert_eq!(req.method, "test.after"); req.reply.send(json!({"ok": true, "result": "still serving"})).expect("replied"); }); @@ -465,7 +469,7 @@ fn a_bad_json_request_gets_a_null_id_and_the_connection_serves_on() { fn a_repeated_auth_is_answered_already_authenticated_without_dispatching() { let (rx, server) = server::listen(0, TOKEN.into(), host()).expect("started"); let handler = std::thread::spawn(move || { - let req = rx.recv_timeout(Duration::from_secs(5)).expect("a request"); + let req = rx.recv_timeout(HANG).expect("a request"); assert_eq!(req.method, "test.after", "a repeated auth must not reach the app"); req.reply.send(json!({"ok": true, "result": "still serving"})).expect("replied"); }); @@ -488,7 +492,7 @@ fn a_repeated_auth_is_answered_already_authenticated_without_dispatching() { fn a_complete_json_request_at_eof_can_reach_the_app() { let (rx, server, ended) = serve_counting_ends(); let handler = std::thread::spawn(move || { - let req = rx.recv_timeout(Duration::from_secs(5)).expect("the request reached the app"); + let req = rx.recv_timeout(HANG).expect("the request reached the app"); assert_eq!(req.method, "test.once"); req.reply.send(json!({"ok": true, "result": "once"})).expect("replied"); rx @@ -515,7 +519,7 @@ fn idle_unauthenticated_connections_are_limited() { // Admission is counted by the accept thread: it is done once the next connection is answered. let refused = |addr| { let stream = TcpStream::connect(addr).expect("connected"); - stream.set_read_timeout(Some(Duration::from_secs(5))).expect("timeout"); + stream.set_read_timeout(Some(HANG)).expect("timeout"); read_json(&mut BufReader::new(stream)) }; let reply = refused(server.addr).expect("a refusal"); @@ -524,10 +528,10 @@ fn idle_unauthenticated_connections_are_limited() { drop(idle); // The freed slots go back as the closed connections are noticed. for _ in 0..MAX_CONNECTIONS { - ended.recv_timeout(Duration::from_secs(5)).expect("a closed connection ended"); + ended.recv_timeout(HANG).expect("a closed connection ended"); } let mut stream = TcpStream::connect(server.addr).expect("connected"); - stream.set_read_timeout(Some(Duration::from_secs(5))).expect("timeout"); + stream.set_read_timeout(Some(HANG)).expect("timeout"); let mut reader = BufReader::new(stream.try_clone().expect("cloned")); writeln!(stream, "{}", json!({"id": "auth", "method": "auth", "params": {"token": TOKEN}})).expect("written"); assert_eq!(read_json(&mut reader).expect("a reply")["ok"], true, "a freed slot is admitted again");