fix(minimateplus): a one-event download is whole or it is an error
BE9441 rebooted mid-download at the bench (2026-10-25): the chunk at 0x7D06 got one stray byte and then silence, the 5A loop logged "unexpected end-of- stream -- proceeding to TERM", the TERM got no reply either, and the short event (17,920 / 17,920 / 17,408 of 21,504 samples) was stored as a success. read_bulk_waveform_stream(strict=True) now raises on every early end: no STRT, a metadata page or chunk with no reply, a mid-stream terminator, the chunk cap, or no TERM reply. get_events passes it for the strict walks (headers_only, only_key) and, in strict mode, re-raises a 5A error instead of logging "metadata unavailable" and returning the event without frames. The legacy full walk is unchanged, call for call (strict passed only when on). Tests replay BE9441's recorded session through the real MiniMateClient: whole, event 1 comes back in full; with the 5A stream quiet from chunk 40 while the walk is still answered -- the bench's reboot -- it raises "ended early". No codec, store or DB change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
1 parent
352f6944e7
commit
75d79ece6c
4 files changed
+247
-5
No files matched your search
@@ -0,0 +1,97 @@
|
||||
"""A one-event download must not keep an event whose 5A stream ended early.
|
||||
|
||||
BE9441 rebooted mid-download at the bench (2026-10-25): the chunk at 0x7D06 got
|
||||
no reply, the loop logged "unexpected end-of-stream -- proceeding to TERM", the
|
||||
TERM got no reply either, and the short event was stored as a success. In
|
||||
strict mode (the one-event download) every early end now raises; the legacy
|
||||
full walk keeps its tolerance. Driven by BE9441's recorded replies for event 1.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import minimateplus.protocol as mp # noqa: E402
|
||||
from minimateplus.framing import S3FrameParser # noqa: E402
|
||||
|
||||
FIX = Path(__file__).parent / "fixtures" / "minimate-bench-10-25-26" / "BE9441_event1_0111646a_rx.bin"
|
||||
KEY = bytes.fromhex("0111646a")
|
||||
|
||||
|
||||
def a5_replies() -> list:
|
||||
"""The raw bytes of each A5 reply, in order (the parser fed one byte at a time)."""
|
||||
raw, out, start, p = FIX.read_bytes(), [], 0, S3FrameParser()
|
||||
for i in range(len(raw)):
|
||||
for f in p.feed(raw[i:i + 1]):
|
||||
if f.sub == 0xA5:
|
||||
out.append(raw[start:i + 1])
|
||||
start = i + 1
|
||||
return out
|
||||
|
||||
|
||||
class Replay:
|
||||
"""One recorded reply per 5A request; from request number `silent_from` on, one
|
||||
stray 0x41 and then silence -- what BE9441 sent as it rebooted."""
|
||||
|
||||
def __init__(self, replies, silent_from=None):
|
||||
self.replies, self.silent_from, self.sent, self._buf = list(replies), silent_from, 0, b""
|
||||
|
||||
def write(self, data):
|
||||
n, self.sent = self.sent, self.sent + 1
|
||||
if self.silent_from is None or n < self.silent_from:
|
||||
self._buf += self.replies[n]
|
||||
elif n == self.silent_from:
|
||||
self._buf += b"\x41"
|
||||
|
||||
def read(self, n):
|
||||
out, self._buf = self._buf[:n], self._buf[n:]
|
||||
return out
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def fast_clock(monkeypatch):
|
||||
t = [0.0]
|
||||
|
||||
def tick():
|
||||
t[0] += 0.5
|
||||
return t[0]
|
||||
monkeypatch.setattr(mp.time, "monotonic", tick)
|
||||
monkeypatch.setattr(mp.time, "sleep", lambda s: None)
|
||||
|
||||
|
||||
def proto(silent_from=None):
|
||||
replies = a5_replies()
|
||||
p = mp.MiniMateProtocol(Replay(replies, silent_from))
|
||||
return p, len(replies)
|
||||
|
||||
|
||||
def test_the_recorded_stream_replays_whole():
|
||||
p, n = proto()
|
||||
frames = p.read_bulk_waveform_stream(KEY, stop_after_metadata=False, include_terminator=True,
|
||||
strict=True)
|
||||
assert len(frames) == n == 51
|
||||
|
||||
|
||||
def test_strict_raises_when_a_chunk_gets_no_reply():
|
||||
p, _ = proto(silent_from=40)
|
||||
with pytest.raises(mp.ProtocolError, match="ended early"):
|
||||
p.read_bulk_waveform_stream(KEY, stop_after_metadata=False, include_terminator=True,
|
||||
strict=True)
|
||||
|
||||
|
||||
def test_strict_raises_when_the_term_gets_no_reply():
|
||||
p, n = proto(silent_from=50) # every chunk answered, the TERM not
|
||||
with pytest.raises(mp.ProtocolError, match="TERM"):
|
||||
p.read_bulk_waveform_stream(KEY, stop_after_metadata=False, include_terminator=True,
|
||||
strict=True)
|
||||
|
||||
|
||||
def test_the_legacy_walk_keeps_its_tolerance():
|
||||
p, _ = proto(silent_from=40)
|
||||
frames = p.read_bulk_waveform_stream(KEY, stop_after_metadata=False, include_terminator=True)
|
||||
assert 0 < len(frames) < 51 # short, returned, not raised -- as before
|
||||
@@ -0,0 +1,108 @@
|
||||
"""BE9441's recorded one-event download, replayed through the real MiniMateClient.
|
||||
|
||||
The bench session (2026-10-25) is replayed reply for reply: SESSION_RESET gets
|
||||
none, every other request the next recorded frame. Whole, it must give event 1
|
||||
in full (the metadata-page fix); silenced partway through the 5A stream, as the
|
||||
unit was when it rebooted, it must raise (the strict-stream fix), never return
|
||||
a short event.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import minimateplus.client as mc # noqa: E402
|
||||
import minimateplus.protocol as mp # noqa: E402
|
||||
from minimateplus.framing import S3FrameParser # noqa: E402
|
||||
|
||||
FIX = Path(__file__).parent / "fixtures" / "minimate-bench-10-25-26" / "BE9441_event1_0111646a_rx.bin"
|
||||
KEY = bytes.fromhex("0111646a")
|
||||
STAMP = "2026-10-25T14:10:49"
|
||||
SESSION_RESET = b"\x41\x03"
|
||||
|
||||
|
||||
def recorded_replies():
|
||||
"""[(sub, raw_bytes)] for every frame of the session, in order."""
|
||||
raw, out, start, p = FIX.read_bytes(), [], 0, S3FrameParser()
|
||||
for i in range(len(raw)):
|
||||
for f in p.feed(raw[i:i + 1]):
|
||||
out.append((f.sub, raw[start:i + 1]))
|
||||
start = i + 1
|
||||
return out
|
||||
|
||||
|
||||
class SessionReplay:
|
||||
"""Answers each request with the next recorded reply of its own SUB (reply =
|
||||
0xFF - request). `quiet_5a_from`: from that 5A request on, the 5A stream gets
|
||||
one stray 0x41 and then nothing, while every other request is still answered
|
||||
-- BE9441 dropped out of the stream as it rebooted, then answered the walk."""
|
||||
|
||||
def __init__(self, replies, quiet_5a_from=None):
|
||||
self.queues = {}
|
||||
for sub, raw in replies:
|
||||
self.queues.setdefault(sub, []).append(raw)
|
||||
self.quiet_5a_from, self.n5a, self._buf, self.open_ = quiet_5a_from, 0, b"", False
|
||||
|
||||
def connect(self):
|
||||
self.open_ = True
|
||||
|
||||
def disconnect(self):
|
||||
self.open_ = False
|
||||
|
||||
def is_connected(self):
|
||||
return self.open_
|
||||
|
||||
def write(self, data):
|
||||
data = bytes(data)
|
||||
if data == SESSION_RESET:
|
||||
return
|
||||
req = data[5] # 41 02 10 10 00 <SUB>
|
||||
if req == 0x5A:
|
||||
n, self.n5a = self.n5a, self.n5a + 1
|
||||
if self.quiet_5a_from is not None and n >= self.quiet_5a_from:
|
||||
if n == self.quiet_5a_from:
|
||||
self._buf += b"\x41"
|
||||
return
|
||||
self._buf += self.queues[0xFF - req].pop(0)
|
||||
|
||||
def read(self, n):
|
||||
out, self._buf = self._buf[:n], self._buf[n:]
|
||||
return out
|
||||
|
||||
def read_until_idle(self, *a, **kw):
|
||||
out, self._buf = self._buf, b""
|
||||
return out
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def fast_clock(monkeypatch):
|
||||
t = [0.0]
|
||||
|
||||
def tick():
|
||||
t[0] += 0.5
|
||||
return t[0]
|
||||
monkeypatch.setattr(mp.time, "monotonic", tick)
|
||||
monkeypatch.setattr(mp.time, "sleep", lambda s: None)
|
||||
|
||||
|
||||
def download(quiet_5a_from=None):
|
||||
client = mc.MiniMateClient(transport=SessionReplay(recorded_replies(), quiet_5a_from))
|
||||
client.open()
|
||||
client.connect()
|
||||
return client.download_event_by_key(KEY, expected_timestamp=STAMP)
|
||||
|
||||
|
||||
def test_the_whole_session_gives_event_1_in_full():
|
||||
ev = download()
|
||||
assert {ch: len(ev.raw_samples[ch]) for ch in ("Tran", "Vert", "Long")} == \
|
||||
{"Tran": 21_504, "Vert": 21_504, "Long": 21_504}
|
||||
|
||||
|
||||
def test_a_unit_that_goes_quiet_mid_stream_raises_instead_of_returning_a_short_event():
|
||||
with pytest.raises(mp.ProtocolError, match="ended early"):
|
||||
download(quiet_5a_from=40)
|
||||
Reference in new issue
Block a user