|
@@ -6,10 +6,14 @@ import types
|
|
|
|
|
|
from contextlib import contextmanager
|
|
|
|
|
|
+from mock import Mock, patch
|
|
|
+
|
|
|
from celery import states
|
|
|
from celery.backends.cache import CacheBackend, DummyClient
|
|
|
from celery.exceptions import ImproperlyConfigured
|
|
|
+from celery.registry import tasks
|
|
|
from celery.result import AsyncResult
|
|
|
+from celery.task import subtask
|
|
|
from celery.utils import uuid
|
|
|
from celery.utils.encoding import str_to_bytes
|
|
|
|
|
@@ -24,60 +28,80 @@ class SomeClass(object):
|
|
|
|
|
|
class test_CacheBackend(unittest.TestCase):
|
|
|
|
|
|
- def test_mark_as_done(self):
|
|
|
- tb = CacheBackend(backend="memory://")
|
|
|
-
|
|
|
- tid = uuid()
|
|
|
+ def setUp(self):
|
|
|
+ self.tb = CacheBackend(backend="memory://")
|
|
|
+ self.tid = uuid()
|
|
|
|
|
|
- self.assertEqual(tb.get_status(tid), states.PENDING)
|
|
|
- self.assertIsNone(tb.get_result(tid))
|
|
|
+ def test_mark_as_done(self):
|
|
|
+ self.assertEqual(self.tb.get_status(self.tid), states.PENDING)
|
|
|
+ self.assertIsNone(self.tb.get_result(self.tid))
|
|
|
|
|
|
- tb.mark_as_done(tid, 42)
|
|
|
- self.assertEqual(tb.get_status(tid), states.SUCCESS)
|
|
|
- self.assertEqual(tb.get_result(tid), 42)
|
|
|
+ self.tb.mark_as_done(self.tid, 42)
|
|
|
+ self.assertEqual(self.tb.get_status(self.tid), states.SUCCESS)
|
|
|
+ self.assertEqual(self.tb.get_result(self.tid), 42)
|
|
|
|
|
|
def test_is_pickled(self):
|
|
|
- tb = CacheBackend(backend="memory://")
|
|
|
-
|
|
|
- tid2 = uuid()
|
|
|
result = {"foo": "baz", "bar": SomeClass(12345)}
|
|
|
- tb.mark_as_done(tid2, result)
|
|
|
+ self.tb.mark_as_done(self.tid, result)
|
|
|
# is serialized properly.
|
|
|
- rindb = tb.get_result(tid2)
|
|
|
+ rindb = self.tb.get_result(self.tid)
|
|
|
self.assertEqual(rindb.get("foo"), "baz")
|
|
|
self.assertEqual(rindb.get("bar").data, 12345)
|
|
|
|
|
|
def test_mark_as_failure(self):
|
|
|
- tb = CacheBackend(backend="memory://")
|
|
|
-
|
|
|
- tid3 = uuid()
|
|
|
try:
|
|
|
raise KeyError("foo")
|
|
|
except KeyError, exception:
|
|
|
- pass
|
|
|
- tb.mark_as_failure(tid3, exception)
|
|
|
- self.assertEqual(tb.get_status(tid3), states.FAILURE)
|
|
|
- self.assertIsInstance(tb.get_result(tid3), KeyError)
|
|
|
+ self.tb.mark_as_failure(self.tid, exception)
|
|
|
+ self.assertEqual(self.tb.get_status(self.tid), states.FAILURE)
|
|
|
+ self.assertIsInstance(self.tb.get_result(self.tid), KeyError)
|
|
|
|
|
|
- def test_mget(self):
|
|
|
+ def test_on_chord_apply(self):
|
|
|
tb = CacheBackend(backend="memory://")
|
|
|
- tb.set("foo", 1)
|
|
|
- tb.set("bar", 2)
|
|
|
+ tb.on_chord_apply("setid", [])
|
|
|
+
|
|
|
+ @patch("celery.result.TaskSetResult")
|
|
|
+ def test_on_chord_part_return(self, setresult):
|
|
|
+ tb = CacheBackend(backend="memory://")
|
|
|
+
|
|
|
+ deps = Mock()
|
|
|
+ deps.total = 2
|
|
|
+ setresult.restore.return_value = deps
|
|
|
+ task = Mock()
|
|
|
+ task.name = "foobarbaz"
|
|
|
+ try:
|
|
|
+ tasks["foobarbaz"] = task
|
|
|
+ task.request.chord = subtask(task)
|
|
|
+ task.request.taskset = "setid"
|
|
|
|
|
|
- self.assertDictEqual(tb.mget(["foo", "bar"]),
|
|
|
+ tb.on_chord_apply(task.request.taskset, [])
|
|
|
+
|
|
|
+ self.assertFalse(deps.join.called)
|
|
|
+ tb.on_chord_part_return(task)
|
|
|
+ self.assertFalse(deps.join.called)
|
|
|
+
|
|
|
+ tb.on_chord_part_return(task)
|
|
|
+ deps.join.assert_called_with(propagate=False)
|
|
|
+ deps.delete.assert_called_with()
|
|
|
+
|
|
|
+ finally:
|
|
|
+ tasks.pop("foobarbaz")
|
|
|
+
|
|
|
+ def test_mget(self):
|
|
|
+ self.tb.set("foo", 1)
|
|
|
+ self.tb.set("bar", 2)
|
|
|
+
|
|
|
+ self.assertDictEqual(self.tb.mget(["foo", "bar"]),
|
|
|
{"foo": 1, "bar": 2})
|
|
|
|
|
|
def test_forget(self):
|
|
|
- tb = CacheBackend(backend="memory://")
|
|
|
- tid = uuid()
|
|
|
- tb.mark_as_done(tid, {"foo": "bar"})
|
|
|
- x = AsyncResult(tid, backend=tb)
|
|
|
+ self.tb.mark_as_done(self.tid, {"foo": "bar"})
|
|
|
+ x = AsyncResult(self.tid, backend=self.tb)
|
|
|
x.forget()
|
|
|
self.assertIsNone(x.result)
|
|
|
|
|
|
def test_process_cleanup(self):
|
|
|
- tb = CacheBackend(backend="memory://")
|
|
|
- tb.process_cleanup()
|
|
|
+ self.tb.process_cleanup()
|
|
|
|
|
|
def test_expires_as_int(self):
|
|
|
tb = CacheBackend(backend="memory://", expires=10)
|
|
@@ -129,8 +153,8 @@ class MockCacheMixin(object):
|
|
|
class test_get_best_memcache(unittest.TestCase, MockCacheMixin):
|
|
|
|
|
|
def test_pylibmc(self):
|
|
|
- with reset_modules("celery.backends.cache"):
|
|
|
- with self.mock_pylibmc():
|
|
|
+ with self.mock_pylibmc():
|
|
|
+ with reset_modules("celery.backends.cache"):
|
|
|
from celery.backends import cache
|
|
|
cache._imp = [None]
|
|
|
self.assertEqual(cache.get_best_memcache().__module__,
|
|
@@ -157,6 +181,7 @@ class test_get_best_memcache(unittest.TestCase, MockCacheMixin):
|
|
|
with self.mock_pylibmc():
|
|
|
with reset_modules("celery.backends.cache"):
|
|
|
from celery.backends import cache
|
|
|
+ cache._imp = [None]
|
|
|
cache.get_best_memcache(behaviors={"foo": "bar"})
|
|
|
self.assertTrue(cache._imp[0])
|
|
|
cache.get_best_memcache()
|