d10bb2ab6f
`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.
Parallel directories for one source package, folded into the mirror:
tests/acp -> tests/acp_adapter (its __init__/conftest move with it)
tests/cli -> tests/hermes_cli (prompt_toolkit fixture merged into
hermes_cli/conftest.py)
tests/run_agent -> tests/agent (backoff fixture becomes
agent/conftest.py)
tests/relay -> tests/gateway/relay
tests/state -> tests/hermes_state
246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.
Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.
Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).
Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
119 lines
3.9 KiB
Python
119 lines
3.9 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
|
|
|
|
from hermes_cli import web_server
|
|
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)
|
|
def test_topology_cache_misses_when_collector_is_swapped(monkeypatch):
|
|
"""Tests (and hot-reload scenarios) monkeypatch the collector; a swapped
|
|
function identity must be a cache miss so stale data from the previous
|
|
collector never leaks across the swap."""
|
|
calls_a, calls_b = [], []
|
|
monkeypatch.setattr(
|
|
_web_server_gateway, "_collect_profile_gateway_topology", _fake_topology(calls_a)
|
|
)
|
|
_reset_cache()
|
|
try:
|
|
first = _web_server_gateway._collect_profile_gateway_topology_cached()
|
|
monkeypatch.setattr(
|
|
_web_server_gateway, "_collect_profile_gateway_topology", _fake_topology(calls_b)
|
|
)
|
|
second = _web_server_gateway._collect_profile_gateway_topology_cached()
|
|
finally:
|
|
_reset_cache()
|
|
|
|
assert len(calls_a) == 1
|
|
assert len(calls_b) == 1
|
|
assert first is not second
|