Skip to content

Commit 960509e

Browse files
committed
PYTHON-1872 Follow-on SrvMonitor testing work
1 parent d279758 commit 960509e

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

test/test_srv_polling.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
WAIT_TIME = 0.1
3434

3535

36-
class SRVPollingKnobs(object):
36+
class SrvPollingKnobs(object):
3737
def __init__(self, ttl_time=None, min_srv_rescan_interval=None,
3838
dns_resolver_nodelist_response=None,
3939
count_resolver_calls=False):
@@ -80,7 +80,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
8080
self.disable()
8181

8282

83-
class TestSRVPolling(unittest.TestCase):
83+
class TestSrvPolling(unittest.TestCase):
8484

8585
BASE_SRV_RESPONSE = [
8686
("localhost.test.build.10gen.cc", 27017),
@@ -113,7 +113,7 @@ def predicate():
113113
if set(expected_nodelist) == set(nodelist):
114114
return True
115115
return False
116-
wait_until(predicate, "see expected nodelist", timeout=10*WAIT_TIME)
116+
wait_until(predicate, "see expected nodelist", timeout=100*WAIT_TIME)
117117

118118
def assert_nodelist_nochange(self, expected_nodelist, client):
119119
"""Check if the client._topology ever deviates from seeing all nodes
@@ -148,15 +148,15 @@ def dns_resolver_response():
148148
expected_response = self.BASE_SRV_RESPONSE
149149

150150
# Patch timeouts to ensure short test running times.
151-
with SRVPollingKnobs(
151+
with SrvPollingKnobs(
152152
ttl_time=WAIT_TIME, min_srv_rescan_interval=WAIT_TIME):
153-
mc = MongoClient(self.CONNECTION_STRING)
154-
self.assert_nodelist_change(self.BASE_SRV_RESPONSE, mc)
153+
client = MongoClient(self.CONNECTION_STRING)
154+
self.assert_nodelist_change(self.BASE_SRV_RESPONSE, client)
155155
# Patch list of hosts returned by DNS query.
156-
with SRVPollingKnobs(
156+
with SrvPollingKnobs(
157157
dns_resolver_nodelist_response=dns_resolver_response,
158158
count_resolver_calls=count_resolver_calls):
159-
assertion_method(expected_response, mc)
159+
assertion_method(expected_response, client)
160160

161161
def test_addition(self):
162162
response = self.BASE_SRV_RESPONSE[:]
@@ -198,13 +198,27 @@ def test_dns_record_lookup_empty(self):
198198
response = []
199199
self.run_scenario(response, False)
200200

201-
def _test_recover_from_initial(self, response_callback):
202-
with SRVPollingKnobs(
201+
def _test_recover_from_initial(self, initial_callback):
202+
# Construct a valid final response callback distinct from base.
203+
response_final = self.BASE_SRV_RESPONSE[:]
204+
response_final.pop()
205+
def final_callback():
206+
return response_final
207+
208+
with SrvPollingKnobs(
203209
ttl_time=WAIT_TIME, min_srv_rescan_interval=WAIT_TIME,
204-
dns_resolver_nodelist_response=response_callback,
210+
dns_resolver_nodelist_response=initial_callback,
205211
count_resolver_calls=True):
206-
mc = MongoClient(self.CONNECTION_STRING)
207-
self.assert_nodelist_nochange(self.BASE_SRV_RESPONSE, mc)
212+
# Client uses unpatched method to get initial nodelist
213+
client = MongoClient(self.CONNECTION_STRING)
214+
# Invalid DNS resolver response should not change nodelist.
215+
self.assert_nodelist_nochange(self.BASE_SRV_RESPONSE, client)
216+
217+
with SrvPollingKnobs(
218+
ttl_time=WAIT_TIME, min_srv_rescan_interval=WAIT_TIME,
219+
dns_resolver_nodelist_response=final_callback):
220+
# Nodelist should reflect new valid DNS resolver response.
221+
self.assert_nodelist_change(response_final, client)
208222

209223
def test_recover_from_initially_empty_seedlist(self):
210224
def empty_seedlist():

0 commit comments

Comments
 (0)