test: restore and fix shadowed test_discovery_http_is_closed#2758
Open
Capstan wants to merge 1 commit into
Open
test: restore and fix shadowed test_discovery_http_is_closed#2758Capstan wants to merge 1 commit into
Capstan wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request relocates and updates the test_discovery_http_is_closed test in tests/test_discovery.py to mock httplib2.Http using unittest.mock. However, the current test implementation uses a single mock instance for multiple instantiations of httplib2.Http during build(), which can lead to false positives. It is recommended to use side_effect to return distinct mock instances for each instantiation to ensure the temporary discovery client is closed and the service client is not closed prematurely.
During a cleanup of duplicate 'class Discovery(unittest.TestCase)' definitions in tests/test_discovery.py, we discovered that one of the classes was completely dead code because Python silently overwrites classes with identical names in the same module. This shadowed class contained a connection lifecycle test: 'test_discovery_http_is_closed'. This commit deletes the duplicate class and restores the 'test_discovery_http_is_closed' test inside the active Discovery class. The test case has been corrected to use standard 'mock.patch' on 'httplib2.Http' to successfully assert that build() closes the temporary HTTP client used during dynamic discovery initialization, preventing socket leakage. #jetski AI_USAGE=true
ff803e6 to
2575000
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This Pull Request deletes a duplicate test class definition in
test_discovery.pyand restores/fixes a shadowed, previously inactive unit test for HTTP connection lifecycle checks.The Bug (Problem)
As reported in #2757, there were two separate definitions for
class Discovery(unittest.TestCase)intests/test_discovery.py(one at line 498 and one at line 1566).In Python, defining a class twice in the same module causes the second definition to silently overwrite the first. Because of this, the first
Discoveryclass—which contained the connection lifecycle testtest_discovery_http_is_closed—was completely dead code and was never executed by the test runner.Furthermore, the original test case was syntactically invalid: it instantiated a regular
HttpMockobject (which does not inherit from mock) but tried to asserthttp.close.assert_called_once(). If it had ever been executed, it would have crashed instantly with anAttributeError.Historical Context
This shadowing bug was introduced in September 2020 in commit 98888da ("fix: add method to close httplib2 connections (#1038)"). That commit added
test_discovery_http_is_closednear the top of the file, without realizing that the module already contained an activeclass Discovery(unittest.TestCase)block near the bottom of the file (which immediately shadowed the new test).As a result, this important connection-lifecycle test has been silently bypassed and inactive in the test suite for over 5 years.
The Fix (Solution)
class Discoverydefinition near the top oftest_discovery.py.test_discovery_http_is_closedtest inside the activeDiscoveryclass near the bottom of the file.unittest.mock.patchonhttplib2.Httpto verify that the temporary HTTP client created bybuild()to fetch the discovery document is safely closed after initialization completes, preventing socket/connection leaks.Verification
Ran the restored test directly under
pytest:Result: Passed successfully.
Fixes #2757