Queued Celery Tasks in Tests

Keep an app’s Celery tasks from reaching a worker, for a whole test class (ADR-030, rule 10).

Celery isn’t eager in tests: .delay() or .apply_async() sends a task to a real worker, which holds real credentials. Each app queues its tasks from one module, its tasks/queue.py, so the tasks that module holds are every task that anything queues. QueuedTasksPatcher patches delay and apply_async on each of them, on the task objects, so that a call is caught wherever it comes from, and records each call on a stand-in per task.

Each app’s test mixin is a thin wrapper, e.g. smarter.apps.llmclient.tests.base_classes.TestLLMClientQueuedTasksMixin: it starts the patcher in setUpClass, stops it in tearDownClass, and names its own attribute, so that two apps’ mixins can share a test class.

smarter.lib.unittest.queued_tasks.QUEUE_FUNCTIONS = ('delay_through_breaker',)

The functions that queue the task they are given as their first argument.

smarter.lib.celery_conf.publish.delay_through_breaker() queues a task through the broker’s circuit breaker.

('delay_through_breaker', )
smarter.lib.unittest.queued_tasks.QUEUE_METHODS = ('delay', 'apply_async')

The methods of a Celery task that queue it.

('delay', 'apply_async')
class smarter.lib.unittest.queued_tasks.QueueConventionTests[source]

Bases: object

The tests that keep an app to ADR-030’s rule 10: its tasks are queued by its tasks/queue.py, and nothing else.

Mix it into an app’s test class, after SmarterTestBase or a class derived from it, and set the class attributes. It is not a test case itself, so it runs only where it is mixed in.

app_directory()[source]

Return the app’s directory, the parent of its tasks package.

Return type:

Path

Returns:

The directory.

not_queued: frozenset[str] = frozenset({})

The app’s tasks that nothing queues, e.g. those that only Celery Beat sends, by name.

queue_module: ModuleType

The app’s tasks/queue.py.

queued_task_names: dict[str, str]

The Celery name of each task that queue.py queues, by its function’s name, which waiting messages use.

smarter_package()[source]

Return the smarter package’s directory, which holds every app.

Return type:

Path

Returns:

The directory.

task_name_prefix: str

The start of the app’s tasks’ Celery names, with its trailing dot.

tasks_package: ModuleType

The app’s tasks package.

test_every_queued_task_has_a_queue_function()[source]

Each of the app’s registered tasks is held by queue.py, except those that nothing queues.

test_no_other_app_queues_its_tasks()[source]

No module of another app calls .delay() or .apply_async() on one of the app’s tasks.

test_nothing_in_the_app_queues_outside_queue_py()[source]

No module of the app, its tasks included, calls .delay() or .apply_async(), except queue.py.

test_task_names_are_kept()[source]

Each queued task keeps the Celery name that messages already waiting in Redis use.

test_the_package_exports_no_task()[source]

The tasks package exports the queue functions, never a task, so that nothing is tempted to call .delay().

class smarter.lib.unittest.queued_tasks.QueuedTasksPatcher(queue_module)[source]

Bases: object

Patch delay and apply_async on every task that an app’s queue module holds, and record their calls.

Parameters:

queue_module (ModuleType) – The app’s tasks/queue.py.

__init__(queue_module)[source]
forget()[source]

Forget every call recorded so far.

Return type:

None

stand_ins

A stand-in per task, by its function’s name, whose delay and apply_async record the calls.

start()[source]

Patch every task.

Return type:

dict[str, MagicMock]

Returns:

The stand-ins, by task.

stop()[source]

Restore every task.

Return type:

None

smarter.lib.unittest.queued_tasks.calls_that_queue(directory, allowed, root, task_names=None)[source]

Return each call, under a directory, outside the allowed modules, the tests and the migrations, that queues a task.

Parameters:
  • directory (Path) – The directory to search, e.g. an app’s, or the whole smarter package.

  • allowed (Collection[Path]) – The modules that may queue, e.g. the queue modules.

  • root (Path) – The directory that the returned paths are relative to.

  • task_names (Collection[str] | None) – Only the calls that queue one of these tasks, by its function’s name. All of them if None.

Return type:

list[str]

Returns:

path:line task of each call, e.g. apps/llmclient/receivers.py:71 deploy_default_api.

smarter.lib.unittest.queued_tasks.queued_calls(stand_in)[source]

Return the keyword arguments of each time a task was queued, whether with delay or apply_async.

Parameters:

stand_in (MagicMock) – The task’s stand-in, from QueuedTasksPatcher.stand_ins.

Return type:

list[dict[str, Any]]

Returns:

The arguments of each call, in order, e.g. [{"hosted_zone_id": "Z1", ...}].

smarter.lib.unittest.queued_tasks.queued_tasks_of(queue_module)[source]

Return every Celery task that an app’s queue module holds, by its function’s name.

Parameters:

queue_module (ModuleType) – The app’s tasks/queue.py, e.g. smarter.apps.llmclient.tasks.queue.

Return type:

dict[str, Task]

Returns:

The tasks, e.g. {"deploy_default_api": deploy_default_api, ...}.

smarter.lib.unittest.queued_tasks.queueing_calls(path)[source]

Return each call in a module that queues a Celery task, and the task it queues.

A call is <task>.delay(...), <task>.apply_async(...), or one of QUEUE_FUNCTIONS with the task as its first argument. The module is parsed, not searched, so the words task.delay() in a docstring or a comment don’t count, and neither does a reference to the method that isn’t called, e.g. partial(task.delay, ...).

Parameters:

path (Path) – The module.

Return type:

list[tuple[int, str]]

Returns:

The line and the task’s expression of each call, e.g. (42, "deploy_default_api").

smarter.lib.unittest.queued_tasks.registered_task_functions(task_name_prefix)[source]

Return the function name of every task that Celery registered under a prefix.

Parameters:

task_name_prefix (str) – The start of the tasks’ Celery names, e.g. smarter.apps.llmclient.tasks..

Return type:

list[str]

Returns:

The names, sorted, e.g. ["deploy_default_api", ...].