1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-14 03:30:02 +00:00

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.
This commit is contained in:
matejcik 2024-07-30 11:30:17 +02:00 committed by M1nd3r
parent 5dc8998a0b
commit 0d089d76a6

View File

@ -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