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:
objectThe 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
SmarterTestBaseor 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:
- 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
smarterpackage’s directory, which holds every app.- Return type:
- Returns:
The directory.
- tasks_package: ModuleType
The app’s
taskspackage.
- 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.
- class smarter.lib.unittest.queued_tasks.QueuedTasksPatcher(queue_module)[source]
Bases:
objectPatch
delayandapply_asyncon every task that an app’s queue module holds, and record their calls.- Parameters:
queue_module (
ModuleType) – The app’stasks/queue.py.
- stand_ins
A stand-in per task, by its function’s name, whose
delayandapply_asyncrecord the calls.
- 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 wholesmarterpackage.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:
- Returns:
path:line taskof 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
delayorapply_async.
- 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’stasks/queue.py, e.g.smarter.apps.llmclient.tasks.queue.- Return type:
- 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 ofQUEUE_FUNCTIONSwith the task as its first argument. The module is parsed, not searched, so the wordstask.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, ...).