Merge pull request #744 from datastax/python-707

fixes for ExponentialReconnectionPolicy
This commit is contained in:
Jim Witschey 2017-05-15 17:16:55 -04:00 committed by GitHub
commit bb485d2ae6
2 changed files with 24 additions and 6 deletions

View File

@ -564,9 +564,17 @@ class ExponentialReconnectionPolicy(ReconnectionPolicy):
self.max_attempts = max_attempts self.max_attempts = max_attempts
def new_schedule(self): def new_schedule(self):
i = 0 i, overflowed = 0, False
while self.max_attempts is None or i < self.max_attempts: while self.max_attempts is None or i < self.max_attempts:
if overflowed:
yield self.max_delay
else:
try:
yield min(self.base_delay * (2 ** i), self.max_delay) yield min(self.base_delay * (2 ** i), self.max_delay)
except OverflowError:
overflowed = True
yield self.max_delay
i += 1 i += 1

View File

@ -887,8 +887,8 @@ class ExponentialReconnectionPolicyTest(unittest.TestCase):
self.assertRaises(ValueError, ExponentialReconnectionPolicy, 1, 2,-1) self.assertRaises(ValueError, ExponentialReconnectionPolicy, 1, 2,-1)
def test_schedule_no_max(self): def test_schedule_no_max(self):
base_delay = 2 base_delay = 2.0
max_delay = 100 max_delay = 100.0
test_iter = 10000 test_iter = 10000
policy = ExponentialReconnectionPolicy(base_delay=base_delay, max_delay=max_delay, max_attempts=None) policy = ExponentialReconnectionPolicy(base_delay=base_delay, max_delay=max_delay, max_attempts=None)
sched_slice = list(islice(policy.new_schedule(), 0, test_iter)) sched_slice = list(islice(policy.new_schedule(), 0, test_iter))
@ -897,8 +897,8 @@ class ExponentialReconnectionPolicyTest(unittest.TestCase):
self.assertEqual(len(sched_slice), test_iter) self.assertEqual(len(sched_slice), test_iter)
def test_schedule_with_max(self): def test_schedule_with_max(self):
base_delay = 2 base_delay = 2.0
max_delay = 100 max_delay = 100.0
max_attempts = 64 max_attempts = 64
policy = ExponentialReconnectionPolicy(base_delay=base_delay, max_delay=max_delay, max_attempts=max_attempts) policy = ExponentialReconnectionPolicy(base_delay=base_delay, max_delay=max_delay, max_attempts=max_attempts)
schedule = list(policy.new_schedule()) schedule = list(policy.new_schedule())
@ -911,6 +911,16 @@ class ExponentialReconnectionPolicyTest(unittest.TestCase):
else: else:
self.assertEqual(delay, max_delay) self.assertEqual(delay, max_delay)
def test_schedule_exactly_one_attempt(self):
base_delay = 2.0
max_delay = 100.0
max_attempts = 1
policy = ExponentialReconnectionPolicy(
base_delay=base_delay, max_delay=max_delay, max_attempts=max_attempts
)
self.assertEqual(len(list(policy.new_schedule())), 1)
ONE = ConsistencyLevel.ONE ONE = ConsistencyLevel.ONE