review: re-arm the settle window on retry, tighten comments, close a test gap

An Opus review of the three prior commits on this branch (PR #306)
turned up two real defects and a documentation/test gap, all fixed
here:

- async_send_command's retry (issue #294) armed the write-settle
  window before the retry existed, so the reconnect pause plus a
  second PUT could eat into the time meant for the confirming poll,
  reviving the revert-then-reapply symptom the window was sized to
  prevent (issue #9). Re-arm it after a successful retry lands.

- test_send_command_reconnects_and_retries_after_socket_closed relied
  on the observe-session fixture's no-op _close_session, so
  self._session never actually went None and _do_put's reconnect
  guard was never exercised -- the test passed even with that guard
  deleted. Now overrides _close_session/_connect_session to actually
  drop and rebuild the session, and asserts the reconnect happened.

- async_send_command's docstring still said "Fire-and-forget", which
  stopped being true the moment it started retrying and raising.

Also trimmed the three comment blocks the review flagged as
reproducing their commit messages verbatim, per CONTRIBUTING.md's
comment-style rules.

One review finding is not addressed here and needs a decision: the
new _close_session() call in the command-retry path isn't
synchronized against _attempt_observe_mode, which touches the session
without _session_lock. A write's reconnect can race an in-flight
observe-mode subscribe attempt and tear down the session it's using.
Fixing it properly means broadening lock scope around observe-mode
entry, which risks blocking a write behind an up to ~15s subscribe
grace period -- a tradeoff not made unilaterally here.

A second finding (dropping the old .strip()'s per-line whitespace
handling in _normalize_pem) did not reproduce against a real
certificate/key, only against the test suite's placeholder PEM body,
so it's left as-is.
This commit is contained in:
Marc Billow committed 2026-08-06 01:06:22 +00:00
1 parent 77c2d7831e
commit 50bb893407
3 files changed
+46 -44

No files matched your search

+21 -1
View File
@@ -1015,7 +1015,14 @@ async def test_send_command_reconnects_and_retries_after_socket_closed(
"""A command lost to a session Samsung's firmware closed between polls
must not just vanish (issue #294): `_do_put` failing once is now
followed by a reconnect and a single retry, mirroring the poll path's
own recovery in `_async_update_data`."""
own recovery in `_async_update_data`.
`mock_coordinator_observe_session` patches `_close_session` to a no-op,
which would leave `self._session` never actually going `None` -- and
with it, `_do_put`'s own `if self._session is None: self._connect_session()`
guard never exercised, so a broken reconnect could still pass. Overridden
here to actually drop the session, so the retry only succeeds if that
guard really rebuilds it."""
from custom_components.localthings.registry.discovery import BoundEntity
from custom_components.localthings.registry.entities import NumberDesc
@@ -1038,8 +1045,19 @@ async def test_send_command_reconnects_and_retries_after_socket_closed(
raise ConnectionError("socket closed")
return (0x44, b"")
def _drop_session():
coordinator._session = None
reconnects = {"n": 0}
def _reconnect():
reconnects["n"] += 1
coordinator._session = fake
with (
patch.object(fake, "subscribe"),
patch.object(coordinator, "_close_session", side_effect=_drop_session),
patch.object(coordinator, "_connect_session", side_effect=_reconnect),
patch(
"custom_components.localthings.coordinator.asyncio.sleep",
new=AsyncMock(),
@@ -1048,6 +1066,8 @@ async def test_send_command_reconnects_and_retries_after_socket_closed(
fake.post = _post
await coordinator.async_send_command(bound, 5)
assert reconnects["n"] == 1
assert calls["n"] == 2
assert coordinator._cache.get("/test/vs/0") == {"value": 5}