CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 10s
CI / test (push) Successful in 1m46s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 5s
The task registry was module-global, so tasks registered by an app with federation enabled would also run under an app without it (and break the "federation off registers nothing" guarantee depending on order). Tasks now live on each app (app.extensions["worker_tasks"]); register_task/register_periodic take the app, and the worker runs the current app's tasks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
import pytest
|
|
|
|
from scrobbler import worker
|
|
|
|
|
|
class _Registry:
|
|
"""The worker module, bound to the test app, with an emptied task registry."""
|
|
|
|
def __init__(self, app):
|
|
self.app = app
|
|
|
|
def register_task(self, name, fn):
|
|
worker.register_task(self.app, name, fn)
|
|
|
|
def register_periodic(self, name, every, fn):
|
|
worker.register_periodic(self.app, name, every, fn)
|
|
|
|
def run(self, **kwargs):
|
|
worker.run(**kwargs)
|
|
|
|
|
|
@pytest.fixture
|
|
def tasks(app):
|
|
"""An empty task registry for the test app, restored afterwards."""
|
|
registry = worker.tasks(app)
|
|
saved = dict(registry)
|
|
registry.clear()
|
|
yield _Registry(app)
|
|
registry.clear()
|
|
registry.update(saved)
|
|
|
|
|
|
def test_imports_are_registered_by_the_app(app):
|
|
assert {"imports", "imports.requeue_stalled"} <= set(worker.registered(app))
|
|
|
|
|
|
def test_apps_have_their_own_tasks(app, fed_app):
|
|
worker.register_task(fed_app, "only-on-fed-app", lambda: False)
|
|
try:
|
|
assert "only-on-fed-app" not in worker.registered(app)
|
|
finally:
|
|
worker.tasks(fed_app).pop("only-on-fed-app")
|
|
|
|
|
|
def test_queue_tasks_run_until_there_is_no_work(tasks, metric_delta):
|
|
work = [3]
|
|
|
|
def drain():
|
|
if work[0] == 0:
|
|
return False
|
|
work[0] -= 1
|
|
return True
|
|
|
|
worked = metric_delta("scrobbler_worker_tasks_total", task="drain", outcome="worked")
|
|
tasks.register_task("drain", drain)
|
|
tasks.run(once=True)
|
|
assert work == [0]
|
|
assert worked.delta == 3
|
|
|
|
|
|
def test_periodic_tasks_keep_their_interval(tasks):
|
|
calls = []
|
|
tasks.register_periodic("tick", 3600, lambda: calls.append(1))
|
|
busy = [2]
|
|
tasks.register_task("busy", lambda: busy.__setitem__(0, busy[0] - 1) or busy[0] >= 0)
|
|
tasks.run(once=True) # several passes while "busy" has work
|
|
assert calls == [1] # ran at start, then not again within the hour
|
|
|
|
|
|
def test_a_failing_task_does_not_stop_the_others(tasks, metric_delta):
|
|
done = []
|
|
|
|
def broken():
|
|
raise RuntimeError("bug")
|
|
|
|
errors = metric_delta("scrobbler_worker_tasks_total", task="broken", outcome="error")
|
|
tasks.register_task("broken", broken)
|
|
tasks.register_task("fine", lambda: done.append(1) and False)
|
|
tasks.run(once=True)
|
|
assert done == [1]
|
|
assert errors.delta == 1
|
|
|
|
|
|
def test_worker_cli_lists_and_runs_tasks(app, tasks):
|
|
ran = []
|
|
tasks.register_task("cli-test", lambda: ran.append(1) and False)
|
|
result = app.test_cli_runner().invoke(args=["worker", "--once", "--no-metrics"])
|
|
assert result.exit_code == 0, result.output
|
|
assert ran == [1]
|
|
alias = app.test_cli_runner().invoke(args=["imports", "worker", "--once", "--no-metrics"])
|
|
assert alias.exit_code == 0, alias.output
|