|
13 | 13 | import os |
14 | 14 | import platform |
15 | 15 | import ssl |
| 16 | +import sys |
16 | 17 | import time |
17 | 18 | from contextlib import asynccontextmanager |
18 | 19 | from os import environ, path |
@@ -86,8 +87,22 @@ async def _asyncio_connect(url, timeout=5): |
86 | 87 | ssl=ssl.create_default_context(), |
87 | 88 | ssl_handshake_timeout=timeout, |
88 | 89 | ) |
89 | | - yield protocol |
90 | | - transport.close() |
| 90 | + try: |
| 91 | + yield protocol |
| 92 | + finally: |
| 93 | + transport.close() |
| 94 | + |
| 95 | + |
| 96 | +@pytest.fixture(scope="module", autouse=True) |
| 97 | +def windows_event_loop_policy(): |
| 98 | + """Set Windows to use SelectorEventLoop for better network stability.""" |
| 99 | + if sys.platform == "win32": |
| 100 | + policy = asyncio.WindowsSelectorEventLoopPolicy() |
| 101 | + asyncio.set_event_loop_policy(policy) |
| 102 | + yield |
| 103 | + asyncio.set_event_loop_policy(None) # Reset |
| 104 | + else: |
| 105 | + yield |
91 | 106 |
|
92 | 107 |
|
93 | 108 | @pytest.fixture(autouse=True) |
@@ -479,7 +494,10 @@ async def test_concurrent_ocsp_requests(tmpdir, session_manager): |
479 | 494 | SnowflakeOCSP.clear_cache() # reset the memory cache |
480 | 495 | SFOCSP(ocsp_response_cache_uri="file://" + cache_file_name) |
481 | 496 |
|
482 | | - target_hosts = TARGET_HOSTS * 5 |
| 497 | + # Windows has lower concurrent connection limits than Linux/Mac |
| 498 | + # Use smaller multiplier to avoid WinError 64: network name is no longer available caused by RuntimeError('Event loop is closed') |
| 499 | + multiplier = 2 if sys.platform == "win32" else 5 |
| 500 | + target_hosts = TARGET_HOSTS * multiplier |
483 | 501 | await asyncio.gather( |
484 | 502 | *[ |
485 | 503 | _validate_certs_using_ocsp(hostname, cache_file_name, session_manager) |
|
0 commit comments