| 1 |
from unittest.mock import Mock, patch |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
# this import adds a @shared_task, which uses connect_on_app_finalize |
| 6 |
# to install the celery.ping task that the test lib uses |
| 7 |
import celery.contrib.testing.tasks # noqa |
| 8 |
from celery import Celery |
| 9 |
from celery.contrib.testing.worker import TestWorkController, start_worker |
| 10 |
|
| 11 |
|
| 12 |
class test_worker: |
| 13 |
def setup_method(self): |
| 14 |
self.app = Celery('celerytest', backend='cache+memory://', broker='memory://', ) |
| 15 |
|
| 16 |
@self.app.task |
| 17 |
def add(x, y): |
| 18 |
return x + y |
| 19 |
|
| 20 |
self.add = add |
| 21 |
|
| 22 |
@self.app.task |
| 23 |
def error_task(): |
| 24 |
raise NotImplementedError() |
| 25 |
|
| 26 |
self.error_task = error_task |
| 27 |
|
| 28 |
self.app.config_from_object({ |
| 29 |
'worker_hijack_root_logger': False, |
| 30 |
}) |
| 31 |
|
| 32 |
# to avoid changing the root logger level to ERROR, |
| 33 |
# we have to set both app.log.loglevel start_worker arg to 0 |
| 34 |
# (see celery.app.log.setup_logging_subsystem) |
| 35 |
self.app.log.loglevel = 0 |
| 36 |
|
| 37 |
def test_start_worker(self): |
| 38 |
with start_worker(app=self.app, loglevel=0): |
| 39 |
result = self.add.s(1, 2).apply_async() |
| 40 |
val = result.get(timeout=5) |
| 41 |
assert val == 3 |
| 42 |
|
| 43 |
def test_start_worker_with_exception(self): |
| 44 |
"""Make sure that start_worker does not hang on exception""" |
| 45 |
|
| 46 |
with pytest.raises(NotImplementedError): |
| 47 |
with start_worker(app=self.app, loglevel=0): |
| 48 |
result = self.error_task.apply_async() |
| 49 |
result.get(timeout=5) |
| 50 |
|
| 51 |
def test_start_worker_with_hostname_config(self): |
| 52 |
"""Make sure a custom hostname can be supplied to the TestWorkController""" |
| 53 |
test_hostname = 'test_name@test_host' |
| 54 |
with start_worker(app=self.app, loglevel=0, hostname=test_hostname) as w: |
| 55 |
|
| 56 |
assert isinstance(w, TestWorkController) |
| 57 |
assert w.hostname == test_hostname |
| 58 |
|
| 59 |
result = self.add.s(1, 2).apply_async() |
| 60 |
val = result.get(timeout=5) |
| 61 |
assert val == 3 |
| 62 |
|
| 63 |
|
| 64 |
class test_TestWorkController: |
| 65 |
|
| 66 |
@patch('celery.contrib.testing.worker.worker.WorkController.__init__') |
| 67 |
def test_init_with_string_pool_cls_prefork(self, mock_super_init): |
| 68 |
mock_super_init.return_value = None |
| 69 |
controller = object.__new__(TestWorkController) |
| 70 |
controller._on_started = None |
| 71 |
controller.pool_cls = 'prefork' |
| 72 |
with patch('celery.contrib.testing.worker.logging.handlers.QueueListener') as mock_listener: |
| 73 |
with patch('billiard.Queue') as mock_queue: |
| 74 |
with patch.dict('sys.modules', {'tblib': None, 'tblib.pickling_support': None}): |
| 75 |
mock_queue.return_value = Mock() |
| 76 |
controller.__init__(app=Mock()) |
| 77 |
mock_listener.assert_called_once() |
| 78 |
assert controller.logger_queue is not None |
| 79 |
|
| 80 |
@patch('celery.contrib.testing.worker.worker.WorkController.__init__') |
| 81 |
def test_init_with_string_pool_cls_solo(self, mock_super_init): |
| 82 |
mock_super_init.return_value = None |
| 83 |
controller = object.__new__(TestWorkController) |
| 84 |
controller._on_started = None |
| 85 |
controller.pool_cls = 'solo' |
| 86 |
controller.__init__(app=Mock()) |
| 87 |
assert controller.logger_queue is None |
| 88 |
|
| 89 |
@patch('celery.contrib.testing.worker.worker.WorkController.__init__') |
| 90 |
def test_init_with_string_pool_cls_gevent(self, mock_super_init): |
| 91 |
mock_super_init.return_value = None |
| 92 |
controller = object.__new__(TestWorkController) |
| 93 |
controller._on_started = None |
| 94 |
controller.pool_cls = 'gevent' |
| 95 |
controller.__init__(app=Mock()) |
| 96 |
assert controller.logger_queue is None |