diff options
Diffstat (limited to 'tests/tasks/test_sender.py')
| -rw-r--r-- | tests/tasks/test_sender.py | 130 |
1 files changed, 123 insertions, 7 deletions
diff --git a/tests/tasks/test_sender.py b/tests/tasks/test_sender.py index ecfc27a..6b189ad 100644 --- a/tests/tasks/test_sender.py +++ b/tests/tasks/test_sender.py @@ -4,7 +4,7 @@ import logging import uuid -from datetime import UTC, datetime +from datetime import UTC, datetime, timedelta from types import ModuleType from unittest.mock import MagicMock, Mock @@ -34,6 +34,8 @@ def create_sent_webmention( endpoint: str | None = None, response_status: int | None = None, status_url: str | None = None, + attempt_count: int = 0, + next_retry_at: datetime | None = None, ) -> uuid.UUID: now = datetime.now(UTC) @@ -58,6 +60,8 @@ def create_sent_webmention( endpoint=endpoint, response_status=response_status, status_url=status_url, + attempt_count=attempt_count, + next_retry_at=next_retry_at, ) source.sent_webmentions.append(webmention) @@ -686,6 +690,8 @@ def test_send_webmention_persists_success( assert webmention.status_url == STATUS_URL assert webmention.last_attempted_at is not None assert webmention.last_sent_at is not None + assert webmention.attempt_count == 1 + assert webmention.next_retry_at is None def test_send_webmention_marks_unsupported_target_processed( @@ -754,7 +760,7 @@ def test_send_webmention_persists_permanent_failure( assert webmention.status_url is None -def test_send_webmention_persists_temporary_failure_and_reraises( +def test_send_webmention_persists_temporary_failure_with_backoff( app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: identifier = create_sent_webmention( @@ -768,11 +774,7 @@ def test_send_webmention_persists_temporary_failure_and_reraises( mock_sender_requests(sender_module, monkeypatch, result=(503, None)) - with pytest.raises( - sender_module.TemporarySenderError, - match="Webmention endpoint returned HTTP 503", - ): - sender_module.send_webmention.call_local(identifier) + sender_module.send_webmention.call_local(identifier) with app.app_context(): webmention = db.session.get(SentWebmention, identifier) @@ -785,6 +787,120 @@ def test_send_webmention_persists_temporary_failure_and_reraises( assert webmention.endpoint == ENDPOINT_URL assert webmention.response_status == 503 assert webmention.status_url is None + assert webmention.attempt_count == 1 + assert webmention.next_retry_at is not None + assert webmention.last_attempted_at is not None + # SQLite drops timezone info when loading timestamp columns. + assert ( + webmention.next_retry_at.replace(tzinfo=None) + - webmention.last_attempted_at.replace(tzinfo=None) + ).total_seconds() == 3600 + + +def test_retry_delay_is_exponential_and_capped( + app: Flask, sender_module: ModuleType +) -> None: + with app.app_context(): + assert [sender_module.retry_delay(n).total_seconds() for n in range(1, 8)] == [ + 3600, + 7200, + 14400, + 28800, + 57600, + 86400, + 86400, + ] + + +def test_temporary_failure_backoff_suppresses_duplicate_tasks_and_caps_attempts( + app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + app.config["SEND_MAX_ATTEMPTS"] = 3 + identifier = create_sent_webmention(app) + _, _, discover, post = mock_sender_requests( + sender_module, monkeypatch, result=(503, None) + ) + + for attempt in range(1, 4): + sender_module.send_webmention.call_local(identifier) + with app.app_context(): + webmention = db.session.get(SentWebmention, identifier) + assert webmention is not None + assert webmention.attempt_count == attempt + assert webmention.status == SentWebmentionStatus.FAILED + assert webmention.processed_revision == (1 if attempt == 3 else None) + if attempt < 3: + assert webmention.next_retry_at is not None + sender_module.send_webmention.call_local(identifier) + assert webmention.attempt_count == attempt + webmention.next_retry_at = datetime.now(UTC) - timedelta(seconds=1) + db.session.commit() + else: + assert webmention.next_retry_at is None + + # A terminal failure stays terminal across scans or stray queued tasks. + sender_module.send_webmention.call_local(identifier) + assert discover.call_count == 3 + assert post.call_count == 3 + + +def test_sender_does_not_retry_before_due_even_after_restart( + app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + identifier = create_sent_webmention( + app, + attempt_count=2, + next_retry_at=datetime.now(UTC) + timedelta(days=1), + ) + httpx_client, _, discover, _ = mock_sender_requests(sender_module, monkeypatch) + sender_module.send_webmention.call_local(identifier) + httpx_client.assert_not_called() + discover.assert_not_called() + with app.app_context(): + webmention = db.session.get(SentWebmention, identifier) + assert webmention is not None + assert webmention.attempt_count == 2 + assert webmention.processed_revision is None + + +def test_sender_finishes_abandoned_final_attempt_without_new_request( + app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + app.config["SEND_MAX_ATTEMPTS"] = 2 + identifier = create_sent_webmention( + app, + attempt_count=2, + next_retry_at=datetime.now(UTC) - timedelta(seconds=1), + status=SentWebmentionStatus.FAILED, + failure_reason="Network unreachable", + ) + httpx_client, _, _, _ = mock_sender_requests(sender_module, monkeypatch) + sender_module.send_webmention.call_local(identifier) + httpx_client.assert_not_called() + with app.app_context(): + webmention = db.session.get(SentWebmention, identifier) + assert webmention is not None + assert webmention.processed_revision == 1 + assert webmention.failure_reason == "Network unreachable" + assert webmention.next_retry_at is None + + +def test_sender_request_error_uses_same_retry_budget( + app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + app.config["SEND_MAX_ATTEMPTS"] = 1 + identifier = create_sent_webmention(app) + _, _, discover, _ = mock_sender_requests(sender_module, monkeypatch) + discover.side_effect = httpx.ConnectError("DNS lookup failed") + sender_module.send_webmention.call_local(identifier) + with app.app_context(): + webmention = db.session.get(SentWebmention, identifier) + assert webmention is not None + assert webmention.status == SentWebmentionStatus.FAILED + assert webmention.failure_reason == "DNS lookup failed" + assert webmention.processed_revision == 1 + assert webmention.attempt_count == 1 + assert webmention.next_retry_at is None def test_send_webmention_ignores_processed_revision( |
