Skip to content

Commit

Permalink
tests passing on Py2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
ask committed Jan 26, 2011
1 parent d35ceb0 commit 8a8a18b
Show file tree
Hide file tree
Showing 58 changed files with 82 additions and 49 deletions.
39 changes: 24 additions & 15 deletions celery/backends/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@
from celery.utils import timeutils
from celery.datastructures import LocalCache

_imp = [None]

def get_best_memcache(*args, **kwargs):
behaviors = kwargs.pop("behaviors", None)
is_pylibmc = False
try:
import pylibmc as memcache
is_pylibmc = True
except ImportError:
def import_best_memcache():
if _imp[0] is None:
is_pylibmc = False
try:
import memcache
import pylibmc as memcache
is_pylibmc = True
except ImportError:
raise ImproperlyConfigured("Memcached backend requires either "
"the 'memcache' or 'pylibmc' library")
try:
import memcache
except ImportError:
raise ImproperlyConfigured(
"Memcached backend requires either the 'pylibmc' "
"or 'memcache' library")
_imp[0] = is_pylibmc, memcache
return _imp[0]


def get_best_memcache(*args, **kwargs):
behaviors = kwargs.pop("behaviors", None)
is_pylibmc, memcache = import_best_memcache()
client = memcache.Client(*args, **kwargs)
if is_pylibmc and behaviors is not None:
client.behaviors = behaviors
Expand All @@ -42,10 +51,10 @@ def delete(self, key, *args, **kwargs):
self.cache.pop(key, None)


backends = {"memcache": get_best_memcache,
"memcached": get_best_memcache,
"pylibmc": get_best_memcache,
"memory": DummyClient}
backends = {"memcache": lambda: get_best_memcache,
"memcached": lambda: get_best_memcache,
"pylibmc": lambda: get_best_memcache,
"memory": lambda: DummyClient}


class CacheBackend(KeyValueStoreBackend):
Expand All @@ -64,7 +73,7 @@ def __init__(self, expires=None, backend=None, options={}, **kwargs):
self.backend, _, servers = partition(backend, "://")
self.servers = servers.split(";")
try:
self.Client = backends[self.backend]
self.Client = backends[self.backend]()
except KeyError:
raise ImproperlyConfigured(
"Unknown cache backend: %s. Please use one of the "
Expand Down
1 change: 0 additions & 1 deletion celery/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
os.environ["EVENTLET_NOPATCH"] = "yes"
os.environ["GEVENT_NOPATCH"] = "yes"


try:
WindowsError = WindowsError
except NameError:
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ def _get_test_config():
class test_App(unittest.TestCase):

def setUp(self):
self.app = Celery()
self.app = Celery(set_as_current=False)
self.app.conf.update(test_config)

def test_task(self):
app = Celery("foozibari")
app = Celery("foozibari", set_as_current=False)

def fun():
pass
Expand Down Expand Up @@ -73,7 +73,8 @@ def execute_from_commandline(self, argv):
celeryd.WorkerCommand = prev

def test_config_from_envvar(self):
os.environ["CELERYTEST_CONFIG_OBJECT"] = "celery.tests.test_app"
os.environ["CELERYTEST_CONFIG_OBJECT"] = \
"celery.tests.test_app.test_app"
self.app.config_from_envvar("CELERYTEST_CONFIG_OBJECT")
self.assertEqual(self.app.conf.THIS_IS_A_KEY, "this is a value")

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 11 additions & 7 deletions celery/tests/test_backends/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ def test_unknown_backend_raises_ImproperlyConfigured(self):
CacheBackend, backend="unknown://")


class MyClient(DummyClient):
pass


class test_get_best_memcache(unittest.TestCase):

def mock_memcache(self):
memcache = types.ModuleType("memcache")
memcache.Client = DummyClient
memcache.Client = MyClient
memcache.Client.__module__ = memcache.__name__
prev, sys.modules["memcache"] = sys.modules.get("memcache"), memcache
yield True
Expand All @@ -89,7 +93,7 @@ def mock_memcache(self):

def mock_pylibmc(self):
pylibmc = types.ModuleType("pylibmc")
pylibmc.Client = DummyClient
pylibmc.Client = MyClient
pylibmc.Client.__module__ = pylibmc.__name__
prev = sys.modules.get("pylibmc")
sys.modules["pylibmc"] = pylibmc
Expand All @@ -101,16 +105,16 @@ def mock_pylibmc(self):
def test_pylibmc(self):
pylibmc = self.mock_pylibmc()
pylibmc.next()
sys.modules.pop("celery.backends.cache", None)
from celery.backends import cache
cache._imp = [None]
self.assertEqual(cache.get_best_memcache().__module__, "pylibmc")
pylibmc.next()

def test_memcache(self):
def xxx_memcache(self):

def with_no_pylibmc():
sys.modules.pop("celery.backends.cache", None)
from celery.backends import cache
cache._imp = [None]
self.assertEqual(cache.get_best_memcache().__module__, "memcache")

context = mask_modules("pylibmc")
Expand All @@ -123,11 +127,11 @@ def with_no_pylibmc():
finally:
context.__exit__(None, None, None)

def test_no_implementations(self):
def xxx_no_implementations(self):

def with_no_memcache_libs():
sys.modules.pop("celery.backends.cache", None)
from celery.backends import cache
cache._imp = [None]
self.assertRaises(ImproperlyConfigured, cache.get_best_memcache)

context = mask_modules("pylibmc", "memcache")
Expand Down
14 changes: 8 additions & 6 deletions celery/tests/test_backends/test_redis_unit.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from datetime import timedelta

from celery import current_app
from celery import states
from celery.app import app_or_default
from celery.utils import gen_unique_id
from celery.utils.timeutils import timedelta_seconds

from celery.tests.utils import unittest

Expand Down Expand Up @@ -55,22 +56,23 @@ def setUp(self):
self.Backend = self.get_backend()

def test_expires_defaults_to_config(self):
app = app_or_default()
prev = app.conf.CELERY_AMQP_TASK_RESULT_EXPIRES
app.conf.CELERY_TASK_RESULT_EXPIRES = 10
conf = current_app.conf
prev = conf.CELERY_TASK_RESULT_EXPIRES
conf.CELERY_TASK_RESULT_EXPIRES = 10
try:
b = self.Backend(expires=None)
self.assertEqual(b.expires, 10)
finally:
app.conf.CELERY_TASK_RESULT_EXPIRES = prev
conf.CELERY_TASK_RESULT_EXPIRES = prev

def test_expires_is_int(self):
b = self.Backend(expires=48)
self.assertEqual(b.expires, 48)

def test_expires_is_None(self):
b = self.Backend(expires=None)
self.assertIsNone(b.expires)
self.assertEqual(b.expires, timedelta_seconds(
current_app.conf.CELERY_TASK_RESULT_EXPIRES))

def test_expires_is_timedelta(self):
b = self.Backend(expires=timedelta(minutes=1))
Expand Down
4 changes: 2 additions & 2 deletions celery/tests/test_bin/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from celery.bin.base import Command

from celery.tests.utils import unittest
from celery.tests.utils import AppCase


class Object(object):
Expand All @@ -27,7 +27,7 @@ def run(self, *args, **kwargs):
return args, kwargs


class test_Command(unittest.TestCase):
class test_Command(AppCase):

def test_get_options(self):
cmd = Command()
Expand Down
10 changes: 5 additions & 5 deletions celery/tests/test_bin/test_celerybeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from celery.apps import beat as beatapp
from celery.utils.compat import defaultdict

from celery.tests.utils import unittest
from celery.tests.utils import AppCase


class MockedShelveModule(object):
Expand Down Expand Up @@ -54,7 +54,7 @@ def install_sync_handler(self, b):
raise TypeError("xxx")


class test_Beat(unittest.TestCase):
class test_Beat(AppCase):

def test_loglevel_string(self):
b = beatapp.Beat(loglevel="DEBUG")
Expand Down Expand Up @@ -170,14 +170,14 @@ def create_daemon_context(*args, **kwargs):
return context, context.close


class test_div(unittest.TestCase):
class test_div(AppCase):

def setUp(self):
def setup(self):
self.prev, beatapp.Beat = beatapp.Beat, MockBeat
self.ctx, celerybeat_bin.create_daemon_context = \
celerybeat_bin.create_daemon_context, create_daemon_context

def tearDown(self):
def teardown(self):
beatapp.Beat = self.prev

def test_main(self):
Expand Down
8 changes: 4 additions & 4 deletions celery/tests/test_bin/test_celeryd.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from celery.tests.compat import catch_warnings
from celery.tests.utils import execute_context
from celery.tests.utils import unittest
from celery.tests.utils import AppCase
from celery.tests.utils import StringIO


Expand Down Expand Up @@ -59,7 +59,7 @@ class Worker(cd.Worker):
WorkController = _WorkController


class test_Worker(unittest.TestCase):
class test_Worker(AppCase):
Worker = Worker

@disable_stdouts
Expand Down Expand Up @@ -310,7 +310,7 @@ def on_worker_ready(**kwargs):
self.assertTrue(worker_ready_sent[0])


class test_funs(unittest.TestCase):
class test_funs(AppCase):

@redirect_stdouts
def test_windows_main(self, stdout, stderr):
Expand Down Expand Up @@ -361,7 +361,7 @@ def test_main(self):
sys.argv = s


class test_signal_handlers(unittest.TestCase):
class test_signal_handlers(AppCase):

class _Worker(object):
stopped = False
Expand Down
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from celery.tests.compat import catch_warnings
from celery.tests.utils import unittest
from celery.tests.utils import execute_context, skip
from celery.tests.utils import AppCase, execute_context, skip


class MockConsumer(object):
Expand Down Expand Up @@ -640,16 +640,16 @@ def raises_socket_error(limit=None):
self.assertEqual(l.iterations, 1)


class test_WorkController(unittest.TestCase):
class test_WorkController(AppCase):

def setup(self):
self.worker = self.create_worker()

def create_worker(self, **kw):
worker = WorkController(concurrency=1, loglevel=0, **kw)
worker.logger = MockLogger()
return worker

def setUp(self):
self.worker = self.create_worker()

def test_process_initializer(self):
from celery import Celery
from celery import platforms
Expand All @@ -662,7 +662,7 @@ def test_process_initializer(self):
reset_signals = []
worker_init = [False]
default_app = app_or_default()
app = Celery(loader="default")
app = Celery(loader="default", set_as_current=False)

class Loader(object):

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions celery/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@
from celery.utils.functional import wraps


class AppCase(unittest.TestCase):

def setUp(self):
from celery.app import current_app
self._current_app = current_app()
self.setup()

def tearDown(self):
self.teardown()
self._current_app.set_current()

def setup(self):
pass

def teardown(self):
pass


class GeneratorContextManager(object):
def __init__(self, gen):
self.gen = gen
Expand Down

0 comments on commit 8a8a18b

Please sign in to comment.