Skip to content

Commit b9fcba7

Browse files
committed
PYTHON-5071 Cleanup
1 parent 3a2d844 commit b9fcba7

File tree

2 files changed

+48
-48
lines changed

2 files changed

+48
-48
lines changed

test/__init__.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,28 @@ def max_message_size_bytes(self):
867867
# Reusable client context
868868
client_context = ClientContext()
869869

870+
# Global event loop for async tests.
871+
LOOP = None
872+
873+
874+
def get_loop() -> asyncio.AbstractEventLoop:
875+
"""Get the test suite's global event loop."""
876+
global LOOP
877+
if LOOP is None:
878+
try:
879+
LOOP = asyncio.get_running_loop()
880+
except RuntimeError:
881+
# no running event loop, fallback to get_event_loop.
882+
try:
883+
# Ignore DeprecationWarning: There is no current event loop
884+
with warnings.catch_warnings():
885+
warnings.simplefilter("ignore", DeprecationWarning)
886+
LOOP = asyncio.get_event_loop()
887+
except RuntimeError:
888+
LOOP = asyncio.new_event_loop()
889+
asyncio.set_event_loop(LOOP)
890+
return LOOP
891+
870892

871893
class PyMongoTestCase(unittest.TestCase):
872894
if not _IS_SYNC:
@@ -878,7 +900,6 @@ def setUp(self):
878900
def tearDown(self):
879901
pass
880902

881-
# See TestCase.addCleanup.
882903
def addCleanup(self, func, /, *args, **kwargs):
883904
self.addCleanup(*(func, *args), **kwargs)
884905

@@ -1214,31 +1235,10 @@ def tearDown(self) -> None:
12141235
super().tearDown()
12151236

12161237

1217-
LOOP = None
1218-
1219-
1220-
def get_loop() -> asyncio.AbstractEventLoop:
1221-
global LOOP
1222-
if LOOP is None:
1223-
try:
1224-
LOOP = asyncio.get_running_loop()
1225-
except RuntimeError:
1226-
# no running event loop, fallback to get_event_loop.
1227-
try:
1228-
# Ignore DeprecationWarning: There is no current event loop
1229-
with warnings.catch_warnings():
1230-
warnings.simplefilter("ignore", DeprecationWarning)
1231-
LOOP = asyncio.get_event_loop()
1232-
except RuntimeError:
1233-
LOOP = asyncio.new_event_loop()
1234-
asyncio.set_event_loop(LOOP)
1235-
return LOOP
1236-
1237-
12381238
def setup():
12391239
if not _IS_SYNC:
1240-
global LOOP
1241-
LOOP = asyncio.get_running_loop()
1240+
# Set up the event loop.
1241+
get_loop()
12421242
client_context.init()
12431243
warnings.resetwarnings()
12441244
warnings.simplefilter("always")

test/asynchronous/__init__.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,28 @@ async def max_message_size_bytes(self):
869869
# Reusable client context
870870
async_client_context = AsyncClientContext()
871871

872+
# Global event loop for async tests.
873+
LOOP = None
874+
875+
876+
def get_loop() -> asyncio.AbstractEventLoop:
877+
"""Get the test suite's global event loop."""
878+
global LOOP
879+
if LOOP is None:
880+
try:
881+
LOOP = asyncio.get_running_loop()
882+
except RuntimeError:
883+
# no running event loop, fallback to get_event_loop.
884+
try:
885+
# Ignore DeprecationWarning: There is no current event loop
886+
with warnings.catch_warnings():
887+
warnings.simplefilter("ignore", DeprecationWarning)
888+
LOOP = asyncio.get_event_loop()
889+
except RuntimeError:
890+
LOOP = asyncio.new_event_loop()
891+
asyncio.set_event_loop(LOOP)
892+
return LOOP
893+
872894

873895
class AsyncPyMongoTestCase(unittest.TestCase):
874896
if not _IS_SYNC:
@@ -880,7 +902,6 @@ async def asyncSetUp(self):
880902
async def asyncTearDown(self):
881903
pass
882904

883-
# See IsolatedAsyncioTestCase.addAsyncCleanup.
884905
def addAsyncCleanup(self, func, /, *args, **kwargs):
885906
self.addCleanup(*(func, *args), **kwargs)
886907

@@ -1232,31 +1253,10 @@ async def asyncTearDown(self) -> None:
12321253
await super().asyncTearDown()
12331254

12341255

1235-
LOOP = None
1236-
1237-
1238-
def get_loop() -> asyncio.AbstractEventLoop:
1239-
global LOOP
1240-
if LOOP is None:
1241-
try:
1242-
LOOP = asyncio.get_running_loop()
1243-
except RuntimeError:
1244-
# no running event loop, fallback to get_event_loop.
1245-
try:
1246-
# Ignore DeprecationWarning: There is no current event loop
1247-
with warnings.catch_warnings():
1248-
warnings.simplefilter("ignore", DeprecationWarning)
1249-
LOOP = asyncio.get_event_loop()
1250-
except RuntimeError:
1251-
LOOP = asyncio.new_event_loop()
1252-
asyncio.set_event_loop(LOOP)
1253-
return LOOP
1254-
1255-
12561256
async def async_setup():
12571257
if not _IS_SYNC:
1258-
global LOOP
1259-
LOOP = asyncio.get_running_loop()
1258+
# Set up the event loop.
1259+
get_loop()
12601260
await async_client_context.init()
12611261
warnings.resetwarnings()
12621262
warnings.simplefilter("always")

0 commit comments

Comments
 (0)