Files
LEDMatrix/test/test_sync_manager.py
T
10e75b977f Cover the next tier of untested modules and endpoints, and fix the 43 bugs that surfaced (#459)
* test(sync): cover the display sync protocol, and fix what that surfaced

DisplaySyncManager had no tests at all — it appeared in the suite only as
a MagicMock() stand-in, so none of its framing, handshake, or socket
handling was ever exercised. Writing that coverage surfaced three bugs.

Both receive loops caught the generic Exception and immediately retried.
A socket left in a bad state raises on every call, so the thread spun at
100% CPU logging the same line; the reverted-code run of the new
regression test takes 24 seconds where the fixed one takes 0.2. Both now
back off briefly before retrying.

The follower dispatched on `data[:8] == _RAW_MAGIC or len(data) > 512`.
That size threshold is not part of either wire format: a control message
over 512 bytes — a hello_ack carrying a long incompatibility error, for
instance — went to the image decoder and was dropped, and a raw frame
under 512 bytes went to the JSON parser. Both formats are already
self-describing, so dispatch on the magic prefix and treat a JSON parse
failure as the legacy unmarked PNG, with the shared frame bookkeeping
factored into _handle_received_frame().

_oversized_frame_warned was created on first use through
getattr(self, ..., False) rather than in __init__, alone among the
instance attributes.

75 tests: role parsing, the hello compatibility matrix, watchdog
timeouts, both receive loops, the TCP image server's length and
dimension caps and decompression-bomb guard, status shape per role, and
one end-to-end loopback handshake so the wire format is exercised for
real and not only against mocks.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NohXi78cwsAKtN1sCfxjUh

* test(logos): cover LogoHelper, and stop bad downloads poisoning the cache

Nothing in test/ referenced logo_helper.py, so its caching, resizing and
download-fallback logic was entirely unexercised. Two bugs surfaced.

_download_logo wrote response.content to disk with no size limit and no
check that the bytes were an image. A logo URL is remote input, so the
response chose how much went into the assets directory; worse, an
undecodable one stayed there, and because load_logo() only reports the
decode failure and returns None, every later call re-read the same
corrupt file. The download path never retried, so a single bad response
made a logo permanently blank rather than falling back to the
placeholder. Cap the response, verify it decodes, and delete it if not,
which lets the existing fallback in load_logo_with_download do its job.

get_cache_stats() divided by self.cache_size with no guard, so a helper
built with cache_size=0 raised ZeroDivisionError from what is only a
stats call.

37 tests: size-qualified cache keys, LRU eviction and refresh, the four
load_logo_with_download paths, download permissions and timeout,
placeholder generation, and the abbreviation normalizer — including a
test pinning its deliberate divergence from
LogoDownloader.normalize_abbreviation, since logo filenames on existing
installs depend on both behaviors staying put.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NohXi78cwsAKtN1sCfxjUh

* test(web): cover the error and response builders, and stop dropping empty values

errors.py and error_handler.py's response builders had no direct tests,
though every API response passes through them. Two bugs surfaced.

WebInterfaceError set suggested_fixes with `or`, so a caller passing []
to mean "I have no suggestions for this one" got the default list
instead. Only None should fall back.

create_success_response gated `data` on `is not None` but `message` and
`metadata` on truthiness, so an explicitly-passed "" or {} vanished from
the response while 0 and False survived — the response shape depended on
the value. api_helpers.success_response() then re-gated metadata the same
way, which is the path every api_v3 endpoint actually calls, so fixing
only the inner function would have changed nothing observable. Both now
use `is not None`.

That wrapper also merged request timing into the caller's own metadata
dict in place. A caller reusing a dict across requests would accumulate
previous responses' timings; it now copies before adding.

79 tests: category inference for every error code, mapped vs fallback
suggestions, the JSON shape including which keys are omitted when empty,
exception-to-code inference, and the success/error builders end to end.
Two behaviours are pinned as deliberate rather than fixed: an empty
context stays out of the response body, and from_exception's `message`
is the fixed per-code string, never the raw exception text.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NohXi78cwsAKtN1sCfxjUh

* test(web): cover the input validators, and close three holes in them

validators.py had tests for dedup_unique_arrays only; the other eight
functions were untested. Three bugs surfaced.

validate_image_url checked for '..' only inside its relative-path
branch, so http://host/../secret passed validation while /../secret was
rejected — the traversal check now runs before the branch split, which
is where a safety check on the whole URL belongs.

validate_file_upload lowercased the uploaded filename's extension but
compared it against the caller's list verbatim, so allowed_extensions of
['.TTF'] rejected every valid .ttf file. Both sides are lowercased now.
The one in-tree caller passes lowercase already, so this only widens what
future callers can hand it.

validate_numeric_range accepted True and False, because bool subclasses
int; a boolean then compared as 1 or 0 against the range and validated
cleanly. Excluded explicitly, matching how base_plugin.py already handles
the same trap for display_duration.

84 tests. Two behaviours are pinned rather than changed:
sanitize_plugin_config deliberately does not HTML-escape strings, since
escaping at this layer would store the escaped form in config.json — the
docstring said "prevent injection", which read as a promise it does not
keep, and now says what it actually does. validate_font_awesome_class's
second 'fa-' check is unreachable behind its own regex; harmless, so
characterized rather than removed.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NohXi78cwsAKtN1sCfxjUh

* test(api): cover wifi and registry endpoints, and fix bodyless POSTs

The /wifi/* routes drive the host's real networking and the registry
routes reach GitHub, and neither had endpoint-level tests. Covering them
surfaced a bug affecting six endpoints.

Six handlers read their body as `request.get_json() or {}`. The `or {}`
says every field is optional and a missing body should fall back to
defaults — but get_json() without silent=True raises UnsupportedMediaType
when there is no JSON Content-Type, and it raises before `or {}` is ever
evaluated. Each handler's catch-all then reported that as a 500. So
POSTing with no body — what curl sends by default, and what a fetch()
without options sends — failed on /plugins/store/refresh,
/display/on-demand/start, /plugins/config/reset,
/plugins/of-the-day/json/delete, /plugins/{id}/limits and
/plugins/authenticate/spotify. The shipped UI always sends a JSON object,
which is why this stayed hidden.

All six now use silent=True. test_api_v3_optional_body.py covers the
affected endpoints and adds a source check, since the combination of
`or <default>` with a non-silent read is self-contradictory wherever it
appears and is easier to catch by inspection than by exercising each
endpoint by hand.

Also adds test/_api_v3_test_helpers.py: the blueprint holds its managers
on a module-level singleton rather than in Flask app state, so a test
that mocks them leaks into every later test unless the originals are
restored. The existing _make_client() does this for unittest classes;
this is the pytest-fixture equivalent, for the five suites still to come.

69 endpoint tests: connect/disconnect/AP/radio including the string-aware
boolean coercion these endpoints deliberately use, the radio's
lockout-refusal path, registry refresh and fetch-from-URL, and a guard
that WiFiManager is never constructed for real.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NohXi78cwsAKtN1sCfxjUh

* test(api): cover the music auth endpoints, and always clean up the wrapper

The Spotify step-2 handler writes a Python wrapper script to a temp file
with the user's redirect URL embedded in its source, then executes it.
That is the most dangerous shape in the blueprint and had no tests.

The wrapper was deleted in the success/failure branch and again in the
TimeoutExpired handler. Any other failure from subprocess.run — no
interpreter, a fork failure, an interrupted call — reached neither, and
left a world-readable temp file containing the user's redirect URL on
disk. Cleanup moves to a finally block, which is what "delete this
whatever happens" should have been from the start.

The injection tests are the point of this file. Eight adversarial
redirect URLs (embedded quotes, backslashes, newlines, triple quotes, a
full `"; import os; os.system("id"); "`) are each pushed through the
endpoint and the generated wrapper is parsed with ast: it must still be
valid Python, the URL must still be a single string literal bound to
redirect_url, and no os.system call may appear anywhere in the tree.
json.dumps holds up, but nothing was checking that it does.

40 tests. Also pins that the two endpoints are not symmetrical despite
the matching names — only Spotify has a two-step flow and a wrapper; YTM
runs its script directly — so a later change does not "restore" a parity
that was never there.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NohXi78cwsAKtN1sCfxjUh

* test(api): cover the credentials upload, and stop it hoarding secrets

The endpoint that receives the user's Google OAuth credentials file had
no tests. Two bugs surfaced.

The OAuth-shape check ran inside `except Exception: pass`. A JSON
document that parses but is not an object — a bare 42, true, null, a
list — makes `'installed' not in creds_data` raise TypeError, which the
bare except swallowed, and the file was then written out as
credentials.json regardless. The check now decides the outcome instead
of being advisory, so anything not credentials-shaped is refused up
front rather than failing later inside the calendar plugin.

Every overwrite copies the old file to credentials.json.backup.<ts> and
nothing removed them, so a user who re-uploaded ten times had ten
complete sets of OAuth client credentials sitting in the plugin
directory, indefinitely. Keep the newest five. Pruning is housekeeping,
so a backup that cannot be removed logs and leaves the upload alone.

27 tests: size and extension limits, malformed JSON, the shape check,
0600 permissions on the written file, backup-on-overwrite, and pruning
including the repeated-upload case that stays bounded.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NohXi78cwsAKtN1sCfxjUh

* test(api): cover the install endpoints, and make 14 dead guards reachable

/plugins/install and /plugins/install-from-url were tested only at the
PluginStoreManager layer, so the route logic — the queue-versus-direct
branch, schema invalidation, discovery, state and history recording — was
unexercised.

Covering them surfaced the wider form of the body-parsing bug fixed for
the `or {}` handlers in the previous commit. Fourteen handlers read
`data = request.get_json()` and immediately guard with `if not data:
return 400, 'No data provided'`. That guard cannot run: get_json()
without silent=True raises UnsupportedMediaType for a request with no
JSON body, so the catch-all answered 500 "an error occurred; see logs
for details" where the handler plainly meant to answer 400 and say
which field was missing. Every one of these endpoints told a caller who
simply forgot the body to go read the server logs.

All fourteen now use silent=True, so the guard each author already wrote
is the one that runs. This covers /config/raw/main and /config/raw/secrets
among them, whose own bodyless case had the same shape.

The two remaining bare reads are left alone: neither declares what a
missing body should do, so there is no stated intent to honour.

31 install tests plus 17 body tests. The install pair is checked against
each other rather than only individually — the same install logic is
written twice, once in the queue callback and once in the fallback, so
the tests assert both produce identical schema, discovery, state and
history effects. They agree today; the one difference is the success
message wording, which is characterized rather than changed.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NohXi78cwsAKtN1sCfxjUh

* test(api): cover the raw config write endpoints

/config/raw/main and /config/raw/secrets write whatever JSON they are
given straight to config.json and config_secrets.json, bypassing the
secret-separation path the rest of the config surface goes through. Given
how carefully that surface keeps secrets out of config.json, the pair
that skips it was worth pinning precisely. Backed by a real
ConfigManager over tmp_path, so the assertions are against files on disk.

20 tests covering both routes: what lands in which file, that a raw
secrets write never touches config.json and vice versa, the GitHub token
reload, the uninitialized-manager and empty-body branches, and the
ConfigError path that carries config_path through to the response.

The bypass itself is pinned as intentional rather than changed — these
back the raw JSON editor, so writing the body verbatim is the feature.
The test says so explicitly, because the failure mode is someone later
routing plugin config through here as a convenience and silently losing
secret separation.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NohXi78cwsAKtN1sCfxjUh

* test(api): cover backup restore and path containment, and fix restore scope

Restore is the most destructive thing the web interface can do — it
overwrites config, secrets, WiFi settings and fonts, then reinstalls
plugins — and neither it nor the file routes beside it had tests.

A malformed `options` field fell back to {}. Every RestoreOptions flag
defaults to True, so a caller who asked for a narrow restore and
mis-serialized the request got a full one instead, secrets included, and
was told it succeeded. Valid JSON that is not an object was worse:
`"null"` or `"[1,2]"` reached .get() on a non-dict and raised, so the
request died as a generic 500. Both are now refused with a 400 that says
what was wrong, and restore_backup is never reached.

The other file routes take a filename straight out of the URL and turn it
into a path — one to read, one to unlink. _safe_backup_path is the only
thing keeping those inside the export directory, and it was untested. No
bypass was found; the thirteen traversal shapes are pinned so a later
loosening of that pattern has to argue with something. The delete route's
by-name enumeration is covered too, including that a directory sharing a
backup's name is not removed.

84 tests. Two behaviours are pinned as intentional: a failed plugin
reinstall turns the whole restore into an error even though file
restoration succeeded, and omitting `options` entirely still means
restore everything — that is the documented default, and it is only the
mis-serialized case that was wrong.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NohXi78cwsAKtN1sCfxjUh

* ci: raise coverage floor to 52%

Measured 54.45% after the Tier 1 and Tier 2 suites, up from 50%. Keeping
the same two points of headroom the 45 -> 48 ratchet used.

The modules this branch set out to cover: sync_manager 0 -> 97%,
logo_helper 0 -> 98%, errors and error_handler 0 -> 100%, validators
0 -> 97%. api_v3 moved less in percentage terms because it is 4,341
statements, but the endpoints covered are the destructive and
credential-handling ones.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NohXi78cwsAKtN1sCfxjUh

* test(sync): probe for a free port on loopback, not every interface

CodeQL flagged the ephemeral-port probe in the handshake test for
binding to all interfaces. The probe only needs a free port number, so
loopback is both sufficient and correct — a test should not open a port
to the network to discover one.

The manager under test still binds to all interfaces, which is
deliberate and already marked nosec: a follower has to receive the
leader's UDP broadcast.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NohXi78cwsAKtN1sCfxjUh

* fix: bound the logo download, and stop malformed input reading as a fault

Review findings on the coverage branch.

The download size cap I added checked len(response.content), which has
already buffered the whole body -- it stopped the bytes reaching disk but
not memory, which was the point. A server that omits Content-Length and
never stops sending would still exhaust the process. Stream it instead,
counting as it arrives, into a sibling .part file that is replaced over
the target only once it decodes. A transfer that dies midway now leaves
nothing behind rather than a truncated logo for load_logo() to cache.

The follower's control-message handler caught three exception types, but
two reachable UDP payloads raise others: a bare JSON scalar makes
msg.get() raise AttributeError, and an "sx" carrying a non-numeric x
raises ValueError or TypeError from float(). Those escaped to the outer
handler, skipping the legacy-PNG fallback and -- since this branch added
a backoff there -- charging one malformed packet a 0.1s stall on the
receive path. The legacy-PNG path also decoded without the dimension cap
its TCP counterpart applies, so a crafted 65KB frame could force a large
allocation on the render thread; both paths now share one constant.

Three repo_url handlers called .strip() on client input without checking
it was a string, so {"repo_url": 12345} answered 500. The credentials
upload parsed the same file twice, the second time inside a bare except
that a preceding parse had already made unreachable. And both raw-config
handlers kept a json.JSONDecodeError arm that get_json(silent=True) had
turned into dead code, collapsing "sent something unparseable" into "sent
nothing" -- they now say which.

Two of the new tests were not testing what they claimed. The pruning
round-trip wrote ten backups inside one second, so all ten landed on the
same int(time.time()) filename and overwrote each other; it never reached
the limit it asserted. And the sync clock helper patched attributes on the
stdlib time module, freezing time process-wide for every daemon thread
earlier tests had left running.

Full suite: 3352 passed, coverage 54%.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NohXi78cwsAKtN1sCfxjUh

* test(sync): probe broadcast by sending, not by listening

The broadcast check added in the previous commit bound INADDR_ANY to
receive its own probe datagram, and the free-port probe did the same to
pick a port. CodeQL flagged both, correctly: a test suite has no reason
to open a socket the whole network can reach.

Sending is enough for what the probe is actually for. An environment
that refuses broadcast raises on sendto, which is the case that occurs
in sandboxes and is the one worth skipping over; confirming delivery
would have required the listening socket. A network that accepts the
send and silently drops it still reaches the assertion, exactly as it
did before either commit. The port probe binds loopback -- it only needs
a number, and the manager's own bind is the one that has to succeed, with
the retry loop already covering a port taken elsewhere.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NohXi78cwsAKtN1sCfxjUh

* fix: keep callback faults out of the frame-decode fallback

Review follow-up on the previous two commits.

Widening the control-message except tuple put the callback dispatch
inside it, so an _on_new_cycle() that raised ValueError, TypeError or
AttributeError sent a perfectly good control packet to the legacy PNG
decoder -- which reported it as an image decode error and buried the
real fault. Split the two: whether the payload parses as JSON decides
frame vs control message, a second guard covers reading the fields of an
attacker-shaped body, and the callback fires outside both. It still
cannot kill the receive thread; the loop's own handler catches it, and
now says what actually went wrong.

The logo download's temp file was a fixed "<name>.part". Two plugins
asking for the same logo at once would interleave writes into it,
publish the mixture, or delete each other's partial. mkstemp gives each
download its own name in the same directory, so os.replace stays atomic.
Its descriptor is adopted by fdopen before the request runs, since a
request that raises before the write would otherwise leak the fd --
quietly, because load_logo_with_download swallows that.

Two test fixes: the oversized-frame test replaced PIL.Image.open
process-wide, the same hazard the clock helper documents, and Ruff B007
on an unused loop variable.

Full suite: 3355 passed, coverage 54%.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NohXi78cwsAKtN1sCfxjUh

* test(sync): cover the announce loop, and reject non-finite scroll positions

Three review findings from the follower receive path.

Non-finite scroll x reached follower rendering. json.loads accepts the
bare NaN/Infinity literals and float() accepts them as strings, so
"x": NaN arrived as a real float and was stored verbatim. NaN loses
every comparison the scroll code makes, so a follower given one sits on
a position it can never advance past. It now raises through the existing
malformed-control-message guard, which logs and drops the packet and
leaves the last good position in place.

_broadcast_available() only proves the host accepts sendto() for a
broadcast; a network that accepts the send and drops the packet would
let TestRealSocketHandshake run to its five-second deadline and fail on
assertions the code did not break. The deadline now distinguishes the
two: if not one packet crossed in either direction, that is the
environment, and the test skips rather than reporting a protocol
failure.

That skip could hide a real regression in the announcing side, so
TestFollowerAnnounceLoop covers it on mock sockets, where no network is
involved and nothing can skip: hello carries this display's hardware
config and goes to the broadcast address, heartbeats follow, an empty
hardware config falls back to 32x64x1, hello is not resent before its
interval, and a send failure is swallowed rather than killing the loop.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NohXi78cwsAKtN1sCfxjUh

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-08-21 11:16:52 -04:00

1088 lines
44 KiB
Python

"""
Tests for src/common/sync_manager.py — the UDP leader/follower protocol
that synchronizes scrolling content across two LED matrix displays.
This module had zero coverage: it only ever appeared in the suite as a
MagicMock() stand-in (test_vegas_continuous_refresh.py,
test_display_controller_vegas_tick.py), so none of its real framing,
handshake, or socket logic was exercised.
Most tests build the manager via object.__new__() + manual attribute
assignment (the test_display_controller_vegas_tick.py bare-stub pattern)
so no real sockets open and no background threads start. Receive loops are
driven synchronously by once_then_stop(): the mocked socket call returns
one crafted packet, then flips _running False and raises socket.timeout,
so `while self._running:` exits after exactly one real iteration.
Regression coverage for three fixed bugs:
- Both recv loops' generic `except Exception` retried with no delay, so a
socket stuck raising a non-timeout error spun the thread at 100% CPU.
- _follower_recv_loop dispatched on `data[:8] == _RAW_MAGIC or
len(data) > 512`, which routed any control message over 512 bytes into
the image decoder (dropping it) and any raw frame under 512 bytes into
the JSON parser.
- _oversized_frame_warned was read via getattr(self, ..., False) instead of
being initialized in __init__.
"""
import io
import json
import socket
import threading
import time
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import numpy as np
import pytest
from PIL import Image
from src.common import sync_manager
from src.common.sync_manager import (
DisplaySyncManager,
FollowerState,
LeaderState,
SyncRole,
)
@pytest.fixture(autouse=True)
def _isolated_status_file(tmp_path, monkeypatch):
# STATUS_FILE is a module-level fixed path under tempfile.gettempdir() —
# genuinely shared state between tests and even between processes.
monkeypatch.setattr(
sync_manager, "STATUS_FILE", str(tmp_path / "led_matrix_sync_status.json"))
def make_manager(role=SyncRole.STANDALONE, hw_config=None):
"""Bare stub bypassing __init__'s socket/thread setup."""
mgr = object.__new__(DisplaySyncManager)
mgr.role = role
mgr.logger = MagicMock()
mgr.port = sync_manager.SYNC_PORT
mgr._hw_config = hw_config or {"rows": 32, "cols": 64, "chain_length": 1}
mgr._leader_state = LeaderState.NO_PEER
mgr._peer_ip = None
mgr._peer_compatible = False
mgr._peer_chain = 0
mgr._last_heartbeat_time = 0.0
mgr._leader_width = 0
mgr._oversized_frame_warned = False
mgr._follower_state = FollowerState.STANDALONE
mgr._latest_frame = None
mgr._latest_scroll_x = None
mgr._last_leader_frame_time = 0.0
mgr._frame_lock = threading.Lock()
mgr._leader_ip = None
mgr._on_new_cycle = None
mgr._on_scroll_image = None
mgr._pending_scroll_image = None
mgr._scroll_image_lock = threading.Lock()
mgr._img_server_sock = None
mgr._on_follower_connected = None
mgr._error_message = None
mgr._running = False
mgr._recv_sock = None
mgr._send_sock = None
return mgr
def once_then_stop(mgr, value):
"""side_effect returning `value` once, then stopping the enclosing loop."""
state = {"served": False}
def _side_effect(*args, **kwargs):
if not state["served"]:
state["served"] = True
return value
mgr._running = False
raise socket.timeout()
return _side_effect
def raise_n_then_stop(mgr, exc, count):
"""side_effect raising `exc` `count` times, then stopping the loop."""
state = {"n": 0}
def _side_effect(*args, **kwargs):
state["n"] += 1
if state["n"] <= count:
raise exc
mgr._running = False
raise socket.timeout()
return _side_effect
def fake_clock(monkeypatch, *, time_fn=None, sleep_fn=None):
"""Swap sync_manager's own `time` reference for a private stand-in.
sync_manager.time IS the stdlib module, so patching attributes on it
would freeze the clock and no-op sleep for the whole process —
including the daemon threads earlier tests left running, which is a
hard-to-trace source of cross-test flakiness. Rebinding the module's
reference keeps the patch scoped to the code under test. Anything not
overridden falls through to the real functions.
"""
monkeypatch.setattr(sync_manager, "time", SimpleNamespace(
time=time_fn or time.time,
sleep=sleep_fn or time.sleep,
))
def run_watchdog_once(monkeypatch, mgr, watchdog, now):
"""Run exactly one watchdog iteration at a frozen wall-clock time."""
fake_clock(monkeypatch,
time_fn=lambda: now,
sleep_fn=lambda _: setattr(mgr, "_running", False))
mgr._running = True
watchdog()
class FakeConn:
"""Minimal TCP connection stand-in whose recv() drains a byte buffer."""
def __init__(self, payload: bytes):
self._buf = payload
self.closed = False
def settimeout(self, _):
pass
def recv(self, n):
chunk, self._buf = self._buf[:n], self._buf[n:]
return chunk
def close(self):
self.closed = True
def png_bytes(size=(10, 10), color=(1, 2, 3)) -> bytes:
buf = io.BytesIO()
Image.new("RGB", size, color).save(buf, format="PNG")
return buf.getvalue()
def raw_frame_packet(width, height, color=(10, 20, 30)) -> bytes:
arr = np.asarray(Image.new("RGB", (width, height), color), dtype=np.uint8)
return _magic_header(width, height) + arr.tobytes()
def _magic_header(width, height) -> bytes:
return sync_manager._RAW_MAGIC + sync_manager._RAW_HEADER.pack(width, height)
def length_prefixed(payload: bytes) -> bytes:
return len(payload).to_bytes(4, "big") + payload
class TestRoleParsing:
def test_leader_role(self, monkeypatch):
monkeypatch.setattr(DisplaySyncManager, "_start_leader", lambda self: None)
assert DisplaySyncManager("leader", {}, {}, MagicMock()).role is SyncRole.LEADER
def test_follower_role(self, monkeypatch):
monkeypatch.setattr(DisplaySyncManager, "_start_follower", lambda self: None)
assert DisplaySyncManager("follower", {}, {}, MagicMock()).role is SyncRole.FOLLOWER
def test_standalone_starts_nothing(self):
mgr = DisplaySyncManager("standalone", {}, {}, MagicMock())
assert mgr.role is SyncRole.STANDALONE
assert mgr._running is False
assert mgr._recv_sock is None
def test_invalid_role_warns_and_falls_back(self):
logger = MagicMock()
assert DisplaySyncManager("bogus", {}, {}, logger).role is SyncRole.STANDALONE
assert logger.warning.called
def test_role_matching_is_case_sensitive(self):
# Pinned: SyncRole's values are lowercase, so "LEADER" is not
# normalized — it is simply invalid and falls back to standalone.
logger = MagicMock()
assert DisplaySyncManager("LEADER", {}, {}, logger).role is SyncRole.STANDALONE
assert logger.warning.called
def test_port_defaults_to_module_constant(self):
assert DisplaySyncManager("standalone", {}, {}, MagicMock()).port == sync_manager.SYNC_PORT
def test_port_read_from_config(self):
assert DisplaySyncManager("standalone", {"port": 9999}, {}, MagicMock()).port == 9999
def test_oversized_frame_warned_initialized_in_init(self, monkeypatch):
# Regression: this attribute was only ever created on first use via
# getattr(self, '_oversized_frame_warned', False).
monkeypatch.setattr(DisplaySyncManager, "_start_leader", lambda self: None)
mgr = DisplaySyncManager("leader", {}, {}, MagicMock())
assert mgr._oversized_frame_warned is False
class TestHandleHello:
def test_matching_panels_connect(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._send_sock = MagicMock()
mgr._handle_hello({"t": "hello", "rows": 32, "cols": 64, "chain": 3}, "10.0.0.5")
assert mgr._leader_state is LeaderState.CONNECTED
assert mgr._peer_ip == "10.0.0.5"
assert mgr._peer_compatible is True
assert mgr._peer_chain == 3
assert mgr._error_message is None
def test_ack_reports_compatibility(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._send_sock = MagicMock()
mgr._leader_width = 128
mgr._handle_hello({"t": "hello", "rows": 32, "cols": 64, "chain": 1}, "10.0.0.5")
payload, dest = mgr._send_sock.sendto.call_args[0]
ack = json.loads(payload.decode("utf-8"))
assert ack["compatible"] is True
assert ack["leader_width"] == 128
assert dest == ("10.0.0.5", mgr.port)
def test_mismatched_panels_are_incompatible(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._send_sock = MagicMock()
mgr._handle_hello({"t": "hello", "rows": 16, "cols": 32, "chain": 1}, "10.0.0.5")
assert mgr._leader_state is LeaderState.INCOMPATIBLE
assert "Incompatible panels" in mgr._error_message
ack = json.loads(mgr._send_sock.sendto.call_args[0][0].decode("utf-8"))
assert ack["compatible"] is False
assert ack["error"] == mgr._error_message
def test_chain_length_may_differ(self):
# Documented rule: rows/cols must match, chain_length need not.
mgr = make_manager(role=SyncRole.LEADER, hw_config={"rows": 32, "cols": 64, "chain_length": 1})
mgr._send_sock = MagicMock()
mgr._handle_hello({"t": "hello", "rows": 32, "cols": 64, "chain": 4}, "10.0.0.5")
assert mgr._leader_state is LeaderState.CONNECTED
def test_connect_callback_fires_only_on_first_transition(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._send_sock = MagicMock()
fired = threading.Event()
calls = []
mgr._on_follower_connected = lambda: (calls.append(1), fired.set())
hello = {"t": "hello", "rows": 32, "cols": 64, "chain": 1}
mgr._handle_hello(hello, "10.0.0.5")
assert fired.wait(timeout=1)
assert len(calls) == 1
fired.clear()
mgr._handle_hello(hello, "10.0.0.5") # already CONNECTED
assert not fired.wait(timeout=0.2)
assert len(calls) == 1
def test_ack_send_failure_is_swallowed(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._send_sock = MagicMock()
mgr._send_sock.sendto.side_effect = OSError("network unreachable")
mgr._handle_hello({"t": "hello", "rows": 32, "cols": 64, "chain": 1}, "10.0.0.5")
assert mgr._leader_state is LeaderState.CONNECTED # state still updated
assert mgr.logger.debug.called
class TestWatchdogs:
def test_leader_drops_peer_after_heartbeat_timeout(self, monkeypatch):
mgr = make_manager(role=SyncRole.LEADER)
mgr._leader_state = LeaderState.CONNECTED
mgr._peer_ip = "10.0.0.1"
mgr._peer_compatible = True
mgr._last_heartbeat_time = 0.0
run_watchdog_once(monkeypatch, mgr, mgr._leader_watchdog,
now=sync_manager.PEER_TIMEOUT + 1)
assert mgr._leader_state is LeaderState.NO_PEER
assert mgr._peer_ip is None
assert mgr._peer_compatible is False
def test_leader_keeps_peer_within_timeout(self, monkeypatch):
mgr = make_manager(role=SyncRole.LEADER)
mgr._leader_state = LeaderState.CONNECTED
mgr._peer_ip = "10.0.0.1"
mgr._last_heartbeat_time = 100.0
run_watchdog_once(monkeypatch, mgr, mgr._leader_watchdog, now=101.0)
assert mgr._leader_state is LeaderState.CONNECTED
assert mgr._peer_ip == "10.0.0.1"
def test_leader_watchdog_ignores_disconnected_state(self, monkeypatch):
mgr = make_manager(role=SyncRole.LEADER)
mgr._leader_state = LeaderState.INCOMPATIBLE
mgr._last_heartbeat_time = 0.0
run_watchdog_once(monkeypatch, mgr, mgr._leader_watchdog, now=10_000)
assert mgr._leader_state is LeaderState.INCOMPATIBLE
def test_follower_returns_to_standalone_after_frame_timeout(self, monkeypatch):
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._follower_state = FollowerState.FOLLOWER
mgr._last_leader_frame_time = 0.0
mgr._latest_frame = Image.new("RGB", (2, 2))
run_watchdog_once(monkeypatch, mgr, mgr._follower_watchdog,
now=sync_manager.LEADER_TIMEOUT + 1)
assert mgr._follower_state is FollowerState.STANDALONE
assert mgr.get_latest_frame() is None
def test_follower_keeps_frames_within_timeout(self, monkeypatch):
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._follower_state = FollowerState.FOLLOWER
mgr._last_leader_frame_time = 100.0
mgr._latest_frame = Image.new("RGB", (2, 2))
run_watchdog_once(monkeypatch, mgr, mgr._follower_watchdog, now=101.0)
assert mgr._follower_state is FollowerState.FOLLOWER
assert mgr.get_latest_frame() is not None
class TestLeaderRecvLoop:
def _drive(self, mgr, payload, sender="10.0.0.8"):
mgr._recv_sock = MagicMock()
mgr._recv_sock.recvfrom.side_effect = once_then_stop(mgr, (payload, (sender, 1)))
mgr._running = True
mgr._leader_recv_loop()
def test_hello_is_dispatched(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._send_sock = MagicMock()
self._drive(mgr, json.dumps(
{"t": "hello", "rows": 32, "cols": 64, "chain": 1}).encode())
assert mgr._leader_state is LeaderState.CONNECTED
assert mgr._peer_ip == "10.0.0.8"
def test_heartbeat_from_known_peer_refreshes_timer(self, monkeypatch):
mgr = make_manager(role=SyncRole.LEADER)
mgr._peer_ip = "10.0.0.8"
fake_clock(monkeypatch, time_fn=lambda: 12345.0)
self._drive(mgr, json.dumps({"t": "hb"}).encode())
assert mgr._last_heartbeat_time == 12345.0
def test_heartbeat_from_stranger_is_ignored(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._peer_ip = "10.0.0.8"
mgr._last_heartbeat_time = 5.0
self._drive(mgr, json.dumps({"t": "hb"}).encode(), sender="10.0.0.99")
assert mgr._last_heartbeat_time == 5.0
def test_unknown_message_type_ignored(self):
mgr = make_manager(role=SyncRole.LEADER)
self._drive(mgr, json.dumps({"t": "who-knows"}).encode())
assert mgr._leader_state is LeaderState.NO_PEER
def test_malformed_json_is_swallowed(self):
mgr = make_manager(role=SyncRole.LEADER)
self._drive(mgr, b"{not json")
assert mgr._leader_state is LeaderState.NO_PEER
def test_undecodable_bytes_are_swallowed(self):
mgr = make_manager(role=SyncRole.LEADER)
self._drive(mgr, b"\xff\xfe\x00bad")
assert mgr._leader_state is LeaderState.NO_PEER
def test_backs_off_between_repeated_errors(self, monkeypatch):
# Regression: without a sleep this loop spun at 100% CPU whenever
# the socket raised a non-timeout error on every call.
mgr = make_manager(role=SyncRole.LEADER)
mgr._recv_sock = MagicMock()
mgr._recv_sock.recvfrom.side_effect = raise_n_then_stop(mgr, OSError("boom"), 3)
sleeps = MagicMock()
fake_clock(monkeypatch, sleep_fn=sleeps)
mgr._running = True
mgr._leader_recv_loop()
assert sleeps.call_count == 3
sleeps.assert_called_with(0.1)
class TestFollowerRecvLoop:
def _drive(self, mgr, payload, sender="10.0.0.2"):
mgr._recv_sock = MagicMock()
mgr._recv_sock.recvfrom.side_effect = once_then_stop(mgr, (payload, (sender, 1)))
mgr._running = True
mgr._follower_recv_loop()
def test_small_raw_frame_is_decoded(self):
# Regression: a raw frame under the old 512-byte threshold was sent
# to the JSON parser and dropped.
mgr = make_manager(role=SyncRole.FOLLOWER)
packet = raw_frame_packet(4, 3)
assert len(packet) <= 512
self._drive(mgr, packet)
frame = mgr.get_latest_frame()
assert frame is not None and frame.size == (4, 3)
assert mgr._follower_state is FollowerState.FOLLOWER
def test_large_raw_frame_is_decoded(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
packet = raw_frame_packet(64, 32)
assert len(packet) > 512
self._drive(mgr, packet)
assert mgr.get_latest_frame().size == (64, 32)
def test_large_control_message_is_not_routed_to_image_decode(self):
# Regression: the old `len(data) > 512` branch treated any large
# control message as frame data and silently discarded it.
mgr = make_manager(role=SyncRole.FOLLOWER)
long_error = "x" * 600
payload = json.dumps(
{"t": "hello_ack", "compatible": False, "error": long_error}).encode()
assert len(payload) > 512
self._drive(mgr, payload, sender="10.0.0.9")
assert mgr._leader_ip == "10.0.0.9"
assert mgr._peer_compatible is False
assert mgr._error_message == long_error
assert mgr.get_latest_frame() is None
assert mgr.logger.error.called
def test_legacy_png_frame_without_magic_is_decoded(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
self._drive(mgr, png_bytes(size=(5, 5)))
frame = mgr.get_latest_frame()
assert frame is not None and frame.size == (5, 5)
assert mgr._follower_state is FollowerState.FOLLOWER
def test_truncated_raw_frame_is_swallowed(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
self._drive(mgr, _magic_header(64, 32) + b"\x00" * 10) # far too short
assert mgr.get_latest_frame() is None
assert mgr.logger.debug.called
def test_garbage_payload_is_swallowed(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
self._drive(mgr, b"neither json nor a png, just bytes 1234567890")
assert mgr.get_latest_frame() is None
def test_hello_ack_updates_peer_state(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
self._drive(mgr, json.dumps(
{"t": "hello_ack", "compatible": True, "error": None}).encode(),
sender="10.0.0.6")
assert mgr._leader_ip == "10.0.0.6"
assert mgr._peer_compatible is True
assert mgr.logger.error.called is False
def test_scroll_x_switches_to_follower_and_builds_cycle(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
calls = []
mgr._on_new_cycle = lambda: calls.append(1)
self._drive(mgr, json.dumps({"t": "sx", "x": 12.34}).encode())
assert mgr._follower_state is FollowerState.FOLLOWER
assert mgr.get_latest_scroll_x() == 12.34
assert calls == [1]
def test_scroll_x_while_already_following_does_not_rebuild(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._follower_state = FollowerState.FOLLOWER
calls = []
mgr._on_new_cycle = lambda: calls.append(1)
self._drive(mgr, json.dumps({"t": "sx", "x": 5.0}).encode())
assert mgr.get_latest_scroll_x() == 5.0
assert calls == []
def test_new_cycle_message_triggers_callback(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._follower_state = FollowerState.FOLLOWER
calls = []
mgr._on_new_cycle = lambda: calls.append(1)
self._drive(mgr, json.dumps({"t": "nc"}).encode())
assert calls == [1]
def test_non_object_json_does_not_reach_the_outer_handler(self):
# A bare JSON scalar parses, then msg.get() raises AttributeError.
# That has to be caught here so the payload still gets its shot at
# the legacy-PNG fallback; escaping to the outer handler would also
# charge one malformed packet the 0.1s error backoff.
mgr = make_manager(role=SyncRole.FOLLOWER)
sleeps = MagicMock()
with patch.object(sync_manager, "time",
SimpleNamespace(time=time.time, sleep=sleeps)):
self._drive(mgr, b"12345")
assert mgr.get_latest_frame() is None
sleeps.assert_not_called()
def test_non_numeric_scroll_x_does_not_reach_the_outer_handler(self):
# float("a") raises ValueError; {"x": null} raises TypeError.
for payload in ({"t": "sx", "x": "a"}, {"t": "sx", "x": None}):
mgr = make_manager(role=SyncRole.FOLLOWER)
sleeps = MagicMock()
with patch.object(sync_manager, "time",
SimpleNamespace(time=time.time, sleep=sleeps)):
self._drive(mgr, json.dumps(payload).encode())
assert mgr.get_latest_scroll_x() is None
sleeps.assert_not_called()
def test_callback_failure_is_not_mistaken_for_a_malformed_packet(self, monkeypatch):
# A payload that parses is a control message, full stop. If the
# callback it triggers raises one of the types the field guard
# catches, that fault belongs to the callback: it must not send
# the packet to the image decoder, which would report it as a
# decode error and bury the real cause. The loop still survives
# it — the outer handler catches it like any other fault.
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._follower_state = FollowerState.FOLLOWER
def boom():
raise ValueError("callback is broken")
mgr._on_new_cycle = boom
fake_clock(monkeypatch, sleep_fn=MagicMock())
self._drive(mgr, json.dumps({"t": "nc"}).encode())
logged = " | ".join(str(c) for c in mgr.logger.debug.call_args_list)
assert "callback is broken" in logged
assert "frame decode error" not in logged
assert "malformed control message" not in logged
def test_oversized_legacy_frame_is_rejected_before_decode(self, monkeypatch):
# The UDP path is reachable by any host on the LAN, so it caps
# dimensions before load() just as the TCP image server does.
mgr = make_manager(role=SyncRole.FOLLOWER)
class Huge:
width, height = 10, sync_manager._MAX_FRAME_H + 1
def load(self):
raise AssertionError("load() must not run past the cap")
# Rebind the module's reference rather than mutating PIL.Image
# itself, which would hand Huge() to every caller in the process
# — including daemon threads earlier tests left running. Same
# reasoning as fake_clock above. The other names the receive loop
# reads off this reference pass through to the real module.
monkeypatch.setattr(sync_manager, "Image", SimpleNamespace(
open=lambda *a, **kw: Huge(),
frombuffer=Image.frombuffer,
DecompressionBombError=Image.DecompressionBombError,
))
self._drive(mgr, b"\x89PNG not really but not JSON either")
assert mgr.get_latest_frame() is None
@pytest.mark.parametrize("literal", ["NaN", "Infinity", "-Infinity"])
def test_non_finite_scroll_x_is_rejected(self, literal):
# json.loads accepts these bare literals, and float() accepts them
# as strings, so they arrive as real floats rather than raising.
# NaN in particular survives every comparison the scroll code makes
# (all false), so the follower would sit on a position it can never
# advance past. It has to be treated as a malformed message.
for payload in (b'{"t": "sx", "x": ' + literal.encode() + b'}',
json.dumps({"t": "sx", "x": literal}).encode()):
mgr = make_manager(role=SyncRole.FOLLOWER)
calls = []
mgr._on_new_cycle = lambda: calls.append(1)
self._drive(mgr, payload)
assert mgr.get_latest_scroll_x() is None
assert mgr._follower_state is FollowerState.STANDALONE
assert calls == []
def test_non_finite_scroll_x_leaves_a_good_value_in_place(self):
# The reject must not clear the last usable position either — a
# follower mid-scroll keeps rendering from where it was.
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._follower_state = FollowerState.FOLLOWER
self._drive(mgr, json.dumps({"t": "sx", "x": 7.5}).encode())
assert mgr.get_latest_scroll_x() == 7.5
self._drive(mgr, b'{"t": "sx", "x": NaN}')
assert mgr.get_latest_scroll_x() == 7.5
def test_scroll_x_missing_key_is_swallowed(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
self._drive(mgr, json.dumps({"t": "sx"}).encode()) # no "x"
assert mgr.get_latest_scroll_x() is None
def test_backs_off_between_repeated_errors(self, monkeypatch):
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._recv_sock = MagicMock()
mgr._recv_sock.recvfrom.side_effect = raise_n_then_stop(mgr, OSError("boom"), 3)
sleeps = MagicMock()
fake_clock(monkeypatch, sleep_fn=sleeps)
mgr._running = True
mgr._follower_recv_loop()
assert sleeps.call_count == 3
sleeps.assert_called_with(0.1)
class TestSendFrame:
def _connected_leader(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._leader_state = LeaderState.CONNECTED
mgr._peer_ip = "10.0.0.1"
mgr._send_sock = MagicMock()
return mgr
def test_frame_sent_with_magic_header(self):
mgr = self._connected_leader()
mgr.send_frame(Image.new("RGB", (8, 8)))
packet = mgr._send_sock.sendto.call_args[0][0]
assert packet[:8] == sync_manager._RAW_MAGIC
assert sync_manager._RAW_HEADER.unpack(packet[8:12]) == (8, 8)
def test_oversized_frame_warns_once_and_is_dropped(self):
mgr = self._connected_leader()
big = Image.new("RGB", (300, 300)) # 270000 bytes > 65000 UDP cap
mgr.send_frame(big)
assert mgr._oversized_frame_warned is True
assert mgr.logger.warning.call_count == 1
assert not mgr._send_sock.sendto.called
mgr.send_frame(big)
assert mgr.logger.warning.call_count == 1 # still warned only once
def test_not_sent_when_no_peer(self):
mgr = self._connected_leader()
mgr._leader_state = LeaderState.NO_PEER
mgr.send_frame(Image.new("RGB", (8, 8)))
assert not mgr._send_sock.sendto.called
def test_follower_never_sends(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._send_sock = MagicMock()
mgr.send_frame(Image.new("RGB", (8, 8)))
assert not mgr._send_sock.sendto.called
def test_send_error_is_swallowed(self):
mgr = self._connected_leader()
mgr._send_sock.sendto.side_effect = OSError("no route")
mgr.send_frame(Image.new("RGB", (8, 8))) # must not raise
assert mgr.logger.debug.called
class TestSendControlMessages:
def _connected_leader(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._leader_state = LeaderState.CONNECTED
mgr._peer_ip = "10.0.0.1"
mgr._send_sock = MagicMock()
return mgr
def test_send_scroll_x_rounds_to_two_places(self):
mgr = self._connected_leader()
mgr.send_scroll_x(3.14159)
msg = json.loads(mgr._send_sock.sendto.call_args[0][0].decode())
assert msg == {"t": "sx", "x": 3.14}
def test_send_new_cycle(self):
mgr = self._connected_leader()
mgr.send_new_cycle()
msg = json.loads(mgr._send_sock.sendto.call_args[0][0].decode())
assert msg == {"t": "nc"}
def test_control_messages_noop_when_disconnected(self):
mgr = self._connected_leader()
mgr._leader_state = LeaderState.NO_PEER
mgr.send_scroll_x(1.0)
mgr.send_new_cycle()
assert not mgr._send_sock.sendto.called
def test_set_leader_width(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr.set_leader_width(256)
assert mgr._leader_width == 256
class TestImageServerLoop:
def _drive(self, mgr, conn):
mgr._img_server_sock = MagicMock()
mgr._img_server_sock.accept.side_effect = once_then_stop(
mgr, (conn, ("10.0.0.1", 1)))
mgr._running = True
mgr._image_server_loop()
def test_rejects_non_positive_length(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._on_scroll_image = MagicMock()
self._drive(mgr, FakeConn((0).to_bytes(4, "big")))
assert mgr.logger.warning.called
mgr._on_scroll_image.assert_not_called()
def test_rejects_oversized_length(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._on_scroll_image = MagicMock()
self._drive(mgr, FakeConn((11 * 1024 * 1024).to_bytes(4, "big")))
assert mgr.logger.warning.called
mgr._on_scroll_image.assert_not_called()
def test_rejects_oversized_dimensions(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._on_scroll_image = MagicMock()
self._drive(mgr, FakeConn(length_prefixed(png_bytes(size=(300, 300)))))
assert mgr.logger.warning.called
mgr._on_scroll_image.assert_not_called()
def test_rejects_decompression_bomb(self, monkeypatch):
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._on_scroll_image = MagicMock()
class BombImage:
width = height = 10
def load(self):
raise Image.DecompressionBombError("too many pixels")
monkeypatch.setattr(sync_manager.Image, "open", lambda *a, **kw: BombImage())
self._drive(mgr, FakeConn(length_prefixed(png_bytes())))
assert mgr.logger.warning.called
mgr._on_scroll_image.assert_not_called()
def test_valid_image_invokes_callback(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
received = []
mgr._on_scroll_image = received.append
self._drive(mgr, FakeConn(length_prefixed(png_bytes(size=(10, 10)))))
assert len(received) == 1
assert received[0].size == (10, 10)
def test_image_cached_when_callback_not_yet_registered(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._on_scroll_image = None
self._drive(mgr, FakeConn(length_prefixed(png_bytes(size=(6, 6)))))
assert mgr._pending_scroll_image is not None
assert mgr._pending_scroll_image.size == (6, 6)
def test_short_header_is_skipped(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._on_scroll_image = MagicMock()
self._drive(mgr, FakeConn(b"\x00\x01")) # under the 4-byte prefix
mgr._on_scroll_image.assert_not_called()
def test_connection_always_closed(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
conn = FakeConn(length_prefixed(png_bytes()))
self._drive(mgr, conn)
assert conn.closed is True
class TestScrollImageCallback:
def test_pending_image_delivered_on_late_registration(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
img = Image.new("RGB", (3, 3))
mgr._pending_scroll_image = img
received = []
mgr.set_on_scroll_image(received.append)
assert received == [img]
assert mgr._pending_scroll_image is None
def test_no_pending_image_means_no_immediate_call(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
received = []
mgr.set_on_scroll_image(received.append)
assert received == []
class TestFollowerConnectedCallback:
def test_fires_immediately_when_already_connected(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._leader_state = LeaderState.CONNECTED
fired = threading.Event()
mgr.set_on_follower_connected(fired.set)
assert fired.wait(timeout=1)
def test_does_not_fire_when_no_peer(self):
mgr = make_manager(role=SyncRole.LEADER)
fired = threading.Event()
mgr.set_on_follower_connected(fired.set)
assert not fired.wait(timeout=0.2)
class TestSendScrollImage:
def test_noop_when_not_connected(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._leader_state = LeaderState.NO_PEER
with patch.object(sync_manager.socket, "socket") as sock:
mgr.send_scroll_image(Image.new("RGB", (4, 4)))
sock.assert_not_called()
def test_noop_for_follower_role(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
with patch.object(sync_manager.socket, "socket") as sock:
mgr.send_scroll_image(Image.new("RGB", (4, 4)))
sock.assert_not_called()
def test_sends_length_prefixed_png(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._leader_state = LeaderState.CONNECTED
mgr._peer_ip = "10.0.0.1"
fake_sock = MagicMock()
fake_sock.__enter__ = lambda s: s
fake_sock.__exit__ = lambda s, *a: False
with patch.object(sync_manager.socket, "socket", return_value=fake_sock):
mgr.send_scroll_image(Image.new("RGB", (4, 4)))
payload = fake_sock.sendall.call_args[0][0]
assert int.from_bytes(payload[:4], "big") == len(payload) - 4
assert payload[4:8] == b"\x89PNG"
def test_connection_error_is_swallowed(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._leader_state = LeaderState.CONNECTED
mgr._peer_ip = "10.0.0.1"
with patch.object(sync_manager.socket, "socket", side_effect=OSError("refused")):
mgr.send_scroll_image(Image.new("RGB", (4, 4))) # must not raise
assert mgr.logger.debug.called
class TestGetStatus:
def test_standalone_shape(self):
status = make_manager(role=SyncRole.STANDALONE).get_status()
assert status["role"] == "standalone"
assert status["state"] == "standalone"
assert status["local_rows"] == 32 and status["local_cols"] == 64
def test_leader_shape(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._leader_state = LeaderState.CONNECTED
mgr._peer_ip = "10.0.0.1"
mgr._peer_compatible = True
mgr._peer_chain = 2
mgr._leader_width = 128
status = mgr.get_status()
assert status["role"] == "leader"
assert status["state"] == "connected"
assert status["peer_ip"] == "10.0.0.1"
assert status["peer_chain"] == 2
assert status["leader_width"] == 128
def test_follower_shape(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._follower_state = FollowerState.FOLLOWER
mgr._leader_ip = "10.0.0.2"
status = mgr.get_status()
assert status["role"] == "follower"
assert status["state"] == "follower"
assert status["leader_ip"] == "10.0.0.2"
assert "peer_chain" not in status
def test_is_follower_active(self):
mgr = make_manager(role=SyncRole.FOLLOWER)
assert mgr.is_follower_active() is False
mgr._follower_state = FollowerState.FOLLOWER
assert mgr.is_follower_active() is True
def test_leader_is_never_follower_active(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._follower_state = FollowerState.FOLLOWER
assert mgr.is_follower_active() is False
class TestWriteStatusFile:
def test_writes_status_and_cleans_up_temp(self):
mgr = make_manager(role=SyncRole.STANDALONE)
mgr.write_status_file()
data = json.loads(Path(sync_manager.STATUS_FILE).read_text())
assert data["role"] == "standalone"
assert "ts" in data
assert not Path(sync_manager.STATUS_FILE + ".tmp").exists()
def test_write_failure_is_swallowed(self, monkeypatch):
mgr = make_manager(role=SyncRole.STANDALONE)
monkeypatch.setattr("builtins.open", MagicMock(side_effect=OSError("disk full")))
mgr.write_status_file() # must not raise
assert mgr.logger.debug.called
class TestStop:
def _stub_with_sockets(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._recv_sock = MagicMock()
mgr._send_sock = MagicMock()
mgr._img_server_sock = MagicMock()
return mgr
def test_closes_every_socket(self):
mgr = self._stub_with_sockets()
mgr.stop()
assert mgr._running is False
mgr._recv_sock.close.assert_called_once()
mgr._send_sock.close.assert_called_once()
mgr._img_server_sock.close.assert_called_once()
def test_is_idempotent(self):
mgr = self._stub_with_sockets()
mgr.stop()
mgr.stop() # must not raise
def test_close_failure_is_swallowed(self):
mgr = make_manager(role=SyncRole.LEADER)
mgr._recv_sock = MagicMock()
mgr._recv_sock.close.side_effect = OSError("already closed")
mgr.stop() # must not raise
assert mgr.logger.debug.called
def test_handles_unset_sockets(self):
make_manager(role=SyncRole.STANDALONE).stop() # all sockets None
class TestFollowerAnnounceLoop:
"""The follower's outbound half of the handshake.
Covered on mock sockets so it does not depend on the network
delivering anything: the real-socket handshake below skips when the
environment drops broadcast, and that skip is only safe because a
regression in what the follower *sends* is caught here instead.
"""
def _run_once(self, monkeypatch, mgr, now=1000.0):
fake_clock(monkeypatch, time_fn=lambda: now,
sleep_fn=lambda _: setattr(mgr, "_running", False))
mgr._running = True
mgr._follower_announce_loop()
def _sent(self, mgr):
return [(json.loads(payload.decode("utf-8")), dest)
for payload, dest in
(call[0] for call in mgr._send_sock.sendto.call_args_list)]
def test_hello_carries_this_display_and_goes_to_broadcast(self, monkeypatch):
mgr = make_manager(role=SyncRole.FOLLOWER,
hw_config={"rows": 64, "cols": 128, "chain_length": 3})
mgr._send_sock = MagicMock()
self._run_once(monkeypatch, mgr)
sent = self._sent(mgr)
assert all(dest == ("<broadcast>", mgr.port) for _, dest in sent)
assert {"t": "hello", "rows": 64, "cols": 128, "chain": 3} in [m for m, _ in sent]
def test_heartbeat_is_announced_too(self, monkeypatch):
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._send_sock = MagicMock()
self._run_once(monkeypatch, mgr)
assert {"t": "hb"} in [m for m, _ in self._sent(mgr)]
def test_hello_defaults_when_hardware_config_is_empty(self, monkeypatch):
mgr = make_manager(role=SyncRole.FOLLOWER, hw_config={})
mgr._send_sock = MagicMock()
self._run_once(monkeypatch, mgr)
hello = next(m for m, _ in self._sent(mgr) if m["t"] == "hello")
assert (hello["rows"], hello["cols"], hello["chain"]) == (32, 64, 1)
def test_hello_is_not_resent_before_its_interval(self, monkeypatch):
# Heartbeat is the faster of the two, so advancing by one heartbeat
# per iteration must produce more heartbeats than hellos.
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._send_sock = MagicMock()
clock = {"now": 1000.0}
ticks = {"n": 0}
def tick(_):
ticks["n"] += 1
clock["now"] += sync_manager.HEARTBEAT_INTERVAL
if ticks["n"] >= 2:
mgr._running = False
fake_clock(monkeypatch, time_fn=lambda: clock["now"], sleep_fn=tick)
mgr._running = True
mgr._follower_announce_loop()
kinds = [m["t"] for m, _ in self._sent(mgr)]
assert kinds.count("hello") == 1
assert kinds.count("hb") == 2
def test_send_failure_is_swallowed(self, monkeypatch):
# This swallow is why a network that drops broadcast looks like
# silence rather than an error — the handshake test's skip exists
# for exactly that reason.
mgr = make_manager(role=SyncRole.FOLLOWER)
mgr._send_sock = MagicMock()
mgr._send_sock.sendto.side_effect = OSError("network unreachable")
self._run_once(monkeypatch, mgr) # must not raise
assert mgr.logger.debug.called
def _broadcast_available(port):
"""True when a UDP broadcast can be sent at all in this environment.
The handshake below depends on broadcast: the follower announces
itself to ("<broadcast>", port), and sync_manager swallows any sendto
error. Without this probe, a sandbox or CI network that refuses
broadcast would make the test wait out its whole deadline and then
fail for a reason that has nothing to do with the code.
This catches only refusal, not silent drop — confirming delivery
would mean binding INADDR_ANY to receive, a listening socket this
suite has no business opening. The drop case is handled at the
deadline instead; see the skip in the handshake test.
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.sendto(b"probe", ("<broadcast>", port))
return True
except OSError:
return False
finally:
sock.close()
class TestRealSocketHandshake:
def test_leader_and_follower_negotiate_over_real_sockets(self, monkeypatch):
# One end-to-end check that the wire format actually round-trips:
# every other test drives the loops with mocked sockets.
#
# Not loopback-only, despite the free-port probe below: the manager
# binds UDP and TCP on all interfaces and the follower announces by
# broadcast. That is the behaviour under test, so the environment
# has to support it.
monkeypatch.setattr(sync_manager, "HELLO_INTERVAL", 0.02)
monkeypatch.setattr(sync_manager, "HEARTBEAT_INTERVAL", 0.02)
hw = {"rows": 32, "cols": 64, "chain_length": 1}
leader = follower = None
# The free-port probe is inherently racy — the port can be taken
# between release and rebind — so retry rather than fail on it.
for _attempt in range(5):
# Probed on loopback: this only needs a port number, and the
# manager's own bind is what has to succeed. If the port turns
# out to be taken on another interface, the retry below covers
# it — same as for the race.
probe = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
probe.bind(("127.0.0.1", 0))
port = probe.getsockname()[1]
probe.close()
if not _broadcast_available(port):
pytest.skip("environment refuses UDP broadcast")
try:
leader = DisplaySyncManager("leader", {"port": port}, hw, MagicMock())
follower = DisplaySyncManager("follower", {"port": port}, hw, MagicMock())
break
except OSError:
# Port taken between probe and bind, or the TCP image
# server could not bind port+1. Tear down whichever end
# came up before retrying with a fresh port.
for mgr in (leader, follower):
if mgr is not None:
mgr.stop()
leader = follower = None
else:
pytest.skip("could not obtain a free port pair for the handshake")
try:
deadline = time.time() + 5.0
while time.time() < deadline:
if (leader._leader_state is LeaderState.CONNECTED
and follower._peer_compatible):
break
time.sleep(0.02)
if (leader._leader_state is LeaderState.NO_PEER
and follower._leader_ip is None):
# Not one packet crossed, in either direction. The sendto
# succeeded — _broadcast_available checked — so this is a
# network that accepts a broadcast and drops it, which no
# up-front probe can detect without binding INADDR_ANY to
# listen for its own datagram. Skip rather than report a
# protocol failure the code did not cause.
#
# This cannot hide a real regression in the announcing
# side: TestFollowerAnnounceLoop covers that on mock
# sockets, where delivery is not a variable.
pytest.skip(
"environment accepted the broadcast but did not deliver it")
assert leader._leader_state is LeaderState.CONNECTED
assert follower._peer_compatible is True
assert follower._leader_ip is not None
finally:
leader.stop()
follower.stop()