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 28, 2021
1 parent 073b549 commit 44f9fa2
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions timeout_socket_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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:
elapsed_time = int(time.time() - connect_time)
print(f'no data after {elapsed_time}s')
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 44f9fa2

Please sign in to comment.