Skip to content

tests: centralize transport and session cleanup in conftest#1683

Open
ZeliardM wants to merge 1 commit intopython-kasa:masterfrom
ZeliardM:tests/transport-session-cleanup
Open

tests: centralize transport and session cleanup in conftest#1683
ZeliardM wants to merge 1 commit intopython-kasa:masterfrom
ZeliardM:tests/transport-session-cleanup

Conversation

@ZeliardM
Copy link
Copy Markdown
Contributor

@ZeliardM ZeliardM commented Apr 6, 2026

Summary

Add a centralized autouse fixture in tests/conftest.py that automatically tracks and cleans up all BaseTransport, HttpClient, and aiohttp.ClientSession instances created during tests. This prevents resource leaks (unclosed sessions/transports) from polluting test output with warnings.

Changes

tests/conftest.py — New _close_transport_and_http_sessions autouse fixture:

  • Monkeypatches BaseTransport.__init__, HttpClient.__init__, and aiohttp.ClientSession.__init__ to track all created instances
  • After each test, closes all tracked transports, HTTP clients, and aiohttp sessions
  • Covers all 6 transport types via the single BaseTransport patch point (all call super().__init__())
  • Catches leaks from tests that create sessions without explicit cleanup (9 raw aiohttp.ClientSession() calls across test_deviceconfig.py, test_discovery.py, test_device_factory.py, and transport tests)

tests/transports/test_klaptransport.py — Remove dead code:

  • Removed protocol._transport.http_client = aiohttp.ClientSession() in test_handshake — this set a nonexistent attribute (the transport uses _http_client, not http_client), creating a dangling session that was never used

Files changed (2)

tests/conftest.py, tests/transports/test_klaptransport.py

Merge order

This is PR 4 of 9 in the test modernization series. Must merge before #1684 and #1688 (which modify conflicting lines in the same files).

Order PR Scope
1 #1677 Tests: cleanup and fixes
2 #1681 CI: pin GitHub Actions to SHA
3 #1682 Docs: modernize docstrings
4 #1683 Tests: centralize session cleanup
5 #1684 Tests: transport type annotations
6 #1685 Tests: IoT type annotations
7 #1686 Tests: Smart type annotations
8 #1687 Tests: CLI/protocols/smartcam type annotations
9 #1688 Tests: top-level type annotations

Merge note: #1684 touches test_klaptransport.py in the same area (splits protocol creation into a local transport variable). Merging #1683 first avoids a trivial context conflict — take #1684's version which completely supersedes the affected lines.

Verification

  • 3,581 tests pass across all affected test files
  • All pre-commit hooks pass
  • Full test suite passes after sequential merge of all 9 PRs (10,656 passed, 194 skipped)

Copilot AI review requested due to automatic review settings April 6, 2026 16:50
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.23%. Comparing base (76d9f68) to head (22121ef).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1683      +/-   ##
==========================================
+ Coverage   93.22%   93.23%   +0.01%     
==========================================
  Files         157      157              
  Lines        9815     9815              
  Branches     1003     1003              
==========================================
+ Hits         9150     9151       +1     
+ Misses        472      471       -1     
  Partials      193      193              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the test suite’s resource hygiene by adding automatic cleanup of transport- and client-owned aiohttp sessions, preventing ResourceWarning: unclosed ... warnings and reducing the chance of session leaks across async tests.

Changes:

  • Added per-module autouse fixtures to track created transport instances and close them during teardown.
  • Added an autouse fixture in tests/test_httpclient.py to close any HttpClient instances created by tests.
  • Replaced several unclosed aiohttp.ClientSession() usages in tests/test_deviceconfig.py with a shared fixture that ensures sessions are closed.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/transports/test_ssltransport.py Add autouse fixture to track/close SslTransport instances created in tests.
tests/transports/test_sslaestransport.py Add autouse fixture to track/close SslAesTransport instances created in tests.
tests/transports/test_linkietransport.py Add autouse fixture to track/close LinkieTransportV2 instances created in tests.
tests/transports/test_klaptransport.py Add autouse fixture to track/close AesTransport/KlapTransport/KlapTransportV2; remove an unclosed bare session injection; explicitly close protocol in test_query.
tests/transports/test_aestransport.py Add autouse fixture to track/close AesTransport instances created in tests.
tests/test_httpclient.py Add autouse fixture to track/close HttpClient instances created in tests.
tests/test_deviceconfig.py Introduce http_client fixture to ensure aiohttp.ClientSession is closed; wire it into affected async tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ZeliardM ZeliardM force-pushed the tests/transport-session-cleanup branch 2 times, most recently from 0b37ca9 to 53fcd98 Compare April 6, 2026 19:16
@ZeliardM ZeliardM changed the title tests: add autouse cleanup fixtures for transport and session leaks tests: centralize transport and session cleanup in conftest Apr 6, 2026
@ZeliardM
Copy link
Copy Markdown
Contributor Author

ZeliardM commented Apr 6, 2026

@rytilahti Ok, here is a much better handling for the tests and the httpclient.py sessions and clients.

Add a single autouse fixture in tests/conftest.py that tracks all
BaseTransport and HttpClient instances via __init__ monkeypatching,
then closes them after each test.

Uses functools.wraps to preserve original __init__ signatures so
existing inspect-based tests continue to pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants