def test_exponential(): r = Retry(wait_exponential_max=100000) assert r.wait(1, 0) == 2 assert r.wait(2, 0) == 4 assert r.wait(3, 0) == 8 assert r.wait(4, 0) == 16 assert r.wait(5, 0) == 32 assert r.wait(6, 0) == 64
def test_random_sleep_without_min(): r = Retry(wait_random_max=2000) times = set() times.add(r.wait(1, 6546)) times.add(r.wait(1, 6546)) times.add(r.wait(1, 6546)) times.add(r.wait(1, 6546)) # this is kind of non-deterministic... assert len(times) > 1 for t in times: assert t >= 0 assert t <= 2000
def test_exponential_with_max_wait_and_multiplier(): r = Retry(wait_exponential_max=50000, wait_exponential_multiplier=1000) assert r.wait(1, 0) == 2000 assert r.wait(2, 0) == 4000 assert r.wait(3, 0) == 8000 assert r.wait(4, 0) == 16000 assert r.wait(5, 0) == 32000 assert r.wait(6, 0) == 50000 assert r.wait(7, 0) == 50000 assert r.wait(50, 0) == 50000
def test_exponential_with_max_wait(): r = Retry(wait_exponential_max=40) assert r.wait(1, 0) == 2 assert r.wait(2, 0) == 4 assert r.wait(3, 0) == 8 assert r.wait(4, 0) == 16 assert r.wait(5, 0) == 32 assert r.wait(6, 0) == 40 assert r.wait(7, 0) == 40 assert r.wait(50, 0) == 40
def test_incrementing_sleep(): r = Retry(wait_incrementing_start=500, wait_incrementing_increment=100) assert 500 == r.wait(1, 6546) assert 600 == r.wait(2, 6546) assert 700 == r.wait(3, 6546)
def test_fixed_sleep(): r = Retry(wait_fixed=1000) assert 1000 == r.wait(12, 6546)
def test_no_sleep(): r = Retry() assert 0 == r.wait(18, 9879)
def test_stop_func(): r = Retry(stop_func=lambda attempt, delay: attempt == delay) assert not r.stop(1, 3) assert not r.stop(100, 99) assert r.stop(101, 101)
def test_legacy_explicit_stop_type(): Retry(stop="stop_after_attempt")
def test_stop_after_delay(): r = Retry(stop_max_delay=1000) assert not r.stop(2, 999) assert r.stop(2, 1000) assert r.stop(2, 1001)
def test_stop_after_attempt(): r = Retry(stop_max_attempt_number=3) assert not r.stop(2, 6546) assert r.stop(3, 6546) assert r.stop(4, 6546)
def test_never_stop(): r = Retry() assert not r.stop(3, 6546)
def test_wait_func(): r = Retry(wait_func=lambda attempt, delay: attempt * delay) assert r.wait(1, 5) == 5 assert r.wait(2, 11) == 22 assert r.wait(10, 100) == 1000
def test_legacy_explicit_wait_type(): Retry(wait="exponential_sleep")