from unittest.mock import MagicMock, patch import pytest from pdp.utils.backoff import get_backoff, get_backoff_with_full_jitter @pytest.mark.parametrize( "base, cap, retry_attempt, expected", [ pytest.param( 1234, 10000, 0, 1234, id="0th attempt", ), pytest.param(1234, 10000, 1, 2468, id="1st attempt"), pytest.param(1234, 10000, 2, 4936, id="2nd attempt"), pytest.param(1234, 10000, 3, 9872, id="3rd attempt"), pytest.param( 1234, 10000, 4, 10000, id="cap is smaller than calculated backoff", ), ], ) def test_get_backoff( base: int, cap: int, retry_attempt: int, expected: int, ) -> None: """Test get_backoff.""" actual = get_backoff(base, cap, retry_attempt) assert actual == expected @patch("pdp.utils.backoff.randint") @patch("pdp.utils.backoff.get_backoff") def test_get_backoff_with_full_jitter( mock_get_backoff: MagicMock, mock_randint: MagicMock, ) -> None: """Test get_backoff_with_full_jitter.""" backoff = 123456 mock_get_backoff.return_value = backoff mock_randint.return_value = 54321 actual = get_backoff_with_full_jitter(1, 2, 3) assert actual == 54.321 mock_get_backoff.assert_called_once_with(1000, 2000, 3) mock_randint.assert_called_once_with(0, backoff)