Skip to content

Commit

Permalink
fix: add a timeout in case dbus fails to respond (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Aug 1, 2022
1 parent 3c7e533 commit eff1022
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 81 deletions.
124 changes: 44 additions & 80 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Sphinx = {version = "^5.0", optional = true}
sphinx-rtd-theme = {version = "^1.0", optional = true}
myst-parser = {version = "^0.18", optional = true}
dbus-next = ">=0.2.3"
async-timeout = ">=4.0.2"

[tool.poetry.extras]
docs = [
Expand Down
11 changes: 10 additions & 1 deletion src/bluetooth_adapters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
__version__ = "0.1.2"

import asyncio
import logging

import async_timeout
from dbus_next import BusType, Message, MessageType
from dbus_next.aio import MessageBus

__all__ = ["get_bluetooth_adapters"]

_LOGGER = logging.getLogger(__name__)

REPLY_TIMEOUT = 8


async def get_bluetooth_adapters() -> list[str]:
"""Return a list of bluetooth adapters."""
Expand All @@ -26,7 +30,12 @@ async def get_bluetooth_adapters() -> list[str]:
interface="org.freedesktop.DBus.ObjectManager",
member="GetManagedObjects",
)
reply = await bus.call(msg)
try:
async with async_timeout.timeout(REPLY_TIMEOUT):
reply = await bus.call(msg)
except asyncio.TimeoutError:
_LOGGER.debug("D-bus timeout waiting for reply to GetManagedObjects")
return adapters
bus.disconnect()
if not reply or reply.message_type != MessageType.METHOD_RETURN:
_LOGGER.debug("Unexpected replay: %s", reply)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import asyncio
from typing import Any
from unittest.mock import AsyncMock, MagicMock, patch

import pytest
from dbus_next import MessageType

import bluetooth_adapters
from bluetooth_adapters import get_bluetooth_adapters


Expand Down Expand Up @@ -50,6 +53,27 @@ async def call(self):
assert await get_bluetooth_adapters() == []


@pytest.mark.asyncio
async def test_get_bluetooth_adapters_times_out():
async def _stall(*args: Any) -> None:
await asyncio.sleep(10)

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

async def connect(self):
return AsyncMock(
disconnect=MagicMock(),
call=AsyncMock(side_effect=_stall),
)

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


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

0 comments on commit eff1022

Please sign in to comment.