Skip to content

Commit 323bdcf

Browse files
authored
Fix unclosed resources in proxy xfail tests (#10868)
1 parent 97eae19 commit 323bdcf

File tree

1 file changed

+51
-41
lines changed

1 file changed

+51
-41
lines changed

tests/test_proxy_functional.py

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -559,15 +559,17 @@ async def test_proxy_https_send_body(
559559
loop: asyncio.AbstractEventLoop,
560560
) -> None:
561561
sess = aiohttp.ClientSession()
562-
proxy = await proxy_test_server()
563-
proxy.return_value = {"status": 200, "body": b"1" * (2**20)}
564-
url = "https://www.google.com.ua/search?q=aiohttp proxy"
562+
try:
563+
proxy = await proxy_test_server()
564+
proxy.return_value = {"status": 200, "body": b"1" * (2**20)}
565+
url = "https://www.google.com.ua/search?q=aiohttp proxy"
565566

566-
async with sess.get(url, proxy=proxy.url) as resp:
567-
body = await resp.read()
568-
await sess.close()
567+
async with sess.get(url, proxy=proxy.url) as resp:
568+
body = await resp.read()
569569

570-
assert body == b"1" * (2**20)
570+
assert body == b"1" * (2**20)
571+
finally:
572+
await sess.close()
571573

572574

573575
@pytest.mark.xfail
@@ -662,19 +664,21 @@ async def test_proxy_https_acquired_cleanup(
662664

663665
conn = aiohttp.TCPConnector()
664666
sess = aiohttp.ClientSession(connector=conn)
665-
proxy = await proxy_test_server()
666-
667-
assert 0 == len(conn._acquired)
667+
try:
668+
proxy = await proxy_test_server()
668669

669-
async def request() -> None:
670-
async with sess.get(url, proxy=proxy.url):
671-
assert 1 == len(conn._acquired)
670+
assert 0 == len(conn._acquired)
672671

673-
await request()
672+
async def request() -> None:
673+
async with sess.get(url, proxy=proxy.url):
674+
assert 1 == len(conn._acquired)
674675

675-
assert 0 == len(conn._acquired)
676+
await request()
676677

677-
await sess.close()
678+
assert 0 == len(conn._acquired)
679+
finally:
680+
await sess.close()
681+
await conn.close()
678682

679683

680684
@pytest.mark.xfail
@@ -686,19 +690,21 @@ async def test_proxy_https_acquired_cleanup_force(
686690

687691
conn = aiohttp.TCPConnector(force_close=True)
688692
sess = aiohttp.ClientSession(connector=conn)
689-
proxy = await proxy_test_server()
693+
try:
694+
proxy = await proxy_test_server()
690695

691-
assert 0 == len(conn._acquired)
696+
assert 0 == len(conn._acquired)
692697

693-
async def request() -> None:
694-
async with sess.get(url, proxy=proxy.url):
695-
assert 1 == len(conn._acquired)
698+
async def request() -> None:
699+
async with sess.get(url, proxy=proxy.url):
700+
assert 1 == len(conn._acquired)
696701

697-
await request()
702+
await request()
698703

699-
assert 0 == len(conn._acquired)
700-
701-
await sess.close()
704+
assert 0 == len(conn._acquired)
705+
finally:
706+
await sess.close()
707+
await conn.close()
702708

703709

704710
@pytest.mark.xfail
@@ -713,26 +719,30 @@ async def test_proxy_https_multi_conn_limit(
713719
sess = aiohttp.ClientSession(connector=conn)
714720
proxy = await proxy_test_server()
715721

716-
current_pid = None
722+
try:
723+
current_pid = None
717724

718-
async def request(pid: int) -> ClientResponse:
719-
# process requests only one by one
720-
nonlocal current_pid
725+
async def request(pid: int) -> ClientResponse:
726+
# process requests only one by one
727+
nonlocal current_pid
721728

722-
async with sess.get(url, proxy=proxy.url) as resp:
723-
current_pid = pid
724-
await asyncio.sleep(0.2)
725-
assert current_pid == pid
729+
async with sess.get(url, proxy=proxy.url) as resp:
730+
current_pid = pid
731+
await asyncio.sleep(0.2)
732+
assert current_pid == pid
726733

727-
return resp
734+
return resp
728735

729-
requests = [request(pid) for pid in range(multi_conn_num)]
730-
responses = await asyncio.gather(*requests)
736+
requests = [request(pid) for pid in range(multi_conn_num)]
737+
responses = await asyncio.gather(*requests, return_exceptions=True)
731738

732-
assert len(responses) == multi_conn_num
733-
assert {resp.status for resp in responses} == {200}
734-
735-
await sess.close()
739+
# Filter out exceptions to count actual responses
740+
actual_responses = [r for r in responses if isinstance(r, ClientResponse)]
741+
assert len(actual_responses) == multi_conn_num
742+
assert {resp.status for resp in actual_responses} == {200}
743+
finally:
744+
await sess.close()
745+
await conn.close()
736746

737747

738748
def _patch_ssl_transport(monkeypatch: pytest.MonkeyPatch) -> None:
@@ -900,7 +910,7 @@ async def test_proxy_from_env_https(
900910
url = "https://aiohttp.io/path"
901911
proxy = await proxy_test_server()
902912
mocker.patch.dict(os.environ, {"https_proxy": str(proxy.url)})
903-
mock.patch("pathlib.Path.is_file", mock_is_file)
913+
mocker.patch("pathlib.Path.is_file", mock_is_file)
904914

905915
await get_request(url=url, trust_env=True)
906916

0 commit comments

Comments
 (0)