Debian Codegraph @

tests/test_bleak_manager.py

raw
1 """Tests for the bleak_manager helpers (DBus socket cache + global manager getter)."""
2
3 from __future__ import annotations
4
5 import asyncio
6 from unittest.mock import AsyncMock
7
8 import pytest
9
10 import bleak_retry_connector
11 from bleak_retry_connector.bleak_manager import (
12 _reset_dbus_socket_cache,
13 get_global_bluez_manager_with_timeout,
14 )
15
16 pytestmark = pytest.mark.asyncio
17
18
19 @pytest.fixture(autouse=True)
20 def _clear_dbus_socket_cache():
21 """Ensure the _has_dbus_socket attribute is reset around every test."""
22 _reset_dbus_socket_cache()
23 yield
24 _reset_dbus_socket_cache()
25
26
27 async def test_returns_none_on_non_linux(mock_macos):
28 """On non-Linux platforms the helper returns None without touching DBus."""
29 assert await get_global_bluez_manager_with_timeout() is None
30
31
32 async def test_returns_cached_manager_when_loop_already_registered(
33 mock_linux, monkeypatch
34 ):
35 """If the loop is already registered in _global_instances, return that manager."""
36 sentinel_manager = object()
37 loop = asyncio.get_running_loop()
38 monkeypatch.setattr(
39 bleak_retry_connector.bleak_manager,
40 "_global_instances",
41 {loop: sentinel_manager},
42 )
43 # If we hit this, get_global_bluez_manager should never be called.
44 mock_get = AsyncMock(side_effect=AssertionError("should not be called"))
45 monkeypatch.setattr(
46 bleak_retry_connector.bleak_manager, "get_global_bluez_manager", mock_get
47 )
48
49 assert await get_global_bluez_manager_with_timeout() is sentinel_manager
50 mock_get.assert_not_called()
51
52
53 async def test_short_circuits_after_filenotfound_is_cached(mock_linux, monkeypatch):
54 """A FileNotFoundError marks the socket missing and skips subsequent calls."""
55 monkeypatch.setattr(bleak_retry_connector.bleak_manager, "_global_instances", {})
56 mock_get = AsyncMock(side_effect=FileNotFoundError(2, "no such file", "/run/dbus"))
57 monkeypatch.setattr(
58 bleak_retry_connector.bleak_manager, "get_global_bluez_manager", mock_get
59 )
60
61 assert await get_global_bluez_manager_with_timeout() is None
62 assert mock_get.call_count == 1
63
64 # Second call must not retry — the cache short-circuits to None.
65 assert await get_global_bluez_manager_with_timeout() is None
66 assert mock_get.call_count == 1
67
68
69 async def test_short_circuits_after_timeout_is_cached(mock_linux, monkeypatch):
70 """An asyncio.TimeoutError also flips the cache to False and skips retries."""
71 monkeypatch.setattr(bleak_retry_connector.bleak_manager, "_global_instances", {})
72 mock_get = AsyncMock(side_effect=asyncio.TimeoutError())
73 monkeypatch.setattr(
74 bleak_retry_connector.bleak_manager, "get_global_bluez_manager", mock_get
75 )
76
77 assert await get_global_bluez_manager_with_timeout() is None
78 assert mock_get.call_count == 1
79
80 # Second call must not retry — the cache short-circuits to None.
81 assert await get_global_bluez_manager_with_timeout() is None
82 assert mock_get.call_count == 1
83
84
85 async def test_generic_exception_returns_none_but_does_not_cache(
86 mock_linux, monkeypatch
87 ):
88 """A generic exception logs and returns None, but does NOT poison the cache."""
89 monkeypatch.setattr(bleak_retry_connector.bleak_manager, "_global_instances", {})
90 mock_get = AsyncMock(side_effect=RuntimeError("boom"))
91 monkeypatch.setattr(
92 bleak_retry_connector.bleak_manager, "get_global_bluez_manager", mock_get
93 )
94
95 assert await get_global_bluez_manager_with_timeout() is None
96 assert mock_get.call_count == 1
97
98 # A generic exception (not FileNotFoundError / TimeoutError) does NOT flip the
99 # _has_dbus_socket cache to False, so a subsequent call still tries again.
100 assert await get_global_bluez_manager_with_timeout() is None
101 assert mock_get.call_count == 2
102
103
104 async def test_returns_manager_on_success(mock_linux, monkeypatch):
105 """When get_global_bluez_manager succeeds, its return value is propagated."""
106 sentinel_manager = object()
107 monkeypatch.setattr(bleak_retry_connector.bleak_manager, "_global_instances", {})
108 mock_get = AsyncMock(return_value=sentinel_manager)
109 monkeypatch.setattr(
110 bleak_retry_connector.bleak_manager, "get_global_bluez_manager", mock_get
111 )
112
113 assert await get_global_bluez_manager_with_timeout() is sentinel_manager
114 mock_get.assert_awaited_once()
115
116
117 async def test_reset_dbus_socket_cache_re_enables_retries(mock_linux, monkeypatch):
118 """_reset_dbus_socket_cache() lets the helper retry after a cached failure."""
119 monkeypatch.setattr(bleak_retry_connector.bleak_manager, "_global_instances", {})
120 sentinel_manager = object()
121 mock_get = AsyncMock(
122 side_effect=[
123 FileNotFoundError(2, "no such file", "/run/dbus"),
124 sentinel_manager,
125 ]
126 )
127 monkeypatch.setattr(
128 bleak_retry_connector.bleak_manager, "get_global_bluez_manager", mock_get
129 )
130
131 # First call: cache flips to False.
132 assert await get_global_bluez_manager_with_timeout() is None
133 # Second call would short-circuit without the reset.
134 assert await get_global_bluez_manager_with_timeout() is None
135 assert mock_get.call_count == 1
136
137 _reset_dbus_socket_cache()
138
139 # After reset, the helper tries again and gets the manager.
140 assert await get_global_bluez_manager_with_timeout() is sentinel_manager
141 assert mock_get.call_count == 2