From 2cc0ec6f0dd7489145fb97c6f656531e95cb9356 Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Mon, 27 Dec 2021 14:24:55 -0800 Subject: [PATCH] Add timeout_socket_server.py This is a UNIX domain socket server that accepts connections and never responds, so that we can test timeouts on the client side. --- timeout_socket_server.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 timeout_socket_server.py diff --git a/timeout_socket_server.py b/timeout_socket_server.py new file mode 100644 index 0000000..53ae16e --- /dev/null +++ b/timeout_socket_server.py @@ -0,0 +1,39 @@ +import socket +import time +import os + +server_address = './uds_socket' + +# Make sure the socket does not already exist +try: + os.unlink(server_address) +except OSError: + if os.path.exists(server_address): + raise + +sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) +print('starting up on {}'.format(server_address)) +sock.bind(server_address) +sock.listen(1) + +while True: + print('\nwaiting for a connection') + + connection, client_address = sock.accept() + connection.settimeout(300) + + try: + print('connected') + connect_time = time.time() + while True: + try: + data = connection.recv(640) + if not data: + print('no data') + break + except socket.timeout as exc: + elapsed_time = int(time.time() - connect_time) + print(f'timeout after {elapsed_time}s on recv') + break + finally: + connection.close()