diff options
| author | Dennis Fink | 2026-10-30 13:38:00 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-30 13:38:00 +0100 |
| commit | 33567a493a18c3a8e17e3715874d49943da494a6 (patch) | |
| tree | 453a85920e0aa17a7fb9895631cbc11a1b3beb88 /tests | |
| parent | f6d96df2c3aecb51ca97426efddb101dce6350d7 (diff) | |
| download | webmentions-ssg-33567a493a18c3a8e17e3715874d49943da494a6.tar.gz webmentions-ssg-33567a493a18c3a8e17e3715874d49943da494a6.zip | |
fix(sender): prevent concurrent duplicate sends
Serialize delivery attempts per Webmention so duplicate queued tasks
cannot send the same revision concurrently or consume additional retry
attempts.
Scope locks by Webmention UUID so unrelated deliveries can still run in
parallel.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/tasks/test_sender.py | 72 |
1 files changed, 69 insertions, 3 deletions
diff --git a/tests/tasks/test_sender.py b/tests/tasks/test_sender.py index 6b189ad..d43f36f 100644 --- a/tests/tasks/test_sender.py +++ b/tests/tasks/test_sender.py @@ -848,9 +848,7 @@ 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), + 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) @@ -903,6 +901,74 @@ def test_sender_request_error_uses_same_retry_budget( assert webmention.next_retry_at is None +def test_send_webmention_skips_duplicate_while_locked( + app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + identifier = create_sent_webmention(app) + _, _, discover, post = mock_sender_requests(sender_module, monkeypatch) + + # Simulate a first worker already sending this Webmention. The second + # worker must not make HTTP requests or consume an attempt. + with sender_module.huey.lock_task(f"send-webmention-{identifier}"): + sender_module.send_webmention.call_local(identifier) + + discover.assert_not_called() + post.assert_not_called() + with app.app_context(): + webmention = db.session.get(SentWebmention, identifier) + assert webmention is not None + assert webmention.attempt_count == 0 + assert webmention.processed_revision is None + + # Once the other worker releases the lock, a queued task can complete. + sender_module.send_webmention.call_local(identifier) + sender_module.send_webmention.call_local(identifier) + discover.assert_called_once() + post.assert_called_once() + + with app.app_context(): + webmention = db.session.get(SentWebmention, identifier) + assert webmention is not None + assert webmention.attempt_count == 1 + assert webmention.processed_revision == 1 + + +def test_send_webmention_lock_is_scoped_to_uuid( + app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch +) -> None: + first_id = create_sent_webmention(app) + other_target = "https://example.com/another-post" + + with app.app_context(): + first = db.session.get(SentWebmention, first_id) + assert first is not None + second = SentWebmention( + source_id=first.source_id, target=other_target, desired_revision=1 + ) + db.session.add(second) + db.session.commit() + second_id = second.uuid + + _, _, discover, post = mock_sender_requests(sender_module, monkeypatch) + + with sender_module.huey.lock_task(f"send-webmention-{first_id}"): + sender_module.send_webmention.call_local(first_id) + sender_module.send_webmention.call_local(second_id) + + discover.assert_called_once() + post.assert_called_once() + assert post.call_args.kwargs["target"] == other_target + + with app.app_context(): + first = db.session.get(SentWebmention, first_id) + second = db.session.get(SentWebmention, second_id) + assert first is not None and second is not None + assert first.attempt_count == 0 + assert first.processed_revision is None + assert second.attempt_count == 1 + assert second.processed_revision == 1 + + def test_send_webmention_ignores_processed_revision( app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: |
