From eec2e382d8be07b395c9c0813b4990050140a01a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Thu, 30 Nov 2023 16:25:59 +0100 Subject: [PATCH] feat(driver): wait for Squish server port to listen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Doing silly retries is just not reliable, this is. Signed-off-by: Jakub SokoĊ‚owski --- test/e2e/driver/server.py | 7 +++++++ test/e2e/scripts/utils/wait_for_port.py | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 test/e2e/scripts/utils/wait_for_port.py diff --git a/test/e2e/driver/server.py b/test/e2e/driver/server.py index f9cb4c9460..b7d32396dc 100644 --- a/test/e2e/driver/server.py +++ b/test/e2e/driver/server.py @@ -3,6 +3,7 @@ import typing import configs.testpath from scripts.utils import local_system +from scripts.utils.wait_for_port import wait_for_port _PROCESS_NAME = '_squishserver' @@ -40,6 +41,12 @@ class SquishServer: cls.pid = None cls.port = None + @classmethod + def wait(cls, timeout: int = 1, retries: int = 10): + LOG.info('Waiting for Squish server port %s:%d...', cls.host, cls.port) + wait_for_port(cls.host, cls.port, timeout, retries) + LOG.info('Squish server port available!') + # https://doc-snapshots.qt.io/squish/cli-squishserver.html @classmethod def configuring(cls, action: str, options: typing.Union[int, str, list]): diff --git a/test/e2e/scripts/utils/wait_for_port.py b/test/e2e/scripts/utils/wait_for_port.py new file mode 100644 index 0000000000..1b0dcf8dd2 --- /dev/null +++ b/test/e2e/scripts/utils/wait_for_port.py @@ -0,0 +1,21 @@ +import time +import socket +import logging + +LOG = logging.getLogger(__name__) + +def wait_for_port(host: str, port: int, timeout: int = 1, retries: int = 0): + for i in range(retries+1): + try: + LOG.debug('Checking TCP port: %s:%d', host, port) + with socket.create_connection((host, port), timeout=timeout): + return + except OSError as err: + LOG.debug('Connection error: %s', err) + time.sleep(1) + continue + + LOG.debug('Timed out waiting for TCP port: %s:%d', host, port) + raise TimeoutError( + 'Unable to establish TCP connection with %s:%s.' % (host, port) + )