|
| 1 | +import sys |
| 2 | +import types |
| 3 | +import unittest2 as unittest |
| 4 | + |
| 5 | +from celery import states |
| 6 | +from celery.backends.cache import CacheBackend, get_best_memcache, DummyClient |
| 7 | +from celery.exceptions import ImproperlyConfigured |
| 8 | +from celery.utils import gen_unique_id |
| 9 | + |
| 10 | +from celery.tests.utils import execute_context, mask_modules |
| 11 | + |
| 12 | + |
| 13 | +class SomeClass(object): |
| 14 | + |
| 15 | + def __init__(self, data): |
| 16 | + self.data = data |
| 17 | + |
| 18 | + |
| 19 | +class test_CacheBackend(unittest.TestCase): |
| 20 | + |
| 21 | + def test_mark_as_done(self): |
| 22 | + tb = CacheBackend(backend="memory://") |
| 23 | + |
| 24 | + tid = gen_unique_id() |
| 25 | + |
| 26 | + self.assertEqual(tb.get_status(tid), states.PENDING) |
| 27 | + self.assertIsNone(tb.get_result(tid)) |
| 28 | + |
| 29 | + tb.mark_as_done(tid, 42) |
| 30 | + self.assertEqual(tb.get_status(tid), states.SUCCESS) |
| 31 | + self.assertEqual(tb.get_result(tid), 42) |
| 32 | + |
| 33 | + def test_is_pickled(self): |
| 34 | + tb = CacheBackend(backend="memory://") |
| 35 | + |
| 36 | + tid2 = gen_unique_id() |
| 37 | + result = {"foo": "baz", "bar": SomeClass(12345)} |
| 38 | + tb.mark_as_done(tid2, result) |
| 39 | + # is serialized properly. |
| 40 | + rindb = tb.get_result(tid2) |
| 41 | + self.assertEqual(rindb.get("foo"), "baz") |
| 42 | + self.assertEqual(rindb.get("bar").data, 12345) |
| 43 | + |
| 44 | + def test_mark_as_failure(self): |
| 45 | + tb = CacheBackend(backend="memory://") |
| 46 | + |
| 47 | + tid3 = gen_unique_id() |
| 48 | + try: |
| 49 | + raise KeyError("foo") |
| 50 | + except KeyError, exception: |
| 51 | + pass |
| 52 | + tb.mark_as_failure(tid3, exception) |
| 53 | + self.assertEqual(tb.get_status(tid3), states.FAILURE) |
| 54 | + self.assertIsInstance(tb.get_result(tid3), KeyError) |
| 55 | + |
| 56 | + def test_process_cleanup(self): |
| 57 | + tb = CacheBackend(backend="memory://") |
| 58 | + tb.process_cleanup() |
| 59 | + |
| 60 | + def test_expires_as_int(self): |
| 61 | + tb = CacheBackend(backend="memory://", expires=10) |
| 62 | + self.assertEqual(tb.expires, 10) |
| 63 | + |
| 64 | + def test_unknown_backend_raises_ImproperlyConfigured(self): |
| 65 | + self.assertRaises(ImproperlyConfigured, |
| 66 | + CacheBackend, backend="unknown://") |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +class test_get_best_memcache(unittest.TestCase): |
| 71 | + |
| 72 | + def mock_memcache(self): |
| 73 | + memcache = types.ModuleType("memcache") |
| 74 | + memcache.Client = DummyClient |
| 75 | + memcache.Client.__module__ = memcache.__name__ |
| 76 | + prev, sys.modules["memcache"] = sys.modules.get("memcache"), memcache |
| 77 | + yield |
| 78 | + if prev is not None: |
| 79 | + sys.modules["memcache"] = prev |
| 80 | + yield |
| 81 | + |
| 82 | + def mock_pylibmc(self): |
| 83 | + pylibmc = types.ModuleType("pylibmc") |
| 84 | + pylibmc.Client = DummyClient |
| 85 | + pylibmc.Client.__module__ = pylibmc.__name__ |
| 86 | + prev = sys.modules.get("pylibmc") |
| 87 | + sys.modules["pylibmc"] = pylibmc |
| 88 | + yield |
| 89 | + if prev is not None: |
| 90 | + sys.modules["pylibmc"] = prev |
| 91 | + yield |
| 92 | + |
| 93 | + def test_pylibmc(self): |
| 94 | + pylibmc = self.mock_pylibmc() |
| 95 | + pylibmc.next() |
| 96 | + import __builtin__ |
| 97 | + sys.modules.pop("celery.backends.cache", None) |
| 98 | + from celery.backends import cache |
| 99 | + self.assertEqual(cache.get_best_memcache().__module__, "pylibmc") |
| 100 | + pylibmc.next() |
| 101 | + |
| 102 | + def test_memcache(self): |
| 103 | + |
| 104 | + def with_no_pylibmc(): |
| 105 | + sys.modules.pop("celery.backends.cache", None) |
| 106 | + from celery.backends import cache |
| 107 | + self.assertEqual(cache.get_best_memcache().__module__, "memcache") |
| 108 | + |
| 109 | + context = mask_modules("pylibmc") |
| 110 | + context.__enter__() |
| 111 | + try: |
| 112 | + memcache = self.mock_memcache() |
| 113 | + memcache.next() |
| 114 | + with_no_pylibmc() |
| 115 | + memcache.next() |
| 116 | + finally: |
| 117 | + context.__exit__(None, None, None) |
| 118 | + |
| 119 | + def test_no_implementations(self): |
| 120 | + |
| 121 | + def with_no_memcache_libs(): |
| 122 | + sys.modules.pop("celery.backends.cache", None) |
| 123 | + from celery.backends import cache |
| 124 | + self.assertRaises(ImproperlyConfigured, cache.get_best_memcache) |
| 125 | + |
| 126 | + context = mask_modules("pylibmc", "memcache") |
| 127 | + context.__enter__() |
| 128 | + try: |
| 129 | + with_no_memcache_libs() |
| 130 | + finally: |
| 131 | + context.__exit__(None, None, None) |
0 commit comments