From: Stefano Rivera <stefano@rivera.za.net>
Date: Sat, 26 Oct 2024 11:08:51 -0700
Subject: More robust work-around for ResourceWarnings

The sleep wasn't reliably long-enough, but requesting the client to
close the connection reliably avoids the issue.

As suggested here:
https://github.com/aio-libs/aiohttp/issues/1925#issuecomment-2030109671

Forwarded: https://github.com/sethmlarson/truststore/pull/162
---
 tests/test_api.py | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/tests/test_api.py b/tests/test_api.py
index 23b9a9f..1a50bcc 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -1,4 +1,3 @@
-import asyncio
 import importlib
 import os
 import platform
@@ -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
@@ -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()
 
 
