mirror of
https://github.com/rstrouse/ESPSomfy-RTS.git
synced 2026-10-31 13:52:15 +01:00
A pytest suite run by hand against a device (test/e2e/README.md): hello, the schema and its hash, the sync with next and resync, replies to bad frames, feed subscriptions, the status event, every client syncing at once, a sixth client refused, a page load, and the socket under the device's security mode. It never transmits, changes settings or reboots. --record saves hello, the sync and the schema as fixtures. PROTOCOL.md now describes the protocol as built. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
68 lines
2.3 KiB
Python
68 lines
2.3 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 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):
|
|
"""Fails the test when the device rebooted or its heap fell under the floor."""
|
|
before = heap(host)
|
|
yield before
|
|
after = heap(host)
|
|
assert after["uptime"] > before["uptime"], "the device rebooted"
|
|
assert after["min"] >= HEAP_FLOOR or after["min"] == before["min"], (
|
|
"the heap fell to %u bytes (floor %u)" % (after["min"], HEAP_FLOOR))
|
|
|
|
|
|
def test_every_client_reconnecting_at_once(connect, schema, watch_heap):
|
|
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):
|
|
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 = ["/", "/main.css", "/widgets.css", "/icons.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
|