Skip to content

Commit 43660d9

Browse files
committed
reset client context after primary steps down
1 parent c58448f commit 43660d9

File tree

6 files changed

+28
-2
lines changed

6 files changed

+28
-2
lines changed

test/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,12 @@ def max_message_size_bytes(self):
859859
client_context = ClientContext()
860860

861861

862+
def recreate_client_context():
863+
global client_context
864+
client_context = ClientContext()
865+
client_context.init()
866+
867+
862868
class PyMongoTestCase(unittest.TestCase):
863869
def assertEqualCommand(self, expected, actual, msg=None):
864870
self.assertEqual(sanitize_cmd(expected), sanitize_cmd(actual), msg)

test/asynchronous/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,12 @@ async def max_message_size_bytes(self):
861861
async_client_context = AsyncClientContext()
862862

863863

864+
async def recreate_client_context():
865+
global async_client_context
866+
async_client_context = AsyncClientContext()
867+
await async_client_context.init()
868+
869+
864870
class AsyncPyMongoTestCase(unittest.IsolatedAsyncioTestCase):
865871
def assertEqualCommand(self, expected, actual, msg=None):
866872
self.assertEqual(sanitize_cmd(expected), sanitize_cmd(actual), msg)

test/asynchronous/test_connections_survive_primary_stepdown_spec.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919

2020
sys.path[0:0] = [""]
2121

22-
from test.asynchronous import AsyncIntegrationTest, async_client_context, unittest
22+
from test.asynchronous import (
23+
AsyncIntegrationTest,
24+
async_client_context,
25+
recreate_client_context,
26+
unittest,
27+
)
2328
from test.asynchronous.helpers import async_repl_set_step_down
2429
from test.utils import (
2530
CMAPListener,
@@ -60,6 +65,7 @@ async def _setup_class(cls):
6065
@classmethod
6166
async def _tearDown_class(cls):
6267
await cls.client.close()
68+
await recreate_client_context()
6369

6470
async def asyncSetUp(self):
6571
# Note that all ops use same write-concern as self.db (majority).

test/asynchronous/test_monitoring.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ async def test_not_primary_error(self):
413413
try:
414414
await client.pymongo_test.test.find_one_and_delete({})
415415
except NotPrimaryError as exc:
416+
print("an error was raised")
416417
error = exc.errors
417418
started = self.listener.started_events[0]
418419
failed = self.listener.failed_events[0]

test/test_connections_survive_primary_stepdown_spec.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919

2020
sys.path[0:0] = [""]
2121

22-
from test import IntegrationTest, client_context, unittest
22+
from test import (
23+
IntegrationTest,
24+
client_context,
25+
recreate_client_context,
26+
unittest,
27+
)
2328
from test.helpers import repl_set_step_down
2429
from test.utils import (
2530
CMAPListener,
@@ -60,6 +65,7 @@ def _setup_class(cls):
6065
@classmethod
6166
def _tearDown_class(cls):
6267
cls.client.close()
68+
recreate_client_context()
6369

6470
def setUp(self):
6571
# Note that all ops use same write-concern as self.db (majority).

test/test_monitoring.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ def test_not_primary_error(self):
413413
try:
414414
client.pymongo_test.test.find_one_and_delete({})
415415
except NotPrimaryError as exc:
416+
print("an error was raised")
416417
error = exc.errors
417418
started = self.listener.started_events[0]
418419
failed = self.listener.failed_events[0]

0 commit comments

Comments
 (0)