summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2024-06-07 13:33:47 +1000
committerDamien George <damien@micropython.org>2024-06-08 09:02:01 +1000
commitdf0d7e942986b65ed42fbbf75712639f4a83dc0e (patch)
tree8bd79d50b91a4df4dcebd31a20587dea8686d5ee /tests
parent80a4f632ee812de9f2b22e88abb36ca36d230ec0 (diff)
extmod/modlwip: Make socket.connect raise ETIMEDOUT on non-zero timeout.
If the socket timeout is 0 then a failed socket.connect() raises EINPROGRESS (which is what the lwIP bindings already did), but if the socket timeout is non-zero then a failed socket.connect() should raise ETIMEDOUT. The latter is fixed in this commit. A test is added for these timeout cases. Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/net_hosted/connect_timeout.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/tests/net_hosted/connect_timeout.py b/tests/net_hosted/connect_timeout.py
new file mode 100644
index 000000000..5f35047c8
--- /dev/null
+++ b/tests/net_hosted/connect_timeout.py
@@ -0,0 +1,24 @@
+# Test that socket.connect() on a socket with timeout raises EINPROGRESS or ETIMEDOUT appropriately.
+
+import errno
+import socket
+
+
+def test(peer_addr, timeout, expected_exc):
+ s = socket.socket()
+ s.settimeout(timeout)
+ try:
+ s.connect(peer_addr)
+ print("OK")
+ except OSError as er:
+ print(er.args[0] in expected_exc)
+ s.close()
+
+
+if __name__ == "__main__":
+ # This test needs an address that doesn't respond to TCP connections.
+ # 1.1.1.1:8000 seem to reliably timeout, so use that.
+ addr = socket.getaddrinfo("1.1.1.1", 8000)[0][-1]
+
+ test(addr, 0, (errno.EINPROGRESS,))
+ test(addr, 1, (errno.ETIMEDOUT, "timed out")) # CPython uses a string instead of errno