|
17 | 17 | from kombu.utils.json import register_type |
18 | 18 |
|
19 | 19 | from taskbadger import Action, EmailIntegration, StatusEnum |
20 | | -from taskbadger.celery import Task |
| 20 | +from taskbadger.celery import Task, task_publish_handler |
21 | 21 | from taskbadger.mug import Badger |
22 | 22 | from tests.utils import task_for_test |
23 | 23 |
|
@@ -280,6 +280,23 @@ def add_with_task_args_in_decorator(self, a, b): |
280 | 280 | ) |
281 | 281 |
|
282 | 282 |
|
| 283 | +@pytest.mark.usefixtures("_bind_settings") |
| 284 | +def test_celery_publish_handler_task_not_registered_locally(): |
| 285 | + """before_task_publish can fire in a process that never imported the task |
| 286 | + being sent (e.g. a producer-only service), so `celery.current_app.tasks.get` |
| 287 | + returns None. The handler should still create the TaskBadger task without |
| 288 | + blowing up trying to call `.update_state()` on the missing task class.""" |
| 289 | + |
| 290 | + with mock.patch("taskbadger.celery.create_task_safe") as create: |
| 291 | + create.return_value = task_for_test() |
| 292 | + |
| 293 | + headers = {"id": "abc123", "task": "unregistered.task", "taskbadger_track": True} |
| 294 | + task_publish_handler(sender="unregistered.task", headers=headers, body=[[], {}, {}]) |
| 295 | + |
| 296 | + create.assert_called_once() |
| 297 | + assert headers["taskbadger_task_id"] == create.return_value.id |
| 298 | + |
| 299 | + |
283 | 300 | @pytest.mark.usefixtures("_bind_settings") |
284 | 301 | def test_celery_task_custom_queue(celery_session_app, celery_session_worker): |
285 | 302 | @celery_session_app.task(bind=True, base=Task) |
|
0 commit comments