|
| 1 | +from datetime import UTC, datetime, timedelta |
| 2 | +from email.utils import format_datetime |
| 3 | +from unittest.mock import AsyncMock, patch |
| 4 | + |
1 | 5 | import aiohttp |
2 | 6 | import pytest |
3 | 7 | from aioresponses import aioresponses |
@@ -45,6 +49,143 @@ async def test_timeout_propagation(self, poller_with_session): |
45 | 49 | await poller_with_session.fetch_page("https://example.com/slow", timeout=1) |
46 | 50 |
|
47 | 51 |
|
| 52 | +class TestFetchPageRetryAfter: |
| 53 | + async def test_429_with_integer_retry_after_waits_and_retries(self, poller_with_session): |
| 54 | + url = "https://example.com/throttled" |
| 55 | + with aioresponses() as mocked: |
| 56 | + mocked.get(url, status=429, headers={"Retry-After": "5"}) |
| 57 | + mocked.get(url, body="recovered") |
| 58 | + with patch("mod_polling.poller.asyncio.sleep", new_callable=AsyncMock) as mock_sleep: |
| 59 | + result = await poller_with_session.fetch_page(url) |
| 60 | + assert result == "recovered" |
| 61 | + mock_sleep.assert_any_await(5.0) |
| 62 | + |
| 63 | + async def test_429_with_http_date_retry_after_waits_and_retries(self, poller_with_session): |
| 64 | + url = "https://example.com/throttled-date" |
| 65 | + future = datetime.now(UTC) + timedelta(seconds=30) |
| 66 | + http_date = format_datetime(future, usegmt=True) |
| 67 | + with aioresponses() as mocked: |
| 68 | + mocked.get(url, status=429, headers={"Retry-After": http_date}) |
| 69 | + mocked.get(url, body="recovered") |
| 70 | + with patch("mod_polling.poller.asyncio.sleep", new_callable=AsyncMock) as mock_sleep: |
| 71 | + result = await poller_with_session.fetch_page(url) |
| 72 | + assert result == "recovered" |
| 73 | + slept = [call.args[0] for call in mock_sleep.await_args_list] |
| 74 | + assert any(29 <= v <= 31 for v in slept), f"Expected ~30s sleep, got {slept}" |
| 75 | + |
| 76 | + async def test_429_with_asctime_obs_date_retry_after(self, poller_with_session): |
| 77 | + """RFC 9110 §5.6.7 obs-date allows asctime form, which has no timezone.""" |
| 78 | + url = "https://example.com/throttled-asctime" |
| 79 | + future = datetime.now(UTC) + timedelta(seconds=20) |
| 80 | + asctime_str = future.strftime("%a %b %d %H:%M:%S %Y") |
| 81 | + with aioresponses() as mocked: |
| 82 | + mocked.get(url, status=429, headers={"Retry-After": asctime_str}) |
| 83 | + mocked.get(url, body="recovered") |
| 84 | + with patch("mod_polling.poller.asyncio.sleep", new_callable=AsyncMock) as mock_sleep: |
| 85 | + result = await poller_with_session.fetch_page(url) |
| 86 | + assert result == "recovered" |
| 87 | + slept = [call.args[0] for call in mock_sleep.await_args_list] |
| 88 | + assert any(18 <= v <= 22 for v in slept), f"Expected ~20s sleep, got {slept}" |
| 89 | + |
| 90 | + async def test_429_without_retry_after_uses_exponential_backoff_fallback(self, poller_with_session): |
| 91 | + poller_with_session._host_delay = 0.5 |
| 92 | + url = "https://example.com/throttled-no-header" |
| 93 | + with aioresponses() as mocked: |
| 94 | + mocked.get(url, status=429) |
| 95 | + mocked.get(url, body="recovered") |
| 96 | + with patch("mod_polling.poller.asyncio.sleep", new_callable=AsyncMock) as mock_sleep: |
| 97 | + result = await poller_with_session.fetch_page(url) |
| 98 | + assert result == "recovered" |
| 99 | + # Backoff fires BEFORE the retry, so it's the first sleep call. |
| 100 | + # attempt 0 fallback: min(0.5 * 2^0, 60) = 0.5 |
| 101 | + slept = [c.args[0] for c in mock_sleep.await_args_list] |
| 102 | + assert slept[0] == 0.5, f"Expected backoff 0.5 as first sleep, got: {slept}" |
| 103 | + |
| 104 | + async def test_429_with_garbled_retry_after_uses_exponential_backoff_fallback(self, poller_with_session): |
| 105 | + poller_with_session._host_delay = 0.5 |
| 106 | + url = "https://example.com/throttled-garbled" |
| 107 | + with aioresponses() as mocked: |
| 108 | + mocked.get(url, status=429, headers={"Retry-After": "soon-ish"}) |
| 109 | + mocked.get(url, body="recovered") |
| 110 | + with patch("mod_polling.poller.asyncio.sleep", new_callable=AsyncMock) as mock_sleep: |
| 111 | + result = await poller_with_session.fetch_page(url) |
| 112 | + assert result == "recovered" |
| 113 | + slept = [c.args[0] for c in mock_sleep.await_args_list] |
| 114 | + assert slept[0] == 0.5, f"Expected backoff 0.5 as first sleep, got: {slept}" |
| 115 | + |
| 116 | + async def test_429_fallback_caps_at_60s_with_large_host_delay(self, poller_with_session): |
| 117 | + poller_with_session._host_delay = 1000 |
| 118 | + url = "https://example.com/throttled-big-delay" |
| 119 | + with aioresponses() as mocked: |
| 120 | + mocked.get(url, status=429) |
| 121 | + mocked.get(url, body="recovered") |
| 122 | + with patch("mod_polling.poller.asyncio.sleep", new_callable=AsyncMock) as mock_sleep: |
| 123 | + result = await poller_with_session.fetch_page(url) |
| 124 | + assert result == "recovered" |
| 125 | + # min(1000 * 2^0, 60) = 60 — the cap kicks in |
| 126 | + mock_sleep.assert_any_await(60.0) |
| 127 | + |
| 128 | + async def test_429_then_429_then_success(self, poller_with_session): |
| 129 | + url = "https://example.com/twice-throttled" |
| 130 | + with aioresponses() as mocked: |
| 131 | + mocked.get(url, status=429, headers={"Retry-After": "5"}) |
| 132 | + mocked.get(url, status=429, headers={"Retry-After": "3"}) |
| 133 | + mocked.get(url, body="finally") |
| 134 | + with patch("mod_polling.poller.asyncio.sleep", new_callable=AsyncMock) as mock_sleep: |
| 135 | + result = await poller_with_session.fetch_page(url) |
| 136 | + assert result == "finally" |
| 137 | + mock_sleep.assert_any_await(5.0) |
| 138 | + mock_sleep.assert_any_await(3.0) |
| 139 | + |
| 140 | + async def test_429_three_times_raises_after_two_retries(self, poller_with_session): |
| 141 | + url = "https://example.com/persistently-throttled" |
| 142 | + with aioresponses() as mocked: |
| 143 | + mocked.get(url, status=429, headers={"Retry-After": "1"}) |
| 144 | + mocked.get(url, status=429, headers={"Retry-After": "1"}) |
| 145 | + mocked.get(url, status=429, headers={"Retry-After": "1"}) |
| 146 | + with ( |
| 147 | + patch("mod_polling.poller.asyncio.sleep", new_callable=AsyncMock), |
| 148 | + pytest.raises(NEMPException, match="HTTP 429"), |
| 149 | + ): |
| 150 | + await poller_with_session.fetch_page(url) |
| 151 | + |
| 152 | + async def test_429_exhausted_logs_warning(self, poller_with_session, caplog): |
| 153 | + url = "https://example.com/exhausted" |
| 154 | + with aioresponses() as mocked: |
| 155 | + mocked.get(url, status=429, headers={"Retry-After": "1"}) |
| 156 | + mocked.get(url, status=429, headers={"Retry-After": "1"}) |
| 157 | + mocked.get(url, status=429, headers={"Retry-After": "1"}) |
| 158 | + with ( |
| 159 | + patch("mod_polling.poller.asyncio.sleep", new_callable=AsyncMock), |
| 160 | + caplog.at_level("WARNING", logger="mod_polling.poller"), |
| 161 | + pytest.raises(NEMPException), |
| 162 | + ): |
| 163 | + await poller_with_session.fetch_page(url) |
| 164 | + matching = [ |
| 165 | + r |
| 166 | + for r in caplog.records |
| 167 | + if r.levelname == "WARNING" and url in r.getMessage() and "exhausted" in r.getMessage().lower() |
| 168 | + ] |
| 169 | + messages = [r.getMessage() for r in caplog.records] |
| 170 | + assert matching, f"Expected WARNING log mentioning exhausted attempts, got: {messages}" |
| 171 | + |
| 172 | + async def test_429_logs_cooldown_at_info(self, poller_with_session, caplog): |
| 173 | + url = "https://example.com/throttled-logged" |
| 174 | + with aioresponses() as mocked: |
| 175 | + mocked.get(url, status=429, headers={"Retry-After": "7"}) |
| 176 | + mocked.get(url, body="recovered") |
| 177 | + with ( |
| 178 | + patch("mod_polling.poller.asyncio.sleep", new_callable=AsyncMock), |
| 179 | + caplog.at_level("INFO", logger="mod_polling.poller"), |
| 180 | + ): |
| 181 | + await poller_with_session.fetch_page(url) |
| 182 | + matching = [ |
| 183 | + r for r in caplog.records if r.levelname == "INFO" and url in r.getMessage() and "7" in r.getMessage() |
| 184 | + ] |
| 185 | + messages = [r.getMessage() for r in caplog.records] |
| 186 | + assert matching, f"Expected INFO log mentioning URL and duration, got: {messages}" |
| 187 | + |
| 188 | + |
48 | 189 | class TestFetchJson: |
49 | 190 | async def test_delegates_to_fetch_page(self, poller_with_session): |
50 | 191 | with aioresponses() as mocked: |
|
0 commit comments