48
48
InvalidURI ,
49
49
NetworkTimeout ,
50
50
OperationFailure ,
51
+ ServerSelectionTimeoutError ,
51
52
WriteConcernError )
52
53
from pymongo .monitoring import (ServerHeartbeatListener ,
53
54
ServerHeartbeatStartedEvent )
@@ -1731,6 +1732,7 @@ def test_discover_primary(self):
1731
1732
mongoses = [],
1732
1733
host = 'b:2' , # Pass a secondary.
1733
1734
replicaSet = 'rs' )
1735
+ self .addCleanup (c .close )
1734
1736
1735
1737
wait_until (lambda : len (c .nodes ) == 3 , 'connect' )
1736
1738
self .assertEqual (c .address , ('a' , 1 ))
@@ -1760,7 +1762,10 @@ def test_reconnect(self):
1760
1762
mongoses = [],
1761
1763
host = 'b:2' , # Pass a secondary.
1762
1764
replicaSet = 'rs' ,
1763
- retryReads = False )
1765
+ retryReads = False ,
1766
+ serverSelectionTimeoutMS = 100 ,
1767
+ )
1768
+ self .addCleanup (c .close )
1764
1769
1765
1770
wait_until (lambda : len (c .nodes ) == 3 , 'connect' )
1766
1771
@@ -1769,8 +1774,13 @@ def test_reconnect(self):
1769
1774
c .kill_host ('b:2' )
1770
1775
c .kill_host ('c:3' )
1771
1776
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).
1773
1780
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 )
1774
1784
1775
1785
# But it can reconnect.
1776
1786
c .revive_host ('a:1' )
@@ -1789,7 +1799,9 @@ def _test_network_error(self, operation_callback):
1789
1799
host = 'a:1' ,
1790
1800
replicaSet = 'rs' ,
1791
1801
connect = False ,
1792
- retryReads = False )
1802
+ retryReads = False ,
1803
+ serverSelectionTimeoutMS = 100 )
1804
+ self .addCleanup (c .close )
1793
1805
1794
1806
# Set host-specific information so we can test whether it is reset.
1795
1807
c .set_wire_version_range ('a:1' , 2 , 6 )
@@ -1799,7 +1811,9 @@ def _test_network_error(self, operation_callback):
1799
1811
1800
1812
c .kill_host ('a:1' )
1801
1813
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).
1803
1817
self .assertRaises (AutoReconnect , operation_callback , c )
1804
1818
1805
1819
# The primary's description is reset.
0 commit comments