Skip to content

Commit

Permalink
More robust work-around for ResourceWarnings
Browse files Browse the repository at this point in the history
The sleep wasn't reliably long-enough, but requesting the client to
close the connection reliably avoids the issue.

As suggested here:
aio-libs/aiohttp#1925 (comment)
  • Loading branch information
stefanor authored and sethmlarson committed Oct 26, 2024
1 parent 3524a6d commit 5ae2476
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import importlib
import os
import platform
Expand Down Expand Up @@ -300,13 +299,14 @@ def test_sslcontext_api_success(host):
@pytest.mark.asyncio
async def test_sslcontext_api_success_async(host):
ctx = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
async with aiohttp.ClientSession() as http:
# connector avoids https://github.com/aio-libs/aiohttp/issues/5426
async with aiohttp.ClientSession(
connector=aiohttp.TCPConnector(force_close=True, enable_cleanup_closed=True)
) as http:
resp = await http.request("GET", f"https://{host}", ssl=ctx)

assert resp.status == 200
assert len(await resp.text()) > 0
# workaround https://github.com/aio-libs/aiohttp/issues/5426
await asyncio.sleep(0.2)


@failure_hosts
Expand All @@ -328,15 +328,15 @@ async def test_sslcontext_api_failures_async(failure):
ctx = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
if platform.system() != "Linux":
ctx.verify_flags |= ssl.VERIFY_CRL_CHECK_CHAIN
async with aiohttp.ClientSession() as http:
# connector avoids https://github.com/aio-libs/aiohttp/issues/5426
async with aiohttp.ClientSession(
connector=aiohttp.TCPConnector(force_close=True, enable_cleanup_closed=True)
) as http:
with pytest.raises(
aiohttp.client_exceptions.ClientConnectorCertificateError
) as e:
await http.request("GET", f"https://{failure.host}", ssl=ctx)

# workaround https://github.com/aio-libs/aiohttp/issues/5426
await asyncio.sleep(0.2)

assert "cert" in repr(e.value).lower() and "verif" in repr(e.value).lower()


Expand Down

0 comments on commit 5ae2476

Please sign in to comment.