Fix a hanging test on Python 3

I'm not entirely sure why the test is hanging, but this seems clear
enough:

- the test setup spawns a (non-daemon) background thread that runs
  forever, or until it is told to quit by receiving a TCP packet on a
  certain port
- the test teardown tries to tell the background thread to quit (which
  doesn't work) and waits for that to happen
- as a result the entire test run hangs forever

This commit adds a timeout as an extra safety net so that the test run
will complete even if the clean shutdown procedure fails for some
reason.
This commit is contained in:
Marius Gedminas 2019-04-26 00:15:10 +03:00
parent 095c6c57d4
commit e9fb9b01bf

View file

@ -49,7 +49,8 @@ class TelnetServerTest (LinkCheckTest):
def setUp (self):
"""Start a new Telnet server in a new thread."""
self.port = start_server(self.host, 0)
deadline = time.time() + 2 * TIMEOUT
self.port = start_server(self.host, 0, deadline=deadline)
self.assertFalse(self.port is None)
def tearDown(self):
@ -60,7 +61,7 @@ class TelnetServerTest (LinkCheckTest):
pass
def start_server (host, port):
def start_server (host, port, deadline):
# Instantiate Telnet server class and listen to host:port
clients = []
def on_connect(client):
@ -68,7 +69,7 @@ def start_server (host, port):
client.send("Telnet test server\n")
server = miniboa.TelnetServer(port=port, address=host, on_connect=on_connect)
port = server.server_socket.getsockname()[1]
t = threading.Thread(None, serve_forever, args=(server, clients))
t = threading.Thread(None, serve_forever, args=(server, clients, deadline))
t.start()
# wait for server to start up
tries = 0
@ -91,9 +92,12 @@ def stop_server (host, port):
client.write("stop\n")
def serve_forever(server, clients):
def serve_forever(server, clients, deadline):
"""Run poll loop for server."""
while True:
if time.time() > deadline:
# It could be that the test is very slow, but that's unlikely.
raise AssertionError('deadlock detected, aborting')
server.poll()
for client in clients:
if client.active and client.cmd_ready: