| 1 |
import subprocess |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from celery import Celery |
| 6 |
|
| 7 |
|
| 8 |
def test_run_worker(): |
| 9 |
with pytest.raises(subprocess.CalledProcessError) as exc_info: |
| 10 |
subprocess.check_output( |
| 11 |
["celery", "--config", "t.integration.test_worker_config", "worker"], |
| 12 |
stderr=subprocess.STDOUT) |
| 13 |
|
| 14 |
called_process_error = exc_info.value |
| 15 |
assert called_process_error.returncode == 1, called_process_error |
| 16 |
output = called_process_error.output.decode('utf-8') |
| 17 |
assert output.find( |
| 18 |
"Retrying to establish a connection to the message broker after a connection " |
| 19 |
"loss has been disabled (app.conf.broker_connection_retry_on_startup=False). " |
| 20 |
"Shutting down...") != -1, output |
| 21 |
|
| 22 |
|
| 23 |
def test_django_fixup_direct_worker(caplog, monkeypatch): |
| 24 |
"""Test Django fixup by directly instantiating Celery worker without subprocess.""" |
| 25 |
import logging |
| 26 |
|
| 27 |
import django |
| 28 |
|
| 29 |
# Set logging level to capture debug messages |
| 30 |
caplog.set_level(logging.DEBUG) |
| 31 |
|
| 32 |
# Configure Django settings |
| 33 |
monkeypatch.setenv('DJANGO_SETTINGS_MODULE', 't.integration.test_django_settings') |
| 34 |
django.setup() |
| 35 |
|
| 36 |
# Create Celery app with Django integration |
| 37 |
app = Celery('test_django_direct') |
| 38 |
app.config_from_object('django.conf:settings', namespace='CELERY') |
| 39 |
app.autodiscover_tasks() |
| 40 |
|
| 41 |
# Test that we can access worker configuration without recursion errors |
| 42 |
# This should trigger the Django fixup initialization |
| 43 |
worker = app.Worker( |
| 44 |
pool='solo', |
| 45 |
concurrency=1, |
| 46 |
loglevel='debug' |
| 47 |
) |
| 48 |
|
| 49 |
# Accessing pool_cls should not cause AttributeError |
| 50 |
pool_cls = worker.pool_cls |
| 51 |
assert pool_cls is not None |
| 52 |
|
| 53 |
# Verify pool_cls has __module__ attribute (should be a class, not a string) |
| 54 |
assert hasattr(pool_cls, '__module__'), \ |
| 55 |
f"pool_cls should be a class with __module__, got {type(pool_cls)}: {pool_cls}" |
| 56 |
|
| 57 |
# Capture and check logs |
| 58 |
log_output = caplog.text |
| 59 |
|
| 60 |
# Verify no recursion-related errors in logs |
| 61 |
assert "RecursionError" not in log_output, f"RecursionError found in logs:\n{log_output}" |
| 62 |
assert "maximum recursion depth exceeded" not in log_output, \ |
| 63 |
f"Recursion depth error found in logs:\n{log_output}" |
| 64 |
|
| 65 |
assert "AttributeError: 'str' object has no attribute '__module__'." not in log_output, \ |
| 66 |
f"AttributeError found in logs:\n{log_output}" |