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 — test: restore and fix shadowed test_discovery_http_is_closed by Capstan · Pull Request #2758 · googleapis/google-api-python-client

Conversation

@Capstan

@Capstan Capstan commented May 31, 2026

Copy link
Copy Markdown

This Pull Request deletes a duplicate test class definition in test_discovery.py and 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) in tests/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 Discovery class—which contained the connection lifecycle test test_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 HttpMock object (which does not inherit from mock) but tried to assert http.close.assert_called_once(). If it had ever been executed, it would have crashed instantly with an AttributeError.

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_closed near the top of the file, without realizing that the module already contained an active class 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)

  1. Deleted the duplicate class Discovery definition near the top of test_discovery.py.
  2. Restored the test_discovery_http_is_closed test inside the active Discovery class near the bottom of the file.
  3. Corrected the test logic to use standard unittest.mock.patch on httplib2.Http to verify that the temporary HTTP client created by build() to fetch the discovery document is safely closed after initialization completes, preventing socket/connection leaks.

Verification

Ran the restored test directly under pytest:

pytest tests/test_discovery.py::Discovery::test_discovery_http_is_closed

Result: Passed successfully.

Fixes #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
@Capstan
Capstan requested a review from a team as a code owner May 31, 2026 01:48
@product-auto-label product-auto-label Bot added the size: s Pull request size is small. label May 31, 2026
gemini-code-assist[bot]
gemini-code-assist Bot reviewed May 31, 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 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.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread tests/test_discovery.py Resolved

Uh oh!

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

@Capstan
test: restore and fix shadowed test_discovery_http_is_closed
2575000
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
@Capstan
Capstan force-pushed the fix-shadowed-http-close-test branch from ff803e6 to 2575000 Compare May 31, 2026 01:58
@skippdot skippdot mentioned this pull request Sep 11, 2026
fix: restore shadowed test_discovery_http_is_closed test #2801
Open

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: s Pull request size is small.

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.

1 participant

@Capstan
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.