|
| 1 | +import random |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from sqs_workers.backoff_policies import ( |
| 7 | + ConstantBackoff, |
| 8 | + ExponentialBackoff, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def mock_message(): |
| 14 | + def _make_message(receive_count: int): |
| 15 | + message = MagicMock() |
| 16 | + message.attributes = {"ApproximateReceiveCount": str(receive_count)} |
| 17 | + return message |
| 18 | + |
| 19 | + return _make_message |
| 20 | + |
| 21 | + |
| 22 | +class TestConstantBackoff: |
| 23 | + def test_returns_constant_value(self): |
| 24 | + policy = ConstantBackoff(backoff_value=30) |
| 25 | + message = MagicMock() |
| 26 | + assert policy.get_visibility_timeout(message) == 30 |
| 27 | + |
| 28 | + def test_default_is_zero(self): |
| 29 | + policy = ConstantBackoff() |
| 30 | + message = MagicMock() |
| 31 | + assert policy.get_visibility_timeout(message) == 0 |
| 32 | + |
| 33 | + |
| 34 | +class TestExponentialBackoff: |
| 35 | + def test_default_parameters_backward_compatible(self, mock_message): |
| 36 | + policy = ExponentialBackoff() |
| 37 | + assert policy.base == 2 |
| 38 | + assert policy.min_visibility_timeout == 0 |
| 39 | + assert policy.max_visibility_timeout == 30 * 60 |
| 40 | + assert policy.multiplier == 1 |
| 41 | + |
| 42 | + def test_first_attempt_uses_base(self, mock_message): |
| 43 | + random.seed(42) |
| 44 | + policy = ExponentialBackoff(base=2, min_visibility_timeout=0, multiplier=1) |
| 45 | + timeout = policy.get_visibility_timeout(mock_message(1)) |
| 46 | + assert timeout == 1 |
| 47 | + |
| 48 | + def test_exponential_growth(self, mock_message): |
| 49 | + random.seed(42) |
| 50 | + policy = ExponentialBackoff(base=2, min_visibility_timeout=0, multiplier=1) |
| 51 | + timeouts = [policy.get_visibility_timeout(mock_message(i)) for i in range(1, 6)] |
| 52 | + for i in range(1, len(timeouts)): |
| 53 | + assert timeouts[i] >= timeouts[i - 1] |
| 54 | + |
| 55 | + def test_multiplier_scales_timeout(self, mock_message): |
| 56 | + random.seed(42) |
| 57 | + policy_no_mult = ExponentialBackoff( |
| 58 | + base=2, min_visibility_timeout=0, multiplier=1 |
| 59 | + ) |
| 60 | + timeout_no_mult = policy_no_mult.get_visibility_timeout(mock_message(3)) |
| 61 | + |
| 62 | + random.seed(42) |
| 63 | + policy_with_mult = ExponentialBackoff( |
| 64 | + base=2, min_visibility_timeout=0, multiplier=10 |
| 65 | + ) |
| 66 | + timeout_with_mult = policy_with_mult.get_visibility_timeout(mock_message(3)) |
| 67 | + |
| 68 | + assert timeout_with_mult == pytest.approx(timeout_no_mult * 10, rel=0.2) |
| 69 | + |
| 70 | + def test_min_visibility_timeout_is_floor(self, mock_message): |
| 71 | + random.seed(42) |
| 72 | + policy = ExponentialBackoff(base=2, min_visibility_timeout=60) |
| 73 | + timeout = policy.get_visibility_timeout(mock_message(1)) |
| 74 | + assert timeout >= 60 |
| 75 | + |
| 76 | + def test_max_visibility_timeout_is_ceiling(self, mock_message): |
| 77 | + random.seed(42) |
| 78 | + policy = ExponentialBackoff( |
| 79 | + base=10, min_visibility_timeout=0, max_visbility_timeout=100, multiplier=100 |
| 80 | + ) |
| 81 | + timeout = policy.get_visibility_timeout(mock_message(10)) |
| 82 | + assert timeout <= 100 |
| 83 | + |
| 84 | + def test_multiplier_combined_with_min_timeout(self, mock_message): |
| 85 | + random.seed(42) |
| 86 | + policy = ExponentialBackoff( |
| 87 | + base=2, min_visibility_timeout=10, multiplier=5, max_visbility_timeout=1000 |
| 88 | + ) |
| 89 | + timeout = policy.get_visibility_timeout(mock_message(3)) |
| 90 | + assert timeout >= 10 |
0 commit comments