|
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 | from huey import __version__ as HUEY_VERSION |
5 | | -from huey import group |
6 | 5 | from huey.api import MemoryHuey, Result |
7 | 6 | from huey.exceptions import RetryTask |
8 | 7 |
|
|
12 | 11 |
|
13 | 12 | HUEY_VERSION = parse_version(HUEY_VERSION) |
14 | 13 |
|
| 14 | +try: |
| 15 | + from huey.api import chord, group |
| 16 | +except ImportError: |
| 17 | + chord = None |
| 18 | + group = None |
15 | 19 |
|
16 | 20 | @pytest.fixture |
17 | 21 | def init_huey(sentry_init): |
@@ -225,6 +229,7 @@ def propagated_trace_task(): |
225 | 229 | assert event["contexts"]["trace"]["origin"] == "auto.queue.huey" |
226 | 230 |
|
227 | 231 |
|
| 232 | +@pytest.mark.skipif(HUEY_VERSION < (3, 0), reason="group was added in 3.0") |
228 | 233 | def test_huey_enqueue_group(init_huey, capture_events): |
229 | 234 | huey = init_huey() |
230 | 235 |
|
@@ -263,8 +268,65 @@ def task2(): |
263 | 268 | assert spans[2]["description"] == "task2" |
264 | 269 |
|
265 | 270 | # Consumer transaction assertions (one per task) |
266 | | - consumer_events = sorted(events[1:], key=lambda e: e["transaction"]) |
267 | | - for i, (consumer_event, expected_name) in enumerate( |
| 271 | + consumer_events = events[1:] |
| 272 | + for _, (consumer_event, expected_name) in enumerate( |
| 273 | + zip(consumer_events, ["task1", "task2"]) |
| 274 | + ): |
| 275 | + assert consumer_event["type"] == "transaction" |
| 276 | + assert consumer_event["transaction"] == expected_name |
| 277 | + assert consumer_event["transaction_info"] == {"source": "task"} |
| 278 | + assert consumer_event["contexts"]["trace"]["op"] == "queue.task.huey" |
| 279 | + assert consumer_event["contexts"]["trace"]["origin"] == "auto.queue.huey" |
| 280 | + assert consumer_event["contexts"]["trace"]["status"] == "ok" |
| 281 | + assert consumer_event["contexts"]["trace"]["trace_id"] == transaction.trace_id |
| 282 | + assert "huey_task_id" in consumer_event["tags"] |
| 283 | + assert consumer_event["tags"]["huey_task_retry"] is False |
| 284 | + |
| 285 | + |
| 286 | +@pytest.mark.skipif(HUEY_VERSION < (3, 0), reason="chord was added in 3.0") |
| 287 | +def test_huey_enqueue_chord(init_huey, capture_events): |
| 288 | + huey = init_huey() |
| 289 | + |
| 290 | + events = capture_events() |
| 291 | + |
| 292 | + @huey.task() |
| 293 | + def task1(): |
| 294 | + pass |
| 295 | + |
| 296 | + @huey.task() |
| 297 | + def task2(results): |
| 298 | + pass |
| 299 | + |
| 300 | + with start_transaction() as transaction: |
| 301 | + huey.enqueue(chord([task1.s()], task2.s())) |
| 302 | + |
| 303 | + for _ in range(2): |
| 304 | + task = huey.dequeue() |
| 305 | + huey.execute(task) |
| 306 | + |
| 307 | + assert len(events) == 3 |
| 308 | + |
| 309 | + # Enqueue spans |
| 310 | + producer_event = events[0] |
| 311 | + assert producer_event["contexts"]["trace"]["trace_id"] == transaction.trace_id |
| 312 | + assert producer_event["contexts"]["trace"]["origin"] == "manual" |
| 313 | + |
| 314 | + spans = producer_event["spans"] |
| 315 | + assert len(spans) == 2 |
| 316 | + assert spans[0]["op"] == "queue.submit.huey" |
| 317 | + assert spans[0]["description"] == "Huey Chord" |
| 318 | + assert spans[1]["op"] == "queue.submit.huey" |
| 319 | + assert spans[1]["description"] == "task1" |
| 320 | + |
| 321 | + task1_event = events[1] |
| 322 | + # Confirm the first task enqueued the chord callback |
| 323 | + task1_spans = task1_event["spans"] |
| 324 | + assert len(task1_spans) == 1 |
| 325 | + assert task1_spans[0]["op"] == "queue.submit.huey" |
| 326 | + assert task1_spans[0]["description"] == "task2" |
| 327 | + |
| 328 | + consumer_events = events[1:] |
| 329 | + for _, (consumer_event, expected_name) in enumerate( |
268 | 330 | zip(consumer_events, ["task1", "task2"]) |
269 | 331 | ): |
270 | 332 | assert consumer_event["type"] == "transaction" |
|
0 commit comments