
    /Jf=                   v   d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z
ddlZddlZddlmZmZmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z" ddl#m$Z$m%Z%m&Z& ddl'm(Z( ddl)m*Z* dd	l+m,Z, dd
l-m.Z.m/Z/ ddl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< ddl=m>Z> ddl?m@Z@ ddlAmBZB ddlCmDZD ddlEm	Z	 dSdZF G d deG          ZHdTdZIdUd ZJ e d!eG"          ZK G d# d$          ZL G d% d&e	jM                  ZN G d' d(e	jO        eL          ZP G d) d*          ZQdVd.ZR G d/ d0e	jO                  ZS G d1 d2e	jO                  ZT G d3 d4e	jO                  ZU G d5 d6e	jO                  ZV G d7 d8e	jO                  ZW G d9 d:e	jO                  ZX G d; d<e	jO        eL          ZY G d= d>e	jM                  ZZdWdCZ[ G dD dEe	jO                  Z\ G dF dGe	jM                  Z] G dH dIe	jM        eL          Z^dXdLZ_ G dM dNe	jM                  Z` G dO dPe	jM                  Za G dQ dRe	jO                  ZbdS )Yz+
Test cases for L{twisted.internet.defer}.
    )annotationsN)AbstractEventLoopCancelledErrorFuturenew_event_loop)AnyCallable	CoroutineDict	GeneratorList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)unittestf0Callable[..., Coroutine[Deferred[Any], Any, _T]]returnCallable[..., Deferred[_T]]c                H     t          j                   d fd            }|S )Nargsobjectkwargsr3   Deferred[_T]c                 <     | i |}t          j        |          S N)r    fromCoroutine)r6   r8   coror1   s      Z/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/twisted/test/test_defer.pywrapperz!ensuringDeferred.<locals>.wrapperI   s(    q$!&!!%d+++    )r6   r7   r8   r7   r3   r9   )	functoolswraps)r1   r?   s   ` r>   ensuringDeferredrC   F   s?     _Q, , , , , , Nr@   c                      e Zd ZdS )GenericErrorN__name__
__module____qualname__ r@   r>   rE   rE   Q   s        Dr@   rE   r6   r7   r8   r/   c                 L    	 ddz   n# t           $ r t          | i |}Y nw xY w|S )z
    Make a L{Failure} of a divide-by-zero error.

    @param args: Any C{*args} are passed to Failure's constructor.
    @param kwargs: Any C{**kwargs} are passed to Failure's constructor.
       r   )BaseExceptionr/   )r6   r8   r1   s      r>   getDivisionFailurerN   U   sH    %	A % % %T$V$$%Hs    !!deferredDeferred[str]Nonec                0    |                      d           dS )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rO   s    r>   fakeCallbackCancellerrW   c   s     '(((((r@   _ExceptionT)boundc                      e Zd ZdZd
dZd	S )ImmediateFailureMixinz+
    Add additional assertion methods.
    rO   Deferred[Any]	exceptionType[_ExceptionT]r3   rX   c                P   t          t          j        |           }g }|                    |j                   |                    t          |          d           |                    |d                             |                     t          t          |d         j
                  S )z
        Assert that the given Deferred current result is a Failure with the
        given exception.

        @return: The exception instance in the Deferred.
        rL   r   )r   r0   TestCase
addErrbackappendassertEquallen
assertTruecheckrX   value)selfrO   r]   testCasefailuress        r>   assertImmediateFailurez,ImmediateFailureMixin.assertImmediateFailureu   s     )400"$HO,,,S]]A...HQK--i88999K!!2333r@   N)rO   r\   r]   r^   r3   rX   )rG   rH   rI   __doc__rk   rJ   r@   r>   r[   r[   p   s2         4 4 4 4 4 4r@   r[   c                  *    e Zd ZdZddZddZddZdS )		UtilTestsz&
    Tests for utility functions.
    r3   rQ   c                    t          t                                }t          j        |          }|                     t                     |                     ||           dS )z<
        L{defer.logError} returns the given error.
        N)r/   RuntimeErrorr   logErrorflushLoggedErrorsassertIs)rh   errorresults      r>   test_logErrorReturnsErrorz#UtilTests.test_logErrorReturnsError   sR     ''&&|,,,eV$$$$$r@   c                    t          t                                }t          j        |           |                     t                    }|                     ||g           dS )z9
        L{defer.logError} logs the given error.
        N)r/   rp   r   rq   rr   rc   )rh   rt   errorss      r>   test_logErrorLogsErrorz UtilTests.test_logErrorLogsError   sV     ''u''55%)))))r@   c                .   g d	fd}t          j        |           t          t                                }t	          j        |           |                     t                     |                     d                             d                     dS )
zR
        The text logged by L{defer.logError} has no repr of the failure.
        	eventDictDict[str, Any]r3   rQ   c                b    t          j        |           }|J                     |           d S r;   )r,   textFromEventDictrb   )r{   textoutputs     r>   emitz4UtilTests.test_logErrorLogsErrorNoRepr.<locals>.emit   s7    (33D###MM$r@   r   Unhandled Error
Traceback N)r{   r|   r3   rQ   )	r,   addObserverr/   rp   r   rq   rr   re   
startswith)rh   r   rt   r   s      @r>   test_logErrorLogsErrorNoReprz&UtilTests.test_logErrorLogsErrorNoRepr   s     	  	  	  	  	  	 
 	''u|,,,q	,,-JKKLLLLLr@   Nr3   rQ   )rG   rH   rI   rl   rv   ry   r   rJ   r@   r>   rn   rn      s`         % % % %* * * *M M M M M Mr@   rn   c                  8   e Zd Zd^dZd_dZd`d	ZdadZd^dZd^dZd^dZ	d^dZ
d^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^d Zd^d!Zd^d"Zd^d#Zd^d$Zd^d%Z d^d&Z!d^d'Z"d^d(Z#d^d)Z$d^d*Z%d^d+Z&d^d,Z'd^d-Z(d^d.Z)d^d/Z*d^d0Z+d^d1Z,d^d2Z-d^d3Z.d^d4Z/d^d5Z0d^d6Z1d^d7Z2d^d8Z3d^d9Z4d^d:Z5d^d;Z6d^d<Z7d^d=Z8d^d>Z9d^d?Z:d^d@Z;d^dAZ<d^dBZ=d^dCZ>d^dDZ?d^dEZ@d^dFZAd^dGZBd^dHZCd^dIZDd^dJZEd^dKZFd^dLZGd^dMZHd^dNZId^dOZJd^dPZKd^dQZLd^dRZMd^dSZNd^dTZOd^dUZPd^dVZQd^dWZRd^dXZS eTjU        eVdY          d^dZ            ZW eTjU        eVdY          d^d[            ZX eTjU        eVdY          d^d\            ZYd]S )bDeferredTestsr3   rQ   c                    d | _         d | _        d | _        |                     t          j        t	          j                               d S r;   )callbackResultscallback2ResultserrbackResults
addCleanupr   setDebugginggetDebuggingrh   s    r>   setUpzDeferredTests.setUp   sQ      	
  	
  	
 	*E,>,@,@AAAAAr@   r6   r7   r8   r   c                $    ||f| _         |d         S Nr   r   rh   r6   r8   s      r>   	_callbackzDeferredTests._callback   s    #V|Awr@   c                    ||f| _         d S r;   r   r   s      r>   
_callback2zDeferredTests._callback2   s     $fr@   r/   c                    ||f| _         d S r;   r   r   s      r>   _errbackzDeferredTests._errback   s    "Flr@   c                    t                      }|                    | j                   |                    d           |                     | j                   |                     | j        di f           d S Nhellor   r    addCallbackr   rU   assertIsNoner   rc   r   rh   rO   s     r>   testCallbackWithoutArgsz%DeferredTests.testCallbackWithoutArgs   sp    "***T^,,,'"""$-...-
B/?@@@@@r@   c                    t                      }|                    | j        d           |                    d           |                     | j                   |                     | j        di f           d S )Nworldr   )r   r   r   r   s     r>   testCallbackWithArgsz"DeferredTests.testCallbackWithArgs   ss    "***T^W555'"""$-...-0BB/GHHHHHr@   c                    t                      }|                    | j        d           |                    d           |                     | j                   |                     | j        dddif           d S )Nr   )r   r   r   r   r   s     r>   testCallbackWithKwArgsz$DeferredTests.testCallbackWithKwArgs   sz    "***T^7;;;'"""$-...-
Wg<N/OPPPPPr@   c                \   t                      }|                    | j                   |                    | j                   |                    d           |                     | j                   |                     | j        di f           |                     | j	        di f           d S r   )
r    r   r   r   rU   r   r   rc   r   r   r   s     r>   testTwoCallbackszDeferredTests.testTwoCallbacks   s    "***T^,,,T_---'"""$-...-
B/?@@@.R0@AAAAAr@   c                P   t          d          }t                      }|                    | j        t	          t
          dt          f         d                     |                    |           |                    | j	                   | 
                    | j                   | j        J |                     t          | j        d                   d           |                     | j        d         d         j        |           |                     | j        d         i            dS )zQ
        If given None for an errback, addCallbacks uses a pass-through.
        oopsie.Nr   rL   )rE   r    addCallbacksr   r   r	   r7   errbackra   r   r   r   r   rc   rd   rg   rh   rt   rO   s      r>   test_addCallbacksNoneErrbackz*DeferredTests.test_addCallbacksNoneErrback   s    X&&#+::dnd8CK3H$.O.OPPPDM***$.///"...T0344a888,Q/28%@@@,Q/44444r@   c           
        t                      }|                    | j        | j        t	          t
          t                   d          t	          t          t          t          f         d          di            |	                    d           | 
                    | j                   |                     | j        di f           dS )zR
        If given None as a callback args and kwargs, () and {} are used.
        NrJ   r   r   )r    r   r   r   r   r   r7   r   strrU   r   r   rc   r   r   s     r>   !test_addCallbacksNoneCallbackArgsz/DeferredTests.test_addCallbacksNoneCallbackArgs   s     #+**NMv%%f%t,,	
 	
 	
 	'"""$-...-
B/?@@@@@r@   c                   t          d          }t                      }|                    | j        | j        di t          t          t                   d          t          t          t          t          f         d                     |
                    |           |                    | j                   |                     | j                   | j        J |                     t!          | j        d                   d           |                     | j        d         d         j        |           |                     | j        d         i            dS )zQ
        If given None as a errback args and kwargs, () and {} are used.
        r   rJ   Nr   rL   )rE   r    r   r   r   r   r   r7   r   r   r   ra   r   r   r   rc   rd   rg   r   s      r>    test_addCallbacksNoneErrbackArgsz.DeferredTests.test_addCallbacksNoneErrbackArgs
  s+    X&&#+::NMv%%f%t,,	
 	
 	
 	DM***$.///"...T0344a888,Q/28%@@@,Q/44444r@   c                   t           t          t          t          t          t
          f         f                  }t                      }t                      }t                      }t          |||g          }g }|fdd}dd	}|                    ||           |	                    d
           |
                    |           |                    t          d                     |	                    d           |                     |d         |d         d         t	          t          t
          |d         d                   j                  f|d         gt           j        d
ft           j        dft           j        dfg           d S )N
resultList
ResultListru   r3   rQ   c                0    |                     |            d S r;   )extendr   ru   s     r>   cbz*DeferredTests.testDeferredList.<locals>.cb)  s    MM*%%%%%r@   errr/   c                    d S r;   rJ   r   s    r>   catchz-DeferredTests.testDeferredList.<locals>.catch,      4r@   123r   rL      )r   r   ru   r   r3   rQ   r   r/   r3   rQ   )r   r   boolr   r   r/   r    r"   r   rU   ra   r   rE   rc   r   rg   r   SUCCESSFAILURE)	rh   r   defr1defr2defr3dlru   r   r   s	            r>   testDeferredListzDeferredTests.testDeferredList   sp   %eCL&9 9:;
'zz'zz'zz5%/00<B 	& 	& 	& 	& 	&	 	 	 	 	Bs 	l3''(((sq	1s41#>#>#DEEFq		 mS!EM3#7%-9MN	
 	
 	
 	
 	
r@   c                   g }|fd
d}t          g           }|                    |           |                     |g g           g |d d <   t          g d	          }|                    |           |                     |g            d S )Nr   _DeferredListResultListT[None]ru   $List[_DeferredListResultListT[None]]r3   rQ   c                0    |                     |            d S r;   rb   r   s     r>   r   z/DeferredTests.testEmptyDeferredList.<locals>.cbG  s     MM*%%%%%r@   TfireOnOneCallback)r   r   ru   r   r3   rQ   )r"   r   rc   )rh   ru   r   dl1dl2s        r>   testEmptyDeferredListz#DeferredTests.testEmptyDeferredListD  s    79 <B	& 	& 	& 	& 	& 9ER8H8H"&&&qqq	:F$;
 ;
 ;
 	$$$$$r@   c                *   t                      }t                      }t                      }t          |||gd          }g }|                    |j                   dd}|                    |           |                    d           |                     |g            |                    t          d	                     |                     t          |          d
           |d         }| 	                    t          |j        t          j                  d|j                   |j        }|                     |j        j        t                     |                     |j        j        j        d           |                     |j        d
           d S )NTfireOnOneErrbackr   r/   r3   rQ   c                    d S r;   rJ   r   s    r>   r   z;DeferredTests.testDeferredListFireOnOneError.<locals>.catchb  r   r@   r   	from def2rL   r   zFissubclass(aFailure.type, defer.FirstError) failed: failure's type is )r   r   )r    r"   ra   rb   rU   rc   r   rE   rd   re   
issubclasstyper   
FirstErrorrg   
subFailurer6   index)	rh   r   r   r   r   ru   r   aFailure
firstErrors	            r>   testDeferredListFireOnOneErrorz,DeferredTests.testDeferredListFireOnOneErrorX  s   'zz'zz'zz5%/$GGG "
fm$$$	 	 	 	 	 	s$$$ 	l;//000Va((( !9 	x}e&6777&.mm6	
 	
 	
 ^
 	.3\BBB.49>JJJ)1-----r@   c                   t                      }t          |g          }g }|                    |j                   g }|                    |j                   |                    t          d                     |                     d|d         j        j	        d                    |                     dt          |                     |d         d         }|J |                     d|d         j        j	        d                    d S )NBangr   rL   )r    r"   ra   rb   r   r   rE   rc   rg   r6   rd   rh   d1r   	errorTrapresultListsfirstResults         r>   !testDeferredListDontConsumeErrorsz/DeferredTests.testDeferredListDontConsumeErrors  s    %ZZ2$#%	
i&'''<>
{)***


<''(((1!3!8!;<<<C,,---!!nQ'&&&Q!5!:1!=>>>>>r@   c                   t                      }t          |gd          }g }|                    |j                   g }|                    |j                   |                    t          d                     |                     g |           |                     dt          |                     |d         d         }|J |                     d|d         j	        j
        d                    d S )NTconsumeErrorsr   rL   r   )r    r"   ra   rb   r   r   rE   rc   rd   rg   r6   r   s         r>   testDeferredListConsumeErrorsz+DeferredTests.testDeferredListConsumeErrors  s    %ZZ2$d333#%	
i&'''<>
{)***


<''(((Y'''C,,---!!nQ'&&&Q!5!:1!=>>>>>r@   c                R   t                      }t                      }|                    t          d                     t          ||gd          }g }|                    |j                   |                     dt          |                     |                    d            d S )Nr   Tr   rL   c                    d S r;   rJ   es    r>   <lambda>zWDeferredTests.testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds.<locals>.<lambda>       r@   )r    r   rE   r"   ra   rb   rc   rd   rh   r   d2r   ru   s        r>   7testDeferredListFireOnOneErrorWithAlreadyFiredDeferredszEDeferredTests.testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds  s    %ZZ%ZZ


<''((( 2r(T::: "
fm$$$CKK(((
nn%%%%%r@   c                x   t                      }t                      }|                    t          d                     |                    d           t	          ||g          }g }|                    |j                   |                     dt          |                     |	                    d            d S )Nr   r   rL   c                    d S r;   rJ   r   s    r>   r   zIDeferredTests.testDeferredListWithAlreadyFiredDeferreds.<locals>.<lambda>  r   r@   )
r    r   rE   rU   r"   r   rb   rc   rd   ra   r   s        r>   )testDeferredListWithAlreadyFiredDeferredsz7DeferredTests.testDeferredListWithAlreadyFiredDeferreds  s    $JJ$JJ


<''(((
A 2r(##/1
v}%%%CKK(((
nn%%%%%r@   c                   t                      }t                      }t          ||g          }|                                 |                     |t          j                   |                     |t          j                   dS )zk
        When cancelling an unfired L{DeferredList}, cancel every
        L{Deferred} in the list.
        Nr    r"   cancelfailureResultOfr   r   rh   deferredOnedeferredTwodeferredLists       r>   test_cancelDeferredListz%DeferredTests.test_cancelDeferredList  sr    
 '/jj&.jj#[+$>??[%*>???[%*>?????r@   c                B   t          t                    }t                      }t          ||g          }|                                 |                     |t
          j                   |                     |          }|                     |d         d                    | 	                    |d         d         d           | 
                    |d         d                    |                     |d         d                             t
          j                             dS )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   rL   rS   N)r    rW   r"   r  r  r   r   successResultOfre   rc   assertFalserf   rh   r  r  r  ru   s        r>   test_cancelDeferredListCallbackz-DeferredTests.test_cancelDeferredListCallback  s     &..C%D%D%-ZZ#[+$>??[%*>???%%l33q	!%%%1'89991&&&q	!**5+?@@AAAAAr@   c                   t                      }t                      }t          ||gd          }|                                 |                     |t          j                   |                     |t          j                   dS )z
        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_cancelDeferredListWithFireOnOneCallbackz:DeferredTests.test_cancelDeferredListWithFireOnOneCallback  sx    
 '/jj&.jj#[+$>RVWWW[%*>???[%*>?????r@   c                .   t          t                    }t                      }t          ||gd          }|                                 |                     |t
          j                   |                     |          }|                     |d           dS )a  
        When cancelling an unfired L{DeferredList} with the flag
        C{fireOnOneCallback} set, if one of the L{Deferred} callbacks
        in its canceller, the L{DeferredList} will callback with the
        result and the index of the L{Deferred} in a C{tuple}.
        Tr   )rS   r   N)	r    rW   r"   r  r  r   r   r
  rc   r  s        r>   ?test_cancelDeferredListWithFireOnOneCallbackAndDeferredCallbackzMDeferredTests.test_cancelDeferredListWithFireOnOneCallbackAndDeferredCallback  s     &..C%D%D%-ZZ#[+$>RVWWW[%*>???%%l33!788888r@   c                   t                      }t                      }t          ||gd          }|                                 |                     |t          j                   |                     |t          j                   |                     |t          j                  }|j        }|                     |j	        
                    t          j                             dS )z
        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   rg   re   r   rf   )rh   r  r  r  deferredListFailurer   s         r>   +test_cancelDeferredListWithFireOnOneErrbackz9DeferredTests.test_cancelDeferredListWithFireOnOneErrback  s    
 '/jj&.jj#[+$>QUVVV[%*>???[%*>???"22<AQRR(.

-33E4HIIJJJJJr@   c                   t          t                    }t          t                    }t          ||gd          }|                                 |                     |          }|                     |d         d                    |                     |d         d         d           |                     |d         d                    |                     |d         d         d           dS )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   rL   rS   N)r    rW   r"   r  r
  re   rc   r  s        r>   ?test_cancelDeferredListWithFireOnOneErrbackAllDeferredsCallbackzMDeferredTests.test_cancelDeferredListWithFireOnOneErrbackAllDeferredsCallback
  s     &..C%D%D%-.C%D%D#[+$>QUVVV%%l33q	!%%%1'8999q	!%%%1'899999r@   c                |   t                      }t                      }||g}t          |          }t                      }|                    |           |                                 |                     |t
          j                   |                     |t
          j                   |                     |           dS )zg
        Cancelling a L{DeferredList} will cancel the original
        L{Deferred}s passed in.
        N)r    r"   rb   r  r  r   r   assertNoResult)rh   r  r  argumentListr  deferredThrees         r>   ,test_cancelDeferredListWithOriginalDeferredsz:DeferredTests.test_cancelDeferredListWithOriginalDeferreds  s    
 '/jj&.jj#[1#L11(0

M***[%*>???[%*>???M*****r@   c                L   dd}t          |          }t                      }t          ||g          }|                                 |                     |t          j                   |                     t                    }|                     t          |          d           dS )	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.
        rO   Deferred[object]r3   rQ   c                     t          d          )z
            A L{Deferred} canceller that raises an exception.

            @param deferred: The cancelled L{Deferred}.
            test)rp   rV   s    r>   cancellerRaisesExceptionzTDeferredTests.test_cancelDeferredListWithException.<locals>.cancellerRaisesException2  s     v&&&r@   rL   N)rO   r  r3   rQ   )
r    r"   r  r  r   r   rr   rp   rc   rd   )rh   r   r  r  r  rx   s         r>   $test_cancelDeferredListWithExceptionz2DeferredTests.test_cancelDeferredListWithException+  s    	' 	' 	' 	' '//G&H&H&.jj#[+$>??[%*>???''55Va(((((r@   c                    t                      }t                      }t          ||gd          }|                    d           |                                 |                     |           dS )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"   rU   r  r  r  s       r>   )test_cancelFiredOnOneCallbackDeferredListz7DeferredTests.test_cancelFiredOnOneCallbackDeferredListB  sn     '/jj&.jj#[+$>RVWWWT"""K(((((r@   c                p   t                      }t                      }t          ||gd          }|                    t          d                     |                                 |                     |           |                     |t                     |                     |t          j                   dS )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   r  N)	r    r"   r   rE   r  r  r  r   r   r  s       r>   (test_cancelFiredOnOneErrbackDeferredListz6DeferredTests.test_cancelFiredOnOneErrbackDeferredListO  s     '/jj&.jj#[+$>QUVVVL00111K((([,777\5+;<<<<<r@   c                    g }t          j        d          }|                    |j                   |                     |dg           d S )Nsuccess)r   succeedr   rb   rc   rh   lds      r>   testImmediateSuccessz"DeferredTests.testImmediateSuccess^  sH     =33	ahYK(((((r@   c                    g }t          j        t          d                    }|                    |j                   |                     t          |d         j                  d           d S Nfailr   )r   r/  rE   ra   rb   rc   r   rg   r)  s      r>   testImmediateFailurez"DeferredTests.testImmediateFailured  sZ    !J|F';';<<	QXQqTZ&11111r@   c                X   g }t          j        t          d                    }|                                 |                    |j                   |                     |g            |                                 |                     t          |d         j	                  d           d S r.  )
r   r/  rE   pausera   rb   rc   unpauser   rg   r)  s      r>   testPausedFailurezDeferredTests.testPausedFailurej  s    J|F++,,					QXB			QqTZ&11111r@   c                    g }t                      }|                    d                               |j                   |                    d           |                     |d         j        t                     g }t          t                               }|                    d                               |j                   |                    d           |                     |d         j        t                     d S )Nc                    ddz  S NrL   r   rJ   _s    r>   r   z2DeferredTests.testCallbackErrors.<locals>.<lambda>v  s
    Q r@   rL   r   c                8    t          t                                S r;   )r/   ZeroDivisionErrorr8  s    r>   r   z2DeferredTests.testCallbackErrors.<locals>.<lambda>{  s    ):)<)<!=!= r@   )	r    r   ra   rb   rU   assertIsInstancerg   r;  int)rh   r*  r+  r   s       r>   testCallbackErrorsz DeferredTests.testCallbackErrorss  s    #::	&&''2218<<<	

1adj*;<<<c]__
==>>II!(SSS
Aadj*;<<<<<r@   c                    t                      }|                                 |                    | j                   |                                 d S r;   )r    r2  r   r   r3  rh   r+  s     r>   testUnpauseBeforeCallbackz'DeferredTests.testUnpauseBeforeCallback  s=    $JJ					dn%%%			r@   c                   t                      }t                                                       |                    fd           |                    | j                   |                    d           |                     | j        d                               d           |                     | j        d                                            |                     | j        d           | j        
J d            | 	                    | j        d	         d	         dd

                    | j                             dS )z
        Callbacks passed to L{Deferred.addCallback} can return Deferreds and
        the next callback will not be run until that Deferred fires.
        c                    S r;   rJ   )rr   s    r>   r   z<DeferredTests.test_callbackReturnsDeferred.<locals>.<lambda>  s     r@   rL   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    r2  r   r   rU   r   r   r3  assertIsNotNoneassertEqualsformatrh   r   r   s     @r>   test_callbackReturnsDeferredz*DeferredTests.test_callbackReturnsDeferred  s8   
 %JJ$JJ




||||$$$
t~&&&
A$.0RSSS
A "J	
 	
 	
 	

T13PQQQ#//1X/// #A&=DD$ 	
 	
 	
 	
 	
r@   c                
   t                      }dt                      dfd}|                    d           |                     |                     |                    |                    d           t          t                               }d	|                    d           |                     |                     |                    |                              |          t                    j	                   d
S )zF
        Callbacks passed to addCallback may return Failures.
        Fru   r=  r3   Union[int, Failure]c                0    rt                    S | dz   S NrL   r.   )ru   rte
shouldFails    r>   	maybeFailzADeferredTests.test_callbackMaybeReturnsFailure.<locals>.maybeFail  s!     "s||#z!r@         TN)ru   r=  r3   rK  )
r    rp   rU   rc   r
  r   r=  rs   r  rg   )rh   r+  rP  rN  rO  s      @@r>    test_callbackMaybeReturnsFailurez.DeferredTests.test_callbackMaybeReturnsFailure  s     $::
nn	" 	" 	" 	" 	" 	" 	" 	


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

1  i((44Y??  	
 	
 	
 	
 	
r@   c                   t                      }t                      g d
fd}dfd}|                    |                              |           |                    t	                                 |                     |           |                     g                                d           |                     dg           d	S )zo
        Errbacks passed to L{Deferred.addErrback} can return Deferreds just as
        callbacks can.
        ru   r/   r3   rP   c                    S r;   rJ   )ru   r   s    r>   asyncErrbackz?DeferredTests.test_errbackReturnsDeferred.<locals>.asyncErrback  s    Ir@   Union[str, int]rQ   c                2                         |            d S r;   r   )ru   resultValuess    r>   syncCallbackz?DeferredTests.test_errbackReturnsDeferred.<locals>.syncCallback  s    '''''r@   N)ru   r/   r3   rP   )ru   rW  r3   rQ   )r    ra   r   r   
ValueErrorr  rc   rU   )rh   r+  rV  rZ  r   rY  s       @@r>   test_errbackReturnsDeferredz)DeferredTests.test_errbackReturnsDeferred  s    
 $::$JJ	 	 	 	 	 		( 	( 	( 	( 	( 	( 	
\""..|<<<			*,,Ar***
Hz22222r@   c                   t                      }t                                          |                                            t                      }|                    fd           |                    d           g }|                    |j                   |                     |g                                             |                     ||g           dS )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                    S r;   rJ   )ignoredpauseds    r>   r   zDDeferredTests.test_chainedPausedDeferredWithResult.<locals>.<lambda>  s    F r@   N)r7   r    rU   r2  r   rb   rc   r3  )rh   expectedchainedru   r`  s       @r>   $test_chainedPausedDeferredWithResultz2DeferredTests.test_chainedPausedDeferredWithResult  s     88#+::!!!"***2222333!FM***$$$(,,,,,r@   c                R   t                      }t                      |                    fd           |                    d           |                                                     d           g }                    |j                   |                     |dg           dS )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                    S r;   rJ   r_  seconds    r>   r   z:DeferredTests.test_pausedDeferredChained.<locals>.<lambda>      & r@   N)r    r   rU   r2  rb   rc   )rh   firstru   rg  s      @r>   test_pausedDeferredChainedz(DeferredTests.test_pausedDeferredChained  s     !)

!)0000111t6=)))$(((((r@   c                d   g }t          j        t          j        d          t          j        d          g                              |j                   |                     |ddgg           g }t          j        d          t          j        t                                g}t          j        |                              |j                   |                     t          |          d           | 
                    |d         t                     |d                             d            d S )NrL   r   r   c                    dS rM  rJ   r   s    r>   r   z2DeferredTests.test_gatherResults.<locals>.<lambda>  s    1 r@   )r   gatherResultsr(  r   rb   rc   r/  r[  ra   rd   r<  r/   )rh   resultsrx   r   s       r>   test_gatherResultsz DeferredTests.test_gatherResults  s   #%U]1--u}Q/?/?@AAMMN	
 	
 	
 	Aq6(+++ "mA
:<< 8 89B**6=999Va(((fQi111
1%%%%%r@   c                .   t          j        d          }t          j        t          d                    }t          j        ||gd          }g }|                    |j                   g }|                    |j                   |                     t          |          t          |          fd           | 	                    |d         j
        t           j                   |d         j
        j        }| 	                    |j
        t                     dS )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}.
        rL   zoh noesTr   r   rL   r   N)r   r(  r/  rp   rm  ra   rb   rc   rd   r<  rg   r   r   )rh   dgooddbadr+  unconsumedErrorsgatheredErrorsr   s          r>   #test_gatherResultsWithConsumeErrorsz1DeferredTests.test_gatherResultsWithConsumeErrors  s     a  z,y1122TBBB*,(/000(*	^*+++#.//^1D1DEvNNNnQ/5u7GHHH#A&,7
j.=====r@   c                   t                      }t                      }t          j        ||g          }|                                 |                     |t          j                   |                     |t          j                   |                     |t          j                  }|j        }|                     |j	        
                    t          j                             dS )z~
        When cancelling the L{defer.gatherResults} call, all the
        L{Deferred}s in the list will be cancelled.
        N)r    r   rm  r  r  r   r   rg   re   r   rf   )rh   r  r  ru   gatherResultsFailurer   s         r>   test_cancelGatherResultsz&DeferredTests.test_cancelGatherResults  s    
 '/jj&.jj$k;%?@@[%*>???[%*>???#33FE<LMM)/

-33E4HIIJJJJJr@   c                D   t          t                    }t          t                    }t          j        ||g          }|                                 |                     |          }|                     |d         d           |                     |d         d           dS )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   rS   rL   N)r    rW   r   rm  r  r
  rc   )rh   r  r  ru   callbackResults        r>   0test_cancelGatherResultsWithAllDeferredsCallbackz>DeferredTests.test_cancelGatherResultsWithAllDeferredsCallback+  s     &..C%D%D%-.C%D%D$k;%?@@--f55*,=>>>*,=>>>>>r@   c                D   t                      g }g }t          j        fd          }|                    |j        |j                   |                     |g            |                     t          |          d           |                     |d                    dS )z
        L{defer.maybeDeferred} should retrieve the result of a synchronous
        function and pass it to its resulting L{Deferred}.
        c                      S r;   rJ   ru   s   r>   r   z6DeferredTests.test_maybeDeferredSync.<locals>.<lambda>B  s     r@   rL   r   N)r7   r   maybeDeferredr   rb   rc   rd   assertIdentical)rh   rn  rx   r+  ru   s       @r>   test_maybeDeferredSyncz$DeferredTests.test_maybeDeferredSync:  s    
  " "//	w~v}555$$$Wq)))WQZ00000r@   c                   dd}g }g }t          j        |d          }t          |t          t                              |                    |j        |j                   |                     |g            |                     |dg           dS )	zV
        L{defer.maybeDeferred} should pass arguments to the called function.
        xr=  r3   c                    | dz   S )N   rJ   r  s    r>   plusFivez>DeferredTests.test_maybeDeferredSyncWithArgs.<locals>.plusFiveM  s    q5Lr@   
      N)r  r=  r3   r=  )r   r  r   r    r=  r   rb   rc   )rh   r  rn  rx   r+  s        r>   test_maybeDeferredSyncWithArgsz,DeferredTests.test_maybeDeferredSyncWithArgsH  s    
	 	 	 	   ""--Ax}%%%	w~v}555$$$2$'''''r@   c                   t          d          dfd}g }g }t          j        |                              |j        |j                   |                     |g            |                     t          |          d           |                     t          |d         j                  t                               dS )	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 unacceptabler3   r   c                      r;   rJ   ra  s   r>   raisesExceptionzFDeferredTests.test_maybeDeferredSyncException.<locals>.raisesException_  s    Nr@   rL   r   N)r3   r   )	r[  r   r  r   rb   rc   rd   r   rg   )rh   r  rn  rx   ra  s       @r>   test_maybeDeferredSyncExceptionz-DeferredTests.test_maybeDeferredSyncExceptionX  s    
 :;;	 	 	 	 	 	   "O,,99'.&-XXX"%%%Va(((VAY_--s8}}=====r@   c                r   	 ddz    n# t           $ r t                      Y nw xY wg }g }t          j        fd          }|                    |j        |j                   |                     |g            |                     t          |          d           |                     |d                    dS )zw
        L{defer.maybeDeferred} should handle a L{Failure} returned by a
        function and errback with it.
        10r  c                      S r;   rJ   r  s   r>   r   z=DeferredTests.test_maybeDeferredSyncFailure.<locals>.<lambda>u  s     r@   rL   r   N)		TypeErrorr/   r   r  r   rb   rc   rd   r  )rh   rn  rx   r+  ra  s       @r>   test_maybeDeferredSyncFailurez+DeferredTests.test_maybeDeferredSyncFailurei  s    
	!1HHH 	! 	! 	!yyHHH	!   " 0 0 0 011	w~v}555"%%%Va(((VAY11111s   	 $$c                    t                      t          j        fd          }                    d           g }|                    |j                   |                     |dg           dS )z}
        L{defer.maybeDeferred} should let L{Deferred} instance pass by
        so that original result is the same.
        c                      S r;   rJ   r   s   r>   r   z7DeferredTests.test_maybeDeferredAsync.<locals>.<lambda>  s     r@   SuccessN)r    r   r  rU   r   rb   rc   )rh   r   ru   r   s      @r>   test_maybeDeferredAsyncz%DeferredTests.test_maybeDeferredAsync{  sm    
 %JJ ,,
I
v}%%%)-----r@   c                    t                      t          j        fd          }                    t	          t                                           |                     |t
                     dS )z
        L{defer.maybeDeferred} should let L{Deferred} instance pass by
        so that L{Failure} returned by the original instance is the
        same.
        c                      S r;   rJ   r  s   r>   r   z<DeferredTests.test_maybeDeferredAsyncError.<locals>.<lambda>  s     r@   N)r    r   r  r   r/   rp   rk   )rh   r   r   s     @r>   test_maybeDeferredAsyncErrorz*DeferredTests.test_maybeDeferredAsyncError  s^     &ZZ"0<<


7<>>**+++##B55555r@   c                    dd}t          j        |          }t          |t          t                              |                     |                     |          d           dS )
        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.
        r3   r=  c                 
   K   dS NrR  rJ   rJ   r@   r>   r1   z;DeferredTests.test_maybeDeferredCoroutineSuccess.<locals>.f        1r@   rR  Nr3   r=  )r   r  r   r    r=  rc   r
  )rh   r1   coroutineDeferreds      r>   "test_maybeDeferredCoroutineSuccessz0DeferredTests.test_maybeDeferredCoroutineSuccess  si    	 	 	 	 "/22%x}555--.?@@!DDDDDr@   c                   d
ddfd}dd}t          j        |          }t          |t          t                              |                    |          }|                     |                     |          d           d	S )r  r3   r=  c                 
   K   dS r  rJ   rJ   r@   r>   r1   zCDeferredTests.test_maybeDeferredCoroutineSuccessIndirect.<locals>.f  r  r@   "Coroutine[Deferred[int], Any, Any]c                                   S r;   rJ   r1   s   r>   gzCDeferredTests.test_maybeDeferredCoroutineSuccessIndirect.<locals>.g      133Jr@   ru   c                    | dz   S rM  rJ   r  s    r>   typedCallbackzODeferredTests.test_maybeDeferredCoroutineSuccessIndirect.<locals>.typedCallback  s    A:r@      Nr  )r3   r  )ru   r=  r3   r=  )r   r  r   r    r   r   rc   r
  )rh   r  r  r  modifiedDeferredr1   s        @r>   *test_maybeDeferredCoroutineSuccessIndirectz8DeferredTests.test_maybeDeferredCoroutineSuccessIndirect  s    	 	 	 	
	 	 	 	 	 	
	 	 	 	 "/22%x}555,88GG--.>??CCCCCr@   c                     G d dt                     d	fdd
fd}t          |                     t          j        |                    j        t                               dS )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                      e Zd ZdS )GDeferredTests.test_maybeDeferredCoroutineFailure.<locals>.SomeExceptionNrF   rJ   r@   r>   SomeExceptionr    s        Dr@   r  r3   rQ   c                    K                 r;   rJ   )r  s   r>   r1   z;DeferredTests.test_maybeDeferredCoroutineFailure.<locals>.f  s      -//!r@   Coroutine[None, None, None]c                                   S r;   rJ   r  s   r>   r  z;DeferredTests.test_maybeDeferredCoroutineFailure.<locals>.g  r  r@   Nr   )r3   r  )	Exceptionr   r  r   r  r   r   )rh   r  r  r1   s     @@r>   "test_maybeDeferredCoroutineFailurez0DeferredTests.test_maybeDeferredCoroutineFailure  s    	 	 	 	 	I 	 	 		" 	" 	" 	" 	" 	"
	 	 	 	 	 	 	  !4Q!7!788=]##	
 	
 	
 	
 	
r@   c                   g g t                      d
fd}t          j        d          }|                    |                               d           |                    j                                       j                   |                    j                   |                     g            |                     g d           d	S )a  
        When a L{Deferred} encounters a result which is another L{Deferred}
        which is waiting on a third L{Deferred}, the middle L{Deferred}'s
        callbacks are executed after the third L{Deferred} fires and before the
        first receives a result.
        ru   r   r3   rP   c                                         d| f           t          j        d          }d	fd}d
fd}|                    |                              |           |                    j                    |S )Nstart-of-cbinnerru   r   r3   rP   c                2                         d           S NfirstCallbackr  r   ru   r  rn  s    r>   r  zMDeferredTests.test_innerCallbacksPreserved.<locals>.cb.<locals>.firstCallback  s    9:::r@   c                <                         d| f           | dz  S NsecondCallbackr   r   ru   rn  s    r>   r  zNDeferredTests.test_innerCallbacksPreserved.<locals>.cb.<locals>.secondCallback  %     0&9:::z!r@   ru   r   r3   rP   ru   r   r3   r   rb   r   r(  r   ra   ru   r+  r  r  rj   r  rn  s       r>   r   z6DeferredTests.test_innerCallbacksPreserved.<locals>.cb  s    NNM62333g&&A      " " " " " " MM-((44^DDDLL)))Hr@   outeroranger  r  r  )r  r  orangeorangeNr  )r    r   r(  r   rU   rb   ra   rc   rh   r   r  rj   r  rn  s      @@@r>   test_innerCallbacksPreservedz*DeferredTests.test_innerCallbacksPreserved  s    68"$'zz	 	 	 	 	 	 	 	  g&&"x   '.)))))))))X&&&  	
 	
 	
 	
 	
r@   c                |   g g t                      dfd}t          j        d                              |                              j                   |                     ddg                               d	           |                     g            |                     ddd
d	gfg           dS )a   
        The continue callback of a L{Deferred} waiting for another L{Deferred}
        is not necessarily the first one. This is somewhat a whitebox test
        checking that we search for that callback among the whole list of
        callbacks.
        ru   r   r3   Deferred[None]c                                        d| f           t                      }d	fd}d
fd}|                    |                              |                              j                   }|                    d            |S )Nr   ru   rQ   r3   Deferred[List[str]]c                \                         d| f           t          j        g          S Nr  rb   r   rm  ru   arn  s    r>   r  zNDeferredTests.test_continueCallbackNotFirst.<locals>.cb.<locals>.firstCallback  -    8999*A3///r@   	List[str]c                6                         d| f           d S Nr  r   r  s    r>   r  zODeferredTests.test_continueCallbackNotFirst.<locals>.cb.<locals>.secondCallback  !     0&9:::::r@   ru   rQ   r3   r  ru   r  r3   rQ   rb   r    r   ra   rU   ru   r+  r  r  returnerr  rj   rn  s        r>   r   z7DeferredTests.test_continueCallbackNotFirst.<locals>.cb      NND&>*** (

A0 0 0 0 0 0 0; ; ; ; ; ; m,,^,,HO,, 
 JJtOr@   r  r   r  r  Nwithersr  Nru   r   r3   r  )r    r   r(  r   ra   rb   rc   rU   )rh   r   r  rj   rn  s     @@@r>   test_continueCallbackNotFirstz+DeferredTests.test_continueCallbackNotFirst  s     BD"$#::	 	 	 	 	 	 	 	& 	g**2..99(/JJJ/+BCWMMM	

9X&&&58H9+7VW	
 	
 	
 	
 	
r@   c                   g g t                      dfd}t                      }|                    |           |                    fd           |                    j                   |                    d           |                     dd	g                               d
           |                     g            |                     dd	dd
gfdg           dS )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.
        ru   r   r3   r  c                                        d| f           t                      }d	fd}d
fd}|                    |                              |                              j                   }|                    d            |S )Nr   ru   rQ   r3   r  c                \                         d| f           t          j        g          S r  r  r  s    r>   r  zLDeferredTests.test_callbackOrderPreserved.<locals>.cb.<locals>.firstCallback1  r  r@   r  c                6                         d| f           d S r  r   r  s    r>   r  zMDeferredTests.test_callbackOrderPreserved.<locals>.cb.<locals>.secondCallback5  r  r@   r  r  r  r  s        r>   r   z5DeferredTests.test_callbackOrderPreserved.<locals>.cb-  r  r@   c                .                         d          S )NfinalNr   )r  rn  s    r>   r   z;DeferredTests.test_callbackOrderPreserved.<locals>.<lambda>B  s    GNN?$C$C r@   r  r  r  r  r  r  Nr  )r    r   ra   rb   rU   rc   )rh   r   r  r  rj   rn  s      @@@r>   test_callbackOrderPreservedz)DeferredTests.test_callbackOrderPreserved#  s*    BD"$#::	 	 	 	 	 	 	 	&  (zz"CCCCDDD)))w/+BCWMMM	

9X&&&'!I;/		
 	
 	
 	
 	
r@   c                   t                      g d	fdd	fd}d	fd}                    |                               |                               d           |                     g d           dS )
z
        A callback added to a L{Deferred} by a callback on that L{Deferred}
        should be added to the end of the callback chain.
        ru   rQ   r3   c                2                         d           d S N   r   ru   calleds    r>   	callback3z;DeferredTests.test_reentrantRunCallbacks.<locals>.callback3Z      MM!r@   c                2                         d           d S Nr   r   r  s    r>   	callback2z;DeferredTests.test_reentrantRunCallbacks.<locals>.callback2]  r  r@   c                \                         d                                          d S rM  rb   r   )ru   r  r  rO   s    r>   	callback1z;DeferredTests.test_reentrantRunCallbacks.<locals>.callback1`  s/    MM!  +++++r@   N)rL   r   r  ru   rQ   r3   rQ   r    r   rU   rc   )rh   r  r  r  r  rO   s      @@@r>   test_reentrantRunCallbacksz(DeferredTests.test_reentrantRunCallbacksR  s    
 $,::	 	 	 	 	 		 	 	 	 	 		, 	, 	, 	, 	, 	, 	, 	, 	Y'''Y'''$+++++r@   c                     t                      g d	fdd	 fd}                    |                               d                                ddg           dS )
z
        A callback added to a L{Deferred} by a callback on that L{Deferred}
        should not be executed until the running callback returns.
        ru   rQ   r3   c                2                         d           d S r  r   r  s    r>   r  z;DeferredTests.test_nonReentrantCallbacks.<locals>.callback2q  r  r@   c                                         d                                                              dg           d S rM  )rb   r   rc   )ru   r  r  rO   rh   s    r>   r  z;DeferredTests.test_nonReentrantCallbacks.<locals>.callback1t  sG    MM!  +++VaS)))))r@   NrL   r   r  r  )rh   r  r  r  rO   s   ` @@@r>   test_nonReentrantCallbacksz(DeferredTests.test_nonReentrantCallbacksi  s    
 $,::	 	 	 	 	 		* 	* 	* 	* 	* 	* 	* 	* 	*
 	Y'''$!Q(((((r@   c                   dt                      d	fdd	fd}                    |                               d           |                     t                    }|                     |j        f           dS )
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 exceptionru   r7   r3   rQ   c                "    t                    r;   r  )ru   exceptionMessages    r>   r  zFDeferredTests.test_reentrantRunCallbacksWithFailure.<locals>.callback2  s    ,---r@   c                2                                    d S r;   )r   )ru   r  rO   s    r>   r  zFDeferredTests.test_reentrantRunCallbacksWithFailure.<locals>.callback1  s      +++++r@   Nru   r7   r3   rQ   )r    r   rU   rk   r  rc   r6   )rh   r  r]   r  rO   r  s      @@@r>   %test_reentrantRunCallbacksWithFailurez3DeferredTests.test_reentrantRunCallbacksWithFailure}  s     7#+::	. 	. 	. 	. 	. 	.	, 	, 	, 	, 	, 	, 	, 	Y'''$//)DD	*:)<=====r@   c                   t                      }t          j        |          t                      }|                    fd           |                    d           g }                    |j                   |                     |d                    |                    |j                   |                     |d         |           dS )a  
        If a first L{Deferred} with a result is returned from a callback on a
        second L{Deferred}, the result of the second L{Deferred} becomes the
        result of the first L{Deferred} and the result of the first L{Deferred}
        becomes L{None}.
        c                    S r;   rJ   ignri  s    r>   r   z=DeferredTests.test_synchronousImplicitChain.<locals>.<lambda>      u r@   Nr   rL   )	r7   r   r(  r    r   rU   rb   r   rs   )rh   ru   rg  rn  ri  s       @r>   test_synchronousImplicitChainz+DeferredTests.test_synchronousImplicitChain  s     f%%!),,,,---*,'.)))'!*%%%7>***gaj&)))))r@   c                   t                      t                      }|                    fd           |                    d           g }                    |j                   g }|                    |j                   |                     |g            |                     |g            t                      }                    |           |                     |dg           |                     ||g           dS )a9  
        If a first L{Deferred} without a result is returned from a callback on
        a second L{Deferred}, the result of the second L{Deferred} becomes the
        result of the first L{Deferred} as soon as the first L{Deferred} has
        one and the result of the first L{Deferred} becomes L{None}.
        c                    S r;   rJ   r  s    r>   r   z>DeferredTests.test_asynchronousImplicitChain.<locals>.<lambda>  r  r@   N)r    r   rU   rb   rc   r7   )rh   rg  r   secondResultru   ri  s        @r>   test_asynchronousImplicitChainz,DeferredTests.test_asynchronousImplicitChain  s     #+**#+::,,,,---$&+,---%'<.///b)))r***vtf---x00000r@   c                h   t          j        t          d                    }|fd
d}t                      }|                    |           |                    d           g }|                    |j                   |                     |d	                    |                     |t                     dS )a  
        If a first L{Deferred} with a L{Failure} result is returned from a
        callback on a second L{Deferred}, the first L{Deferred}'s result is
        converted to L{None} and no unhandled error is logged when it is
        garbage collected.
        First Deferred's Failurer9  rQ   ri  r  r3   c                    |S r;   rJ   )r9  ri  s     r>   r   z<DeferredTests.test_synchronousImplicitErrorChain.<locals>.cb  s    Lr@   Nr   )r9  rQ   ri  r  r3   r  )	r   r/  rp   r    r   rU   rb   r   rk   )rh   ri  r   rg  r   s        r>   "test_synchronousImplicitErrorChainz0DeferredTests.test_synchronousImplicitErrorChain  s     
<(BCCDD05 	 	 	 	 	 "*2"$+,---+a.)))##FL99999r@   c                   t                      t                      }|                    fd           |                    d           g }|                    |j                   g }                    |j                   g }|                    |j                   |                     |g            |                     |g                                t          d                     |                     |d         	                    t                               |                     |dg           |                     t          |          d           dS )aP  
        Let C{a} and C{b} be two L{Deferred}s.

        If C{a} has no result and is returned from a callback on C{b} then when
        C{a} fails, C{b}'s result becomes the L{Failure} that was C{a}'s result,
        the result of C{a} becomes L{None} so that no unhandled error is logged
        when it is garbage collected.
        c                    S r;   rJ   r  s    r>   r   zCDeferredTests.test_asynchronousImplicitErrorChain.<locals>.<lambda>  r  r@   Nr  r   rL   )r    r   rU   ra   rb   rc   r   rp   re   rf   rd   )rh   rg  secondErrorr   r  ri  s        @r>   #test_asynchronousImplicitErrorChainz1DeferredTests.test_asynchronousImplicitErrorChain  sB    !)

!),,,,---%'+,---"$+,---#%<.///b)))r***l#=>>???A,,\::;;;tf---\**A.....r@   c                   t                      t                                          fd           t                      }|                    fd           g }|                    |j                   t                      }                    d           |                    d           |                     |g                                |           |                     ||g           dS )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                    S r;   rJ   r  s    r>   r   zGDeferredTests.test_doubleAsynchronousImplicitChaining.<locals>.<lambda>  r  r@   c                    S r;   rJ   )r  rg  s    r>   r   zGDeferredTests.test_doubleAsynchronousImplicitChaining.<locals>.<lambda>  s    f r@   N)r    r   rb   r7   rU   rc   )rh   thirdthirdResultru   ri  rg  s       @@r>   'test_doubleAsynchronousImplicitChainingz5DeferredTests.test_doubleAsynchronousImplicitChaining  s     #+**#+::,,,,---"***,,,,---$&+,---t 	b))) 	vvh/////r@   c                   g g t                      dfd}t          j        d                              |                              j                  }|                     ddg                               d	                               j                   |                    j                   |                     g d
           |                     g d           dS )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.
        ru   r   r3   rP   c                                         d| f           t          j        d          }d	fd}d
fd}|                    |                              |           |                    j                    |S )Nr  r  ru   r   r3   rP   c                2                         d           S r  r   r  s    r>   r  zXDeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb.<locals>.firstCallback  s    9::: r@   c                <                         d| f           | dz  S r  r   r  s    r>   r  zYDeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb.<locals>.secondCallback$  r  r@   r  r  r  r  s       r>   r   zADeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb  s    NNM62333g&&A      " " " " " " MM-((44^DDDLL)))Hr@   r  r  r  r  &Got errbacks but wasn't expecting any.r  Nr  r    r   r(  r   rb   rc   rU   ra   r  s      @@@r>   'test_nestedAsynchronousChainedDeferredsz5DeferredTests.test_nestedAsynchronousChainedDeferreds  s+    68"$  (zz	 	 	 	 	 	 	 	, M'""..r22>>w~NN 	
 	.0JK	
 	
 	
 	x    	))))))X'OPPP  	
 	
 	
 	
 	
r@   c                   g g t                      dfd}t          j        d          }|                    |           |                    j                   |                     ddg                               d	           |                    j                                       j                   |                     g d
           |                     dddd	gfd	d	gg           dS )aK  
        L{Deferred}s can have callbacks that themselves return L{Deferred}s.
        These L{Deferred}s can have other callbacks added before they are
        returned, which subtly changes the callback chain. When these "inner"
        L{Deferred}s fire (even asynchronously), the outer callback chain
        continues.
        ru   r   r3   Deferred[Optional[List[str]]]c                                         d| f           t          j        d          }d
fd}dfd	}|                    |                              |                              j                   S )Nr  r  ru   r   r3   r  c                d                         d| f           dd}                    |          S )Nr  ru   r   r3   r  c                    | gS r;   rJ   r  s    r>   	transformz}DeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.firstCallback.<locals>.transforme  s	    "8Or@   )ru   r   r3   r  r  )ru   r5  r  rn  s     r>   r  zjDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.firstCallback^  sD    8999$ $ $ $ ((333r@   r  c                <                         d| f           | dz  S r  r   r  s    r>   r  zkDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.secondCallbackj  r  r@   )ru   r   r3   r  )ru   r  r3   r  r  r  s       r>   r   zSDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cbZ  s    NNM62333g&&A
4 
4 
4 
4 
4 
4 
4" " " " " "
 m,,^,,HO,,r@   r  r  r  r  r-  r  N)ru   r   r3   r1  r.  r  s      @@@r>   9test_nestedAsynchronousChainedDeferredsWithExtraCallbackszGDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacksK  sB     "$  (zz	 	 	 	 	 	 	 	: g&&"'.))) 	.0JK	
 	
 	
 	y!!! 	))))))X'OPPP(*!I;/I&		
 	
 	
 	
 	
r@   c                    t                      }t                      }|                    |           |                     |j        |           dS )zT
        When we chain a L{Deferred}, that chaining is recorded explicitly.
        N)r    chainDeferredrs   
_chainedTorh   r  bs      r>   &test_chainDeferredRecordsExplicitChainz4DeferredTests.test_chainDeferredRecordsExplicitChain  sD     %JJ$JJ	alA&&&&&r@   c                    t                      }t                      }|                    |           |                    d           |                     |j                   dS )aR  
        Any recorded chaining is cleared once the chaining is resolved, since
        it no longer exists.

        In other words, if one L{Deferred} is recorded as depending on the
        result of another, and I{that} L{Deferred} has fired, then the
        dependency is resolved and we no longer benefit from recording it.
        N)r    r9  rU   r   r:  r;  s      r>   %test_explicitChainClearedWhenResolvedz3DeferredTests.test_explicitChainClearedWhenResolved  sV     %JJ$JJ		

4!,'''''r@   c                    t                      }t                      |                    fd           |                    d           |                     |j                   dS )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                    S r;   rJ   )r_  r<  s    r>   r   zFDeferredTests.test_chainDeferredRecordsImplicitChain.<locals>.<lambda>  s    a r@   N)r    r   rU   rs   r:  r;  s     @r>   &test_chainDeferredRecordsImplicitChainz4DeferredTests.test_chainDeferredRecordsImplicitChain  sa     %JJ$JJ	''''(((	

4alA&&&&&r@   c                   t                      dfd}                    |                               d           |                     |g          }|                     t          |          d           |d         }|                     |d	         t                     d
}|                     t          j	        ||d                   d
                    ||d                              dS )z
        When a Deferred is returned from a callback directly attached to that
        same Deferred, a warning is emitted.
        ru   r   r3   rP   c                    S r;   rJ   ru   r+  s    r>   circularCallbackzADeferredTests.test_circularChainWarning.<locals>.circularCallback      Hr@   foorL   r   categoryz1Callback returned the Deferred it was attached tomessagez
Expected match: {!r}
Got: {!r}Nr  )r    r   rU   flushWarningsrc   rd   DeprecationWarningre   researchrG  )rh   rF  circular_warningswarningpatternr+  s        @r>   test_circularChainWarningz'DeferredTests.test_circularChainWarning  s   
 $::	 	 	 	 	 	 	
&'''	

5 ..0@/ABB.//333#A&,.@AAAEIgwy122/66w	@RSS	
 	
 	
 	
 	
r@   c                p   |                      t          t          dt          j                   t          j        dt
                     t                      dfd}                    |                               d	           | 	                              }|
                    t
                     d
S )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.
        filtersrt   )rI  ru   r   r3   rP   c                    S r;   rJ   rE  s    r>   rF  zCDeferredTests.test_circularChainException.<locals>.circularCallback  rG  r@   rH  Nr  )r   setattrwarningsrT  filterwarningsrL  r    r   rU   r  trap)rh   rF  failurer+  s      @r>   test_circularChainExceptionz)DeferredTests.test_circularChainException  s     			
 	
 	
 	2DEEEE#::	 	 	 	 	 	 	
&'''	

5&&q))'(((((r@   c                    t                      }t          |          }|                     t          |          d|dd           dS )z
        The C{repr()} of a L{Deferred} contains the class name and a
        representation of the internal Python ID.
        <Deferred at 0xr  >N)r    idrc   repr)rh   r+  addresss      r>   	test_reprzDeferredTests.test_repr  sI    
 %JJQ%%a"@G"@"@"@"@AAAAAr@   c                    t                      }|                    d           |                     t          |          dt	          |          dd           dS )zb
        If a L{Deferred} has been fired, then its C{repr()} contains its
        result.
        r  r]  r  z current result: 'orange'>N)r    rU   rc   r`  r_  r@  s     r>   test_reprWithResultz!DeferredTests.test_reprWithResult  sV    
 $::	

8a"WBqEE"W"W"W"WXXXXXr@   c           	         t                      }t                      }|                    |           |                     t          |          dt	          |          ddt	          |          dd           dS )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    r9  rc   r`  r_  r;  s      r>   test_reprWithChainingz#DeferredTests.test_reprWithChaining  s{     %JJ$JJ	GGKbeeKKKAKKKK	
 	
 	
 	
 	
r@   c                d     d fd}                       |d           |d                     dS )	zw
        The depth of the call stack does not grow as more L{Deferred} instances
        are chained together.
        howManyr=  r3   c                |  	
 g 
d
fd}t                      }d t          |           D             }|d d          }t                      	|                                }|fdd	}|                    |           |                    |           |r3|                                }|fdd
}|                    |           |}|3|                    	fd           |                    d            |D ]}|                    d                                
g            	                    d            
d         S )Nr_  r7   r3   rQ   c                n                         t          t          j                                         d S r;   )rb   rd   	tracebackextract_stack)r_  stacks    r>   recordStackDepthzVDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.recordStackDepth  s,    S!8!:!:;;<<<<<r@   c                *    g | ]}t                      S rJ   r    ).0r_  s     r>   
<listcomp>zPDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.<listcomp>  s)     4 4 4&

4 4 4r@   r  r  c                    |S r;   rJ   r_  r  s     r>   cbInnerzMDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.cbInner  s	     r@   c                    |S r;   rJ   rt  s     r>   
cbNewInnerzPDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.cbNewInner%  s	     !Lr@   c                    S r;   rJ   )r  lasts    r>   r   zNDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.<lambda>-  s    $ r@   r   r_  r7   r3   rQ   )r_  r7   r  r  r3   r  )r    rangepopr   rU   rc   )rh  rn  topinnerDeferredsoriginalInnersr  ru  newInnerrw  ry  rm  rh   s            @@r>   chainDeferredsz<DeferredTests.test_boundedStackDepth.<locals>.chainDeferreds  s   E= = = = = = #+**C4 4*/..4 4 4N ,AAA.N#+::D"&&((E :?    
 OOG$$$OO,---  	!)--// >F! ! ! ! !
 !!*---  ! 	! ....///LL' % %t$$$$ UB''' MM$8Or@   rL   r   N)rh  r=  r3   r=  )rc   )rh   r  s   ` r>   test_boundedStackDepthz$DeferredTests.test_boundedStackDepth  sT    /	 /	 /	 /	 /	 /	f 	**NN1,=,=>>>>>r@   c                   t                      }t                      t                      |                    fd                               fd                               d           |                    d                               d           g }                    |j                   |                     |dg           dS )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                    S r;   rJ   rf  s    r>   r   zZDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled.<locals>.<lambda>N  rh  r@   c                    S r;   rJ   )r_  r&  s    r>   r   zZDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled.<locals>.<lambda>O  s    5 r@   N)r    r   rU   rb   rc   )rh   ri  rn  rg  r&  s      @@r>   :test_resultOfDeferredResultOfDeferredOfFiredDeferredCalledzHDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled@  s     !)

!) (

00001110000111tt 7>***4&)))))r@   c                   t          j        d           t                      }g }t          d          }	 |# t          $ r |                                 Y nw xY w|                    |j                   |d         }|                     |j	        |           |j
        d         dd         \  }}|                     g |           |                     g |           dS )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   N)r   r   r    rE   rM   r   ra   rb   rc   rg   framesrh   r+  r*  excr/  localzglobalzs          r>   test_errbackWithNoArgsNoDebugz+DeferredTests.test_errbackWithNoArgsNoDebugW  s     	5!!!$JJ6""	I 	 	 	IIKKKKK		QXtS)))+a.-V$$$W%%%%%   7 AAc                   t          j        d           t                      }g }t          d          }	 |# t          $ r |                                 Y nw xY w|                    |j                   |d         }|                     |j	        |           |j
        d         dd         \  }}|                     g |           |                     g |           dS )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    rE   rM   r   ra   rb   rc   rg   r  assertNotEqualr  s          r>   test_errbackWithNoArgsz$DeferredTests.test_errbackWithNoArgsl  s     	4   $JJ6""	I 	 	 	IIKKKKK		QXtS)))+a.-B'''B(((((r  c                v   t                      }|                    d           t          j        d           d
d}|                    |           g }|                    |j                   |d         }|j        d         d	d         \  }}|                     g |           |                     g |           dS )
        An error raised by a callback creates a Failure.  The Failure captures
        locals and globals if and only if C{Deferred.debug} is set.
        NFr_  r7   r3   rQ   c                     t          d          Nr   rE   r_  s    r>   
raiseErrorzHDeferredTests.test_errorInCallbackDoesNotCaptureVars.<locals>.raiseError      v&&&r@   r   r  rz  )	r    rU   r   r   r   ra   rb   r  rc   rh   r+  r  r*  r/  r  r  s          r>   &test_errorInCallbackDoesNotCaptureVarsz4DeferredTests.test_errorInCallbackDoesNotCaptureVars  s    
 %JJ	

45!!!	' 	' 	' 	' 	
j!!!	QXt+a.-V$$$W%%%%%r@   c                v   t                      }|                    d           t          j        d           d
d}|                    |           g }|                    |j                   |d         }|j        d         d	d         \  }}|                     g |           |                     g |           dS )r  NTr_  r7   r3   rQ   c                     t          d          r  r  r  s    r>   r  zODeferredTests.test_errorInCallbackCapturesVarsWhenDebugging.<locals>.raiseError  r  r@   r   r  rz  )	r    rU   r   r   r   ra   rb   r  r  r  s          r>   -test_errorInCallbackCapturesVarsWhenDebuggingz;DeferredTests.test_errorInCallbackCapturesVarsWhenDebugging  s    
 %JJ	

44   	' 	' 	' 	' 	
j!!!	QXt+a.-B'''B(((((r@   c                6   t                      }t                      }	 |                                 n$# t          $ r |                                 Y nw xY wdd}t          j        |           |                     |          }t          j	        |
                                          }|                     t          |          d           |                     d|d         d	                    |                     d
|d         d                    |                     d|d         d                    |                     d|d	         d	                    |                     d|d	         d                    |                     d|d	         d                    dS )
        L{defer.inlineCallbacks} that re-raise tracebacks into their deferred
        should not lose their tracebacks.
        r+  r7   r3   Generator[Any, Any, None]c              3     K   | V  dS )aJ  
            This is never called.
            It is only used as the decorated function.
            The resulting function is never called in this test.
            This is used to make sure that if we wrap
            an already failed deferred, inlineCallbacks
            will not add any extra traceback frames.
            NrJ   r+  s    r>   icz8DeferredTests.test_inlineCallbacksTracebacks.<locals>.ic  s       GGGGGr@   r  
test_deferr   r   rN   1 / 0test_inlineCallbacksTracebacksf.raiseException()Nr+  r7   r3   r  rN   r    raiseExceptionrM   r   r   inlineCallbacksr  rk  
extract_tbgetTracebackObjectrc   rd   assertInrh   r1   r+  r  
newFailuretbs         r>   r  z,DeferredTests.test_inlineCallbacksTracebacks  s   
   $JJ	 	 	 	IIKKKKK			 		 		 		 	b!!!))!,,
!*"?"?"A"ABBR!$$$lBqE!H----r!uQx888"Q%(+++lBqE!H---92a58DDD-r!uQx88888   3 AAc                    dd}t          j         |                      }t          |t           t                              |                     d|                     |                     dS )zM
        It can discover the return type of a first level coroutine.
        r3   r=  c                 
   K   dS rM  rJ   rJ   r@   r>   
returnsIntz4DeferredTests.test_fromCoroutine.<locals>.returnsInt  r  r@   rL   Nr  )r    r<   r   r=  rc   r
  )rh   r  r+  s      r>   test_fromCoroutinez DeferredTests.test_fromCoroutine  sl    
	 	 	 	 "::<<00Ax}%%%D003344444r@   c                    d dd| j         dt          g}|D ]-}|                     t          j        t          j        |           .dS )
        L{Deferred.fromCoroutine} requires a coroutine object or a generator,
        and will reject things that are not that.
        c                    | S r;   rJ   r  s    r>   r   zCDeferredTests.test_fromCoroutineRequiresCoroutine.<locals>.<lambda>      a r@   rL   TN#test_fromCoroutineRequiresCoroutiner   assertRaisesNotACoroutineErrorr    r<   rh   thingsThatAreNotCoroutinesthings      r>   r  z1DeferredTests.test_fromCoroutineRequiresCoroutine  d     K4&
" 0 	W 	WEe68NPUVVVV	W 	Wr@   GC works differently on PyPy.c                @   t                      }t          j        |          }t          |          }||_        t          j        |          }|                    d           ~~|                      |                       |                      |                       dS )z|
        A circular reference between a `Deferred` and its canceller
        is broken when the deferred is called.
        N)DummyCancellerweakrefrefr    rO   rU   r   )rh   	cancellerweakCancellerrO   weakDeferreds        r>   *test_canceller_circular_reference_callbackz8DeferredTests.test_canceller_circular_reference_callback  s     #$$	I.. #+9"5"5%	{8,,$  	--//***,,..)))))r@   c                   t                      }t          j        |          }t          |          }||_        t          j        |          }t          t          d                    }|                    |           |                     |           ~~| 	                     |                       | 	                     |                       dS )zx
        A circular reference between a `Deferred` and its canceller
        is broken when the deferred fails.
        The test demands failures.N)
r  r  r  r    rO   r/   r  r   r  r   )rh   r  r  rO   r  rZ  s         r>   )test_canceller_circular_reference_errbackz7DeferredTests.test_canceller_circular_reference_errback  s     #$$	I.. #+9"5"5%	{8,,)$@AABB!!! 	X&&& 	--//***,,..)))))r@   c                
   t                      }t          j        |          }t          |          }||_        |                    d            |                    d           ~~|                      |                       dS )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                    t                      S r;   rp  r8  s    r>   r   zKDeferredTests.test_canceller_circular_reference_non_final.<locals>.<lambda>>  s
    xzz r@   N)r  r  r  r    rO   r   rU   r   )rh   r  r  rO   s       r>   +test_canceller_circular_reference_non_finalz9DeferredTests.test_canceller_circular_reference_non_final-  s     #$$	I.. #+9"5"5%	11222$ 	--//*****r@   Nr   )r6   r7   r8   r7   r3   r   r6   r7   r8   r7   r3   rQ   )r6   r/   r8   r7   r3   rQ   )ZrG   rH   rI   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r!  r#  r%  r,  r0  r4  r>  rA  rI  rS  r\  rc  rj  ro  rv  ry  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r	  r  r  r  r  r"  r(  r/  r7  r=  r?  rB  rR  r[  rb  rd  rf  r  r  r  r  r  r  r  r  r  pyunitskipIfr-   r  r  r  rJ   r@   r>   r   r      sE       B B B B   - - - -+ + + +A A A AI I I IQ Q Q QB B B B5 5 5 5A A A A"5 5 5 5,"
 "
 "
 "
H% % % %('. '. '. '.R? ? ? ?"? ? ? ?"& & & && & & &"
@ 
@ 
@ 
@B B B B$
@ 
@ 
@ 
@9 9 9 9K K K K: : : :"+ + + + ) ) ) ).) ) ) )= = = =) ) ) )2 2 2 22 2 2 2
= 
= 
= 
=   
 
 
 
6
 
 
 
<3 3 3 3,- - - -() ) ) ) & & & & > > > >(K K K K? ? ? ?1 1 1 1( ( ( ( > > > >"2 2 2 2$
. 
. 
. 
.	6 	6 	6 	6E E E ED D D D2
 
 
 
.*
 *
 *
 *
X%
 %
 %
 %
N-
 -
 -
 -
^, , , ,.) ) ) )(> > > >(* * * *&1 1 1 14: : : :(/ / / /:0 0 0 0>;
 ;
 ;
 ;
zF
 F
 F
 F
P' ' ' '( ( ( (
' 
' 
' 
'
 
 
 
.) ) ) ).B B B BY Y Y Y
 
 
 
9? 9? 9? 9?v* * * *.& & & &*) ) ) )*& & & &() ) ) )("9 "9 "9 "9H
5 
5 
5 
5W W W W. V]59::* * * ;:*8 V]59::* * * ;:*> V]59::+ + + ;:+ + +r@   r   c                  *    e Zd ZU dZdZded<   d	dZdS )
r  z
    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]]rO   r\   r3   rQ   c                    dS )zV
        This is not expected to be called as part of the current test suite.
        NrJ   r   s     r>   __call__zDummyCanceller.__call__R  s      r@   )rO   r\   r3   rQ   )rG   rH   rI   rl   rO   __annotations__r  rJ   r@   r>   r  r  I  sG           )-H,,,,     r@   r  numDeferredsr=  (tuple[list[int], list[Deferred[object]]]c                    dg| z  g }t          |           D ]-}|fd
fd}|                    t          |	                     .|fS )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   r+  r  nr=  r3   rQ   c                (    |xx         dz  cc<   d S rM  rJ   )r+  r  cancelledStates     r>   r  z_setupRaceState.<locals>.cancelc  s%    1"r@   r  )r+  r  r  r=  r3   rQ   )r{  rb   r    )r  dsr  r  r  s       @r>   _setupRaceStater  X  s     S<'N!#B<   . .12 	# 	# 	# 	# 	# 	# 	# 			(V,,,----2r@   c                  j   e Zd ZdZ e edd           edd                    dd            Z e edd           edd                    dd            Z e edd           edd                    dd            ZddZ	 e edd                    dd            Z
dS )	RaceTestsz
    Tests for L{race}.
    r   r  )	min_value	max_value)beforeWinnerafterWinnerr  r=  r  r3   rQ   c                f   t          |dz   |z             \  }}t          |          }t                      }||                             |           t	          |                     |          t          ||f                     dg|z  dgz   dg|z  z   }t	          |t          |                     dS )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.
        rL   r   N)r  r*   r7   rU   r   r
  r   )rh   r  r  r  r  
raceResultra  expectedCancelledStates           r>   test_successzRaceTests.test_successp  s      -\A-=-KLL"XX
88
<!!(+++ 	  ,,lH-..	
 	
 	

 #$|!3qc!9QC+<M!M+,,	
 	
 	
 	
 	
r@   c           	        t          |dz   |z             \  }}t          t          d                    }t          |          }|D ]}|                    |           |                     |t                    }t          |j        j	        t          |gt          |          z                       t          |t          dgt          |          z                       dS )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.
        rL   r  r   N)r  r/   r  r*   r   r  r&   r   rg   rj   r   rd   )	rh   r  r  r  r  rZ  r  r+  actualFailures	            r>   test_failurezRaceTests.test_failure  s      -\A-=-KLL)$@AABB"XX
 	 	AIIg,,ZFF(gYR())	
 	
 	
 	aS3r77]##	
 	
 	
 	
 	
r@   c                Z   d t          |dz   |z             D             }t          |          }||                             d           ||dz                                d           |                     |           t	          |                                 t                                 dS )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.
        c                *    g | ]}t                      S rJ   rp  )rq  r  s     r>   rr  z4RaceTests.test_resultAfterCancel.<locals>.<listcomp>  s)     $
 $
 $
HJJ$
 $
 $
r@   r   NrL   )r{  r*   rU   r
  r   rr   r   )rh   r  r  r  r  s        r>   test_resultAfterCancelz RaceTests.test_resultAfterCancel  s    $
 $
 %lQ&6&D E E$
 $
 $
 "XX

<!!$'''
<!%%d+++Z(((D**,,egg66666r@   c                   t                      }|t          d           g}t                      }t          |          }|                    |           t	          |                     |          t          d|f                     dS )z~
        If one of the input Deferreds has a cancel function that fires it
        with success, nothing bad happens.
        c                D    |                      t                                S r;   )rU   r7   r  s    r>   r   z1RaceTests.test_resultFromCancel.<locals>.<lambda>  s    FHH)=)= r@   r  r   N)r    r7   r*   rU   r   r
  r   )rh   winnerr  ra  r  s        r>   test_resultFromCancelzRaceTests.test_resultFromCancel  s    
 $,::==>>>&
 88"XX
!!!D((44h8}6M6MNNNNNr@   rL   )r  r  c                    t          |          \  }}t          |          }|                                 t          |t	          dg|z                       |                     |t                     dS )zu
        If the result of L{race} is cancelled then all of the L{Deferred}s
        passed in are cancelled.
        rL   N)r  r*   r  r   r   r  r&   )rh   r  r  r  r  s        r>   test_cancelzRaceTests.test_cancel  sn     -\::"XX
NHaS<-?$@$@AAAZ66666r@   N)r  r=  r  r=  r3   rQ   r   )r  r=  r3   rQ   )rG   rH   rI   rl   r   r   r  r  r  r  r  rJ   r@   r>   r  r  k  s         UXQ777HqA666  
 
 
	 
> UXQ777HqA666  
 
 
	 
: UXQ777HqA666  7 7 7	 7&O O O O  UXQ777  7 7 7 7 7 7r@   r  c                  *    e Zd ZdZddZddZddZdS )	FirstErrorTestsz"
    Tests for L{FirstError}.
    r3   rQ   c                    t          d          }	 |# t          $ r t                      }Y nw xY wt          j        |d          }|                     t          |          dt          |           d           dS )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 textr  zFirstError[#3, ]N)r[  rM   r/   r   r   rc   r`  rh   r  r1   rt   s       r>   rb  zFirstErrorTests.test_repr  s    
 %%	I 	 	 			AAA	  A&&e&DS		&D&D&DEEEEE    ..c                    t          d          }	 |# t          $ r t                      }Y nw xY wt          j        |d          }|                     t          |          dt          |           d           dS )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[  rM   r/   r   r   rc   r   r  s       r>   test_strzFirstErrorTests.test_str  s    
 %%	I 	 	 			AAA	  A&&U%@s1vv%@%@%@AAAAAr   c                $   	 ddz   n# t           $ r t                      }Y nw xY wt          j        |d          }t          j        |d          }	 t	          d          # t           $ r t                      }Y nw xY wt          j        |d          }|                     ||k               |                     ||k               |                     ||k               |                     ||k               |                     |dk               dS )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.
        rL   r      bar	   r  N)rM   r/   r   r   r[  re   r  )rh   firstFailureone
anotherOnesecondFailureanothers         r>   test_comparisonzFirstErrorTests.test_comparison  s+   	%FFF 	% 	% 	%"99LLL	% |R00%lB77
	&U### 	& 	& 	&#IIMMM	& "=!44z)***(((w'''
*+++#####s    ##A   A;:A;Nr   )rG   rH   rI   rl   rb  r  r  rJ   r@   r>   r  r    sb         F F F FB B B B$ $ $ $ $ $r@   r  c                      e Zd Zd)dZd)dZd*dZd*d	Zd+dZd+dZd+dZ	d+dZ
d)dZd)dZd)dZd)dZd,dZd-d!Zd)d"Zd)d#Zd)d$Zd)d%Zd)d&Zd)d'Zd(S ).AlreadyCalledTestsr3   rQ   c                ^    t          j                    | _        t          j        d           d S NT)r   r   _deferredWasDebuggingr   r   s    r>   r   zAlreadyCalledTests.setUp+  s*    %*%7%9%9"4     r@   c                8    t          j        | j                   d S r;   )r   r   r  r   s    r>   tearDownzAlreadyCalledTests.tearDown/  s    4566666r@   r6   r7   r8   c                    d S r;   rJ   r   s      r>   r   zAlreadyCalledTests._callback2      r@   c                    d S r;   rJ   r   s      r>   r   zAlreadyCalledTests._errback5  r  r@   r+  rP   c                0    |                     d           d S )Nr   rT   r@  s     r>   _call_1zAlreadyCalledTests._call_18      	

7r@   c                0    |                     d           d S )NtwicerT   r@  s     r>   _call_2zAlreadyCalledTests._call_2;  r  r@   c                b    |                     t          t                                           d S r;   r   r/   rp   r@  s     r>   _err_1zAlreadyCalledTests._err_1>  &    			',..))*****r@   c                b    |                     t          t                                           d S r;   r  r@  s     r>   _err_2zAlreadyCalledTests._err_2A  r   r@   c                    t                      }|                    | j        | j                   |                     |           |                     t          j        | j        |           d S r;   )	r    r   r   r   r  r  r   AlreadyCalledErrorr  r@  s     r>   testAlreadyCalled_CCz'AlreadyCalledTests.testAlreadyCalled_CCD  sU    #::	t~t}555Q%2DL!DDDDDr@   c                    t                      }|                    | j        | j                   |                     |           |                     t          j        | j        |           d S r;   )	r    r   r   r   r  r  r   r$  r"  r@  s     r>   testAlreadyCalled_CEz'AlreadyCalledTests.testAlreadyCalled_CEJ  sU    #::	t~t}555Q%2DKCCCCCr@   c                    t                      }|                    | j        | j                   |                     |           |                     t          j        | j        |           d S r;   )	r    r   r   r   r  r  r   r$  r"  r@  s     r>   testAlreadyCalled_EEz'AlreadyCalledTests.testAlreadyCalled_EEP  sU    #::	t~t}555A%2DKCCCCCr@   c                    t                      }|                    | j        | j                   |                     |           |                     t          j        | j        |           d S r;   )	r    r   r   r   r  r  r   r$  r  r@  s     r>   testAlreadyCalled_ECz'AlreadyCalledTests.testAlreadyCalled_ECV  sU    #::	t~t}555A%2DL!DDDDDr@   linetyper   funclinesr  ra  r=  c                    d}|D ]7}|                     d|z            r|                    d|z            r|dz  }8|                     ||k               d S )Nr   z %s:z %srL   )r   endswithre   )rh   r,  r-  r.  ra  countlines          r>   _countzAlreadyCalledTests._count\  sm     	 	Dv011 dmmEDL6Q6Q 
)*****r@   r   r  callerinvoker1invoker2c                   |j         d                             d          }|                     d||d           |                     dd|d           |                     dd|d           |                     dd|d           |                     dd|d           |                     d	||d           |                     d	||d           d S )
Nr   
CrL   r  r  r  r"  I)r6   splitr3  )rh   r   r4  r5  r6  r.  s         r>   _checkzAlreadyCalledTests._checkc  s    q	%% 	C***CE1---CE1---C5!,,,C5!,,,C5!,,,C5!,,,,,r@   c                L   t                      }|                    | j        | j                   |                     |           	 |                     |           |                     d           d S # t          j        $ r#}| 	                    |ddd           Y d }~d S d }~ww xY w)N2second callback failed to raise AlreadyCalledErrortestAlreadyCalledDebug_CCr  r  )
r    r   r   r   r  r  r/  r   r$  r<  rh   r+  r   s      r>   r?  z,AlreadyCalledTests.testAlreadyCalledDebug_CCq  s    #::	t~t}555Q	LLLOOO IIJKKKKK ' 	N 	N 	NKK6	9MMMMMMMMM	N   A1 1B# BB#c                L   t                      }|                    | j        | j                   |                     |           	 |                     |           |                     d           d S # t          j        $ r#}| 	                    |ddd           Y d }~d S d }~ww xY w)N1second errback failed to raise AlreadyCalledErrortestAlreadyCalledDebug_CEr  r"  )
r    r   r   r   r  r"  r/  r   r$  r<  r@  s      r>   rD  z,AlreadyCalledTests.testAlreadyCalledDebug_CE|  s    #::	t~t}555Q	KKKNNN IIIJJJJJ ' 	M 	M 	MKK6	8LLLLLLLLL	MrA  c                L   t                      }|                    | j        | j                   |                     |           	 |                     |           |                     d           d S # t          j        $ r#}| 	                    |ddd           Y d }~d S d }~ww xY w)Nr>  testAlreadyCalledDebug_ECr  r  )
r    r   r   r   r  r  r/  r   r$  r<  r@  s      r>   rF  z,AlreadyCalledTests.testAlreadyCalledDebug_EC  s    #::	t~t}555A	LLLOOO IIJKKKKK ' 	M 	M 	MKK6)LLLLLLLLL	MrA  c                L   t                      }|                    | j        | j                   |                     |           	 |                     |           |                     d           d S # t          j        $ r#}| 	                    |ddd           Y d }~d S d }~ww xY w)NrC  testAlreadyCalledDebug_EEr  r"  )
r    r   r   r   r  r"  r/  r   r$  r<  r@  s      r>   rH  z,AlreadyCalledTests.testAlreadyCalledDebug_EE  s    #::	t~t}555A	KKKNNN IIIJJJJJ ' 	L 	L 	LKK6(KKKKKKKKK	LrA  c                x   t          j        d           t                      }|                    | j        | j                   |                     |           	 |                     |           |                     d           d S # t           j	        $ r%}| 
                    |j                   Y d }~d S d }~ww xY w)NFr>  )r   r   r    r   r   r   r  r  r/  r$  r  r6   r@  s      r>   testNoDebuggingz"AlreadyCalledTests.testNoDebugging  s    5!!!#::	t~t}555Q	LLLOOO IIJKKKKK ' 	% 	% 	%QV$$$$$$$$$	%s   B B9B44B9c                   t          j        d           t                      }|                    d            t          j        d           |                    d            t          j        d           t                      }|                    d            t          j        d           |                    d            d S )NFc                    d S r;   rJ   r  s    r>   r   z8AlreadyCalledTests.testSwitchDebugging.<locals>.<lambda>      d r@   Tc                    d S r;   rJ   rM  s    r>   r   z8AlreadyCalledTests.testSwitchDebugging.<locals>.<lambda>  rN  r@   )r   r   r    addBothrU   r@  s     r>   testSwitchDebuggingz&AlreadyCalledTests.testSwitchDebugging  s     	5!!!$JJ			""###4   	

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

44   			""#####r@   Nr   r  )r+  rP   r3   rQ   )
r,  r   r-  r   r.  r  ra  r=  r3   rQ   )
r   r  r4  r   r5  r   r6  r   r3   rQ   )rG   rH   rI   r   r  r   r   r  r  r  r"  r%  r'  r)  r+  r3  r<  r?  rD  rF  rH  rJ  rQ  rJ   r@   r>   r  r  *  s       ! ! ! !7 7 7 7            + + + ++ + + +E E E ED D D DD D D DE E E E+ + + +- - - -	L 	L 	L 	L	K 	K 	K 	K	L 	L 	L 	L	K 	K 	K 	K
L 
L 
L 
L$ $ $ $ $ $r@   r  c                      e Zd ZddZddZddZddZddZddZddZ	ddZ
ddZddZddZddZddZddZddZddZddZddZddZdS )DeferredCancellerTestsr3   rQ   c                >    d | _         d | _        d | _        d| _        d S r   )r   r   r   cancellerCallCountr   s    r>   r   zDeferredCancellerTests.setUp  s(    .215/3"#r@   c                <    |                      | j        d           d S )Nrq  )r  rU  r   s    r>   r  zDeferredCancellerTests.tearDown  s    d-v66666r@   datar   c                    || _         |S r;   r   rh   rW  s     r>   r   z DeferredCancellerTests._callback  s    #r@   c                    || _         d S r;   r   rY  s     r>   r   z!DeferredCancellerTests._callback2  s     $r@   rt   r/   c                    || _         d S r;   r   )rh   rt   s     r>   r   zDeferredCancellerTests._errback  s    #r@   c                $   t                      }|                    | j        | j                   |                                 | j        J |                     | j        j        t          j	                   | 
                    | j                   dS )zy
        A L{Deferred} without a canceller must errback with a
        L{defer.CancelledError} and not callback.
        N)r    r   r   r   r  r   rc   r   r   r   r   r   r@  s     r>   test_noCancellerz'DeferredCancellerTests.test_noCanceller  s}    
 %JJ	t~t}555	


"...,153GHHH$./////r@   c                d   t                      }|                    | j        | j                   |                                 |                    d           |                     t          j        |j        d           |                     t          j        |j	        t                                 dS )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  rU   r  r   r$  r   r  r@  s     r>   !test_raisesAfterCancelAndCallbackz8DeferredCancellerTests.test_raisesAfterCancelAndCallback  s     %JJ	t~t}555	


 	


4 	%2AJEEE%2AIy{{KKKKKr@   c                |   t                      }|                    | j        | j                   |                                 |                    t                                 |                     t          j	        |j
        d           |                     t          j	        |j        t                                 dS )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  r  r   r$  rU   r@  s     r>    test_raisesAfterCancelAndErrbackz7DeferredCancellerTests.test_raisesAfterCancelAndErrback  s     %JJ	t~t}555	


 	
		)++ 	%2AJEEE%2AIy{{KKKKKr@   c                    t                      }|                    | j        | j                   |                                 | j        }|                    d           |                                 |                     || j                   dS )z
        A L{Deferred} without a canceller, when cancelled and then
        callbacked, ignores multiple cancels thereafter.
        N)r    r   r   r   r  r   rU   rs   rh   r+  currentFailures      r>   5test_noCancellerMultipleCancelsAfterCancelAndCallbackzLDeferredCancellerTests.test_noCancellerMultipleCancelsAfterCancelAndCallback  sv    
 %JJ	t~t}555	


,	

4	


nd&9:::::r@   c                   t                      }|                    | j        | j                   |                                 | j        J |                     | j        j        t          j	                   | j        }|
                    t                                 |                     | j        j        t          j	                   |                                 |                     || j                   dS )z
        A L{Deferred} without a canceller, when cancelled and then
        errbacked, ignores multiple cancels thereafter.
        N)r    r   r   r   r  r   rc   r   r   r   r   rE   rs   rc  s      r>   4test_noCancellerMultipleCancelsAfterCancelAndErrbackzKDeferredCancellerTests.test_noCancellerMultipleCancelsAfterCancelAndErrback	  s    
 %JJ	t~t}555	


"...,153GHHH,			,..!!!,153GHHH	


nd&9:::::r@   c                \   t                      }|                    | j        | j                   |                                 | j        J |                     | j        j        t          j	                   | j        }|                                 | 
                    || j                   dS )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   rc   r   r   r   rs   rc  s      r>   test_noCancellerMultipleCancelz5DeferredCancellerTests.test_noCancellerMultipleCancel	  s     %JJ	t~t}555	


"...,153GHHH,	


nd&9:::::r@   c                    d	 fd}t          |          }|                     j         j                   |                                  j        J                       j        j        t          j	                    j        }|                                  
                    | j                                         j        d           dS )
a,  
        Verify that calling cancel multiple times on a deferred with a
        canceller that does not errback results in a
        L{defer.CancelledError} and that subsequent calls to cancel do not
        cause an error and that after all that, the canceller was only
        called once.
        r+  r  r3   rQ   c                (    xj         dz  c_         d S rM  rU  r+  rh   s    r>   r  zCDeferredCancellerTests.test_cancellerMultipleCancel.<locals>.cancel5	      ##q(####r@   r  NrL   r+  r  r3   rQ   )r    r   r   r   r  r   rc   r   r   r   rs   rU  )rh   r  r+  rd  s   `   r>   test_cancellerMultipleCancelz3DeferredCancellerTests.test_cancellerMultipleCancel,	  s    	) 	) 	) 	) 	) 	) %v666	t~t}555	


"...,153GHHH,	


nd&9:::0!44444r@   c                    d	 fd}t          |          }|                     j         j                   |                                                       j        d            j        J                       j        j        t          j
                                        t          j        |j        d                                t          j        |j        t                                 dS )
z
        Verify that a L{Deferred} calls its specified canceller when
        it is cancelled, and that further call/errbacks raise
        L{defer.AlreadyCalledError}.
        r+  r  r3   rQ   c                (    xj         dz  c_         d S rM  rl  rm  s    r>   r  z;DeferredCancellerTests.test_simpleCanceller.<locals>.cancelI	  rn  r@   r  rL   Nro  )r    r   r   r   r  rc   rU  r   r   r   r   r  r$  rU   r   r  rh   r  r+  s   `  r>   test_simpleCancellerz+DeferredCancellerTests.test_simpleCancellerB	  s    	) 	) 	) 	) 	) 	) %v666	t~t}555	


0!444"...,153GHHH 	%2AJEEE%2AIy{{KKKKKr@   c                     d fd}t          |                               j         j                                                    dS )	zQ
        Verify that a canceller is given the correct deferred argument.
        r   r  r3   rQ   c                4                         |            d S r;   )rs   )r   r+  rh   s    r>   r  z8DeferredCancellerTests.test_cancellerArg.<locals>.cancel\	  s    MM"a     r@   r  N)r   r  r3   rQ   )r    r   r   r   r  rs  s   ` @r>   test_cancellerArgz(DeferredCancellerTests.test_cancellerArgW	  sb    
	! 	! 	! 	! 	! 	! 	! %v666	t~t}555	




r@   c                f    d
 fd}t          |          }|                     j         j                   |                    d           |                                                       j        d                                 j	                                         j
        d           d	S )zo
        Test that cancelling a deferred after it has been callbacked does
        not cause an error.
        r+  r  r3   rQ   c                j    xj         dz  c_         |                     t                                 d S rM  rU  r   rE   rm  s    r>   r  z?DeferredCancellerTests.test_cancelAfterCallback.<locals>.canceli	  3    ##q(##IIlnn%%%%%r@   r  zbiff!r   Nro  )r    r   r   r   rU   r  rc   rU  r   r   r   rs  s   `  r>   test_cancelAfterCallbackz/DeferredCancellerTests.test_cancelAfterCallbackc	  s    	& 	& 	& 	& 	& 	& $f555	t~t}555	

7	


0!444$-...-w77777r@   c                    d	 fd}t          |          }|                     j         j                   |                    t                                 |                                                       j        d            j	        J                       j	        j
        t
                                           j                   dS )
z
        Test that cancelling a L{Deferred} after it has been errbacked does
        not result in a L{defer.CancelledError}.
        r+  r  r3   rQ   c                j    xj         dz  c_         |                     t                                 d S rM  rz  rm  s    r>   r  z>DeferredCancellerTests.test_cancelAfterErrback.<locals>.cancel{	  r{  r@   r  r   Nro  )r    r   r   r   r   rE   r  rc   rU  r   r   r   r   rs  s   `  r>   test_cancelAfterErrbackz.DeferredCancellerTests.test_cancelAfterErrbacku	  s    	& 	& 	& 	& 	& 	& %v666	t~t}555			,..!!!	


0!444"...,1<@@@$./////r@   c                .    d	 fd}t          |          }|                     j         j                   |                                                       j        d            j        J                       j        j        t                     dS )
z?
        Test a canceller which errbacks its deferred.
        r+  r  r3   rQ   c                j    xj         dz  c_         |                     t                                 d S rM  rz  rm  s    r>   r  zADeferredCancellerTests.test_cancellerThatErrbacks.<locals>.cancel	  r{  r@   r  rL   Nro  )
r    r   r   r   r  rc   rU  r   r   rE   rs  s   `  r>   test_cancellerThatErrbacksz1DeferredCancellerTests.test_cancellerThatErrbacks	  s    
	& 	& 	& 	& 	& 	& %v666	t~t}555	


0!444"...,1<@@@@@r@   c                <    d
 fd}t          |          }|                     j         j                   |                                                       j        d                                 j        d                                 j	                   d	S )z<
        Test a canceller which calls its deferred.
        r+  r  r3   rQ   c                R    xj         dz  c_         |                     d           d S )NrL   hello!)rU  rU   rm  s    r>   r  zBDeferredCancellerTests.test_cancellerThatCallbacks.<locals>.cancel	  s/    ##q(##JJx     r@   r  rL   r  Nro  )
r    r   r   r   r  rc   rU  r   r   r   rs  s   `  r>   test_cancellerThatCallbacksz2DeferredCancellerTests.test_cancellerThatCallbacks	  s    
	! 	! 	! 	! 	! 	! %v666	t~t}555	


0!444-x888$-.....r@   c                    d fd}d fd}t          |          t          |          }|                    d           |                    fd	           |                                 |                     j         j                                         j        d
            j	        J                       j	        j
        t          j                   dS )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.
        r+  r  r3   rQ   c                (    xj         dz  c_         d S rM  rl  rm  s    r>   innerCancelzEDeferredCancellerTests.test_cancelNestedDeferred.<locals>.innerCancel	  rn  r@   c                2                         d           d S NF)re   rm  s    r>   r  z@DeferredCancellerTests.test_cancelNestedDeferred.<locals>.cancel	  s    OOE"""""r@   r  Nc                    S r;   rJ   )rW  r<  s    r>   r   zBDeferredCancellerTests.test_cancelNestedDeferred.<locals>.<lambda>	  s    1 r@   rL   ro  )r    rU   r   r  r   r   r   rc   rU  r   r   r   r   )rh   r  r  r  r<  s   `   @r>   test_cancelNestedDeferredz0DeferredCancellerTests.test_cancelNestedDeferred	  s    	) 	) 	) 	) 	) 	)	# 	# 	# 	# 	# 	# %{;;;$v666	

4	nnnn%%%	


	t~t}5550!444 "...,153GHHHHHr@   Nr   )rW  r   r3   r   )rW  r   r3   rQ   )rt   r/   r3   rQ   )rG   rH   rI   r   r  r   r   r   r]  r_  ra  re  rg  ri  rp  rt  rw  r|  r  r  r  r  rJ   r@   r>   rS  rS    s       $ $ $ $7 7 7 7   % % % %$ $ $ $
0 
0 
0 
0L L L L"L L L L"; ; ; ;; ; ; ;$; ; ; ;5 5 5 5,L L L L*
 
 
 
8 8 8 8$0 0 0 0&A A A A / / / / I I I I I Ir@   rS  c                  j    e Zd ZdZddZddZddZddZdd	Zdd
Z	ddZ
ddZddZddZddZdS )LogTestsz+
    Test logging of unhandled errors.
    r3   rQ   c                P    g | _         t          j        | j         j                   dS )z<
        Add a custom observer to observer logging.
        N)cr,   r   rb   r   s    r>   r   zLogTests.setUp	  s%     (*&&&&&r@   c                B    t          j        | j        j                   dS )z&
        Remove the observer.
        N)r,   removeObserverr  rb   r   s    r>   r  zLogTests.tearDown	  s     	46=)))))r@   List[Dict[str, Any]]c                $    d | j         D             S )Nc                "    g | ]}|d          
|S )isErrorrJ   )rq  r   s     r>   rr  z*LogTests._loggedErrors.<locals>.<listcomp>	  s!    222aQy\2222r@   r  r   s    r>   _loggedErrorszLogTests._loggedErrors	  s    22462222r@   c                    |                                  }|                     t          |          d           |d         d                             t                     |                     t                     dS )zV
        Check the output of the log observer to see if the error is present.
        r   rL   rZ  N)r  rc   rd   rY  r;  rr   )rh   c2s     r>   r<  zLogTests._check	  sj     !!R!$$$
1i/000011111r@   c                    t                                          d                               d           t          j                     |                                  dS )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                    ddz  S r7  rJ   r  s    r>   r   z(LogTests.test_errorLog.<locals>.<lambda>	  
    a r@   rL   N)r    r   rU   gccollectr<  r   s    r>   test_errorLogzLogTests.test_errorLog	  sJ     	

//0099!<<<

r@   c                p    dd} |             t          j                     |                                  dS )zD
        Same as L{test_errorLog}, but with an inner frame.
        r3   rQ   c                 x    t                      } |                     d            |                     d           d S )Nc                    ddz  S r7  rJ   r  s    r>   r   z`LogTests.test_errorLogWithInnerFrameRef.<locals>._subErrorLogWithInnerFrameRef.<locals>.<lambda>	  
    AF r@   rL   )r    r   rU   r  s    r>   _subErrorLogWithInnerFrameRefzNLogTests.test_errorLogWithInnerFrameRef.<locals>._subErrorLogWithInnerFrameRef	  s4    'zzAMM**+++JJqMMMMMr@   Nr   r  r  r<  )rh   r  s     r>   test_errorLogWithInnerFrameRefz'LogTests.test_errorLogWithInnerFrameRef	  sC    
	 	 	 	
 	&%'''

r@   c                p    dd} |             t          j                     |                                  dS )zQ
        Same as L{test_errorLogWithInnerFrameRef}, plus create a cycle.
        r3   rQ   c                     t                      } | fdd}|                     |           | | _        |                     d           d S )	Nr  r=  r+  Deferred[int]r3   c                    ddz  S r7  rJ   )r  r+  s     r>   unusedCyclezgLogTests.test_errorLogWithInnerFrameCycle.<locals>._subErrorLogWithInnerFrameCycle.<locals>.unusedCycle
  s    Avr@   rL   )r  r=  r+  r  r3   r=  )r    r   _drU   )r+  r  s     r>   _subErrorLogWithInnerFrameCyclezRLogTests.test_errorLogWithInnerFrameCycle.<locals>._subErrorLogWithInnerFrameCycle	  sU    'zzA78      MM+&&&ADJJqMMMMMr@   Nr   r  )rh   r  s     r>    test_errorLogWithInnerFrameCyclez)LogTests.test_errorLogWithInnerFrameCycle	  sC    
		 		 		 		 	(')))

r@   c                   t                                          d                               d           t          j                     |                                  |                     dt          | j                             t          j
        | j        d                   }|J d}|                     |                    |          d|           dS )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                    ddz  S r7  rJ   r  s    r>   r   z.LogTests.test_errorLogNoRepr.<locals>.<lambda>
  r  r@   rL   r   Nr    Expected message starting with: )r    r   rU   r  r  r<  rc   rd   r  r,   r~   re   r   )rh   msgra  s      r>   test_errorLogNoReprzLogTests.test_errorLogNoRepr
  s    
 	

//0099!<<<

CKK(((#DF2J//0NN8$$;x;;	
 	
 	
 	
 	
r@   c                b   d	d} |             t          j                     |                                  |                     dt	          | j                             t          j        | j        d                   }|J d}|                     |	                    |          d|           dS )
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.
        r3   rQ   c                     t                      } d| _        |                     d            |                     d           d S )NTc                    ddz  S r7  rJ   r  s    r>   r   z?LogTests.test_errorLogDebugInfo.<locals>.doit.<locals>.<lambda>*
  r  r@   rL   )r    debugr   rU   r  s    r>   doitz-LogTests.test_errorLogDebugInfo.<locals>.doit'
  s;    'zzAAGMM**+++JJqMMMMMr@   r   r  Nz
(debug:  Ir  r   )
r  r  r<  rc   rd   r  r,   r~   re   r   )rh   r  r  ra  s       r>   test_errorLogDebugInfozLogTests.test_errorLogDebugInfo 
  s    	 	 	 	 	

CKK(((#DF2J//NN8$$;x;;	
 	
 	
 	
 	
r@   c                   t                      }|                    d            |                    d           g }g }|                    |j        |j                   |                     |g            |                     t          |          d           |d                             t                     ~~~t          j
                     |                     |                                 g            dS )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                D    t          j        t          d                    S )Nzoop)r   r/  rp   rM  s    r>   r   z3LogTests.test_chainedErrorCleanup.<locals>.<lambda>A
  s    %*\&-A-A"B"B r@   NrL   r   )r    r   rU   r   rb   rc   rd   rY  r  r  r  r  )rh   r+  rn  rx   s       r>   test_chainedErrorCleanupz!LogTests.test_chainedErrorCleanup:
  s     %JJ	BBCCC	

4 ! "	w~v}555"%%%Va(((q	y!!! VQ 	
++--r22222r@   c                   t          j        t          d                    }t                      |J |                    fd           dx}t          j                     |                     |                                 g            dS )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                    S r;   rJ   )r_  goods    r>   r   z6LogTests.test_errorClearedByChaining.<locals>.<lambda>a
  s    t r@   )	r   r/  r  r    ra   r  r  rc   r  )rh   badr  s     @r>   test_errorClearedByChainingz$LogTests.test_errorClearedByChainingV
  s     ).
9W3E3E(F(F)1++++,,, s

++--r22222r@   Nr   )r3   r  )rG   rH   rI   rl   r   r  r  r<  r  r  r  r  r  r  r  rJ   r@   r>   r  r  	  s         ' ' ' '* * * *3 3 3 32 2 2 2         (
 
 
 
&
 
 
 
43 3 3 383 3 3 3 3 3r@   r  c                  .    e Zd Zd
dZd
dZddZd
dZd	S )DeferredListEmptyTestsr3   rQ   c                    d| _         d S r   )callbackRanr   s    r>   r   zDeferredListEmptyTests.setUpj
  s    r@   c                X    t          g           }|                    | j                   dS )zTesting empty DeferredList.N)r"   r   cb_empty)rh   r   s     r>   testDeferredListEmptyz,DeferredListEmptyTests.testDeferredListEmptym
  s)    9Eb9I9I
t}%%%%%r@   resList[Tuple[bool, object]]c                @    d| _         |                     g |           d S rM  )r  rc   )rh   r  s     r>   r  zDeferredListEmptyTests.cb_emptyr
  s&    S!!!!!r@   c                <    |                      | j        d           d S )NzCallback was never run.)re   r  r   s    r>   r  zDeferredListEmptyTests.tearDownv
  s     (*CDDDDDr@   Nr   )r  r  r3   rQ   )rG   rH   rI   r   r  r  r  rJ   r@   r>   r  r  i
  sj           & & & &
" " " "E E E E E Er@   r  c                  n    e Zd ZddZddZddZddZdd	Zdd
ZddZ	ddZ
ddZddZddZddZdS )OtherPrimitivesTestsru   r7   r3   rQ   c                &    | xj         dz  c_         d S rM  counterrh   ru   s     r>   _incrzOtherPrimitivesTests._incr{
  s    r@   c                    d| _         d S r   r  r   s    r>   r   zOtherPrimitivesTests.setUp~
  s    r@   c                  
 t                      }|                                                    | j                   |                     |j                   |                     | j        d           |                                                    | j                   |                     |j                   |                     | j        d           |                                 |                     |j                   |                     | j        d           |                                 | 	                    |j                   |                     | j        d           | 
                    t          |j                   t                      }t                      }t                      }d 
dd
fd}|                    |||          }|                     |j                   |                     
|           |                    |           |                                                    | j                   |                     |j                   |                     | j        d           |                    |           |                     
|           |                     |j                   |                     | j        d	           |                                                    |          }|                                 |                     
t&                     |                     t)          t&          
          j        t,          j                   |                                 | 	                    |j                   dd}dd}	t1          |                    |          t          t2                              t1          |                    |	          t          t2                              d S )NrL   r   resultValuer7   returnValuer3   c                    | |S r;   rJ   )r  r  ru   s     r>   helperz-OtherPrimitivesTests.testLock.<locals>.helper
  s     Fr@   )r  r  r  r  c                 *    t          j        d          S r  r   r(  rJ   r@   r>   r  z1OtherPrimitivesTests.testLock.<locals>.returnsInt
  s    =###r@   r=  c                 
   K   dS rM  rJ   rJ   r@   r>   returnsCoroIntz5OtherPrimitivesTests.testLock.<locals>.returnsCoroInt
  r  r@   r;   )r  r7   r  r7   r3   r7   )r3   r  r  )r#   acquirer   r  re   lockedrc   r  releaser  r  r  runr7   r    rU   rP  r  r<  r/   r   r   r   r   r   r=  )rh   lockfirstUniquesecondUniquecontrolDeferredr  resultDeferredr+  r  r  ru   s             @r>   testLockzOtherPrimitivesTests.testLock
  sg   ~~""4:...$$$q)))""4:...$$$q)))$$$q)))%%%q))))TX...hhxx,4JJ#'	 	 	 	 	 	 	
  " 
 
 	$$$---""6***""4:...$$$q)))  ......$$$q)))LLNN""6**	


fg...gv..3U5IJJJ%%%	$ 	$ 	$ 	$	 	 	 	 	DHHZ(((3-888DHH^,,hsm<<<<<r@   c                     d fd}t                      }|                                }|                    |           |                                 dS )z
        When canceling a L{Deferred} from a L{DeferredLock} that already
        has the lock, the cancel should have no effect.
        r1   r/   r3   rQ   c                2                         d           d S NzUnexpected errback call!r/  r1   rh   s    r>   failOnErrbackzHOtherPrimitivesTests.test_cancelLockAfterAcquired.<locals>.failOnErrback
      II011111r@   Nr1   r/   r3   rQ   )r#   r  ra   r  )rh   r  r  r+  s   `   r>   test_cancelLockAfterAcquiredz1OtherPrimitivesTests.test_cancelLockAfterAcquired
  s\    	2 	2 	2 	2 	2 	2 ~~LLNN	]###	




r@   c                    t                      }|                                 |                                }|                                 |                     |t          j                   dS )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  rk   r   r   )rh   r  r+  s      r>   test_cancelLockBeforeAcquiredz2OtherPrimitivesTests.test_cancelLockBeforeAcquired
  sS     ~~LLNN	


##Au';<<<<<r@   c                $   d}t          |          }t                      d dfd}g }t                      }|                    ||          }|                    |j                   |                    | j                   |                     |g            |                     |                               d            | 	                    |
                                           |                     | j        d           d	| _        t          dd|z             D ]I}|                                                    | j                   |                     | j        |           Jg dfd}dfd}	|                                                    ||	          }
|
                                 |                     dg           |                                                    | j                   |                     | j        |           |                                 |                     | j        |dz              t          dd|z             D ]4}|                                 |                     | j        |dz              5d S )Nr  argr7   r3   r  c                    | S r;   rJ   )r  r  	helperArgs    r>   r  z2OtherPrimitivesTests.testSemaphore.<locals>.helper
  s    I""r@   )r  rL   r   rD  rQ   c                2                         d           d S r  r   rD  r'  s    r>   r/  z0OtherPrimitivesTests.testSemaphore.<locals>.fail
  s    NN5!!!!!r@   c                2                         d           d S r  r   r  s    r>   r(  z3OtherPrimitivesTests.testSemaphore.<locals>.succeed
  s    NN4     r@   T)r  r7   r3   r  )rD  r7   r3   rQ   )r%   r    r7   r  r   rb   r  rc   rU   r   r|  r  r{  r  r   r  r  )rh   Nsemr  rn  uniqueObjectr  ir/  r(  r+  r  r  r'  s              @@@r>   testSemaphorez"OtherPrimitivesTests.testSemaphore
  s   ""*2** 		# 	# 	# 	# 	# 	# 	#
 !#xx\::""7>222""4:..."%%%L111  &&&'++--(((q)))q!a% 	. 	.AKKMM%%dj111T\1----	" 	" 	" 	" 	" 	"	! 	! 	! 	! 	! 	! KKMM&&tW55	


4&)))!!$*---q)))q1u---q!a% 	2 	2AKKMMMT\1q51111	2 	2r@   c                    |                      t          t          d           |                      t          t          d           dS )zz
        If the token count passed to L{DeferredSemaphore} is less than one
        then L{ValueError} is raised.
        r   r  N)r  r[  r%   r   s    r>   test_semaphoreInvalidTokensz0OtherPrimitivesTests.test_semaphoreInvalidTokens  s<    
 	*&7;;;*&7<<<<<r@   c                     d fd}t          d          }|                                }|                    |           |                                 dS )	z
        When canceling a L{Deferred} from a L{DeferredSemaphore} that
        already has the semaphore, the cancel should have no effect.
        r1   r/   r3   rQ   c                2                         d           d S r  r  r  s    r>   r  zMOtherPrimitivesTests.test_cancelSemaphoreAfterAcquired.<locals>.failOnErrback  r  r@   rL   Nr  )r%   r  ra   r  )rh   r  r  r+  s   `   r>   !test_cancelSemaphoreAfterAcquiredz6OtherPrimitivesTests.test_cancelSemaphoreAfterAcquired  s`    	2 	2 	2 	2 	2 	2  ""KKMM	]###	




r@   c                    t          d          }|                                 |                                }|                                 |                     |t          j                   dS )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.
        rL   N)r%   r  r  rk   r   r   )rh   r  r+  s      r>   "test_cancelSemaphoreBeforeAcquiredz7OtherPrimitivesTests.test_cancelSemaphoreBeforeAcquired&  sW      ""KKMM	


##Au';<<<<<r@   c                   d\  }}t          ||          }g }t          |          D ].}|                                                    |j                   /|                     t          j        |j                   t          |          D ]J}|                    |           | 	                    |t          t          |dz                                  Kt          |          D ]J}|                    ||z              | 	                    |t          t          |                               K|                     t          j        |j        d            g }t          |          D ]e}|                                                    |j                   | 	                    |t          t          |||z   dz                                  ft                      }g }t          |          D ].}|                                                    |j                   /t          |          D ]}|                    |           | 	                    |t          t          |                               t          d          }|                     t          j        |j        d            t          d          }|                     t          j        |j                   d S )N)r   r   rL   r   )size)backlog)r$   r{  getr   rb   r  r   QueueUnderflowputrc   listQueueOverflow)rh   r  Mqueuegottenr  s         r>   	testQueuezOtherPrimitivesTests.testQueue2  s   1$1!Q$7$7q 	3 	3AIIKK##FM2222%.	:::q 	9 	9AIIaLLLVT%A,,%7%78888q 	5 	5AIIa!eVT%((^^4444%-uy$???q 	@ 	@AIIKK##FM222VT%1q519*=*=%>%>????q 	3 	3AIIKK##FM2222q 	 	AIIaLLLLeAhh0001%%%%-uy$???a(((%.	:::::r@   c                     d fd}t                      }|                                }|                    |           |                    d           |                                 dS )z
        When canceling a L{Deferred} from a L{DeferredQueue} that already has
        a result, the cancel should have no effect.
        r1   r/   r3   rQ   c                2                         d           d S r  r  r  s    r>   r  zOOtherPrimitivesTests.test_cancelQueueAfterSynchronousGet.<locals>.failOnErrback]  r  r@   Nr  )r$   r  ra   r  r  )rh   r  r  r+  s   `   r>   #test_cancelQueueAfterSynchronousGetz8OtherPrimitivesTests.test_cancelQueueAfterSynchronousGetW  sk    	2 	2 	2 	2 	2 	2 &3__IIKK	]###		$	




r@   c                l    t                                                      }|                                                      |t          j                   d fd}|                    |           g }|                    |j                                        t          |          d           dS )	a  
        When canceling a L{Deferred} from a L{DeferredQueue} that does not
        have a result (i.e., the L{Deferred} has not fired), the cancel
        causes a L{defer.CancelledError} failure. If the queue has a result
        later on, it doesn't try to fire the deferred.
        ignorer7   r3   r  c                                         d                                                                j        d           S r;   )r  r  r   rs   )r  r  rh   s    r>   r   z9OtherPrimitivesTests.test_cancelQueueAfterGet.<locals>.cbr  s4     IIdOOO99;;**4=$???r@   rL   N)r  r7   r3   r  )
r$   r  r  rk   r   r   r   rb   rc   rd   )rh   r+  r   doner  s   `   @r>   test_cancelQueueAfterGetz-OtherPrimitivesTests.test_cancelQueueAfterGetf  s     &3__IIKK	


##Au';<<<	@ 	@ 	@ 	@ 	@ 	@ 	@ 	
b	dk"""TA&&&&&r@   Nr  r   )rG   rH   rI   r  r   r  r  r  r  r  r  r	  r  r  r  rJ   r@   r>   r  r  z
  s             @= @= @= @=D   
= 
= 
= 
=12 12 12 12f= = = =   
= 
= 
= 
=#; #; #; #;J   ' ' ' ' ' 'r@   r  c                  Z    e Zd ZdZddZddZddZddZdd	Zdd
Z	ddZ
ddZddZdS )DeferredFilesystemLockTestsz8
    Test the behavior of L{DeferredFilesystemLock}
    r3   rQ   c                    t                      | _        t          |                                 | j                  | _        d S )N)	scheduler)r+   clockr!   mktempr  r   s    r>   r   z!DeferredFilesystemLockTests.setUp  s/    WW
*4;;==DJOOO			r@   r  c                8    | j                             d          S )zI
        Test that the lock can be acquired when no lock is held
        rL   timeout)r  deferUntilLockedr   s    r>   test_waitUntilLockedWithNoLockz:DeferredFilesystemLockTests.test_waitUntilLockedWithNoLock  s     y))!)444r@   c                   |                      | j                                                   | j                            d          }|                     |t          j                   | j                            dgdz             |S )zs
        Test that the lock can not be acquired when the lock is held
        for longer than the timeout.
        g      @r%  rL   r  )re   r  r'  assertFailurer   TimeoutErrorr"  pumpr@  s     r>   %test_waitUntilLockedWithTimeoutLockedzADeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutLocked  sr    
 		(()))I&&s&331e0111
b!!!r@   c                R    d	 fd}                       j                                                    j                            d j        j                    j                            d          }|                    |            j                            dgdz             |S )
z|
        Test that a lock can be acquired while a lock is held
        but the lock is unlocked before our timeout.
        r1   r/   r3   rQ   c                p    |                      t          j                                       d           d S )NzShould not have timed out)rY  r   r+  r/  r  s    r>   	onTimeoutzVDeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutUnlocked.<locals>.onTimeout  s1    FF5%&&&II122222r@   rL   r  r%  r  )re   r  r"  	callLaterunlockr'  ra   r,  )rh   r0  r+  s   `  r>   'test_waitUntilLockedWithTimeoutUnlockedzCDeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutUnlocked  s    	3 	3 	3 	3 	3 	3 		(()))
Q	 0111I&&r&22	Y
b!!!r@   c                    t          |                                           }|                     |j        t                     dS )zE
        Test that the default scheduler is set up properly.
        N)r!   r#  rc   
_schedulerr   rh   r  s     r>   test_defaultSchedulerz1DeferredFilesystemLockTests.test_defaultScheduler  s7     &dkkmm44'22222r@   c                Z   | j                                           | j                            d| j         j                   | j                                         }| j                                         }|                     |t          j                   | j                            d           |S )z|
        Test that an appropriate exception is raised when attempting
        to use deferUntilLocked concurrently.
        rL   )	r  r"  r1  r2  r'  r*  r   AlreadyTryingToLockErroradvancerH  s      r>   test_concurrentUsagez0DeferredFilesystemLockTests.test_concurrentUsage  s    
 		
Q	 0111Y''))Y''))2u=>>>
1	r@   c                     d fd} j                                            j                            d j         j                    j                                         }|                    |            j                            d           |S )zO
        Test that a DeferredFilesystemLock can be used multiple times
        r  r7   r3   r  c                l    j                                          j                                         }|S r;   )r  r2  r'  )r  r+  rh   s     r>   lockAquiredzDDeferredFilesystemLockTests.test_multipleUsages.<locals>.lockAquired  s/    I	**,,AHr@   rL   )r  r7   r3   r  )r  r"  r1  r2  r'  r   r:  )rh   r>  r+  s   `  r>   test_multipleUsagesz/DeferredFilesystemLockTests.test_multipleUsages  s    
	 	 	 	 	 	
 		
Q	 0111I&&((	k"""
1r@   c                ~   | j                                           | j                                         }| j         j        }|J |                                 |                     |                                           |                     | j         j                   |                     |t          j	                   dS )z
        When cancelling a L{Deferred} returned by
        L{DeferredFilesystemLock.deferUntilLocked}, the
        L{DeferredFilesystemLock._tryLockCall} is cancelled.
        N)
r  r'  _tryLockCallr  r  activer   r  r   r   )rh   rO   tryLockCalls      r>   test_cancelDeferUntilLockedz7DeferredFilesystemLockTests.test_cancelDeferUntilLocked  s     		9--//i,&&&++--...$)0111Xu';<<<<<r@   c                   | j                                           | j                             d          }| j         j        }|J |                                 |                     |                                           |                     | j         j                   |                     |t          j	                   dS )z
        When cancel a L{Deferred} returned by
        L{DeferredFilesystemLock.deferUntilLocked}, if the timeout is
        set, the timeout call will be cancelled.
        rL   r%  N)
r  r'  _timeoutCallr  r  rB  r   r  r   r   )rh   rO   timeoutCalls      r>   &test_cancelDeferUntilLockedWithTimeoutzBDeferredFilesystemLockTests.test_cancelDeferUntilLockedWithTimeout  s     		9--a-88i,&&&++--...$)0111Xu';<<<<<r@   Nr   )r3   r  )rG   rH   rI   rl   r   r(  r-  r3  r7  r;  r?  rD  rH  rJ   r@   r>   r  r  ~  s         P P P P5 5 5 5      (3 3 3 3   "   (= = = == = = = = =r@   r  vtfloatr   c                    dS )zj
    Private function to be used to pass as an alternate onTimeoutCancel value
    to timeoutDeferred
    
OVERRIDDENrJ   )rI  rJ  s     r>   _overrideFuncrN    s	    
 <r@   c                      e Zd ZdZddZddZddZddZddZdd	Z	dd
Z
ddZddZddZddZddZddZddZddZddZddZddZdS )DeferredAddTimeoutTestsz7
    Tests for the function L{Deferred.addTimeout}
    r3   rQ   c                   t                      }|                    dt                                 |                    d            |                    d           |                     d|                     |                     dS )zu
        L{Deferred.addTimeout} returns its own L{Deferred} so it
        can be called in a callback chain.
        r  c                    dS )Nr  rJ   r8  s    r>   r   z?DeferredAddTimeoutTests.test_timeoutChainable.<locals>.<lambda>  s     r@   Nr  )r    
addTimeoutr+   r   rU   rc   r
  r@  s     r>   test_timeoutChainablez-DeferredAddTimeoutTests.test_timeoutChainable  sw    
 %JJ	Q   	&&'''	

4!5!5a!8!899999r@   c                   t                      }t                      }|                    d|           t                      dd	fd}|                    |           |                    d           |                     d           |                     d           |                    d           |                                dS )
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  Nrn  r   r3   r  c                    | S r;   rJ   rn  dCallbackedinnerDeferreds    r>   
onCallbackzKDeferredAddTimeoutTests.test_successResultBeforeTimeout.<locals>.onCallback"      !K  r@   r  rn  r   r3   r  )	r+   r    rS  r   rU   assertIsNotrc   r:  r  rh   r"  r+  rZ  rX  rY  s       @@r>   test_successResultBeforeTimeoutz7DeferredAddTimeoutTests.test_successResultBeforeTimeout  s     #::	R )1

%)	! 	! 	! 	! 	! 	! 	!
 	
j!!!	

9 	{+++i000 	bM*****r@   c                   t                      }t                      }|                    d|t                     t                      dd
fd}|                    |           |                    d           |                     d           |                     d           |                    d	           | 	                               dS )a  
        The L{Deferred} callbacks with the result if it succeeds before
        the timeout, even if a custom C{onTimeoutCancel} function is provided.
        No cancellation happens after the callback either, which could also
        cancel inner deferreds.
        r  onTimeoutCancelNrn  r   r3   r  c                    | S r;   rJ   rW  s    r>   rZ  zQDeferredAddTimeoutTests.test_successResultBeforeTimeoutCustom.<locals>.onCallbackD  r[  r@   r  r\  )
r+   r    rS  rN  r   rU   r]  rc   r:  r  r^  s       @@r>   %test_successResultBeforeTimeoutCustomz=DeferredAddTimeoutTests.test_successResultBeforeTimeoutCustom4  s     #::	R>>> )1

%)	! 	! 	! 	! 	! 	! 	!
 	
j!!!	

9 	{+++i000 	bM*****r@   c                   t                      }t                      }|                    d|           t                      dt          d          }d
fd}|                    |           |                    |           J |                     t                     |                     j	        |           |
                    d	           |                                dS )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  Nr/  r1   r/   r3   r  c                    | S r;   rJ   r1   
dErrbackedrY  s    r>   	onErrbackzDDeferredAddTimeoutTests.test_failureBeforeTimeout.<locals>.onErrbackf      J  r@   r  r1   r/   r3   r  )r+   r    rS  r[  ra   r   r<  r/   rs   rg   r:  r  rh   r"  r+  rt   ri  rh  rY  s        @@r>   test_failureBeforeTimeoutz1DeferredAddTimeoutTests.test_failureBeforeTimeoutV  s     $JJ	R )1

(,
6""	! 	! 	! 	! 	! 	! 	!
 	
Y			% %%%j'222j&... 	bM*****r@   c                   t                      }t                      }|                    d|t                     t                      dt	          d          }dfd	}|                    |           |                    |           J |                     t                     | 	                    j
        |           |                    d
           |                                dS )a  
        The L{Deferred} errbacks with the failure if it fails before the
        timeout, even if using a custom C{onTimeoutCancel} function.
        No cancellation happens after the errback either, which could also
        cancel inner deferreds.
        r  ra  Nr/  r1   r/   r3   r  c                    | S r;   rJ   rg  s    r>   ri  zJDeferredAddTimeoutTests.test_failureBeforeTimeoutCustom.<locals>.onErrback  rj  r@   r  rk  )r+   r    rS  rN  r[  ra   r   r<  r/   rs   rg   r:  r  rl  s        @@r>   test_failureBeforeTimeoutCustomz7DeferredAddTimeoutTests.test_failureBeforeTimeoutCustomy  s     $JJ	R>>> )1

(,
6""	! 	! 	! 	! 	! 	! 	!
 	
Y			% %%%j'222j&... 	bM*****r@   c                    t                      }t                      }|                    d|           |                     |           |                    d           |                     |t          j                   dS )z
        The L{Deferred} by default errbacks with a L{defer.TimeoutError}
        if it times out before callbacking or errbacking.
        r  r  N)r+   r    rS  r  r:  r  r   r+  rh   r"  r+  s      r>   test_timedOutz%DeferredAddTimeoutTests.test_timedOut  so    
 $JJ	RAbQ 233333r@   c                   t                      }t                      }|                    d|t                     |                     |           |                    d           |                     d|                     |                     dS )a1  
        If a custom C{onTimeoutCancel] function is provided, the
        L{Deferred} returns the custom function's return value if the
        L{Deferred} times out before callbacking or errbacking.
        The custom C{onTimeoutCancel} function can return a result instead of
        a failure.
        r  ra  r  rM  Nr+   r    rS  rN  r  r:  rc   r
  rr  s      r>   test_timedOutCustomz+DeferredAddTimeoutTests.test_timedOutCustom  s~     $JJ	R>>>Abt';';A'>'>?????r@   c                   t                      }t          d           }|                    d|           |                     |           |                    d           |                     |                     |          d           dS )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                ,    |                      d          S )NI was cancelled!rT   r  s    r>   r   zLDeferredAddTimeoutTests.test_timedOutProvidedCancelSuccess.<locals>.<lambda>  s    ajj9K.L.L r@   r  r  ry  N)r+   r    rS  r  r:  rc   r
  rr  s      r>   "test_timedOutProvidedCancelSuccessz:DeferredAddTimeoutTests.test_timedOutProvidedCancelSuccess  s     #$L$LMM	RAb--a002DEEEEEr@   c                R   t                      }t          d          t          fd          }|                    d|           |                     |           |                    d           |                     |t                    }|                     |j                   dS )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                .    |                                S r;   )r   )r  rt   s    r>   r   zLDeferredAddTimeoutTests.test_timedOutProvidedCancelFailure.<locals>.<lambda>  s    qyy/?/? r@   r  r  N)	r+   r[  r    rS  r  r:  r  rs   rg   )rh   r"  r+  r1   rt   s       @r>   "test_timedOutProvidedCancelFailurez:DeferredAddTimeoutTests.test_timedOutProvidedCancelFailure  s     7##$%?%?%?%?@@	RAb  J//agu%%%%%r@   c                   t                      }t                      }|                    d|           t                      dd	fd}|                    |           |                                 J |                     t                     |                     j        t          j
                   |                    d           |                                dS )
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  Nr1   r/   r3   r  c                    | S r;   rJ   r1   	dCanceledrY  s    r>   ri  zCDeferredAddTimeoutTests.test_cancelBeforeTimeout.<locals>.onErrback      I  r@   r  rk  )r+   r    rS  ra   r  r<  r/   rs   r   r   r   r:  r  rh   r"  r+  ri  r  rY  s       @@r>   test_cancelBeforeTimeoutz0DeferredAddTimeoutTests.test_cancelBeforeTimeout  s     $JJ	R )1

		! 	! 	! 	! 	! 	! 	!
 	
Y	


 $$$i111ine&:;;; 	bM*****r@   c                   t                      }t                      }|                    d|t                     t                      dd
fd}|                    |           |                                 J |                     t                     |                     j	        t          j                   |                    d	           |                                dS )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  ra  Nr1   r/   r3   r  c                    | S r;   rJ   r  s    r>   ri  zIDeferredAddTimeoutTests.test_cancelBeforeTimeoutCustom.<locals>.onErrback  r  r@   r  rk  )r+   r    rS  rN  ra   r  r<  r/   rs   r   r   r   r:  r  r  s       @@r>   test_cancelBeforeTimeoutCustomz6DeferredAddTimeoutTests.test_cancelBeforeTimeoutCustom  s     $JJ	R>>> )1

		! 	! 	! 	! 	! 	! 	!
 	
Y	


 $$$i111ine&:;;; 	bM*****r@   c                "   t                      }t          d           }|                    d|t                     |                     |           |                    d           |                     d|                     |                     dS )zu
        A custom translation function can handle a L{Deferred} with a
        custom cancellation function.
        c                F    |                      t          d                    S )Nr|  )r   r[  r  s    r>   r   zVDeferredAddTimeoutTests.test_providedCancelCalledBeforeTimeoutCustom.<locals>.<lambda>(  s    qyyG9L9L/M/M r@   r  ra  r  rM  Nru  rr  s      r>   ,test_providedCancelCalledBeforeTimeoutCustomzDDeferredAddTimeoutTests.test_providedCancelCalledBeforeTimeoutCustom"  s    
 $%M%MNN	R>>>Abt';';A'>'>?????r@   c                   t                      }t                      }ddfd}|                    |           |                    d|           |                    d           J |                     t                     |                     j        t          j	                   | 
                    |t          j                   dS )	a  
        An errback added before a timeout is added errbacks with a
        L{defer.CancelledError} when the timeout fires.  If the
        errback returns the L{defer.CancelledError}, it is translated
        to a L{defer.TimeoutError} by the timeout implementation.
        Nr1   r/   r3   c                    | | S r;   rJ   r1   rh  s    r>   r   zGDeferredAddTimeoutTests.test_errbackAddedBeforeTimeout.<locals>.errback<      JHr@   r  r  r1   r/   r3   r/   )r+   r    ra   rS  r:  r<  r/   rg   r   r   r  r+  rh   r"  r+  r   rh  s       @r>   test_errbackAddedBeforeTimeoutz6DeferredAddTimeoutTests.test_errbackAddedBeforeTimeout0  s     $JJ
	 	 	 	 	 	
 	
W	Rb%%%j'222j.0DEEEQ 233333r@   c                   t                      }t                      }dd	fd}|                    |           |                    d|           |                    d           J |                     t                     |                     j        t          j	                   | 
                    |           dS )
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.
        Nr1   r/   r3   rQ   c                J    | |                      t          j                   d S r;   )rY  r   r   r  s    r>   r   z]DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellation.<locals>.errbackX  s#    JFF5'(((((r@   r  r  r  )r+   r    ra   rS  r:  r<  r/   rg   r   r   r
  r  s       @r>   4test_errbackAddedBeforeTimeoutSuppressesCancellationzLDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationL  s     $JJ
	) 	) 	) 	) 	) 	)
 	
W	Rb%%%j'222j.0DEEEQr@   c                   t                      }t                      }dd	fd}|                    |           |                    d|t                     |                    d           J |                     t                     |                     j        t          j
                   |                     d|                     |                     dS )
a  
        An errback added before a timeout is added with a custom
        timeout function errbacks with a L{defer.CancelledError} when
        the timeout fires.  The timeout function runs if the errback
        returns the L{defer.CancelledError}.
        Nr1   r/   r3   c                    | | S r;   rJ   r  s    r>   r   zMDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutCustom.<locals>.errbackt  r  r@   r  r  rM  r  r+   r    ra   rS  rN  r:  r<  r/   rg   r   r   rc   r
  r  s       @r>   $test_errbackAddedBeforeTimeoutCustomz<DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutCustomh  s     $JJ
	 	 	 	 	 	
 	
W	R...b%%%j'222j.0DEEEt';';A'>'>?????r@   c                   t                      }t                      }dd
fd}|                    |           |                    d|t                     |                    d           J |                     t                     |                     j        t          j
                   |                     d	|                     |                     dS )a  
        An errback added before a timeout is added with a custom
        timeout function errbacks with a L{defer.CancelledError} when
        the timeout fires.  The timeout function runs if the errback
        suppresses the L{defer.CancelledError}.
        Nr1   r/   r3   rQ   c                    | d S r;   rJ   r  s    r>   r   zcDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationCustom.<locals>.errback  s    JJJr@   r  r  rM  r  r  r  s       @r>   :test_errbackAddedBeforeTimeoutSuppressesCancellationCustomzRDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationCustom  s     $JJ
	 	 	 	 	 	 	
W	R...b%%%j'222j.0DEEEt';';A'>'>?????r@   c                \   t                      }dt          fd          }dd
fd}|                    |           |                    d|           |                    d	           |                                |                     |                     |                     dS )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}.
        r'  c                .    |                                S r;   rT   r+  r'  s    r>   r   zSDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout.<locals>.<lambda>      ajj.A.A r@   Nrg   r   r3   c                    | | S r;   rJ   rg   rX  s    r>   rU   zSDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout.<locals>.callback      KLr@   r  r  rg   r   r3   r   )r+   r    r   rS  r:  rc   rs   r
  rh   r"  r+  rU   rX  r'  s       @@r>   )test_callbackAddedToCancelerBeforeTimeoutzADeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout  s     #$A$A$A$ABB	 	 	 	 	 	
 	
h	Rbg...gt33A6677777r@   c                j   t                      }dt          fd          }ddfd}|                    |           |                    d|t          	           |                    d
           |                                |                     d|                     |                     dS )ad  
        Given a deferred with a cancellation function that resumes the
        callback chain, a callback that is added to the deferred
        before a timeout is added to runs when the timeout fires.  The
        deferred completes successfully, without a
        L{defer.TimeoutError}.  The timeout's custom timeout function
        also runs.
        r'  c                .    |                                S r;   rT   r  s    r>   r   zYDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom.<locals>.<lambda>  r  r@   Nrg   r   r3   c                    | | S r;   rJ   r  s    r>   rU   zYDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom.<locals>.callback  r  r@   r  ra  r  rM  r  )r+   r    r   rS  rN  r:  rc   r
  r  s       @@r>   /test_callbackAddedToCancelerBeforeTimeoutCustomzGDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom  s     #$A$A$A$ABB	 	 	 	 	 	
 	
h	R>>>bg...t';';A'>'>?????r@   Nr   )rG   rH   rI   rl   rT  r_  rd  rm  rp  rs  rv  rz  r~  r  r  r  r  r  r  r  r  r  rJ   r@   r>   rP  rP    s        	: 	: 	: 	:+ + + +B +  +  +  +D!+ !+ !+ !+F"+ "+ "+ "+H4 4 4 4@ @ @ @"F F F F& & & &"!+ !+ !+ !+F!+ !+ !+ !+F@ @ @ @4 4 4 48       8@ @ @ @8@ @ @ @68 8 8 88@ @ @ @ @ @r@   rP  c                  2    e Zd ZdZd	dZd	dZd	dZd	dZdS )
EnsureDeferredTestsz&
    Tests for L{ensureDeferred}.
    r3   rQ   c                l    t                      }t          |          }|                     ||           dS )zK
        L{ensureDeferred} will pass through a Deferred unchanged.
        N)r    r)   rs   rH  s      r>   test_passesThroughDeferredsz/EnsureDeferredTests.test_passesThroughDeferreds  s4     &ZZBb"r@   c                    |                      t          j                  5  t          d           ddd           dS # 1 swxY w Y   dS )zr
        Passing L{ensureDeferred} a non-coroutine and a non-Deferred will
        raise a L{ValueError}.
        	somethingN)r  r   r  r)   r   s    r>   'test_willNotAllowNonDeferredOrCoroutinez;EnsureDeferredTests.test_willNotAllowNonDeferredOrCoroutine  s    
 u788 	( 	(;'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(s   =AAc                L   dd} |            }|                      |t          j                   t          |          }t	          |t
          t                              |                      |t
                     |                     |          }|                     |d           dS )zM
        L{ensureDeferred} will turn a coroutine into a L{Deferred}.
        r3   r   c                 B   K   t          j        d          } |  d {V }|S NrH  r  r+  r  s     r>   r  z=EnsureDeferredTests.test_ensureDeferredCoroutine.<locals>.run  s+      e$$A''''''CJr@   rH  N)r3   r   )	r<  typesCoroutineTyper)   r   r    r   r
  rc   rh   r  rD  r+  r  s        r>   test_ensureDeferredCoroutinez0EnsureDeferredTests.test_ensureDeferredCoroutine      
	 	 	 	 CEEa!4555 1Ax}%%%a*** ""1%%e$$$$$r@   c                L   dd} |            }|                      |t          j                   t          |          }t	          |t
          t                              |                      |t
                     |                     |          }|                     |d           dS )zX
        L{ensureDeferred} will turn a yield-from coroutine into a L{Deferred}.
        r3   #Generator[Deferred[str], None, str]c               3  h   K   t          j        d          } t          t          | E d {V           }|S r  )r   r(  r   r   r  s     r>   r  z=EnsureDeferredTests.test_ensureDeferredGenerator.<locals>.run
  s5      e$$As\\\\\\++CJr@   rH  N)r3   r  )	r<  r  GeneratorTyper)   r   r    r   r
  rc   r  s        r>   test_ensureDeferredGeneratorz0EnsureDeferredTests.test_ensureDeferredGenerator  r  r@   Nr   )rG   rH   rI   rl   r  r  r  r  rJ   r@   r>   r  r    sn            ( ( ( (% % % %.% % % % % %r@   r  c                      e Zd ZdZddZdS )TimeoutErrorTestsz1
    L{twisted.internet.defer} timeout code.
    r3   rQ   c                   t                      }t          j        |           |                     |t          j                   |                     | j        g          }|                     t          |          d           | 	                    |d         d         t                     |                     |d         d         d           dS )zB
        L{twisted.internet.defer.timeout} is deprecated.
        rL   r   rI  rJ  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+  rK  test_deprecatedTimeoutrc   rd   rs   rL  )rh   rO   warningsShowns      r>   r  z(TimeoutErrorTests.test_deprecatedTimeout"  s     &.ZZh8U%7888**D,G+HII]++Q///mA&z24FGGG!Y'M	
 	
 	
 	
 	
r@   Nr   )rG   rH   rI   rl   r  rJ   r@   r>   r  r    s2         
 
 
 
 
 
r@   r  loopr   c                b    |                      | j                   |                                  dS )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r  s    r>   callAllSoonCallsr  3  s0     	NN49r@   c                  N    e Zd ZddZddZddZddZddZdd	Zdd
Z	ddZ
dS )DeferredFutureAdapterTestsr3   r   c                V    t                      }|                     |j                   |S )zU
        Create a new event loop that will be closed at the end of the test.
        )_new_event_loopr   closer  s     r>   newLoopz"DeferredFutureAdapterTests.newLoop@  s(     !""%%%r@   rQ   c                   t                      }|                                 }|                    |          }|                     |                                d           |                    d           t          |           |                     |                     |          d           |                     |                                d           dS )zw
        L{Deferred.asFuture} returns a L{asyncio.Future} which fires when
        the given L{Deferred} does.
        Fr  N)	r    r  asFuturerc   r  rU   r  r
  ru   )rh   r+  r  aFutures       r>   test_asFuturez(DeferredFutureAdapterTests.test_asFutureH  s    
 $::||~~**T""///	

2--a00$777))2.....r@   c                   ddfd}t          |          }|                                 }|                    |          }|                                 t	          |           |                                |                     |                     |          d           |                     t          |j
                   dS )	z
        L{Deferred.asFuture} returns a L{asyncio.Future} which, when
        cancelled, will cancel the original L{Deferred}.
        Fdprimer  r3   rQ   c                    dd S r  rJ   )r  r  s    r>   cancelerzFDeferredFutureAdapterTests.test_asFutureCancelFuture.<locals>.canceler^  s    FFFr@   Nr  r  r3   rQ   )r    r  r  r  r  re   rc   r
  r  r   ru   )rh   r  r+  r  r  r  s        @r>   test_asFutureCancelFuturez4DeferredFutureAdapterTests.test_asFutureCancelFutureV  s     	 	 	 	 	 	 %X..||~~**T""--a00$777.'.99999r@   c                V   dd}t          |          }|                                 }|                    |          }|                                 t	          |           |                     |                     |          d           |                     t          |j	                   dS )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.
        r  r  r3   rQ   c                0    |                      d           d S )Nr  rT   )r  s    r>   r  zGDeferredFutureAdapterTests.test_asFutureSuccessCancel.<locals>.cancelerr  s    OOAr@   Nr  )
r    r  r  r  r  rc   r
  r  r   ru   )rh   r  r+  r  r  s        r>   test_asFutureSuccessCancelz5DeferredFutureAdapterTests.test_asFutureSuccessCancelk  s    	 	 	 	 %X..||~~**T""--a00$777.'.99999r@   c                P   t                      }t          t                                }|                                 }|                    |          }t          |           |                    |           t          |           |                     t          |j                   dS )z
        L{Deferred.asFuture} makes a L{asyncio.Future} fire with an
        exception when the given L{Deferred} does.
        N)	r    r/   r;  r  r  r  r   r  ru   )rh   r+  
theFailurer  futures        r>   test_asFutureFailurez/DeferredFutureAdapterTests.test_asFutureFailure}  s    
 %JJ.0011
||~~D!!			*+V];;;;;r@   c                :   |                                  }t          |          }t          j        |          }|                     |           |                    d           t          |           |                     |                     |          d           dS )zx
        L{Deferred.fromFuture} returns a L{Deferred} that fires
        when the given L{asyncio.Future} does.
        r  rR  N)	r  r   r    
fromFuturer  
set_resultr  rc   r
  )rh   r  r  r+  s       r>   test_fromFuturez*DeferredFutureAdapterTests.test_fromFuture  s    
 ||~~%4000((A1--a00!44444r@   c                V   |                                  }t          |          }t          j        |          }|                                 t          |           |                     t          |j                   | 	                    |          
                    t                     dS )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  r  r   ru   r  rY  rh   r  	cancelledr+  s       r>   test_fromFutureFutureCancelledz9DeferredFutureAdapterTests.test_fromFutureFutureCancelled  s     ||~~"(d"3"3"3		**.)*:;;;Q$$^44444r@   c                   |                                  }t          |          }t          j        |          }|                                 t          |           |                     |                                d           |                     t          |j
                   |                     |                              t                     dS )z
        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  rc   r  r  r   ru   r  rY  r  s       r>    test_fromFutureDeferredCancelledz;DeferredFutureAdapterTests.test_fromFutureDeferredCancelled  s    
 ||~~"(d"3"3"3		**	


,,..555.)*:;;;Q$$^44444r@   N)r3   r   r   )rG   rH   rI   r  r  r  r  r  r  r  r  rJ   r@   r>   r  r  ?  s           / / / /: : : :*: : : :$< < < <5 5 5 55 5 5 55 5 5 5 5 5r@   r  c                  n    e Zd Zd
dZd
dZed
d            Zed
d            Zed
d            Zd
dZ	d	S )CoroutineContextVarsTestsr3   rQ   c                ^    t                      t          j        d                              d           t	                                          fd           t	                                          fd                               d            t          j        dfd            t          t          g t          d	         f                    t          j        d fd
            }t          |t          g t          d	         f                                        d            |            }
                    d           
                    d           
                    d                                                     |          d           d	S )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.
        testvarrL   c                .                         d          S r  setr9  vars    r>   r   zDCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.<lambda>      swwqzz r@   c                .                         d          S N   r  r  s    r>   r   zDCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.<lambda>      

 r@   c                    ddz  S r7  rJ   r8  s    r>   r   zDCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.<lambda>  
    A r@   r3   #Generator[Deferred[Any], Any, None]c               3     K   t                      }                     d| j        d           | V                      d           d S NrL   Tr  r    r1  rU   r  r+  r"  r  s    r>   yieldingDeferredzLCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.yieldingDeferred  sD      'zzAOOAqz4000GGGGGAJJJJJr@   Nc               3    K                                                        d           t          j        d          V                                                       d                                dj        d           V                                                       d                                dj        d           	 V  t          d          # t          $ r+                                                      d           Y nw xY w             V                                                       d           t          j        d           d S )Nr   rL   T???? should have failed)rc   r  r   r(  r1  rU   r  r  )r"  mutatingDeferredmutatingDeferredThatFailsrh   r  r  s   r>   testFunctionzHCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.testFunction  sr      SWWYY*** -""""" SWWYY*** OOA/8$???"""" SWWYY*** OOA8A4HHH;////   9:::  / / /  A...../ #"$$$$$SWWYY***d#####s   C" "2DDr   Tr3   r  )r+   contextvars
ContextVarr  r    r   r   r  r   r	   r:  rc   r
  )rh   r  r+  r"  r  r  r  r  s   `  @@@@@r>   test_withInlineCallbacksz2CoroutineContextVarsTests.test_withInlineCallbacks  s   
 +6+A)+L+L


 ,4::$$%9%9%9%9:::3;::!!--.B.B.B.BCCC!--oo>>>			 	 	 	 	 	 
		 	$hr8D>/A&BCCC 
	"	$ "	$ "	$ "	$ "	$ "	$ "	$ "	$ "	$ "	$ 
	"	$H 	L(2x~+=">???


LNN 	a 	a 	a--a00$77777r@   c                    t                      t          j        d          t          j        d fd            }                    d            |            }                    d                                |           dS )	z
        When an inlineCallbacks function resumes, we should be able to reset() a
        contextvar that was set when it was first called.
        r  r3   r  c               3  \  K                        d          } t                      }                    d|j        d           |V                                                      d                               |                                                                d           d S )Nr  rL   Tr   )r  r    r1  rU   rc   r  reset)tokenr+  r"  rh   r  s     r>   r  zQCoroutineContextVarsTests.test_resetWithInlineCallbacks.<locals>.yieldingDeferred  s       GGAJJE  (zzAOOAqz4000GGGSWWYY***IIeSWWYY*****r@   r   rL   Nr  )r+   r	  r
  r   r  r  r:  r
  )rh   r  r+  r"  r  s   `  @@r>   test_resetWithInlineCallbacksz7CoroutineContextVarsTests.test_resetWithInlineCallbacks  s    
 +6+A)+L+L			+ 	+ 	+ 	+ 	+ 	+ 	+ 
		+ 	


 	aQr@   c                  K   t                      d}4 d{V  |                     j                                                   }|                    fd           |                     j                   |                     |j                   ddd          d{V  n# 1 d{V swxY w Y   |                     |j                   | d{V  |                     j                   dS )zQ
        L{DeferredLock} can be used as an asynchronous context manager.
        Nc                ,                                     S r;   r  )r9  r  s    r>   r   z>CoroutineContextVarsTests.test_asyncWithLock.<locals>.<lambda>1  s    DLLNN r@   )r#   re   r  r  r   r  r  )rh   r+  r  s     @r>   test_asyncWithLockz,CoroutineContextVarsTests.test_asyncWithLock'  sv     
 ~~ 	' 	' 	' 	' 	' 	' 	' 	'OODK(((AMM2222333OODK(((QX&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	!!!%%%%%s   A;B))
B36B3c                  K   t          d          4 d{V  |                     j        d           4 d{V  |                     j        d                                           }                                }|                     j        d           |                     |j                   |                     |j                   ddd          d{V  n# 1 d{V swxY w Y   |                     j        d           |                     |j                   |                    fd           |                    fd           | d{V  | d{V  |                     j        d           ddd          d{V  n# 1 d{V swxY w Y   |                     j        d           dS )z^
        L{DeferredSemaphore} can be used as an asynchronous context
        manager.
        r  Nr   rL   r   c                ,                                     S r;   r  r9  r  s    r>   r   zCCoroutineContextVarsTests.test_asyncWithSemaphore.<locals>.<lambda>K      S[[]] r@   c                ,                                     S r;   r  r  s    r>   r   zCCoroutineContextVarsTests.test_asyncWithSemaphore.<locals>.<lambda>L  r  r@   )r%   rc   tokensr  re   r  r  r   )rh   r   r   r  s      @r>   test_asyncWithSemaphorez1CoroutineContextVarsTests.test_asyncWithSemaphore8  s       "" 	, 	, 	, 	, 	, 	, 	, 	,SZ+++ , , , , , , , ,  Q///[[]][[]]  Q///	***  +++, , , , , , , , , , , , , , , , , , , , , , , , , , , SZ+++OOBI&&&NN2222333NN2222333HHHHHHHHHHHHHHSZ+++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,  	Q'''''s6   $FBC$F$
C.	.F1C.	2BF
F!$F!c                H  K   t                      }|                     t          d          5  |4 d{V  |                     |j                   t          d          # 1 d{V swxY w Y   	 ddd           n# 1 swxY w Y   |                     |j                   dS )zw
        C{DeferredLock} correctly propagates exceptions when
        used as an asynchronous context manager.
        zsome specific exceptionN)r#   assertRaisesRegexr  re   r  r  r6  s     r>   test_asyncWithLockExceptionz5CoroutineContextVarsTests.test_asyncWithLockExceptionR  s7      ~~##I/HII 	; 	; ; ; ; ; ; ; ; ;,,, 9:::; ; ; ; ; ; ; ; ; ; ; ; ; ; ;	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	%%%%%s.   	A=*A
A)	)A=,A)	-A==BBc                    t                      t          j        d                              d           t	                                          fd           t	                                          fd                               d            dfdd fd
}                    d           t           |                      }                    d                               d                               d                                 	                    |          d           dS )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  rL   c                .                         d          S r  r  r  s    r>   r   zJCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.<lambda>k  r  r@   c                .                         d          S r  r  r  s    r>   r   zJCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.<lambda>n  r  r@   c                    ddz  S r7  rJ   r8  s    r>   r   zJCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.<lambda>o  r  r@   r3   rQ   c                    K   t                      }                     d| j        d           |  d {V                      d           d S r  r   r  s    r>   asyncFuncAwaitingDeferredz[CoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.asyncFuncAwaitingDeferredq  sL       (

AOOAqz4000GGGGGGGGGAJJJJJr@   r   c                   K                                                        d           t          j        d           d {V                                                       d                               dj        d            d {V                                                       d                               dj        d           	  d {V  t          d          # t          $ r+                                                      d           Y nw xY w               d {V                                                       d           dS )Nr   rL   r   Tr  r  )rc   r  r   r(  r1  rU   r  )r$  r"  r  r  rh   r  s   r>   r  zNCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.testFunctionx  s     SWWYY*** -""""""""" SWWYY*** OOA/8$???"""""""" SWWYY*** OOA8A4HHH;////////   9:::  / / /  A...../ ,+---------SWWYY***4s   C. .2D#"D#r   TNr   )r3   r   )
r+   r	  r
  r  r    r   r)   r:  rc   r
  )rh   r  r+  r$  r"  r  r  r  s   `  @@@@@r>   test_contextvarsWithAsyncAwaitz8CoroutineContextVarsTests.test_contextvarsWithAsyncAwait_  s   
 +6+A)+L+L


 ,4::$$%9%9%9%9:::4<JJ!!--.B.B.B.BCCC!--oo>>>	 	 	 	 	 	 	!	 !	 !	 !	 !	 !	 !	 !	 !	 !	 !	H 	


<<>>** 	a 	a 	a--a00$77777r@   Nr   )
rG   rH   rI   r  r  rC   r  r  r  r&  rJ   r@   r>   r  r    s        O8 O8 O8 O8b       @ & & & &  ( ( ( (2 
& 
& 
& 
&I8 I8 I8 I8 I8 I8r@   r  c                      e Zd Zd
dZd
dZd
dZ ej        ed          d
d            Z	 ej        ed          d
d            Z
d	S )InlineCallbackTestsr3   rQ   c                6   t                      }t                      }	 |                                 n$# t          $ r |                                 Y nw xY wdd}t          j        |           |                     |          }t          j	        |
                                          }|                     t          |          d           |                     d|d         d	                    |                     d
|d         d                    |                     d|d         d                    |                     d|d	         d	                    |                     d|d	         d                    |                     d|d	         d                    dS )r  r+  r7   r3   r  c              3     K   | V  d S r;   rJ   r  s    r>   r  z>InlineCallbackTests.test_inlineCallbacksTracebacks.<locals>.ic  s      GGGGGr@   r  r  r   r   rN   r  r  r  Nr  r  r  s         r>   r  z2InlineCallbackTests.test_inlineCallbacksTracebacks  s   
   $JJ	 	 	 	IIKKKKK		 	 	 	 	b!!!))!,,
!*"?"?"A"ABBR!$$$lBqE!H----r!uQx888"Q%(+++lBqE!H---92a58DDD-r!uQx88888r  c                    d dd| j         dt          g}|D ]-}|                     t          j        t          j        |           .dS )r  c                    | S r;   rJ   r  s    r>   r   zIInlineCallbackTests.test_fromCoroutineRequiresCoroutine.<locals>.<lambda>  r  r@   rL   TNr  r  s      r>   r  z7InlineCallbackTests.test_fromCoroutineRequiresCoroutine  r  r@   c                   g }g }t          |j                  t          |j                  t          j        dfd            } |            }|                                |                                |                     |           |                     |g            |                     |g            |                                 |                     |g           |                     |g            |                     |           |                                 |                     |          }|                     |j        t          j	                   |                     |g           dS )a  
        Cancelling an L{defer.inlineCallbacks} correctly handles the function
        catching the L{defer.CancelledError}.

        The desired behavior is:
            1. If the function is waiting on an inner deferred, that inner
               deferred is cancelled, and a L{defer.CancelledError} is raised
               within the function.
            2. If the function catches that exception, execution continues, and
               the deferred returned by the function is not resolved.
            3. Cancelling the deferred again cancels any deferred the function
               is waiting on, and the exception is raised.
        r3   )Generator[Deferred[object], object, None]c               3  @   K   	  V  n# t           $ r Y nw xY wV  d S r;   r  )r   r   s   r>   testFunczHInlineCallbackTests.test_inlineCallbacksCancelCaptured.<locals>.testFunc  sF          HHHHHs   
 
N)r3   r.  )
r    rb   r   r  r  rc   r  r  r   r   )rh   canceller1Callscanceller2Callsr0  funcDrZ  r   r   s         @@r>   "test_inlineCallbacksCancelCapturedz6InlineCallbackTests.test_inlineCallbacksCancelCaptured  s    3524'(>??'(>??			 	 	 	 	 	 
		 

BBE""""---"---
 	2$///"---E"""
 	&&u--u';<<<2$/////r@   r  c                ~   t                      }t          j        |          }t          j        dd            } ||          }|                     ||                     |                     t          j        |          }~~|                      |                       |                      |                       dS )a  
        When using L{defer.inlineCallbacks}, after the function exits, it will
        not keep references to the function itself or the arguments.

        This ensures that the machinery gets deallocated immediately rather than
        waiting for a GC, on CPython.

        The GC on PyPy works differently (del doesn't immediately deallocate the
        object), so we skip the test.
        r  r   r3   c              3     K   | V  | S r;   rJ   r  s    r>   r-  zIInlineCallbackTests.test_inlineCallbacksNoCircularReference.<locals>.func'  s      GGGHr@   Nr  r   r3   r   )r  r  r  r   r  rc   r
  r   rh   obj
objWeakRefr-  r3  funcDWeakRefs         r>   'test_inlineCallbacksNoCircularReferencez;InlineCallbackTests.test_inlineCallbacksNoCircularReference  s     [%%
			 	 	 
		
 S		d22599:::{5))  	**,,''',,..)))))r@   c                ~   t                      }t          j        |          }dd}t          j         ||                    }|                     ||                     |                     t          j        |          }~~|                      |                       |                      |                       dS )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.
        r  r   r3   c                
   K   | S r;   rJ   r7  s    r>   r-  zCInlineCallbackTests.test_coroutineNoCircularReference.<locals>.funcF  s      Hr@   Nr8  )r  r  r  r    r<   rc   r
  r   r9  s         r>   !test_coroutineNoCircularReferencez5InlineCallbackTests.test_coroutineNoCircularReference:  s     [%%
	 	 	 	 &ttCyy11d22599:::{5))  	**,,''',,..)))))r@   Nr   )rG   rH   rI   r  r  r4  r  r  r-   r=  r@  rJ   r@   r>   r(  r(    s        9 9 9 98W W W W.50 50 50 50n V]59::!* !* !* ;:!*F V]59::* * * ;:* * *r@   r(  )r1   r2   r3   r4   )r6   r7   r8   r7   r3   r/   )rO   rP   r3   rQ   )r  r=  r3   r  )rI  r7   rJ  rK  r3   r   )r  r   r3   rQ   )crl   
__future__r   r	  rA   r  rM  rk  r  r0   r  rW  r  asyncior   r   r   r   r  typingr   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.failurer/   twisted.trialrC   r  rE   rN   rW   rX   r[   r`   rn   SynchronousTestCaser   r  r  r  r  r  rS  r  r  r  r  rN  rP  r  r  r  r  r  r(  rJ   r@   r>   <module>rP     s6    # " " " " "         				 				                                                       $ 2 1 1 1 1 1 1 1 1 1       * * * * * * ) ) ) ) ) ) + + + + + + + +                            ( ' ' ' ' '       ' ' ' ' ' ' * * * * * * " " " " " "   	 	 	 	 	9 	 	 	   ) ) ) ) gm95554 4 4 4 4 4 4 4,*M *M *M *M *M! *M *M *MZS+ S+ S+ S+ S+H02G S+ S+ S+l4          &~7 ~7 ~7 ~7 ~7, ~7 ~7 ~7B;$ ;$ ;$ ;$ ;$h2 ;$ ;$ ;$|L$ L$ L$ L$ L$5 L$ L$ L$^GI GI GI GI GIX9 GI GI GITc3 c3 c3 c3 c3x+ c3 c3 c3LE E E E EX9 E E E"A' A' A' A' A'879N A' A' A'Hz= z= z= z= z=("3 z= z= z=z   S@ S@ S@ S@ S@h: S@ S@ S@lA% A% A% A% A%(+ A% A% A%H
 
 
 
 
)+@ 
 
 
,	 	 	 	s5 s5 s5 s5 s5!2 s5 s5 s5ls8 s8 s8 s8 s8 1 s8 s8 s8lj* j* j* j* j*(6 j* j* j* j* j*r@   