Skip to content

Commit

Permalink
Fix issue in SetupMixin (crash when test packages are launch individu…
Browse files Browse the repository at this point in the history
…aly). Fix issue due to integer overflow and python < 3 (converted to long). Fix redis version in order to have SCAN command.
  • Loading branch information
joehybird committed Jan 15, 2019
1 parent 777d4d5 commit ae33e69
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions install_redis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
: ${REDIS_VERSION:="4.0.11"}

test -d redis || git clone https://github.com/antirez/redis
git -C redis fetch
git -C redis checkout $REDIS_VERSION
make -C redis
5 changes: 4 additions & 1 deletion redis_cache/sharder.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ def __init__(self, node, i):
self._position = get_slot(key)

def __gt__(self, other):
if isinstance(other, int):
# WARNING PYTHON < 3 : in get_slot() if the hash value overflows integer a LONG is returned !
if isinstance(other, (int, long)):
return self._position > other
elif isinstance(other, Node):
return self._position > other._position
raise TypeError(
'Cannot compare this class with "%s" type' % type(other)
)

def __str__(self):
return 'Node(position=%s, i=%s, node=%s)' % (self._position, self._i, self.node)

class HashRing(object):

Expand Down
4 changes: 4 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

DEBUG = True

DATABASES = {
Expand Down Expand Up @@ -33,3 +35,5 @@
}
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
MIDDLEWARE_CLASSES = tuple()

TEST_REDIS_SERVER = os.getenv('REDIS_CACHE_TEST_SERVER', './redis/src/redis-server')
15 changes: 12 additions & 3 deletions tests/testapp/tests/base_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
except ImportError:
import pickle

from django.conf import settings
from django.core.cache import caches
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase, override_settings
Expand Down Expand Up @@ -52,6 +53,12 @@ def start_redis_servers(servers, db=None, master=None):
db=db,
password=REDIS_PASSWORD
)
server_path = getattr(
settings,
'TEST_REDIS_SERVER',
'./redis/src/redis-server',
)

for i, server in enumerate(servers):
connection_kwargs = parse_connection_kwargs(
server,
Expand All @@ -78,7 +85,7 @@ def start_redis_servers(servers, db=None, master=None):
)
)

args = ['./redis/src/redis-server'] + [
args = [server_path] + [
"--{parameter} {value}".format(parameter=parameter, value=value)
for parameter, value in parameters.items()
]
Expand All @@ -93,8 +100,10 @@ class SetupMixin(object):

@classmethod
def tearDownClass(cls):
for p in cls.processes:
p.kill()
if cls.processes:
for p in cls.processes:
p.kill()

cls.processes = None

# Give redis processes some time to shutdown
Expand Down
10 changes: 7 additions & 3 deletions tests/testapp/tests/serializers_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ class BaseSerializerTestCase(SetupMixin, TestCase):
converts_tuple_to_list = False
serializes_objects = True

def assertion_message(self, name):
return '%s %s' % (self.__class__.__name__, name)

def test_string(self):
self.cache.set('a', 'a')
self.assertEqual(self.cache.get('a'), 'a')
self.assertEqual(self.cache.get('a'), 'a', self.assertion_message('test_string'))

def test_unicode(self):
self.cache.set('Iñtërnâtiônàlizætiøn', 'Iñtërnâtiônàlizætiøn2')
self.assertEqual(
self.cache.get('Iñtërnâtiônàlizætiøn'),
'Iñtërnâtiônàlizætiøn2'
'Iñtërnâtiônàlizætiøn2',
self.assertion_message('test_unicode')
)

def test_number(self):
Expand Down Expand Up @@ -67,7 +71,7 @@ def test_dictionary(self):
'function': f,
'class': C,
})
self.assertEqual(stuff, data)
self.assertEqual(stuff, data, self.assertion_message('test_dictionary'))


@override_settings(CACHES={
Expand Down

0 comments on commit ae33e69

Please sign in to comment.