From a82517ee1c24d5db22c4d02ce1ada8a74d8ca8c1 Mon Sep 17 00:00:00 2001 From: AmiPoke Date: Tue, 3 Nov 2026 12:09:07 +0100 Subject: [PATCH] Pty.wait_for_bytes bricht ab, wenn der Monitor beendet ist MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Die Wartefunktion endete nur bei genügend Bytes oder nach TIMEOUT (15 s); der Lesethread der Pty bekommt kein EIO, solange die eigene Slave-Seite offen ist. Beendete sich der Monitor gleich beim Start (ungültige Konfiguration, Importfehler, Absturz), saß test_echte_schnittstelle_bytestrom_… trotzdem 15 s je Fall ab, mit beiden Parametern 30 s (gemessen mit der Mutation MIN_INTERVAL >= → >). Der im Review vorgeschlagene Tausch der beiden Wartevorgänge (erst „Display verbunden“, dann die Bytes) passt nicht mehr: Die Zeile kommt seit dem Ausfall-Commit erst nach dem ersten vollständigen Bild, im Fall mitten-im-ersten-bild also nie vor der Pause. Stattdessen bekommt wait_for_bytes den Prozess und prüft in Abständen von 0,1 s, ob er noch läuft. Mit derselben Mutation scheitern beide Fälle jetzt nach je 0,2 s, die Meldung zeigt die ERROR-Zeile des Monitors. Test: wait_for_bytes gibt bei einem sofort beendeten Prozess schnell False zurück. Co-Authored-By: Claude Opus 5.5 --- tests/test_cli.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) 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: