
    \d                    f   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mZ ddl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	l0m1Z1 dd
l2m3Z3 ddl4m5Z5 erddl6Z6n	 ddl6Z6n# e7$ r dZ6Y nw xY wdedee$e,         ee,f         f         dede$e,         f         fdZ8 G d de9          Z:de;de;de3fdZ<de$e=         ddfdZ> ede9          Z? G d d          Z@ G d de5jA                  ZB G d de5jC        e@          ZD G d d e5jC                  ZE G d! d"e5jC                  ZF G d# d$e5jC                  ZG G d% d&e5jC                  ZH G d' d(e5jC                  ZI G d) d*e5jC        e@          ZJ G d+ d,e5jA                  ZKd-e;d.eLde=fd/ZM G d0 d1e5jC                  ZN G d2 d3e5jA                  ZO G d4 d5e5jA        e@          ZPd6eddfd7ZQ G d8 d9e5jA                  ZR G d: d;e5jA                  ZSd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NoReturnOptionalTupleTypeTypeVarUnioncast)assert_thatequal_tois_)deferreactor)
DeferredDeferredFilesystemLockDeferredListDeferredLockDeferredQueueDeferredSemaphore_DeferredListResultListT_DeferredListSingleResultT_DeferredResultTensureDeferred)Clock)log)Failure)unittestf.returnc                      t          j                   dt          dt          dt          t                   f fd            }|S )Nargskwargsr*   c                  <     | i |}t          j        |          S N)r   fromCoroutine)r,   r-   coror)   s      7lib/python3.11/site-packages/twisted/test/test_defer.pywrapperz!ensuringDeferred.<locals>.wrapperB   s(    q$!&!!%d+++    )	functoolswrapsobjectr   r#   )r)   r3   s   ` r2   ensuringDeferredr8   ?   sZ     _Q,v , ,H=M4N , , , , , , Nr4   c                       e Zd ZdS )GenericErrorN__name__
__module____qualname__ r4   r2   r:   r:   J   s        Dr4   r:   r,   r-   c                  L    	 ddz   n# t           $ r t          | i |}Y nw xY w|S )z
    Make a L{Failure} of a divide-by-zero error.

    @param args: Any C{*args} are passed to Failure's constructor.
    @param kwargs: Any C{**kwargs} are passed to Failure's constructor.
       r   )BaseExceptionr'   )r,   r-   r)   s      r2   getDivisionFailurerC   N   sH    %	A % % %T$V$$%Hs    !!deferredc                 0    |                      d           dS )z
    A fake L{Deferred} canceller which callbacks the L{Deferred}
    with C{str} "Callback Result" when cancelling it.

    @param deferred: The cancelled L{Deferred}.
    Callback ResultNcallbackrD   s    r2   fakeCallbackCancellerrJ   \   s     '(((((r4   _ExceptionT)boundc                   >    e Zd ZdZdee         dee         defdZdS )ImmediateFailureMixinz+
    Add additional assertion methods.
    rD   	exceptionr*   c                 P   t          t          j        |           }g }|                    |j                   |                    t          |          d           |                    |d                             |                     t          t          |d         j
                  S )z
        Assert that the given Deferred current result is a Failure with the
        given exception.

        @return: The exception instance in the Deferred.
        rA   r   )r   r(   TestCase
addErrbackappendassertEquallen
assertTruecheckrK   value)selfrD   rO   testCasefailuress        r2   assertImmediateFailurez,ImmediateFailureMixin.assertImmediateFailuren   s     )400"$HO,,,S]]A...HQK--i88999K!!2333r4   N)	r<   r=   r>   __doc__r   r   r   rK   r\   r?   r4   r2   rN   rN   i   sS         4 426{2C4	4 4 4 4 4 4r4   rN   c                   *    e Zd ZdZddZddZddZdS )	UtilTestsz&
    Tests for utility functions.
    r*   Nc                     t          t                                }t          j        |          }|                     t                     |                     ||           dS )z<
        L{defer.logError} returns the given error.
        N)r'   RuntimeErrorr   logErrorflushLoggedErrorsassertIs)rY   errorresults      r2   test_logErrorReturnsErrorz#UtilTests.test_logErrorReturnsError   sR     ''&&|,,,eV$$$$$r4   c                     t          t                                }t          j        |           |                     t                    }|                     ||g           dS )z9
        L{defer.logError} logs the given error.
        N)r'   ra   r   rb   rc   rT   )rY   re   errorss      r2   test_logErrorLogsErrorz UtilTests.test_logErrorLogsError   sV     ''u''55%)))))r4   c                 d   g dt           t          t          f         ddffd}t          j        |           t          t                                }t          j        |           | 	                    t                     | 
                    d                             d                     dS )zR
        The text logged by L{defer.logError} has no repr of the failure.
        	eventDictr*   Nc                 b    t          j        |           }|J                     |           d S r/   )r&   textFromEventDictrS   )rl   textoutputs     r2   emitz4UtilTests.test_logErrorLogsErrorNoRepr.<locals>.emit   s7    (33D###MM$r4   r   Unhandled Error
Traceback )r   strr   r&   addObserverr'   ra   r   rb   rc   rV   
startswith)rY   rq   re   rp   s      @r2   test_logErrorLogsErrorNoReprz&UtilTests.test_logErrorLogsErrorNoRepr   s     	 DcN 	 t 	  	  	  	  	  	 
 	''u|,,,q	,,-JKKLLLLLr4   r*   N)r<   r=   r>   r]   rg   rj   rv   r?   r4   r2   r_   r_      s`         % % % %* * * *M M M M M Mr4   r_   c                      e Zd ZdRdZdededefdZdededdfdZdededdfdZ	dRd	Z
dRd
ZdRdZdRdZdRdZdRdZdRdZdRdZdRdZdRdZdRdZdRdZdRdZdRdZdRdZdRdZdRdZdRdZdRdZdRdZdRdZdRdZdRdZ dRd Z!dRd!Z"dRd"Z#dRd#Z$dRd$Z%dRd%Z&dRd&Z'dRd'Z(dRd(Z)dRd)Z*dRd*Z+dRd+Z,dRd,Z-dRd-Z.dRd.Z/dRd/Z0dRd0Z1dRd1Z2dRd2Z3dRd3Z4dRd4Z5dRd5Z6dRd6Z7dRd7Z8dRd8Z9dRd9Z:dRd:Z;dRd;Z<dRd<Z=dRd=Z>dRd>Z?dRd?Z@dRd@ZAdRdAZBdRdBZCdRdCZDdRdDZEdRdEZFdRdFZGdRdGZHdRdHZIdRdIZJdRdJZKdRdKZLdRdLZMdRdMZNdRdNZOdRdOZPdRdPZQdRdQZRdS )SDeferredTestsr*   Nc                     d | _         d | _        d | _        |                     t          j        t	          j                               d S r/   )callbackResultscallback2ResultserrbackResults
addCleanupr   setDebugginggetDebuggingrY   s    r2   setUpzDeferredTests.setUp   sQ      	
  	
  	
 	*E,>,@,@AAAAAr4   r,   r-   c                 $    ||f| _         |d         S Nr   r{   rY   r,   r-   s      r2   	_callbackzDeferredTests._callback   s    #V|Awr4   c                     ||f| _         d S r/   r|   r   s      r2   
_callback2zDeferredTests._callback2   s     $fr4   c                     ||f| _         d S r/   r}   r   s      r2   _errbackzDeferredTests._errback   s    "Flr4   c                     t                      }|                    | j                   |                    d           |                     | j                   |                     | j        di f           d S Nhellor   r   addCallbackr   rH   assertIsNoner}   rT   r{   rY   rD   s     r2   testCallbackWithoutArgsz%DeferredTests.testCallbackWithoutArgs   sp    "***T^,,,'"""$-...-
B/?@@@@@r4   c                     t                      }|                    | j        d           |                    d           |                     | j                   |                     | j        di f           d S )Nworldr   )r   r   r   r   s     r2   testCallbackWithArgsz"DeferredTests.testCallbackWithArgs   ss    "***T^W555'"""$-...-0BB/GHHHHHr4   c                     t                      }|                    | j        d           |                    d           |                     | j                   |                     | j        dddif           d S )Nr   )r   r   r   r   r   s     r2   testCallbackWithKwArgsz$DeferredTests.testCallbackWithKwArgs   sz    "***T^7;;;'"""$-...-
Wg<N/OPPPPPr4   c                 \   t                      }|                    | j                   |                    | j                   |                    d           |                     | j                   |                     | j        di f           |                     | j	        di f           d S r   )
r   r   r   r   rH   r   r}   rT   r{   r|   r   s     r2   testTwoCallbackszDeferredTests.testTwoCallbacks   s    "***T^,,,T_---'"""$-...-
B/?@@@.R0@AAAAAr4   c                 P   t          d          }t                      }|                    | j        t	          t
          dt          f         d                     |                    |           |                    | j	                   | 
                    | j                   | j        J |                     t          | j        d                   d           |                     | j        d         d         j        |           |                     | j        d         i            dS )zQ
        If given None for an errback, addCallbacks uses a pass-through.
        oopsie.Nr   rA   )r:   r   addCallbacksr   r   r	   r7   errbackrR   r   r   r{   r}   rT   rU   rX   rY   re   rD   s      r2   test_addCallbacksNoneErrbackz*DeferredTests.test_addCallbacksNoneErrback   s    X&&#+::dnd8CK3H$.O.OPPPDM***$.///"...T0344a888,Q/28%@@@,Q/44444r4   c           
         t                      }|                    | j        | j        t	          t
          t                   d          t	          t          t          t          f         d          di            |	                    d           | 
                    | j                   |                     | j        di f           dS )zR
        If given None as a callback args and kwargs, () and {} are used.
        Nr?   r   r   )r   r   r   r   r   r   r7   r   rs   rH   r   r}   rT   r{   r   s     r2   !test_addCallbacksNoneCallbackArgsz/DeferredTests.test_addCallbacksNoneCallbackArgs   s     #+**NMv%%f%t,,	
 	
 	
 	'"""$-...-
B/?@@@@@r4   c                    t          d          }t                      }|                    | j        | j        di t          t          t                   d          t          t          t          t          f         d                     |
                    |           |                    | j                   |                     | j                   | j        J |                     t!          | j        d                   d           |                     | j        d         d         j        |           |                     | j        d         i            dS )zQ
        If given None as a errback args and kwargs, () and {} are used.
        r   r?   Nr   rA   )r:   r   r   r   r   r   r   r7   r   rs   r   rR   r   r{   r}   rT   rU   rX   r   s      r2    test_addCallbacksNoneErrbackArgsz.DeferredTests.test_addCallbacksNoneErrbackArgs  s+    X&&#+::NMv%%f%t,,	
 	
 	
 	DM***$.///"...T0344a888,Q/28%@@@,Q/44444r4   c                 $   t           t          t          t          t          t
          f         f                  }t                      }t                      }t                      }t          |||g          }g }|fd|d|dd fd}dt
          dd f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
resultListrf   r*   c                 0    |                     |            d S r/   )extendr   rf   s     r2   cbz*DeferredTests.testDeferredList.<locals>.cb"  s    MM*%%%%%r4   errc                     d S r/   r?   r   s    r2   catchz-DeferredTests.testDeferredList.<locals>.catch%      4r4   123r   rA      )r   r   boolr   rs   r'   r   r   r   rH   rR   r   r:   rT   r   rX   r   SUCCESSFAILURE)	rY   
ResultListdefr1defr2defr3dlrf   r   r   s	            r2   testDeferredListzDeferredTests.testDeferredList  s   %eCL&9 9:;
'zz'zz'zz5%/00<B 	& 	&: 	&z 	&t 	& 	& 	& 	&	w 	4 	 	 	 	 	Bs 	l3''(((sq	1s41#>#>#DEEFq		 mS!EM3#7%-9MN	
 	
 	
 	
 	
r4   c                 H   g }|fdt           dt          t                    dd fd}t          g           }|                    |           |                     |g g           g |d d <   t          g d          }|                    |           |                     |g            d S )Nr   rf   r*   c                 0    |                     |            d S r/   rS   r   s     r2   r   z/DeferredTests.testEmptyDeferredList.<locals>.cb@  s     MM*%%%%%r4   TfireOnOneCallback)r!   r   r   r   rT   )rY   rf   r   dl1dl2s        r2   testEmptyDeferredListz#DeferredTests.testEmptyDeferredList=  s    13 6<	& 	&0	&12	& 	& 	& 	& 	& 3?r2B2B"&&&qqq	4@$5
 5
 5
 	$$$$$r4   c                 <   t                      }t                      }t                      }t          |||gd          }g }|                    |j                   dt          dd fd}|                    |           |                    d           |                     |g            |                    t          d                     |                     t          |          d           |d	         }| 
                    t          |j        t          j                  d
|j                   |j        }|                     |j        j        t                     |                     |j        j        j        d           |                     |j        d           d S )NTfireOnOneErrbackr   r*   c                     d S r/   r?   r   s    r2   r   z;DeferredTests.testDeferredListFireOnOneError.<locals>.catch[  r   r4   r   	from def2rA   r   zFissubclass(aFailure.type, defer.FirstError) failed: failure's type is )r   )r   r   rR   rS   r'   rH   rT   r   r:   rU   rV   
issubclasstyper   
FirstErrorrX   
subFailurer,   index)	rY   r   r   r   r   rf   r   aFailure
firstErrors	            r2   testDeferredListFireOnOneErrorz,DeferredTests.testDeferredListFireOnOneErrorQ  s   'zz'zz'zz5%/$GGG "
fm$$$	w 	4 	 	 	 	 	 	s$$$ 	l;//000Va((( !9 	x}e&6777&.mm6	
 	
 	
 ^
 	.3\BBB.49>JJJ)1-----r4   c                    t                      }t          |g          }g }|                    |j                   g }|                    |j                   |                    t          d                     |                     d|d         j        j	        d                    |                     dt          |                     |d         d         }|J |                     d|d         j        j	        d                    d S )NBangr   rA   )r   r   rR   rS   r   r   r:   rT   rX   r,   rU   rY   d1r   	errorTrapresultListsfirstResults         r2   !testDeferredListDontConsumeErrorsz/DeferredTests.testDeferredListDontConsumeErrorsz  s    %ZZ2$#%	
i&'''68
{)***


<''(((1!3!8!;<<<C,,---!!nQ'&&&Q!5!:1!=>>>>>r4   c                    t                      }t          |gd          }g }|                    |j                   g }|                    |j                   |                    t          d                     |                     g |           |                     dt          |                     |d         d         }|J |                     d|d         j	        j
        d                    d S )NTconsumeErrorsr   rA   r   )r   r   rR   rS   r   r   r:   rT   rU   rX   r,   r   s         r2   testDeferredListConsumeErrorsz+DeferredTests.testDeferredListConsumeErrors  s    %ZZ2$d333#%	
i&'''68
{)***


<''(((Y'''C,,---!!nQ'&&&Q!5!:1!=>>>>>r4   c                 R   t                      }t                      }|                    t          d                     t          ||gd          }g }|                    |j                   |                     dt          |                     |                    d            d S )Nr   Tr   rA   c                     d S r/   r?   es    r2   <lambda>zWDeferredTests.testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds.<locals>.<lambda>       r4   )r   r   r:   r   rR   rS   rT   rU   rY   r   d2r   rf   s        r2   7testDeferredListFireOnOneErrorWithAlreadyFiredDeferredszEDeferredTests.testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds  s    %ZZ%ZZ


<''((( 2r(T::: "
fm$$$CKK(((
nn%%%%%r4   c                 x   t                      }t                      }|                    t          d                     |                    d           t	          ||g          }g }|                    |j                   |                     dt          |                     |	                    d            d S )Nr   r   rA   c                     d S r/   r?   r   s    r2   r   zIDeferredTests.testDeferredListWithAlreadyFiredDeferreds.<locals>.<lambda>  r   r4   )
r   r   r:   rH   r   r   rS   rT   rU   rR   r   s        r2   )testDeferredListWithAlreadyFiredDeferredsz7DeferredTests.testDeferredListWithAlreadyFiredDeferreds  s    $JJ$JJ


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

M***[%*>???[%*>???M*****r4   c                 t   dt           t                   ddf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.
        rD   r*   Nc                      t          d          )z
            A L{Deferred} canceller that raises an exception.

            @param deferred: The cancelled L{Deferred}.
            test)ra   rI   s    r2   cancellerRaisesExceptionzTDeferredTests.test_cancelDeferredListWithException.<locals>.cancellerRaisesException+  s     v&&&r4   rA   )r   r7   r   r   r   r   r   rc   ra   rT   rU   )rY   r  r   r   r   ri   s         r2   $test_cancelDeferredListWithExceptionz2DeferredTests.test_cancelDeferredListWithException$  s    	'x/? 	'D 	' 	' 	' 	' '//G&H&H&.jj#[+$>??[%*>???''55Va(((((r4   c                     t                      }t                      }t          ||gd          }|                    d           |                                 |                     |           dS )z
        When a L{DeferredList} has fired because one L{Deferred} in
        the list fired with a non-failure result, the cancellation will do
        nothing instead of cancelling the rest of the L{Deferred}s.
        Tr   N)r   r   rH   r   r  r   s       r2   )test_cancelFiredOnOneCallbackDeferredListz7DeferredTests.test_cancelFiredOnOneCallbackDeferredList;  sn     '/jj&.jj#[+$>RVWWWT"""K(((((r4   c                 p   t                      }t                      }t          ||gd          }|                    t          d                     |                                 |                     |           |                     |t                     |                     |t          j                   dS )z
        When a L{DeferredList} has fired because one L{Deferred} in
        the list fired with a failure result, the cancellation will do
        nothing instead of cancelling the rest of the L{Deferred}s.
        Tr   r  N)	r   r   r   r:   r   r  r   r   r   r   s       r2   (test_cancelFiredOnOneErrbackDeferredListz6DeferredTests.test_cancelFiredOnOneErrbackDeferredListH  s     '/jj&.jj#[+$>QUVVVL00111K((([,777\5+;<<<<<r4   c                     g }t          j        d          }|                    |j                   |                     |dg           d S )Nsuccess)r   succeedr   rS   rT   rY   lds      r2   testImmediateSuccessz"DeferredTests.testImmediateSuccessW  sH     =33	ahYK(((((r4   c                     g }t          j        t          d                    }|                    |j                   |                     t          |d         j                  d           d S Nfailr   )r   r  r:   rR   rS   rT   rs   rX   r  s      r2   testImmediateFailurez"DeferredTests.testImmediateFailure]  sZ    !J|F';';<<	QXQqTZ&11111r4   c                 X   g }t          j        t          d                    }|                                 |                    |j                   |                     |g            |                                 |                     t          |d         j	                  d           d S r  )
r   r  r:   pauserR   rS   rT   unpausers   rX   r  s      r2   testPausedFailurezDeferredTests.testPausedFailurec  s    J|F++,,					QXB			QqTZ&11111r4   c                    g }t                                          d                               |j                  }|                    d           |                     |d         j        t                     g }t                                          d                               |j                  }|                    d           |                     |d         j        t                     d S )Nc                     ddz  S NrA   r   r?   _s    r2   r   z2DeferredTests.testCallbackErrors.<locals>.<lambda>n  s
    Q!V r4   rA   r   c                 8    t          t                                S r/   )r'   ZeroDivisionErrorr$  s    r2   r   z2DeferredTests.testCallbackErrors.<locals>.<lambda>t  s    7+<+>+>#?#? r4   )r   r   rR   rS   rH   assertIsInstancerX   r'  r  s      r2   testCallbackErrorsz DeferredTests.testCallbackErrorsl  s    JJ""#3#344??II	

1adj*;<<<JJ[??@@Z!! 	

 	


1adj*;<<<<<r4   c                     t                      }|                                 |                    | j                   |                                 d S r/   )r   r  r   r   r  rY   r  s     r2   testUnpauseBeforeCallbackz'DeferredTests.testUnpauseBeforeCallbackz  s=    $JJ					dn%%%			r4   c                    t                      }t                      }|                                 |                    |fd           |                    | j                   |                    d           | j        
J d            |                    d           | j        
J d            |                                 | j        J | j        d         d         dk    s"J d                    | j                              d S )Nc                 ,    t          t          |          S r/   )r   int)rr   s     r2   r   z2DeferredTests.testReturnDeferred.<locals>.<lambda>  s    S" r4   rA   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   r   rH   r{   r  formatrY   r   r   s      r2   testReturnDeferredz DeferredTests.testReturnDeferred  s    $JJ$JJ




B555666
t~&&&
A#++-O+++
A#++-U+++


#/// #A&!+++<CC 
 
 ,++++r4   c                    t                      }t                                          |                                            t                      }|                    fd           |                    d           g }|                    |j                   |                     |g                                             |                     ||g           dS )z
        When a paused Deferred with a result is returned from a callback on
        another Deferred, the other Deferred is chained to the first and waits
        for it to be unpaused.
        c                     S r/   r?   )ignoredpauseds    r2   r   zDDeferredTests.test_chainedPausedDeferredWithResult.<locals>.<lambda>  s    F r4   N)r7   r   rH   r  r   rS   rT   r  )rY   expectedchainedrf   r7  s       @r2   $test_chainedPausedDeferredWithResultz2DeferredTests.test_chainedPausedDeferredWithResult  s     88#+::!!!"***2222333!FM***$$$(,,,,,r4   c                 R   t                      }t                      |                    fd           |                    d           |                                                     d           g }                    |j                   |                     |dg           dS )z
        A paused Deferred encountered while pushing a result forward through a
        chain does not prevent earlier Deferreds from continuing to execute
        their callbacks.
        c                     S r/   r?   r6  seconds    r2   r   z:DeferredTests.test_pausedDeferredChained.<locals>.<lambda>      & r4   N)r   r   rH   r  rS   rT   )rY   firstrf   r>  s      @r2   test_pausedDeferredChainedz(DeferredTests.test_pausedDeferredChained  s     !)

!)0000111t6=)))$(((((r4   c                 d   g }t          j        t          j        d          t          j        d          g                              |j                   |                     |ddgg           g }t          j        d          t          j        t                                g}t          j        |                              |j                   |                     t          |          d           | 
                    |d         t                     |d                             d            d S )NrA   r   r   c                     dS NrA   r?   r   s    r2   r   z2DeferredTests.test_gatherResults.<locals>.<lambda>  s    1 r4   )r   gatherResultsr  r   rS   rT   r  
ValueErrorrR   rU   r(  r'   )rY   resultsri   r   s       r2   test_gatherResultsz DeferredTests.test_gatherResults  s   #%U]1--u}Q/?/?@AAMMN	
 	
 	
 	Aq6(+++ "mA
:<< 8 89B**6=999Va(((fQi111
1%%%%%r4   c                 .   t          j        d          }t          j        t          d                    }t          j        ||gd          }g }|                    |j                   g }|                    |j                   |                     t          |          t          |          fd           | 	                    |d         j
        t           j                   |d         j
        j        }| 	                    |j
        t                     dS )z
        If a L{Deferred} in the list passed to L{gatherResults} fires with a
        failure and C{consumerErrors} is C{True}, the failure is converted to a
        L{None} result on that L{Deferred}.
        rA   zoh noesTr   r   rA   r   N)r   r  r  ra   rE  rR   rS   rT   rU   r(  rX   r   r   )rY   dgooddbadr  unconsumedErrorsgatheredErrorsr   s          r2   #test_gatherResultsWithConsumeErrorsz1DeferredTests.test_gatherResultsWithConsumeErrors  s     a  z,y1122TBBB*,(/000(*	^*+++#.//^1D1DEvNNNnQ/5u7GHHH#A&,7
j.=====r4   c                    t                      }t                      }t          j        ||g          }|                                 |                     |t          j                   |                     |t          j                   |                     |t          j                  }|j        }|                     |j	        
                    t          j                             dS )z~
        When cancelling the L{defer.gatherResults} call, all the
        L{Deferred}s in the list will be cancelled.
        N)r   r   rE  r   r   r   r   rX   rV   r   rW   )rY   r   r   rf   gatherResultsFailurer   s         r2   test_cancelGatherResultsz&DeferredTests.test_cancelGatherResults  s    
 '/jj&.jj$k;%?@@[%*>???[%*>???#33FE<LMM)/

-33E4HIIJJJJJr4   c                 D   t          t                    }t          t                    }t          j        ||g          }|                                 |                     |          }|                     |d         d           |                     |d         d           dS )z
        When cancelling the L{defer.gatherResults} call, if all the
        L{Deferred}s callback in their canceller, the L{Deferred}
        returned by L{defer.gatherResults} will be callbacked with the C{list}
        of the results.
        r   rF   rA   N)r   rJ   r   rE  r   r   rT   )rY   r   r   rf   callbackResults        r2   0test_cancelGatherResultsWithAllDeferredsCallbackz>DeferredTests.test_cancelGatherResultsWithAllDeferredsCallback  s     &..C%D%D%-.C%D%D$k;%?@@--f55*,=>>>*,=>>>>>r4   c                 D   t                      g }g }t          j        fd          }|                    |j        |j                   |                     |g            |                     t          |          d           |                     |d                    dS )z
        L{defer.maybeDeferred} should retrieve the result of a synchronous
        function and pass it to its resulting L{Deferred}.
        c                       S r/   r?   rf   s   r2   r   z6DeferredTests.test_maybeDeferredSync.<locals>.<lambda>   s     r4   rA   r   N)r7   r   maybeDeferredr   rS   rT   rU   assertIdentical)rY   rG  ri   r  rf   s       @r2   test_maybeDeferredSyncz$DeferredTests.test_maybeDeferredSync  s    
  " "//	w~v}555$$$Wq)))WQZ00000r4   c                     dt           dt           fd}g }g }t          j        |d          }|                    |j        |j                   |                     |g            |                     |dg           dS )zV
        L{defer.maybeDeferred} should pass arguments to the called function.
        xr*   c                     | dz   S )N   r?   r]  s    r2   plusFivez>DeferredTests.test_maybeDeferredSyncWithArgs.<locals>.plusFive  s    q5Lr4   
      N)r/  r   rY  r   rS   rT   )rY   ra  rG  ri   r  s        r2   test_maybeDeferredSyncWithArgsz,DeferredTests.test_maybeDeferredSyncWithArgs  s    
	 	 	 	 	 	   ""--	w~v}555$$$2$'''''r4   c                    t          d          dt          ffd}g }g }t          j        |          }|                    |j        |j                   |                     |g            |                     t          |          d           |                     t          |d         j	                  t                               dS )z
        L{defer.maybeDeferred} should catch an exception raised by a synchronous
        function and errback its resulting L{Deferred} with it.
        zthat value is unacceptabler*   c                       r/   r?   r8  s   r2   raisesExceptionzFDeferredTests.test_maybeDeferredSyncException.<locals>.raisesException  s    Nr4   rA   r   N)
rF  r   r   rY  r   rS   rT   rU   rs   rX   )rY   rh  rG  ri   r  r8  s        @r2   test_maybeDeferredSyncExceptionz-DeferredTests.test_maybeDeferredSyncException  s    
 :;;	 	 	 	 	 	 	   "00	w~v}555"%%%Va(((VAY_--s8}}=====r4   c                 r   	 ddz    n# t           $ r t                      Y nw xY wg }g }t          j        fd          }|                    |j        |j                   |                     |g            |                     t          |          d           |                     |d                    dS )zw
        L{defer.maybeDeferred} should handle a L{Failure} returned by a
        function and errback with it.
        10r_  c                       S r/   r?   rg  s   r2   r   z=DeferredTests.test_maybeDeferredSyncFailure.<locals>.<lambda>3  s     r4   rA   r   N)		TypeErrorr'   r   rY  r   rS   rT   rU   rZ  )rY   rG  ri   r  r8  s       @r2   test_maybeDeferredSyncFailurez+DeferredTests.test_maybeDeferredSyncFailure'  s    
	!1HHH 	! 	! 	!yyHHH	!   " 0 0 0 011	w~v}555"%%%Va(((VAY11111s   	 $$c                     t                      t          j        fd          }                    d           g }|                    |j                   |                     |dg           dS )z}
        L{defer.maybeDeferred} should let L{Deferred} instance pass by
        so that original result is the same.
        c                       S r/   r?   r   s   r2   r   z7DeferredTests.test_maybeDeferredAsync.<locals>.<lambda>?  s     r4   SuccessN)r   r   rY  rH   r   rS   rT   )rY   r   rf   r   s      @r2   test_maybeDeferredAsyncz%DeferredTests.test_maybeDeferredAsync9  sm    
 %JJ ,,
I
v}%%%)-----r4   c                     t                      t          j        fd          }                    t	          t                                           |                     |t
                     dS )z
        L{defer.maybeDeferred} should let L{Deferred} instance pass by
        so that L{Failure} returned by the original instance is the
        same.
        c                       S r/   r?   rq  s   r2   r   z<DeferredTests.test_maybeDeferredAsyncError.<locals>.<lambda>M  s    B r4   N)r   r   rY  r   r'   ra   r\   )rY   r   r   s     @r2   test_maybeDeferredAsyncErrorz*DeferredTests.test_maybeDeferredAsyncErrorE  se     &ZZ"0JJJ
 
 	

7<>>**+++##B55555r4   c                     t                      dt           ffddt          ffd}t          |                     t	          j        |                    t                               dS )z
        When called with a coroutine function L{defer.maybeDeferred} returns a
        L{defer.Deferred} which has the same result as the coroutine returned
        by the function.
        r*   c                     K    S r/   r?   rX  s   r2   r)   z;DeferredTests.test_maybeDeferredCoroutineSuccess.<locals>.fZ  s      Mr4   c                                    S r/   r?   r)   s   r2   gz;DeferredTests.test_maybeDeferredCoroutineSuccess.<locals>.g_      133Jr4   N)r7   r
   r   r   r   rY  r   )rY   r{  r)   rf   s     @@r2   "test_maybeDeferredCoroutineSuccessz0DeferredTests.test_maybeDeferredCoroutineSuccessR  s     	 	 	 	 	 	 	
	9 	 	 	 	 	 	 	  !4Q!7!788KK	
 	
 	
 	
 	
r4   c                      G d dt                     dfddt          ffd}t          |                     t	          j        |                    j        t                               dS )z
        When called with a coroutine function L{defer.maybeDeferred} returns a
        L{defer.Deferred} which has a L{Failure} result wrapping the exception
        raised by the coroutine function.
        c                       e Zd ZdS )GDeferredTests.test_maybeDeferredCoroutineFailure.<locals>.SomeExceptionNr;   r?   r4   r2   SomeExceptionr  n  s        Dr4   r  r*   Nc                     K                 r/   r?   )r  s   r2   r)   z;DeferredTests.test_maybeDeferredCoroutineFailure.<locals>.fq  s      -//!r4   c                                    S r/   r?   rz  s   r2   r{  z;DeferredTests.test_maybeDeferredCoroutineFailure.<locals>.gv  r|  r4   rw   )	Exceptionr
   r   r   r   rY  r   r   )rY   r{  r  r)   s     @@r2   "test_maybeDeferredCoroutineFailurez0DeferredTests.test_maybeDeferredCoroutineFailureg  s    	 	 	 	 	I 	 	 		" 	" 	" 	" 	" 	"
	9 	 	 	 	 	 	 	  !4Q!7!788=]##	
 	
 	
 	
 	
r4   c                    g g t                      dt          dt           t                   ffd}t          j        d          }|                    |                               d           |                    j                                       j                   |                    j                   |                     g            |                     g d           dS )a  
        When a L{Deferred} encounters a result which is another L{Deferred}
        which is waiting on a third L{Deferred}, the middle L{Deferred}'s
        callbacks are executed after the third L{Deferred} fires and before the
        first receives a result.
        rf   r*   c                 J                        d| f           t          j        d          }dt          dt          t                   ffd}dt          dt          ffd}|                    |                              |           |                    j                    |S )Nstart-of-cbinnerrf   r*   c                 2                         d           S NfirstCallbackr  r   rf   r  rG  s    r2   r  zMDeferredTests.test_innerCallbacksPreserved.<locals>.cb.<locals>.firstCallback  s    9:::r4   c                 <                         d| f           | dz  S NsecondCallbackr   r   rf   rG  s    r2   r  zNDeferredTests.test_innerCallbacksPreserved.<locals>.cb.<locals>.secondCallback  %     0&9:::z!r4   rS   r   r  rs   r   r   rR   rf   r  r  r  r[   r  rG  s       r2   r   z6DeferredTests.test_innerCallbacksPreserved.<locals>.cb  s    NNM62333g&&Ac hsm       "s "s " " " " " " MM-((44^DDDLL)))Hr4   outeroranger  r  r  )r  r  orangeorangeN)	r   rs   r   r  r   rH   rS   rR   rT   rY   r   r  r[   r  rG  s      @@@r2   test_innerCallbacksPreservedz*DeferredTests.test_innerCallbacksPreserved~  s    *,"$'zz	s 	x} 	 	 	 	 	 	 	 	  g&&"x   '.)))))))))X&&&  	
 	
 	
 	
 	
r4   c                    g g t                      dt          dt           t          t                            ffd}t          j        d          }|                    |           |                    j                   |                     ddg           	                    d           |                     g            |                     ddddgfg           d	S )
a   
        The continue callback of a L{Deferred} waiting for another L{Deferred}
        is not necessarily the first one. This is somewhat a whitebox test
        checking that we search for that callback among the whole list of
        callbacks.
        rf   r*   c                 x                        d| f           t                      }dt          dt          t          t                            ffd}dt          dd ffd}|                    |           |                    |           |                    j                    |                    d            |S )Nr   rf   r*   c                 \                         d| f           t          j        g          S Nr  rS   r   rE  rf   arG  s    r2   r  zNDeferredTests.test_continueCallbackNotFirst.<locals>.cb.<locals>.firstCallback  -    8999*A3///r4   c                 6                         d| f           d S Nr  r   r  s    r2   r  zODeferredTests.test_continueCallbackNotFirst.<locals>.cb.<locals>.secondCallback  !     0&9:::::r4   rS   r   rs   r   r   rR   rH   rf   r  r  r  r  r[   rG  s       r2   r   z7DeferredTests.test_continueCallbackNotFirst.<locals>.cb      NND&>***)1A0c 0htCy.A 0 0 0 0 0 0 0;s ;t ; ; ; ; ; ; MM-(((MM.)))LL)))JJtHr4   r  r   r  r  Nwithersr  N)
r   rs   r   r   r  r   rR   rS   rT   rH   rY   r   r  r  r[   rG  s      @@@r2   test_continueCallbackNotFirstz+DeferredTests.test_continueCallbackNotFirst  s    46"$#::	s 	x6 	 	 	 	 	 	 	 	" g&&")))/+BCWMMM	

9X&&&58H9+7VW	
 	
 	
 	
 	
r4   c                    g g t                      dt          dt           t          t                            ffd}t                      }|                    |           |                    fd           |                    j                   |                    d           |                     ddg                               d           |                     g            |                     ddd	dgfd
g           dS )z
        A callback added to a L{Deferred} after a previous callback attached
        another L{Deferred} as a result is run after the callbacks of the other
        L{Deferred} are run.
        rf   r*   c                 x                        d| f           t                      }dt          dt          t          t                            ffd}dt          dd ffd}|                    |           |                    |           |                    j                    |                    d            |S )Nr   rf   r*   c                 \                         d| f           t          j        g          S r  r  r  s    r2   r  zLDeferredTests.test_callbackOrderPreserved.<locals>.cb.<locals>.firstCallback  r  r4   c                 6                         d| f           d S r  r   r  s    r2   r  zMDeferredTests.test_callbackOrderPreserved.<locals>.cb.<locals>.secondCallback  r  r4   r  r  s       r2   r   z5DeferredTests.test_callbackOrderPreserved.<locals>.cb  r  r4   c                 .                         d          S )NfinalNr   )r]  rG  s    r2   r   z;DeferredTests.test_callbackOrderPreserved.<locals>.<lambda>  s    GNN?$C$C r4   r  r  r  r  r  r  N)r   rs   r   r   rR   rS   rH   rT   r  s      @@@r2   test_callbackOrderPreservedz)DeferredTests.test_callbackOrderPreserved  s?    46"$%-ZZ	s 	x6 	 	 	 	 	 	 	 	"  (zz"CCCCDDD)))w/+BCWMMM	

9X&&&'!I;/		
 	
 	
 	
 	
r4   c                    t                      g dfddfd}dfd}                    |                               |                               d           |                     g d           dS )	z
        A callback added to a L{Deferred} by a callback on that L{Deferred}
        should be added to the end of the callback chain.
        rf   Nr*   c                 2                         d           d S N   r   rf   calleds    r2   	callback3z;DeferredTests.test_reentrantRunCallbacks.<locals>.callback3      MM!r4   c                 2                         d           d S Nr   r   r  s    r2   	callback2z;DeferredTests.test_reentrantRunCallbacks.<locals>.callback2	  r  r4   c                 \                         d                                          d S rD  )rS   r   )rf   r  r  rD   s    r2   	callback1z;DeferredTests.test_reentrantRunCallbacks.<locals>.callback1  s/    MM!  +++++r4   )rA   r   r  rf   Nr*   Nr   r   rH   rT   )rY   r  r  r  r  rD   s      @@@r2   test_reentrantRunCallbacksz(DeferredTests.test_reentrantRunCallbacks  s    
 $,::	 	 	 	 	 		 	 	 	 	 		, 	, 	, 	, 	, 	, 	, 	, 	Y'''Y'''$+++++r4   c                      t                      g dfdd fd}                    |                               d                                ddg           dS )	z
        A callback added to a L{Deferred} by a callback on that L{Deferred}
        should not be executed until the running callback returns.
        rf   Nr*   c                 2                         d           d S r  r   r  s    r2   r  z;DeferredTests.test_nonReentrantCallbacks.<locals>.callback2  r  r4   c                                          d                                                              dg           d S rD  )rS   r   rT   )rf   r  r  rD   rY   s    r2   r  z;DeferredTests.test_nonReentrantCallbacks.<locals>.callback1   sG    MM!  +++VaS)))))r4   rA   r   r  r  )rY   r  r  r  rD   s   ` @@@r2   test_nonReentrantCallbacksz(DeferredTests.test_nonReentrantCallbacks  s    
 $,::	 	 	 	 	 		* 	* 	* 	* 	* 	* 	* 	* 	*
 	Y'''$!Q(((((r4   c                 ,   dt                      dt          ddffddt          ddffd}                    |                               d           |                     t
                    }|                     |j        f           dS )z
        After an exception is raised by a callback which was added to a
        L{Deferred} by a callback on that L{Deferred}, the L{Deferred} should
        call the first errback with a L{Failure} wrapping that exception.
        zcallback raised exceptionrf   r*   Nc                 "    t                    r/   )r  )rf   exceptionMessages    r2   r  zFDeferredTests.test_reentrantRunCallbacksWithFailure.<locals>.callback22  s    ,---r4   c                 2                                    d S r/   )r   )rf   r  rD   s    r2   r  zFDeferredTests.test_reentrantRunCallbacksWithFailure.<locals>.callback15  s      +++++r4   )r   r7   r   rH   r\   r  rT   r,   )rY   r  rO   r  rD   r  s      @@@r2   %test_reentrantRunCallbacksWithFailurez3DeferredTests.test_reentrantRunCallbacksWithFailure)  s     7#+::	.f 	. 	. 	. 	. 	. 	. 	.	,f 	, 	, 	, 	, 	, 	, 	, 	, 	Y'''$//)DD	*:)<=====r4   c                    t                      }t          j        |          t                      }|                    fd           |                    d           g }                    |j                   |                     |d                    |                    |j                   |                     |d         |           dS )a  
        If a first L{Deferred} with a result is returned from a callback on a
        second L{Deferred}, the result of the second L{Deferred} becomes the
        result of the first L{Deferred} and the result of the first L{Deferred}
        becomes L{None}.
        c                     S r/   r?   ignr@  s    r2   r   z=DeferredTests.test_synchronousImplicitChain.<locals>.<lambda>G      u r4   Nr   rA   )	r7   r   r  r   r   rH   rS   r   rd   )rY   rf   r>  rG  r@  s       @r2   test_synchronousImplicitChainz+DeferredTests.test_synchronousImplicitChain=  s     f%%!),,,,---*,'.)))'!*%%%7>***gaj&)))))r4   c                    t                      t                      }|                    fd           |                    d           g }                    |j                   g }|                    |j                   |                     |g            |                     |g            t                      }                    |           |                     |dg           |                     ||g           dS )a9  
        If a first L{Deferred} without a result is returned from a callback on
        a second L{Deferred}, the result of the second L{Deferred} becomes the
        result of the first L{Deferred} as soon as the first L{Deferred} has
        one and the result of the first L{Deferred} becomes L{None}.
        c                     S r/   r?   r  s    r2   r   z>DeferredTests.test_asynchronousImplicitChain.<locals>.<lambda>Y  r  r4   N)r   r   rH   rS   rT   r7   )rY   r>  r   secondResultrf   r@  s        @r2   test_asynchronousImplicitChainz,DeferredTests.test_asynchronousImplicitChainP  s     #+**#+::,,,,---$&+,---%'<.///b)))r***vtf---x00000r4   c                    t          j        t          d                    }|fdddt          d         dt          d         f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 r/   r?   )r%  r@  s     r2   r   z<DeferredTests.test_synchronousImplicitErrorChain.<locals>.cbs  s    Lr4   r   )	r   r  ra   r   r   rH   rS   r   r\   )rY   r@  r   r>  r   s        r2   "test_synchronousImplicitErrorChainz0DeferredTests.test_synchronousImplicitErrorChainj  s     
<(BCCDD05 	 	$ 	x~ 	(4. 	 	 	 	 "*2"$+,---+a.)))##FL99999r4   c                    t                      t                      }|                    fd           |                    d           g }|                    |j                   g }                    |j                   g }|                    |j                   |                     |g            |                     |g                                t          d                     |                     |d         	                    t                               |                     |dg           |                     t          |          d           dS )aP  
        Let C{a} and C{b} be two L{Deferred}s.

        If C{a} has no result and is returned from a callback on C{b} then when
        C{a} fails, C{b}'s result becomes the L{Failure} that was C{a}'s result,
        the result of C{a} becomes L{None} so that no unhandled error is logged
        when it is garbage collected.
        c                     S r/   r?   r  s    r2   r   zCDeferredTests.test_asynchronousImplicitErrorChain.<locals>.<lambda>  r  r4   Nr  r   rA   )r   r   rH   rR   rS   rT   r   ra   rV   rW   rU   )rY   r>  secondErrorr   r  r@  s        @r2   #test_asynchronousImplicitErrorChainz1DeferredTests.test_asynchronousImplicitErrorChain~  sB    !)

!),,,,---%'+,---"$+,---#%<.///b)))r***l#=>>???A,,\::;;;tf---\**A.....r4   c                    t                      t                                          fd           t                      }|                    fd           g }|                    |j                   t                      }                    d           |                    d           |                     |g                                |           |                     ||g           dS )z
        L{Deferred} chaining is transitive.

        In other words, let A, B, and C be Deferreds.  If C is returned from a
        callback on B and B is returned from a callback on A then when C fires,
        A fires.
        c                     S r/   r?   r  s    r2   r   zGDeferredTests.test_doubleAsynchronousImplicitChaining.<locals>.<lambda>  r  r4   c                     S r/   r?   )r  r>  s    r2   r   zGDeferredTests.test_doubleAsynchronousImplicitChaining.<locals>.<lambda>  s    f r4   N)r   r   rS   r7   rH   rT   )rY   thirdthirdResultrf   r@  r>  s       @@r2   'test_doubleAsynchronousImplicitChainingz5DeferredTests.test_doubleAsynchronousImplicitChaining  s     #+**#+::,,,,---"***,,,,---$&+,---t 	b))) 	vvh/////r4   c                    g g t                      dt          dt           t                   ffd}t          j        d          }|                    |           |                    j                   |                     ddg                               d                               j                   |                    j                   |                     g d           |                     g d	           d
S )z
        L{Deferred}s can have callbacks that themselves return L{Deferred}s.
        When these "inner" L{Deferred}s fire (even asynchronously), the
        callback chain continues.
        rf   r*   c                 J                        d| f           t          j        d          }dt          dt          t                   ffd}dt          dt          ffd}|                    |                              |           |                    j                    |S )Nr  r  rf   r*   c                 2                         d           S r  r   r  s    r2   r  zXDeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb.<locals>.firstCallback  s    9::: r4   c                 <                         d| f           | dz  S r  r   r  s    r2   r  zYDeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb.<locals>.secondCallback  r  r4   r  r  s       r2   r   zADeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb  s    NNM62333g&&Ac hsm       "s "s " " " " " " MM-((44^DDDLL)))Hr4   r  r  r  r  &Got errbacks but wasn't expecting any.r  N	r   rs   r   r  r   rS   rT   rH   rR   r  s      @@@r2   'test_nestedAsynchronousChainedDeferredsz5DeferredTests.test_nestedAsynchronousChainedDeferreds  sF    *,"$  (zz	s 	x} 	 	 	 	 	 	 	 	* g&&"'.))) 	.0JK	
 	
 	
 	x    	))))))X'OPPP  	
 	
 	
 	
 	
r4   c                 &   g g t                      dt          dt           t                   ffd}t          j        d          }|                    |           |                    j                   |                     ddg                               d           |                    j                                       j                   |                     g d           |                     ddd	dgfddgg           d
S )aK  
        L{Deferred}s can have callbacks that themselves return L{Deferred}s.
        These L{Deferred}s can have other callbacks added before they are
        returned, which subtly changes the callback chain. When these "inner"
        L{Deferred}s fire (even asynchronously), the outer callback chain
        continues.
        rf   r*   c                                         d| f           t          j        d          }dt          dt          t
          t                            ffd}dt
          t                   dt
          t                   ffd}|                    |           |                    |           |                    j                    |S )Nr  r  rf   r*   c                                          d| f           dt          dt          t                   fd}                    |          S )Nr  rf   r*   c                     | gS r/   r?   rX  s    r2   	transformz}DeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.firstCallback.<locals>.transform  s	    "8Or4   )rS   rs   r   r   )rf   r  r  rG  s     r2   r  zjDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.firstCallback
  sV    8999$c $d3i $ $ $ $ ((333r4   c                 <                         d| f           | dz  S r  r   r  s    r2   r  zkDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.secondCallback  r  r4   )rS   r   r  rs   r   r   r   rR   r  s       r2   r   zSDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb  s    NNM62333g&&A
4c 
4htCy.A 
4 
4 
4 
4 
4 
4 
4"tCy "T#Y " " " " " " MM-(((MM.)))LL)))Hr4   r  r  r  r  r  r  Nr  r  s      @@@r2   9test_nestedAsynchronousChainedDeferredsWithExtraCallbackszGDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks  sT     "$ 2:	s 	x} 	 	 	 	 	 	 	 	8 g&&"'.))) 	.0JK	
 	
 	
 	y!!! 	))))))X'OPPP(*!I;/I&		
 	
 	
 	
 	
r4   c                     t                      }t                      }|                    |           |                     |j        |           dS )zT
        When we chain a L{Deferred}, that chaining is recorded explicitly.
        N)r   chainDeferredrd   
_chainedTorY   r  bs      r2   &test_chainDeferredRecordsExplicitChainz4DeferredTests.test_chainDeferredRecordsExplicitChain>  sD     %JJ$JJ	alA&&&&&r4   c                     t                      }t                      }|                    |           |                    d           |                     |j                   dS )aR  
        Any recorded chaining is cleared once the chaining is resolved, since
        it no longer exists.

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

4!,'''''r4   c                     t                      }t                      |                    fd           |                    d           |                     |j                   dS )z
        We can chain L{Deferred}s implicitly by adding callbacks that return
        L{Deferred}s. When this chaining happens, we record it explicitly as
        soon as we can find out about it.
        c                     S r/   r?   )r6  r  s    r2   r   zFDeferredTests.test_chainDeferredRecordsImplicitChain.<locals>.<lambda>^  s    a r4   N)r   r   rH   rd   r  r  s     @r2   &test_chainDeferredRecordsImplicitChainz4DeferredTests.test_chainDeferredRecordsImplicitChainV  sa     %JJ$JJ	''''(((	

4alA&&&&&r4   c                    t                      dt          dt           t                   ffd}                    |                               d           |                     |g          }|                     t          |          d           |d         }|                     |d         t                     d}|                     t          j
        ||d	                   d
                    ||d	                              dS )z
        When a Deferred is returned from a callback directly attached to that
        same Deferred, a warning is emitted.
        rf   r*   c                     S r/   r?   rf   r  s    r2   circularCallbackzADeferredTests.test_circularChainWarning.<locals>.circularCallbacki      Hr4   foorA   r   categoryz1Callback returned the Deferred it was attached tomessagez
Expected match: {!r}
Got: {!r}N)r   rs   r   rH   flushWarningsrT   rU   DeprecationWarningrV   researchr1  )rY   r  circular_warningswarningpatternr  s        @r2   test_circularChainWarningz'DeferredTests.test_circularChainWarningb  s   
 $::	S 	Xc] 	 	 	 	 	 	 	
&'''	

5 ..0@/ABB.//333#A&,.@AAAEIgwy122/66w	@RSS	
 	
 	
 	
 	
r4   c                    |                      t          t          dt          j                   t          j        dt
                     t                      dt          dt          t                   ffd}                    |           	                    d           | 
                              }|                    t
                     dS )	z
        If the deprecation warning for circular deferred callbacks is
        configured to be an error, the exception will become the failure
        result of the Deferred.
        filtersre   )r  rf   r*   c                     S r/   r?   r  s    r2   r  zCDeferredTests.test_circularChainException.<locals>.circularCallback  r  r4   r  N)r~   setattrwarningsr  filterwarningsr  r   rs   r   rH   r   trap)rY   r  failurer  s      @r2   test_circularChainExceptionz)DeferredTests.test_circularChainExceptiony  s     			
 	
 	
 	2DEEEE#::	S 	Xc] 	 	 	 	 	 	 	
&'''	

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

8a"WBqEE"W"W"W"WXXXXXr4   c           	          t                      }t                      }|                    |           |                     t          |          dt	          |          ddt	          |          dd           dS )z
        If a L{Deferred} C{a} has been fired, but is waiting on another
        L{Deferred} C{b} that appears in its callback chain, then C{repr(a)}
        says that it is waiting on C{b}.
        r  r]  z waiting on Deferred at 0xr  N)r   r  rT   r  r  r  s      r2   test_reprWithChainingz#DeferredTests.test_reprWithChaining  s{     %JJ$JJ	GGKbeeKKKAKKKK	
 	
 	
 	
 	
r4   c                      dt           dt           f fd}                      |d           |d                     dS )zw
        The depth of the call stack does not grow as more L{Deferred} instances
        are chained together.
        howManyr*   c                   	
 g 
dt           dd f
fd}t                      }d t          |           D             }|d d          }t                      	|                                }|fdt           dt          d          dt          d          fd}|                    |           |                    |           |rT|                                }|fdt           dt          d          dt          d          fd}|                    |           |}|T|                    	fd           |                    d            |D ]}|                    d                                
g            	                    d            
d	         S )
Nr6  r*   c                 n                         t          t          j                                         d S r/   )rS   rU   	tracebackextract_stack)r6  stacks    r2   recordStackDepthzVDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.recordStackDepth  s,    S!8!:!:;;<<<<<r4   c                 *    g | ]}t                      S r?   )r   ).0r6  s     r2   
<listcomp>zPDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.<listcomp>  s)     4 4 4&

4 4 4r4   r  c                     |S r/   r?   r6  r  s     r2   cbInnerzMDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.cbInner  s	     r4   c                     |S r/   r?   r2  s     r2   
cbNewInnerzPDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.cbNewInner  s	     !Lr4   c                     S r/   r?   )r  lasts    r2   r   zNDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.<lambda>  s    $ r4   r   )r7   r   rangepopr   rH   rT   )r'  r-  topinnerDeferredsoriginalInnersr  r3  newInnerr5  r7  r,  rY   s            @@r2   chainDeferredsz<DeferredTests.test_boundedStackDepth.<locals>.chainDeferreds  s   E=& =T = = = = = = #+**C4 4*/..4 4 4N ,AAA.N#+::D"&&((E :? (0$   
 OOG$$$OO,---  	!)--// >F! !#!,4TN!d^! ! ! !
 !!*---  ! 	! ....///LL' % %t$$$$ UB''' MM$8Or4   rA   r   N)r/  rT   )rY   r>  s   ` r2   test_boundedStackDepthz$DeferredTests.test_boundedStackDepth  sb    /	C /	C /	 /	 /	 /	 /	 /	f 	**NN1,=,=>>>>>r4   c                    t                      }t                      t                      |                    fd                               fd                               d           |                    d                               d           g }                    |j                   |                     |dg           dS )a  
        Given three Deferreds, one chained to the next chained to the next,
        callbacks on the middle Deferred which are added after the chain is
        created are called once the last Deferred fires.

        This is more of a regression-style test.  It doesn't exercise any
        particular code path through the current implementation of Deferred, but
        it does exercise a broken codepath through one of the variations of the
        implementation proposed as a resolution to ticket #411.
        c                     S r/   r?   r=  s    r2   r   zZDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled.<locals>.<lambda>  r?  r4   c                     S r/   r?   )r6  r  s    r2   r   zZDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled.<locals>.<lambda>  s    5 r4   N)r   r   rH   rS   rT   )rY   r@  rG  r>  r  s      @@r2   :test_resultOfDeferredResultOfDeferredOfFiredDeferredCalledzHDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled  s     !)

!) (

00001110000111tt 7>***4&)))))r4   c                    t          j        d           t                      }g }t          d          }	 |# t          $ r |                                 Y nw xY w|                    |j                   |d         }|                     |j	        |           |j
        d         dd         \  }}|                     g |           |                     g |           dS )z
        C{Deferred.errback()} creates a failure from the current Python
        exception.  When Deferred.debug is not set no globals or locals are
        captured in that failure.
        Fr   r   N)r   r   r   r:   rB   r   rR   rS   rT   rX   framesrY   r  r  excr  localzglobalzs          r2   test_errbackWithNoArgsNoDebugz+DeferredTests.test_errbackWithNoArgsNoDebug  s     	5!!!$JJ6""	I 	 	 	IIKKKKK		QXtS)))+a.-V$$$W%%%%%   7 AAc                    t          j        d           t                      }g }t          d          }	 |# t          $ r |                                 Y nw xY w|                    |j                   |d         }|                     |j	        |           |j
        d         dd         \  }}|                     g |           |                     g |           dS )z
        C{Deferred.errback()} creates a failure from the current Python
        exception.  When Deferred.debug is set globals and locals are captured
        in that failure.
        Tr   r   rE  N)r   r   r   r:   rB   r   rR   rS   rT   rX   rF  assertNotEqualrG  s          r2   test_errbackWithNoArgsz$DeferredTests.test_errbackWithNoArgs  s     	4   $JJ6""	I 	 	 	IIKKKKK		QXtS)))+a.-B'''B(((((rL  c                    t                      }|                    d           t          j        d           dt          ddf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.
        NFr6  r*   c                      t          d          Nr   r:   r6  s    r2   
raiseErrorzHDeferredTests.test_errorInCallbackDoesNotCaptureVars.<locals>.raiseError5      v&&&r4   r   rE  )
r   rH   r   r   r7   r   rR   rS   rF  rT   rY   r  rV  r  r  rI  rJ  s          r2   &test_errorInCallbackDoesNotCaptureVarsz4DeferredTests.test_errorInCallbackDoesNotCaptureVars,  s    
 %JJ	

45!!!	' 	'4 	' 	' 	' 	' 	
j!!!	QXt+a.-V$$$W%%%%%r4   c                    t                      }|                    d           t          j        d           dt          ddfd}|                    |           g }|                    |j                   |d         }|j        d         dd         \  }}| 	                    g |           | 	                    g |           dS )rQ  NTr6  r*   c                      t          d          rS  rT  rU  s    r2   rV  zODeferredTests.test_errorInCallbackCapturesVarsWhenDebugging.<locals>.raiseErrorI  rW  r4   r   rE  )
r   rH   r   r   r7   r   rR   rS   rF  rN  rX  s          r2   -test_errorInCallbackCapturesVarsWhenDebuggingz;DeferredTests.test_errorInCallbackCapturesVarsWhenDebugging@  s    
 %JJ	

44   	' 	'4 	' 	' 	' 	' 	
j!!!	QXt+a.-B'''B(((((r4   c                 x   t                      }t                      }	 |                                 n$# t          $ r |                                 Y nw xY wdt
          dt          t          t          df         fd}t          j	        |           | 
                    |          }t          j        |                                          }|                     t          |          d           |                     d|d         d                    |                     d	|d         d                    |                     d
|d         d                    |                     d|d         d                    |                     d|d         d                    |                     d|d         d                    dS )z
        L{defer.inlineCallbacks} that re-raise tracebacks into their deferred
        should not lose their tracebacks.
        r  r*   Nc              3      K   | V  d S r/   r?   r  s    r2   icz8DeferredTests.test_inlineCallbacksTracebacks.<locals>.ic`  s      GGGGGr4   r  
test_deferr   r   rC   z1 / 0test_inlineCallbacksTracebackszf.raiseException())rC   r   raiseExceptionrB   r   r7   r   r   r   inlineCallbacksr   r*  
extract_tbgetTracebackObjectrT   rU   assertIn)rY   r)   r  r`  
newFailuretbs         r2   rb  z,DeferredTests.test_inlineCallbacksTracebacksT  s   
   $JJ	 	 	 	IIKKKKK		& 	YsC~6 	 	 	 	 	b!!!))!,,
!*"?"?"A"ABBR!$$$lBqE!H----r!uQx888"Q%(+++lBqE!H---92a58DDD-r!uQx88888s   3 AAc                     d dd| j         dt          g}|D ]-}|                     t          j        t          j        |           .dS )z
        L{Deferred.fromCoroutine} requires a coroutine object or a generator,
        and will reject things that are not that.
        c                     | S r/   r?   r`  s    r2   r   zCDeferredTests.test_fromCoroutineRequiresCoroutine.<locals>.<lambda>w  s    a r4   rA   TN)#test_fromCoroutineRequiresCoroutiner   assertRaisesNotACoroutineErrorr   r0   )rY   thingsThatAreNotCoroutinesthings      r2   rl  z1DeferredTests.test_fromCoroutineRequiresCoroutinep  sd     K4&
" 0 	W 	WEe68NPUVVVV	W 	Wr4   rw   )Sr<   r=   r>   r   r7   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,  r3  r:  rA  rH  rO  rR  rU  r[  rd  ri  rn  rs  rv  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r!  r#  r%  r?  rC  rK  rO  rY  r\  rb  rl  r?   r4   r2   ry   ry      s       B B B Bv  C    - -& -T - - - -+g + +D + + + +A A A AI I I IQ Q Q QB B B B5 5 5 5A A A A"5 5 5 5,"
 "
 "
 "
H% % % %('. '. '. '.R? ? ? ?"? ? ? ?"& & & && & & &"
@ 
@ 
@ 
@B B B B$
@ 
@ 
@ 
@9 9 9 9K K K K: : : :"+ + + + ) ) ) ).) ) ) )= = = =) ) ) )2 2 2 22 2 2 2= = = =   
 
 
 
$- - - -() ) ) ) & & & & > > > >(K K K K? ? ? ?1 1 1 1( ( ( (> > > >$2 2 2 2$
. 
. 
. 
.6 6 6 6
 
 
 
*
 
 
 
.*
 *
 *
 *
X%
 %
 %
 %
N+
 +
 +
 +
Z, , , ,.) ) ) )(> > > >(* * * *&1 1 1 14: : : :(/ / / /:0 0 0 0>;
 ;
 ;
 ;
zE
 E
 E
 E
N' ' ' '( ( ( (
' 
' 
' 
'
 
 
 
.) ) ) ).B B B BY Y Y Y
 
 
 
9? 9? 9? 9?v* * * *.& & & &*) ) ) )*& & & &() ) ) )(9 9 9 98W W W W W Wr4   ry   c                   *    e Zd ZdZddZddZddZdS )FirstErrorTestsz"
    Tests for L{FirstError}.
    r*   Nc                     t          d          }	 |# t          $ r t                      }Y nw xY wt          j        |d          }|                     t          |          dt          |           d           dS )z
        The repr of a L{FirstError} instance includes the repr of the value of
        the sub-failure and the index which corresponds to the L{FirstError}.
        	some textr  zFirstError[#3, ]N)rF  rB   r'   r   r   rT   r  rY   rH  r)   re   s       r2   r!  zFirstErrorTests.test_repr  s    
 %%	I 	 	 			AAA	  A&&e&DS		&D&D&DEEEEE    ..c                     t          d          }	 |# t          $ r t                      }Y nw xY wt          j        |d          }|                     t          |          dt          |           d           dS )z
        The str of a L{FirstError} instance includes the str of the
        sub-failure and the index which corresponds to the L{FirstError}.
        rt  r_  zFirstError[#5, ru  N)rF  rB   r'   r   r   rT   rs   rv  s       r2   test_strzFirstErrorTests.test_str  s    
 %%	I 	 	 			AAA	  A&&U%@s1vv%@%@%@AAAAArw  c                 $   	 ddz   n# t           $ r t                      }Y nw xY wt          j        |d          }t          j        |d          }	 t	          d          # t           $ r t                      }Y nw xY wt          j        |d          }|                     ||k               |                     ||k               |                     ||k               |                     ||k               |                     |dk               dS )z
        L{FirstError} instances compare equal to each other if and only if
        their failure and index compare equal.  L{FirstError} instances do not
        compare equal to instances of other types.
        rA   r      bar	   rb  N)rB   r'   r   r   rF  rV   r   )rY   firstFailureone
anotherOnesecondFailureanothers         r2   test_comparisonzFirstErrorTests.test_comparison  s+   	%FFF 	% 	% 	%"99LLL	% |R00%lB77
	&U### 	& 	& 	&#IIMMM	& "=!44z)***(((w'''
*+++#####s    ##A   A;:A;rw   )r<   r=   r>   r]   r!  ry  r  r?   r4   r2   rr  rr    sb         F F F FB B B B$ $ $ $ $ $r4   rr  c            
       J   e Zd Zd"dZd"dZdededdfdZdededdfdZd	ee	         ddfd
Z
d	ee	         ddfdZd	ee	         ddfdZd	ee	         ddfdZd"dZd"dZd"dZd"dZde	de	dee	         deddf
dZdede	de	de	ddf
dZd"dZd"dZd"dZd"dZd"d Zd"d!ZdS )#AlreadyCalledTestsr*   Nc                 ^    t          j                    | _        t          j        d           d S NT)r   r   _deferredWasDebuggingr   r   s    r2   r   zAlreadyCalledTests.setUp  s*    %*%7%9%9"4     r4   c                 8    t          j        | j                   d S r/   )r   r   r  r   s    r2   tearDownzAlreadyCalledTests.tearDown  s    4566666r4   r,   r-   c                     d S r/   r?   r   s      r2   r   zAlreadyCalledTests._callback      r4   c                     d S r/   r?   r   s      r2   r   zAlreadyCalledTests._errback  r  r4   r  c                 0    |                     d           d S )Nr   rG   r+  s     r2   _call_1zAlreadyCalledTests._call_1      	

7r4   c                 0    |                     d           d S )NtwicerG   r+  s     r2   _call_2zAlreadyCalledTests._call_2  r  r4   c                 b    |                     t          t                                           d S r/   r   r'   ra   r+  s     r2   _err_1zAlreadyCalledTests._err_1  &    			',..))*****r4   c                 b    |                     t          t                                           d S r/   r  r+  s     r2   _err_2zAlreadyCalledTests._err_2  r  r4   c                     t                      }|                    | j        | j                   |                     |           |                     t          j        | j        |           d S r/   )	r   r   r   r   r  rm  r   AlreadyCalledErrorr  r+  s     r2   testAlreadyCalled_CCz'AlreadyCalledTests.testAlreadyCalled_CC  sU    #::	t~t}555Q%2DL!DDDDDr4   c                     t                      }|                    | j        | j                   |                     |           |                     t          j        | j        |           d S r/   )	r   r   r   r   r  rm  r   r  r  r+  s     r2   testAlreadyCalled_CEz'AlreadyCalledTests.testAlreadyCalled_CE  sU    #::	t~t}555Q%2DKCCCCCr4   c                     t                      }|                    | j        | j                   |                     |           |                     t          j        | j        |           d S r/   )	r   r   r   r   r  rm  r   r  r  r+  s     r2   testAlreadyCalled_EEz'AlreadyCalledTests.testAlreadyCalled_EE  sU    #::	t~t}555A%2DKCCCCCr4   c                     t                      }|                    | j        | j                   |                     |           |                     t          j        | j        |           d S r/   )	r   r   r   r   r  rm  r   r  r  r+  s     r2   testAlreadyCalled_ECz'AlreadyCalledTests.testAlreadyCalled_EC  sU    #::	t~t}555A%2DL!DDDDDr4   linetypefunclinesr8  c                     d}|D ]7}|                     d|z            r|                    d|z            r|dz  }8|                     ||k               d S )Nr   z %s:z %srA   )ru   endswithrV   )rY   r  r  r  r8  countlines          r2   _countzAlreadyCalledTests._count  sm     	 	Dv011 dmmEDL6Q6Q 
)*****r4   r   callerinvoker1invoker2c                    |j         d                             d          }|                     d||d           |                     dd|d           |                     dd|d           |                     dd|d           |                     dd|d           |                     d	||d           |                     d	||d           d S )
Nr   
CrA   r  r  r  r  I)r,   splitr  )rY   r   r  r  r  r  s         r2   _checkzAlreadyCalledTests._check  s    q	%% 	C***CE1---CE1---C5!,,,C5!,,,C5!,,,C5!,,,,,r4   c                 L   t                      }|                    | j        | j                   |                     |           	 |                     |           |                     d           d S # t          j        $ r#}| 	                    |ddd           Y d }~d S d }~ww xY w)N2second callback failed to raise AlreadyCalledErrortestAlreadyCalledDebug_CCr  r  )
r   r   r   r   r  r  r  r   r  r  rY   r  r   s      r2   r  z,AlreadyCalledTests.testAlreadyCalledDebug_CC  s    #::	t~t}555Q	LLLOOO IIJKKKKK ' 	N 	N 	NKK6	9MMMMMMMMM	N   A1 1B# BB#c                 L   t                      }|                    | j        | j                   |                     |           	 |                     |           |                     d           d S # t          j        $ r#}| 	                    |ddd           Y d }~d S d }~ww xY w)N1second errback failed to raise AlreadyCalledErrortestAlreadyCalledDebug_CEr  r  )
r   r   r   r   r  r  r  r   r  r  r  s      r2   r  z,AlreadyCalledTests.testAlreadyCalledDebug_CE  s    #::	t~t}555Q	KKKNNN IIIJJJJJ ' 	M 	M 	MKK6	8LLLLLLLLL	Mr  c                 L   t                      }|                    | j        | j                   |                     |           	 |                     |           |                     d           d S # t          j        $ r#}| 	                    |ddd           Y d }~d S d }~ww xY w)Nr  testAlreadyCalledDebug_ECr  r  )
r   r   r   r   r  r  r  r   r  r  r  s      r2   r  z,AlreadyCalledTests.testAlreadyCalledDebug_EC#  s    #::	t~t}555A	LLLOOO IIJKKKKK ' 	M 	M 	MKK6)LLLLLLLLL	Mr  c                 L   t                      }|                    | j        | j                   |                     |           	 |                     |           |                     d           d S # t          j        $ r#}| 	                    |ddd           Y d }~d S d }~ww xY w)Nr  testAlreadyCalledDebug_EEr  r  )
r   r   r   r   r  r  r  r   r  r  r  s      r2   r  z,AlreadyCalledTests.testAlreadyCalledDebug_EE.  s    #::	t~t}555A	KKKNNN IIIJJJJJ ' 	L 	L 	LKK6(KKKKKKKKK	Lr  c                 x   t          j        d           t                      }|                    | j        | j                   |                     |           	 |                     |           |                     d           d S # t           j	        $ r%}| 
                    |j                   Y d }~d S d }~ww xY w)NFr  )r   r   r   r   r   r   r  r  r  r  r   r,   r  s      r2   testNoDebuggingz"AlreadyCalledTests.testNoDebugging9  s    5!!!#::	t~t}555Q	LLLOOO IIJKKKKK ' 	% 	% 	%QV$$$$$$$$$	%s   B B9B44B9c                    t          j        d           t                      }|                    d            t          j        d           |                    d            t          j        d           t                      }|                    d            t          j        d           |                    d            d S )NFc                     d S r/   r?   r  s    r2   r   z8AlreadyCalledTests.testSwitchDebugging.<locals>.<lambda>J      d r4   Tc                     d S r/   r?   r  s    r2   r   z8AlreadyCalledTests.testSwitchDebugging.<locals>.<lambda>R  r  r4   )r   r   r   addBothrH   r+  s     r2   testSwitchDebuggingz&AlreadyCalledTests.testSwitchDebuggingE  s     	5!!!$JJ			""###4   	

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

44   			""#####r4   rw   )r<   r=   r>   r   r  r7   r   r   r   rs   r  r  r  r  r  r  r  r  r   r/  r  r  r  r  r  r  r  r  r  r?   r4   r2   r  r    s       ! ! ! !7 7 7 7v  D    f  4    # 4    # 4    + +$ + + + ++ +$ + + + +E E E ED D D DD D D DE E E E+s +# +d3i +3 +SW + + + +-	 -3 -# - -QU - - - -	L 	L 	L 	L	K 	K 	K 	K	L 	L 	L 	L	K 	K 	K 	K
L 
L 
L 
L$ $ $ $ $ $r4   r  c                       e Zd ZddZddZdedefdZdeddfdZdeddfd	Z	dd
Z
ddZddZddZddZddZddZddZddZddZddZddZddZddZdS )DeferredCancellerTestsr*   Nc                 >    d | _         d | _        d | _        d| _        d S r   )r{   r}   r|   cancellerCallCountr   s    r2   r   zDeferredCancellerTests.setUpV  s(    .215/3"#r4   c                 <    |                      | j        d           d S )NrJ  )rg  r  r   s    r2   r  zDeferredCancellerTests.tearDown\  s    d-v66666r4   datac                     || _         |S r/   r   rY   r  s     r2   r   z DeferredCancellerTests._callback`  s    #r4   c                     || _         d S r/   r   r  s     r2   r   z!DeferredCancellerTests._callback2d  s     $r4   re   c                     || _         d S r/   r   )rY   re   s     r2   r   zDeferredCancellerTests._errbackg  s    #r4   c                 $   t                      }|                    | j        | j                   |                                 | j        J |                     | j        j        t          j	                   | 
                    | j                   dS )zy
        A L{Deferred} without a canceller must errback with a
        L{defer.CancelledError} and not callback.
        N)r   r   r   r   r   r}   rT   r   r   r   r   r{   r+  s     r2   test_noCancellerz'DeferredCancellerTests.test_noCancellerj  s}    
 %JJ	t~t}555	


"...,153GHHH$./////r4   c                 d   t                      }|                    | j        | j                   |                                 |                    d           |                     t          j        |j        d           |                     t          j        |j	        t                                 dS )z
        A L{Deferred} without a canceller, when cancelled must allow
        a single extra call to callback, and raise
        L{defer.AlreadyCalledError} if callbacked or errbacked thereafter.
        N)r   r   r   r   r   rH   rm  r   r  r   r  r+  s     r2   !test_raisesAfterCancelAndCallbackz8DeferredCancellerTests.test_raisesAfterCancelAndCallbackv  s     %JJ	t~t}555	


 	


4 	%2AJEEE%2AIy{{KKKKKr4   c                 |   t                      }|                    | j        | j                   |                                 |                    t                                 |                     t          j	        |j
        d           |                     t          j	        |j        t                                 dS )z
        A L{Deferred} without a canceller, when cancelled must allow
        a single extra call to errback, and raise
        L{defer.AlreadyCalledError} if callbacked or errbacked thereafter.
        N)r   r   r   r   r   r   r  rm  r   r  rH   r+  s     r2    test_raisesAfterCancelAndErrbackz7DeferredCancellerTests.test_raisesAfterCancelAndErrback  s     %JJ	t~t}555	


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


,	

4	


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


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


nd&9:::::r4   c                 \   t                      }|                    | j        | j                   |                                 | j        J |                     | j        j        t          j	                   | j        }|                                 | 
                    || j                   dS )z
        Calling cancel multiple times on a deferred with no canceller
        results in a L{defer.CancelledError}. Subsequent calls to cancel
        do not cause an error.
        N)r   r   r   r   r   r}   rT   r   r   r   rd   r  s      r2   test_noCancellerMultipleCancelz5DeferredCancellerTests.test_noCancellerMultipleCancel  s     %JJ	t~t}555	


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


nd&9:::::r4   c                     dt           t                   ddf fd}t          |          }|                     j         j                   |                                  j        J                       j        j        t          j
                    j        }|                                                      | j                                         j        d           dS )a,  
        Verify that calling cancel multiple times on a deferred with a
        canceller that does not errback results in a
        L{defer.CancelledError} and that subsequent calls to cancel do not
        cause an error and that after all that, the canceller was only
        called once.
        r  r*   Nc                 (    xj         dz  c_         d S rD  r  r  rY   s    r2   r   zCDeferredCancellerTests.test_cancellerMultipleCancel.<locals>.cancel      ##q(####r4   	cancellerrA   )r   r7   r   r   r   r   r}   rT   r   r   r   rd   r  )rY   r   r  r  s   `   r2   test_cancellerMultipleCancelz3DeferredCancellerTests.test_cancellerMultipleCancel  s    	)hv& 	)4 	) 	) 	) 	) 	) 	) %v666	t~t}555	


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


nd&9:::0!44444r4   c                     dt           t                   ddf fd}t          |          }|                     j         j                   |                                                       j        d            j        J                       j        j	        t          j                                        t          j        |j        d                                t          j        |j        t!                                 dS )z
        Verify that a L{Deferred} calls its specified canceller when
        it is cancelled, and that further call/errbacks raise
        L{defer.AlreadyCalledError}.
        r  r*   Nc                 (    xj         dz  c_         d S rD  r  r  s    r2   r   z;DeferredCancellerTests.test_simpleCanceller.<locals>.cancel  r  r4   r  rA   )r   r7   r   r   r   r   rT   r  r}   r   r   r   rm  r  rH   r   r  rY   r   r  s   `  r2   test_simpleCancellerz+DeferredCancellerTests.test_simpleCanceller  s    	)hv& 	)4 	) 	) 	) 	) 	) 	) %v666	t~t}555	


0!444"...,153GHHH 	%2AJEEE%2AIy{{KKKKKr4   c                      dt           t                   ddf fd}t          |                               j         j                                                    dS )zQ
        Verify that a canceller is given the correct deferred argument.
        r   r*   Nc                 4                         |            d S r/   )rd   )r   r  rY   s    r2   r   z8DeferredCancellerTests.test_cancellerArg.<locals>.cancel  s    MM"a     r4   r  )r   r7   r   r   r   r   r  s   ` @r2   test_cancellerArgz(DeferredCancellerTests.test_cancellerArg  su    
	!x' 	!D 	! 	! 	! 	! 	! 	! 	! %v666	t~t}555	




r4   c                     dt           t                   ddf fd}t          |          }|                     j         j                   |                    d           |                                                       j        d            	                     j
                                         j        d           dS )zo
        Test that cancelling a deferred after it has been callbacked does
        not cause an error.
        r  r*   Nc                 j    xj         dz  c_         |                     t                                 d S rD  r  r   r:   r  s    r2   r   z?DeferredCancellerTests.test_cancelAfterCallback.<locals>.cancel  3    ##q(##IIlnn%%%%%r4   r  zbiff!r   )r   r7   r   r   r   rH   r   rT   r  r   r}   r{   r  s   `  r2   test_cancelAfterCallbackz/DeferredCancellerTests.test_cancelAfterCallback  s    	&hv& 	&4 	& 	& 	& 	& 	& 	& $f555	t~t}555	

7	


0!444$-...-w77777r4   c                     dt           t                   ddf fd}t          |          }|                     j         j                   |                    t                                 |                                                       j	        d            j
        J                       j
        j        t                                           j                   dS )z
        Test that cancelling a L{Deferred} after it has been errbacked does
        not result in a L{defer.CancelledError}.
        r  r*   Nc                 j    xj         dz  c_         |                     t                                 d S rD  r  r  s    r2   r   z>DeferredCancellerTests.test_cancelAfterErrback.<locals>.cancel  r  r4   r  r   )r   r7   r   r   r   r   r:   r   rT   r  r}   r   r   r{   r  s   `  r2   test_cancelAfterErrbackz.DeferredCancellerTests.test_cancelAfterErrback  s    	&hv& 	&4 	& 	& 	& 	& 	& 	& %v666	t~t}555			,..!!!	


0!444"...,1<@@@$./////r4   c                 V    dt           t                   ddf fd}t          |          }|                     j         j                   |                                                       j        d            j        J                       j        j	        t                     dS )z?
        Test a canceller which errbacks its deferred.
        r  r*   Nc                 j    xj         dz  c_         |                     t                                 d S rD  r  r  s    r2   r   zADeferredCancellerTests.test_cancellerThatErrbacks.<locals>.cancel)  r  r4   r  rA   )r   r7   r   r   r   r   rT   r  r}   r   r:   r  s   `  r2   test_cancellerThatErrbacksz1DeferredCancellerTests.test_cancellerThatErrbacks$  s    
	&hv& 	&4 	& 	& 	& 	& 	& 	& %v666	t~t}555	


0!444"...,1<@@@@@r4   c                 d    dt           t                   ddf fd}t          |          }|                     j         j                   |                                                       j        d                                 j        d            	                     j
                   dS )z<
        Test a canceller which calls its deferred.
        r  r*   Nc                 R    xj         dz  c_         |                     d           d S )NrA   hello!)r  rH   r  s    r2   r   zBDeferredCancellerTests.test_cancellerThatCallbacks.<locals>.cancel9  s/    ##q(##JJx     r4   r  rA   r  )r   r7   r   r   r   r   rT   r  r{   r   r}   r  s   `  r2   test_cancellerThatCallbacksz2DeferredCancellerTests.test_cancellerThatCallbacks4  s    
	!hv& 	!4 	! 	! 	! 	! 	! 	! %v666	t~t}555	


0!444-x888$-.....r4   c                     dt           t                   ddf fd}dt           t                   ddf fd}t          |          t          |          }|                    d           |                    fd           |                                 |                     j         j                                         j	        d            j
        J                       j
        j        t          j                   dS )	z
        Verify that a Deferred, a, which is waiting on another Deferred, b,
        returned from one of its callbacks, will propagate
        L{defer.CancelledError} when a is cancelled.
        r  r*   Nc                 (    xj         dz  c_         d S rD  r  r  s    r2   innerCancelzEDeferredCancellerTests.test_cancelNestedDeferred.<locals>.innerCancelK  r  r4   c                 2                         d           d S NF)rV   r  s    r2   r   z@DeferredCancellerTests.test_cancelNestedDeferred.<locals>.cancelN  s    OOE"""""r4   r  c                     S r/   r?   )r  r  s    r2   r   zBDeferredCancellerTests.test_cancelNestedDeferred.<locals>.<lambda>T  s    1 r4   rA   )r   r7   rH   r   r   r   r   r   rT   r  r}   r   r   r   )rY   r  r   r  r  s   `   @r2   test_cancelNestedDeferredz0DeferredCancellerTests.test_cancelNestedDeferredD  s&   	)8F+ 	) 	) 	) 	) 	) 	) 	)	#hv& 	#4 	# 	# 	# 	# 	# 	# %{;;;$v666	

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


	t~t}5550!444 "...,153GHHHHHr4   rw   )r<   r=   r>   r   r  rs   r   r   r'   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r?   r4   r2   r  r  U  s       $ $ $ $7 7 7 7c c    %s %t % % % %$g $$ $ $ $ $
0 
0 
0 
0L L L L"L L L L"; ; ; ;; ; ; ;$; ; ; ;5 5 5 5,L L L L*
 
 
 
8 8 8 8$0 0 0 0&A A A A / / / / I I I I I Ir4   r  c                       e Zd ZdZddZddZdeeee	f                  fdZ
ddZddZdd	Zdd
ZddZddZddZddZdS )LogTestsz+
    Test logging of unhandled errors.
    r*   Nc                 P    g | _         t          j        | j         j                   dS )z<
        Add a custom observer to observer logging.
        N)cr&   rt   rS   r   s    r2   r   zLogTests.setUpd  s%     (*&&&&&r4   c                 B    t          j        | j        j                   dS )z&
        Remove the observer.
        N)r&   removeObserverr	  rS   r   s    r2   r  zLogTests.tearDownk  s     	46=)))))r4   c                 $    d | j         D             S )Nc                 "    g | ]}|d          
|S )isErrorr?   )r/  r   s     r2   r0  z*LogTests._loggedErrors.<locals>.<listcomp>r  s!    222aQy\2222r4   r	  r   s    r2   _loggedErrorszLogTests._loggedErrorsq  s    22462222r4   c                     |                                  }|                     t          |          d           |d         d                             t                     |                     t                     dS )zV
        Check the output of the log observer to see if the error is present.
        r   rA   r  N)r  rT   rU   r  r'  rc   )rY   c2s     r2   r  zLogTests._checkt  sj     !!R!$$$
1i/000011111r4   c                     t                                          d                               d           t          j                     |                                  dS )z
        Verify that when a L{Deferred} with no references to it is fired,
        and its final result (the one not handled by any callback) is an
        exception, that exception will be logged immediately.
        c                     ddz  S r#  r?   r`  s    r2   r   z(LogTests.test_errorLog.<locals>.<lambda>  
    a r4   rA   N)r   r   rH   gccollectr  r   s    r2   test_errorLogzLogTests.test_errorLog}  sJ     	

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

r4   c                 p    dd} |             t          j                     |                                  dS )zD
        Same as L{test_errorLog}, but with an inner frame.
        r*   Nc                  x    t                      } |                     d            |                     d           d S )Nc                     ddz  S r#  r?   r`  s    r2   r   z`LogTests.test_errorLogWithInnerFrameRef.<locals>._subErrorLogWithInnerFrameRef.<locals>.<lambda>  
    AF r4   rA   )r   r   rH   r_  s    r2   _subErrorLogWithInnerFrameRefzNLogTests.test_errorLogWithInnerFrameRef.<locals>._subErrorLogWithInnerFrameRef  s4    'zzAMM**+++JJqMMMMMr4   rw   r  r  r  )rY   r  s     r2   test_errorLogWithInnerFrameRefz'LogTests.test_errorLogWithInnerFrameRef  sC    
	 	 	 	
 	&%'''

r4   c                 p    dd} |             t          j                     |                                  dS )zQ
        Same as L{test_errorLogWithInnerFrameRef}, plus create a cycle.
        r*   Nc                      t                      } |                     | fd           | | _        |                     d           d S )Nc                     ddz  S r#  r?   )r]  r  s     r2   r   zdLogTests.test_errorLogWithInnerFrameCycle.<locals>._subErrorLogWithInnerFrameCycle.<locals>.<lambda>  r  r4   rA   )r   r   _drH   r_  s    r2   _subErrorLogWithInnerFrameCyclezRLogTests.test_errorLogWithInnerFrameCycle.<locals>._subErrorLogWithInnerFrameCycle  s@    'zzAMMa///000ADJJqMMMMMr4   rw   r  )rY   r$  s     r2    test_errorLogWithInnerFrameCyclez)LogTests.test_errorLogWithInnerFrameCycle  sC    
	 	 	 	 	(')))

r4   c                    t                                          d                               d           t          j                     |                                  |                     dt          | j                             t          j
        | j        d                   }|J d}|                     |                    |          d|           dS )z
        Verify that when a L{Deferred} with no references to it is fired,
        the logged message does not contain a repr of the failure object.
        c                     ddz  S r#  r?   r`  s    r2   r   z.LogTests.test_errorLogNoRepr.<locals>.<lambda>  r  r4   rA   r   Nrr    Expected message starting with: )r   r   rH   r  r  r  rT   rU   r	  r&   rn   rV   ru   )rY   msgr8  s      r2   test_errorLogNoReprzLogTests.test_errorLogNoRepr  s    
 	

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

CKK(((#DF2J//0NN8$$;x;;	
 	
 	
 	
 	
r4   c                 b   dd} |             t          j                     |                                  |                     dt	          | j                             t          j        | j        d                   }|J d}|                     |	                    |          d|           dS )	z
        Verify that when a L{Deferred} with no references to it is fired,
        the logged message includes debug info if debugging on the deferred
        is enabled.
        r*   Nc                      t                      } d| _        |                     d            |                     d           d S )NTc                     ddz  S r#  r?   r`  s    r2   r   z?LogTests.test_errorLogDebugInfo.<locals>.doit.<locals>.<lambda>  r  r4   rA   )r   debugr   rH   r_  s    r2   doitz-LogTests.test_errorLogDebugInfo.<locals>.doit  s;    'zzAAGMM**+++JJqMMMMMr4   r   r(  z
(debug:  Ir)  rw   )
r  r  r  rT   rU   r	  r&   rn   rV   ru   )rY   r0  r*  r8  s       r2   test_errorLogDebugInfozLogTests.test_errorLogDebugInfo  s    	 	 	 	 	

CKK(((#DF2J//NN8$$;x;;	
 	
 	
 	
 	
r4   c                    t                      }|                    d            |                    d           g }g }|                    |j        |j                   |                     |g            |                     t          |          d           |d                             t                     ~~~t          j
                     |                     |                                 g            dS )z
        If one Deferred with an error result is returned from a callback on
        another Deferred, when the first Deferred is garbage collected it does
        not log its error.
        c                 D    t          j        t          d                    S )Nzoop)r   r  ra   r  s    r2   r   z3LogTests.test_chainedErrorCleanup.<locals>.<lambda>  s    %*\&-A-A"B"B r4   NrA   r   )r   r   rH   r   rS   rT   rU   r  r  r  r  r  )rY   r  rG  ri   s       r2   test_chainedErrorCleanupz!LogTests.test_chainedErrorCleanup  s     %JJ	BBCCC	

4 ! "	w~v}555"%%%Va(((q	y!!! VQ 	
++--r22222r4   c                    t          j        t          d                    }t                      |J |                    fd           dx}t          j                     |                     |                                 g            dS )z
        If a Deferred with a failure result has an errback which chains it to
        another Deferred, the initial failure is cleared by the errback so it is
        not logged.
        zoh noNc                     S r/   r?   )r6  goods    r2   r   z6LogTests.test_errorClearedByChaining.<locals>.<lambda>  s    t r4   )	r   r  r  r   rR   r  r  rT   r  )rY   badr8  s     @r2   test_errorClearedByChainingz$LogTests.test_errorClearedByChaining  s     ).
9W3E3E(F(F)1++++,,, s

++--r22222r4   rw   )r<   r=   r>   r]   r   r  r   r   rs   r   r  r  r  r  r%  r+  r1  r5  r:  r?   r4   r2   r  r  _  s        ' ' ' '* * * *3tDcN3 3 3 3 32 2 2 2          
 
 
 
&
 
 
 
43 3 3 383 3 3 3 3 3r4   r  c                   B    e Zd ZddZddZdee         ddfdZddZdS )	DeferredListEmptyTestsr*   Nc                     d| _         d S r   )callbackRanr   s    r2   r   zDeferredListEmptyTests.setUp	  s    r4   c                 X    t          g           }|                    | j                   dS )zTesting empty DeferredList.N)r   r   cb_empty)rY   r   s     r2   testDeferredListEmptyz,DeferredListEmptyTests.testDeferredListEmpty	  s)    "
t}%%%%%r4   resc                 @    d| _         |                     g |           d S rD  )r>  rT   )rY   rB  s     r2   r@  zDeferredListEmptyTests.cb_empty
	  s&    S!!!!!r4   c                 <    |                      | j        d           d S )NzCallback was never run.)rV   r>  r   s    r2   r  zDeferredListEmptyTests.tearDown	  s     (*CDDDDDr4   rw   )	r<   r=   r>   r   rA  r   r7   r@  r  r?   r4   r2   r<  r<  	  s|           & & & &
"DL "T " " " "E E E E E Er4   r<  c                   v    e Zd ZdeddfdZddZddZddZddZdd	Z	dd
Z
ddZddZddZddZddZdS )OtherPrimitivesTestsrf   r*   Nc                 &    | xj         dz  c_         d S rD  counter)rY   rf   s     r2   _incrzOtherPrimitivesTests._incr	  s    r4   c                     d| _         d S r   rH  r   s    r2   r   zOtherPrimitivesTests.setUp	  s    r4   c                 P   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}|                    |||          }|                     |j                   |                     |           |                    |           |                                                    | j                   |                     |j                   |                     | j        d           |                    |           |                     |           |                     |j                   |                     | j        d           |                                                    |          }|                                 |                     t&                     |                     t)          t&                    j        t,          j                   |                                 | 	                    |j                   d S )
NrA   r   resultValuereturnValuer*   c                     | |S r/   r?   )rM  rN  rf   s     r2   helperz-OtherPrimitivesTests.testLock.<locals>.helper4	  s     Fr4   )rM  rN  r  r/   )r   acquirer   rJ  rV   lockedrT   rI  releaser   rm  rm  runr7   r   rH   r  r   r(  r'   r   r   r   r   )	rY   lockfirstUniquesecondUniquecontrolDeferredrP  resultDeferredr  rf   s	           @r2   testLockzOtherPrimitivesTests.testLock	  s   ~~""4:...$$$q)))""4:...$$$q)))$$$q)))%%%q))))TX...hhxx,4JJ#'	 	 	V 	v 	 	 	 	 	 	
  " 
 
 	$$$---""6***""4:...$$$q)))  ......$$$q)))LLNN""6**	


fg...gv..3U5IJJJ%%%%%r4   c                      dt           ddf fd}t                      }|                                }|                    |           |                                 dS )z
        When canceling a L{Deferred} from a L{DeferredLock} that already
        has the lock, the cancel should have no effect.
        r)   r*   Nc                 2                         d           d S NzUnexpected errback call!r  r)   rY   s    r2   failOnErrbackzHOtherPrimitivesTests.test_cancelLockAfterAcquired.<locals>.failOnErrbackX	      II011111r4   )r'   r   rQ  rR   r   )rY   r`  rU  r  s   `   r2   test_cancelLockAfterAcquiredz1OtherPrimitivesTests.test_cancelLockAfterAcquiredR	  sj    	2W 	2 	2 	2 	2 	2 	2 	2 ~~LLNN	]###	




r4   c                     t                      }|                                 |                                }|                                 |                     |t          j                   dS )z
        When canceling a L{Deferred} from a L{DeferredLock} that does not
        yet have the lock (i.e., the L{Deferred} has not fired), the cancel
        should cause a L{defer.CancelledError} failure.
        N)r   rQ  r   r\   r   r   )rY   rU  r  s      r2   test_cancelLockBeforeAcquiredz2OtherPrimitivesTests.test_cancelLockBeforeAcquired`	  sS     ~~LLNN	


##Au';<<<<<r4   c                 p   d}t          |          }t                      d dt          dt          d          ffd}g }t                      }|                    ||          }|                    |j                   |                    | j                   |                     |g            |                     |                               d            | 	                    |
                                           |                     | j        d           d| _        t          dd|z             D ]I}|                                                    | j                   |                     | j        |           Jg dt          dd ffd	}dt          dd ffd
}	|                                                    ||	          }
|
                                 |                     dg           |                                                    | j                   |                     | j        |           |                                 |                     | j        |dz              t          dd|z             D ]4}|                                 |                     | j        |dz              5d S )Nr{  argr*   c                     | S r/   r?   )rf  rX  	helperArgs    r2   rP  z2OtherPrimitivesTests.testSemaphore.<locals>.helpert	  s    I""r4   )rf  rA   r   r0  c                 2                         d           d S r  r   r0  r  s    r2   r  z0OtherPrimitivesTests.testSemaphore.<locals>.fail	  s    NN5!!!!!r4   c                 2                         d           d S r  r   rj  s    r2   r  z3OtherPrimitivesTests.testSemaphore.<locals>.succeed	  s    NN4     r4   T)r    r   r7   rT  r   rS   rJ  rT   rH   r   r9  rI  r8  rQ  r   r   rS  )rY   NsemrP  rG  uniqueObjectrY  ir  r  r  rX  rh  r  s              @@@r2   testSemaphorez"OtherPrimitivesTests.testSemaphorel	  s   ""*2** 		# 	#8D> 	# 	# 	# 	# 	# 	# 	#
 !#xx\::""7>222""4:..."%%%L111  &&&'++--(((q)))q!a% 	. 	.AKKMM%%dj111T\1----	"F 	"t 	" 	" 	" 	" 	" 	"	!v 	!$ 	! 	! 	! 	! 	! 	! KKMM&&tW55	


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




r4   c                     t          d          }|                                 |                                }|                                 |                     |t          j                   dS )z
        When canceling a L{Deferred} from a L{DeferredSemaphore} that does
        not yet have the semaphore (i.e., the L{Deferred} has not fired),
        the cancel should cause a L{defer.CancelledError} failure.
        rA   N)r    rQ  r   r\   r   r   )rY   rm  r  s      r2   "test_cancelSemaphoreBeforeAcquiredz7OtherPrimitivesTests.test_cancelSemaphoreBeforeAcquired	  sW      ""KKMM	


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




r4   c                     t                                                      }|                                                      |t          j                   dt          dt          d         f fd}|                    |           g }|                    |j	                    
                    t          |          d           dS )a  
        When canceling a L{Deferred} from a L{DeferredQueue} that does not
        have a result (i.e., the L{Deferred} has not fired), the cancel
        causes a L{defer.CancelledError} failure. If the queue has a result
        later on, it doesn't try to fire the deferred.
        ignorer*   Nc                                          d                                                                j        d           S r/   )r}  r{  r   rd   )r  r  rY   s    r2   r   z9OtherPrimitivesTests.test_cancelQueueAfterGet.<locals>.cb
  s4     IIdOOO99;;**4=$???r4   rA   )r   r{  r   r\   r   r   r7   r   r   rS   rT   rU   )rY   r  r   doner  s   `   @r2   test_cancelQueueAfterGetz-OtherPrimitivesTests.test_cancelQueueAfterGet	  s     &3__IIKK	


##Au';<<<	@v 	@(4. 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	
b	dk"""TA&&&&&r4   rw   )r<   r=   r>   r7   rJ  r   rZ  rb  rd  rp  rr  ru  rw  r  r  r  r?   r4   r2   rF  rF  	  s       F t       7& 7& 7& 7&r   
= 
= 
= 
=12 12 12 12f= = = =   
= 
= 
= 
=#; #; #; #;J   ' ' ' ' ' 'r4   rF  c                       e Zd ZdZddZded         fdZded         fdZded         fdZddZ	ded         fd	Z
ded         fd
ZddZddZdS )DeferredFilesystemLockTestsz8
    Test the behavior of L{DeferredFilesystemLock}
    r*   Nc                     t                      | _        t          |                                 | j                  | _        d S )N)	scheduler)r%   clockr   mktemprU  r   s    r2   r   z!DeferredFilesystemLockTests.setUp
  s/    WW
*4;;==DJOOO			r4   c                 8    | j                             d          S )zI
        Test that the lock can be acquired when no lock is held
        rA   timeout)rU  deferUntilLockedr   s    r2   test_waitUntilLockedWithNoLockz:DeferredFilesystemLockTests.test_waitUntilLockedWithNoLock
  s     y))!)444r4   c                    |                      | j                                                   | j                            d          }|                     |t          j                   | j                            dgdz             |S )zs
        Test that the lock can not be acquired when the lock is held
        for longer than the timeout.
        g      @r  rA   rb  )rV   rU  r  assertFailurer   TimeoutErrorr  pumpr+  s     r2   %test_waitUntilLockedWithTimeoutLockedzADeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutLocked
  sr    
 		(()))I&&s&331e0111
b!!!r4   c                 d    dt           ddf fd}                      j                                                    j                            d j        j                    j                            d          }|                    |            j                            dgdz             |S )z|
        Test that a lock can be acquired while a lock is held
        but the lock is unlocked before our timeout.
        r)   r*   Nc                 p    |                      t          j                                       d           d S )NzShould not have timed out)r  r   r  r  r_  s    r2   	onTimeoutzVDeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutUnlocked.<locals>.onTimeout0
  s1    FF5%&&&II122222r4   rA   rb  r  )	r'   rV   rU  r  	callLaterunlockr  rR   r  )rY   r  r  s   `  r2   'test_waitUntilLockedWithTimeoutUnlockedzCDeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutUnlocked*
  s    	3 	3T 	3 	3 	3 	3 	3 	3 		(()))
Q	 0111I&&r&22	Y
b!!!r4   c                     t          |                                           }|                     |j        t                     dS )zE
        Test that the default scheduler is set up properly.
        N)r   r  rT   
_schedulerr   rY   rU  s     r2   test_defaultSchedulerz1DeferredFilesystemLockTests.test_defaultScheduler>
  s7     &dkkmm44'22222r4   c                 Z   | j                                           | j                            d| j         j                   | j                                         }| j                                         }|                     |t          j                   | j                            d           |S )z|
        Test that an appropriate exception is raised when attempting
        to use deferUntilLocked concurrently.
        rA   )	rU  r  r  r  r  r  r   AlreadyTryingToLockErroradvancer2  s      r2   test_concurrentUsagez0DeferredFilesystemLockTests.test_concurrentUsageF
  s    
 		
Q	 0111Y''))Y''))2u=>>>
1	r4   c                 H    dt           dt          d         f fd} j                                          j                            d j        j                    j                                        }|                    |            j                            d           |S )zO
        Test that a DeferredFilesystemLock can be used multiple times
        r  r*   Nc                 l    j                                          j                                         }|S r/   )rU  r  r  )r  r  rY   s     r2   lockAquiredzDDeferredFilesystemLockTests.test_multipleUsages.<locals>.lockAquired\
  s/    I	**,,AHr4   rA   )	r7   r   rU  r  r  r  r  r   r  )rY   r  r  s   `  r2   test_multipleUsagesz/DeferredFilesystemLockTests.test_multipleUsagesW
  s    
	V 	 	 	 	 	 	 	
 		
Q	 0111I&&((	k"""
1r4   c                 ~   | j                                           | j                                         }| j         j        }|J |                                 |                     |                                           |                     | j         j                   |                     |t          j	                   dS )z
        When cancelling a L{Deferred} returned by
        L{DeferredFilesystemLock.deferUntilLocked}, the
        L{DeferredFilesystemLock._tryLockCall} is cancelled.
        N)
rU  r  _tryLockCallr   r   activer   r   r   r   )rY   rD   tryLockCalls      r2   test_cancelDeferUntilLockedz7DeferredFilesystemLockTests.test_cancelDeferUntilLockedk
  s     		9--//i,&&&++--...$)0111Xu';<<<<<r4   c                    | j                                           | j                             d          }| j         j        }|J |                                 |                     |                                           |                     | j         j                   |                     |t          j	                   dS )z
        When cancel a L{Deferred} returned by
        L{DeferredFilesystemLock.deferUntilLocked}, if the timeout is
        set, the timeout call will be cancelled.
        rA   r  N)
rU  r  _timeoutCallr   r   r  r   r   r   r   )rY   rD   timeoutCalls      r2   &test_cancelDeferUntilLockedWithTimeoutzBDeferredFilesystemLockTests.test_cancelDeferUntilLockedWithTimeoutz
  s     		9--a-88i,&&&++--...$)0111Xu';<<<<<r4   rw   )r<   r=   r>   r]   r   r   r  r  r  r  r  r  r  r  r?   r4   r2   r  r  
  s        P P P P5 5 5 5 5x~    $    (3 3 3 3htn    "Xd^    (= = = == = = = = =r4   r  vtc                     dS )zj
    Private function to be used to pass as an alternate onTimeoutCancel value
    to timeoutDeferred
    
OVERRIDDENr?   )r  r  s     r2   _overrideFuncr  
  s	    
 <r4   c                       e Zd ZdZddZddZddZddZddZdd	Z	dd
Z
ddZddZddZddZddZddZddZddZddZddZddZdS )DeferredAddTimeoutTestsz7
    Tests for the function L{Deferred.addTimeout}
    r*   Nc                    t                      }|                    dt                                 |                    d            |                    d           |                     d|                     |                     dS )zu
        L{Deferred.addTimeout} returns its own L{Deferred} so it
        can be called in a callback chain.
        r_  c                     dS )Nr  r?   r$  s    r2   r   z?DeferredAddTimeoutTests.test_timeoutChainable.<locals>.<lambda>
  s     r4   Nr  )r   
addTimeoutr%   r   rH   rT   r   r+  s     r2   test_timeoutChainablez-DeferredAddTimeoutTests.test_timeoutChainable
  sw    
 %JJ	Q   	&&'''	

4!5!5a!8!899999r4   c                    t                      }t                      }|                    d|           t                      ddt          dt          d         ffd}|                    |           |                    d           |                     d           |                     d           |                    d           | 	                               dS )z
        The L{Deferred} callbacks with the result if it succeeds before
        the timeout. No cancellation happens after the callback either,
        which could also cancel inner deferreds.
        rb  NrG  r*   c                     | S r/   r?   rG  dCallbackedinnerDeferreds    r2   
onCallbackzKDeferredAddTimeoutTests.test_successResultBeforeTimeout.<locals>.onCallback
      !K  r4   rc  )
r%   r   r  rs   r   rH   assertIsNotrT   r  r  rY   r  r  r  r  r  s       @@r2   test_successResultBeforeTimeoutz7DeferredAddTimeoutTests.test_successResultBeforeTimeout
  s     #::	R )1

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

9 	{+++i000 	bM*****r4   c                    t                      }t                      }|                    d|t                     t                      ddt          dt          d         ffd}|                    |           |                    d           |                     d           |                     d           |	                    d           | 
                               dS )a  
        The L{Deferred} callbacks with the result if it succeeds before
        the timeout, even if a custom C{onTimeoutCancel} function is provided.
        No cancellation happens after the callback either, which could also
        cancel inner deferreds.
        rb  onTimeoutCancelNrG  r*   c                     | S r/   r?   r  s    r2   r  zQDeferredAddTimeoutTests.test_successResultBeforeTimeoutCustom.<locals>.onCallback
  r  r4   rc  )r%   r   r  r  rs   r   rH   r  rT   r  r  r  s       @@r2   %test_successResultBeforeTimeoutCustomz=DeferredAddTimeoutTests.test_successResultBeforeTimeoutCustom
  s     #::	R>>> )1

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

9 	{+++i000 	bM*****r4   c                    t                      }t                      }|                    d|           t                      dt          d          }dt          dt          d         ffd}|                    |           |                    |           J |                     t                     |                     j	        |           |
                    d           |                                dS )z
        The L{Deferred} errbacks with the failure if it fails before the
        timeout. No cancellation happens after the errback either, which
        could also cancel inner deferreds.
        rb  Nr  r)   r*   c                     | S r/   r?   r)   
dErrbackedr  s    r2   	onErrbackzDDeferredAddTimeoutTests.test_failureBeforeTimeout.<locals>.onErrback
      J  r4   rc  )r%   r   r  rF  r'   rR   r   r(  rd   rX   r  r  rY   r  r  re   r  r  r  s        @@r2   test_failureBeforeTimeoutz1DeferredAddTimeoutTests.test_failureBeforeTimeout
  s    $JJ	R )1

(,
6""	! 	!Xd^ 	! 	! 	! 	! 	! 	! 	!
 	
Y			% %%%j'222j&... 	bM*****r4   c                    t                      }t                      }|                    d|t                     t                      dt	          d          }dt
          dt          d         ffd}|                    |           |                    |           J |                     t
                     | 	                    j
        |           |                    d           |                                dS )	a  
        The L{Deferred} errbacks with the failure if it fails before the
        timeout, even if using a custom C{onTimeoutCancel} function.
        No cancellation happens after the errback either, which could also
        cancel inner deferreds.
        rb  r  Nr  r)   r*   c                     | S r/   r?   r  s    r2   r  zJDeferredAddTimeoutTests.test_failureBeforeTimeoutCustom.<locals>.onErrback  r  r4   rc  )r%   r   r  r  rF  r'   rR   r   r(  rd   rX   r  r  r  s        @@r2   test_failureBeforeTimeoutCustomz7DeferredAddTimeoutTests.test_failureBeforeTimeoutCustom  s    $JJ	R>>> )1

(,
6""	! 	!Xd^ 	! 	! 	! 	! 	! 	! 	!
 	
Y			% %%%j'222j&... 	bM*****r4   c                     t                      }t                      }|                    d|           |                     |           |                    d           |                     |t          j                   dS )z
        The L{Deferred} by default errbacks with a L{defer.TimeoutError}
        if it times out before callbacking or errbacking.
        rb  rc  N)r%   r   r  r  r  r   r   r  rY   r  r  s      r2   test_timedOutz%DeferredAddTimeoutTests.test_timedOut,  so    
 $JJ	RAbQ 233333r4   c                    t                      }t                      }|                    d|t                     |                     |           |                    d           |                     d|                     |                     dS )a1  
        If a custom C{onTimeoutCancel] function is provided, the
        L{Deferred} returns the custom function's return value if the
        L{Deferred} times out before callbacking or errbacking.
        The custom C{onTimeoutCancel} function can return a result instead of
        a failure.
        rb  r  rc  r  Nr%   r   r  r  r  r  rT   r   r  s      r2   test_timedOutCustomz+DeferredAddTimeoutTests.test_timedOutCustom:  s~     $JJ	R>>>Abt';';A'>'>?????r4   c                    t                      }t          d           }|                    d|           |                     |           |                    d           |                     |                     |          d           dS )z
        If a cancellation function is provided when the L{Deferred} is
        initialized, the L{Deferred} returns the cancellation value's
        non-failure return value when the L{Deferred} times out.
        c                 ,    |                      d          S )NI was cancelled!rG   r  s    r2   r   zLDeferredAddTimeoutTests.test_timedOutProvidedCancelSuccess.<locals>.<lambda>R  s    ajj9K.L.L r4   rb  rc  r  N)r%   r   r  r  r  rT   r   r  s      r2   "test_timedOutProvidedCancelSuccessz:DeferredAddTimeoutTests.test_timedOutProvidedCancelSuccessK  s     #$L$LMM	RAb--a002DEEEEEr4   c                 R   t                      }t          d          t          fd          }|                    d|           |                     |           |                    d           |                     |t                    }|                     |j                   dS )z
        If a cancellation function is provided when the L{Deferred} is
        initialized, the L{Deferred} returns the cancellation value's
        non-L{CanceledError} failure when the L{Deferred} times out.
        what!c                 .    |                                S r/   )r   )r	  re   s    r2   r   zLDeferredAddTimeoutTests.test_timedOutProvidedCancelFailure.<locals>.<lambda>b  s    qyy/?/? r4   rb  rc  N)	r%   rF  r   r  r  r  r   rd   rX   )rY   r  r  r)   re   s       @r2   "test_timedOutProvidedCancelFailurez:DeferredAddTimeoutTests.test_timedOutProvidedCancelFailureZ  s     7##$%?%?%?%?@@	RAb  J//agu%%%%%r4   c                    t                      }t                      }|                    d|           t                      ddt          dt          d         ffd}|                    |           |                                 J |                     t                     |                     j        t          j
                   |                    d           |                                dS )a-  
        If the L{Deferred} is manually cancelled before the timeout, it
        is not re-cancelled (no L{AlreadyCancelled} error, and also no
        canceling of inner deferreds), and the default C{onTimeoutCancel}
        function is not called, preserving the original L{CancelledError}.
        rb  Nr)   r*   c                     | S r/   r?   r)   	dCanceledr  s    r2   r  zCDeferredAddTimeoutTests.test_cancelBeforeTimeout.<locals>.onErrback{      I  r4   rc  )r%   r   r  r'   rR   r   r(  rd   r   r   r   r  r  rY   r  r  r  r  r  s       @@r2   test_cancelBeforeTimeoutz0DeferredAddTimeoutTests.test_cancelBeforeTimeoutk  s     $JJ	R )1

		! 	!Xd^ 	! 	! 	! 	! 	! 	! 	!
 	
Y	


 $$$i111ine&:;;; 	bM*****r4   c                     t                      }t                      }|                    d|t                     t                      ddt          dt          d         ffd}|                    |           |                                 J |                     t                     |                     j	        t          j                   |                    d           |                                dS )a,  
        If the L{Deferred} is manually cancelled before the timeout, it
        is not re-cancelled (no L{AlreadyCancelled} error, and also no
        canceling of inner deferreds), and the custom C{onTimeoutCancel}
        function is not called, preserving the original L{CancelledError}.
        rb  r  Nr)   r*   c                     | S r/   r?   r  s    r2   r  zIDeferredAddTimeoutTests.test_cancelBeforeTimeoutCustom.<locals>.onErrback  r  r4   rc  )r%   r   r  r  r'   rR   r   r(  rd   r   r   r   r  r  r  s       @@r2   test_cancelBeforeTimeoutCustomz6DeferredAddTimeoutTests.test_cancelBeforeTimeoutCustom  s    $JJ	R>>> )1

		! 	!Xd^ 	! 	! 	! 	! 	! 	! 	!
 	
Y	


 $$$i111ine&:;;; 	bM*****r4   c                 "   t                      }t          d           }|                    d|t                     |                     |           |                    d           |                     d|                     |                     dS )zu
        A custom translation function can handle a L{Deferred} with a
        custom cancellation function.
        c                 F    |                      t          d                    S )Nr  )r   rF  r  s    r2   r   zVDeferredAddTimeoutTests.test_providedCancelCalledBeforeTimeoutCustom.<locals>.<lambda>  s    qyyG9L9L/M/M r4   rb  r  rc  r  Nr  r  s      r2   ,test_providedCancelCalledBeforeTimeoutCustomzDDeferredAddTimeoutTests.test_providedCancelCalledBeforeTimeoutCustom  s    
 $%M%MNN	R>>>Abt';';A'>'>?????r4   c                    t                      }t                      }ddt          dt          ffd}|                    |           |                    d|           |                    d           J |                     t                     |                     j        t          j	                   | 
                    |t          j                   dS )a  
        An errback added before a timeout is added errbacks with a
        L{defer.CancelledError} when the timeout fires.  If the
        errback returns the L{defer.CancelledError}, it is translated
        to a L{defer.TimeoutError} by the timeout implementation.
        Nr)   r*   c                     | | S r/   r?   r)   r  s    r2   r   zGDeferredAddTimeoutTests.test_errbackAddedBeforeTimeout.<locals>.errback      JHr4   rb  rc  )r%   r   r'   rR   r  r  r(  rX   r   r   r   r  rY   r  r  r   r  s       @r2   test_errbackAddedBeforeTimeoutz6DeferredAddTimeoutTests.test_errbackAddedBeforeTimeout  s     $JJ
	w 	7 	 	 	 	 	 	
 	
W	Rb%%%j'222j.0DEEEQ 233333r4   c                    t                      }t                      }ddt          ddffd}|                    |           |                    d|           |                    d           J |                     t                     |                     j        t          j	                   | 
                    |           dS )z
        An errback added before a timeout is added errbacks with a
        L{defer.CancelledError} when the timeout fires.  If the
        errback suppresses the L{defer.CancelledError}, the deferred
        successfully completes.
        Nr)   r*   c                 J    | |                      t          j                   d S r/   )r  r   r   r  s    r2   r   z]DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellation.<locals>.errback  s#    JFF5'(((((r4   rb  rc  )r%   r   r'   rR   r  r  r(  rX   r   r   r   r  s       @r2   4test_errbackAddedBeforeTimeoutSuppressesCancellationzLDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellation  s     $JJ
	)w 	)4 	) 	) 	) 	) 	) 	)
 	
W	Rb%%%j'222j.0DEEEQr4   c                    t                      }t                      }ddt          dt          ffd}|                    |           |                    d|t
                     |                    d           J |                     t                     |                     j        t          j
                   |                     d|                     |                     dS )a  
        An errback added before a timeout is added with a custom
        timeout function errbacks with a L{defer.CancelledError} when
        the timeout fires.  The timeout function runs if the errback
        returns the L{defer.CancelledError}.
        Nr)   r*   c                     | | S r/   r?   r  s    r2   r   zMDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutCustom.<locals>.errback  r  r4   rb  rc  r  r%   r   r'   rR   r  r  r  r(  rX   r   r   rT   r   r  s       @r2   $test_errbackAddedBeforeTimeoutCustomz<DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutCustom  s     $JJ
	w 	7 	 	 	 	 	 	
 	
W	R...b%%%j'222j.0DEEEt';';A'>'>?????r4   c                    t                      }t                      }ddt          ddffd}|                    |           |                    d|t
                     |                    d           J |                     t                     |                     j        t          j
                   |                     d|                     |                     dS )a  
        An errback added before a timeout is added with a custom
        timeout function errbacks with a L{defer.CancelledError} when
        the timeout fires.  The timeout function runs if the errback
        suppresses the L{defer.CancelledError}.
        Nr)   r*   c                     | d S r/   r?   r  s    r2   r   zcDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationCustom.<locals>.errback  s    JJJr4   rb  rc  r  r  r  s       @r2   :test_errbackAddedBeforeTimeoutSuppressesCancellationCustomzRDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationCustom  s     $JJ
	w 	4 	 	 	 	 	 	 	
W	R...b%%%j'222j.0DEEEt';';A'>'>?????r4   c                 x   t                      }dt          fd          }ddt          dt          ffd}|                    |           |                    d|           |                    d           |                                |                     |                     |                     dS )	a*  
        Given a deferred with a cancellation function that resumes the
        callback chain, a callback that is added to the deferred
        before a timeout is added to runs when the timeout fires.  The
        deferred completes successfully, without a
        L{defer.TimeoutError}.
        r  c                 .    |                                S r/   rG   r  r  s    r2   r   zSDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout.<locals>.<lambda>8      ajj.A.A r4   NrX   r*   c                     | | S r/   r?   rX   r  s    r2   rH   zSDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout.<locals>.callback<      KLr4   rb  rc  )	r%   r   rs   r   r  r  rT   rd   r   rY   r  r  rH   r  r  s       @@r2   )test_callbackAddedToCancelerBeforeTimeoutzADeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout.  s     #$A$A$A$ABB	C 	C 	 	 	 	 	 	
 	
h	Rbg...gt33A6677777r4   c                    t                      }dt          fd          }ddt          dt          ffd}|                    |           |                    d|t
                     |                    d	           |                                |                     d
|                     |                     dS )ad  
        Given a deferred with a cancellation function that resumes the
        callback chain, a callback that is added to the deferred
        before a timeout is added to runs when the timeout fires.  The
        deferred completes successfully, without a
        L{defer.TimeoutError}.  The timeout's custom timeout function
        also runs.
        r  c                 .    |                                S r/   rG   r	  s    r2   r   zYDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom.<locals>.<lambda>U  r
  r4   NrX   r*   c                     | | S r/   r?   r  s    r2   rH   zYDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom.<locals>.callbackY  r  r4   rb  r  rc  r  )	r%   r   rs   r   r  r  r  rT   r   r  s       @@r2   /test_callbackAddedToCancelerBeforeTimeoutCustomzGDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustomJ  s     #$A$A$A$ABB	C 	C 	 	 	 	 	 	
 	
h	R>>>bg...t';';A'>'>?????r4   rw   )r<   r=   r>   r]   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r?   r4   r2   r  r  
  s        	: 	: 	: 	:+ + + +B +  +  +  +D!+ !+ !+ !+F"+ "+ "+ "+H4 4 4 4@ @ @ @"F F F F& & & &"!+ !+ !+ !+F!+ !+ !+ !+F@ @ @ @4 4 4 48       8@ @ @ @8@ @ @ @68 8 8 88@ @ @ @ @ @r4   r  c                   2    e Zd ZdZddZddZddZddZdS )	EnsureDeferredTestsz&
    Tests for L{ensureDeferred}.
    r*   Nc                 l    t                      }t          |          }|                     ||           dS )zK
        L{ensureDeferred} will pass through a Deferred unchanged.
        N)r   r$   rd   r2  s      r2   test_passesThroughDeferredsz/EnsureDeferredTests.test_passesThroughDeferredsm  s4     &ZZBb"r4   c                     |                      t          j                  5  t          d           ddd           dS # 1 swxY w Y   dS )zr
        Passing L{ensureDeferred} a non-coroutine and a non-Deferred will
        raise a L{ValueError}.
        	somethingN)rm  r   rn  r$   r   s    r2   'test_willNotAllowNonDeferredOrCoroutinez;EnsureDeferredTests.test_willNotAllowNonDeferredOrCoroutineu  s    
 u788 	( 	(;'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(s   =AAc                    dt           fd} |            }|                     |t          j                   t	          |          }|                     |t
                     |                     |          }|                     |d           dS )zM
        L{ensureDeferred} will turn a coroutine into a L{Deferred}.
        r*   c                  B   K   t          j        d          } |  d {V }|S Nr  )r   r  r  rB  s     r2   rT  z=EnsureDeferredTests.test_ensureDeferredCoroutine.<locals>.run  s+      e$$A''''''CJr4   r  N)rs   r(  typesCoroutineTyper$   r   r   rT   rY   rT  r0  r  rB  s        r2   test_ensureDeferredCoroutinez0EnsureDeferredTests.test_ensureDeferredCoroutine}  s    
	3 	 	 	 	 CEEa!4555 1a*** ""1%%e$$$$$r4   c                 V   dt           t          t                   dt          f         fd} |            }|                     |t          j                   t          |          }|                     |t                     |                     |          }|                     |d           dS )zX
        L{ensureDeferred} will turn a yield-from coroutine into a L{Deferred}.
        r*   Nc               3   h   K   t          j        d          } t          t          | E d {V           }|S r  )r   r  r   rs   r  s     r2   rT  z=EnsureDeferredTests.test_ensureDeferredGenerator.<locals>.run  s5      e$$As\\\\\\++CJr4   r  )	r   r   rs   r(  r  GeneratorTyper$   r   rT   r!  s        r2   test_ensureDeferredGeneratorz0EnsureDeferredTests.test_ensureDeferredGenerator  s    
	Yx}dC78 	 	 	 	 CEEa!4555 *!,,a*** ""1%%e$$$$$r4   rw   )r<   r=   r>   r]   r  r  r"  r&  r?   r4   r2   r  r  h  sn            ( ( ( (% % % %,% % % % % %r4   r  c                       e Zd ZdZddZdS )TimeoutErrorTestsz1
    L{twisted.internet.defer} timeout code.
    r*   Nc                    t                      }t          j        |           |                     |t          j                   |                     | j        g          }|                     t          |          d           | 	                    |d         d         t                     |                     |d         d         d           dS )zB
        L{twisted.internet.defer.timeout} is deprecated.
        rA   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_deprecatedTimeoutrT   rU   rd   r  )rY   rD   warningsShowns      r2   r*  z(TimeoutErrorTests.test_deprecatedTimeout  s     $,::h8U%7888**D,G+HII]++Q///mA&z24FGGG!Y'M	
 	
 	
 	
 	
r4   rw   )r<   r=   r>   r]   r*  r?   r4   r2   r(  r(    s2         
 
 
 
 
 
r4   r(  loopc                 b    |                      | j                   |                                  dS )z
    Tickle an asyncio event loop to call all of the things scheduled with
    call_soon, inasmuch as this can be done via the public API.

    @param loop: The asyncio event loop to flush the previously-called
        C{call_soon} entries from.
    N)	call_soonstoprun_foreverr,  s    r2   callAllSoonCallsr2    s0     	NN49r4   c                   F    e Zd Zd
dZd
dZd
dZd
dZd
dZd
dZd
d	Z	dS )DeferredFutureAdapterTestsr*   Nc                    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   asFuturerT   r  rH   r2  r   rf   )rY   r  r,  aFutures       r2   test_asFuturez(DeferredFutureAdapterTests.test_asFuture  s    
 $::**T""///	

2--a00$777))2.....r4   c                    ddt           t                   ddff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                     dd S r  r?   )r:  r  s    r2   cancelerzFDeferredFutureAdapterTests.test_asFutureCancelFuture.<locals>.canceler  s    FFFr4   )r   r7   r   r6  r   r2  rV   rT   r   rm  r   rf   )rY   r<  r  r,  r7  r  s        @r2   test_asFutureCancelFuturez4DeferredFutureAdapterTests.test_asFutureCancelFuture  s     	Xf- 	$ 	 	 	 	 	 	 %X..**T""--a00$777.'.99999r4   c                 r   dt           t                   ddf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                 0    |                      d           d S )Nr}  rG   )r:  s    r2   r<  zGDeferredFutureAdapterTests.test_asFutureSuccessCancel.<locals>.canceler  s    OOAr4   )r   r7   r   r6  r   r2  rT   r   rm  r   rf   )rY   r<  r  r,  r7  s        r2   test_asFutureSuccessCancelz5DeferredFutureAdapterTests.test_asFutureSuccessCancel  s    	Xf- 	$ 	 	 	 	 %X..**T""--a00$777.'.99999r4   c                 D   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   r6  r2  r   rm  rf   )rY   r  
theFailurer,  futures        r2   test_asFutureFailurez/DeferredFutureAdapterTests.test_asFutureFailure  s    
 %JJ.0011
D!!			*+V];;;;;r4   c                 .   t                      }t          |          }t          j        |          }|                     |           |                    d           t          |           |                     |                     |          d           dS )zx
        L{Deferred.fromFuture} returns a L{Deferred} that fires
        when the given L{asyncio.Future} does.
        r1     N)	r   r   r   
fromFuturer  
set_resultr2  rT   r   )rY   r,  r7  r  s       r2   test_fromFuturez*DeferredFutureAdapterTests.test_fromFuture  s    
 %4000((A1--a00!44444r4   c                 J   t                      }t          |          }t          j        |          }|                                 t          |           |                     t          |j                   | 	                    |          
                    t                     dS )z
        L{Deferred.fromFuture} makes a L{Deferred} fire with
        an L{asyncio.CancelledError} when the given
        L{asyncio.Future} is cancelled.
        r1  N)r   r   r   rG  r   r2  rm  r   rf   r   r  rY   r,  	cancelledr  s       r2   test_fromFutureFutureCancelledz9DeferredFutureAdapterTests.test_fromFutureFutureCancelled  s     "(d"3"3"3		**.)*:;;;Q$$^44444r4   c                    t                      }t          |          }t          j        |          }|                                 t          |           |                     |                                d           |                     t          |j
                   |                     |                              t                     dS )z
        L{Deferred.fromFuture} makes a L{Deferred} which, when
        cancelled, cancels the L{asyncio.Future} it was created from.
        r1  TN)r   r   r   rG  r   r2  rT   rL  rm  r   rf   r   r  rK  s       r2    test_fromFutureDeferredCancelledz;DeferredFutureAdapterTests.test_fromFutureDeferredCancelled+  s    
 "(d"3"3"3		**	


,,..555.)*:;;;Q$$^44444r4   rw   )
r<   r=   r>   r8  r=  r@  rD  rI  rM  rO  r?   r4   r2   r4  r4    s        / / / /: : : :*: : : :$< < < <5 5 5 55 5 5 55 5 5 5 5 5r4   r4  c                   v    e Zd ZedZd
dZd
dZed
d            Zed
d            Z	ed
d            Z
d
d	ZdS )CoroutineContextVarsTestsNzcontextvars is not availabler*   c                 R    t                      t          j        d                              d           t	                                          fd           t	                                          fd                               d            t          j        dt          t          t                   t          df         ffd            t          j        dt          t          t                   t          df         f f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.
        testvarrA   c                 .                         d          S r  setr%  vars    r2   r   zDCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.<lambda>K      swwqzz r4   c                 .                         d          S N   rU  rW  s    r2   r   zDCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.<lambda>N      

 r4   c                     ddz  S r#  r?   r$  s    r2   r   zDCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.<lambda>O  
    A r4   r*   Nc               3      K   t                      }                     d| j        d           | V                      d           d S NrA   Tr  r   r  rH   rV  r  r  rX  s    r2   yieldingDeferredzLCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.yieldingDeferredQ  sD      'zzAOOAqz4000GGGGGAJJJJJr4   c               3     K                                                        d           t          j        d          V                                                       d                                dj        d           V                                                       d                                dj        d           	 V  t          d          # t          $ r+                                                      d           Y nw xY w             V                                                       d           t          j        d           d S )Nr   rA   T???? should have failed)rT   r{  r   r  r  rH   r  rN  )r  mutatingDeferredmutatingDeferredThatFailsrY   rX  rd  s   r2   testFunctionzHCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.testFunctionY  sr      SWWYY*** -""""" SWWYY*** OOA/8$???"""" SWWYY*** OOA8A4HHH;////   9:::  / / /  A...../ #"$$$$$SWWYY***d#####s   C" "2DDr   T)r%   contextvars
ContextVarrV  r   r   r   rd  r   r   r  rT   r   )rY   ri  r  r  rg  rh  rX  rd  s   `  @@@@@r2   test_withInlineCallbacksz2CoroutineContextVarsTests.test_withInlineCallbacks?  s   
 +6+A)+L+L


 ,4::$$%9%9%9%9:::3;::!!--.B.B.B.BCCC!--oo>>>			)HSM3,D"E 	 	 	 	 	 	 
		 
	#	$isD(@A #	$ #	$ #	$ #	$ #	$ #	$ #	$ #	$ #	$ #	$ 
	#	$L 	


LNN 	a 	a 	a--a00$77777r4   c                 `    t                      t          j        d          t          j        dt
          t          t                   t          df         f fd            }                    d            |            }	                    d            
                    |           dS )z
        When an inlineCallbacks function resumes, we should be able to reset() a
        contextvar that was set when it was first called.
        rS  r*   Nc               3   \  K                        d          } t                      }                    d|j        d           |V                                                      d                               |                                                                d           d S )Nr  rA   Tr   )rV  r   r  rH   rT   r{  reset)tokenr  r  rY   rX  s     r2   rd  zQCoroutineContextVarsTests.test_resetWithInlineCallbacks.<locals>.yieldingDeferred  s       GGAJJE  (zzAOOAqz4000GGGSWWYY***IIeSWWYY*****r4   r   rA   )r%   rj  rk  r   rd  r   r   r   rV  r  r   )rY   rd  r  r  rX  s   `  @@r2   test_resetWithInlineCallbacksz7CoroutineContextVarsTests.test_resetWithInlineCallbacks  s    
 +6+A)+L+L			+)HSM3,D"E 	+ 	+ 	+ 	+ 	+ 	+ 	+ 
		+ 	


 	aQr4   c                   K   t                      4 d{V  |                     j                                                   }|                    fd           |                     j                   |                     |j                   ddd          d{V  n# 1 d{V swxY w Y   |                     |j                   | d{V  |                     j                   dS )zQ
        L{DeferredLock} can be used as an asynchronous context manager.
        Nc                 ,                                     S r/   rS  )r%  rU  s    r2   r   z>CoroutineContextVarsTests.test_asyncWithLock.<locals>.<lambda>  s    DLLNN r4   )r   rV   rR  rQ  r   r   r  )rY   r  rU  s     @r2   test_asyncWithLockz,CoroutineContextVarsTests.test_asyncWithLock  sq     
 ~~ 	' 	' 	' 	' 	' 	' 	' 	'OODK(((AMM2222333OODK(((QX&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	!!!%%%%%s   A;B''
B14B1c                   K   t          d          4 d{V  |                     j        d           4 d{V  |                     j        d                                           }                                }|                     j        d           |                     |j                   |                     |j                   ddd          d{V  n# 1 d{V swxY w Y   |                     j        d           |                     |j                   |                    fd           |                    fd           | d{V  | d{V  |                     j        d           ddd          d{V  n# 1 d{V swxY w Y   |                     j        d           dS )z^
        L{DeferredSemaphore} can be used as an asynchronous context
        manager.
        r  Nr   rA   r   c                 ,                                     S r/   rt  r%  rm  s    r2   r   zCCoroutineContextVarsTests.test_asyncWithSemaphore.<locals>.<lambda>      S[[]] r4   c                 ,                                     S r/   rt  rx  s    r2   r   zCCoroutineContextVarsTests.test_asyncWithSemaphore.<locals>.<lambda>  ry  r4   )r    rT   tokensrQ  rV   r  r   r   )rY   r   r   rm  s      @r2   test_asyncWithSemaphorez1CoroutineContextVarsTests.test_asyncWithSemaphore  s       "" 	, 	, 	, 	, 	, 	, 	, 	,SZ+++ , , , , , , , ,  Q///[[]][[]]  Q///	***  +++, , , , , , , , , , , , , , , , , , , , , , , , , , , SZ+++OOBI&&&NN2222333NN2222333HHHHHHHHHHHHHHSZ+++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,  	Q'''''s6   $FBC$F$
C.	.F1C.	2BF
F!$F!c                 H  K   t                      }|                     t          d          5  |4 d{V  |                     |j                   t          d          # 1 d{V swxY w Y   	 ddd           n# 1 swxY w Y   |                     |j                   dS )zw
        C{DeferredLock} correctly propagates exceptions when
        used as an asynchronous context manager.
        zsome specific exceptionN)r   assertRaisesRegexpr  rV   rR  r   r  s     r2   test_asyncWithLockExceptionz5CoroutineContextVarsTests.test_asyncWithLockException  s7      ~~$$Y0IJJ 	; 	; ; ; ; ; ; ; ; ;,,, 9:::; ; ; ; ; ; ; ; ; ; ; ; ; ; ;	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	%%%%%s.   	A=*A
A)	)A=,A)	-A==BBc                     t                      t          j        d                              d           t	                                          fd           t	                                          fd                               d            dfddt          f fd	}                    d
           t           |                      }                    d                               d                               d            	                     
                    |          d           dS )z
        When a coroutine is called, the context is taken from when it was first
        called. When it resumes, the same context is applied.
        rS  rA   c                 .                         d          S r  rU  rW  s    r2   r   zJCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.<lambda>  rY  r4   c                 .                         d          S r[  rU  rW  s    r2   r   zJCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.<lambda>  r]  r4   c                     ddz  S r#  r?   r$  s    r2   r   zJCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.<lambda>  r_  r4   r*   Nc                     K   t                      }                     d| j        d           |  d {V                      d           d S ra  rb  rc  s    r2   asyncFuncAwaitingDeferredz[CoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.asyncFuncAwaitingDeferred  sL       (

AOOAqz4000GGGGGGGGGAJJJJJr4   c                    K                                                        d           t          j        d           d {V                                                       d                               dj        d            d {V                                                       d                               dj        d           	  d {V  t          d          # t          $ r+                                                      d           Y nw xY w               d {V                                                       d           dS )Nr   rA   r   Trf  r  )rT   r{  r   r  r  rH   r  )r  r  rg  rh  rY   rX  s   r2   ri  zNCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.testFunction  s      SWWYY*** -""""""""" SWWYY*** OOA/8$???"""""""" SWWYY*** OOA8A4HHH;////////   9:::  / / /  A...../ ,+---------SWWYY***4s   C. .2D#"D#r   Trw   )r%   rj  rk  rV  r   r   r   r$   r  rT   r   )rY   ri  r  r  r  rg  rh  rX  s   `  @@@@@r2   test_contextvarsWithAsyncAwaitz8CoroutineContextVarsTests.test_contextvarsWithAsyncAwait  s   
 +6+A)+L+L


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


<<>>** 	a 	a 	a--a00$77777r4   rw   )r<   r=   r>   rj  skiprl  rq  r8   ru  r|  r  r  r?   r4   r2   rQ  rQ  :  s        -M8 M8 M8 M8^       @ & & & & ( ( ( (2 
& 
& 
& 
&J8 J8 J8 J8 J8 J8r4   rQ  )Tr]   r5   r  r  r*  r  r  asyncior   r   r   r   typingr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   hamcrestr   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(   rj  ImportErrorr8   r  r:   r7   rC   rs   rJ   rK   rN   rQ   r_   SynchronousTestCasery   rr  r  r  r  r<  rF  r  floatr  r  r  r(  r2  r4  rQ  r?   r4   r2   <module>r     s   
     				 				       M M M M M M M M M M M M                                 $ 0 / / / / / / / / / + + + + + + + +                        ( ' ' ' ' '       * * * * * * " " " " " "    Yx(893@PPQQRc8,--.   	 	 	 	 	9 	 	 	f  7    )HSM )d ) ) ) ) gm95554 4 4 4 4 4 4 4,*M *M *M *M *M! *M *M *MZYW YW YW YW YWH02G YW YW YWx.;$ ;$ ;$ ;$ ;$h2 ;$ ;$ ;$|L$ L$ L$ L$ L$5 L$ L$ L$^GI GI GI GI GIX9 GI GI GIT_3 _3 _3 _3 _3x+ _3 _3 _3DE E E E EX9 E E E"x' x' x' x' x'879N x' x' x'vz= z= z= z= z=("3 z= z= z=zV  #    S@ S@ S@ S@ S@h: S@ S@ S@l?% ?% ?% ?% ?%(+ ?% ?% ?%D
 
 
 
 
)+@ 
 
 
,	, 	 	 	 	 	k5 k5 k5 k5 k5!2 k5 k5 k5\u8 u8 u8 u8 u8 1 u8 u8 u8 u8 u8s   B B"!B"