testCMFActivity.py 117 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
##############################################################################
#
# 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.
#
##############################################################################

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

32
from Products.ERP5Type.tests.utils import LogInterceptor
Sebastien Robin's avatar
Sebastien Robin committed
33 34
from Testing import ZopeTestCase
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
35
from Products.ERP5Type.tests.utils import createZODBPythonScript
36
from Products.ERP5Type.Base import Base
37
from Products.CMFActivity.Activity.SQLBase import INVOKE_ERROR_STATE
38
from Products.CMFActivity.Activity.Queue import VALIDATION_ERROR_DELAY
39
from Products.CMFActivity.Activity.SQLDict import SQLDict
40
import Products.CMFActivity.ActivityTool
41
from Products.CMFActivity.Errors import ActivityPendingError, ActivityFlushError
42
from erp5.portal_type import Organisation
43
from AccessControl.SecurityManagement import newSecurityManager
Sebastien Robin's avatar
Sebastien Robin committed
44
from zLOG import LOG
45
from ZODB.POSException import ConflictError
46
from DateTime import DateTime
47
from Products.CMFActivity.ActivityTool import Message
48 49
from _mysql_exceptions import OperationalError
from Products.ZMySQLDA.db import DB
50
from sklearn.externals.joblib.hashing import hash as joblib_hash
51
import gc
52
import random
53
import threading
54
import weakref
55
import transaction
56

Julien Muchembled's avatar
Julien Muchembled committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70
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()

71
class TestCMFActivity(ERP5TypeTestCase, LogInterceptor):
Sebastien Robin's avatar
Sebastien Robin committed
72 73 74 75 76

  # Different variables used for this test
  company_id = 'Nexedi'
  title1 = 'title1'
  title2 = 'title2'
77
  company_id2 = 'Coramy'
78
  company_id3 = 'toto'
Sebastien Robin's avatar
Sebastien Robin committed
79

Sebastien Robin's avatar
Sebastien Robin committed
80 81 82
  def getTitle(self):
    return "CMFActivity"

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

  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)

101
  def afterSetUp(self):
102
    super(TestCMFActivity, self).afterSetUp()
103 104 105 106
    from Products.CMFActivity.ActivityRuntimeEnvironment import BaseMessage
    # Set 'max_retry' to a known value so that we can test the feature
    BaseMessage.max_retry = property(lambda self:
      self.activity_kw.get('max_retry', 5))
Sebastien Robin's avatar
Sebastien Robin committed
107
    self.login()
108
    portal = self.portal
109 110 111 112 113
    # 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)
114

Sebastien Robin's avatar
Sebastien Robin committed
115 116 117 118 119 120 121 122
    # 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)
123
    self.tic()
Sebastien Robin's avatar
Sebastien Robin committed
124

125
  def login(self):
Sebastien Robin's avatar
Sebastien Robin committed
126 127
    uf = self.getPortal().acl_users
    uf._doAddUser('seb', '', ['Manager'], [])
128
    uf._doAddUser('ERP5TypeTestCase', '', ['Manager'], [])
Sebastien Robin's avatar
Sebastien Robin committed
129 130 131
    user = uf.getUserById('seb').__of__(uf)
    newSecurityManager(None, user)

132
  def InvokeAndCancelActivity(self, activity):
133 134 135
    """
    Simple test where we invoke and cancel an activity
    """
Sebastien Robin's avatar
Sebastien Robin committed
136 137
    portal = self.getPortal()
    organisation =  portal.organisation._getOb(self.company_id)
138
    organisation._setTitle(self.title1)
139
    self.assertEqual(self.title1,organisation.getTitle())
140
    organisation.activate(activity=activity)._setTitle(self.title2)
Sebastien Robin's avatar
Sebastien Robin committed
141
    # Needed so that the message are commited into the queue
142
    self.commit()
143
    message_list = portal.portal_activities.getMessageList()
144
    self.assertEqual(len(message_list),1)
145
    portal.portal_activities.manageCancel(organisation.getPhysicalPath(),'_setTitle')
146
    # Needed so that the message are removed from the queue
147
    self.commit()
148
    self.assertEqual(self.title1,organisation.getTitle())
149
    message_list = portal.portal_activities.getMessageList()
150
    self.assertEqual(len(message_list),0)
151
    organisation.activate(activity=activity)._setTitle(self.title2)
152
    # Needed so that the message are commited into the queue
153
    self.commit()
154
    message_list = portal.portal_activities.getMessageList()
155
    self.assertEqual(len(message_list),1)
156
    portal.portal_activities.manageInvoke(organisation.getPhysicalPath(),'_setTitle')
157
    # Needed so that the message are removed from the queue
158
    self.commit()
159
    self.assertEqual(self.title2,organisation.getTitle())
Sebastien Robin's avatar
Sebastien Robin committed
160
    message_list = portal.portal_activities.getMessageList()
161
    self.assertEqual(len(message_list),0)
Sebastien Robin's avatar
Sebastien Robin committed
162

Yoshinori Okuji's avatar
Yoshinori Okuji committed
163
  def DeferredSetTitleActivity(self, activity):
164 165 166 167
    """
    We check that the title is changed only after that
    the activity was called
    """
Sebastien Robin's avatar
Sebastien Robin committed
168
    portal = self.getPortal()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
169
    organisation = portal.organisation._getOb(self.company_id)
170
    organisation._setTitle(self.title1)
171
    self.assertEqual(self.title1,organisation.getTitle())
172
    organisation.activate(activity=activity)._setTitle(self.title2)
Sebastien Robin's avatar
Sebastien Robin committed
173
    # Needed so that the message are commited into the queue
174
    self.commit()
175
    self.assertEqual(self.title1,organisation.getTitle())
Sebastien Robin's avatar
Sebastien Robin committed
176
    portal.portal_activities.tic()
177
    self.assertEqual(self.title2,organisation.getTitle())
Sebastien Robin's avatar
Sebastien Robin committed
178
    message_list = portal.portal_activities.getMessageList()
179
    self.assertEqual(len(message_list),0)
Sebastien Robin's avatar
Sebastien Robin committed
180

181
  def CallOnceWithActivity(self, activity):
182 183 184 185
    """
    With this test we can check if methods are called
    only once (sometimes it was twice !!!)
    """
Sebastien Robin's avatar
Sebastien Robin committed
186
    portal = self.getPortal()
187 188 189 190 191 192 193
    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
194
    organisation =  portal.organisation._getOb(self.company_id)
195 196 197
    Organisation.setFoobar = setFoobar
    Organisation.getFoobar = getFoobar
    organisation.foobar = 0
198
    organisation._setTitle(self.title1)
199
    self.assertEqual(0,organisation.getFoobar())
200
    organisation.activate(activity=activity).setFoobar()
Sebastien Robin's avatar
Sebastien Robin committed
201
    # Needed so that the message are commited into the queue
202
    self.commit()
203
    message_list = portal.portal_activities.getMessageList()
204
    self.assertEqual(len(message_list),1)
Sebastien Robin's avatar
Sebastien Robin committed
205
    portal.portal_activities.tic()
206
    self.assertEqual(1,organisation.getFoobar())
Sebastien Robin's avatar
Sebastien Robin committed
207
    message_list = portal.portal_activities.getMessageList()
208
    self.assertEqual(len(message_list),0)
209 210
    organisation.activate(activity=activity).setFoobar()
    # Needed so that the message are commited into the queue
211
    self.commit()
212
    message_list = portal.portal_activities.getMessageList()
213
    self.assertEqual(len(message_list),1)
214 215
    portal.portal_activities.manageInvoke(organisation.getPhysicalPath(),'setFoobar')
    # Needed so that the message are commited into the queue
216
    self.commit()
217
    message_list = portal.portal_activities.getMessageList()
218 219
    self.assertEqual(len(message_list),0)
    self.assertEqual(2,organisation.getFoobar())
220

221
  def TryFlushActivity(self, activity):
222 223 224
    """
    Check the method flush
    """
225 226
    portal = self.getPortal()
    organisation =  portal.organisation._getOb(self.company_id)
227 228
    organisation._setTitle(self.title1)
    organisation.activate(activity=activity)._setTitle(self.title2)
229
    organisation.flushActivity(invoke=1)
230
    self.assertEqual(organisation.getTitle(),self.title2)
231
    self.commit()
232
    message_list = portal.portal_activities.getMessageList()
233 234
    self.assertEqual(len(message_list),0)
    self.assertEqual(organisation.getTitle(),self.title2)
235
    # Try again with different commit order
236 237
    organisation._setTitle(self.title1)
    organisation.activate(activity=activity)._setTitle(self.title2)
238
    self.commit()
239
    organisation.flushActivity(invoke=1)
240 241
    self.assertEqual(len(message_list),0)
    self.assertEqual(organisation.getTitle(),self.title2)
242
    self.commit()
243

244
  def TryActivateInsideFlush(self, activity):
245 246 247
    """
    Create a new activity inside a flush action
    """
248 249
    portal = self.getPortal()
    def DeferredSetTitle(self,value):
250
      self.activate(activity=activity)._setTitle(value)
251 252
    Organisation.DeferredSetTitle = DeferredSetTitle
    organisation =  portal.organisation._getOb(self.company_id)
253
    organisation._setTitle(self.title1)
254 255
    organisation.activate(activity=activity).DeferredSetTitle(self.title2)
    organisation.flushActivity(invoke=1)
256
    self.commit()
257
    portal.portal_activities.tic()
258
    self.commit()
259
    message_list = portal.portal_activities.getMessageList()
260 261
    self.assertEqual(len(message_list),0)
    self.assertEqual(organisation.getTitle(),self.title2)
262 263

  def TryTwoMethods(self, activity):
264 265 266
    """
    Try several activities
    """
267 268
    portal = self.getPortal()
    def DeferredSetDescription(self,value):
269
      self._setDescription(value)
270
    def DeferredSetTitle(self,value):
271
      self._setTitle(value)
272 273 274
    Organisation.DeferredSetTitle = DeferredSetTitle
    Organisation.DeferredSetDescription = DeferredSetDescription
    organisation =  portal.organisation._getOb(self.company_id)
275
    organisation._setTitle(None)
276 277 278
    organisation.setDescription(None)
    organisation.activate(activity=activity).DeferredSetTitle(self.title1)
    organisation.activate(activity=activity).DeferredSetDescription(self.title1)
279
    self.commit()
280 281
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
282
    self.commit()
283
    message_list = portal.portal_activities.getMessageList()
284 285 286
    self.assertEqual(len(message_list),0)
    self.assertEqual(organisation.getTitle(),self.title1)
    self.assertEqual(organisation.getDescription(),self.title1)
287 288

  def TryTwoMethodsAndFlushThem(self, activity):
289 290 291
    """
    make sure flush works with several activities
    """
292 293
    portal = self.getPortal()
    def DeferredSetTitle(self,value):
294
      self.activate(activity=activity)._setTitle(value)
295
    def DeferredSetDescription(self,value):
296
      self.activate(activity=activity)._setDescription(value)
297 298 299
    Organisation.DeferredSetTitle = DeferredSetTitle
    Organisation.DeferredSetDescription = DeferredSetDescription
    organisation =  portal.organisation._getOb(self.company_id)
300
    organisation._setTitle(None)
301 302 303 304
    organisation.setDescription(None)
    organisation.activate(activity=activity).DeferredSetTitle(self.title1)
    organisation.activate(activity=activity).DeferredSetDescription(self.title1)
    organisation.flushActivity(invoke=1)
305
    self.commit()
306 307
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
308
    self.commit()
309
    message_list = portal.portal_activities.getMessageList()
310 311 312
    self.assertEqual(len(message_list),0)
    self.assertEqual(organisation.getTitle(),self.title1)
    self.assertEqual(organisation.getDescription(),self.title1)
313

314
  def TryActivateFlushActivateTic(self, activity,second=None,commit_sub=0):
315 316 317
    """
    try to commit sub transactions
    """
318 319 320
    portal = self.getPortal()
    def DeferredSetTitle(self,value,commit_sub=0):
      if commit_sub:
321
        transaction.savepoint(optimistic=True)
322
      self.activate(activity=second or activity,priority=4)._setTitle(value)
323 324
    def DeferredSetDescription(self,value,commit_sub=0):
      if commit_sub:
325
        transaction.savepoint(optimistic=True)
326
      self.activate(activity=second or activity,priority=4)._setDescription(value)
327 328 329
    Organisation.DeferredSetTitle = DeferredSetTitle
    Organisation.DeferredSetDescription = DeferredSetDescription
    organisation =  portal.organisation._getOb(self.company_id)
330
    organisation._setTitle(None)
331 332 333 334
    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)
335
    self.commit()
336 337
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
338
    self.commit()
339
    message_list = portal.portal_activities.getMessageList()
340 341 342
    self.assertEqual(len(message_list),0)
    self.assertEqual(organisation.getTitle(),self.title1)
    self.assertEqual(organisation.getDescription(),self.title1)
343

344
  def TryMessageWithErrorOnActivity(self, activity):
345 346 347
    """
    Make sure that message with errors are not deleted
    """
348 349
    portal = self.getPortal()
    def crashThisActivity(self):
350
      self.IWillCrash()
351 352 353 354
    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
355
    self.commit()
356
    message_list = portal.portal_activities.getMessageList()
357
    LOG('Before MessageWithErrorOnActivityFails, message_list',0,[x.__dict__ for x in message_list])
358
    self.assertEqual(len(message_list),1)
359
    portal.portal_activities.tic()
360
    # XXX HERE WE SHOULD USE TIME SHIFT IN ORDER TO SIMULATE MULTIPLE TICS
361 362
    # Test if there is still the message after it crashed
    message_list = portal.portal_activities.getMessageList()
363
    self.assertEqual(len(message_list),1)
364 365
    portal.portal_activities.manageCancel(organisation.getPhysicalPath(),'crashThisActivity')
    # Needed so that the message are commited into the queue
366
    self.commit()
367
    message_list = portal.portal_activities.getMessageList()
368
    self.assertEqual(len(message_list),0)
369

Yoshinori Okuji's avatar
Yoshinori Okuji committed
370
  def DeferredSetTitleWithRenamedObject(self, activity):
371
    """
372 373
    make sure that it is impossible to rename an object
    if some activities are still waiting for this object
374 375 376
    """
    portal = self.getPortal()
    organisation =  portal.organisation._getOb(self.company_id)
377
    organisation._setTitle(self.title1)
378
    self.assertEqual(self.title1,organisation.getTitle())
379
    organisation.activate(activity=activity)._setTitle(self.title2)
380
    # Needed so that the message are commited into the queue
381
    self.commit()
382
    self.assertEqual(self.title1,organisation.getTitle())
383
    self.assertRaises(ActivityPendingError,organisation.edit,id=self.company_id2)
384 385 386 387 388 389 390 391
    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)
392
    organisation._setTitle(self.title1)
393
    active_process = portal.portal_activities.newActiveProcess()
394
    self.assertEqual(self.title1,organisation.getTitle())
395 396
    organisation.activate(activity=activity,active_process=active_process).getTitle()
    # Needed so that the message are commited into the queue
397
    self.commit()
398 399
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
400
    self.assertEqual(self.title1,organisation.getTitle())
401
    result = active_process.getResultList()[0]
402 403
    self.assertEqual(result.method_id , 'getTitle')
    self.assertEqual(result.result , self.title1)
404
    message_list = portal.portal_activities.getMessageList()
405
    self.assertEqual(len(message_list),0)
406

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
  def TryActiveProcessWithResultDict(self, activity):
    """
    Try to store the result inside an active process using result list
    """
    portal = self.getPortal()
    organisation =  portal.organisation._getOb(self.company_id)
    organisation._setTitle(self.title1)
    active_process = portal.portal_activities.newActiveProcess()
    self.assertEqual(self.title1,organisation.getTitle())

    # Post SQLjoblib tasks with explicit signature 
    organisation.activate(activity=activity,active_process=active_process, signature=1).getTitle()
    organisation.activate(activity=activity,active_process=active_process, signature=2).getTitle()
    organisation.activate(activity=activity,active_process=active_process, signature=3).getTitle()
    
    self.commit()
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
    result_dict = active_process.getResultDict()
    result = result_dict[1]
    self.assertEqual(result_dict[1].method_id, 'getTitle')
    self.assertEqual(result.result , self.title1)
    result = result_dict[2]
    self.assertEqual(result_dict[2].method_id, 'getTitle')
    self.assertEqual(result.result , self.title1)
    result = result_dict[3]
    self.assertEqual(result_dict[3].method_id, 'getTitle')
    self.assertEqual(result.result , self.title1)
    message_list = portal.portal_activities.getMessageList()
    self.assertEqual(len(message_list),0)


439 440
  def TryMethodAfterMethod(self, activity):
    """
441
      Ensure the order of an execution by a method id
442 443
    """
    portal = self.getPortal()
444 445 446 447
    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)
448

449
    o.setTitle('a')
450
    self.assertEqual(o.getTitle(), 'a')
451
    self.tic()
452

453 454 455
    def toto(self, value):
      self.setTitle(self.getTitle() + value)
    o.__class__.toto = toto
456

457 458 459
    def titi(self, value):
      self.setTitle(self.getTitle() + value)
    o.__class__.titi = titi
460

461 462 463
    o.activate(after_method_id = 'titi', activity = activity).toto('b')
    o.activate(activity = activity).titi('c')
    self.tic()
464
    self.assertEqual(o.getTitle(), 'acb')
465

466 467 468 469 470 471 472 473 474
  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)
475

476
    o.setTitle('?')
477
    self.assertEqual(o.getTitle(), '?')
478
    self.tic()
479

480 481 482
    o.activate(after_tag = 'toto', activity = activity).setTitle('b')
    o.activate(tag = 'toto', activity = activity).setTitle('a')
    self.tic()
483
    self.assertEqual(o.getTitle(), 'b')
484

485
    o.setDefaultActivateParameterDict({'tag': 'toto'})
486 487 488 489 490 491
    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')
    self.tic()
492
    self.assertEqual(o.getCorporateName(), 'cd')
493

494 495 496 497 498 499 500 501 502 503 504 505
  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('?')
506 507
    self.assertEqual(o.getTitle(), '?')
    self.assertEqual(o.getDescription(), '?')
508 509 510 511
    self.tic()

    o.activate(after_tag = 'toto', activity = activity).setDescription('b')
    o.activate(tag = 'toto', activity = activity).setTitle('a')
512
    self.commit()
513 514 515
    tool = self.getActivityTool()
    self.assertRaises(ActivityFlushError,tool.manageInvoke,o.getPath(),'setDescription')
    tool.manageInvoke(o.getPath(),'setTitle')
516
    self.commit()
517 518
    self.assertEqual(o.getTitle(), 'a')
    self.assertEqual(o.getDescription(), '?')
519
    self.tic()
520 521 522
    self.assertEqual(len(tool.getMessageList()),0)
    self.assertEqual(o.getTitle(), 'a')
    self.assertEqual(o.getDescription(), 'b')
523

Yoshinori Okuji's avatar
Yoshinori Okuji committed
524 525 526 527 528 529 530 531 532
  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)
533

Yoshinori Okuji's avatar
Yoshinori Okuji committed
534
    o.setTitle('?')
535
    self.assertEqual(o.getTitle(), '?')
Yoshinori Okuji's avatar
Yoshinori Okuji committed
536
    self.tic()
537

Yoshinori Okuji's avatar
Yoshinori Okuji committed
538 539 540
    def toto(self, s):
      self.setTitle(self.getTitle() + s)
    o.__class__.toto = toto
541

Yoshinori Okuji's avatar
Yoshinori Okuji committed
542
    o.activate(tag = 'toto', activity = activity).toto('a')
543
    self.commit()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
544
    o.activate(after_tag = 'titi', activity = activity).toto('b')
545
    self.commit()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
546 547
    o.activate(tag = 'titi', after_tag = 'toto', activity = activity).setTitle('c')
    self.tic()
548
    self.assertEqual(o.getTitle(), 'cb')
549

550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
  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('')
    self.tic()

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

    o.activate(tag='A', activity=activity).toto('a')
569
    self.commit()
570
    o.activate(tag='B', activity=activity).toto('b')
571
    self.commit()
572 573
    o.activate(after_tag=('A', 'B'), activity=activity).setTitle('last')
    self.tic()
574
    self.assertEqual(o.getTitle(), 'last')
575

576 577 578 579 580 581 582 583 584 585 586 587 588 589
  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('?')
    self.tic()

    o.activate(tag = 'toto', activity = activity).setTitle('a')
590
    self.commit()
591 592
    self.assertEqual(o.getTitle(), '?')
    self.assertEqual(portal_activities.countMessageWithTag('toto'), 1)
593
    self.tic()
594 595
    self.assertEqual(o.getTitle(), 'a')
    self.assertEqual(portal_activities.countMessageWithTag('toto'), 0)
596

597 598 599 600 601
  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
602
    activity_tool.manageClearActivities()
603 604 605 606 607 608

    # 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)
609
    self.commit()
610
    self.flushAllActivities(silent = 1, loop_size = 10)
611
    self.assertEqual(len(activity_tool.getMessageList()), 0)
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630

    # 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()
631
        self.commit()
632
        self.flushAllActivities(silent = 1, loop_size = i + 10)
633
        self.assertEqual(len(activity_tool.getMessageList()), 0)
634 635 636 637 638 639
    finally:
      Queue.validate = Queue.original_validate
      del Queue.original_validate
      del Queue.current_num_conflict_errors
      del Queue.conflict_errors_limit

640 641 642 643 644
  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
645
    activity_tool.manageClearActivities()
646 647 648 649 650 651

    # 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)
652
    self.commit()
653
    self.flushAllActivities(silent=1, loop_size=10)
654
    self.assertEqual(len(activity_tool.getMessageList()), 0)
655 656 657

    # Monkey patch Queue to induce conflict errors artificially.
    def query(self, query_string,*args, **kw):
658 659
      # Not so nice, this is specific to zsql method
      if "REPLACE INTO" in query_string:
660
        raise OperationalError
661
      return self.original_query(query_string,*args, **kw)
662 663
    portal = self.getPortal()

664
    # Test some range of conflict error occurences.
665
    organisation_module.reindexObject()
666 667
    self.commit()
    self.assertTrue(len(activity_tool.getMessageList()), 1)
668 669 670 671 672
    try:
      DB.original_query = DB.query
      DB.query = query
      portal.portal_activities.distribute()
      portal.portal_activities.tic()
673
      self.commit()
674 675 676
    finally:
      DB.query = DB.original_query
      del DB.original_query
677
    self.assertEqual(len(portal.portal_activities.getMessageList()), 1)
678

679 680 681 682 683 684
  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)
685
    activity_tool.manageClearActivities()
686
    self.commit()
687
    # First case: creating the same activity twice must only register one.
688
    self.assertEqual(len(activity_tool.getMessageList()), 0) # Sanity check
689 690
    object_a.activate(activity=activity).getId()
    object_a.activate(activity=activity).getId()
691
    self.commit()
692
    self.assertEqual(len(activity_tool.getMessageList()), 1)
693
    activity_tool.manageClearActivities()
694
    self.commit()
695 696
    # Second case: creating activity with same tag must only register one.
    # This behaviour is actually the same as the no-tag behaviour.
697
    self.assertEqual(len(activity_tool.getMessageList()), 0) # Sanity check
698 699
    object_a.activate(activity=activity, tag='foo').getId()
    object_a.activate(activity=activity, tag='foo').getId()
700
    self.commit()
701
    self.assertEqual(len(activity_tool.getMessageList()), 1)
702
    activity_tool.manageClearActivities()
703
    self.commit()
704
    # Third case: creating activities with different tags must register both.
705
    self.assertEqual(len(activity_tool.getMessageList()), 0) # Sanity check
706 707
    object_a.activate(activity=activity, tag='foo').getId()
    object_a.activate(activity=activity, tag='bar').getId()
708
    self.commit()
709
    self.assertEqual(len(activity_tool.getMessageList()), 2)
710
    activity_tool.manageClearActivities()
711
    self.commit()
712 713
    # Fourth case: creating activities on different objects must register
    # both.
714
    self.assertEqual(len(activity_tool.getMessageList()), 0) # Sanity check
715 716
    object_a.activate(activity=activity).getId()
    object_b.activate(activity=activity).getId()
717
    self.commit()
718
    self.assertEqual(len(activity_tool.getMessageList()), 2)
719
    activity_tool.manageClearActivities()
720
    self.commit()
721 722
    # Fifth case: creating activities with different method must register
    # both.
723
    self.assertEqual(len(activity_tool.getMessageList()), 0) # Sanity check
724 725
    object_a.activate(activity=activity).getId()
    object_a.activate(activity=activity).getTitle()
726
    self.commit()
727
    self.assertEqual(len(activity_tool.getMessageList()), 2)
728
    activity_tool.manageClearActivities()
729
    self.commit()
730

731
  def test_01_DeferredSetTitleSQLDict(self):
732
    # Test if we can add a complete sales order
Yoshinori Okuji's avatar
Yoshinori Okuji committed
733
    self.DeferredSetTitleActivity('SQLDict')
734

735
  def test_02_DeferredSetTitleSQLQueue(self):
736
    # Test if we can add a complete sales order
Yoshinori Okuji's avatar
Yoshinori Okuji committed
737
    self.DeferredSetTitleActivity('SQLQueue')
738

739 740 741 742
  def test_03_DeferredSetTitleSQLJoblib(self):
    # Test if we can add a complete sales order
    self.DeferredSetTitleActivity('SQLJoblib')

743
  def test_05_InvokeAndCancelSQLDict(self):
Sebastien Robin's avatar
Sebastien Robin committed
744
    # Test if we can add a complete sales order
745
    self.InvokeAndCancelActivity('SQLDict')
Sebastien Robin's avatar
Sebastien Robin committed
746

747
  def test_06_InvokeAndCancelSQLQueue(self):
Sebastien Robin's avatar
Sebastien Robin committed
748
    # Test if we can add a complete sales order
749
    self.InvokeAndCancelActivity('SQLQueue')
Sebastien Robin's avatar
Sebastien Robin committed
750

751 752 753
  def test_07_InvokeAndCancelSQLJoblib(self):
    self.InvokeAndCancelActivity('SQLJoblib')

754
  def test_09_CallOnceWithSQLDict(self):
755 756 757
    # Test if we call methods only once
    self.CallOnceWithActivity('SQLDict')

758
  def test_10_CallOnceWithSQLQueue(self):
Sebastien Robin's avatar
Sebastien Robin committed
759
    # Test if we call methods only once
760 761
    self.CallOnceWithActivity('SQLQueue')

762 763 764
  def test_11_CallOnceWithSQLJoblib(self):
    self.CallOnceWithActivity('SQLJoblib')

765
  def test_13_TryMessageWithErrorOnSQLDict(self):
766 767 768
    # Test if we call methods only once
    self.TryMessageWithErrorOnActivity('SQLDict')

769
  def test_14_TryMessageWithErrorOnSQLQueue(self):
770 771
    # Test if we call methods only once
    self.TryMessageWithErrorOnActivity('SQLQueue')
772 773 774
  
  def test_15_TryMessageWithErrorOnSQLJoblib(self):
    self.TryMessageWithErrorOnActivity('SQLJoblib')
775

776
  def test_17_TryFlushActivityWithSQLDict(self):
777 778 779
    # Test if we call methods only once
    self.TryFlushActivity('SQLDict')

780
  def test_18_TryFlushActivityWithSQLQueue(self):
781 782 783
    # Test if we call methods only once
    self.TryFlushActivity('SQLQueue')

784 785 786 787
  def test_19_TryFlushActivityWithSQLJoblib(self):
    # Test if we call methods only once
    self.TryFlushActivity('SQLJoblib')

788
  def test_21_TryActivateInsideFlushWithSQLDict(self):
789 790 791
    # Test if we call methods only once
    self.TryActivateInsideFlush('SQLDict')

792
  def test_22_TryActivateInsideFlushWithSQLQueue(self):
793 794 795
    # Test if we call methods only once
    self.TryActivateInsideFlush('SQLQueue')

796 797 798 799
  def test_23_TryActivateInsideFlushWithSQLQueue(self):
    # Test if we call methods only once
    self.TryActivateInsideFlush('SQLJoblib')

800
  def test_25_TryTwoMethodsWithSQLDict(self):
801 802 803
    # Test if we call methods only once
    self.TryTwoMethods('SQLDict')

804
  def test_26_TryTwoMethodsWithSQLQueue(self):
805 806 807
    # Test if we call methods only once
    self.TryTwoMethods('SQLQueue')

808 809 810 811
  def test_27_TryTwoMethodsWithSQLJoblib(self):
    # Test if we call methods only once
    self.TryTwoMethods('SQLJoblib')

812
  def test_29_TryTwoMethodsAndFlushThemWithSQLDict(self):
813 814 815
    # Test if we call methods only once
    self.TryTwoMethodsAndFlushThem('SQLDict')

816
  def test_30_TryTwoMethodsAndFlushThemWithSQLQueue(self):
817 818 819
    # Test if we call methods only once
    self.TryTwoMethodsAndFlushThem('SQLQueue')

820 821 822 823
  def test_31_TryTwoMethodsAndFlushThemWithSQLJoblib(self):
    # Test if we call methods only once
    self.TryTwoMethodsAndFlushThem('SQLJoblib')

824
  def test_33_TryActivateFlushActivateTicWithSQLDict(self):
825 826 827
    # Test if we call methods only once
    self.TryActivateFlushActivateTic('SQLDict')

828
  def test_34_TryActivateFlushActivateTicWithSQLQueue(self):
829 830 831
    # Test if we call methods only once
    self.TryActivateFlushActivateTic('SQLQueue')

832
  def test_37_TryActivateFlushActivateTicWithMultipleActivities(self):
833 834 835 836
    # Test if we call methods only once
    self.TryActivateFlushActivateTic('SQLQueue',second='SQLDict')
    self.TryActivateFlushActivateTic('SQLDict',second='SQLQueue')

837
  def test_38_TryCommitSubTransactionWithSQLDict(self):
838 839 840
    # Test if we call methods only once
    self.TryActivateFlushActivateTic('SQLDict',commit_sub=1)

841
  def test_39_TryCommitSubTransactionWithSQLQueue(self):
842 843 844
    # Test if we call methods only once
    self.TryActivateFlushActivateTic('SQLQueue',commit_sub=1)

845
  def test_42_TryRenameObjectWithSQLDict(self):
846
    # Test if we call methods only once
Yoshinori Okuji's avatar
Yoshinori Okuji committed
847
    self.DeferredSetTitleWithRenamedObject('SQLDict')
848

849
  def test_43_TryRenameObjectWithSQLQueue(self):
850
    # Test if we call methods only once
Yoshinori Okuji's avatar
Yoshinori Okuji committed
851
    self.DeferredSetTitleWithRenamedObject('SQLQueue')
852

853 854 855 856
  def test_44_TryRenameObjectWithSQLJoblib(self):
    # Test if we call methods only once
    self.DeferredSetTitleWithRenamedObject('SQLJoblib')

857
  def test_46_TryActiveProcessWithSQLDict(self):
858 859
    # Test if we call methods only once
    self.TryActiveProcess('SQLDict')
Sebastien Robin's avatar
Sebastien Robin committed
860

861
  def test_47_TryActiveProcessWithSQLQueue(self):
862 863
    # Test if we call methods only once
    self.TryActiveProcess('SQLQueue')
Sebastien Robin's avatar
Sebastien Robin committed
864

865 866 867 868
  def test_48_TryActiveProcessWithSQLJoblib(self):
    # Test if we call methods only once
    self.TryActiveProcessWithResultDict('SQLJoblib')

869
  def test_54_TryAfterMethodIdWithSQLDict(self):
870 871
    # Test if after_method_id can be used
    self.TryMethodAfterMethod('SQLDict')
872

873
  def test_55_TryAfterMethodIdWithSQLQueue(self):
874 875
    # Test if after_method_id can be used
    self.TryMethodAfterMethod('SQLQueue')
876

877 878 879 880 881
  def test_56_TryAfterMethodIdWithSQLJoblib(self):
    # Test if after_method_id can be used
    self.TryMethodAfterMethod('SQLJoblib')

  def test_57_TryCallActivityWithRightUser(self):
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
    # Test if me execute methods with the right user
    # This should be independant of the activity used
    # 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)
897
    self.commit()
898 899 900 901
    portal.portal_activities.distribute()
    portal.portal_activities.tic()
    email = organisation.get('email')
    # Check if what we did was executed as toto
902
    self.assertEqual(email.getOwnerInfo()['id'],'toto')
903

904
  def test_59_TryAfterTagWithSQLDict(self):
905 906 907
    # Test if after_tag can be used
    self.TryAfterTag('SQLDict')

908
  def test_60_TryAfterTagWithSQLQueue(self):
909 910 911
    # Test if after_tag can be used
    self.TryAfterTag('SQLQueue')

912 913 914 915 916
  def test_61_TryAfterTagWithSQLJoblib(self):
    # Test if after_tag can be used
    self.TryAfterTag('SQLJoblib')

  def test_62_CheckSchedulingWithSQLDict(self):
Yoshinori Okuji's avatar
Yoshinori Okuji committed
917 918 919
    # Test if scheduling is correct with SQLDict
    self.CheckScheduling('SQLDict')

920
  def test_63_CheckSchedulingWithSQLQueue(self):
Yoshinori Okuji's avatar
Yoshinori Okuji committed
921 922 923
    # Test if scheduling is correct with SQLQueue
    self.CheckScheduling('SQLQueue')

924 925 926 927 928
  def test_64_CheckSchedulingWithSQLJoblib(self):
    # Test if scheduling is correct with SQLQueue
    self.CheckScheduling('SQLJoblib')

  def test_65_CheckSchedulingAfterTagListWithSQLDict(self):
929 930 931
    # Test if scheduling is correct with SQLDict
    self.CheckSchedulingAfterTagList('SQLDict')

932
  def test_66_CheckSchedulingWithAfterTagListSQLQueue(self):
933 934 935
    # Test if scheduling is correct with SQLQueue
    self.CheckSchedulingAfterTagList('SQLQueue')

936 937 938 939
  def test_67_CheckSchedulingWithAfterTagListSQLJoblib(self):
    # Test if scheduling is correct with SQLQueue
    self.CheckSchedulingAfterTagList('SQLJoblib')

940
  def flushAllActivities(self, silent=0, loop_size=1000):
941 942 943 944
    """Executes all messages until the queue only contains failed
    messages.
    """
    activity_tool = self.getPortal().portal_activities
945
    for _ in xrange(loop_size):
946 947
      activity_tool.distribute(node_count=1)
      activity_tool.tic(processing_node=1)
948

949 950
      finished = 1
      for message in activity_tool.getMessageList():
951
        if message.processing_node != INVOKE_ERROR_STATE:
952 953 954
          finished = 0

      activity_tool.timeShift(3 * VALIDATION_ERROR_DELAY)
955
      self.commit()
956
      if finished:
957
        return
958 959
    if not silent:
      self.fail('flushAllActivities maximum loop count reached')
960

961
  def test_68_TestMessageValidationAndFailedActivities(self):
962 963 964 965
    """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
966 967 968 969 970 971
    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."""
972
    activity_tool = self.getPortal().portal_activities
973
    original_title = 'something'
974
    obj = self.getPortal().organisation_module.newContent(
975 976
                    portal_type='Organisation',
                    title=original_title)
977 978 979 980 981
    # Monkey patch Organisation to add a failing method
    def failingMethod(self):
      raise ValueError, 'This method always fail'
    Organisation.failingMethod = failingMethod

982
    activity_list = ['SQLQueue', 'SQLDict', 'SQLJoblib']
983 984
    for activity in activity_list:
      # reset
985
      activity_tool.manageClearActivities()
986
      obj.setTitle(original_title)
987
      self.commit()
988 989 990 991

      # activate failing message and flush
      for fail_activity in activity_list:
        obj.activate(activity = fail_activity).failingMethod()
992
      self.commit()
993 994 995 996
      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']
997
      if len(full_message_list) != 3:
998 999 1000 1001
        self.fail('failingMethod should not have been flushed')
      if len(remaining_messages) != 0:
        self.fail('Activity tool should have no other remaining messages')

1002 1003 1004 1005
      # activate our message
      new_title = 'nothing'
      obj.activate(after_method_id = ['failingMethod'],
                   activity = activity ).setTitle(new_title)
1006
      self.commit()
1007 1008 1009 1010
      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']
1011
      if len(full_message_list) != 4:
1012 1013 1014
        self.fail('failingMethod should not have been flushed')
      if len(remaining_messages) != 1:
        self.fail('Activity tool should have one blocked setTitle activity')
1015
      self.assertEqual(remaining_messages[0].activity_kw['after_method_id'],
1016
          ['failingMethod'])
1017
      self.assertEqual(obj.getTitle(), original_title)
1018

1019
  def test_69_TestCountMessageWithTagWithSQLDict(self):
1020 1021 1022 1023 1024
    """
      Test new countMessageWithTag function with SQLDict.
    """
    self.CheckCountMessageWithTag('SQLDict')

1025
  def test_70_TestCancelFailedActiveObject(self):
1026
    """Cancel an active object to make sure that it does not refer to
1027 1028 1029 1030
    a persistent object.

    XXX: this test fails if run first
    """
1031
    activity_tool = self.getPortal().portal_activities
1032
    activity_tool.manageClearActivities()
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044

    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

    # First, index the object.
1045
    self.commit()
1046
    self.flushAllActivities(silent=1, loop_size=100)
1047
    self.assertEqual(len(activity_tool.getMessageList()), 0)
1048 1049 1050

    # Insert a failing active object.
    obj.activate().failingMethod()
1051
    self.commit()
1052
    self.assertEqual(len(activity_tool.getMessageList()), 1)
1053 1054

    # Just wait for the active object to be abandoned.
1055
    self.flushAllActivities(silent=1, loop_size=100)
1056
    self.assertEqual(len(activity_tool.getMessageList()), 1)
1057
    self.assertEqual(activity_tool.getMessageList()[0].processing_node,
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
                      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)
1068
    self.commit()
1069
    self.assertEqual(len(activity_tool.getMessageList()), 0)
1070

1071
  def test_71_RetryMessageExecution(self):
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
    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:
1084
        raise ConflictError if conflict else Exception
1085
    def check(retry_list, **activate_kw):
1086
      fail = retry_list[-1][0] is not None and 1 or 0
1087
      for activity in 'SQLDict', 'SQLQueue', 'SQLJoblib':
1088
        exec_count[0] = 0
1089 1090
        activity_tool.activate(activity=activity, priority=priority(1,6),
                               **activate_kw).doSomething(retry_list)
1091
        self.commit()
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
        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})])
1113
      check([(True, None)] * 6, conflict_retry=False)
1114 1115 1116 1117 1118 1119 1120 1121 1122
      # 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)])
1123

1124 1125 1126
    finally:
      del activity_tool.__class__.doSomething
    self.assertFalse(activity_tool.getMessageList())
1127

1128
  def test_72_TestConflictErrorsWhileValidatingWithSQLDict(self):
1129 1130 1131 1132 1133
    """
      Test if conflict errors spoil out active objects with SQLDict.
    """
    self.TryConflictErrorsWhileValidating('SQLDict')

1134
  def test_73_TestConflictErrorsWhileValidatingWithSQLQueue(self):
1135 1136 1137 1138 1139
    """
      Test if conflict errors spoil out active objects with SQLQueue.
    """
    self.TryConflictErrorsWhileValidating('SQLQueue')

1140 1141 1142 1143 1144 1145 1146
  def test_74_TestConflictErrorsWhileValidatingWithSQLJoblib(self):
    """
      Test if conflict errors spoil out active objects with SQLJoblib.
    """
    self.TryConflictErrorsWhileValidating('SQLJoblib')

  def test_75_TestErrorsWhileFinishingCommitDBWithSQLDict(self):
1147 1148 1149 1150
    """
    """
    self.TryErrorsWhileFinishingCommitDB('SQLDict')

1151
  def test_76_TestErrorsWhileFinishingCommitDBWithSQLQueue(self):
1152 1153 1154 1155
    """
    """
    self.TryErrorsWhileFinishingCommitDB('SQLQueue')

1156
  def test_77_TryFlushActivityWithAfterTagSQLDict(self):
1157 1158 1159
    # Test if after_tag can be used
    self.TryFlushActivityWithAfterTag('SQLDict')

1160
  def test_78_TryFlushActivityWithAfterTagWithSQLQueue(self):
1161 1162 1163
    # Test if after_tag can be used
    self.TryFlushActivityWithAfterTag('SQLQueue')

1164
  def test_79_ActivateKwForNewContent(self):
1165 1166
    o1 = self.getOrganisationModule().newContent(
                                  activate_kw=dict(tag='The Tag'))
1167
    self.commit()
1168 1169 1170 1171
    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:
1172
      self.assertEqual(m.activity_kw.get('tag'), 'The Tag')
1173

1174

1175
  def test_80_FlushAfterMultipleActivate(self):
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
    orga_module = self.getOrganisationModule()
    p = orga_module.newContent(portal_type='Organisation')
    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()
1191
      self.commit()
1192

1193
    self.assertEqual(len(activity_tool.getMessageList()), 10)
1194 1195 1196 1197 1198 1199
    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()
1200
      self.commit()
1201 1202

    self.assertEqual(len(activity_tool.getMessageList()), 10)
1203
    activity_tool.flush(p, invoke=0)
1204
    self.commit()
1205 1206
    self.assertEqual(len(activity_tool.getMessageList()), 0)

1207
  def test_81_IsMessageRegisteredSQLDict(self):
1208 1209 1210 1211 1212
    """
      This test tests behaviour of IsMessageRegistered method.
    """
    self.checkIsMessageRegisteredMethod('SQLDict')

1213
  def test_82_AbortTransactionSynchronously(self):
1214
    """
1215 1216
      This test checks if transaction.abort() synchronizes connections. It
      didn't do so back in Zope 2.7
1217 1218 1219 1220 1221 1222
    """
    # 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()
1223
    self.commit()
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
    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)

1239 1240
    # Access to invalidated object in non-MVCC connections should raise a
    # conflict error
1241 1242
    organisation = module[organisation_id]
    self.assertRaises(ReadConflictError, getattr, organisation, 'uid')
1243 1244

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

1247
    self.abort()
1248 1249
    getattr(organisation, 'uid')

1250

1251
  def callWithGroupIdParamater(self, activity):
1252
    portal = self.getPortal()
1253 1254
    organisation =  portal.organisation._getOb(self.company_id)
    # Defined a group method
1255
    foobar_list = []
1256
    def setFoobar(self, object_list):
1257
      foobar_list.append(len(object_list))
1258
      for m in object_list:
1259 1260 1261
        obj = m.object
        obj.foobar = getattr(obj.aq_base, 'foobar', 0) + m.kw.get('number', 1)
        m.result = None
1262
    from Products.ERP5Type.Core.Folder import Folder
1263
    Folder.setFoobar = setFoobar
1264 1265 1266 1267 1268 1269

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

    organisation.foobar = 0
1270
    self.assertEqual(0,organisation.getFoobar())
1271 1272 1273

    # Test group_method_id is working without group_id
    for x in xrange(5):
1274
      organisation.activate(activity=activity, group_method_id="organisation_module/setFoobar").reindexObject(number=1)
1275
      self.commit()
1276 1277

    message_list = portal.portal_activities.getMessageList()
1278
    self.assertEqual(len(message_list),5)
1279
    portal.portal_activities.tic()
1280
    expected = dict(SQLDict=1, SQLQueue=5, SQLJoblib=1)[activity]
1281
    self.assertEqual(expected, organisation.getFoobar())
1282 1283 1284 1285


    # Test group_method_id is working with one group_id defined
    for x in xrange(5):
1286
      organisation.activate(activity=activity, group_method_id="organisation_module/setFoobar", group_id="1").reindexObject(number=1)
1287
      self.commit()
1288 1289

    message_list = portal.portal_activities.getMessageList()
1290
    self.assertEqual(len(message_list),5)
1291
    portal.portal_activities.tic()
1292
    self.assertEqual(expected * 2, organisation.getFoobar())
1293

1294
    self.assertEqual([expected, expected], foobar_list)
1295
    del foobar_list[:]
1296 1297 1298

    # Test group_method_id is working with many group_id defined
    for x in xrange(5):
1299
      organisation.activate(activity=activity, group_method_id="organisation_module/setFoobar", group_id="1").reindexObject(number=1)
1300
      self.commit()
1301
      organisation.activate(activity=activity, group_method_id="organisation_module/setFoobar", group_id="2").reindexObject(number=3)
1302
      self.commit()
1303
      organisation.activate(activity=activity, group_method_id="organisation_module/setFoobar", group_id="1").reindexObject(number=1)
1304
      self.commit()
1305
      organisation.activate(activity=activity, group_method_id="organisation_module/setFoobar", group_id="3").reindexObject(number=5)
1306
      self.commit()
1307 1308

    message_list = portal.portal_activities.getMessageList()
1309
    self.assertEqual(len(message_list),20)
1310
    portal.portal_activities.tic()
1311
    self.assertEqual(dict(SQLDict=11, SQLQueue=60, SQLJoblib=11)[activity],
1312
                      organisation.getFoobar())
1313
    self.assertEqual(dict(SQLDict=[1, 1, 1], SQLQueue=[5, 5, 10], SQLJoblib=[1,1,1])[activity],
1314
                      sorted(foobar_list))
1315
    message_list = portal.portal_activities.getMessageList()
1316
    self.assertEqual(len(message_list), 0)
1317

1318
  def test_83a_CallWithGroupIdParamaterSQLDict(self):
1319 1320 1321
    """
    Test that group_id parameter is used to separate execution of the same method
    """
1322
    self.callWithGroupIdParamater('SQLDict')
1323

1324
  def test_83b_CallWithGroupIdParamaterSQLQueue(self):
1325 1326 1327
    """
    Test that group_id parameter is used to separate execution of the same method
    """
1328
    self.callWithGroupIdParamater('SQLQueue')
1329

1330 1331 1332 1333 1334 1335 1336
  def test_83c_CallWithGroupIdParamaterSQLJoblib(self):
    """
    Test that group_id parameter is used to separate execution of the same method
    """
    self.callWithGroupIdParamater('SQLJoblib')

  def test_84_ActivateKwForWorkflowTransition(self):
1337 1338 1339 1340 1341 1342
    """
    Test call of a workflow transition with activate_kw parameter propagate them
    """
    o1 = self.getOrganisationModule().newContent()
    self.tic()
    o1.validate(activate_kw=dict(tag='The Tag'))
1343
    self.commit()
1344 1345 1346 1347
    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:
1348
      self.assertEqual(m.activity_kw.get('tag'), 'The Tag')
1349

1350
  def test_85_LossOfVolatileAttribute(self):
1351 1352 1353 1354 1355 1356
    """
    Test that the loss of volatile attribute doesn't loose activities
    """
    self.tic()
    activity_tool = self.getActivityTool()
    message_list = activity_tool.getMessageList()
1357
    self.assertEqual(len(message_list), 0)
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
    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()
1371
    self.commit()
1372
    message_list = activity_tool.getMessageList()
1373
    self.assertEqual(len(message_list), 2)
1374

1375
  def test_88_ProcessingMultipleMessagesMustRevertIndividualMessagesOnError(self):
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
    """
      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
    """
    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'
1392 1393 1394 1395 1396 1397 1398
    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()
1399
      self.commit()
1400 1401 1402 1403
      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)
1404
      self.assertEqual(sorted(obj.getTitle()), ['a', 'b', 'd'])
1405 1406
    finally:
      delattr(Organisation, 'appendToTitle')
1407

1408
  def test_89_RequestIsolationInsideSameTic(self):
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
    """
      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
    """
    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')
      self.tic()
      self.assertEqual(obj.getTitle(), 'Success')
    finally:
      delattr(Organisation, 'putMarkerValue')
      delattr(Organisation, 'checkMarkerValue')

1437
  def TryUserNotificationOnActivityFailure(self, activity):
1438 1439 1440
    message_list = self.portal.MailHost._message_list
    del message_list[:]
    obj = self.portal.organisation_module.newContent(portal_type='Organisation')
1441
    self.tic()
1442
    def failingMethod(self): raise ValueError('This method always fails')
1443 1444
    Organisation.failingMethod = failingMethod
    try:
1445 1446
      # MESSAGE_NOT_EXECUTED
      obj.activate(activity=activity).failingMethod()
1447
      self.commit()
1448
      self.assertFalse(message_list)
1449
      self.flushAllActivities(silent=1, loop_size=100)
1450 1451 1452 1453 1454
      # Check there is a traceback in the email notification
      sender, recipients, mail = message_list.pop()
      self.assertTrue("Module %s, line %s, in failingMethod" % (
        __name__, inspect.getsourcelines(failingMethod)[1]) in mail, mail)
      self.assertTrue("ValueError:" in mail, mail)
1455 1456
      # MESSAGE_NOT_EXECUTABLE
      obj.getParentValue()._delObject(obj.getId())
1457
      obj.activate(activity=activity).failingMethod()
1458
      self.commit()
1459 1460 1461 1462
      self.assertTrue(obj.hasActivity())
      self.tic()
      self.assertFalse(obj.hasActivity())
      self.assertFalse(message_list)
1463
    finally:
1464
      del Organisation.failingMethod
1465

1466
  def test_90_userNotificationOnActivityFailureWithSQLDict(self):
1467 1468 1469 1470 1471 1472
    """
      Check that a user notification method is called on message when activity
      fails and will not be tried again.
    """
    self.TryUserNotificationOnActivityFailure('SQLDict')

1473 1474 1475 1476 1477 1478 1479
  def test_91_userNotificationOnActivityFailureWithSQLJoblib(self):
    """
      Check user notification sent on activity final error
    """
    self.TryUserNotificationOnActivityFailure('SQLJoblib')

  def test_92_userNotificationOnActivityFailureWithSQLQueue(self):
1480 1481 1482 1483 1484 1485
    """
      Check that a user notification method is called on message when activity
      fails and will not be tried again.
    """
    self.TryUserNotificationOnActivityFailure('SQLQueue')

1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
  def TryUserNotificationRaise(self, activity):
    self.tic()
    obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
    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
1496
    getMessageList = self.getPortalObject().portal_activities.getMessageList
1497 1498
    try:
      obj.activate(activity=activity, priority=6).failingMethod()
1499
      self.commit()
1500
      self.flushAllActivities(silent=1, loop_size=100)
1501 1502
      message, = getMessageList(activity=activity, method_id='failingMethod')
      self.assertEqual(message.processing, 0)
1503 1504 1505 1506
    finally:
      Message.notifyUser = original_notifyUser
      delattr(Organisation, 'failingMethod')

1507
  def test_93_userNotificationRaiseWithSQLDict(self):
1508 1509 1510 1511 1512
    """
      Check that activities are not left with processing=1 when notifyUser raises.
    """
    self.TryUserNotificationRaise('SQLDict')

1513
  def test_94_userNotificationRaiseWithSQLQueue(self):
1514 1515 1516 1517
    """
      Check that activities are not left with processing=1 when notifyUser raises.
    """
    self.TryUserNotificationRaise('SQLQueue')
1518

1519 1520 1521 1522 1523 1524
  def test_95_userNotificationRaiseWithSQLJoblib(self):
    """
      Check that activities are not left with processing=1 when notifyUser raises.
    """
    self.TryUserNotificationRaise('SQLJoblib')

1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
  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.
    """
    self.tic()
    activity_tool = self.getActivityTool()
    try:
      Organisation.registerFailingTransactionManager = registerFailingTransactionManager
      obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
      self.tic()
      now = DateTime()
      obj.activate(activity=activity).registerFailingTransactionManager()
1539
      self.commit()
1540
      self.flushAllActivities(silent=1, loop_size=100)
1541
      self.commit()
1542
      # Check that cmf_activity SQL connection still works
1543
      connection_da = self.getPortalObject().cmf_activity_sql_connection()
1544
      self.assertFalse(connection_da._registered)
1545
      connection_da.query('select 1')
1546
      self.assertTrue(connection_da._registered)
1547
      self.commit()
1548 1549 1550 1551
      self.assertFalse(connection_da._registered)
    finally:
      delattr(Organisation, 'registerFailingTransactionManager')

1552
  def test_96_ActivityRaiseInCommitDoesNotStallActivityConectionSQLDict(self):
1553 1554
    self.TryActivityRaiseInCommitDoesNotStallActivityConection('SQLDict')

1555
  def test_97_ActivityRaiseInCommitDoesNotStallActivityConectionSQLQueue(self):
1556 1557
    self.TryActivityRaiseInCommitDoesNotStallActivityConection('SQLQueue')

1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
  def TryActivityRaiseInCommitDoesNotLooseMessages(self, activity):
    """
    """
    self.tic()
    activity_tool = self.getActivityTool()
    try:
      Organisation.registerFailingTransactionManager = registerFailingTransactionManager
      obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
      self.tic()
      now = DateTime()
      obj.activate(activity=activity).registerFailingTransactionManager()
1569
      self.commit()
1570
      self.flushAllActivities(silent=1, loop_size=100)
1571
      self.commit()
1572
      self.assertEqual(activity_tool.countMessage(method_id='registerFailingTransactionManager'), 1)
1573 1574 1575
    finally:
      delattr(Organisation, 'registerFailingTransactionManager')

1576
  def test_98_ActivityRaiseInCommitDoesNotLooseMessagesSQLDict(self):
1577 1578
    self.TryActivityRaiseInCommitDoesNotLooseMessages('SQLDict')

1579
  def test_99_ActivityRaiseInCommitDoesNotLooseMessagesSQLQueue(self):
1580
    self.TryActivityRaiseInCommitDoesNotLooseMessages('SQLQueue')
1581

1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
  def TryChangeSkinInActivity(self, activity):
    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')
      self.tic()
      organisation.activate(activity=activity).changeSkinToNone()
1592
      self.commit()
1593
      self.assertEqual(len(activity_tool.getMessageList()), 1)
1594
      self.flushAllActivities(silent=1, loop_size=100)
1595
      self.assertEqual(len(activity_tool.getMessageList()), 0)
1596 1597 1598
    finally:
      delattr(Organisation, 'changeSkinToNone')

1599
  def test_100_TryChangeSkinInActivitySQLDict(self):
1600 1601
    self.TryChangeSkinInActivity('SQLDict')

1602
  def test_101_TryChangeSkinInActivitySQLQueue(self):
1603
    self.TryChangeSkinInActivity('SQLQueue')
1604

1605 1606 1607 1608
  def test_102_TryChangeSkinInActivitySQLJoblib(self):
    self.TryChangeSkinInActivity('SQLJoblib')

  def test_103_1_CheckSQLDictDoesNotDeleteSimilaritiesBeforeExecution(self):
1609 1610 1611 1612
    """
      Test that SQLDict does not delete similar messages which have the same
      method_id and path but a different tag before execution.
    """
1613
    activity_tool = self.getActivityTool()
1614 1615 1616 1617
    marker = []
    def doSomething(self, other_tag):
      marker.append(self.countMessage(tag=other_tag))
    activity_tool.__class__.doSomething = doSomething
1618
    try:
1619
      # Adds two similar but not the same activities.
1620 1621 1622 1623
      activity_tool.activate(activity='SQLDict', after_tag='foo',
        tag='a').doSomething(other_tag='b')
      activity_tool.activate(activity='SQLDict', after_tag='bar',
        tag='b').doSomething(other_tag='a')
1624
      self.commit()
1625
      activity_tool.tic() # make sure distribution phase was not skipped
1626 1627 1628
      activity_tool.distribute()
      # after distribute, similarities are still there.
      self.assertEqual(len(activity_tool.getMessageList()), 2)
1629
      activity_tool.tic()
1630
      self.assertEqual(len(activity_tool.getMessageList()), 0)
1631
      self.assertEqual(marker, [1])
1632
    finally:
1633
      del activity_tool.__class__.doSomething
1634

1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
  def test_103_2_CheckSQLDictDoesNotDeleteDuplicatesBeforeExecution(self):
    """
      Test that SQLDict does not delete messages before execution
      even if messages have the same method_id and path and tag.
      There could be other things which differ (ex: serialization_tag) and may
      not all be cheap to check during validation. Validation node is the only
      non-paralelisable Zope-side task around activities, so it should be kept
      simple.
      Deduplication is cheap:
      - inside the transaction which spawned duplicate activities, because it
        has to have created activities around anyway, and can keep track
      - inside the CMFActvitiy-level processing surrounding activity execution
        because it has to load the activities to process them anyway
1648 1649
    """
    activity_tool = self.getActivityTool()
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
    # Adds two same activities.
    activity_tool.activate(activity='SQLDict', after_tag='foo', priority=2,
      tag='a').getId()
    self.commit()
    uid1, = [x.uid for x in activity_tool.getMessageList()]
    activity_tool.activate(activity='SQLDict', after_tag='bar', priority=1,
      tag='a').getId()
    self.commit()
    uid2, = [x.uid for x in activity_tool.getMessageList() if x.uid != uid1]
    self.assertEqual(len(activity_tool.getMessageList()), 2)
    activity_tool.distribute()
    # After distribute, duplicate is still present.
    self.assertItemsEqual([uid1, uid2], [x.uid for x in activity_tool.getMessageList()])
    activity_tool.tic()
    self.assertEqual(len(activity_tool.getMessageList()), 0)
1665

1666
  def test_103_3_CheckSQLJoblibDoesNotDeleteDuplicatesBeforeExecution(self):
1667
    """
1668
    (see test_103_2_CheckSQLDictDoesNotDeleteDuplicatesBeforeExecution)
1669 1670
    """
    activity_tool = self.getActivityTool()
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
    # Adds two same activities.
    activity_tool.activate(activity='SQLJoblib', after_tag='foo', priority=2,
      tag='a').getId()
    self.commit()
    uid1, = [x.uid for x in activity_tool.getMessageList()]
    activity_tool.activate(activity='SQLJoblib', after_tag='bar', priority=1,
      tag='a').getId()
    self.commit()
    uid2, = [x.uid for x in activity_tool.getMessageList() if x.uid != uid1]
    self.assertEqual(len(activity_tool.getMessageList()), 2)
    activity_tool.distribute()
    # After distribute, duplicate is still present.
    self.assertItemsEqual([uid1, uid2], [x.uid for x in activity_tool.getMessageList()])
    activity_tool.tic()
    self.assertEqual(len(activity_tool.getMessageList()), 0)
1686 1687

  def test_103_4_CheckSQLDictDistributeWithSerializationTagAndGroupMethodId(
1688
      self):
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
    """
      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')
    self.tic()
    activity_tool = self.getActivityTool()
    organisation.activate(serialization_tag='a').getId()
1700
    self.commit()
1701 1702
    organisation.activate(serialization_tag='a',
              group_method_id='portal_catalog/catalogObjectList').getTitle()
1703
    self.commit()
1704 1705 1706 1707 1708 1709 1710
    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)

1711
  def test_104_interQueuePriorities(self):
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
    """
      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.
    """
    organisation = self.getPortal().organisation_module.newContent(portal_type='Organisation')
    self.tic()
    activity_tool = self.getActivityTool()
    check_result_dict = {}
    def runAndCheck():
      check_result_dict.clear()
1725
      self.commit()
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
      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')
1757 1758

  def CheckActivityRuntimeEnvironment(self, activity):
1759 1760
    document = self.portal.organisation_module
    activity_result = []
1761
    def extractActivityRuntimeEnvironment(self):
1762 1763
      activity_result.append(self.getActivityRuntimeEnvironment())
    document.__class__.doSomething = extractActivityRuntimeEnvironment
1764
    try:
1765
      document.activate(activity=activity).doSomething()
1766
      self.commit()
1767 1768
      # Check that getActivityRuntimeEnvironment raises outside of activities
      self.assertRaises(KeyError, document.getActivityRuntimeEnvironment)
1769
      # Check Runtime isolation
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
      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')
1783
    finally:
1784
      del document.__class__.doSomething
1785

1786
  def test_105_activityRuntimeEnvironmentSQLDict(self):
1787 1788
    self.CheckActivityRuntimeEnvironment('SQLDict')

1789
  def test_106_activityRuntimeEnvironmentSQLQueue(self):
1790 1791
    self.CheckActivityRuntimeEnvironment('SQLQueue')

1792 1793 1794
  def test_107_activityRuntimeEnvironmentSQLJoblib(self):
    self.CheckActivityRuntimeEnvironment('SQLJoblib')

1795 1796 1797 1798 1799 1800 1801 1802 1803
  def CheckSerializationTag(self, activity):
    organisation = self.getPortal().organisation_module.newContent(portal_type='Organisation')
    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()
1804
    self.commit()
1805 1806 1807 1808 1809 1810 1811
    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()
1812
    self.commit()
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822
    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)
1823
    organisation.activate(activity=activity, serialization_tag='1', priority=2).getTitle()
1824
    # Use a different method just so that SQLDict doesn't merge both activities prior to insertion.
1825
    organisation.activate(activity=activity, serialization_tag='1', priority=1).getId()
1826
    self.commit()
1827 1828 1829 1830
    result = activity_tool.getMessageList()
    self.assertEqual(len(result), 2)
    activity_tool.distribute()
    result = activity_tool.getMessageList()
1831 1832 1833 1834 1835 1836
    # 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')
1837 1838
    self.tic()
    result = activity_tool.getMessageList()
1839
    self.assertEqual(len(result), 0)
1840 1841 1842 1843 1844
    # 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)
1845

1846
  def test_108_checkSerializationTagSQLDict(self):
1847 1848
    self.CheckSerializationTag('SQLDict')

1849
  def test_109_checkSerializationTagSQLQueue(self):
1850 1851
    self.CheckSerializationTag('SQLQueue')

1852
  def test_110_testAbsoluteUrl(self):
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
    # 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')
1869
      self.assertEqual(o.absolute_url(),
1870 1871
          'http://test.erp5.org:9080/virtual_root/test_obj')
      o.activate().checkAbsoluteUrl()
1872

1873 1874 1875 1876 1877 1878 1879
      # 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
1880
      self.assertEqual(o.absolute_url(),
1881 1882 1883 1884 1885
          '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)
1886
      self.assertEqual(calls, ['http://test.erp5.org:9080/virtual_root/test_obj'])
1887 1888 1889
    finally:
      delattr(Organisation, 'checkAbsoluteUrl')

1890 1891 1892 1893 1894
  def CheckLocalizerWorks(self, activity):
    FROM_STRING = 'Foo'
    TO_STRING = 'Bar'
    LANGUAGE = 'xx'
    def translationTest(context):
1895
      from Products.ERP5Type.Message import Message
1896
      context.setTitle(context.Base_translateString(FROM_STRING))
1897
      context.setDescription(str(Message('erp5_ui', FROM_STRING)))
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
    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')
    self.tic()
    Organisation.translationTest = translationTest
    try:
1910 1911 1912
      REQUEST = organisation.REQUEST
      # Simulate what a browser would have sent to Zope
      REQUEST.environ['HTTP_ACCEPT_LANGUAGE'] = LANGUAGE
1913
      organisation.activate(activity=activity).translationTest()
1914
      self.commit()
1915 1916 1917
      # Remove request parameter to check that it was saved at activate call
      # and restored at message execution.
      del REQUEST.environ['HTTP_ACCEPT_LANGUAGE']
1918 1919 1920 1921
      self.tic()
    finally:
      delattr(Organisation, 'translationTest')
    self.assertEqual(TO_STRING, organisation.getTitle())
1922
    self.assertEqual(TO_STRING, organisation.getDescription())
1923

1924
  def test_112_checkLocalizerWorksSQLQueue(self):
1925 1926
    self.CheckLocalizerWorks('SQLQueue')

1927
  def test_113_checkLocalizerWorksSQLDict(self):
1928 1929
    self.CheckLocalizerWorks('SQLDict')

1930
  def test_114_checkSQLQueueActivitySucceedsAfterActivityChangingSkin(self):
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
    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')
    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()
1955
      self.commit()
1956 1957 1958 1959 1960 1961 1962
      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)
1963
      self.assertEqual(len(activity_tool.getMessageList()), 0)
1964 1965 1966 1967
    finally:
      delattr(Organisation, 'firstTest')
      delattr(Organisation, 'secondTest')

1968
  def test_115_checkProcessShutdown(self):
1969 1970 1971 1972 1973 1974 1975 1976 1977
    # 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)
1978
    #
1979 1980 1981 1982
    # 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
1983
    #   This is not perfect though, since it would require to have access to
1984 1985 1986 1987 1988 1989
    #   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')
    self.tic()
1990 1991
    activity_event = threading.Event()
    rendez_vous_event = threading.Event()
1992 1993
    def waitingActivity(context):
      # Inform test that we arrived at rendez-vous.
1994 1995 1996
      rendez_vous_event.set()
      # When this event is available, it means test has called process_shutdown.
      activity_event.wait()
1997 1998
    from Products.CMFActivity.Activity.SQLDict import SQLDict
    original_dequeue = SQLDict.dequeueMessage
1999
    queue_tic_test_dict = {}
2000
    def dequeueMessage(self, activity_tool, processing_node):
2001
      # This is a one-shot method, revert after execution
2002 2003 2004
      SQLDict.dequeueMessage = original_dequeue
      result = self.dequeueMessage(activity_tool, processing_node)
      queue_tic_test_dict['isAlive'] = process_shutdown_thread.isAlive()
2005
      return result
2006
    SQLDict.dequeueMessage = dequeueMessage
2007 2008
    Organisation.waitingActivity = waitingActivity
    try:
Vincent Pelletier's avatar
Vincent Pelletier committed
2009
      # Use SQLDict with no group method so that both activities won't be
2010 2011
      # executed in the same batch, letting activity tool a chance to check
      # if execution should stop processing activities.
2012 2013
      organisation.activate(activity='SQLDict', tag='foo').waitingActivity()
      organisation.activate(activity='SQLDict', after_tag='foo').getTitle()
2014
      self.commit()
2015 2016
      self.assertEqual(len(activity_tool.getMessageList()), 2)
      activity_tool.distribute()
2017
      self.commit()
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041

      # 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.
2042
      rendez_vous_event.wait()
2043 2044 2045 2046
      # Initiate shutdown
      process_shutdown_thread.start()
      try:
        # Let waiting activity finish and wait for thread exit
2047
        activity_event.set()
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
        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()
2064
        except StandardException:
2065 2066 2067 2068 2069 2070
          # 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')
2071
      SQLDict.dequeueMessage = original_dequeue
2072 2073

  def test_hasActivity(self):
2074 2075
    active_object = self.portal.organisation_module.newContent(
                                            portal_type='Organisation')
2076
    active_process = self.portal.portal_activities.newActiveProcess()
2077
    self.tic()
2078

2079
    self.assertFalse(active_object.hasActivity())
2080 2081 2082
    self.assertFalse(active_process.hasActivity())

    def test(obj, **kw):
2083
      for activity in ('SQLDict', 'SQLQueue', 'SQLJoblib'):
2084
        active_object.activate(activity=activity, **kw).getTitle()
2085
        self.commit()
2086 2087 2088 2089 2090 2091 2092
        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())
2093

2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
  def _test_hasErrorActivity_error(self, activity):
    # Monkey patch Organisation to add a failing method
    def failingMethod(self):
      raise ValueError('This method always fail')
    Organisation.failingMethod = failingMethod
    active_object = self.portal.organisation_module.newContent(
                                            portal_type='Organisation')
    active_process = self.portal.portal_activities.newActiveProcess()
    self.tic()


    self.assertFalse(active_object.hasErrorActivity())
    self.assertFalse(active_process.hasErrorActivity())

    active_object.activate(
      activity=activity, active_process=active_process).failingMethod()
    self.commit()
    # assert that any activity is created
    self.assertTrue(active_object.hasActivity())
    self.assertTrue(active_process.hasActivity())
    # assert that no error is reported
    self.assertFalse(active_object.hasErrorActivity())
    self.assertFalse(active_process.hasErrorActivity())
    self.flushAllActivities()
    # assert that any activity is created
    self.assertTrue(active_object.hasActivity())
    self.assertTrue(active_process.hasActivity())
    # assert that an error has been seen
    self.assertTrue(active_object.hasErrorActivity())
    self.assertTrue(active_process.hasErrorActivity())

  def test_hasErrorActivity_error_SQLQueue(self):
    self._test_hasErrorActivity_error('SQLQueue')

  def test_hasErrorActivity_error_SQLDict(self):
    self._test_hasErrorActivity_error('SQLDict')

2131 2132 2133
  def test_hasErrorActivity_error_SQLJoblib(self):
    self._test_hasErrorActivity_error('SQLJoblib')

2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
  def _test_hasErrorActivity(self, activity):
    active_object = self.portal.organisation_module.newContent(
                                            portal_type='Organisation')
    active_process = self.portal.portal_activities.newActiveProcess()
    self.tic()

    self.assertFalse(active_object.hasErrorActivity())
    self.assertFalse(active_process.hasErrorActivity())

    active_object.activate(
      activity=activity, active_process=active_process).getTitle()
    self.commit()
    # assert that any activity is created
    self.assertTrue(active_object.hasActivity())
    self.assertTrue(active_process.hasActivity())
    # assert that no error is reported
    self.assertFalse(active_object.hasErrorActivity())
    self.assertFalse(active_process.hasErrorActivity())
    self.flushAllActivities()
    # assert that any activity is created
    self.assertFalse(active_object.hasActivity())
    self.assertFalse(active_process.hasActivity())
    # assert that no error is reported
    self.assertFalse(active_object.hasErrorActivity())
    self.assertFalse(active_process.hasErrorActivity())

  def test_hasErrorActivity_SQLQueue(self):
    self._test_hasErrorActivity('SQLQueue')

  def test_hasErrorActivity_SQLDict(self):
    self._test_hasErrorActivity('SQLDict')

2166 2167 2168
  def test_hasErrorActivity_SQLJoblib(self):
    self._test_hasErrorActivity('SQLJoblib')

2169 2170 2171
  def test_active_object_hasActivity_does_not_catch_exceptions(self):
    """
    Some time ago, hasActivity was doing a silent try/except, and this was
2172
    a possible disaster for some projects. Here we make sure that if the
2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
    SQL request fails, then the exception is not ignored
    """
    active_object = self.portal.organisation_module.newContent(
                                            portal_type='Organisation')
    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()
2188
      self.commit()
2189 2190 2191 2192 2193 2194 2195 2196 2197
      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
2198

2199 2200 2201
  def test_MAX_MESSAGE_LIST_SIZE(self):
    from Products.CMFActivity.Activity import SQLBase
    MAX_MESSAGE_LIST_SIZE = SQLBase.MAX_MESSAGE_LIST_SIZE
2202
    try:
2203 2204 2205 2206
      SQLBase.MAX_MESSAGE_LIST_SIZE = 3
      def dummy_counter(o):
        self.__call_count += 1
      o = self.portal.organisation_module.newContent(portal_type='Organisation')
2207

2208
      for activity in "SQLDict", "SQLQueue", "SQLJoblib":
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219
        self.__call_count = 0
        try:
          for i in xrange(10):
            method_name = 'dummy_counter_%s' % i
            getattr(o.activate(activity=activity), method_name)()
            setattr(Organisation, method_name, dummy_counter)
          self.flushAllActivities()
        finally:
          for i in xrange(10):
            delattr(Organisation, 'dummy_counter_%s' % i)
        self.assertEqual(self.__call_count, 10)
2220
    finally:
2221
      SQLBase.MAX_MESSAGE_LIST_SIZE = MAX_MESSAGE_LIST_SIZE
2222

2223
  def test_115_TestSerializationTagSQLDictPreventsParallelExecution(self):
2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
    """
      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.
    """
    from Products.CMFActivity import ActivityTool

    portal = self.getPortal()
    activity_tool = portal.portal_activities
    self.tic()

    # Add 6 activities
    portal.organisation_module.activate(activity='SQLDict', tag='', serialization_tag='test_115').getId()
2237
    self.commit()
2238
    portal.organisation_module.activate(activity='SQLDict', serialization_tag='test_115').getTitle()
2239
    self.commit()
2240
    portal.organisation_module.activate(activity='SQLDict', tag='tag_1', serialization_tag='test_115').getId()
2241
    self.commit()
2242
    portal.person_module.activate(activity='SQLDict', serialization_tag='test_115').getId()
2243
    self.commit()
2244
    portal.person_module.activate(activity='SQLDict', tag='tag_2').getId()
2245
    self.commit()
2246
    portal.organisation_module.activate(activity='SQLDict', tag='', serialization_tag='test_115').getId()
2247
    self.commit()
2248 2249 2250

    # distribute and assign them to 3 nodes
    activity_tool.distribute()
2251
    self.commit()
2252 2253

    from Products.CMFActivity import ActivityTool
2254 2255
    activity = ActivityTool.activity_dict['SQLDict']
    activity.getProcessableMessageList(activity_tool, 1)
2256
    self.commit()
2257
    activity.getProcessableMessageList(activity_tool, 2)
2258
    self.commit()
2259
    activity.getProcessableMessageList(activity_tool, 3)
2260
    self.commit()
2261

2262
    result = activity._getMessageList(activity_tool)
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
    try:
      self.assertEqual(len([message
                            for message in result
                            if (message.processing_node>0 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')]),
2274
                       4)
2275 2276 2277 2278 2279 2280 2281 2282

      self.assertEqual(len([message
                            for message in result
                            if (message.processing_node>0 and
                                message.serialization_tag=='')]),
                       1)
    finally:
      # Clear activities from all nodes
2283 2284
      activity_tool.SQLBase_delMessage(table=SQLDict.sql_table,
                                       uid=[message.uid for message in result])
2285
      self.commit()
2286

2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
  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:
2297
      for activity in 'SQLDict', 'SQLQueue', 'SQLJoblib':
2298
        activity_tool.activate(activity=activity).doSomething(activity)
2299
        self.commit()
2300 2301 2302 2303
        # Make first commit in dequeueMessage raise
        registerFailingTransactionManager()
        self.assertRaises(CommitFailed, activity_tool.tic)
        # Normally, the request stops here and Zope aborts the transaction
2304
        self.abort()
2305 2306 2307 2308 2309 2310 2311 2312
        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

2313 2314 2315 2316 2317
  def test_117_PlacelessDefaultReindexParameters(self):
    """
      Test behaviour of PlacelessDefaultReindexParameters.
    """
    portal = self.portal
2318

2319 2320 2321 2322 2323 2324
    # Make a new Person object to make sure that the portal type
    # is migrated to an instance of a portal type class, otherwise
    # the portal type may generate an extra active object.
    portal.person_module.newContent(portal_type='Person')
    self.tic()

2325 2326 2327
    original_reindex_parameters = portal.getPlacelessDefaultReindexParameters()
    if original_reindex_parameters is None:
      original_reindex_parameters = {}
2328 2329

    tag = 'SOME_RANDOM_TAG'
2330
    activate_kw = original_reindex_parameters.get('activate_kw', {}).copy()
2331
    activate_kw['tag'] = tag
2332 2333 2334
    portal.setPlacelessDefaultReindexParameters(activate_kw=activate_kw, \
                                                **original_reindex_parameters)
    current_default_reindex_parameters = portal.getPlacelessDefaultReindexParameters()
2335
    self.assertEqual({'activate_kw': {'tag': tag}}, \
2336 2337
                       current_default_reindex_parameters)
    person = portal.person_module.newContent(portal_type='Person')
2338
    self.commit()
2339 2340
    # as we specified it in setPlacelessDefaultReindexParameters we should have
    # an activity for this tags
2341
    self.assertEqual(1, portal.portal_activities.countMessageWithTag(tag))
2342
    self.tic()
2343
    self.assertEqual(0, portal.portal_activities.countMessageWithTag(tag))
2344

2345 2346 2347
    # restore originals ones
    portal.setPlacelessDefaultReindexParameters(**original_reindex_parameters)
    person = portal.person_module.newContent(portal_type='Person')
Łukasz Nowak's avatar
Łukasz Nowak committed
2348
    # .. now no messages with this tag should apper
2349
    self.assertEqual(0, portal.portal_activities.countMessageWithTag(tag))
2350

2351 2352 2353 2354 2355
  def TryNotificationSavedOnEventLogWhenNotifyUserRaises(self, activity):
    activity_tool = self.getActivityTool()
    self.tic()
    obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
    self.tic()
2356
    original_notifyUser = Message.notifyUser.im_func
2357 2358
    def failSendingEmail(self, *args, **kw):
      raise MailHostError, 'Mail is not sent'
2359
    activity_unit_test_error = Exception()
2360 2361 2362
    def failingMethod(self):
      raise activity_unit_test_error
    try:
2363 2364 2365
      Message.notifyUser = failSendingEmail
      Organisation.failingMethod = failingMethod
      self._catch_log_errors()
2366
      obj.activate(activity=activity, priority=6).failingMethod()
2367
      self.commit()
2368
      self.flushAllActivities(silent=1, loop_size=100)
2369
      message, = activity_tool.getMessageList()
2370
      self.commit()
2371 2372 2373
      for log_record in self.logged:
        if log_record.name == 'ActivityTool' and log_record.levelname == 'WARNING':
          type, value, trace = log_record.exc_info
2374
      self.commit()
2375 2376 2377
      self.assertTrue(activity_unit_test_error is value)
    finally:
      Message.notifyUser = original_notifyUser
2378 2379
      del Organisation.failingMethod
      self._ignore_log_errors()
2380

2381
  def test_118_userNotificationSavedOnEventLogWhenNotifyUserRaisesWithSQLDict(self):
2382 2383 2384 2385 2386
    """
      Check the error is saved on event log even if the mail notification is not sent.
    """
    self.TryNotificationSavedOnEventLogWhenNotifyUserRaises('SQLDict')

2387
  def test_119_userNotificationSavedOnEventLogWhenNotifyUserRaisesWithSQLQueue(self):
2388 2389 2390
    """
      Check the error is saved on event log even if the mail notification is not sent.
    """
2391
    self.TryNotificationSavedOnEventLogWhenNotifyUserRaises('SQLQueue')
2392

2393 2394 2395 2396 2397 2398
  def test_120_userNotificationSavedOnEventLogWhenNotifyUserRaisesWithSQLJoblib(self):
    """
      Check the error is saved on event log even if the mail notification is not sent.
    """
    self.TryNotificationSavedOnEventLogWhenNotifyUserRaises('SQLJoblib')

2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417
  def TryUserMessageContainingNoTracebackIsStillSent(self, activity):
    portal = self.getPortalObject()
    activity_tool = self.getActivityTool()
    # With Message.__call__
    # 1: activity context does not exist when activity is executed
    self.tic()
    obj = self.getPortal().organisation_module.newContent(portal_type='Organisation')
    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()
2418
      self.commit()
2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
      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
2429
      delattr(Organisation, 'failingMethod')
2430

2431
  def test_121_sendMessageWithNoTracebackWithSQLQueue(self):
2432 2433
    self.TryUserMessageContainingNoTracebackIsStillSent('SQLQueue')

2434
  def test_122_sendMessageWithNoTracebackWithSQLDict(self):
2435
    self.TryUserMessageContainingNoTracebackIsStillSent('SQLDict')
2436

2437 2438 2439 2440 2441 2442
  def test_123_sendMessageWithNoTracebackWithSQLJoblib(self):
    """
      Check that message with no traceback is still sen
    """
    self.TryUserMessageContainingNoTracebackIsStillSent('SQLJoblib')

2443 2444 2445
  def TryNotificationSavedOnEventLogWhenSiteErrorLoggerRaises(self, activity):
    # Make sure that no active object is installed.
    activity_tool = self.getPortal().portal_activities
2446
    activity_tool.manageClearActivities()
2447 2448 2449 2450 2451 2452

    # 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)
2453
    self.commit()
2454
    self.flushAllActivities(silent = 1, loop_size = 10)
2455
    self.assertEqual(len(activity_tool.getMessageList()), 0)
2456 2457 2458 2459 2460 2461
    class ActivityUnitTestError(Exception):
      pass
    activity_unit_test_error = ActivityUnitTestError()
    def failingMethod(self):
      raise activity_unit_test_error
    from Products.SiteErrorLog.SiteErrorLog import SiteErrorLog
2462
    original_raising = SiteErrorLog.raising.im_func
2463 2464 2465 2466 2467

    # Monkey patch Site Error to induce conflict errors artificially.
    def raising(self, info):
      raise AttributeError
    try:
2468 2469 2470
      SiteErrorLog.raising = raising
      Organisation.failingMethod = failingMethod
      self._catch_log_errors()
2471
      o.activate(activity = activity).failingMethod()
2472
      self.commit()
2473
      self.assertEqual(len(activity_tool.getMessageList()), 1)
2474
      self.flushAllActivities(silent = 1)
2475
      SiteErrorLog.raising = original_raising
2476
      self.commit()
2477 2478
      for log_record in self.logged:
        if log_record.name == 'ActivityTool' and log_record.levelname == 'WARNING':
2479
          type, value, trace = log_record.exc_info
2480 2481
      self.assertTrue(activity_unit_test_error is value)
    finally:
2482 2483
      SiteErrorLog.raising = original_raising
      del Organisation.failingMethod
2484 2485
      self._ignore_log_errors()

2486 2487 2488 2489 2490 2491 2492 2493 2494 2495
  def test_124_userNotificationSavedOnEventLogWhenSiteErrorLoggerRaisesWithSQLJoblib(self):
    """
      Check that message not saved in site error logger is not lost
    """
    self.TryNotificationSavedOnEventLogWhenSiteErrorLoggerRaises('SQLJoblib')

  def test_125_userNotificationSavedOnEventLogWhenSiteErrorLoggerRaisesWithSQLDict(self):
    """
      Check that message not saved in site error logger is not lost'
    """
2496 2497
    self.TryNotificationSavedOnEventLogWhenSiteErrorLoggerRaises('SQLDict')

2498 2499 2500 2501 2502 2503 2504
  def test_125_userNotificationSavedOnEventLogWhenSiteErrorLoggerRaisesWithSQLJoblib(self):
    """
      Check that message not saved in site error logger is not lost'
    """
    self.TryNotificationSavedOnEventLogWhenSiteErrorLoggerRaises('SQLJoblib')

  def test_126_userNotificationSavedOnEventLogWhenSiteErrorLoggerRaisesWithSQLQueue(self):
2505
    self.TryNotificationSavedOnEventLogWhenSiteErrorLoggerRaises('SQLQueue')
2506

2507
  def test_127_checkConflictErrorAndNoRemainingActivities(self):
2508 2509 2510 2511
    """
    When an activity creates several activities, make sure that all newly
    created activities are not commited if there is ZODB Conflict error
    """
2512 2513
    from Products.CMFActivity.Activity import SQLBase
    MAX_MESSAGE_LIST_SIZE = SQLBase.MAX_MESSAGE_LIST_SIZE
2514
    try:
2515
      SQLBase.MAX_MESSAGE_LIST_SIZE = 1
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527
      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})
2528

2529 2530
      activity_tool.__class__.doSomething = doSomething
      activity_tool.activate(activity='SQLQueue').doSomething()
2531
      self.commit()
2532 2533
      activity_tool.tic()
      message_list = activity_tool.getMessageList()
2534
      self.assertEqual(['doSomething'],[x.method_id for x in message_list])
2535
      activity_tool.manageClearActivities()
2536
    finally:
2537
      SQLBase.MAX_MESSAGE_LIST_SIZE = MAX_MESSAGE_LIST_SIZE
2538

2539
  def test_128_CheckDistributeWithSerializationTagAndGroupMethodId(self):
2540 2541 2542 2543 2544 2545
    activity_tool = self.portal.portal_activities
    obj1 = activity_tool.newActiveProcess()
    obj2 = activity_tool.newActiveProcess()
    self.tic()
    group_method_call_list = []
    def doSomething(self, message_list):
2546 2547
      r = []
      for m in message_list:
2548
        m.result = r.append((m.object.getPath(), m.args, m.kw))
2549 2550
      r.sort()
      group_method_call_list.append(r)
2551 2552
    activity_tool.__class__.doSomething = doSomething
    try:
2553
      for activity in 'SQLDict', 'SQLQueue', 'SQLJoblib':
2554 2555 2556 2557
        activity_kw = dict(activity=activity, serialization_tag=self.id(),
                           group_method_id='portal_activities/doSomething')
        obj1.activate(**activity_kw).dummy(1, x=None)
        obj2.activate(**activity_kw).dummy(2, y=None)
2558
        self.commit()
2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569
        activity_tool.distribute()
        activity_tool.tic()
        self.assertEqual(group_method_call_list.pop(),
                         sorted([(obj1.getPath(), (1,), dict(x=None)),
                                 (obj2.getPath(), (2,), dict(y=None))]))
        self.assertFalse(group_method_call_list)
        self.assertFalse(activity_tool.getMessageList())
        obj1.activate(priority=2, **activity_kw).dummy1(1, x=None)
        obj1.activate(priority=1, **activity_kw).dummy2(2, y=None)
        message1 = obj1.getPath(), (1,), dict(x=None)
        message2 = obj1.getPath(), (2,), dict(y=None)
2570
        self.commit()
2571 2572 2573 2574 2575
        activity_tool.distribute()
        self.assertEqual(len(activity_tool.getMessageList()), 2)
        activity_tool.tic()
        self.assertEqual(group_method_call_list.pop(),
                         dict(SQLDict=[message2],
2576 2577
                              SQLQueue=[message1, message2],
                              SQLJoblib=[message2])[activity])
2578 2579 2580 2581 2582
        self.assertFalse(group_method_call_list)
        self.assertFalse(activity_tool.getMessageList())
    finally:
      del activity_tool.__class__.doSomething

2583
  def test_129_beforeCommitHook(self):
2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594
    """
    Check it is possible to activate an object from a before commit hook
    """
    def doSomething(person):
      person.activate(activity='SQLDict')._setFirstName('John')
      person.activate(activity='SQLQueue')._setLastName('Smith')
    person = self.portal.person_module.newContent()
    transaction.get().addBeforeCommitHook(doSomething, (person,))
    self.tic()
    self.assertEqual(person.getTitle(), 'John Smith')

2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
  def test_connection_migration(self):
    """
    Make sure the cmf_activity_sql_connection is automatically migrated from
    the ZMySQLDA Connection class to ActivityConnection
    """
    # replace the activity connector with a standard ZMySQLDA one
    portal = self.portal
    activity_tool = portal.portal_activities
    stdconn = self.portal.cmf_activity_sql_connection
    portal._delObject('cmf_activity_sql_connection')
    portal.manage_addProduct['ZMySQLDA'].manage_addZMySQLConnection(
        stdconn.id,
        stdconn.title,
        stdconn.connection_string,
    )
    oldconn = portal.cmf_activity_sql_connection
2611
    self.assertEqual(oldconn.meta_type, 'Z MySQL Database Connection')
2612
    # force rebootstrap and check that migration of the connection happens
2613
    # automatically
2614 2615 2616
    from Products.ERP5Type.dynamic import portal_type_class
    portal_type_class._bootstrapped.clear()
    portal_type_class.synchronizeDynamicModules(activity_tool, True)
2617 2618 2619
    activity_tool.activate(activity='SQLQueue').getId()
    self.tic()
    newconn = portal.cmf_activity_sql_connection
2620
    self.assertEqual(newconn.meta_type, 'CMFActivity Database Connection')
2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640

  def test_connection_installable(self):
    """
    Test if the cmf_activity_sql_connector can be installed
    """
    # delete the activity connection
    portal = self.portal
    activity_tool = portal.portal_activities
    stdconn = self.portal.cmf_activity_sql_connection
    portal._delObject('cmf_activity_sql_connection')
    # check the installation form can be rendered
    portal.manage_addProduct['CMFActivity'].connectionAdd(
        portal.REQUEST
    )
    # check it can be installed
    portal.manage_addProduct['CMFActivity'].manage_addActivityConnection(
        stdconn.id,
        stdconn.title,
        stdconn.connection_string
    )
2641
    newconn = portal.cmf_activity_sql_connection
2642
    self.assertEqual(newconn.meta_type, 'CMFActivity Database Connection')
2643

2644 2645 2646 2647 2648 2649 2650 2651 2652
  def test_connection_sortkey(self):
    """
    Check that SQL connection has properly initialized sort key,
    even when its container (ZODB connection) is reused by another thread.
    """
    def sortKey():
      app = ZopeTestCase.app()
      try:
        c = app[self.getPortalName()].cmf_activity_sql_connection()
2653
        return app._p_jar, c.sortKey()
2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665
      finally:
        ZopeTestCase.close(app)
    jar, sort_key = sortKey()
    self.assertNotEqual(1, sort_key)
    result = []
    t = threading.Thread(target=lambda: result.extend(sortKey()))
    t.daemon = True
    t.start()
    t.join()
    self.assertTrue(result[0] is jar)
    self.assertEqual(result[1], sort_key)

2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687
  def test_onErrorCallback(self):
    activity_tool = self.portal.portal_activities
    obj = activity_tool.newActiveProcess()
    self.tic()
    def _raise(exception): # I wish exceptions are callable raising themselves
      raise exception
    def doSomething(self, conflict_error, cancel):
      self.activity_count += 1
      error = ConflictError() if conflict_error else Exception()
      def onError(exc_type, exc_value, traceback):
        assert exc_value is error
        env = self.getActivityRuntimeEnvironment()
        weakref_list.extend(map(weakref.ref, (env, env._message)))
        self.on_error_count += 1
        return cancel
      self.getActivityRuntimeEnvironment().edit(on_error_callback=onError)
      if not self.on_error_count:
        if not conflict_error:
          raise error
        transaction.get().addBeforeCommitHook(_raise, (error,))
    obj.__class__.doSomething = doSomething
    try:
2688
      for activity in 'SQLDict', 'SQLQueue', 'SQLJoblib':
2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
        for conflict_error in False, True:
          weakref_list = []
          obj.activity_count = obj.on_error_count = 0
          obj.activate(activity=activity).doSomething(conflict_error, True)
          self.tic()
          self.assertEqual(obj.activity_count, 0)
          self.assertEqual(obj.on_error_count, 1)
          gc.collect()
          self.assertEqual([x() for x in weakref_list], [None, None])
          weakref_list = []
          obj.activate(activity=activity).doSomething(conflict_error, False)
          obj.on_error_count = 0
          self.tic()
          self.assertEqual(obj.activity_count, 1)
          self.assertEqual(obj.on_error_count, 1)
          gc.collect()
          self.assertEqual([x() for x in weakref_list], [None, None])
    finally:
      del obj.__class__.doSomething

2709 2710 2711 2712
  def test_duplicateGroupedMessage(self):
    activity_tool = self.portal.portal_activities
    obj = activity_tool.newActiveProcess()
    obj.reindexObject(activate_kw={'tag': 'foo', 'after_tag': 'bar'})
2713
    self.commit()
2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725
    invoked = []
    def invokeGroup(self, *args):
      invoked.append(len(args[1]))
      return ActivityTool_invokeGroup(self, *args)
    ActivityTool_invokeGroup = activity_tool.__class__.invokeGroup
    try:
      activity_tool.__class__.invokeGroup = invokeGroup
      self.tic()
    finally:
      activity_tool.__class__.invokeGroup = ActivityTool_invokeGroup
    self.assertEqual(invoked, [1])

2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741
  def test_mergeParent(self):
    category_tool = self.portal.portal_categories
    # Test data:     c0
    #               /  \
    #             c1    c2
    #            /  \   |
    #           c3  c4  c5
    c = [category_tool.newContent()]
    for i in xrange(5):
      c.append(c[i//2].newContent())
    self.tic()
    def activate(i, priority=1, **kw):
      kw.setdefault('merge_parent', c[0].getPath())
      c[i].activate(priority=priority, **kw).doSomething()
    def check(*expected):
      self.tic()
2742
      self.assertEqual(tuple(invoked), expected)
2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768
      del invoked[:]
    invoked = []
    def doSomething(self):
      invoked.append(c.index(self))
    Base.doSomething = doSomething
    try:
      for t in (0, 1), (0, 4, 2), (1, 0, 5), (3, 2, 0):
        for p, i in enumerate(t):
          activate(i, p)
        check(0)
      activate(1, 0); activate(5, 1); check(1, 5)
      activate(3, 0); activate(1, 1); check(1)
      activate(2, 0); activate(1, 1); activate(4, 2); check(2, 1)
      activate(4, 0); activate(5, 1); activate(3, 2); check(4, 5, 3)
      activate(3, 0, merge_parent=c[1].getPath()); activate(0, 1); check(3, 0)
      # Following test shows that a child can be merged with a parent even if
      # 'merge_parent' is not specified. This can't be avoided without loading
      # all found duplicates, which would be bad for performance.
      activate(0, 0); activate(4, 1, merge_parent=None); check(0)
    finally:
      del Base.doSomething
    def activate(i, priority=1, **kw):
      c[i].activate(group_method_id='portal_categories/invokeGroup',
                    merge_parent=c[(i-1)//2 or i].getPath(),
                    priority=priority, **kw).doSomething()
    def invokeGroup(self, message_list):
2769 2770
      r = []
      for m in message_list:
2771
        m.result = r.append(c.index(m.object))
2772 2773
      r.sort()
      invoked.append(r)
2774 2775
    category_tool.__class__.invokeGroup = invokeGroup
    try:
2776
      activate(5, 0); activate(1, 1); check([1, 5])
2777 2778 2779 2780 2781 2782 2783 2784
      activate(4, 0); activate(1, 1); activate(2, 0); check([1, 2])
      activate(1, 0); activate(5, 0); activate(3, 1); check([1, 5])
      for p, i in enumerate((5, 3, 2, 1, 4)):
        activate(i, p, group_id=str(2 != i != 5))
      check([2], [1])
      for cost in 0.3, 0.1:
        activate(2, 0, group_method_cost=cost)
        activate(3, 1);  activate(4, 2); activate(1, 3)
2785
        check([1, 2])
2786 2787 2788 2789 2790
    finally:
      del category_tool.__class__.invokeGroup
    category_tool._delObject(c[0].getId())
    self.tic()

2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811
  def test_getMessageList(self):
    activity_tool = self.portal.portal_activities
    module = self.portal.person_module
    module.activate(after_tag="foo").getUid()
    module.activate(activity='SQLQueue', tag="foo").getId()
    activity_tool.activate(priority=-1).getId()
    def check(expected, **kw):
      self.assertEqual(expected, len(activity_tool.getMessageList(**kw)))
    def test(check=lambda _, **kw: check(0, **kw)):
      check(2, path=module.getPath())
      check(3, method_id=("getId", "getUid"))
      check(1, tag="foo")
      check(0, tag="foo", method_id="getUid")
      check(1, processing_node=-1)
      check(3, processing_node=range(-5,5))
    test()
    self.commit()
    test(check)
    self.tic()
    test()

2812 2813 2814 2815 2816 2817 2818
  def test_MessageNonExecutable(self):
    message_list = self.portal.MailHost._message_list
    del message_list[:]
    activity_tool = self.portal.portal_activities
    kw = {}
    self._catch_log_errors(subsystem='CMFActivity')
    try:
2819
      for kw['activity'] in 'SQLDict', 'SQLQueue', 'SQLJoblib':
2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845
        for kw['group_method_id'] in '', None:
          obj = activity_tool.newActiveProcess()
          self.tic()
          obj.activate(**kw).getId()
          activity_tool._delOb(obj.getId())
          obj = activity_tool.newActiveProcess(id=obj.getId(),
                                               is_indexable=False)
          self.commit()
          self.assertEqual(1, activity_tool.countMessage())
          self.flushAllActivities()
          sender, recipients, mail = message_list.pop()
          self.assertTrue('OID mismatch' in mail, mail)
          m, = activity_tool.getMessageList()
          self.assertEqual(m.processing_node, INVOKE_ERROR_STATE)
          obj.flushActivity()
          obj.activate(**kw).getId()
          activity_tool._delOb(obj.getId())
          self.commit()
          self.assertEqual(1, activity_tool.countMessage())
          activity_tool.tic()
          self.assertTrue('no object found' in self.logged.pop().getMessage())
    finally:
      self._ignore_log_errors()
    self.assertFalse(self.logged)
    self.assertFalse(message_list, message_list)

2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858
  def test_activateByPath(self):
    portal = self.getPortal()
    organisation_module = self.getOrganisationModule()
    if not organisation_module.hasContent(self.company_id):
      organisation_module.newContent(id=self.company_id)
      self.tic()
    organisation = organisation_module._getOb(self.company_id)
    portal.portal_activities.activateObject(
      organisation.getPath(),
      activity='SQLDict',
      active_process=None
      ).getTitle()
    self.tic()
2859

2860 2861 2862 2863 2864 2865 2866 2867 2868 2869
  def test_activateOnZsqlBrain(self):
    portal = self.getPortal()
    organisation_module = self.getOrganisationModule()
    if not organisation_module.hasContent(self.company_id):
      organisation_module.newContent(id=self.company_id)
      self.tic()
    organisation = organisation_module.searchFolder(id=self.company_id)[0]
    organisation.activate().getTitle()
    self.tic()

2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894
  def test_flushActivitiesOnDelete(self):
    organisation_module = self.getOrganisationModule()
    if not organisation_module.hasContent(self.company_id):
      organisation_module.newContent(id=self.company_id)
      self.tic()

    organisation = organisation_module[self.company_id]
    organisation_module.manage_delObjects(ids=[organisation.getId()])
    organisation.activate().getTitle()
    self.tic()

  def test_flushActivitiesOnDeleteWithAcquierableObject(self):
    organisation_module = self.getOrganisationModule()
    if not organisation_module.hasContent(self.company_id):
      organisation_module.newContent(id=self.company_id)
      self.tic()

    # Create an object with the same ID that can be acquired
    self.portal._setObject(self.company_id, Organisation(self.company_id))

    organisation = organisation_module[self.company_id]
    organisation_module.manage_delObjects(ids=[organisation.getId()])
    organisation.reindexObject()
    self.tic()

2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928
  def test_failingGroupMethod(self):
    activity_tool = self.portal.portal_activities
    obj = activity_tool.newActiveProcess()
    self.tic()
    obj.x = 1
    def doSomething(self):
      self.x %= self.x
    obj.__class__.doSomething = doSomething
    try:
      activity_kw = dict(activity="SQLQueue", group_method_id=None)
      obj.activate(**activity_kw).doSomething()
      obj.activate(**activity_kw).doSomething()
      obj.activate(**activity_kw).doSomething()
      self.commit()
      self.assertEqual(3, len(activity_tool.getMessageList()))
      activity_tool.tic()
      self.assertEqual(obj.x, 0)
      skipped, failed = activity_tool.getMessageList()
      self.assertEqual(0, skipped.retry)
      self.assertEqual(1, failed.retry)
      obj.x = 1
      self.commit()
      activity_tool.timeShift(VALIDATION_ERROR_DELAY)
      activity_tool.tic()
      m, = activity_tool.getMessageList()
      self.assertEqual(1, failed.retry)
      obj.x = 1
      self.commit()
      activity_tool.timeShift(VALIDATION_ERROR_DELAY)
      activity_tool.tic()
      self.assertFalse(activity_tool.getMessageList())
    finally:
      del obj.__class__.doSomething

2929 2930 2931
  def test_restrictedGroupMethod(self):
    skin = self.portal.portal_skins.custom
    script_id = self.id()
2932
    script = createZODBPythonScript(skin, script_id, "message_list", """if 1:
2933 2934 2935
      for m in message_list:
        m.result = m.object.getProperty(*m.args, **m.kw)
    """)
2936
    script.manage_proxy(("Manager",))
2937 2938
    obj = self.portal.portal_activities.newActiveProcess(causality_value_list=(
      self.portal.person_module, self.portal.organisation_module))
2939 2940
    obj.manage_permission('Access contents information', ['Manager'])
    self.logout()
2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951
    foo = obj.activate(activity='SQLQueue',
                       group_method_id=script_id,
                       active_process=obj.getPath()).foo
    foo('causality', portal_type='Organisation Module')
    foo('stop_date', 'bar')
    self.tic()
    self.assertEqual(sorted(x.getResult() for x in obj.getResultList()),
                     ['bar', 'organisation_module'])
    skin.manage_delObjects([script_id])
    self.tic()

Jérome Perrin's avatar
Jérome Perrin committed
2952 2953 2954 2955
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestCMFActivity))
  return suite
Sebastien Robin's avatar
Sebastien Robin committed
2956