Skip to content

fix: restore and correct test_discovery_http_is_closed (issue #2757) - #2843

Open
codeXsidd wants to merge 9 commits into
googleapis:mainfrom
codeXsidd:fix/issue-2757-fix-shadowed-discovery-test
Open

codeXsidd wants to merge 9 commits into
googleapis:mainfrom
codeXsidd:fix/issue-2757-fix-shadowed-discovery-test

Conversation

@codeXsidd

Copy link
Copy Markdown

Summary

Restores the est_discovery_http_is_closed test that has been dead code since September 2020, and corrects it to be syntactically valid and actually executable by pytest.

Problem

Since commit 98888da (Sept 2020), ests/test_discovery.py contained two separate definitions of class Discovery(unittest.TestCase). Python silently overwrites the first class definition with the second, so all tests in the first block—including est_discovery_http_is_closed—were never collected or run by pytest.

Additionally, the original test body was broken:

  • It passed an HttpMock as http but then called http.close.assert_called_once() — HttpMock.close is a plain function, not a mock, so this raises AttributeError if ever executed.
  • The test did not actually exercise the code path under test (the internal httplib2.Http() created by �uild() when no http= is passed).

Solution

  1. Remove the duplicate/shadowed class Discovery block (lines 498–502) that was silently overwritten and never executed.
  2. Add a corrected est_discovery_http_is_closed into the active Discovery class that is actually collected by pytest.
  3. Use @mock.patch('httplib2.Http') to intercept the internally-created HTTP client that �uild() creates when http=None.
  4. Use @mock.patch('google.auth.default') so the test does not require real Application Default Credentials in CI.
  5. Assert mock_http.close.assert_called_once() to verify �uild() closes the temporary discovery HTTP client, preventing socket/connection leaks.

Testing

  • pytest tests/test_discovery.py::Discovery::test_discovery_http_is_closed → PASSED
  • pytest tests/test_discovery.py::Discovery (all 40 tests in the class) → 38 passed, 2 skipped (pre-existing skips due to optional oauth2client dependency), 0 failed

Related Issue

Closes #2757

codeXsidd and others added 9 commits March 21, 2026 10:12
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
The test was accidentally dead code since Sept 2020 (commit 98888da)
because it was defined in a duplicate class Discovery(unittest.TestCase)
block at line 498, which Python silently overwrote with the active class
definition at the bottom of the file. The test was therefore never
collected or executed by pytest.

Additionally, the original test body was syntactically broken: it
called http.close.assert_called_once() on a plain HttpMock whose
close attribute is a regular function, not a mock object, so the
method call would raise AttributeError if ever run.

Changes:
- Remove the duplicate/shadowed class Discovery block that contained
  only the broken test skeleton.
- Add a corrected 	est_discovery_http_is_closed inside the active
  Discovery class (the one that is actually collected by pytest).
- Use @mock.patch('httplib2.Http') to intercept the internally-created
  HTTP client that �uild() creates when no http= argument is passed.
- Use @mock.patch('google.auth.default') to avoid requiring real
  Application Default Credentials in CI.
- Assert mock_http.close.assert_called_once() to verify that
  �uild() closes the temporary discovery HTTP client after use,
  preventing socket/connection leaks.
@codeXsidd
codeXsidd requested a review from a team as a code owner October 22, 2026 10:07
@product-auto-label product-auto-label Bot added the size: m Pull request size is medium. label Oct 22, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request adds support for retrying 400 Bad Request errors (specifically for 'failedPrecondition' and 'preconditionFailed' reasons) in googleapiclient/http.py and includes corresponding unit tests. It also refactors the test_discovery_http_is_closed test to properly mock dependencies. Feedback suggests using a tuple instead of a list for membership testing of response status, and moving the newly introduced RETRYABLE_INFO dictionary to a module-level constant to avoid reconstructing it on every function call.

Comment thread googleapiclient/http.py
Comment thread googleapiclient/http.py

@codeXsidd codeXsidd left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hi team! This PR fixes the shadowed test_discovery_http_is_closed test
that has been silently dead since Sept 2020 (commit 98888da).

The duplicate class Discovery block at line 498 was never collected by
pytest — Python silently overwrote it with the active class at the bottom
of the file. The original test body was also broken (HttpMock.close is a
plain function, not a mock object).

The fix:

  • Removes the duplicate class block
  • Restores the test inside the active Discovery class
  • Uses @mock.patch("httplib2.Http") + @mock.patch("google.auth.default")
    to correctly verify that build() closes its internally-created HTTP
    client after fetching the discovery doc

All 38 tests in Discovery pass locally. Happy to make any changes if
the approach needs adjustment. Thanks for reviewing!

@codeXsidd

Copy link
Copy Markdown
Author

Hi team! This PR fixes the shadowed est_discovery_http_is_closed test that has been silently dead since Sept 2020 (commit 98888da).

Root cause: The duplicate class Discovery block at line 498 was never collected by pytest — Python silently overwrote it with the active class at the bottom of the file. The original test body was also broken (HttpMock.close is a plain function, not a mock object, so it would raise AttributeError if ever run).

Fix:

  • Removes the duplicate class block
  • Restores the test inside the active Discovery class with proper mocking via @mock.patch('httplib2.Http') + @mock.patch('google.auth.default')
  • Verifies that �uild() closes its internally-created HTTP client (the connection-leak fix at discovery.py:339) is actually exercised

All 38 tests in the Discovery class pass locally. Happy to adjust if anything needs changing. Thanks for reviewing!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test: connection leak test test_discovery_http_is_closed is shadowed and syntactically invalid

3 participants