Skip to content

Commit efcecc9

Browse files
authored
PYTHON-4648 Fix handling of event_loop_policy in tests (mongodb#1799)
1 parent 559d8b1 commit efcecc9

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

test/__init__.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,6 @@
7979

8080
_IS_SYNC = True
8181

82-
# The default asyncio loop implementation on Windows
83-
# has issues with sharing sockets across loops (https://github.com/python/cpython/issues/122240)
84-
# We explicitly use a different loop implementation here to prevent that issue
85-
if (
86-
not _IS_SYNC
87-
and sys.platform == "win32"
88-
and asyncio.get_event_loop_policy() == asyncio.WindowsProactorEventLoopPolicy
89-
):
90-
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # type: ignore[attr-defined]
91-
9282

9383
class ClientContext:
9484
client: MongoClient

test/asynchronous/__init__.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,6 @@
7979

8080
_IS_SYNC = False
8181

82-
# The default asyncio loop implementation on Windows
83-
# has issues with sharing sockets across loops (https://github.com/python/cpython/issues/122240)
84-
# We explicitly use a different loop implementation here to prevent that issue
85-
if (
86-
not _IS_SYNC
87-
and sys.platform == "win32"
88-
and asyncio.get_event_loop_policy() == asyncio.WindowsProactorEventLoopPolicy
89-
):
90-
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # type: ignore[attr-defined]
91-
9282

9383
class AsyncClientContext:
9484
client: AsyncMongoClient

test/asynchronous/conftest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
from __future__ import annotations
22

3+
import asyncio
4+
import sys
35
from test import pytest_conf
46
from test.asynchronous import async_setup, async_teardown
57

8+
import pytest
69
import pytest_asyncio
710

811
_IS_SYNC = False
912

1013

14+
@pytest.fixture(scope="session")
15+
def event_loop_policy():
16+
# The default asyncio loop implementation on Windows
17+
# has issues with sharing sockets across loops (https://github.com/python/cpython/issues/122240)
18+
# We explicitly use a different loop implementation here to prevent that issue
19+
if sys.platform == "win32":
20+
return asyncio.WindowsSelectorEventLoopPolicy() # type: ignore[attr-defined]
21+
22+
return asyncio.get_event_loop_policy()
23+
24+
1125
@pytest_asyncio.fixture(scope="session", autouse=True)
1226
async def test_setup_and_teardown():
1327
await async_setup()

test/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
from __future__ import annotations
22

3+
import asyncio
4+
import sys
35
from test import pytest_conf, setup, teardown
46

57
import pytest
68

79
_IS_SYNC = True
810

911

12+
@pytest.fixture(scope="session")
13+
def event_loop_policy():
14+
# The default asyncio loop implementation on Windows
15+
# has issues with sharing sockets across loops (https://github.com/python/cpython/issues/122240)
16+
# We explicitly use a different loop implementation here to prevent that issue
17+
if sys.platform == "win32":
18+
return asyncio.WindowsSelectorEventLoopPolicy() # type: ignore[attr-defined]
19+
20+
return asyncio.get_event_loop_policy()
21+
22+
1023
@pytest.fixture(scope="session", autouse=True)
1124
def test_setup_and_teardown():
1225
setup()

0 commit comments

Comments
 (0)