std: Make connect_timeout return Err on zero duration

[breaking-change]
This commit is contained in:
Brian Anderson 2014-08-04 16:21:47 -07:00
parent a391934ba8
commit ee10f3501c
3 changed files with 27 additions and 12 deletions

View File

@ -27,6 +27,7 @@ use io::net::addrinfo::get_host_addresses;
use io::net::ip::SocketAddr;
use io::{IoError, ConnectionFailed, InvalidInput};
use io::{Reader, Writer, Listener, Acceptor};
use io::{standard_error, TimedOut};
use from_str::FromStr;
use kinds::Send;
use option::{None, Some, Option};
@ -102,13 +103,14 @@ impl TcpStream {
/// Note that the `addr` argument may one day be split into a separate host
/// and port, similar to the API seen in `connect`.
///
/// # Failure
///
/// Fails on a `timeout` of zero or negative duration.
/// If a `timeout` with zero or negative duration is specified then
/// the function returns `Err`, with the error kind set to `TimedOut`.
#[experimental = "the timeout argument may eventually change types"]
pub fn connect_timeout(addr: SocketAddr,
timeout: Duration) -> IoResult<TcpStream> {
assert!(timeout > Duration::milliseconds(0));
if timeout <= Duration::milliseconds(0) {
return standard_error(TimedOut);
}
let SocketAddr { ip, port } = addr;
let addr = rtio::SocketAddr { ip: super::to_rtio(ip), port: port };

View File

@ -29,6 +29,7 @@ use prelude::*;
use c_str::ToCStr;
use clone::Clone;
use io::{Listener, Acceptor, Reader, Writer, IoResult, IoError};
use io::{standard_error, TimedOut};
use kinds::Send;
use boxed::Box;
use rt::rtio::{IoFactory, LocalIo, RtioUnixListener};
@ -68,13 +69,14 @@ impl UnixStream {
/// This function is similar to `connect`, except that if `timeout_ms`
/// elapses the function will return an error of kind `TimedOut`.
///
/// # Failure
///
/// Fails on a `timeout` of zero or negative duration.
/// If a `timeout` with zero or negative duration is specified then
/// the function returns `Err`, with the error kind set to `TimedOut`.
#[experimental = "the timeout argument is likely to change types"]
pub fn connect_timeout<P: ToCStr>(path: &P,
timeout: Duration) -> IoResult<UnixStream> {
assert!(timeout > Duration::milliseconds(0));
if timeout <= Duration::milliseconds(0) {
return standard_error(TimedOut);
}
LocalIo::maybe_raise(|io| {
let s = io.unix_connect(&path.to_c_str(), Some(timeout.num_milliseconds() as u64));
@ -518,14 +520,14 @@ mod tests {
iotest!(fn connect_timeout_zero() {
let addr = next_test_unix();
let _a = UnixListener::bind(&addr).unwrap().listen().unwrap();
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(0)).is_ok());
} #[should_fail])
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(0)).is_err());
})
iotest!(fn connect_timeout_negative() {
let addr = next_test_unix();
let _a = UnixListener::bind(&addr).unwrap().listen().unwrap();
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(-1)).is_ok());
} #[should_fail])
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(-1)).is_err());
})
iotest!(fn close_readwrite_smoke() {
let addr = next_test_unix();

View File

@ -96,3 +96,14 @@ iotest!(fn timeout_error() {
assert!(TcpStream::connect_timeout(addr, Duration::milliseconds(1000)).is_err());
})
iotest!(fn connect_timeout_zero() {
let addr = next_test_ip4();
assert!(TcpStream::connect_timeout(&addr, Duration::milliseconds(0)).is_err());
})
iotest!(fn connect_timeout_negative() {
let addr = next_test_ip4();
assert!(TcpStream::connect_timeout(&addr, Duration::milliseconds(-1)).is_err());
})