Files
hermes-agent/tests/hermes_cli/test_web_server_status_topology_cache.py
teknium1 8f6109e493 test: purge low-value tests, lane py14 (360 removed)
Change-detectors, tautologies, source-reading tests, redundant duplicates,
mock-echo tests and dead/unrunnable tests. Per-test rationale in the lane
ledger (category + reason for every removal).
2026-09-23 03:15:26 -07:00

97 lines
3.0 KiB
Python

"""Regression tests for the /api/status profile-topology cache.
The desktop app polls /api/status ~1/s while waiting for the backend to become
ready. Before the cache, every poll ran a full _collect_profile_gateway_topology
scan (per-profile yaml.safe_load with the pure-Python loader + psutil
process-table probes + realpath walks) in the default executor; on multi-profile
installs the concurrent scans held the GIL for 14-16s and starved the event
loop, so the desktop WS never received gateway.ready and boot escalated to the
"Hermes couldn't start" overlay (#60800).
"""
import threading
import time
import hermes_cli.web_server_gateway as _web_server_gateway
def _reset_cache():
_web_server_gateway._TOPOLOGY_CACHE["ts"] = 0.0
_web_server_gateway._TOPOLOGY_CACHE["data"] = None
_web_server_gateway._TOPOLOGY_CACHE["fn"] = None
def _fake_topology(calls, delay=0.0):
def _collect():
if delay:
time.sleep(delay)
calls.append(1)
return {"profiles": ["default"], "gateway_mode": "single", "gateways": []}
return _collect
def test_topology_cache_returns_cached_result_within_ttl(monkeypatch):
calls = []
monkeypatch.setattr(
_web_server_gateway, "_collect_profile_gateway_topology", _fake_topology(calls)
)
_reset_cache()
try:
first = _web_server_gateway._collect_profile_gateway_topology_cached()
second = _web_server_gateway._collect_profile_gateway_topology_cached()
finally:
_reset_cache()
assert len(calls) == 1
assert first is second
def test_topology_cache_rescans_after_ttl(monkeypatch):
calls = []
monkeypatch.setattr(
_web_server_gateway, "_collect_profile_gateway_topology", _fake_topology(calls)
)
_reset_cache()
try:
_web_server_gateway._collect_profile_gateway_topology_cached()
# Age the cache entry past the TTL instead of sleeping through it.
_web_server_gateway._TOPOLOGY_CACHE["ts"] -= _web_server_gateway._TOPOLOGY_CACHE_TTL + 1.0
_web_server_gateway._collect_profile_gateway_topology_cached()
finally:
_reset_cache()
assert len(calls) == 2
def test_topology_cache_collapses_concurrent_scans(monkeypatch):
"""Concurrent status polls must not each run their own scan — that pile-up
is exactly the GIL storm the cache exists to prevent."""
calls = []
monkeypatch.setattr(
_web_server_gateway,
"_collect_profile_gateway_topology",
_fake_topology(calls, delay=0.05),
)
_reset_cache()
results = []
try:
threads = [
threading.Thread(
target=lambda: results.append(
_web_server_gateway._collect_profile_gateway_topology_cached()
)
)
for _ in range(8)
]
for t in threads:
t.start()
for t in threads:
t.join()
finally:
_reset_cache()
assert len(calls) == 1
assert len(results) == 8
assert all(r == results[0] for r in results)