o
    bn                    @   s  d 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 ddl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( ddl)m*Z* ddl+m,Z, dd	l-m.Z. dd
l/m0Z0 erddl1Z1nzddl1Z1W n e2y   dZ1Y nw dedeee' ee'f f dedee' f fddZ3G dd de4Z5de6de6de.fddZ7dee8 ddfddZ9ede4dZ:G dd dZ;G dd de0j<Z=G dd  d e0j>e;Z?G d!d" d"e0j>Z@G d#d$ d$e0j>ZAG d%d& d&e0j>ZBG d'd( d(e0j>ZCG d)d* d*e0j>ZDG d+d, d,e0j>e;ZEG d-d. d.e0j<ZFd/e6d0eGde8fd1d2ZHG d3d4 d4e0j>ZIG d5d6 d6e0j<ZJG d7d8 d8e0j<e;ZKd9eddfd:d;ZLG d<d= d=e0j<ZMG d>d? d?e0j<ZNdS )@z+
Test cases for L{twisted.internet.defer}.
    N)AbstractEventLoopCancelledErrorFuturenew_event_loop)TYPE_CHECKINGAnyCallable	CoroutineDict	GeneratorListMappingOptionalTupleTypeTypeVarUnioncast)deferreactor)
DeferredDeferredFilesystemLockDeferredListDeferredLockDeferredQueueDeferredSemaphore_DeferredListResultListT_DeferredListSingleResultT_DeferredResultTensureDeferred)Clock)log)Failure)unittestf.returnc                    s,   t  dtdtdtt f fdd}|S )Nargskwargsr%   c                     s    | i |}t |S N)r   fromCoroutine)r&   r'   coror$    9/usr/lib/python3/dist-packages/twisted/test/test_defer.pywrapper?   s   
z!ensuringDeferred.<locals>.wrapper)	functoolswrapsobjectr   r   )r$   r.   r,   r+   r-   ensuringDeferred<   s    r2   c                   @   s   e Zd ZdS )GenericErrorN)__name__
__module____qualname__r,   r,   r,   r-   r3   G   s    r3   r&   r'   c                  O   s2   zdd  W |S  t y   t| i |}Y |S w )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"   )r&   r'   r$   r,   r,   r-   getDivisionFailureK   s   
r9   deferredc                 C   s   |  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r:   r,   r,   r-   fakeCallbackCancellerY   s   r?   _ExceptionT)boundc                   @   s.   e Zd ZdZdee dee defddZdS )ImmediateFailureMixinz+
    Add additional assertion methods.
    r:   	exceptionr%   c                 C   sP   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.
        r7   r   )r   r#   TestCase
addErrbackappendassertEquallen
assertTruecheckr@   value)selfr:   rC   testCasefailuresr,   r,   r-   assertImmediateFailurek   s   	z,ImmediateFailureMixin.assertImmediateFailureN)	r4   r5   r6   __doc__r   r   r   r@   rO   r,   r,   r,   r-   rB   f   s    rB   c                   @   .   e Zd ZdZd
ddZd
ddZd
dd	ZdS )	UtilTestsz&
    Tests for utility functions.
    r%   Nc                 C   s.   t t }t|}| t | || dS )z<
        L{defer.logError} returns the given error.
        N)r"   RuntimeErrorr   logErrorflushLoggedErrorsassertIs)rL   errorresultr,   r,   r-   test_logErrorReturnsError   s   


z#UtilTests.test_logErrorReturnsErrorc                 C   s0   t t }t| | t}| ||g dS )z9
        L{defer.logError} logs the given error.
        N)r"   rS   r   rT   rU   rG   )rL   rW   errorsr,   r,   r-   test_logErrorLogsError   s   


z UtilTests.test_logErrorLogsErrorc                    sb   g  dt ttf ddf fdd}t| tt }t| | 	t | 
 d d dS )zR
        The text logged by L{defer.logError} has no repr of the failure.
        	eventDictr%   Nc                    s$   t | }|d usJ  | d S r(   )r!   textFromEventDictrF   )r\   textoutputr,   r-   emit   s   
z4UtilTests.test_logErrorLogsErrorNoRepr.<locals>.emitr   Unhandled Error
Traceback )r
   strr   r!   addObserverr"   rS   r   rT   rU   rI   
startswith)rL   ra   rW   r,   r_   r-   test_logErrorLogsErrorNoRepr   s   



z&UtilTests.test_logErrorLogsErrorNoReprr%   N)r4   r5   r6   rP   rY   r[   rf   r,   r,   r,   r-   rR   |   s
    



rR   c                   @   s  e Zd ZdddZdededefddZdededdfd	d
ZdededdfddZ	dddZ
dddZdddZdddZdddZdddZdddZdddZdddZddd Zdd!d"Zdd#d$Zdd%d&Zdd'd(Zdd)d*Zdd+d,Zdd-d.Zdd/d0Zdd1d2Zdd3d4Zdd5d6Zdd7d8Zdd9d:Z dd;d<Z!dd=d>Z"dd?d@Z#ddAdBZ$ddCdDZ%ddEdFZ&ddGdHZ'ddIdJZ(ddKdLZ)ddMdNZ*ddOdPZ+ddQdRZ,ddSdTZ-ddUdVZ.ddWdXZ/ddYdZZ0dd[d\Z1dd]d^Z2dd_d`Z3ddadbZ4ddcddZ5ddedfZ6ddgdhZ7ddidjZ8ddkdlZ9ddmdnZ:ddodpZ;ddqdrZ<ddsdtZ=ddudvZ>ddwdxZ?ddydzZ@dd{d|ZAdd}d~ZBdddZCdddZDdddZEdddZFdddZGdddZHdddZIdddZJdddZKdddZLdddZMdddZNdddZOdddZPdS )DeferredTestsr%   Nc                 C   s(   d | _ d | _d | _| tjt  d S r(   )callbackResultscallback2ResultserrbackResults
addCleanupr   setDebugginggetDebuggingrL   r,   r,   r-   setUp   s   zDeferredTests.setUpr&   r'   c                 O   s   ||f| _ |d S Nr   ri   rL   r&   r'   r,   r,   r-   	_callback   s   
zDeferredTests._callbackc                 O      ||f| _ d S r(   rj   rs   r,   r,   r-   
_callback2      zDeferredTests._callback2c                 O   ru   r(   rk   rs   r,   r,   r-   _errback   rx   zDeferredTests._errbackc                 C   s>   t  }|| j |d | | j | | jdi f d S Nhellor|   r   addCallbackrt   r=   assertIsNonerk   rG   ri   rL   r:   r,   r,   r-   testCallbackWithoutArgs   s
   
z%DeferredTests.testCallbackWithoutArgsc                 C   s@   t  }|| jd |d | | j | | jdi f d S )Nworldr|   )r|   r   r~   r   r,   r,   r-   testCallbackWithArgs   s
   
z"DeferredTests.testCallbackWithArgsc                 C   sF   t  }|j| jdd |d | | j | | jdddif d S )Nr   )r   r|   r}   r~   r   r,   r,   r-   testCallbackWithKwArgs   s
   
z$DeferredTests.testCallbackWithKwArgsc                 C   s\   t  }|| j || j |d | | j | | jdi f | | j	di f d S r{   )
r   r   rt   rw   r=   r   rk   rG   ri   rj   r   r,   r,   r-   testTwoCallbacks   s   
zDeferredTests.testTwoCallbacksc                 C   s   t d}t }|| jttdtf d || || j	 | 
| j | jdus-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   r7   )r3   r   addCallbacksrt   r   r   r1   errbackrE   rz   r   ri   rk   rG   rH   rK   rL   rW   r:   r,   r,   r-   test_addCallbacksNoneErrback   s   
z*DeferredTests.test_addCallbacksNoneErrbackc              	   C   sb   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.
        Nr,   r|   r}   )r   r   rt   rz   r   r   r1   r   rc   r=   r   rk   rG   ri   r   r,   r,   r-   !test_addCallbacksNoneCallbackArgs   s   
z/DeferredTests.test_addCallbacksNoneCallbackArgsc                 C   s   t d}t }|| j| jdi ttt dttt	tf d |
| || j | | j | jdus7J | 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   r,   Nr   r7   )r3   r   r   rt   rz   r   r   r1   r   rc   r   rE   r   ri   rk   rG   rH   rK   r   r,   r,   r-    test_addCallbacksNoneErrbackArgs   s"   
z.DeferredTests.test_addCallbacksNoneErrbackArgsc           	   	   C   s   t tttttf f  }t }t }t }t|||g}g }|fd|d|dd fdd}dtdd f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
resultListrX   r%   c                 S      | |  d S r(   )extendr   rX   r,   r,   r-   cb  rx   z*DeferredTests.testDeferredList.<locals>.cberrc                 S      d S r(   r,   r   r,   r,   r-   catch"     z-DeferredTests.testDeferredList.<locals>.catch123r   r7      )r   r   boolr   rc   r"   r   r   r   r=   rE   r   r3   rG   r   rK   r   SUCCESSFAILURE)	rL   
ResultListdefr1defr2defr3dlrX   r   r   r,   r,   r-   testDeferredList  s(   


"zDeferredTests.testDeferredListc                 C   st   g }|fdt dtt  dd fdd}tg }|| | |g g g |d d < tg dd}|| | |g  d S )Nr   rX   r%   c                 S   r   r(   rF   r   r,   r,   r-   r   =     z/DeferredTests.testEmptyDeferredList.<locals>.cbTfireOnOneCallback)r   r   r   r   rG   )rL   rX   r   dl1dl2r,   r,   r-   testEmptyDeferredList:  s$   


z#DeferredTests.testEmptyDeferredListc           	      C   s   t  }t  }t  }t|||gdd}g }||j dtdd fdd}|| |d | |g  |td | t	|d	 |d
 }| 
t|jtjd|jf  |j}| |jjt | |jjjd | |jd	 d S )NTfireOnOneErrbackr   r%   c                 S   r   r(   r,   r   r,   r,   r-   r   X  r   z;DeferredTests.testDeferredListFireOnOneError.<locals>.catchr   	from def2r7   r   zHissubclass(aFailure.type, defer.FirstError) failed: failure's type is %r)r   )r   r   rE   rF   r"   r=   rG   r   r3   rH   rI   
issubclasstyper   
FirstErrorrK   
subFailurer&   index)	rL   r   r   r   r   rX   r   aFailure
firstErrorr,   r,   r-   testDeferredListFireOnOneErrorN  s.   

z,DeferredTests.testDeferredListFireOnOneErrorc                 C   s   t  }t|g}g }||j g }||j |td | d|d jj	d  | dt
| |d d }|d us?J | d|d jj	d  d S )NBangr   r7   )r   r   rE   rF   r   r   r3   rG   rK   r&   rH   rL   d1r   	errorTrapresultListsfirstResultr,   r,   r-   !testDeferredListDontConsumeErrorsw  s   
z/DeferredTests.testDeferredListDontConsumeErrorsc                 C   s   t  }t|gdd}g }||j g }||j |td | g | | dt| |d d }|d us;J | d|d j	j
d  d S )NTconsumeErrorsr   r7   r   )r   r   rE   rF   r   r   r3   rG   rH   rK   r&   r   r,   r,   r-   testDeferredListConsumeErrors  s   z+DeferredTests.testDeferredListConsumeErrorsc                 C   s\   t  }t  }|td t||gdd}g }||j | dt| |dd  d S )Nr   Tr   r7   c                 S   r   r(   r,   er,   r,   r-   <lambda>      zWDeferredTests.testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds.<locals>.<lambda>)r   r   r3   r   rE   rF   rG   rH   rL   r   d2r   rX   r,   r,   r-   7testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds  s   zEDeferredTests.testDeferredListFireOnOneErrorWithAlreadyFiredDeferredsc                 C   sb   t  }t  }|td |d t||g}g }||j | dt| |	dd  d S )Nr   r   r7   c                 S   r   r(   r,   r   r,   r,   r-   r     r   zIDeferredTests.testDeferredListWithAlreadyFiredDeferreds.<locals>.<lambda>)
r   r   r3   r=   r   r   rF   rG   rH   rE   r   r,   r,   r-   )testDeferredListWithAlreadyFiredDeferreds  s   
z7DeferredTests.testDeferredListWithAlreadyFiredDeferredsc                 C   s@   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   rL   deferredOnedeferredTwodeferredListr,   r,   r-   test_cancelDeferredList  s   z%DeferredTests.test_cancelDeferredListc                 C   s   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   r7   r;   N)r   r?   r   r   r   r   r   successResultOfrI   rG   assertFalserJ   rL   r   r   r   rX   r,   r,   r-   test_cancelDeferredListCallback  s   
z-DeferredTests.test_cancelDeferredListCallbackc                 C   sD   t  }t  }t||gd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   r,   r,   r-   ,test_cancelDeferredListWithFireOnOneCallback  s   z:DeferredTests.test_cancelDeferredListWithFireOnOneCallbackc                 C   sN   t t}t  }t||gd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   )r;   r   N)	r   r?   r   r   r   r   r   r   rG   r   r,   r,   r-   ?test_cancelDeferredListWithFireOnOneCallbackAndDeferredCallback  s   
zMDeferredTests.test_cancelDeferredListWithFireOnOneCallbackAndDeferredCallbackc                 C   sl   t  }t  }t||gd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   rK   rI   r   rJ   )rL   r   r   r   deferredListFailurer   r,   r,   r-   +test_cancelDeferredListWithFireOnOneErrback  s   z9DeferredTests.test_cancelDeferredListWithFireOnOneErrbackc                 C   s   t t}t t}t||gd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   r7   r;   N)r   r?   r   r   r   rI   rG   r   r,   r,   r-   ?test_cancelDeferredListWithFireOnOneErrbackAllDeferredsCallback   s   
zMDeferredTests.test_cancelDeferredListWithFireOnOneErrbackAllDeferredsCallbackc                 C   s^   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   rF   r   r   r   r   assertNoResult)rL   r   r   argumentListr   deferredThreer,   r,   r-   ,test_cancelDeferredListWithOriginalDeferreds  s   
z:DeferredTests.test_cancelDeferredListWithOriginalDeferredsc                 C   sd   dt t ddf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.
        r:   r%   Nc                 S      t d)z
            A L{Deferred} canceller that raises an exception.

            @param deferred: The cancelled L{Deferred}.
            test)rS   r>   r,   r,   r-   cancellerRaisesException(  s   zTDeferredTests.test_cancelDeferredListWithException.<locals>.cancellerRaisesExceptionr7   )r   r1   r   r   r   r   r   rU   rS   rG   rH   )rL   r   r   r   r   rZ   r,   r,   r-   $test_cancelDeferredListWithException!  s   
z2DeferredTests.test_cancelDeferredListWithExceptionc                 C   s<   t  }t  }t||gd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   r=   r   r   r   r,   r,   r-   )test_cancelFiredOnOneCallbackDeferredList8  s   
z7DeferredTests.test_cancelFiredOnOneCallbackDeferredListc                 C   sZ   t  }t  }t||gd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   r3   r   r   r   r   r   r   r,   r,   r-   (test_cancelFiredOnOneErrbackDeferredListE  s   
z6DeferredTests.test_cancelFiredOnOneErrbackDeferredListc                 C   s,   g }t d}||j | |dg d S )Nsuccess)r   succeedr   rF   rG   rL   ldr,   r,   r-   testImmediateSuccessT  s   
z"DeferredTests.testImmediateSuccessc                 C   s8   g }t td}||j | t|d jd d S Nfailr   )r   r   r3   rE   rF   rG   rc   rK   r   r,   r,   r-   testImmediateFailureZ  s   z"DeferredTests.testImmediateFailurec                 C   sT   g }t td}|  ||j | |g  |  | t|d j	d d S r   )
r   r   r3   pauserE   rF   rG   unpauserc   rK   r   r,   r,   r-   testPausedFailure`  s   zDeferredTests.testPausedFailurec                 C   st   g }t  dd |j}|d | |d jt g }t  dd |j}|d | |d jt d S )Nc                 S      dd S Nr7   r   r,   _r,   r,   r-   r   k      z2DeferredTests.testCallbackErrors.<locals>.<lambda>r7   r   c                 S   s
   t t S r(   )r"   ZeroDivisionErrorr   r,   r,   r-   r   q     
 )r   r   rE   rF   r=   assertIsInstancerK   r   r   r,   r,   r-   testCallbackErrorsi  s   


z DeferredTests.testCallbackErrorsc                 C   s&   t  }|  || j |  d S r(   )r   r   r   rt   r   rL   r   r,   r,   r-   testUnpauseBeforeCallbackw  s   z'DeferredTests.testUnpauseBeforeCallbackc                 C   s   t  }t  }|  ||fdd || j |d | jd u s'J d|d | jd u s5J d|  | jd us@J | jd d dksQJ d| jd S )	Nc                 S   s
   t t|S r(   )r   int)rr   r,   r,   r-   r     r   z2DeferredTests.testReturnDeferred.<locals>.<lambda>r7   z Should not have been called yet.r   z&Still should not have been called yet.r   z/Result should have been from second deferred:{})r   r   r   rt   r=   ri   r   formatrL   r   r   r,   r,   r-   testReturnDeferred}  s    

z DeferredTests.testReturnDeferredc                    sv   t  }t   |    t }| fd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(   r,   ignoredpausedr,   r-   r     r   zDDeferredTests.test_chainedPausedDeferredWithResult.<locals>.<lambda>N)r1   r   r=   r   r   rF   rG   r   )rL   expectedchainedrX   r,   r  r-   $test_chainedPausedDeferredWithResult  s   

z2DeferredTests.test_chainedPausedDeferredWithResultc                    s\   t  }t   | fd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                    r   r(   r,   r  secondr,   r-   r     r   z:DeferredTests.test_pausedDeferredChained.<locals>.<lambda>N)r   r   r=   r   rF   rG   )rL   firstrX   r,   r  r-   test_pausedDeferredChained  s   

z(DeferredTests.test_pausedDeferredChainedc                 C   s   g }t t dt dg|j | |ddgg g }t dt t g}t ||j | t	|d | 
|d t |d dd  d S )Nr7   r   r   c                 S      dS Nr7   r,   r   r,   r,   r-   r     r   z2DeferredTests.test_gatherResults.<locals>.<lambda>)r   gatherResultsr   r   rF   rG   r   
ValueErrorrE   rH   r   r"   )rL   resultsrZ   r   r,   r,   r-   test_gatherResults  s   z DeferredTests.test_gatherResultsc                 C   s   t d}t td}t j||gd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}.
        r7   zoh noesTr   r   r7   r   N)r   r   r   rS   r  rE   rF   rG   rH   r   rK   r   r   )rL   dgooddbadr   unconsumedErrorsgatheredErrorsr   r,   r,   r-   #test_gatherResultsWithConsumeErrors  s   
z1DeferredTests.test_gatherResultsWithConsumeErrorsc                 C   sj   t  }t  }t||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   r  r   r   r   r   rK   rI   r   rJ   )rL   r   r   rX   gatherResultsFailurer   r,   r,   r-   test_cancelGatherResults  s   z&DeferredTests.test_cancelGatherResultsc                 C   sT   t t}t t}t||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   r;   r7   N)r   r?   r   r  r   r   rG   )rL   r   r   rX   callbackResultr,   r,   r-   0test_cancelGatherResultsWithAllDeferredsCallback  s   
z>DeferredTests.test_cancelGatherResultsWithAllDeferredsCallbackc                    s`   t   g }g }t fd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                      r   r(   r,   r,   rX   r,   r-   r     r   z6DeferredTests.test_maybeDeferredSync.<locals>.<lambda>r7   r   N)r1   r   maybeDeferredr   rF   rG   rH   assertIdenticalrL   r  rZ   r   r,   r  r-   test_maybeDeferredSync  s   z$DeferredTests.test_maybeDeferredSyncc                 C   sT   dt dt fdd}g }g }t|d}||j|j | |g  | |dg dS )zV
        L{defer.maybeDeferred} should pass arguments to the called function.
        xr%   c                 S      | d S N   r,   r!  r,   r,   r-   plusFive     z>DeferredTests.test_maybeDeferredSyncWithArgs.<locals>.plusFive
      N)r   r   r  r   rF   rG   )rL   r&  r  rZ   r   r,   r,   r-   test_maybeDeferredSyncWithArgs  s   z,DeferredTests.test_maybeDeferredSyncWithArgsc              
   C   s   zdd  W n t y } z
t|}W Y d}~nd}~ww dtdtfdd}g }g }t|d}||j|j | |g  | t|d | t|d	 j	| dS )
z
        L{defer.maybeDeferred} should catch an exception raised by a synchronous
        function and errback its resulting L{Deferred} with it.
        10r$  Nr!  r%   c                 S   r"  r#  r,   r%  r,   r,   r-   r&    r'  z?DeferredTests.test_maybeDeferredSyncException.<locals>.plusFiver7   r   )
	TypeErrorrc   r   r   r  r   rF   rG   rH   rK   )rL   r   r  r&  r  rZ   r   r,   r,   r-   test_maybeDeferredSyncException  s   z-DeferredTests.test_maybeDeferredSyncExceptionc                    s   zdd  W n t y   t  Y nw g }g }t fd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.
        r+  r$  c                      r   r(   r,   r,   r  r,   r-   r   3  r   z=DeferredTests.test_maybeDeferredSyncFailure.<locals>.<lambda>r7   r   N)	r,  r"   r   r  r   rF   rG   rH   r  r  r,   r.  r-   test_maybeDeferredSyncFailure'  s   
z+DeferredTests.test_maybeDeferredSyncFailurec                    sD   t   t fd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                      r   r(   r,   r,   r   r,   r-   r   ?  r   z7DeferredTests.test_maybeDeferredAsync.<locals>.<lambda>SuccessN)r   r   r  r=   r   rF   rG   )rL   r   rX   r,   r0  r-   test_maybeDeferredAsync9  s   
z%DeferredTests.test_maybeDeferredAsyncc                    s8   t   t fd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                      r   r(   r,   r,   r0  r,   r-   r   M  r   z<DeferredTests.test_maybeDeferredAsyncError.<locals>.<lambda>N)r   r   r  r   r"   rS   rO   )rL   r   r,   r0  r-   test_maybeDeferredAsyncErrorE  s   
z*DeferredTests.test_maybeDeferredAsyncErrorc                    s   g g  t  dtdt t f fdd}t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.
        rX   r%   c                    j    d| f td}dtdtt ffdd}dtdtffdd}||| | j  |S )	Nstart-of-cbinnerrX   r%   c                        d  S NfirstCallbackr6  r   r  r6  r  r,   r-   r:  a  s   
zMDeferredTests.test_innerCallbacksPreserved.<locals>.cb.<locals>.firstCallbackc                         d| f | d S NsecondCallbackr   r   r  r  r,   r-   r>  e     zNDeferredTests.test_innerCallbacksPreserved.<locals>.cb.<locals>.secondCallbackrF   r   r   rc   r   r   rE   rX   r   r:  r>  rN   r6  r  r,   r-   r   ]  s   
z6DeferredTests.test_innerCallbacksPreserved.<locals>.cbouteroranger5  rD  r9  )r>  rE  orangeorangeN)	r   rc   r   r   r   r=   rF   rE   rG   rL   r   rD  r,   rC  r-   test_innerCallbacksPreservedR  s   


z*DeferredTests.test_innerCallbacksPreservedc                    s   g g t   dtdt tt  f fdd}t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.
        rX   r%   c                    x    d| f t }dtdttt  f fdd}dtdd ffdd}|| || |j  |d  |S )Nr   rX   r%   c                        d| f t gS Nr:  rF   r   r  r  ar  r,   r-   r:       zNDeferredTests.test_continueCallbackNotFirst.<locals>.cb.<locals>.firstCallbackc                         d| f d S Nr>  r   r  r?  r,   r-   r>       zODeferredTests.test_continueCallbackNotFirst.<locals>.cb.<locals>.secondCallbackrF   r   rc   r   r   rE   r=   rB  rP  rN   r  r,   r-   r         


z7DeferredTests.test_continueCallbackNotFirst.<locals>.cbrD  r   rD  r:  Nwithersr>  N)
r   rc   r   r   r   r   rE   rF   rG   r=   rI  r,   rV  r-   test_continueCallbackNotFirst~  s   "


z+DeferredTests.test_continueCallbackNotFirstc                    s   g g t   dtdt tt  f fdd}t  }|| |fd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.
        rX   r%   c                    rK  )Nr   rX   r%   c                    rL  rM  rN  r  rO  r,   r-   r:    rQ  zLDeferredTests.test_callbackOrderPreserved.<locals>.cb.<locals>.firstCallbackc                    rR  rS  r   r  r?  r,   r-   r>    rT  zMDeferredTests.test_callbackOrderPreserved.<locals>.cb.<locals>.secondCallbackrU  rB  rV  r,   r-   r     rW  z5DeferredTests.test_callbackOrderPreserved.<locals>.cbc                    
     dS )NfinalNr   r%  r?  r,   r-   r     r   z;DeferredTests.test_callbackOrderPreserved.<locals>.<lambda>rD  rX  rY  rZ  r>  r]  N)r   rc   r   r   rE   rF   r=   rG   rI  r,   rV  r-   test_callbackOrderPreserved  s(   "


z)DeferredTests.test_callbackOrderPreservedc                    sj   t  g dfdd dfdd}d fd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.
        rX   Nr%   c                         d d S N   r   r  calledr,   r-   	callback3  rx   z;DeferredTests.test_reentrantRunCallbacks.<locals>.callback3c                    r`  Nr   r   r  rc  r,   r-   	callback2  rx   z;DeferredTests.test_reentrantRunCallbacks.<locals>.callback2c                    s    d   d S r  )rF   r   r  re  rd  r:   r,   r-   	callback1     
z;DeferredTests.test_reentrantRunCallbacks.<locals>.callback1)r7   r   rb  rX   Nr%   Nr   r   r=   rG   )rL   rg  ri  r,   rh  r-   test_reentrantRunCallbacks  s   


z(DeferredTests.test_reentrantRunCallbacksc                    sT   t  g d
fdd d
 fd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.
        rX   Nr%   c                    r`  rf  r   r  rc  r,   r-   rg    rx   z;DeferredTests.test_nonReentrantCallbacks.<locals>.callback2c                    s&    d   dg d S r  )rF   r   rG   r  rg  rd  r:   rL   r,   r-   ri    s   

z;DeferredTests.test_nonReentrantCallbacks.<locals>.callback1r7   r   rk  rl  )rL   ri  r,   rn  r-   test_nonReentrantCallbacks  s   

z(DeferredTests.test_nonReentrantCallbacksc                    sl   dt  dtddffdd dtddf fd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 exceptionrX   r%   Nc                    s   t  r(   )	Exceptionr  )exceptionMessager,   r-   rg    r'  zFDeferredTests.test_reentrantRunCallbacksWithFailure.<locals>.callback2c                    s      d S r(   )r   r  )rg  r:   r,   r-   ri  	  rx   zFDeferredTests.test_reentrantRunCallbacksWithFailure.<locals>.callback1)r   r1   r   r=   rO   rp  rG   r&   )rL   ri  rC   r,   )rg  r:   rq  r-   %test_reentrantRunCallbacksWithFailure  s   

z3DeferredTests.test_reentrantRunCallbacksWithFailurec                    sp   t  }t| t }| fd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                    r   r(   r,   ignr
  r,   r-   r     r   z=DeferredTests.test_synchronousImplicitChain.<locals>.<lambda>Nr   r7   )	r1   r   r   r   r   r=   rF   r   rV   )rL   rX   r	  r  r,   ru  r-   test_synchronousImplicitChain  s   

z+DeferredTests.test_synchronousImplicitChainc                    s   t   t  }| fd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                    r   r(   r,   rs  ru  r,   r-   r   -  r   z>DeferredTests.test_asynchronousImplicitChain.<locals>.<lambda>N)r   r   r=   rF   rG   r1   )rL   r	  r   secondResultrX   r,   ru  r-   test_asynchronousImplicitChain$  s   

z,DeferredTests.test_asynchronousImplicitChainc                 C   sx   t td}|fdddtd d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 Failurer   Nr
  r%   c                 S      |S r(   r,   )r   r
  r,   r,   r-   r   G  r   z<DeferredTests.test_synchronousImplicitErrorChain.<locals>.cbr   )	r   r   rS   r   r   r=   rF   r   rO   )rL   r
  r   r	  r   r,   r,   r-   "test_synchronousImplicitErrorChain>  s   "

z0DeferredTests.test_synchronousImplicitErrorChainc                    s   t   t  }| fd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                    r   r(   r,   rs  ru  r,   r-   r   ]  r   zCDeferredTests.test_asynchronousImplicitErrorChain.<locals>.<lambda>Nry  r   r7   )r   r   r=   rE   rF   rG   r   rS   rI   rJ   rH   )rL   r	  secondErrorr   rw  r,   ru  r-   #test_asynchronousImplicitErrorChainR  s    	
z1DeferredTests.test_asynchronousImplicitErrorChainc                    s   t   t   fdd t  }|fd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                    r   r(   r,   rs  ru  r,   r-   r   y  r   zGDeferredTests.test_doubleAsynchronousImplicitChaining.<locals>.<lambda>c                    r   r(   r,   rs  r  r,   r-   r   {  r   N)r   r   rF   r1   r=   rG   )rL   thirdthirdResultrX   r,   )r
  r	  r-   'test_doubleAsynchronousImplicitChainingo  s   


z5DeferredTests.test_doubleAsynchronousImplicitChainingc                    s   g g  t  dtdt t f fdd}t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.
        rX   r%   c                    r4  )	Nr5  r6  rX   r%   c                    r7  r8  r   r  r;  r,   r-   r:    s   
zXDeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb.<locals>.firstCallbackc                    r<  r=  r   r  r?  r,   r-   r>    r@  zYDeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb.<locals>.secondCallbackrA  rB  rC  r,   r-   r     s   
zADeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cbrD  rG  r9  rE  &Got errbacks but wasn't expecting any.rF  N	r   rc   r   r   r   rF   rG   r=   rE   rI  r,   rC  r-   'test_nestedAsynchronousChainedDeferreds  s$   


z5DeferredTests.test_nestedAsynchronousChainedDeferredsc                    s   g g  t  dtdt t f fdd}t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.
        rX   r%   c                    sz    d| f td}dtdttt  ffdd}dtt dtt ffdd}|| || | j  |S )	Nr5  r6  rX   r%   c                    s.    d| f dtdtt fdd} |S )Nr:  rX   r%   c                 S   s   | gS r(   r,   r  r,   r,   r-   	transform  s   z}DeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.firstCallback.<locals>.transform)rF   rc   r   r   )rX   r  r;  r,   r-   r:    s   
zjDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.firstCallbackc                    r<  r=  r   r  r?  r,   r-   r>    r@  zkDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.secondCallback)rF   r   r   rc   r   r   r   rE   rB  rC  r,   r-   r     s   
 

zSDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cbrD  rG  r9  rZ  r  r>  Nr  rI  r,   rC  r-   9test_nestedAsynchronousChainedDeferredsWithExtraCallbacks  s,   


zGDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacksc                 C   s(   t  }t  }|| | |j| dS )zT
        When we chain a L{Deferred}, that chaining is recorded explicitly.
        N)r   chainDeferredrV   
_chainedTorL   rP  br,   r,   r-   &test_chainDeferredRecordsExplicitChain  s   
z4DeferredTests.test_chainDeferredRecordsExplicitChainc                 C   s0   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   r  r=   r   r  r  r,   r,   r-   %test_explicitChainClearedWhenResolved  s
   	

z3DeferredTests.test_explicitChainClearedWhenResolvedc                    s:   t  }t   | fd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                    r   r(   r,   r  r  r,   r-   r   2  r   zFDeferredTests.test_chainDeferredRecordsImplicitChain.<locals>.<lambda>N)r   r   r=   rV   r  )rL   rP  r,   r  r-   &test_chainDeferredRecordsImplicitChain*  s
   
z4DeferredTests.test_chainDeferredRecordsImplicitChainc                    s   t   dtdt t f fdd} |  d | |g}| t|d |d }| |d t d	}| t	
||d
 d||d
  dS )z
        When a Deferred is returned from a callback directly attached to that
        same Deferred, a warning is emitted.
        rX   r%   c                    r   r(   r,   r  r   r,   r-   circularCallback=  r   zADeferredTests.test_circularChainWarning.<locals>.circularCallbackfoor7   r   categoryz1Callback returned the Deferred it was attached tomessagez
Expected match: {!r}
Got: {!r}N)r   rc   r   r=   flushWarningsrG   rH   DeprecationWarningrI   researchr   )rL   r  circular_warningswarningpatternr,   r  r-   test_circularChainWarning6  s   

z'DeferredTests.test_circularChainWarningc                    sl   |  ttdtj tjdtd t  dtdtt f fd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.
        filtersrW   )r  rX   r%   c                    r   r(   r,   r  r  r,   r-   r  \  r   zCDeferredTests.test_circularChainException.<locals>.circularCallbackr  N)rl   setattrwarningsr  filterwarningsr  r   rc   r   r=   r   trap)rL   r  failurer,   r  r-   test_circularChainExceptionM  s   


z)DeferredTests.test_circularChainExceptionc                 C   s,   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   idrG   repr)rL   r   addressr,   r,   r-   	test_reprd  s   zDeferredTests.test_reprc                 C   s2   t  }|d | t|dt|dd dS )zb
        If a L{Deferred} has been fired, then its C{repr()} contains its
        result.
        rE  r  r!  z current result: 'orange'>N)r   r=   rG   r  r  r   r,   r,   r-   test_reprWithResultm  s   
"z!DeferredTests.test_reprWithResultc                 C   sD   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   r  rG   r  r  r  r,   r,   r-   test_reprWithChainingv  s   
z#DeferredTests.test_reprWithChainingc                    s.   dt dt f fdd} |d|d dS )zw
        The depth of the call stack does not grow as more L{Deferred} instances
        are chained together.
        howManyr%   c           	         s  g dt dd ffdd}t }dd t| D }|d d  }t  | }|fdt dtd  dtd  fdd	}|| || |ra| }|fdt dtd  dtd  fd
d}|| |}|sC| fdd |d  |D ]}|d  qqg   d  d S )Nr  r%   c                    s     tt  d S r(   )rF   rH   	tracebackextract_stackr  )stackr,   r-   recordStackDepth  s   zVDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.recordStackDepthc                 S   s   g | ]}t  qS r,   )r   ).0r  r,   r,   r-   
<listcomp>  s    zPDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.<listcomp>r6  c                 S   rz  r(   r,   r  r6  r,   r,   r-   cbInner     zMDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.cbInnerc                 S   rz  r(   r,   r  r,   r,   r-   
cbNewInner  r  zPDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.cbNewInnerc                    r   r(   r,   rs  )lastr,   r-   r     r   zNDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.<lambda>r   )r1   r   rangepopr   r=   rG   )	r  r  topinnerDeferredsoriginalInnersr6  r  newInnerr  ro   )r  r  r-   chainDeferreds  sN   






z<DeferredTests.test_boundedStackDepth.<locals>.chainDeferredsr7   r   N)r   rG   )rL   r  r,   ro   r-   test_boundedStackDepth  s   3z$DeferredTests.test_boundedStackDepthc                    sv   t  }t   t  | fdd  fd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                    r   r(   r,   r  r  r,   r-   r     r   zZDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled.<locals>.<lambda>c                    r   r(   r,   r  )r~  r,   r-   r     r   N)r   r   r=   rF   rG   )rL   r
  r  r,   )r	  r~  r-   :test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled  s   


zHDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalledc                 C   s   t d t }g }td}z| ty   |  Y n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   rm   r   r3   r8   r   rE   rF   rG   rK   framesrL   r   r   excr   localzglobalzr,   r,   r-   test_errbackWithNoArgsNoDebug     
z+DeferredTests.test_errbackWithNoArgsNoDebugc                 C   s   t d t }g }td}z| ty   |  Y n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   rm   r   r3   r8   r   rE   rF   rG   rK   r  assertNotEqualr  r,   r,   r-   test_errbackWithNoArgs  r  z$DeferredTests.test_errbackWithNoArgsc                 C      t  }|d td dtddf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  r%   c                 S   r   Nr   r3   r  r,   r,   r-   
raiseError	  r'  zHDeferredTests.test_errorInCallbackDoesNotCaptureVars.<locals>.raiseErrorr   r  )
r   r=   r   rm   r1   r   rE   rF   r  rG   rL   r   r  r   r   r  r  r,   r,   r-   &test_errorInCallbackDoesNotCaptureVars      


z4DeferredTests.test_errorInCallbackDoesNotCaptureVarsc                 C   r  )	r  NTr  r%   c                 S   r   r  r  r  r,   r,   r-   r    r'  zODeferredTests.test_errorInCallbackCapturesVarsWhenDebugging.<locals>.raiseErrorr   r  )
r   r=   r   rm   r1   r   rE   rF   r  r  r  r,   r,   r-   -test_errorInCallbackCapturesVarsWhenDebugging  r  z;DeferredTests.test_errorInCallbackCapturesVarsWhenDebuggingc                 C   s   t  }t }z|  W n ty   |  Y nw dtdtttdf fdd}t	| | 
|}t| }| t|d | d|d d	  | d
|d d  | d|d d  | d|d	 d	  | d|d	 d  | d|d	 d  dS )z
        L{defer.inlineCallbacks} that re-raise tracebacks into their deferred
        should not lose their tracebacks.
        r   r%   Nc                 s   s    | V  d S r(   r,   r  r,   r,   r-   ic4  s   
z8DeferredTests.test_inlineCallbacksTracebacks.<locals>.icrb  
test_deferr   r   r9   z1 / 0test_inlineCallbacksTracebackszf.raiseException())r9   r   raiseExceptionr8   r   r1   r   r   r   inlineCallbacksr   r  
extract_tbgetTracebackObjectrG   rH   assertIn)rL   r$   r   r  
newFailuretbr,   r,   r-   r  (  s$   

z,DeferredTests.test_inlineCallbacksTracebacksc                 C   s6   dd dd| j dtg}|D ]}| tjtj| qdS )z
        L{Deferred.fromCoroutine} requires a coroutine object or a generator,
        and will reject things that are not that.
        c                 S   s   | S r(   r,   r%  r,   r,   r-   r   K  r   zCDeferredTests.test_fromCoroutineRequiresCoroutine.<locals>.<lambda>r7   TN)#test_fromCoroutineRequiresCoroutiner   assertRaisesNotACoroutineErrorr   r)   )rL   thingsThatAreNotCoroutinesthingr,   r,   r-   r  D  s   z1DeferredTests.test_fromCoroutineRequiresCoroutinerg   )Qr4   r5   r6   rp   r1   r   rt   rw   r"   rz   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   r   r  r  r  r  r  r  r   r*  r-  r/  r2  r3  rJ  r[  r_  rm  ro  rr  rv  rx  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-   rh      s    





	



$

)
















	















,
'
-








=
G
	




	
	

;




rh   c                   @   rQ   )FirstErrorTestsz"
    Tests for L{FirstError}.
    r%   Nc                 C   sR   t d}z| ty   t }Y nw t|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 textrb  zFirstError[#3, ]N)r  r8   r"   r   r   rG   r  rL   r  r$   rW   r,   r,   r-   r  a     
 zFirstErrorTests.test_reprc                 C   sR   t d}z| ty   t }Y nw t|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  r8   r"   r   r   rG   rc   r  r,   r,   r-   test_stro  r  zFirstErrorTests.test_strc                 C   s   zdd  W n t y   t }Y nw t|d}t|d}ztd t y/   t }Y nw t|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.
        r7   r      bar	   r(  N)r8   r"   r   r   r  rI   r   )rL   firstFailureone
anotherOnesecondFailureanotherr,   r,   r-   test_comparison}  s$   

zFirstErrorTests.test_comparisonrg   )r4   r5   r6   rP   r  r  r  r,   r,   r,   r-   r  \  s
    

r  c                
   @   sH  e Zd Zd6ddZd6ddZdededdfd	d
ZdededdfddZdee	 ddfddZ
dee	 ddfddZdee	 ddfddZdee	 ddfddZd6ddZd6ddZd6ddZd6ddZde	de	d ee	 d!eddf
d"d#Zd$ed%e	d&e	d'e	ddf
d(d)Zd6d*d+Zd6d,d-Zd6d.d/Zd6d0d1Zd6d2d3Zd6d4d5ZdS )7AlreadyCalledTestsr%   Nc                 C   s   t  | _t d d S NT)r   rn   _deferredWasDebuggingrm   ro   r,   r,   r-   rp     rj  zAlreadyCalledTests.setUpc                 C   s   t | j d S r(   )r   rm   r  ro   r,   r,   r-   tearDown     zAlreadyCalledTests.tearDownr&   r'   c                 O   r   r(   r,   rs   r,   r,   r-   rt     r   zAlreadyCalledTests._callbackc                 O   r   r(   r,   rs   r,   r,   r-   rz     r   zAlreadyCalledTests._errbackr   c                 C      | d d S )Nr|   r<   r   r,   r,   r-   _call_1  rx   zAlreadyCalledTests._call_1c                 C   r  )Ntwicer<   r   r,   r,   r-   _call_2  rx   zAlreadyCalledTests._call_2c                 C      | tt  d S r(   r   r"   rS   r   r,   r,   r-   _err_1     zAlreadyCalledTests._err_1c                 C   r  r(   r  r   r,   r,   r-   _err_2  r  zAlreadyCalledTests._err_2c                 C   6   t  }|| j| j | | | tj| j| d S r(   )	r   r   rt   rz   r  r  r   AlreadyCalledErrorr  r   r,   r,   r-   testAlreadyCalled_CC     
z'AlreadyCalledTests.testAlreadyCalled_CCc                 C   r  r(   )	r   r   rt   rz   r  r  r   r  r  r   r,   r,   r-   testAlreadyCalled_CE  r  z'AlreadyCalledTests.testAlreadyCalled_CEc                 C   r  r(   )	r   r   rt   rz   r  r  r   r  r  r   r,   r,   r-   testAlreadyCalled_EE  r  z'AlreadyCalledTests.testAlreadyCalled_EEc                 C   r  r(   )	r   r   rt   rz   r  r  r   r  r  r   r,   r,   r-   testAlreadyCalled_EC  r  z'AlreadyCalledTests.testAlreadyCalled_EClinetypefunclinesr  c                 C   sD   d}|D ]}| d| r|d| r|d7 }q| ||k d S )Nr   z %s:z %sr7   )re   endswithrI   )rL   r  r  r  r  countliner,   r,   r-   _count  s   zAlreadyCalledTests._countr   callerinvoker1invoker2c                 C   s   |j d d}| d||d | dd|d | dd|d | dd|d | dd|d | d	||d | d	||d d S )
Nr   
Cr7   r  r  r  r  I)r&   splitr  )rL   r   r  r  r  r  r,   r,   r-   _check  s   zAlreadyCalledTests._checkc              
   C   v   t  }|| j| j | | z| | W n tjy3 } z| |ddd W Y d }~d S d }~ww | 	d d S )NtestAlreadyCalledDebug_CCr  r  2second callback failed to raise AlreadyCalledError)
r   r   rt   rz   r  r  r   r  r  r   rL   r   r   r,   r,   r-   r       
z,AlreadyCalledTests.testAlreadyCalledDebug_CCc              
   C   r  )NtestAlreadyCalledDebug_CEr  r  1second errback failed to raise AlreadyCalledError)
r   r   rt   rz   r  r  r   r  r  r   r  r,   r,   r-   r    r  z,AlreadyCalledTests.testAlreadyCalledDebug_CEc              
   C   r  )NtestAlreadyCalledDebug_ECr  r  r  )
r   r   rt   rz   r  r  r   r  r  r   r  r,   r,   r-   r    r  z,AlreadyCalledTests.testAlreadyCalledDebug_ECc              
   C   r  )NtestAlreadyCalledDebug_EEr  r  r  )
r   r   rt   rz   r  r  r   r  r  r   r  r,   r,   r-   r    r  z,AlreadyCalledTests.testAlreadyCalledDebug_EEc              
   C   s|   t d t }|| j| j | | z| | W n t jy6 } z| 	|j
 W Y d }~d S d }~ww | d d S )NFr  )r   rm   r   r   rt   rz   r  r  r  r   r&   r   r  r,   r,   r-   testNoDebugging  s   

z"AlreadyCalledTests.testNoDebuggingc                 C   sh   t d t }|dd  t d |d  t d t }|d  t d |dd  d S )NFc                 S   r   r(   r,   rs  r,   r,   r-   r     r   z8AlreadyCalledTests.testSwitchDebugging.<locals>.<lambda>Tc                 S   r   r(   r,   rs  r,   r,   r-   r   &  r   )r   rm   r   addBothr=   r   r,   r,   r-   testSwitchDebugging  s   





z&AlreadyCalledTests.testSwitchDebuggingrg   )r4   r5   r6   rp   r  r1   rt   rz   r   rc   r  r  r  r  r   r  r  r  r   r   r  rp  r  r  r  r  r  r  r  r,   r,   r,   r-   r    s*    





"




r  c                   @   s   e Zd Zd+ddZd+ddZdedefdd	Zdeddfd
dZdeddfddZ	d+ddZ
d+ddZd+ddZd+ddZd+ddZd+ddZd+ddZd+ddZd+dd Zd+d!d"Zd+d#d$Zd+d%d&Zd+d'd(Zd+d)d*ZdS ),DeferredCancellerTestsr%   Nc                 C   s   d | _ d | _d | _d| _d S rq   )ri   rk   rj   cancellerCallCountro   r,   r,   r-   rp   *  s   
zDeferredCancellerTests.setUpc                 C      |  | jd d S )Nr  )r  r!  ro   r,   r,   r-   r  0  s   zDeferredCancellerTests.tearDowndatac                 C   s
   || _ |S r(   rr   rL   r#  r,   r,   r-   rt   4  s   z DeferredCancellerTests._callbackc                 C   
   || _ d S r(   rv   r$  r,   r,   r-   rw   8     
z!DeferredCancellerTests._callback2rW   c                 C   r%  r(   ry   )rL   rW   r,   r,   r-   rz   ;  r&  zDeferredCancellerTests._errbackc                 C   sN   t  }|| j| j |  | jdus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   rt   rz   r   rk   rG   r   r   r   r   ri   r   r,   r,   r-   test_noCanceller>  s   z'DeferredCancellerTests.test_noCancellerc                 C   sR   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   rt   rz   r   r=   r  r   r  r   rp  r   r,   r,   r-   !test_raisesAfterCancelAndCallbackJ  s   
z8DeferredCancellerTests.test_raisesAfterCancelAndCallbackc                 C   sT   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   rt   rz   r   r   rp  r  r   r  r=   r   r,   r,   r-    test_raisesAfterCancelAndErrback[  s   z7DeferredCancellerTests.test_raisesAfterCancelAndErrbackc                 C   sH   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   rt   rz   r   rk   r=   rV   rL   r   currentFailurer,   r,   r-   5test_noCancellerMultipleCancelsAfterCancelAndCallbackl  s   
zLDeferredCancellerTests.test_noCancellerMultipleCancelsAfterCancelAndCallbackc                 C   s|   t  }|| j| j |  | jdus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   rt   rz   r   rk   rG   r   r   r   r   r3   rV   r*  r,   r,   r-   4test_noCancellerMultipleCancelsAfterCancelAndErrback{  s   zKDeferredCancellerTests.test_noCancellerMultipleCancelsAfterCancelAndErrbackc                 C   s^   t  }|| j| j |  | jdus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   rt   rz   r   rk   rG   r   r   r   rV   r*  r,   r,   r-   test_noCancellerMultipleCancel  s   z5DeferredCancellerTests.test_noCancellerMultipleCancelc                    s   dt t ddf fdd}t |d}| j j |   jdus%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%   Nc                         j d7  _ d S r  r!  r  ro   r,   r-   r     rT  zCDeferredCancellerTests.test_cancellerMultipleCancel.<locals>.cancel	cancellerr7   )r   r1   r   rt   rz   r   rk   rG   r   r   r   rV   r!  )rL   r   r   r+  r,   ro   r-   test_cancellerMultipleCancel  s   	
z3DeferredCancellerTests.test_cancellerMultipleCancelc                    s   dt t ddf fdd}t |d}| j j |    jd  jdus,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%   Nc                    r/  r  r0  r  ro   r,   r-   r     rT  z;DeferredCancellerTests.test_simpleCanceller.<locals>.cancelr1  r7   )r   r1   r   rt   rz   r   rG   r!  rk   r   r   r   r  r  r=   r   rp  rL   r   r   r,   ro   r-   test_simpleCanceller  s   
z+DeferredCancellerTests.test_simpleCancellerc                    sB   dt t ddf fdd}t |d  jj    dS )zQ
        Verify that a canceller is given the correct deferred argument.
        r   r%   Nc                    s    |   d S r(   )rV   r0  r   rL   r,   r-   r     r  z8DeferredCancellerTests.test_cancellerArg.<locals>.cancelr1  )r   r1   r   rt   rz   r   )rL   r   r,   r6  r-   test_cancellerArg  s   
z(DeferredCancellerTests.test_cancellerArgc                    sr   dt t ddf fdd}t |d}| 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%   Nc                         j d7  _ | t  d S r  r!  r   r3   r  ro   r,   r-   r        z?DeferredCancellerTests.test_cancelAfterCallback.<locals>.cancelr1  zbiff!r   )r   r1   r   rt   rz   r=   r   rG   r!  r   rk   ri   r4  r,   ro   r-   test_cancelAfterCallback  s   

z/DeferredCancellerTests.test_cancelAfterCallbackc                    s   dt t ddf fdd}t |d}| j j |t  |    j	d  j
dus2J   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%   Nc                    r8  r  r9  r  ro   r,   r-   r     r:  z>DeferredCancellerTests.test_cancelAfterErrback.<locals>.cancelr1  r   )r   r1   r   rt   rz   r   r3   r   rG   r!  rk   r   r   ri   r4  r,   ro   r-   test_cancelAfterErrback  s   
z.DeferredCancellerTests.test_cancelAfterErrbackc                    sl   dt t ddf fdd}t |d}| j j |    jd  jdus,J   jj	t
 dS )z?
        Test a canceller which errbacks its deferred.
        r   r%   Nc                    r8  r  r9  r  ro   r,   r-   r     r:  zADeferredCancellerTests.test_cancellerThatErrbacks.<locals>.cancelr1  r7   )r   r1   r   rt   rz   r   rG   r!  rk   r   r3   r4  r,   ro   r-   test_cancellerThatErrbacks  s   
z1DeferredCancellerTests.test_cancellerThatErrbacksc                    sh   dt t ddf fdd}t |d}| j j |    jd   jd  	 j
 dS )	z<
        Test a canceller which calls its deferred.
        r   r%   Nc                    s     j d7  _ | d d S )Nr7   hello!)r!  r=   r  ro   r,   r-   r     s   zBDeferredCancellerTests.test_cancellerThatCallbacks.<locals>.cancelr1  r7   r>  )r   r1   r   rt   rz   r   rG   r!  ri   r   rk   r4  r,   ro   r-   test_cancellerThatCallbacks  s   
z2DeferredCancellerTests.test_cancellerThatCallbacksc                    s   dt t ddffdd}dt t ddffdd}t |d t |d}|d | fd	d
 |  |jj j	d j
dusLJ 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%   Nc                    r/  r  r0  r  ro   r,   r-   innerCancel  rT  zEDeferredCancellerTests.test_cancelNestedDeferred.<locals>.innerCancelc                    r`  NF)rI   r  ro   r,   r-   r   "  rx   z@DeferredCancellerTests.test_cancelNestedDeferred.<locals>.cancelr1  c                    r   r(   r,   )r#  r  r,   r-   r   (  r   zBDeferredCancellerTests.test_cancelNestedDeferred.<locals>.<lambda>r7   )r   r1   r=   r   r   r   rt   rz   rG   r!  rk   r   r   r   )rL   r@  r   rP  r,   )r  rL   r-   test_cancelNestedDeferred  s   


z0DeferredCancellerTests.test_cancelNestedDeferredrg   )r4   r5   r6   rp   r  rc   rt   rw   r"   rz   r'  r(  r)  r,  r-  r.  r3  r5  r7  r;  r<  r=  r?  rB  r,   r,   r,   r-   r   )  s(    














r   c                   @   s   e Zd ZdZdddZdddZdeeee	f  fdd	Z
dd
dZdddZdddZdddZdddZdddZdddZdddZdS )LogTestsz+
    Test logging of unhandled errors.
    r%   Nc                 C   s   g | _ t| j j dS )z<
        Add a custom observer to observer logging.
        N)cr!   rd   rF   ro   r,   r,   r-   rp   8  s   zLogTests.setUpc                 C   s   t | jj dS )z&
        Remove the observer.
        N)r!   removeObserverrD  rF   ro   r,   r,   r-   r  ?  s   zLogTests.tearDownc                 C   s   dd | j D S )Nc                 S   s   g | ]}|d  r|qS )isErrorr,   )r  r   r,   r,   r-   r  F  s    z*LogTests._loggedErrors.<locals>.<listcomp>rD  ro   r,   r,   r-   _loggedErrorsE  r  zLogTests._loggedErrorsc                 C   s8   |   }| t|d |d d t | t dS )zV
        Check the output of the log observer to see if the error is present.
        r   r7   r  N)rH  rG   rH   r  r   rU   )rL   c2r,   r,   r-   r  H  s   zLogTests._checkc                 C   s*   t  dd d t  |   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                 S   r   r   r,   r%  r,   r,   r-   r   W  r   z(LogTests.test_errorLog.<locals>.<lambda>r7   N)r   r   r=   gccollectr  ro   r,   r,   r-   test_errorLogQ  s   zLogTests.test_errorLogc                 C   $   ddd}|  t   |   dS )zD
        Same as L{test_errorLog}, but with an inner frame.
        r%   Nc                  S   s"   t  } | dd  | d d S )Nc                 S   r   r   r,   r%  r,   r,   r-   r   b  r   z`LogTests.test_errorLogWithInnerFrameRef.<locals>._subErrorLogWithInnerFrameRef.<locals>.<lambda>r7   )r   r   r=   r  r,   r,   r-   _subErrorLogWithInnerFrameRef`  s   zNLogTests.test_errorLogWithInnerFrameRef.<locals>._subErrorLogWithInnerFrameRefrg   rJ  rK  r  )rL   rN  r,   r,   r-   test_errorLogWithInnerFrameRef[  s   
z'LogTests.test_errorLogWithInnerFrameRefc                 C   rM  )zQ
        Same as L{test_errorLogWithInnerFrameRef}, plus create a cycle.
        r%   Nc                  S   s,   t  } | | fdd | | _| d d S )Nc                 S   r   r   r,   )r!  r   r,   r,   r-   r   p  r   zdLogTests.test_errorLogWithInnerFrameCycle.<locals>._subErrorLogWithInnerFrameCycle.<locals>.<lambda>r7   )r   r   _dr=   r  r,   r,   r-   _subErrorLogWithInnerFrameCyclen  s   zRLogTests.test_errorLogWithInnerFrameCycle.<locals>._subErrorLogWithInnerFrameCyclerg   rO  )rL   rR  r,   r,   r-    test_errorLogWithInnerFrameCyclei  s   
z)LogTests.test_errorLogWithInnerFrameCyclec                 C   st   t  dd d t  |   | dt| j t	
| jd }|dus*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                 S   r   r   r,   r%  r,   r,   r-   r   ~  r   z.LogTests.test_errorLogNoRepr.<locals>.<lambda>r7   r   Nrb    Expected message starting with: )r   r   r=   rJ  rK  r  rG   rH   rD  r!   r]   rI   re   )rL   msgr  r,   r,   r-   test_errorLogNoRepry  s   zLogTests.test_errorLogNoReprc                 C   sn   d	dd}|  t   |   | dt| j t| jd }|dus'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.
        r%   Nc                  S   s(   t  } d| _| dd  | d d S )NTc                 S   r   r   r,   r%  r,   r,   r-   r     r   z?LogTests.test_errorLogDebugInfo.<locals>.doit.<locals>.<lambda>r7   )r   debugr   r=   r  r,   r,   r-   doit  s   z-LogTests.test_errorLogDebugInfo.<locals>.doitr   rT  z
(debug:  IrU  rg   )
rJ  rK  r  rG   rH   rD  r!   r]   rI   re   )rL   rY  rV  r  r,   r,   r-   test_errorLogDebugInfo  s   
zLogTests.test_errorLogDebugInfoc                 C   s   t  }|dd  |d g }g }||j|j | |g  | t|d |d t ~~~t	
  | |  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                 S   s   t tdS )Nzoop)r   r   rS   rs  r,   r,   r-   r         z3LogTests.test_chainedErrorCleanup.<locals>.<lambda>Nr7   r   )r   r   r=   r   rF   rG   rH   r  rp  rJ  rK  rH  )rL   r   r  rZ   r,   r,   r-   test_chainedErrorCleanup  s   
z!LogTests.test_chainedErrorCleanupc                    sV   t td}t  |dusJ | fdd d  }t  | |  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                    r   r(   r,   r  goodr,   r-   r     r   z6LogTests.test_errorClearedByChaining.<locals>.<lambda>)	r   r   rp  r   rE   rJ  rK  rG   rH  )rL   badr,   r^  r-   test_errorClearedByChaining  s   z$LogTests.test_errorClearedByChainingrg   )r4   r5   r6   rP   rp   r  r   r
   rc   r   rH  r  rL  rP  rS  rW  rZ  r]  ra  r,   r,   r,   r-   rC  3  s    



	





rC  c                   @   s@   e Zd ZdddZdddZdee ddfdd	Zdd
dZdS )DeferredListEmptyTestsr%   Nc                 C   
   d| _ d S rq   )callbackRanro   r,   r,   r-   rp     r&  zDeferredListEmptyTests.setUpc                 C   s   t g }|| j dS )zTesting empty DeferredList.N)r   r   cb_empty)rL   r   r,   r,   r-   testDeferredListEmpty  s   z,DeferredListEmptyTests.testDeferredListEmptyresc                 C   s   d| _ | g | d S r  )rd  rG   )rL   rg  r,   r,   r-   re    s   zDeferredListEmptyTests.cb_emptyc                 C   r"  )NzCallback was never run.)rI   rd  ro   r,   r,   r-   r    rT  zDeferredListEmptyTests.tearDownrg   )	r4   r5   r6   rp   rf  r   r1   re  r  r,   r,   r,   r-   rb    s
    

rb  c                   @   s   e Zd ZdeddfddZdddZddd	Zdd
dZdddZdddZ	dddZ
dddZdddZdddZdddZdddZdS )OtherPrimitivesTestsrX   r%   Nc                 C   s   |  j d7  _ d S r  counter)rL   rX   r,   r,   r-   _incr  rT  zOtherPrimitivesTests._incrc                 C   rc  rq   ri  ro   r,   r,   r-   rp     r&  zOtherPrimitivesTests.setUpc                    s  t  }| | j | |j | | jd | | j | |j | | jd |  | |j | | jd |  | 	|j | | jd | 
t|j t }t }t }d  d
dtdtdtf fdd}|j|||d}| |j |  | || | | j | |j | | jd || |  | | |j | | jd	 | |}|  |  t | tt jtj |  | 	|j d S )Nr7   r   resultValuereturnValuer%   c                    s   |  |S r(   r,   rl  rm  r  r,   r-   helper	     z-OtherPrimitivesTests.testLock.<locals>.helperrn  rb  r(   )r   acquirer   rk  rI   lockedrG   rj  releaser   r  r,  runr1   r   r=   r  r   r   r"   r   r   r   r   )rL   lockfirstUniquesecondUniquecontrolDeferredro  resultDeferredr   r,   r  r-   testLock  sL   

zOtherPrimitivesTests.testLockc                    s:   dt ddf fd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.
        r$   r%   Nc                    r`  NzUnexpected errback call!r   r+   ro   r,   r-   failOnErrback,	  rx   zHOtherPrimitivesTests.test_cancelLockAfterAcquired.<locals>.failOnErrback)r"   r   rq  rE   r   )rL   r}  ru  r   r,   ro   r-   test_cancelLockAfterAcquired&	  s
   
z1OtherPrimitivesTests.test_cancelLockAfterAcquiredc                 C   s0   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   rq  r   rO   r   r   )rL   ru  r   r,   r,   r-   test_cancelLockBeforeAcquired4	  s
   z2OtherPrimitivesTests.test_cancelLockBeforeAcquiredc                    s  d}t |}t  d dtdtd  f fdd}g }t }|j||d}||j || j | |g  | |  d  | 	|
  | | jd d| _tdd| D ]}| | j | | j| qZg d	tdd ffd
d}d	tdd ffdd}	| ||	}
|
  | dg | | j | | j| |  | | j|d  tdd| D ]}|  | | j|d  qd S )Nr  argr%   c                    s   |  S r(   r,   r  )rx  	helperArgr,   r-   ro  H	  rp  z2OtherPrimitivesTests.testSemaphore.<locals>.helperr  r7   r   r   c                    r`  rA  r   r   r   r,   r-   r   _	  rx   z0OtherPrimitivesTests.testSemaphore.<locals>.failc                    r`  r  r   r  r  r,   r-   r   b	  rx   z3OtherPrimitivesTests.testSemaphore.<locals>.succeedT)r   r   r1   rt  r   rF   rk  rG   r=   r   r  rj  r  rq  r   r   rs  )rL   Nsemro  r  uniqueObjectry  ir   r   r   r,   )rx  r  r   r-   testSemaphore@	  sB   
z"OtherPrimitivesTests.testSemaphorec                 C   s    |  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   rT  N)r  r  r   ro   r,   r,   r-   test_semaphoreInvalidTokenss	  s   z0OtherPrimitivesTests.test_semaphoreInvalidTokensc                    s<   dt ddf fd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.
        r$   r%   Nc                    r`  r{  r|  r+   ro   r,   r-   r}  	  rx   zMOtherPrimitivesTests.test_cancelSemaphoreAfterAcquired.<locals>.failOnErrbackr7   )r"   r   rq  rE   r   )rL   r}  r  r   r,   ro   r-   !test_cancelSemaphoreAfterAcquired{	  s
   
z6OtherPrimitivesTests.test_cancelSemaphoreAfterAcquiredc                 C   s2   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.
        r7   N)r   rq  r   rO   r   r   )rL   r  r   r,   r,   r-   "test_cancelSemaphoreBeforeAcquired	  s
   z7OtherPrimitivesTests.test_cancelSemaphoreBeforeAcquiredc              	   C   s  d\}}t ||}g }t|D ]
}| |j q| tj|j t|D ]}|| | 	|t
t|d  q&t|D ]}|||  | 	|t
t| q>| tj|jd  g }t|D ]}| |j | 	|t
t||| d  qat  }g }t|D ]
}| |j qt|D ]}|| q| 	|t
t| t dd}| tj|jd  t dd}| tj|j d S )N)r   r   r7   r   )size)backlog)r   r  getr   rF   r  r   QueueUnderflowputrG   listQueueOverflow)rL   r  Mqueuegottenr  r,   r,   r-   	testQueue	  s8   

 

zOtherPrimitivesTests.testQueuec                    sD   dt ddf fd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.
        r$   r%   Nc                    r`  r{  r|  r+   ro   r,   r-   r}  	  rx   zOOtherPrimitivesTests.test_cancelQueueAfterSynchronousGet.<locals>.failOnErrback)r"   r   r  rE   r  r   )rL   r}  r  r   r,   ro   r-   #test_cancelQueueAfterSynchronousGet	  s   

z8OtherPrimitivesTests.test_cancelQueueAfterSynchronousGetc                    sn   t     }|  |tj dtdtd f fd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.
        ignorer%   Nc                    s     d    jd S r(   )r  r  r   rV   )r  r  rL   r,   r-   r   	  s   
z9OtherPrimitivesTests.test_cancelQueueAfterGet.<locals>.cbr7   )r   r  r   rO   r   r   r1   r   r   rF   rG   rH   )rL   r   r   doner,   r  r-   test_cancelQueueAfterGet	  s   
z-OtherPrimitivesTests.test_cancelQueueAfterGetrg   )r4   r5   r6   r1   rk  rp   rz  r~  r  r  r  r  r  r  r  r  r,   r,   r,   r-   rh    s    


9


3



%rh  c                   @   s   e Zd ZdZdddZded fddZded fdd	Zded fd
dZdddZ	ded fddZ
ded fddZdddZdddZdS )DeferredFilesystemLockTestsz8
    Test the behavior of L{DeferredFilesystemLock}
    r%   Nc                 C   s    t  | _t|  | jd| _d S )N)	scheduler)r    clockr   mktempru  ro   r,   r,   r-   rp   	  s   z!DeferredFilesystemLockTests.setUpc                 C   s   | j jddS )zI
        Test that the lock can be acquired when no lock is held
        r7   timeout)ru  deferUntilLockedro   r,   r,   r-   test_waitUntilLockedWithNoLock	  r   z:DeferredFilesystemLockTests.test_waitUntilLockedWithNoLockc                 C   sB   |  | j  | jjdd}| |tj | jdgd  |S )zs
        Test that the lock can not be acquired when the lock is held
        for longer than the timeout.
        g      @r  r7   r(  )rI   ru  r  assertFailurer   TimeoutErrorr  pumpr   r,   r,   r-   %test_waitUntilLockedWithTimeoutLocked	  s
   zADeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutLockedc                    sf   dt ddf fdd}  j   jd jj  jjdd}||  jdgd  |S )	z|
        Test that a lock can be acquired while a lock is held
        but the lock is unlocked before our timeout.
        r$   r%   Nc                    s   |  tj  d d S )NzShould not have timed out)r  r   r  r   r+   ro   r,   r-   	onTimeout
  s   zVDeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutUnlocked.<locals>.onTimeoutr7   r(  r  )	r"   rI   ru  r  	callLaterunlockr  rE   r  )rL   r  r   r,   ro   r-   'test_waitUntilLockedWithTimeoutUnlocked	  s   
zCDeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutUnlockedc                 C   s   t |  }| |jt dS )zE
        Test that the default scheduler is set up properly.
        N)r   r  rG   
_schedulerr   rL   ru  r,   r,   r-   test_defaultScheduler
  s   z1DeferredFilesystemLockTests.test_defaultSchedulerc                 C   sN   | 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.
        r7   )	ru  r  r  r  r  r  r   AlreadyTryingToLockErroradvancer   r,   r,   r-   test_concurrentUsage
  s   


z0DeferredFilesystemLockTests.test_concurrentUsagec                    sZ   dt dtd f fdd} j   jd jj  j }||  jd |S )zO
        Test that a DeferredFilesystemLock can be used multiple times
        rt  r%   Nc                    s    j    j  }|S r(   )ru  r  r  )rt  r   ro   r,   r-   lockAquired0
  s   

zDDeferredFilesystemLockTests.test_multipleUsages.<locals>.lockAquiredr7   )	r1   r   ru  r  r  r  r  r   r  )rL   r  r   r,   ro   r-   test_multipleUsages+
  s   


z/DeferredFilesystemLockTests.test_multipleUsagesc                 C   s^   | j    | j  }| j j}|dus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)
ru  r  _tryLockCallr   r   activer   r   r   r   )rL   r:   tryLockCallr,   r,   r-   test_cancelDeferUntilLocked?
  s   

z7DeferredFilesystemLockTests.test_cancelDeferUntilLockedc                 C   sb   | j    | j jdd}| j j}|dus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.
        r7   r  N)
ru  r  _timeoutCallr   r   r  r   r   r   r   )rL   r:   timeoutCallr,   r,   r-   &test_cancelDeferUntilLockedWithTimeoutN
  s   
zBDeferredFilesystemLockTests.test_cancelDeferUntilLockedWithTimeoutrg   )r4   r5   r6   rP   rp   r   r  r  r  r  r  r  r  r  r,   r,   r,   r-   r  	  s    


r  vtc                 C   r  )zj
    Private function to be used to pass as an alternate onTimeoutCancel value
    to timeoutDeferred
    
OVERRIDDENr,   )r  r  r,   r,   r-   _overrideFunc^
  s   r  c                   @   s   e Zd ZdZd(ddZd(ddZd(dd	Zd(d
dZd(ddZd(ddZ	d(ddZ
d(ddZd(ddZd(ddZd(ddZd(ddZd(ddZd(ddZd(d d!Zd(d"d#Zd(d$d%Zd(d&d'ZdS ))DeferredAddTimeoutTestsz7
    Tests for the function L{Deferred.addTimeout}
    r%   Nc                 C   sB   t  }|dt  |d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                 S   r  )Nr  r,   r   r,   r,   r-   r   r
  r   z?DeferredAddTimeoutTests.test_timeoutChainable.<locals>.<lambda>Nr  )r   
addTimeoutr    r   r=   rG   r   r   r,   r,   r-   test_timeoutChainablek
  s
   
z-DeferredAddTimeoutTests.test_timeoutChainablec                    s   t  }t }|d| t d dtdtd f fd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(  Nr  r%   c                       |  S r(   r,   r?  dCallbackedinnerDeferredr,   r-   
onCallback
  rp  zKDeferredAddTimeoutTests.test_successResultBeforeTimeout.<locals>.onCallbackr)  )
r    r   r  rc   r   r=   assertIsNotrG   r  r   rL   r  r   r  r,   r  r-   test_successResultBeforeTimeoutv
  s   


z7DeferredAddTimeoutTests.test_successResultBeforeTimeoutc                    s   t  }t }|jd|td t d dtdtd f fd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(  onTimeoutCancelNr  r%   c                    r  r(   r,   r?  r  r,   r-   r  
  rp  zQDeferredAddTimeoutTests.test_successResultBeforeTimeoutCustom.<locals>.onCallbackr)  )r    r   r  r  rc   r   r=   r  rG   r  r   r  r,   r  r-   %test_successResultBeforeTimeoutCustom
  s   


z=DeferredAddTimeoutTests.test_successResultBeforeTimeoutCustomc                    s   t  }t }|d| t d td}dtdtd f fdd}|| ||  dus3J |  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   r$   r%   c                    r  r(   r,   r+   
dErrbackedr  r,   r-   	onErrback
  rp  zDDeferredAddTimeoutTests.test_failureBeforeTimeout.<locals>.onErrbackr)  )r    r   r  r  r"   rE   r   r   rV   rK   r  r   rL   r  r   rW   r  r,   r  r-   test_failureBeforeTimeout
  s   


z1DeferredAddTimeoutTests.test_failureBeforeTimeoutc                    s   t  }t }|jd|td t d td}dtdtd f fdd}|| ||  dus5J |  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(  r  Nr   r$   r%   c                    r  r(   r,   r+   r  r,   r-   r  
  rp  zJDeferredAddTimeoutTests.test_failureBeforeTimeoutCustom.<locals>.onErrbackr)  )r    r   r  r  r  r"   rE   r   r   rV   rK   r  r   r  r,   r  r-   test_failureBeforeTimeoutCustom
  s   


z7DeferredAddTimeoutTests.test_failureBeforeTimeoutCustomc                 C   s>   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   r  r   r  r   r   r  rL   r  r   r,   r,   r-   test_timedOut   s   

z%DeferredAddTimeoutTests.test_timedOutc                 C   sF   t  }t }|jd|td | | |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(  r  r)  r  Nr    r   r  r  r   r  rG   r   r  r,   r,   r-   test_timedOutCustom  s   

z+DeferredAddTimeoutTests.test_timedOutCustomc                 C   sH   t  }td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                 S   s
   |  dS )NI was cancelled!r<   rG  r,   r,   r-   r   &  r   zLDeferredAddTimeoutTests.test_timedOutProvidedCancelSuccess.<locals>.<lambda>r(  r)  r  N)r    r   r  r   r  rG   r   r  r,   r,   r-   "test_timedOutProvidedCancelSuccess  s   

z:DeferredAddTimeoutTests.test_timedOutProvidedCancelSuccessc                    s\   t  }td t fd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   rG  rW   r,   r-   r   6  r   zLDeferredAddTimeoutTests.test_timedOutProvidedCancelFailure.<locals>.<lambda>r(  r)  N)	r    r  r   r  r   r  r   rV   rK   )rL   r  r   r$   r,   r  r-   "test_timedOutProvidedCancelFailure.  s   

z:DeferredAddTimeoutTests.test_timedOutProvidedCancelFailurec                    s   t  }t }|d| t d dtdtd f fdd}|| |   dus.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(  Nr$   r%   c                    r  r(   r,   r+   	dCanceledr  r,   r-   r  O  rp  zCDeferredAddTimeoutTests.test_cancelBeforeTimeout.<locals>.onErrbackr)  )r    r   r  r"   rE   r   r   rV   r   r   r   r  r   rL   r  r   r  r,   r  r-   test_cancelBeforeTimeout?  s   

z0DeferredAddTimeoutTests.test_cancelBeforeTimeoutc                    s   t  }t }|jd|td t d dtdtd f fdd}|| |   dus0J |  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(  r  Nr$   r%   c                    r  r(   r,   r+   r  r,   r-   r  r  rp  zIDeferredAddTimeoutTests.test_cancelBeforeTimeoutCustom.<locals>.onErrbackr)  )r    r   r  r  r"   rE   r   r   rV   r   r   r   r  r   r  r,   r  r-   test_cancelBeforeTimeoutCustomb  s   

z6DeferredAddTimeoutTests.test_cancelBeforeTimeoutCustomc                 C   sL   t  }tdd }|jd|td | | |d | d| | dS )zu
        A custom translation function can handle a L{Deferred} with a
        custom cancellation function.
        c                 S   s   |  tdS )Nr  )r   r  rG  r,   r,   r-   r     r\  zVDeferredAddTimeoutTests.test_providedCancelCalledBeforeTimeoutCustom.<locals>.<lambda>r(  r  r)  r  Nr  r  r,   r,   r-   ,test_providedCancelCalledBeforeTimeoutCustom  s   

zDDeferredAddTimeoutTests.test_providedCancelCalledBeforeTimeoutCustomc                    s   t  }t }d dtdtf fdd}|| |d| |d  dus)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.
        Nr$   r%   c                       |  | S r(   r,   r+   r  r,   r-   r     rp  zGDeferredAddTimeoutTests.test_errbackAddedBeforeTimeout.<locals>.errbackr(  r)  )r    r   r"   rE   r  r  r   rK   r   r   r   r  rL   r  r   r   r,   r  r-   test_errbackAddedBeforeTimeout  s   

z6DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutc                    s|   t  }t }d dtddf fdd}|| |d| |d  dus)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.
        Nr$   r%   c                    s   |  |  tj d S r(   )r  r   r   r+   r  r,   r-   r     s   z]DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellation.<locals>.errbackr(  r)  )r    r   r"   rE   r  r  r   rK   r   r   r   r  r,   r  r-   4test_errbackAddedBeforeTimeoutSuppressesCancellation  s   

zLDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationc                    s   t  }t }d dtdtf fdd}|| |d|t |d  dus*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}.
        Nr$   r%   c                    r  r(   r,   r+   r  r,   r-   r     rp  zMDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutCustom.<locals>.errbackr(  r)  r  r    r   r"   rE   r  r  r  r   rK   r   r   rG   r   r  r,   r  r-   $test_errbackAddedBeforeTimeoutCustom  s   

z<DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutCustomc                    s   t  }t }d dtddf fdd}|| |d|t |d  dus*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}.
        Nr$   r%   c                    s   |  d S r(   r,   r+   r  r,   r-   r        zcDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationCustom.<locals>.errbackr(  r)  r  r  r  r,   r  r-   :test_errbackAddedBeforeTimeoutSuppressesCancellationCustom  s   

zRDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationCustomc                    sv   t  }dtfdd}d dtdtf fd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                    r  r(   r<   r  r  r,   r-   r     r   zSDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout.<locals>.<lambda>NrK   r%   c                    r  r(   r,   rK   r  r,   r-   r=     rp  zSDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout.<locals>.callbackr(  r)  )	r    r   rc   r   r  r  rG   rV   r   rL   r  r   r=   r,   r  r   r-   )test_callbackAddedToCancelerBeforeTimeout  s   

zADeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutc                    sz   t  }dtfdd}d dtdtf fdd}|| |jd	|td
 |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                    r  r(   r<   r  r  r,   r-   r   )  r   zYDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom.<locals>.<lambda>NrK   r%   c                    r  r(   r,   r  r  r,   r-   r=   -  rp  zYDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom.<locals>.callbackr(  r  r)  r  )	r    r   rc   r   r  r  r  rG   r   r  r,   r  r-   /test_callbackAddedToCancelerBeforeTimeoutCustom  s   	

zGDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustomrg   )r4   r5   r6   rP   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r,   r,   r,   r-   r  f
  s(    


!
"
#
$




#
#




r  c                   @   s8   e Zd ZdZdddZdddZddd	Zdd
dZdS )EnsureDeferredTestsz&
    Tests for L{ensureDeferred}.
    r%   Nc                 C   s   t  }t|}| || dS )zK
        L{ensureDeferred} will pass through a Deferred unchanged.
        N)r   r   rV   r   r,   r,   r-   test_passesThroughDeferredsA  s   z/EnsureDeferredTests.test_passesThroughDeferredsc                 C   s:   |  tj td W d   dS 1 s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   ro   r,   r,   r-   'test_willNotAllowNonDeferredOrCoroutineI  s   
"z;EnsureDeferredTests.test_willNotAllowNonDeferredOrCoroutinec                 C   sP   dt fdd}| }| |tj t|}| |t | |}| |d dS )zM
        L{ensureDeferred} will turn a coroutine into a L{Deferred}.
        r%   c                     s   t d} | I d H }|S Nr  )r   r   r   rg  r,   r,   r-   rt  V  s   

z=EnsureDeferredTests.test_ensureDeferredCoroutine.<locals>.runr  N)rc   r   typesCoroutineTyper   r   r   rG   rL   rt  r   r   rg  r,   r,   r-   test_ensureDeferredCoroutineQ  s   
z0EnsureDeferredTests.test_ensureDeferredCoroutinec                 C   s^   dt tt dtf fdd}| }| |tj t|}| |t | |}| |d dS )zX
        L{ensureDeferred} will turn a yield-from coroutine into a L{Deferred}.
        r%   Nc                  s   s     t d} tt| E d H }|S r  )r   r   r   rc   r  r,   r,   r-   rt  l  s   
z=EnsureDeferredTests.test_ensureDeferredGenerator.<locals>.runr  )	r   r   rc   r   r  GeneratorTyper   r   rG   r  r,   r,   r-   test_ensureDeferredGeneratorg  s   
z0EnsureDeferredTests.test_ensureDeferredGeneratorrg   )r4   r5   r6   rP   r  r  r  r  r,   r,   r,   r-   r  <  s    


r  c                   @   s   e Zd ZdZdddZdS )TimeoutErrorTestsz1
    L{twisted.internet.defer} timeout code.
    r%   Nc                 C   sh   t  }t| | |tj | | jg}| t|d | 	|d d t
 | |d d d dS )zB
        L{twisted.internet.defer.timeout} is deprecated.
        r7   r   r  r  z~twisted.internet.defer.timeout was deprecated in Twisted 17.1.0; please use twisted.internet.defer.Deferred.addTimeout insteadN)r   r   r  r  r  r  test_deprecatedTimeoutrG   rH   rV   r  )rL   r:   warningsShownr,   r,   r-   r     s   

z(TimeoutErrorTests.test_deprecatedTimeoutrg   )r4   r5   r6   rP   r   r,   r,   r,   r-   r  ~  s    r  loopc                 C   s   |  | 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  r,   r,   r-   callAllSoonCalls  s   r  c                   @   sR   e Zd ZdddZdddZdddZdd	d
ZdddZdddZdddZ	dS )DeferredFutureAdapterTestsr%   Nc                 C   s^   t  }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   asFuturerG   r  r=   r  r   rX   )rL   r   r  aFuturer,   r,   r-   test_asFuture  s   

z(DeferredFutureAdapterTests.test_asFuturec                    st   d dt t ddf fdd}t |}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%   Nc                    s   d d S r  r,   r  rc  r,   r-   canceler  r  zFDeferredFutureAdapterTests.test_asFutureCancelFuture.<locals>.canceler)r   r1   r   r	  r   r  rI   rG   r   r  r   rX   rL   r  r   r  r
  r,   rc  r-   test_asFutureCancelFuture  s   

z4DeferredFutureAdapterTests.test_asFutureCancelFuturec                 C   sb   dt t ddfdd}t |}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%   Nc                 S   s   |  d d S )Nr  r<   r  r,   r,   r-   r    rx   zGDeferredFutureAdapterTests.test_asFutureSuccessCancel.<locals>.canceler)r   r1   r   r	  r   r  rG   r   r  r   rX   r  r,   r,   r-   test_asFutureSuccessCancel  s   
z5DeferredFutureAdapterTests.test_asFutureSuccessCancelc                 C   sL   t  }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  rX   )rL   r   
theFailurer  futurer,   r,   r-   test_asFutureFailure  s   


z/DeferredFutureAdapterTests.test_asFutureFailurec                 C   sL   t  }t|d}t|}| | |d t| | | |d dS )zx
        L{Deferred.fromFuture} returns a L{Deferred} that fires
        when the given L{asyncio.Future} does.
        r     N)	r   r   r   
fromFuturer   
set_resultr  rG   r   )rL   r  r
  r   r,   r,   r-   test_fromFuture  s   



z*DeferredFutureAdapterTests.test_fromFuturec                 C   sL   t  }t|d}t|}|  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   rX   r   r  rL   r  	cancelledr   r,   r,   r-   test_fromFutureFutureCancelled  s   

z9DeferredFutureAdapterTests.test_fromFutureFutureCancelledc                 C   s\   t  }t|d}t|}|  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  rG   r  r  r   rX   r   r  r  r,   r,   r-    test_fromFutureDeferredCancelled  s   

z;DeferredFutureAdapterTests.test_fromFutureDeferredCancelledrg   )
r4   r5   r6   r  r  r  r  r  r  r  r,   r,   r,   r-   r    s    





r  c                   @   sV   e Zd Zedu r
dZdddZedddZeddd	Zedd
dZ	dddZ
dS )CoroutineContextVarsTestsNzcontextvars is not availabler%   c                    s   t   tdd t fdd t fdd dd  tjdttt	 t	df f fd	d
tjdttt	 t	df f fdd}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.
        testvarr7   c                    r\  ra  setr   varr,   r-   r     r   zDCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.<lambda>c                    r\  N   r  r   r!  r,   r-   r   "  r   c                 S      dd S r   r,   r   r,   r,   r-   r   #  r   r%   Nc                  3   s,    t  }  d| jd | V  d d S Nr7   Trb  r   r  r=   r   r  r  r"  r,   r-   yieldingDeferred%  s
   zLCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.yieldingDeferredc                   3   s      d tdV    d  djd V    d  djd zV  W td tyK     d Y nw  V    d td d S )Nr   r7   T???? should have failed)rG   r  r   r   r  r=   rp  rm  r,   r  mutatingDeferredmutatingDeferredThatFailsrL   r"  r)  r,   r-   testFunction-  s"   	zHCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.testFunctionr   T)r    contextvars
ContextVarr   r   r   r   r  r   r   r  rG   r   rL   r.  r   r,   r+  r-   test_withInlineCallbacks  s$   

$,
&


z2CoroutineContextVarsTests.test_withInlineCallbacksc              	      s   t    4 I dH * |  j   }| fdd |  j | |j W d  I dH  n1 I dH s:w   Y  | |j |I dH  |  j dS )zQ
        L{DeferredLock} can be used as an asynchronous context manager.
        Nc                          S r(   rs  r   ru  r,   r-   r   k  r   z>CoroutineContextVarsTests.test_asyncWithLock.<locals>.<lambda>)r   rI   rr  rq  r   r   rd  r   r,   r5  r-   test_asyncWithLockb  s   (
z,CoroutineContextVarsTests.test_asyncWithLockc              
      s>  t d  4 I dH  |  jd  4 I dH - |  jd   }  }|  jd | |j | |j W d  I dH  n1 I dH sLw   Y  |  jd | |j | fdd | fdd |I dH  |I dH  |  jd W d  I dH  n1 I dH sw   Y  |  jd dS )	z^
        L{DeferredSemaphore} can be used as an asynchronous context
        manager.
        rb  Nr   r7   r   c                    r3  r(   r4  r   r  r,   r-   r     r   zCCoroutineContextVarsTests.test_asyncWithSemaphore.<locals>.<lambda>c                    r3  r(   r4  r   r7  r,   r-   r     r   )r   rG   tokensrq  rI   rd  r   r   r   r,   r7  r-   test_asyncWithSemaphorer  s*   (

(z1CoroutineContextVarsTests.test_asyncWithSemaphorec              
      s|   t  }| td$ |4 I dH  | |j td1 I dH s"w   Y  W d   n1 s1w   Y  | |j dS )zw
        C{DeferredLock} correctly propagates exceptions when
        used as an asynchronous context manager.
        zsome specific exceptionN)r   assertRaisesRegexprp  rI   rr  r   r  r,   r,   r-   test_asyncWithLockException  s   z5CoroutineContextVarsTests.test_asyncWithLockExceptionc                    s   t  tdd t fdd t fdd dd  dfd	d
 dtf fd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  r7   c                    r\  ra  r  r   r!  r,   r-   r     r   zJCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.<lambda>c                    r\  r#  r  r   r!  r,   r-   r     r   c                 S   r%  r   r,   r   r,   r,   r-   r     r   r%   Nc                     s0   t  }  d| jd | I d H  d d S r&  r'  r  r(  r,   r-   asyncFuncAwaitingDeferred  s
   
z[CoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.asyncFuncAwaitingDeferredc                      s     d tdI d H    d djd I d H    d djd z
I d H  W td tyQ     d Y nw   I d H    d dS )Nr   r7   r   Tr*  rb  )rG   r  r   r   r  r=   rp  r,   r<  r  r,  r-  rL   r"  r,   r-   r.    s"   
zNCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.testFunctionr   Trg   )r    r/  r0  r   r   r   r   r   r  rG   r   r1  r,   r=  r-   test_contextvarsWithAsyncAwait  s    


%



z8CoroutineContextVarsTests.test_contextvarsWithAsyncAwaitrg   )r4   r5   r6   r/  skipr2  r2   r6  r9  r;  r>  r,   r,   r,   r-   r    s    
Or  )OrP   r/   rJ  r  r  r  r  asyncior   r   r   r   typingr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   twisted.internetr   r   twisted.internet.deferr   r   r   r   r   r   r   r   r   r   twisted.internet.taskr    twisted.pythonr!   twisted.python.failurer"   twisted.trialr#   r/  ImportErrorr2   rp  r3   r1   r9   rc   r?   r@   rB   rD   rR   SynchronousTestCaserh   r  r  r   rC  rb  rh  r  floatr  r  r  r  r  r  r  r,   r,   r,   r-   <module>   s   @0


-           >>    # |}   YBn