Skip to content

Commit a1cd684

Browse files
committed
100% coverage for celery.concurrency.gevent
1 parent 81f032d commit a1cd684

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

celery/concurrency/gevent.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
PATCHED[0] += 1
1616
from gevent import monkey, version_info
1717
monkey.patch_all()
18-
if version_info[0] == 0:
18+
if version_info[0] == 0: # pragma: no cover
1919
# Signals are not working along gevent in version prior 1.0
2020
# and they are not monkey patch by monkey.patch_all()
2121
from gevent import signal as _gevent_signal
@@ -24,7 +24,7 @@
2424

2525
try:
2626
from gevent import Timeout
27-
except ImportError:
27+
except ImportError: # pragma: no cover
2828
Timeout = None # noqa
2929

3030
from time import time
@@ -36,7 +36,8 @@
3636

3737
def apply_timeout(target, args=(), kwargs={}, callback=None,
3838
accept_callback=None, pid=None, timeout=None,
39-
timeout_callback=None, **rest):
39+
timeout_callback=None, Timeout=Timeout,
40+
apply_target=apply_target, **rest):
4041
try:
4142
with Timeout(timeout):
4243
return apply_target(target, args, kwargs, callback,
@@ -51,9 +52,7 @@ def __init__(self, *args, **kwargs):
5152
from gevent.greenlet import Greenlet, GreenletExit
5253

5354
class _Greenlet(Greenlet):
54-
55-
def cancel(self):
56-
self.kill()
55+
cancel = Greenlet.kill
5756

5857
self._Greenlet = _Greenlet
5958
self._GreenletExit = GreenletExit

celery/tests/concurrency/test_gevent.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
Schedule,
1111
Timer,
1212
TaskPool,
13+
apply_timeout,
14+
)
15+
16+
from celery.tests.case import (
17+
Case, mock_module, patch_many, skip_if_pypy,
1318
)
1419

15-
from celery.tests.case import Case, mock_module, patch_many, skip_if_pypy
1620
gevent_modules = (
1721
'gevent',
1822
'gevent.monkey',
@@ -80,6 +84,9 @@ def test_sched(self):
8084
g.kill.side_effect = KeyError()
8185
x.clear()
8286

87+
g = x._Greenlet()
88+
g.cancel()
89+
8390

8491
class test_TasKPool(Case):
8592

@@ -118,3 +125,37 @@ def test_timer(self):
118125
x.start()
119126
x.stop()
120127
x.schedule.clear.assert_called_with()
128+
129+
130+
class test_apply_timeout(Case):
131+
132+
def test_apply_timeout(self):
133+
134+
class Timeout(Exception):
135+
value = None
136+
137+
def __init__(self, value):
138+
self.__class__.value = value
139+
140+
def __enter__(self):
141+
return self
142+
143+
def __exit__(self, *exc_info):
144+
pass
145+
timeout_callback = Mock(name='timeout_callback')
146+
apply_target = Mock(name='apply_target')
147+
apply_timeout(
148+
Mock(), timeout=10, callback=Mock(name='callback'),
149+
timeout_callback=timeout_callback,
150+
apply_target=apply_target, Timeout=Timeout,
151+
)
152+
self.assertEqual(Timeout.value, 10)
153+
self.assertTrue(apply_target.called)
154+
155+
apply_target.side_effect = Timeout(10)
156+
apply_timeout(
157+
Mock(), timeout=10, callback=Mock(),
158+
timeout_callback=timeout_callback,
159+
apply_target=apply_target, Timeout=Timeout,
160+
)
161+
timeout_callback.assert_called_with(False, 10)

0 commit comments

Comments
 (0)