Skip to content

Commit

Permalink
Fix cache tests for new result setup
Browse files Browse the repository at this point in the history
  • Loading branch information
bbangert committed Feb 1, 2012
1 parent 2d06e12 commit 711fd9d
Showing 1 changed file with 52 additions and 8 deletions.
60 changes: 52 additions & 8 deletions retools/tests/test_cache.py
Expand Up @@ -2,6 +2,7 @@
import unittest
import time
import cPickle
from contextlib import nested

import redis
import redis.client
Expand Down Expand Up @@ -183,7 +184,7 @@ def side_effect(*args, **kwargs):
CR = self._makeOne()
CR.add_region('short_term', 60)

def a_func():
def a_func(): # pragma: nocover
return "This is a value: %s" % time.time()
value = CR.load('short_term', 'my_func', '1 2 3', callable=a_func)
assert 'This is a NEW value' in value
Expand Down Expand Up @@ -214,17 +215,22 @@ def a_func(): # pragma: nocover
if x[0] == 'execute']
eq_(len(exec_calls), 1)

def test_existing_expired_value_with_lock(self):
def test_existing_expired_value_with_lock_timeout(self):
mock_redis = Mock(spec=redis.client.Redis)
mock_pipeline = Mock(spec=redis.client.Pipeline)
now = time.time()
results = [0, ({'created': now - 200,
'value': cPickle.dumps("This is a value")},
'0')]

def side_effect():
def side_effect(*args):
return results.pop()

# Mock up an existing lock
mock_redis.setnx.return_value = False
mock_redis.get.return_value = False

# Other mocks
mock_redis.exists.return_value = True
mock_redis.pipeline.return_value = mock_pipeline
mock_pipeline.execute.side_effect = side_effect
Expand All @@ -244,10 +250,48 @@ def a_func(): # pragma: nocover
if x[0] == 'execute']
eq_(len(exec_calls), 1)

def test_no_value_with_lock_timeout(self):
from retools.cache import NoneMarker
mock_redis = Mock(spec=redis.client.Redis)
mock_pipeline = Mock(spec=redis.client.Pipeline)
results = [0, ({}, '0')]

def side_effect(*args):
return results.pop()

# Mock up an existing lock
mock_redis.setnx.return_value = False
mock_redis.get.return_value = False

# Other mocks
mock_redis.exists.return_value = True
mock_redis.pipeline.return_value = mock_pipeline
mock_pipeline.execute.side_effect = side_effect

mock_sleep = Mock(spec=time.sleep)

with nested(
patch('retools.global_connection._redis', mock_redis),
patch('time.sleep', mock_sleep)
):
CR = self._makeOne()
CR.add_region('short_term', 60)

called = []

def a_func(): # pragma: nocover
called.append(1)
return "This is a value: %s" % time.time()
value = CR.load('short_term', 'my_func', '1 2 3', callable=a_func)
eq_(value, NoneMarker)
eq_(called, [])
exec_calls = [x for x in mock_pipeline.method_calls \
if x[0] == 'execute']
eq_(len(exec_calls), 1)

def test_new_value_and_misses(self):
mock_redis = Mock(spec=redis.client.Redis)
mock_pipeline = Mock(spec=redis.client.Pipeline)
now = time.time()
results = [None, ['30'], (None, '0')]

def side_effect(*args, **kwargs):
Expand Down Expand Up @@ -366,7 +410,7 @@ def _makeCR(self):

def test_invalidate_function_without_args(self):

def my_func():
def my_func(): # pragma: nocover
return "Hello"
my_func._region = 'short_term'
my_func._namespace = 'retools:a_key'
Expand All @@ -389,7 +433,7 @@ def my_func():

def test_invalidate_function_with_args(self):

def my_func(name):
def my_func(name): # pragma: nocover
return "Hello %s" % name
my_func._region = 'short_term'
my_func._namespace = 'retools:a_key decarg'
Expand Down Expand Up @@ -439,10 +483,10 @@ def test_it():
CR = self._makeOne()
CR.add_region('short_term', 60)

def dummy_func():
def dummy_func(): # pragma: nocover
return "This is a value: %s" % time.time()
decorated = self._decorateFunc(dummy_func, 'long_term')
value = decorated()
decorated()
test_it()

def test_generate(self):
Expand Down

0 comments on commit 711fd9d

Please sign in to comment.