1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
6
from __future__ import (
17
from celery import current_app
18
from celery.decorators import task
19
from celery.result import EagerResult
20
from maastesting.celery import CeleryFixture
21
from maastesting.testcase import TestCase
30
def task_exception(x, y):
34
class TestCeleryFixture(TestCase):
35
"""Tests `CeleryFixture`."""
38
super(TestCeleryFixture, self).setUp()
39
self.celery = self.useFixture(CeleryFixture())
41
def test_celery_config(self):
42
self.assertTrue(current_app.conf.CELERY_ALWAYS_EAGER)
43
self.assertTrue(current_app.conf.CELERY_EAGER_PROPAGATES_EXCEPTIONS)
45
def test_celery_eagerresult_contains_result(self):
46
# The result is an instance of EagerResult and it contains the actual
48
x = random.randrange(100)
49
y = random.randrange(100)
50
result = task_add.delay(x, y)
51
self.assertIsInstance(result, EagerResult)
52
self.assertEqual(x + y, result.result)
54
def test_celery_exception_raised(self):
55
self.assertRaises(RuntimeError, task_exception.delay, 1, 2)
57
def test_celery_records_tasks(self):
58
x = random.randrange(100)
59
y = random.randrange(100)
60
task_add.delay(x=x, y=y)
61
z = random.randrange(100)
62
t = random.randrange(100)
63
task_add.delay(x=z, y=t)
64
tasks = self.celery.tasks
65
self.assertEqual(2, len(tasks))
67
['maastesting.tests.test_celery.task_add'] * 2,
68
[task['task'].name for task in tasks])
69
self.assertEqual({'x': x, 'y': y}, tasks[0]['kwargs'])
70
self.assertEqual({'x': z, 'y': t}, tasks[1]['kwargs'])