Commit e8bcec7f by Piotr Mitros

Added test for memoize_query

parent 89be6f41
...@@ -127,7 +127,7 @@ def memoize_query(cache_time = 60*4, timeout = 60*15): ...@@ -127,7 +127,7 @@ def memoize_query(cache_time = 60*4, timeout = 60*15):
# process of computing it # process of computing it
cached = cache.get(key) cached = cache.get(key)
if cached: if cached:
print "Cache hit", key #print "Cache hit", key
# If we're already computing it, wait to finish # If we're already computing it, wait to finish
# computation # computation
while cached == 'Processing': while cached == 'Processing':
...@@ -139,7 +139,7 @@ def memoize_query(cache_time = 60*4, timeout = 60*15): ...@@ -139,7 +139,7 @@ def memoize_query(cache_time = 60*4, timeout = 60*15):
results = cached results = cached
if not cached: if not cached:
print "Cache miss", key #print "Cache miss", key
# HACK: There's a slight race condition here, where we # HACK: There's a slight race condition here, where we
# might recompute twice. # might recompute twice.
cache.set(key, 'Processing', timeout) cache.set(key, 'Processing', timeout)
...@@ -171,20 +171,3 @@ def cron_new(period, params=None): ...@@ -171,20 +171,3 @@ def cron_new(period, params=None):
f(fs, db, params) f(fs, db, params)
return decorator(run,f) return decorator(run,f)
return factory return factory
if False:
# Test case. Should be made into a proper test case.
@memoize_query(1)
def test_function(x):
print "Boo", x, ">",2*x
return 2*x
print test_function(2)
print test_function(4)
print test_function(2)
print test_function(4)
time.sleep(2)
print test_function(2)
print test_function(4)
print test_function(2)
print test_function(4)
...@@ -4,9 +4,11 @@ when you run "manage.py test". ...@@ -4,9 +4,11 @@ when you run "manage.py test".
Replace this with more appropriate tests for your application. Replace this with more appropriate tests for your application.
""" """
import time
from django.test import TestCase from django.test import TestCase
from decorators import memoize_query
class SimpleTest(TestCase): class SimpleTest(TestCase):
def test_basic_addition(self): def test_basic_addition(self):
...@@ -14,3 +16,26 @@ class SimpleTest(TestCase): ...@@ -14,3 +16,26 @@ class SimpleTest(TestCase):
Tests that 1 + 1 always equals 2. Tests that 1 + 1 always equals 2.
""" """
self.assertEqual(1 + 1, 2) self.assertEqual(1 + 1, 2)
def __init__(self, arg):
TestCase.__init__(self, arg)
self.calls = 0
def test_memoize(self):
self.calls = 0
@memoize_query(0.05)
def double_trouble(x):
self.calls = self.calls + 1
return 2*x
self.assertEqual(double_trouble(2), 4)
self.assertEqual(double_trouble(4), 8)
self.assertEqual(double_trouble(2), 4)
self.assertEqual(double_trouble(4), 8)
self.assertEqual(self.calls, 2)
time.sleep(0.1)
self.assertEqual(double_trouble(2), 4)
self.assertEqual(double_trouble(4), 8)
self.assertEqual(double_trouble(2), 4)
self.assertEqual(double_trouble(4), 8)
self.assertEqual(self.calls, 4)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment