Commit 28cdbbf9 authored by Sebastien Robin's avatar Sebastien Robin

various improvements on testCacheTool

- avoid random failures by stopping relying on calculation time
- really make sure if the cached method is called or not
- make the test much faster by avoiding long calculation that
  are not needed any more
parent 5b036203
...@@ -185,20 +185,13 @@ class TestCacheTool(ERP5TypeTestCase): ...@@ -185,20 +185,13 @@ class TestCacheTool(ERP5TypeTestCase):
def createCachedMethod(self): def createCachedMethod(self):
portal = self.getPortal() portal = self.getPortal()
if getattr(portal, self.python_script_id, None) is not None: if getattr(portal, self.python_script_id, None) is not None:
return portal.manage_delObjects(ids=[self.python_script_id])
## add test cached method ## add test cached method
py_script_params = "value=10000, portal_path=('','erp5'), result=''" py_script_params = "value=10000, portal_path=('','erp5'), result=''"
py_script_body = """ py_script_body = """
def veryExpensiveMethod(value): portal = context.getPortalObject()
## do something expensive for some time portal.newCacheCookie('cache_tool_test')
## no 'time.sleep()' available in Zope
## so concatenate strings
s = ""
for i in range(0, value):
s = str(value*value*value) + s
return value
veryExpensiveMethod(value)
return result return result
""" """
portal.manage_addProduct['PythonScripts'].manage_addPythonScript( portal.manage_addProduct['PythonScripts'].manage_addPythonScript(
...@@ -230,87 +223,79 @@ return result ...@@ -230,87 +223,79 @@ return result
cache_factory=cf_name) cache_factory=cf_name)
self._cacheFactoryInstanceTest(my_cache, cf_name, clear_allowed=False) self._cacheFactoryInstanceTest(my_cache, cf_name, clear_allowed=False)
def _cacheFactoryInstanceTest(self, my_cache, cf_name, clear_allowed):
def _getCacheCookieValue(self):
portal = self.getPortal() portal = self.getPortal()
print return portal.getCacheCookie('cache_tool_test')
print "="*40
print "TESTING:", cf_name
# if the test fails because your machine is too fast, increase this value. def _callCache(self, my_cache, real_calculation=False, result=""):
result = 'a short value' portal = self.getPortal()
portal.portal_caches.clearCacheFactory(cf_name) before_cookie_value = self._getCacheCookieValue()
## 1st call
start = time.time() start = time.time()
cached = my_cache(self.nb_iterations, cached = my_cache(self.nb_iterations,
portal_path=('', portal.getId()), portal_path=('', portal.getId()),
result=result) result=result)
end = time.time() end = time.time()
calculation_time = end-start calculation_time = end-start
print "\n\tCalculation time (1st call)", calculation_time
self.assertEquals(cached, result) self.assertEquals(cached, result)
transaction.commit() after_cookie_value = self._getCacheCookieValue()
# if there is cache miss, then real calculation is done,
# then the cookie is increased with a value 1
self.assertEquals(after_cookie_value-before_cookie_value,
int(real_calculation))
return calculation_time
## 2nd call - should be cached now def _cacheFactoryInstanceTest(self, my_cache, cf_name, clear_allowed):
start = time.time() portal = self.getPortal()
cached = my_cache(self.nb_iterations, print
print "="*40
print "TESTING:", cf_name
result = 'a short value'
#portal.portal_caches.clearCacheFactory(cf_name)
def clearCache():
my_cache.delete(self.nb_iterations,
portal_path=('', portal.getId()), portal_path=('', portal.getId()),
result=result) result=result)
end = time.time()
calculation_time = end-start
print "\n\tCalculation time (2nd call)", calculation_time
self.assertEquals(cached, result)
transaction.commit() transaction.commit()
# check if cache works by getting calculation_time for last cache # Make sure we do not have values in cache
# operation even remote cache must have access time less than a second. clearCache()
# if it's greater than method wasn't previously cached and was calculated
# instead
self.assertTrue(1.0 > calculation_time, "1.0 <= %s" % calculation_time)
## check if equal. # redefine callCache to avoid passing parameters all the time
self.assertEquals(cached, result) def callCache(real_calculation=True):
return self._callCache(my_cache, real_calculation=real_calculation,
result=result)
## 1st call
calculation_time = callCache(real_calculation=True)
print "\n\tCalculation time (1st call)", calculation_time
transaction.commit()
## 2nd call - should be cached now
calculation_time = callCache(real_calculation=False)
print "\n\tCalculation time (2nd call)", calculation_time
transaction.commit()
## OK so far let's clear cache ## OK so far let's clear cache
if clear_allowed: if clear_allowed:
portal.portal_caches.clearCacheFactory(cf_name) portal.portal_caches.clearCacheFactory(cf_name)
## 1st call ## 1st call
start = time.time() calculation_time = callCache(real_calculation=True)
cached = my_cache(self.nb_iterations,
portal_path=('', portal.getId()),
result=result)
end = time.time()
calculation_time = end-start
print "\n\tCalculation time (after cache clear)", calculation_time print "\n\tCalculation time (after cache clear)", calculation_time
## Cache cleared shouldn't be previously cached
self.assertTrue(1.0 < calculation_time, "1.0 >= %s" % calculation_time)
self.assertEquals(cached, result)
# Test delete method on CachingMethod # Test delete method on CachingMethod
print "\n\tCalculation time (3rd call)", calculation_time print "\n\tCalculation time (3rd call)", calculation_time
# fill the cache # make sure cache id filled
cached = my_cache(self.nb_iterations, calculation_time = callCache(real_calculation=False)
portal_path=('', portal.getId()),
result=result)
self.assertEquals(cached, result)
# Purge the Caching Method # Purge the Caching Method
my_cache.delete(self.nb_iterations, clearCache()
portal_path=('', portal.getId()),
result=result)
transaction.commit()
# Check that result is computed # Check that result is computed
start = time.time() calculation_time = callCache(real_calculation=True)
cached = my_cache(self.nb_iterations,
portal_path=('', portal.getId()),
result=result)
end = time.time()
calculation_time = end-start
print "\n\tCalculation time (4th call)", calculation_time print "\n\tCalculation time (4th call)", calculation_time
self.assertTrue(1.0 < calculation_time, "1.0 >= %s" % calculation_time)
self.assertEquals(cached, result)
transaction.commit() transaction.commit()
def test_03_cachePersistentObjects(self): def test_03_cachePersistentObjects(self):
...@@ -477,27 +462,17 @@ return 'a' * 1024 * 1024 * 25 ...@@ -477,27 +462,17 @@ return 'a' * 1024 * 1024 * 25
'distributed_persistent_cache_factory') 'distributed_persistent_cache_factory')
for cache_factory in cache_factory_list: for cache_factory in cache_factory_list:
print '\n\t==> %s' % cache_factory print '\n\t==> %s' % cache_factory
cached_method = CachingMethod(py_script_obj, my_cache = CachingMethod(py_script_obj,
'py_script_obj', 'py_script_obj',
cache_factory=cache_factory) cache_factory=cache_factory)
# First call, fill the cache # First call, fill the cache
start = time.time() calculation_time = self._callCache(my_cache, real_calculation=True)
cached_method(self.nb_iterations, portal_path='something')
transaction.commit()
end = time.time()
calculation_time = end-start
print "\n\tCalculation time (1st call)", calculation_time print "\n\tCalculation time (1st call)", calculation_time
self.assertTrue(calculation_time > 1.0, "%s <= 1.0" % calculation_time)
## 2nd call - should be cached now ## 2nd call - should be cached now
start = time.time() calculation_time = self._callCache(my_cache, real_calculation=False)
cached = cached_method(self.nb_iterations, portal_path='something')
transaction.commit()
end = time.time()
calculation_time = end-start
print "\n\tCalculation time (2nd call)", calculation_time print "\n\tCalculation time (2nd call)", calculation_time
self.assertTrue(calculation_time < 1.0, "%s >= 1.0" % calculation_time)
# Wait expiration period then check that value is computed # Wait expiration period then check that value is computed
# .1 is an additional epsilon delay to work around time precision issues # .1 is an additional epsilon delay to work around time precision issues
...@@ -506,13 +481,8 @@ return 'a' * 1024 * 1024 * 25 ...@@ -506,13 +481,8 @@ return 'a' * 1024 * 1024 * 25
time.sleep(time_left_to_wait) time.sleep(time_left_to_wait)
# Call conversion for ram_cache_factory # Call conversion for ram_cache_factory
start = time.time() calculation_time = self._callCache(my_cache, real_calculation=True)
cached = cached_method(self.nb_iterations, portal_path='something')
transaction.commit()
end = time.time()
calculation_time = end-start
print "\n\tCalculation time (3rd call)", calculation_time print "\n\tCalculation time (3rd call)", calculation_time
self.assertTrue(calculation_time > 1.0, "%s <= 1.0" % calculation_time)
def test_99_CachePluginInterface(self): def test_99_CachePluginInterface(self):
"""Test Class against Interface """Test Class against Interface
......
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