Files
Shadowbroker/backend/tests/mesh/test_prekey_lookup_event_loop.py
Clint Bodungen 625b6030ed Run prekey and pubkey fleet lookups off the event loop
dm_get_prekey_bundle and dm_get_pubkey are async but called the
synchronous fetch_dm_prekey_bundle directly. When the lookup handle
isn't local it calls peers over the network, which blocked the event
loop so every endpoint stopped answering until it returned. Run it with
asyncio.to_thread, as main.py does for other blocking calls, and add a
regression test that checks the loop keeps ticking during a slow lookup.
2026-09-29 09:50:52 -05:00

83 lines
2.5 KiB
Python

"""Prekey and pubkey lookups don't block the event loop while they wait on the network."""
from __future__ import annotations
import asyncio
import time
from unittest.mock import patch
import pytest
def _request(path: str):
import main
return main.Request(
{
"type": "http",
"method": "GET",
"path": path,
"headers": [],
"client": ("127.0.0.1", 12345),
}
)
def _slow_lookup(*_args, **_kwargs):
# Stands in for a fleet lookup waiting on a slow or unreachable peer.
time.sleep(1.0)
return {"ok": False, "detail": "Prekey bundle not found"}
async def _longest_stall(endpoint) -> float:
"""Runs the endpoint while a 50 ms ticker runs; returns the longest gap between ticks."""
gaps: list[float] = []
done = asyncio.Event()
async def ticker():
last = time.monotonic()
while not done.is_set():
await asyncio.sleep(0.05)
now = time.monotonic()
gaps.append(now - last)
last = now
task = asyncio.create_task(ticker())
await asyncio.sleep(0)
try:
await endpoint()
finally:
done.set()
await task
return max(gaps)
@pytest.mark.asyncio
async def test_prekey_bundle_lookup_does_not_block_the_event_loop():
import main
request = _request("/api/mesh/dm/prekey-bundle")
with patch("main.fetch_dm_prekey_bundle", side_effect=_slow_lookup):
stall = await _longest_stall(
lambda: main.dm_get_prekey_bundle(request, lookup_token="unknown-handle-token")
)
# A 1 s lookup on the loop stalls it about 1 s; off the loop, a 50 ms tick stays well under 0.5 s.
assert stall < 0.5, f"the event loop stalled for {stall:.2f}s during the lookup"
@pytest.mark.asyncio
async def test_pubkey_fleet_lookup_does_not_block_the_event_loop():
import main
request = _request("/api/mesh/dm/pubkey")
with patch("services.mesh.mesh_dm_relay.dm_relay") as relay, patch(
"services.mesh.mesh_wormhole_prekey.fetch_dm_prekey_bundle",
side_effect=_slow_lookup,
):
relay.get_dh_key_by_lookup.return_value = (None, "")
stall = await _longest_stall(
lambda: main.dm_get_pubkey(request, lookup_token="unknown-handle-token")
)
# A 1 s lookup on the loop stalls it about 1 s; off the loop, a 50 ms tick stays well under 0.5 s.
assert stall < 0.5, f"the event loop stalled for {stall:.2f}s during the lookup"