Commit ca8991d7 authored by Jérome Perrin's avatar Jérome Perrin

Check that we don't store persistent object in caching methods. The cache will

only be enabled in unit tests.
It uses pickle to check if the object is persistent or not, I'm too lazy to do
this by iterating in gc.get_referents graphs.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@17504 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 89dfe712
No related merge requests found
......@@ -779,5 +779,21 @@ def optimize():
from Products.CMFCore.FSPythonScript import FSPythonScript
FSPythonScript._compile = PythonScript_compile
optimize()
def fortify():
'''Add some extra checks that we don't have at runtime, not to slow down the
system.
'''
# check that we don't store persistent objects in cache
from pickle import dumps
from Products.ERP5Type.CachePlugins.BaseCache import CacheEntry
CacheEntry__init__ = CacheEntry.__init__
def __init__(self, value, *args, **kw):
# this will raise TypeError if you try to cache a persistent object
dumps(value)
CacheEntry__init__(self, value, *args, **kw)
CacheEntry.__init__ = __init__
fortify()
......@@ -277,6 +277,23 @@ return result
## Cache cleared shouldn't be previously cached
self.assert_(1.0 < calculation_time)
def test_CachePersistentObjects(self):
# storing persistent objects in cache is not allowed, but this check is
# only performed in unit tests.
from Products.ERP5Type.Cache import CachingMethod
def func():
# return a persistent object
return self.portal
cached_func = CachingMethod(func, 'cache_persistent_obj')
self.assertRaises(TypeError, cached_func)
def func():
# return a method bound on a persistent object
return self.portal.getTitle
cached_func = CachingMethod(func, 'cache_bound_method')
self.assertRaises(TypeError, cached_func)
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestCacheTool))
......
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