|
9 | 9 | """ |
10 | 10 |
|
11 | 11 | import logging |
| 12 | +import time |
12 | 13 | from unittest import mock |
13 | 14 |
|
14 | 15 | import celery |
|
21 | 22 | from tests.utils import task_for_test |
22 | 23 |
|
23 | 24 |
|
| 25 | +def _wait_for_call_count(mock_obj, expected, timeout=10.0): |
| 26 | + """Wait until ``mock_obj`` has been called ``expected`` times. |
| 27 | +
|
| 28 | + The worker's ``task_success`` signal fires asynchronously, so the final |
| 29 | + status update can lag behind ``result.get()`` returning. Poll for it while |
| 30 | + the mock patch is still active to avoid a race with the ``with`` block exit. |
| 31 | + """ |
| 32 | + deadline = time.monotonic() + timeout |
| 33 | + while mock_obj.call_count < expected and time.monotonic() < deadline: |
| 34 | + time.sleep(0.01) |
| 35 | + |
| 36 | + |
24 | 37 | @pytest.fixture(autouse=True) |
25 | 38 | def _check_log_errors(caplog): |
26 | 39 | yield |
@@ -52,6 +65,7 @@ def add_normal(self, a, b): |
52 | 65 | assert result.taskbadger_task_id == tb_task.id |
53 | 66 | assert result.get_taskbadger_task() is not None |
54 | 67 | assert result.get(timeout=10, propagate=True) == 4 |
| 68 | + _wait_for_call_count(update, 2) |
55 | 69 |
|
56 | 70 | create.assert_called_once() |
57 | 71 | assert get_task.call_count == 2 |
@@ -416,6 +430,7 @@ def task_map_fn(self, a): |
416 | 430 | create.return_value = tb_task |
417 | 431 | result = map_canvas.delay() |
418 | 432 | assert result.get(timeout=10, propagate=True) == [0, 2, 4, 6, 8] |
| 433 | + _wait_for_call_count(update, 2) |
419 | 434 |
|
420 | 435 | # Map operation should create one TaskBadger task |
421 | 436 | assert create.call_count == 1 |
@@ -449,6 +464,7 @@ def task_starmap_fn(self, a, b): |
449 | 464 | create.return_value = tb_task |
450 | 465 | result = starmap_canvas.delay() |
451 | 466 | assert result.get(timeout=10, propagate=True) == [3, 7, 11] |
| 467 | + _wait_for_call_count(update, 2) |
452 | 468 |
|
453 | 469 | # Starmap operation should create one TaskBadger task |
454 | 470 | assert create.call_count == 1 |
@@ -482,6 +498,7 @@ def task_chunks_fn(self, a): |
482 | 498 | create.return_value = tb_task |
483 | 499 | result = chunks_canvas.delay() |
484 | 500 | assert result.get(timeout=10, propagate=True) == [[0, 2], [4, 6], [8, 10]] |
| 501 | + _wait_for_call_count(update, 6) |
485 | 502 |
|
486 | 503 | # Each chunk should create a TaskBadger task (3 chunks of 2) |
487 | 504 | assert create.call_count == 3 |
|
0 commit comments