diff --git a/src/stream.rs b/src/stream.rs index 042821e..31a56ec 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -73,7 +73,16 @@ impl Read for DeadlineStream { socket.set_write_timeout(Some(timeout))?; } } - self.stream.read(buf) + self.stream.read(buf).map_err(|e| { + // On unix-y platforms set_read_timeout and set_write_timeout + // causes ErrorKind::WouldBlock instead of ErrorKind::TimedOut. + // Since the socket most definitely not set_nonblocking(true), + // we can safely normalize WouldBlock to TimedOut + if e.kind() == ErrorKind::WouldBlock { + return io_error_timeout(); + } + e + }) } } @@ -82,14 +91,15 @@ impl Read for DeadlineStream { fn time_until_deadline(deadline: Instant) -> IoResult { let now = Instant::now(); match deadline.checked_duration_since(now) { - None => Err(IoError::new( - ErrorKind::TimedOut, - "timed out reading response", - )), + None => Err(io_error_timeout()), Some(duration) => Ok(duration), } } +fn io_error_timeout() -> IoError { + IoError::new(ErrorKind::TimedOut, "timed out reading response") +} + impl ::std::fmt::Debug for Stream { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> { write!( diff --git a/src/test/timeout.rs b/src/test/timeout.rs index 0ccafca..570dc4b 100644 --- a/src/test/timeout.rs +++ b/src/test/timeout.rs @@ -32,7 +32,6 @@ fn get_and_expect_timeout(url: String) { match resp.into_string() { Err(io_error) => match io_error.kind() { - io::ErrorKind::WouldBlock => Ok(()), io::ErrorKind::TimedOut => Ok(()), _ => Err(format!("{:?}", io_error)), },