diff options
| author | Eval EXEC <execvy@gmail.com> | 2023-06-11 08:57:16 +0800 |
|---|---|---|
| committer | Eval EXEC <execvy@gmail.com> | 2023-06-18 01:56:11 +0800 |
| commit | fca9e6e706d1b611c81c97ccdff8265f5f1ee638 (patch) | |
| tree | 1705e5779850004817b750b7e51e7f86eb849388 /library/std/src/net/tcp | |
| parent | 22f62df337b015df73104d24999e6f4e30f18d14 (diff) | |
| download | rust-fca9e6e706d1b611c81c97ccdff8265f5f1ee638.tar.gz rust-fca9e6e706d1b611c81c97ccdff8265f5f1ee638.zip | |
Add unit test for `TcpStream::connect_timeout`
Signed-off-by: Eval EXEC <execvy@gmail.com>
Diffstat (limited to 'library/std/src/net/tcp')
| -rw-r--r-- | library/std/src/net/tcp/tests.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/library/std/src/net/tcp/tests.rs b/library/std/src/net/tcp/tests.rs index 7a3c66e4504..08ee451dfcd 100644 --- a/library/std/src/net/tcp/tests.rs +++ b/library/std/src/net/tcp/tests.rs @@ -47,6 +47,26 @@ fn connect_error() { } #[test] +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)); + + 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(); let listener = t!(TcpListener::bind(&socket_addr)); |
