Skip to content

Commit

Permalink
fix: handle ConnectionRefusedError (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Sep 26, 2022
1 parent 0ef386c commit 7e85613
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/bluetooth_adapters/dbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ async def _get_dbus_managed_objects() -> dict[str, Any]:
"DBus connection broken: %s; try restarting " "`bluetooth` and `dbus`", ex
)
return {}
except ConnectionRefusedError as ex:
if is_docker_env():
_LOGGER.debug(
"DBus connection refused: %s; try restarting "
"`bluetooth`, `dbus`, and finally the docker container",
ex,
)
_LOGGER.debug(
"DBus connection refused: %s; try restarting " "`bluetooth` and `dbus`", ex
)
return {}
msg = Message(
destination="org.bluez",
path="/",
Expand Down
30 changes: 30 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,36 @@ def __init__(self, *args, **kwargs):
assert await get_bluetooth_adapters() == []


@pytest.mark.asyncio
async def test_get_bluetooth_adapters_connection_refused():
"""Test get_bluetooth_adapters with connection refused."""

class MockMessageBus:
def __init__(self, *args, **kwargs):
raise ConnectionRefusedError

with patch("bluetooth_adapters.dbus.MessageBus", MockMessageBus):
assert await get_bluetooth_adapters() == []


@pytest.mark.asyncio
async def test_get_bluetooth_adapters_connect_refused_docker():
class MockMessageBus:
def __init__(self, *args, **kwargs):
pass

async def connect(self):
raise ConnectionRefusedError

async def call(self):
return None

with patch("bluetooth_adapters.dbus.MessageBus", MockMessageBus), patch(
"bluetooth_adapters.dbus.is_docker_env", return_value=True
):
assert await get_bluetooth_adapters() == []


@pytest.mark.asyncio
async def test_get_bluetooth_adapters_connect_fails():
class MockMessageBus:
Expand Down

0 comments on commit 7e85613

Please sign in to comment.