Skip to content

Commit a085889

Browse files
author
Ask Solem
committed
91% total coverage
1 parent 1519112 commit a085889

File tree

5 files changed

+127
-17
lines changed

5 files changed

+127
-17
lines changed

celery/tests/test_task_control.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from celery.task import control
44
from celery.task.builtins import PingTask
55
from celery.utils import gen_unique_id
6+
from celery.utils.functional import wraps
67

78

89
class MockBroadcastPublisher(object):
@@ -18,26 +19,56 @@ def close(self):
1819
pass
1920

2021

22+
class MockControlReplyConsumer(object):
23+
24+
def __init__(self, *args, **kwarg):
25+
pass
26+
27+
def collect(self, *args, **kwargs):
28+
pass
29+
30+
def close(self):
31+
pass
32+
33+
2134
def with_mock_broadcast(fun):
2235

36+
@wraps(fun)
2337
def _mocked(*args, **kwargs):
2438
old_pub = control.BroadcastPublisher
39+
old_rep = control.ControlReplyConsumer
2540
control.BroadcastPublisher = MockBroadcastPublisher
41+
control.ControlReplyConsumer = MockControlReplyConsumer
2642
try:
2743
return fun(*args, **kwargs)
2844
finally:
2945
MockBroadcastPublisher.sent = []
3046
control.BroadcastPublisher = old_pub
47+
control.ControlReplyConsumer = old_rep
3148
return _mocked
3249

3350

34-
class TestBroadcast(unittest.TestCase):
51+
class test_Broadcast(unittest.TestCase):
52+
53+
def test_discard_all(self):
54+
control.discard_all()
3555

3656
@with_mock_broadcast
3757
def test_broadcast(self):
3858
control.broadcast("foobarbaz", arguments=[])
3959
self.assertIn("foobarbaz", MockBroadcastPublisher.sent)
4060

61+
@with_mock_broadcast
62+
def test_broadcast_limit(self):
63+
control.broadcast("foobarbaz1", arguments=[], limit=None,
64+
destination=[1, 2, 3])
65+
self.assertIn("foobarbaz1", MockBroadcastPublisher.sent)
66+
67+
@with_mock_broadcast
68+
def test_broadcast_validate(self):
69+
self.assertRaises(ValueError, control.broadcast, "foobarbaz2",
70+
destination="foo")
71+
4172
@with_mock_broadcast
4273
def test_rate_limit(self):
4374
control.rate_limit(PingTask.name, "100/m")

celery/tests/test_utils.py

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from celery.tests.utils import sleepdeprived, execute_context
88
from celery.tests.utils import mask_modules
99

10-
class TestChunks(unittest.TestCase):
10+
class test_chunks(unittest.TestCase):
1111

1212
def test_chunks(self):
1313

@@ -27,7 +27,7 @@ def test_chunks(self):
2727
[[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
2828

2929

30-
class TestGenUniqueId(unittest.TestCase):
30+
class test_gen_unique_id(unittest.TestCase):
3131

3232
def test_gen_unique_id_without_ctypes(self):
3333
old_utils = sys.modules.pop("celery.utils")
@@ -47,7 +47,7 @@ def with_ctypes_masked(_val):
4747
sys.modules["celery.utils"] = old_utils
4848

4949

50-
class TestDivUtils(unittest.TestCase):
50+
class test_utils(unittest.TestCase):
5151

5252
def test_repeatlast(self):
5353
items = range(6)
@@ -57,8 +57,50 @@ def test_repeatlast(self):
5757
for j in items:
5858
self.assertEqual(it.next(), i)
5959

60+
def test_get_full_cls_name(self):
61+
Class = type("Fox", (object, ), {"__module__": "quick.brown"})
62+
self.assertEqual(utils.get_full_cls_name(Class), "quick.brown.Fox")
63+
64+
def test_is_iterable(self):
65+
for a in "f", ["f"], ("f", ), {"f": "f"}:
66+
self.assertTrue(utils.is_iterable(a))
67+
for b in object(), 1:
68+
self.assertFalse(utils.is_iterable(b))
69+
70+
def test_padlist(self):
71+
self.assertListEqual(utils.padlist(["George", "Costanza", "NYC"], 3),
72+
["George", "Costanza", "NYC"])
73+
self.assertListEqual(utils.padlist(["George", "Costanza"], 3),
74+
["George", "Costanza", None])
75+
self.assertListEqual(utils.padlist(["George", "Costanza", "NYC"], 4,
76+
default="Earth"),
77+
["George", "Costanza", "NYC", "Earth"])
78+
79+
def test_firstmethod_AttributeError(self):
80+
self.assertIsNone(utils.firstmethod("foo")([object()]))
81+
82+
def test_first(self):
83+
iterations = [0]
84+
85+
def predicate(value):
86+
iterations[0] += 1
87+
if value == 5:
88+
return True
89+
return False
90+
91+
self.assertEqual(5, utils.first(predicate, xrange(10)))
92+
self.assertEqual(iterations[0], 6)
93+
94+
iterations[0] = 0
95+
self.assertIsNone(utils.first(predicate, xrange(10, 20)))
96+
self.assertEqual(iterations[0], 10)
97+
98+
def test_get_cls_by_name__instance_returns_instance(self):
99+
instance = object()
100+
self.assertIs(utils.get_cls_by_name(instance), instance)
101+
60102

61-
class TestRetryOverTime(unittest.TestCase):
103+
class test_retry_over_time(unittest.TestCase):
62104

63105
def test_returns_retval_on_success(self):
64106

celery/tests/test_worker.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ def ack(self, delivery_tag):
8888

8989

9090
class MockPool(object):
91+
_terminated = False
92+
_stopped = False
9193

9294
def __init__(self, *args, **kwargs):
9395
self.raise_regular = kwargs.get("raise_regular", False)
@@ -103,9 +105,13 @@ def start(self):
103105
pass
104106

105107
def stop(self):
106-
pass
108+
self._stopped = True
107109
return True
108110

111+
def terminate(self):
112+
self._terminated = True
113+
self.stop()
114+
109115

110116
class MockController(object):
111117

@@ -453,6 +459,14 @@ def setUp(self):
453459
self.worker = WorkController(concurrency=1, loglevel=0)
454460
self.worker.logger = MockLogger()
455461

462+
def test_with_rate_limits_disabled(self):
463+
conf.DISABLE_RATE_LIMITS = True
464+
try:
465+
worker = WorkController(concurrency=1, loglevel=0)
466+
self.assertIsInstance(worker.ready_queue, FastQueue)
467+
finally:
468+
conf.DISABLE_RATE_LIMITS = False
469+
456470
def test_attrs(self):
457471
worker = self.worker
458472
self.assertIsInstance(worker.eta_schedule, Scheduler)
@@ -462,6 +476,12 @@ def test_attrs(self):
462476
self.assertTrue(worker.mediator)
463477
self.assertTrue(worker.components)
464478

479+
def test_with_embedded_clockservice(self):
480+
worker = WorkController(concurrency=1, loglevel=0,
481+
embed_clockservice=True)
482+
self.assertTrue(worker.clockservice)
483+
self.assertIn(worker.clockservice, worker.components)
484+
465485
def test_process_task(self):
466486
worker = self.worker
467487
worker.pool = MockPool()
@@ -492,7 +512,7 @@ def test_process_task_raise_regular(self):
492512
worker.process_task(task)
493513
worker.pool.stop()
494514

495-
def test_start_stop(self):
515+
def test_start__stop(self):
496516
worker = self.worker
497517
w1 = {"started": False}
498518
w2 = {"started": False}
@@ -508,3 +528,23 @@ def test_start_stop(self):
508528
worker.stop()
509529
for component in worker.components:
510530
self.assertTrue(component._stopped)
531+
532+
def test_start__terminate(self):
533+
worker = self.worker
534+
w1 = {"started": False}
535+
w2 = {"started": False}
536+
w3 = {"started": False}
537+
w4 = {"started": False}
538+
worker.components = [MockController(w1), MockController(w2),
539+
MockController(w3), MockController(w4),
540+
MockPool()]
541+
542+
worker.start()
543+
for w in (w1, w2, w3, w4):
544+
self.assertTrue(w["started"])
545+
self.assertTrue(worker._running, len(worker.components))
546+
self.assertEqual(worker._state, RUN)
547+
worker.terminate()
548+
for component in worker.components:
549+
self.assertTrue(component._stopped)
550+
self.assertTrue(worker.components[4]._terminated)

celery/worker/__init__.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,11 @@ def start(self):
191191
"""Starts the workers main loop."""
192192
self._state = RUN
193193

194-
try:
195-
for i, component in enumerate(self.components):
196-
self.logger.debug("Starting thread %s..." % \
197-
component.__class__.__name__)
198-
self._running = i + 1
199-
component.start()
200-
finally:
201-
self.stop()
194+
for i, component in enumerate(self.components):
195+
self.logger.debug("Starting thread %s..." % (
196+
component.__class__.__name__))
197+
self._running = i + 1
198+
component.start()
202199

203200
def process_task(self, wrapper):
204201
"""Process task by sending it to the pool of workers."""

celery/worker/listener.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@
9494
from celery.exceptions import NotRegistered
9595
from celery.datastructures import SharedCounter
9696

97-
RUN = 0x0
98-
CLOSE = 0x1
97+
RUN = 0x1
98+
CLOSE = 0x2
9999

100100

101101
class QoS(object):

0 commit comments

Comments
 (0)