From f83cf5377ab6ac5923e1dcd977e03317b56033cf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Wed, 27 Aug 2014 16:11:58 +0200
Subject: [PATCH] Keep a reference to _module_cache_set in each test request

see 3a92d8b6a1c0ce6c2a1fb78fa348e3c95d0da526
---
 product/ERP5Type/tests/ERP5TypeLiveTestCase.py | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/product/ERP5Type/tests/ERP5TypeLiveTestCase.py b/product/ERP5Type/tests/ERP5TypeLiveTestCase.py
index 1188d95da0..db61e87d96 100644
--- a/product/ERP5Type/tests/ERP5TypeLiveTestCase.py
+++ b/product/ERP5Type/tests/ERP5TypeLiveTestCase.py
@@ -37,6 +37,7 @@ from Testing import ZopeTestCase
 from Testing.ZopeTestCase import PortalTestCase, user_name
 from Products.CMFCore.utils import getToolByName
 from Products.ERP5Type.tests.ProcessingNodeTestCase import ProcessingNodeTestCase
+from Products.ERP5Type.Globals import get_request
 from Products.ERP5Type.tests.ERP5TypeTestCase import \
   ERP5TypeTestCaseMixin, ERP5TypeTestCase
 from glob import glob
@@ -74,10 +75,18 @@ class ERP5TypeLiveTestCase(ERP5TypeTestCaseMixin):
 
     def getPortal(self):
       """Returns the portal object, i.e. the "fixture root".
+
+      Rewrap the portal in an independant request for this test.
       """
       if self.portal is not None:
         return self.portal
 
+      # _module_cache_set is used to keep a reference to the code of modules
+      # before they get reloaded. As we will use another request we need to
+      # make sure that we still have a reference to _module_cache_set so that
+      # it does not get garbage collected.
+      module_cache_set = getattr(get_request(), '_module_cache_set', None)
+
       from Products.ERP5.ERP5Site import getSite
       site = getSite()
       # reconstruct the acquistion chain with an independant request.
@@ -85,6 +94,9 @@ class ERP5TypeLiveTestCase(ERP5TypeTestCaseMixin):
       from Testing.ZopeTestCase.utils import makerequest
       portal = getattr(makerequest(site.aq_parent), site.getId())
 
+      if module_cache_set:
+        portal.REQUEST._module_cache_set = module_cache_set
+
       # Make the various get_request patches return this request.
       # This is for ERP5TypeTestCase patch
       from Testing.ZopeTestCase.connections import registry
-- 
2.30.9