mock 'time()' in cache tests
instead of calling 'sleep()' to let time advance. This shortens the time needed to run those tests, and ensures consistent results. (Tests would randomly fail when using 'sleep()')
This commit is contained in:
@@ -57,7 +57,7 @@ class MemoryCacheDecorator(CacheDecorator):
|
|||||||
value, expires = self.cache[key]
|
value, expires = self.cache[key]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
expires = 0
|
expires = 0
|
||||||
if expires < timestamp:
|
if expires <= timestamp:
|
||||||
value = self.func(*args, **kwargs)
|
value = self.func(*args, **kwargs)
|
||||||
expires = timestamp + self.maxage
|
expires = timestamp + self.maxage
|
||||||
self.cache[key] = value, expires
|
self.cache[key] = value, expires
|
||||||
|
|||||||
@@ -10,8 +10,8 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
import time
|
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
@@ -86,29 +86,47 @@ class TestCache(unittest.TestCase):
|
|||||||
self.assertEqual(ka(9, 9, 2), 6)
|
self.assertEqual(ka(9, 9, 2), 6)
|
||||||
|
|
||||||
def test_expires_mem(self):
|
def test_expires_mem(self):
|
||||||
@cache.memcache(maxage=1)
|
@cache.memcache(maxage=2)
|
||||||
def ex(a, b, c):
|
def ex(a, b, c):
|
||||||
return a+b+c
|
return a+b+c
|
||||||
|
|
||||||
|
with patch("time.time") as tmock:
|
||||||
|
tmock.return_value = 0.001
|
||||||
self.assertEqual(ex(1, 1, 1), 3)
|
self.assertEqual(ex(1, 1, 1), 3)
|
||||||
self.assertEqual(ex(2, 2, 2), 3)
|
self.assertEqual(ex(2, 2, 2), 3)
|
||||||
self.assertEqual(ex(3, 3, 3), 3)
|
self.assertEqual(ex(3, 3, 3), 3)
|
||||||
|
|
||||||
time.sleep(2)
|
# value is still cached after 1 second
|
||||||
|
tmock.return_value += 1.0
|
||||||
|
self.assertEqual(ex(3, 3, 3), 3)
|
||||||
|
self.assertEqual(ex(2, 2, 2), 3)
|
||||||
|
self.assertEqual(ex(1, 1, 1), 3)
|
||||||
|
|
||||||
|
# new value after 'maxage' seconds
|
||||||
|
tmock.return_value += 1.0
|
||||||
self.assertEqual(ex(3, 3, 3), 9)
|
self.assertEqual(ex(3, 3, 3), 9)
|
||||||
self.assertEqual(ex(2, 2, 2), 9)
|
self.assertEqual(ex(2, 2, 2), 9)
|
||||||
self.assertEqual(ex(1, 1, 1), 9)
|
self.assertEqual(ex(1, 1, 1), 9)
|
||||||
|
|
||||||
def test_expires_db(self):
|
def test_expires_db(self):
|
||||||
@cache.cache(maxage=1)
|
@cache.cache(maxage=2)
|
||||||
def ex(a, b, c):
|
def ex(a, b, c):
|
||||||
return a+b+c
|
return a+b+c
|
||||||
|
|
||||||
|
with patch("time.time") as tmock:
|
||||||
|
tmock.return_value = 0.999
|
||||||
self.assertEqual(ex(1, 1, 1), 3)
|
self.assertEqual(ex(1, 1, 1), 3)
|
||||||
self.assertEqual(ex(2, 2, 2), 3)
|
self.assertEqual(ex(2, 2, 2), 3)
|
||||||
self.assertEqual(ex(3, 3, 3), 3)
|
self.assertEqual(ex(3, 3, 3), 3)
|
||||||
|
|
||||||
time.sleep(2)
|
# value is still cached after 1 second
|
||||||
|
tmock.return_value += 1.0
|
||||||
|
self.assertEqual(ex(3, 3, 3), 3)
|
||||||
|
self.assertEqual(ex(2, 2, 2), 3)
|
||||||
|
self.assertEqual(ex(1, 1, 1), 3)
|
||||||
|
|
||||||
|
# new value after 'maxage' seconds
|
||||||
|
tmock.return_value += 1.0
|
||||||
self.assertEqual(ex(3, 3, 3), 9)
|
self.assertEqual(ex(3, 3, 3), 9)
|
||||||
self.assertEqual(ex(2, 2, 2), 9)
|
self.assertEqual(ex(2, 2, 2), 9)
|
||||||
self.assertEqual(ex(1, 1, 1), 9)
|
self.assertEqual(ex(1, 1, 1), 9)
|
||||||
|
|||||||
Reference in New Issue
Block a user