Skip to content

Commit ffd6e42

Browse files
author
jimcockburn
committed
unit test
1 parent 4c7141f commit ffd6e42

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

tests/test_backoff.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from unittest.mock import Mock
2+
3+
import pytest
4+
5+
from redis.backoff import ExponentialWithJitterBackoff
6+
7+
8+
def test_exponential_with_jitter_backoff(monkeypatch: pytest.MonkeyPatch) -> None:
9+
mock_random = Mock(side_effect=[0.25, 0.5, 0.75, 1.0, 0.9])
10+
monkeypatch.setattr("random.random", mock_random)
11+
12+
bo = ExponentialWithJitterBackoff(cap=5, base=1)
13+
14+
assert bo.compute(0) == 0.25 # min(5, 0.25*2^0)
15+
assert bo.compute(1) == 1.0 # min(5, 0.5*2^1)
16+
assert bo.compute(2) == 3.0 # min(5, 0.75*2^2)
17+
assert bo.compute(3) == 5.0 # min(5, 1*2^3)
18+
assert bo.compute(4) == 5.0 # min(5, 0.9*2^4)

0 commit comments

Comments
 (0)