Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions library/std/src/net/tcp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,37 @@ fn connect_error() {
}
}

#[test]
fn connect_timeout_to_unreachable_address() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested, this unit test will cost at 2 minutes and 8 seconds on Linux platform, and cost 20 seconds on Windows.

@ChrisDenton ChrisDenton Jun 20, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we only need one test here. We only need to focus on testing the problematic code path in one way. 2 minutes and 8 secs is a long time for a single unit test so I'd prefer if we just used connect_timeout_error test and removed the other two.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think 2 minutes and 8 seconds is a long time too.

let now = Instant::now();
match TcpStream::connect_timeout(&format!("1.1.1.1:9999").parse().unwrap(), Duration::MAX) {
Ok(..) => panic!("connected to an unreachable address, this is impossible"),
Err(e) => assert_eq!(e.kind(), ErrorKind::TimedOut),
}

assert!(now.elapsed() > Duration::from_secs(20));
}

#[test]
Comment thread
eval-exec marked this conversation as resolved.
Outdated
fn connect_timeout_error() {
let socket_addr = next_test_ip4();
let result = TcpStream::connect_timeout(&socket_addr, Duration::MAX);
assert!(!matches!(result, Err(e) if e.kind() == ErrorKind::TimedOut));

@RalfJung RalfJung May 12, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a strange test. It says to only timeout after billions of years, yet apparently it expects the OS to timeout immediately. What is going on here? And why is there no comment explaining this?^^

Context: #156385

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed a ! :)

@eval-exec eval-exec May 14, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, I reviewed your PR. Is there some tasks we can do next for further steps?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the comment there makes sense then I think that's all, thanks :)


let _listener = TcpListener::bind(&socket_addr).unwrap();
assert!(TcpStream::connect_timeout(&socket_addr, Duration::MAX).is_ok());
}

#[test]
fn connect_timeout_ok_bind() {
let listener = TcpListener::bind("127.0.0.1:0").unwrap(); // :0 picks some free port
let port = listener.local_addr().unwrap().port(); // obtain the port it picked
assert!(
TcpStream::connect_timeout(&format!("127.0.0.1:{port}").parse().unwrap(), Duration::MAX)
.is_ok()
);
}

#[test]
fn listen_localhost() {
let socket_addr = next_test_ip4();
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/windows/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl Socket {
}

let mut timeout = c::timeval {
tv_sec: timeout.as_secs() as c_long,
tv_sec: cmp::min(timeout.as_secs(), c_long::MAX as u64) as c_long,
tv_usec: (timeout.subsec_nanos() / 1000) as c_long,
};

Expand Down