From 12cf2082370797040cb5031fc18795a436e890be Mon Sep 17 00:00:00 2001 From: matejcik Date: Tue, 30 Jul 2024 11:30:17 +0200 Subject: [PATCH] fix(tests): stop waiting for background task after a timeout Otherwise a test can hang if the result is not returned. This is not even covered by pytest-timeout because if a test fails, the call to task.kill() happens during teardown, and pytest-timeout doesn't cover that. --- tests/device_handler.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/device_handler.py b/tests/device_handler.py index 364d6351dd..4baf8e87ef 100644 --- a/tests/device_handler.py +++ b/tests/device_handler.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from concurrent.futures import ThreadPoolExecutor from typing import TYPE_CHECKING, Any, Callable @@ -69,7 +71,7 @@ class BackgroundDeviceHandler: while self.client.session_counter > 0: self.client.close() try: - self.task.result() + self.task.result(timeout=1) except Exception: pass self.task = None @@ -80,11 +82,11 @@ class BackgroundDeviceHandler: emulator.restart() self._configure_client(emulator.client) # type: ignore [client cannot be None] - def result(self): + def result(self, timeout: float | None = None) -> Any: if self.task is None: raise RuntimeError("No task running") try: - return self.task.result() + return self.task.result(timeout=timeout) finally: self.task = None