diff options
Diffstat (limited to 'tests/tasks/test_receiver.py')
| -rw-r--r-- | tests/tasks/test_receiver.py | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/tests/tasks/test_receiver.py b/tests/tasks/test_receiver.py index c795115..44d1cc2 100644 --- a/tests/tasks/test_receiver.py +++ b/tests/tasks/test_receiver.py @@ -35,6 +35,7 @@ def create_webmention( target: str = TARGET_URL, status: str = "received", failure_reason: str | None = None, + recovery_attempts: int = 0, ) -> uuid.UUID: identifier = uuid.uuid7() @@ -45,6 +46,7 @@ def create_webmention( target=target, status=status, failure_reason=failure_reason, + recovery_attempts=recovery_attempts, ) db.session.add(webmention) @@ -669,6 +671,106 @@ def test_reaper_requeues_and_verifies_integration( check.assert_called_once_with(SOURCE_URL, TARGET_URL) +@patch("webmentions_ssg.tasks.receiver.source_mentions_target") +@pytest.mark.parametrize( + "unexpected_error", + [ + pytest.param(httpx.InvalidURL("Invalid IDNA hostname"), id="invalid-url"), + pytest.param( + RuntimeError("Unexpected HTML parser error"), id="unexpected-error" + ), + ], +) +def test_verify_webmention_records_unexpected_error_as_terminal_failure( + source_mentions_target: Mock, + app: Flask, + receiver: ModuleType, + unexpected_error: Exception, +) -> None: + identifier = create_webmention(app, source="http://ex☃mple.test/") + source_mentions_target.side_effect = unexpected_error + + receiver.verify_webmention.call_local(identifier) + + assert get_webmention_state(app, identifier) == ("failed", str(unexpected_error)) + source_mentions_target.assert_called_once() + # Failed rows are not eligible for automatic reaper recovery. + set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1)) + with app.app_context(): + assert receiver.recover_stale_webmentions() == 0 + + +def test_reaper_caps_stale_verification_recoveries( + app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + app.config["VERIFICATION_MAX_RECOVERIES"] = 3 + identifier = create_webmention(app, status="verifying") + enqueue = Mock() + monkeypatch.setattr(receiver, "verify_webmention", enqueue) + + # Simulate workers that repeatedly crash before completing the task. + for attempt in range(3): + set_webmention_updated_at( + app, identifier, datetime.now(UTC) - timedelta(hours=1) + ) + with app.app_context(): + assert receiver.recover_stale_webmentions() == 1 + row = db.session.get(ReceivedWebmention, identifier) + assert row is not None + assert row.status == "received" + assert row.recovery_attempts == attempt + 1 + + set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1)) + with app.app_context(): + assert receiver.recover_stale_webmentions() == 0 + row = db.session.get(ReceivedWebmention, identifier) + assert row is not None + assert row.status == "failed" + assert row.failure_reason == receiver.RECOVERY_EXHAUSTED_REASON + assert row.recovery_attempts == 3 + assert receiver.recover_stale_webmentions() == 0 + assert enqueue.call_count == 3 + + +def test_reaper_caps_received_rows_lost_before_first_task( + app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + app.config["VERIFICATION_MAX_RECOVERIES"] = 1 + identifier = create_webmention(app, status="received", recovery_attempts=1) + set_webmention_updated_at(app, identifier, datetime.now(UTC) - timedelta(hours=1)) + enqueue = Mock() + monkeypatch.setattr(receiver, "verify_webmention", enqueue) + + with app.app_context(): + assert receiver.recover_stale_webmentions() == 0 + assert get_webmention_state(app, identifier) == ( + "failed", + receiver.RECOVERY_EXHAUSTED_REASON, + ) + enqueue.assert_not_called() + + +def test_exhausted_row_cannot_be_revived_by_late_huey_retry( + app: Flask, receiver: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + identifier = create_webmention( + app, + status="failed", + failure_reason=receiver.RECOVERY_EXHAUSTED_REASON, + recovery_attempts=3, + ) + source_mentions_target = Mock(return_value=True) + monkeypatch.setattr(receiver, "source_mentions_target", source_mentions_target) + + receiver.verify_webmention.call_local(identifier) + + assert get_webmention_state(app, identifier) == ( + "failed", + receiver.RECOVERY_EXHAUSTED_REASON, + ) + source_mentions_target.assert_not_called() + + def test_verify_webmention_ignores_unknown_identifier( receiver: ModuleType, caplog: pytest.LogCaptureFixture ) -> None: |
