diff --git a/tests/test_cli.py b/tests/test_cli.py index fd7ab68..71cf26f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -218,9 +218,17 @@ class Pty: self._data += chunk self._cond.notify_all() - def wait_for_bytes(self, count: int, timeout: float = TIMEOUT) -> bool: + def wait_for_bytes(self, count: int, proc: subprocess.Popen, timeout: float = TIMEOUT) -> bool: + """Wartet auf count Bytes, gibt aber auf, sobald proc beendet ist: der Lesethread bekommt kein EIO, + solange die eigene Slave-Seite offen ist, und sähe das Ende nie.""" + deadline = time.monotonic() + timeout with self._cond: - return self._cond.wait_for(lambda: len(self._data) >= count, timeout) + while len(self._data) < count: + remaining = deadline - time.monotonic() + if remaining <= 0 or proc.poll() is not None: + return False + self._cond.wait(min(remaining, 0.1)) + return True def resume(self) -> None: with self._cond: @@ -243,6 +251,14 @@ class Pty: return bytes(self._data) +def test_warten_auf_bytes_endet_mit_dem_prozess(): + # sonst säße ein Test, dessen Monitor gleich beim Start scheitert, die vollen TIMEOUT je Fall ab + with Pty() as pty, subprocess.Popen([sys.executable, "-c", ""]) as proc: + start = time.monotonic() + assert not pty.wait_for_bytes(1, proc) + assert time.monotonic() - start < STOP_TIMEOUT + + def assert_port_locked(port: str) -> None: """Der Monitor hält die Schnittstelle exklusiv (pyserial nutzt dafür flock).""" fd = os.open(port, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK) @@ -271,7 +287,7 @@ def test_echte_schnittstelle_bytestrom_geht_beim_beenden_exakt_auf(tmp_path, mid with Pty(pause_at) as pty: config = write_config(tmp_path, pty.port, "memory_interval = 0.5\n") with MonitorProcess(config) as monitor: - assert pty.wait_for_bytes(pause_at or CONNECT + FULL_FRAME), monitor.output + assert pty.wait_for_bytes(pause_at or CONNECT + FULL_FRAME, monitor.proc), monitor.output if mid_transfer: assert "Display verbunden" not in monitor.output # erst nach dem ersten vollständigen Bild else: