fixes for ExponentialReconnectionPolicy

- documentation specified floats for base_delay and max_delay; fix tests
  to match
- fix issue from PYTHON-707
This commit is contained in:
Jim Witschey 2017-04-21 17:16:06 -04:00
parent 3336fe1e4e
commit 4b7330ddf7
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:
yield min(self.base_delay * (2 ** i), self.max_delay) if overflowed:
yield self.max_delay
else:
try:
yield min(self.base_delay * (2 ** i), self.max_delay)
except OverflowError:
overflowed = True
yield self.max_delay
i += 1 i += 1

View File

@ -885,8 +885,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))
@ -895,8 +895,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())
@ -909,6 +909,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