diff --git a/tests/snippets/stdlib_socket.py b/tests/snippets/stdlib_socket.py
index d5727bb25..ddd8d6ccc 100644
--- a/tests/snippets/stdlib_socket.py
+++ b/tests/snippets/stdlib_socket.py
@@ -137,3 +137,13 @@ with assertRaises(OSError):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
pass
+
+with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as listener:
+ listener.bind(("127.0.0.1", 0))
+ listener.listen(1)
+ connector = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ connector.connect(("127.0.0.1", listener.getsockname()[1]))
+ (connection, addr) = listener.accept()
+ connection.settimeout(1.0)
+ with assertRaises(OSError):
+ connection.recv(len(MESSAGE_A))
diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs
index c83b75656..e0fd0a505 100644
--- a/vm/src/stdlib/socket.rs
+++ b/vm/src/stdlib/socket.rs
@@ -3,6 +3,7 @@ use std::io;
use std::io::Read;
use std::io::Write;
use std::net::{Ipv4Addr, SocketAddr, TcpListener, TcpStream, ToSocketAddrs, UdpSocket};
+use std::time::Duration;
#[cfg(all(unix, not(target_os = "redox")))]
use nix::unistd::sethostname;
@@ -122,6 +123,27 @@ impl Connection {
fn fileno(&self) -> i64 {
unimplemented!();
}
+
+ fn setblocking(&mut self, value: bool) -> io::Result<()> {
+ match self {
+ Connection::TcpListener(con) => con.set_nonblocking(!value),
+ Connection::UdpSocket(con) => con.set_nonblocking(!value),
+ Connection::TcpStream(con) => con.set_nonblocking(!value),
+ }
+ }
+
+ fn settimeout(&mut self, duration: Duration) -> io::Result<()> {
+ match self {
+ // net
+ Connection::TcpListener(_con) => Ok(()),
+ Connection::UdpSocket(con) => con
+ .set_read_timeout(Some(duration))
+ .and_then(|_| con.set_write_timeout(Some(duration))),
+ Connection::TcpStream(con) => con
+ .set_read_timeout(Some(duration))
+ .and_then(|_| con.set_write_timeout(Some(duration))),
+ }
+ }
}
impl Read for Connection {
@@ -152,6 +174,7 @@ pub struct Socket {
address_family: AddressFamily,
socket_kind: SocketKind,
con: RefCell