Skip to content

Commit 0ba6cf2

Browse files
Fix flaky tests with unclosed warnings (#8391)
1 parent 04b1212 commit 0ba6cf2

File tree

1 file changed

+30
-58
lines changed

1 file changed

+30
-58
lines changed

tests/test_proxy_functional.py

Lines changed: 30 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,6 @@
1717
from aiohttp import web
1818
from aiohttp.client_exceptions import ClientConnectionError
1919

20-
pytestmark = [
21-
pytest.mark.filterwarnings(
22-
"ignore:unclosed <socket.socket fd=.*:ResourceWarning",
23-
),
24-
pytest.mark.filterwarnings(
25-
"ignore:"
26-
"unclosed transport <_SelectorSocketTransport closing fd=.*"
27-
":ResourceWarning",
28-
),
29-
]
30-
31-
3220
ASYNCIO_SUPPORTS_TLS_IN_TLS = sys.version_info >= (3, 11)
3321

3422

@@ -120,16 +108,14 @@ async def test_secure_https_proxy_absolute_path(
120108
conn = aiohttp.TCPConnector()
121109
sess = aiohttp.ClientSession(connector=conn)
122110

123-
response = await sess.get(
111+
async with sess.get(
124112
web_server_endpoint_url,
125113
proxy=secure_proxy_url,
126114
ssl=client_ssl_ctx, # used for both proxy and endpoint connections
127-
)
128-
129-
assert response.status == 200
130-
assert await response.text() == web_server_endpoint_payload
115+
) as response:
116+
assert response.status == 200
117+
assert await response.text() == web_server_endpoint_payload
131118

132-
response.close()
133119
await sess.close()
134120
await conn.close()
135121

@@ -195,7 +181,8 @@ async def test_https_proxy_unsupported_tls_in_tls(
195181
ClientConnectionError,
196182
match=expected_exception_reason,
197183
) as conn_err:
198-
await sess.get(url, proxy=secure_proxy_url, ssl=client_ssl_ctx)
184+
async with sess.get(url, proxy=secure_proxy_url, ssl=client_ssl_ctx):
185+
pass
199186

200187
assert isinstance(conn_err.value.__cause__, TypeError)
201188
assert match_regex(f"^{type_err!s}$", str(conn_err.value.__cause__))
@@ -255,13 +242,11 @@ async def proxy_server():
255242
def get_request(loop: Any):
256243
async def _request(method="GET", *, url, trust_env=False, **kwargs):
257244
connector = aiohttp.TCPConnector(ssl=False)
258-
client = aiohttp.ClientSession(connector=connector, trust_env=trust_env)
259-
try:
260-
resp = await client.request(method, url, **kwargs)
261-
await resp.release()
262-
return resp
263-
finally:
264-
await client.close()
245+
async with aiohttp.ClientSession(
246+
connector=connector, trust_env=trust_env
247+
) as client:
248+
async with client.request(method, url, **kwargs) as resp:
249+
return resp
265250

266251
return _request
267252

@@ -411,11 +396,8 @@ async def test_proxy_http_acquired_cleanup_force(
411396
assert 0 == len(conn._acquired)
412397

413398
async def request():
414-
resp = await sess.get(url, proxy=proxy.url)
415-
416-
assert 1 == len(conn._acquired)
417-
418-
await resp.release()
399+
async with sess.get(url, proxy=proxy.url):
400+
assert 1 == len(conn._acquired)
419401

420402
await request()
421403

@@ -439,13 +421,11 @@ async def request(pid):
439421
# process requests only one by one
440422
nonlocal current_pid
441423

442-
resp = await sess.get(url, proxy=proxy.url)
443-
444-
current_pid = pid
445-
await asyncio.sleep(0.2)
446-
assert current_pid == pid
424+
async with sess.get(url, proxy=proxy.url) as resp:
425+
current_pid = pid
426+
await asyncio.sleep(0.2)
427+
assert current_pid == pid
447428

448-
await resp.release()
449429
return resp
450430

451431
requests = [request(pid) for pid in range(multi_conn_num)]
@@ -498,9 +478,8 @@ async def xtest_proxy_https_send_body(proxy_test_server: Any, loop: Any) -> None
498478
proxy.return_value = {"status": 200, "body": b"1" * (2**20)}
499479
url = "https://www.google.com.ua/search?q=aiohttp proxy"
500480

501-
resp = await sess.get(url, proxy=proxy.url)
502-
body = await resp.read()
503-
await resp.release()
481+
async with sess.get(url, proxy=proxy.url) as resp:
482+
body = await resp.read()
504483
await sess.close()
505484

506485
assert body == b"1" * (2**20)
@@ -598,11 +577,8 @@ async def xtest_proxy_https_acquired_cleanup(proxy_test_server: Any, loop: Any)
598577
assert 0 == len(conn._acquired)
599578

600579
async def request():
601-
resp = await sess.get(url, proxy=proxy.url)
602-
603-
assert 1 == len(conn._acquired)
604-
605-
await resp.release()
580+
async with sess.get(url, proxy=proxy.url):
581+
assert 1 == len(conn._acquired)
606582

607583
await request()
608584

@@ -624,11 +600,8 @@ async def xtest_proxy_https_acquired_cleanup_force(
624600
assert 0 == len(conn._acquired)
625601

626602
async def request():
627-
resp = await sess.get(url, proxy=proxy.url)
628-
629-
assert 1 == len(conn._acquired)
630-
631-
await resp.release()
603+
async with sess.get(url, proxy=proxy.url):
604+
assert 1 == len(conn._acquired)
632605

633606
await request()
634607

@@ -652,13 +625,11 @@ async def request(pid):
652625
# process requests only one by one
653626
nonlocal current_pid
654627

655-
resp = await sess.get(url, proxy=proxy.url)
656-
657-
current_pid = pid
658-
await asyncio.sleep(0.2)
659-
assert current_pid == pid
628+
async with sess.get(url, proxy=proxy.url) as resp:
629+
current_pid = pid
630+
await asyncio.sleep(0.2)
631+
assert current_pid == pid
660632

661-
await resp.release()
662633
return resp
663634

664635
requests = [request(pid) for pid in range(multi_conn_num)]
@@ -872,8 +843,9 @@ async def test_proxy_auth() -> None:
872843
with pytest.raises(
873844
ValueError, match=r"proxy_auth must be None or BasicAuth\(\) tuple"
874845
):
875-
await session.get(
846+
async with session.get(
876847
"http://python.org",
877848
proxy="http://proxy.example.com",
878849
proxy_auth=("user", "pass"),
879-
)
850+
):
851+
pass

0 commit comments

Comments
 (0)