CI / notify (push) Successful in 6s
CI / lint (push) Successful in 18s
CI / test (push) Successful in 5m26s
CI / validate-monitoring (push) Successful in 15s
CI / federation-interop (push) Successful in 1m17s
CI / images (push) Successful in 33s
CI / assets (push) Successful in 9s
CI / python-package (push) Successful in 18s
CI / trigger-deploy (push) Successful in 3s
Shared LOG_LEVEL/LOG_FORMAT (text or json) set up in create_app, per-task activity and duration logs in the worker, and more context on delivery, inbox and import failures. Alembic's fileConfig no longer disables loggers. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
121 lines
3.7 KiB
Python
121 lines
3.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
|
|
|
|
|
|
def test_a_pass_that_did_work_is_logged_with_its_duration(tasks, caplog):
|
|
tasks.register_task("busy", lambda: True)
|
|
with caplog.at_level("INFO", logger="scrobbler.worker"):
|
|
worker.run_once()
|
|
record = next(r for r in caplog.records if r.__dict__.get("task") == "busy")
|
|
assert record.levelname == "INFO"
|
|
assert record.outcome == "worked"
|
|
assert record.duration_ms >= 0
|
|
|
|
|
|
def test_idle_passes_are_only_logged_at_debug(tasks, caplog):
|
|
tasks.register_task("idle", lambda: False)
|
|
with caplog.at_level("INFO", logger="scrobbler.worker"):
|
|
worker.run_once()
|
|
assert not [r for r in caplog.records if r.__dict__.get("task") == "idle"]
|
|
|
|
|
|
def test_failures_log_the_task_name_and_duration(tasks, caplog):
|
|
def boom():
|
|
raise RuntimeError("nope")
|
|
|
|
tasks.register_task("broken", boom)
|
|
with caplog.at_level("ERROR", logger="scrobbler.worker"):
|
|
worker.run_once()
|
|
record = next(r for r in caplog.records if r.__dict__.get("task") == "broken")
|
|
assert record.outcome == "error"
|
|
assert "broken" in record.getMessage()
|