23
23
import pymongo
24
24
25
25
from pymongo import common
26
+ from pymongo .errors import ConfigurationError
26
27
from pymongo .srv_resolver import _HAVE_DNSPYTHON
27
28
from pymongo .mongo_client import MongoClient
28
29
from test import client_knobs , unittest
@@ -91,6 +92,14 @@ def setUp(self):
91
92
if not _HAVE_DNSPYTHON :
92
93
raise unittest .SkipTest ("SRV polling tests require the dnspython "
93
94
"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 ()
94
103
95
104
def get_nodelist (self , client ):
96
105
return client ._topology .description .server_descriptions ().keys ()
@@ -122,7 +131,7 @@ def assert_nodelist_nochange(self, expected_nodelist, client):
122
131
1 , "resolver was never called" )
123
132
return True
124
133
125
- def _run_scenario (self , dns_response , expect_change ):
134
+ def run_scenario (self , dns_response , expect_change ):
126
135
if callable (dns_response ):
127
136
dns_resolver_response = dns_response
128
137
else :
@@ -149,13 +158,6 @@ def dns_resolver_response():
149
158
count_resolver_calls = count_resolver_calls ):
150
159
assertion_method (expected_response , mc )
151
160
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
-
159
161
def test_addition (self ):
160
162
response = self .BASE_SRV_RESPONSE [:]
161
163
response .append (
@@ -196,6 +198,24 @@ def test_dns_record_lookup_empty(self):
196
198
response = []
197
199
self .run_scenario (response , False )
198
200
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
+
199
219
200
220
if __name__ == '__main__' :
201
221
unittest .main ()
0 commit comments