PYTHON-915 - Accept 0 for localThresholdMS.

This commit is contained in:
Bernie Hackett 2015-05-08 08:34:15 -07:00
parent 3d65bab521
commit f506d8b69f
4 changed files with 54 additions and 4 deletions

View File

@ -220,6 +220,15 @@ def validate_positive_float(option, value):
return value
def validate_positive_float_or_zero(option, value):
"""Validates that 'value' is 0 or a positive float, or can be converted to
0 or a positive float.
"""
if value == 0 or value == "0":
return 0
return validate_positive_float(option, value)
def validate_timeout_or_none(option, value):
"""Validates a timeout specified in milliseconds returning
a value in floating point seconds.
@ -393,7 +402,7 @@ VALIDATORS = {
'read_preference': validate_read_preference,
'readpreference': validate_read_preference_mode,
'readpreferencetags': validate_read_preference_tags,
'localthresholdms': validate_positive_float,
'localthresholdms': validate_positive_float_or_zero,
'serverselectiontimeoutms': validate_timeout_or_zero,
'authmechanism': validate_auth_mechanism,
'authsource': validate_string,

View File

@ -112,7 +112,7 @@ def apply_local_threshold(latency_ms, server_descriptions):
fastest = min(s.round_trip_time for s in server_descriptions)
return [
s for s in server_descriptions
if (s.round_trip_time - fastest) < latency_ms / 1000.]
if (s.round_trip_time - fastest) <= latency_ms / 1000.]
def secondary_with_tags_server_selector(tag_sets, server_descriptions):

View File

@ -75,7 +75,7 @@ class TestMongosLoadBalancing(MockClientTest):
# Latencies in seconds.
mock_client.mock_rtts['a:1'] = 0.020
mock_client.mock_rtts['b:2'] = 0.025
mock_client.mock_rtts['c:3'] = 0.040
mock_client.mock_rtts['c:3'] = 0.045
return mock_client
def test_lazy_connect(self):
@ -140,6 +140,7 @@ class TestMongosLoadBalancing(MockClientTest):
def test_local_threshold(self):
client = connected(self.mock_client(localThresholdMS=30))
self.assertEqual(30, client.local_threshold_ms)
wait_until(lambda: len(client.nodes) == 3, 'connect to all mongoses')
topology = client._topology
@ -147,6 +148,22 @@ class TestMongosLoadBalancing(MockClientTest):
self.assertEqual(set([('a', 1), ('b', 2), ('c', 3)]),
writable_addresses(topology))
# No error
client.db.collection.find_one()
client = connected(self.mock_client(localThresholdMS=0))
self.assertEqual(0, client.local_threshold_ms)
# No error
client.db.collection.find_one()
# Our chosen mongos goes down.
client.kill_host('%s:%s' % next(iter(client.nodes)))
try:
client.db.collection.find_one()
except:
pass
# No error
client.db.collection.find_one()
def test_load_balancing(self):
# Although the server selection JSON tests already prove that
# select_servers works for sharded topologies, here we do an end-to-end
@ -167,7 +184,7 @@ class TestMongosLoadBalancing(MockClientTest):
self.assertEqual(set([('a', 1), ('b', 2)]),
writable_addresses(topology))
client.mock_rtts['a:1'] = 0.040
client.mock_rtts['a:1'] = 0.045
# Discover only b is within latency window.
wait_until(lambda: set([('b', 2)]) == writable_addresses(topology),

View File

@ -31,6 +31,7 @@ from pymongo.read_preferences import (ReadPreference, MovingAverage,
Primary, PrimaryPreferred,
Secondary, SecondaryPreferred,
Nearest, _ServerMode)
from pymongo.server_description import ServerDescription
from pymongo.server_selectors import readable_server_selector
from pymongo.server_type import SERVER_TYPE
from pymongo.write_concern import WriteConcern
@ -203,6 +204,29 @@ class TestReadPreferences(TestReadPreferencesBase):
localthresholdms=666
).local_threshold_ms)
self.assertEqual(0, rs_client(
localthresholdms=0
).local_threshold_ms)
self.assertRaises(ValueError,
rs_client,
localthresholdms=-1)
def test_zero_latency(self):
ping_times = set()
# Generate unique ping times.
while len(ping_times) < len(self.client.nodes):
ping_times.add(random.random())
for ping_time, host in zip(ping_times, self.client.nodes):
ServerDescription._host_to_round_trip_time[host] = ping_time
try:
client = rs_client(readPreference='nearest', localThresholdMS=0)
host = self.read_from_which_host(client)
for _ in range(5):
self.assertEqual(host, self.read_from_which_host(client))
finally:
ServerDescription._host_to_round_trip_time.clear()
def test_primary(self):
self.assertReadsFrom(
'primary', read_preference=ReadPreference.PRIMARY)