Skip to content

Commit

Permalink
fix: missing backoff execution with esp32 (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 24, 2022
1 parent 6ea7871 commit 3229424
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/bleak_retry_connector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ async def wait_for_disconnect(device: BLEDevice, min_wait_time: float) -> None:
or not isinstance(device.details, dict)
or "path" not in device.details
):
await asyncio.sleep(min_wait_time)
return
start = time.monotonic() if min_wait_time else 0
try:
Expand Down
81 changes: 48 additions & 33 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,9 @@ async def connect(self, *args, **kwargs):
async def disconnect(self, *args, **kwargs):
pass

with pytest.raises(BleakConnectionError):
with patch(
"bleak_retry_connector.calculate_backoff_time", return_value=0
), pytest.raises(BleakConnectionError):
await establish_connection(FakeBleakClient, MagicMock(), "test")


Expand All @@ -352,7 +354,9 @@ async def connect(self, *args, **kwargs):
async def disconnect(self, *args, **kwargs):
pass

with pytest.raises(BleakNotFoundError):
with patch(
"bleak_retry_connector.calculate_backoff_time", return_value=0
), pytest.raises(BleakNotFoundError):
await establish_connection(FakeBleakClient, MagicMock(), "test")


Expand All @@ -375,7 +379,8 @@ async def connect(self, *args, **kwargs):
async def disconnect(self, *args, **kwargs):
pass

client = await establish_connection(FakeBleakClient, MagicMock(), "test")
with patch("bleak_retry_connector.calculate_backoff_time", return_value=0):
client = await establish_connection(FakeBleakClient, MagicMock(), "test")
assert isinstance(client, FakeBleakClient)
assert attempts == 9

Expand Down Expand Up @@ -416,14 +421,15 @@ async def connect(self, *args, **kwargs):
async def disconnect(self, *args, **kwargs):
pass

try:
await establish_connection(
FakeBleakClient,
BLEDevice("aa:bb:cc:dd:ee:ff", "name", {"path": "/dev/1"}),
"test",
)
except BleakError as e:
exc = e
with patch("bleak_retry_connector.calculate_backoff_time", return_value=0):
try:
await establish_connection(
FakeBleakClient,
BLEDevice("aa:bb:cc:dd:ee:ff", "name", {"path": "/dev/1"}),
"test",
)
except BleakError as e:
exc = e

assert isinstance(exc, BleakAbortedError)
assert str(exc) == (
Expand All @@ -448,14 +454,15 @@ async def connect(self, *args, **kwargs):
async def disconnect(self, *args, **kwargs):
pass

try:
await establish_connection(
FakeBleakClient,
BLEDevice("aa:bb:cc:dd:ee:ff", "name", {"path": "/dev/1"}),
"test",
)
except BleakError as e:
exc = e
with patch("bleak_retry_connector.calculate_backoff_time", return_value=0):
try:
await establish_connection(
FakeBleakClient,
BLEDevice("aa:bb:cc:dd:ee:ff", "name", {"path": "/dev/1"}),
"test",
)
except BleakError as e:
exc = e

assert isinstance(exc, BleakOutOfConnectionSlotsError)
assert str(exc) == (
Expand Down Expand Up @@ -484,14 +491,15 @@ async def connect(self, *args, **kwargs):
async def disconnect(self, *args, **kwargs):
pass

try:
await establish_connection(
FakeBleakClient,
BLEDevice("aa:bb:cc:dd:ee:ff", "name", {"path": "/dev/1"}),
"test",
)
except BleakError as e:
exc = e
with patch("bleak_retry_connector.calculate_backoff_time", return_value=0):
try:
await establish_connection(
FakeBleakClient,
BLEDevice("aa:bb:cc:dd:ee:ff", "name", {"path": "/dev/1"}),
"test",
)
except BleakError as e:
exc = e

assert isinstance(exc, BleakNotFoundError)
assert str(exc) == (
Expand Down Expand Up @@ -547,7 +555,9 @@ async def connect(self, *args, **kwargs):
async def disconnect(self, *args, **kwargs):
pass

with pytest.raises(BleakConnectionError):
with patch(
"bleak_retry_connector.calculate_backoff_time", return_value=0
), pytest.raises(BleakConnectionError):
await establish_connection(FakeBleakClient, MagicMock(), "test")


Expand All @@ -563,7 +573,9 @@ async def connect(self, *args, **kwargs):
async def disconnect(self, *args, **kwargs):
pass

with patch.object(bleak_retry_connector, "BLEAK_SAFETY_TIMEOUT", 0), pytest.raises(
with patch(
"bleak_retry_connector.calculate_backoff_time", return_value=0
), patch.object(bleak_retry_connector, "BLEAK_SAFETY_TIMEOUT", 0), pytest.raises(
BleakNotFoundError
):
await establish_connection(FakeBleakClient, MagicMock(), "test")
Expand Down Expand Up @@ -619,9 +631,10 @@ async def connect(self, *args, **kwargs):
async def disconnect(self, *args, **kwargs):
pass

client = await establish_connection(
FakeBleakClient, ble_device_1, "test", ble_device_callback=_get_ble_device
)
with patch("bleak_retry_connector.calculate_backoff_time", return_value=0):
client = await establish_connection(
FakeBleakClient, ble_device_1, "test", ble_device_callback=_get_ble_device
)
assert isinstance(client, FakeBleakClient)
assert attempts == 3

Expand Down Expand Up @@ -887,7 +900,9 @@ def __init__(self):
)
bleak_retry_connector.defs = defs

with patch.object(bleak_retry_connector, "IS_LINUX", True):
with patch(
"bleak_retry_connector.calculate_backoff_time", return_value=0
), patch.object(bleak_retry_connector, "IS_LINUX", True):
client = await establish_connection(
FakeBleakClientWithServiceCache,
BLEDevice(
Expand Down

0 comments on commit 3229424

Please sign in to comment.