m Ec@sIdZdkZdkZdkZdklZdklZeidde e dei fdYZ dfd YZ d efd YZd fd YZdefdYZdefdYZdefdYZdZdZdZdZdZdZe djoeiiendS(sTest transaction behavior for variety of cases. I wrote these unittests to investigate some odd transaction behavior when doing unittests of integrating non sub transaction aware objects, and to insure proper txn behavior. these tests test the transaction system independent of the rest of the zodb. you can see the method calls to a jar by passing the keyword arg tracing to the modify method of a dataobject. the value of the arg is a prefix used for tracing print calls to that objects jar. the number of times a jar method was called can be inspected by looking at an attribute of the jar that is the method name prefixed with a c (count/check). i've included some tracing examples for tests that i thought were illuminating as doc strings below. TODO add in tests for objects which are modified multiple times, for example an object that gets modified in multiple sub txns. $Id: test_transaction.py 41164 2006-01-05 21:12:02Z anguenot $ N(s positive_id(s WarningsHooktignores!.* subtransactions are deprecatedtTransactionTestscBsttZdZdZdZdZdZdZdZdZ d Z d Z d Z d Z RS( NcCsYti}|_t||_t||_t||_t|dd|_ dS(Ntnosti( t transactiontTransactionManagertmgrtselfttransaction_managert DataObjecttsub1tsub2tsub3tnosub1(RR((tA/data/zmath/zope/lib/python/transaction/tests/test_transaction.pytsetUp9s cCse|ii|ii|ii|iiidjpt|iii djptdS(Nii( RR tmodifyR Rtcommitt_p_jart ccommit_subtAssertionErrort ctpc_finish(R((R ttestTransactionCommitCs    cCsH|ii|ii|ii|iiidjptdS(Ni( RR RR RtabortRtcabortR(R((R ttestTransactionAbortMs   cCs]|ii}|id|i|id|id|i|id|idS(NsThis is a note.sAnother.sThis is a note. Another.(RRtgettttnotet assertEqualt descriptionR(RR((R ttestTransactionNoteVs   cCs;|ii|ii|iiidjptdS(Ni(RR RRRRRR(R((R ttestNSJTransactionCommitds  cCsX|ii|ii|iiidjpt|iiidjptdS(Nii( RR RRRRRRR(R((R ttestNSJTransactionAbortls  cCs|iidd|iidd|iid|iiidjpt|iiidjpt|ii |iii djpt|iii djptdS(s this reveals a bug in transaction.py the nosub jar should not have tpc_finish called on it till the containing txn ends. sub calling method commit nosub calling method tpc_begin sub calling method tpc_finish nosub calling method tpc_finish nosub calling method abort sub calling method abort_sub ttracingtsubtnosubiiN( RR RR RRRRRRRt cabort_sub(R((R t#BUGtestNSJSubTransactionCommitAbortus  cCstdd|i_|ii|iidd|iiy|iiWnt j onX|iii djpt |iii djpt dS(NterrorsRtnojari( tSubTransactionJarRR RR RR RRtTestTxnExceptionRR(R((R ttestExceptionInAborts  cCstdd|i_|ii|iiddy|iiWntj onX|iii djpt |iii djpt |iii djpt dS(NR&RR'ii( R(RR RR RRRR)RRtccommitt ctpc_abort(R((R ttestExceptionInCommits cCstdd|i_|ii|iiddy|iiWntj onX|iii djpt |iii djpt |iii djpt |iii djpt dS(NR&ttpc_voteR'ii( R(RR RR RRRR)RRR+R,(R((R ttestExceptionInTpcVotes cCstdd|i_|ii|iiddy|iiWntj onX|iii djpt |iii djpt dS(sH ok this test reveals a bug in the TM.py as the nosub tpc_abort there is ignored. nosub calling method tpc_begin nosub calling method commit sub calling method tpc_begin sub calling method abort sub calling method tpc_abort nosub calling method tpc_abort R&t tpc_beginR'iN( R(RR RR RRRR)R,R(R((R ttestExceptionInTpcBegins  cCs}tdd|i_|ii|iiddy|iiWntj onX|iii djpt dS(NR&t tpc_abortR.R'i(s tpc_abortstpc_vote( R(RR RR RRRR)R,R(R((R ttestExceptionInTpcAborts (t__name__t __module__RRRRRR R%R*R-R/R1R3(((R R7s   *    RcBs#tZddZdddZRS(NicCs||_||_d|_dS(N(RRRtNoneR(RRR((R t__init__s  cCsZ|p6|iotd||_q=td||_n|iii |idS(NR!( R'RRtNoSubTransactionJarR!RR(RRtjoin(RR'R!((R Rs  (R4R5R7R(((R Rs R)cBstZRS(N(R4R5(((R R)#stBasicJarcBsktZfddZdZdZdZdZdZddZd Z d Z d Z RS( NicCs{t|tp |f}n||_||_d|_d|_d|_d|_d|_ d|_ d|_ d|_ dS(Ni( t isinstanceR&ttupleRR!RR+t ctpc_beginR,t ctpc_voteRR$R(RR&R!((R R7(s          cCs d|iit||ifS(Ns <%s %X %s>(Rt __class__R4t positive_idR&(R((R t__repr__6scCs |iiS(N(RR?R4(R((R tsortKey;scCsN|iodt|i|fGHn||ijotd|ndS(Ns%s calling method %sserror %s(RR!tstrtmethodR&R)(RRD((R tcheck@s cGs |id|id7_dS(NRi(RRER(Rtargs((R RIs cGs |id|id7_dS(NRi(RRER+(RRF((R RMs cCs |id|id7_dS(NR0i(RRER=(RttxnR"((R R0Qs cGs |id|id7_dS(NR.i(RRER>(RRF((R R.Us cGs |id|id7_dS(NR2i(RRER,(RRF((R R2Ys cGs |id|id7_dS(Nt tpc_finishi(RRER(RRF((R RH]s ( R4R5R7RARBRERRR0R.R2RH(((R R:&s       R(cBstZdZdZRS(NcCs|idd|_dS(Nt abort_subi(RRER$(RRG((R RIcs cCs|idd|_dS(Nt commit_subi(RRER(RRG((R RJgs (R4R5RIRJ(((R R(as R8cBstZRS(N(R4R5(((R R8kstHoserJarcBs)tZdZdZdZdZRS(NicCs dt_dS(Ni(RKt committed(R((R tresetvscCs(tidjoti||ndS(Ni(RKRLR:RERRD(RRD((R REzscGs/|id|id7_tid7_dS(NRHi(RRERRKRL(RRF((R RH~s (R4R5RLRMRERH(((R RKns  cCsdS(sWhite-box test of the join method The join method is provided for "backward-compatability" with ZODB 4 data managers. The argument to join must be a zodb4 data manager, transaction.interfaces.IDataManager. >>> from ZODB.tests.sampledm import DataManager >>> from transaction._transaction import DataManagerAdapter >>> t = transaction.Transaction() >>> dm = DataManager() >>> t.join(dm) The end result is that a data manager adapter is one of the transaction's objects: >>> isinstance(t._resources[0], DataManagerAdapter) True >>> t._resources[0]._datamanager is dm True N((((R t test_joinscCsdS(N((((R thookscCsdS(s1Test beforeCommitHook. Let's define a hook to call, and a way to see that it was called. >>> log = [] >>> def reset_log(): ... del log[:] >>> def hook(arg='no_arg', kw1='no_kw1', kw2='no_kw2'): ... log.append("arg %r kw1 %r kw2 %r" % (arg, kw1, kw2)) beforeCommitHook is deprecated, so we need cruft to suppress the warnings. >>> whook = WarningsHook() >>> whook.install() Fool the warnings module into delivering the warnings despite that they've been seen before; this is needed in case this test is run more than once. >>> import warnings >>> warnings.filterwarnings("always", category=DeprecationWarning) Now register the hook with a transaction. >>> import transaction >>> t = transaction.begin() >>> t.beforeCommitHook(hook, '1') Make sure it triggered a deprecation warning: >>> len(whook.warnings) 1 >>> message, category, filename, lineno = whook.warnings[0] >>> print message This will be removed in ZODB 3.8: Use addBeforeCommitHook instead of beforeCommitHook. >>> category.__name__ 'DeprecationWarning' >>> whook.clear() We can see that the hook is indeed registered. >>> [(hook.func_name, args, kws) ... for hook, args, kws in t.getBeforeCommitHooks()] [('hook', ('1',), {})] When transaction commit starts, the hook is called, with its arguments. >>> log [] >>> t.commit() >>> log ["arg '1' kw1 'no_kw1' kw2 'no_kw2'"] >>> reset_log() A hook's registration is consumed whenever the hook is called. Since the hook above was called, it's no longer registered: >>> len(list(t.getBeforeCommitHooks())) 0 >>> transaction.commit() >>> log [] The hook is only called for a full commit, not for a savepoint or subtransaction. >>> t = transaction.begin() >>> t.beforeCommitHook(hook, 'A', kw1='B') >>> dummy = t.savepoint() >>> log [] >>> t.commit(subtransaction=True) >>> log [] >>> t.commit() >>> log ["arg 'A' kw1 'B' kw2 'no_kw2'"] >>> reset_log() If a transaction is aborted, no hook is called. >>> t = transaction.begin() >>> t.beforeCommitHook(hook, "OOPS!") >>> transaction.abort() >>> log [] >>> transaction.commit() >>> log [] The hook is called before the commit does anything, so even if the commit fails the hook will have been called. To provoke failures in commit, we'll add failing resource manager to the transaction. >>> class CommitFailure(Exception): ... pass >>> class FailingDataManager: ... def tpc_begin(self, txn, sub=False): ... raise CommitFailure ... def abort(self, txn): ... pass >>> t = transaction.begin() >>> t.join(FailingDataManager()) >>> t.beforeCommitHook(hook, '2') >>> t.commit() Traceback (most recent call last): ... CommitFailure >>> log ["arg '2' kw1 'no_kw1' kw2 'no_kw2'"] >>> reset_log() Let's register several hooks. >>> t = transaction.begin() >>> t.beforeCommitHook(hook, '4', kw1='4.1') >>> t.beforeCommitHook(hook, '5', kw2='5.2') They are returned in the same order by getBeforeCommitHooks. >>> [(hook.func_name, args, kws) #doctest: +NORMALIZE_WHITESPACE ... for hook, args, kws in t.getBeforeCommitHooks()] [('hook', ('4',), {'kw1': '4.1'}), ('hook', ('5',), {'kw2': '5.2'})] And commit also calls them in this order. >>> t.commit() >>> len(log) 2 >>> log #doctest: +NORMALIZE_WHITESPACE ["arg '4' kw1 '4.1' kw2 'no_kw2'", "arg '5' kw1 'no_kw1' kw2 '5.2'"] >>> reset_log() While executing, a hook can itself add more hooks, and they will all be called before the real commit starts. >>> def recurse(txn, arg): ... log.append('rec' + str(arg)) ... if arg: ... txn.beforeCommitHook(hook, '-') ... txn.beforeCommitHook(recurse, txn, arg-1) >>> t = transaction.begin() >>> t.beforeCommitHook(recurse, t, 3) >>> transaction.commit() >>> log #doctest: +NORMALIZE_WHITESPACE ['rec3', "arg '-' kw1 'no_kw1' kw2 'no_kw2'", 'rec2', "arg '-' kw1 'no_kw1' kw2 'no_kw2'", 'rec1', "arg '-' kw1 'no_kw1' kw2 'no_kw2'", 'rec0'] >>> reset_log() We have to uninstall the warnings hook so that other warnings don't get lost. >>> whook.uninstall() Obscure: There is no API call for removing the filter we added, but filters appears to be a public variable. >>> del warnings.filters[0] N((((R ttest_beforeCommitHookscCsdS(sTest addBeforeCommitHook. Let's define a hook to call, and a way to see that it was called. >>> log = [] >>> def reset_log(): ... del log[:] >>> def hook(arg='no_arg', kw1='no_kw1', kw2='no_kw2'): ... log.append("arg %r kw1 %r kw2 %r" % (arg, kw1, kw2)) Now register the hook with a transaction. >>> import transaction >>> t = transaction.begin() >>> t.addBeforeCommitHook(hook, '1') We can see that the hook is indeed registered. >>> [(hook.func_name, args, kws) ... for hook, args, kws in t.getBeforeCommitHooks()] [('hook', ('1',), {})] When transaction commit starts, the hook is called, with its arguments. >>> log [] >>> t.commit() >>> log ["arg '1' kw1 'no_kw1' kw2 'no_kw2'"] >>> reset_log() A hook's registration is consumed whenever the hook is called. Since the hook above was called, it's no longer registered: >>> len(list(t.getBeforeCommitHooks())) 0 >>> transaction.commit() >>> log [] The hook is only called for a full commit, not for a savepoint or subtransaction. >>> t = transaction.begin() >>> t.addBeforeCommitHook(hook, 'A', dict(kw1='B')) >>> dummy = t.savepoint() >>> log [] >>> t.commit(subtransaction=True) >>> log [] >>> t.commit() >>> log ["arg 'A' kw1 'B' kw2 'no_kw2'"] >>> reset_log() If a transaction is aborted, no hook is called. >>> t = transaction.begin() >>> t.addBeforeCommitHook(hook, ["OOPS!"]) >>> transaction.abort() >>> log [] >>> transaction.commit() >>> log [] The hook is called before the commit does anything, so even if the commit fails the hook will have been called. To provoke failures in commit, we'll add failing resource manager to the transaction. >>> class CommitFailure(Exception): ... pass >>> class FailingDataManager: ... def tpc_begin(self, txn, sub=False): ... raise CommitFailure ... def abort(self, txn): ... pass >>> t = transaction.begin() >>> t.join(FailingDataManager()) >>> t.addBeforeCommitHook(hook, '2') >>> t.commit() Traceback (most recent call last): ... CommitFailure >>> log ["arg '2' kw1 'no_kw1' kw2 'no_kw2'"] >>> reset_log() Let's register several hooks. >>> t = transaction.begin() >>> t.addBeforeCommitHook(hook, '4', dict(kw1='4.1')) >>> t.addBeforeCommitHook(hook, '5', dict(kw2='5.2')) They are returned in the same order by getBeforeCommitHooks. >>> [(hook.func_name, args, kws) #doctest: +NORMALIZE_WHITESPACE ... for hook, args, kws in t.getBeforeCommitHooks()] [('hook', ('4',), {'kw1': '4.1'}), ('hook', ('5',), {'kw2': '5.2'})] And commit also calls them in this order. >>> t.commit() >>> len(log) 2 >>> log #doctest: +NORMALIZE_WHITESPACE ["arg '4' kw1 '4.1' kw2 'no_kw2'", "arg '5' kw1 'no_kw1' kw2 '5.2'"] >>> reset_log() While executing, a hook can itself add more hooks, and they will all be called before the real commit starts. >>> def recurse(txn, arg): ... log.append('rec' + str(arg)) ... if arg: ... txn.addBeforeCommitHook(hook, '-') ... txn.addBeforeCommitHook(recurse, (txn, arg-1)) >>> t = transaction.begin() >>> t.addBeforeCommitHook(recurse, (t, 3)) >>> transaction.commit() >>> log #doctest: +NORMALIZE_WHITESPACE ['rec3', "arg '-' kw1 'no_kw1' kw2 'no_kw2'", 'rec2', "arg '-' kw1 'no_kw1' kw2 'no_kw2'", 'rec1', "arg '-' kw1 'no_kw1' kw2 'no_kw2'", 'rec0'] >>> reset_log() When modifing persitent objects within before commit hooks modifies the objects, of course :) Start a new transaction >>> t = transaction.begin() Create a DB instance and add a IOBTree within >>> from ZODB.tests.util import DB >>> from ZODB.tests.util import P >>> db = DB() >>> con = db.open() >>> root = con.root() >>> root['p'] = P('julien') >>> p = root['p'] >>> p.name 'julien' This hook will get the object from the `DB` instance and change the flag attribute. >>> def hookmodify(status, arg=None, kw1='no_kw1', kw2='no_kw2'): ... p.name = 'jul' Now register this hook and commit. >>> t.addBeforeCommitHook(hookmodify, (p, 1)) >>> transaction.commit() Nothing should have changed since it should have been aborted. >>> p.name 'jul' >>> db.close() N((((R ttest_addBeforeCommitHookPscCsdS(sTest addAfterCommitHook. Let's define a hook to call, and a way to see that it was called. >>> log = [] >>> def reset_log(): ... del log[:] >>> def hook(status, arg='no_arg', kw1='no_kw1', kw2='no_kw2'): ... log.append("%r arg %r kw1 %r kw2 %r" % (status, arg, kw1, kw2)) Now register the hook with a transaction. >>> import transaction >>> t = transaction.begin() >>> t.addAfterCommitHook(hook, '1') We can see that the hook is indeed registered. >>> [(hook.func_name, args, kws) ... for hook, args, kws in t.getAfterCommitHooks()] [('hook', ('1',), {})] When transaction commit is done, the hook is called, with its arguments. >>> log [] >>> t.commit() >>> log ["True arg '1' kw1 'no_kw1' kw2 'no_kw2'"] >>> reset_log() A hook's registration is consumed whenever the hook is called. Since the hook above was called, it's no longer registered: >>> len(list(t.getAfterCommitHooks())) 0 >>> transaction.commit() >>> log [] The hook is only called after a full commit, not for a savepoint or subtransaction. >>> t = transaction.begin() >>> t.addAfterCommitHook(hook, 'A', dict(kw1='B')) >>> dummy = t.savepoint() >>> log [] >>> t.commit(subtransaction=True) >>> log [] >>> t.commit() >>> log ["True arg 'A' kw1 'B' kw2 'no_kw2'"] >>> reset_log() If a transaction is aborted, no hook is called. >>> t = transaction.begin() >>> t.addAfterCommitHook(hook, ["OOPS!"]) >>> transaction.abort() >>> log [] >>> transaction.commit() >>> log [] The hook is called after the commit is done, so even if the commit fails the hook will have been called. To provoke failures in commit, we'll add failing resource manager to the transaction. >>> class CommitFailure(Exception): ... pass >>> class FailingDataManager: ... def tpc_begin(self, txn, sub=False): ... raise CommitFailure ... def abort(self, txn): ... pass >>> t = transaction.begin() >>> t.join(FailingDataManager()) >>> t.addAfterCommitHook(hook, '2') >>> t.commit() Traceback (most recent call last): ... CommitFailure >>> log ["False arg '2' kw1 'no_kw1' kw2 'no_kw2'"] >>> reset_log() Let's register several hooks. >>> t = transaction.begin() >>> t.addAfterCommitHook(hook, '4', dict(kw1='4.1')) >>> t.addAfterCommitHook(hook, '5', dict(kw2='5.2')) They are returned in the same order by getAfterCommitHooks. >>> [(hook.func_name, args, kws) #doctest: +NORMALIZE_WHITESPACE ... for hook, args, kws in t.getAfterCommitHooks()] [('hook', ('4',), {'kw1': '4.1'}), ('hook', ('5',), {'kw2': '5.2'})] And commit also calls them in this order. >>> t.commit() >>> len(log) 2 >>> log #doctest: +NORMALIZE_WHITESPACE ["True arg '4' kw1 '4.1' kw2 'no_kw2'", "True arg '5' kw1 'no_kw1' kw2 '5.2'"] >>> reset_log() While executing, a hook can itself add more hooks, and they will all be called before the real commit starts. >>> def recurse(status, txn, arg): ... log.append('rec' + str(arg)) ... if arg: ... txn.addAfterCommitHook(hook, '-') ... txn.addAfterCommitHook(recurse, (txn, arg-1)) >>> t = transaction.begin() >>> t.addAfterCommitHook(recurse, (t, 3)) >>> transaction.commit() >>> log #doctest: +NORMALIZE_WHITESPACE ['rec3', "True arg '-' kw1 'no_kw1' kw2 'no_kw2'", 'rec2', "True arg '-' kw1 'no_kw1' kw2 'no_kw2'", 'rec1', "True arg '-' kw1 'no_kw1' kw2 'no_kw2'", 'rec0'] >>> reset_log() If an after commit hook is raising an exception then it will log a message at error level so that if other hooks are registered they can be executed. We don't support execution dependencies at this level. >>> mgr = transaction.TransactionManager() >>> do = DataObject(mgr) >>> def hookRaise(status, arg='no_arg', kw1='no_kw1', kw2='no_kw2'): ... raise TypeError("Fake raise") >>> t = transaction.begin() >>> t.addAfterCommitHook(hook, ('-', 1)) >>> t.addAfterCommitHook(hookRaise, ('-', 2)) >>> t.addAfterCommitHook(hook, ('-', 3)) >>> transaction.commit() >>> log ["True arg '-' kw1 1 kw2 'no_kw2'", "True arg '-' kw1 3 kw2 'no_kw2'"] >>> reset_log() Test that the associated transaction manager has been cleanup when after commit hooks are registered >>> mgr = transaction.TransactionManager() >>> do = DataObject(mgr) >>> t = transaction.begin() >>> len(t._manager._txns) 1 >>> t.addAfterCommitHook(hook, ('-', 1)) >>> transaction.commit() >>> log ["True arg '-' kw1 1 kw2 'no_kw2'"] >>> len(t._manager._txns) 0 >>> reset_log() The transaction is already committed when the after commit hooks will be executed. Executing the hooks must not have further effects on persistent objects. Start a new transaction >>> t = transaction.begin() Create a DB instance and add a IOBTree within >>> from ZODB.tests.util import DB >>> from ZODB.tests.util import P >>> db = DB() >>> con = db.open() >>> root = con.root() >>> root['p'] = P('julien') >>> p = root['p'] >>> p.name 'julien' This hook will get the object from the `DB` instance and change the flag attribute. >>> def badhook(status, arg=None, kw1='no_kw1', kw2='no_kw2'): ... p.name = 'jul' Now register this hook and commit. >>> t.addAfterCommitHook(badhook, (p, 1)) >>> transaction.commit() Nothing should have changed since it should have been aborted. >>> p.name 'julien' >>> db.close() N((((R ttest_addAfterCommitHookscCs,dkl}ti|titfS(N(s DocTestSuite(tzope.testing.doctestt DocTestSuitetunittestt TestSuitet makeSuiteR(RT((R t test_suites t__main__(t__doc__RUtwarningsRt ZODB.utilsR@tZODB.tests.warnhookt WarningsHooktfilterwarningstDeprecationWarningR4tTestCaseRRt ExceptionR)R:R(R8RKRNRORPRQRRRXtTextTestRunnertrun(RNRR:R[R)RURPR(RRORRRXRKRQR@RR^R8((R t?(s.       ;