Skip to content

Commit

Permalink
Add timeout_socket_server.py
Browse files Browse the repository at this point in the history
This is a UNIX domain socket server that accepts connections and never
responds, so that we can test timeouts on the client side.
  • Loading branch information
msabramo committed Dec 27, 2021
1 parent dbaaacf commit 2cc0ec6
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions timeout_socket_server.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 2cc0ec6

Please sign in to comment.