| 1 |
from unittest.mock import Mock |
| 2 |
|
| 3 |
from celery.concurrency.gevent import TaskPool, Timer, apply_timeout |
| 4 |
|
| 5 |
gevent_modules = ( |
| 6 |
'gevent', |
| 7 |
'gevent.greenlet', |
| 8 |
'gevent.monkey', |
| 9 |
'gevent.pool', |
| 10 |
'gevent.signal', |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
class test_gevent_patch: |
| 15 |
|
| 16 |
def test_is_patched(self): |
| 17 |
self.patching.modules(*gevent_modules) |
| 18 |
patch_all = self.patching('gevent.monkey.patch_all') |
| 19 |
import gevent |
| 20 |
gevent.version_info = (1, 0, 0) |
| 21 |
from celery import maybe_patch_concurrency |
| 22 |
maybe_patch_concurrency(['x', '-P', 'gevent']) |
| 23 |
patch_all.assert_called() |
| 24 |
|
| 25 |
|
| 26 |
class test_Timer: |
| 27 |
|
| 28 |
def setup_method(self): |
| 29 |
self.patching.modules(*gevent_modules) |
| 30 |
self.greenlet = self.patching('gevent.greenlet') |
| 31 |
self.GreenletExit = self.patching('gevent.greenlet.GreenletExit') |
| 32 |
|
| 33 |
def test_sched(self): |
| 34 |
self.greenlet.Greenlet = object |
| 35 |
x = Timer() |
| 36 |
self.greenlet.Greenlet = Mock() |
| 37 |
x._Greenlet.spawn_later = Mock() |
| 38 |
x._GreenletExit = KeyError |
| 39 |
entry = Mock() |
| 40 |
g = x._enter(1, 0, entry) |
| 41 |
assert x.queue |
| 42 |
|
| 43 |
x._entry_exit(g) |
| 44 |
g.kill.assert_called_with() |
| 45 |
assert not x._queue |
| 46 |
|
| 47 |
x._queue.add(g) |
| 48 |
x.clear() |
| 49 |
x._queue.add(g) |
| 50 |
g.kill.side_effect = KeyError() |
| 51 |
x.clear() |
| 52 |
|
| 53 |
g = x._Greenlet() |
| 54 |
g.cancel() |
| 55 |
|
| 56 |
|
| 57 |
class test_TaskPool: |
| 58 |
|
| 59 |
def setup_method(self): |
| 60 |
self.patching.modules(*gevent_modules) |
| 61 |
self.spawn_raw = self.patching('gevent.spawn_raw') |
| 62 |
self.Pool = self.patching('gevent.pool.Pool') |
| 63 |
|
| 64 |
def test_pool(self): |
| 65 |
x = TaskPool() |
| 66 |
x.on_start() |
| 67 |
x.on_stop() |
| 68 |
x.on_apply(Mock()) |
| 69 |
x._pool = None |
| 70 |
x.on_stop() |
| 71 |
|
| 72 |
x._pool = Mock() |
| 73 |
x._pool._semaphore.counter = 1 |
| 74 |
x._pool.size = 1 |
| 75 |
x.grow() |
| 76 |
assert x._pool.size == 2 |
| 77 |
assert x._pool._semaphore.counter == 2 |
| 78 |
x.shrink() |
| 79 |
assert x._pool.size, 1 |
| 80 |
assert x._pool._semaphore.counter == 1 |
| 81 |
|
| 82 |
x._pool = [4, 5, 6] |
| 83 |
assert x.num_processes == 3 |
| 84 |
|
| 85 |
def test_terminate_job(self): |
| 86 |
func = Mock() |
| 87 |
pool = TaskPool(10) |
| 88 |
pool.on_start() |
| 89 |
pool.on_apply(func) |
| 90 |
|
| 91 |
assert len(pool._pool_map.keys()) == 1 |
| 92 |
pid = list(pool._pool_map.keys())[0] |
| 93 |
greenlet = pool._pool_map[pid] |
| 94 |
greenlet.link.assert_called_once() |
| 95 |
|
| 96 |
pool.terminate_job(pid) |
| 97 |
import gevent |
| 98 |
|
| 99 |
gevent.kill.assert_called_once() |
| 100 |
|
| 101 |
def test_make_killable_target(self): |
| 102 |
def valid_target(): |
| 103 |
return "some result..." |
| 104 |
|
| 105 |
def terminating_target(): |
| 106 |
from greenlet import GreenletExit |
| 107 |
raise GreenletExit |
| 108 |
|
| 109 |
assert TaskPool._make_killable_target(valid_target)() == "some result..." |
| 110 |
assert TaskPool._make_killable_target(terminating_target)() == (False, None, None) |
| 111 |
|
| 112 |
def test_cleanup_after_job_finish(self): |
| 113 |
testMap = {'1': None} |
| 114 |
TaskPool._cleanup_after_job_finish(None, testMap, '1') |
| 115 |
assert len(testMap) == 0 |
| 116 |
|
| 117 |
|
| 118 |
class test_apply_timeout: |
| 119 |
|
| 120 |
def test_apply_timeout(self): |
| 121 |
self.patching.modules(*gevent_modules) |
| 122 |
|
| 123 |
class Timeout(Exception): |
| 124 |
value = None |
| 125 |
|
| 126 |
def __init__(self, value): |
| 127 |
self.__class__.value = value |
| 128 |
|
| 129 |
def __enter__(self): |
| 130 |
return self |
| 131 |
|
| 132 |
def __exit__(self, *exc_info): |
| 133 |
pass |
| 134 |
timeout_callback = Mock(name='timeout_callback') |
| 135 |
apply_target = Mock(name='apply_target') |
| 136 |
getpid = Mock(name='getpid') |
| 137 |
apply_timeout( |
| 138 |
Mock(), timeout=10, callback=Mock(name='callback'), |
| 139 |
timeout_callback=timeout_callback, getpid=getpid, |
| 140 |
apply_target=apply_target, Timeout=Timeout, |
| 141 |
) |
| 142 |
assert Timeout.value == 10 |
| 143 |
apply_target.assert_called() |
| 144 |
|
| 145 |
apply_target.side_effect = Timeout(10) |
| 146 |
apply_timeout( |
| 147 |
Mock(), timeout=10, callback=Mock(), |
| 148 |
timeout_callback=timeout_callback, getpid=getpid, |
| 149 |
apply_target=apply_target, Timeout=Timeout, |
| 150 |
) |
| 151 |
timeout_callback.assert_called_with(False, 10) |