2323import pymongo
2424
2525from pymongo import common
26+ from pymongo .errors import ConfigurationError
2627from pymongo .srv_resolver import _HAVE_DNSPYTHON
2728from pymongo .mongo_client import MongoClient
2829from test import client_knobs , unittest
@@ -91,6 +92,14 @@ def setUp(self):
9192 if not _HAVE_DNSPYTHON :
9293 raise unittest .SkipTest ("SRV polling tests require the dnspython "
9394 "module" )
95+ # Patch timeouts to ensure short rescan SRV interval.
96+ self .client_knobs = client_knobs (
97+ heartbeat_frequency = WAIT_TIME , min_heartbeat_interval = WAIT_TIME ,
98+ events_queue_frequency = WAIT_TIME )
99+ self .client_knobs .enable ()
100+
101+ def tearDown (self ):
102+ self .client_knobs .disable ()
94103
95104 def get_nodelist (self , client ):
96105 return client ._topology .description .server_descriptions ().keys ()
@@ -122,7 +131,7 @@ def assert_nodelist_nochange(self, expected_nodelist, client):
122131 1 , "resolver was never called" )
123132 return True
124133
125- def _run_scenario (self , dns_response , expect_change ):
134+ def run_scenario (self , dns_response , expect_change ):
126135 if callable (dns_response ):
127136 dns_resolver_response = dns_response
128137 else :
@@ -149,13 +158,6 @@ def dns_resolver_response():
149158 count_resolver_calls = count_resolver_calls ):
150159 assertion_method (expected_response , mc )
151160
152- def run_scenario (self , dns_response , expect_change ):
153- # Patch timeouts to ensure short rescan SRV interval.
154- with client_knobs (heartbeat_frequency = WAIT_TIME ,
155- min_heartbeat_interval = WAIT_TIME ,
156- events_queue_frequency = WAIT_TIME ):
157- self ._run_scenario (dns_response , expect_change )
158-
159161 def test_addition (self ):
160162 response = self .BASE_SRV_RESPONSE [:]
161163 response .append (
@@ -196,6 +198,24 @@ def test_dns_record_lookup_empty(self):
196198 response = []
197199 self .run_scenario (response , False )
198200
201+ def _test_recover_from_initial (self , response_callback ):
202+ with SRVPollingKnobs (
203+ ttl_time = WAIT_TIME , min_srv_rescan_interval = WAIT_TIME ,
204+ dns_resolver_nodelist_response = response_callback ,
205+ count_resolver_calls = True ):
206+ mc = MongoClient (self .CONNECTION_STRING )
207+ self .assert_nodelist_nochange (self .BASE_SRV_RESPONSE , mc )
208+
209+ def test_recover_from_initially_empty_seedlist (self ):
210+ def empty_seedlist ():
211+ return []
212+ self ._test_recover_from_initial (empty_seedlist )
213+
214+ def test_recover_from_initially_erroring_seedlist (self ):
215+ def erroring_seedlist ():
216+ raise ConfigurationError
217+ self ._test_recover_from_initial (erroring_seedlist )
218+
199219
200220if __name__ == '__main__' :
201221 unittest .main ()
0 commit comments