File: test_backoff.py

package info (click to toggle)
python-redis 6.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,432 kB
  • sloc: python: 60,318; sh: 179; makefile: 128
file content (18 lines) | stat: -rw-r--r-- 616 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from unittest.mock import Mock

import pytest

from redis.backoff import ExponentialWithJitterBackoff


def test_exponential_with_jitter_backoff(monkeypatch: pytest.MonkeyPatch) -> None:
    mock_random = Mock(side_effect=[0.25, 0.5, 0.75, 1.0, 0.9])
    monkeypatch.setattr("random.random", mock_random)

    bo = ExponentialWithJitterBackoff(cap=5, base=1)

    assert bo.compute(0) == 0.25  # min(5, 0.25*2^0)
    assert bo.compute(1) == 1.0  # min(5, 0.5*2^1)
    assert bo.compute(2) == 3.0  # min(5, 0.75*2^2)
    assert bo.compute(3) == 5.0  # min(5, 1*2^3)
    assert bo.compute(4) == 5.0  # min(5, 0.9*2^4)