Pin per-branch cleanup contract for bluetooth_device_connect#1761
Conversation
Add test_bluetooth_device_connect_cleanup_contract covering all five outcomes (success, timeout, task-cancelled, external future-cancelled, non-CancelledError BaseException). Each branch asserts the documented unsub call count, _bluetooth_disconnect_no_wait call count, and that no message handlers leak. This pins existing behaviour so any future refactor of the cleanup flow can't silently regress one branch while another still passes.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1761 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 26 26
Lines 4251 4251
=========================================
Hits 4251 4251 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughAdded a comprehensive test for the ChangesBluetooth Device Connect Cleanup Contract Test
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Adds a regression/contract test to pin the per-outcome cleanup behavior of APIClient.bluetooth_device_connect, ensuring future refactors don’t accidentally change unsub()/disconnect behavior or leak message handlers.
Changes:
- Add
test_bluetooth_device_connect_cleanup_contractcovering five outcomes (success, timeout, task-cancelled, externally future-cancelled, non-CancelledErrorBaseException) with assertions onunsuband_bluetooth_disconnect_no_waitcall counts and handler leak checks. - Update
TYPE_CHECKINGimports to includeAwaitablefor the new helper type annotations.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| """Pin the per-branch cleanup contract for ``bluetooth_device_connect``. | ||
|
|
||
| Each outcome (success, timeout, task-cancelled, future-cancelled, | ||
| base-exception) must call ``unsub`` and ``_bluetooth_disconnect_no_wait`` | ||
| exactly the documented number of times and leave no leaked message | ||
| handlers or pending timeout handles. | ||
| """ |
What
Add
test_bluetooth_device_connect_cleanup_contractpinning theper-outcome cleanup contract of
APIClient.bluetooth_device_connectacross all five branches: success, timeout, task-cancelled, external
future-cancelled, and a non-
CancelledErrorBaseException.Why
The cleanup flow today threads three booleans (
connect_ok,timeout_expired,unhandled_exception) through one sharedfinally. Any refactor that simplifies that bookkeeping riskssilently double-
unsub'ing, skipping anunsub, or firing thewrong disconnect path on one branch while the others keep passing.
This test pins each branch's
unsubcount,_bluetooth_disconnect_no_waitcount, and that no message handler leaks, so a regression on any
single outcome fails loudly.
Split out of #1756 so the contract test can land independently of
the refactor it was written to enable.
How
The test runs each outcome through a shared
_run_branchhelperthat wraps
send_message_callback_responseto countunsubinvocations, captures the response future via
loop.create_future,and wraps
_bluetooth_disconnect_no_waitto count calls. Outcomesare triggered by feeding the connect response, sleeping past the
timeout, cancelling the task, cancelling the captured future, or
setting an exception on it. Handler leak is asserted by snapshotting
_message_handlersbefore/after.Test plan
SKIP_CYTHON=1 ./venv/bin/python -m pytest tests/test_client.py -k bluetooth_device_connect -v— all 11 connect tests pass (the new one plus the 10 existing)../venv/bin/ruff check tests/test_client.py && ./venv/bin/ruff format --check tests/test_client.py— clean.Types of changes
Quality Report
Changes: 1 file changed, 188 insertions(+), 1 deletion(-)
Code scan: clean
Tests: failed ([Errno 13] Permission denied: 'pytest')
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline