blitz_api/tests/test_hw_gatherer_resilience.py
fusion44 5089925c88
feat(api): serve realtime updates over /ws WebSocket
Replace the /sse/subscribe endpoint with a /ws WebSocket endpoint backed
by ws_mgr, and port the warmup helper (_send_sse_event -> _send_ws_event,
SSE -> Event, broadcast_sse_msg -> broadcast_msg). Also fixes two tests
left over from the prior SSE->Event/broadcast_msg rename that still
referenced the old names and were failing collection/execution.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-12 18:41:18 +02:00

44 lines
1.3 KiB
Python

"""
The hardware-info gatherer must survive a failing get_hardware_info.
_handle_gather_hardware_info had no error handling, so a single exception
from get_hardware_info killed the task permanently ("Task exception was
never retrieved") and hardware SSE updates stopped until restart.
"""
import asyncio
import pytest
from app.system import service
async def test_gatherer_survives_get_hardware_info_error(monkeypatch):
calls = {"n": 0}
async def flaky_hardware_info():
calls["n"] += 1
if calls["n"] == 1:
raise ValueError("bad redis value")
return {"ok": True}
sent = []
async def fake_broadcast(event, data):
sent.append(data)
# break the infinite loop after the second iteration
async def fake_sleep(_seconds):
if calls["n"] >= 2:
raise asyncio.CancelledError()
monkeypatch.setattr(service, "get_hardware_info", flaky_hardware_info)
monkeypatch.setattr(service, "broadcast_msg", fake_broadcast)
monkeypatch.setattr(service.asyncio, "sleep", fake_sleep)
with pytest.raises(asyncio.CancelledError):
await service._handle_gather_hardware_info()
# first iteration errored but did not kill the loop; second succeeded
assert calls["n"] == 2
assert sent == [{"ok": True}]