Skip to content

Commit fca9e6e

Browse files
committed
Add unit test for TcpStream::connect_timeout
Signed-off-by: Eval EXEC <[email protected]>
1 parent 22f62df commit fca9e6e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

library/std/src/net/tcp/tests.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ fn connect_error() {
4646
}
4747
}
4848

49+
#[test]
50+
fn connect_timeout_error() {
51+
let socket_addr = next_test_ip4();
52+
let result = TcpStream::connect_timeout(&socket_addr, Duration::MAX);
53+
assert!(!matches!(result, Err(e) if e.kind() == ErrorKind::TimedOut));
54+
55+
let _listener = TcpListener::bind(&socket_addr).unwrap();
56+
assert!(TcpStream::connect_timeout(&socket_addr, Duration::MAX).is_ok());
57+
}
58+
59+
#[test]
60+
fn connect_timeout_ok_bind() {
61+
let listener = TcpListener::bind("127.0.0.1:0").unwrap(); // :0 picks some free port
62+
let port = listener.local_addr().unwrap().port(); // obtain the port it picked
63+
assert!(
64+
TcpStream::connect_timeout(&format!("127.0.0.1:{port}").parse().unwrap(), Duration::MAX)
65+
.is_ok()
66+
);
67+
}
68+
4969
#[test]
5070
fn listen_localhost() {
5171
let socket_addr = next_test_ip4();

0 commit comments

Comments
 (0)