
    h+                      S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKrSSK	r
SSKrSSKrSSKJrJrJrJr  SSKJrJrJrJrJrJrJrJrJrJrJrJrJ r J!r!J"r"J#r#  SSK$J%r%J&r&J'r'  SSK(J)r)  SSK*J+r+  SS	K,J-r-  SS
K.J/r/J0r0  SSK1J2r2J3r3J4r4J5r5J6r6J7r7J8r8J9r9J:r:J;r;J<r<J=r=  SSK>J?r?  SSK@JArA  SSKBJCrC  SSKDJErE  SSKFJ	r	      S>S jrG " S S\H5      rIS?S jrJ\!" S\HS9rK " S S5      rL " S S\	R                  5      rN " S S\	R                  \L5      rP " S S5      rQS@S jrR " S  S!\	R                  5      rS " S" S#\	R                  5      rT " S$ S%\	R                  5      rU " S& S'\	R                  5      rV " S( S)\	R                  5      rW " S* S+\	R                  5      rX " S, S-\	R                  \L5      rY " S. S/\	R                  5      rZSAS0 jr[ " S1 S2\	R                  5      r\ " S3 S4\	R                  5      r] " S5 S6\	R                  \L5      r^SBS7 jr_ " S8 S9\	R                  5      r` " S: S;\	R                  5      ra " S< S=\	R                  5      rbg)Cz+
Test cases for L{twisted.internet.defer}.
    )annotationsN)AbstractEventLoopCancelledErrorFuturenew_event_loop)AnyCallable	CoroutineDict	GeneratorListLiteralMappingNoReturnOptionalSetTupleTypeTypeVarUnioncast)assert_thatemptyequal_to)given)integers)assert_type)deferreactor)_TDeferredDeferredFilesystemLockDeferredListDeferredLockDeferredQueueDeferredSemaphoreFailureGroup_DeferredListResultListT_DeferredListSingleResultTensureDeferredrace)Clock)log)_PYPYFailure)unittestc                J   ^  [         R                  " T 5      SU 4S jj5       nU$ )Nc                 @   > T" U 0 UD6n[         R                  " U5      $ N)r!   fromCoroutine)argskwargscorofs      ^/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/test/test_defer.pywrapper!ensuringDeferred.<locals>.wrapperJ   s#    $!&!%%d++    )r6   objectr7   r>   returnzDeferred[_T])	functoolswraps)r9   r;   s   ` r:   ensuringDeferredrB   G   s'     __Q, , Nr=   c                      \ rS rSrSrg)GenericErrorR    N__name__
__module____qualname____firstlineno____static_attributes__rF   r=   r:   rD   rD   R   s    r=   rD   c                &    U R                  S5        g)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 ResultNcallbackdeferreds    r:   fakeCallbackCancellerrS   V   s     '(r=   _ExceptionT)boundc                  .    \ rS rSrSr      SS jrSrg)ImmediateFailureMixinc   z#
Add additional assertion methods.
c                &   [        [        R                  U 5      n/ nUR                  UR                  5        UR                  [        U5      S5        UR                  US   R                  U5      5        [        [        US   R                  5      $ )z
Assert that the given Deferred current result is a Failure with the
given exception.

@return: The exception instance in the Deferred.
   r   )r   r1   TestCase
addErrbackappendassertEquallen
assertTruecheckrT   value)selfrR   	exceptiontestCasefailuress        r:   assertImmediateFailure,ImmediateFailureMixin.assertImmediateFailureh   su     ))40"$HOO,S]A.HQK--i89K!!2!233r=   rF   N)rR   Deferred[Any]rd   zType[_ExceptionT]r?   rT   )rH   rI   rJ   rK   __doc__rg   rL   rF   r=   r:   rW   rW   c   s$    4%42C4	4r=   rW   c                  6    \ rS rSrSrSS jrSS jrSS jrSrg)		UtilTestsy   z
Tests for utility functions.
c                    [        [        5       5      n[        R                  " U5      nU R	                  [        5        U R                  X5        g)z,
L{defer.logError} returns the given error.
N)r0   RuntimeErrorr   logErrorflushLoggedErrorsassertIs)rc   errorresults      r:   test_logErrorReturnsError#UtilTests.test_logErrorReturnsError~   s8     '&|,e$r=   c                    [        [        5       5      n[        R                  " U5        U R	                  [        5      nU R                  X!/5        g)z)
L{defer.logError} logs the given error.
N)r0   ro   r   rp   rq   r^   )rc   rs   errorss      r:   test_logErrorLogsError UtilTests.test_logErrorLogsError   s<     'u''5)r=   c                  ^ / mSU4S jjn[         R                  " U5        [        [        5       5      n[        R
                  " U5        U R                  [        5        U R                  TS   R                  S5      5        g)zB
The text logged by L{defer.logError} has no repr of the failure.
c                ^   > [         R                  " U 5      nUc   eTR                  U5        g r4   )r-   textFromEventDictr]   )	eventDicttextoutputs     r:   emit4UtilTests.test_logErrorLogsErrorNoRepr.<locals>.emit   s+    ((3D###MM$r=   r   Unhandled Error
Traceback N)r~   zDict[str, Any]r?   None)	r-   addObserverr0   ro   r   rp   rq   r`   
startswith)rc   r   rs   r   s      @r:   test_logErrorLogsErrorNoRepr&UtilTests.test_logErrorLogsErrorNoRepr   s_     	 
 	'u|,q	,,-JKLr=   rF   Nr?   r   )	rH   rI   rJ   rK   rj   ru   ry   r   rL   rF   r=   r:   rl   rl   y   s    %*Mr=   rl   c                     \ rS rSrS[S jrS\S jrS]S jrS^S jrS[S jrS[S jr	S[S jr
S[S	 jrS[S
 jrS[S jrS[S jrS[S jrS[S jrS[S jrS[S jrS[S jrS[S jrS[S jrS[S jrS[S jrS[S jrS[S jrS[S jrS[S jrS[S jrS[S jrS[S jrS[S jrS[S jr S[S jr!S[S  jr"S[S! jr#S[S" jr$S[S# jr%S[S$ jr&S[S% jr'S[S& jr(S[S' jr)S[S( jr*S[S) jr+S[S* jr,S[S+ jr-S[S, jr.S[S- jr/S[S. jr0S[S/ jr1S[S0 jr2S[S1 jr3S[S2 jr4S[S3 jr5S[S4 jr6S[S5 jr7S[S6 jr8S[S7 jr9S[S8 jr:S[S9 jr;S[S: jr<S[S; jr=S[S< jr>S[S= jr?S[S> jr@S[S? jrAS[S@ jrBS[SA jrCS[SB jrDS[SC jrES[SD jrFS[SE jrGS[SF jrHS[SG jrIS[SH jrJS[SI jrKS[SJ jrLS[SK jrMS[SL jrNS[SM jrOS[SN jrPS[SO jrQS[SP jrRS[SQ jrSS[SR jrTS[SS jrU\VR                  " \XST5      S[SU j5       rY\VR                  " \XST5      S[SV j5       rZ\VR                  " \XST5      S[SW j5       r[S[SX jr\SYr]gZ)_DeferredTests   c                    S U l         S U l        S U l        U R                  [        R
                  [        R                  " 5       5        g r4   )callbackResultscallback2ResultserrbackResults
addCleanupr   setDebugginggetDebuggingrc   s    r:   setUpDeferredTests.setUp   sH      	
  	
  	
 	**E,>,>,@Ar=   c                    X4U l         US   $ Nr   r   rc   r6   r7   s      r:   	_callbackDeferredTests._callback   s    #|Awr=   c                    X4U l         g r4   r   r   s      r:   
_callback2DeferredTests._callback2   s     $r=   c                    X4U l         g r4   r   r   s      r:   _errbackDeferredTests._errback   s    "lr=   c                    [        5       nUR                  U R                  5        UR                  S5        U R	                  U R
                  5        U R                  U R                  S0 45        g Nhellor   r!   addCallbackr   rP   assertIsNoner   r^   r   rc   rR   s     r:   testCallbackWithoutArgs%DeferredTests.testCallbackWithoutArgs   sX    "**T^^,'"$--.--
B/?@r=   c                    [        5       nUR                  U R                  S5        UR                  S5        U R	                  U R
                  5        U R                  U R                  S0 45        g )Nworldr   )r   r   r   r   s     r:   testCallbackWithArgs"DeferredTests.testCallbackWithArgs   s[    "**T^^W5'"$--.--0BB/GHr=   c                    [        5       nUR                  U R                  SS9  UR                  S5        U R	                  U R
                  5        U R                  U R                  SSS045        g )Nr   )r   r   r   r   r   s     r:   testCallbackWithKwArgs$DeferredTests.testCallbackWithKwArgs   sb    "**T^^7;'"$--.--
Wg<N/OPr=   c                T   [        5       nUR                  U R                  5        UR                  U R                  5        UR	                  S5        U R                  U R                  5        U R                  U R                  S0 45        U R                  U R                  S0 45        g r   )
r!   r   r   r   rP   r   r   r^   r   r   r   s     r:   testTwoCallbacksDeferredTests.testTwoCallbacks   s    "**T^^,T__-'"$--.--
B/?@..R0@Ar=   c                    [        5       nUR                    U R                  5       nU R                  S[	        U5      5        U R                  [        US   S   5        U R                  SUS   S   5        g )NrZ   r   categoryzCtwisted.internet.defer.Deferred.callbacks was deprecated in Twistedmessage)r!   	callbacksflushWarningsr^   r_   rr   DeprecationWarningassertIn)rc   rR   warningss      r:   !test_callbacksAttributeDeprecated/DeferredTests.test_callbacksAttributeDeprecated   sg    "**%%'CM*((1+j*ABQQK	"	
r=   c                &   [        S5      n[        5       nUR                  U R                  [	        [
        S[        4   S5      5        UR                  U5        UR                  U R                  5        U R                  U R                  5        U R                  c   eU R                  [        U R                  S   5      S5        U R                  U R                  S   S   R                  U5        U R                  U R                  S   0 5        g)zA
If given None for an errback, addCallbacks uses a pass-through.
oopsie.Nr   rZ   )rD   r!   addCallbacksr   r   r	   r>   errbackr\   r   r   r   r   r^   r_   rb   rc   rs   rR   s      r:   test_addCallbacksNoneErrback*DeferredTests.test_addCallbacksNoneErrback   s     X&#+:dnnd8CK3H$.OPDMM*$../""...T0034a8,,Q/288%@,,Q/4r=   c           
     `   [        5       nUR                  U R                  U R                  [	        [
        [           S5      [	        [        [        [        4   S5      S0 5        UR                  S5        U R                  U R                  5        U R                  U R                  S0 45        g)zB
If given None as a callback args and kwargs, () and {} are used.
NrF   r   r   )r!   r   r   r   r   r   r>   r   strrP   r   r   r^   r   r   s     r:   !test_addCallbacksNoneCallbackArgs/DeferredTests.test_addCallbacksNoneCallbackArgs   s     #+*NNMMv%f%t,	
 	'"$--.--
B/?@r=   c                t   [        S5      n[        5       nUR                  U R                  U R                  S0 [        [        [           S5      [        [        [        [        4   S5      5        UR                  U5        UR                  U R                  5        U R                  U R                  5        U R                  c   eU R                  [!        U R                  S   5      S5        U R                  U R                  S   S   R"                  U5        U R                  U R                  S   0 5        g)zA
If given None as a errback args and kwargs, () and {} are used.
r   rF   Nr   rZ   )rD   r!   r   r   r   r   r   r>   r   r   r   r\   r   r   r   r^   r_   rb   r   s      r:    test_addCallbacksNoneErrbackArgs.DeferredTests.test_addCallbacksNoneErrbackArgs	  s    X&#+:NNMMv%f%t,	
 	DMM*$../""...T0034a8,,Q/288%@,,Q/4r=   c                |   [         [        [        [        [        [
        4   4      n[        5       n[        5       n[        5       n[        X#U/5      n/ nU4S	S jjnS
S jnUR                  Xw5        UR                  S5        UR                  U5        UR                  [        S5      5        UR                  S5        U R                  US   US   S   [	        [        [
        US   S   5      R                  5      4US   /[         R"                  S4[         R$                  S4[         R"                  S4/5        g )Nc                &    UR                  U 5        g r4   )extend
resultListrt   s     r:   cb*DeferredTests.testDeferredList.<locals>.cb(  s    MM*%r=   c                    g r4   rF   errs    r:   catch-DeferredTests.testDeferredList.<locals>.catch+      r=   123r   rZ      )r   
ResultListrt   r   r?   r   r   r0   r?   r   )r   r   boolr   r   r0   r!   r#   r   rP   r\   r   rD   r^   r   rb   r   SUCCESSFAILURE)	rc   r   defr1defr2defr3dlrt   r   r   s	            r:   testDeferredListDeferredTests.testDeferredList  s   %eCL&9 9:;
'z'z'z5/0<B 	&	 	s 	l3'(sq	1s41#>#D#DEFq		 mmS!EMM3#7%--9MN	
r=   c                    / nU4     SS jjn[        / 5      nUR                  U5        U R                  U/ /5        / US S & [        / SS9nUR                  U5        U R                  U/ 5        g )Nc                &    UR                  U 5        g r4   r]   r   s     r:   r   /DeferredTests.testEmptyDeferredList.<locals>.cbF  s     MM*%r=   TfireOnOneCallback)r   z_DeferredListResultListT[None]rt   z$List[_DeferredListResultListT[None]]r?   r   )r#   r   r^   )rc   rt   r   dl1dl2s        r:   testEmptyDeferredList#DeferredTests.testEmptyDeferredListC  s    79 <B	&6	&8	& 	& 9ER8H"&q	:F$;
 	$r=   c                    [        5       n[        5       n[        5       n[        XU/SS9n/ nUR                  UR                  5        S
S jnUR                  U5        UR	                  S5        U R                  U/ 5        UR                  [        S5      5        U R                  [        U5      S5        US   nU R                  [        UR                  [        R                  5      SUR                  < 35        UR                  nU R                  UR                  R                  [        5        U R                  UR                  R                  R                   S	5        U R                  UR"                  S5        g )NTfireOnOneErrbackc                    g r4   rF   r   s    r:   r   ;DeferredTests.testDeferredListFireOnOneError.<locals>.catcha  r   r=   r   	from def2rZ   r   zFissubclass(aFailure.type, defer.FirstError) failed: failure's type is )r   r   )r!   r#   r\   r]   rP   r^   r   rD   r_   r`   
issubclasstyper   
FirstErrorrb   
subFailurer6   index)	rc   r   r   r   r   rt   r   aFailure
firstErrors	            r:   testDeferredListFireOnOneError,DeferredTests.testDeferredListFireOnOneErrorW  s1   'z'z'z5/$G "
fmm$	 	 	s$ 	l;/0Va( !9 	x}}e&6&67&.mm6	
 ^^
 	..33\B..4499>J))1-r=   c                   [        5       n[        U/5      n/ nUR                  UR                  5        / nUR	                  UR                  5        UR                  [        S5      5        U R                  SUS   R                  R                  S   5        U R                  S[        U5      5        US   S   nUc   eU R                  SUS   R                  R                  S   5        g )NBangr   rZ   )r!   r#   r\   r]   r   r   rD   r^   rb   r6   r_   rc   d1r   	errorTrapresultListsfirstResults         r:   !testDeferredListDontConsumeErrors/DeferredTests.testDeferredListDontConsumeErrors  s    %Z2$#%	
i&&'<>
{))*


<'(1!3!3!8!8!;<C,-!!nQ'&&&Q!5!5!:!:1!=>r=   c                   [        5       n[        U/SS9n/ nUR                  UR                  5        / nUR	                  UR                  5        UR                  [        S5      5        U R                  / U5        U R                  S[        U5      5        US   S   nUc   eU R                  SUS   R                  R                  S   5        g )NTconsumeErrorsr  rZ   r   )r!   r#   r\   r]   r   r   rD   r^   r_   rb   r6   r  s         r:   testDeferredListConsumeErrors+DeferredTests.testDeferredListConsumeErrors  s    %Z2$d3#%	
i&&'<>
{))*


<'(Y'C,-!!nQ'&&&Q!5!5!:!:1!=>r=   c                
   [        5       n[        5       nUR                  [        S5      5        [        X/SS9n/ nUR	                  UR
                  5        U R                  S[        U5      5        UR	                  S 5        g )Nr  Tr   rZ   c                    g r4   rF   es    r:   <lambda>WDeferredTests.testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds.<locals>.<lambda>      r=   )r!   r   rD   r#   r\   r]   r^   r_   rc   r	  d2r   rt   s        r:   7testDeferredListFireOnOneErrorWithAlreadyFiredDeferredsEDeferredTests.testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds  sf    %Z%Z


<'( 2(T: "
fmm$CK(
n%r=   c                .   [        5       n[        5       nUR                  [        S5      5        UR                  S5        [	        X/5      n/ nUR                  UR                  5        U R                  S[        U5      5        UR                  S 5        g )Nr  r   rZ   c                    g r4   rF   r  s    r:   r  IDeferredTests.testDeferredListWithAlreadyFiredDeferreds.<locals>.<lambda>  r  r=   )
r!   r   rD   rP   r#   r   r]   r^   r_   r\   r  s        r:   )testDeferredListWithAlreadyFiredDeferreds7DeferredTests.testDeferredListWithAlreadyFiredDeferreds  so    $J$J


<'(
A 2(#/1
v}}%CK(
n%r=   c                    [        5       n[        5       n[        X/5      nUR                  5         U R                  U[        R
                  5        U R                  U[        R
                  5        g)zS
When cancelling an unfired L{DeferredList}, cancel every
L{Deferred} in the list.
Nr!   r#   cancelfailureResultOfr   r   rc   deferredOnedeferredTwodeferredLists       r:   test_cancelDeferredList%DeferredTests.test_cancelDeferredList  sW    
 '/j&.j#[$>?[%*>*>?[%*>*>?r=   c                   [        [        5      n[        5       n[        X/5      nUR                  5         U R	                  U[
        R                  5        U R                  U5      nU R                  US   S   5        U R                  US   S   S5        U R                  US   S   5        U R                  US   S   R                  [
        R                  5      5        g)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.
r   rZ   rN   N)r!   rS   r#   r&  r'  r   r   successResultOfr`   r^   assertFalsera   rc   r)  r*  r+  rt   s        r:   test_cancelDeferredListCallback-DeferredTests.test_cancelDeferredListCallback  s     &..C%D%-Z#[$>?[%*>*>?%%l3q	!%1'891&q	!**5+?+?@Ar=   c                    [        5       n[        5       n[        X/SS9nUR                  5         U R                  U[        R
                  5        U R                  U[        R
                  5        g)zz
When cancelling an unfired L{DeferredList} with the flag
C{fireOnOneCallback} set, cancel every L{Deferred} in the list.
Tr   Nr%  r(  s       r:   ,test_cancelDeferredListWithFireOnOneCallback:DeferredTests.test_cancelDeferredListWithFireOnOneCallback  sZ    
 '/j&.j#[$>RVW[%*>*>?[%*>*>?r=   c                    [        [        5      n[        5       n[        X/SS9nUR                  5         U R	                  U[
        R                  5        U R                  U5      nU R                  US5        g)z
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   )rN   r   N)	r!   rS   r#   r&  r'  r   r   r/  r^   r1  s        r:   ?test_cancelDeferredListWithFireOnOneCallbackAndDeferredCallbackMDeferredTests.test_cancelDeferredListWithFireOnOneCallbackAndDeferredCallback  si     &..C%D%-Z#[$>RVW[%*>*>?%%l3!78r=   c                   [        5       n[        5       n[        X/SS9nUR                  5         U R                  U[        R
                  5        U R                  U[        R
                  5        U R                  U[        R                  5      nUR                  nU R                  UR                  R                  [        R
                  5      5        g)zy
When cancelling an unfired L{DeferredList} with the flag
C{fireOnOneErrback} set, cancel every L{Deferred} in the list.
Tr   N)r!   r#   r&  r'  r   r   r   rb   r`   r   ra   )rc   r)  r*  r+  deferredListFailurer  s         r:   +test_cancelDeferredListWithFireOnOneErrback9DeferredTests.test_cancelDeferredListWithFireOnOneErrback  s    
 '/j&.j#[$>QUV[%*>*>?[%*>*>?"22<AQAQR(..

--33E4H4HIJr=   c                T   [        [        5      n[        [        5      n[        X/SS9nUR                  5         U R	                  U5      nU R                  US   S   5        U R                  US   S   S5        U R                  US   S   5        U R                  US   S   S5        g)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.
Tr   r   rZ   rN   N)r!   rS   r#   r&  r/  r`   r^   r1  s        r:   ?test_cancelDeferredListWithFireOnOneErrbackAllDeferredsCallbackMDeferredTests.test_cancelDeferredListWithFireOnOneErrbackAllDeferredsCallback	  s     &..C%D%-.C%D#[$>QUV%%l3q	!%1'89q	!%1'89r=   c                @   [        5       n[        5       nX/n[        U5      n[        5       nUR                  U5        UR                  5         U R	                  U[
        R                  5        U R	                  U[
        R                  5        U R                  U5        g)zO
Cancelling a L{DeferredList} will cancel the original
L{Deferred}s passed in.
N)r!   r#   r]   r&  r'  r   r   assertNoResult)rc   r)  r*  argumentListr+  deferredThrees         r:   ,test_cancelDeferredListWithOriginalDeferreds:DeferredTests.test_cancelDeferredListWithOriginalDeferreds  s    
 '/j&.j#1#L1(0
M*[%*>*>?[%*>*>?M*r=   c                   SS jn[        U5      n[        5       n[        X#/5      nUR                  5         U R                  U[        R
                  5        U R                  [        5      nU R                  [        U5      S5        g)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.
c                    [        S5      e)z`
A L{Deferred} canceller that raises an exception.

@param deferred: The cancelled L{Deferred}.
test)ro   rQ   s    r:   cancellerRaisesExceptionTDeferredTests.test_cancelDeferredListWithException.<locals>.cancellerRaisesException1  s     v&&r=   rZ   N)rR   Deferred[object]r?   r   )
r!   r#   r&  r'  r   r   rq   ro   r^   r_   )rc   rJ  r)  r*  r+  rx   s         r:   $test_cancelDeferredListWithException2DeferredTests.test_cancelDeferredListWithException*  sn    	' '//G&H&.j#[$>?[%*>*>?''5Va(r=   c                    [        5       n[        5       n[        X/SS9nUR                  S5        UR                  5         U R	                  U5        g)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.
Tr   N)r!   r#   rP   r&  rB  r(  s       r:   )test_cancelFiredOnOneCallbackDeferredList7DeferredTests.test_cancelFiredOnOneCallbackDeferredListA  sJ     '/j&.j#[$>RVWT"K(r=   c                $   [        5       n[        5       n[        X/SS9nUR                  [        S5      5        UR	                  5         U R                  U5        U R                  U[        5        U R                  U[        R                  5        g)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.
Tr   rI  N)	r!   r#   r   rD   r&  rB  r'  r   r   r(  s       r:   (test_cancelFiredOnOneErrbackDeferredList6DeferredTests.test_cancelFiredOnOneErrbackDeferredListN  su     '/j&.j#[$>QUVL01K([,7\5+;+;<r=   c                    / n[         R                  " S5      nUR                  UR                  5        U R	                  US/5        g )Nsuccess)r   succeedr   r]   r^   rc   lds      r:   testImmediateSuccess"DeferredTests.testImmediateSuccess]  s7     ==3	ahhYK(r=   c                    [        5       nUR                  S5        [        R                  " S5      nU R	                  UR
                  UR
                  5        g)zI
C{defer.succeed(x)} is the same as as C{d = Deferred(); d.callback(x)}.
rV  N)r!   rP   r   rW  r^   __dict__)rc   rZ  r  s      r:    test_succeedMatchesManualSuccess.DeferredTests.test_succeedMatchesManualSuccessc  s=     $:	

9!MM)4R[[1r=   c                    / n[         R                  " [        S5      5      nUR                  UR                  5        U R                  [        US   R                  5      S5        g Nfailr   )r   rc  rD   r\   r]   r^   r   rb   rX  s      r:   testImmediateFailure"DeferredTests.testImmediateFailurel  sF    !JJ|F';<	QXXQqTZZ&1r=   c                0   / n[         R                  " [        S5      5      nUR                  5         UR	                  UR
                  5        U R                  U/ 5        UR                  5         U R                  [        US   R                  5      S5        g rb  )
r   rc  rD   pauser\   r]   r^   unpauser   rb   rX  s      r:   testPausedFailureDeferredTests.testPausedFailurer  sh    JJ|F+,			QXXB			QqTZZ&1r=   c                   / n[        5       nUR                  S 5      R                  UR                  5        UR	                  S5        U R                  US   R                  [        5        / n[         [           " 5       nUR                  S 5      R                  UR                  5        UR	                  S5        U R                  US   R                  [        5        g )Nc                    SS-  $ NrZ   r   rF   _s    r:   r  2DeferredTests.testCallbackErrors.<locals>.<lambda>~  s    Qr=   rZ   r   c                (    [        [        5       5      $ r4   )r0   ZeroDivisionErrorrn  s    r:   r  rp    s    ):)<!=r=   )	r!   r   r\   r]   rP   assertIsInstancerb   rr  int)rc   rY  rZ  r  s       r:   testCallbackErrors DeferredTests.testCallbackErrors{  s    #:	&'22188<	

1adjj*;<c]_
=>II!((S
Aadjj*;<r=   c                    [        5       nUR                  5         UR                  U R                  5        UR	                  5         g r4   )r!   rg  r   r   rh  rc   rZ  s     r:   testUnpauseBeforeCallback'DeferredTests.testUnpauseBeforeCallback  s+    $J			dnn%			r=   c                Z  ^ [        5       n[        5       mTR                  5         UR                  U4S j5        UR                  U R                  5        UR	                  S5        U R                  U R                  S5        TR	                  S5        U R                  U R                  S5        TR                  5         U R                  U R                  S5        U R                  c   S5       eU R                  U R                  S	   S	   SS
R                  U R                  5      5        g)z
Callbacks passed to L{Deferred.addCallback} can return Deferreds and
the next callback will not be run until that Deferred fires.
c                   > T$ r4   rF   )rr  s    r:   r  <DeferredTests.test_callbackReturnsDeferred.<locals>.<lambda>  s    r=   rZ   z Should not have been called yet.r   z&Still should not have been called yet.zShould have been called nowNz%make that legible to the type checkerr   z/Result should have been from second deferred:{})r!   rg  r   r   rP   r   r   rh  assertIsNotNoneassertEqualsformatrc   r	  r  s     @r:   test_callbackReturnsDeferred*DeferredTests.test_callbackReturnsDeferred  s    
 %J$J


|$
t~~&
A$..0RS
A  "J	
 	

T113PQ##/X1XX/  #A&=DD$$	
r=   c                  ^^ [        5       nSm[        5       mSUU4S jjnUR                  S5        U R                  U R	                  UR                  U5      5      S5        [         [           " 5       nSmUR                  S5        U R                  U R                  UR                  U5      R                  U5      [        5      R                  T5        g)z6
Callbacks passed to addCallback may return Failures.
Fc                2   > T(       a  [        T5      $ U S-   $ NrZ   r/   )rt   rte
shouldFails    r:   	maybeFailADeferredTests.test_callbackMaybeReturnsFailure.<locals>.maybeFail  s    s|#z!r=         TN)rt   rt  r?   zUnion[int, Failure])
r!   ro   rP   r^   r/  r   rt  rr   r'  rb   )rc   rZ  r  r  r  s      @@r:    test_callbackMaybeReturnsFailure.DeferredTests.test_callbackMaybeReturnsFailure  s     $:
n	" 	" 	


1--ammI.FGKSMO
	

1  i(44Y? e	
r=   c                T  ^^ [        5       n[        5       m/ mSU4S jjnSU4S jjnUR                  U5      R                  U5        UR                  [	        5       5        U R                  U5        U R                  T/ 5        TR                  S5        U R                  TS/5        g)zW
Errbacks passed to L{Deferred.addErrback} can return Deferreds just as
callbacks can.
rt   c                   > T$ r4   rF   )rt   r  s    r:   asyncErrback?DeferredTests.test_errbackReturnsDeferred.<locals>.asyncErrback  s    Ir=   c                (   > TR                  U 5        g r4   r   )rt   resultValuess    r:   syncCallback?DeferredTests.test_errbackReturnsDeferred.<locals>.syncCallback  s    'r=   N)rt   r0   r?   Deferred[str])rt   zUnion[str, int]r?   r   )r!   r\   r   r   
ValueErrorrB  r^   rP   )rc   rZ  r  r  r  r  s       @@r:   test_errbackReturnsDeferred)DeferredTests.test_errbackReturnsDeferred  s    
 $:$J		( 	
\"..|<			*,Ar*
Hz2r=   c                r  ^ [        5       n[        5       mTR                  U5        TR                  5         [        5       nUR	                  U4S j5        UR                  S5        / nUR	                  UR
                  5        U R                  U/ 5        TR                  5         U R                  X1/5        g)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.
c                   > T$ r4   rF   )ignoredpauseds    r:   r  DDeferredTests.test_chainedPausedDeferredWithResult.<locals>.<lambda>  s    Fr=   N)r>   r!   rP   rg  r   r]   r^   rh  )rc   expectedchainedrt   r  s       @r:   $test_chainedPausedDeferredWithResult2DeferredTests.test_chainedPausedDeferredWithResult  s     8#+:!"**23!FMM*$,r=   c                  ^ [        5       n[        5       mUR                  U4S j5        UR                  S5        UR                  5         TR                  S5        / nTR                  UR                  5        U R                  US/5        g)z
A paused Deferred encountered while pushing a result forward through a
chain does not prevent earlier Deferreds from continuing to execute
their callbacks.
c                   > T$ r4   rF   r  seconds    r:   r  :DeferredTests.test_pausedDeferredChained.<locals>.<lambda>      &r=   N)r!   r   rP   rg  r]   r^   )rc   firstrt   r  s      @r:   test_pausedDeferredChained(DeferredTests.test_pausedDeferredChained  sl     !)
!)01t6==)$(r=   c                @   / n[         R                  " [         R                  " S5      [         R                  " S5      /5      R                  UR                  5        U R                  USS//5        / n[         R                  " S5      [         R                  " [        5       5      /n[         R                  " U5      R                  UR                  5        U R                  [        U5      S5        U R                  US   [        5        US   R                  S 5        g )NrZ   r   r   c                    gr  rF   r  s    r:   r  2DeferredTests.test_gatherResults.<locals>.<lambda>  s    1r=   )r   gatherResultsrW  r   r]   r^   rc  r  r\   r_   rs  r0   )rc   resultsrx   r   s       r:   test_gatherResults DeferredTests.test_gatherResults   s    #%U]]1-u}}Q/?@AMMNN	
 	Aq6(+ "mmA

:< 89B**6==9Va(fQi1
1%r=   c                &   [         R                  " S5      n[         R                  " [        S5      5      n[         R                  " X/SS9n/ nUR                  UR                  5        / nUR                  UR                  5        U R                  [        U5      [        U5      4S5        U R                  US   R                  [         R                  5        US   R                  R                  nU R                  UR                  [        5        g)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}.
rZ   zoh noesTr  r   rZ   r   N)r   rW  rc  ro   r  r\   r]   r^   r_   rs  rb   r   r   )rc   dgooddbadrZ  unconsumedErrorsgatheredErrorsr  s          r:   #test_gatherResultsWithConsumeErrors1DeferredTests.test_gatherResultsWithConsumeErrors  s     a zz,y12TB*,(//0(*	^**+#./^1DEvNnQ/55u7G7GH#A&,,77
j..=r=   c                   [        5       n[        5       n[        R                  " X/5      nUR                  5         U R	                  U[        R
                  5        U R	                  U[        R
                  5        U R	                  U[        R                  5      nUR                  nU R                  UR                  R                  [        R
                  5      5        g)zf
When cancelling the L{defer.gatherResults} call, all the
L{Deferred}s in the list will be cancelled.
N)r!   r   r  r&  r'  r   r   rb   r`   r   ra   )rc   r)  r*  rt   gatherResultsFailurer  s         r:   test_cancelGatherResults&DeferredTests.test_cancelGatherResults$  s    
 '/j&.j$$k%?@[%*>*>?[%*>*>?#33FE<L<LM)//

--33E4H4HIJr=   c                   [        [        5      n[        [        5      n[        R                  " X/5      nUR	                  5         U R                  U5      nU R                  US   S5        U R                  US   S5        g)z
When cancelling the L{defer.gatherResults} call, if all the
L{Deferred}s callback in their canceller, the L{Deferred}
returned by L{defer.gatherResults} will be callbacked with the C{list}
of the results.
r   rN   rZ   N)r!   rS   r   r  r&  r/  r^   )rc   r)  r*  rt   callbackResults        r:   0test_cancelGatherResultsWithAllDeferredsCallback>DeferredTests.test_cancelGatherResultsWithAllDeferredsCallback3  sr     &..C%D%-.C%D$$k%?@--f5*,=>*,=>r=   c                &  ^ [        5       m/ n/ n[        R                  " U4S j5      nUR                  UR                  UR                  5        U R                  U/ 5        U R                  [        U5      S5        U R                  US   T5        g)zw
L{defer.maybeDeferred} should retrieve the result of a synchronous
function and pass it to its resulting L{Deferred}.
c                    > T $ r4   rF   rt   s   r:   r  6DeferredTests.test_maybeDeferredSync.<locals>.<lambda>J  s    r=   rZ   r   N)r>   r   maybeDeferredr   r]   r^   r_   assertIdentical)rc   r  rx   rZ  rt   s       @r:   test_maybeDeferredSync$DeferredTests.test_maybeDeferredSyncB  st    
  " "/	w~~v}}5$Wq)WQZ0r=   c                   SS jn/ n/ n[         R                  " US5      n[        U[        [           5        UR                  UR                  UR                  5        U R                  U/ 5        U R                  US/5        g)zF
L{defer.maybeDeferred} should pass arguments to the called function.
c                    U S-   $ )N   rF   xs    r:   plusFive>DeferredTests.test_maybeDeferredSyncWithArgs.<locals>.plusFiveU  s    q5Lr=   
      N)r  rt  r?   rt  )r   r  r   r!   rt  r   r]   r^   )rc   r  r  rx   rZ  s        r:   test_maybeDeferredSyncWithArgs,DeferredTests.test_maybeDeferredSyncWithArgsP  sk    
	   ""-Ax}%	w~~v}}5$2$'r=   c                d  ^ [        S5      mSU4S jjn/ n/ n[        R                  " U5      R                  UR                  UR                  5        U R                  U/ 5        U R                  [        U5      S5        U R                  [        US   R                  5      [        T5      5        g)z
L{defer.maybeDeferred} should catch an exception raised by a synchronous
function and errback its resulting L{Deferred} with it.
zthat value is unacceptablec                    > T er4   rF   r  s   r:   raisesExceptionFDeferredTests.test_maybeDeferredSyncException.<locals>.raisesExceptiong  s    Nr=   rZ   r   N)r?   r   )	r  r   r  r   r]   r^   r_   r   rb   )rc   r  r  rx   r  s       @r:   test_maybeDeferredSyncException-DeferredTests.test_maybeDeferredSyncException`  s    
 :;	   "O,99'..&--X"%Va(VAY__-s8}=r=   c                R  ^  SS-     / n/ n[        R                  " U4S j5      nUR	                  UR
                  UR
                  5        U R                  U/ 5        U R                  [        U5      S5        U R                  US   T5        g! [          a    [        5       m Nf = f)z_
L{defer.maybeDeferred} should handle a L{Failure} returned by a
function and errback with it.
10r  c                    > T $ r4   rF   r  s   r:   r  =DeferredTests.test_maybeDeferredSyncFailure.<locals>.<lambda>}  s    r=   rZ   r   N)		TypeErrorr0   r   r  r   r]   r^   r_   r  )rc   r  rx   rZ  r  s       @r:   test_maybeDeferredSyncFailure+DeferredTests.test_maybeDeferredSyncFailureq  s    
	!1H   " 01	w~~v}}5"%Va(VAY1  	!yH	!s   B B&%B&c                   ^ [        5       m[        R                  " U4S j5      nTR                  S5        / nUR	                  UR
                  5        U R                  US/5        g)ze
L{defer.maybeDeferred} should let L{Deferred} instance pass by
so that original result is the same.
c                    > T $ r4   rF   r	  s   r:   r  7DeferredTests.test_maybeDeferredAsync.<locals>.<lambda>  s    r=   SuccessN)r!   r   r  rP   r   r]   r^   )rc   r  rt   r	  s      @r:   test_maybeDeferredAsync%DeferredTests.test_maybeDeferredAsync  sO    
 %J  ,
I
v}}%)-r=   c                   ^ [        5       m[        R                  " U4S j5      nTR                  [	        [        5       5      5        U R                  U[
        5        g)z
L{defer.maybeDeferred} should let L{Deferred} instance pass by
so that L{Failure} returned by the original instance is the
same.
c                    > T $ r4   rF   r  s   r:   r  <DeferredTests.test_maybeDeferredAsyncError.<locals>.<lambda>  s    r=   N)r!   r   r  r   r0   ro   rg   )rc   r  r	  s     @r:   test_maybeDeferredAsyncError*DeferredTests.test_maybeDeferredAsyncError  s?     &Z"00<


7<>*+##B5r=   c                    SS jn[         R                  " U5      n[        U[        [           5        U R                  U R                  U5      S5        g)
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                    #    g7fNr  rF   rF   r=   r:   r9   ;DeferredTests.test_maybeDeferredCoroutineSuccess.<locals>.f          r  Nr?   rt  )r   r  r   r!   rt  r^   r/  )rc   r9   coroutineDeferreds      r:   "test_maybeDeferredCoroutineSuccess0DeferredTests.test_maybeDeferredCoroutineSuccess  sE    	 "//2%x}5--.?@!Dr=   c                   ^ SS jmSU4S jjnSS jn[         R                  " U5      n[        U[        [           5        UR                  U5      nU R                  U R                  U5      S5        g)	r  c                    #    g7fr  rF   rF   r=   r:   r9   CDeferredTests.test_maybeDeferredCoroutineSuccessIndirect.<locals>.f  r  r  c                    > T " 5       $ r4   rF   r9   s   r:   gCDeferredTests.test_maybeDeferredCoroutineSuccessIndirect.<locals>.g  
    3Jr=   c                    U S-   $ r  rF   r  s    r:   typedCallbackODeferredTests.test_maybeDeferredCoroutineSuccessIndirect.<locals>.typedCallback  s    A:r=      Nr  )r?   z"Coroutine[Deferred[int], Any, Any])rt   rt  r?   rt  )r   r  r   r!   r   r   r^   r/  )rc   r  r  r  modifiedDeferredr9   s        @r:   *test_maybeDeferredCoroutineSuccessIndirect8DeferredTests.test_maybeDeferredCoroutineSuccessIndirect  sa    	
	
	 "//2%x}5,88G--.>?Cr=   c                   ^^  " S S[         5      mSU4S jjmSU4S jjn[        U R                  [        R                  " U5      5      R
                  [        T5      5        g)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                      \ rS rSrSrg)GDeferredTests.test_maybeDeferredCoroutineFailure.<locals>.SomeExceptioni  rF   NrG   rF   r=   r:   SomeExceptionr        r=   r  c                    >#    T " 5       e7fr4   rF   )r  s   r:   r9   ;DeferredTests.test_maybeDeferredCoroutineFailure.<locals>.f  s     /!s   c                    > T " 5       $ r4   rF   r  s   r:   r  ;DeferredTests.test_maybeDeferredCoroutineFailure.<locals>.g  r  r=   Nr   )r?   zCoroutine[None, None, None])	Exceptionr   r'  r   r  r   r   )rc   r  r  r9   s     @@r:   "test_maybeDeferredCoroutineFailure0DeferredTests.test_maybeDeferredCoroutineFailure  sJ    	I 		"
	 	  !4!4Q!78==]#	
r=   c                  ^^^ / m/ m[        5       mSUUU4S jjn[        R                  " S5      nUR                  U5        TR	                  S5        UR                  TR
                  5        TR                  TR
                  5        UR                  TR
                  5        U R                  / T5        U R                  T/ SQ5        g)z
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.
c                   > TR                  SU 45        [        R                  " S5      nSUU4S jjnSU4S jjnUR                  U5      R                  U5        UR	                  TR                   5        U$ )Nstart-of-cbinnerc                *   > TR                  S5        T$ NfirstCallbackr  r   rt   r  r  s    r:   r  MDeferredTests.test_innerCallbacksPreserved.<locals>.cb.<locals>.firstCallback  s    9:r=   c                4   > TR                  SU 45        U S-  $ NsecondCallbackr   r   rt   r  s    r:   r$  NDeferredTests.test_innerCallbacksPreserved.<locals>.cb.<locals>.secondCallback       0&9:z!r=   rt   r   r?   r  rt   r   r?   r   r]   r   rW  r   r\   rt   rZ  r  r$  rf   r  r  s       r:   r   6DeferredTests.test_innerCallbacksPreserved.<locals>.cb  s_    NNM623g&A " MM-(44^DLL)Hr=   outeroranger  r-  r  )r$  r.  orangeorangeNr(  )r!   r   rW  r   rP   r]   r\   r^   rc   r   r-  rf   r  r  s      @@@r:   test_innerCallbacksPreserved*DeferredTests.test_innerCallbacksPreserved  s     68"$'z	 	  g&"x '..)))X&	
r=   c                T  ^^^ / m/ m[        5       mSUUU4S jjn[        R                  " S5      R                  U5      R	                  TR
                  5        U R                  SS/T5        TR                  S5        U R                  / T5        U R                  TSSSS/4/5        g)	z
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.
c                   > TR                  SU 45        [        5       nSUU4S jjnSU4S jjnUR                  U5      R                  U5      R                  TR                   5      nUR	                  S 5        U$ )Nr   c                X   > TR                  SU 45        [        R                  " T/5      $ Nr  r]   r   r  rt   ar  s    r:   r  NDeferredTests.test_continueCallbackNotFirst.<locals>.cb.<locals>.firstCallback  '    89**A3//r=   c                ,   > TR                  SU 45        g Nr$  r   r%  s    r:   r$  ODeferredTests.test_continueCallbackNotFirst.<locals>.cb.<locals>.secondCallback       0&9:r=   rt   r   r?   Deferred[List[str]]rt   	List[str]r?   r   r]   r!   r   r\   rP   rt   rZ  r  r$  returnerr;  rf   r  s        r:   r   7DeferredTests.test_continueCallbackNotFirst.<locals>.cb  g    NND&>* (
A0 0; m,^,HOO, 
 JJtOr=   r-  r   r-  r  Nwithersr$  Nrt   r   r?   Deferred[None])r!   r   rW  r   r\   r]   r^   rP   )rc   r   r;  rf   r  s     @@@r:   test_continueCallbackNotFirst+DeferredTests.test_continueCallbackNotFirst  s     BD"$#:	 	& 	g**2.99(//J/+BCWM	

9X&58H9+7VW	
r=   c                  ^^^ / m/ m[        5       mS
UUU4S jjn[        5       nUR                  U5        UR                  U4S j5        UR                  TR                  5        UR	                  S5        U R                  SS/T5        TR	                  S5        U R                  / T5        U R                  TSSSS/4S/5        g	)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.
c                   > TR                  SU 45        [        5       nSUU4S jjnSU4S jjnUR                  U5      R                  U5      R                  TR                   5      nUR	                  S 5        U$ )Nr   c                X   > TR                  SU 45        [        R                  " T/5      $ r8  r9  r:  s    r:   r  LDeferredTests.test_callbackOrderPreserved.<locals>.cb.<locals>.firstCallback9  r=  r=   c                ,   > TR                  SU 45        g r?  r   r%  s    r:   r$  MDeferredTests.test_callbackOrderPreserved.<locals>.cb.<locals>.secondCallback=  rA  r=   rB  rD  rF  rG  s        r:   r   5DeferredTests.test_callbackOrderPreserved.<locals>.cb5  rJ  r=   c                &   > TR                  S5      $ )NfinalNr   )r  r  s    r:   r  ;DeferredTests.test_callbackOrderPreserved.<locals>.<lambda>J  s    GNN?$Cr=   r-  rK  rL  rM  r$  rZ  NrN  )r!   r   r\   r]   rP   r^   )rc   r   r-  r;  rf   r  s      @@@r:   test_callbackOrderPreserved)DeferredTests.test_callbackOrderPreserved+  s     BD"$#:	 	&  (z"CD)w/+BCWM	

9X&'!I;/		
r=   c                   ^^^ [        5       m/ mSU4S jjmSU4S jjnSUUU4S jjnTR                  U5        TR                  U5        TR                  S5        U R                  T/ SQ5        g)zw
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                (   > TR                  S5        g N   r   rt   calleds    r:   	callback3;DeferredTests.test_reentrantRunCallbacks.<locals>.callback3b      MM!r=   c                (   > TR                  S5        g Nr   r   rc  s    r:   	callback2;DeferredTests.test_reentrantRunCallbacks.<locals>.callback2e  rg  r=   c                J   > TR                  S5        TR                  T5        g r  r]   r   )rt   re  rd  rR   s    r:   	callback1;DeferredTests.test_reentrantRunCallbacks.<locals>.callback1h  s    MM!  +r=   N)rZ   r   rb  rt   r   r?   r   r!   r   rP   r^   )rc   rj  rn  re  rd  rR   s      @@@r:   test_reentrantRunCallbacks(DeferredTests.test_reentrantRunCallbacksZ  sa    
 $,:			, 	, 	Y'Y'$+r=   c                   ^ ^^^ [        5       m/ mSU4S jjmSUUUU 4S jjnTR                  U5        TR                  S5        T R                  TSS/5        g)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                (   > TR                  S5        g ri  r   rc  s    r:   rj  ;DeferredTests.test_nonReentrantCallbacks.<locals>.callback2y  rg  r=   c                p   > TR                  S5        TR                  T5        TR                  TS/5        g r  )r]   r   r^   )rt   rj  rd  rR   rc   s    r:   rn  ;DeferredTests.test_nonReentrantCallbacks.<locals>.callback1|  s/    MM!  +VaS)r=   NrZ   r   rp  rq  )rc   rn  rj  rd  rR   s   ` @@@r:   test_nonReentrantCallbacks(DeferredTests.test_nonReentrantCallbacksq  sR    
 $,:		* 	*
 	Y'$!Q(r=   c                   ^^^ Sm[        5       mSU4S jjmSUU4S jjnTR                  U5        TR                  S5        U R                  T[        5      nU R                  UR                  T45        g)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.
zcallback raised exceptionc                   > [        T5      er4   )r  )rt   exceptionMessages    r:   rj  FDeferredTests.test_reentrantRunCallbacksWithFailure.<locals>.callback2  s    ,--r=   c                (   > TR                  T5        g r4   )r   )rt   rj  rR   s    r:   rn  FDeferredTests.test_reentrantRunCallbacksWithFailure.<locals>.callback1  s      +r=   Nrt   r>   r?   r   )r!   r   rP   rg   r  r^   r6   )rc   rn  rd   rj  rR   r}  s      @@@r:   %test_reentrantRunCallbacksWithFailure3DeferredTests.test_reentrantRunCallbacksWithFailure  si     7#+:	.	, 	, 	Y'$//)D	*:)<=r=   c                h  ^ [        5       n[        R                  " U5      m[        5       nUR	                  U4S j5        UR                  S5        / nTR	                  UR                  5        U R                  US   5        UR	                  UR                  5        U R                  US   U5        g)z
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}.
c                   > T$ r4   rF   ignr  s    r:   r  =DeferredTests.test_synchronousImplicitChain.<locals>.<lambda>      ur=   Nr   rZ   )	r>   r   rW  r!   r   rP   r]   r   rr   )rc   rt   r  r  r  s       @r:   test_synchronousImplicitChain+DeferredTests.test_synchronousImplicitChain  s     f%!),-*,'..)'!*%7>>*gaj&)r=   c                  ^ [        5       m[        5       nUR                  U4S j5        UR                  S5        / nTR                  UR                  5        / nUR                  UR                  5        U R	                  U/ 5        U R	                  U/ 5        [        5       nTR                  U5        U R	                  US/5        U R	                  X4/5        g)a  
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}.
c                   > T$ r4   rF   r  s    r:   r  >DeferredTests.test_asynchronousImplicitChain.<locals>.<lambda>  r  r=   N)r!   r   rP   r]   r^   r>   )rc   r  r  secondResultrt   r  s        @r:   test_asynchronousImplicitChain,DeferredTests.test_asynchronousImplicitChain  s     #+*#+:,-$&+,,-%'<../b)r*vtf-x0r=   c                8   [         R                  " [        S5      5      nU4SS jjn[        5       nUR	                  U5        UR                  S5        / nUR	                  UR                  5        U R                  US   5        U R                  U[        5        g)z
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                    U$ r4   rF   )ro  r  s     r:   r   <DeferredTests.test_synchronousImplicitErrorChain.<locals>.cb  s    Lr=   Nr   )ro  r   r  rO  r?   rO  )	r   rc  ro   r!   r   rP   r]   r   rg   )rc   r  r   r  r  s        r:   "test_synchronousImplicitErrorChain0DeferredTests.test_synchronousImplicitErrorChain  s~     

<(BCD05 	 "*2"$+,,-+a.)##FL9r=   c                N  ^ [        5       m[        5       nUR                  U4S j5        UR                  S5        / nUR                  UR                  5        / nTR                  UR                  5        / nUR                  UR                  5        U R                  U/ 5        U R                  U/ 5        TR                  [        S5      5        U R                  US   R                  [        5      5        U R                  US/5        U R                  [        U5      S5        g)a   
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.
c                   > T$ r4   rF   r  s    r:   r  CDeferredTests.test_asynchronousImplicitErrorChain.<locals>.<lambda>  r  r=   Nr  r   rZ   )r!   r   rP   r\   r]   r^   r   ro   r`   ra   r_   )rc   r  secondErrorr  r  r  s        @r:   #test_asynchronousImplicitErrorChain1DeferredTests.test_asynchronousImplicitErrorChain  s     !)
!),-%'+,,-"$+,,-#%<../b)r*l#=>?A,,\:;tf-\*A.r=   c                  ^^ [        5       m[        5       mTR                  U4S j5        [        5       nUR                  U4S j5        / nUR                  UR                  5        [        5       nTR	                  S5        UR	                  S5        U R                  U/ 5        TR	                  U5        U R                  X#/5        g)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.
c                   > T$ r4   rF   r  s    r:   r  GDeferredTests.test_doubleAsynchronousImplicitChaining.<locals>.<lambda>  r  r=   c                   > T$ r4   rF   )r  r  s    r:   r  r    s    fr=   N)r!   r   r]   r>   rP   r^   )rc   thirdthirdResultrt   r  r  s       @@r:   'test_doubleAsynchronousImplicitChaining5DeferredTests.test_doubleAsynchronousImplicitChaining  s     #+*#+:,-"**,-$&+,,-t 	b) 	vh/r=   c                  ^^^ / m/ m[        5       mS	UUU4S jjn[        R                  " S5      R                  U5      R                  TR                  5      nU R                  TSS/5        TR                  S5        TR                  TR                  5        UR                  TR                  5        U R                  / TS5        U R                  T/ SQ5        g)
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.
c                   > TR                  SU 45        [        R                  " S5      nSUU4S jjnSU4S jjnUR                  U5      R                  U5        UR	                  TR                   5        U$ )Nr  r  c                *   > TR                  S5        T$ r  r   r   s    r:   r  XDeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb.<locals>.firstCallback&  s    9: r=   c                4   > TR                  SU 45        U S-  $ r#  r   r%  s    r:   r$  YDeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb.<locals>.secondCallback,  r'  r=   r(  r)  r*  r+  s       r:   r   ADeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb"  s_    NNM623g&A " MM-(44^DLL)Hr=   r-  r0  r  r.  &Got errbacks but wasn't expecting any.r/  Nr(  r!   r   rW  r   r]   r^   rP   r\   r2  s      @@@r:   'test_nestedAsynchronousChainedDeferreds5DeferredTests.test_nestedAsynchronousChainedDeferreds  s     68"$  (z	 	, MM'"..r2>>w~~N 	
 	.0JK	
 	x  	))X'OP	
r=   c                  ^^^ / m/ m[        5       mS	UUU4S jjn[        R                  " S5      nUR                  U5        UR                  TR                  5        U R                  TSS/5        TR                  S5        UR                  TR                  5        TR                  TR                  5        U R                  / TS5        U R                  TSSSS/4SS//5        g)
a  
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.
c                   > TR                  SU 45        [        R                  " S5      nSUU4S jjnSU4S jjnUR                  U5      R                  U5      R	                  TR                   5      $ )Nr  r  c                V   > TR                  SU 45        SS jnTR                  U5      $ )Nr  c                    U /$ r4   rF   r  s    r:   	transform}DeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.firstCallback.<locals>.transformm  s	    "8Or=   )rt   r   r?   rE  rm  )rt   r  r  r  s     r:   r  jDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.firstCallbackf  s,    89$ ((33r=   c                4   > TR                  SU 45        U S-  $ r#  r   r%  s    r:   r$  kDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.secondCallbackr  r'  r=   )rt   r   r?   rC  )rt   rE  r?   rE  r*  r+  s       r:   r   SDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cbb  s\    NNM623g&A
4 
4"
 m,^,HOO,r=   r-  r0  r  rM  r  r$  N)rt   r   r?   zDeferred[Optional[List[str]]]r  r2  s      @@@r:   9test_nestedAsynchronousChainedDeferredsWithExtraCallbacksGDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacksS  s      "$  (z	 	: g&"'..) 	.0JK	
 	y! 	))X'OP(*!I;/I&		
r=   c                    [        5       n[        5       nUR                  U5        U R                  UR                  U5        g)zD
When we chain a L{Deferred}, that chaining is recorded explicitly.
N)r!   chainDeferredrr   
_chainedTorc   r;  bs      r:   &test_chainDeferredRecordsExplicitChain4DeferredTests.test_chainDeferredRecordsExplicitChain  s0     %J$J	allA&r=   c                    [        5       n[        5       nUR                  U5        UR                  S5        U R                  UR                  5        g)a"  
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  rP   r   r  r  s      r:   %test_explicitChainClearedWhenResolved3DeferredTests.test_explicitChainClearedWhenResolved  s<     %J$J		

4!,,'r=   c                   ^ [        5       n[        5       mUR                  U4S j5        UR                  S5        U R                  UR                  T5        g)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.
c                   > T$ r4   rF   )r  r  s    r:   r  FDeferredTests.test_chainDeferredRecordsImplicitChain.<locals>.<lambda>  s    ar=   N)r!   r   rP   rr   r  r  s     @r:   &test_chainDeferredRecordsImplicitChain4DeferredTests.test_chainDeferredRecordsImplicitChain  s>     %J$J	'(	

4allA&r=   c                ~  ^ [        5       mS
U4S jjnTR                  U5        TR                  S5        U R                  U/5      nU R	                  [        U5      S5        US   nU R	                  US   [        5        SnU R                  [        R                  " XCS   5      SR                  XCS   5      5        g	)zl
When a Deferred is returned from a callback directly attached to that
same Deferred, a warning is emitted.
c                   > T$ r4   rF   rt   rZ  s    r:   circularCallbackADeferredTests.test_circularChainWarning.<locals>.circularCallback      Hr=   foorZ   r   r   z1Callback returned the Deferred it was attached tor   z
Expected match: {!r}
Got: {!r}Nr(  )r!   r   rP   r   r^   r_   r   r`   researchr  )rc   r  circular_warningswarningpatternrZ  s        @r:   test_circularChainWarning'DeferredTests.test_circularChainWarning  s    
 $:	 	
&'	

5 ..0@/AB./3#A&,.@AEIIgy12/66w	@RS	
r=   c                @  ^ U R                  [        [        S[        R                  5        [        R                  " S[
        S9  [        5       mSU4S jjnTR                  U5        TR                  S5        U R                  T5      nUR                  [
        5        g)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.
filtersrs   )r   c                   > T$ r4   rF   r  s    r:   r  CDeferredTests.test_circularChainException.<locals>.circularCallback  r  r=   r  Nr(  )r   setattrr   r  filterwarningsr   r!   r   rP   r'  trap)rc   r  failurerZ  s      @r:   test_circularChainException)DeferredTests.test_circularChainException  sz     			
 	2DE#:	 	
&'	

5&&q)'(r=   c                n    [        5       n[        U5      nU R                  [        U5      SUS S35        g)zh
The C{repr()} of a L{Deferred} contains the class name and a
representation of the internal Python ID.
<Deferred at 0xr  >N)r!   idr^   repr)rc   rZ  addresss      r:   	test_reprDeferredTests.test_repr  s3    
 %JQ%aOGA;a"@Ar=   c                    [        5       nUR                  S5        U R                  [        U5      S[	        U5      S S35        g)zJ
If a L{Deferred} has been fired, then its C{repr()} contains its
result.
r.  r  r  z current result: 'orange'>N)r!   rP   r^   r  r  rx  s     r:   test_reprWithResult!DeferredTests.test_reprWithResult  s;    
 $:	

8aOBqE!9<V"WXr=   c           	         [        5       n[        5       nUR                  U5        U R                  [        U5      S[	        U5      S S[	        U5      S S35        g)z
If a L{Deferred} C{a} has been fired, but is waiting on another
L{Deferred} C{b} that appears in its callback chain, then C{repr(a)}
says that it is waiting on C{b}.
r  r  z waiting on Deferred at 0xr  N)r!   r  r^   r  r  r  s      r:   test_reprWithChaining#DeferredTests.test_reprWithChaining  sR     %J$J	GbeAY&@Aq	K	
r=   c                R   ^  SU 4S jjnT R                  U" S5      U" S5      5        g)z_
The depth of the call stack does not grow as more L{Deferred} instances
are chained together.
c                l  >^
^ / mSU4S jjn[        5       n[        U 5       Vs/ s H  n[        5       PM     nnUS S  n[        5       m
UR                  5       nU4     SS jjnUR                  U5        UR                  U5        U(       a9  UR                  5       nU4     SS jjn	UR                  U	5        UnU(       a  M9  UR                  U
4S j5        UR	                  S 5        U H  nUR	                  S 5        M     TR                  T/ 5        T
R	                  S 5        TS   $ s  snf )Nc                `   > TR                  [        [        R                  " 5       5      5        g r4   )r]   r_   	tracebackextract_stack)r  stacks    r:   recordStackDepthVDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.recordStackDepth  s    S!8!8!:;<r=   c                    U$ r4   rF   r  r  s     r:   cbInnerMDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.cbInner"  s	     r=   c                    U$ r4   rF   r  s     r:   
cbNewInnerPDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.cbNewInner-  s	     !Lr=   c                   > T$ r4   rF   )r  lasts    r:   r  NDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.<lambda>5  s    $r=   r   r  r>   r?   r   )r  r>   r  rO  r?   rO  )r!   rangepopr   rP   r^   )howManyr  topr  innerDeferredsoriginalInnersr  r  newInnerr  r  r  rc   s             @@r:   chainDeferreds<DeferredTests.test_boundedStackDepth.<locals>.chainDeferreds  sH   E= #+*C*/.4*8w
.  4 ,A.N#+:D"&&(E :?(6
 OOG$OO,- )--/ >F!#!,:!#!
 !!*-  !. ./LL't$ (
 UB' MM$8OQ4s   D1rZ   r   N)r  rt  r?   rt  )r^   )rc   r  s   ` r:   test_boundedStackDepth$DeferredTests.test_boundedStackDepth  s%    /	f 	*N1,=>r=   c                ^  ^^ [        5       n[        5       m[        5       mUR                  U4S j5        TR                  U4S j5        TR                  S5        UR                  S5        TR                  S5        / nTR                  UR                  5        U R	                  US/5        g)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.
c                   > T$ r4   rF   r  s    r:   r  ZDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled.<locals>.<lambda>V  r  r=   c                   > T$ r4   rF   )r  r  s    r:   r  r  W  s    5r=   N)r!   r   rP   r]   r^   )rc   r  r  r  r  s      @@r:   :test_resultOfDeferredResultOfDeferredOfFiredDeferredCalledHDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalledH  s     !)
!) (
0101tt 7>>*4&)r=   c                   [         R                  " S5        [        5       n/ n[        S5      n Ue! [         a    UR                  5          Of = fUR                  UR                  5        US   nU R                  UR                  U5        UR                  S   SS u  pVU R                  SU5        U R                  SU5        g)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.
Fr  r   NrF   )r   r   r!   rD   BaseExceptionr   r\   r]   r^   rb   framesrc   rZ  rY  excrc  localzglobalzs          r:   test_errbackWithNoArgsNoDebug+DeferredTests.test_errbackWithNoArgsNoDebug_  s     	5!$J6"	I 	IIK		QXXtS)++a.-V$W%   1 AAc                   [         R                  " S5        [        5       n/ n[        S5      n Ue! [         a    UR                  5          Of = fUR                  UR                  5        US   nU R                  UR                  U5        UR                  S   SS u  pVU R                  / U5        U R                  / U5        g)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.
Tr  r   r  N)r   r   r!   rD   r  r   r\   r]   r^   rb   r  assertNotEqualr  s          r:   test_errbackWithNoArgs$DeferredTests.test_errbackWithNoArgst  s     	4 $J6"	I 	IIK		QXXtS)++a.-B'B(r   c                F   [        5       nUR                  S5        [        R                  " S5        SS jnUR	                  U5        / nUR                  UR                  5        US   nUR                  S   SS u  pVU R                  SU5        U R                  SU5        g)
An error raised by a callback creates a Failure.  The Failure captures
locals and globals if and only if C{Deferred.debug} is set.
NFc                    [        S5      eNr  rD   r  s    r:   
raiseErrorHDeferredTests.test_errorInCallbackDoesNotCaptureVars.<locals>.raiseError      v&&r=   r   r  rF   r  )	r!   rP   r   r   r   r\   r]   r  r^   rc   rZ  r+  rY  rc  r  r  s          r:   &test_errorInCallbackDoesNotCaptureVars4DeferredTests.test_errorInCallbackDoesNotCaptureVars  s    
 %J	

45!	' 	
j!	QXXt++a.-V$W%r=   c                F   [        5       nUR                  S5        [        R                  " S5        SS jnUR	                  U5        / nUR                  UR                  5        US   nUR                  S   SS u  pVU R                  / U5        U R                  / U5        g)r&  NTc                    [        S5      er(  r)  r*  s    r:   r+  ODeferredTests.test_errorInCallbackCapturesVarsWhenDebugging.<locals>.raiseError  r-  r=   r   r  r  )	r!   rP   r   r   r   r\   r]   r  r"  r.  s          r:   -test_errorInCallbackCapturesVarsWhenDebugging;DeferredTests.test_errorInCallbackCapturesVarsWhenDebugging  s    
 %J	

44 	' 	
j!	QXXt++a.-B'B(r=   c                    SS jn[         R                  " U" 5       5      n[        U[         [           5        U R	                  SU R                  U5      5        g)z=
It can discover the return type of a first level coroutine.
c                    #    g7fr  rF   rF   r=   r:   
returnsInt4DeferredTests.test_fromCoroutine.<locals>.returnsInt  r  r  rZ   Nr  )r!   r5   r   rt  r^   r/  )rc   r8  rZ  s      r:   test_fromCoroutine DeferredTests.test_fromCoroutine  sD    
	 "":<0Ax}%D0034r=   c                    S SSU R                   S[        /nU H2  nU R                  [        R                  [        R
                  U5        M4     g)q
L{Deferred.fromCoroutine} requires a coroutine object or a generator,
and will reject things that are not that.
c                    U $ r4   rF   r  s    r:   r  CDeferredTests.test_fromCoroutineRequiresCoroutine.<locals>.<lambda>      ar=   rZ   TN#test_fromCoroutineRequiresCoroutiner   assertRaisesNotACoroutineErrorr!   r5   rc   thingsThatAreNotCoroutinesthings      r:   rB  1DeferredTests.test_fromCoroutineRequiresCoroutine  O     44&
" 0Ee668N8NPUV 0r=   GC works differently on PyPy.c                   [        5       n[        R                  " U5      n[        U5      nX1l        [        R                  " U5      nUR                  S5        AAU R                  U" 5       5        U R                  U" 5       5        g)zd
A circular reference between a `Deferred` and its canceller
is broken when the deferred is called.
N)DummyCancellerweakrefrefr!   rR   rP   r   )rc   	cancellerweakCancellerrR   weakDeferreds        r:   *test_canceller_circular_reference_callback8DeferredTests.test_canceller_circular_reference_callback  sr     #$	I. #+9"5%{{8,$  	-/*,.)r=   c                Z   [        5       n[        R                  " U5      n[        U5      nX1l        [        R                  " U5      n[        [        S5      5      nUR                  U5        U R                  U5        AAU R                  U" 5       5        U R                  U" 5       5        g)z`
A circular reference between a `Deferred` and its canceller
is broken when the deferred fails.
The test demands failures.N)
rL  rM  rN  r!   rR   r0   r  r   r'  r   )rc   rO  rP  rR   rQ  r  s         r:   )test_canceller_circular_reference_errback7DeferredTests.test_canceller_circular_reference_errback  s     #$	I. #+9"5%{{8,)$@AB! 	X& 	-/*,.)r=   c                    [        5       n[        R                  " U5      n[        U5      nX1l        UR                  S 5        UR                  S5        AAU R                  U" 5       5        g)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.
c                    [        5       $ r4   )r!   rn  s    r:   r  KDeferredTests.test_canceller_circular_reference_non_final.<locals>.<lambda>"  s    xzr=   N)rL  rM  rN  r!   rR   r   rP   r   )rc   rO  rP  rR   s       r:   +test_canceller_circular_reference_non_final9DeferredTests.test_canceller_circular_reference_non_final  sa     #$	I. #+9"5%12$ 	-/*r=   c                z   U R                  [        R                  SS [        [        /5         " S S[        [
           5      nU R                  [        R                  S   U5        [        R                  R                  S5        [        R                   H"  nU R                  [        U[        5      5        M$     g)zA
C{_DEFERRED_SUBCLASSES} includes all subclasses of L{Deferred}.
Nr   c                      \ rS rSrSrg)2DeferredTests.test_DEFERRED_SUBCLASSES.<locals>.D2i2  rF   NrG   rF   r=   r:   D2r_  2  r  r=   r`  )	r^   r   _DEFERRED_SUBCLASSESr!   r#   rt  r  r`   r   )rc   r`  klasss      r:   test_DEFERRED_SUBCLASSES&DeferredTests.test_DEFERRED_SUBCLASSES,  s     	33BQ7(L9QR	# 	 	33B7<""&&r* //EOOJuh78 0r=   )r   r   r   Nr   )r6   r>   r7   r>   r?   r   r6   r>   r7   r>   r?   r   )r6   r0   r7   r>   r?   r   )^rH   rI   rJ   rK   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r"  r,  r2  r5  r8  r<  r?  rE  rM  rP  rS  r[  r_  rd  ri  ru  ry  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r
  r  r3  rP  r]  rr  ry  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r#  r/  r4  r:  rB  pyunitskipIfr.   rR  rV  r[  rd  rL   rF   r=   r:   r   r      s   B-+AIQB

5A"5,"
H%('.R?"?"&&"
@B$
@9K:"+ ).)=)222
=
6
<3,-() & >(K?1( >"2$
.	6ED2
.*
X%
N-
^,.)(>(*&14:(/:0>;
zF
P'(
'
.).BY
9?v*.&*)*&()(
5W. ]]59:* ;*8 ]]59:* ;*> ]]59:+ ;+49r=   r   c                  2    \ rS rSr% SrSrS\S'   SS jrSrg)	rL  i>  zv
A callable that does nothing.
It is intended to be used just to get an object reference
to support GC related tests.
NzOptional[Deferred[Any]]rR   c                    g)zF
This is not expected to be called as part of the current test suite.
NrF   r   s     r:   __call__DummyCanceller.__call__G  s    r=   rF   )rR   ri   r?   r   )	rH   rI   rJ   rK   rj   rR   __annotations__rk  rL   rF   r=   r:   rL  rL  >  s     )-H%,r=   rL  c                   ^ S/U -  m/ n[        U 5       H&  nU4SU4S jjjnUR                  [        US95        M(     TU4$ )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.
r   c                    > TU==   S-  ss'   g r  rF   )rZ  ncancelledStates     r:   r&  _setupRaceState.<locals>.cancelX  s    1"r=   rO  )rZ  rL  rp  rt  r?   r   )r  r]   r!   )numDeferredsdsrp  r&  rq  s       @r:   _setupRaceStaterv  M  sR     S<'N!#B< 12 	# 	# 			(V,- ! 2r=   c                      \ rS rSrSr\" \" SSS9\" SSS9S9SS j5       r\" \" SSS9\" SSS9S9SS j5       r\" \" SSS9\" SSS9S9SS	 j5       r	SS
 jr
\" \" SSS9S9SS j5       rSrg)	RaceTestsi`  z
Tests for L{race}.
r   rb  )	min_value	max_value)beforeWinnerafterWinnerc                   [        US-   U-   5      u  p4[        U5      n[        5       nXA   R                  U5        [	        U R                  U5      [        X45      5        S/U-  S/-   S/U-  -   n[	        U[        U5      5        g)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.
rZ   r   N)rv  r+   r>   rP   r   r/  r   )rc   r{  r|  rq  ru  
raceResultr  expectedCancelledStates           r:   test_successRaceTests.test_successe  s      -\A-=-KL"X
8
!!(+ 	  ,l-.	

 #$|!3qc!9QC+<M!M+,	
r=   c           	     x   [        US-   U-   5      u  p4[        [        S5      5      n[        U5      nU H  nUR	                  U5        M     U R                  U[        5      n[        UR                  R                  [        U/[        U5      -  5      5        [        U[        S/[        U5      -  5      5        g)a  
When all of the L{Deferred}s passed to L{race} fire with failures,
the L{Deferred} return by L{race} fires with L{FailureGroup} wrapping
all of their failures.

@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.
rZ   rU  r   N)rv  r0   r  r+   r   r'  r'   r   rb   rf   r   r_   )	rc   r{  r|  rq  ru  r  r~  rZ  actualFailures	            r:   test_failureRaceTests.test_failure  s      -\A-=-KL)$@AB"X
AIIg  ,,ZF((gYR()	
 	aS3r7]#	
r=   c                .   [        US-   U-   5       Vs/ s H  n[        5       PM     nn[        U5      nXA   R                  S5        XAS-      R                  S5        U R	                  U5        [        U R                  5       [        5       5        gs  snf )z
If one of the Deferreds fires after it was cancelled its result
goes nowhere.  In particular, it does not cause any errors to be
logged.
r   NrZ   )r  r!   r+   rP   r/  r   rq   r   )rc   r{  r|  rp  ru  r~  s         r:   test_resultAfterCancel RaceTests.test_resultAfterCancel  s     !&lQ&6&D E$
 E1HJ E 	 $
 "X

!!$'
!%%d+Z(D**,eg6$
s   Bc                    [        5       nU[        S S9/n[        5       n[        U5      nUR                  U5        [	        U R                  U5      [        SU45      5        g)zf
If one of the input Deferreds has a cancel function that fires it
with success, nothing bad happens.
c                4    U R                  [        5       5      $ r4   )rP   r>   rZ  s    r:   r  1RaceTests.test_resultFromCancel.<locals>.<lambda>  s    FH)=r=   rs  r   N)r!   r>   r+   rP   r   r/  r   )rc   winnerru  r  r~  s        r:   test_resultFromCancelRaceTests.test_resultFromCancel  sZ    
 $,:=>&
 8"X
!D((4h8}6MNr=   rZ   )rt  c                    [        U5      u  p#[        U5      nUR                  5         [        U[	        S/U-  5      5        U R                  U[        5        g)z]
If the result of L{race} is cancelled then all of the L{Deferred}s
passed in are cancelled.
rZ   N)rv  r+   r&  r   r   r'  r'   )rc   rt  rq  ru  r~  s        r:   test_cancelRaceTests.test_cancel  sL     -\:"X
NHaS<-?$@AZ6r=   rF   N)r{  rt  r|  rt  r?   r   r   )rt  rt  r?   r   )rH   rI   rJ   rK   rj   r   r   r  r  r  r  r  rL   rF   r=   r:   rx  rx  `  s     Q7qA6
	
> Q7qA6
	
: Q7qA67	7&O  Q777r=   rx  c                  6    \ rS rSrSrSS jrSS jrSS jrSrg)	FirstErrorTestsi  z
Tests for L{FirstError}.
c                    [        S5      n Ue! [         a    [        5       n Of = f[        R                  " US5      nU R                  [        U5      S[        U5       S35        g)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 textrb  zFirstError[#3, ]N)r  r  r0   r   r   r^   r  rc   r  r9   rs   s       r:   r  FirstErrorTests.test_repr  sc    
 %	I 		A	   A&eS	{!&DE    &&c                    [        S5      n Ue! [         a    [        5       n Of = f[        R                  " US5      nU R                  [        U5      S[        U5       S35        g)z
The str of a L{FirstError} instance includes the str of the
sub-failure and the index which corresponds to the L{FirstError}.
r  r  zFirstError[#5, r  N)r  r  r0   r   r   r^   r   r  s       r:   test_strFirstErrorTests.test_str  sc    
 %	I 		A	   A&Us1vha%@Ar  c                    SS-    [        R                  " WS5      n[        R                  " US5      n [	        S5      e! [          a    [        5       n NPf = f! [          a    [        5       n Of = f[        R                  " US5      nU R                  X#:H  5        U R                  X%:H  5        U R                  X%:g  5        U R                  X#:g  5        U R                  US:H  5        g)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.
rZ   r      bar	   r  N)r  r0   r   r   r  r`   r0  )rc   firstFailureone
anotherOnesecondFailureanothers         r:   test_comparisonFirstErrorTests.test_comparison  s    	%F |R0%%lB7
	&U##  	%"9L	%  	&#IM	& ""=!4)*('*+#s"   A A AAA21A2rF   Nr   )	rH   rI   rJ   rK   rj   r  r  r  rL   rF   r=   r:   r  r    s    FB$r=   r  c                      \ rS rSrSS jrSS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSrg)AlreadyCalledTestsi  c                d    [         R                  " 5       U l        [         R                  " S5        g NT)r   r   _deferredWasDebuggingr   r   s    r:   r   AlreadyCalledTests.setUp   s!    %*%7%7%9"4 r=   c                D    [         R                  " U R                  5        g r4   )r   r   r  r   s    r:   tearDownAlreadyCalledTests.tearDown$  s    4556r=   c                    g r4   rF   r   s      r:   r   AlreadyCalledTests._callback'      r=   c                    g r4   rF   r   s      r:   r   AlreadyCalledTests._errback*  r  r=   c                &    UR                  S5        g )Nr   rO   rx  s     r:   _call_1AlreadyCalledTests._call_1-      	

7r=   c                &    UR                  S5        g )NtwicerO   rx  s     r:   _call_2AlreadyCalledTests._call_20  r  r=   c                H    UR                  [        [        5       5      5        g r4   r   r0   ro   rx  s     r:   _err_1AlreadyCalledTests._err_13      			',.)*r=   c                H    UR                  [        [        5       5      5        g r4   r  rx  s     r:   _err_2AlreadyCalledTests._err_26  r  r=   c                    [        5       nUR                  U R                  U R                  5        U R	                  U5        U R                  [        R                  U R                  U5        g r4   )	r!   r   r   r   r  rC  r   AlreadyCalledErrorr  rx  s     r:   testAlreadyCalled_CC'AlreadyCalledTests.testAlreadyCalled_CC9  sF    #:	t~~t}}5Q%22DLL!Dr=   c                    [        5       nUR                  U R                  U R                  5        U R	                  U5        U R                  [        R                  U R                  U5        g r4   )	r!   r   r   r   r  rC  r   r  r  rx  s     r:   testAlreadyCalled_CE'AlreadyCalledTests.testAlreadyCalled_CE?  sF    #:	t~~t}}5Q%22DKKCr=   c                    [        5       nUR                  U R                  U R                  5        U R	                  U5        U R                  [        R                  U R                  U5        g r4   )	r!   r   r   r   r  rC  r   r  r  rx  s     r:   testAlreadyCalled_EE'AlreadyCalledTests.testAlreadyCalled_EEE  sF    #:	t~~t}}5A%22DKKCr=   c                    [        5       nUR                  U R                  U R                  5        U R	                  U5        U R                  [        R                  U R                  U5        g r4   )	r!   r   r   r   r  rC  r   r  r  rx  s     r:   testAlreadyCalled_EC'AlreadyCalledTests.testAlreadyCalled_ECK  sF    #:	t~~t}}5A%22DLL!Dr=   c                    SnU H>  nUR                  SU-  5      (       d  M  UR                  SU-  5      (       d  M9  US-  nM@     U R                  XT:H  5        g )Nr   z %s:z %srZ   )r   endswithr`   )rc   linetypefunclinesr  countlines          r:   _countAlreadyCalledTests._countQ  sQ    Dv011dmmEDL6Q6Q
  	)*r=   c                R   UR                   S   R                  S5      nU R                  SX%S5        U R                  SSUS5        U R                  SSUS5        U R                  SSUS5        U R                  SSUS5        U R                  S	X5S5        U R                  S	XES5        g )
Nr   
CrZ   r  r  r  r  I)r6   splitr  )rc   r  callerinvoker1invoker2r  s         r:   _checkAlreadyCalledTests._checkX  s    q	% 	C*CE1-CE1-C5!,C5!,C!,C!,r=   c                6   [        5       nUR                  U R                  U R                  5        U R	                  U5         U R                  U5        U R                  S5        g ! [        R                   a  nU R                  USSS5         S nAg S nAff = f)N2second callback failed to raise AlreadyCalledErrortestAlreadyCalledDebug_CCr  r  )
r!   r   r   r   r  r  rc  r   r  r  rc   rZ  r  s      r:   r  ,AlreadyCalledTests.testAlreadyCalledDebug_CCf  sz    #:	t~~t}}5Q	LLLO IIJK '' 	NKK6	9MM	N   A& &B:BBc                6   [        5       nUR                  U R                  U R                  5        U R	                  U5         U R                  U5        U R                  S5        g ! [        R                   a  nU R                  USSS5         S nAg S nAff = f)N1second errback failed to raise AlreadyCalledErrortestAlreadyCalledDebug_CEr  r  )
r!   r   r   r   r  r  rc  r   r  r  r  s      r:   r  ,AlreadyCalledTests.testAlreadyCalledDebug_CEq  sz    #:	t~~t}}5Q	KKKN IIIJ '' 	MKK6	8LL	Mr  c                6   [        5       nUR                  U R                  U R                  5        U R	                  U5         U R                  U5        U R                  S5        g ! [        R                   a  nU R                  USSS5         S nAg S nAff = f)Nr  testAlreadyCalledDebug_ECr  r  )
r!   r   r   r   r  r  rc  r   r  r  r  s      r:   r  ,AlreadyCalledTests.testAlreadyCalledDebug_EC|  sz    #:	t~~t}}5A	LLLO IIJK '' 	MKK6)LL	Mr  c                6   [        5       nUR                  U R                  U R                  5        U R	                  U5         U R                  U5        U R                  S5        g ! [        R                   a  nU R                  USSS5         S nAg S nAff = f)Nr  testAlreadyCalledDebug_EEr  r  )
r!   r   r   r   r  r  rc  r   r  r  r  s      r:   r  ,AlreadyCalledTests.testAlreadyCalledDebug_EE  sz    #:	t~~t}}5A	KKKN IIIJ '' 	LKK6(KK	Lr  c                p   [         R                  " S5        [        5       nUR                  U R                  U R
                  5        U R                  U5         U R                  U5        U R                  S5        g ! [         R                   a%  nU R                  UR                  5         S nAg S nAff = f)NFr  )r   r   r!   r   r   r   r  r  rc  r  r0  r6   r  s      r:   testNoDebugging"AlreadyCalledTests.testNoDebugging  s    5!#:	t~~t}}5Q	LLLO IIJK '' 	%QVV$$	%s   A< <B5B00B5c                h   [         R                  " S5        [        5       nUR                  S 5        [         R                  " S5        UR	                  S 5        [         R                  " S5        [        5       nUR	                  S 5        [         R                  " S5        UR                  S 5        g )NFc                    g r4   rF   r  s    r:   r  8AlreadyCalledTests.testSwitchDebugging.<locals>.<lambda>      dr=   Tc                    g r4   rF   r  s    r:   r  r    r  r=   )r   r   r!   addBothrP   rx  s     r:   testSwitchDebugging&AlreadyCalledTests.testSwitchDebugging  s|     	5!$J			"#4 	

45!J	

44 			"#r=   )r  Nr   rf  )rZ  r  r?   r   )
r  r   r  r   r  rE  r  rt  r?   r   )
r  r  r  r   r  r   r  r   r?   r   )rH   rI   rJ   rK   r   r  r   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rL   rF   r=   r:   r  r    so    !7++EDDE+-	L	K	L	K
L$r=   r  c                      \ rS rSrSS jrSS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSrg)DeferredCancellerTestsi  c                <    S U l         S U l        S U l        SU l        g r   )r   r   r   cancellerCallCountr   s    r:   r   DeferredCancellerTests.setUp  s"    .215/3"#r=   c                <    U R                  U R                  S5        g )Nr  )r   r  r   s    r:   r  DeferredCancellerTests.tearDown  s    d--v6r=   c                    Xl         U$ r4   r   rc   datas     r:   r    DeferredCancellerTests._callback  s    #r=   c                    Xl         g r4   r   r  s     r:   r   !DeferredCancellerTests._callback2  s     $r=   c                    Xl         g r4   r   )rc   rs   s     r:   r   DeferredCancellerTests._errback  s    #r=   c                @   [        5       nUR                  U R                  U R                  5        UR	                  5         U R
                  c   eU R                  U R
                  R                  [        R                  5        U R                  U R                  5        g)za
A L{Deferred} without a canceller must errback with a
L{defer.CancelledError} and not callback.
N)r!   r   r   r   r&  r   r^   r   r   r   r   r   rx  s     r:   test_noCanceller'DeferredCancellerTests.test_noCanceller  sr    
 %J	t~~t}}5	
""...,,1153G3GH$../r=   c                b   [        5       nUR                  U R                  U R                  5        UR	                  5         UR                  S5        U R                  [        R                  UR
                  S5        U R                  [        R                  UR                  [        5       5        g)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!   r   r   r   r&  rP   rC  r   r  r   r  rx  s     r:   !test_raisesAfterCancelAndCallback8DeferredCancellerTests.test_raisesAfterCancelAndCallback  st     %J	t~~t}}5	
 	


4 	%22AJJE%22AIIy{Kr=   c                r   [        5       nUR                  U R                  U R                  5        UR	                  5         UR                  [        5       5        U R                  [        R                  UR                  S5        U R                  [        R                  UR
                  [        5       5        g)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!   r   r   r   r&  r   r  rC  r   r  rP   rx  s     r:    test_raisesAfterCancelAndErrback7DeferredCancellerTests.test_raisesAfterCancelAndErrback  sv     %J	t~~t}}5	
 	
		)+ 	%22AJJE%22AIIy{Kr=   c                   [        5       nUR                  U R                  U R                  5        UR	                  5         U R
                  nUR                  S5        UR	                  5         U R                  X R
                  5        g)zm
A L{Deferred} without a canceller, when cancelled and then
callbacked, ignores multiple cancels thereafter.
N)r!   r   r   r   r&  r   rP   rr   rc   rZ  currentFailures      r:   5test_noCancellerMultipleCancelsAfterCancelAndCallbackLDeferredCancellerTests.test_noCancellerMultipleCancelsAfterCancelAndCallback  s\    
 %J	t~~t}}5	
,,	

4	
n&9&9:r=   c                   [        5       nUR                  U R                  U R                  5        UR	                  5         U R
                  c   eU R                  U R
                  R                  [        R                  5        U R
                  nUR                  [        5       5        U R                  U R
                  R                  [        R                  5        UR	                  5         U R                  X R
                  5        g)zl
A L{Deferred} without a canceller, when cancelled and then
errbacked, ignores multiple cancels thereafter.
N)r!   r   r   r   r&  r   r^   r   r   r   r   rD   rr   r  s      r:   4test_noCancellerMultipleCancelsAfterCancelAndErrbackKDeferredCancellerTests.test_noCancellerMultipleCancelsAfterCancelAndErrback 	  s    
 %J	t~~t}}5	
""...,,1153G3GH,,			,.!,,1153G3GH	
n&9&9:r=   c                x   [        5       nUR                  U R                  U R                  5        UR	                  5         U R
                  c   eU R                  U R
                  R                  [        R                  5        U R
                  nUR	                  5         U R                  X R
                  5        g)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!   r   r   r   r&  r   r^   r   r   r   rr   r  s      r:   test_noCancellerMultipleCancel5DeferredCancellerTests.test_noCancellerMultipleCancel	  s     %J	t~~t}}5	
""...,,1153G3GH,,	
n&9&9:r=   c                  ^  SU 4S jjn[        US9nUR                  T R                  T R                  5        UR	                  5         T R
                  c   eT R                  T R
                  R                  [        R                  5        T R
                  nUR	                  5         T R                  UT R
                  5        T R                  T R                  S5        g)z
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.
c                0   > T=R                   S-  sl         g r  r  rZ  rc   s    r:   r&  CDeferredCancellerTests.test_cancellerMultipleCancel.<locals>.cancel*	      ##q(#r=   rs  NrZ   rZ  rL  r?   r   )r!   r   r   r   r&  r   r^   r   r   r   rr   r  )rc   r&  rZ  r  s   `   r:   test_cancellerMultipleCancel3DeferredCancellerTests.test_cancellerMultipleCancel!	  s    	) %v6	t~~t}}5	
""...,,1153G3GH,,	
nd&9&9:00!4r=   c                  ^  SU 4S jjn[        US9nUR                  T R                  T R                  5        UR	                  5         T R                  T R                  S5        T R                  c   eT R                  T R                  R                  [        R                  5        T R                  [        R                  UR                  S5        T R                  [        R                  UR                  [        5       5        g)z
Verify that a L{Deferred} calls its specified canceller when
it is cancelled, and that further call/errbacks raise
L{defer.AlreadyCalledError}.
c                0   > T=R                   S-  sl         g r  r(  r)  s    r:   r&  ;DeferredCancellerTests.test_simpleCanceller.<locals>.cancel>	  r+  r=   rs  rZ   Nr,  )r!   r   r   r   r&  r^   r  r   r   r   r   rC  r  rP   r   r  rc   r&  rZ  s   `  r:   test_simpleCanceller+DeferredCancellerTests.test_simpleCanceller7	  s    	) %v6	t~~t}}5	
00!4""...,,1153G3GH 	%22AJJE%22AIIy{Kr=   c                   ^ ^ SUU 4S jjn[        US9mTR                  T R                  T R                  5        TR	                  5         g)zA
Verify that a canceller is given the correct deferred argument.
c                *   > TR                  U T5        g r4   )rr   )r	  rZ  rc   s    r:   r&  8DeferredCancellerTests.test_cancellerArg.<locals>.cancelQ	  s    MM"a r=   rs  N)r	  rL  r?   r   )r!   r   r   r   r&  r2  s   ` @r:   test_cancellerArg(DeferredCancellerTests.test_cancellerArgL	  s8    
	! 	! %v6	t~~t}}5	
r=   c                \  ^  SU 4S jjn[        US9nUR                  T R                  T R                  5        UR	                  S5        UR                  5         T R                  T R                  S5        T R                  T R                  5        T R                  T R                  S5        g)zW
Test that cancelling a deferred after it has been callbacked does
not cause an error.
c                b   > T=R                   S-  sl         U R                  [        5       5        g r  r  r   rD   r)  s    r:   r&  ?DeferredCancellerTests.test_cancelAfterCallback.<locals>.cancel^	  "    ##q(#IIln%r=   rs  zbiff!r   Nr,  )r!   r   r   r   rP   r&  r^   r  r   r   r   r2  s   `  r:   test_cancelAfterCallback/DeferredCancellerTests.test_cancelAfterCallbackX	  s    	& $f5	t~~t}}5	

7	
00!4$--.--w7r=   c                  ^  SU 4S jjn[        US9nUR                  T R                  T R                  5        UR	                  [        5       5        UR                  5         T R                  T R                  S5        T R                  c   eT R                  T R                  R                  [
        5        T R                  T R                  5        g)zn
Test that cancelling a L{Deferred} after it has been errbacked does
not result in a L{defer.CancelledError}.
c                b   > T=R                   S-  sl         U R                  [        5       5        g r  r<  r)  s    r:   r&  >DeferredCancellerTests.test_cancelAfterErrback.<locals>.cancelp	  r>  r=   rs  r   Nr,  )r!   r   r   r   r   rD   r&  r^   r  r   r   r   r   r2  s   `  r:   test_cancelAfterErrback.DeferredCancellerTests.test_cancelAfterErrbackj	  s    	& %v6	t~~t}}5			,.!	
00!4""...,,11<@$../r=   c                >  ^  SU 4S jjn[        US9nUR                  T R                  T R                  5        UR	                  5         T R                  T R                  S5        T R                  c   eT R                  T R                  R                  [        5        g)z/
Test a canceller which errbacks its deferred.
c                b   > T=R                   S-  sl         U R                  [        5       5        g r  r<  r)  s    r:   r&  ADeferredCancellerTests.test_cancellerThatErrbacks.<locals>.cancel	  r>  r=   rs  rZ   Nr,  )
r!   r   r   r   r&  r^   r  r   r   rD   r2  s   `  r:   test_cancellerThatErrbacks1DeferredCancellerTests.test_cancellerThatErrbacks}	  sw    
	& %v6	t~~t}}5	
00!4""...,,11<@r=   c                :  ^  SU 4S jjn[        US9nUR                  T R                  T R                  5        UR	                  5         T R                  T R                  S5        T R                  T R                  S5        T R                  T R                  5        g)z,
Test a canceller which calls its deferred.
c                R   > T=R                   S-  sl         U R                  S5        g )NrZ   hello!)r  rP   r)  s    r:   r&  BDeferredCancellerTests.test_cancellerThatCallbacks.<locals>.cancel	  s     ##q(#JJx r=   rs  rZ   rM  Nr,  )
r!   r   r   r   r&  r^   r  r   r   r   r2  s   `  r:   test_cancellerThatCallbacks2DeferredCancellerTests.test_cancellerThatCallbacks	  ss    
	! %v6	t~~t}}5	
00!4--x8$--.r=   c                  ^ ^ SU 4S jjnSU 4S jjn[        US9m[        US9nUR                  S5        UR                  U4S j5        UR                  5         UR	                  T R
                  T R                  5        T R                  T R                  S5        T R                  c   eT R                  T R                  R                  [        R                  5        g)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.
c                0   > T=R                   S-  sl         g r  r(  r)  s    r:   innerCancelEDeferredCancellerTests.test_cancelNestedDeferred.<locals>.innerCancel	  r+  r=   c                (   > TR                  S5        g NF)r`   r)  s    r:   r&  @DeferredCancellerTests.test_cancelNestedDeferred.<locals>.cancel	  s    OOE"r=   rs  Nc                   > T$ r4   rF   )r  r  s    r:   r  BDeferredCancellerTests.test_cancelNestedDeferred.<locals>.<lambda>	  s    1r=   rZ   r,  )r!   rP   r   r&  r   r   r   r^   r  r   r   r   r   )rc   rS  r&  r;  r  s   `   @r:   test_cancelNestedDeferred0DeferredCancellerTests.test_cancelNestedDeferred	  s    	)	# %{;$v6	

4	n%	
	t~~t}}500!4 ""...,,1153G3GHr=   )r   r   r  r   Nr   )r  r   r?   r   )r  r   r?   r   )rs   r0   r?   r   )rH   rI   rJ   rK   r   r  r   r   r   r  r  r  r  r!  r$  r-  r3  r8  r?  rD  rI  rO  rZ  rL   rF   r=   r:   r  r    sf    $7%$
0L"L";;$;5,L*
8$0&A / Ir=   r  c                      \ rS rSrSrSS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSS jrSS jrSS jrSrg)LogTestsi	  z#
Test logging of unhandled errors.
c                f    / U l         [        R                  " U R                   R                  5        g)z,
Add a custom observer to observer logging.
N)cr-   r   r]   r   s    r:   r   LogTests.setUp	  s     (*&r=   c                X    [         R                  " U R                  R                  5        g)z
Remove the observer.
N)r-   removeObserverr_  r]   r   s    r:   r  LogTests.tearDown	  s     	466==)r=   c                \    U R                    Vs/ s H  oS   (       d  M  UPM     sn$ s  snf )NisErrorr_  )rc   r  s     r:   _loggedErrorsLogTests._loggedErrors	  s"    6626ay\6222s   ))c                    U R                  5       nU R                  [        U5      S5        US   S   R                  [        5        U R                  [        5        g)zF
Check the output of the log observer to see if the error is present.
r   rZ   r  N)rg  r^   r_   r  rr  rq   )rc   c2s     r:   r  LogTests._check	  sM     !R!$
1i/001r=   c                    [        5       R                  S 5      R                  S5        [        R                  " 5         U R                  5         g)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                    SS-  $ rm  rF   r  s    r:   r  (LogTests.test_errorLog.<locals>.<lambda>	      ar=   rZ   N)r!   r   rP   gccollectr  r   s    r:   test_errorLogLogTests.test_errorLog	  s2     	
/099!<


r=   c                f    SS jnU" 5         [         R                  " 5         U R                  5         g)z4
Same as L{test_errorLog}, but with an inner frame.
c                 ^    [        5       n U R                  S 5        U R                  S5        g )Nc                    SS-  $ rm  rF   r  s    r:   r  `LogTests.test_errorLogWithInnerFrameRef.<locals>._subErrorLogWithInnerFrameRef.<locals>.<lambda>	      AFr=   rZ   )r!   r   rP   r  s    r:   _subErrorLogWithInnerFrameRefNLogTests.test_errorLogWithInnerFrameRef.<locals>._subErrorLogWithInnerFrameRef	  s!    'zAMM*+JJqMr=   Nr   rp  rq  r  )rc   ry  s     r:   test_errorLogWithInnerFrameRef'LogTests.test_errorLogWithInnerFrameRef	  s!    
	
 	&'


r=   c                f    SS jnU" 5         [         R                  " 5         U R                  5         g)zA
Same as L{test_errorLogWithInnerFrameRef}, plus create a cycle.
c                 x    [        5       n U 4SS jjnU R                  U5        X l        U R                  S5        g )Nc                    SS-  $ rm  rF   )r  rZ  s     r:   unusedCyclegLogTests.test_errorLogWithInnerFrameCycle.<locals>._subErrorLogWithInnerFrameCycle.<locals>.unusedCycle	  s    Avr=   rZ   )r  rt  rZ  Deferred[int]r?   rt  )r!   r   _drP   )rZ  r  s     r:   _subErrorLogWithInnerFrameCycleRLogTests.test_errorLogWithInnerFrameCycle.<locals>._subErrorLogWithInnerFrameCycle	  s/    'zA78  MM+&DJJqMr=   Nr   r{  )rc   r  s     r:    test_errorLogWithInnerFrameCycle)LogTests.test_errorLogWithInnerFrameCycle	  s!    
		 	()


r=   c                   [        5       R                  S 5      R                  S5        [        R                  " 5         U R                  5         U R                  S[        U R                  5      5        [        R                  " U R                  S   5      nUc   eSnU R                  UR                  U5      SU< 35        g)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                    SS-  $ rm  rF   r  s    r:   r  .LogTests.test_errorLogNoRepr.<locals>.<lambda>
  ro  r=   rZ   r   ra  Nr    Expected message starting with: )r!   r   rP   rp  rq  r  r^   r_   r_  r-   r}   r`   r   )rc   msgr  s      r:   test_errorLogNoReprLogTests.test_errorLogNoRepr
  s    
 	
/099!<


CK(##DFF2J/0NN8$.xl;	
r=   c                N   SS jnU" 5         [         R                  " 5         U R                  5         U R                  S[	        U R
                  5      5        [        R                  " U R
                  S   5      nUc   eSnU R                  UR                  U5      SU< 35        g)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.
c                 l    [        5       n SU l        U R                  S 5        U R                  S5        g )NTc                    SS-  $ rm  rF   r  s    r:   r  ?LogTests.test_errorLogDebugInfo.<locals>.doit.<locals>.<lambda>
  rx  r=   rZ   )r!   debugr   rP   r  s    r:   doit-LogTests.test_errorLogDebugInfo.<locals>.doit
  s(    'zAAGMM*+JJqMr=   r   ra  Nz
(debug:  Ir  r   )
rp  rq  r  r^   r_   r_  r-   r}   r`   r   )rc   r  r  r  s       r:   test_errorLogDebugInfoLogTests.test_errorLogDebugInfo
  s    	 	


CK(##DFF2J/NN8$.xl;	
r=   c                   [        5       nUR                  S 5        UR                  S5        / n/ nUR                  UR                  UR                  5        U R                  U/ 5        U R                  [        U5      S5        US   R                  [        5        AAA[        R                  " 5         U R                  U R                  5       / 5        g)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                @    [         R                  " [        S5      5      $ )Nzoop)r   rc  ro   r  s    r:   r  3LogTests.test_chainedErrorCleanup.<locals>.<lambda>6
  s    %**\&-A"Br=   NrZ   r   )r!   r   rP   r   r]   r^   r_   r  r  rp  rq  rg  )rc   rZ  r  rx   s       r:   test_chainedErrorCleanup!LogTests.test_chainedErrorCleanup/
  s     %J	BC	

4 ! "	w~~v}}5"%Va(q	y! VQ 	

++-r2r=   c                   ^ [         R                  " [        S5      5      n[        5       mUc   eUR	                  U4S j5        S=mn[
        R                  " 5         U R                  U R                  5       / 5        g)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.
zoh noNc                   > T$ r4   rF   )r  goods    r:   r  6LogTests.test_errorClearedByChaining.<locals>.<lambda>V
  s    tr=   )	r   rc  r  r!   r\   rp  rq  r^   rg  )rc   badr  s     @r:   test_errorClearedByChaining$LogTests.test_errorClearedByChainingK
  sd     ).

9W3E(F)1+, s


++-r2r=   rf  Nr   )r?   zList[Dict[str, Any]])rH   rI   rJ   rK   rj   r   r  rg  r  rr  r|  r  r  r  r  r  rL   rF   r=   r:   r]  r]  	  s>    '*32(
&
4383r=   r]  c                  <    \ rS rSrSS jrSS jrS	S jrSS jrSrg)
DeferredListEmptyTestsi^
  c                    SU l         g r   callbackRanr   s    r:   r   DeferredListEmptyTests.setUp_
  s
    r=   c                P    [        / 5      nUR                  U R                  5        g)zTesting empty DeferredList.N)r#   r   cb_empty)rc   r   s     r:   testDeferredListEmpty,DeferredListEmptyTests.testDeferredListEmptyb
  s    9Eb9I
t}}%r=   c                6    SU l         U R                  / U5        g r  )r  r^   )rc   ress     r:   r  DeferredListEmptyTests.cb_emptyg
  s    S!r=   c                <    U R                  U R                  S5        g )NzCallback was never run.)r`   r  r   s    r:   r  DeferredListEmptyTests.tearDownk
  s    ((*CDr=   r  Nr   )r  zList[Tuple[bool, object]]r?   r   )	rH   rI   rJ   rK   r   r  r  r  rL   rF   r=   r:   r  r  ^
  s    &
"Er=   r  c                      \ rS rSrSS jrSS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSS jrSS jrSS jrSrg)OtherPrimitivesTestsio
  c                .    U =R                   S-  sl         g r  counterrc   rt   s     r:   _incrOtherPrimitivesTests._incrp
  s    r=   c                    SU l         g r   r  r   s    r:   r   OtherPrimitivesTests.setUps
  s	    r=   c                V  ^
 [        5       nUR                  5       R                  U R                  5        U R	                  UR
                  5        U R                  U R                  S5        UR                  5       R                  U R                  5        U R	                  UR
                  5        U R                  U R                  S5        UR                  5         U R	                  UR
                  5        U R                  U R                  S5        UR                  5         U R                  UR
                  5        U R                  U R                  S5        U R                  [        UR                  5        [        5       n[        5       n[        5       nS m
SS	U
4S jjjnUR                  XRUS9nU R	                  UR
                  5        U R                  T
U5        UR                  U5        UR                  5       R                  U R                  5        U R	                  UR
                  5        U R                  U R                  S5        UR                  U5        U R                  T
U5        U R	                  UR
                  5        U R                  U R                  S5        UR                  5       R!                  U5      nUR#                  5         U R%                  T
[&        5        U R                  [)        [&        T
5      R*                  [,        R.                  5        UR                  5         U R                  UR
                  5        S
S jnSS jn	[1        UR                  U5      [        [2           5        [1        UR                  U	5      [        [2           5        g )NrZ   r   c                   > U mU$ r4   rF   )resultValuereturnValuert   s     r:   helper-OtherPrimitivesTests.testLock.<locals>.helper
  s     Fr=   )r  r  rb  c                 .    [         R                  " S5      $ ri  r   rW  rF   r=   r:   r8  1OtherPrimitivesTests.testLock.<locals>.returnsInt
  s    ==##r=   c                    #    g7fr  rF   rF   r=   r:   returnsCoroInt5OtherPrimitivesTests.testLock.<locals>.returnsCoroInt
  r  r  r4   )r  r>   r  r>   r?   r>   )r?   r  r  )r$   acquirer   r  r`   lockedr^   r  releaser0  rC  r  runr>   r!   rP   r   r&  rs  r0   r   r   r   r   r   rt  )rc   lockfirstUniquesecondUniquecontrolDeferredr  resultDeferredrZ  r8  r  rt   s             @r:   testLockOtherPrimitivesTests.testLockv
  s   ~""4::.$q)""4::.$q)$q)%q))TXX.hx,4J#'	 	
  " 
 	$-""6*""4::.$q)  ..$q)LLN""6*	
fg.gv.33U5I5IJ%	$	 	DHHZ((3-8DHH^,hsm<r=   c                   ^  SU 4S jjn[        5       nUR                  5       nUR                  U5        UR                  5         g)zr
When canceling a L{Deferred} from a L{DeferredLock} that already
has the lock, the cancel should have no effect.
c                (   > TR                  S5        g NzUnexpected errback call!rc  r9   rc   s    r:   failOnErrbackHOtherPrimitivesTests.test_cancelLockAfterAcquired.<locals>.failOnErrback
      II01r=   Nr9   r0   r?   r   )r$   r  r\   r&  )rc   r  r  rZ  s   `   r:   test_cancelLockAfterAcquired1OtherPrimitivesTests.test_cancelLockAfterAcquired
  s1    	2 ~LLN	]#	
r=   c                    [        5       nUR                  5         UR                  5       nUR                  5         U R                  U[        R
                  5        g)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$   r  r&  rg   r   r   )rc   r  rZ  s      r:   test_cancelLockBeforeAcquired2OtherPrimitivesTests.test_cancelLockBeforeAcquired
  s>     ~LLN	
##Au';';<r=   c                  ^^^ Sn[        U5      n[        5       mS mS	UU4S jjn/ n[        5       nUR                  X5S9nUR	                  UR
                  5        UR	                  U R                  5        U R                  U/ 5        U R                  TU5        TR                  S 5        U R                  UR                  5       5        U R                  U R                  S5        SU l        [        SSU-   5       HH  nUR                  5       R	                  U R                  5        U R                  U R                  U5        MJ     / mS
U4S jjnS
U4S jjn	UR                  5       R                  X5      n
U
R                  5         U R                  TS/5        UR                  5       R	                  U R                  5        U R                  U R                  U5        UR!                  5         U R                  U R                  US-   5        [        SSU-   5       H2  nUR!                  5         U R                  U R                  US-   5        M4     g )Nr  c                   > U mT$ r4   rF   )argr  	helperArgs    r:   r  2OtherPrimitivesTests.testSemaphore.<locals>.helper
  s    I""r=   )r  rZ   r   c                (   > TR                  S5        g rV  r   r}  rV  s    r:   rc  0OtherPrimitivesTests.testSemaphore.<locals>.fail
  s    NN5!r=   c                (   > TR                  S5        g r  r   r  s    r:   rW  3OtherPrimitivesTests.testSemaphore.<locals>.succeed
  s    NN4 r=   T)r  r>   r?   rO  )r}  r>   r?   r   )r&   r!   r>   r  r   r]   r  r^   rP   r   r  r  r  r  r   r&  r  )rc   Nsemr  r  uniqueObjectr  irc  rW  rZ  r  r  rV  s              @@@r:   testSemaphore"OtherPrimitivesTests.testSemaphore
  s   "*2* 		# 	#
 !#x:""7>>2""4::."%L1  &'++-(q)q!a%AKKM%%djj1T\\1- ! 	"	! KKM&&t5	
4&)!!$**-q)q1u-q!a%AKKMT\\1q51 !r=   c                p    U R                  [        [        S5        U R                  [        [        S5        g)zb
If the token count passed to L{DeferredSemaphore} is less than one
then L{ValueError} is raised.
r   ra  N)rC  r  r&   r   s    r:   test_semaphoreInvalidTokens0OtherPrimitivesTests.test_semaphoreInvalidTokens  s*    
 	*&7;*&7<r=   c                   ^  SU 4S jjn[        S5      nUR                  5       nUR                  U5        UR                  5         g)z|
When canceling a L{Deferred} from a L{DeferredSemaphore} that
already has the semaphore, the cancel should have no effect.
c                (   > TR                  S5        g r  r  r  s    r:   r  MOtherPrimitivesTests.test_cancelSemaphoreAfterAcquired.<locals>.failOnErrback  r  r=   rZ   Nr  )r&   r  r\   r&  )rc   r  r  rZ  s   `   r:   !test_cancelSemaphoreAfterAcquired6OtherPrimitivesTests.test_cancelSemaphoreAfterAcquired  s4    	2  "KKM	]#	
r=   c                    [        S5      nUR                  5         UR                  5       nUR                  5         U R                  U[        R
                  5        g)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.
rZ   N)r&   r  r&  rg   r   r   )rc   r  rZ  s      r:   "test_cancelSemaphoreBeforeAcquired7OtherPrimitivesTests.test_cancelSemaphoreBeforeAcquired  sA      "KKM	
##Au';';<r=   c                $   Su  p[        X5      n/ n[        U5       H,  nUR                  5       R                  UR                  5        M.     U R                  [        R                  UR                  5        [        U5       H;  nUR                  U5        U R                  U[        [        US-   5      5      5        M=     [        U5       H:  nUR                  X-   5        U R                  U[        [        U5      5      5        M<     U R                  [        R                  UR                  S 5        / n[        U5       HV  nUR                  5       R                  UR                  5        U R                  U[        [        XU-   S-   5      5      5        MX     [        5       n/ n[        U5       H,  nUR                  5       R                  UR                  5        M.     [        U5       H  nUR                  U5        M     U R                  U[        [        U5      5      5        [        SS9nU R                  [        R                  UR                  S 5        [        SS9nU R                  [        R                  UR                  5        g )N)r   r   rZ   r   )size)backlog)r%   r  getr   r]   rC  r   QueueUnderflowputr^   listQueueOverflow)rc   r  Mqueuegottenr  s         r:   	testQueueOtherPrimitivesTests.testQueue'  s   $1!$7qAIIK##FMM2 %..		:qAIIaLVT%A,%78  qAIIaeVT%(^4  	%--uyy$?qAIIK##FMM2VT%q519*=%>?  qAIIK##FMM2 qAIIaL eAh01%%--uyy$?a(%..		:r=   c                   ^  SU 4S jjn[        5       nUR                  5       nUR                  U5        UR                  S5        UR	                  5         g)zs
When canceling a L{Deferred} from a L{DeferredQueue} that already has
a result, the cancel should have no effect.
c                (   > TR                  S5        g r  r  r  s    r:   r  OOtherPrimitivesTests.test_cancelQueueAfterSynchronousGet.<locals>.failOnErrbackR  r  r=   Nr  )r%   r  r\   r  r&  )rc   r  r  rZ  s   `   r:   #test_cancelQueueAfterSynchronousGet8OtherPrimitivesTests.test_cancelQueueAfterSynchronousGetL  s<    	2 &3_IIK	]#		$	
r=   c                @  ^ ^ [        5       mTR                  5       nUR                  5         T R                  U[        R
                  5        SUU 4S jjnUR                  U5        / nUR                  UR                  5        T R                  [        U5      S5        g)z
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.
c                z   > TR                  S 5        TR                  5       R                  TR                  S 5      $ r4   )r  r  r   rr   )ignorer  rc   s    r:   r   9OtherPrimitivesTests.test_cancelQueueAfterGet.<locals>.cbg  s-     IIdO99;**4==$??r=   rZ   N)r  r>   r?   rO  )
r%   r  r&  rg   r   r   r   r]   r^   r_   )rc   rZ  r   doner  s   `   @r:   test_cancelQueueAfterGet-OtherPrimitivesTests.test_cancelQueueAfterGet[  sy     &3_IIK	
##Au';';<	@ 	@ 	
b	dkk"TA&r=   r  Nr  r   )rH   rI   rJ   rK   r  r   r  r  r  r  r  r  r  r
  r  r  rL   rF   r=   r:   r  r  o
  sB    @=D
=12f=
=#;J'r=   r  c                  r    \ rS rSrSrSS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSS jrSrg)DeferredFilesystemLockTestsis  z0
Test the behavior of L{DeferredFilesystemLock}
c                p    [        5       U l        [        U R                  5       U R                  S9U l        g )N)	scheduler)r,   clockr"   mktempr  r   s    r:   r   !DeferredFilesystemLockTests.setUpx  s#    W
*4;;=DJJO	r=   c                4    U R                   R                  SS9$ )z9
Test that the lock can be acquired when no lock is held
rZ   timeout)r  deferUntilLockedr   s    r:   test_waitUntilLockedWithNoLock:DeferredFilesystemLockTests.test_waitUntilLockedWithNoLock|  s     yy))!)44r=   c                   U R                  U R                  R                  5       5        U R                  R                  SS9nU R                  U[        R
                  5        U R                  R                  S/S-  5        U$ )z[
Test that the lock can not be acquired when the lock is held
for longer than the timeout.
g      @r   rZ   r  )r`   r  r"  assertFailurer   TimeoutErrorr  pumprx  s     r:   %test_waitUntilLockedWithTimeoutLockedADeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutLocked  sc    
 			()II&&s&31e001

b!r=   c                \  ^  SU 4S jjnT R                  T R                  R                  5       5        T R                  R                  ST R                  R                  5        T R                  R                  SS9nUR                  U5        T R                  R                  S/S-  5        U$ )zd
Test that a lock can be acquired while a lock is held
but the lock is unlocked before our timeout.
c                f   > U R                  [        R                  5        TR                  S5        g )NzShould not have timed out)r  r   r'  rc  r  s    r:   	onTimeoutVDeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutUnlocked.<locals>.onTimeout  s"    FF5%%&II12r=   rZ   r  r   r  )r`   r  r  	callLaterunlockr"  r\   r(  )rc   r-  rZ  s   `  r:   'test_waitUntilLockedWithTimeoutUnlockedCDeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutUnlocked  s}    	3 			()

Q		 0 01II&&r&2	Y

b!r=   c                v    [        U R                  5       5      nU R                  UR                  [        5        g)z5
Test that the default scheduler is set up properly.
N)r"   r  r^   
_schedulerr   rc   r  s     r:   test_defaultScheduler1DeferredFilesystemLockTests.test_defaultScheduler  s(     &dkkm4'2r=   c                x   U R                   R                  5         U R                  R                  SU R                   R                  5        U R                   R	                  5       nU R                   R	                  5       nU R                  U[        R                  5        U R                  R                  S5        U$ )zd
Test that an appropriate exception is raised when attempting
to use deferUntilLocked concurrently.
rZ   )	r  r  r/  r0  r"  r&  r   AlreadyTryingToLockErroradvancer  s      r:   test_concurrentUsage0DeferredFilesystemLockTests.test_concurrentUsage  s    
 			

Q		 0 01YY'')YY'')2u==>

1	r=   c                8  ^  SU 4S jjnT R                   R                  5         T R                  R                  ST R                   R                  5        T R                   R	                  5       nUR                  U5        T R                  R                  S5        U$ )z?
Test that a DeferredFilesystemLock can be used multiple times
c                p   > TR                   R                  5         TR                   R                  5       nU$ r4   )r  r0  r"  )r  rZ  rc   s     r:   lockAquiredDDeferredFilesystemLockTests.test_multipleUsages.<locals>.lockAquired  s*    II		**,AHr=   rZ   )r  r>   r?   rO  )r  r  r/  r0  r"  r   r:  )rc   r?  rZ  s   `  r:   test_multipleUsages/DeferredFilesystemLockTests.test_multipleUsages  sk    
	
 			

Q		 0 01II&&(	k"

1r=   c                   U R                   R                  5         U R                   R                  5       nU R                   R                  nUc   eUR                  5         U R	                  UR                  5       5        U R                  U R                   R                  5        U R                  U[        R                  5        g)z
When cancelling a L{Deferred} returned by
L{DeferredFilesystemLock.deferUntilLocked}, the
L{DeferredFilesystemLock._tryLockCall} is cancelled.
N)
r  r"  _tryLockCallr&  r0  activer   r'  r   r   )rc   rR   tryLockCalls      r:   test_cancelDeferUntilLocked7DeferredFilesystemLockTests.test_cancelDeferUntilLocked  s     			99--/ii,,&&&++-.$))001Xu';';<r=   c                   U R                   R                  5         U R                   R                  SS9nU R                   R                  nUc   eUR                  5         U R	                  UR                  5       5        U R                  U R                   R                  5        U R                  U[        R                  5        g)z
When cancel a L{Deferred} returned by
L{DeferredFilesystemLock.deferUntilLocked}, if the timeout is
set, the timeout call will be cancelled.
rZ   r   N)
r  r"  _timeoutCallr&  r0  rE  r   r'  r   r   )rc   rR   timeoutCalls      r:   &test_cancelDeferUntilLockedWithTimeoutBDeferredFilesystemLockTests.test_cancelDeferUntilLockedWithTimeout  s     			99--a-8ii,,&&&++-.$))001Xu';';<r=   )r  r  Nr   )r?   rO  )rH   rI   rJ   rK   rj   r   r#  r)  r1  r6  r;  rA  rG  rL  rL   rF   r=   r:   r  r  s  s5    P5(3"(==r=   r  c                    g)z^
Private function to be used to pass as an alternate onTimeoutCancel value
to timeoutDeferred

OVERRIDDENrF   )vts     r:   _overrideFuncrR    s    
 r=   c                      \ rS rSrSrSS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSrg)DeferredAddTimeoutTestsi  z/
Tests for the function L{Deferred.addTimeout}
c                    [        5       nUR                  S[        5       5        UR                  S 5        UR	                  S5        U R                  SU R                  U5      5        g)z]
L{Deferred.addTimeout} returns its own L{Deferred} so it
can be called in a callback chain.
r  c                    g)Nr  rF   rn  s    r:   r  ?DeferredAddTimeoutTests.test_timeoutChainable.<locals>.<lambda>  s    r=   Nr  )r!   
addTimeoutr,   r   rP   r^   r/  rx  s     r:   test_timeoutChainable-DeferredAddTimeoutTests.test_timeoutChainable  sO    
 %J	Q 	&'	

4!5!5a!89r=   c                N  ^^ [        5       n[        5       nUR                  SU5        [        5       mSmSUU4S jjnUR                  U5        UR	                  S5        U R                  ST5        U R                  TS5        UR                  S5        U R                  T5        g)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.
r  Nr  c                   > U mT$ r4   rF   r  dCallbackedinnerDeferreds    r:   
onCallbackKDeferredAddTimeoutTests.test_successResultBeforeTimeout.<locals>.onCallback      !K  r=   r  r  r   r?   rO  )	r,   r!   rX  r   rP   assertIsNotr^   r:  rB  rc   r  rZ  r`  r^  r_  s       @@r:   test_successResultBeforeTimeout7DeferredAddTimeoutTests.test_successResultBeforeTimeout  s     #:	R )1
%)	! 	!
 	
j!	

9 	{+i0 	bM*r=   c                T  ^^ [        5       n[        5       nUR                  SU[        S9  [        5       mSmSUU4S jjnUR	                  U5        UR                  S5        U R                  ST5        U R                  TS5        UR                  S5        U R                  T5        g)z
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.
r  onTimeoutCancelNr  c                   > U mT$ r4   rF   r]  s    r:   r`  QDeferredAddTimeoutTests.test_successResultBeforeTimeoutCustom.<locals>.onCallback9  rb  r=   r  rc  )
r,   r!   rX  rR  r   rP   rd  r^   r:  rB  re  s       @@r:   %test_successResultBeforeTimeoutCustom=DeferredAddTimeoutTests.test_successResultBeforeTimeoutCustom)  s     #:	R> )1
%)	! 	!
 	
j!	

9 	{+i0 	bM*r=   c                  ^^ [        5       n[        5       nUR                  SU5        [        5       mSm[        S5      nSUU4S jjnUR	                  U5        UR                  U5        Tc   eU R                  T[        5        U R                  TR                  U5        UR                  S5        U R                  T5        g)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.
r  Nrc  c                   > U mT$ r4   rF   r9   
dErrbackedr_  s    r:   	onErrbackDDeferredAddTimeoutTests.test_failureBeforeTimeout.<locals>.onErrback[      J  r=   r  r9   r0   r?   rO  )r,   r!   rX  r  r\   r   rs  r0   rr   rb   r:  rB  rc   r  rZ  rs   rs  rr  r_  s        @@r:   test_failureBeforeTimeout1DeferredAddTimeoutTests.test_failureBeforeTimeoutK  s     $J	R )1
(,
6"	! 	!
 	
Y			% %%%j'2j&&. 	bM*r=   c                  ^^ [        5       n[        5       nUR                  SU[        S9  [        5       mSm[	        S5      nSUU4S jjnUR                  U5        UR                  U5        Tc   eU R                  T[        5        U R                  TR                  U5        UR                  S5        U R                  T5        g)z
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.
r  ri  Nrc  c                   > U mT$ r4   rF   rq  s    r:   rs  JDeferredAddTimeoutTests.test_failureBeforeTimeoutCustom.<locals>.onErrback  ru  r=   r  rv  )r,   r!   rX  rR  r  r\   r   rs  r0   rr   rb   r:  rB  rw  s        @@r:   test_failureBeforeTimeoutCustom7DeferredAddTimeoutTests.test_failureBeforeTimeoutCustomn  s     $J	R> )1
(,
6"	! 	!
 	
Y			% %%%j'2j&&. 	bM*r=   c                    [        5       n[        5       nUR                  SU5        U R                  U5        UR	                  S5        U R                  U[        R                  5        g)zt
The L{Deferred} by default errbacks with a L{defer.TimeoutError}
if it times out before callbacking or errbacking.
r  r  N)r,   r!   rX  rB  r:  r'  r   r'  rc   r  rZ  s      r:   test_timedOut%DeferredAddTimeoutTests.test_timedOut  sP    
 $J	RAbQ 2 23r=   c                    [        5       n[        5       nUR                  SU[        S9  U R	                  U5        UR                  S5        U R                  SU R                  U5      5        g)a  
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.
r  ri  r  rO  Nr,   r!   rX  rR  rB  r:  r^   r/  r  s      r:   test_timedOutCustom+DeferredAddTimeoutTests.test_timedOutCustom  sY     $J	R>Abt';';A'>?r=   c                    [        5       n[        S 5      nUR                  SU5        U R                  U5        UR	                  S5        U R                  U R                  U5      S5        g)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                $    U R                  S5      $ )NI was cancelled!rO   rf  s    r:   r  LDeferredAddTimeoutTests.test_timedOutProvidedCancelSuccess.<locals>.<lambda>  s    ajj9K.Lr=   r  r  r  N)r,   r!   rX  rB  r:  r^   r/  r  s      r:   "test_timedOutProvidedCancelSuccess:DeferredAddTimeoutTests.test_timedOutProvidedCancelSuccess  sZ     #$LM	RAb--a02DEr=   c                  ^ [        5       n[        S5      m[        U4S j5      nUR                  SU5        U R	                  U5        UR                  S5        U R                  U[        5      nU R                  UR                  T5        g)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                &   > U R                  T5      $ r4   )r   )r_  rs   s    r:   r  LDeferredAddTimeoutTests.test_timedOutProvidedCancelFailure.<locals>.<lambda>  s    qyy/?r=   r  r  N)	r,   r  r!   rX  rB  r:  r'  rr   rb   )rc   r  rZ  r9   rs   s       @r:   "test_timedOutProvidedCancelFailure:DeferredAddTimeoutTests.test_timedOutProvidedCancelFailure  sm     7#$%?@	RAb  J/aggu%r=   c                  ^^ [        5       n[        5       nUR                  SU5        [        5       mSmSUU4S jjnUR                  U5        UR	                  5         Tc   eU R                  T[        5        U R                  TR                  [        R                  5        UR                  S5        U R                  T5        g)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}.
r  Nc                   > U mT$ r4   rF   r9   	dCanceledr_  s    r:   rs  CDeferredAddTimeoutTests.test_cancelBeforeTimeout.<locals>.onErrback      I  r=   r  rv  )r,   r!   rX  r\   r&  rs  r0   rr   r   r   r   r:  rB  rc   r  rZ  rs  r  r_  s       @@r:   test_cancelBeforeTimeout0DeferredAddTimeoutTests.test_cancelBeforeTimeout  s     $J	R )1
		! 	!
 	
Y	
 $$$i1inne&:&:; 	bM*r=   c                  ^^ [        5       n[        5       nUR                  SU[        S9  [        5       mSmSUU4S jjnUR	                  U5        UR                  5         Tc   eU R                  T[        5        U R                  TR                  [        R                  5        UR                  S5        U R                  T5        g)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}.
r  ri  Nc                   > U mT$ r4   rF   r  s    r:   rs  IDeferredAddTimeoutTests.test_cancelBeforeTimeoutCustom.<locals>.onErrback  r  r=   r  rv  )r,   r!   rX  rR  r\   r&  rs  r0   rr   r   r   r   r:  rB  r  s       @@r:   test_cancelBeforeTimeoutCustom6DeferredAddTimeoutTests.test_cancelBeforeTimeoutCustom  s     $J	R> )1
		! 	!
 	
Y	
 $$$i1inne&:&:; 	bM*r=   c                    [        5       n[        S 5      nUR                  SU[        S9  U R	                  U5        UR                  S5        U R                  SU R                  U5      5        g)z]
A custom translation function can handle a L{Deferred} with a
custom cancellation function.
c                6    U R                  [        S5      5      $ )Nr  )r   r  rf  s    r:   r  VDeferredAddTimeoutTests.test_providedCancelCalledBeforeTimeoutCustom.<locals>.<lambda>  s    qyyG9L/Mr=   r  ri  r  rO  Nr  r  s      r:   ,test_providedCancelCalledBeforeTimeoutCustomDDeferredAddTimeoutTests.test_providedCancelCalledBeforeTimeoutCustom  s]    
 $%MN	R>Abt';';A'>?r=   c                t  ^ [        5       n[        5       nSmSU4S jjnUR                  U5        UR                  SU5        UR	                  S5        Tc   eU R                  T[        5        U R                  TR                  [        R                  5        U R                  U[        R                  5        g)z
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                   > U mU $ r4   rF   r9   rr  s    r:   r   GDeferredAddTimeoutTests.test_errbackAddedBeforeTimeout.<locals>.errback1      JHr=   r  r  r9   r0   r?   r0   )r,   r!   r\   rX  r:  rs  r0   rb   r   r   r'  r'  rc   r  rZ  r   rr  s       @r:   test_errbackAddedBeforeTimeout6DeferredAddTimeoutTests.test_errbackAddedBeforeTimeout%  s     $J
	
 	
W	Rb%%%j'2j..0D0DEQ 2 23r=   c                V  ^ [        5       n[        5       nSmSU4S jjnUR                  U5        UR                  SU5        UR	                  S5        Tc   eU R                  T[        5        U R                  TR                  [        R                  5        U R                  U5        g)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.
Nc                H   > U mU R                  [        R                  5        g r4   )r  r   r   r  s    r:   r   ]DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellation.<locals>.errbackM  s    JFF5''(r=   r  r  r  )r,   r!   r\   rX  r:  rs  r0   rb   r   r   r/  r  s       @r:   4test_errbackAddedBeforeTimeoutSuppressesCancellationLDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationA  s     $J
	)
 	
W	Rb%%%j'2j..0D0DEQr=   c                  ^ [        5       n[        5       nSmSU4S jjnUR                  U5        UR                  SU[        5        UR                  S5        Tc   eU R                  T[        5        U R                  TR                  [        R                  5        U R                  SU R                  U5      5        g)z
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                   > U mU $ r4   rF   r  s    r:   r   MDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutCustom.<locals>.errbacki  r  r=   r  r  rO  r  r,   r!   r\   rX  rR  r:  rs  r0   rb   r   r   r^   r/  r  s       @r:   $test_errbackAddedBeforeTimeoutCustom<DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutCustom]  s     $J
	
 	
W	R.b%%%j'2j..0D0DEt';';A'>?r=   c                  ^ [        5       n[        5       nSmSU4S jjnUR                  U5        UR                  SU[        5        UR                  S5        Tc   eU R                  T[        5        U R                  TR                  [        R                  5        U R                  SU R                  U5      5        g)z
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                
   > U mg r4   rF   r  s    r:   r   cDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationCustom.<locals>.errback  s    Jr=   r  r  rO  r  r  r  s       @r:   :test_errbackAddedBeforeTimeoutSuppressesCancellationCustomRDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationCustomy  s     $J
	 	
W	R.b%%%j'2j..0D0DEt';';A'>?r=   c                   ^^ [        5       nSm[        U4S j5      nSmSU4S jjnUR                  U5        UR                  SU5        UR	                  S5        U R                  TT5        U R                  TU R                  U5      5        g)z
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}.
rV  c                &   > U R                  T5      $ r4   rO   rZ  rV  s    r:   r  SDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout.<locals>.<lambda>      ajj.Ar=   Nc                   > U mU $ r4   rF   rb   r^  s    r:   rP   SDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout.<locals>.callback      KLr=   r  r  rb   r   r?   r   )r,   r!   r   rX  r:  r^   rr   r/  rc   r  rZ  rP   r^  rV  s       @@r:   )test_callbackAddedToCancelerBeforeTimeoutADeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout  sw     #$AB	
 	
h	Rbg.gt33A67r=   c                &  ^^ [        5       nSm[        U4S j5      nSmS	U4S jjnUR                  U5        UR                  SU[        S9  UR                  S5        U R                  TT5        U R                  SU R                  U5      5        g)
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}.  The timeout's custom timeout function
also runs.
rV  c                &   > U R                  T5      $ r4   rO   r  s    r:   r  YDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom.<locals>.<lambda>  r  r=   Nc                   > U mU $ r4   rF   r  s    r:   rP   YDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom.<locals>.callback  r  r=   r  ri  r  rO  r  )r,   r!   r   rX  rR  r:  r^   r/  r  s       @@r:   /test_callbackAddedToCancelerBeforeTimeoutCustomGDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom  s}     #$AB	
 	
h	R>bg.t';';A'>?r=   rF   Nr   )rH   rI   rJ   rK   rj   rY  rf  rm  rx  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  rL   rF   r=   r:   rT  rT    sm    	:+B +D!+F"+H4@"F&"!+F!+F@48 8@8@688@r=   rT  c                  @    \ rS rSrSrS	S jrS	S jrS	S jrS	S jrSr	g)
EnsureDeferredTestsi  z
Tests for L{ensureDeferred}.
c                P    [        5       n[        U5      nU R                  X5        g)z;
L{ensureDeferred} will pass through a Deferred unchanged.
N)r!   r*   rr   r  s      r:   test_passesThroughDeferreds/EnsureDeferredTests.test_passesThroughDeferreds  s!     &ZBbr=   c                    U R                  [        R                  5         [        S5        SSS5        g! , (       d  f       g= f)zZ
Passing L{ensureDeferred} a non-coroutine and a non-Deferred will
raise a L{ValueError}.
	somethingN)rC  r   rD  r*   r   s    r:   'test_willNotAllowNonDeferredOrCoroutine;EnsureDeferredTests.test_willNotAllowNonDeferredOrCoroutine  s,    
 u778;' 988s	   5
Ac                   SS jnU" 5       nU R                  U[        R                  5        [        U5      n[	        U[
        [           5        U R                  U[
        5        U R                  U5      nU R                  US5        g)z=
L{ensureDeferred} will turn a coroutine into a L{Deferred}.
c                 R   #    [         R                  " S5      n U I S h  vN nU$  N7fNr  r  rZ  r  s     r:   r  =EnsureDeferredTests.test_ensureDeferredCoroutine.<locals>.run  s$     e$A'CJ    '%'r  N)r?   r   )	rs  typesCoroutineTyper*   r   r!   r   r/  r^   rc   r  r}  rZ  r  s        r:   test_ensureDeferredCoroutine0EnsureDeferredTests.test_ensureDeferredCoroutine  r    
	 Ea!4!45 1Ax}%a* ""1%e$r=   c                   SS jnU" 5       nU R                  U[        R                  5        [        U5      n[	        U[
        [           5        U R                  U[
        5        U R                  U5      nU R                  US5        g)zH
L{ensureDeferred} will turn a yield-from coroutine into a L{Deferred}.
c               3  R   #    [         R                  " S5      n U  S h  vN nU$  N7fr  r  r  s     r:   r  =EnsureDeferredTests.test_ensureDeferredGenerator.<locals>.run  s$     e$A,CJ r  r  N)r?   z#Generator[Deferred[str], None, str])	rs  r  GeneratorTyper*   r   r!   r   r/  r^   r  s        r:   test_ensureDeferredGenerator0EnsureDeferredTests.test_ensureDeferredGenerator  r  r=   rF   Nr   )
rH   rI   rJ   rK   rj   r  r  r  r  rL   rF   r=   r:   r  r    s    (%.%r=   r  c                  "    \ rS rSrSrSS jrSrg)TimeoutErrorTestsi  z)
L{twisted.internet.defer} timeout code.
c                Z   [        5       n[        R                  " U5        U R                  U[        R                  5        U R                  U R                  /5      nU R                  [        U5      S5        U R                  US   S   [        5        U R                  US   S   S5        g)z2
L{twisted.internet.defer.timeout} is deprecated.
rZ   r   r   r   z~twisted.internet.defer.timeout was deprecated in Twisted 17.1.0; please use twisted.internet.defer.Deferred.addTimeout insteadN)r!   r   r!  r&  r'  r   test_deprecatedTimeoutr^   r_   rr   r   )rc   rR   warningsShowns      r:   r  (TimeoutErrorTests.test_deprecatedTimeout  s     &.Zh8U%7%78**D,G,G+HI]+Q/mA&z24FG!Y'M	
r=   rF   Nr   )rH   rI   rJ   rK   rj   r  rL   rF   r=   r:   r  r    s    
r=   r  c                Z    U R                  U R                  5        U R                  5         g)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_soonstoprun_foreverloops    r:   callAllSoonCallsr  (  s      	NN499r=   c                  d    \ rS rSrSS jrSS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrS
rg)DeferredFutureAdapterTestsi4  c                P    [        5       nU R                  UR                  5        U$ )zE
Create a new event loop that will be closed at the end of the test.
)_new_event_loopr   closer  s     r:   newLoop"DeferredFutureAdapterTests.newLoop5  s!     !"%r=   c                T   [        5       nU R                  5       nUR                  U5      nU R                  UR	                  5       S5        UR                  S5        [        U5        U R                  U R                  U5      S5        U R                  UR                  5       S5        g)z_
L{Deferred.asFuture} returns a L{asyncio.Future} which fires when
the given L{Deferred} does.
Fr  N)	r!   r   asFuturer^   r  rP   r  r/  rt   )rc   rZ  r  aFutures       r:   test_asFuture(DeferredFutureAdapterTests.test_asFuture=  s    
 $:||~**T"/	

2--a0$7)2.r=   c                L  ^ SmSU4S jjn[        U5      nU R                  5       nUR                  U5      nUR                  5         [	        U5        U R                  T5        U R                  U R                  U5      S5        U R                  [        UR                  5        g)zo
L{Deferred.asFuture} returns a L{asyncio.Future} which, when
cancelled, will cancel the original L{Deferred}.
Fc                
   > Smg r  rF   )dprimerd  s    r:   cancelerFDeferredFutureAdapterTests.test_asFutureCancelFuture.<locals>.cancelerS  s    Fr=   Nr	  rL  r?   r   )r!   r   r  r&  r  r`   r^   r/  rC  r   rt   )rc   r
  rZ  r  r  rd  s        @r:   test_asFutureCancelFuture4DeferredFutureAdapterTests.test_asFutureCancelFutureK  s     	 %X.||~**T"--a0$7.'..9r=   c                   SS jn[        U5      nU R                  5       nUR                  U5      nUR                  5         [	        U5        U R                  U R                  U5      S5        U R                  [        UR                  5        g)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                &    U R                  S5        g )Nr  rO   )r	  s    r:   r
  GDeferredFutureAdapterTests.test_asFutureSuccessCancel.<locals>.cancelerg  s    OOAr=   Nr  )
r!   r   r  r&  r  r^   r/  rC  r   rt   )rc   r
  rZ  r  r  s        r:   test_asFutureSuccessCancel5DeferredFutureAdapterTests.test_asFutureSuccessCancel`  sm    	 %X.||~**T"--a0$7.'..9r=   c                   [        5       n[        [        5       5      nU R                  5       nUR	                  U5      n[        U5        UR                  U5        [        U5        U R                  [        UR                  5        g)zh
L{Deferred.asFuture} makes a L{asyncio.Future} fire with an
exception when the given L{Deferred} does.
N)	r!   r0   rr  r   r  r  r   rC  rt   )rc   rZ  
theFailurer  futures        r:   test_asFutureFailure/DeferredFutureAdapterTests.test_asFutureFailurer  sc    
 %J.01
||~D!			*+V]];r=   c                    U R                  5       n[        US9n[        R                  " U5      nU R	                  U5        UR                  S5        [        U5        U R                  U R                  U5      S5        g)z`
L{Deferred.fromFuture} returns a L{Deferred} that fires
when the given L{asyncio.Future} does.
r  r  N)	r   r   r!   
fromFuturerB  
set_resultr  r^   r/  )rc   r  r  rZ  s       r:   test_fromFuture*DeferredFutureAdapterTests.test_fromFuture  sh    
 ||~%40(A1--a0!4r=   c                    U R                  5       n[        US9n[        R                  " U5      nUR	                  5         [        U5        U R                  [        UR                  5        U R                  U5      R                  [        5        g)z
L{Deferred.fromFuture} makes a L{Deferred} fire with
an L{asyncio.CancelledError} when the given
L{asyncio.Future} is cancelled.
r  N)r   r   r!   r  r&  r  rC  r   rt   r'  r  rc   r  	cancelledrZ  s       r:   test_fromFutureFutureCancelled9DeferredFutureAdapterTests.test_fromFutureFutureCancelled  sl     ||~"(d"3		*.)*:*:;Q$$^4r=   c                `   U R                  5       n[        US9n[        R                  " U5      nUR	                  5         [        U5        U R                  UR                  5       S5        U R                  [        UR                  5        U R                  U5      R                  [        5        g)zv
L{Deferred.fromFuture} makes a L{Deferred} which, when
cancelled, cancels the L{asyncio.Future} it was created from.
r  TN)r   r   r!   r  r&  r  r^   r   rC  r   rt   r'  r  r  s       r:    test_fromFutureDeferredCancelled;DeferredFutureAdapterTests.test_fromFutureDeferredCancelled  s    
 ||~"(d"3		*	
,,.5.)*:*:;Q$$^4r=   rF   N)r?   r   r   )rH   rI   rJ   rK   r   r  r  r  r  r  r!  r$  rL   rF   r=   r:   r  r  4  s*    /:*:$<555r=   r  c                  n    \ rS rSrS
S jrS
S jr\S
S j5       r\S
S j5       r\S
S j5       r	S
S jr
Srg	)CoroutineContextVarsTestsi  c                  ^ ^^^^^ [        5       m[        R                  " S5      mTR                  S5        [	        5       mTR                  U4S j5        [	        5       mTR                  U4S j5        TR                  S 5        [        R                  SUU4S jj5       m[        T[        / [        S   4   5        [        R                  SUUUU UU4S jj5       n[        U[        / [        [        S	      4   5        TR                  S
5        U" 5       nTR                  S5        TR                  S5        TR                  S5        T R                  T R                  U5      S	5        g)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.
testvarrZ   c                &   > TR                  S5      $ ra  setro  vars    r:   r  DCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.<lambda>      swwqzr=   c                &   > TR                  S5      $ N   r+  r-  s    r:   r  r/        
r=   c                    SS-  $ rm  rF   rn  s    r:   r  r/        Ar=   c               3     >#    [        5       n TR                  SU R                  S5        U v   TR                  S5        g 7fNrZ   Trb  r!   r/  rP   r,  rZ  r  r.  s    r:   yieldingDeferredLCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.yieldingDeferred  s1     'zAOOAqzz40GGGAJs   >ANc               3  L  >#    TR                  TR                  5       S5        [        R                  " S5      v   TR                  TR                  5       S5        T R	                  STR
                  S5        Tv   TR                  TR                  5       S5        T R	                  STR
                  S5         Tv   [        S5      e! [         a#    TR                  TR                  5       S5         Of = fT" 5       v   TR                  TR                  5       S5        g7f)Nr   rZ   T???? should have failedr^   r  r   rW  r/  rP   r  )r  mutatingDeferredmutatingDeferredThatFailsrc   r.  r;  s   r:   testFunctionHCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.testFunction  s      SWWY* --"" SWWY* OOA/88$?"" SWWY* OOA8AA4H;//   9::  /  A./ #$$SWWY*s*   B7D$;C
 ?D$
*C74D$6C77-D$Tr   r?   z#Generator[Deferred[Any], Any, None])r?   z,Generator[Deferred[Any], Any, Literal[True]])r,   contextvars
ContextVarr,  r!   r   r   inlineCallbacksr   r	   r   r:  r^   r/  )rc   rB  rZ  r  r@  rA  r.  r;  s   `  @@@@@r:   test_withInlineCallbacks2CoroutineContextVarsTests.test_withInlineCallbacks  s.   
 +6+A+A)+L
 ,4:$$%9:3;:!!--.BC!--o>				 
	 	$hr8D>/A&BC 
		"	 "	 
"	H 	L(2x/F+F"GH
N 	a 	a 	a--a0$7r=   c                   ^ ^^ [        5       m[        R                  " S5      m[        R                  SUU U4S jj5       nTR                  S5        U" 5       nTR                  S5        T R                  U5        g)z|
When an inlineCallbacks function resumes, we should be able to reset() a
contextvar that was set when it was first called.
r)  c               3  (  >#    TR                  S5      n [        5       nTR                  SUR                  S5        Uv   TR	                  TR                  5       S5        TR                  U 5        TR	                  TR                  5       S5        g 7f)Nrb  rZ   Tr   )r,  r!   r/  rP   r^   r  reset)tokenrZ  r  rc   r.  s     r:   r;  QCoroutineContextVarsTests.test_resetWithInlineCallbacks.<locals>.yieldingDeferred  so      GGAJE  (zAOOAqzz40GSWWY*IIeSWWY*s   BBr   rZ   NrD  )r,   rE  rF  r   rG  r,  r:  r/  )rc   r;  rZ  r  r.  s   `  @@r:   test_resetWithInlineCallbacks7CoroutineContextVarsTests.test_resetWithInlineCallbacks  sj    
 +6+A+A)+L				+ 	+ 
	+ 	
 	aQr=   c                  ^#    [        5       mSnT ISh  vN   U R                  TR                  5        TR                  5       nUR	                  U4S j5        U R                  TR                  5        U R                  UR                  5        SSS5      ISh  vN   U R                  UR                  5        UI Sh  vN   U R                  TR                  5        g N NG! , ISh  vN  (       d  f       N\= f N>7f)zA
L{DeferredLock} can be used as an asynchronous context manager.
Nc                $   > TR                  5       $ r4   r  )ro  r  s    r:   r  >CoroutineContextVarsTests.test_asyncWithLock.<locals>.<lambda>&  s    DLLNr=   )r$   r`   r  r  r   r0  rd  )rc   rZ  r  s     @r:   test_asyncWithLock,CoroutineContextVarsTests.test_asyncWithLock  s     
 ~4OODKK(AMM23OODKK(QXX& 4 	!% 444 	sQ   DC"DA7C&DC$$DD !D$D&C=,C/-C=9Dc                  ^#    [        S5      mT ISh  vN   U R                  TR                  S5        T ISh  vN   U R                  TR                  S5        TR                  5       nTR                  5       nU R                  TR                  S5        U R	                  UR
                  5        U R                  UR
                  5        SSS5      ISh  vN   U R                  TR                  S5        U R	                  WR
                  5        WR                  U4S j5        UR                  U4S j5        UI Sh  vN   UI Sh  vN   U R                  TR                  S5        SSS5      ISh  vN   U R                  TR                  S5        g GN GNd N! , ISh  vN  (       d  f       N= f Ny Nq NG! , ISh  vN  (       d  f       N\= f7f)zF
L{DeferredSemaphore} can be used as an asynchronous context
manager.
rb  Nr   rZ   r   c                $   > TR                  5       $ r4   rS  ro  r  s    r:   r  CCoroutineContextVarsTests.test_asyncWithSemaphore.<locals>.<lambda>@      S[[]r=   c                $   > TR                  5       $ r4   rS  rY  s    r:   r  rZ  A  r[  r=   )r&   r^   tokensr  r`   rd  r0  r   )rc   r	  r  r  s      @r:   test_asyncWithSemaphore1CoroutineContextVarsTests.test_asyncWithSemaphore-  s8      "3SZZ+s  Q/[[][[]  Q/		*  + s SZZ+OOBII&NN23NN23HHHHSZZ+ 3  	Q'! sss  333s   GFG#GF G BF%GF#A*GF?	GG G0G;G<"G G#G%F<	+F.,F<	8GGGGGGGc                T  #    [        5       nU R                  [        S5         U ISh  vN   U R                  UR                  5        [        S5      e N*! , ISh  vN  (       d  f       O= f SSS5        O! , (       d  f       O= fU R                  UR                  5        g7f)z_
C{DeferredLock} correctly propagates exceptions when
used as an asynchronous context manager.
zsome specific exceptionN)r$   assertRaisesRegexr  r`   r  r0  r5  s     r:   test_asyncWithLockException5CoroutineContextVarsTests.test_asyncWithLockExceptionG  so      ~##I/HIt, 9:: tttt JII 	%sI   !B(A;AA;'AA;A.	A A.	*A;2	B(;
B	#B(c                (  ^ ^^^^^ [        5       m[        R                  " S5      mTR                  S5        [	        5       mTR                  U4S j5        [	        5       mTR                  U4S j5        TR                  S 5        SUU4S jjmSUUUUU U4S jjnTR                  S5        [        U" 5       5      nTR                  S5        TR                  S5        TR                  S5        T R                  T R                  U5      S	5        g
)z
When a coroutine is called, the context is taken from when it was first
called. When it resumes, the same context is applied.
r)  rZ   c                &   > TR                  S5      $ ra  r+  r-  s    r:   r  JCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.<lambda>`  r0  r=   c                &   > TR                  S5      $ r2  r+  r-  s    r:   r  rf  c  r4  r=   c                    SS-  $ rm  rF   rn  s    r:   r  rf  d  r6  r=   c                    >#    [        5       n TR                  SU R                  S5        U I S h  vN   TR                  S5        g  N7fr8  r9  r:  s    r:   asyncFuncAwaitingDeferred[CoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.asyncFuncAwaitingDeferredf  s8      (
AOOAqzz40GGGGAJ s   -A	AA	c                   >#    TR                  TR                  5       S5        [        R                  " S5      I S h  vN   TR                  TR                  5       S5        TR	                  STR
                  S5        TI S h  vN   TR                  TR                  5       S5        TR	                  STR
                  S5         TI S h  vN   [        S5      e N NY N! [         a#    TR                  TR                  5       S5         Of = fT " 5       I S h  vN    TR                  TR                  5       S5        g7f)Nr   rZ   r   Tr>  rb  r?  )rj  r  r@  rA  rc   r.  s   r:   rB  NCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.testFunctionm  s    SWWY* --""" SWWY* OOA/88$?""" SWWY* OOA8AA4H;///   9::) # # 0 /  A./ ,---SWWY*se   :ECAECAEC" C C" EE C" "*DEDED&Er   TNr   )r?   r   )
r,   rE  rF  r,  r!   r   r*   r:  r^   r/  )rc   rB  rZ  rj  r  r@  rA  r.  s   `  @@@@@r:   test_contextvarsWithAsyncAwait8CoroutineContextVarsTests.test_contextvarsWithAsyncAwaitT  s    
 +6+A+A)+L
 ,4:$$%9:4<J!!--.BC!--o>	 	!	 !	H 	
<>* 	a 	a 	a--a0$7r=   rF   Nr   )rH   rI   rJ   rK   rH  rO  rB   rU  r^  rb  rn  rL   rF   r=   r:   r'  r'    sQ    O8b @ & &  ( (2 
& 
&I8r=   r'  c                  T    \ rS rSrSS jr\R                  " \S5      SS j5       rSr	g)InlineCallbackTestsi  c                    S SSU R                   S[        /nU H2  nU R                  [        R                  [        R
                  U5        M4     g)r=  c                    U $ r4   rF   r  s    r:   r  IInlineCallbackTests.test_fromCoroutineRequiresCoroutine.<locals>.<lambda>  r@  r=   rZ   TNrA  rE  s      r:   rB  7InlineCallbackTests.test_fromCoroutineRequiresCoroutine  rI  r=   rJ  c                N   [        5       n[        R                  " U5      nSS jn[        R                  " U" U5      5      nU R                  XR                  U5      5        [        R                  " U5      nAAU R                  U" 5       5        U R                  U" 5       5        g)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.
c                   #    U $ 7fr4   rF   )r;  s    r:   r  CInlineCallbackTests.test_coroutineNoCircularReference.<locals>.func  s
     Hs   N)r;  r   r?   r   )r,  rM  rN  r!   r5   r^   r/  r   )rc   obj
objWeakRefr  funcDfuncDWeakRefs         r:   !test_coroutineNoCircularReference5InlineCallbackTests.test_coroutineNoCircularReference  s     [[%
	 &&tCy12259:{{5)  	*,',.)r=   rF   Nr   )
rH   rI   rJ   rK   rB  rg  rh  r.   r}  rL   rF   r=   r:   rq  rq    s(    W. ]]59:* ;*r=   rq  )r9   z0Callable[..., Coroutine[Deferred[Any], Any, _T]]r?   zCallable[..., Deferred[_T]])rR   r  r?   r   )rt  rt  r?   z(tuple[list[int], list[Deferred[object]]])rP  r>   rQ  floatr?   r   )r  r   r?   r   )crj   
__future__r   rE  r@   rp  r  r  r  r1   rg  r   rM  asyncior   r   r   r   r  typingr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   hamcrestr   r   r   
hypothesisr   hypothesis.strategiesr   typing_extensionsr   twisted.internetr   r   twisted.internet.deferr    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   twisted.internet.taskr,   twisted.pythonr-   twisted.python.compatr.   twisted.python.failurer0   twisted.trialrB   r  rD   rS   rT   rW   r[   rl   SynchronousTestCaser   rL  rv  rx  r  r  r  r]  r  r  r  rR  rT  r  r  r  r  r'  rq  rF   r=   r:   <module>r     s1   #   	 	          & 2 1  * ) +    (  ' * "7 	9 	) m954 4,*M!! *MZU9H002G U9p4 &~7,, ~7B;$h22 ;$|L$55 L$^GIX99 GITc3x++ c3LEX99 E"A'8779N A'Hz=("3"3 z=zS@h:: S@lA%(++ A%H
))+@ 
,	s5!2!2 s5ls8 1 1 s8l3*(66 3*r=   