Browse Source

100% Coverage for celery.task.*

Ask Solem 14 years ago
parent
commit
fc5fc51060

+ 1 - 0
celery/app/__init__.py

@@ -105,6 +105,7 @@ class App(base.BaseApp):
         def inner_create_task_cls(**options):
 
             def _create_task_cls(fun):
+                options["app"] = self
                 options.setdefault("accept_magic_kwargs", False)
                 base = options.pop("base", None) or self.create_task_cls()
 

+ 2 - 9
celery/task/base.py

@@ -545,10 +545,7 @@ class BaseTask(object):
         # If task was executed eagerly using apply(),
         # then the retry must also be executed eagerly.
         if request.is_eager:
-            result = self.apply(args=args, kwargs=kwargs, **options)
-            if isinstance(result, EagerResult):
-                return result.get()             # propogates exceptions.
-            return result
+            return self.apply(args=args, kwargs=kwargs, **options).get()
 
         self.apply_async(args=args, kwargs=kwargs, **options)
 
@@ -714,11 +711,7 @@ class BaseTask(object):
 
     def __repr__(self):
         """`repr(task)`"""
-        try:
-            kind = self.__class__.mro()[1].__name__
-        except (AttributeError, IndexError):            # pragma: no cover
-            kind = "%s(Task)" % self.__class__.__name__
-        return "<%s: %s (%s)>" % (kind, self.name, self.type)
+        return "<@task: %s>" % (self.name, )
 
     @classmethod
     def subtask(cls, *args, **kwargs):

+ 10 - 0
celery/tests/test_app.py

@@ -35,6 +35,16 @@ class test_App(unittest.TestCase):
         self.app = Celery()
         self.app.conf.update(test_config)
 
+    def test_task(self):
+        app = Celery("foozibari")
+
+        def fun():
+            pass
+
+        fun.__module__ = "__main__"
+        task = app.task(fun)
+        self.assertEqual(task.name, app.main + ".fun")
+
     def test_TaskSet(self):
         ts = self.app.TaskSet()
         self.assertListEqual(ts.tasks, [])

+ 43 - 0
celery/tests/test_task.py

@@ -331,6 +331,31 @@ class TestCeleryTasks(unittest.TestCase):
         publisher = t1.get_publisher()
         self.assertTrue(publisher.exchange)
 
+    def test_send_task_sent_event(self):
+        T1 = self.createTaskCls("T1", "c.unittest.t.t1")
+        conn = T1.app.broker_connection()
+        chan = conn.channel()
+        prev = T1.app.conf.CELERY_SEND_TASK_SENT_EVENT
+        T1.app.conf.CELERY_SEND_TASK_SENT_EVENT = True
+        dispatcher = [None]
+
+        class Pub(object):
+            channel = chan
+
+            def delay_task(self, *args, **kwargs):
+                dispatcher[0] = kwargs.get("event_dispatcher")
+
+        try:
+            T1.apply_async(publisher=Pub())
+        finally:
+            T1.app.conf.CELERY_SEND_TASK_SENT_EVENT = False
+            chan.close()
+            conn.close()
+
+        self.assertTrue(dispatcher[0])
+
+
+
     def test_get_publisher(self):
         from celery.app import amqp
         old_pub = amqp.TaskPublisher
@@ -357,6 +382,19 @@ class TestCeleryTasks(unittest.TestCase):
         self.assertEqual(yyy.AsyncResult(tid).status, "FROBULATING")
         self.assertDictEqual(yyy.AsyncResult(tid).result, {"fooz": "baaz"})
 
+        yyy.request.id = tid
+        yyy.update_state(state="FROBUZATING", meta={"fooz": "baaz"})
+        self.assertEqual(yyy.AsyncResult(tid).status, "FROBUZATING")
+        self.assertDictEqual(yyy.AsyncResult(tid).result, {"fooz": "baaz"})
+
+    def test_repr(self):
+
+        @task_dec
+        def task_test_repr():
+            pass
+
+        self.assertIn("task_test_repr", repr(task_test_repr))
+
     def test_has___name__(self):
 
         @task_dec
@@ -372,6 +410,11 @@ class TestCeleryTasks(unittest.TestCase):
         logger = t1.get_logger(logfile=logfh, loglevel=0)
         self.assertTrue(logger)
 
+        T1.request.loglevel = 3
+        logger = t1.get_logger(logfile=logfh, loglevel=None)
+        self.assertTrue(logger)
+
+
 
 class TestTaskSet(unittest.TestCase):
 

+ 34 - 4
celery/tests/test_task_builtins.py

@@ -1,16 +1,46 @@
-from celery.tests.utils import unittest
+import warnings
 
-from celery.task import PingTask, backend_cleanup
+from celery.app import app_or_default
+from celery.task import ping, PingTask, backend_cleanup
+from celery.tests.compat import catch_warnings
+from celery.tests.utils import unittest, execute_context
 
 
 def some_func(i):
     return i * i
 
 
-class test_PingTask(unittest.TestCase):
+class test_deprecated(unittest.TestCase):
 
     def test_ping(self):
-        self.assertEqual(PingTask.apply().get(), 'pong')
+        warnings.resetwarnings()
+
+        def block(log):
+            prev = PingTask.app.conf.CELERY_ALWAYS_EAGER
+            PingTask.app.conf.CELERY_ALWAYS_EAGER = True
+            try:
+                return ping(), log[0].message
+            finally:
+                PingTask.app.conf.CELERY_ALWAYS_EAGER = prev
+
+        pong, warning = execute_context(catch_warnings(record=True), block)
+        self.assertEqual(pong, "pong")
+        self.assertIsInstance(warning, DeprecationWarning)
+        self.assertIn("ping task has been deprecated",
+                      warning.args[0])
+
+    def test_TaskSet_import_from_task_base(self):
+        warnings.resetwarnings()
+
+        def block(log):
+            from celery.task.base import TaskSet, subtask
+            x = TaskSet()
+            y = subtask(PingTask)
+            return log[0].message, log[1].message
+
+        for w in execute_context(catch_warnings(record=True), block):
+            self.assertIsInstance(w, DeprecationWarning)
+            self.assertIn("Please use", w.args[0])
 
 
 class test_backend_cleanup(unittest.TestCase):

+ 11 - 0
celery/tests/test_task_control.py

@@ -98,6 +98,17 @@ class test_inspect(unittest.TestCase):
         self.i.ping()
         self.assertIn("ping", MockMailbox.sent)
 
+    @with_mock_broadcast
+    def test_add_consumer(self):
+        self.i.add_consumer("foo")
+        self.assertIn("add_consumer", MockMailbox.sent)
+
+    @with_mock_broadcast
+    def test_cancel_consumer(self):
+        self.i.cancel_consumer("foo")
+        self.assertIn("cancel_consumer", MockMailbox.sent)
+
+
 
 class test_Broadcast(unittest.TestCase):