Skip to content

Commit

Permalink
Fix assertion raised in _sched_connect_or_signal_run_fut()
Browse files Browse the repository at this point in the history
When `_sched_connect_or_signal_run_fut()` was called by `_stop()` while a
connection attempt is in progress, an assertion would be raised because
`_connect_timer` was not reset to None by `_stop()`.

Fixes #53
  • Loading branch information
jfroy committed Jun 6, 2023
1 parent d764c3d commit 6919a0f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions aiobafi6/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ def _stop(self) -> None:
# `_sched_connect_or_signal_run_fut` directly because nothing else will.
elif self._connect_timer is not None:
self._connect_timer.cancel()
self._connect_timer = None
self._sched_connect_or_signal_run_fut()

def _finish_run(self, _: asyncio.Future) -> None:
Expand Down
30 changes: 30 additions & 0 deletions aiobafi6/device_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

"""Tests for device."""

from __future__ import annotations

import asyncio
import typing as t

import pytest

from .device import Device
Expand Down Expand Up @@ -124,3 +129,28 @@ def callback(_: Device):
d._process_message(buf)
assert d._properties.current_rpm == 42
assert called


@pytest.mark.asyncio
async def test_cancel_between_connect_attempt():
d = Device(
Service(("127.0.0.1",), PORT),
ignore_volatile_props=False,
delay_between_connects_seconds=1,
)
fut = d.async_run()
except_context: t.Optional[dict[str, t.Any]] = None

def exception_handler(_: t.Any, context: dict[str, t.Any]) -> None:
nonlocal except_context
except_context = context

def cancel_fut():
fut.cancel()

loop = fut.get_loop()
loop.set_exception_handler(exception_handler)
loop.call_later(1.5, cancel_fut)
with pytest.raises(asyncio.CancelledError):
await fut
assert except_context is None

0 comments on commit 6919a0f

Please sign in to comment.