Commit eb519a76 authored by Nicolas Delaby's avatar Nicolas Delaby

Test cache_duration feature of CacheTool


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@34780 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 6f13d778
...@@ -34,6 +34,7 @@ from Testing import ZopeTestCase ...@@ -34,6 +34,7 @@ from Testing import ZopeTestCase
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.CachePlugins.DummyCache import DummyCache from Products.ERP5Type.CachePlugins.DummyCache import DummyCache
from AccessControl.SecurityManagement import newSecurityManager from AccessControl.SecurityManagement import newSecurityManager
from Products.ERP5Type.Cache import CachingMethod
from zLOG import LOG from zLOG import LOG
import transaction import transaction
...@@ -50,7 +51,8 @@ class TestingCache(DummyCache): ...@@ -50,7 +51,8 @@ class TestingCache(DummyCache):
class TestCacheTool(ERP5TypeTestCase): class TestCacheTool(ERP5TypeTestCase):
py_script_id = "testCachedMethod" cache_duration = 10 # second
python_script_id = "testCachedMethod"
def getTitle(self): def getTitle(self):
return "Cache Tool" return "Cache Tool"
...@@ -112,14 +114,16 @@ class TestCacheTool(ERP5TypeTestCase): ...@@ -112,14 +114,16 @@ class TestCacheTool(ERP5TypeTestCase):
if getattr(portal_caches, 'ram_cache_factory', None) is None: if getattr(portal_caches, 'ram_cache_factory', None) is None:
ram_cache_factory = portal_caches.newContent(portal_type="Cache Factory", ram_cache_factory = portal_caches.newContent(portal_type="Cache Factory",
id='ram_cache_factory', id='ram_cache_factory',
container=portal_caches) container=portal_caches,
cache_duration=self.cache_duration)
ram_cache_plugin = ram_cache_factory.newContent(portal_type="Ram Cache") ram_cache_plugin = ram_cache_factory.newContent(portal_type="Ram Cache")
ram_cache_plugin.setIntIndex(0) ram_cache_plugin.setIntIndex(0)
if getattr(portal_caches, 'another_ram_cache_factory', None) is None: if getattr(portal_caches, 'another_ram_cache_factory', None) is None:
cache_factory = portal_caches.newContent(portal_type="Cache Factory", cache_factory = portal_caches.newContent(portal_type="Cache Factory",
id='another_ram_cache_factory', id='another_ram_cache_factory',
container=portal_caches) container=portal_caches,
cache_duration=self.cache_duration)
cache_plugin = cache_factory.newContent(portal_type="Ram Cache") cache_plugin = cache_factory.newContent(portal_type="Ram Cache")
cache_plugin.setIntIndex(0) cache_plugin.setIntIndex(0)
...@@ -127,46 +131,50 @@ class TestCacheTool(ERP5TypeTestCase): ...@@ -127,46 +131,50 @@ class TestCacheTool(ERP5TypeTestCase):
## distributed_ram_cache_factory (to test Distributed Ram Cache Plugin) ## distributed_ram_cache_factory (to test Distributed Ram Cache Plugin)
dram_cache_factory = portal_caches.newContent(portal_type="Cache Factory", dram_cache_factory = portal_caches.newContent(portal_type="Cache Factory",
id='distributed_ram_cache_factory', id='distributed_ram_cache_factory',
container=portal_caches) container=portal_caches,
cache_duration=self.cache_duration)
dram_cache_plugin = dram_cache_factory.newContent( dram_cache_plugin = dram_cache_factory.newContent(
portal_type="Distributed Ram Cache", specialise='portal_memcached/default_memcached_plugin' ) portal_type="Distributed Ram Cache",
specialise='portal_memcached/default_memcached_plugin')
dram_cache_plugin.setIntIndex(0) dram_cache_plugin.setIntIndex(0)
if getattr(portal_caches, 'distributed_persistent_cache_factory', None) is None: if getattr(portal_caches, 'distributed_persistent_cache_factory', None) is None:
## distributed_ram_cache_factory (to test Distributed Ram Cache Plugin) ## distributed_ram_cache_factory (to test Distributed Ram Cache Plugin)
dram_cache_factory = portal_caches.newContent(portal_type="Cache Factory", dram_cache_factory = portal_caches.newContent(portal_type="Cache Factory",
id='distributed_persistent_cache_factory', id='distributed_persistent_cache_factory',
container=portal_caches) container=portal_caches,
cache_duration=self.cache_duration)
dram_cache_plugin = dram_cache_factory.newContent( dram_cache_plugin = dram_cache_factory.newContent(
portal_type="Distributed Ram Cache", specialise='portal_memcached/flare' ) portal_type="Distributed Ram Cache",
specialise='portal_memcached/flare')
dram_cache_plugin.setIntIndex(0) dram_cache_plugin.setIntIndex(0)
if getattr(portal_caches, 'erp5_user_factory', None) is None: if getattr(portal_caches, 'erp5_user_factory', None) is None:
## erp5_user_factory (to test a combination of all cache plugins) ## erp5_user_factory (to test a combination of all cache plugins)
erp5_user_factory = portal_caches.newContent(portal_type="Cache Factory", erp5_user_factory = portal_caches.newContent(portal_type="Cache Factory",
id="erp5_user_factory") id="erp5_user_factory",
cache_duration=self.cache_duration)
ram_cache_plugin = erp5_user_factory.newContent(portal_type="Ram Cache") ram_cache_plugin = erp5_user_factory.newContent(portal_type="Ram Cache")
ram_cache_plugin.setIntIndex(0) ram_cache_plugin.setIntIndex(0)
dram_cache_plugin = erp5_user_factory.newContent(portal_type="Distributed Ram Cache", dram_cache_plugin = erp5_user_factory.newContent(
portal_type="Distributed Ram Cache",
specialise='portal_memcached/default_memcached_plugin') specialise='portal_memcached/default_memcached_plugin')
dram_cache_plugin.setIntIndex(1) dram_cache_plugin.setIntIndex(1)
## update Ram Cache structure ## update Ram Cache structure
portal_caches.updateCache() portal_caches.updateCache()
from Products.ERP5Type.Cache import CachingMethod
## do we have the same structure we created above? ## do we have the same structure we created above?
self.assert_('ram_cache_factory' in CachingMethod.factories) self.assertTrue('ram_cache_factory' in CachingMethod.factories)
self.assert_('another_ram_cache_factory' in CachingMethod.factories) self.assertTrue('another_ram_cache_factory' in CachingMethod.factories)
self.assert_('distributed_ram_cache_factory' in CachingMethod.factories) self.assertTrue('distributed_ram_cache_factory' in CachingMethod.factories)
self.assert_('distributed_persistent_cache_factory' in CachingMethod.factories) self.assertTrue('distributed_persistent_cache_factory' in CachingMethod.factories)
self.assert_('erp5_user_factory' in CachingMethod.factories) self.assertTrue('erp5_user_factory' in CachingMethod.factories)
def createCachedMethod(self): def createCachedMethod(self):
portal = self.getPortal() portal = self.getPortal()
if getattr(portal, 'testCachedMethod', None) is not None: if getattr(portal, self.python_script_id, None) is not None:
return return
## 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=''"
...@@ -184,15 +192,15 @@ veryExpensiveMethod(value) ...@@ -184,15 +192,15 @@ veryExpensiveMethod(value)
return result return result
""" """
portal.manage_addProduct['PythonScripts'].manage_addPythonScript( portal.manage_addProduct['PythonScripts'].manage_addPythonScript(
id=self.py_script_id) id=self.python_script_id)
py_script_obj = getattr(portal, self.py_script_id) py_script_obj = getattr(portal, self.python_script_id)
py_script_obj.ZPythonScript_edit(py_script_params, py_script_body) py_script_obj.ZPythonScript_edit(py_script_params, py_script_body)
return
def test_01_CacheFactoryOnePlugin(self): def test_01_CacheFactoryOnePlugin(self):
""" Test cache factory containing only one cache plugin. """ """ Test cache factory containing only one cache plugin. """
portal = self.getPortal() portal = self.getPortal()
from Products.ERP5Type.Cache import CachingMethod py_script_obj = getattr(portal, self.python_script_id)
py_script_obj = getattr(portal, self.py_script_id)
for cf_name, clear_allowed in (('ram_cache_factory', True), for cf_name, clear_allowed in (('ram_cache_factory', True),
('distributed_ram_cache_factory', False), ('distributed_ram_cache_factory', False),
('distributed_persistent_cache_factory', False), ('distributed_persistent_cache_factory', False),
...@@ -205,8 +213,7 @@ return result ...@@ -205,8 +213,7 @@ return result
def test_02_CacheFactoryMultiPlugins(self): def test_02_CacheFactoryMultiPlugins(self):
""" Test a cache factory containing multiple cache plugins. """ """ Test a cache factory containing multiple cache plugins. """
portal = self.getPortal() portal = self.getPortal()
from Products.ERP5Type.Cache import CachingMethod py_script_obj = getattr(portal, self.python_script_id)
py_script_obj = getattr(portal, self.py_script_id)
cf_name = 'erp5_user_factory' cf_name = 'erp5_user_factory'
my_cache = CachingMethod(py_script_obj, my_cache = CachingMethod(py_script_obj,
'py_script_obj', 'py_script_obj',
...@@ -300,7 +307,6 @@ return result ...@@ -300,7 +307,6 @@ return result
def test_03_cachePersistentObjects(self): def test_03_cachePersistentObjects(self):
# storing persistent objects in cache is not allowed, but this check is # storing persistent objects in cache is not allowed, but this check is
# only performed in unit tests. # only performed in unit tests.
from Products.ERP5Type.Cache import CachingMethod
def func(): def func():
# return a persistent object # return a persistent object
return self.portal return self.portal
...@@ -322,8 +328,8 @@ return result ...@@ -322,8 +328,8 @@ return result
portal = self.getPortal() portal = self.getPortal()
nb_iterations = 30000 nb_iterations = 30000
result = 'Something short' result = 'Something short'
from Products.ERP5Type.Cache import CachingMethod
py_script_obj = getattr(portal, self.py_script_id) py_script_obj = getattr(portal, self.python_script_id)
ram_cached_method = CachingMethod(py_script_obj, ram_cached_method = CachingMethod(py_script_obj,
'py_script_obj', 'py_script_obj',
...@@ -381,8 +387,11 @@ return result ...@@ -381,8 +387,11 @@ return result
connection_pool connection_pool
getattr(connection_pool, 'local_dict', {}).clear() getattr(connection_pool, 'local_dict', {}).clear()
python_script_id = 'ERP5Site_getLargeStringValue'
portal.manage_addProduct['PythonScripts'].manage_addPythonScript(
id=python_script_id)
# Edit python script which return large value ie: 25 MB string # Edit python script which return large value ie: 25 MB string
py_script_obj = getattr(portal, self.py_script_id) py_script_obj = getattr(portal, python_script_id)
py_script_params = "value=10000, long_parameter=''" py_script_params = "value=10000, long_parameter=''"
py_script_body = """ py_script_body = """
def veryExpensiveMethod(value): def veryExpensiveMethod(value):
...@@ -401,9 +410,8 @@ return 'a' * 1024 * 1024 * 25 ...@@ -401,9 +410,8 @@ return 'a' * 1024 * 1024 * 25
nb_iterations = 30000 nb_iterations = 30000
result = 'a' * 1024 * 1024 * 25 # 25 MB result = 'a' * 1024 * 1024 * 25 # 25 MB
long_parameter = 'a' * 1024 long_parameter = 'a' * 1024
from Products.ERP5Type.Cache import CachingMethod
py_script_id = "testCachedMethod" py_script_obj = getattr(portal, python_script_id)
py_script_obj = getattr(portal, py_script_id)
# First, call a ram based distributed cache to # First, call a ram based distributed cache to
# trig a bug which fill in the MemcachedDict connection pool # trig a bug which fill in the MemcachedDict connection pool
...@@ -448,6 +456,57 @@ return 'a' * 1024 * 1024 * 25 ...@@ -448,6 +456,57 @@ return 'a' * 1024 * 1024 * 25
self.assertEquals(cached, result) self.assertEquals(cached, result)
transaction.commit() transaction.commit()
def test_06_CheckCacheExpiration(self):
"""Check that expiracy is well handle by Cache Plugins
"""
print
print "="*40
print "TESTING: Cache Expiration Time"
portal = self.getPortal()
nb_iterations = 30000
py_script_obj = getattr(portal, self.python_script_id)
cache_factory_list = ('ram_cache_factory', 'distributed_ram_cache_factory',
'distributed_persistent_cache_factory')
for cache_factory in cache_factory_list:
print '\n\t==> %s' % cache_factory
cached_method = CachingMethod(py_script_obj,
'py_script_obj',
cache_factory=cache_factory)
# First call, fill the cache
start = time.time()
cached_method(nb_iterations, portal_path='something')
transaction.commit()
end = time.time()
calculation_time = end-start
print "\n\tCalculation time (1st call)", calculation_time
self.assertTrue(calculation_time > 1.0)
## 2nd call - should be cached now
start = time.time()
cached = cached_method(nb_iterations, portal_path='something')
transaction.commit()
end = time.time()
calculation_time = end-start
print "\n\tCalculation time (2nd call)", calculation_time
self.assertTrue(calculation_time < 1.0)
# Wait expiration period then check that value is computed
time_left_to_wait = (self.cache_duration - calculation_time)
print "\n\tSleep %.2f seconds to wait expiration time" % time_left_to_wait
time.sleep(time_left_to_wait)
# Call conversion for ram_cache_factory
start = time.time()
cached = cached_method(nb_iterations, portal_path='something')
transaction.commit()
end = time.time()
calculation_time = end-start
print "\n\tCalculation time (3rd call)", calculation_time
self.assertTrue(calculation_time > 1.0)
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