aboutsummaryrefslogtreecommitdiff
path: root/tests/tasks
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/tasks/test_extension.py61
-rw-r--r--tests/tasks/test_notifications.py18
-rw-r--r--tests/tasks/test_scanner.py3
3 files changed, 79 insertions, 3 deletions
diff --git a/tests/tasks/test_extension.py b/tests/tasks/test_extension.py
index 19f1191..a2495e8 100644
--- a/tests/tasks/test_extension.py
+++ b/tests/tasks/test_extension.py
@@ -8,7 +8,7 @@ from typing import Any
import huey as huey_package
import pytest
-from flask import Flask, has_app_context
+from flask import Flask, current_app, has_app_context
from huey import crontab
from webmentions_ssg.tasks.extension import Huey
@@ -94,6 +94,65 @@ def test_task_raises_without_app(app: Flask, periodic: bool) -> None:
task.call_local()
+@pytest.mark.parametrize("periodic", [False, True], ids=["task", "periodic-task"])
+def test_task_declaration_before_init_and_rebind_after_init(periodic: bool) -> None:
+ """Imported task handles do not capture the first application backend."""
+ queue = Huey()
+ observed_apps: list[str] = []
+
+ @task_decorator(queue, periodic)
+ def identify_app() -> str:
+ name = current_app.import_name
+ observed_apps.append(name)
+ return name
+
+ assert identify_app.__name__ == "identify_app"
+ with pytest.raises(RuntimeError, match="Huey has not been initialized"):
+ identify_app.call_local()
+
+ first = Flask("first")
+ first.config["TESTING"] = True
+ queue.init_app(first)
+ assert identify_app.call_local() == "first"
+ # Huey discards periodic-task results, even in immediate mode. Verify
+ # execution through a side effect as well as the returned result.
+ assert identify_app().get() == (None if periodic else "first")
+ assert observed_apps == ["first", "first"]
+
+ previous_backend = queue.huey
+ second = Flask("second")
+ second.config["TESTING"] = True
+ queue.init_app(second)
+
+ assert queue.huey is not previous_backend
+ assert identify_app.call_local() == "second"
+ assert identify_app().get() == (None if periodic else "second")
+ assert observed_apps == ["first", "first", "second", "second"]
+ # The new broker must recognize the registered task when deserializing work.
+ queued = identify_app.s()
+ restored = queue.huey.deserialize_task(queue.huey.serialize_task(queued))
+ assert type(restored) is type(queued)
+
+
+def test_multiple_deferred_tasks_registered_on_initialization() -> None:
+ queue = Huey()
+
+ @queue.task(retries=2, retry_delay=50)
+ def first(value: int) -> int:
+ return value + 1
+
+ @queue.task()
+ def second(value: int) -> int:
+ return first.call_local(value) + 1
+
+ app = Flask("tasks")
+ app.config["TESTING"] = True
+ queue.init_app(app)
+
+ assert second(2).get() == 4
+ assert first.call_local(2) == 3
+
+
def test_getattr_delegates_to_huey(app: Flask) -> None:
huey = Huey(app)
assert huey.immediate is True
diff --git a/tests/tasks/test_notifications.py b/tests/tasks/test_notifications.py
index 0254b19..f87fd47 100644
--- a/tests/tasks/test_notifications.py
+++ b/tests/tasks/test_notifications.py
@@ -9,8 +9,10 @@ from unittest.mock import patch
import pytest
from flask import Flask
+from huey.exceptions import TaskLockedException
from webmentions_ssg import db
+from webmentions_ssg.extensions import huey
from webmentions_ssg.models import ReceivedWebmention
@@ -128,3 +130,19 @@ def test_digest_skips_when_no_new_mentions(
notifications.send_webmention_digest()
send.assert_not_called()
+
+
+def test_digest_lock_uses_current_backend(
+ notifications: ModuleType, app: Flask
+) -> None:
+ """A digest imported before reinitialization uses the new backend's lock."""
+ with app.app_context():
+ previous_backend = huey.huey
+ huey.init_app(app)
+ assert huey.huey is not previous_backend
+
+ with (
+ huey.lock_task("send-webmention-digest"),
+ pytest.raises(TaskLockedException),
+ ):
+ notifications.send_webmention_digest()
diff --git a/tests/tasks/test_scanner.py b/tests/tasks/test_scanner.py
index d11b3f2..c0b9223 100644
--- a/tests/tasks/test_scanner.py
+++ b/tests/tasks/test_scanner.py
@@ -42,8 +42,7 @@ def test_manual_scan_retries_when_periodic_scan_holds_lock(
app: Flask, scanner_module: ModuleType
) -> None:
with app.app_context():
- # Import-time decorators previously captured an old backend here,
- # making this test pass or fail depending on module import order.
+ # A task imported earlier must still use the reinitialized backend.
previous_backend = scanner_module.huey.huey
scanner_module.huey.init_app(app)
assert scanner_module.huey.huey is not previous_backend