summaryrefslogtreecommitdiff
path: root/tests/multi_net/tcp_accept_recv.py
blob: 4108a6f8a3e9b8455ce35452eaa772bdeb2d8f12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Test recv on listening socket after accept(), with various listen() arguments

import socket

PORT = 8000

# Test cases for listen() function
LISTEN_ARGS = [None, 0, 1, 2]  # None means no argument


# Server
def instance0():
    multitest.globals(IP=multitest.get_network_ip())
    multitest.next()

    test_num = 0
    for blocking_mode in [True, False]:
        for listen_arg in LISTEN_ARGS:
            test_num += 1
            s = socket.socket()
            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            s.bind(socket.getaddrinfo("0.0.0.0", PORT)[0][-1])

            # Call listen with or without argument based on test case
            if listen_arg is None:
                print(f"Test case {test_num}/8: listen() blocking={blocking_mode}")
                s.listen()
            else:
                print(f"Test case {test_num}/8: listen({listen_arg}) blocking={blocking_mode}")
                s.listen(listen_arg)

            # Signal client that server is ready
            multitest.broadcast(f"server_ready_{test_num}")

            # Wait for client connection
            c, _ = s.accept()

            # Set blocking mode after accept
            s.setblocking(blocking_mode)

            try:
                print("recv", s.recv(10))  # should raise Errno 107 ENOTCONN
            except OSError as er:
                # Verify the error code is either 107 (ENOTCONN) or 128 (ENOTCONN on Windows)
                print(er.errno in (107, 128))

            # Cleanup
            c.close()
            s.close()

            # Signal client we're done with this test case
            multitest.broadcast(f"server_done_{test_num}")


# Client
def instance1():
    multitest.next()

    test_num = 0
    for blocking_mode in [True, False]:
        for _ in LISTEN_ARGS:
            test_num += 1
            # Wait for server to be ready
            multitest.wait(f"server_ready_{test_num}")

            # Connect to server
            s = socket.socket()
            s.connect(socket.getaddrinfo(IP, PORT)[0][-1])
            s.send(b"GET / HTTP/1.0\r\n\r\n")
            s.close()

            # Wait for server to finish this test case
            multitest.wait(f"server_done_{test_num}")