From 651a453ce7012a203a6a8493c9fb719315ca6a95 Mon Sep 17 00:00:00 2001 From: whonore Date: Fri, 11 Sep 2020 12:13:18 -0400 Subject: [PATCH] Compute exponential by multiplying previous result --- backoff/_wait_gen.py | 6 +++--- tests/test_wait_gen.py | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/backoff/_wait_gen.py b/backoff/_wait_gen.py index 33b37b8..1b54dde 100644 --- a/backoff/_wait_gen.py +++ b/backoff/_wait_gen.py @@ -22,12 +22,12 @@ def expo( """ # Advance past initial .send() call yield # type: ignore[misc] - n = 0 + base_n: float = 1 while True: - a = factor * base ** n + a = factor * base_n if max_value is None or a < max_value: yield a - n += 1 + base_n *= base else: yield max_value diff --git a/tests/test_wait_gen.py b/tests/test_wait_gen.py index a658712..bebe58b 100644 --- a/tests/test_wait_gen.py +++ b/tests/test_wait_gen.py @@ -67,6 +67,14 @@ def test_expo_max_value(): assert expect == next(gen) +def test_expo_max_value_factor(): + gen = backoff.expo(factor=3, max_value=2 ** 4) + gen.send(None) + expected = [3 * 1, 3 * 2, 3 * 4, 16, 16, 16, 16] + for expect in expected: + assert expect == next(gen) + + def test_fibo(): gen = backoff.fibo() gen.send(None)