From efcecc9a7fbe9ee3850d274b8a6dff74e0a0b30e Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 19 Aug 2024 12:57:15 -0500 Subject: [PATCH] PYTHON-4648 Fix handling of event_loop_policy in tests (#1799) --- test/__init__.py | 10 ---------- test/asynchronous/__init__.py | 10 ---------- test/asynchronous/conftest.py | 14 ++++++++++++++ test/conftest.py | 13 +++++++++++++ 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/test/__init__.py b/test/__init__.py index e60736e3e..2a23ae0fd 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -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 diff --git a/test/asynchronous/__init__.py b/test/asynchronous/__init__.py index 900b260c2..3d22b5ff7 100644 --- a/test/asynchronous/__init__.py +++ b/test/asynchronous/__init__.py @@ -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 diff --git a/test/asynchronous/conftest.py b/test/asynchronous/conftest.py index f5bcd953a..e443dff6c 100644 --- a/test/asynchronous/conftest.py +++ b/test/asynchronous/conftest.py @@ -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() diff --git a/test/conftest.py b/test/conftest.py index 431dd152f..a3d954c7c 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -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()