Skip to content

Commit b84417d

Browse files
authored
[PR #10868/323bdcf backport][3.11] Fix unclosed resources in proxy xfail tests (#10869)
1 parent 12c6a78 commit b84417d

File tree

1 file changed

+62
-48
lines changed

1 file changed

+62
-48
lines changed

tests/test_proxy_functional.py

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import ssl
66
import sys
77
from re import match as match_regex
8+
from typing import Awaitable, Callable
89
from unittest import mock
910
from uuid import uuid4
1011

@@ -13,7 +14,7 @@
1314
from yarl import URL
1415

1516
import aiohttp
16-
from aiohttp import web
17+
from aiohttp import ClientResponse, web
1718
from aiohttp.client_exceptions import ClientConnectionError
1819
from aiohttp.helpers import IS_MACOS, IS_WINDOWS
1920

@@ -498,17 +499,22 @@ async def xtest_proxy_https_connect_with_port(proxy_test_server, get_request):
498499

499500

500501
@pytest.mark.xfail
501-
async def xtest_proxy_https_send_body(proxy_test_server, loop):
502-
sess = aiohttp.ClientSession(loop=loop)
503-
proxy = await proxy_test_server()
504-
proxy.return_value = {"status": 200, "body": b"1" * (2**20)}
505-
url = "https://www.google.com.ua/search?q=aiohttp proxy"
502+
async def xtest_proxy_https_send_body(
503+
proxy_test_server: Callable[[], Awaitable[mock.Mock]],
504+
loop: asyncio.AbstractEventLoop,
505+
) -> None:
506+
sess = aiohttp.ClientSession()
507+
try:
508+
proxy = await proxy_test_server()
509+
proxy.return_value = {"status": 200, "body": b"1" * (2**20)}
510+
url = "https://www.google.com.ua/search?q=aiohttp proxy"
506511

507-
async with sess.get(url, proxy=proxy.url) as resp:
508-
body = await resp.read()
509-
await sess.close()
512+
async with sess.get(url, proxy=proxy.url) as resp:
513+
body = await resp.read()
510514

511-
assert body == b"1" * (2**20)
515+
assert body == b"1" * (2**20)
516+
finally:
517+
await sess.close()
512518

513519

514520
@pytest.mark.xfail
@@ -592,42 +598,46 @@ async def xtest_proxy_https_auth(proxy_test_server, get_request):
592598
async def xtest_proxy_https_acquired_cleanup(proxy_test_server, loop):
593599
url = "https://secure.aiohttp.io/path"
594600

595-
conn = aiohttp.TCPConnector(loop=loop)
596-
sess = aiohttp.ClientSession(connector=conn, loop=loop)
597-
proxy = await proxy_test_server()
598-
599-
assert 0 == len(conn._acquired)
601+
conn = aiohttp.TCPConnector()
602+
sess = aiohttp.ClientSession(connector=conn)
603+
try:
604+
proxy = await proxy_test_server()
600605

601-
async def request():
602-
async with sess.get(url, proxy=proxy.url):
603-
assert 1 == len(conn._acquired)
606+
assert 0 == len(conn._acquired)
604607

605-
await request()
608+
async def request() -> None:
609+
async with sess.get(url, proxy=proxy.url):
610+
assert 1 == len(conn._acquired)
606611

607-
assert 0 == len(conn._acquired)
612+
await request()
608613

609-
await sess.close()
614+
assert 0 == len(conn._acquired)
615+
finally:
616+
await sess.close()
617+
await conn.close()
610618

611619

612620
@pytest.mark.xfail
613621
async def xtest_proxy_https_acquired_cleanup_force(proxy_test_server, loop):
614622
url = "https://secure.aiohttp.io/path"
615623

616-
conn = aiohttp.TCPConnector(force_close=True, loop=loop)
617-
sess = aiohttp.ClientSession(connector=conn, loop=loop)
618-
proxy = await proxy_test_server()
619-
620-
assert 0 == len(conn._acquired)
624+
conn = aiohttp.TCPConnector(force_close=True)
625+
sess = aiohttp.ClientSession(connector=conn)
626+
try:
627+
proxy = await proxy_test_server()
621628

622-
async def request():
623-
async with sess.get(url, proxy=proxy.url):
624-
assert 1 == len(conn._acquired)
629+
assert 0 == len(conn._acquired)
625630

626-
await request()
631+
async def request() -> None:
632+
async with sess.get(url, proxy=proxy.url):
633+
assert 1 == len(conn._acquired)
627634

628-
assert 0 == len(conn._acquired)
635+
await request()
629636

630-
await sess.close()
637+
assert 0 == len(conn._acquired)
638+
finally:
639+
await sess.close()
640+
await conn.close()
631641

632642

633643
@pytest.mark.xfail
@@ -639,26 +649,30 @@ async def xtest_proxy_https_multi_conn_limit(proxy_test_server, loop):
639649
sess = aiohttp.ClientSession(connector=conn, loop=loop)
640650
proxy = await proxy_test_server()
641651

642-
current_pid = None
652+
try:
653+
current_pid = None
643654

644-
async def request(pid):
645-
# process requests only one by one
646-
nonlocal current_pid
655+
async def request(pid: int) -> ClientResponse:
656+
# process requests only one by one
657+
nonlocal current_pid
647658

648-
async with sess.get(url, proxy=proxy.url) as resp:
649-
current_pid = pid
650-
await asyncio.sleep(0.2, loop=loop)
651-
assert current_pid == pid
659+
async with sess.get(url, proxy=proxy.url) as resp:
660+
current_pid = pid
661+
await asyncio.sleep(0.2)
662+
assert current_pid == pid
652663

653-
return resp
664+
return resp
654665

655-
requests = [request(pid) for pid in range(multi_conn_num)]
656-
responses = await asyncio.gather(*requests, loop=loop)
666+
requests = [request(pid) for pid in range(multi_conn_num)]
667+
responses = await asyncio.gather(*requests, return_exceptions=True)
657668

658-
assert len(responses) == multi_conn_num
659-
assert {resp.status for resp in responses} == {200}
660-
661-
await sess.close()
669+
# Filter out exceptions to count actual responses
670+
actual_responses = [r for r in responses if isinstance(r, ClientResponse)]
671+
assert len(actual_responses) == multi_conn_num
672+
assert {resp.status for resp in actual_responses} == {200}
673+
finally:
674+
await sess.close()
675+
await conn.close()
662676

663677

664678
def _patch_ssl_transport(monkeypatch):
@@ -809,7 +823,7 @@ async def xtest_proxy_from_env_https(proxy_test_server, get_request, mocker):
809823
url = "https://aiohttp.io/path"
810824
proxy = await proxy_test_server()
811825
mocker.patch.dict(os.environ, {"https_proxy": str(proxy.url)})
812-
mock.patch("pathlib.Path.is_file", mock_is_file)
826+
mocker.patch("pathlib.Path.is_file", mock_is_file)
813827

814828
await get_request(url=url, trust_env=True)
815829

0 commit comments

Comments
 (0)