testCMFActivity.py 152 KB
Newer Older
Sebastien Robin's avatar
Sebastien Robin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
##############################################################################
#
# Copyright (c) 2004 Nexedi SARL and Contributors. All Rights Reserved.
#          Sebastien Robin <seb@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################


Jérome Perrin's avatar
Jérome Perrin committed
30
import unittest
Sebastien Robin's avatar
Sebastien Robin committed
31

32 33
from Products.ERP5Type.tests.utils import LogInterceptor
from Products.ERP5Type.tests.backportUnittest import expectedFailure
Sebastien Robin's avatar
Sebastien Robin committed
34 35
from Testing import ZopeTestCase
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
36
from Products.ERP5Type.tests.utils import DummyMailHost
37
from Products.ERP5Type.TransactionalVariable import getTransactionalVariable
38 39 40
from Products.CMFActivity.ActiveObject import INVOKE_ERROR_STATE,\
                                              VALIDATE_ERROR_STATE
from Products.CMFActivity.Activity.Queue import VALIDATION_ERROR_DELAY
41
from Products.CMFActivity.Activity.SQLDict import SQLDict
42
from Products.CMFActivity.Errors import ActivityPendingError, ActivityFlushError
43
#from Products.ERP5Type.Document.Organisation import Organisation
Leonardo Rochael Almeida's avatar
Leonardo Rochael Almeida committed
44 45 46
# The above cannot be imported at top level because it doesn't exist until
# Products.ERP5 has been initialized. We set it up as global and populate it
# later:
47
Organisation = None
48
from AccessControl.SecurityManagement import newSecurityManager
Sebastien Robin's avatar
Sebastien Robin committed
49
from zLOG import LOG
50
from ZODB.POSException import ConflictError
51
from DateTime import DateTime
52 53
import cPickle as pickle
from Products.CMFActivity.ActivityTool import Message
54
import random
55
import threading
56
import sys
Sebastien Robin's avatar
Sebastien Robin committed
57

58
import transaction
59

Julien Muchembled's avatar
Julien Muchembled committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73
class CommitFailed(Exception):
  pass

def registerFailingTransactionManager(*args, **kw):
  from Shared.DC.ZRDB.TM import TM
  class dummy_tm(TM):
    def tpc_vote(self, *ignored):
      raise CommitFailed
    def _finish(self):
      pass
    def _abort(self):
      pass
  dummy_tm()._register()

74
class TestCMFActivity(ERP5TypeTestCase, LogInterceptor):
Sebastien Robin's avatar
Sebastien Robin committed
75 76 77 78 79 80

  run_all_test = 1
  # Different variables used for this test
  company_id = 'Nexedi'
  title1 = 'title1'
  title2 = 'title2'
81
  company_id2 = 'Coramy'
82
  company_id3 = 'toto'
Sebastien Robin's avatar
Sebastien Robin committed
83

Sebastien Robin's avatar
Sebastien Robin committed
84 85 86
  def getTitle(self):
    return "CMFActivity"

Sebastien Robin's avatar
Sebastien Robin committed
87 88 89 90
  def getBusinessTemplateList(self):
    """
      Return the list of business templates.
    """
Sebastien Robin's avatar
Sebastien Robin committed
91
    return ('erp5_base',)
Sebastien Robin's avatar
Sebastien Robin committed
92 93 94 95 96 97 98 99 100 101 102 103 104

  def getCategoriesTool(self):
    return getattr(self.getPortal(), 'portal_categories', None)

  def getRuleTool(self):
    return getattr(self.getPortal(), 'portal_Rules', None)

  def getPersonModule(self):
    return getattr(self.getPortal(), 'person', None)

  def getOrganisationModule(self):
    return getattr(self.getPortal(), 'organisation', None)

105
  def afterSetUp(self):
106
    super(TestCMFActivity, self).afterSetUp()
Sebastien Robin's avatar
Sebastien Robin committed
107
    self.login()
108 109 110 111 112 113 114
    portal = self.portal
    # trap outgoing e-mails
    self.oldMailHost = getattr(self.portal, 'MailHost', None)
    if self.oldMailHost is not None:
      self.portal.manage_delObjects(['MailHost'])
      self.portal._setObject('MailHost', DummyMailHost('MailHost'))
    
115 116 117 118 119
    # remove all message in the message_table because
    # the previous test might have failed
    message_list = portal.portal_activities.getMessageList()
    for message in message_list:
      portal.portal_activities.manageCancel(message.object_path,message.method_id)
120

Sebastien Robin's avatar
Sebastien Robin committed
121 122 123 124 125 126 127 128
    # Then add new components
    if not(hasattr(portal,'organisation')):
      portal.portal_types.constructContent(type_name='Organisation Module',
                                         container=portal,
                                         id='organisation')
    organisation_module = self.getOrganisationModule()
    if not(organisation_module.hasContent(self.company_id)):
      o1 = organisation_module.newContent(id=self.company_id)
129
    self.stepTic()
130 131 132 133
    # import it now that Products.ERP5 has been initialized
    global Organisation
    from Products.ERP5Type.Document.Organisation import Organisation as Org
    Organisation = Org
Sebastien Robin's avatar
Sebastien Robin committed
134 135 136 137

  def login(self, quiet=0, run=run_all_test):
    uf = self.getPortal().acl_users
    uf._doAddUser('seb', '', ['Manager'], [])
138
    uf._doAddUser('ERP5TypeTestCase', '', ['Manager'], [])
Sebastien Robin's avatar
Sebastien Robin committed
139 140 141
    user = uf.getUserById('seb').__of__(uf)
    newSecurityManager(None, user)

142
  def InvokeAndCancelActivity(self, activity):
143 144 145
    """
    Simple test where we invoke and cancel an activity
    """
Sebastien Robin's avatar
Sebastien Robin committed
146 147
    portal = self.getPortal()
    organisation =  portal.organisation._getOb(self.company_id)
148
    organisation._setTitle(self.title1)
Sebastien Robin's avatar
Sebastien Robin committed
149
    self.assertEquals(self.title1,organisation.getTitle())
150
    organisation.activate(activity=activity)._setTitle(self.title2)
Sebastien Robin's avatar
Sebastien Robin committed
151
    # Needed so that the message are commited into the queue
152
    transaction.commit()
153 154
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),1)
155
    portal.portal_activities.manageCancel(organisation.getPhysicalPath(),'_setTitle')
156
    # Needed so that the message are removed from the queue
157
    transaction.commit()
Sebastien Robin's avatar
Sebastien Robin committed
158
    self.assertEquals(self.title1,organisation.getTitle())
159 160
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),0)
161
    organisation.activate(activity=activity)._setTitle(self.title2)
162
    # Needed so that the message are commited into the queue
163
    transaction.commit()
164 165
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),1)
166
    portal.portal_activities.manageInvoke(organisation.getPhysicalPath(),'_setTitle')
167
    # Needed so that the message are removed from the queue
168
    transaction.commit()
Sebastien Robin's avatar
Sebastien Robin committed
169 170 171 172
    self.assertEquals(self.title2,organisation.getTitle())
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),0)

Yoshinori Okuji's avatar
Yoshinori Okuji committed
173
  def DeferredSetTitleActivity(self, activity):
174 175 176 177
    """
    We check that the title is changed only after that
    the activity was called
    """
Sebastien Robin's avatar
Sebastien Robin committed
178
    portal = self.getPortal()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
179
    organisation = portal.organisation._getOb(self.company_id)
180
    organisation._setTitle(self.title1)
Sebastien Robin's avatar
Sebastien Robin committed
181
    self.assertEquals(self.title1,organisation.getTitle())
182
    organisation.activate(activity=activity)._setTitle(self.title2)
Sebastien Robin's avatar
Sebastien Robin committed
183
    # Needed so that the message are commited into the queue
184
    transaction.commit()
Sebastien Robin's avatar
Sebastien Robin committed
185 186 187 188 189 190 191
    self.assertEquals(self.title1,organisation.getTitle())
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
    self.assertEquals(self.title2,organisation.getTitle())
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),0)

192
  def CallOnceWithActivity(self, activity):
193 194 195 196
    """
    With this test we can check if methods are called
    only once (sometimes it was twice !!!)
    """
Sebastien Robin's avatar
Sebastien Robin committed
197
    portal = self.getPortal()
198 199 200 201 202 203 204
    def setFoobar(self):
      if hasattr(self,'foobar'):
        self.foobar = self.foobar + 1
      else:
        self.foobar = 1
    def getFoobar(self):
      return (getattr(self,'foobar',0))
Sebastien Robin's avatar
Sebastien Robin committed
205
    organisation =  portal.organisation._getOb(self.company_id)
206 207 208
    Organisation.setFoobar = setFoobar
    Organisation.getFoobar = getFoobar
    organisation.foobar = 0
209
    organisation._setTitle(self.title1)
210 211
    self.assertEquals(0,organisation.getFoobar())
    organisation.activate(activity=activity).setFoobar()
Sebastien Robin's avatar
Sebastien Robin committed
212
    # Needed so that the message are commited into the queue
213
    transaction.commit()
214 215
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),1)
Sebastien Robin's avatar
Sebastien Robin committed
216 217
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
218
    self.assertEquals(1,organisation.getFoobar())
Sebastien Robin's avatar
Sebastien Robin committed
219 220
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),0)
221 222
    organisation.activate(activity=activity).setFoobar()
    # Needed so that the message are commited into the queue
223
    transaction.commit()
224 225 226 227
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),1)
    portal.portal_activities.manageInvoke(organisation.getPhysicalPath(),'setFoobar')
    # Needed so that the message are commited into the queue
228
    transaction.commit()
229 230 231 232
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),0)
    self.assertEquals(2,organisation.getFoobar())

233
  def TryFlushActivity(self, activity):
234 235 236
    """
    Check the method flush
    """
237 238
    portal = self.getPortal()
    organisation =  portal.organisation._getOb(self.company_id)
239 240
    organisation._setTitle(self.title1)
    organisation.activate(activity=activity)._setTitle(self.title2)
241
    organisation.flushActivity(invoke=1)
242
    self.assertEquals(organisation.getTitle(),self.title2)
243
    transaction.commit()
244 245 246
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),0)
    self.assertEquals(organisation.getTitle(),self.title2)
247
    # Try again with different commit order
248 249
    organisation._setTitle(self.title1)
    organisation.activate(activity=activity)._setTitle(self.title2)
250
    transaction.commit()
251 252 253
    organisation.flushActivity(invoke=1)
    self.assertEquals(len(message_list),0)
    self.assertEquals(organisation.getTitle(),self.title2)
254
    transaction.commit()
255

256
  def TryActivateInsideFlush(self, activity):
257 258 259
    """
    Create a new activity inside a flush action
    """
260 261
    portal = self.getPortal()
    def DeferredSetTitle(self,value):
262
      self.activate(activity=activity)._setTitle(value)
263 264 265
    from Products.ERP5Type.Document.Organisation import Organisation
    Organisation.DeferredSetTitle = DeferredSetTitle
    organisation =  portal.organisation._getOb(self.company_id)
266
    organisation._setTitle(self.title1)
267 268
    organisation.activate(activity=activity).DeferredSetTitle(self.title2)
    organisation.flushActivity(invoke=1)
269
    transaction.commit()
270 271
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
272
    transaction.commit()
273 274 275 276 277
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),0)
    self.assertEquals(organisation.getTitle(),self.title2)

  def TryTwoMethods(self, activity):
278 279 280
    """
    Try several activities
    """
281 282
    portal = self.getPortal()
    def DeferredSetDescription(self,value):
283
      self._setDescription(value)
284
    def DeferredSetTitle(self,value):
285
      self._setTitle(value)
286 287 288 289
    from Products.ERP5Type.Document.Organisation import Organisation
    Organisation.DeferredSetTitle = DeferredSetTitle
    Organisation.DeferredSetDescription = DeferredSetDescription
    organisation =  portal.organisation._getOb(self.company_id)
290
    organisation._setTitle(None)
291 292 293
    organisation.setDescription(None)
    organisation.activate(activity=activity).DeferredSetTitle(self.title1)
    organisation.activate(activity=activity).DeferredSetDescription(self.title1)
294
    transaction.commit()
295 296
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
297
    transaction.commit()
298 299 300 301 302 303
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),0)
    self.assertEquals(organisation.getTitle(),self.title1)
    self.assertEquals(organisation.getDescription(),self.title1)

  def TryTwoMethodsAndFlushThem(self, activity):
304 305 306
    """
    make sure flush works with several activities
    """
307 308
    portal = self.getPortal()
    def DeferredSetTitle(self,value):
309
      self.activate(activity=activity)._setTitle(value)
310
    def DeferredSetDescription(self,value):
311
      self.activate(activity=activity)._setDescription(value)
312 313 314 315
    from Products.ERP5Type.Document.Organisation import Organisation
    Organisation.DeferredSetTitle = DeferredSetTitle
    Organisation.DeferredSetDescription = DeferredSetDescription
    organisation =  portal.organisation._getOb(self.company_id)
316
    organisation._setTitle(None)
317 318 319 320
    organisation.setDescription(None)
    organisation.activate(activity=activity).DeferredSetTitle(self.title1)
    organisation.activate(activity=activity).DeferredSetDescription(self.title1)
    organisation.flushActivity(invoke=1)
321
    transaction.commit()
322 323
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
324
    transaction.commit()
325 326 327 328 329
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),0)
    self.assertEquals(organisation.getTitle(),self.title1)
    self.assertEquals(organisation.getDescription(),self.title1)

330
  def TryActivateFlushActivateTic(self, activity,second=None,commit_sub=0):
331 332 333
    """
    try to commit sub transactions
    """
334 335 336
    portal = self.getPortal()
    def DeferredSetTitle(self,value,commit_sub=0):
      if commit_sub:
337
        transaction.savepoint(optimistic=True)
338
      self.activate(activity=second or activity,priority=4)._setTitle(value)
339 340
    def DeferredSetDescription(self,value,commit_sub=0):
      if commit_sub:
341
        transaction.savepoint(optimistic=True)
342
      self.activate(activity=second or activity,priority=4)._setDescription(value)
343 344 345 346
    from Products.ERP5Type.Document.Organisation import Organisation
    Organisation.DeferredSetTitle = DeferredSetTitle
    Organisation.DeferredSetDescription = DeferredSetDescription
    organisation =  portal.organisation._getOb(self.company_id)
347
    organisation._setTitle(None)
348 349 350 351
    organisation.setDescription(None)
    organisation.activate(activity=activity).DeferredSetTitle(self.title1,commit_sub=commit_sub)
    organisation.flushActivity(invoke=1)
    organisation.activate(activity=activity).DeferredSetDescription(self.title1,commit_sub=commit_sub)
352
    transaction.commit()
353 354
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
355
    transaction.commit()
356 357
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
358
    transaction.commit()
359 360 361 362 363
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),0)
    self.assertEquals(organisation.getTitle(),self.title1)
    self.assertEquals(organisation.getDescription(),self.title1)

364
  def TryMessageWithErrorOnActivity(self, activity):
365 366 367
    """
    Make sure that message with errors are not deleted
    """
368 369 370 371 372 373 374 375
    portal = self.getPortal()
    def crashThisActivity(self):
      self.IWillCrach()
    from Products.ERP5Type.Document.Organisation import Organisation
    organisation =  portal.organisation._getOb(self.company_id)
    Organisation.crashThisActivity = crashThisActivity
    organisation.activate(activity=activity).crashThisActivity()
    # Needed so that the message are commited into the queue
376
    transaction.commit()
377
    message_list = portal.portal_activities.getMessageList()
378
    LOG('Before MessageWithErrorOnActivityFails, message_list',0,[x.__dict__ for x in message_list])
379 380 381
    self.assertEquals(len(message_list),1)
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
382
    # XXX HERE WE SHOULD USE TIME SHIFT IN ORDER TO SIMULATE MULTIPLE TICS
383 384 385 386 387
    # Test if there is still the message after it crashed
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),1)
    portal.portal_activities.manageCancel(organisation.getPhysicalPath(),'crashThisActivity')
    # Needed so that the message are commited into the queue
388
    transaction.commit()
389 390 391
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),0)

Yoshinori Okuji's avatar
Yoshinori Okuji committed
392
  def DeferredSetTitleWithRenamedObject(self, activity):
393
    """
394 395
    make sure that it is impossible to rename an object
    if some activities are still waiting for this object
396 397 398
    """
    portal = self.getPortal()
    organisation =  portal.organisation._getOb(self.company_id)
399
    organisation._setTitle(self.title1)
400
    self.assertEquals(self.title1,organisation.getTitle())
401
    organisation.activate(activity=activity)._setTitle(self.title2)
402
    # Needed so that the message are commited into the queue
403
    transaction.commit()
404
    self.assertEquals(self.title1,organisation.getTitle())
405
    self.assertRaises(ActivityPendingError,organisation.edit,id=self.company_id2)
406 407 408 409 410 411 412 413 414
    portal.portal_activities.distribute()
    portal.portal_activities.tic()

  def TryActiveProcess(self, activity):
    """
    Try to store the result inside an active process
    """
    portal = self.getPortal()
    organisation =  portal.organisation._getOb(self.company_id)
415
    organisation._setTitle(self.title1)
416 417 418 419
    active_process = portal.portal_activities.newActiveProcess()
    self.assertEquals(self.title1,organisation.getTitle())
    organisation.activate(activity=activity,active_process=active_process).getTitle()
    # Needed so that the message are commited into the queue
420
    transaction.commit()
421 422 423 424 425 426 427 428 429 430 431
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
    self.assertEquals(self.title1,organisation.getTitle())
    result = active_process.getResultList()[0]
    self.assertEquals(result.method_id , 'getTitle')
    self.assertEquals(result.result , self.title1)
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),0)

  def TryActiveProcessInsideActivity(self, activity):
    """
432
    Try two levels with active_process, we create one first
433 434 435 436 437
    activity with an acitive process, then this new activity
    uses another active process
    """
    portal = self.getPortal()
    organisation =  portal.organisation._getOb(self.company_id)
438
    organisation._setTitle(self.title1)
439 440 441 442 443 444 445 446 447
    def Organisation_test(self):
      active_process = self.portal_activities.newActiveProcess()
      self.activate(active_process=active_process).getTitle()
      return active_process
    from Products.ERP5Type.Document.Organisation import Organisation
    Organisation.Organisation_test = Organisation_test
    active_process = portal.portal_activities.newActiveProcess()
    organisation.activate(activity=activity,active_process=active_process).Organisation_test()
    # Needed so that the message are commited into the queue
448
    transaction.commit()
449 450 451 452 453 454
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
    sub_active_process = active_process.getResultList()[0].result
    LOG('TryActiveProcessInsideActivity, sub_active_process',0,sub_active_process)
455 456 457
    result = sub_active_process.getResultList()[0]
    self.assertEquals(result.method_id , 'getTitle')
    self.assertEquals(result.result , self.title1)
458 459 460
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),0)

461 462
  def TryMethodAfterMethod(self, activity):
    """
463
      Ensure the order of an execution by a method id
464 465
    """
    portal = self.getPortal()
466 467 468 469
    organisation_module = self.getOrganisationModule()
    if not organisation_module.hasContent(self.company_id):
      organisation_module.newContent(id=self.company_id)
    o = portal.organisation._getOb(self.company_id)
470

471 472
    o.setTitle('a')
    self.assertEquals(o.getTitle(), 'a')
473
    transaction.commit()
474
    self.tic()
475

476 477 478
    def toto(self, value):
      self.setTitle(self.getTitle() + value)
    o.__class__.toto = toto
479

480 481 482
    def titi(self, value):
      self.setTitle(self.getTitle() + value)
    o.__class__.titi = titi
483

484 485
    o.activate(after_method_id = 'titi', activity = activity).toto('b')
    o.activate(activity = activity).titi('c')
486
    transaction.commit()
487 488
    self.tic()
    self.assertEquals(o.getTitle(), 'acb')
489

490 491 492
  def ExpandedMethodWithDeletedSubObject(self, activity):
    """
    Do recursiveReindexObject, then delete a
493
    subobject an see if there is only one activity
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
    in the queue
    """
    portal = self.getPortal()
    organisation_module = self.getOrganisationModule()
    if not(organisation_module.hasContent(self.company_id2)):
      o2 = organisation_module.newContent(id=self.company_id2)
    o1 =  portal.organisation._getOb(self.company_id)
    o2 =  portal.organisation._getOb(self.company_id2)
    for o in (o1,o2):
      if not(o.hasContent('1')):
        o.newContent(portal_type='Email',id='1')
      if not(o.hasContent('2')):
        o.newContent(portal_type='Email',id='2')
    o1.recursiveReindexObject()
    o2.recursiveReindexObject()
    o1._delOb('2')
510
    transaction.commit()
511 512
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
513
    transaction.commit()
514 515 516 517 518 519
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),1)

  def ExpandedMethodWithDeletedObject(self, activity):
    """
    Do recursiveReindexObject, then delete a
520
    subobject an see if there is only one activity
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
    in the queue
    """
    portal = self.getPortal()
    organisation_module = self.getOrganisationModule()
    if not(organisation_module.hasContent(self.company_id2)):
      o2 = organisation_module.newContent(id=self.company_id2)
    o1 =  portal.organisation._getOb(self.company_id)
    o2 =  portal.organisation._getOb(self.company_id2)
    for o in (o1,o2):
      if not(o.hasContent('1')):
        o.newContent(portal_type='Email',id='1')
      if not(o.hasContent('2')):
        o.newContent(portal_type='Email',id='2')
    o1.recursiveReindexObject()
    o2.recursiveReindexObject()
    organisation_module._delOb(self.company_id2)
537
    transaction.commit()
538 539
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
540
    transaction.commit()
541 542 543
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),1)

544 545 546 547 548 549 550 551 552
  def TryAfterTag(self, activity):
    """
      Ensure the order of an execution by a tag
    """
    portal = self.getPortal()
    organisation_module = self.getOrganisationModule()
    if not organisation_module.hasContent(self.company_id):
      organisation_module.newContent(id=self.company_id)
    o = portal.organisation._getOb(self.company_id)
553

554 555
    o.setTitle('?')
    self.assertEquals(o.getTitle(), '?')
556
    transaction.commit()
557
    self.tic()
558

559 560
    o.activate(after_tag = 'toto', activity = activity).setTitle('b')
    o.activate(tag = 'toto', activity = activity).setTitle('a')
561
    transaction.commit()
562 563
    self.tic()
    self.assertEquals(o.getTitle(), 'b')
564

565
    o.setDefaultActivateParameters(tag = 'toto')
566 567 568 569 570
    def titi(self):
      self.setCorporateName(self.getTitle() + 'd')
    o.__class__.titi = titi
    o.activate(after_tag_and_method_id=('toto', 'setTitle'), activity = activity).titi()
    o.activate(activity = activity).setTitle('c')
571
    transaction.commit()
572 573
    self.tic()
    self.assertEquals(o.getCorporateName(), 'cd')
574

575 576 577 578 579 580 581 582 583 584 585 586 587 588
  def TryFlushActivityWithAfterTag(self, activity):
    """
      Ensure the order of an execution by a tag
    """
    portal = self.getPortal()
    organisation_module = self.getOrganisationModule()
    if not organisation_module.hasContent(self.company_id):
      organisation_module.newContent(id=self.company_id)
    o = portal.organisation._getOb(self.company_id)

    o.setTitle('?')
    o.setDescription('?')
    self.assertEquals(o.getTitle(), '?')
    self.assertEquals(o.getDescription(), '?')
589
    transaction.commit()
590 591 592 593
    self.tic()

    o.activate(after_tag = 'toto', activity = activity).setDescription('b')
    o.activate(tag = 'toto', activity = activity).setTitle('a')
594
    transaction.commit()
595 596 597
    tool = self.getActivityTool()
    self.assertRaises(ActivityFlushError,tool.manageInvoke,o.getPath(),'setDescription')
    tool.manageInvoke(o.getPath(),'setTitle')
598
    transaction.commit()
599 600 601 602 603 604 605
    self.assertEquals(o.getTitle(), 'a')
    self.assertEquals(o.getDescription(), '?')
    self.tic()
    self.assertEquals(len(tool.getMessageList()),0)
    self.assertEquals(o.getTitle(), 'a')
    self.assertEquals(o.getDescription(), 'b')

Yoshinori Okuji's avatar
Yoshinori Okuji committed
606 607 608 609 610 611 612 613 614
  def CheckScheduling(self, activity):
    """
      Check if active objects with different after parameters are executed in a correct order
    """
    portal = self.getPortal()
    organisation_module = self.getOrganisationModule()
    if not organisation_module.hasContent(self.company_id):
      organisation_module.newContent(id=self.company_id)
    o = portal.organisation._getOb(self.company_id)
615

Yoshinori Okuji's avatar
Yoshinori Okuji committed
616 617
    o.setTitle('?')
    self.assertEquals(o.getTitle(), '?')
618
    transaction.commit()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
619
    self.tic()
620

Yoshinori Okuji's avatar
Yoshinori Okuji committed
621 622 623
    def toto(self, s):
      self.setTitle(self.getTitle() + s)
    o.__class__.toto = toto
624

Yoshinori Okuji's avatar
Yoshinori Okuji committed
625
    o.activate(tag = 'toto', activity = activity).toto('a')
626
    transaction.commit()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
627
    o.activate(after_tag = 'titi', activity = activity).toto('b')
628
    transaction.commit()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
629
    o.activate(tag = 'titi', after_tag = 'toto', activity = activity).setTitle('c')
630
    transaction.commit()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
631 632
    self.tic()
    self.assertEquals(o.getTitle(), 'cb')
633

634 635 636 637 638 639 640 641 642 643 644 645
  def CheckSchedulingAfterTagList(self, activity):
    """
      Check if active objects with different after parameters are executed in a
      correct order, when after_tag is passed as a list
    """
    portal = self.getPortal()
    organisation_module = self.getOrganisationModule()
    if not organisation_module.hasContent(self.company_id):
      organisation_module.newContent(id=self.company_id)
    o = portal.organisation._getOb(self.company_id)

    o.setTitle('')
646
    transaction.commit()
647 648 649 650 651 652 653
    self.tic()

    def toto(self, s):
      self.setTitle(self.getTitle() + s)
    o.__class__.toto = toto

    o.activate(tag='A', activity=activity).toto('a')
654
    transaction.commit()
655
    o.activate(tag='B', activity=activity).toto('b')
656
    transaction.commit()
657
    o.activate(after_tag=('A', 'B'), activity=activity).setTitle('last')
658
    transaction.commit()
659 660 661
    self.tic()
    self.assertEquals(o.getTitle(), 'last')

662
  def CheckClearActivities(self, activity, activity_count=1):
663 664 665 666 667 668 669
    """
      Check if active objects are held even after clearing the tables.
    """
    portal = self.getPortal()
    organisation_module = self.getOrganisationModule()
    if not organisation_module.hasContent(self.company_id):
      organisation_module.newContent(id=self.company_id)
670
    transaction.commit()
671 672 673 674
    self.tic()

    def check(o):
      message_list = portal.portal_activities.getMessageList()
675
      self.assertEquals(len(message_list), activity_count)
676 677 678
      m = message_list[0]
      self.assertEquals(m.object_path, o.getPhysicalPath())
      self.assertEquals(m.method_id, '_setTitle')
679

680
    o = portal.organisation._getOb(self.company_id)
681 682
    for i in range(activity_count):
      o.activate(activity=activity)._setTitle('foo')
683
    transaction.commit()
684
    check(o)
685

686
    portal.portal_activities.manageClearActivities()
687
    transaction.commit()
688
    check(o)
689

690
    transaction.commit()
691 692 693
    self.tic()

    self.assertEquals(o.getTitle(), 'foo')
694 695 696 697 698 699 700 701 702 703 704 705

  def CheckCountMessageWithTag(self, activity):
    """
      Check countMessageWithTag function.
    """
    portal = self.getPortal()
    portal_activities = portal.portal_activities
    organisation_module = self.getOrganisationModule()
    if not organisation_module.hasContent(self.company_id):
      organisation_module.newContent(id=self.company_id)
    o = portal.organisation._getOb(self.company_id)
    o.setTitle('?')
706
    transaction.commit()
707 708 709
    self.tic()

    o.activate(tag = 'toto', activity = activity).setTitle('a')
710
    transaction.commit()
711 712 713 714 715 716
    self.assertEquals(o.getTitle(), '?')
    self.assertEquals(portal_activities.countMessageWithTag('toto'), 1)
    self.tic()
    self.assertEquals(o.getTitle(), 'a')
    self.assertEquals(portal_activities.countMessageWithTag('toto'), 0)

717 718 719 720 721 722 723 724 725 726 727 728
  def TryConflictErrorsWhileValidating(self, activity):
    """Try to execute active objects which may throw conflict errors
    while validating, and check if they are still executed."""
    # Make sure that no active object is installed.
    activity_tool = self.getPortal().portal_activities
    activity_tool.manageClearActivities(keep=0)

    # Need an object.
    organisation_module = self.getOrganisationModule()
    if not organisation_module.hasContent(self.company_id):
      organisation_module.newContent(id=self.company_id)
    o = organisation_module._getOb(self.company_id)
729
    transaction.commit()
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
    self.flushAllActivities(silent = 1, loop_size = 10)
    self.assertEquals(len(activity_tool.getMessageList()), 0)

    # Monkey patch Queue to induce conflict errors artificially.
    def validate(self, *args, **kwargs):
      from Products.CMFActivity.Activity.Queue import Queue
      if Queue.current_num_conflict_errors < Queue.conflict_errors_limit:
        Queue.current_num_conflict_errors += 1
        # LOG('TryConflictErrorsWhileValidating', 0, 'causing a conflict error artificially')
        raise ConflictError
      return self.original_validate(*args, **kwargs)
    from Products.CMFActivity.Activity.Queue import Queue
    Queue.original_validate = Queue.validate
    Queue.validate = validate

    try:
      # Test some range of conflict error occurences.
      for i in xrange(10):
        Queue.current_num_conflict_errors = 0
        Queue.conflict_errors_limit = i
        o.activate(activity = activity).getId()
751
        transaction.commit()
752 753 754 755 756 757 758 759
        self.flushAllActivities(silent = 1, loop_size = i + 10)
        self.assertEquals(len(activity_tool.getMessageList()), 0)
    finally:
      Queue.validate = Queue.original_validate
      del Queue.original_validate
      del Queue.current_num_conflict_errors
      del Queue.conflict_errors_limit

760 761 762 763 764 765 766 767 768 769 770 771
  def TryErrorsWhileFinishingCommitDB(self, activity):
    """Try to execute active objects which may throw conflict errors
    while validating, and check if they are still executed."""
    # Make sure that no active object is installed.
    activity_tool = self.getPortal().portal_activities
    activity_tool.manageClearActivities(keep=0)

    # Need an object.
    organisation_module = self.getOrganisationModule()
    if not organisation_module.hasContent(self.company_id):
      organisation_module.newContent(id=self.company_id)
    o = organisation_module._getOb(self.company_id)
772
    transaction.commit()
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
    self.flushAllActivities(silent = 1, loop_size = 10)
    self.assertEquals(len(activity_tool.getMessageList()), 0)

    from _mysql_exceptions import OperationalError

    # Monkey patch Queue to induce conflict errors artificially.
    def query(self, query_string,*args, **kw):
      # No so nice, this is specific to zsql method
      if query_string.find("REPLACE INTO")>=0:
        raise OperationalError
      else:
        return self.original_query(query_string,*args, **kw)
    from Products.ZMySQLDA.db import DB
    portal = self.getPortal()

    try:
      # Test some range of conflict error occurences.
      organisation_module.recursiveReindexObject()
791
      transaction.commit()
792 793 794 795 796
      self.assertEquals(len(activity_tool.getMessageList()), 1)
      DB.original_query = DB.query
      DB.query = query
      portal.portal_activities.distribute()
      portal.portal_activities.tic()
797
      transaction.commit()
798 799 800 801 802 803 804
      DB.query = DB.original_query
      message_list = portal.portal_activities.getMessageList()
      self.assertEquals(len(message_list),1)
    finally:
      DB.query = DB.original_query
      del DB.original_query

805 806 807 808 809 810 811
  def checkIsMessageRegisteredMethod(self, activity):
    activity_tool = self.getPortal().portal_activities
    object_a = self.getOrganisationModule()
    if not object_a.hasContent(self.company_id):
      object_a.newContent(id=self.company_id)
    object_b = object_a._getOb(self.company_id)
    activity_tool.manageClearActivities(keep=0)
812
    transaction.commit()
813 814 815 816
    # First case: creating the same activity twice must only register one.
    self.assertEquals(len(activity_tool.getMessageList()), 0) # Sanity check
    object_a.activate(activity=activity).getId()
    object_a.activate(activity=activity).getId()
817
    transaction.commit()
818 819
    self.assertEquals(len(activity_tool.getMessageList()), 1)
    activity_tool.manageClearActivities(keep=0)
820
    transaction.commit()
821 822 823 824 825
    # Second case: creating activity with same tag must only register one.
    # This behaviour is actually the same as the no-tag behaviour.
    self.assertEquals(len(activity_tool.getMessageList()), 0) # Sanity check
    object_a.activate(activity=activity, tag='foo').getId()
    object_a.activate(activity=activity, tag='foo').getId()
826
    transaction.commit()
827 828
    self.assertEquals(len(activity_tool.getMessageList()), 1)
    activity_tool.manageClearActivities(keep=0)
829
    transaction.commit()
830 831 832 833
    # Third case: creating activities with different tags must register both.
    self.assertEquals(len(activity_tool.getMessageList()), 0) # Sanity check
    object_a.activate(activity=activity, tag='foo').getId()
    object_a.activate(activity=activity, tag='bar').getId()
834
    transaction.commit()
835 836
    self.assertEquals(len(activity_tool.getMessageList()), 2)
    activity_tool.manageClearActivities(keep=0)
837
    transaction.commit()
838 839 840 841 842
    # Fourth case: creating activities on different objects must register
    # both.
    self.assertEquals(len(activity_tool.getMessageList()), 0) # Sanity check
    object_a.activate(activity=activity).getId()
    object_b.activate(activity=activity).getId()
843
    transaction.commit()
844 845
    self.assertEquals(len(activity_tool.getMessageList()), 2)
    activity_tool.manageClearActivities(keep=0)
846
    transaction.commit()
847 848 849 850 851
    # Fifth case: creating activities with different method must register
    # both.
    self.assertEquals(len(activity_tool.getMessageList()), 0) # Sanity check
    object_a.activate(activity=activity).getId()
    object_a.activate(activity=activity).getTitle()
852
    transaction.commit()
853 854
    self.assertEquals(len(activity_tool.getMessageList()), 2)
    activity_tool.manageClearActivities(keep=0)
855
    transaction.commit()
856

Yoshinori Okuji's avatar
Yoshinori Okuji committed
857
  def test_01_DeferredSetTitleSQLDict(self, quiet=0, run=run_all_test):
858 859 860
    # Test if we can add a complete sales order
    if not run: return
    if not quiet:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
861
      message = '\nTest Deferred Set Title SQLDict '
862 863
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
864
    self.DeferredSetTitleActivity('SQLDict')
865

Yoshinori Okuji's avatar
Yoshinori Okuji committed
866
  def test_02_DeferredSetTitleSQLQueue(self, quiet=0, run=run_all_test):
867 868 869
    # Test if we can add a complete sales order
    if not run: return
    if not quiet:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
870
      message = '\nTest Deferred Set Title SQLQueue '
871 872
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
873
    self.DeferredSetTitleActivity('SQLQueue')
874

Yoshinori Okuji's avatar
Yoshinori Okuji committed
875
  def test_03_DeferredSetTitleRAMDict(self, quiet=0, run=run_all_test):
876 877 878
    # Test if we can add a complete sales order
    if not run: return
    if not quiet:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
879
      message = '\nTest Deferred Set Title RAMDict '
880 881
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
882
    self.DeferredSetTitleActivity('RAMDict')
Sebastien Robin's avatar
Sebastien Robin committed
883

Yoshinori Okuji's avatar
Yoshinori Okuji committed
884
  def test_04_DeferredSetTitleRAMQueue(self, quiet=0, run=run_all_test):
Sebastien Robin's avatar
Sebastien Robin committed
885 886 887
    # Test if we can add a complete sales order
    if not run: return
    if not quiet:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
888
      message = '\nTest Deferred Set Title RAMQueue '
Sebastien Robin's avatar
Sebastien Robin committed
889 890
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
891
    self.DeferredSetTitleActivity('RAMQueue')
Sebastien Robin's avatar
Sebastien Robin committed
892 893 894 895 896 897 898 899

  def test_05_InvokeAndCancelSQLDict(self, quiet=0, run=run_all_test):
    # Test if we can add a complete sales order
    if not run: return
    if not quiet:
      message = '\nTest Invoke And Cancel SQLDict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
900
    self.InvokeAndCancelActivity('SQLDict')
Sebastien Robin's avatar
Sebastien Robin committed
901 902 903 904 905 906 907 908

  def test_06_InvokeAndCancelSQLQueue(self, quiet=0, run=run_all_test):
    # Test if we can add a complete sales order
    if not run: return
    if not quiet:
      message = '\nTest Invoke And Cancel SQLQueue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
909
    self.InvokeAndCancelActivity('SQLQueue')
Sebastien Robin's avatar
Sebastien Robin committed
910

911
  def test_07_InvokeAndCancelRAMDict(self, quiet=0, run=run_all_test):
Sebastien Robin's avatar
Sebastien Robin committed
912 913 914
    # Test if we can add a complete sales order
    if not run: return
    if not quiet:
915
      message = '\nTest Invoke And Cancel RAMDict '
Sebastien Robin's avatar
Sebastien Robin committed
916 917
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
918
    self.InvokeAndCancelActivity('RAMDict')
Sebastien Robin's avatar
Sebastien Robin committed
919

920
  def test_08_InvokeAndCancelRAMQueue(self, quiet=0, run=run_all_test):
Sebastien Robin's avatar
Sebastien Robin committed
921 922 923
    # Test if we can add a complete sales order
    if not run: return
    if not quiet:
924
      message = '\nTest Invoke And Cancel RAMQueue '
Sebastien Robin's avatar
Sebastien Robin committed
925 926
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
927
    self.InvokeAndCancelActivity('RAMQueue')
Sebastien Robin's avatar
Sebastien Robin committed
928

929 930 931 932 933 934 935 936 937 938
  def test_09_CallOnceWithSQLDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nCall Once With SQL Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.CallOnceWithActivity('SQLDict')

  def test_10_CallOnceWithSQLQueue(self, quiet=0, run=run_all_test):
Sebastien Robin's avatar
Sebastien Robin committed
939 940 941 942 943 944
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nCall Once With SQL Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
945 946 947 948 949 950 951 952 953 954
    self.CallOnceWithActivity('SQLQueue')

  def test_11_CallOnceWithRAMDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nCall Once With RAM Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.CallOnceWithActivity('RAMDict')
Sebastien Robin's avatar
Sebastien Robin committed
955

956
  def test_12_CallOnceWithRAMQueue(self, quiet=0, run=run_all_test):
Sebastien Robin's avatar
Sebastien Robin committed
957 958 959 960 961 962
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nCall Once With RAM Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
963
    self.CallOnceWithActivity('RAMQueue')
Sebastien Robin's avatar
Sebastien Robin committed
964

965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
  def test_13_TryMessageWithErrorOnSQLDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Message With Error On SQL Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryMessageWithErrorOnActivity('SQLDict')

  def test_14_TryMessageWithErrorOnSQLQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Message With Error On SQL Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryMessageWithErrorOnActivity('SQLQueue')

  def test_15_TryMessageWithErrorOnRAMDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Message With Error On RAM Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryMessageWithErrorOnActivity('RAMDict')

  def test_16_TryMessageWithErrorOnRAMQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Message With Error On RAM Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryMessageWithErrorOnActivity('RAMQueue')

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
  def test_17_TryFlushActivityWithSQLDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Flush Activity With SQL Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryFlushActivity('SQLDict')

  def test_18_TryFlushActivityWithSQLQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Flush Activity With SQL Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryFlushActivity('SQLQueue')

  def test_19_TryFlushActivityWithRAMDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Flush Activity With RAM Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryFlushActivity('RAMDict')

  def test_20_TryFlushActivityWithRAMQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Flush Activity With RAM Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryFlushActivity('RAMQueue')

1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
  def test_21_TryActivateInsideFlushWithSQLDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Activate Inside Flush With SQL Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivateInsideFlush('SQLDict')

  def test_22_TryActivateInsideFlushWithSQLQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Activate Inside Flush With SQL Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivateInsideFlush('SQLQueue')

  def test_23_TryActivateInsideFlushWithRAMDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Activate Inside Flush With RAM Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivateInsideFlush('RAMDict')

  def test_24_TryActivateInsideFlushWithRAMQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Activate Inside Flush With RAM Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivateInsideFlush('RAMQueue')

  def test_25_TryTwoMethodsWithSQLDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Two Methods With SQL Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryTwoMethods('SQLDict')

  def test_26_TryTwoMethodsWithSQLQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Two Methods With SQL Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryTwoMethods('SQLQueue')

  def test_27_TryTwoMethodsWithRAMDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Two Methods With RAM Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryTwoMethods('RAMDict')

  def test_28_TryTwoMethodsWithRAMQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Two Methods With RAM Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryTwoMethods('RAMQueue')

  def test_29_TryTwoMethodsAndFlushThemWithSQLDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Two Methods And Flush Them With SQL Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryTwoMethodsAndFlushThem('SQLDict')

  def test_30_TryTwoMethodsAndFlushThemWithSQLQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Two Methods And Flush Them With SQL Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryTwoMethodsAndFlushThem('SQLQueue')

  def test_31_TryTwoMethodsAndFlushThemWithRAMDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Two Methods And Flush Them With RAM Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryTwoMethodsAndFlushThem('RAMDict')

  def test_32_TryTwoMethodsAndFlushThemWithRAMQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Two Methods And Flush Them With RAM Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryTwoMethodsAndFlushThem('RAMQueue')
Sebastien Robin's avatar
Sebastien Robin committed
1144

1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
  def test_33_TryActivateFlushActivateTicWithSQLDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Activate Flush Activate Tic With SQL Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivateFlushActivateTic('SQLDict')

  def test_34_TryActivateFlushActivateTicWithSQLQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Activate Flush Activate Tic With SQL Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivateFlushActivateTic('SQLQueue')

  def test_35_TryActivateFlushActivateTicWithRAMDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Activate Flush Activate Tic With RAM Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivateFlushActivateTic('RAMDict')

  def test_36_TryActivateFlushActivateTicWithRAMQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Activate Flush Activate Tic With RAM Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivateFlushActivateTic('RAMQueue')

  def test_37_TryActivateFlushActivateTicWithMultipleActivities(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Activate Flush Activate Tic With MultipleActivities '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivateFlushActivateTic('SQLQueue',second='SQLDict')
    self.TryActivateFlushActivateTic('SQLDict',second='SQLQueue')

  def test_38_TryCommitSubTransactionWithSQLDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Commit Sub Transaction With SQL Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivateFlushActivateTic('SQLDict',commit_sub=1)

  def test_39_TryCommitSubTransactionWithSQLQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Commit Sub Transaction With SQL Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivateFlushActivateTic('SQLQueue',commit_sub=1)

  def test_40_TryCommitSubTransactionWithRAMDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Commit Sub Transaction With RAM Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivateFlushActivateTic('RAMDict',commit_sub=1)

  def test_41_TryCommitSubTransactionWithRAMQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Commit Sub Transaction With RAM Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivateFlushActivateTic('RAMQueue',commit_sub=1)

1227 1228 1229 1230 1231 1232 1233
  def test_42_TryRenameObjectWithSQLDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Rename Object With SQL Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
1234
    self.DeferredSetTitleWithRenamedObject('SQLDict')
1235 1236 1237 1238 1239 1240 1241 1242

  def test_43_TryRenameObjectWithSQLQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Rename Object With SQL Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
1243
    self.DeferredSetTitleWithRenamedObject('SQLQueue')
1244 1245 1246 1247 1248 1249 1250 1251

  def test_44_TryRenameObjectWithRAMDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Rename Object With RAM Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
1252
    self.DeferredSetTitleWithRenamedObject('RAMDict')
1253

1254 1255 1256 1257 1258 1259 1260
  def test_45_TryRenameObjectWithRAMQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Rename Object With RAM Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
1261
    self.DeferredSetTitleWithRenamedObject('RAMQueue')
Sebastien Robin's avatar
Sebastien Robin committed
1262

1263 1264 1265 1266 1267 1268 1269 1270
  def test_46_TryActiveProcessWithSQLDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Active Process With SQL Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActiveProcess('SQLDict')
Sebastien Robin's avatar
Sebastien Robin committed
1271

1272 1273 1274 1275 1276 1277 1278 1279
  def test_47_TryActiveProcessWithSQLQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Active Process With SQL Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActiveProcess('SQLQueue')
Sebastien Robin's avatar
Sebastien Robin committed
1280

1281 1282 1283 1284 1285 1286 1287 1288
  def test_48_TryActiveProcessWithRAMDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Active Process With RAM Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActiveProcess('RAMDict')
Sebastien Robin's avatar
Sebastien Robin committed
1289

1290 1291 1292 1293 1294 1295 1296 1297
  def test_49_TryActiveProcessWithRAMQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Active Process With RAM Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActiveProcess('RAMQueue')
Sebastien Robin's avatar
Sebastien Robin committed
1298

1299
  def test_50_TryActiveProcessInsideActivityWithSQLDict(self, quiet=0, run=run_all_test):
1300 1301 1302
    # Test if we call methods only once
    if not run: return
    if not quiet:
1303
      message = '\nTry Active Process Inside Activity With SQL Dict '
1304 1305 1306
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActiveProcessInsideActivity('SQLDict')
Sebastien Robin's avatar
Sebastien Robin committed
1307

1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
  def test_51_TryActiveProcessInsideActivityWithSQLQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Active Process Inside Activity With SQL Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActiveProcessInsideActivity('SQLQueue')

  def test_52_TryActiveProcessInsideActivityWithRAMDict(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Active Process Inside Activity With RAM Dict '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActiveProcessInsideActivity('RAMDict')

  def test_53_TryActiveProcessInsideActivityWithRAMQueue(self, quiet=0, run=run_all_test):
    # Test if we call methods only once
    if not run: return
    if not quiet:
      message = '\nTry Active Process Inside Activity With RAM Queue '
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActiveProcessInsideActivity('RAMQueue')

1335 1336 1337 1338
  def test_54_TryAfterMethodIdWithSQLDict(self, quiet=0, run=run_all_test):
    # Test if after_method_id can be used
    if not run: return
    if not quiet:
1339
      message = '\nTry Active Method After Another Activate Method With SQLDict'
1340 1341 1342
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryMethodAfterMethod('SQLDict')
1343

1344 1345 1346 1347 1348 1349 1350 1351
  def test_55_TryAfterMethodIdWithSQLQueue(self, quiet=0, run=run_all_test):
    # Test if after_method_id can be used
    if not run: return
    if not quiet:
      message = '\nTry Active Method After Another Activate Method With SQLQueue'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryMethodAfterMethod('SQLQueue')
1352

1353
  def test_56_TryCallActivityWithRightUser(self, quiet=0, run=run_all_test):
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
    # Test if me execute methods with the right user
    # This should be independant of the activity used
    if not run: return
    if not quiet:
      message = '\nTry Call Activity With Right User'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    # We are first logged as seb
    portal = self.getPortal()
    organisation =  portal.organisation._getOb(self.company_id)
    # Add new user toto
    uf = self.getPortal().acl_users
    uf._doAddUser('toto', '', ['Manager'], [])
    user = uf.getUserById('toto').__of__(uf)
    newSecurityManager(None, user)
    # Execute something as toto
    organisation.activate().newContent(portal_type='Email',id='email')
    # Then execute activities as seb
    user = uf.getUserById('seb').__of__(uf)
    newSecurityManager(None, user)
1374
    transaction.commit()
1375 1376 1377 1378 1379 1380
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
    email = organisation.get('email')
    # Check if what we did was executed as toto
    self.assertEquals(email.getOwnerInfo()['id'],'toto')

1381 1382 1383 1384 1385 1386 1387 1388
  def test_57_ExpandedMethodWithDeletedSubObject(self, quiet=0, run=run_all_test):
    # Test if after_method_id can be used
    if not run: return
    if not quiet:
      message = '\nTry Expanded Method With Deleted Sub Object'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.ExpandedMethodWithDeletedSubObject('SQLDict')
1389

1390 1391 1392 1393 1394 1395 1396 1397
  def test_58_ExpandedMethodWithDeletedObject(self, quiet=0, run=run_all_test):
    # Test if after_method_id can be used
    if not run: return
    if not quiet:
      message = '\nTry Expanded Method With Deleted Object'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.ExpandedMethodWithDeletedObject('SQLDict')
Sebastien Robin's avatar
Sebastien Robin committed
1398

1399 1400 1401 1402 1403 1404 1405 1406 1407
  def test_59_TryAfterTagWithSQLDict(self, quiet=0, run=run_all_test):
    # Test if after_tag can be used
    if not run: return
    if not quiet:
      message = '\nTry After Tag With SQL Dict'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryAfterTag('SQLDict')

1408
  def test_60_TryAfterTagWithSQLQueue(self, quiet=0, run=run_all_test):
1409 1410 1411 1412 1413 1414 1415 1416
    # Test if after_tag can be used
    if not run: return
    if not quiet:
      message = '\nTry After Tag With SQL Queue'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryAfterTag('SQLQueue')

Yoshinori Okuji's avatar
Yoshinori Okuji committed
1417 1418 1419 1420 1421 1422 1423 1424 1425
  def test_61_CheckSchedulingWithSQLDict(self, quiet=0, run=run_all_test):
    # Test if scheduling is correct with SQLDict
    if not run: return
    if not quiet:
      message = '\nCheck Scheduling With SQL Dict'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.CheckScheduling('SQLDict')

1426
  def test_62_CheckSchedulingWithSQLQueue(self, quiet=0, run=run_all_test):
Yoshinori Okuji's avatar
Yoshinori Okuji committed
1427 1428 1429 1430 1431 1432 1433 1434
    # Test if scheduling is correct with SQLQueue
    if not run: return
    if not quiet:
      message = '\nCheck Scheduling With SQL Queue'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.CheckScheduling('SQLQueue')

1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
  def test_61_CheckSchedulingAfterTagListWithSQLDict(self, quiet=0, run=run_all_test):
    # Test if scheduling is correct with SQLDict
    if not run: return
    if not quiet:
      message = '\nCheck Scheduling After Tag List With SQL Dict'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.CheckSchedulingAfterTagList('SQLDict')

  def test_62_CheckSchedulingWithAfterTagListSQLQueue(self, quiet=0, run=run_all_test):
    # Test if scheduling is correct with SQLQueue
    if not run: return
    if not quiet:
      message = '\nCheck Scheduling After Tag List With SQL Queue'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.CheckSchedulingAfterTagList('SQLQueue')

1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
  def test_63_CheckClearActivitiesWithSQLDict(self, quiet=0, run=run_all_test):
    # Test if clearing tables does not remove active objects with SQLDict
    if not run: return
    if not quiet:
      message = '\nCheck Clearing Activities With SQL Dict'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.CheckClearActivities('SQLDict')

  def test_64_CheckClearActivitiesWithSQLQueue(self, quiet=0, run=run_all_test):
    # Test if clearing tables does not remove active objects with SQLQueue
    if not run: return
    if not quiet:
      message = '\nCheck Clearing Activities With SQL Queue'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
1469
    self.CheckClearActivities('SQLQueue', activity_count=2)
1470

1471
  def flushAllActivities(self, silent=0, loop_size=1000):
1472 1473 1474 1475 1476 1477 1478 1479
    """Executes all messages until the queue only contains failed
    messages.
    """
    activity_tool = self.getPortal().portal_activities
    loop_count=0
    # flush activities
    while 1:
      loop_count += 1
1480 1481 1482
      if loop_count >= loop_size:
        if silent:
          return
1483 1484 1485 1486
        self.fail('flushAllActivities maximum loop count reached')

      activity_tool.distribute(node_count=1)
      activity_tool.tic(processing_node=1)
1487

1488 1489 1490 1491 1492 1493 1494
      finished = 1
      for message in activity_tool.getMessageList():
        if message.processing_node not in (INVOKE_ERROR_STATE,
                                           VALIDATE_ERROR_STATE):
          finished = 0

      activity_tool.timeShift(3 * VALIDATION_ERROR_DELAY)
1495
      transaction.commit()
1496
      if finished:
1497
        return
1498 1499 1500 1501 1502 1503 1504

  def test_65_TestMessageValidationAndFailedActivities(self,
                                              quiet=0, run=run_all_test):
    """after_method_id and failed activities.

    Tests that if we have an active method scheduled by
    after_method_id and a failed activity with this method id, the
1505 1506 1507 1508 1509 1510
    method is NOT executed.

    Note: earlier version of this test checked exactly the contrary, but it
    was eventually agreed that this was a bug. If an activity fails, all the
    activities that depend on it should be block until the first one is
    resolved."""
1511 1512 1513 1514 1515 1516
    if not run: return
    if not quiet:
      message = '\nafter_method_id and failed activities'
      ZopeTestCase._print(message)
      LOG('Testing... ', 0, message)
    activity_tool = self.getPortal().portal_activities
1517
    original_title = 'something'
1518
    obj = self.getPortal().organisation_module.newContent(
1519 1520 1521
                    portal_type='Organisation',
                    title=original_title)

1522 1523 1524 1525 1526 1527 1528 1529
    # Monkey patch Organisation to add a failing method
    def failingMethod(self):
      raise ValueError, 'This method always fail'
    Organisation.failingMethod = failingMethod

    # Monkey patch Message not to send failure notification emails
    from Products.CMFActivity.ActivityTool import Message
    originalNotifyUser = Message.notifyUser
1530
    def notifyUserSilent(self, *args, **kw):
1531 1532 1533 1534 1535 1536
      pass
    Message.notifyUser = notifyUserSilent

    activity_list = ['SQLQueue', 'SQLDict', ]
    for activity in activity_list:
      # reset
1537 1538
      activity_tool.manageClearActivities(keep=0)
      obj.setTitle(original_title)
1539
      transaction.commit()
1540 1541 1542 1543

      # activate failing message and flush
      for fail_activity in activity_list:
        obj.activate(activity = fail_activity).failingMethod()
1544
      transaction.commit()
1545 1546 1547 1548 1549 1550 1551 1552 1553
      self.flushAllActivities(silent=1, loop_size=100)
      full_message_list = activity_tool.getMessageList()
      remaining_messages = [a for a in full_message_list if a.method_id !=
          'failingMethod']
      if len(full_message_list) != 2:
        self.fail('failingMethod should not have been flushed')
      if len(remaining_messages) != 0:
        self.fail('Activity tool should have no other remaining messages')

1554 1555 1556 1557
      # activate our message
      new_title = 'nothing'
      obj.activate(after_method_id = ['failingMethod'],
                   activity = activity ).setTitle(new_title)
1558
      transaction.commit()
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
      self.flushAllActivities(silent=1, loop_size=100)
      full_message_list = activity_tool.getMessageList()
      remaining_messages = [a for a in full_message_list if a.method_id !=
          'failingMethod']
      if len(full_message_list) != 3:
        self.fail('failingMethod should not have been flushed')
      if len(remaining_messages) != 1:
        self.fail('Activity tool should have one blocked setTitle activity')
      self.assertEquals(remaining_messages[0].activity_kw['after_method_id'],
          ['failingMethod'])
      self.assertEquals(obj.getTitle(), original_title)

    # restore notification and flush failed and blocked activities
1572
    Message.notifyUser = originalNotifyUser
1573
    activity_tool.manageClearActivities(keep=0)
1574

1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
  def test_66_TestCountMessageWithTagWithSQLDict(self, quiet=0, run=run_all_test):
    """
      Test new countMessageWithTag function with SQLDict.
    """
    if not run: return
    if not quiet:
      message = '\nCheck countMessageWithTag'
      ZopeTestCase._print(message)
      LOG('Testing... ', 0, message)
    self.CheckCountMessageWithTag('SQLDict')

1586 1587
  def test_67_TestCancelFailedActiveObject(self, quiet=0, run=run_all_test):
    """Cancel an active object to make sure that it does not refer to
1588 1589 1590 1591
    a persistent object.

    XXX: this test fails if run first
    """
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
    if not run: return
    if not quiet:
      message = '\nTest if it is possible to safely cancel an active object'
      ZopeTestCase._print(message)
      LOG('Testing... ', 0, message)
    activity_tool = self.getPortal().portal_activities
    activity_tool.manageClearActivities(keep=0)

    original_title = 'something'
    obj = self.getPortal().organisation_module.newContent(
                    portal_type='Organisation',
                    title=original_title)

    # Monkey patch Organisation to add a failing method
    def failingMethod(self):
      raise ValueError, 'This method always fail'
    Organisation.failingMethod = failingMethod

    # Monkey patch Message not to send failure notification emails
    from Products.CMFActivity.ActivityTool import Message
    originalNotifyUser = Message.notifyUser
1613
    def notifyUserSilent(self, *args, **kw):
1614 1615 1616 1617
      pass
    Message.notifyUser = notifyUserSilent

    # First, index the object.
1618
    transaction.commit()
1619 1620 1621 1622 1623
    self.flushAllActivities(silent=1, loop_size=100)
    self.assertEquals(len(activity_tool.getMessageList()), 0)

    # Insert a failing active object.
    obj.activate().failingMethod()
1624
    transaction.commit()
1625 1626 1627
    self.assertEquals(len(activity_tool.getMessageList()), 1)

    # Just wait for the active object to be abandoned.
1628
    self.flushAllActivities(silent=1, loop_size=100)
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
    self.assertEquals(len(activity_tool.getMessageList()), 1)
    self.assertEquals(activity_tool.getMessageList()[0].processing_node, 
                      INVOKE_ERROR_STATE)

    # Make sure that persistent objects are not present in the connection
    # cache to emulate a restart of Zope. So all volatile attributes will
    # be flushed, and persistent objects will be reloaded.
    activity_tool._p_jar._resetCache()

    # Cancel it via the management interface.
    message = activity_tool.getMessageList()[0]
    activity_tool.manageCancel(message.object_path, message.method_id)
1641
    transaction.commit()
1642 1643
    self.assertEquals(len(activity_tool.getMessageList()), 0)

1644
  def test_68_RetryMessageExecution(self, quiet=0):
1645
    if not quiet:
1646
      message = '\nCheck number of executions of failing activities'
1647 1648
      ZopeTestCase._print(message)
      LOG('Testing... ', 0, message)
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
    activity_tool = self.portal.portal_activities
    self.assertFalse(activity_tool.getMessageList())
    exec_count = [0]
    # priority does not matter anymore
    priority = random.Random().randint
    def doSomething(self, retry_list):
      i = exec_count[0]
      exec_count[0] = i + 1
      conflict, edit_kw = retry_list[i]
      if edit_kw:
        self.getActivityRuntimeEnvironment().edit(**edit_kw)
      if conflict is not None:
        raise conflict and ConflictError or Exception
1662
    def check(retry_list, **activate_kw):
1663 1664 1665
      fail = retry_list[-1][0] is not None and 1 or 0
      for activity in 'SQLDict', 'SQLQueue':
        exec_count[0] = 0
1666 1667
        activity_tool.activate(activity=activity, priority=priority(1,6),
                               **activate_kw).doSomething(retry_list)
1668
        transaction.commit()
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
        self.flushAllActivities(silent=1)
        self.assertEqual(len(retry_list), exec_count[0])
        self.assertEqual(fail, len(activity_tool.getMessageList()))
        self.portal.portal_activities.manageCancel(
          activity_tool.getPhysicalPath(), 'doSomething')
    activity_tool.__class__.doSomething = doSomething
    try:
      ## Default behaviour
      # Usual successful case: activity is run only once
      check([(None, None)])
      # Usual error case: activity is run 6 times before being frozen
      check([(False, None)] * 6)
      # On ConflictError, activity is reexecuted without increasing retry count
      check([(True, None)] * 10 + [(None, None)])
      check([(True, None), (False, None)] * 6)
      ## Customized behaviour
      # Do not retry
      check([(False, {'max_retry': 0})])
      # ... even in case of ConflictError
      check([(True, {'max_retry': 0}),
             (True, {'max_retry': 0, 'conflict_retry': 0})])
1690
      check([(True, None)] * 6, conflict_retry=False)
1691 1692 1693 1694 1695 1696 1697 1698 1699
      # Customized number of retries
      for n in 3, 9:
        check([(False, {'max_retry': n})] * n + [(None, None)])
        check([(False, {'max_retry': n})] * (n + 1))
      # Infinite retry
      for n in 3, 9:
        check([(False, {'max_retry': None})] * n + [(None, None)])
        check([(False, {'max_retry': None})] * n + [(False, {'max_retry': 0})])
      check([(False, {'max_retry': None})] * 9 + [(False, None)])
1700

1701 1702 1703
    finally:
      del activity_tool.__class__.doSomething
    self.assertFalse(activity_tool.getMessageList())
1704

1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
  def test_70_TestConflictErrorsWhileValidatingWithSQLDict(self, quiet=0, run=run_all_test):
    """
      Test if conflict errors spoil out active objects with SQLDict.
    """
    if not run: return
    if not quiet:
      message = '\nTest Conflict Errors While Validating With SQLDict'
      ZopeTestCase._print(message)
      LOG('Testing... ', 0, message)
    self.TryConflictErrorsWhileValidating('SQLDict')

  def test_71_TestConflictErrorsWhileValidatingWithSQLQueue(self, quiet=0, run=run_all_test):
    """
      Test if conflict errors spoil out active objects with SQLQueue.
    """
    if not run: return
    if not quiet:
      message = '\nTest Conflict Errors While Validating With SQLQueue'
      ZopeTestCase._print(message)
      LOG('Testing... ', 0, message)
    self.TryConflictErrorsWhileValidating('SQLQueue')

1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
  def test_72_TestErrorsWhileFinishingCommitDBWithSQLDict(self, quiet=0, run=run_all_test):
    """
    """
    if not run: return
    if not quiet:
      message = '\nTest Errors While Finishing Commit DB With SQLDict'
      ZopeTestCase._print(message)
      LOG('Testing... ', 0, message)
    self.TryErrorsWhileFinishingCommitDB('SQLDict')

  def test_73_TestErrorsWhileFinishingCommitDBWithSQLQueue(self, quiet=0, run=run_all_test):
    """
    """
    if not run: return
    if not quiet:
      message = '\nTest Errors While Finishing Commit DB With SQLQueue'
      ZopeTestCase._print(message)
      LOG('Testing... ', 0, message)
    self.TryErrorsWhileFinishingCommitDB('SQLQueue')

1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
  def test_74_TryFlushActivityWithAfterTagSQLDict(self, quiet=0, run=run_all_test):
    # Test if after_tag can be used
    if not run: return
    if not quiet:
      message = '\nTry Flus Activity With After Tag With SQL Dict'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryFlushActivityWithAfterTag('SQLDict')

  def test_75_TryFlushActivityWithAfterTagWithSQLQueue(self, quiet=0, run=run_all_test):
    # Test if after_tag can be used
    if not run: return
    if not quiet:
      message = '\nTry Flush Activity With After Tag With SQL Queue'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryFlushActivityWithAfterTag('SQLQueue')

1765 1766 1767 1768 1769 1770 1771 1772 1773
  def test_76_ActivateKwForNewContent(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck reindex message uses activate_kw passed to newContent'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)

    o1 = self.getOrganisationModule().newContent(
                                  activate_kw=dict(tag='The Tag'))
1774
    transaction.commit()
1775 1776 1777 1778 1779 1780
    messages_for_o1 = [m for m in self.getActivityTool().getMessageList()
                       if m.object_path == o1.getPhysicalPath()]
    self.assertNotEquals(0, len(messages_for_o1))
    for m in messages_for_o1:
      self.assertEquals(m.activity_kw.get('tag'), 'The Tag')

1781

1782 1783 1784 1785 1786 1787 1788 1789
  def test_77_FlushAfterMultipleActivate(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck all message are flushed in SQLDict'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    orga_module = self.getOrganisationModule()
    p = orga_module.newContent(portal_type='Organisation')
1790
    transaction.commit()
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
    self.tic()
    self.assertEqual(p.getDescription(), "")
    activity_tool = self.getPortal().portal_activities

    def updateDesc(self):
      d =self.getDescription()
      self.setDescription(d+'a')
    Organisation.updateDesc = updateDesc

    self.assertEqual(len(activity_tool.getMessageList()), 0)
    # First check dequeue read same message only once
    for i in xrange(10):
      p.activate(activity="SQLDict").updateDesc()
1804
      transaction.commit()
1805

1806
    self.assertEqual(len(activity_tool.getMessageList()), 10)
1807 1808 1809 1810 1811 1812
    self.tic()
    self.assertEqual(p.getDescription(), "a")

    # Check if there is pending activity after deleting an object
    for i in xrange(10):
      p.activate(activity="SQLDict").updateDesc()
1813
      transaction.commit()
1814 1815

    self.assertEqual(len(activity_tool.getMessageList()), 10)
1816
    activity_tool.flush(p, invoke=0)
1817
    transaction.commit()
1818 1819
    self.assertEqual(len(activity_tool.getMessageList()), 0)

1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
  def test_78_IsMessageRegisteredSQLDict(self, quiet=0, run=run_all_test):
    """
      This test tests behaviour of IsMessageRegistered method.
    """
    if not run: return
    if not quiet:
      message = '\nTest IsMessageRegistered behaviour with SQLDict'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.checkIsMessageRegisteredMethod('SQLDict')

1831 1832
  def test_79_AbortTransactionSynchronously(self, quiet=0, run=run_all_test):
    """
1833 1834
      This test checks if transaction.abort() synchronizes connections. It
      didn't do so back in Zope 2.7
1835 1836 1837
    """
    if not run: return
    if not quiet:
1838
      message = '\nTest Aborting Transaction Synchronizes'
1839 1840 1841 1842 1843 1844 1845 1846
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)

    # Make a new persistent object, and commit it so that an oid gets
    # assigned.
    module = self.getOrganisationModule()
    organisation = module.newContent(portal_type = 'Organisation')
    organisation_id = organisation.getId()
1847
    transaction.commit()
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
    organisation = module[organisation_id]

    # Now fake a read conflict.
    from ZODB.POSException import ReadConflictError
    tid = organisation._p_serial
    oid = organisation._p_oid
    conn = organisation._p_jar
    if getattr(conn, '_mvcc', 0):
      conn._mvcc = 0 # XXX disable MVCC forcibly
    try:
      conn.db().invalidate({oid: tid})
    except TypeError:
      conn.db().invalidate(tid, {oid: tid})
    conn._cache.invalidate(oid)

1863 1864
    # Access to invalidated object in non-MVCC connections should raise a
    # conflict error
1865 1866
    organisation = module[organisation_id]
    self.assertRaises(ReadConflictError, getattr, organisation, 'uid')
1867 1868

    # In Zope 2.7, abort does not sync automatically, so even after abort,
1869 1870 1871
    # ReadConflictError would be raised. But in Zope 2.8, this is automatic.

    transaction.abort()
1872 1873
    getattr(organisation, 'uid')

1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907

  def test_80_CallWithGroupIdParamater(self, quiet=0, run=run_all_test):
    """
    Test that group_id parameter is used to separate execution of the same method
    """
    if not run: return
    if not quiet:
      message = '\nTest Activity with group_id parameter'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)

    portal = self.getPortal()    
    organisation =  portal.organisation._getOb(self.company_id)
    # Defined a group method
    def setFoobar(self, object_list, number=1):
      for obj in object_list:
        if getattr(obj,'foobar', None) is not None:
          obj.foobar = obj.foobar + number
        else:
          obj.foobar = number
      object_list[:] = []
    from Products.ERP5Type.Document.Folder import Folder
    Folder.setFoobar = setFoobar    

    def getFoobar(self):
      return (getattr(self,'foobar',0))
    Organisation.getFoobar = getFoobar

    organisation.foobar = 0
    self.assertEquals(0,organisation.getFoobar())

    # Test group_method_id is working without group_id
    for x in xrange(5):
      organisation.activate(activity='SQLDict', group_method_id="organisation_module/setFoobar").reindexObject(number=1)
1908
      transaction.commit()      
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919

    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),5)
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
    self.assertEquals(1, organisation.getFoobar())


    # Test group_method_id is working with one group_id defined
    for x in xrange(5):
      organisation.activate(activity='SQLDict', group_method_id="organisation_module/setFoobar", group_id="1").reindexObject(number=1)
1920
      transaction.commit()      
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930

    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),5)
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
    self.assertEquals(2, organisation.getFoobar())

    # Test group_method_id is working with many group_id defined
    for x in xrange(5):
      organisation.activate(activity='SQLDict', group_method_id="organisation_module/setFoobar", group_id="1").reindexObject(number=1)
1931
      transaction.commit()      
1932
      organisation.activate(activity='SQLDict', group_method_id="organisation_module/setFoobar", group_id="2").reindexObject(number=3)
1933
      transaction.commit()
1934
      organisation.activate(activity='SQLDict', group_method_id="organisation_module/setFoobar", group_id="1").reindexObject(number=1)
1935
      transaction.commit()
1936
      organisation.activate(activity='SQLDict', group_method_id="organisation_module/setFoobar", group_id="3").reindexObject(number=5)
1937
      transaction.commit()
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947

    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list),20)
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
    self.assertEquals(11, organisation.getFoobar())
    message_list = portal.portal_activities.getMessageList()
    self.assertEquals(len(message_list), 0)
    

1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
  def test_81_ActivateKwForWorkflowTransition(self, quiet=0, run=run_all_test):
    """
    Test call of a workflow transition with activate_kw parameter propagate them
    """
    if not run: return
    if not quiet:
      message = '\nCheck reindex message uses activate_kw passed to workflow transition'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    o1 = self.getOrganisationModule().newContent()
1958
    transaction.commit()
1959 1960
    self.tic()
    o1.validate(activate_kw=dict(tag='The Tag'))
1961
    transaction.commit()
1962 1963 1964 1965 1966 1967
    messages_for_o1 = [m for m in self.getActivityTool().getMessageList()
                       if m.object_path == o1.getPhysicalPath()]
    self.assertNotEquals(0, len(messages_for_o1))
    for m in messages_for_o1:
      self.assertEquals(m.activity_kw.get('tag'), 'The Tag')
  
1968 1969 1970 1971 1972 1973 1974 1975 1976
  def test_82_LossOfVolatileAttribute(self, quiet=0, run=run_all_test):
    """
    Test that the loss of volatile attribute doesn't loose activities
    """
    if not run: return
    if not quiet:
      message = '\nCheck loss of volatile attribute doesn\'t cause message to be lost'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
1977
    transaction.commit()
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
    self.tic()
    activity_tool = self.getActivityTool()
    message_list = activity_tool.getMessageList()
    self.assertEquals(len(message_list), 0)
    def delete_volatiles():
      for property_id in activity_tool.__dict__.keys():
        if property_id.startswith('_v_'):
          delattr(activity_tool, property_id)
    organisation_module = self.getOrganisationModule()
    active_organisation_module = organisation_module.activate()
    delete_volatiles()
    # Cause a message to be created
    # If the buffer cannot be created, this will raise
    active_organisation_module.getTitle()
    delete_volatiles()
    # Another activity to check that first one did not get lost even if volatile disapears
    active_organisation_module.getId()
1995
    transaction.commit()
1996 1997
    message_list = activity_tool.getMessageList()
    self.assertEquals(len(message_list), 2)
1998

1999 2000 2001 2002 2003 2004 2005 2006 2007 2008
  def test_83_ActivityModificationsViaCMFActivityConnectionRolledBackOnErrorSQLDict(self, quiet=0, run=run_all_test):
    """
      When an activity modifies tables through CMFActivity SQL connection and
      raises, check that its changes are correctly rolled back.
    """
    if not run: return
    if not quiet:
      message = '\nCheck activity modifications via CMFActivity connection are rolled back on error (SQLDict)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
2009
    transaction.commit()
2010
    self.tic()
2011
    activity_tool = self.getActivityTool()
2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
    def modifySQLAndFail(self, object_list, **kw):
      # Only create the dummy activity if none is present: we would just
      # generate missleading errors (duplicate uid).
      if activity_tool.countMessage(method_id='dummy_activity') == 0:
        # Add a dumy activity which will not be executed
        # Modified table does not matter
        method_id = 'dummy_activity'
        path = '/'.join(self.getPhysicalPath())
        message = Message(self, None, {}, method_id, (), {})
        pickled_message = pickle.dumps(message)
        self.SQLDict_writeMessageList(
          uid_list=[0], # This uid is never automaticaly assigned (starts at 1)
          date_list=[DateTime().Date()],
          path_list=[path],
2026
          active_process_uid=[None],
2027 2028 2029 2030 2031 2032
          method_id_list=[method_id],
          message_list=[pickled_message],
          priority_list=[1],
          processing_node_list=[-2],
          group_method_id_list=[''],
          tag_list=[''],
2033 2034
          order_validation_text_list=[''],
          serialization_tag_list=[''],
2035 2036 2037 2038 2039 2040 2041
          )
      if len(object_list) == 2:
        # Remove one entry from object list: this is understood by caller as a
        # success for this entry.
        object_list.pop()
    def dummy(self):
      pass
2042 2043 2044 2045 2046 2047 2048 2049
    try:
      Organisation.modifySQLAndFail = modifySQLAndFail
      Organisation.dummy = dummy
      obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
      group_method_id = '%s/modifySQLAndFail' % (obj.getPath(), )
      obj.activate(activity='SQLDict', group_method_id=group_method_id).dummy()
      obj2 = self.getPortal().organisation_module.newContent(portal_type='Organisation')
      obj2.activate(activity='SQLDict', group_method_id=group_method_id).dummy()
2050
      transaction.commit()
2051 2052 2053 2054 2055
      self.flushAllActivities(silent=1, loop_size=100)
      self.assertEquals(activity_tool.countMessage(method_id='dummy_activity'), 0)
    finally:
      delattr(Organisation, 'modifySQLAndFail')
      delattr(Organisation, 'dummy')
2056

2057
  def test_84_ActivityModificationsViaCMFActivityConnectionRolledBackOnErrorSQLQueue(self, quiet=0, run=run_all_test):
2058 2059 2060 2061 2062 2063
    """
      When an activity modifies tables through CMFActivity SQL connection and
      raises, check that its changes are correctly rolled back.
    """
    if not run: return
    if not quiet:
2064
      message = '\nCheck activity modifications via CMFActivity connection are rolled back on error (SQLQueue)'
2065 2066
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
2067
    transaction.commit()
2068
    self.tic()
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
    activity_tool = self.getActivityTool()
    def modifySQLAndFail(self):
      # Only create the dummy activity if none is present: we would just
      # generate missleading errors (duplicate uid).
      if activity_tool.countMessage(method_id='dummy_activity') == 0:
        # Add a dumy activity which will not be executed
        # Modified table does not matter
        method_id = 'dummy_activity'
        path = '/'.join(self.getPhysicalPath())
        message = Message(self, None, {}, method_id, (), {})
        pickled_message = pickle.dumps(message)
        self.SQLDict_writeMessageList(
          uid_list=[0], # This uid is never automaticaly assigned (starts at 1)
          date_list=[DateTime().Date()],
          path_list=[path],
          method_id_list=[method_id],
          message_list=[pickled_message],
          priority_list=[1],
          processing_node_list=[-2],
          group_method_id_list=[''],
          tag_list=[''],
2090 2091
          order_validation_text_list=[''],
          serialization_tag_list=['']
2092 2093 2094
          )
      # Fail
      raise ValueError, 'This method always fail'
2095 2096 2097 2098
    try:
      Organisation.modifySQLAndFail = modifySQLAndFail
      obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
      obj.activate(activity='SQLQueue').modifySQLAndFail()
2099
      transaction.commit()
2100 2101 2102
      self.flushAllActivities(silent=1, loop_size=100)
      self.assertEquals(activity_tool.countMessage(method_id='dummy_activity'), 0)
    finally:
Vincent Pelletier's avatar
Vincent Pelletier committed
2103
      delattr(Organisation, 'modifySQLAndFail')
2104

2105
  def test_85_MessagePathMustBeATuple(self, quiet=0, run=run_all_test):
2106
    """
2107 2108 2109 2110
      Message property 'object_path' must be a tuple, whatever it is generated from.
      Possible path sources are:
       - bare string
       - object
2111 2112 2113
    """
    if not run: return
    if not quiet:
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
      message = '\nCheck that message property \'object_path\' is a tuple, whatever it is generated from.'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    def check(value):
      message = Message(value, None, {}, 'dummy', (), {})
      self.assertTrue(isinstance(message.object_path, tuple))
    # Bare string
    check('/foo/bar')
    # Object
    check(self.getPortalObject().person_module)

  def test_86_ActivityToolInvokeGroupFailureDoesNotCommitCMFActivitySQLConnectionSQLDict(self, quiet=0, run=run_all_test):
    """
      Check that CMFActivity SQL connection is rollback if activity_tool.invokeGroup raises.
    """
    if not run: return
    if not quiet:
      message = '\nCheck that activity modifications via CMFActivity connection are rolled back on ActivityTool error (SQLDict)'
2132 2133
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
2134
    transaction.commit()
2135
    self.tic()
2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156
    activity_tool = self.getActivityTool()
    def modifySQLAndFail(self, *arg, **kw):
      # Only create the dummy activity if none is present: we would just
      # generate missleading errors (duplicate uid).
      if activity_tool.countMessage(method_id='dummy_activity') == 0:
        # Add a dumy activity which will not be executed
        # Modified table does not matter
        method_id = 'dummy_activity'
        path = '/'.join(self.getPhysicalPath())
        message = Message(self, None, {}, method_id, (), {})
        pickled_message = pickle.dumps(message)
        self.SQLDict_writeMessageList(
          uid_list=[0], # This uid is never automaticaly assigned (starts at 1)
          date_list=[DateTime().Date()],
          path_list=[path],
          method_id_list=[method_id],
          message_list=[pickled_message],
          priority_list=[1],
          processing_node_list=[-2],
          group_method_id_list=[''],
          tag_list=[''],
2157 2158
          order_validation_text_list=[''],
          serialization_tag_list=[''],
2159 2160 2161 2162 2163
          )
      # Fail
      raise ValueError, 'This method always fail'
    def dummy(self):
      pass
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
    invoke = activity_tool.__class__.invoke
    invokeGroup = activity_tool.__class__.invokeGroup
    try: 
      activity_tool.__class__.invoke = modifySQLAndFail
      activity_tool.__class__.invokeGroup = modifySQLAndFail
      Organisation.dummy = dummy
      obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
      group_method_id = '%s/dummy' % (obj.getPath(), )
      obj.activate(activity='SQLDict', group_method_id=group_method_id).dummy()
      obj2 = self.getPortal().organisation_module.newContent(portal_type='Organisation')
      obj2.activate(activity='SQLDict', group_method_id=group_method_id).dummy()
2175
      transaction.commit()
2176 2177 2178 2179 2180 2181
      self.flushAllActivities(silent=1, loop_size=100)
      self.assertEquals(activity_tool.countMessage(method_id='dummy_activity'), 0)
    finally:
      delattr(Organisation, 'dummy')
      activity_tool.__class__.invoke = invoke
      activity_tool.__class__.invokeGroup = invokeGroup
2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
  
  def test_87_ActivityToolInvokeFailureDoesNotCommitCMFActivitySQLConnectionSQLQueue(self, quiet=0, run=run_all_test):
    """
      Check that CMFActivity SQL connection is rollback if activity_tool.invoke raises.
    """
    if not run: return
    if not quiet:
      message = '\nCheck that activity modifications via CMFActivity connection are rolled back on ActivityTool error (SQLQueue)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
2192
    transaction.commit()
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
    self.tic()
    activity_tool = self.getActivityTool()
    def modifySQLAndFail(self, *args, **kw):
      # Only create the dummy activity if none is present: we would just
      # generate missleading errors (duplicate uid).
      if activity_tool.countMessage(method_id='dummy_activity') == 0:
        # Add a dumy activity which will not be executed
        # Modified table does not matter
        method_id = 'dummy_activity'
        path = '/'.join(self.getPhysicalPath())
        message = Message(self, None, {}, method_id, (), {})
        pickled_message = pickle.dumps(message)
        self.SQLDict_writeMessageList(
          uid_list=[0], # This uid is never automaticaly assigned (starts at 1)
          date_list=[DateTime().Date()],
          path_list=[path],
          method_id_list=[method_id],
          message_list=[pickled_message],
          priority_list=[1],
          processing_node_list=[-2],
          group_method_id_list=[''],
          tag_list=[''],
2215 2216
          order_validation_text_list=[''],
          serialization_tag_list=[''],
2217 2218 2219 2220 2221
          )
      # Fail
      raise ValueError, 'This method always fail'
    def dummy(self):
      pass
2222 2223 2224 2225 2226 2227 2228 2229
    invoke = activity_tool.__class__.invoke
    invokeGroup = activity_tool.__class__.invokeGroup
    try:
      activity_tool.__class__.invoke = modifySQLAndFail
      activity_tool.__class__.invokeGroup = modifySQLAndFail
      Organisation.dummy = dummy
      obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
      obj.activate(activity='SQLQueue').dummy()
2230
      transaction.commit()
2231 2232 2233 2234 2235 2236
      self.flushAllActivities(silent=1, loop_size=100)
      self.assertEquals(activity_tool.countMessage(method_id='dummy_activity'), 0)
    finally:
      delattr(Organisation, 'dummy')
      activity_tool.__class__.invoke = invoke
      activity_tool.__class__.invokeGroup = invokeGroup
2237

2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251
  def test_88_ProcessingMultipleMessagesMustRevertIndividualMessagesOnError(self, quiet=0, run=run_all_test):
    """
      Check that, on queues which support it, processing a batch of multiple
      messages doesn't cause failed ones to becommited along with succesful
      ones.

      Queues supporting message batch processing:
       - SQLQueue
    """
    if not run: return
    if not quiet:
      message = '\nCheck processing a batch of messages with failures'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
2252
    transaction.commit()
2253 2254 2255 2256 2257 2258 2259 2260
    self.tic()
    activity_tool = self.getActivityTool()
    obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
    active_obj = obj.activate(activity='SQLQueue')
    def appendToTitle(self, to_append, fail=False):
      self.setTitle(self.getTitle() + to_append)
      if fail:
        raise ValueError, 'This method always fail'
2261 2262 2263 2264 2265 2266 2267
    try:
      Organisation.appendToTitle = appendToTitle
      obj.setTitle('a')
      active_obj.appendToTitle('b')
      active_obj.appendToTitle('c', fail=True)
      active_obj.appendToTitle('d')
      object_id = obj.getId()
2268
      transaction.commit()
2269 2270 2271 2272
      self.assertEqual(obj.getTitle(), 'a')
      self.assertEqual(activity_tool.countMessage(method_id='appendToTitle'), 3)
      self.flushAllActivities(silent=1, loop_size=100)
      self.assertEqual(activity_tool.countMessage(method_id='appendToTitle'), 1)
2273
      self.assertEqual(sorted(obj.getTitle()), ['a', 'b', 'd'])
2274 2275
    finally:
      delattr(Organisation, 'appendToTitle')
2276

2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
  def test_89_RequestIsolationInsideSameTic(self, quiet=0, run=run_all_test):
    """
      Check that request information do not leak from one activity to another
      inside the same TIC invocation.
      This only apply to queues supporting batch processing:
        - SQLQueue
    """
    if not run: return
    if not quiet:
      message = '\nCheck request isolation between messages of the same batch'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
2289
    transaction.commit()
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305
    self.tic()
    obj = self.getPortal().organisation_module.newContent(portal_type='Organisation', title='Pending')
    marker_id = 'marker_%i' % (random.randint(1, 10), )
    def putMarkerValue(self, marker_id):
      self.REQUEST.set(marker_id, 1)
    def checkMarkerValue(self, marker_id):
      if self.REQUEST.get(marker_id) is not None:
        self.setTitle('Failed')
      else:
        self.setTitle('Success')
    try:
      Organisation.putMarkerValue = putMarkerValue
      Organisation.checkMarkerValue = checkMarkerValue
      obj.activate(activity='SQLQueue', tag='set_first').putMarkerValue(marker_id=marker_id)
      obj.activate(activity='SQLQueue', after_tag='set_first').checkMarkerValue(marker_id=marker_id)
      self.assertEqual(obj.getTitle(), 'Pending')
2306
      transaction.commit()
2307 2308 2309 2310 2311 2312
      self.tic()
      self.assertEqual(obj.getTitle(), 'Success')
    finally:
      delattr(Organisation, 'putMarkerValue')
      delattr(Organisation, 'checkMarkerValue')

2313
  def TryUserNotificationOnActivityFailure(self, activity):
2314
    transaction.commit()
2315 2316
    self.tic()
    obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
2317
    transaction.commit()
2318 2319 2320 2321 2322
    self.tic()
    # Use a mutable variable to be able to modify the same instance from
    # monkeypatch method.
    notification_done = []
    from Products.CMFActivity.ActivityTool import Message
2323
    def fake_notifyUser(self, *args, **kw):
2324 2325 2326 2327 2328 2329 2330
      notification_done.append(True)
    original_notifyUser = Message.notifyUser
    def failingMethod(self):
      raise ValueError, 'This method always fail'
    Message.notifyUser = fake_notifyUser
    Organisation.failingMethod = failingMethod
    try:
2331 2332
      # MESSAGE_NOT_EXECUTED
      obj.activate(activity=activity).failingMethod()
2333
      transaction.commit()
2334 2335 2336
      self.assertEqual(len(notification_done), 0)
      self.flushAllActivities(silent=1, loop_size=100)
      self.assertEqual(len(notification_done), 1)
2337 2338 2339
      # MESSAGE_NOT_EXECUTABLE
      obj.getParentValue()._delObject(obj.getId())
      obj.activate(activity=activity).getId()
2340
      transaction.commit()
2341 2342 2343
      self.assertEqual(len(notification_done), 1)
      self.flushAllActivities(silent=1, loop_size=100)
      self.assertEqual(len(notification_done), 2)
2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372
    finally:
      Message.notifyUser = original_notifyUser
      delattr(Organisation, 'failingMethod')


  def test_90_userNotificationOnActivityFailureWithSQLDict(self, quiet=0, run=run_all_test):
    """
      Check that a user notification method is called on message when activity
      fails and will not be tried again.
    """
    if not run: return
    if not quiet:
      message = '\nCheck user notification sent on activity final error (SQLDict)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryUserNotificationOnActivityFailure('SQLDict')

  def test_91_userNotificationOnActivityFailureWithSQLQueue(self, quiet=0, run=run_all_test):
    """
      Check that a user notification method is called on message when activity
      fails and will not be tried again.
    """
    if not run: return
    if not quiet:
      message = '\nCheck user notification sent on activity final error (SQLQueue)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryUserNotificationOnActivityFailure('SQLQueue')

2373
  def TryUserNotificationRaise(self, activity):
2374
    transaction.commit()
2375 2376
    self.tic()
    obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
2377
    transaction.commit()
2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
    self.tic()
    from Products.CMFActivity.ActivityTool import Message
    original_notifyUser = Message.notifyUser
    def failingMethod(self, *args, **kw):
      raise ValueError, 'This method always fail'
    Message.notifyUser = failingMethod
    Organisation.failingMethod = failingMethod
    readMessageList = getattr(self.getPortalObject(), '%s_readMessageList'% (activity, ))
    try:
      obj.activate(activity=activity, priority=6).failingMethod()
2388
      transaction.commit()
2389
      self.flushAllActivities(silent=1, loop_size=100)
2390 2391 2392
      with_processing_len = len(readMessageList(path=None,
                                                to_date=None,
                                                method_id='failingMethod',
2393 2394
                                                include_processing=1,
                                                processing_node=None))
2395 2396 2397
      without_processing_len = len(readMessageList(path=None,
                                                   to_date=None,
                                                   method_id='failingMethod',
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436
                                                   include_processing=0,
                                                   processing_node=None))
      self.assertEqual(with_processing_len, 1)
      self.assertEqual(without_processing_len, 1)
    finally:
      Message.notifyUser = original_notifyUser
      delattr(Organisation, 'failingMethod')

  def test_92_userNotificationRaiseWithSQLDict(self, quiet=0, run=run_all_test):
    """
      Check that activities are not left with processing=1 when notifyUser raises.
    """
    if not run: return
    if not quiet:
      message = '\nCheck that activities are not left with processing=1 when notifyUser raises (SQLDict)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryUserNotificationRaise('SQLDict')

  def test_93_userNotificationRaiseWithSQLQueue(self, quiet=0, run=run_all_test):
    """
      Check that activities are not left with processing=1 when notifyUser raises.
    """
    if not run: return
    if not quiet:
      message = '\nCheck that activities are not left with processing=1 when notifyUser raises (SQLQueue)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryUserNotificationRaise('SQLQueue')
    
  def test_94_ActivityToolCommitFailureDoesNotCommitCMFActivitySQLConnectionSQLDict(self, quiet=0, run=run_all_test):
    """
      Check that CMFActivity SQL connection is rollback if transaction commit raises.
    """
    if not run: return
    if not quiet:
      message = '\nCheck that activity modifications via CMFActivity connection are rolled back on commit error (SQLDict)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
2437
    transaction.commit()
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
    self.tic()
    activity_tool = self.getActivityTool()
    def modifySQL(self, object_list, *arg, **kw):
      # Only create the dummy activity if none is present: we would just
      # generate missleading errors (duplicate uid).
      if activity_tool.countMessage(method_id='dummy_activity') == 0:
        # Add a dumy activity which will not be executed
        # Modified table does not matter
        method_id = 'dummy_activity'
        path = '/'.join(self.getPhysicalPath())
        message = Message(self, None, {}, method_id, (), {})
        pickled_message = pickle.dumps(message)
        self.SQLDict_writeMessageList(
          uid_list=[0], # This uid is never automaticaly assigned (starts at 1)
          date_list=[DateTime().Date()],
          path_list=[path],
          method_id_list=[method_id],
          message_list=[pickled_message],
          priority_list=[1],
          processing_node_list=[-2],
          group_method_id_list=[''],
          tag_list=[''],
2460
          order_validation_text_list=[''],
2461
          )
2462
      transaction.get().__class__.commit = fake_commit
2463
      object_list[:] = []
2464
    commit = transaction.get().__class__.commit
2465
    def fake_commit(*args, **kw):
2466
      transaction.get().__class__.commit = commit
2467 2468 2469 2470 2471 2472
      raise KeyError, 'always fail'
    try: 
      Organisation.modifySQL = modifySQL
      obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
      group_method_id = '%s/modifySQL' % (obj.getPath(), )
      obj2 = self.getPortal().organisation_module.newContent(portal_type='Organisation')
2473
      transaction.commit()
2474 2475 2476
      self.tic()
      obj.activate(activity='SQLDict', group_method_id=group_method_id).modifySQL()
      obj2.activate(activity='SQLDict', group_method_id=group_method_id).modifySQL()
2477
      transaction.commit()
2478 2479 2480
      try:
        self.flushAllActivities(silent=1, loop_size=100)
      finally:
2481
        transaction.get().__class__.commit = commit
2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494
      self.assertEquals(activity_tool.countMessage(method_id='dummy_activity'), 0)
    finally:
      delattr(Organisation, 'modifySQL')
  
  def test_95_ActivityToolCommitFailureDoesNotCommitCMFActivitySQLConnectionSQLQueue(self, quiet=0, run=run_all_test):
    """
      Check that CMFActivity SQL connection is rollback if activity_tool.invoke raises.
    """
    if not run: return
    if not quiet:
      message = '\nCheck that activity modifications via CMFActivity connection are rolled back on commit error (SQLQueue)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
2495
    transaction.commit()
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517
    self.tic()
    activity_tool = self.getActivityTool()
    def modifySQL(self, *args, **kw):
      # Only create the dummy activity if none is present: we would just
      # generate missleading errors (duplicate uid).
      if activity_tool.countMessage(method_id='dummy_activity') == 0:
        # Add a dumy activity which will not be executed
        # Modified table does not matter
        method_id = 'dummy_activity'
        path = '/'.join(self.getPhysicalPath())
        message = Message(self, None, {}, method_id, (), {})
        pickled_message = pickle.dumps(message)
        self.SQLDict_writeMessageList(
          uid_list=[0], # This uid is never automaticaly assigned (starts at 1)
          date_list=[DateTime().Date()],
          path_list=[path],
          method_id_list=[method_id],
          message_list=[pickled_message],
          priority_list=[1],
          processing_node_list=[-2],
          group_method_id_list=[''],
          tag_list=[''],
2518
          order_validation_text_list=[''],
2519
         )
2520 2521
      transaction.get().__class__.commit = fake_commit
    commit = transaction.get().__class__.commit
2522
    def fake_commit(self, *args, **kw):
2523
      transaction.get().__class__.commit = commit
2524 2525 2526 2527
      raise KeyError, 'always fail'
    try:
      Organisation.modifySQL = modifySQL
      obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
2528
      transaction.commit()
2529 2530
      self.tic()
      obj.activate(activity='SQLQueue').modifySQL()
2531
      transaction.commit()
2532 2533 2534
      try:
        self.flushAllActivities(silent=1, loop_size=100)
      finally:
2535
        transaction.get().__class__.commit = commit
2536 2537 2538
      self.assertEquals(activity_tool.countMessage(method_id='dummy_activity'), 0)
    finally:
      delattr(Organisation, 'modifySQL')
2539

2540 2541 2542 2543 2544 2545
  def TryActivityRaiseInCommitDoesNotStallActivityConection(self, activity):
    """
      Check that an activity which commit raises (as would a regular conflict
      error be raised in tpc_vote) does not cause activity connection to
      stall.
    """
2546
    transaction.commit()
2547 2548 2549 2550 2551 2552
    self.tic()
    activity_tool = self.getActivityTool()
    from Shared.DC.ZRDB.TM import TM
    try:
      Organisation.registerFailingTransactionManager = registerFailingTransactionManager
      obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
2553
      transaction.commit()
2554 2555 2556
      self.tic()
      now = DateTime()
      obj.activate(activity=activity).registerFailingTransactionManager()
2557
      transaction.commit()
2558
      self.flushAllActivities(silent=1, loop_size=100)
2559
      transaction.commit()
2560 2561 2562 2563 2564 2565 2566
      # Check that cmf_activity SQL connection still works
      connection_da_pool = self.getPortalObject().cmf_activity_sql_connection()
      import thread
      connection_da = connection_da_pool._db_pool[thread.get_ident()]
      self.assertFalse(connection_da._registered)
      connection_da_pool.query('select 1')
      self.assertTrue(connection_da._registered)
2567
      transaction.commit()
2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587
      self.assertFalse(connection_da._registered)
    finally:
      delattr(Organisation, 'registerFailingTransactionManager')

  def test_96_ActivityRaiseInCommitDoesNotStallActivityConectionSQLDict(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck that raising in commit does not stall cmf activity SQL connection (SQLDict)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivityRaiseInCommitDoesNotStallActivityConection('SQLDict')

  def test_97_ActivityRaiseInCommitDoesNotStallActivityConectionSQLQueue(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck that raising in commit does not stall cmf activity SQL connection (SQLQueue)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivityRaiseInCommitDoesNotStallActivityConection('SQLQueue')

2588 2589 2590
  def TryActivityRaiseInCommitDoesNotLooseMessages(self, activity):
    """
    """
2591
    transaction.commit()
2592 2593 2594 2595 2596
    self.tic()
    activity_tool = self.getActivityTool()
    try:
      Organisation.registerFailingTransactionManager = registerFailingTransactionManager
      obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
2597
      transaction.commit()
2598 2599 2600
      self.tic()
      now = DateTime()
      obj.activate(activity=activity).registerFailingTransactionManager()
2601
      transaction.commit()
2602
      self.flushAllActivities(silent=1, loop_size=100)
2603
      transaction.commit()
2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622
      self.assertEquals(activity_tool.countMessage(method_id='registerFailingTransactionManager'), 1)
    finally:
      delattr(Organisation, 'registerFailingTransactionManager')

  def test_98_ActivityRaiseInCommitDoesNotLooseMessagesSQLDict(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck that raising in commit does not loose messages (SQLDict)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivityRaiseInCommitDoesNotLooseMessages('SQLDict')

  def test_99_ActivityRaiseInCommitDoesNotLooseMessagesSQLQueue(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck that raising in commit does not loose messages (SQLQueue)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryActivityRaiseInCommitDoesNotLooseMessages('SQLQueue')
2623

2624
  def TryChangeSkinInActivity(self, activity):
2625
    transaction.commit()
2626 2627 2628 2629 2630 2631 2632
    self.tic()
    activity_tool = self.getActivityTool()
    def changeSkinToNone(self):
      self.getPortalObject().changeSkin(None)
    Organisation.changeSkinToNone = changeSkinToNone
    try:
      organisation = self.getPortal().organisation_module.newContent(portal_type='Organisation')
2633
      transaction.commit()
2634 2635
      self.tic()
      organisation.activate(activity=activity).changeSkinToNone()
2636
      transaction.commit()
2637 2638 2639 2640 2641 2642
      self.assertEquals(len(activity_tool.getMessageList()), 1)
      self.flushAllActivities(silent=1, loop_size=100)
      self.assertEquals(len(activity_tool.getMessageList()), 0)
    finally:
      delattr(Organisation, 'changeSkinToNone')

Vincent Pelletier's avatar
Vincent Pelletier committed
2643
  def test_100_TryChangeSkinInActivitySQLDict(self, quiet=0, run=run_all_test):
2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657
    if not run: return
    if not quiet:
      message = '\nTry Change Skin In Activity (SQLDict)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryChangeSkinInActivity('SQLDict')

  def test_101_TryChangeSkinInActivitySQLQueue(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nTry ChangeSkin In Activity (SQLQueue)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryChangeSkinInActivity('SQLQueue')
2658

2659 2660 2661 2662 2663
  def test_102_1_CheckSQLDictDoesNotDeleteSimilaritiesBeforeExecution(self, quiet=0, run=run_all_test):
    """
      Test that SQLDict does not delete similar messages which have the same
      method_id and path but a different tag before execution.
    """
2664 2665
    if not run: return
    if not quiet:
2666
      message = '\nCheck similarities are not deleted before execution of original message (SQLDict)'
2667 2668 2669
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    organisation = self.getPortal().organisation_module.newContent(portal_type='Organisation')
2670
    transaction.commit()
2671 2672 2673 2674 2675 2676 2677 2678
    self.tic()
    activity_tool = self.getActivityTool()
    check_result_dict = {}
    def checkActivityCount(self, other_tag):
      if len(check_result_dict) == 0:
        check_result_dict['done'] = activity_tool.countMessage(tag=other_tag)
    try:
      Organisation.checkActivityCount = checkActivityCount
2679
      # Adds two similar but not the same activities.
2680 2681
      organisation.activate(activity='SQLDict', tag='a').checkActivityCount(other_tag='b')
      organisation.activate(activity='SQLDict', tag='b').checkActivityCount(other_tag='a')
2682
      transaction.commit()
2683
      self.assertEqual(len(activity_tool.getMessageList()), 2)
2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704
      activity_tool.distribute()
      # after distribute, similarities are still there.
      self.assertEqual(len(activity_tool.getMessageList()), 2)
      self.tic()
      self.assertEqual(len(activity_tool.getMessageList()), 0)
      self.assertEqual(len(check_result_dict), 1)
      self.assertEqual(check_result_dict['done'], 1)
    finally:
      delattr(Organisation, 'checkActivityCount')

  def test_102_2_CheckSQLDictDeleteDuplicatesBeforeExecution(self, quiet=0, run=run_all_test):
    """
      Test that SQLDict delete the same messages before execution if messages
      has the same method_id and path and tag.
    """
    if not run: return
    if not quiet:
      message = '\nCheck duplicates are deleted before execution of original message (SQLDict)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    organisation = self.getPortal().organisation_module.newContent(portal_type='Organisation')
2705
    transaction.commit()
2706 2707 2708 2709 2710 2711 2712 2713 2714
    self.tic()
    activity_tool = self.getActivityTool()
    check_result_dict = {}
    def checkActivityCount(self, other_tag):
      if len(check_result_dict) == 0:
        check_result_dict['done'] = activity_tool.countMessage(tag=other_tag)
    try:
      Organisation.checkActivityCount = checkActivityCount
      # Adds two same activities.
2715
      organisation.activate(activity='SQLDict', tag='a', priority=2).checkActivityCount(other_tag='a')
2716
      transaction.commit()
2717 2718
      uid1, = [x.uid for x in activity_tool.getMessageList()]
      organisation.activate(activity='SQLDict', tag='a', priority=1).checkActivityCount(other_tag='a')
2719
      transaction.commit()
2720 2721 2722
      self.assertEqual(len(activity_tool.getMessageList()), 2)
      activity_tool.distribute()
      # After distribute, duplicate is deleted.
2723 2724
      uid2, = [x.uid for x in activity_tool.getMessageList()]
      self.assertNotEqual(uid1, uid2)
2725 2726 2727 2728 2729 2730 2731
      self.tic()
      self.assertEqual(len(activity_tool.getMessageList()), 0)
      self.assertEqual(len(check_result_dict), 1)
      self.assertEqual(check_result_dict['done'], 1)
    finally:
      delattr(Organisation, 'checkActivityCount')

2732 2733 2734 2735 2736 2737 2738 2739 2740 2741
  def test_102_3_CheckSQLDictDistributeWithSerializationTagAndGroupMethodId(
      self, quiet=0):
    """
      Distribuation was at some point buggy with this scenario when there was
      activate with the same serialization_tag and one time with a group_method
      id and one without group_method_id :
        foo.activate(serialization_tag='a', group_method_id='x').getTitle()
        foo.activate(serialization_tag='a').getId()
    """
    organisation = self.getPortal().organisation_module.newContent(portal_type='Organisation')
2742
    transaction.commit()
2743 2744 2745
    self.tic()
    activity_tool = self.getActivityTool()
    organisation.activate(serialization_tag='a').getId()
2746
    transaction.commit()
2747 2748
    organisation.activate(serialization_tag='a',
              group_method_id='portal_catalog/catalogObjectList').getTitle()
2749
    transaction.commit()
2750 2751 2752 2753 2754 2755 2756
    self.assertEqual(len(activity_tool.getMessageList()), 2)
    activity_tool.distribute()
    # After distribute, there is no deletion because it is different method
    self.assertEqual(len(activity_tool.getMessageList()), 2)
    self.tic()
    self.assertEqual(len(activity_tool.getMessageList()), 0)

2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770
  def test_103_interQueuePriorities(self, quiet=0, run=run_all_test):
    """
      Important note: there is no way to really reliably check that this
      feature is correctly implemented, as activity execution order is
      non-deterministic.
      The best which can be done is to check that under certain circumstances
      the activity exeicution order match expectations.
    """
    if not run: return
    if not quiet:
      message = '\nCheck inter-queue priorities'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    organisation = self.getPortal().organisation_module.newContent(portal_type='Organisation')
2771
    transaction.commit()
2772 2773 2774 2775 2776
    self.tic()
    activity_tool = self.getActivityTool()
    check_result_dict = {}
    def runAndCheck():
      check_result_dict.clear()
2777
      transaction.commit()
2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808
      self.assertEqual(len(check_result_dict), 0)
      self.tic()
      self.assertEqual(len(check_result_dict), 2)
      self.assertTrue(check_result_dict['before_ran'])
      self.assertTrue(check_result_dict['after_ran'])
    def mustRunBefore(self):
      check_result_dict['before_ran'] = 'after_ran' not in check_result_dict
    def mustRunAfter(self):
      check_result_dict['after_ran'] = 'before_ran' in check_result_dict
    Organisation.mustRunBefore = mustRunBefore
    Organisation.mustRunAfter = mustRunAfter
    try:
      # Check that ordering looks good (SQLQueue first)
      organisation.activate(activity='SQLQueue', priority=1).mustRunBefore()
      organisation.activate(activity='SQLDict',  priority=2).mustRunAfter()
      runAndCheck()
      # Check that ordering looks good (SQLDict first)
      organisation.activate(activity='SQLDict',  priority=1).mustRunBefore()
      organisation.activate(activity='SQLQueue', priority=2).mustRunAfter()
      runAndCheck()
      # Check that tag takes precedence over priority (SQLQueue first by priority)
      organisation.activate(activity='SQLQueue', priority=1, after_tag='a').mustRunAfter()
      organisation.activate(activity='SQLDict',  priority=2, tag='a').mustRunBefore()
      runAndCheck()
      # Check that tag takes precedence over priority (SQLDict first by priority)
      organisation.activate(activity='SQLDict',  priority=1, after_tag='a').mustRunAfter()
      organisation.activate(activity='SQLQueue', priority=2, tag='a').mustRunBefore()
      runAndCheck()
    finally:
      delattr(Organisation, 'mustRunBefore')
      delattr(Organisation, 'mustRunAfter')
2809 2810

  def CheckActivityRuntimeEnvironment(self, activity):
2811 2812
    document = self.portal.organisation_module
    activity_result = []
2813
    def extractActivityRuntimeEnvironment(self):
2814 2815
      activity_result.append(self.getActivityRuntimeEnvironment())
    document.__class__.doSomething = extractActivityRuntimeEnvironment
2816
    try:
2817
      document.activate(activity=activity).doSomething()
2818
      transaction.commit()
2819 2820
      # Check that getActivityRuntimeEnvironment raises outside of activities
      self.assertRaises(KeyError, document.getActivityRuntimeEnvironment)
2821
      # Check Runtime isolation
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834
      self.tic()
      # Check that it still raises outside of activities
      self.assertRaises(KeyError, document.getActivityRuntimeEnvironment)
      # Check activity runtime environment instance
      env = activity_result.pop()
      self.assertFalse(activity_result)
      message = env._message
      self.assertEqual(message.line.priority, 1)
      self.assertEqual(message.object_path, document.getPhysicalPath())
      self.assertTrue(message.conflict_retry) # default value
      env.edit(max_retry=0, conflict_retry=False)
      self.assertFalse(message.conflict_retry) # edited value
      self.assertRaises(AttributeError, env.edit, foo='bar')
2835
    finally:
2836
      del document.__class__.doSomething
2837

Vincent Pelletier's avatar
Vincent Pelletier committed
2838
  def test_104_activityRuntimeEnvironmentSQLDict(self, quiet=0, run=run_all_test):
2839 2840 2841 2842 2843 2844 2845
    if not run: return
    if not quiet:
      message = '\nCheck ActivityRuntimeEnvironment (SQLDict)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.CheckActivityRuntimeEnvironment('SQLDict')

Vincent Pelletier's avatar
Vincent Pelletier committed
2846
  def test_105_activityRuntimeEnvironmentSQLQueue(self, quiet=0, run=run_all_test):
2847 2848 2849 2850 2851 2852 2853
    if not run: return
    if not quiet:
      message = '\nCheck ActivityRuntimeEnvironment (SQLQueue)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.CheckActivityRuntimeEnvironment('SQLQueue')

2854 2855
  def CheckSerializationTag(self, activity):
    organisation = self.getPortal().organisation_module.newContent(portal_type='Organisation')
2856
    transaction.commit()
2857 2858 2859 2860 2861 2862 2863
    self.tic()
    activity_tool = self.getActivityTool()
    result = activity_tool.getMessageList()
    self.assertEqual(len(result), 0)
    # First scenario: activate, distribute, activate, distribute
    # Create first activity and distribute: it must be distributed
    organisation.activate(activity=activity, serialization_tag='1').getTitle()
2864
    transaction.commit()
2865 2866 2867 2868 2869 2870 2871
    result = activity_tool.getMessageList()
    self.assertEqual(len(result), 1)
    activity_tool.distribute()
    result = activity_tool.getMessageList()
    self.assertEqual(len([x for x in result if x.processing_node == 0]), 1)
    # Create second activity and distribute: it must *NOT* be distributed
    organisation.activate(activity=activity, serialization_tag='1').getTitle()
2872
    transaction.commit()
2873 2874 2875 2876 2877 2878 2879 2880 2881 2882
    result = activity_tool.getMessageList()
    self.assertEqual(len(result), 2)
    activity_tool.distribute()
    result = activity_tool.getMessageList()
    self.assertEqual(len([x for x in result if x.processing_node == 0]), 1) # Distributed message list len is still 1
    self.tic()
    result = activity_tool.getMessageList()
    self.assertEqual(len(result), 0)
    # Second scenario: activate, activate, distribute
    # Both messages must be distributed (this is different from regular tags)
2883
    organisation.activate(activity=activity, serialization_tag='1', priority=2).getTitle()
2884
    # Use a different method just so that SQLDict doesn't merge both activities prior to insertion.
2885
    organisation.activate(activity=activity, serialization_tag='1', priority=1).getId()
2886
    transaction.commit()
2887 2888 2889 2890
    result = activity_tool.getMessageList()
    self.assertEqual(len(result), 2)
    activity_tool.distribute()
    result = activity_tool.getMessageList()
2891 2892 2893 2894 2895 2896
    # at most 1 activity for a given serialization tag can be validated
    message, = [x for x in result if x.processing_node == 0]
    self.assertEqual(message.method_id, 'getId')
    # the other one is still waiting for validation
    message, = [x for x in result if x.processing_node == -1]
    self.assertEqual(message.method_id, 'getTitle')
2897 2898
    self.tic()
    result = activity_tool.getMessageList()
2899
    self.assertEqual(len(result), 0)
2900 2901 2902 2903 2904
    # Check that giving a None value to serialization_tag does not confuse
    # CMFActivity
    organisation.activate(activity=activity, serialization_tag=None).getTitle()
    self.tic()
    self.assertEqual(len(activity_tool.getMessageList()), 0)
2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921

  def test_106_checkSerializationTagSQLDict(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck serialization tag (SQLDict)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.CheckSerializationTag('SQLDict')

  def test_107_checkSerializationTagSQLQueue(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck serialization tag (SQLQueue)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.CheckSerializationTag('SQLQueue')

2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959
  def test_108_testAbsoluteUrl(self):
    # Tests that absolute_url works in activities. The URL generation is based
    # on REQUEST information when the method was activated.
    request = self.portal.REQUEST

    request.setServerURL('http', 'test.erp5.org', '9080')
    request.other['PARENTS'] = [self.portal.organisation_module]
    request.setVirtualRoot('virtual_root')

    calls = []
    def checkAbsoluteUrl(self):
      calls.append(self.absolute_url())
    Organisation.checkAbsoluteUrl = checkAbsoluteUrl

    try:
      o = self.portal.organisation_module.newContent(
                    portal_type='Organisation', id='test_obj')
      self.assertEquals(o.absolute_url(),
          'http://test.erp5.org:9080/virtual_root/test_obj')
      o.activate().checkAbsoluteUrl()
      
      # Reset server URL and virtual root before executing messages.
      # This simulates the case of activities beeing executed with different
      # REQUEST, such as TimerServer.
      request.setServerURL('https', 'anotherhost.erp5.org', '443')
      request.other['PARENTS'] = [self.app]
      request.setVirtualRoot('')
      # obviously, the object url is different
      self.assertEquals(o.absolute_url(),
          'https://anotherhost.erp5.org/%s/organisation_module/test_obj'
           % self.portal.getId())

      # but activities are executed using the previous request information
      self.flushAllActivities(loop_size=1000)
      self.assertEquals(calls, ['http://test.erp5.org:9080/virtual_root/test_obj'])
    finally:
      delattr(Organisation, 'checkAbsoluteUrl')

2960 2961 2962 2963 2964 2965 2966 2967 2968 2969
  def CheckMissingActivityContextObject(self, activity):
    """
      Check that a message whose context has ben deleted goes to -3
      processing_node.
      This must happen on first message execution, without any delay.
    """
    readMessageList = getattr(self.getPortalObject(), '%s_readMessageList' % (activity, ))
    activity_tool = self.getActivityTool()
    container = self.getPortal().organisation_module
    organisation = container.newContent(portal_type='Organisation')
2970
    transaction.commit()
2971 2972
    self.tic()
    organisation.activate(activity=activity).getTitle()
2973
    transaction.commit()
2974 2975 2976 2977 2978 2979
    self.assertEqual(len(activity_tool.getMessageList()), 1)
    # Here, we delete the subobject using most low-level method, to avoid
    # pending activity to be removed.
    organisation_id = organisation.id
    container._delOb(organisation_id)
    del organisation # Avoid keeping a reference to a deleted object.
2980
    transaction.commit()
2981 2982 2983 2984
    self.assertEqual(getattr(container, organisation_id, None), None)
    self.assertEqual(len(activity_tool.getMessageList()), 1)
    activity_tool.distribute()
    self.assertEquals(len(readMessageList(processing_node=-3,
2985 2986
                            include_processing=1, path=None, method_id=None,
                            to_date=None)), 0)
2987 2988
    activity_tool.tic()
    self.assertEquals(len(readMessageList(processing_node=-3,
2989 2990
                            include_processing=1, path=None, method_id=None,
                            to_date=None)), 1)
2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026

  def test_109_checkMissingActivityContextObjectSQLDict(self, quiet=0,
      run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck missing activity context object (SQLDict)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.CheckMissingActivityContextObject('SQLDict')

  def test_110_checkMissingActivityContextObjectSQLQueue(self, quiet=0,
      run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck missing activity context object (SQLQueue)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.CheckMissingActivityContextObject('SQLQueue')

  def test_111_checkMissingActivityContextObjectSQLDict(self, quiet=0,
      run=run_all_test):
    """
      This is similar to tst 108, but here the object will be missing for an
      activity with a group_method_id.
    """
    if not run: return
    if not quiet:
      message = '\nCheck missing activity context object with ' \
                'group_method_id (SQLDict)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    readMessageList = self.getPortalObject().SQLDict_readMessageList
    activity_tool = self.getActivityTool()
    container = self.getPortalObject().organisation_module
    organisation = container.newContent(portal_type='Organisation')
    organisation_2 = container.newContent(portal_type='Organisation')
3027
    transaction.commit()
3028 3029 3030
    self.tic()
    organisation.reindexObject()
    organisation_2.reindexObject()
3031
    transaction.commit()
3032 3033 3034 3035 3036 3037
    self.assertEqual(len(activity_tool.getMessageList()), 2)
    # Here, we delete the subobject using most low-level method, to avoid
    # pending activity to be removed.
    organisation_id = organisation.id
    container._delOb(organisation_id)
    del organisation # Avoid keeping a reference to a deleted object.
3038
    transaction.commit()
3039 3040 3041 3042
    self.assertEqual(getattr(container, organisation_id, None), None)
    self.assertEqual(len(activity_tool.getMessageList()), 2)
    activity_tool.distribute()
    self.assertEquals(len(readMessageList(processing_node=-3,
3043 3044
                            include_processing=1, path=None, method_id=None,
                            to_date=None)), 0)
3045 3046
    activity_tool.tic()
    self.assertEquals(len(readMessageList(processing_node=-3,
3047 3048
                            include_processing=1, path=None, method_id=None,
                            to_date=None)), 1)
3049 3050
    # The message excuted on "organisation_2" must have succeeded.
    self.assertEqual(len(activity_tool.getMessageList()), 1)
3051

3052 3053 3054 3055 3056
  def CheckLocalizerWorks(self, activity):
    FROM_STRING = 'Foo'
    TO_STRING = 'Bar'
    LANGUAGE = 'xx'
    def translationTest(context):
3057
      from Products.ERP5Type.Message import Message
3058
      context.setTitle(context.Base_translateString(FROM_STRING))
3059
      context.setDescription(str(Message('erp5_ui', FROM_STRING)))
3060 3061 3062 3063 3064 3065 3066 3067 3068
    portal = self.getPortalObject()
    portal.Localizer.erp5_ui.manage_addLanguage(LANGUAGE)
    # Add FROM_STRING to the message catalog
    portal.Localizer.erp5_ui.gettext(FROM_STRING)
    # ...and translate it.
    portal.Localizer.erp5_ui.message_edit(message=FROM_STRING,
      language=LANGUAGE, translation=TO_STRING, note='')
    organisation = portal.organisation_module.newContent(
      portal_type='Organisation')
3069
    transaction.commit()
3070 3071 3072
    self.tic()
    Organisation.translationTest = translationTest
    try:
3073 3074 3075
      REQUEST = organisation.REQUEST
      # Simulate what a browser would have sent to Zope
      REQUEST.environ['HTTP_ACCEPT_LANGUAGE'] = LANGUAGE
3076
      organisation.activate(activity=activity).translationTest()
3077
      transaction.commit()
3078 3079 3080
      # Remove request parameter to check that it was saved at activate call
      # and restored at message execution.
      del REQUEST.environ['HTTP_ACCEPT_LANGUAGE']
3081 3082 3083 3084
      self.tic()
    finally:
      delattr(Organisation, 'translationTest')
    self.assertEqual(TO_STRING, organisation.getTitle())
3085
    self.assertEqual(TO_STRING, organisation.getDescription())
3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102

  def test_112_checkLocalizerWorksSQLQueue(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck Localizer works (SQLQueue)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.CheckLocalizerWorks('SQLQueue')

  def test_113_checkLocalizerWorksSQLDict(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck Localizer works (SQLDict)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.CheckLocalizerWorks('SQLDict')

3103 3104 3105 3106 3107 3108 3109 3110
  def testMessageContainsFailureTraceback(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck message contains failure traceback'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    portal = self.getPortalObject()
    activity_tool = self.getActivityTool()
3111
    def checkMessage(message, exception_type):
3112
      self.assertNotEqual(message.getExecutionState(), 1) # 1 == MESSAGE_EXECUTED
3113
      self.assertEqual(message.exc_type, exception_type)
3114 3115 3116 3117
      self.assertNotEqual(message.traceback, None)
    # With Message.__call__
    # 1: activity context does not exist when activity is executed
    organisation = portal.organisation_module.newContent(portal_type='Organisation')
3118
    transaction.commit()
3119 3120
    self.tic()
    organisation.activate().getTitle() # This generates the mssage we want to test.
3121
    transaction.commit()
3122 3123 3124 3125 3126
    message_list = activity_tool.getMessageList()
    self.assertEqual(len(message_list), 1)
    message = message_list[0]
    portal.organisation_module._delOb(organisation.id)
    message(activity_tool)
3127
    checkMessage(message, KeyError)
3128 3129 3130
    activity_tool.manageCancel(message.object_path, message.method_id)
    # 2: activity method does not exist when activity is executed
    portal.organisation_module.activate().this_method_does_not_exist()
3131
    transaction.commit()
3132 3133 3134 3135
    message_list = activity_tool.getMessageList()
    self.assertEqual(len(message_list), 1)
    message = message_list[0]
    message(activity_tool)
3136
    checkMessage(message, AttributeError)
3137 3138 3139 3140 3141
    activity_tool.manageCancel(message.object_path, message.method_id)

    # With ActivityTool.invokeGroup
    # 1: activity context does not exist when activity is executed
    organisation = portal.organisation_module.newContent(portal_type='Organisation')
3142
    transaction.commit()
3143 3144
    self.tic()
    organisation.activate().getTitle() # This generates the mssage we want to test.
3145
    transaction.commit()
3146 3147 3148 3149 3150
    message_list = activity_tool.getMessageList()
    self.assertEqual(len(message_list), 1)
    message = message_list[0]
    portal.organisation_module._delOb(organisation.id)
    activity_tool.invokeGroup('getTitle', [message])
3151
    checkMessage(message, KeyError)
3152 3153 3154
    activity_tool.manageCancel(message.object_path, message.method_id)
    # 2: activity method does not exist when activity is executed
    portal.organisation_module.activate().this_method_does_not_exist()
3155
    transaction.commit()
3156 3157 3158 3159
    message_list = activity_tool.getMessageList()
    self.assertEqual(len(message_list), 1)
    message = message_list[0]
    activity_tool.invokeGroup('this_method_does_not_exist', [message])
3160
    checkMessage(message, KeyError)
3161 3162 3163 3164 3165
    activity_tool.manageCancel(message.object_path, message.method_id)

    # Unadressed error paths (in both cases):
    #  3: activity commit raises
    #  4: activity raises
3166

3167
  def test_114_checkSQLQueueActivitySucceedsAfterActivityChangingSkin(self,
3168
    quiet=0, run=run_all_test):
3169 3170 3171 3172 3173
    if not run: return
    if not quiet:
      message = '\nCheck SQLQueue activity succeeds after an activity changing skin selection'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
3174 3175 3176 3177 3178 3179 3180 3181 3182 3183
    portal = self.getPortalObject()
    activity_tool = self.getActivityTool()
    # Check that a reference script can be reached
    script_id = 'ERP5Site_reindexAll'
    self.assertTrue(getattr(portal, script_id, None) is not None)
    # Create a new skin selection
    skin_selection_name = 'test_114'
    portal.portal_skins.manage_skinLayers(add_skin=1, skinpath=[''], skinname=skin_selection_name)
    # Create a dummy document
    organisation = portal.organisation_module.newContent(portal_type='Organisation')
3184
    transaction.commit()
3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198
    self.tic()
    # Set custom methods to call as activities.
    def first(context):
      context.changeSkin(skin_selection_name)
      if getattr(context, script_id, None) is not None:
        raise Exception, '%s is not supposed to be found here.' % (script_id, )
    def second(context):
      # If the wrong skin is selected this will raise.
      getattr(context, script_id)
    Organisation.firstTest = first
    Organisation.secondTest = second
    try:
      organisation.activate(tag='foo', activity='SQLQueue').firstTest()
      organisation.activate(after_tag='foo', activity='SQLQueue').secondTest()
3199
      transaction.commit()
3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211
      import gc
      gc.disable()
      self.tic()
      gc.enable()
      # Forcibly restore skin selection, otherwise getMessageList would only
      # emit a log when retrieving the ZSQLMethod.
      portal.changeSkin(None)
      self.assertEquals(len(activity_tool.getMessageList()), 0)
    finally:
      delattr(Organisation, 'firstTest')
      delattr(Organisation, 'secondTest')

3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231
  def test_115_checkProcessShutdown(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck that no activity is executed after process_shutdown has been called'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    # Thread execution plan for this test:
    # main                             ActivityThread           ProcessShutdownThread
    # start ActivityThread             None                     None
    # wait for rendez_vous_lock        (run)                    None
    # wait for rendez_vous_lock        release rendez_vous_lock None
    # start ProcessShutdownThread      wait for activity_lock   None
    # release activity_lock            wait for activity_lock   internal wait
    # wait for activity_thread         (finish)                 internal wait
    # wait for process_shutdown_thread None                     (finish)
    # 
    # This test only checks that:
    # - activity tool can exit between 2 processable activity batches
    # - activity tool won't process activities after process_shutdown was called
    # - process_shutdown returns before Activity.tic()
Vincent Pelletier's avatar
Vincent Pelletier committed
3232
    #   This is not perfect though, since it would require to have access to
3233 3234 3235 3236 3237
    #   the waiting queue of CMFActivity's internal lock (is_running_lock) to
    #   make sure that it's what is preventing process_shutdown from returning.
    portal = self.getPortalObject()
    activity_tool = self.getActivityTool()
    organisation = portal.organisation_module.newContent(portal_type='Organisation')
3238
    transaction.commit()
3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261
    self.tic()
    activity_lock = threading.Lock()
    activity_lock.acquire()
    rendez_vous_lock = threading.Lock()
    rendez_vous_lock.acquire()
    def waitingActivity(context):
      # Inform test that we arrived at rendez-vous.
      rendez_vous_lock.release()
      # When this lock is available, it means test has called process_shutdown.
      activity_lock.acquire()
      activity_lock.release()
    from Products.CMFActivity.Activity.Queue import Queue
    original_queue_tic = Queue.tic
    queue_tic_test_dict = {}
    def Queue_tic(self, activity_tool, processing_node):
      result = original_queue_tic(self, activity_tool, processing_node)
      queue_tic_test_dict['isAlive'] = process_shutdown_thread.isAlive()
      # This is a one-shot method, revert after execution
      Queue.tic = original_queue_tic
      return result
    Queue.tic = Queue_tic
    Organisation.waitingActivity = waitingActivity
    try:
Vincent Pelletier's avatar
Vincent Pelletier committed
3262
      # Use SQLDict with no group method so that both activities won't be
3263 3264
      # executed in the same batch, letting activity tool a chance to check
      # if execution should stop processing activities.
3265 3266
      organisation.activate(activity='SQLDict', tag='foo').waitingActivity()
      organisation.activate(activity='SQLDict', after_tag='foo').getTitle()
3267
      transaction.commit()
3268 3269
      self.assertEqual(len(activity_tool.getMessageList()), 2)
      activity_tool.distribute()
3270
      transaction.commit()
3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328

      # Start a tic in another thread, so they can meet at rendez-vous.
      class ActivityThread(threading.Thread):
        def run(self):
          # Call changeskin, since skin selection depend on thread id, and we
          # are in a new thread.
          activity_tool.changeSkin(None)
          activity_tool.tic()
      activity_thread = ActivityThread()
      # Do not try to outlive main thread.
      activity_thread.setDaemon(True)
      # Call process_shutdown in yet another thread because it will wait for
      # running activity to complete before returning, and we need to unlock
      # activity *after* calling process_shutdown to make sure the next
      # activity won't be executed.
      class ProcessShutdownThread(threading.Thread):
        def run(self):
          activity_tool.process_shutdown(3, 0)
      process_shutdown_thread = ProcessShutdownThread()
      # Do not try to outlive main thread.
      process_shutdown_thread.setDaemon(True)

      activity_thread.start()
      # Wait at rendez-vous for activity to arrive.
      arrived = False
      while (not arrived) and activity_thread.isAlive():
        arrived = rendez_vous_lock.acquire(1)
      if not arrived:
        raise Exception, 'Something wrong happened in activity thread.'
      # Initiate shutdown
      process_shutdown_thread.start()
      try:
        # Let waiting activity finish and wait for thread exit
        activity_lock.release()
        activity_thread.join()
        process_shutdown_thread.join()
        # Check that there is still one activity pending
        message_list = activity_tool.getMessageList()
        self.assertEqual(len(message_list), 1)
        self.assertEqual(message_list[0].method_id, 'getTitle')
        # Check that process_shutdown_thread was still runing when Queue_tic returned.
        self.assertTrue(queue_tic_test_dict.get('isAlive'), repr(queue_tic_test_dict))
        # Call tic in foreground. This must not lead to activity execution.
        activity_tool.tic()
        self.assertEqual(len(activity_tool.getMessageList()), 1)
      finally:
        # Put activity tool back in a working state
        from Products.CMFActivity.ActivityTool import cancelProcessShutdown
        try:
          cancelProcessShutdown()
        except:
          # If something failed in process_shutdown, shutdown lock might not
          # be taken in CMFActivity, leading to a new esception here hiding
          # test error.
          pass
    finally:
      delattr(Organisation, 'waitingActivity')
      Queue.tic = original_queue_tic
3329 3330

  def test_hasActivity(self):
3331 3332
    active_object = self.portal.organisation_module.newContent(
                                            portal_type='Organisation')
3333
    active_process = self.portal.portal_activities.newActiveProcess()
3334
    transaction.commit()
3335
    self.tic()
3336

3337
    self.assertFalse(active_object.hasActivity())
3338 3339 3340 3341 3342
    self.assertFalse(active_process.hasActivity())

    def test(obj, **kw):
      for activity in ('SQLDict', 'SQLQueue'):
        active_object.activate(activity=activity, **kw).getTitle()
3343
        transaction.commit()
3344 3345 3346 3347 3348 3349 3350
        self.assertTrue(obj.hasActivity(), activity)
        self.tic()
        self.assertFalse(obj.hasActivity(), activity)

    test(active_object)
    test(active_process, active_process=active_process)
    test(active_process, active_process=active_process.getPath())
3351

3352 3353 3354 3355 3356 3357 3358 3359
  def test_active_object_hasActivity_does_not_catch_exceptions(self):
    """
    Some time ago, hasActivity was doing a silent try/except, and this was
    a possible disaster for some projects. Here we make sure that if the 
    SQL request fails, then the exception is not ignored
    """
    active_object = self.portal.organisation_module.newContent(
                                            portal_type='Organisation')
3360
    transaction.commit()
3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371
    self.tic()
    self.assertFalse(active_object.hasActivity())

    # Monkey patch to induce any error artificially in the sql connection.
    def query(self, query_string,*args, **kw):
      raise ValueError

    from Products.ZMySQLDA.db import DB
    DB.original_query = DB.query
    try:
      active_object.activate().getTitle()
3372
      transaction.commit()
3373 3374 3375 3376 3377 3378 3379 3380 3381
      self.assertTrue(active_object.hasActivity())
      # Make the sql request not working
      DB.original_query = DB.query
      DB.query = query
      # Make sure then that hasActivity fails
      self.assertRaises(ValueError, active_object.hasActivity)
    finally:
      DB.query = DB.original_query
      del DB.original_query
3382

3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433
  def test_MAX_MESSAGE_LIST_SIZE_SQLQueue(self):
    from Products.CMFActivity.Activity import SQLQueue
    old_MAX_MESSAGE_LIST_SIZE = SQLQueue.MAX_MESSAGE_LIST_SIZE
    SQLQueue.MAX_MESSAGE_LIST_SIZE = 3

    try:
      global call_count
      call_count = 0
      def dummy_counter(self):
        global call_count
        call_count += 1

      Organisation.dummy_counter = dummy_counter
      o = self.portal.organisation_module.newContent(portal_type='Organisation',)

      for i in range(10):
        o.activate(activity='SQLQueue').dummy_counter()
        
      self.flushAllActivities()
      self.assertEquals(call_count, 10)
    finally:
      SQLQueue.MAX_MESSAGE_LIST_SIZE = old_MAX_MESSAGE_LIST_SIZE
      del Organisation.dummy_counter

  def test_MAX_MESSAGE_LIST_SIZE_SQLDict(self):
    from Products.CMFActivity.Activity import SQLDict
    old_MAX_MESSAGE_LIST_SIZE = SQLDict.MAX_MESSAGE_LIST_SIZE
    SQLDict.MAX_MESSAGE_LIST_SIZE = 3

    try:
      global call_count
      call_count = 0
      def dummy_counter(self):
        global call_count
        call_count += 1

      o = self.portal.organisation_module.newContent(portal_type='Organisation',)

      for i in range(10):
        method_name = 'dummy_counter_%s' % i
        setattr(Organisation, method_name, dummy_counter)
        getattr(o.activate(activity='SQLDict'), method_name)()
        
      self.flushAllActivities()
      self.assertEquals(call_count, 10)
    finally:
      SQLDict.MAX_MESSAGE_LIST_SIZE = old_MAX_MESSAGE_LIST_SIZE
      for i in range(10):
        method_name = 'dummy_counter_%s' % i
        delattr(Organisation, method_name)

3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448
  def test_115_TestSerializationTagSQLDictPreventsParallelExecution(self, quiet=0, run=run_all_test):
    """
      Test if there are multiple activities with the same serialization tag,
      then serialization tag guarantees that only one of the same serialization
      tagged activities can be processed at the same time.
    """
    if not run: return
    if not quiet:
      message = '\n'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    from Products.CMFActivity import ActivityTool

    portal = self.getPortal()
    activity_tool = portal.portal_activities
3449
    transaction.commit()
3450 3451 3452 3453
    self.tic()

    # Add 6 activities
    portal.organisation_module.activate(activity='SQLDict', tag='', serialization_tag='test_115').getId()
3454
    transaction.commit()
3455
    portal.organisation_module.activate(activity='SQLDict', serialization_tag='test_115').getTitle()
3456
    transaction.commit()
3457
    portal.organisation_module.activate(activity='SQLDict', tag='tag_1', serialization_tag='test_115').getId()
3458
    transaction.commit()
3459
    portal.person_module.activate(activity='SQLDict', serialization_tag='test_115').getId()
3460
    transaction.commit()
3461
    portal.person_module.activate(activity='SQLDict', tag='tag_2').getId()
3462
    transaction.commit()
3463
    portal.organisation_module.activate(activity='SQLDict', tag='', serialization_tag='test_115').getId()
3464
    transaction.commit()
3465 3466 3467

    # distribute and assign them to 3 nodes
    activity_tool.distribute()
3468
    transaction.commit()
3469 3470 3471

    from Products.CMFActivity import ActivityTool
    ActivityTool.activity_dict['SQLDict'].getProcessableMessageList(activity_tool, 1)
3472
    transaction.commit()
3473
    ActivityTool.activity_dict['SQLDict'].getProcessableMessageList(activity_tool, 2)
3474
    transaction.commit()
3475
    ActivityTool.activity_dict['SQLDict'].getProcessableMessageList(activity_tool, 3)
3476
    transaction.commit()
3477 3478 3479 3480 3481 3482 3483 3484 3485

    result = activity_tool.SQLDict_readMessageList(include_processing=1,
                                                   processing_node=None,
                                                   path=None,
                                                   method_id=None,
                                                   to_date=None,
                                                   offset=0,
                                                   count=1000)

3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507
    try:
      self.assertEqual(len([message
                            for message in result
                            if (message.processing_node>0 and
                                message.processing==1 and
                                message.serialization_tag=='test_115')]),
                       1)

      self.assertEqual(len([message
                            for message in result
                            if (message.processing_node==-1 and
                                message.serialization_tag=='test_115')]),
                       3)

      self.assertEqual(len([message
                            for message in result
                            if (message.processing_node>0 and
                                message.processing==1 and
                                message.serialization_tag=='')]),
                       1)
    finally:
      # Clear activities from all nodes
3508 3509
      activity_tool.SQLBase_delMessage(table=SQLDict.sql_table,
                                       uid=[message.uid for message in result])
3510
      transaction.commit()
3511

3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523
  def test_116_RaiseInCommitBeforeMessageExecution(self):
    """
      Test behaviour of CMFActivity when the commit just before message
      execution fails. In particular, CMFActivity should restart the
      activities it selected (processing=1) instead of ignoring them forever.
    """
    processed = []
    activity_tool = self.portal.portal_activities
    activity_tool.__class__.doSomething = processed.append
    try:
      for activity in 'SQLDict', 'SQLQueue':
        activity_tool.activate(activity=activity).doSomething(activity)
3524
        transaction.commit()
3525 3526 3527 3528 3529
        activity_tool.distribute()
        # Make first commit in dequeueMessage raise
        registerFailingTransactionManager()
        self.assertRaises(CommitFailed, activity_tool.tic)
        # Normally, the request stops here and Zope aborts the transaction
3530
        transaction.abort()
3531 3532 3533 3534 3535 3536 3537 3538
        self.assertEqual(processed, [])
        # Activity is already in 'processing=1' state. Check tic reselects it.
        activity_tool.tic()
        self.assertEqual(processed, [activity])
        del processed[:]
    finally:
      del activity_tool.__class__.doSomething

3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557
  def test_117_PlacelessDefaultReindexParameters(self):
    """
      Test behaviour of PlacelessDefaultReindexParameters.
    """
    portal = self.portal
    
    original_reindex_parameters = portal.getPlacelessDefaultReindexParameters()
    if original_reindex_parameters is None:
      original_reindex_parameters = {}
    
    tag = 'SOME_RANDOM_TAG'      
    activate_kw = original_reindex_parameters.get('activate_kw', {}).copy()
    activate_kw['tag'] = tag 
    portal.setPlacelessDefaultReindexParameters(activate_kw=activate_kw, \
                                                **original_reindex_parameters)
    current_default_reindex_parameters = portal.getPlacelessDefaultReindexParameters()
    self.assertEquals({'activate_kw': {'tag': tag}}, \
                       current_default_reindex_parameters)
    person = portal.person_module.newContent(portal_type='Person')
3558
    transaction.commit()
3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570
    # as we specified it in setPlacelessDefaultReindexParameters we should have
    # an activity for this tags
    self.assertEquals(1, portal.portal_activities.countMessageWithTag(tag))
    self.tic()
    self.assertEquals(0, portal.portal_activities.countMessageWithTag(tag))
    
    # restore originals ones
    portal.setPlacelessDefaultReindexParameters(**original_reindex_parameters)
    person = portal.person_module.newContent(portal_type='Person')
    # .. now now messages with this tag should apper
    self.assertEquals(0, portal.portal_activities.countMessageWithTag(tag))    

3571 3572
  def TryNotificationSavedOnEventLogWhenNotifyUserRaises(self, activity):
    activity_tool = self.getActivityTool()
3573
    transaction.commit()
3574 3575
    self.tic()
    obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
3576
    transaction.commit()
3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592
    self.tic()
    original_notifyUser = Message.notifyUser
    def failSendingEmail(self, *args, **kw):
      raise MailHostError, 'Mail is not sent'
    Message.notifyUser = failSendingEmail
    class ActivityUnitTestError(Exception):
      pass
    activity_unit_test_error = ActivityUnitTestError()
    def failingMethod(self):
      raise activity_unit_test_error
    Organisation.failingMethod = failingMethod
    self._catch_log_errors(ignored_level=sys.maxint) 

    try:
      import traceback
      obj.activate(activity=activity, priority=6).failingMethod()
3593
      transaction.commit()
3594 3595 3596 3597 3598 3599
      self.flushAllActivities(silent=1, loop_size=100)   
      message_list = activity_tool.getMessageList()
      self.assertEqual(len(message_list), 1)
      message = message_list[0]
      logged_errors = []
      logged_errors = self.logged
3600
      transaction.commit()
3601 3602 3603
      for log_record in self.logged:
        if log_record.name == 'ActivityTool' and log_record.levelname == 'WARNING':
          type, value, trace = log_record.exc_info
3604
      transaction.commit()
3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637
      self.assertTrue(activity_unit_test_error is value)
    finally:
      self._ignore_log_errors()
      Message.notifyUser = original_notifyUser
      delattr(Organisation, 'failingMethod')

  def test_118_userNotificationSavedOnEventLogWhenNotifyUserRaisesWithSQLDict(self, quiet=0, run=run_all_test):
    """
      Check the error is saved on event log even if the mail notification is not sent.
    """
    if not run: return
    if not quiet:
      message = '\nCheck the error is saved on event log even if the mail notification is not sent (SQLDict)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryNotificationSavedOnEventLogWhenNotifyUserRaises('SQLDict')

  def test_119_userNotificationSavedOnEventLogWhenNotifyUserRaisesWithSQLQueue(self, quiet=0, run=run_all_test):
    """
      Check the error is saved on event log even if the mail notification is not sent.
    """
    if not run: return
    if not quiet:
      message = '\nCheck the error is saved on event log even if the mail notification is not sent (SQLQueue)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryNotificationSavedOnEventLogWhenNotifyUserRaises('SQLQueue') 

  def TryUserMessageContainingNoTracebackIsStillSent(self, activity):
    portal = self.getPortalObject()
    activity_tool = self.getActivityTool()
    # With Message.__call__
    # 1: activity context does not exist when activity is executed
3638
    transaction.commit()
3639 3640
    self.tic()
    obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
3641
    transaction.commit()
3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653
    self.tic()
    notification_done = []
    def fake_notifyUser(self, *args, **kw):
      notification_done.append(True)
      self.traceback = None
    original_notifyUser = Message.notifyUser
    def failingMethod(self):
      raise ValueError, "This method always fail"
    Message.notifyUser = fake_notifyUser
    Organisation.failingMethod = failingMethod
    try:
      obj.activate(activity=activity).failingMethod()
3654
      transaction.commit()
3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692
      self.flushAllActivities(silent=1, loop_size=100)
      message_list = activity_tool.getMessageList()
      self.assertEqual(len(message_list), 1)
      self.assertEqual(len(notification_done), 1)
      message = message_list[0]
      self.assertEqual(message.traceback, None)
      message(activity_tool)
      activity_tool.manageCancel(message.object_path, message.method_id)
    finally:
      Message.notifyUser = original_notifyUser
      delattr(Organisation, 'failingMethod')    

  def test_120_sendMessageWithNoTracebackWithSQLQueue(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck that message with no traceback is still sent (SQLQueue)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryUserMessageContainingNoTracebackIsStillSent('SQLQueue')

  def test_121_sendMessageWithNoTracebackWithSQLDict(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck that message with no traceback is still sent (SQLDict)'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryUserMessageContainingNoTracebackIsStillSent('SQLDict')
  
  def TryNotificationSavedOnEventLogWhenSiteErrorLoggerRaises(self, activity):
    # Make sure that no active object is installed.
    activity_tool = self.getPortal().portal_activities
    activity_tool.manageClearActivities(keep=0)

    # Need an object.
    organisation_module = self.getOrganisationModule()
    if not organisation_module.hasContent(self.company_id):
      organisation_module.newContent(id=self.company_id)
    o = organisation_module._getOb(self.company_id)
3693
    transaction.commit()
3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716
    self.flushAllActivities(silent = 1, loop_size = 10)
    self.assertEquals(len(activity_tool.getMessageList()), 0)
    class ActivityUnitTestError(Exception):
      pass
    activity_unit_test_error = ActivityUnitTestError()
    def failingMethod(self):
      raise activity_unit_test_error
    from Products.SiteErrorLog.SiteErrorLog import SiteErrorLog
    SiteErrorLog.original_raising = SiteErrorLog.raising

    # Monkey patch Site Error to induce conflict errors artificially.
    def raising(self, info):
      from Products.SiteErrorLog.SiteErrorLog import SiteErrorLog
      raise AttributeError
      return self.original_raising(info)
    from Products.SiteErrorLog.SiteErrorLog import SiteErrorLog
    SiteErrorLog.original_raising = SiteErrorLog.raising
    SiteErrorLog.raising = raising
    Organisation.failingMethod = failingMethod
    self._catch_log_errors(ignored_level=sys.maxint)

    try:
      o.activate(activity = activity).failingMethod()
3717
      transaction.commit()
3718 3719 3720 3721
      self.assertEquals(len(activity_tool.getMessageList()), 1)
      self.flushAllActivities(silent = 1)
      SiteErrorLog.raising = SiteErrorLog.original_raising
      logged_errors = self.logged
3722
      transaction.commit()
3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748
      for log_record in self.logged:
        if log_record.name == 'ActivityTool' and log_record.levelname == 'WARNING':
          type, value, trace = log_record.exc_info     
      self.assertTrue(activity_unit_test_error is value)
    finally:
      self._ignore_log_errors()
      SiteErrorLog.raising = SiteErrorLog.original_raising
      delattr(Organisation, 'failingMethod')
      del SiteErrorLog.original_raising

  def test_122_userNotificationSavedOnEventLogWhenSiteErrorLoggerRaisesWithSQLDict(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck that message not saved in site error logger is not lost'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryNotificationSavedOnEventLogWhenSiteErrorLoggerRaises('SQLDict')

  @expectedFailure
  def test_123_userNotificationSavedOnEventLogWhenSiteErrorLoggerRaisesWithSQLQueue(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      message = '\nCheck that message not saved in site error logger is not lost'
      ZopeTestCase._print(message)
      LOG('Testing... ',0,message)
    self.TryNotificationSavedOnEventLogWhenSiteErrorLoggerRaises('SQLQueue')
3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773

  def test_124_checkConflictErrorAndNoRemainingActivities(self):
    """
    When an activity creates several activities, make sure that all newly
    created activities are not commited if there is ZODB Conflict error
    """
    from Products.CMFActivity.Activity import SQLQueue
    old_MAX_MESSAGE_LIST_SIZE = SQLQueue.MAX_MESSAGE_LIST_SIZE
    SQLQueue.MAX_MESSAGE_LIST_SIZE = 1
    try:
      activity_tool = self.getPortal().portal_activities
      def doSomething(self):
        self.serialize()
        self.activate(activity='SQLQueue').getId()
        self.activate(activity='SQLQueue').getTitle()
        conn = self._p_jar
        tid = self._p_serial
        oid = self._p_oid
        try:
          conn.db().invalidate({oid: tid})
        except TypeError:
          conn.db().invalidate(tid, {oid: tid})
        
      activity_tool.__class__.doSomething = doSomething
      activity_tool.activate(activity='SQLQueue').doSomething()
3774
      transaction.commit()
3775 3776 3777 3778 3779 3780 3781
      activity_tool.distribute()
      activity_tool.tic()
      message_list = activity_tool.getMessageList()
      self.assertEquals(['doSomething'],[x.method_id for x in message_list])
      activity_tool.manageClearActivities(keep=0)
    finally:
      SQLQueue.MAX_MESSAGE_LIST_SIZE = old_MAX_MESSAGE_LIST_SIZE
3782
  
Jérome Perrin's avatar
Jérome Perrin committed
3783 3784 3785 3786
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestCMFActivity))
  return suite
Sebastien Robin's avatar
Sebastien Robin committed
3787