Tests: only closure errors mean the control peer closed (#6448)

CodeRabbit pointed out that read_line returns InvalidData for bytes that are not UTF-8 while the socket stays open, and the helper treated every non-timeout error as a close. closed_by_peer() now accepts only ConnectionReset, ConnectionAborted, NotConnected and BrokenPipe, and the regression test pins InvalidData, TimedOut and WouldBlock as still open; the new assertion fails without the stricter match.

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
batistalucasvandQwen-Coder committed 2026-11-12 16:42:47 -03:00
1 parent 56a28f33b7
commit bd188c19d8
1 file changed
+12 -4
+12 -4
View File
@@ -397,11 +397,18 @@ mod tests {
/// Whether a read means the peer closed the connection. End of stream does; so does a reset,
/// which is how Windows can deliver a close when the writer had nothing to read back (the
/// Windows Print job failed here). Only a timeout means the connection is still open.
/// Windows Print job failed here). Any other error, such as `InvalidData` for bytes that are
/// not UTF-8, leaves the connection open.
fn closed_by_peer(read: std::io::Result<usize>) -> bool {
match read {
Ok(n) => n == 0,
Err(e) => !matches!(e.kind(), std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock),
Err(e) => matches!(
e.kind(),
std::io::ErrorKind::ConnectionReset
| std::io::ErrorKind::ConnectionAborted
| std::io::ErrorKind::NotConnected
| std::io::ErrorKind::BrokenPipe
),
}
}
@@ -487,14 +494,15 @@ mod tests {
}
#[test]
fn a_reset_counts_as_closed_but_a_timeout_does_not() {
fn only_closure_errors_mean_the_peer_closed() {
use std::io::{Error, ErrorKind};
assert!(closed_by_peer(Ok(0)), "end of stream");
for kind in [ErrorKind::ConnectionReset, ErrorKind::ConnectionAborted, ErrorKind::NotConnected, ErrorKind::BrokenPipe] {
assert!(closed_by_peer(Err(Error::new(kind, "closed"))), "{kind:?}");
}
assert!(!closed_by_peer(Ok(1)), "a reply arrived");
for kind in [ErrorKind::TimedOut, ErrorKind::WouldBlock] {
// Not UTF-8, a timeout or a would-block leave the socket open.
for kind in [ErrorKind::InvalidData, ErrorKind::TimedOut, ErrorKind::WouldBlock] {
assert!(!closed_by_peer(Err(Error::new(kind, "still open"))), "{kind:?}");
}
}