|
33 | 33 | WAIT_TIME = 0.1 |
34 | 34 |
|
35 | 35 |
|
36 | | -class SRVPollingKnobs(object): |
| 36 | +class SrvPollingKnobs(object): |
37 | 37 | def __init__(self, ttl_time=None, min_srv_rescan_interval=None, |
38 | 38 | dns_resolver_nodelist_response=None, |
39 | 39 | count_resolver_calls=False): |
@@ -80,7 +80,7 @@ def __exit__(self, exc_type, exc_val, exc_tb): |
80 | 80 | self.disable() |
81 | 81 |
|
82 | 82 |
|
83 | | -class TestSRVPolling(unittest.TestCase): |
| 83 | +class TestSrvPolling(unittest.TestCase): |
84 | 84 |
|
85 | 85 | BASE_SRV_RESPONSE = [ |
86 | 86 | ("localhost.test.build.10gen.cc", 27017), |
@@ -113,7 +113,7 @@ def predicate(): |
113 | 113 | if set(expected_nodelist) == set(nodelist): |
114 | 114 | return True |
115 | 115 | return False |
116 | | - wait_until(predicate, "see expected nodelist", timeout=10*WAIT_TIME) |
| 116 | + wait_until(predicate, "see expected nodelist", timeout=100*WAIT_TIME) |
117 | 117 |
|
118 | 118 | def assert_nodelist_nochange(self, expected_nodelist, client): |
119 | 119 | """Check if the client._topology ever deviates from seeing all nodes |
@@ -148,15 +148,15 @@ def dns_resolver_response(): |
148 | 148 | expected_response = self.BASE_SRV_RESPONSE |
149 | 149 |
|
150 | 150 | # Patch timeouts to ensure short test running times. |
151 | | - with SRVPollingKnobs( |
| 151 | + with SrvPollingKnobs( |
152 | 152 | 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) |
155 | 155 | # Patch list of hosts returned by DNS query. |
156 | | - with SRVPollingKnobs( |
| 156 | + with SrvPollingKnobs( |
157 | 157 | dns_resolver_nodelist_response=dns_resolver_response, |
158 | 158 | count_resolver_calls=count_resolver_calls): |
159 | | - assertion_method(expected_response, mc) |
| 159 | + assertion_method(expected_response, client) |
160 | 160 |
|
161 | 161 | def test_addition(self): |
162 | 162 | response = self.BASE_SRV_RESPONSE[:] |
@@ -198,13 +198,27 @@ def test_dns_record_lookup_empty(self): |
198 | 198 | response = [] |
199 | 199 | self.run_scenario(response, False) |
200 | 200 |
|
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( |
203 | 209 | ttl_time=WAIT_TIME, min_srv_rescan_interval=WAIT_TIME, |
204 | | - dns_resolver_nodelist_response=response_callback, |
| 210 | + dns_resolver_nodelist_response=initial_callback, |
205 | 211 | 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) |
208 | 222 |
|
209 | 223 | def test_recover_from_initially_empty_seedlist(self): |
210 | 224 | def empty_seedlist(): |
|
0 commit comments