From 8ea7197f761af5a58c5ee5e9b6e883e5c0f42e08 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Wed, 24 Feb 2021 12:58:39 +0100 Subject: [PATCH] Crash when a controlled process stopped instead of waiting forever. --- irctest/basecontrollers.py | 14 +++++++++++++- irctest/cases.py | 12 +++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/irctest/basecontrollers.py b/irctest/basecontrollers.py index 92366eb..5418733 100644 --- a/irctest/basecontrollers.py +++ b/irctest/basecontrollers.py @@ -8,6 +8,12 @@ import time from .runner import NotImplementedByController +class ProcessStopped(Exception): + """Raised when the controlled process stopped unexpectedly""" + + pass + + class _BaseController: """Base class for software controllers. @@ -17,6 +23,12 @@ class _BaseController: def __init__(self, test_config): self.test_config = test_config + self.proc = None + + def check_is_alive(self): + self.proc.poll() + if self.proc.returncode is not None: + raise ProcessStopped() class DirectoryBasedController(_BaseController): @@ -26,7 +38,6 @@ class DirectoryBasedController(_BaseController): def __init__(self, test_config): super().__init__(test_config) self.directory = None - self.proc = None def kill_proc(self): """Terminates the controlled process, waits for it to exit, and @@ -132,6 +143,7 @@ class BaseServerController(_BaseController): def wait_for_port(self): while not self.port_open: + self.check_is_alive() time.sleep(self._port_wait_interval) try: c = socket.create_connection(("localhost", self.port), timeout=1.0) diff --git a/irctest/cases.py b/irctest/cases.py index 47b26ad..6d0a72b 100644 --- a/irctest/cases.py +++ b/irctest/cases.py @@ -165,10 +165,20 @@ class BaseClientTestCase(_IrcTestCase): self.server.bind(("", 0)) # Bind any free port self.server.listen(1) + # Used to check if the client is alive from time to time + self.server.settimeout(1) + def acceptClient(self, tls_cert=None, tls_key=None, server=None): """Make the server accept a client connection. Blocking.""" server = server or self.server - (self.conn, addr) = server.accept() + # Wait for the client to connect + while True: + try: + (self.conn, addr) = server.accept() + except socket.timeout: + self.controller.check_is_alive() + else: + break if tls_cert is None and tls_key is None: pass else: