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