File: connect_timeout.py

package info (click to toggle)
micropython 1.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 48,944 kB
  • sloc: ansic: 317,850; python: 59,539; xml: 4,241; makefile: 3,530; sh: 1,421; javascript: 744; asm: 681; cpp: 45; exp: 11; pascal: 6
file content (24 lines) | stat: -rw-r--r-- 713 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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