| 1 |
import asyncio |
| 2 |
from unittest.mock import AsyncMock, MagicMock, patch |
| 3 |
|
| 4 |
import pytest |
| 5 |
from bleak.backends.bluezdbus import defs |
| 6 |
from bleak.backends.device import BLEDevice |
| 7 |
|
| 8 |
from bleak_retry_connector.dbus import disconnect_devices |
| 9 |
|
| 10 |
pytestmark = pytest.mark.asyncio |
| 11 |
|
| 12 |
|
| 13 |
@pytest.fixture(scope="module", autouse=True) |
| 14 |
def _prime_dbus_coverage() -> None: |
| 15 |
# On Python 3.14, coverage's sysmon backend lazily calls os.stat to read |
| 16 |
# source the first time a branch fires in a tracked file; the autouse |
| 17 |
# blockbuster fixture in conftest then flags that stat as a blocking |
| 18 |
# call. Module-scoped fixtures run before the function-scoped blockbuster, |
| 19 |
# so executing the trivial path here warms the per-file source cache. |
| 20 |
loop = asyncio.new_event_loop() |
| 21 |
try: |
| 22 |
loop.run_until_complete(disconnect_devices([])) |
| 23 |
finally: |
| 24 |
loop.close() |
| 25 |
|
| 26 |
|
| 27 |
def _device(path: str | None = "/org/bluez/hci0/dev_FA_23_9D_AA_45_46") -> BLEDevice: |
| 28 |
details: dict[str, str] = {"source": "aa:bb:cc:dd:ee:ff"} |
| 29 |
if path is not None: |
| 30 |
details["path"] = path |
| 31 |
return BLEDevice("FA:23:9D:AA:45:46", "FA:23:9D:AA:45:46", details) |
| 32 |
|
| 33 |
|
| 34 |
async def test_disconnect_devices_empty_list() -> None: |
| 35 |
"""Empty list returns early without touching the bluez manager.""" |
| 36 |
with patch( |
| 37 |
"bleak_retry_connector.dbus.get_global_bluez_manager_with_timeout", |
| 38 |
new=AsyncMock(), |
| 39 |
) as mock_manager: |
| 40 |
await disconnect_devices([]) |
| 41 |
mock_manager.assert_not_called() |
| 42 |
|
| 43 |
|
| 44 |
async def test_disconnect_devices_filters_invalid_devices() -> None: |
| 45 |
"""Devices without dict details or a 'path' key are skipped.""" |
| 46 |
no_dict_details = BLEDevice("AA:BB", "AA:BB", "not-a-dict") |
| 47 |
no_path = BLEDevice("CC:DD", "CC:DD", {"source": "xx"}) |
| 48 |
with patch( |
| 49 |
"bleak_retry_connector.dbus.get_global_bluez_manager_with_timeout", |
| 50 |
new=AsyncMock(), |
| 51 |
) as mock_manager: |
| 52 |
await disconnect_devices([no_dict_details, no_path]) |
| 53 |
mock_manager.assert_not_called() |
| 54 |
|
| 55 |
|
| 56 |
async def test_disconnect_devices_no_bluez_manager() -> None: |
| 57 |
"""If the bluez manager times out (returns None), do nothing.""" |
| 58 |
with patch( |
| 59 |
"bleak_retry_connector.dbus.get_global_bluez_manager_with_timeout", |
| 60 |
new=AsyncMock(return_value=None), |
| 61 |
): |
| 62 |
await disconnect_devices([_device()]) |
| 63 |
|
| 64 |
|
| 65 |
async def test_disconnect_devices_calls_bus_for_each_valid_device() -> None: |
| 66 |
"""Each valid device gets a BlueZ Disconnect call with the right path.""" |
| 67 |
bus = MagicMock() |
| 68 |
bus.call = AsyncMock() |
| 69 |
bluez_manager = MagicMock() |
| 70 |
bluez_manager._bus = bus |
| 71 |
|
| 72 |
device_a = _device("/org/bluez/hci0/dev_AA") |
| 73 |
device_b = _device("/org/bluez/hci0/dev_BB") |
| 74 |
invalid = BLEDevice("EE:FF", "EE:FF", {"source": "x"}) |
| 75 |
|
| 76 |
fake_message_cls = MagicMock() |
| 77 |
with ( |
| 78 |
patch( |
| 79 |
"bleak_retry_connector.dbus.get_global_bluez_manager_with_timeout", |
| 80 |
new=AsyncMock(return_value=bluez_manager), |
| 81 |
), |
| 82 |
patch("bleak_retry_connector.dbus.Message", new=fake_message_cls), |
| 83 |
): |
| 84 |
await disconnect_devices([device_a, invalid, device_b]) |
| 85 |
|
| 86 |
assert bus.call.await_count == 2 |
| 87 |
paths = [ |
| 88 |
kwargs["path"] if "path" in kwargs else args[0] |
| 89 |
for args, kwargs in [ |
| 90 |
(call.args, call.kwargs) for call in fake_message_cls.call_args_list |
| 91 |
] |
| 92 |
] |
| 93 |
assert paths == ["/org/bluez/hci0/dev_AA", "/org/bluez/hci0/dev_BB"] |
| 94 |
for call in fake_message_cls.call_args_list: |
| 95 |
assert call.kwargs["destination"] == defs.BLUEZ_SERVICE |
| 96 |
assert call.kwargs["interface"] == defs.DEVICE_INTERFACE |
| 97 |
assert call.kwargs["member"] == "Disconnect" |
| 98 |
|
| 99 |
|
| 100 |
async def test_disconnect_devices_suppresses_exceptions() -> None: |
| 101 |
"""An exception from bus.call must not stop the loop.""" |
| 102 |
bus = MagicMock() |
| 103 |
bus.call = AsyncMock(side_effect=RuntimeError("boom")) |
| 104 |
bluez_manager = MagicMock() |
| 105 |
bluez_manager._bus = bus |
| 106 |
|
| 107 |
devices = [_device("/org/bluez/hci0/dev_A"), _device("/org/bluez/hci0/dev_B")] |
| 108 |
|
| 109 |
with ( |
| 110 |
patch( |
| 111 |
"bleak_retry_connector.dbus.get_global_bluez_manager_with_timeout", |
| 112 |
new=AsyncMock(return_value=bluez_manager), |
| 113 |
), |
| 114 |
patch("bleak_retry_connector.dbus.Message", new=MagicMock()), |
| 115 |
): |
| 116 |
await disconnect_devices(devices) |
| 117 |
|
| 118 |
assert bus.call.await_count == 2 |
| 119 |
|
| 120 |
|
| 121 |
async def test_disconnect_devices_suppresses_timeout() -> None: |
| 122 |
"""A timeout from the inner asyncio_timeout context must be suppressed.""" |
| 123 |
import asyncio |
| 124 |
|
| 125 |
async def slow(*args, **kwargs): |
| 126 |
await asyncio.sleep(10) |
| 127 |
|
| 128 |
bus = MagicMock() |
| 129 |
bus.call = AsyncMock(side_effect=slow) |
| 130 |
bluez_manager = MagicMock() |
| 131 |
bluez_manager._bus = bus |
| 132 |
|
| 133 |
with ( |
| 134 |
patch( |
| 135 |
"bleak_retry_connector.dbus.get_global_bluez_manager_with_timeout", |
| 136 |
new=AsyncMock(return_value=bluez_manager), |
| 137 |
), |
| 138 |
patch("bleak_retry_connector.dbus.Message", new=MagicMock()), |
| 139 |
patch("bleak_retry_connector.dbus.DISCONNECT_TIMEOUT", 0.01), |
| 140 |
): |
| 141 |
await disconnect_devices([_device()]) |
| 142 |
|
| 143 |
assert bus.call.await_count == 1 |