PYTHON-4648 Fix handling of event_loop_policy in tests (#1799)

This commit is contained in:
Steven Silvester 2024-08-19 12:57:15 -05:00 committed by GitHub
parent 559d8b1ea1
commit efcecc9a7f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 27 additions and 20 deletions

View File

@ -79,16 +79,6 @@ from pymongo.synchronous.mongo_client import MongoClient
_IS_SYNC = True
# The default asyncio loop implementation on Windows
# has issues with sharing sockets across loops (https://github.com/python/cpython/issues/122240)
# We explicitly use a different loop implementation here to prevent that issue
if (
not _IS_SYNC
and sys.platform == "win32"
and asyncio.get_event_loop_policy() == asyncio.WindowsProactorEventLoopPolicy
):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # type: ignore[attr-defined]
class ClientContext:
client: MongoClient

View File

@ -79,16 +79,6 @@ from pymongo.ssl_support import HAVE_SSL, _ssl # type:ignore[attr-defined]
_IS_SYNC = False
# The default asyncio loop implementation on Windows
# has issues with sharing sockets across loops (https://github.com/python/cpython/issues/122240)
# We explicitly use a different loop implementation here to prevent that issue
if (
not _IS_SYNC
and sys.platform == "win32"
and asyncio.get_event_loop_policy() == asyncio.WindowsProactorEventLoopPolicy
):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # type: ignore[attr-defined]
class AsyncClientContext:
client: AsyncMongoClient

View File

@ -1,13 +1,27 @@
from __future__ import annotations
import asyncio
import sys
from test import pytest_conf
from test.asynchronous import async_setup, async_teardown
import pytest
import pytest_asyncio
_IS_SYNC = False
@pytest.fixture(scope="session")
def event_loop_policy():
# The default asyncio loop implementation on Windows
# has issues with sharing sockets across loops (https://github.com/python/cpython/issues/122240)
# We explicitly use a different loop implementation here to prevent that issue
if sys.platform == "win32":
return asyncio.WindowsSelectorEventLoopPolicy() # type: ignore[attr-defined]
return asyncio.get_event_loop_policy()
@pytest_asyncio.fixture(scope="session", autouse=True)
async def test_setup_and_teardown():
await async_setup()

View File

@ -1,5 +1,7 @@
from __future__ import annotations
import asyncio
import sys
from test import pytest_conf, setup, teardown
import pytest
@ -7,6 +9,17 @@ import pytest
_IS_SYNC = True
@pytest.fixture(scope="session")
def event_loop_policy():
# The default asyncio loop implementation on Windows
# has issues with sharing sockets across loops (https://github.com/python/cpython/issues/122240)
# We explicitly use a different loop implementation here to prevent that issue
if sys.platform == "win32":
return asyncio.WindowsSelectorEventLoopPolicy() # type: ignore[attr-defined]
return asyncio.get_event_loop_policy()
@pytest.fixture(scope="session", autouse=True)
def test_setup_and_teardown():
setup()