From e9fb9b01bf653c655f00ca110e120e7402bd5a97 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Fri, 26 Apr 2019 00:15:10 +0300 Subject: [PATCH] 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. --- tests/checker/telnetserver.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/checker/telnetserver.py b/tests/checker/telnetserver.py index 8923e313..b294cbb3 100644 --- a/tests/checker/telnetserver.py +++ b/tests/checker/telnetserver.py @@ -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: