Skip to content

Commit 87cbd36

Browse files
committed
PYTHON-5071 Stop reinitializing client_context
1 parent 2ed7c60 commit 87cbd36

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

test/__init__.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -868,30 +868,28 @@ def max_message_size_bytes(self):
868868
client_context = ClientContext()
869869

870870

871-
def reset_client_context():
872-
if _IS_SYNC:
873-
# sync tests don't need to reset a client context
874-
return
875-
elif client_context.client is not None:
876-
client_context.client.close()
877-
client_context.client = None
878-
client_context._init_client()
879-
880-
881871
class PyMongoTestCase(unittest.TestCase):
882872
if not _IS_SYNC:
883873
# An async TestCase that uses a single event loop for all tests.
884874
# Inspired by TestCase.
885875
def __init__(self, methodName="runTest"):
886876
super().__init__(methodName)
877+
self._loop = None
878+
879+
@property
880+
def loop(self):
881+
if self._loop:
882+
return self._loop
887883
try:
888884
with warnings.catch_warnings():
889885
# Ignore DeprecationWarning: There is no current event loop
890886
warnings.simplefilter("ignore", DeprecationWarning)
891-
self.loop = asyncio.get_event_loop()
887+
loop = asyncio.get_event_loop()
892888
except RuntimeError:
893-
self.loop = asyncio.new_event_loop()
894-
asyncio.set_event_loop(self.loop)
889+
loop = asyncio.new_event_loop()
890+
asyncio.set_event_loop(loop)
891+
self._loop = loop
892+
return loop
895893

896894
def setUp(self):
897895
pass
@@ -1188,8 +1186,6 @@ class IntegrationTest(PyMongoTestCase):
11881186

11891187
@client_context.require_connection
11901188
def setUp(self) -> None:
1191-
if not _IS_SYNC:
1192-
reset_client_context()
11931189
if client_context.load_balancer and not getattr(self, "RUN_ON_LOAD_BALANCER", False):
11941190
raise SkipTest("this test does not support load balancers")
11951191
if client_context.serverless and not getattr(self, "RUN_ON_SERVERLESS", False):

test/asynchronous/__init__.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -870,30 +870,28 @@ async def max_message_size_bytes(self):
870870
async_client_context = AsyncClientContext()
871871

872872

873-
async def reset_client_context():
874-
if _IS_SYNC:
875-
# sync tests don't need to reset a client context
876-
return
877-
elif async_client_context.client is not None:
878-
await async_client_context.client.close()
879-
async_client_context.client = None
880-
await async_client_context._init_client()
881-
882-
883873
class AsyncPyMongoTestCase(unittest.TestCase):
884874
if not _IS_SYNC:
885875
# An async TestCase that uses a single event loop for all tests.
886876
# Inspired by IsolatedAsyncioTestCase.
887877
def __init__(self, methodName="runTest"):
888878
super().__init__(methodName)
879+
self._loop = None
880+
881+
@property
882+
def loop(self):
883+
if self._loop:
884+
return self._loop
889885
try:
890886
with warnings.catch_warnings():
891887
# Ignore DeprecationWarning: There is no current event loop
892888
warnings.simplefilter("ignore", DeprecationWarning)
893-
self.loop = asyncio.get_event_loop()
889+
loop = asyncio.get_event_loop()
894890
except RuntimeError:
895-
self.loop = asyncio.new_event_loop()
896-
asyncio.set_event_loop(self.loop)
891+
loop = asyncio.new_event_loop()
892+
asyncio.set_event_loop(loop)
893+
self._loop = loop
894+
return loop
897895

898896
async def asyncSetUp(self):
899897
pass
@@ -1206,8 +1204,6 @@ class AsyncIntegrationTest(AsyncPyMongoTestCase):
12061204

12071205
@async_client_context.require_connection
12081206
async def asyncSetUp(self) -> None:
1209-
if not _IS_SYNC:
1210-
await reset_client_context()
12111207
if async_client_context.load_balancer and not getattr(self, "RUN_ON_LOAD_BALANCER", False):
12121208
raise SkipTest("this test does not support load balancers")
12131209
if async_client_context.serverless and not getattr(self, "RUN_ON_SERVERLESS", False):

0 commit comments

Comments
 (0)