A client that calls bladerf_connect and never disconnects kept the USB claim until the server exited, blocking every other tool on the shared bladeRF. A watchdog thread now releases the device after MCBLADERF_IDLE_TIMEOUT seconds without a tool call (default 600, 0 disables). Middleware counts tool calls in flight, so a release never lands mid-call, and it also skips while a capture runs. The RX/TX settings are saved first; the next device call reconnects and restores them, sample rate included, since the radio resets that to 4 MHz on open. bladerf_status reports auto_released in between. Version 2026.11.15.
121 lines
3.5 KiB
Python
121 lines
3.5 KiB
Python
import asyncio
|
|
|
|
import pytest
|
|
from fastmcp import Client
|
|
|
|
from mcbladerf import device
|
|
from mcbladerf.device import RFConfig
|
|
|
|
|
|
class _FakeDevice:
|
|
pass
|
|
|
|
|
|
@pytest.fixture
|
|
def state(monkeypatch):
|
|
s = device.BladeRFState()
|
|
monkeypatch.setattr(device, "_state", s)
|
|
cfg = RFConfig(915e6, 30e6, 28e6, {"lna": 3.0})
|
|
monkeypatch.setattr(device, "get_rf_config", lambda d, channel=0: cfg)
|
|
restored = []
|
|
monkeypatch.setattr(device, "_restore_rf_config", lambda d, c: restored.append((d, c)))
|
|
s.restored = restored
|
|
|
|
def fake_connect(serial=None, device_index=None):
|
|
device._clear_auto_release(s)
|
|
s.device = _FakeDevice()
|
|
s.serial = serial
|
|
return None
|
|
|
|
monkeypatch.setattr(device, "connect", fake_connect)
|
|
s.device = _FakeDevice()
|
|
s.serial = "abc123"
|
|
return s
|
|
|
|
|
|
def test_releases_only_after_timeout(state):
|
|
t = state.last_activity
|
|
assert not device.release_if_idle(600, now=t + 599)
|
|
assert device.is_connected()
|
|
|
|
assert device.release_if_idle(600, now=t + 601)
|
|
assert not device.is_connected()
|
|
assert state.auto_released_serial == "abc123"
|
|
assert set(state.auto_released_config) == {"rx", "tx"}
|
|
|
|
|
|
def test_never_releases_mid_call(state):
|
|
device.tool_started()
|
|
assert not device.release_if_idle(0, now=state.last_activity + 1e6)
|
|
device.tool_finished()
|
|
assert state.in_flight == 0
|
|
assert device.release_if_idle(0, now=state.last_activity + 1)
|
|
|
|
|
|
def test_never_releases_during_capture(state):
|
|
state.capture_info["active"] = True
|
|
assert not device.release_if_idle(0, now=state.last_activity + 1e6)
|
|
assert device.is_connected()
|
|
|
|
|
|
def test_next_device_call_reconnects_and_restores(state):
|
|
device.release_if_idle(0, now=state.last_activity + 1)
|
|
|
|
device.ensure_connected()
|
|
|
|
assert device.is_connected()
|
|
assert state.serial == "abc123"
|
|
assert [d for d, _ in state.restored] == ["rx", "tx"]
|
|
assert state.auto_released_serial is None
|
|
|
|
|
|
def test_failed_reconnect_explains_and_can_retry(state, monkeypatch):
|
|
device.release_if_idle(0, now=state.last_activity + 1)
|
|
|
|
def busy(serial=None, device_index=None):
|
|
device._clear_auto_release(state)
|
|
raise RuntimeError("Failed to connect to BladeRF: No device(s) available")
|
|
|
|
monkeypatch.setattr(device, "connect", busy)
|
|
with pytest.raises(RuntimeError, match="another process is probably using it"):
|
|
device.ensure_connected()
|
|
assert state.auto_released_serial == "abc123"
|
|
|
|
|
|
def test_explicit_disconnect_forgets_auto_release(state):
|
|
device.release_if_idle(0, now=state.last_activity + 1)
|
|
device.disconnect()
|
|
with pytest.raises(RuntimeError, match="Use bladerf_connect first"):
|
|
device.ensure_connected()
|
|
|
|
|
|
def test_middleware_tracks_tool_calls(monkeypatch):
|
|
from mcbladerf.server import mcp
|
|
|
|
s = device.BladeRFState()
|
|
monkeypatch.setattr(device, "_state", s)
|
|
before = s.last_activity
|
|
|
|
async def go():
|
|
async with Client(mcp) as c:
|
|
await c.call_tool("bladerf_stream_status", {})
|
|
|
|
asyncio.run(go())
|
|
assert s.in_flight == 0
|
|
assert s.last_activity > before
|
|
|
|
|
|
def test_status_reports_auto_release(state):
|
|
from mcbladerf import server
|
|
|
|
device.release_if_idle(0, now=state.last_activity + 1)
|
|
st = asyncio.run(_call(server.mcp, "bladerf_status"))
|
|
assert st["auto_released"] is True
|
|
assert st["serial"] == "abc123"
|
|
|
|
|
|
async def _call(mcp, name):
|
|
async with Client(mcp) as c:
|
|
r = await c.call_tool(name, {})
|
|
return r.structured_content.get("result", r.structured_content)
|