Files
seismo-relay/tests/test_minimate_boot_on_connect.py
T
serversdownandClaude Opus 5.5 f6166b4b04 fix(minimateplus): the boot-aware first POLL resets the parser; a drained or long-trailed banner is seen
Review of d73a27d: _await_poll_or_boot no longer reset the parser and pending
frames the way _recv_one(reset_parser=True) did, so a reused protocol object
with half a frame left over failed startup (UnexpectedResponse A4 vs A5).
Restored.  Also: the banner check now runs before the tail is trimmed (a read
carrying >48 bytes after the banner lost it), and a banner caught during the
200 ms drain is reported, so startup goes straight to the re-poll loop instead
of the full timeout.  Tests for all three.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
2026-10-25 18:43:49 -04:00

106 lines
3.9 KiB
Python

"""A MiniMate that boots when its port opens is waited for, not given up on.
BE9441 on a serial cable (bench, 2026-10-25) twice sent its boot banner
("Operating System") ~3 s after the port opened and ignored the POLL sent
before it; the request then failed at the full receive timeout (15 s, or 120 s
on the download path). After a mid-download reboot it answered 8.8 s after its
banner. startup() now stops waiting on the moment it sees the banner and polls
again until the unit answers (up to 20 s); with no banner, nothing changes.
"""
from __future__ import annotations
import os
import sys
import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import minimateplus.client as mc # noqa: E402
import minimateplus.protocol as mp # noqa: E402
from minimate_replay import SessionReplay, fast_clock, recorded_replies # noqa: E402
BANNER = b"Operating System"
@pytest.fixture(autouse=True)
def _fast(monkeypatch):
fast_clock(monkeypatch)
class Booting(SessionReplay):
"""Banner instead of an answer to the first POLL; then `deaf` more POLL probes
go unanswered (still booting); after that, the recorded session as normal."""
def __init__(self, replies, deaf=2, banner=True, banner_on_open=False, noise_after=0):
super().__init__(replies)
self.deaf, self.banner, self.polls = deaf, banner, 0
self.noise_after = noise_after # non-frame bytes sent after the banner
if banner_on_open: # the banner lands inside the drain window
self._buf, self.banner = BANNER, False
def write(self, data):
data = bytes(data)
if data[:2] == b"\x41\x02" and data[5] == 0x5B and data[8] == 0x00: # the POLL probe
self.polls += 1
if self.polls == 1:
if self.banner:
self._buf += BANNER + b"." * self.noise_after
return
if self.polls <= 1 + self.deaf:
return
super().write(data)
def connect(transport):
client = mc.MiniMateClient(transport=transport)
client.open()
return client, client.connect()
def test_a_unit_booting_on_connect_is_waited_for():
t = Booting(recorded_replies(), deaf=2)
_client, info = connect(t)
assert info.serial == "BE9441"
assert t.polls == 4 # the lost one, two while booting, the answered one
def test_a_unit_that_boots_but_never_answers_says_so():
t = Booting(recorded_replies(), deaf=10_000)
with pytest.raises(mp.TimeoutError, match="boot"):
connect(t)
def test_a_silent_unit_without_a_banner_fails_as_before():
t = Booting(recorded_replies(), deaf=10_000, banner=False)
with pytest.raises(mp.TimeoutError) as e:
connect(t)
assert "boot" not in str(e.value)
assert t.polls == 1 # no re-polling without the banner
def test_startup_on_a_reused_protocol_ignores_leftover_bytes():
replies = recorded_replies()
p = mp.MiniMateProtocol(SessionReplay(replies))
a5 = next(raw for sub, raw in replies if sub == 0xA5)
p._parser.feed(a5[:20]) # half a frame left over from an earlier exchange
assert p.startup().sub == 0xA4
def test_a_banner_followed_by_more_text_in_the_same_read_is_still_seen():
t = Booting(recorded_replies(), deaf=1, noise_after=200)
assert connect(t)[1].serial == "BE9441"
def test_a_banner_caught_while_draining_is_acted_on(monkeypatch):
t_now = [0.0]
def fine(): # fine enough that the 200 ms drain really reads
t_now[0] += 0.01
return t_now[0]
monkeypatch.setattr(mp.time, "monotonic", fine)
t = Booting(recorded_replies(), deaf=1, banner_on_open=True)
assert connect(t)[1].serial == "BE9441"
assert t._buf == b"" # the drain consumed the banner