mirror of
https://github.com/rstrouse/ESPSomfy-RTS.git
synced 2026-10-31 22:02:17 +01:00
Step 4a of docs/SECURITY_PLAN.md. Port 80 keeps the eleven HTTP routes of ADR 0006 and the socket at /ws; everything else moved to protocol 2. - Port 8081 (ApiRoutes) and the socket on 8080 (SocketEmitter) are gone. Protocol 2 owns its frame buffer and loop-task check. - Port 80 drops every route the old web UI called: reads, edits, settings, scans, reboot, /shades.cfg, /discovery. HttpRoutes.cpp holds what stays. - ModelSnapshot, SnapshotSlots and the read documents are gone. The item snapshot types stay, as ItemSnapshot, for events, MQTT and the command replies. - Model edits reply only to their protocol 2 op: requestResult goes. - The settings' toJSON writers, used only by the settings routes, go. - The _espsomfy_rts._tcp mDNS record moves to port 80, with path=/ws. - The memory event reports the loop-rate window it closes. - A deleted room no longer re-publishes its retained MQTT topics. - e2e: the heap check reads the sync's status; the sync is checked on its own instead of against /controller. - README gets an API and ports section. BREAKING CHANGE: ports 8081 and 8080 and the old HTTP routes are gone. Home Assistant needs integration 3.0.0 or newer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
90 lines
3.2 KiB
Python
90 lines
3.2 KiB
Python
"""The device must survive the loads that ran its heap out
|
|
(docs/adr/0005-one-websocket-protocol.md): every client reconnecting at once,
|
|
and a browser loading the page."""
|
|
import threading
|
|
import time
|
|
import urllib.request
|
|
|
|
import pytest
|
|
|
|
from proto_client import ProtoClient, heap, run_sync
|
|
|
|
# What the heap may drop to under load. Below it the next allocation in the
|
|
# web server is close to failing, which aborts.
|
|
HEAP_FLOOR = 20 * 1024
|
|
MAX_CLIENTS = 5
|
|
|
|
|
|
@pytest.fixture
|
|
def watch_heap(host, token):
|
|
"""Fails the test when the device rebooted or its heap fell under the floor.
|
|
|
|
The device reports its lowest heap since it booted, so a dip under the
|
|
floor only shows while that low is still above it."""
|
|
before = heap(host, token)
|
|
if before["min"] < HEAP_FLOOR:
|
|
pytest.skip("the heap has been down to %u bytes since boot: reboot the device to check the floor" % before["min"])
|
|
yield before
|
|
after = heap(host, token)
|
|
assert after["uptime"] > before["uptime"], "the device rebooted"
|
|
assert after["min"] >= HEAP_FLOOR, "the heap fell to %u bytes (floor %u)" % (after["min"], HEAP_FLOOR)
|
|
|
|
|
|
@pytest.fixture
|
|
def all_slots_free(host, token):
|
|
"""Skips unless every socket slot is free. The web UI holds one per open
|
|
tab, which would leave these tests a client short."""
|
|
clients = [ProtoClient(host, params={"apikey": token} if token else None) for _ in range(MAX_CLIENTS)]
|
|
try:
|
|
time.sleep(1)
|
|
taken = sum(1 for c in clients if c.closed.is_set())
|
|
finally:
|
|
for c in clients:
|
|
c.close()
|
|
# The device frees a slot when it sees the disconnect.
|
|
time.sleep(0.5)
|
|
if taken:
|
|
pytest.skip("%u socket slot(s) in use, likely by the web UI: close its tabs" % taken)
|
|
|
|
|
|
def test_every_client_reconnecting_at_once(connect, schema, watch_heap, all_slots_free):
|
|
clients = [connect() for _ in range(MAX_CLIENTS)]
|
|
errors = []
|
|
|
|
def sync(c):
|
|
try:
|
|
run_sync(c, timeout=60)
|
|
except Exception as e: # reported below
|
|
errors.append(e)
|
|
threads = [threading.Thread(target=sync, args=(c,)) for c in clients]
|
|
[t.start() for t in threads]
|
|
[t.join() for t in threads]
|
|
assert not errors, errors
|
|
|
|
|
|
def test_one_client_too_many_is_closed(connect, schema, all_slots_free):
|
|
clients = [connect(schema=schema.hash) for _ in range(MAX_CLIENTS)]
|
|
for c in clients:
|
|
run_sync(c)
|
|
extra = connect(schema=schema.hash)
|
|
assert extra.closed.wait(5), "a sixth client stayed connected"
|
|
|
|
|
|
def test_page_load(host, token, connect, watch_heap):
|
|
"""The static files in parallel, as a browser fetches them, then the socket."""
|
|
files = ["/", "/app.css", "/index.js", "/icon.png"]
|
|
errors = []
|
|
|
|
def get(path):
|
|
try:
|
|
req = urllib.request.Request(f"http://{host}{path}", headers={"Accept-Encoding": "gzip"})
|
|
with urllib.request.urlopen(req, timeout=60) as r:
|
|
r.read()
|
|
except Exception as e: # reported below
|
|
errors.append((path, e))
|
|
threads = [threading.Thread(target=get, args=(p,)) for p in files]
|
|
[t.start() for t in threads]
|
|
c = connect()
|
|
run_sync(c, timeout=60)
|
|
[t.join() for t in threads]
|
|
assert not errors, errors
|