Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ default_language_version:
python: python3.12
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.15.12
rev: v0.15.20
hooks:
- id: ruff-check
args: [ --fix ]
Expand Down
17 changes: 17 additions & 0 deletions tests/test_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""

import logging
import time
from unittest import mock

import celery
Expand All @@ -21,6 +22,18 @@
from tests.utils import task_for_test


def _wait_for_call_count(mock_obj, expected, timeout=10.0):
"""Wait until ``mock_obj`` has been called ``expected`` times.

The worker's ``task_success`` signal fires asynchronously, so the final
status update can lag behind ``result.get()`` returning. Poll for it while
the mock patch is still active to avoid a race with the ``with`` block exit.
"""
deadline = time.monotonic() + timeout
while mock_obj.call_count < expected and time.monotonic() < deadline:
time.sleep(0.01)


@pytest.fixture(autouse=True)
def _check_log_errors(caplog):
yield
Expand Down Expand Up @@ -52,6 +65,7 @@ def add_normal(self, a, b):
assert result.taskbadger_task_id == tb_task.id
assert result.get_taskbadger_task() is not None
assert result.get(timeout=10, propagate=True) == 4
_wait_for_call_count(update, 2)

create.assert_called_once()
assert get_task.call_count == 2
Expand Down Expand Up @@ -416,6 +430,7 @@ def task_map_fn(self, a):
create.return_value = tb_task
result = map_canvas.delay()
assert result.get(timeout=10, propagate=True) == [0, 2, 4, 6, 8]
_wait_for_call_count(update, 2)

# Map operation should create one TaskBadger task
assert create.call_count == 1
Expand Down Expand Up @@ -449,6 +464,7 @@ def task_starmap_fn(self, a, b):
create.return_value = tb_task
result = starmap_canvas.delay()
assert result.get(timeout=10, propagate=True) == [3, 7, 11]
_wait_for_call_count(update, 2)

# Starmap operation should create one TaskBadger task
assert create.call_count == 1
Expand Down Expand Up @@ -482,6 +498,7 @@ def task_chunks_fn(self, a):
create.return_value = tb_task
result = chunks_canvas.delay()
assert result.get(timeout=10, propagate=True) == [[0, 2], [4, 6], [8, 10]]
_wait_for_call_count(update, 6)

# Each chunk should create a TaskBadger task (3 chunks of 2)
assert create.call_count == 3
Expand Down
Loading