[Erp5-report] r19405 - in /erp5/trunk/products/CMFActivity: ./ Activity/ tests/

nobody at svn.erp5.org nobody at svn.erp5.org
Tue Feb 19 17:36:37 CET 2008


Author: vincent
Date: Tue Feb 19 17:36:37 2008
New Revision: 19405

URL: http://svn.erp5.org?rev=19405&view=rev
Log:
Add run-time activity environment data container, and fill it from SQLDict and SQLQueue.

Added:
    erp5/trunk/products/CMFActivity/ActivityRuntimeEnvironment.py
Modified:
    erp5/trunk/products/CMFActivity/ActiveObject.py
    erp5/trunk/products/CMFActivity/Activity/SQLDict.py
    erp5/trunk/products/CMFActivity/Activity/SQLQueue.py
    erp5/trunk/products/CMFActivity/tests/testCMFActivity.py

Modified: erp5/trunk/products/CMFActivity/ActiveObject.py
URL: http://svn.erp5.org/erp5/trunk/products/CMFActivity/ActiveObject.py?rev=19405&r1=19404&r2=19405&view=diff
==============================================================================
--- erp5/trunk/products/CMFActivity/ActiveObject.py (original)
+++ erp5/trunk/products/CMFActivity/ActiveObject.py Tue Feb 19 17:36:37 2008
@@ -32,6 +32,7 @@
 from ZODB.POSException import ConflictError
 from Products.CMFCore.utils import getToolByName
 from Products.ERP5Type.TransactionalVariable import getTransactionalVariable
+from ActivityRuntimeEnvironment import getActivityRuntimeEnvironment
 
 try:
   from Products.CMFCore import permissions
@@ -201,3 +202,5 @@
     key = ('default_activate_parameter', id(aq_base(self)))
     return tv.get(key)
 
+  def getActivityRuntimeEnvironment(self):
+    return getActivityRuntimeEnvironment()

Modified: erp5/trunk/products/CMFActivity/Activity/SQLDict.py
URL: http://svn.erp5.org/erp5/trunk/products/CMFActivity/Activity/SQLDict.py?rev=19405&r1=19404&r2=19405&view=diff
==============================================================================
--- erp5/trunk/products/CMFActivity/Activity/SQLDict.py (original)
+++ erp5/trunk/products/CMFActivity/Activity/SQLDict.py Tue Feb 19 17:36:37 2008
@@ -37,6 +37,7 @@
 from types import ClassType
 #from time import time
 from SQLBase import SQLBase
+from Products.CMFActivity.ActivityRuntimeEnvironment import setActivityRuntimeValue, updateActivityRuntimeValue, clearActivityRuntimeEnvironment
 
 try:
   from transaction import get as get_transaction
@@ -428,12 +429,19 @@
       if group_method_id is not None:
         group_method_id = group_method_id.split('\0')[0]
       message_list = [x[1] for x in message_uid_priority_list]
+      clearActivityRuntimeEnvironment()
       if group_method_id not in (None, ""):
+        setActivityRuntimeValue('group_method_id', group_method_id)
         method  = activity_tool.invokeGroup
         args = (group_method_id, message_list)
       else:
         method = activity_tool.invoke
-        args = (message_list[0], )
+        message = message_list[0]
+        args = (message, )
+        updateActivityRuntimeValue({'activity_kw': message.activity_kw,
+                                    'priority': message_uid_priority_list[0][2],
+                                    'uid': message_uid_priority_list[0][0]})
+      setActivityRuntimeValue('processing_node', processing_node)
       # Commit right before executing messages.
       # As MySQL transaction do no start exactly at the same time as ZODB
       # transactions but a bit later, messages available might be called

Modified: erp5/trunk/products/CMFActivity/Activity/SQLQueue.py
URL: http://svn.erp5.org/erp5/trunk/products/CMFActivity/Activity/SQLQueue.py?rev=19405&r1=19404&r2=19405&view=diff
==============================================================================
--- erp5/trunk/products/CMFActivity/Activity/SQLQueue.py (original)
+++ erp5/trunk/products/CMFActivity/Activity/SQLQueue.py Tue Feb 19 17:36:37 2008
@@ -38,6 +38,7 @@
 from time import time
 from sets import ImmutableSet
 from SQLBase import SQLBase
+from Products.CMFActivity.ActivityRuntimeEnvironment import setActivityRuntimeValue, updateActivityRuntimeValue, clearActivityRuntimeEnvironment
 
 try:
   from transaction import get as get_transaction
@@ -273,6 +274,11 @@
       # everything needed from MySQL to get a fresh view of ZODB objects.
       get_transaction().commit()
       for value in message_uid_priority_list:
+        clearActivityRuntimeEnvironment()
+        updateActivityRuntimeValue({'processing_node': processing_node,
+                                    'activity_kw': value[1].activity_kw,
+                                    'priority': value[2],
+                                    'uid': value[0]})
         processed_message_uid_list.append(value)
         # Try to invoke
         try:

Added: erp5/trunk/products/CMFActivity/ActivityRuntimeEnvironment.py
URL: http://svn.erp5.org/erp5/trunk/products/CMFActivity/ActivityRuntimeEnvironment.py?rev=19405&view=auto
==============================================================================
--- erp5/trunk/products/CMFActivity/ActivityRuntimeEnvironment.py (added)
+++ erp5/trunk/products/CMFActivity/ActivityRuntimeEnvironment.py Tue Feb 19 17:36:37 2008
@@ -1,0 +1,33 @@
+import threading
+import copy
+
+activity_runtime_environment_container = threading.local()
+
+def getActivityRuntimeEnvironment():
+  """
+    Raises AttributeError if called outside activity.
+  """
+  return copy.deepcopy(activity_runtime_environment_container.current)
+
+def _getActivityRuntimeEnvironment():
+  current = getattr(activity_runtime_environment_container, 'current', None)
+  if current is None:
+    current = activity_runtime_environment_container.current = {}
+  return current
+
+def setActivityRuntimeValue(key, value):
+  """
+    TODO: protect against unauthorized use ?
+  """
+  _getActivityRuntimeEnvironment()[key] = value
+
+def updateActivityRuntimeValue(new_dict):
+  """
+    TODO: protect against unauthorized use ?
+  """
+  _getActivityRuntimeEnvironment().update(new_dict)
+
+def clearActivityRuntimeEnvironment():
+  if getattr(activity_runtime_environment_container, 'current', None) is not None:
+    delattr(activity_runtime_environment_container, 'current')
+

Modified: erp5/trunk/products/CMFActivity/tests/testCMFActivity.py
URL: http://svn.erp5.org/erp5/trunk/products/CMFActivity/tests/testCMFActivity.py?rev=19405&r1=19404&r2=19405&view=diff
==============================================================================
--- erp5/trunk/products/CMFActivity/tests/testCMFActivity.py (original)
+++ erp5/trunk/products/CMFActivity/tests/testCMFActivity.py Tue Feb 19 17:36:37 2008
@@ -43,6 +43,7 @@
 import cPickle as pickle
 from Products.CMFActivity.ActivityTool import Message
 import random
+from Products.CMFActivity.ActivityRuntimeEnvironment import setActivityRuntimeValue, clearActivityRuntimeEnvironment
 
 try:
   from transaction import get as get_transaction
@@ -2677,6 +2678,63 @@
     finally:
       delattr(Organisation, 'mustRunBefore')
       delattr(Organisation, 'mustRunAfter')
+
+  def CheckActivityRuntimeEnvironment(self, activity):
+    organisation = self.getPortal().organisation_module.newContent(portal_type='Organisation')
+    get_transaction().commit()
+    self.tic()
+    activity_tool = self.getActivityTool()
+    check_result_dict = {}
+    initial_list_check_value = [1, 2]
+    def extractActivityRuntimeEnvironment(self):
+      setActivityRuntimeValue('list_check', initial_list_check_value)
+      environment = self.getActivityRuntimeEnvironment()
+      check_result_dict['environment'] = environment
+    def runAndCheck():
+      check_result_dict.clear()
+      self.assertFalse('environment' in check_result_dict)
+      get_transaction().commit()
+      self.tic()
+      self.assertTrue('environment' in check_result_dict)
+    Organisation.extractActivityRuntimeEnvironment = extractActivityRuntimeEnvironment
+    try:
+      # Check that organisation.getActivityRuntimeEnvironment raises outside
+      # of activities.
+      clearActivityRuntimeEnvironment()
+      #organisation.getActivityRuntimeEnvironment()
+      self.assertRaises(AttributeError, organisation.getActivityRuntimeEnvironment)
+      # Check Runtime isolation
+      setActivityRuntimeValue('blah', True)
+      organisation.activate(activity=activity).extractActivityRuntimeEnvironment()
+      runAndCheck()
+      self.assertEqual(check_result_dict['environment'].get('blah'), None)
+      # Check Runtime presence
+      self.assertTrue(len(check_result_dict['environment']) > 0)
+      self.assertTrue('processing_node' in check_result_dict['environment'])
+      # Check Runtime does a deepcopy
+      self.assertTrue('list_check' in check_result_dict['environment'])
+      check_result_dict['environment']['list_check'].append(3)
+      self.assertTrue(check_result_dict['environment']['list_check'] != \
+                      initial_list_check_value)
+    finally:
+      delattr(Organisation, 'extractActivityRuntimeEnvironment')
+
+  def test_104_activityRuntimeEnvironment(self, quiet=0, run=run_all_test):
+    if not run: return
+    if not quiet:
+      message = '\nCheck ActivityRuntimeEnvironment (SQLDict)'
+      ZopeTestCase._print(message)
+      LOG('Testing... ',0,message)
+    self.CheckActivityRuntimeEnvironment('SQLDict')
+
+  def test_105_TryChangeSkinInActivitySQLQueue(self, quiet=0, run=run_all_test):
+    if not run: return
+    if not quiet:
+      message = '\nCheck ActivityRuntimeEnvironment (SQLQueue)'
+      ZopeTestCase._print(message)
+      LOG('Testing... ',0,message)
+    self.CheckActivityRuntimeEnvironment('SQLQueue')
+
 def test_suite():
   suite = unittest.TestSuite()
   suite.addTest(unittest.makeSuite(TestCMFActivity))




More information about the Erp5-report mailing list