test: connection leak test test_discovery_http_is_closed is shadowed and syntactically invalid
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 75/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 冷清
- 技术栈
- python
- 领域
- testing-qa
调研方向
从 tests/test_discovery.py 开始,比较两个 Discovery 类定义并定位 test_discovery_http_is_closed。使用 pytest 运行聚焦的 Discovery 测试,然后验证修正后的测试能够观察到临时 HTTP 客户端的关闭,且不会出现 shadowing 或 AttributeError。
由索引模型根据 Issue 内容生成。
描述
Environment details
- OS type and version: Linux (gLinux / Debian based)
- Python version:
3.13.12(also affects all supported Python versions3.10+) - pip version:
26.0.1 google-api-python-clientversion:2.197.0(at commit6e471c075039dfef24e28d11658e03d5c949c7c3)
Steps to reproduce
- Look at
tests/test_discovery.pyonmain. Note that there are two separate definitions forclass Discovery(unittest.TestCase)(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. This means the first
Discoveryclass—which contains the test casetest_discovery_http_is_closed—is completely dead code and is never executed bypytest. - Attempting to manually extract and execute this test case crashes immediately because
HttpMock.close()is a regular function, but the test tries to callassert_called_once()on it.
Code example
The shadowed class in tests/test_discovery.py contains this test definition:
class Discovery(unittest.TestCase):
def test_discovery_http_is_closed(self):
http = HttpMock(datafile("malformed.json"), {"status": "200"})
# build() is called without passing the 'http' mock client!
service = build("plus", "v1", credentials=mock.sentinel.credentials)
# Fails: HttpMock.close is a standard method, not a mock object
http.close.assert_called_once()
Historical Context
This bug was introduced in September 2020 by commit 98888dadf ("fix: add method to close httplib2 connections (#1038)").
That commit added test_discovery_http_is_closed by declaring a new class Discovery(unittest.TestCase) block near the top of the file, without realizing that a class Discovery(unittest.TestCase) block was already defined and active near the bottom of the file (which immediately shadowed the new test).
The Proposed Solution
- Delete the duplicate class definition at line 498.
- Restore
test_discovery_http_is_closedinside the activeDiscoveryclass (at line 1566). - Correct the test case to use standard
unittest.mock.patchonhttplib2.Httpto verify that the temporary HTTP client created bybuild()to fetch the discovery document is safely closed after initialization completes, preventing connection/socket leaks.
@mock.patch("httplib2.Http")
def test_discovery_http_is_closed(self, mock_http_class):
mock_http = mock_http_class.return_value
mock_http.request.return_value = (
httplib2.Response({"status": "200"}),
read_datafile("plus.json"),
)
service = build("plus", "v1", static_discovery=False)
mock_http.close.assert_called_once()
Stack trace
If you isolate the legacy test case and run it, it crashes immediately with an AttributeError:
AttributeError: 'function' object has no attribute 'assert_called_once'
- 主要语言
- Python
- 星标
- 8.9k
- 派生
- 2.6k
- 平均合并
- 2 天 2 小时
- 30 天内合并 PR
- 15
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
googleapis/google-api-python-client 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
-
难度 1/5 1 小时以内 新手友好度 92/100
-
难度 4/5 3-5 天 新手友好度 52/100
-
samples type: docs
难度 3/5 1-2 天 新手友好度 52/100
-
YouTube Data API videos.insert returns hidden “Video Uploads per day” 429 despite quota headroom 未关闭priority: p2 type: bug
难度 5/5 一周以上 新手友好度 25/100
googleapis/google-api-python-client#2753 · 5 个 reaction ·
查看 googleapis/google-api-python-client 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
xinnan-tech/xiaozhi-fde-talk#263 ·
-
rules
难度 1/5 1 小时以内 新手友好度 90/100
-
难度 2/5 1-3 小时 新手友好度 70/100
huggingface/Repo2RLEnv#163 · 1 条评论 ·
-
难度 1/5 1 小时以内 新手友好度 95/100
huggingface/sentence-transformers#4074 ·
-
comp/dashboard invalid P3
难度 2/5 1-3 小时 新手友好度 70/100
NousResearch/hermes-agent#121143 ·