From 0def1be1f5473acbd184fe6cedbbedc6853637e7 Mon Sep 17 00:00:00 2001 From: Arihant Deva Date: Sat, 28 Nov 2026 20:56:14 -0500 Subject: [PATCH] test(net): stop asserting that a stop() worked without waiting to see Both of these tests fire a stop at 200ms (or after a 100ms id_wait) against a route that never answers, then assert only that `send()`/`post_watched()` returned an error. The read timeout produces that same error, so both pass unchanged when `StopHandle::stop()` is emptied out -- verified by emptying it: stop_handle_unblocks_a_blocked_read ok, 7.02s dead_reader_during_prefill_stops_after_id_wait ok, 5.01s A test named for an unblocked read that also passes on a timed-out one is not covering the thing it is named for. Each now measures how long the call took: ~2s when the socket is shut down, ~7s / ~5s when it is not, so a broken stop fails instead of quietly passing later. Thresholds sit between those two bands (4s and 2s) rather than near either, and the read timeout stays at 30s. The route sleeps drop from 30s to 2s. They only need to hold the read open past the 200ms stop / 100ms id_wait; everything after that is teardown, and `ServerHandle::stop()` joins every connection thread before returning (see `bind_and_run`), so a handler parked for the full read timeout makes teardown wait out a deadline the assertion already met. Lib suite: 31.8s -> 6.8s, stable across 12 consecutive runs. `Instant` was already in scope via `use super::*`. --- src/net/http1.rs | 18 +++++++++++++++++- src/net/upstream_call.rs | 20 +++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/net/http1.rs b/src/net/http1.rs index 1e2ec4dc..7bdc0cc4 100644 --- a/src/net/http1.rs +++ b/src/net/http1.rs @@ -2067,9 +2067,18 @@ mod tests { // A server route that never answers; stop_handle().stop() must // unblock the client's read instead of hanging until the read // timeout. + // + // The handler sleep only keeps the read blocked; what has to hold is + // that `send()` returns *before* the read timeout. Asserting + // `result.is_err()` alone does not say that: the read timeout produces + // the same `Err`, so a `stop()` that does nothing still passes it once + // the read runs out. The elapsed check below is what separates an + // unblocked read from a timed-out one -- measured here, a void `stop()` + // takes ~7s to reach the same `Err`, against ~2s when the socket is + // actually shut down. let mut server = Server::new(); server.route("GET", "/hang", |_req, _res, _peer| { - std::thread::sleep(Duration::from_secs(30)); + std::thread::sleep(Duration::from_secs(2)); }); let (mut handle, port) = server.bind_and_run("127.0.0.1").unwrap(); @@ -2084,8 +2093,15 @@ mod tests { thread::sleep(Duration::from_millis(200)); stop.stop(); }); + let started = Instant::now(); let result = client.send(&Request::get("/hang"), None, None); + let elapsed = started.elapsed(); assert!(result.is_err()); + assert!( + elapsed < Duration::from_secs(4), + "stop() must unblock the read, not let it run to the 30s read \ + timeout; send() returned after {elapsed:?}" + ); stopper.join().unwrap(); handle.stop(); } diff --git a/src/net/upstream_call.rs b/src/net/upstream_call.rs index cd3113ca..cc16be20 100644 --- a/src/net/upstream_call.rs +++ b/src/net/upstream_call.rs @@ -497,9 +497,14 @@ mod tests { // The server never answers; reader_gone is true from the very start // (prefill), and id_wait is short, so the watch must stop the // upstream and name the abandon with an empty request id. + // + // The handler sleep only needs to outlast the 100ms `id_wait` so the + // abandon is named while the read is still blocked. `ServerHandle::stop()` + // joins every connection thread, so matching it to the read timeout + // makes teardown wait out a deadline the assertion already met. let mut server = Server::new(); server.route("POST", "/chat", |_req, _res, _peer| { - std::thread::sleep(Duration::from_secs(30)); + std::thread::sleep(Duration::from_secs(2)); }); let (mut handle, port) = server.bind_and_run("127.0.0.1").unwrap(); let pool = UpstreamPool::new(options(format!("http://127.0.0.1:{port}"))); @@ -523,7 +528,9 @@ mod tests { id_wait: Duration::from_millis(100), ..Default::default() }; + let started = Instant::now(); let result = post_watched(&mut lease, call); + let elapsed = started.elapsed(); assert!(result.abandoned); assert!(!result.received_any); @@ -534,6 +541,17 @@ mod tests { calls[0].2, "the reader left before headers arrived: a prefill abandon" ); + drop(calls); + // The abandon is named after `id_wait` (100ms) either way; what the + // watch's `stop_handle.stop()` adds is that the read does not run on to + // the client's read timeout. `post_watched` returns only once that read + // ends, so the elapsed time is the evidence: ~200ms when the socket is + // dropped, ~5s (the test client's read timeout) when it is not. + assert!( + elapsed < Duration::from_secs(2), + "the watch must stop the upstream instead of letting the read run \ + to the client's read timeout; post_watched returned after {elapsed:?}" + ); handle.stop(); }