| 1 |
import sys |
| 2 |
from unittest.mock import Mock, patch |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
pytest.importorskip('eventlet') |
| 7 |
|
| 8 |
from greenlet import GreenletExit # noqa |
| 9 |
|
| 10 |
import t.skip # noqa |
| 11 |
from celery.concurrency.eventlet import TaskPool, Timer, apply_target # noqa |
| 12 |
|
| 13 |
eventlet_modules = ( |
| 14 |
'eventlet', |
| 15 |
'eventlet.debug', |
| 16 |
'eventlet.greenthread', |
| 17 |
'eventlet.greenpool', |
| 18 |
'greenlet', |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
@t.skip.if_pypy |
| 23 |
class EventletCase: |
| 24 |
|
| 25 |
def setup_method(self): |
| 26 |
self.patching.modules(*eventlet_modules) |
| 27 |
|
| 28 |
def teardown_method(self): |
| 29 |
for mod in [mod for mod in sys.modules |
| 30 |
if mod.startswith('eventlet')]: |
| 31 |
try: |
| 32 |
del (sys.modules[mod]) |
| 33 |
except KeyError: |
| 34 |
pass |
| 35 |
|
| 36 |
|
| 37 |
class test_aaa_eventlet_patch(EventletCase): |
| 38 |
|
| 39 |
def test_aaa_is_patched(self): |
| 40 |
with patch('eventlet.monkey_patch', create=True) as monkey_patch: |
| 41 |
from celery import maybe_patch_concurrency |
| 42 |
maybe_patch_concurrency(['x', '-P', 'eventlet']) |
| 43 |
monkey_patch.assert_called_with() |
| 44 |
|
| 45 |
@patch('eventlet.debug.hub_blocking_detection', create=True) |
| 46 |
@patch('eventlet.monkey_patch', create=True) |
| 47 |
def test_aaa_blockdetecet( |
| 48 |
self, monkey_patch, hub_blocking_detection, patching): |
| 49 |
patching.setenv('EVENTLET_NOBLOCK', '10.3') |
| 50 |
from celery import maybe_patch_concurrency |
| 51 |
maybe_patch_concurrency(['x', '-P', 'eventlet']) |
| 52 |
monkey_patch.assert_called_with() |
| 53 |
hub_blocking_detection.assert_called_with(10.3, 10.3) |
| 54 |
|
| 55 |
|
| 56 |
class test_Timer(EventletCase): |
| 57 |
|
| 58 |
@pytest.fixture(autouse=True) |
| 59 |
def setup_patches(self, patching): |
| 60 |
self.spawn_after = patching('eventlet.greenthread.spawn_after') |
| 61 |
self.GreenletExit = patching('greenlet.GreenletExit') |
| 62 |
|
| 63 |
def test_sched(self): |
| 64 |
x = Timer() |
| 65 |
x.GreenletExit = KeyError |
| 66 |
entry = Mock() |
| 67 |
g = x._enter(1, 0, entry) |
| 68 |
assert x.queue |
| 69 |
|
| 70 |
x._entry_exit(g, entry) |
| 71 |
g.wait.side_effect = KeyError() |
| 72 |
x._entry_exit(g, entry) |
| 73 |
entry.cancel.assert_called_with() |
| 74 |
assert not x._queue |
| 75 |
|
| 76 |
x._queue.add(g) |
| 77 |
x.clear() |
| 78 |
x._queue.add(g) |
| 79 |
g.cancel.side_effect = KeyError() |
| 80 |
x.clear() |
| 81 |
|
| 82 |
def test_cancel(self): |
| 83 |
x = Timer() |
| 84 |
tref = Mock(name='tref') |
| 85 |
x.cancel(tref) |
| 86 |
tref.cancel.assert_called_with() |
| 87 |
x.GreenletExit = KeyError |
| 88 |
tref.cancel.side_effect = KeyError() |
| 89 |
x.cancel(tref) |
| 90 |
|
| 91 |
|
| 92 |
class test_TaskPool(EventletCase): |
| 93 |
|
| 94 |
@pytest.fixture(autouse=True) |
| 95 |
def setup_patches(self, patching): |
| 96 |
self.GreenPool = patching('eventlet.greenpool.GreenPool') |
| 97 |
self.greenthread = patching('eventlet.greenthread') |
| 98 |
|
| 99 |
def test_pool(self): |
| 100 |
x = TaskPool() |
| 101 |
x.on_start() |
| 102 |
x.on_stop() |
| 103 |
x.on_apply(Mock()) |
| 104 |
x._pool = None |
| 105 |
x.on_stop() |
| 106 |
assert len(x._pool_map.keys()) == 1 |
| 107 |
assert x.getpid() |
| 108 |
|
| 109 |
@patch('celery.concurrency.eventlet.base') |
| 110 |
def test_apply_target(self, base): |
| 111 |
apply_target(Mock(), getpid=Mock()) |
| 112 |
base.apply_target.assert_called() |
| 113 |
|
| 114 |
def test_grow(self): |
| 115 |
x = TaskPool(10) |
| 116 |
x._pool = Mock(name='_pool') |
| 117 |
x.grow(2) |
| 118 |
assert x.limit == 12 |
| 119 |
x._pool.resize.assert_called_with(12) |
| 120 |
|
| 121 |
def test_shrink(self): |
| 122 |
x = TaskPool(10) |
| 123 |
x._pool = Mock(name='_pool') |
| 124 |
x.shrink(2) |
| 125 |
assert x.limit == 8 |
| 126 |
x._pool.resize.assert_called_with(8) |
| 127 |
|
| 128 |
def test_get_info(self): |
| 129 |
x = TaskPool(10) |
| 130 |
x._pool = Mock(name='_pool') |
| 131 |
assert x._get_info() == { |
| 132 |
'implementation': 'celery.concurrency.eventlet:TaskPool', |
| 133 |
'max-concurrency': 10, |
| 134 |
'free-threads': x._pool.free(), |
| 135 |
'running-threads': x._pool.running(), |
| 136 |
} |
| 137 |
|
| 138 |
def test_terminate_job(self): |
| 139 |
func = Mock() |
| 140 |
pool = TaskPool(10) |
| 141 |
pool.on_start() |
| 142 |
pool.on_apply(func) |
| 143 |
|
| 144 |
assert len(pool._pool_map.keys()) == 1 |
| 145 |
pid = list(pool._pool_map.keys())[0] |
| 146 |
greenlet = pool._pool_map[pid] |
| 147 |
|
| 148 |
pool.terminate_job(pid) |
| 149 |
greenlet.link.assert_called_once() |
| 150 |
greenlet.kill.assert_called_once() |
| 151 |
|
| 152 |
def test_make_killable_target(self): |
| 153 |
def valid_target(): |
| 154 |
return "some result..." |
| 155 |
|
| 156 |
def terminating_target(): |
| 157 |
raise GreenletExit() |
| 158 |
|
| 159 |
assert TaskPool._make_killable_target(valid_target)() == "some result..." |
| 160 |
assert TaskPool._make_killable_target(terminating_target)() == (False, None, None) |
| 161 |
|
| 162 |
def test_cleanup_after_job_finish(self): |
| 163 |
testMap = {'1': None} |
| 164 |
TaskPool._cleanup_after_job_finish(None, testMap, '1') |
| 165 |
assert len(testMap) == 0 |