| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import json |
| 4 |
|
| 5 |
from pytest_celery import RESULT_TIMEOUT, CeleryTestWorker |
| 6 |
|
| 7 |
from celery.canvas import StampingVisitor |
| 8 |
from t.integration.tasks import add, identity |
| 9 |
|
| 10 |
|
| 11 |
class test_stamping_visitor: |
| 12 |
def test_callback(self, dev_worker: CeleryTestWorker): |
| 13 |
on_signature_stamp = {"on_signature_stamp": 4} |
| 14 |
no_visitor_stamp = {"no_visitor_stamp": "Stamp without visitor"} |
| 15 |
on_callback_stamp = {"on_callback_stamp": 2} |
| 16 |
link_stamp = { |
| 17 |
**on_signature_stamp, |
| 18 |
**no_visitor_stamp, |
| 19 |
**on_callback_stamp, |
| 20 |
} |
| 21 |
|
| 22 |
class CustomStampingVisitor(StampingVisitor): |
| 23 |
def on_signature(self, sig, **headers) -> dict: |
| 24 |
return on_signature_stamp.copy() |
| 25 |
|
| 26 |
def on_callback(self, callback, **header) -> dict: |
| 27 |
return on_callback_stamp.copy() |
| 28 |
|
| 29 |
stamped_task = identity.si(123).set(queue=dev_worker.worker_queue) |
| 30 |
stamped_task.link( |
| 31 |
add.s(0) |
| 32 |
.stamp(no_visitor_stamp=no_visitor_stamp["no_visitor_stamp"]) |
| 33 |
.set(queue=dev_worker.worker_queue) |
| 34 |
) |
| 35 |
stamped_task.stamp(visitor=CustomStampingVisitor()) |
| 36 |
stamped_task.delay().get(timeout=RESULT_TIMEOUT) |
| 37 |
assert dev_worker.logs().count( |
| 38 |
json.dumps(on_signature_stamp, indent=4, sort_keys=True) |
| 39 |
) |
| 40 |
assert dev_worker.logs().count(json.dumps(link_stamp, indent=4, sort_keys=True)) |