Skip to content

Commit 2c631fa

Browse files
committed
PYTHON-2239 Avoid 30 second stalls in TestMongoClientFailover
1 parent 33c9322 commit 2c631fa

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

test/pymongo_mocks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def get_socket(self, all_credentials, checkout=False):
5151
+ client.mock_members
5252
+ client.mock_mongoses), "bad host: %s" % host_and_port
5353

54-
with Pool.get_socket(self, all_credentials) as sock_info:
54+
with Pool.get_socket(self, all_credentials, checkout) as sock_info:
5555
sock_info.mock_host = self.mock_host
5656
sock_info.mock_port = self.mock_port
5757
yield sock_info
@@ -74,7 +74,7 @@ def __init__(
7474
pool,
7575
topology_settings)
7676

77-
def _check_once(self, metadata=None, cluster_time=None):
77+
def _check_once(self):
7878
address = self._server_description.address
7979
response, rtt = self.client.mock_is_master('%s:%d' % address)
8080
return ServerDescription(address, IsMaster(response), rtt)

test/test_client.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
InvalidURI,
4949
NetworkTimeout,
5050
OperationFailure,
51+
ServerSelectionTimeoutError,
5152
WriteConcernError)
5253
from pymongo.monitoring import (ServerHeartbeatListener,
5354
ServerHeartbeatStartedEvent)
@@ -1731,6 +1732,7 @@ def test_discover_primary(self):
17311732
mongoses=[],
17321733
host='b:2', # Pass a secondary.
17331734
replicaSet='rs')
1735+
self.addCleanup(c.close)
17341736

17351737
wait_until(lambda: len(c.nodes) == 3, 'connect')
17361738
self.assertEqual(c.address, ('a', 1))
@@ -1760,7 +1762,10 @@ def test_reconnect(self):
17601762
mongoses=[],
17611763
host='b:2', # Pass a secondary.
17621764
replicaSet='rs',
1763-
retryReads=False)
1765+
retryReads=False,
1766+
serverSelectionTimeoutMS=100,
1767+
)
1768+
self.addCleanup(c.close)
17641769

17651770
wait_until(lambda: len(c.nodes) == 3, 'connect')
17661771

@@ -1769,8 +1774,13 @@ def test_reconnect(self):
17691774
c.kill_host('b:2')
17701775
c.kill_host('c:3')
17711776

1772-
# MongoClient discovers it's alone.
1777+
# MongoClient discovers it's alone. The first attempt raises either
1778+
# ServerSelectionTimeoutError or AutoReconnect (from
1779+
# MockPool.get_socket).
17731780
self.assertRaises(AutoReconnect, c.db.collection.find_one)
1781+
# The second attempt always raises ServerSelectionTimeoutError.
1782+
self.assertRaises(ServerSelectionTimeoutError,
1783+
c.db.collection.find_one)
17741784

17751785
# But it can reconnect.
17761786
c.revive_host('a:1')
@@ -1789,7 +1799,9 @@ def _test_network_error(self, operation_callback):
17891799
host='a:1',
17901800
replicaSet='rs',
17911801
connect=False,
1792-
retryReads=False)
1802+
retryReads=False,
1803+
serverSelectionTimeoutMS=100)
1804+
self.addCleanup(c.close)
17931805

17941806
# Set host-specific information so we can test whether it is reset.
17951807
c.set_wire_version_range('a:1', 2, 6)
@@ -1799,7 +1811,9 @@ def _test_network_error(self, operation_callback):
17991811

18001812
c.kill_host('a:1')
18011813

1802-
# MongoClient is disconnected from the primary.
1814+
# MongoClient is disconnected from the primary. This raises either
1815+
# ServerSelectionTimeoutError or AutoReconnect (from
1816+
# MockPool.get_socket).
18031817
self.assertRaises(AutoReconnect, operation_callback, c)
18041818

18051819
# The primary's description is reset.

0 commit comments

Comments
 (0)