Ϫf{=dZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl mZmZmZmZddlmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"ddl#m$Z$m%Z%m&Z&ddl'm(Z(ddl)m*Z*dd lm+Z+dd l,m-Z-m.Z.dd l/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;dd lm?Z?ddl@mAZAddlBmCZCddlDm Z  d?dZEGddeFZGd@dZHdAdZIe deFZJGddZKGdde jZMGdde jeKZOGddZPdBd ZQGd!d"e jZRGd#d$e jZSGd%d&e jZTGd'd(e jZUGd)d*e jZVGd+d,e jZWGd-d.e jeKZXGd/d0e jZYdCd1ZZGd2d3e jZ[Gd4d5e jZ\Gd6d7e jeKZ]dDd8Z^Gd9d:e jZ_Gd;de jZay)Ez+ Test cases for L{twisted.internet.defer}. ) annotationsN)AbstractEventLoopCancelledErrorFuturenew_event_loop)AnyCallable CoroutineDict GeneratorListMappingNoReturnOptionalSetTupleTypeTypeVarUnioncast) assert_thatemptyequal_to)given)integers) assert_type)deferreactor) _TDeferredDeferredFilesystemLock DeferredList DeferredLock DeferredQueueDeferredSemaphore FailureGroup_DeferredListResultListT_DeferredListSingleResultTensureDeferredrace)Clock)log)_PYPYFailure)unittestcDtjdfd }|S)Nc>|i|}tj|SN)r fromCoroutine)argskwargscorofs 9/usr/lib/python3/dist-packages/twisted/test/test_defer.pywrapperz!ensuringDeferred..wrapperIs#$!&!%%d++)r5objectr6r<returnz Deferred[_T]) functoolswraps)r8r:s` r9ensuringDeferredr@Fs'__Q,, Nr;c eZdZy) GenericErrorN__name__ __module__ __qualname__r;r9rBrBQsr;rBcH ddz S#t$rt|i|}Y|SwxYw)z Make a L{Failure} of a divide-by-zero error. @param args: Any C{*args} are passed to Failure's constructor. @param kwargs: Any C{**kwargs} are passed to Failure's constructor. r) BaseExceptionr/)r5r6r8s r9getDivisionFailurerKUs:% A H % T $V $ H%s  !!c&|jdy)z A fake L{Deferred} canceller which callbacks the L{Deferred} with C{str} "Callback Result" when cancelling it. @param deferred: The cancelled L{Deferred}. Callback ResultNcallbackdeferreds r9fakeCallbackCancellerrRcs '(r; _ExceptionT)boundc$eZdZdZ ddZy)ImmediateFailureMixinz+ Add additional assertion methods. c&ttj|}g}|j|j|j t |d|j|dj|tt|djS)z Assert that the given Deferred current result is a Failure with the given exception. @return: The exception instance in the Deferred. rIr) rr0TestCase addErrbackappend assertEquallen assertTruecheckrSvalue)selfrQ exceptiontestCasefailuress r9assertImmediateFailurez,ImmediateFailureMixin.assertImmediateFailureusu))40"$HOO,S]A.HQK--i89K!!2!233r;N)rQ Deferred[Any]razType[_ExceptionT]r=rS)rDrErF__doc__rdrGr;r9rVrVps$4%42C4 4r;rVc(eZdZdZddZddZddZy) UtilTestsz& Tests for utility functions. ctt}tj|}|j t|j ||y)z< L{defer.logError} returns the given error. N)r/ RuntimeErrorrlogErrorflushLoggedErrorsassertIs)r`errorresults r9test_logErrorReturnsErrorz#UtilTests.test_logErrorReturnsErrors: '& |, eV$r;ctt}tj||j t}|j ||gy)z9 L{defer.logError} logs the given error. N)r/rjrrkrlr[)r`rnerrorss r9test_logErrorLogsErrorz UtilTests.test_logErrorLogsErrors> ' u'' 5 %)r;cgdfd }tj|tt}t j ||j t|jdjdy)zR The text logged by L{defer.logError} has no repr of the failure. cZtj|}|Jj|yr3)r,textFromEventDictrZ) eventDicttextoutputs r9emitz4UtilTests.test_logErrorLogsErrorNoRepr..emits+((3D# ## MM$ r;rUnhandled Error Traceback N)rwzDict[str, Any]r=None) r, addObserverr/rjrrkrlr] startswith)r`rzrnrys @r9test_logErrorLogsErrorNoReprz&UtilTests.test_logErrorLogsErrorNoReprs_  ' u |, q ,,-JKLr;Nr=r|)rDrErFrfrprsrrGr;r9rhrhs%*Mr;rhc0eZdZdWdZdXdZdYdZdZdZdWdZdWdZdWdZ dWdZ dWd Z dWd Z dWd Z dWd ZdWd ZdWdZdWdZdWdZdWdZdWdZdWdZdWdZdWdZdWdZdWdZdWdZdWdZdWdZdWdZdWdZdWdZdWdZ dWdZ!dWd Z"dWd!Z#dWd"Z$dWd#Z%dWd$Z&dWd%Z'dWd&Z(dWd'Z)dWd(Z*dWd)Z+dWd*Z,dWd+Z-dWd,Z.dWd-Z/dWd.Z0dWd/Z1dWd0Z2dWd1Z3dWd2Z4dWd3Z5dWd4Z6dWd5Z7dWd6Z8dWd7Z9dWd8Z:dWd9Z;dWd:ZdWd=Z?dWd>Z@dWd?ZAdWd@ZBdWdAZCdWdBZDdWdCZEdWdDZFdWdEZGdWdFZHdWdGZIdWdHZJdWdIZKdWdJZLdWdKZMdWdLZNdWdMZOdWdNZPdWdOZQdWdPZRdWdQZSeTjeVdRdWdSZWeTjeVdRdWdTZXeTjeVdRdWdUZYyV)[ DeferredTestscd|_d|_d|_|jtj t j yr3)callbackResultscallback2ResultserrbackResults addCleanupr setDebugging getDebuggingr`s r9setUpzDeferredTests.setUpsH       **E,>,>,@Ar;c||f|_|dSNrrr`r5r6s r9 _callbackzDeferredTests._callbacks#V|Awr;c||f|_yr3rrs r9 _callback2zDeferredTests._callback2s $f r;c||f|_yr3rrs r9_errbackzDeferredTests._errbacks"Flr;ct}|j|j|jd|j |j |j |jdifyNhellorr addCallbackrrO assertIsNonerr[rr`rQs r9testCallbackWithoutArgsz%DeferredTests.testCallbackWithoutArgssX"**T^^,'" $--. -- B/?@r;ct}|j|jd|jd|j |j |j |jdify)Nworldr)rrrrs r9testCallbackWithArgsz"DeferredTests.testCallbackWithArgss["**T^^W5'" $--. --0BB/GHr;ct}|j|jd|jd|j |j |j |jdddify)Nr)rrrrrs r9testCallbackWithKwArgsz$DeferredTests.testCallbackWithKwArgssb"**T^^7;'" $--. -- Wg.cb)s MM* %r;cyr3rGerrs r9catchz-DeferredTests.testDeferredList..catch,r;123rrI)r ResultListrorr=r|rr/r=r|)r rboolrrr/r r"rrOrYrrBr[rr_rSUCCESSFAILURE) r`rdefr1defr2defr3dlrorrs r9testDeferredListzDeferredTests.testDeferredList s%eCL&9 9:; 'z'z'z 5%/ 0#D#DEFq   mmS !EMM3#7%--9M N r;cg}|f dd}tg}|j||j|ggg|ddtgd}|j||j|gy)Nc&|j|yr3rZrs r9rz/DeferredTests.testEmptyDeferredList..cbGs MM* %r;TfireOnOneCallback)rz_DeferredListResultListT[None]roz$List[_DeferredListResultListT[None]]r=r|)r"rr[)r`rordl1dl2s r9testEmptyDeferredListz#DeferredTests.testEmptyDeferredListDs79.catchbrr;r from def2rIrzFissubclass(aFailure.type, defer.FirstError) failed: failure's type is )rr)r r"rYrZrOr[rrBr\r] issubclasstyper FirstErrorr_ subFailurer5index) r`rrrrroraFailure firstErrors r9testDeferredListFireOnOneErrorz,DeferredTests.testDeferredListFireOnOneErrorXs3'z'z'z 5%/$ G " fmm$   s $  l;/0 Va(!9  x}}e&6&6 7&.mm 6 ^^  ..33\B ..4499>J ))1-r;ct}t|g}g}|j|jg}|j |j|j t d|jd|djjd|jdt||dd}|J|jd|djjdy)NBangrrI) r r"rYrZrrrBr[r_r5r\r`d1r errorTrap resultLists firstResults r9!testDeferredListDontConsumeErrorsz/DeferredTests.testDeferredListDontConsumeErrorss%Z 2$ #%  i&&'<>  {))* <'( 1!3!3!8!8!;< C ,-!!nQ' &&& Q!5!5!:!:1!=>r;ct}t|gd}g}|j|jg}|j |j|j t d|jg||jdt||dd}|J|jd|djjdy)NT consumeErrorsrrIr) r r"rYrZrrrBr[r\r_r5rs r9testDeferredListConsumeErrorsz+DeferredTests.testDeferredListConsumeErrorss%Z 2$d 3#%  i&&'<>  {))* <'( Y' C ,-!!nQ' &&& Q!5!5!:!:1!=>r;ct}t}|jtdt||gd}g}|j |j |j dt||j dy)NrTrrIcyr3rGes r9zWDeferredTests.testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds..r;)r rrBr"rYrZr[r\r`rd2rros r97testDeferredListFireOnOneErrorWithAlreadyFiredDeferredszEDeferredTests.testDeferredListFireOnOneErrorWithAlreadyFiredDeferredssh%Z%Z <'(2r(T : " fmm$ CK( n%r;c0t}t}|jtd|jdt ||g}g}|j |j |jdt||jdy)NrrrIcyr3rGrs r9rzIDeferredTests.testDeferredListWithAlreadyFiredDeferreds..rr;) r rrBrOr"rrZr[r\rYrs r9)testDeferredListWithAlreadyFiredDeferredsz7DeferredTests.testDeferredListWithAlreadyFiredDeferredssq$J$J <'( A2r( #/1 v}}% CK( n%r;ct}t}t||g}|j|j|tj |j|tj y)zk When cancelling an unfired L{DeferredList}, cancel every L{Deferred} in the list. Nr r"cancelfailureResultOfrrr` deferredOne deferredTwo deferredLists r9test_cancelDeferredListz%DeferredTests.test_cancelDeferredListsY '/j &.j #[+$>?  [%*>*>? [%*>*>?r;ctt}t}t||g}|j|j |t j |j|}|j|dd|j|ddd|j|dd|j|ddjt j y)z When cancelling an unfired L{DeferredList} without the C{fireOnOneCallback} and C{fireOnOneErrback} flags set, the L{DeferredList} will be callback with a C{list} of (success, result) C{tuple}s. rrIrMN) r rRr"rrrrsuccessResultOfr]r[ assertFalser^r`rrrros r9test_cancelDeferredListCallbackz-DeferredTests.test_cancelDeferredListCallbacks&..C%D %-Z #[+$>?  [%*>*>?%%l3 q ! % 1'89 1& q ! **5+?+?@Ar;ct}t}t||gd}|j|j|tj |j|tj y)z When cancelling an unfired L{DeferredList} with the flag C{fireOnOneCallback} set, cancel every L{Deferred} in the list. TrNrrs r9,test_cancelDeferredListWithFireOnOneCallbackz:DeferredTests.test_cancelDeferredListWithFireOnOneCallbacks\ '/j &.j #[+$>RVW  [%*>*>? [%*>*>?r;ctt}t}t||gd}|j|j |t j |j|}|j|dy)a When cancelling an unfired L{DeferredList} with the flag C{fireOnOneCallback} set, if one of the L{Deferred} callbacks in its canceller, the L{DeferredList} will callback with the result and the index of the L{Deferred} in a C{tuple}. Tr)rMrN) r rRr"rrrrrr[rs r9?test_cancelDeferredListWithFireOnOneCallbackAndDeferredCallbackzMDeferredTests.test_cancelDeferredListWithFireOnOneCallbackAndDeferredCallbacksk&..C%D %-Z #[+$>RVW  [%*>*>?%%l3 !78r;ct}t}t||gd}|j|j|tj |j|tj |j|tj }|j}|j|jjtj y)z When cancelling an unfired L{DeferredList} with the flag C{fireOnOneErrback} set, cancel every L{Deferred} in the list. TrN) r r"rrrrrr_r]rr^)r`rrrdeferredListFailurers r9+test_cancelDeferredListWithFireOnOneErrbackz9DeferredTests.test_cancelDeferredListWithFireOnOneErrbacks '/j &.j #[+$>QUV  [%*>*>? [%*>*>?"22<AQAQR(..   --33E4H4HIJr;c\tt}tt}t||gd}|j|j |}|j |dd|j |ddd|j |dd|j |dddy)z When cancelling an unfired L{DeferredList} with the flag C{fireOnOneErrback} set, if all the L{Deferred} callbacks in its canceller, the L{DeferredList} will callback with a C{list} of (success, result) C{tuple}s. TrrrIrMN)r rRr"rrr]r[rs r9?test_cancelDeferredListWithFireOnOneErrbackAllDeferredsCallbackzMDeferredTests.test_cancelDeferredListWithFireOnOneErrbackAllDeferredsCallback s&..C%D %-.C%D #[+$>QUV %%l3 q ! % 1'89 q ! % 1'89r;cBt}t}||g}t|}t}|j||j|j |t j |j |t j |j|y)zg Cancelling a L{DeferredList} will cancel the original L{Deferred}s passed in. N)r r"rZrrrrassertNoResult)r`rr argumentListr deferredThrees r9,test_cancelDeferredListWithOriginalDeferredsz:DeferredTests.test_cancelDeferredListWithOriginalDeferredss '/j &.j #[1 #L1 (0 M* [%*>*>? [%*>*>? M*r;cdd}t|}t}t||g}|j|j|tj |j t}|jt|dy)z Cancelling a L{DeferredList} will cancel every L{Deferred} in the list even exceptions raised from the C{cancel} method of the L{Deferred}s. ctd)z A L{Deferred} canceller that raises an exception. @param deferred: The cancelled L{Deferred}. test)rjrPs r9cancellerRaisesExceptionzTDeferredTests.test_cancelDeferredListWithException..cancellerRaisesException2s v& &r;rIN)rQDeferred[object]r=r|) r r"rrrrrlrjr[r\)r`rrrrrrs r9$test_cancelDeferredListWithExceptionz2DeferredTests.test_cancelDeferredListWithException+sp ''//G&H &.j #[+$>?  [%*>*>?'' 5 Va(r;ct}t}t||gd}|jd|j|j |y)z When a L{DeferredList} has fired because one L{Deferred} in the list fired with a non-failure result, the cancellation will do nothing instead of cancelling the rest of the L{Deferred}s. TrN)r r"rOrrrs r9)test_cancelFiredOnOneCallbackDeferredListz7DeferredTests.test_cancelFiredOnOneCallbackDeferredListBsL '/j &.j #[+$>RVW T" K(r;c,t}t}t||gd}|jtd|j |j ||j |t|j |tjy)z When a L{DeferredList} has fired because one L{Deferred} in the list fired with a failure result, the cancellation will do nothing instead of cancelling the rest of the L{Deferred}s. TrrN) r r"rrBrrrrrrs r9(test_cancelFiredOnOneErrbackDeferredListz6DeferredTests.test_cancelFiredOnOneErrbackDeferredListOsw '/j &.j #[+$>QUV L01 K( [,7 \5+;+;.vs Qr;rIrc(ttSr3)r/ZeroDivisionErrorr0s r9rz2DeferredTests.testCallbackErrors..{s):)II!((S A adjj*;.sr;rIz Should not have been called yet.rz&Still should not have been called yet.zShould have been called nowNz%make that legible to the type checkerrz/Result should have been from second deferred:{}) r r*rrrOrrr+assertIsNotNone assertEqualsformatr`rrs @r9test_callbackReturnsDeferredz*DeferredTests.test_callbackReturnsDeferreds %J$J   |$ t~~& A $..0RS A   "J   T113PQ##/X1XX/    #A & = D D$$  r;ct}dtdfd }|jd|j|j |j |dtt }d|jd|j|j|j |j |tjy)zF Callbacks passed to addCallback may return Failures. Fc(r tS|dzSNrIr.)rorte shouldFails r9 maybeFailzADeferredTests.test_callbackMaybeReturnsFailure..maybeFailss|#z!r;TN)ror5r=zUnion[int, Failure]) r rjrOr[rrr5rmrr_)r`r#rGrErFs @@r9 test_callbackMaybeReturnsFailurez.DeferredTests.test_callbackMaybeReturnsFailures$: n " 1  --ammI.FGK SMO  1    i(44Y? e   r;cLt}tgdfd }dfd }|j|j||jt |j ||j gjd|j dgy)zo Errbacks passed to L{Deferred.addErrback} can return Deferreds just as callbacks can. rocSr3rG)rors r9 asyncErrbackz?DeferredTests.test_errbackReturnsDeferred..asyncErrbacksIr;c(j|yr3r)ro resultValuess r9 syncCallbackz?DeferredTests.test_errbackReturnsDeferred..syncCallbacks    'r;N)ror/r= Deferred[str])rozUnion[str, int]r=r|)r rYrr ValueErrorrr[rO)r`r#rMrPrrOs @@r9test_errbackReturnsDeferredz)DeferredTests.test_errbackReturnsDeferreds $:$J   ( \"..|< *, A r* H z2r;crt}tj|jt}|j fd|jdg}|j |j |j |gj|j ||gy)z When a paused Deferred with a result is returned from a callback on another Deferred, the other Deferred is chained to the first and waits for it to be unpaused. cSr3rG)ignoredpauseds r9rzDDeferredTests.test_chainedPausedDeferredWithResult..sFr;N)r<r rOr*rrZr[r+)r`expectedchainedrorWs @r9$test_chainedPausedDeferredWithResultz2DeferredTests.test_chainedPausedDeferredWithResults 8#+:! "**23!FMM* $ (,r;ct}t|jfd|jd|jjdg}j|j|j |dgy)z A paused Deferred encountered while pushing a result forward through a chain does not prevent earlier Deferreds from continuing to execute their callbacks. cSr3rGrVseconds r9rz:DeferredTests.test_pausedDeferredChained..&r;N)r rrOr*rZr[)r`firstror^s @r9test_pausedDeferredChainedz(DeferredTests.test_pausedDeferredChainedsl !) !) 01 t 6==) $(r;c4g}tjtjdtjdgj|j|j |ddggg}tjdtj tg}tj|j|j|j t|d|j|dt|djdy)NrIrrcyrDrGrs r9rz2DeferredTests.test_gatherResults..rr;) r gatherResultsr rrZr[r'rRrYr\r4r/)r`resultsrrrs r9test_gatherResultsz DeferredTests.test_gatherResultss#% U]]1-u}}Q/?@AMM NN  Aq6(+ "mmA :< 8 9 B**6==9 Va( fQi1 1%r;c(tjd}tjtd}tj||gd}g}|j |j g}|j |j |jt|t|fd|j|djtj|djj}|j|jty)z If a L{Deferred} in the list passed to L{gatherResults} fires with a failure and C{consumerErrors} is C{True}, the failure is converted to a L{None} result on that L{Deferred}. rIzoh noesTrrrIrN) rr r'rjrdrYrZr[r\r4r_rr)r`dgooddbadr#unconsumedErrorsgatheredErrorsrs r9#test_gatherResultsWithConsumeErrorsz1DeferredTests.test_gatherResultsWithConsumeErrorss a zz,y12    T B*, (//0(* ^**+ #./^1DEvN nQ/55u7G7GH#A&,,77  j.. =r;ct}t}tj||g}|j|j |tj |j |tj |j |tj }|j}|j|jjtj y)z~ When cancelling the L{defer.gatherResults} call, all the L{Deferred}s in the list will be cancelled. N) r rrdrrrrr_r]rr^)r`rrrogatherResultsFailurers r9test_cancelGatherResultsz&DeferredTests.test_cancelGatherResultss '/j &.j $$k;%?@  [%*>*>? [%*>*>?#33FEDeferredTests.test_cancelGatherResultsWithAllDeferredsCallback+st&..C%D %-.C%D $$k;%?@ --f5 *,=> *,=>r;c"tg}g}tjfd}|j|j|j|j |g|j t |d|j|dy)z L{defer.maybeDeferred} should retrieve the result of a synchronous function and pass it to its resulting L{Deferred}. cSr3rGrosr9rz6DeferredTests.test_maybeDeferredSync..Bsr;rIrN)r<r maybeDeferredrrZr[r\assertIdentical)r`rerrr#ros @r9test_maybeDeferredSyncz$DeferredTests.test_maybeDeferredSync:st  " "    / w~~v}}5 $ Wq) WQZ0r;cdd}g}g}tj|d}t|tt|j |j |j |j|g|j|dgy)zV L{defer.maybeDeferred} should pass arguments to the called function. c |dzS)NrGxs r9plusFivez>DeferredTests.test_maybeDeferredSyncWithArgs..plusFiveMs q5Lr; N)r~r5r=r5)rrwrr r5rrZr[)r`rrerrr#s r9test_maybeDeferredSyncWithArgsz,DeferredTests.test_maybeDeferredSyncWithArgsHsk    "   " -Ax}% w~~v}}5 $ 2$'r;c^tddfd }g}g}tj|j|j|j|j |g|j t |d|j t|djty)z L{defer.maybeDeferred} should catch an exception raised by a synchronous function and errback its resulting L{Deferred} with it. zthat value is unacceptablecr3rGrXsr9raisesExceptionzFDeferredTests.test_maybeDeferredSyncException..raisesException_sNr;rIrN)r=r) rRrrwrrZr[r\rr_)r`rrerrrXs @r9test_maybeDeferredSyncExceptionz-DeferredTests.test_maybeDeferredSyncExceptionXs :;   " O,99'..&--X "% Va( VAY__-s8}=r;cL ddzg}g}tjfd}|j |j |j |j |g|j t|d|j|dy#t$r tYwxYw)zw L{defer.maybeDeferred} should handle a L{Failure} returned by a function and errback with it. 10r|cSr3rGrsr9rz=DeferredTests.test_maybeDeferredSyncFailure..usr;rIrN) TypeErrorr/rrwrrZr[r\rx)r`rerrr#rXs @r9test_maybeDeferredSyncFailurez+DeferredTests.test_maybeDeferredSyncFailureis  ! 1H  "    0 1 w~~v}}5 "% Va( VAY1 !yH !sB B#"B#cttjfd}jdg}|j |j |j |dgy)z} L{defer.maybeDeferred} should let L{Deferred} instance pass by so that original result is the same. cSr3rGrsr9rz7DeferredTests.test_maybeDeferredAsync..sr;SuccessN)r rrwrOrrZr[)r`rrors @r9test_maybeDeferredAsyncz%DeferredTests.test_maybeDeferredAsync{sO %J   , I v}}% )-r;cttjfd}jt t |j |t y)z L{defer.maybeDeferred} should let L{Deferred} instance pass by so that L{Failure} returned by the original instance is the same. cSr3rGrsr9rz.sr;N)r rrwrr/rjrd)r`rrs @r9test_maybeDeferredAsyncErrorz*DeferredTests.test_maybeDeferredAsyncErrors? &Z"00< 7<>*+ ##B 5r;cdd}tj|}t|tt|j |j |dy) When called with a coroutine function L{defer.maybeDeferred} returns a L{defer.Deferred} which has the same result as the coroutine returned by the function. c KywNrIrGrGr;r9r8z;DeferredTests.test_maybeDeferredCoroutineSuccess..f rINr=r5)rrwrr r5r[r)r`r8coroutineDeferreds r9"test_maybeDeferredCoroutineSuccessz0DeferredTests.test_maybeDeferredCoroutineSuccesssE "//2%x}5 --.?@!Dr;cdddfd }dd}tj|}t|tt|j |}|j |j|dy) rc KywrrGrGr;r9r8zCDeferredTests.test_maybeDeferredCoroutineSuccessIndirect..frrcSr3rGr8sr9gzCDeferredTests.test_maybeDeferredCoroutineSuccessIndirect..g 3Jr;c |dzSrDrGrvs r9 typedCallbackzODeferredTests.test_maybeDeferredCoroutineSuccessIndirect..typedCallbacks A: r;Nr)r=z"Coroutine[Deferred[int], Any, Any])ror5r=r5)rrwrr rrr[r)r`rrrmodifiedDeferredr8s @r9*test_maybeDeferredCoroutineSuccessIndirectz8DeferredTests.test_maybeDeferredCoroutineSuccessIndirectsa     "//2%x}5,88G --.>?Cr;cGddtdfd dfd }t|jtj|j t y)z When called with a coroutine function L{defer.maybeDeferred} returns a L{defer.Deferred} which has a L{Failure} result wrapping the exception raised by the coroutine function. c eZdZy)GDeferredTests.test_maybeDeferredCoroutineFailure..SomeExceptionNrCrGr;r9 SomeExceptionrs r;rcKwr3rG)rsr9r8z;DeferredTests.test_maybeDeferredCoroutineFailure..fs/ !s cSr3rGrsr9rz;DeferredTests.test_maybeDeferredCoroutineFailure..grr;Nr)r=zCoroutine[None, None, None]) Exceptionrrrrwrr)r`rrr8s @@r9"test_maybeDeferredCoroutineFailurez0DeferredTests.test_maybeDeferredCoroutineFailuresJ I  "     !4!4Q!7 8 = = ] # r;cggtdfd }tjd}|j|j d|jj j j |j j |jg|jgdy)a When a L{Deferred} encounters a result which is another L{Deferred} which is waiting on a third L{Deferred}, the middle L{Deferred}'s callbacks are executed after the third L{Deferred} fires and before the first receives a result. cjd|ftjd}dfd }dfd }|j|j||j j|S)N start-of-cbinnerc*jdSN firstCallbackrrrorres r9rzMDeferredTests.test_innerCallbacksPreserved..cb..firstCallbacks9: r;c4jd|f|dzSNsecondCallbackrrrores r9rzNDeferredTests.test_innerCallbacksPreserved..cb..secondCallback 0&9:z!r;rorr=rQrorr=rrZrr rrYror#rrrcrres r9rz6DeferredTests.test_innerCallbacksPreserved..cbsZ NNM62 3 g&A  " MM- ( 4 4^ D LL )Hr;outerorangerrr)rr orangeorangeNr)r rr rrOrZrYr[r`rrrcrres @@@r9test_innerCallbacksPreservedz*DeferredTests.test_innerCallbacksPreserveds68"$'z   g& " x  '..) ) ) X&    r;cNggtdfd }tjdj|j j |j ddgjd|j g|j ddddgfgy) a The continue callback of a L{Deferred} waiting for another L{Deferred} is not necessarily the first one. This is somewhat a whitebox test checking that we search for that callback among the whole list of callbacks. cjd|ft}dfd }dfd }|j|j|jj}|j d|S)NrcVjd|ftjgSNrrZrrdroares r9rzNDeferredTests.test_continueCallbackNotFirst..cb..firstCallback '89**A3//r;c,jd|fyNrrrs r9rzODeferredTests.test_continueCallbackNotFirst..cb..secondCallback 0&9:r;ror|r=Deferred[List[str]]ro List[str]r=r|rZr rrYrOror#rrreturnerrrcres r9rz7DeferredTests.test_continueCallbackNotFirst..cbb NND&> * ( A 0 ; m,^,HOO,  JJt Or;rrrrNwithersrNrorr=Deferred[None])r rr rrYrZr[rO)r`rrrcres @@@r9test_continueCallbackNotFirstz+DeferredTests.test_continueCallbackNotFirstsBD"$#: &  g**2.99(//J /+BCWM 9 X&   58H9+7V W r;cggtd fd }t}|j||jfd|jj|j d|j ddgj d|j g|j ddddgfdgy ) z A callback added to a L{Deferred} after a previous callback attached another L{Deferred} as a result is run after the callbacks of the other L{Deferred} are run. cjd|ft}dfd }dfd }|j|j|jj}|j d|S)NrcVjd|ftjgSrrrs r9rzLDeferredTests.test_callbackOrderPreserved..cb..firstCallback1rr;c,jd|fyrrrs r9rzMDeferredTests.test_callbackOrderPreserved..cb..secondCallback5rr;rrrrs r9rz5DeferredTests.test_callbackOrderPreserved..cb-rr;c&jdS)NfinalNr)r~res r9rz;DeferredTests.test_callbackOrderPreserved..BsGNN?$Cr;rrrrrrNr)r rrYrZrOr[)r`rrrrcres @@@r9test_callbackOrderPreservedz)DeferredTests.test_callbackOrderPreserved#s BD"$#: & (z " CD ) w /+BCWM 9 X&  '!I;/   r;ctgdfd dfd }dfd }j|j|jd|jgdy)z A callback added to a L{Deferred} by a callback on that L{Deferred} should be added to the end of the callback chain. c(jdyNrrocalleds r9 callback3z;DeferredTests.test_reentrantRunCallbacks..callback3Z MM! r;c(jdyNrrrs r9 callback2z;DeferredTests.test_reentrantRunCallbacks..callback2]rr;cJjdjyrDrZr)rorrrQs r9 callback1z;DeferredTests.test_reentrantRunCallbacks..callback1`s MM!    +r;N)rIrrror|r=r|r rrOr[)r`rrrrrQs @@@r9test_reentrantRunCallbacksz(DeferredTests.test_reentrantRunCallbacksRs\ $,:   , Y'Y'$ +r;ctgdfd dfd }j|jdjddgy)z A callback added to a L{Deferred} by a callback on that L{Deferred} should not be executed until the running callback returns. c(jdyrrrs r9rz;DeferredTests.test_nonReentrantCallbacks..callback2qrr;cpjdjjdgyrD)rZrr[)rorrrQr`s r9rz;DeferredTests.test_nonReentrantCallbacks..callback1ts/ MM!    +   VaS )r;NrIrrr)r`rrrrQs` @@@r9test_nonReentrantCallbacksz(DeferredTests.test_nonReentrantCallbacksisR $,:  * * Y'$ !Q(r;cdtdfd dfd }j|jd|jt}|j |j fy)z After an exception is raised by a callback which was added to a L{Deferred} by a callback on that L{Deferred}, the L{Deferred} should call the first errback with a L{Failure} wrapping that exception. zcallback raised exceptionctr3r)roexceptionMessages r9rzFDeferredTests.test_reentrantRunCallbacksWithFailure..callback2s,- -r;c(jyr3)r)rorrQs r9rzFDeferredTests.test_reentrantRunCallbacksWithFailure..callback1s   +r;Nror<r=r|)r rrOrdrr[r5)r`rrarrQrs @@@r9%test_reentrantRunCallbacksWithFailurez3DeferredTests.test_reentrantRunCallbacksWithFailure}sd 7#+: . , Y'$//)D  *:)<=r;cdt}tj|t}|j fd|j dg}j |j |j|d|j |j |j|d|y)a  If a first L{Deferred} with a result is returned from a callback on a second L{Deferred}, the result of the second L{Deferred} becomes the result of the first L{Deferred} and the result of the first L{Deferred} becomes L{None}. cSr3rGignr`s r9rz=DeferredTests.test_synchronousImplicitChain..ur;NrrI) r<rr r rrOrZrrm)r`ror^rer`s @r9test_synchronousImplicitChainz+DeferredTests.test_synchronousImplicitChains f%!),-*, '..) '!*%7>>* gaj&)r;ctt}|jfd|jdg}j|jg}|j|j|j |g|j |gt }j||j |dg|j ||gy)a9 If a first L{Deferred} without a result is returned from a callback on a second L{Deferred}, the result of the second L{Deferred} becomes the result of the first L{Deferred} as soon as the first L{Deferred} has one and the result of the first L{Deferred} becomes L{None}. cSr3rGr s r9rz>DeferredTests.test_asynchronousImplicitChain..r r;N)r rrOrZr[r<)r`r^r secondResultror`s @r9test_asynchronousImplicitChainz,DeferredTests.test_asynchronousImplicitChains#+*#+:,-$&  +,,-%' <../ b) r* v tf- x0r;c2tjtd}|fdd}t}|j ||j dg}|j |j |j|d|j|ty)a If a first L{Deferred} with a L{Failure} result is returned from a callback on a second L{Deferred}, the first L{Deferred}'s result is converted to L{None} and no unhandled error is logged when it is garbage collected. First Deferred's Failurec|Sr3rG)r1r`s r9rz.cbsLr;Nr)r1r|r`rr=r) rr'rjr rrOrZrrd)r`r`rr^rs r9"test_synchronousImplicitErrorChainz0DeferredTests.test_synchronousImplicitErrorChains~ <(BCD05 "*2"$  +,,- +a.) ##FL9r;cLtt}|jfd|jdg}|j|jg}j|jg}|j|j|j |g|j |gj td|j|djt|j |dg|j t|dy)aP Let C{a} and C{b} be two L{Deferred}s. If C{a} has no result and is returned from a callback on C{b} then when C{a} fails, C{b}'s result becomes the L{Failure} that was C{a}'s result, the result of C{a} becomes L{None} so that no unhandled error is logged when it is garbage collected. cSr3rGr s r9rzCDeferredTests.test_asynchronousImplicitErrorChain..r r;NrrrI) r rrOrYrZr[rrjr]r^r\)r`r^ secondErrorrrr`s @r9#test_asynchronousImplicitErrorChainz1DeferredTests.test_asynchronousImplicitErrorChains!) !),-%' +,,-"$  +,,-#% <../ b) r* l#=>?  A,,\:; tf- \*A.r;cttjfdt}|jfdg}|j|jt}j d|j d|j |gj ||j ||gy)z L{Deferred} chaining is transitive. In other words, let A, B, and C be Deferreds. If C is returned from a callback on B and B is returned from a callback on A then when C fires, A fires. cSr3rGr s r9rzGDeferredTests.test_doubleAsynchronousImplicitChaining..r r;cSr3rG)r r^s r9rzGDeferredTests.test_doubleAsynchronousImplicitChaining..sfr;N)r rrZr<rOr[)r`third thirdResultror`r^s @@r9'test_doubleAsynchronousImplicitChainingz5DeferredTests.test_doubleAsynchronousImplicitChainings#+*#+:,-"** ,-$&  +,,- t b) v vh/r;cggtd fd }tjdj|jj}|j ddgj djj|jj|j gd|j gdy) z L{Deferred}s can have callbacks that themselves return L{Deferred}s. When these "inner" L{Deferred}s fire (even asynchronously), the callback chain continues. cjd|ftjd}dfd }dfd }|j|j||j j|S)Nrrc*jdSrrrs r9rzXDeferredTests.test_nestedAsynchronousChainedDeferreds..cb..firstCallbacks9: r;c4jd|f|dzSrrrs r9rzYDeferredTests.test_nestedAsynchronousChainedDeferreds..cb..secondCallback$rr;rrrrs r9rzADeferredTests.test_nestedAsynchronousChainedDeferreds..cbsZ NNM62 3 g&A   " MM- ( 4 4^ D LL )Hr;rrrr&Got errbacks but wasn't expecting any.rNrr rr rrZr[rOrYrs @@@r9'test_nestedAsynchronousChainedDeferredsz5DeferredTests.test_nestedAsynchronousChainedDeferredss 68"$ (z , MM' " . .r 2 > >w~~ N   .0JK x  ) ) X'OP    r;cggtd fd }tjd}|j||jj|j ddgj d|jjjj|j gd|j ddddgfddggy) aK L{Deferred}s can have callbacks that themselves return L{Deferred}s. These L{Deferred}s can have other callbacks added before they are returned, which subtly changes the callback chain. When these "inner" L{Deferred}s fire (even asynchronously), the outer callback chain continues. cjd|ftjd}dfd }dfd }|j|j|j jS)NrrcTjd|fdd}j|S)Nrc|gSr3rGrvs r9 transformz}DeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks..cb..firstCallback..transformes "8Or;)rorr=rr)ror*rres r9rzjDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks..cb..firstCallback^s,89 $((33r;c4jd|f|dzSrrrs r9rzkDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks..cb..secondCallbackjrr;)rorr=r)rorr=rrrs r9rzSDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks..cbZsW NNM62 3 g&A 4 "  m,^,HOO, r;rrrrr#rN)rorr=zDeferred[Optional[List[str]]]r$rs @@@r99test_nestedAsynchronousChainedDeferredsWithExtraCallbackszGDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacksKs "$ (z : g& " '..)  .0JK y! ) ) X'OP  (*!I;/I&   r;ct}t}|j||j|j|y)zT When we chain a L{Deferred}, that chaining is recorded explicitly. N)r chainDeferredrm _chainedTor`rbs r9&test_chainDeferredRecordsExplicitChainz4DeferredTests.test_chainDeferredRecordsExplicitChains0%J$J  allA&r;ct}t}|j||jd|j|jy)aR Any recorded chaining is cleared once the chaining is resolved, since it no longer exists. In other words, if one L{Deferred} is recorded as depending on the result of another, and I{that} L{Deferred} has fired, then the dependency is resolved and we no longer benefit from recording it. N)r r.rOrr/r0s r9%test_explicitChainClearedWhenResolvedz3DeferredTests.test_explicitChainClearedWhenResolveds<%J$J  4 !,,'r;ct}t|jfd|jd|j|jy)z We can chain L{Deferred}s implicitly by adding callbacks that return L{Deferred}s. When this chaining happens, we record it explicitly as soon as we can find out about it. cSr3rG)rVr1s r9rzFDeferredTests.test_chainDeferredRecordsImplicitChain..sar;N)r rrOrmr/r0s @r9&test_chainDeferredRecordsImplicitChainz4DeferredTests.test_chainDeferredRecordsImplicitChains> %J$J '( 4 allA&r;c|td fd }j|jd|j|g}|j t |d|d}|j |dt d}|jtj||ddj||dy ) z When a Deferred is returned from a callback directly attached to that same Deferred, a warning is emitted. cSr3rGror#s r9circularCallbackzADeferredTests.test_circularChainWarning..circularCallbackHr;foorIrcategoryz1Callback returned the Deferred it was attached tomessagez Expected match: {!r} Got: {!r}Nr) r rrO flushWarningsr[r\DeprecationWarningr]researchr?)r`r;circular_warningswarningpatternr#s @r9test_circularChainWarningz'DeferredTests.test_circularChainWarnings $:  &' 5 ..0@/AB ./3#A& ,.@AE  IIgwy1 2 / 6 6w @R S r;c@|jttdtjtjdt t dfd }j|jd|j}|jt y)z If the deprecation warning for circular deferred callbacks is configured to be an error, the exception will become the failure result of the Deferred. filtersrn)r>cSr3rGr:s r9r;zCDeferredTests.test_circularChainException..circularCallbackr<r;r=Nr) rsetattrwarningsrIfilterwarningsrAr rrOrtrap)r`r;failurer#s @r9test_circularChainExceptionz)DeferredTests.test_circularChainExceptionsz         2DE#:  &' 5&&q) '(r;cnt}t|}|jt|d|ddy)z The C{repr()} of a L{Deferred} contains the class name and a representation of the internal Python ID. N)r idr[repr)r`r#addresss r9 test_reprzDeferredTests.test_reprs3 %JQ% aOGA;a"@Ar;ct}|jd|jt|dt |ddy)zb If a L{Deferred} has been fired, then its C{repr()} contains its result. rrRr~z current result: 'orange'>N)r rOr[rUrTr8s r9test_reprWithResultz!DeferredTests.test_reprWithResults; $: 8 aOBqE!9 g d fd }t}t|Dcgc] }t}}|dd}t |j}|f dd}|j||j||r1|j}|f dd} |j| |}|r1|j fd|j d|D]}|j d j g j d dScc}w)Nc^jttjyr3)rZr\ traceback extract_stack)rVstacks r9recordStackDepthzVDeferredTests.test_boundedStackDepth..chainDeferreds..recordStackDepths S!8!8!:;.chainDeferreds..cbInners  r;c|Sr3rGrds r9 cbNewInnerzPDeferredTests.test_boundedStackDepth..chainDeferreds..cbNewInner%s !Lr;cSr3rG)r lasts r9rzNDeferredTests.test_boundedStackDepth..chainDeferreds..-s$r;rrVr<r=r|)rVr<rrr=r)r rangepoprrOr[) howManyrbtoprVinnerDeferredsoriginalInnersrrenewInnerrgrirar`s @@r9chainDeferredsz.chainDeferreds sIE =#+*C*/.4& 4N4,A.N#+:D"&&(E:?  (6   OOG $ OO, - )--/>F!#!,:!#! !!*- !   . / LL ' %t$ %   UB ' MM$ 8OQ4sDrIrN)rmr5r=r5)r[)r`rrs` r9test_boundedStackDepthz$DeferredTests.test_boundedStackDepths% / f *N1,=>r;cZt}tt|jfdjfdjd|jdjdg}j|j|j |dgy)a Given three Deferreds, one chained to the next chained to the next, callbacks on the middle Deferred which are added after the chain is created are called once the last Deferred fires. This is more of a regression-style test. It doesn't exercise any particular code path through the current implementation of Deferred, but it does exercise a broken codepath through one of the variations of the implementation proposed as a resolution to ticket #411. cSr3rGr]s r9rzZDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled..Nr_r;cSr3rG)rVrs r9rzZDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled..Os5r;N)r rrOrZr[)r`r`rer^rs @@r9:test_resultOfDeferredResultOfDeferredOfFiredDeferredCalledzHDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled@s!) !) (  0101 t t 7>>* 4&)r;ctjdt}g}td} |#t$r|j YnwxYw|j |j|d}|j|j||jddd\}}|jg||jg|y)z C{Deferred.errback()} creates a failure from the current Python exception. When Deferred.debug is not set no globals or locals are captured in that failure. FrrN) rrr rBrJrrYrZr[r_framesr`r#r"excr'localzglobalzs r9test_errbackWithNoArgsNoDebugz+DeferredTests.test_errbackWithNoArgsNoDebugWs 5!$J6" I  IIK  QXXt S)++a.- V$ W%0A  A ctjdt}g}td} |#t$r|j YnwxYw|j |j|d}|j|j||jddd\}}|jg||jg|y)z C{Deferred.errback()} creates a failure from the current Python exception. When Deferred.debug is set globals and locals are captured in that failure. TrrryN) rrr rBrJrrYrZr[r_rzassertNotEqualr{s r9test_errbackWithNoArgsz$DeferredTests.test_errbackWithNoArgsls 4 $J6" I  IIK  QXXt S)++a.- B' B(rcDt}|jdtjddd}|j |g}|j |j |d}|jddd\}}|jg||jg|y) An error raised by a callback creates a Failure. The Failure captures locals and globals if and only if C{Deferred.debug} is set. NFctdNrrBrVs r9 raiseErrorzHDeferredTests.test_errorInCallbackDoesNotCaptureVars..raiseError v& &r;rryrj) r rOrrrrYrZrzr[r`r#rr"r'r}r~s r9&test_errorInCallbackDoesNotCaptureVarsz4DeferredTests.test_errorInCallbackDoesNotCaptureVarss %J 4 5! ' j! QXXt++a.- V$ W%r;cDt}|jdtjddd}|j |g}|j |j |d}|jddd\}}|jg||jg|y)rNTctdrrrs r9rzODeferredTests.test_errorInCallbackCapturesVarsWhenDebugging..raiseErrorrr;rryrj) r rOrrrrYrZrzrrs r9-test_errorInCallbackCapturesVarsWhenDebuggingz;DeferredTests.test_errorInCallbackCapturesVarsWhenDebuggings %J 4 4  ' j! QXXt++a.- B' B(r;c~t}t} |jd d}t j ||j|}tj|j}|jt|d|jd|dd|jd|dd|jd|dd|jd|dd|jd|dd|jd |ddy #t$r|j YwxYw) L{defer.inlineCallbacks} that re-raise tracebacks into their deferred should not lose their tracebacks. c3K|yw)aJ This is never called. It is only used as the decorated function. The resulting function is never called in this test. This is used to make sure that if we wrap an already failed deferred, inlineCallbacks will not add any extra traceback frames. NrGr#s r9icz8DeferredTests.test_inlineCallbacksTracebacks..ics Gr test_deferrrrK1 / 0test_inlineCallbacksTracebacksf.raiseException()Nr#r<r=zGenerator[Any, Any, None]rKr raiseExceptionrJrrinlineCallbacksrr_ extract_tbgetTracebackObjectr[r\assertInr`r8r#r newFailuretbs r9rz,DeferredTests.test_inlineCallbacksTracebackss  $J      b!))!,  ! !*"?"?"A B R!$ lBqE!H- -r!uQx8 "Q%(+ lBqE!H- 92a58D -r!uQx83  IIK DD<;D<cdd}tj|}t|tt|j d|j |y)zM It can discover the return type of a first level coroutine. c KywrDrGrGr;r9 returnsIntz4DeferredTests.test_fromCoroutine..returnsIntrrrINr)r r4rr5r[r)r`rr#s r9test_fromCoroutinez DeferredTests.test_fromCoroutinesD    " ":< 0Ax}% D0034r;cddd|jdtg}|D]1}|jtjtj |3y) L{Deferred.fromCoroutine} requires a coroutine object or a generator, and will reject things that are not that. c|Sr3rGr}s r9rzCDeferredTests.test_fromCoroutineRequiresCoroutine..ar;rITN#test_fromCoroutineRequiresCoroutiner assertRaisesNotACoroutineErrorr r4r`thingsThatAreNotCoroutinesthings r9rz1DeferredTests.test_fromCoroutineRequiresCoroutineV    4 4   & "0 WE   e668N8NPU V Wr;GC works differently on PyPy.ct}tj|}t|}||_tj|}|j d~~|j ||j |y)z| A circular reference between a `Deferred` and its canceller is broken when the deferred is called. N)DummyCancellerweakrefrefr rQrOr)r` canceller weakCancellerrQ weakDeferreds r9*test_canceller_circular_reference_callbackz8DeferredTests.test_canceller_circular_reference_callbackst#$  I. #+9"5% {{8, $   -/* ,.)r;cXt}tj|}t|}||_tj|}t t d}|j||j|~~|j||j|y)zx A circular reference between a `Deferred` and its canceller is broken when the deferred fails. The test demands failures.N) rrrr rQr/rrrr)r`rrrQrrOs r9)test_canceller_circular_reference_errbackz7DeferredTests.test_canceller_circular_reference_errback s#$  I. #+9"5% {{8, )$@AB! X&   -/* ,.)r;ct}tj|}t|}||_|j d|j d~~|j|y)z A circular reference between a `Deferred` and its canceller is broken when the deferred is called, even if another deferred gets added to its chain. ctSr3)r r0s r9rzKDeferredTests.test_canceller_circular_reference_non_final..>sxzr;N)rrrr rQrrOr)r`rrrQs r9+test_canceller_circular_reference_non_finalz9DeferredTests.test_canceller_circular_reference_non_final-sc#$  I. #+9"5% 12$   -/*r;Nr)r5r<r6r<r=rr5r<r6r<r=r|)r5r/r6r<r=r|)ZrDrErFrrrrrrrrrrrrrrrrrrrrrr r rrrrrr$r(r,r6r9rArJrSrZrarfrmrprsryrrrrrrrrrrrrrrr rrrrr%r,r2r4r7rGrPrWrYr[rsrwrrrrrrrpyunitskipIfr-rrrrGr;r9rrs B-+AIQB 5A"5," H%('.R?"?" &&" @B$ @ 9 K:"+ ). ) =) 2 2 =  6 <3,-() & >( K ? 1( >"2$ . 6 ED2 .* X% N- ^,.)(>(*&14:(/:0>; zF P' ( ' .).BY  9?v*.&*)*&()("9H 5W.V]]59:*;*8V]]59:*;*>V]]59:+;+r;rc(eZdZUdZdZded<ddZy)rz A callable that does nothing. It is intended to be used just to get an object reference to support GC related tests. NzOptional[Deferred[Any]]rQcy)zV This is not expected to be called as part of the current test suite. NrGrs r9__call__zDummyCanceller.__call__Rrr;)rQrer=r|)rDrErFrfrQ__annotations__rrGr;r9rrIs )-H%, r;rcdg|zg}t|D]%}|fdfd }|jt|'|fS)z Create a list of Deferreds and a corresponding list of integers tracking how many times each Deferred has been cancelled. Without additional steps the Deferreds will never fire. rc |xxdz cc<yrDrG)r#ncancelledStates r9rz_setupRaceState..cancelcs 1  " r;r)r#rrr5r=r|)rkrZr ) numDeferredsdsrrrs @r9_setupRaceStaterXsR S<'N!#B < .12 # (V,- . 2 r;ceZdZdZeeddeddddZeeddeddddZeeddeddddZdd Z eed d dd Z y ) RaceTestsz Tests for L{race}. rr) min_value max_value) beforeWinner afterWinnerct|dz|z\}}t|}t}||j|t |j |t ||fdg|zdgzdg|zz}t |t |y)a  When one of the L{Deferred}s passed to L{race} fires successfully, the L{Deferred} return by L{race} fires with the index of that L{Deferred} and its result and cancels the rest of the L{Deferred}s. @param beforeWinner: A randomly selected number of Deferreds to appear before the "winning" Deferred in the list passed in. @param beforeWinner: A randomly selected number of Deferreds to appear after the "winning" Deferred in the list passed in. rIrN)rr*r<rOrrr)r`rrrr raceResultrXexpectedCancelledStates r9 test_successzRaceTests.test_successps -\A-= -KL"X 8 <!!(+    , lH- . #$|!3qc!9QC+.sFH)=r;rrN)r r<r*rOrrr)r`winnerrrXrs r9test_resultFromCancelzRaceTests.test_resultFromCancelsZ $,:  = >& 8"X !D((4h8}6MNr;rI)rct|\}}t|}|jt|t dg|z|j |t y)zu If the result of L{race} is cancelled then all of the L{Deferred}s passed in are cancelled. rIN)rr*rrrrr&)r`rrrrs r9 test_cancelzRaceTests.test_cancelsN-\:"X NHaS<-?$@A Z6r;N)rr5rr5r=r|r)rr5r=r|) rDrErFrfrrrrrrrrGr;r9rrks Q7qA6  > Q7qA6  : Q7qA67 7&O  Q7 7 7r;rc(eZdZdZddZddZddZy)FirstErrorTestsz" Tests for L{FirstError}. ctd} |#t$r t}YnwxYwtj|d}|j t |dt |dy)z The repr of a L{FirstError} instance includes the repr of the value of the sub-failure and the index which corresponds to the L{FirstError}. some textrzFirstError[#3, ]N)rRrJr/rrr[rUr`r|r8rns r9rWzFirstErrorTests.test_reprsc % I  A   A& eS {!&DE %%ctd} |#t$r t}YnwxYwtj|d}|j t |dt |dy)z The str of a L{FirstError} instance includes the str of the sub-failure and the index which corresponds to the L{FirstError}. rr|zFirstError[#5, rN)rRrJr/rrr[rrs r9test_strzFirstErrorTests.test_strsc % I  A   A& Us1vha%@Arc ddztjd}tj|d} t d#t$r t}YMwxYw#t$r t}YnwxYwtj|d}|j ||k(|j ||k(|j ||k7|j ||k7|j |dk(y)z L{FirstError} instances compare equal to each other if and only if their failure and index compare equal. L{FirstError} instances do not compare equal to instances of other types. rIr bar rN)rJr/rrrRr]r)r` firstFailureone anotherOne secondFailureanothers r9test_comparisonzFirstErrorTests.test_comparison s  % F|R0%%lB7  &U# # %"9L % &#IM &""=!4 z)* ( w'  *+ #s ? AAAA.-A.Nr)rDrErFrfrWrrrGr;r9rrs F B$r;rceZdZddZddZddZddZddZddZddZ ddZ dd Z dd Z dd Z dd Zdd ZddZddZddZddZddZddZddZy)AlreadyCalledTestsc`tj|_tjdyNT)rr_deferredWasDebuggingrrs r9rzAlreadyCalledTests.setUp+s!%*%7%7%9" 4 r;cBtj|jyr3)rrrrs r9tearDownzAlreadyCalledTests.tearDown/s 4556r;cyr3rGrs r9rzAlreadyCalledTests._callback2 r;cyr3rGrs r9rzAlreadyCalledTests._errback5rr;c&|jdy)NrrNr8s r9_call_1zAlreadyCalledTests._call_18 7r;c&|jdy)NtwicerNr8s r9_call_2zAlreadyCalledTests._call_2;rr;cH|jttyr3rr/rjr8s r9_err_1zAlreadyCalledTests._err_1> ',.)*r;cH|jttyr3r r8s r9_err_2zAlreadyCalledTests._err_2Arr;ct}|j|j|j|j ||j t j|j|yr3) r rrrrrrAlreadyCalledErrorr r8s r9testAlreadyCalled_CCz'AlreadyCalledTests.testAlreadyCalled_CCDsF#: t~~t}}5 Q %22DLL!Dr;ct}|j|j|j|j ||j t j|j|yr3) r rrrrrrrrr8s r9testAlreadyCalled_CEz'AlreadyCalledTests.testAlreadyCalled_CEJsF#: t~~t}}5 Q %22DKKCr;ct}|j|j|j|j ||j t j|j|yr3) r rrrrrrrrr8s r9testAlreadyCalled_EEz'AlreadyCalledTests.testAlreadyCalled_EEPsF#: t~~t}}5 A %22DKKCr;ct}|j|j|j|j ||j t j|j|yr3) r rrrrrrrr r8s r9testAlreadyCalled_ECz'AlreadyCalledTests.testAlreadyCalled_ECVsF#: t~~t}}5 A %22DLL!Dr;cd}|D]1}|jd|zs|jd|zs-|dz }3|j||k(y)Nrz %s:z %srI)r~endswithr])r`linetypefunclinesrXcountlines r9_countzAlreadyCalledTests._count\sR Dv01dmmEDL6Q   )*r;cX|jdjd}|jd||d|jdd|d|jdd|d|jdd|d|jdd|d|jd ||d|jd ||dy) Nr CrIrr rrI)r5splitr")r`rcallerinvoker1invoker2rs r9_checkzAlreadyCalledTests._checkcsq % C* CE1- CE1- C5!, C5!, C5!, C5!,r;c4t}|j|j|j|j | |j ||j dy#tj$r}|j|dddYd}~yd}~wwxYw)N2second callback failed to raise AlreadyCalledErrortestAlreadyCalledDebug_CCrr ) r rrrrr r'rrr+r`r#rs r9r.z,AlreadyCalledTests.testAlreadyCalledDebug_CCqsz#: t~~t}}5 Q L LLO IIJ K'' N KK6 9 M M NA&&B9BBc4t}|j|j|j|j | |j ||j dy#tj$r}|j|dddYd}~yd}~wwxYw)N1second errback failed to raise AlreadyCalledErrortestAlreadyCalledDebug_CErr) r rrrrrr'rrr+r/s r9r3z,AlreadyCalledTests.testAlreadyCalledDebug_CE|sz#: t~~t}}5 Q K KKN III J'' M KK6 8 L L Mr0c4t}|j|j|j|j | |j ||j dy#tj$r}|j|dddYd}~yd}~wwxYw)Nr-testAlreadyCalledDebug_ECrr ) r rrrrr r'rrr+r/s r9r5z,AlreadyCalledTests.testAlreadyCalledDebug_ECsz#: t~~t}}5 A L LLO IIJ K'' M KK6) L L Mr0c4t}|j|j|j|j | |j ||j dy#tj$r}|j|dddYd}~yd}~wwxYw)Nr2testAlreadyCalledDebug_EErr) r rrrrrr'rrr+r/s r9r7z,AlreadyCalledTests.testAlreadyCalledDebug_EEsz#: t~~t}}5 A K KKN III J'' L KK6( K K Lr0cltjdt}|j|j|j |j | |j||jdy#tj$r%}|j|jYd}~yd}~wwxYw)NFr-) rrr rrrrr r'rrr5r/s r9testNoDebuggingz"AlreadyCalledTests.testNoDebuggings 5!#: t~~t}}5 Q L LLO IIJ K'' %   QVV $ $ %sA;;B3B..B3c`tjdt}|jdtjd|j dtjdt}|j dtjd|jdy)NFcyr3rGr s r9rz8AlreadyCalledTests.testSwitchDebugging..rr;Tcyr3rGr<s r9rz8AlreadyCalledTests.testSwitchDebugging..rr;)rrr addBothrOr8s r9testSwitchDebuggingz&AlreadyCalledTests.testSwitchDebuggings| 5!$J "# 4  4 5! J 4 4  "#r;Nrr)r#rQr=r|) rrrrrrrXr5r=r|) rrr(rr)rr*rr=r|)rDrErFrrrrrr rrrrrrr"r+r.r3r5r7r9r?rGr;r9rr*so!7  ++E D D E + - L K L K L $r;rceZdZddZddZddZddZddZddZddZ ddZ dd Z dd Z dd Z dd Zdd ZddZddZddZddZddZddZy)DeferredCancellerTestsc<d|_d|_d|_d|_yr)rrrcancellerCallCountrs r9rzDeferredCancellerTests.setUps".215/3"#r;c<|j|jdy)Nrh)rrCrs r9rzDeferredCancellerTests.tearDowns d--v6r;c||_|Sr3rr`datas r9rz DeferredCancellerTests._callbacks# r;c||_yr3rrFs r9rz!DeferredCancellerTests._callback2s $r;c||_yr3r)r`rns r9rzDeferredCancellerTests._errbacks #r;c>t}|j|j|j|j |j J|j |j jtj|j|jy)zy A L{Deferred} without a canceller must errback with a L{defer.CancelledError} and not callback. N) r rrrrrr[rrrrrr8s r9test_noCancellerz'DeferredCancellerTests.test_noCancellersr %J t~~t}}5  ""... ,,1153G3GH $../r;cbt}|j|j|j|j |j d|j tj|j d|j tj|jty)z A L{Deferred} without a canceller, when cancelled must allow a single extra call to callback, and raise L{defer.AlreadyCalledError} if callbacked or errbacked thereafter. N) r rrrrrOrrrrrr8s r9!test_raisesAfterCancelAndCallbackz8DeferredCancellerTests.test_raisesAfterCancelAndCallbackst %J t~~t}}5   4 %22AJJE %22AIIy{Kr;crt}|j|j|j|j |j t |jtj|jd|jtj|j t y)z A L{Deferred} without a canceller, when cancelled must allow a single extra call to errback, and raise L{defer.AlreadyCalledError} if callbacked or errbacked thereafter. N) r rrrrrrrrrrOr8s r9 test_raisesAfterCancelAndErrbackz7DeferredCancellerTests.test_raisesAfterCancelAndErrbacksv %J t~~t}}5   )+ %22AJJE %22AIIy{Kr;ct}|j|j|j|j |j }|j d|j |j||j y)z A L{Deferred} without a canceller, when cancelled and then callbacked, ignores multiple cancels thereafter. N)r rrrrrrOrmr`r#currentFailures r95test_noCancellerMultipleCancelsAfterCancelAndCallbackzLDeferredCancellerTests.test_noCancellerMultipleCancelsAfterCancelAndCallbacks^ %J t~~t}}5  ,, 4   nd&9&9:r;ct}|j|j|j|j |j J|j |j jtj|j }|jt|j |j jtj|j |j||j y)z A L{Deferred} without a canceller, when cancelled and then errbacked, ignores multiple cancels thereafter. N) r rrrrrr[rrrrrBrmrQs r94test_noCancellerMultipleCancelsAfterCancelAndErrbackzKDeferredCancellerTests.test_noCancellerMultipleCancelsAfterCancelAndErrback s %J t~~t}}5  ""... ,,1153G3GH,, ,.! ,,1153G3GH   nd&9&9:r;cxt}|j|j|j|j |j J|j |j jtj|j }|j |j||j y)z Calling cancel multiple times on a deferred with no canceller results in a L{defer.CancelledError}. Subsequent calls to cancel do not cause an error. N) r rrrrrr[rrrrmrQs r9test_noCancellerMultipleCancelz5DeferredCancellerTests.test_noCancellerMultipleCancel s %J t~~t}}5  ""... ,,1153G3GH,,   nd&9&9:r;cdfd }t|}|jjj|j j Jj j jtjj }|j j|j j jdy)a, Verify that calling cancel multiple times on a deferred with a canceller that does not errback results in a L{defer.CancelledError} and that subsequent calls to cancel do not cause an error and that after all that, the canceller was only called once. c0xjdz c_yrDrCr#r`s r9rzCDeferredCancellerTests.test_cancellerMultipleCancel..cancel5   # #q ( #r;rNrIr#rr=r|) r rrrrrr[rrrrmrC)r`rr#rRs` r9test_cancellerMultipleCancelz3DeferredCancellerTests.test_cancellerMultipleCancel, s )%v6 t~~t}}5  ""... ,,1153G3GH,,   nd&9&9: 00!4r;cdfd }t|}|jjj|j j j djJj jjtjjtj|jdjtj|jty)z Verify that a L{Deferred} calls its specified canceller when it is cancelled, and that further call/errbacks raise L{defer.AlreadyCalledError}. c0xjdz c_yrDrZr[s r9rz;DeferredCancellerTests.test_simpleCanceller..cancelI r\r;rrINr])r rrrrr[rCrrrrrrrOrrr`rr#s` r9test_simpleCancellerz+DeferredCancellerTests.test_simpleCancellerB s )%v6 t~~t}}5   00!4""... ,,1153G3GH %22AJJE %22AIIy{Kr;cdfd }t|jjjj y)zQ Verify that a canceller is given the correct deferred argument. c*j|yr3)rm)rr#r`s r9rz8DeferredCancellerTests.test_cancellerArg..cancel\ s MM"a r;rN)rrr=r|)r rrrrras` @r9test_cancellerArgz(DeferredCancellerTests.test_cancellerArgW s3  !%v6 t~~t}}5  r;c^dfd }t|}|jjj|j d|j j jdjjj jdy)zo Test that cancelling a deferred after it has been callbacked does not cause an error. cbxjdz c_|jtyrDrCrrBr[s r9rz?DeferredCancellerTests.test_cancelAfterCallback..canceli "  # #q ( # IIln %r;rzbiff!rNr]) r rrrrOrr[rCrrrras` r9test_cancelAfterCallbackz/DeferredCancellerTests.test_cancelAfterCallbackc s  &$f5 t~~t}}5 7   00!4 $--. --w7r;cdfd }t|}|jjj|j t |j jjdjJjjjt jjy)z Test that cancelling a L{Deferred} after it has been errbacked does not result in a L{defer.CancelledError}. cbxjdz c_|jtyrDrhr[s r9rz>DeferredCancellerTests.test_cancelAfterErrback..cancel{ rir;rrNr]) r rrrrrBrr[rCrrrrras` r9test_cancelAfterErrbackz.DeferredCancellerTests.test_cancelAfterErrbacku s  &%v6 t~~t}}5 ,.!   00!4""... ,,11<@ $../r;c>dfd }t|}|jjj|j j j djJj jjty)z? Test a canceller which errbacks its deferred. cbxjdz c_|jtyrDrhr[s r9rzADeferredCancellerTests.test_cancellerThatErrbacks..cancel rir;rrINr]) r rrrrr[rCrrrBras` r9test_cancellerThatErrbacksz1DeferredCancellerTests.test_cancellerThatErrbacks sw  &%v6 t~~t}}5   00!4""... ,,11<@r;c<dfd }t|}|jjj|j j j dj jdjjy)z< Test a canceller which calls its deferred. cRxjdz c_|jdy)NrIhello!)rCrOr[s r9rzBDeferredCancellerTests.test_cancellerThatCallbacks..cancel s  # #q ( # JJx r;rrIrsNr]) r rrrrr[rCrrrras` r9test_cancellerThatCallbacksz2DeferredCancellerTests.test_cancellerThatCallbacks ss  !%v6 t~~t}}5   00!4 --x8 $--.r;cdfd }dfd }t|t|}|jd|jfd|j|j j j jjdjJjjjtjy)z Verify that a Deferred, a, which is waiting on another Deferred, b, returned from one of its callbacks, will propagate L{defer.CancelledError} when a is cancelled. c0xjdz c_yrDrZr[s r9 innerCancelzEDeferredCancellerTests.test_cancelNestedDeferred..innerCancel r\r;c(jdyNF)r]r[s r9rz@DeferredCancellerTests.test_cancelNestedDeferred..cancel s OOE "r;rNcSr3rG)rGr1s r9rzBDeferredCancellerTests.test_cancelNestedDeferred.. s1r;rIr]) r rOrrrrrr[rCrrrr)r`rwrrr1s` @r9test_cancelNestedDeferredz0DeferredCancellerTests.test_cancelNestedDeferred s ) #%{;$v6 4 n%   t~~t}}5 00!4""... ,,1153G3GHr;Nr)rGrr=r)rGrr=r|)rnr/r=r|)rDrErFrrrrrrKrMrOrSrUrWr^rbrerjrmrprtr{rGr;r9rArAsf$ 7%$ 0L"L" ;;$ ;5,L* 8$0&A / Ir;rAcheZdZdZddZddZddZddZddZddZ ddZ dd Z dd Z dd Z dd Zy )LogTestsz+ Test logging of unhandled errors. cdg|_tj|jjy)z< Add a custom observer to observer logging. N)cr,r}rZrs r9rzLogTests.setUp s(*  &r;cVtj|jjy)z& Remove the observer. N)r,removeObserverrrZrs r9rzLogTests.tearDown s 466==)r;cL|jDcgc] }|ds | c}Scc}w)NisErrorr)r`rs r9 _loggedErrorszLogTests._loggedErrors s 662aQy\222s !!c|j}|jt|d|ddjt|j ty)zV Check the output of the log observer to see if the error is present. rrIrON)rr[r\rNr3rl)r`c2s r9r+zLogTests._check sM   ! R!$ 1i/0 01r;ctjdjdtj|j y)z Verify that when a L{Deferred} with no references to it is fired, and its final result (the one not handled by any callback) is an exception, that exception will be logged immediately. c ddzSr/rGr}s r9rz(LogTests.test_errorLog.. ar;rIN)r rrOgccollectr+rs r9 test_errorLogzLogTests.test_errorLog s2  /099!<  r;cbdd}|tj|jy)zD Same as L{test_errorLog}, but with an inner frame. c^t}|jd|jdy)Nc ddzSr/rGr}s r9rz`LogTests.test_errorLogWithInnerFrameRef.._subErrorLogWithInnerFrameRef.. AFr;rI)r rrOrs r9_subErrorLogWithInnerFrameRefzNLogTests.test_errorLogWithInnerFrameRef.._subErrorLogWithInnerFrameRef s!'zA MM* + JJqMr;Nrrrr+)r`rs r9test_errorLogWithInnerFrameRefz'LogTests.test_errorLogWithInnerFrameRef s!   &'  r;cbdd}|tj|jy)zQ Same as L{test_errorLogWithInnerFrameRef}, plus create a cycle. cvt}|fdd}|j|||_|jdy)Nc ddzSr/rG)r~r#s r9 unusedCyclezgLogTests.test_errorLogWithInnerFrameCycle.._subErrorLogWithInnerFrameCycle..unusedCycle s Av r;rI)r~r5r# Deferred[int]r=r5)r r_drO)r#rs r9_subErrorLogWithInnerFrameCyclezRLogTests.test_errorLogWithInnerFrameCycle.._subErrorLogWithInnerFrameCycle s1'zA78  MM+ &AD JJqMr;Nrr)r`rs r9 test_errorLogWithInnerFrameCyclez)LogTests.test_errorLogWithInnerFrameCycle s!  ()  r;ctjdjdtj|j |j dt|jtj|jd}|Jd}|j|j|d|y)z Verify that when a L{Deferred} with no references to it is fired, the logged message does not contain a repr of the failure object. c ddzSr/rGr}s r9rz.LogTests.test_errorLogNoRepr.. rr;rIrNr{ Expected message starting with: ) r rrOrrr+r[r\rr,rvr]r~)r`msgrXs r9test_errorLogNoReprzLogTests.test_errorLogNoRepr s  /099!<   CK(##DFF2J/0  NN8 $.xl ; r;cDdd}|tj|j|jdt |j t j|j d}|Jd}|j|j|d|y)z Verify that when a L{Deferred} with no references to it is fired, the logged message includes debug info if debugging on the deferred is enabled. clt}d|_|jd|jdy)NTc ddzSr/rGr}s r9rz?LogTests.test_errorLogDebugInfo..doit..* rr;rI)r debugrrOrs r9doitz-LogTests.test_errorLogDebugInfo..doit' s('zAAG MM* + JJqMr;rrNz (debug: Irr) rrr+r[r\rr,rvr]r~)r`rrrXs r9test_errorLogDebugInfozLogTests.test_errorLogDebugInfo s     CK(##DFF2J/  NN8 $.xl ; r;ct}|jd|jdg}g}|j|j|j|j |g|j t |d|djt~~~tj|j |jgy)z If one Deferred with an error result is returned from a callback on another Deferred, when the first Deferred is garbage collected it does not log its error. c>tjtdS)Nzoop)rr'rjr<s r9rz3LogTests.test_chainedErrorCleanup..A s%**\&-A"Br;NrIr) r rrOrrZr[r\rNrrrr)r`r#rerrs r9test_chainedErrorCleanupz!LogTests.test_chainedErrorCleanup: s %J BC 4! " w~~v}}5 "% Va(q y! VQ  ++-r2r;ctjtd}t|J|j fddx}t j |j|jgy)z If a Deferred with a failure result has an errback which chains it to another Deferred, the initial failure is cleared by the errback so it is not logged. zoh noNcSr3rG)rVgoods r9rz6LogTests.test_errorClearedByChaining..a str;) rr'rr rYrrr[r)r`badrs @r9test_errorClearedByChainingz$LogTests.test_errorClearedByChainingV sd). 9W3E(F)1 +,s  ++-r2r;Nr)r=zList[Dict[str, Any]])rDrErFrfrrrr+rrrrrrrrGr;r9r}r} s>'* 32 ( & 4383r;r}c,eZdZddZddZddZddZy)DeferredListEmptyTestscd|_yr) callbackRanrs r9rzDeferredListEmptyTests.setUpj s r;cPtg}|j|jy)zTesting empty DeferredList.N)r"rcb_empty)r`rs r9testDeferredListEmptyz,DeferredListEmptyTests.testDeferredListEmptym s9Eb9I t}}%r;c6d|_|jg|yrD)rr[)r`ress r9rzDeferredListEmptyTests.cb_emptyr s S!r;c<|j|jdy)NzCallback was never run.)r]rrs r9rzDeferredListEmptyTests.tearDownv s ((*CDr;Nr)rzList[Tuple[bool, object]]r=r|)rDrErFrrrrrGr;r9rri s& "Er;rcleZdZddZddZddZddZddZddZddZ ddZ dd Z dd Z dd Z dd Zy )OtherPrimitivesTestsc.|xjdz c_yrDcounterr`ros r9_incrzOtherPrimitivesTests._incr{ s  r;cd|_yrrrs r9rzOtherPrimitivesTests.setUp~ s  r;cT t}|jj|j|j |j |j |jd|jj|j|j |j |j |jd|j|j |j |j |jd|j|j|j |j |jd|jt|jt}t}t}d dd fd }|j|||}|j |j |j ||j||jj|j|j |j |j |jd|j||j ||j |j |j |jd|jj!|}|j#|j% t&|j t)t& j*t,j.|j|j|j d d}d d} t1|j|tt2t1|j| tt2y) NrIrc ||Sr3rG) resultValue returnValueros r9helperz-OtherPrimitivesTests.testLock..helper s F r;)rrrc,tjdSrrr rGr;r9rz1OtherPrimitivesTests.testLock..returnsInt s==# #r;c KywrDrGrGr;r9returnsCoroIntz5OtherPrimitivesTests.testLock..returnsCoroInt rrr3)rr<rr<r=r<)r=rr)r#acquirerrr]lockedr[rreleaserrrrunr<r rOr>rr4r/rrrrrr5) r`lock firstUnique secondUniquecontrolDeferredrresultDeferredr#rrros @r9testLockzOtherPrimitivesTests.testLock s~ ""4::.  $ q) ""4::.  $ q)   $ q)  % q) )TXX.h x ,4J#'    "   $ -""6* ""4::.  $ q)  . .  $ q) LLN " "6 *   fg. gv.33U5I5IJ  % $  DHHZ((3-8DHH^,hsm.failOnErrback  II0 1r;Nr8r/r=r|)r#rrYr)r`rrr#s` r9test_cancelLockAfterAcquiredz1OtherPrimitivesTests.test_cancelLockAfterAcquired s1  2~ LLN ]#  r;ct}|j|j}|j|j|tj y)z When canceling a L{Deferred} from a L{DeferredLock} that does not yet have the lock (i.e., the L{Deferred} has not fired), the cancel should cause a L{defer.CancelledError} failure. N)r#rrrdrr)r`rr#s r9test_cancelLockBeforeAcquiredz2OtherPrimitivesTests.test_cancelLockBeforeAcquired s> ~  LLN   ##Au';';.helper sI" "r;)rrIrc(jdyryrr<rs r9r'z0OtherPrimitivesTests.testSemaphore..fail s NN5 !r;c(jdyrrrs r9r z3OtherPrimitivesTests.testSemaphore..succeed s NN4 r;T)rr<r=r)r<r<r=r|)r%r r<rrrZrr[rOrrlrrkrrrr)r`Nsemrre uniqueObjectrir'r r#rrrs @@@r9 testSemaphorez"OtherPrimitivesTests.testSemaphore s "*2*  # !#x \:""7>>2""4::. "% L1  & '++-( q) q!a% .A KKM % %djj 1   T\\1 - . " ! KKM & &tW 5   4&) !!$**- q)  q1u-q!a% 2A KKM   T\\1q5 1 2r;cp|jttd|jttdy)zz If the token count passed to L{DeferredSemaphore} is less than one then L{ValueError} is raised. rrN)rrRr%rs r9test_semaphoreInvalidTokensz0OtherPrimitivesTests.test_semaphoreInvalidTokens s* *&7; *&7.failOnErrback rr;rINr)r%rrYr)r`rrr#s` r9!test_cancelSemaphoreAfterAcquiredz6OtherPrimitivesTests.test_cancelSemaphoreAfterAcquired s4  2 " KKM ]#  r;ctd}|j|j}|j|j|tj y)z When canceling a L{Deferred} from a L{DeferredSemaphore} that does not yet have the semaphore (i.e., the L{Deferred} has not fired), the cancel should cause a L{defer.CancelledError} failure. rIN)r%rrrdrr)r`rr#s r9"test_cancelSemaphoreBeforeAcquiredz7OtherPrimitivesTests.test_cancelSemaphoreBeforeAcquired& sA  " KKM   ##Au';'; ? @q 3A IIK # #FMM 2 3q A IIaL  eAh01% %--uyy$?a( %.. :r;cdfd }t}|j}|j||jd|j y)z When canceling a L{Deferred} from a L{DeferredQueue} that already has a result, the cancel should have no effect. c(jdyrrrs r9rzOOtherPrimitivesTests.test_cancelQueueAfterSynchronousGet..failOnErrback] rr;Nr)r$rrYrr)r`rrr#s` r9#test_cancelQueueAfterSynchronousGetz8OtherPrimitivesTests.test_cancelQueueAfterSynchronousGetW s<  2&3_ IIK ]# $  r;c<tj}|jj|tj dfd }|j |g}|j |jjt|dy)a When canceling a L{Deferred} from a L{DeferredQueue} that does not have a result (i.e., the L{Deferred} has not fired), the cancel causes a L{defer.CancelledError} failure. If the queue has a result later on, it doesn't try to fire the deferred. czjdjjjdSr3)rrrrm)ignorerr`s r9rz9OtherPrimitivesTests.test_cancelQueueAfterGet..cbr s- IIdO99;**4==$? ?r;rIN)rr<r=r) r$rrrdrrrrZr[r\)r`r#rdoners` @r9test_cancelQueueAfterGetz-OtherPrimitivesTests.test_cancelQueueAfterGetf ss&3_ IIK   ##Au';';< @ b dkk" TA&r;Nrr)rDrErFrrrrrrrrrrrrrGr;r9rrz sB@=D  =12f=  =#;J 'r;rcXeZdZdZd dZd dZd dZd dZd dZd dZ d dZ d d Z d d Z y )DeferredFilesystemLockTestsz8 Test the behavior of L{DeferredFilesystemLock} cvt|_t|j|j|_y)N) scheduler)r+clockr!mktemprrs r9rz!DeferredFilesystemLockTests.setUp s#W *4;;=DJJO r;c:|jjdS)zI Test that the lock can be acquired when no lock is held rItimeout)rdeferUntilLockedrs r9test_waitUntilLockedWithNoLockz:DeferredFilesystemLockTests.test_waitUntilLockedWithNoLock syy))!)44r;c|j|jj|jjd}|j|tj |j jdgdz|S)zs Test that the lock can not be acquired when the lock is held for longer than the timeout. g@rrIr)r]rr assertFailurer TimeoutErrorr pumpr8s r9%test_waitUntilLockedWithTimeoutLockedzADeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutLocked sc  () II & &s & 3 1e001 b!r;c^dfd }jjjjjdjjjj d}|j |jjdgdz|S)z| Test that a lock can be acquired while a lock is held but the lock is unlocked before our timeout. cf|jtjjdy)NzShould not have timed out)rNrrr'rs r9 onTimeoutzVDeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutUnlocked..onTimeout s" FF5%% & II1 2r;rIrrr)r]rr  callLaterunlockrrYr)r`rr#s` r9'test_waitUntilLockedWithTimeoutUnlockedzCDeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutUnlocked s}  3  () Q 0 01 II & &r & 2 Y b!r;cvt|j}|j|jty)zE Test that the default scheduler is set up properly. N)r!rr[ _schedulerrr`rs r9test_defaultSchedulerz1DeferredFilesystemLockTests.test_defaultScheduler s(&dkkm4 '2r;cx|jj|jjd|jj|jj }|jj }|j |t j|jjd|S)z| Test that an appropriate exception is raised when attempting to use deferUntilLocked concurrently. rI) rr rrrrrAlreadyTryingToLockErroradvancer@s r9test_concurrentUsagez0DeferredFilesystemLockTests.test_concurrentUsage s  Q 0 01 YY ' ' ) YY ' ' ) 2u==> 1 r;c4dfd }jjjjdjjjj }|j |jj d|S)zO Test that a DeferredFilesystemLock can be used multiple times cpjjjj}|Sr3)rrr)r r#r`s r9 lockAquiredzDDeferredFilesystemLockTests.test_multipleUsages..lockAquired s* II    **,AHr;rI)r r<r=r)rr rrrrr%)r`r)r#s` r9test_multipleUsagesz/DeferredFilesystemLockTests.test_multipleUsages sk    Q 0 01 II & & ( k" 1r;c|jj|jj}|jj}|J|j|j |j |j |jj|j|tjy)z When cancelling a L{Deferred} returned by L{DeferredFilesystemLock.deferUntilLocked}, the L{DeferredFilesystemLock._tryLockCall} is cancelled. N) rr _tryLockCallrractiverrrr)r`rQ tryLockCalls r9test_cancelDeferUntilLockedz7DeferredFilesystemLockTests.test_cancelDeferUntilLocked s 99--/ii,, &&& ++-. $))001 Xu';';. rr;Nr)r addTimeoutr+rrOr[rr8s r9test_timeoutChainablez-DeferredAddTimeoutTests.test_timeoutChainable sO %J Q  &' 4 !5!5a!89r;cJt}t}|jd|tddfd }|j||j d|j d|j d|jd|jy)z The L{Deferred} callbacks with the result if it succeeds before the timeout. No cancellation happens after the callback either, which could also cancel inner deferreds. rNrec |Sr3rGre dCallbacked innerDeferreds r9 onCallbackzKDeferredAddTimeoutTests.test_successResultBeforeTimeout..onCallback" !K r;rrerr=r) r+r r=rrO assertIsNotr[r%rr`r r#rDrBrCs @@r9test_successResultBeforeTimeoutz7DeferredAddTimeoutTests.test_successResultBeforeTimeout s #: R)1 %)  ! j! 9 {+ i0  b M*r;cVt}t}|jd|ttddfd }|j ||j d|j d|jd|jd|jy)a  The L{Deferred} callbacks with the result if it succeeds before the timeout, even if a custom C{onTimeoutCancel} function is provided. No cancellation happens after the callback either, which could also cancel inner deferreds. ronTimeoutCancelNrec |Sr3rGrAs r9rDzQDeferredAddTimeoutTests.test_successResultBeforeTimeoutCustom..onCallbackD rEr;rrF) r+r r=r8rrOrGr[r%rrHs @@r9%test_successResultBeforeTimeoutCustomz=DeferredAddTimeoutTests.test_successResultBeforeTimeoutCustom4 s#: R >)1 %)  ! j! 9 {+ i0  b M*r;ct}t}|jd|tdtd}dfd }|j ||j |J|j t|jj||jd|jy)z The L{Deferred} errbacks with the failure if it fails before the timeout. No cancellation happens after the errback either, which could also cancel inner deferreds. rNr'c |Sr3rGr8 dErrbackedrCs r9 onErrbackzDDeferredAddTimeoutTests.test_failureBeforeTimeout..onErrbackf J r;rr8r/r=r) r+r r=rRrYrr4r/rmr_r%rr`r r#rnrSrRrCs @@r9test_failureBeforeTimeoutz1DeferredAddTimeoutTests.test_failureBeforeTimeoutV s $J R)1 (, 6" ! Y %%%% j'2 j&&.  b M*r;ct}t}|jd|ttdt d}dfd }|j ||j |J|jt|jj||jd|jy)a The L{Deferred} errbacks with the failure if it fails before the timeout, even if using a custom C{onTimeoutCancel} function. No cancellation happens after the errback either, which could also cancel inner deferreds. rrKNr'c |Sr3rGrQs r9rSzJDeferredAddTimeoutTests.test_failureBeforeTimeoutCustom..onErrback rTr;rrU) r+r r=r8rRrYrr4r/rmr_r%rrVs @@r9test_failureBeforeTimeoutCustomz7DeferredAddTimeoutTests.test_failureBeforeTimeoutCustomy s$J R >)1 (, 6" ! Y %%%% j'2 j&&.  b M*r;ct}t}|jd||j||j d|j |t jy)z The L{Deferred} by default errbacks with a L{defer.TimeoutError} if it times out before callbacking or errbacking. rrN)r+r r=rr%rrrr`r r#s r9 test_timedOutz%DeferredAddTimeoutTests.test_timedOut sP $J R A b Q 2 23r;ct}t}|jd|t|j ||j d|j d|j|y)a1 If a custom C{onTimeoutCancel] function is provided, the L{Deferred} returns the custom function's return value if the L{Deferred} times out before callbacking or errbacking. The custom C{onTimeoutCancel} function can return a result instead of a failure. rrKrr5Nr+r r=r8rr%r[rr\s r9test_timedOutCustomz+DeferredAddTimeoutTests.test_timedOutCustom sY$J R > A b t';';A'>?r;ct}td}|jd||j||j d|j |j |dy)z If a cancellation function is provided when the L{Deferred} is initialized, the L{Deferred} returns the cancellation value's non-failure return value when the L{Deferred} times out. c$|jdS)NI was cancelled!rNrs r9rzLDeferredAddTimeoutTests.test_timedOutProvidedCancelSuccess.. sajj9K.Lr;rrrcN)r+r r=rr%r[rr\s r9"test_timedOutProvidedCancelSuccessz:DeferredAddTimeoutTests.test_timedOutProvidedCancelSuccess sZ #$LM R A b --a02DEr;ct}tdtfd}|jd||j ||j d|j |t}|j|jy)z If a cancellation function is provided when the L{Deferred} is initialized, the L{Deferred} returns the cancellation value's non-L{CanceledError} failure when the L{Deferred} times out. what!c&|jSr3)r)rrns r9rzLDeferredAddTimeoutTests.test_timedOutProvidedCancelFailure.. sqyy/?r;rrN) r+rRr r=rr%rrmr_)r`r r#r8rns @r9"test_timedOutProvidedCancelFailurez:DeferredAddTimeoutTests.test_timedOutProvidedCancelFailure sm 7#$%?@ R A b  J / aggu%r;ct}t}|jd|tddfd }|j||j J|j t |jjtj|jd|jy)a- If the L{Deferred} is manually cancelled before the timeout, it is not re-cancelled (no L{AlreadyCancelled} error, and also no canceling of inner deferreds), and the default C{onTimeoutCancel} function is not called, preserving the original L{CancelledError}. rNc |Sr3rGr8 dCanceledrCs r9rSzCDeferredAddTimeoutTests.test_cancelBeforeTimeout..onErrback I r;rrU) r+r r=rYrr4r/rmrrrr%rr`r r#rSrlrCs @@r9test_cancelBeforeTimeoutz0DeferredAddTimeoutTests.test_cancelBeforeTimeout s$J R)1   ! Y  $$$ i1 inne&:&:;  b M*r;ct}t}|jd|ttddfd }|j ||j J|j t|jjtj|jd|jy)a, If the L{Deferred} is manually cancelled before the timeout, it is not re-cancelled (no L{AlreadyCancelled} error, and also no canceling of inner deferreds), and the custom C{onTimeoutCancel} function is not called, preserving the original L{CancelledError}. rrKNc |Sr3rGrks r9rSzIDeferredAddTimeoutTests.test_cancelBeforeTimeoutCustom..onErrback rmr;rrU)r+r r=r8rYrr4r/rmrrrr%rrns @@r9test_cancelBeforeTimeoutCustomz6DeferredAddTimeoutTests.test_cancelBeforeTimeoutCustom s$J R >)1   ! Y  $$$ i1 inne&:&:;  b M*r;ct}td}|jd|t|j ||j d|j d|j|y)zu A custom translation function can handle a L{Deferred} with a custom cancellation function. c6|jtdS)Nrf)rrRrs r9rzVDeferredAddTimeoutTests.test_providedCancelCalledBeforeTimeoutCustom..( sqyyG9L/Mr;rrKrr5Nr_r\s r9,test_providedCancelCalledBeforeTimeoutCustomzDDeferredAddTimeoutTests.test_providedCancelCalledBeforeTimeoutCustom" s] $%MN R > A b t';';A'>?r;cnt}t}ddfd }|j||jd||j dJ|j t |j jtj|j|tjy)a An errback added before a timeout is added errbacks with a L{defer.CancelledError} when the timeout fires. If the errback returns the L{defer.CancelledError}, it is translated to a L{defer.TimeoutError} by the timeout implementation. Nc ||Sr3rGr8rRs r9rzGDeferredAddTimeoutTests.test_errbackAddedBeforeTimeout..errback< JHr;rrr8r/r=r/) r+r rYr=r%r4r/r_rrrrr`r r#rrRs @r9test_errbackAddedBeforeTimeoutz6DeferredAddTimeoutTests.test_errbackAddedBeforeTimeout0 s$J   W R b%%% j'2 j..0D0DE Q 2 23r;cPt}t}ddfd }|j||jd||j dJ|j t |j jtj|j|y)z An errback added before a timeout is added errbacks with a L{defer.CancelledError} when the timeout fires. If the errback suppresses the L{defer.CancelledError}, the deferred successfully completes. NcH||jtjyr3)rNrrrxs r9rz]DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellation..errbackX sJ FF5'' (r;rrr) r+r rYr=r%r4r/r_rrrr{s @r94test_errbackAddedBeforeTimeoutSuppressesCancellationzLDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationL s$J  ) W R b%%% j'2 j..0D0DE Qr;czt}t}ddfd }|j||jd|t|j dJ|j t|j jtj|jd|j|y)a An errback added before a timeout is added with a custom timeout function errbacks with a L{defer.CancelledError} when the timeout fires. The timeout function runs if the errback returns the L{defer.CancelledError}. Nc ||Sr3rGrxs r9rzMDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutCustom..errbackt ryr;rrr5rz r+r rYr=r8r%r4r/r_rrr[rr{s @r9$test_errbackAddedBeforeTimeoutCustomz?r;czt}t}ddfd }|j||jd|t|j dJ|j t|j jtj|jd|j|y)a An errback added before a timeout is added with a custom timeout function errbacks with a L{defer.CancelledError} when the timeout fires. The timeout function runs if the errback suppresses the L{defer.CancelledError}. Nc |yr3rGrxs r9rzcDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationCustom..errback sJr;rrr5rrr{s @r9:test_errbackAddedBeforeTimeoutSuppressesCancellationCustomzRDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationCustom s$J   W R . b%%% j'2 j..0D0DE t';';A'>?r;ct}dtfd}ddfd }|j||jd||j d|j |j |j|y)a* Given a deferred with a cancellation function that resumes the callback chain, a callback that is added to the deferred before a timeout is added to runs when the timeout fires. The deferred completes successfully, without a L{defer.TimeoutError}. rc&|jSr3rNr#rs r9rzSDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout.. ajj.Ar;Nc ||Sr3rGr_rBs r9rOzSDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout..callback KLr;rrr_rr=r)r+r rr=r%r[rmrr`r r#rOrBrs @@r9)test_callbackAddedToCancelerBeforeTimeoutzADeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout sw#$AB   h R b g. gt33A67r;c&t}dtfd}dd fd }|j||jd|t|j d|j |j d|j|y) ad Given a deferred with a cancellation function that resumes the callback chain, a callback that is added to the deferred before a timeout is added to runs when the timeout fires. The deferred completes successfully, without a L{defer.TimeoutError}. The timeout's custom timeout function also runs. rc&|jSr3rNrs r9rzYDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom.. rr;Nc ||Sr3rGrs r9rOzYDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom..callback rr;rrKrr5r)r+r rr=r8r%r[rrs @@r9/test_callbackAddedToCancelerBeforeTimeoutCustomzGDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom s}#$AB   h R > b g. t';';A'>?r;Nr)rDrErFrfr>rIrNrWrZr]r`rdrhrorrrur|rrrrrrGr;r9r:r: sm :+B +D!+F"+H 4@" F&"!+F!+F @48 8@8@688@r;r:c0eZdZdZddZddZddZddZy)EnsureDeferredTestsz& Tests for L{ensureDeferred}. cRt}t|}|j||y)zK L{ensureDeferred} will pass through a Deferred unchanged. N)r r)rmr@s r9test_passesThroughDeferredsz/EnsureDeferredTests.test_passesThroughDeferreds s#&Z B  b"r;c|jtj5tddddy#1swYyxYw)zr Passing L{ensureDeferred} a non-coroutine and a non-Deferred will raise a L{ValueError}. somethingN)rrrr)rs r9'test_willNotAllowNonDeferredOrCoroutinez;EnsureDeferredTests.test_willNotAllowNonDeferredOrCoroutine s5   u77 8 ( ; ' ( ( (s 5>cdd}|}|j|tjt|}t |t t |j|t |j|}|j|dy)zM L{ensureDeferred} will turn a coroutine into a L{Deferred}. cPKtjd}|d{}|S7wNr=rr#rs r9rz=EnsureDeferredTests.test_ensureDeferredCoroutine..run s% e$A'CJs &$&r=N)r=r) r4types CoroutineTyper)rr rrr[r`rr<r#rs r9test_ensureDeferredCoroutinez0EnsureDeferredTests.test_ensureDeferredCoroutine r   E a!4!45 1 Ax}% a*""1% e$r;cdd}|}|j|tjt|}t |t t |j|t |j|}|j|dy)zX L{ensureDeferred} will turn a yield-from coroutine into a L{Deferred}. c3lKtjd}tt|Ed{}|S7 wr)rr rrrs r9rz=EnsureDeferredTests.test_ensureDeferredGenerator..run s, e$As\+CJ*s %42 4r=N)r=z#Generator[Deferred[str], None, str]) r4r GeneratorTyper)rr rrr[rs r9test_ensureDeferredGeneratorz0EnsureDeferredTests.test_ensureDeferredGeneratorrr;Nr)rDrErFrfrrrrrGr;r9rr s(%.%r;rceZdZdZddZy)TimeoutErrorTestsz1 L{twisted.internet.defer} timeout code. cXt}tj||j|tj|j |j g}|jt|d|j|ddt|j|dddy)zB L{twisted.internet.defer.timeout} is deprecated. rIrr>r?z~twisted.internet.defer.timeout was deprecated in Twisted 17.1.0; please use twisted.internet.defer.Deferred.addTimeout insteadN) r rrrrr@test_deprecatedTimeoutr[r\rmrA)r`rQ warningsShowns r9rz(TimeoutErrorTests.test_deprecatedTimeout"s&.Z h 8U%7%78**D,G,G+HI  ]+Q/ mA&z24FG  ! Y ' M r;Nr)rDrErFrfrrGr;r9rrs  r;rcZ|j|j|jy)z Tickle an asyncio event loop to call all of the things scheduled with call_soon, inasmuch as this can be done via the public API. @param loop: The asyncio event loop to flush the previously-called C{call_soon} entries from. N) call_soonstop run_foreverloops r9callAllSoonCallsr3s  NN499r;cLeZdZd dZd dZd dZd dZd dZd dZd dZ d dZ y ) DeferredFutureAdapterTestscPt}|j|j|S)zU Create a new event loop that will be closed at the end of the test. )_new_event_looprclosers r9newLoopz"DeferredFutureAdapterTests.newLoop@s!!"  % r;cTt}|j}|j|}|j|j d|j dt ||j|j|d|j|jdy)zw L{Deferred.asFuture} returns a L{asyncio.Future} which fires when the given L{Deferred} does. FrN) r rasFuturer[rrOrrro)r`r#raFutures r9 test_asFuturez(DeferredFutureAdapterTests.test_asFutureHs $:||~**T" / 2 --a0$7 )2.r;cHddfd }t|}|j}|j|}|jt ||j |j |j|d|jt|jy)z L{Deferred.asFuture} returns a L{asyncio.Future} which, when cancelled, will cancel the original L{Deferred}. Fc dyrrG)dprimers r9cancelerzFDeferredFutureAdapterTests.test_asFutureCancelFuture..canceler^sFr;Nrrr=r|) r rrrrr]r[rrrro)r`rr#rrrs @r9test_asFutureCancelFuturez4DeferredFutureAdapterTests.test_asFutureCancelFutureVs  %X.||~**T"  --a0$7 .'..9r;cdd}t|}|j}|j|}|jt ||j |j |d|jt|jy)z While Futures don't support succeeding in response to cancellation, Deferreds do; if a Deferred is coerced into a success by a Future cancellation, that should just be ignored. c&|jdy)NrrN)rs r9rzGDeferredFutureAdapterTests.test_asFutureSuccessCancel..cancelerrs OOA r;Nr) r rrrrr[rrrro)r`rr#rrs r9test_asFutureSuccessCancelz5DeferredFutureAdapterTests.test_asFutureSuccessCancelksm %X.||~**T" --a0$7 .'..9r;ct}tt}|j}|j |}t ||j |t ||jt|jy)z L{Deferred.asFuture} makes a L{asyncio.Future} fire with an exception when the given L{Deferred} does. N) r r/r3rrrrrro)r`r# theFailurerfutures r9test_asFutureFailurez/DeferredFutureAdapterTests.test_asFutureFailure}sc %J.01 ||~D! * +V]];r;c|j}t|}tj|}|j ||j dt ||j|j|dy)zx L{Deferred.fromFuture} returns a L{Deferred} that fires when the given L{asyncio.Future} does. rrIN) rrr fromFuturer set_resultrr[r)r`rrr#s r9test_fromFuturez*DeferredFutureAdapterTests.test_fromFuturesh ||~%40    ( A1 --a0!4r;c$|j}t|}tj|}|j t ||j t|j|j|jty)z L{Deferred.fromFuture} makes a L{Deferred} fire with an L{asyncio.CancelledError} when the given L{asyncio.Future} is cancelled. rN) rrr rrrrrrorrNr`r cancelledr#s r9test_fromFutureFutureCancelledz9DeferredFutureAdapterTests.test_fromFutureFutureCancelledsl ||~"(d"3    * .)*:*:; Q$$^4r;cd|j}t|}tj|}|j t ||j |jd|jt|j|j|jty)z L{Deferred.fromFuture} makes a L{Deferred} which, when cancelled, cancels the L{asyncio.Future} it was created from. rTN) rrr rrrr[rrrrorrNrs r9 test_fromFutureDeferredCancelledz;DeferredFutureAdapterTests.test_fromFutureDeferredCancelleds ||~"(d"3    *   ,,.5 .)*:*:; Q$$^4r;N)r=rr) rDrErFrrrrrrrrrGr;r9rr?s* /:*:$ < 5 5 5r;rcZeZdZddZddZeddZeddZeddZddZ y) CoroutineContextVarsTestscttjdjdt j fdt j fdj dt jd fd ttgtdft jd fd }t|tgtdfjd |}jdjdjdjj|d y) z When an inlineCallbacks function is called, the context is taken from when it was first called. When it resumes, the same context is applied. testvarrIc&jdSrsetr1vars r9rzDCoroutineContextVarsTests.test_withInlineCallbacks..swwqzr;c&jdSNrrs r9rzDCoroutineContextVarsTests.test_withInlineCallbacks.. r;c ddz Sr/rGr0s r9rzDCoroutineContextVarsTests.test_withInlineCallbacks.. Ar;c3Kt}jd|jd|jdywNrITrr rrOrr#r rs r9yieldingDeferredzLCoroutineContextVarsTests.test_withInlineCallbacks..yieldingDeferreds2'zA OOAqzz4 0G GGAJs>ANc3rKjjdtjdjjdj dj djjdj dj d t d#t $r#jjdYnwxYwjjdtjdyw)NrrIT???? should have failed)r[rrr rrOrr)r mutatingDeferredmutatingDeferredThatFailsr`rrsr9 testFunctionzHCoroutineContextVarsTests.test_withInlineCallbacks..testFunctions   SWWY *--" "   SWWY * OOA/88$ ?" "   SWWY * OOA8AA4 H ;// 9:: /  A. /#$ $   SWWY *   d #s+B6D7:C > D7 )C52D74C55AD7rTr=z#Generator[Deferred[Any], Any, None]) r+ contextvars ContextVarrr rrrrr r%r[r)r`rr#r rrrrs` @@@@@r9test_withInlineCallbacksz2CoroutineContextVarsTests.test_withInlineCallbackss) +6+A+A)+L  ,4:$$%9:3;:!!--.BC!--o>       $hr8D>/A&BC   " $" $ " $H L(2x~+=">?  N  a  a  a --a0$7r;cttjdtjdfd }j d|}j dj|y)z When an inlineCallbacks function resumes, we should be able to reset() a contextvar that was set when it was first called. rc3(Kjd}t}jd|jd|j j dj |j j dyw)NrrITr)rr rrOr[rreset)tokenr#r r`rs r9rzQCoroutineContextVarsTests.test_resetWithInlineCallbacks..yieldingDeferredspGGAJE (zA OOAqzz4 0G   SWWY * IIe    SWWY *sBBrrINr)r+rrrrrr%r)r`rr#r rs` @@r9test_resetWithInlineCallbacksz7CoroutineContextVarsTests.test_resetWithInlineCallbacksse +6+A+A)+L    +  +     a Qr;cKtd}4d{|jjj}|j fd|jj|j |j dddd{|j|j |d{|j jy77G#1d{7swYWxYw79w)zQ L{DeferredLock} can be used as an asynchronous context manager. Nc$jSr3r)r1rs r9rz>CoroutineContextVarsTests.test_asyncWithLock..1s DLLNr;)r#r]rrrrr)r`r#rs @r9test_asyncWithLockz,CoroutineContextVarsTests.test_asyncWithLock's ~  ' ' OODKK ( A MM2 3 OODKK (   QXX &  ' ' ! % ' ' ' ' ' sQC<C!C<A6C% C<C#$C<C:!C<#C<%C7+C. ,C73C<cKtd4d{|jjd4d{|jjdj}j}|jjd|j |j |j |j dddd{|jjd|j j jfd|jfd|d{|d{|jjddddd{|jjdy77b7#1d{7swYxYw7t7l7B#1d{7swYRxYww)z^ L{DeferredSemaphore} can be used as an asynchronous context manager. rNrrIrc$jSr3rr1rs r9rzCCoroutineContextVarsTests.test_asyncWithSemaphore..K S[[]r;c$jSr3rrs r9rzCCoroutineContextVarsTests.test_asyncWithSemaphore..Lrr;)r%r[tokensrr]rrr)r`rrrs @r9test_asyncWithSemaphorez1CoroutineContextVarsTests.test_asyncWithSemaphore8sn  " , ,   SZZ + , ,  Q/[[][[]  Q/ *  +  , ,   SZZ + OOBII & NN2 3 NN2 3HHHH   SZZ + , , Q'! , , , , , ,   , , , ,sGFG#F>FF>BF# F>F!A(F>F8 F> F: F>. G9F<:"GF>!F>#F5 )F,*F5 1F>:F><G>GG G Gc@Kt}|jtd5|4d{|j|jtd7*#1d{7swYnxYw dddn #1swYnxYw|j |jyw)zw C{DeferredLock} correctly propagates exceptions when used as an asynchronous context manager. zsome specific exceptionN)r#assertRaisesRegexrr]rrr!s r9test_asyncWithLockExceptionz5CoroutineContextVarsTests.test_asyncWithLockExceptionRs ~  # #I/H I ; ; ; , 9:: ; ; ; ; ; ; ; ; %sI!BA6AA6'AA6A) A A) %A6- B6A?;#Bcttjdjdt j fdt j fdj dd fd d fd }jdt |}jdjdjdjj|d y ) z When a coroutine is called, the context is taken from when it was first called. When it resumes, the same context is applied. rrIc&jdSrrrs r9rzJCoroutineContextVarsTests.test_contextvarsWithAsyncAwait..krr;c&jdSrrrs r9rzJCoroutineContextVarsTests.test_contextvarsWithAsyncAwait..nrr;c ddz Sr/rGr0s r9rzJCoroutineContextVarsTests.test_contextvarsWithAsyncAwait..orr;cKt}jd|jd|d{jdy7wrrrs r9asyncFuncAwaitingDeferredz[CoroutineContextVarsTests.test_contextvarsWithAsyncAwait..asyncFuncAwaitingDeferredqs9 ( A OOAqzz4 0GG GGAJ s-A AA cKjjdtjdd{jjdj dj dd{jjdj dj d d{t d77Y7#t $r#jjdYnwxYwd{7jjdyw)NrrIrTrr)r[rrr rrOr)r r rrr`rsr9rzNCoroutineContextVarsTests.test_contextvarsWithAsyncAwait..testFunctionxs   SWWY *--" " "   SWWY * OOA/88$ ?" " "   SWWY * OOA8AA4 H ;/// 9::) # #0 /  A. /,- - -   SWWY *se9ECAECAEC! C C! EEC!!)D  E D  ED&ErTNr)r=r) r+rrrr rr)r%r[r)r`rr#r r rrrs` @@@@@r9test_contextvarsWithAsyncAwaitz8CoroutineContextVarsTests.test_contextvarsWithAsyncAwait_s +6+A+A)+L  ,4:$$%9:4 ! ! H  <> *  a  a  a --a0$7r;Nr) rDrErFrrr@rrrrrGr;r9rrsQO8b @&& ((2 & &I8r;rceZdZddZddZddZejedddZ ejedddZ y) InlineCallbackTestsc~t}t} |jd d}t j ||j|}tj|j}|jt|d|jd|dd|jd|dd|jd|dd|jd|dd|jd|dd|jd |ddy #t$r|j YwxYw) rc3K|ywr3rGrs r9rz>InlineCallbackTests.test_inlineCallbacksTracebacks..ics GrrrrrrKrrrNrrrs r9rz2InlineCallbackTests.test_inlineCallbacksTracebackss  $J      b!))!,  ! !*"?"?"A B R!$ lBqE!H- -r!uQx8 "Q%(+ lBqE!H- 92a58D -r!uQx8#  IIK rcddd|jdtg}|D]1}|jtjtj |3y)rc|Sr3rGr}s r9rzIInlineCallbackTests.test_fromCoroutineRequiresCoroutine..rr;rITNrrs r9rz7InlineCallbackTests.test_fromCoroutineRequiresCoroutinerr;cg}g}t|jt|jtjdfd }|}|j |j |j ||j |g|j |g|j |j |g|j |g|j ||j |j|}|j |jtj|j |gy)a Cancelling an L{defer.inlineCallbacks} correctly handles the function catching the L{defer.CancelledError}. The desired behavior is: 1. If the function is waiting on an inner deferred, that inner deferred is cancelled, and a L{defer.CancelledError} is raised within the function. 2. If the function catches that exception, execution continues, and the deferred returned by the function is not resolved. 3. Cancelling the deferred again cancels any deferred the function is waiting on, and the exception is raised. c3>K y#t$rYwxYwwr3r)rrsr9testFunczHInlineCallbackTests.test_inlineCallbacksCancelCaptured..testFuncs+ H  s N)r=z)Generator[Deferred[object], object, None]) r rZrrrr[rrrr)r`canceller1Callscanceller2CallsrfuncDrOrrs @@r9"test_inlineCallbacksCancelCapturedz6InlineCallbackTests.test_inlineCallbacksCancelCaptureds3524'(>(>?'(>(>?        B B E" "- "-   2$/ "- E"  &&u- u';';< 2$/r;rcHt}tj|}tjdd}||}|j ||j |tj|}~~|j||j|y)a When using L{defer.inlineCallbacks}, after the function exits, it will not keep references to the function itself or the arguments. This ensures that the machinery gets deallocated immediately rather than waiting for a GC, on CPython. The GC on PyPy works differently (del doesn't immediately deallocate the object), so we skip the test. c3K||Swr3rGrs r9rzIInlineCallbackTests.test_inlineCallbacksNoCircularReference..func'sGHs Nrrr=r)rrrrrr[rrr`obj objWeakRefrr funcDWeakRefs r9'test_inlineCallbacksNoCircularReferencez;InlineCallbackTests.test_inlineCallbacksNoCircularReferences[[%       S  d2259:{{5)    *,' ,.)r;cHt}tj|}dd}tj||}|j ||j |tj|}~~|j||j|y)z Tests that there is no circular dependency when using L{Deferred.fromCoroutine}, so that the machinery gets cleaned up immediately rather than waiting for a GC. cK|Swr3rGrs r9rzCInlineCallbackTests.test_coroutineNoCircularReference..funcFs HsNr)rrrr r4r[rrr s r9!test_coroutineNoCircularReferencez5InlineCallbackTests.test_coroutineNoCircularReference:s[[%  &&tCy1 d2259:{{5)    *,' ,.)r;Nr) rDrErFrrrrrr-r$r'rGr;r9rrsX98W.50nV]]59:!*;!*FV]]59:*;*r;r)r8z0Callable[..., Coroutine[Deferred[Any], Any, _T]]r=zCallable[..., Deferred[_T]])r5r<r6r<r=r/)rQrQr=r|)rr5r=z(tuple[list[int], list[Deferred[object]]])r6r<r7floatr=r)rrr=r|)brf __future__rrr>rrBr_rr0rrLrasynciorrrrrtypingrr r r r r rrrrrrrrrhamcrestrrr hypothesisrhypothesis.strategiesrrtwisted.internetrrtwisted.internet.deferrr r!r"r#r$r%r&r'r(r)r*twisted.internet.taskr+twisted.pythonr,twisted.python.compatr-twisted.python.failurer/ twisted.trialr@rrBrKrRrSrVrXrhSynchronousTestCaserrrrrrrAr}rrr r8r:rrrrrrrGr;r9r7s8#   $21*+    ('*"7  9   )m95 44,*M!!*MZS+H002GS+l4   &~7,,~7B;$h22;$|L$55L$^GIX99GITc3x++c3LEX99E"A'8779NA'Hz=("3"3z=zS@h::S@lA%(++A%H ))+@ , s5!2!2s5ls8 1 1s8lj*(66j*r;