Skip to content

Commit 2da8af0

Browse files
authored
PYTHON-3416 Mongos SRV Poller should wait 60 seconds to poll (#1571)
1 parent 167b964 commit 2da8af0

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

pymongo/monitor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,12 @@ def __init__(self, topology: Topology, topology_settings: TopologySettings):
335335
self._seedlist = self._settings._seeds
336336
assert isinstance(self._settings.fqdn, str)
337337
self._fqdn: str = self._settings.fqdn
338+
self._startup_time = time.monotonic()
338339

339340
def _run(self) -> None:
341+
# Don't poll right after creation, wait 60 seconds first
342+
if time.monotonic() < self._startup_time + common.MIN_SRV_RESCAN_INTERVAL:
343+
return
340344
seedlist = self._get_seedlist()
341345
if seedlist:
342346
self._seedlist = seedlist

test/test_srv_polling.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def tearDown(self):
111111
def get_nodelist(self, client):
112112
return client._topology.description.server_descriptions().keys()
113113

114-
def assert_nodelist_change(self, expected_nodelist, client):
114+
def assert_nodelist_change(self, expected_nodelist, client, timeout=(100 * WAIT_TIME)):
115115
"""Check if the client._topology eventually sees all nodes in the
116116
expected_nodelist.
117117
"""
@@ -122,9 +122,9 @@ def predicate():
122122
return True
123123
return False
124124

125-
wait_until(predicate, "see expected nodelist", timeout=100 * WAIT_TIME)
125+
wait_until(predicate, "see expected nodelist", timeout=timeout)
126126

127-
def assert_nodelist_nochange(self, expected_nodelist, client):
127+
def assert_nodelist_nochange(self, expected_nodelist, client, timeout=(100 * WAIT_TIME)):
128128
"""Check if the client._topology ever deviates from seeing all nodes
129129
in the expected_nodelist. Consistency is checked after sleeping for
130130
(WAIT_TIME * 10) seconds. Also check that the resolver is called at
@@ -136,7 +136,7 @@ def predicate():
136136
return pymongo.srv_resolver._SrvResolver.get_hosts_and_min_ttl.call_count >= 1
137137
return False
138138

139-
wait_until(predicate, "Node list equals expected nodelist", timeout=100 * WAIT_TIME)
139+
wait_until(predicate, "Node list equals expected nodelist", timeout=timeout)
140140

141141
nodelist = self.get_nodelist(client)
142142
if set(expected_nodelist) != set(nodelist):
@@ -330,6 +330,22 @@ def nodelist_callback():
330330
with SrvPollingKnobs(nodelist_callback=nodelist_callback):
331331
self.assert_nodelist_change(response, client)
332332

333+
def test_srv_waits_to_poll(self):
334+
modified = [("localhost.test.build.10gen.cc", 27019)]
335+
336+
def resolver_response():
337+
return modified
338+
339+
with SrvPollingKnobs(
340+
ttl_time=WAIT_TIME,
341+
min_srv_rescan_interval=WAIT_TIME,
342+
nodelist_callback=resolver_response,
343+
):
344+
client = MongoClient(self.CONNECTION_STRING)
345+
self.assertRaises(
346+
AssertionError, self.assert_nodelist_change, modified, client, timeout=WAIT_TIME / 2
347+
)
348+
333349

334350
if __name__ == "__main__":
335351
unittest.main()

0 commit comments

Comments
 (0)