Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Duration type to std and use it in Timer methods. #15934

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use std::os;
use std::str;
use std::string::String;
use std::task;
use std::time::Duration;
use test::MetricMap;

pub fn run(config: Config, testfile: String) {
Expand Down Expand Up @@ -400,7 +401,7 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
.expect(format!("failed to exec `{}`", config.adb_path).as_slice());
loop {
//waiting 1 second for gdbserver start
timer::sleep(1000);
timer::sleep(Duration::milliseconds(1000));
let result = task::try(proc() {
tcp::TcpStream::connect("127.0.0.1", 5039).unwrap();
});
Expand Down
3 changes: 2 additions & 1 deletion src/libgreen/sched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,7 @@ mod test {
use std::rt::task::TaskOpts;
use std::rt::task::Task;
use std::rt::local::Local;
use std::time::Duration;

use {TaskState, PoolConfig, SchedPool};
use basic;
Expand Down Expand Up @@ -1291,7 +1292,7 @@ mod test {
// doesn't exit before emptying the work queue
pool.spawn(TaskOpts::new(), proc() {
spawn(proc() {
timer::sleep(10);
timer::sleep(Duration::milliseconds(10));
});
});

Expand Down
18 changes: 14 additions & 4 deletions src/libstd/io/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ 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};
use boxed::Box;
use rt::rtio::{IoFactory, LocalIo, RtioSocket, RtioTcpListener};
use rt::rtio::{RtioTcpAcceptor, RtioTcpStream};
use rt::rtio;
use time::Duration;

/// A structure which represents a TCP stream between a local socket and a
/// remote socket.
Expand Down Expand Up @@ -92,21 +94,28 @@ impl TcpStream {
}

/// Creates a TCP connection to a remote socket address, timing out after
/// the specified number of milliseconds.
/// the specified duration.
///
/// This is the same as the `connect` method, except that if the timeout
/// specified (in milliseconds) elapses before a connection is made an error
/// will be returned. The error's kind will be `TimedOut`.
///
/// Note that the `addr` argument may one day be split into a separate host
/// and port, similar to the API seen in `connect`.
///
/// 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_ms: u64) -> IoResult<TcpStream> {
timeout: Duration) -> IoResult<TcpStream> {
if timeout <= Duration::milliseconds(0) {
return Err(standard_error(TimedOut));
}

let SocketAddr { ip, port } = addr;
let addr = rtio::SocketAddr { ip: super::to_rtio(ip), port: port };
LocalIo::maybe_raise(|io| {
io.tcp_connect(addr, Some(timeout_ms)).map(TcpStream::new)
io.tcp_connect(addr, Some(timeout.num_milliseconds() as u64)).map(TcpStream::new)
}).map_err(IoError::from_rtio_error)
}

Expand Down Expand Up @@ -164,13 +173,14 @@ impl TcpStream {
/// # #![allow(unused_must_use)]
/// use std::io::timer;
/// use std::io::TcpStream;
/// use std::time::Duration;
///
/// let mut stream = TcpStream::connect("127.0.0.1", 34254).unwrap();
/// let stream2 = stream.clone();
///
/// spawn(proc() {
/// // close this stream after one second
/// timer::sleep(1000);
/// timer::sleep(Duration::seconds(1));
/// let mut stream = stream2;
/// stream.close_read();
/// });
Expand Down
29 changes: 25 additions & 4 deletions src/libstd/io/net/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ 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};
use rt::rtio::{RtioUnixAcceptor, RtioPipe};
use time::Duration;

/// A stream which communicates over a named pipe.
pub struct UnixStream {
Expand Down Expand Up @@ -66,11 +68,18 @@ impl UnixStream {
///
/// This function is similar to `connect`, except that if `timeout_ms`
/// elapses the function will return an error of kind `TimedOut`.
///
/// 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_ms: u64) -> IoResult<UnixStream> {
timeout: Duration) -> IoResult<UnixStream> {
if timeout <= Duration::milliseconds(0) {
return Err(standard_error(TimedOut));
}

LocalIo::maybe_raise(|io| {
let s = io.unix_connect(&path.to_c_str(), Some(timeout_ms));
let s = io.unix_connect(&path.to_c_str(), Some(timeout.num_milliseconds() as u64));
s.map(|p| UnixStream { obj: p })
}).map_err(IoError::from_rtio_error)
}
Expand Down Expand Up @@ -499,13 +508,25 @@ mod tests {

iotest!(fn connect_timeout_error() {
let addr = next_test_unix();
assert!(UnixStream::connect_timeout(&addr, 100).is_err());
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(100)).is_err());
})

iotest!(fn connect_timeout_success() {
let addr = next_test_unix();
let _a = UnixListener::bind(&addr).unwrap().listen().unwrap();
assert!(UnixStream::connect_timeout(&addr, 100).is_ok());
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(100)).is_ok());
})

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_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_err());
})

iotest!(fn close_readwrite_smoke() {
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/io/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ mod tests {
assert!(!p.wait().unwrap().success());
return
}
timer::sleep(100);
timer::sleep(Duration::milliseconds(100));
}
fail!("never saw the child go away");
})
Expand Down
7 changes: 4 additions & 3 deletions src/libstd/io/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ mod test_unix {
use comm::Empty;
use io::timer;
use super::{Listener, Interrupt};
use time::Duration;

fn sigint() {
unsafe {
Expand All @@ -179,7 +180,7 @@ mod test_unix {
let mut signal = Listener::new();
signal.register(Interrupt).unwrap();
sigint();
timer::sleep(10);
timer::sleep(Duration::milliseconds(10));
match signal.rx.recv() {
Interrupt => (),
s => fail!("Expected Interrupt, got {:?}", s),
Expand All @@ -193,7 +194,7 @@ mod test_unix {
s1.register(Interrupt).unwrap();
s2.register(Interrupt).unwrap();
sigint();
timer::sleep(10);
timer::sleep(Duration::milliseconds(10));
match s1.rx.recv() {
Interrupt => (),
s => fail!("Expected Interrupt, got {:?}", s),
Expand All @@ -212,7 +213,7 @@ mod test_unix {
s2.register(Interrupt).unwrap();
s2.unregister(Interrupt);
sigint();
timer::sleep(10);
timer::sleep(Duration::milliseconds(10));
assert_eq!(s2.rx.try_recv(), Err(Empty));
}
}
Expand Down
1 change: 1 addition & 0 deletions src/libstd/io/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ macro_rules! iotest (
use io::process::*;
use rt::running_on_valgrind;
use str;
use time::Duration;

fn f() $b

Expand Down