GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: restore and correct test_discovery_http_is_closed (issue #2757) by codeXsidd · Pull Request #2843 · googleapis/google-api-python-client

Conversation

@codeXsidd

@codeXsidd codeXsidd commented Oct 22, 2026

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gemini-code-assist[bot] reacted with eyes emoji
All reactions
  • 👀 1 reaction
codeXsidd and others added 9 commits March 21, 2026 10:12
@codeXsidd
fix(http): retry on HTTP 400 failedPrecondition
806eab9
Resolves googleapis#2723
@codeXsidd
Merge branch 'main' into main
9ec244f
@daniel-sanche
Merge branch 'main' into main
bcbf0bf
@ohmayr @gemini-code-assist
Apply suggestions from code review
f3603a7
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@ohmayr
Update http.py
cc60f41
@ohmayr
Merge branch 'main' into main
cb1607c
@codeXsidd
Merge branch 'googleapis:main' into main
bbd9cd9
@codeXsidd
Merge branch 'main' into main
2b38f3d
@codeXsidd
fix: restore and correct test_discovery_http_is_closed (googleapis#2757)
d4aed74
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[bot]
gemini-code-assist Bot reviewed Oct 22, 2026
View reviewed changes

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

Copy link
Copy Markdown

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread googleapiclient/http.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread googleapiclient/http.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

codeXsidd
codeXsidd commented Oct 22, 2026
View reviewed changes

@codeXsidd codeXsidd left a comment

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

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!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@codeXsidd codeXsidd mentioned this pull request Oct 22, 2026
test: connection leak test test_discovery_http_is_closed is shadowed and syntactically invalid #2757
Open
@codeXsidd

codeXsidd commented Oct 22, 2026

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!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

codeXsidd added 2 commits November 10, 2026 18:32
@codeXsidd
Merge branch 'main' into fix/issue-2757-fix-shadowed-discovery-test
681dc54
@codeXsidd
Merge branch 'main' into fix/issue-2757-fix-shadowed-discovery-test
a8c4efc

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

Reviewers

1 more reviewer
@gemini-code-assist gemini-code-assist[bot] gemini-code-assist[bot] left review comments
Reviewers whose approvals may not affect merge requirements

At least 1 approving review is required to merge this pull request.

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

size: m Pull request size is medium.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

test: connection leak test test_discovery_http_is_closed is shadowed and syntactically invalid

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@codeXsidd @daniel-sanche @ohmayr
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.