fix: restore shadowed test_discovery_http_is_closed test - #2801
Conversation
- Remove the duplicate `class Discovery(unittest.TestCase)` definition that has shadowed the active class of the same name since 2020, leaving its only test permanently uncollected by the test runner - Re-add `test_discovery_http_is_closed` inside the surviving class, rewritten to patch `httplib2.Http`: the original asserted on `HttpMock.close`, a plain method that would raise AttributeError if the test ever ran - Pass `cache_discovery=False` so the discovery document is always fetched through the mocked client and the close assertion cannot be bypassed by a cache hit; the test fails if `build()` stops closing its temporary client Fixes googleapis#2757
There was a problem hiding this comment.
Code Review
This pull request refactors the test_discovery_http_is_closed test in tests/test_discovery.py. The test was moved to the Discovery test class and updated to use @mock.patch on httplib2.Http to verify that the temporary HTTP client created during service discovery is properly closed, preventing connection leaks. There are no review comments, so I have no feedback to provide.
|
Noting for reviewers: #2758, opened earlier, addresses the same issue (#2757) with a similar approach (remove the shadowed class, re-add the test with
#2758 additionally asserts that the caller-facing http client is not closed, which is a nice extra check. Happy to fold that assertion in here, or to defer to #2758 if maintainers prefer it — flagging the overlap so both are not reviewed in parallel unknowingly. |
Fixes #2757
tests/test_discovery.pyhas contained twoclass Discovery(unittest.TestCase)definitions since September 2020 (#1038). The second definition silently shadows the first, so its only test,test_discovery_http_is_closed, has never been collected — pytest picks up 0 instances of it onmain. The dead test also could not have passed if it ever ran: it callsassert_called_once()onHttpMock.close, a plain method, which raisesAttributeError.Changes
test_discovery_http_is_closedinside the survivingDiscoveryclass, rewritten to patchhttplib2.Httpso the assertion targets a real mock. It verifies thatbuild()closes the temporary http client it creates to fetch the discovery document (discovery.py, thediscovery_http.close()call).cache_discovery=Falsein addition tostatic_discovery=False, so the discovery document is always fetched through the mocked client — a discovery-cache hit cannot bypass the code path under test.One file, +14/−7.
Verification
tests/test_discovery.py: 173 → 174.discovery_http.close()call removed fromgoogleapiclient/discovery.py, the restored test fails; with it in place, the test passes — so it genuinely guards the close behavior.tests/folder on Python 3.14: 339 passed, 1 skipped.black --checkclean;flake8 --select=E9,F63,F7,F82(the set CI enforces) clean.Note: running
tests/test_discovery.pyin isolation shows one pre-existing failure,DiscoveryErrors::test_tests_should_be_run_with_strict_positional_enforcement— that is #2755, reproducible on an unmodifiedmain, and untouched by this PR.