Commit e5587493 authored by Nicolas Wavrant's avatar Nicolas Wavrant

erp5_trade: move the composition cache in

Since was moved to portal_components, the cache in mixin/,
which was kept at the level of the zope process, was causing issue when portal_components
was reset : the classes kept in __class_cache would not get resetted, and later calls
to asComposedDocument would reuse them. In consequence, these classes would kept pointers
to objects in the old class BusinessProcess, causing bugs as these objects would have
been cleaned by the reset.

The first intuition was to remove this cache, unfortunately the reason of this introduction
(fixing a memory leak) is still valid nowadays, so I've decided to move this cache within
BusinessProcess, as it has the advantage of emptying it whenever portal_components is resetted.

For more information of the bug mentionned above, and the trials about removing the cache can
be found in the discussion of this MR :
parent 22a6aaad
Pipeline #10122 failed with stage
in 0 seconds
......@@ -120,6 +120,12 @@ class BusinessProcess(Path, XMLObject):
# Cache used by composition mixin (as a composed document always inherit
# from BusinessProcess. The cache avoids memory leak, and has been moved
# here to be automatically flushed when BusinessProcess class is reseted
# by portal_components
asComposedDocument__class_cache = {}
# ITradeModelPathProcess implementation
security.declareProtected(Permissions.AccessContentsInformation, 'getTradeModelPathValueList')
def getTradeModelPathValueList(self, trade_phase=None, context=None, **kw):
......@@ -119,18 +119,16 @@ class asComposedDocument(object):
This class should be seen as a function, and it is named accordingly.
It is out of CompositionMixin class to avoid excessive indentation.
# Cache created classes to make other caches (like Base.aq_portal_type)
# useful and avoid memory leaks.
__class_cache = {}
def __new__(cls, orig_self, portal_type_list=None):
from erp5.component.document.BusinessProcess import BusinessProcess
class_cache = BusinessProcess.asComposedDocument__class_cache
self = orig_self.asContext(_portal_type_list=portal_type_list) # XXX-JPS orig_self -> original_self - please follow conventions
base_class = self.__class__
self.__class__ = cls.__class_cache[base_class]
self.__class__ = class_cache[base_class]
except KeyError:
from erp5.component.document.BusinessProcess import BusinessProcess
cls.__class_cache[base_class] = self.__class__ = \
class_cache[base_class] = self.__class__ = \
type(base_class.__name__, (cls, base_class, BusinessProcess), {})
# here we could inherit many "useful" classes dynamically - héhé
# that would be a "real" abstract composition system
......@@ -3232,6 +3232,60 @@ class TestZodbMixinComponent(TestZodbInterfaceComponent):
class TestZodbDocumentComponentReload(TestZodbDocumentComponent):
def getBusinessTemplateList(self):
return (
def _setBusinessProcessComponentTextContent(self, value):
component = self.portal.portal_components['document.erp5.BusinessProcess']
def testAsComposedDocumentCacheIsCorrectlyFlushed(self):
component = self.portal.portal_components['document.erp5.BusinessProcess']
component_original_text_content = component.getTextContent()
# Use try/finally to restore the content of
# document.erp5.BusinessProcess as using addCleanup function here raises
# with :
# ConnectionStateError: Shouldn't load state for
# Products.DCWorkflow.Scripts.Scripts 0x099ad38a68606074
# when the connection is closed
component_original_text_content + """
def getVersion(self):
return 1
movement = self.portal.newContent(portal_type='Movement')
composed_movement = movement.asComposedDocument()
self.assertEqual(composed_movement.getVersion(), 1)
component_original_text_content + """
def getVersion(self):
return 2
composed_movement = movement.asComposedDocument()
self.assertEqual(composed_movement.getVersion(), 2)
def test_suite():
suite = unittest.TestSuite()
......@@ -3241,4 +3295,5 @@ def test_suite():
return suite
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