|
13 | 13 |
|
14 | 14 | import celery |
15 | 15 | import pytest |
| 16 | +from kombu.utils.json import register_type |
16 | 17 |
|
17 | 18 | from taskbadger import Action, EmailIntegration, StatusEnum |
18 | 19 | from taskbadger.celery import Task |
@@ -179,6 +180,41 @@ def add_with_task_args(self, a, b, c=0): |
179 | 180 | ) |
180 | 181 |
|
181 | 182 |
|
| 183 | +def test_celery_record_task_args_custom_serialization(celery_session_app, celery_session_worker, bind_settings): |
| 184 | + class A: |
| 185 | + def __init__(self, a, b): |
| 186 | + self.a = a |
| 187 | + self.b = b |
| 188 | + |
| 189 | + register_type(A, "A", lambda o: [o.a, o.b], lambda o: A(*o)) |
| 190 | + |
| 191 | + @celery_session_app.task(bind=True, base=Task) |
| 192 | + def add_with_task_args(self, a): |
| 193 | + assert self.taskbadger_task is not None |
| 194 | + return a.a + a.b |
| 195 | + |
| 196 | + celery_session_worker.reload() |
| 197 | + |
| 198 | + with ( |
| 199 | + mock.patch("taskbadger.celery.create_task_safe") as create, |
| 200 | + mock.patch("taskbadger.celery.update_task_safe"), |
| 201 | + mock.patch("taskbadger.celery.get_task"), |
| 202 | + ): |
| 203 | + create.return_value = task_for_test() |
| 204 | + |
| 205 | + result = add_with_task_args.delay( |
| 206 | + A(2, 2), |
| 207 | + taskbadger_record_task_args=True, |
| 208 | + ) |
| 209 | + assert result.get(timeout=10, propagate=True) == 4 |
| 210 | + |
| 211 | + create.assert_called_once_with( |
| 212 | + "tests.test_celery.add_with_task_args", |
| 213 | + data={"celery_task_args": [{"__type__": "A", "__value__": [2, 2]}], "celery_task_kwargs": {}}, |
| 214 | + status=StatusEnum.PENDING, |
| 215 | + ) |
| 216 | + |
| 217 | + |
182 | 218 | def test_celery_task_with_args_in_decorator(celery_session_app, celery_session_worker, bind_settings): |
183 | 219 | @celery_session_app.task( |
184 | 220 | bind=True, |
|
0 commit comments