4848 InvalidURI ,
4949 NetworkTimeout ,
5050 OperationFailure ,
51+ ServerSelectionTimeoutError ,
5152 WriteConcernError )
5253from 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