示例#1
0
 def test_execute_success_no_kwargs(self):
     tid = gen_unique_id()
     tw = TaskWrapper(mytask_no_kwargs.name, tid, [4], {})
     self.assertEqual(tw.execute(), 256)
     meta = TaskMeta.objects.get(task_id=tid)
     self.assertEqual(meta.result, 256)
     self.assertEqual(meta.status, states.SUCCESS)
示例#2
0
 def test_executed_bit(self):
     from celery.worker.job import AlreadyExecutedError
     tw = TaskWrapper(mytask.name, gen_unique_id(), [], {})
     self.assertFalse(tw.executed)
     tw._set_executed_bit()
     self.assertTrue(tw.executed)
     self.assertRaises(AlreadyExecutedError, tw._set_executed_bit)
示例#3
0
 def test_execute_fail(self):
     tid = gen_unique_id()
     tw = TaskWrapper(mytask_raising.name, tid, [4], {"f": "x"})
     self.assertIsInstance(tw.execute(), ExceptionInfo)
     meta = TaskMeta.objects.get(task_id=tid)
     self.assertEqual(meta.status, states.FAILURE)
     self.assertIsInstance(meta.result, KeyError)
示例#4
0
 def test_execute(self):
     tid = gen_unique_id()
     tw = TaskWrapper("cu.mytask", tid, mytask, [4], {"f": "x"})
     self.assertEquals(tw.execute(), 256)
     meta = TaskMeta.objects.get(task_id=tid)
     self.assertEquals(meta.result, 256)
     self.assertEquals(meta.status, "DONE")
示例#5
0
 def test_execute(self):
     tid = gen_unique_id()
     tw = TaskWrapper(mytask.name, tid, [4], {"f": "x"})
     self.assertEquals(tw.execute(), 256)
     meta = TaskMeta.objects.get(task_id=tid)
     self.assertEquals(meta.result, 256)
     self.assertEquals(meta.status, "SUCCESS")
示例#6
0
    def test_send_email(self):
        from celery import conf
        from celery.worker import job
        old_mail_admins = job.mail_admins
        old_enable_mails = conf.CELERY_SEND_TASK_ERROR_EMAILS
        mail_sent = [False]

        def mock_mail_admins(*args, **kwargs):
            mail_sent[0] = True

        job.mail_admins = mock_mail_admins
        conf.CELERY_SEND_TASK_ERROR_EMAILS = True
        try:
            tw = TaskWrapper(mytask.name, gen_unique_id(), [1], {"f": "x"})
            try:
                raise KeyError("foo")
            except KeyError:
                einfo = ExceptionInfo(sys.exc_info())

            tw.on_failure(einfo)
            self.assertTrue(mail_sent[0])

            mail_sent[0] = False
            conf.CELERY_SEND_TASK_ERROR_EMAILS = False
            tw.on_failure(einfo)
            self.assertFalse(mail_sent[0])

        finally:
            job.mail_admins = old_mail_admins
            conf.CELERY_SEND_TASK_ERROR_EMAILS = old_enable_mails
示例#7
0
 def test_execute_using_pool(self):
     tid = gen_unique_id()
     tw = TaskWrapper(mytask.name, tid, [4], {"f": "x"})
     p = TaskPool(2)
     p.start()
     asyncres = tw.execute_using_pool(p)
     self.assertEqual(asyncres.get(), 256)
     p.stop()
示例#8
0
 def test_execute_using_pool(self):
     tid = gen_unique_id()
     tw = TaskWrapper("cu.mytask", tid, mytask, [4], {"f": "x"})
     p = TaskPool(2)
     p.start()
     asyncres = tw.execute_using_pool(p)
     self.assertTrue(asyncres.get(), 256)
     p.stop()
示例#9
0
 def test_execute_fail(self):
     tid = gen_unique_id()
     tw = TaskWrapper("cu.mytask-raising", tid, mytask_raising, [4],
                      {"f": "x"})
     self.assertTrue(isinstance(tw.execute(), ExceptionInfo))
     meta = TaskMeta.objects.get(task_id=tid)
     self.assertEquals(meta.status, "FAILURE")
     self.assertTrue(isinstance(meta.result, KeyError))
示例#10
0
 def test_execute_success_some_kwargs(self):
     tid = gen_unique_id()
     tw = TaskWrapper(mytask_some_kwargs.name, tid, [4], {})
     self.assertEqual(tw.execute(logfile="foobaz.log"), 256)
     meta = TaskMeta.objects.get(task_id=tid)
     self.assertEqual(some_kwargs_scratchpad.get("logfile"), "foobaz.log")
     self.assertEqual(meta.result, 256)
     self.assertEqual(meta.status, states.SUCCESS)
示例#11
0
 def test_execute_ack(self):
     tid = gen_unique_id()
     tw = TaskWrapper(mytask.name, tid, [4], {"f": "x"},
                     on_ack=on_ack)
     self.assertEqual(tw.execute(), 256)
     meta = TaskMeta.objects.get(task_id=tid)
     self.assertTrue(scratch["ACK"])
     self.assertEqual(meta.result, 256)
     self.assertEqual(meta.status, states.SUCCESS)
示例#12
0
 def test_execute_ack(self):
     tid = gen_unique_id()
     tw = TaskWrapper("cu.mytask", tid, mytask, [4], {"f": "x"},
                     on_acknowledge=on_ack)
     self.assertEquals(tw.execute(), 256)
     meta = TaskMeta.objects.get(task_id=tid)
     self.assertTrue(scratch["ACK"])
     self.assertEquals(meta.result, 256)
     self.assertEquals(meta.status, "DONE")
示例#13
0
 def test_default_kwargs(self):
     tid = gen_unique_id()
     tw = TaskWrapper("cu.mytask", tid, mytask, [4], {"f": "x"})
     self.assertEquals(tw.extend_with_default_kwargs(10, "some_logfile"), {
         "f": "x",
         "logfile": "some_logfile",
         "loglevel": 10,
         "task_id": tw.task_id,
         "task_name": tw.task_name})
示例#14
0
 def test_default_kwargs(self):
     tid = gen_unique_id()
     tw = TaskWrapper(mytask.name, tid, [4], {"f": "x"})
     self.assertDictEqual(
             tw.extend_with_default_kwargs(10, "some_logfile"), {
                 "f": "x",
                 "logfile": "some_logfile",
                 "loglevel": 10,
                 "task_id": tw.task_id,
                 "task_retries": 0,
                 "task_is_eager": False,
                 "delivery_info": {},
                 "task_name": tw.task_name})
示例#15
0
 def test_safe_process_task_raise_regular(self):
     worker = self.worker
     worker.pool = MockPool(raise_regular=True)
     backend = MockBackend()
     m = create_message(backend, task="c.u.foo", args=[4, 8, 10], kwargs={})
     task = TaskWrapper.from_message(m, m.decode())
     worker.safe_process_task(task)
     worker.pool.stop()
示例#16
0
 def test_safe_process_task(self):
     worker = self.worker
     worker.pool = MockPool()
     backend = MockBackend()
     m = create_message(backend, task=foo_task.name, args=[4, 8, 10],
                        kwargs={})
     task = TaskWrapper.from_message(m, m.decode())
     worker.safe_process_task(task)
     worker.pool.stop()
示例#17
0
    def _test_on_failure(self, exception):
        tid = gen_unique_id()
        tw = TaskWrapper(mytask.name, tid, [4], {"f": "x"})
        try:
            raise exception
        except Exception:
            exc_info = ExceptionInfo(sys.exc_info())

        logfh = StringIO()
        tw.logger.handlers = []
        tw.logger = setup_logger(logfile=logfh, loglevel=logging.INFO)

        from celery import conf
        conf.CELERY_SEND_TASK_ERROR_EMAILS = True

        tw.on_failure(exc_info)
        logvalue = logfh.getvalue()
        self.assertIn(mytask.name, logvalue)
        self.assertIn(tid, logvalue)
        self.assertIn("ERROR", logvalue)

        conf.CELERY_SEND_TASK_ERROR_EMAILS = False
示例#18
0
    def receive_message(self, message_data, message):
        """The callback called when a new message is received.

        If the message has an ``eta`` we move it to the hold queue,
        otherwise we move it the bucket queue for immediate processing.

        """
        try:
            task = TaskWrapper.from_message(message, message_data,
                                            logger=self.logger)
        except NotRegistered, exc:
            self.logger.error("Unknown task ignored: %s" % (exc))
            return
示例#19
0
    def test_on_failure(self):
        tid = gen_unique_id()
        tw = TaskWrapper("cu.mytask", tid, mytask, [4], {"f": "x"})
        try:
            raise Exception("Inside unit tests")
        except Exception:
            exc_info = ExceptionInfo(sys.exc_info())

        logfh = StringIO()
        tw.logger.handlers = []
        tw.logger = setup_logger(logfile=logfh, loglevel=logging.INFO)

        from celery import conf
        conf.SEND_CELERY_TASK_ERROR_EMAILS = True

        tw.on_failure(exc_info, {"task_id": tid, "task_name": "cu.mytask"})
        logvalue = logfh.getvalue()
        self.assertTrue("cu.mytask" in logvalue)
        self.assertTrue(tid in logvalue)
        self.assertTrue("ERROR" in logvalue)

        conf.SEND_CELERY_TASK_ERROR_EMAILS = False
示例#20
0
 def test_from_message(self):
     body = {"task": mytask.name, "id": gen_unique_id(),
             "args": [2], "kwargs": {u"æØåveéðƒeæ": "bar"}}
     m = BaseMessage(body=simplejson.dumps(body), backend="foo",
                     content_type="application/json",
                     content_encoding="utf-8")
     tw = TaskWrapper.from_message(m, m.decode())
     self.assertIsInstance(tw, TaskWrapper)
     self.assertEqual(tw.task_name, body["task"])
     self.assertEqual(tw.task_id, body["id"])
     self.assertEqual(tw.args, body["args"])
     self.assertEqual(tw.kwargs.keys()[0],
                       u"æØåveéðƒeæ".encode("utf-8"))
     self.assertNotIsInstance(tw.kwargs.keys()[0], unicode)
     self.assertTrue(tw.logger)
示例#21
0
 def test_send_event(self):
     tw = TaskWrapper(mytask.name, gen_unique_id(), [1], {"f": "x"})
     tw.eventer = MockEventDispatcher()
     tw.send_event("task-frobulated")
     self.assertIn("task-frobulated", tw.eventer.sent)