
    Yf+(                   r   d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z
ddlZddlZddlmZmZmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z# ddl$m%Z%m&Z&m'Z' ddl(m)Z) ddl*m+Z+ dd	l,m-Z- dd
l.m/Z/m0Z0 ddl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZC ddlDmEZE ddlFm	Z	 dOdZG G d deH          ZIdPdZJ e!deH          ZK G d d          ZL G d  d!e	jM                  ZN G d" d#e	jO        eL          ZP G d$ d%          ZQdQd)ZR G d* d+e	jO                  ZS G d, d-e	jO                  ZT G d. d/e	jO                  ZU G d0 d1e	jO                  ZV G d2 d3e	jO                  ZW G d4 d5e	jO                  ZX G d6 d7e	jO        eL          ZY G d8 d9e	jM                  ZZdRd?Z[ G d@ dAe	jO                  Z\ G dB dCe	jM                  Z] G dD dEe	jM        eL          Z^dSdHZ_ G dI dJe	jM                  Z` G dK dLe	jM                  Za G dM dNe	jO                  ZbdS )Tz+
Test cases for L{twisted.internet.defer}.
    )annotationsN)AbstractEventLoopCancelledErrorFuturenew_event_loop)AnyCallable	CoroutineDict	GeneratorListLiteralMappingNoReturnOptionalSetTupleTypeTypeVarUnioncast)assert_thatemptyequal_to)given)integers)assert_type)deferreactor)_TDeferredDeferredFilesystemLockDeferredListDeferredLockDeferredQueueDeferredSemaphoreFailureGroup_DeferredListResultListT_DeferredListSingleResultTensureDeferredrace)Clock)log)_PYPYFailure)unittestf0Callable[..., Coroutine[Deferred[Any], Any, _T]]returnCallable[..., Deferred[_T]]c                H     t          j                   d fd            }|S )Nargsobjectkwargsr4   Deferred[_T]c                 <     | i |}t          j        |          S N)r!   fromCoroutine)r7   r9   coror2   s      I/var/www/html/env/lib/python3.11/site-packages/twisted/test/test_defer.pywrapperz!ensuringDeferred.<locals>.wrapperJ   s(    q$!&!!%d+++    )r7   r8   r9   r8   r4   r:   )	functoolswraps)r2   r@   s   ` r?   ensuringDeferredrD   G   s?     _Q, , , , , , NrA   c                      e Zd ZdS )GenericErrorN__name__
__module____qualname__ rA   r?   rF   rF   R   s        DrA   rF   deferredDeferred[str]Nonec                0    |                      d           dS )z
    A fake L{Deferred} canceller which callbacks the L{Deferred}
    with C{str} "Callback Result" when cancelling it.

    @param deferred: The cancelled L{Deferred}.
    Callback ResultNcallbackrL   s    r?   fakeCallbackCancellerrT   V   s     '(((((rA   _ExceptionT)boundc                      e Zd ZdZd
dZd	S )ImmediateFailureMixinz+
    Add additional assertion methods.
    rL   Deferred[Any]	exceptionType[_ExceptionT]r4   rU   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.
           r   )r   r1   TestCase
addErrbackappendassertEquallen
assertTruecheckrU   value)selfrL   rZ   testCasefailuress        r?   assertImmediateFailurez,ImmediateFailureMixin.assertImmediateFailureh   s     )400"$HO,,,S]]A...HQK--i88999K!!2333rA   N)rL   rY   rZ   r[   r4   rU   )rH   rI   rJ   __doc__ri   rK   rA   r?   rX   rX   c   s2         4 4 4 4 4 4rA   rX   c                  *    e Zd ZdZddZddZddZdS )		UtilTestsz&
    Tests for utility functions.
    r4   rN   c                    t          t                                }t          j        |          }|                     t                     |                     ||           dS )z<
        L{defer.logError} returns the given error.
        N)r0   RuntimeErrorr   logErrorflushLoggedErrorsassertIs)rf   errorresults      r?   test_logErrorReturnsErrorz#UtilTests.test_logErrorReturnsError~   sR     ''&&|,,,eV$$$$$rA   c                    t          t                                }t          j        |           |                     t                    }|                     ||g           dS )z9
        L{defer.logError} logs the given error.
        N)r0   rn   r   ro   rp   ra   )rf   rr   errorss      r?   test_logErrorLogsErrorz UtilTests.test_logErrorLogsError   sV     ''u''55%)))))rA   c                .   g d	fd}t          j        |           t          t                                }t	          j        |           |                     t                     |                     d                             d                     dS )
zR
        The text logged by L{defer.logError} has no repr of the failure.
        	eventDictDict[str, Any]r4   rN   c                b    t          j        |           }|J                     |           d S r<   )r-   textFromEventDictr`   )ry   textoutputs     r?   emitz4UtilTests.test_logErrorLogsErrorNoRepr.<locals>.emit   s7    (33D###MM$rA   r   Unhandled Error
Traceback N)ry   rz   r4   rN   )	r-   addObserverr0   rn   r   ro   rp   rc   
startswith)rf   r   rr   r~   s      @r?   test_logErrorLogsErrorNoReprz&UtilTests.test_logErrorLogsErrorNoRepr   s     	  	  	  	  	  	 
 	''u|,,,q	,,-JKKLLLLLrA   Nr4   rN   )rH   rI   rJ   rj   rt   rw   r   rK   rA   r?   rl   rl   y   s`         % % % %* * * *M M M M M MrA   rl   c                  8   e Zd Zd^dZd_dZd`d	ZdadZd^dZd^dZd^dZ	d^dZ
d^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^dZd^d Zd^d!Zd^d"Zd^d#Zd^d$Zd^d%Z d^d&Z!d^d'Z"d^d(Z#d^d)Z$d^d*Z%d^d+Z&d^d,Z'd^d-Z(d^d.Z)d^d/Z*d^d0Z+d^d1Z,d^d2Z-d^d3Z.d^d4Z/d^d5Z0d^d6Z1d^d7Z2d^d8Z3d^d9Z4d^d:Z5d^d;Z6d^d<Z7d^d=Z8d^d>Z9d^d?Z:d^d@Z;d^dAZ<d^dBZ=d^dCZ>d^dDZ?d^dEZ@d^dFZAd^dGZBd^dHZCd^dIZDd^dJZEd^dKZFd^dLZGd^dMZHd^dNZId^dOZJd^dPZKd^dQZLd^dRZMd^dSZNd^dTZOd^dUZPd^dVZQd^dWZR eSjT        eUdX          d^dY            ZV eSjT        eUdX          d^dZ            ZW eSjT        eUdX          d^d[            ZXd^d\ZYd]S )bDeferredTestsr4   rN   c                    d | _         d | _        d | _        |                     t          j        t	          j                               d S r<   )callbackResultscallback2ResultserrbackResults
addCleanupr   setDebugginggetDebuggingrf   s    r?   setUpzDeferredTests.setUp   sQ      	
  	
  	
 	*E,>,@,@AAAAArA   r7   r8   r9   r   c                $    ||f| _         |d         S Nr   r   rf   r7   r9   s      r?   	_callbackzDeferredTests._callback   s    #V|AwrA   c                    ||f| _         d S r<   r   r   s      r?   
_callback2zDeferredTests._callback2   s     $frA   r0   c                    ||f| _         d S r<   r   r   s      r?   _errbackzDeferredTests._errback   s    "FlrA   c                    t                      }|                    | j                   |                    d           |                     | j                   |                     | j        di f           d S Nhellor   r!   addCallbackr   rR   assertIsNoner   ra   r   rf   rL   s     r?   testCallbackWithoutArgsz%DeferredTests.testCallbackWithoutArgs   sp    "***T^,,,'"""$-...-
B/?@@@@@rA   c                    t                      }|                    | j        d           |                    d           |                     | j                   |                     | j        di f           d S )Nworldr   )r   r   r   r   s     r?   testCallbackWithArgsz"DeferredTests.testCallbackWithArgs   ss    "***T^W555'"""$-...-0BB/GHHHHHrA   c                    t                      }|                    | j        d           |                    d           |                     | j                   |                     | j        dddif           d S )Nr   )r   r   r   r   r   s     r?   testCallbackWithKwArgsz$DeferredTests.testCallbackWithKwArgs   sz    "***T^7;;;'"""$-...-
Wg<N/OPPPPPrA   c                \   t                      }|                    | j                   |                    | j                   |                    d           |                     | j                   |                     | j        di f           |                     | j	        di f           d S r   )
r!   r   r   r   rR   r   r   ra   r   r   r   s     r?   testTwoCallbackszDeferredTests.testTwoCallbacks   s    "***T^,,,T_---'"""$-...-
B/?@@@.R0@AAAAArA   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   r]   )rF   r!   addCallbacksr   r   r	   r8   errbackr_   r   r   r   r   ra   rb   re   rf   rr   rL   s      r?   test_addCallbacksNoneErrbackz*DeferredTests.test_addCallbacksNoneErrback   s    X&&#+::dnd8CK3H$.O.OPPPDM***$.///"...T0344a888,Q/28%@@@,Q/44444rA   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.
        NrK   r   r   )r!   r   r   r   r   r   r8   r   strrR   r   r   ra   r   r   s     r?   !test_addCallbacksNoneCallbackArgsz/DeferredTests.test_addCallbacksNoneCallbackArgs   s     #+**NMv%%f%t,,	
 	
 	
 	'"""$-...-
B/?@@@@@rA   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   rK   Nr   r]   )rF   r!   r   r   r   r   r   r8   r   r   r   r_   r   r   r   ra   rb   re   r   s      r?    test_addCallbacksNoneErrbackArgsz.DeferredTests.test_addCallbacksNoneErrbackArgs   s+    X&&#+::NMv%%f%t,,	
 	
 	
 	DM***$.///"...T0344a888,Q/28%@@@,Q/44444rA   c                   t           t          t          t          t          t
          f         f                  }t                      }t                      }t                      }t          |||g          }g }|fdd}dd	}|                    ||           |	                    d
           |
                    |           |                    t          d                     |	                    d           |                     |d         |d         d         t	          t          t
          |d         d                   j                  f|d         gt           j        d
ft           j        dft           j        dfg           d S )N
resultList
ResultListrs   r4   rN   c                0    |                     |            d S r<   )extendr   rs   s     r?   cbz*DeferredTests.testDeferredList.<locals>.cb  s    MM*%%%%%rA   errr0   c                    d S r<   rK   r   s    r?   catchz-DeferredTests.testDeferredList.<locals>.catch      4rA   123r   r]      )r   r   rs   r   r4   rN   r   r0   r4   rN   )r   r   boolr   r   r0   r!   r#   r   rR   r_   r   rF   ra   r   re   r   SUCCESSFAILURE)	rf   r   defr1defr2defr3dlrs   r   r   s	            r?   testDeferredListzDeferredTests.testDeferredList  sp   %eCL&9 9:;
'zz'zz'zz5%/00<B 	& 	& 	& 	& 	&	 	 	 	 	Bs 	l3''(((sq	1s41#>#>#DEEFq		 mS!EM3#7%-9MN	
 	
 	
 	
 	
rA   c                   g }|fd
d}t          g           }|                    |           |                     |g g           g |d d <   t          g d	          }|                    |           |                     |g            d S )Nr   _DeferredListResultListT[None]rs   $List[_DeferredListResultListT[None]]r4   rN   c                0    |                     |            d S r<   r`   r   s     r?   r   z/DeferredTests.testEmptyDeferredList.<locals>.cb:  s     MM*%%%%%rA   TfireOnOneCallback)r   r   rs   r   r4   rN   )r#   r   ra   )rf   rs   r   dl1dl2s        r?   testEmptyDeferredListz#DeferredTests.testEmptyDeferredList7  s    79 <B	& 	& 	& 	& 	& 9ER8H8H"&&&qqq	:F$;
 ;
 ;
 	$$$$$rA   c                *   t                      }t                      }t                      }t          |||gd          }g }|                    |j                   dd}|                    |           |                    d           |                     |g            |                    t          d	                     |                     t          |          d
           |d         }| 	                    t          |j        t          j                  d|j                   |j        }|                     |j        j        t                     |                     |j        j        j        d           |                     |j        d
           d S )NTfireOnOneErrbackr   r0   r4   rN   c                    d S r<   rK   r   s    r?   r   z;DeferredTests.testDeferredListFireOnOneError.<locals>.catchU  r   rA   r   	from def2r]   r   zFissubclass(aFailure.type, defer.FirstError) failed: failure's type is )r   r   )r!   r#   r_   r`   rR   ra   r   rF   rb   rc   
issubclasstyper   
FirstErrorre   
subFailurer7   index)	rf   r   r   r   r   rs   r   aFailure
firstErrors	            r?   testDeferredListFireOnOneErrorz,DeferredTests.testDeferredListFireOnOneErrorK  s   'zz'zz'zz5%/$GGG "
fm$$$	 	 	 	 	 	s$$$ 	l;//000Va((( !9 	x}e&6777&.mm6	
 	
 	
 ^
 	.3\BBB.49>JJJ)1-----rA   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   r]   )r!   r#   r_   r`   r   r   rF   ra   re   r7   rb   rf   d1r   	errorTrapresultListsfirstResults         r?   !testDeferredListDontConsumeErrorsz/DeferredTests.testDeferredListDontConsumeErrorst  s    %ZZ2$#%	
i&'''<>
{)***


<''(((1!3!8!;<<<C,,---!!nQ'&&&Q!5!:1!=>>>>>rA   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   r]   r   )r!   r#   r_   r`   r   r   rF   ra   rb   re   r7   r   s         r?   testDeferredListConsumeErrorsz+DeferredTests.testDeferredListConsumeErrors  s    %ZZ2$d333#%	
i&'''<>
{)***


<''(((Y'''C,,---!!nQ'&&&Q!5!:1!=>>>>>rA   c                R   t                      }t                      }|                    t          d                     t          ||gd          }g }|                    |j                   |                     dt          |                     |                    d            d S )Nr   Tr   r]   c                    d S r<   rK   es    r?   <lambda>zWDeferredTests.testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds.<locals>.<lambda>       rA   )r!   r   rF   r#   r_   r`   ra   rb   rf   r   d2r   rs   s        r?   7testDeferredListFireOnOneErrorWithAlreadyFiredDeferredszEDeferredTests.testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds  s    %ZZ%ZZ


<''((( 2r(T::: "
fm$$$CKK(((
nn%%%%%rA   c                x   t                      }t                      }|                    t          d                     |                    d           t	          ||g          }g }|                    |j                   |                     dt          |                     |	                    d            d S )Nr   r   r]   c                    d S r<   rK   r   s    r?   r   zIDeferredTests.testDeferredListWithAlreadyFiredDeferreds.<locals>.<lambda>  r   rA   )
r!   r   rF   rR   r#   r   r`   ra   rb   r_   r   s        r?   )testDeferredListWithAlreadyFiredDeferredsz7DeferredTests.testDeferredListWithAlreadyFiredDeferreds  s    $JJ$JJ


<''(((
A 2r(##/1
v}%%%CKK(((
nn%%%%%rA   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   rf   deferredOnedeferredTwodeferredLists       r?   test_cancelDeferredListz%DeferredTests.test_cancelDeferredList  sr    
 '/jj&.jj#[+$>??[%*>???[%*>?????rA   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   r]   rP   N)r!   rT   r#   r   r  r   r   successResultOfrc   ra   assertFalserd   rf   r  r  r  rs   s        r?   test_cancelDeferredListCallbackz-DeferredTests.test_cancelDeferredListCallback  s     &..C%D%D%-ZZ#[+$>??[%*>???%%l33q	!%%%1'89991&&&q	!**5+?@@AAAAArA   c                   t                      }t                      }t          ||gd          }|                                 |                     |t          j                   |                     |t          j                   dS )z
        When cancelling an unfired L{DeferredList} with the flag
        C{fireOnOneCallback} set, cancel every L{Deferred} in the list.
        Tr   Nr   r  s       r?   ,test_cancelDeferredListWithFireOnOneCallbackz:DeferredTests.test_cancelDeferredListWithFireOnOneCallback  sx    
 '/jj&.jj#[+$>RVWWW[%*>???[%*>?????rA   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   )rP   r   N)	r!   rT   r#   r   r  r   r   r  ra   r
  s        r?   ?test_cancelDeferredListWithFireOnOneCallbackAndDeferredCallbackzMDeferredTests.test_cancelDeferredListWithFireOnOneCallbackAndDeferredCallback  s     &..C%D%D%-ZZ#[+$>RVWWW[%*>???%%l33!788888rA   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   re   rc   r   rd   )rf   r  r  r  deferredListFailurer   s         r?   +test_cancelDeferredListWithFireOnOneErrbackz9DeferredTests.test_cancelDeferredListWithFireOnOneErrback  s    
 '/jj&.jj#[+$>QUVVV[%*>???[%*>???"22<AQRR(.

-33E4HIIJJJJJrA   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   r]   rP   N)r!   rT   r#   r   r  rc   ra   r
  s        r?   ?test_cancelDeferredListWithFireOnOneErrbackAllDeferredsCallbackzMDeferredTests.test_cancelDeferredListWithFireOnOneErrbackAllDeferredsCallback  s     &..C%D%D%-.C%D%D#[+$>QUVVV%%l33q	!%%%1'8999q	!%%%1'899999rA   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#   r`   r   r  r   r   assertNoResult)rf   r  r  argumentListr  deferredThrees         r?   ,test_cancelDeferredListWithOriginalDeferredsz:DeferredTests.test_cancelDeferredListWithOriginalDeferreds  s    
 '/jj&.jj#[1#L11(0

M***[%*>???[%*>???M*****rA   c                L   dd}t          |          }t                      }t          ||g          }|                                 |                     |t          j                   |                     t                    }|                     t          |          d           dS )	z
        Cancelling a L{DeferredList} will cancel every L{Deferred}
        in the list even exceptions raised from the C{cancel} method of the
        L{Deferred}s.
        rL   Deferred[object]r4   rN   c                     t          d          )z
            A L{Deferred} canceller that raises an exception.

            @param deferred: The cancelled L{Deferred}.
            test)rn   rS   s    r?   cancellerRaisesExceptionzTDeferredTests.test_cancelDeferredListWithException.<locals>.cancellerRaisesException%  s     v&&&rA   r]   N)rL   r  r4   rN   )
r!   r#   r   r  r   r   rp   rn   ra   rb   )rf   r  r  r  r  rv   s         r?   $test_cancelDeferredListWithExceptionz2DeferredTests.test_cancelDeferredListWithException  s    	' 	' 	' 	' '//G&H&H&.jj#[+$>??[%*>???''55Va(((((rA   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#   rR   r   r  r  s       r?   )test_cancelFiredOnOneCallbackDeferredListz7DeferredTests.test_cancelFiredOnOneCallbackDeferredList5  sn     '/jj&.jj#[+$>RVWWWT"""K(((((rA   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   rF   r   r  r  r   r   r  s       r?   (test_cancelFiredOnOneErrbackDeferredListz6DeferredTests.test_cancelFiredOnOneErrbackDeferredListB  s     '/jj&.jj#[+$>QUVVVL00111K((([,777\5+;<<<<<rA   c                    g }t          j        d          }|                    |j                   |                     |dg           d S )Nsuccess)r   succeedr   r`   ra   rf   lds      r?   testImmediateSuccessz"DeferredTests.testImmediateSuccessQ  sH     =33	ahYK(((((rA   c                    g }t          j        t          d                    }|                    |j                   |                     t          |d         j                  d           d S Nfailr   )r   r-  rF   r_   r`   ra   r   re   r'  s      r?   testImmediateFailurez"DeferredTests.testImmediateFailureW  sZ    !J|F';';<<	QXQqTZ&11111rA   c                X   g }t          j        t          d                    }|                                 |                    |j                   |                     |g            |                                 |                     t          |d         j	                  d           d S r,  )
r   r-  rF   pauser_   r`   ra   unpauser   re   r'  s      r?   testPausedFailurezDeferredTests.testPausedFailure]  s    J|F++,,					QXB			QqTZ&11111rA   c                    g }t                      }|                    d                               |j                   |                    d           |                     |d         j        t                     g }t          t                               }|                    d                               |j                   |                    d           |                     |d         j        t                     d S )Nc                    ddz  S Nr]   r   rK   _s    r?   r   z2DeferredTests.testCallbackErrors.<locals>.<lambda>i  s
    Q rA   r]   r   c                8    t          t                                S r<   )r0   ZeroDivisionErrorr6  s    r?   r   z2DeferredTests.testCallbackErrors.<locals>.<lambda>n  s    ):)<)<!=!= rA   )	r!   r   r_   r`   rR   assertIsInstancere   r9  int)rf   r(  r)  r   s       r?   testCallbackErrorsz DeferredTests.testCallbackErrorsf  s    #::	&&''2218<<<	

1adj*;<<<c]__
==>>II!(SSS
Aadj*;<<<<<rA   c                    t                      }|                                 |                    | j                   |                                 d S r<   )r!   r0  r   r   r1  rf   r)  s     r?   testUnpauseBeforeCallbackz'DeferredTests.testUnpauseBeforeCallbackr  s=    $JJ					dn%%%			rA   c                   t                      }t                                                       |                    fd           |                    | j                   |                    d           |                     | j        d                               d           |                     | j        d                                            |                     | j        d           | j        
J d            | 	                    | j        d	         d	         dd

                    | j                             dS )z
        Callbacks passed to L{Deferred.addCallback} can return Deferreds and
        the next callback will not be run until that Deferred fires.
        c                    S r<   rK   )rr   s    r?   r   z<DeferredTests.test_callbackReturnsDeferred.<locals>.<lambda>  s     rA   r]   z Should not have been called yet.r   z&Still should not have been called yet.zShould have been called nowNz%make that legible to the type checkerr   z/Result should have been from second deferred:{})r!   r0  r   r   rR   r   r   r1  assertIsNotNoneassertEqualsformatrf   r   r   s     @r?   test_callbackReturnsDeferredz*DeferredTests.test_callbackReturnsDeferredx  s8   
 %JJ$JJ



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

T13PQQQ#//1X/// #A&=DD$ 	
 	
 	
 	
 	
rA   c                
   t                      }dt                      dfd}|                    d           |                     |                     |                    |                    d           t          t                               }d	|                    d           |                     |                     |                    |                              |          t                    j	                   d
S )zF
        Callbacks passed to addCallback may return Failures.
        Frs   r;  r4   Union[int, Failure]c                0    rt                    S | dz   S Nr]   r/   )rs   rte
shouldFails    r?   	maybeFailzADeferredTests.test_callbackMaybeReturnsFailure.<locals>.maybeFail  s!     "s||#z!rA         TN)rs   r;  r4   rI  )
r!   rn   rR   ra   r  r   r;  rq   r  re   )rf   r)  rN  rL  rM  s      @@r?    test_callbackMaybeReturnsFailurez.DeferredTests.test_callbackMaybeReturnsFailure  s     $::
nn	" 	" 	" 	" 	" 	" 	" 	


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

1  i((44Y??  	
 	
 	
 	
 	
rA   c                   t                      }t                      g d
fd}dfd}|                    |                              |           |                    t	                                 |                     |           |                     g                                d           |                     dg           d	S )zo
        Errbacks passed to L{Deferred.addErrback} can return Deferreds just as
        callbacks can.
        rs   r0   r4   rM   c                    S r<   rK   )rs   r   s    r?   asyncErrbackz?DeferredTests.test_errbackReturnsDeferred.<locals>.asyncErrback  s    IrA   Union[str, int]rN   c                2                         |            d S r<   r   )rs   resultValuess    r?   syncCallbackz?DeferredTests.test_errbackReturnsDeferred.<locals>.syncCallback  s    '''''rA   N)rs   r0   r4   rM   )rs   rU  r4   rN   )r!   r_   r   r   
ValueErrorr  ra   rR   )rf   r)  rT  rX  r   rW  s       @@r?   test_errbackReturnsDeferredz)DeferredTests.test_errbackReturnsDeferred  s    
 $::$JJ	 	 	 	 	 		( 	( 	( 	( 	( 	( 	
\""..|<<<			*,,Ar***
Hz22222rA   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<   rK   )ignoredpauseds    r?   r   zDDeferredTests.test_chainedPausedDeferredWithResult.<locals>.<lambda>  s    F rA   N)r8   r!   rR   r0  r   r`   ra   r1  )rf   expectedchainedrs   r^  s       @r?   $test_chainedPausedDeferredWithResultz2DeferredTests.test_chainedPausedDeferredWithResult  s     88#+::!!!"***2222333!FM***$$$(,,,,,rA   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<   rK   r]  seconds    r?   r   z:DeferredTests.test_pausedDeferredChained.<locals>.<lambda>      & rA   N)r!   r   rR   r0  r`   ra   )rf   firstrs   re  s      @r?   test_pausedDeferredChainedz(DeferredTests.test_pausedDeferredChained  s     !)

!)0000111t6=)))$(((((rA   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 )Nr]   r   r   c                    dS rK  rK   r   s    r?   r   z2DeferredTests.test_gatherResults.<locals>.<lambda>  s    1 rA   )r   gatherResultsr&  r   r`   ra   r-  rY  r_   rb   r:  r0   )rf   resultsrv   r   s       r?   test_gatherResultsz DeferredTests.test_gatherResults  s   #%U]1--u}Q/?/?@AAMMN	
 	
 	
 	Aq6(+++ "mA
:<< 8 89B**6=999Va(((fQi111
1%%%%%rA   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}.
        r]   zoh noesTr   r   r]   r   N)r   r&  r-  rn   rk  r_   r`   ra   rb   r:  re   r   r   )rf   dgooddbadr)  unconsumedErrorsgatheredErrorsr   s          r?   #test_gatherResultsWithConsumeErrorsz1DeferredTests.test_gatherResultsWithConsumeErrors  s     a  z,y1122TBBB*,(/000(*	^*+++#.//^1D1DEvNNNnQ/5u7GHHH#A&,7
j.=====rA   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   rk  r   r  r   r   re   rc   r   rd   )rf   r  r  rs   gatherResultsFailurer   s         r?   test_cancelGatherResultsz&DeferredTests.test_cancelGatherResults  s    
 '/jj&.jj$k;%?@@[%*>???[%*>???#33FE<LMM)/

-33E4HIIJJJJJrA   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   rP   r]   N)r!   rT   r   rk  r   r  ra   )rf   r  r  rs   callbackResults        r?   0test_cancelGatherResultsWithAllDeferredsCallbackz>DeferredTests.test_cancelGatherResultsWithAllDeferredsCallback  s     &..C%D%D%-.C%D%D$k;%?@@--f55*,=>>>*,=>>>>>rA   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<   rK   rs   s   r?   r   z6DeferredTests.test_maybeDeferredSync.<locals>.<lambda>5  s     rA   r]   r   N)r8   r   maybeDeferredr   r`   ra   rb   assertIdentical)rf   rl  rv   r)  rs   s       @r?   test_maybeDeferredSyncz$DeferredTests.test_maybeDeferredSync-  s    
  " "//	w~v}555$$$Wq)))WQZ00000rA   c                   dd}g }g }t          j        |d          }t          |t          t                              |                    |j        |j                   |                     |g            |                     |dg           dS )	zV
        L{defer.maybeDeferred} should pass arguments to the called function.
        xr;  r4   c                    | dz   S )N   rK   r  s    r?   plusFivez>DeferredTests.test_maybeDeferredSyncWithArgs.<locals>.plusFive@  s    q5LrA   
      N)r  r;  r4   r;  )r   r~  r   r!   r;  r   r`   ra   )rf   r  rl  rv   r)  s        r?   test_maybeDeferredSyncWithArgsz,DeferredTests.test_maybeDeferredSyncWithArgs;  s    
	 	 	 	   ""--Ax}%%%	w~v}555$$$2$'''''rA   c                   t          d          dfd}g }g }t          j        |                              |j        |j                   |                     |g            |                     t          |          d           |                     t          |d         j                  t                               dS )	z
        L{defer.maybeDeferred} should catch an exception raised by a synchronous
        function and errback its resulting L{Deferred} with it.
        zthat value is unacceptabler4   r   c                      r<   rK   r_  s   r?   raisesExceptionzFDeferredTests.test_maybeDeferredSyncException.<locals>.raisesExceptionR  s    NrA   r]   r   N)r4   r   )	rY  r   r~  r   r`   ra   rb   r   re   )rf   r  rl  rv   r_  s       @r?   test_maybeDeferredSyncExceptionz-DeferredTests.test_maybeDeferredSyncExceptionK  s    
 :;;	 	 	 	 	 	   "O,,99'.&-XXX"%%%Va(((VAY_--s8}}=====rA   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<   rK   r  s   r?   r   z=DeferredTests.test_maybeDeferredSyncFailure.<locals>.<lambda>h  s     rA   r]   r   N)		TypeErrorr0   r   r~  r   r`   ra   rb   r  )rf   rl  rv   r)  r_  s       @r?   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<   rK   r   s   r?   r   z7DeferredTests.test_maybeDeferredAsync.<locals>.<lambda>t  s     rA   SuccessN)r!   r   r~  rR   r   r`   ra   )rf   r   rs   r   s      @r?   test_maybeDeferredAsyncz%DeferredTests.test_maybeDeferredAsyncn  sm    
 %JJ ,,
I
v}%%%)-----rA   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<   rK   r  s   r?   r   z<DeferredTests.test_maybeDeferredAsyncError.<locals>.<lambda>  s     rA   N)r!   r   r~  r   r0   rn   ri   )rf   r   r   s     @r?   test_maybeDeferredAsyncErrorz*DeferredTests.test_maybeDeferredAsyncErrorz  s^     &ZZ"0<<


7<>>**+++##B55555rA   c                    dd}t          j        |          }t          |t          t                              |                     |                     |          d           dS )
        When called with a coroutine function L{defer.maybeDeferred} returns a
        L{defer.Deferred} which has the same result as the coroutine returned
        by the function.
        r4   r;  c                 
   K   dS NrP  rK   rK   rA   r?   r2   z;DeferredTests.test_maybeDeferredCoroutineSuccess.<locals>.f        1rA   rP  Nr4   r;  )r   r~  r   r!   r;  ra   r  )rf   r2   coroutineDeferreds      r?   "test_maybeDeferredCoroutineSuccessz0DeferredTests.test_maybeDeferredCoroutineSuccess  si    	 	 	 	 "/22%x}555--.?@@!DDDDDrA   c                   d
ddfd}dd}t          j        |          }t          |t          t                              |                    |          }|                     |                     |          d           d	S )r  r4   r;  c                 
   K   dS r  rK   rK   rA   r?   r2   zCDeferredTests.test_maybeDeferredCoroutineSuccessIndirect.<locals>.f  r  rA   "Coroutine[Deferred[int], Any, Any]c                                   S r<   rK   r2   s   r?   gzCDeferredTests.test_maybeDeferredCoroutineSuccessIndirect.<locals>.g      133JrA   rs   c                    | dz   S rK  rK   r}  s    r?   typedCallbackzODeferredTests.test_maybeDeferredCoroutineSuccessIndirect.<locals>.typedCallback  s    A:rA      Nr  )r4   r  )rs   r;  r4   r;  )r   r~  r   r!   r   r   ra   r  )rf   r  r  r  modifiedDeferredr2   s        @r?   *test_maybeDeferredCoroutineSuccessIndirectz8DeferredTests.test_maybeDeferredCoroutineSuccessIndirect  s    	 	 	 	
	 	 	 	 	 	
	 	 	 	 "/22%x}555,88GG--.>??CCCCCrA   c                     G d dt                     d	fdd
fd}t          |                     t          j        |                    j        t                               dS )z
        When called with a coroutine function L{defer.maybeDeferred} returns a
        L{defer.Deferred} which has a L{Failure} result wrapping the exception
        raised by the coroutine function.
        c                      e Zd ZdS )GDeferredTests.test_maybeDeferredCoroutineFailure.<locals>.SomeExceptionNrG   rK   rA   r?   SomeExceptionr            DrA   r  r4   rN   c                    K                 r<   rK   )r  s   r?   r2   z;DeferredTests.test_maybeDeferredCoroutineFailure.<locals>.f  s      -//!rA   Coroutine[None, None, None]c                                   S r<   rK   r  s   r?   r  z;DeferredTests.test_maybeDeferredCoroutineFailure.<locals>.g  r  rA   Nr   )r4   r  )	Exceptionr   r  r   r~  r   r   )rf   r  r  r2   s     @@r?   "test_maybeDeferredCoroutineFailurez0DeferredTests.test_maybeDeferredCoroutineFailure  s    	 	 	 	 	I 	 	 		" 	" 	" 	" 	" 	"
	 	 	 	 	 	 	  !4Q!7!788=]##	
 	
 	
 	
 	
rA   c                   g g t                      d
fd}t          j        d          }|                    |                               d           |                    j                                       j                   |                    j                   |                     g            |                     g d           d	S )a  
        When a L{Deferred} encounters a result which is another L{Deferred}
        which is waiting on a third L{Deferred}, the middle L{Deferred}'s
        callbacks are executed after the third L{Deferred} fires and before the
        first receives a result.
        rs   r   r4   rM   c                                         d| f           t          j        d          }d	fd}d
fd}|                    |                              |           |                    j                    |S )Nstart-of-cbinnerrs   r   r4   rM   c                2                         d           S NfirstCallbackr  r   rs   r  rl  s    r?   r  zMDeferredTests.test_innerCallbacksPreserved.<locals>.cb.<locals>.firstCallback  s    9:::rA   c                <                         d| f           | dz  S NsecondCallbackr   r   rs   rl  s    r?   r  zNDeferredTests.test_innerCallbacksPreserved.<locals>.cb.<locals>.secondCallback  %     0&9:::z!rA   rs   r   r4   rM   rs   r   r4   r   r`   r   r&  r   r_   rs   r)  r  r  rh   r  rl  s       r?   r   z6DeferredTests.test_innerCallbacksPreserved.<locals>.cb  s    NNM62333g&&A      " " " " " " MM-((44^DDDLL)))HrA   outeroranger  r  r  )r  r  orangeorangeNr  )r!   r   r&  r   rR   r`   r_   ra   rf   r   r  rh   r  rl  s      @@@r?   test_innerCallbacksPreservedz*DeferredTests.test_innerCallbacksPreserved  s    68"$'zz	 	 	 	 	 	 	 	  g&&"x   '.)))))))))X&&&  	
 	
 	
 	
 	
rA   c                |   g g t                      dfd}t          j        d                              |                              j                   |                     ddg                               d	           |                     g            |                     ddd
d	gfg           dS )a   
        The continue callback of a L{Deferred} waiting for another L{Deferred}
        is not necessarily the first one. This is somewhat a whitebox test
        checking that we search for that callback among the whole list of
        callbacks.
        rs   r   r4   Deferred[None]c                                        d| f           t                      }d	fd}d
fd}|                    |                              |                              j                   }|                    d            |S )Nr   rs   rN   r4   Deferred[List[str]]c                \                         d| f           t          j        g          S Nr  r`   r   rk  rs   arl  s    r?   r  zNDeferredTests.test_continueCallbackNotFirst.<locals>.cb.<locals>.firstCallback  -    8999*A3///rA   	List[str]c                6                         d| f           d S Nr  r   r  s    r?   r  zODeferredTests.test_continueCallbackNotFirst.<locals>.cb.<locals>.secondCallback  !     0&9:::::rA   rs   rN   r4   r  rs   r  r4   rN   r`   r!   r   r_   rR   rs   r)  r  r  returnerr  rh   rl  s        r?   r   z7DeferredTests.test_continueCallbackNotFirst.<locals>.cb      NND&>*** (

A0 0 0 0 0 0 0; ; ; ; ; ; m,,^,,HO,, 
 JJtOrA   r  r   r  r  Nwithersr  Nrs   r   r4   r  )r!   r   r&  r   r_   r`   ra   rR   )rf   r   r  rh   rl  s     @@@r?   test_continueCallbackNotFirstz+DeferredTests.test_continueCallbackNotFirst  s     BD"$#::	 	 	 	 	 	 	 	& 	g**2..99(/JJJ/+BCWMMM	

9X&&&58H9+7VW	
 	
 	
 	
 	
rA   c                   g g t                      dfd}t                      }|                    |           |                    fd           |                    j                   |                    d           |                     dd	g                               d
           |                     g            |                     dd	dd
gfdg           dS )z
        A callback added to a L{Deferred} after a previous callback attached
        another L{Deferred} as a result is run after the callbacks of the other
        L{Deferred} are run.
        rs   r   r4   r  c                                        d| f           t                      }d	fd}d
fd}|                    |                              |                              j                   }|                    d            |S )Nr   rs   rN   r4   r  c                \                         d| f           t          j        g          S r  r  r  s    r?   r  zLDeferredTests.test_callbackOrderPreserved.<locals>.cb.<locals>.firstCallback$  r  rA   r  c                6                         d| f           d S r  r   r  s    r?   r  zMDeferredTests.test_callbackOrderPreserved.<locals>.cb.<locals>.secondCallback(  r  rA   r  r  r  r  s        r?   r   z5DeferredTests.test_callbackOrderPreserved.<locals>.cb   r  rA   c                .                         d          S )NfinalNr   )r  rl  s    r?   r   z;DeferredTests.test_callbackOrderPreserved.<locals>.<lambda>5  s    GNN?$C$C rA   r  r  r  r  r  r  Nr  )r!   r   r_   r`   rR   ra   )rf   r   r  r  rh   rl  s      @@@r?   test_callbackOrderPreservedz)DeferredTests.test_callbackOrderPreserved  s*    BD"$#::	 	 	 	 	 	 	 	&  (zz"CCCCDDD)))w/+BCWMMM	

9X&&&'!I;/		
 	
 	
 	
 	
rA   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.
        rs   rN   r4   c                2                         d           d S N   r   rs   calleds    r?   	callback3z;DeferredTests.test_reentrantRunCallbacks.<locals>.callback3M      MM!rA   c                2                         d           d S Nr   r   r  s    r?   	callback2z;DeferredTests.test_reentrantRunCallbacks.<locals>.callback2P  r  rA   c                \                         d                                          d S rK  r`   r   )rs   r  r  rL   s    r?   	callback1z;DeferredTests.test_reentrantRunCallbacks.<locals>.callback1S  s/    MM!  +++++rA   N)r]   r   r  rs   rN   r4   rN   r!   r   rR   ra   )rf   r  r  r  r  rL   s      @@@r?   test_reentrantRunCallbacksz(DeferredTests.test_reentrantRunCallbacksE  s    
 $,::	 	 	 	 	 		 	 	 	 	 		, 	, 	, 	, 	, 	, 	, 	, 	Y'''Y'''$+++++rA   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.
        rs   rN   r4   c                2                         d           d S r  r   r  s    r?   r  z;DeferredTests.test_nonReentrantCallbacks.<locals>.callback2d  r  rA   c                                         d                                                              dg           d S rK  )r`   r   ra   )rs   r  r  rL   rf   s    r?   r  z;DeferredTests.test_nonReentrantCallbacks.<locals>.callback1g  sG    MM!  +++VaS)))))rA   Nr]   r   r  r  )rf   r  r  r  rL   s   ` @@@r?   test_nonReentrantCallbacksz(DeferredTests.test_nonReentrantCallbacks\  s    
 $,::	 	 	 	 	 		* 	* 	* 	* 	* 	* 	* 	* 	*
 	Y'''$!Q(((((rA   c                   dt                      d	fdd	fd}                    |                               d           |                     t                    }|                     |j        f           dS )
z
        After an exception is raised by a callback which was added to a
        L{Deferred} by a callback on that L{Deferred}, the L{Deferred} should
        call the first errback with a L{Failure} wrapping that exception.
        zcallback raised exceptionrs   r8   r4   rN   c                "    t                    r<   )r  )rs   exceptionMessages    r?   r  zFDeferredTests.test_reentrantRunCallbacksWithFailure.<locals>.callback2y  s    ,---rA   c                2                                    d S r<   )r   )rs   r  rL   s    r?   r  zFDeferredTests.test_reentrantRunCallbacksWithFailure.<locals>.callback1|  s      +++++rA   Nrs   r8   r4   rN   )r!   r   rR   ri   r  ra   r7   )rf   r  rZ   r  rL   r  s      @@@r?   %test_reentrantRunCallbacksWithFailurez3DeferredTests.test_reentrantRunCallbacksWithFailurep  s     7#+::	. 	. 	. 	. 	. 	.	, 	, 	, 	, 	, 	, 	, 	Y'''$//)DD	*:)<=====rA   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<   rK   ignrg  s    r?   r   z=DeferredTests.test_synchronousImplicitChain.<locals>.<lambda>      u rA   Nr   r]   )	r8   r   r&  r!   r   rR   r`   r   rq   )rf   rs   re  rl  rg  s       @r?   test_synchronousImplicitChainz+DeferredTests.test_synchronousImplicitChain  s     f%%!),,,,---*,'.)))'!*%%%7>***gaj&)))))rA   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<   rK   r  s    r?   r   z>DeferredTests.test_asynchronousImplicitChain.<locals>.<lambda>  r  rA   N)r!   r   rR   r`   ra   r8   )rf   re  r   secondResultrs   rg  s        @r?   test_asynchronousImplicitChainz,DeferredTests.test_asynchronousImplicitChain  s     #+**#+::,,,,---$&+,---%'<.///b)))r***vtf---x00000rA   c                h   t          j        t          d                    }|fd
d}t                      }|                    |           |                    d           g }|                    |j                   |                     |d	                    |                     |t                     dS )a  
        If a first L{Deferred} with a L{Failure} result is returned from a
        callback on a second L{Deferred}, the first L{Deferred}'s result is
        converted to L{None} and no unhandled error is logged when it is
        garbage collected.
        First Deferred's Failurer7  rN   rg  r  r4   c                    |S r<   rK   )r7  rg  s     r?   r   z<DeferredTests.test_synchronousImplicitErrorChain.<locals>.cb  s    LrA   Nr   )r7  rN   rg  r  r4   r  )	r   r-  rn   r!   r   rR   r`   r   ri   )rf   rg  r   re  r   s        r?   "test_synchronousImplicitErrorChainz0DeferredTests.test_synchronousImplicitErrorChain  s     
<(BCCDD05 	 	 	 	 	 "*2"$+,---+a.)))##FL99999rA   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<   rK   r  s    r?   r   zCDeferredTests.test_asynchronousImplicitErrorChain.<locals>.<lambda>  r  rA   Nr  r   r]   )r!   r   rR   r_   r`   ra   r   rn   rc   rd   rb   )rf   re  secondErrorr   r  rg  s        @r?   #test_asynchronousImplicitErrorChainz1DeferredTests.test_asynchronousImplicitErrorChain  sB    !)

!),,,,---%'+,---"$+,---#%<.///b)))r***l#=>>???A,,\::;;;tf---\**A.....rA   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<   rK   r  s    r?   r   zGDeferredTests.test_doubleAsynchronousImplicitChaining.<locals>.<lambda>  r  rA   c                    S r<   rK   )r  re  s    r?   r   zGDeferredTests.test_doubleAsynchronousImplicitChaining.<locals>.<lambda>  s    f rA   N)r!   r   r`   r8   rR   ra   )rf   thirdthirdResultrs   rg  re  s       @@r?   'test_doubleAsynchronousImplicitChainingz5DeferredTests.test_doubleAsynchronousImplicitChaining  s     #+**#+::,,,,---"***,,,,---$&+,---t 	b))) 	vvh/////rA   c                   g g t                      dfd}t          j        d                              |                              j                  }|                     ddg                               d	                               j                   |                    j                   |                     g d
           |                     g d           dS )z
        L{Deferred}s can have callbacks that themselves return L{Deferred}s.
        When these "inner" L{Deferred}s fire (even asynchronously), the
        callback chain continues.
        rs   r   r4   rM   c                                         d| f           t          j        d          }d	fd}d
fd}|                    |                              |           |                    j                    |S )Nr  r  rs   r   r4   rM   c                2                         d           S r  r   r  s    r?   r  zXDeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb.<locals>.firstCallback  s    9::: rA   c                <                         d| f           | dz  S r  r   r  s    r?   r  zYDeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb.<locals>.secondCallback  r  rA   r  r  r  r  s       r?   r   zADeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb  s    NNM62333g&&A      " " " " " " MM-((44^DDDLL)))HrA   r  r  r  r  &Got errbacks but wasn't expecting any.r  Nr  r!   r   r&  r   r`   ra   rR   r_   r  s      @@@r?   'test_nestedAsynchronousChainedDeferredsz5DeferredTests.test_nestedAsynchronousChainedDeferreds  s+    68"$  (zz	 	 	 	 	 	 	 	, M'""..r22>>w~NN 	
 	.0JK	
 	
 	
 	x    	))))))X'OPPP  	
 	
 	
 	
 	
rA   c                   g g t                      dfd}t          j        d          }|                    |           |                    j                   |                     ddg                               d	           |                    j                                       j                   |                     g d
           |                     dddd	gfd	d	gg           dS )aK  
        L{Deferred}s can have callbacks that themselves return L{Deferred}s.
        These L{Deferred}s can have other callbacks added before they are
        returned, which subtly changes the callback chain. When these "inner"
        L{Deferred}s fire (even asynchronously), the outer callback chain
        continues.
        rs   r   r4   Deferred[Optional[List[str]]]c                                         d| f           t          j        d          }d
fd}dfd	}|                    |                              |                              j                   S )Nr  r  rs   r   r4   r  c                d                         d| f           dd}                    |          S )Nr  rs   r   r4   r  c                    | gS r<   rK   r}  s    r?   	transformz}DeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.firstCallback.<locals>.transformX  s	    "8OrA   )rs   r   r4   r  r   )rs   r3  r  rl  s     r?   r  zjDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.firstCallbackQ  sD    8999$ $ $ $ ((333rA   r  c                <                         d| f           | dz  S r  r   r  s    r?   r  zkDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.secondCallback]  r  rA   )rs   r   r4   r  )rs   r  r4   r  r  r  s       r?   r   zSDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cbM  s    NNM62333g&&A
4 
4 
4 
4 
4 
4 
4" " " " " "
 m,,^,,HO,,rA   r  r  r  r  r+  r  N)rs   r   r4   r/  r,  r  s      @@@r?   9test_nestedAsynchronousChainedDeferredsWithExtraCallbackszGDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks>  sB     "$  (zz	 	 	 	 	 	 	 	: g&&"'.))) 	.0JK	
 	
 	
 	y!!! 	))))))X'OPPP(*!I;/I&		
 	
 	
 	
 	
rA   c                    t                      }t                      }|                    |           |                     |j        |           dS )zT
        When we chain a L{Deferred}, that chaining is recorded explicitly.
        N)r!   chainDeferredrq   
_chainedTorf   r  bs      r?   &test_chainDeferredRecordsExplicitChainz4DeferredTests.test_chainDeferredRecordsExplicitChain  sD     %JJ$JJ	alA&&&&&rA   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!   r7  rR   r   r8  r9  s      r?   %test_explicitChainClearedWhenResolvedz3DeferredTests.test_explicitChainClearedWhenResolved  sV     %JJ$JJ		

4!,'''''rA   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<   rK   )r]  r:  s    r?   r   zFDeferredTests.test_chainDeferredRecordsImplicitChain.<locals>.<lambda>  s    a rA   N)r!   r   rR   rq   r8  r9  s     @r?   &test_chainDeferredRecordsImplicitChainz4DeferredTests.test_chainDeferredRecordsImplicitChain  sa     %JJ$JJ	''''(((	

4alA&&&&&rA   c                   t                      dfd}                    |                               d           |                     |g          }|                     t          |          d           |d         }|                     |d	         t                     d
}|                     t          j	        ||d                   d
                    ||d                              dS )z
        When a Deferred is returned from a callback directly attached to that
        same Deferred, a warning is emitted.
        rs   r   r4   rM   c                    S r<   rK   rs   r)  s    r?   circularCallbackzADeferredTests.test_circularChainWarning.<locals>.circularCallback      HrA   foor]   r   categoryz1Callback returned the Deferred it was attached tomessagez
Expected match: {!r}
Got: {!r}Nr  )r!   r   rR   flushWarningsra   rb   DeprecationWarningrc   researchrE  )rf   rD  circular_warningswarningpatternr)  s        @r?   test_circularChainWarningz'DeferredTests.test_circularChainWarning  s   
 $::	 	 	 	 	 	 	
&'''	

5 ..0@/ABB.//333#A&,.@AAAEIgwy122/66w	@RSS	
 	
 	
 	
 	
rA   c                p   |                      t          t          dt          j                   t          j        dt
                     t                      dfd}                    |                               d	           | 	                              }|
                    t
                     d
S )z
        If the deprecation warning for circular deferred callbacks is
        configured to be an error, the exception will become the failure
        result of the Deferred.
        filtersrr   )rG  rs   r   r4   rM   c                    S r<   rK   rC  s    r?   rD  zCDeferredTests.test_circularChainException.<locals>.circularCallback  rE  rA   rF  Nr  )r   setattrwarningsrR  filterwarningsrJ  r!   r   rR   r  trap)rf   rD  failurer)  s      @r?   test_circularChainExceptionz)DeferredTests.test_circularChainException  s     			
 	
 	
 	2DEEEE#::	 	 	 	 	 	 	
&'''	

5&&q))'(((((rA   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!   idra   repr)rf   r)  addresss      r?   	test_reprzDeferredTests.test_repr  sI    
 %JJQ%%a"@G"@"@"@"@AAAAArA   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!   rR   ra   r^  r]  r>  s     r?   test_reprWithResultz!DeferredTests.test_reprWithResult  sV    
 $::	

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

4 4 4rA   r  r  c                    |S r<   rK   r]  r  s     r?   cbInnerzMDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.cbInner  s	     rA   c                    |S r<   rK   rr  s     r?   
cbNewInnerzPDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.cbNewInner  s	     !LrA   c                    S r<   rK   )r  lasts    r?   r   zNDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.<lambda>   s    $ rA   r   r]  r8   r4   rN   )r]  r8   r  r  r4   r  )r!   rangepopr   rR   ra   )rf  rl  topinnerDeferredsoriginalInnersr  rs  newInnerru  rw  rk  rf   s            @@r?   chainDeferredsz<DeferredTests.test_boundedStackDepth.<locals>.chainDeferreds  s   E= = = = = = #+**C4 4*/..4 4 4N ,AAA.N#+::D"&&((E :?    
 OOG$$$OO,---  	!)--// >F! ! ! ! !
 !!*---  ! 	! ....///LL' % %t$$$$ UB''' MM$8OrA   r]   r   N)rf  r;  r4   r;  )ra   )rf   r  s   ` r?   test_boundedStackDepthz$DeferredTests.test_boundedStackDepth  sT    /	 /	 /	 /	 /	 /	f 	**NN1,=,=>>>>>rA   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<   rK   rd  s    r?   r   zZDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled.<locals>.<lambda>A  rf  rA   c                    S r<   rK   )r]  r$  s    r?   r   zZDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled.<locals>.<lambda>B  s    5 rA   N)r!   r   rR   r`   ra   )rf   rg  rl  re  r$  s      @@r?   :test_resultOfDeferredResultOfDeferredOfFiredDeferredCalledzHDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled3  s     !)

!) (

00001110000111tt 7>***4&)))))rA   c                   t          j        d           t                      }g }t          d          }	 |# t          $ r |                                 Y nw xY w|                    |j                   |d         }|                     |j	        |           |j
        d         dd         \  }}|                     d|           |                     d|           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   NrK   )r   r   r!   rF   BaseExceptionr   r_   r`   ra   re   framesrf   r)  r(  excr-  localzglobalzs          r?   test_errbackWithNoArgsNoDebugz+DeferredTests.test_errbackWithNoArgsNoDebugJ  s     	5!!!$JJ6""	I 	 	 	IIKKKKK		QXtS)))+a.-V$$$W%%%%%   7 AAc                   t          j        d           t                      }g }t          d          }	 |# t          $ r |                                 Y nw xY w|                    |j                   |d         }|                     |j	        |           |j
        d         dd         \  }}|                     g |           |                     g |           dS )z
        C{Deferred.errback()} creates a failure from the current Python
        exception.  When Deferred.debug is set globals and locals are captured
        in that failure.
        Tr   r   r  N)r   r   r!   rF   r  r   r_   r`   ra   re   r  assertNotEqualr  s          r?   test_errbackWithNoArgsz$DeferredTests.test_errbackWithNoArgs_  s     	4   $JJ6""	I 	 	 	IIKKKKK		QXtS)))+a.-B'''B(((((r  c                v   t                      }|                    d           t          j        d           dd}|                    |           g }|                    |j                   |d         }|j        d         d	d         \  }}|                     d
|           |                     d
|           dS )
        An error raised by a callback creates a Failure.  The Failure captures
        locals and globals if and only if C{Deferred.debug} is set.
        NFr]  r8   r4   rN   c                     t          d          Nr   rF   r]  s    r?   
raiseErrorzHDeferredTests.test_errorInCallbackDoesNotCaptureVars.<locals>.raiseError}      v&&&rA   r   r  rK   rx  )	r!   rR   r   r   r   r_   r`   r  ra   rf   r)  r  r(  r-  r  r  s          r?   &test_errorInCallbackDoesNotCaptureVarsz4DeferredTests.test_errorInCallbackDoesNotCaptureVarst  s    
 %JJ	

45!!!	' 	' 	' 	' 	
j!!!	QXt+a.-V$$$W%%%%%rA   c                v   t                      }|                    d           t          j        d           d
d}|                    |           g }|                    |j                   |d         }|j        d         d	d         \  }}|                     g |           |                     g |           dS )r  NTr]  r8   r4   rN   c                     t          d          r  r  r  s    r?   r  zODeferredTests.test_errorInCallbackCapturesVarsWhenDebugging.<locals>.raiseError  r  rA   r   r  rx  )	r!   rR   r   r   r   r_   r`   r  r  r  s          r?   -test_errorInCallbackCapturesVarsWhenDebuggingz;DeferredTests.test_errorInCallbackCapturesVarsWhenDebugging  s    
 %JJ	

44   	' 	' 	' 	' 	
j!!!	QXt+a.-B'''B(((((rA   c                    dd}t          j         |                      }t          |t           t                              |                     d|                     |                     dS )zM
        It can discover the return type of a first level coroutine.
        r4   r;  c                 
   K   dS rK  rK   rK   rA   r?   
returnsIntz4DeferredTests.test_fromCoroutine.<locals>.returnsInt  r  rA   r]   Nr  )r!   r=   r   r;  ra   r  )rf   r  r)  s      r?   test_fromCoroutinez DeferredTests.test_fromCoroutine  sl    
	 	 	 	 "::<<00Ax}%%%D003344444rA   c                    d dd| j         dt          g}|D ]-}|                     t          j        t          j        |           .dS )
        L{Deferred.fromCoroutine} requires a coroutine object or a generator,
        and will reject things that are not that.
        c                    | S r<   rK   r  s    r?   r   zCDeferredTests.test_fromCoroutineRequiresCoroutine.<locals>.<lambda>      a rA   r]   TN#test_fromCoroutineRequiresCoroutiner   assertRaisesNotACoroutineErrorr!   r=   rf   thingsThatAreNotCoroutinesthings      r?   r  z1DeferredTests.test_fromCoroutineRequiresCoroutine  d     K4&
" 0 	W 	WEe68NPUVVVV	W 	WrA   GC works differently on PyPy.c                @   t                      }t          j        |          }t          |          }||_        t          j        |          }|                    d           ~~|                      |                       |                      |                       dS )z|
        A circular reference between a `Deferred` and its canceller
        is broken when the deferred is called.
        N)DummyCancellerweakrefrefr!   rL   rR   r   )rf   	cancellerweakCancellerrL   weakDeferreds        r?   *test_canceller_circular_reference_callbackz8DeferredTests.test_canceller_circular_reference_callback  s     #$$	I.. #+9"5"5%	{8,,$  	--//***,,..)))))rA   c                   t                      }t          j        |          }t          |          }||_        t          j        |          }t          t          d                    }|                    |           |                     |           ~~| 	                     |                       | 	                     |                       dS )zx
        A circular reference between a `Deferred` and its canceller
        is broken when the deferred fails.
        The test demands failures.N)
r  r  r  r!   rL   r0   r  r   r  r   )rf   r  r  rL   r  rX  s         r?   )test_canceller_circular_reference_errbackz7DeferredTests.test_canceller_circular_reference_errback  s     #$$	I.. #+9"5"5%	{8,,)$@AABB!!! 	X&&& 	--//***,,..)))))rA   c                
   t                      }t          j        |          }t          |          }||_        |                    d            |                    d           ~~|                      |                       dS )z
        A circular reference between a `Deferred` and its canceller is broken
        when the deferred is called, even if another deferred gets added to its
        chain.
        c                    t                      S r<   rn  r6  s    r?   r   zKDeferredTests.test_canceller_circular_reference_non_final.<locals>.<lambda>  s
    xzz rA   N)r  r  r  r!   rL   r   rR   r   )rf   r  r  rL   s       r?   +test_canceller_circular_reference_non_finalz9DeferredTests.test_canceller_circular_reference_non_final  s     #$$	I.. #+9"5"5%	11222$ 	--//*****rA   c                   |                      t          j        dd         t          t          g            G d dt          t
                             }|                      t          j        d         |           t          j                            d           t          j        D ]*}|                     t          |t                               +dS )zQ
        C{_DEFERRED_SUBCLASSES} includes all subclasses of L{Deferred}.
        Nr   c                      e Zd ZdS )2DeferredTests.test_DEFERRED_SUBCLASSES.<locals>.D2NrG   rK   rA   r?   D2r    r  rA   r  )	ra   r   _DEFERRED_SUBCLASSESr!   r#   r;  rz  rc   r   )rf   r  klasss      r?   test_DEFERRED_SUBCLASSESz&DeferredTests.test_DEFERRED_SUBCLASSES  s     	3BQB7(L9QRRR	 	 	 	 	# 	 	 	 	3B7<<<"&&r*** / 	9 	9EOOJuh778888	9 	9rA   Nr   )r7   r8   r9   r8   r4   r   r7   r8   r9   r8   r4   rN   )r7   r0   r9   r8   r4   rN   )ZrH   rI   rJ   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r!  r#  r*  r.  r2  r<  r?  rG  rQ  rZ  ra  rh  rm  rt  rw  rz  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r&  r-  r5  r;  r=  r@  rP  rY  r`  rb  rd  r  r  r  r  r  r  r  r  pyunitskipIfr.   r  r  r  r  rK   rA   r?   r   r      sD       B B B B   - - - -+ + + +A A A AI I I IQ Q Q QB B B B5 5 5 5A A A A"5 5 5 5,"
 "
 "
 "
H% % % %('. '. '. '.R? ? ? ?"? ? ? ?"& & & && & & &"
@ 
@ 
@ 
@B B B B$
@ 
@ 
@ 
@9 9 9 9K K K K: : : :"+ + + + ) ) ) ).) ) ) )= = = =) ) ) )2 2 2 22 2 2 2
= 
= 
= 
=   
 
 
 
6
 
 
 
<3 3 3 3,- - - -() ) ) ) & & & & > > > >(K K K K? ? ? ?1 1 1 1( ( ( ( > > > >"2 2 2 2$
. 
. 
. 
.	6 	6 	6 	6E E E ED D D D2
 
 
 
.*
 *
 *
 *
X%
 %
 %
 %
N-
 -
 -
 -
^, , , ,.) ) ) )(> > > >(* * * *&1 1 1 14: : : :(/ / / /:0 0 0 0>;
 ;
 ;
 ;
zF
 F
 F
 F
P' ' ' '( ( ( (
' 
' 
' 
'
 
 
 
.) ) ) ).B B B BY Y Y Y
 
 
 
9? 9? 9? 9?v* * * *.& & & &*) ) ) )*& & & &() ) ) )(
5 
5 
5 
5W W W W. V]59::* * * ;:*8 V]59::* * * ;:*> V]59::+ + + ;:+49 9 9 9 9 9rA   r   c                  *    e Zd ZU dZdZded<   d	dZdS )
r  z
    A callable that does nothing.
    It is intended to be used just to get an object reference
    to support GC related tests.
    NzOptional[Deferred[Any]]rL   rY   r4   rN   c                    dS )zV
        This is not expected to be called as part of the current test suite.
        NrK   r   s     r?   __call__zDummyCanceller.__call__2  s      rA   )rL   rY   r4   rN   )rH   rI   rJ   rj   rL   __annotations__r  rK   rA   r?   r  r  )  sG           )-H,,,,     rA   r  numDeferredsr;  (tuple[list[int], list[Deferred[object]]]c                    dg| z  g }t          |           D ]-}|fd
fd}|                    t          |	                     .|fS )z
    Create a list of Deferreds and a corresponding list of integers
    tracking how many times each Deferred has been cancelled.  Without
    additional steps the Deferreds will never fire.
    r   r)  r  nr;  r4   rN   c                (    |xx         dz  cc<   d S rK  rK   )r)  r  cancelledStates     r?   r   z_setupRaceState.<locals>.cancelC  s%    1"rA   r  )r)  r  r  r;  r4   rN   )ry  r`   r!   )r  dsr  r   r  s       @r?   _setupRaceStater  8  s     S<'N!#B<   . .12 	# 	# 	# 	# 	# 	# 	# 			(V,,,----2rA   c                  j   e Zd ZdZ e edd           edd                    dd            Z e edd           edd                    dd            Z e edd           edd                    dd            ZddZ	 e edd                    dd            Z
dS )	RaceTestsz
    Tests for L{race}.
    r   r  )	min_value	max_value)beforeWinnerafterWinnerr  r;  r  r4   rN   c                f   t          |dz   |z             \  }}t          |          }t                      }||                             |           t	          |                     |          t          ||f                     dg|z  dgz   dg|z  z   }t	          |t          |                     dS )a	  
        When one of the L{Deferred}s passed to L{race} fires successfully,
        the L{Deferred} return by L{race} fires with the index of that
        L{Deferred} and its result and cancels the rest of the L{Deferred}s.

        @param beforeWinner: A randomly selected number of Deferreds to
            appear before the "winning" Deferred in the list passed in.

        @param beforeWinner: A randomly selected number of Deferreds to
            appear after the "winning" Deferred in the list passed in.
        r]   r   N)r  r+   r8   rR   r   r  r   )rf   r  r  r  r  
raceResultr_  expectedCancelledStates           r?   test_successzRaceTests.test_successP  s      -\A-=-KLL"XX
88
<!!(+++ 	  ,,lH-..	
 	
 	

 #$|!3qc!9QC+<M!M+,,	
 	
 	
 	
 	
rA   c           	        t          |dz   |z             \  }}t          t          d                    }t          |          }|D ]}|                    |           |                     |t                    }t          |j        j	        t          |gt          |          z                       t          |t          dgt          |          z                       dS )a  
        When all of the L{Deferred}s passed to L{race} fire with failures,
        the L{Deferred} return by L{race} fires with L{FailureGroup} wrapping
        all of their failures.

        @param beforeWinner: A randomly selected number of Deferreds to
            appear before the "winning" Deferred in the list passed in.

        @param beforeWinner: A randomly selected number of Deferreds to
            appear after the "winning" Deferred in the list passed in.
        r]   r  r   N)r  r0   r  r+   r   r  r'   r   re   rh   r   rb   )	rf   r  r  r  r  rX  r  r)  actualFailures	            r?   test_failurezRaceTests.test_failures  s      -\A-=-KLL)$@AABB"XX
 	 	AIIg,,ZFF(gYR())	
 	
 	
 	aS3r77]##	
 	
 	
 	
 	
rA   c                Z   d t          |dz   |z             D             }t          |          }||                             d           ||dz                                d           |                     |           t	          |                                 t                                 dS )z
        If one of the Deferreds fires after it was cancelled its result
        goes nowhere.  In particular, it does not cause any errors to be
        logged.
        c                *    g | ]}t                      S rK   rn  )ro  r  s     r?   rp  z4RaceTests.test_resultAfterCancel.<locals>.<listcomp>  s)     $
 $
 $
HJJ$
 $
 $
rA   r   Nr]   )ry  r+   rR   r  r   rp   r   )rf   r  r  r  r  s        r?   test_resultAfterCancelz RaceTests.test_resultAfterCancel  s    $
 $
 %lQ&6&D E E$
 $
 $
 "XX

<!!$'''
<!%%d+++Z(((D**,,egg66666rA   c                   t                      }|t          d           g}t                      }t          |          }|                    |           t	          |                     |          t          d|f                     dS )z~
        If one of the input Deferreds has a cancel function that fires it
        with success, nothing bad happens.
        c                D    |                      t                                S r<   )rR   r8   r)  s    r?   r   z1RaceTests.test_resultFromCancel.<locals>.<lambda>  s    FHH)=)= rA   r  r   N)r!   r8   r+   rR   r   r  r   )rf   winnerr  r_  r  s        r?   test_resultFromCancelzRaceTests.test_resultFromCancel  s    
 $,::==>>>&
 88"XX
!!!D((44h8}6M6MNNNNNrA   r]   )r  r  c                    t          |          \  }}t          |          }|                                 t          |t	          dg|z                       |                     |t                     dS )zu
        If the result of L{race} is cancelled then all of the L{Deferred}s
        passed in are cancelled.
        r]   N)r  r+   r   r   r   r  r'   )rf   r  r  r  r  s        r?   test_cancelzRaceTests.test_cancel  sn     -\::"XX
NHaS<-?$@$@AAAZ66666rA   N)r  r;  r  r;  r4   rN   r   )r  r;  r4   rN   )rH   rI   rJ   rj   r   r   r  r  r  r  r  rK   rA   r?   r  r  K  s         UXQ777HqA666  
 
 
	 
> UXQ777HqA666  
 
 
	 
: UXQ777HqA666  7 7 7	 7&O O O O  UXQ777  7 7 7 7 7 7rA   r  c                  *    e Zd ZdZddZddZddZdS )	FirstErrorTestsz"
    Tests for L{FirstError}.
    r4   rN   c                    t          d          }	 |# t          $ r t                      }Y nw xY wt          j        |d          }|                     t          |          dt          |           d           dS )z
        The repr of a L{FirstError} instance includes the repr of the value of
        the sub-failure and the index which corresponds to the L{FirstError}.
        	some textr  zFirstError[#3, ]N)rY  r  r0   r   r   ra   r^  rf   r  r2   rr   s       r?   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}.
        r  r  zFirstError[#5, r  N)rY  r  r0   r   r   ra   r   r  s       r?   test_strzFirstErrorTests.test_str  s    
 %%	I 	 	 			AAA	  A&&U%@s1vv%@%@%@AAAAAr  c                $   	 ddz   n# t           $ r t                      }Y nw xY wt          j        |d          }t          j        |d          }	 t	          d          # t           $ r t                      }Y nw xY wt          j        |d          }|                     ||k               |                     ||k               |                     ||k               |                     ||k               |                     |dk               dS )z
        L{FirstError} instances compare equal to each other if and only if
        their failure and index compare equal.  L{FirstError} instances do not
        compare equal to instances of other types.
        r]   r      bar	   r  N)r  r0   r   r   rY  rc   r	  )rf   firstFailureone
anotherOnesecondFailureanothers         r?   test_comparisonzFirstErrorTests.test_comparison  s+   	%FFF 	% 	% 	%"99LLL	% |R00%lB77
	&U### 	& 	& 	&#IIMMM	& "=!44z)***(((w'''
*+++#####s    ##A   A;:A;Nr   )rH   rI   rJ   rj   r`  r  r  rK   rA   r?   r  r    sb         F F F FB B B B$ $ $ $ $ $rA   r  c                      e Zd Zd)dZd)dZd*dZd*d	Zd+dZd+dZd+dZ	d+dZ
d)dZd)dZd)dZd)dZd,dZd-d!Zd)d"Zd)d#Zd)d$Zd)d%Zd)d&Zd)d'Zd(S ).AlreadyCalledTestsr4   rN   c                ^    t          j                    | _        t          j        d           d S NT)r   r   _deferredWasDebuggingr   r   s    r?   r   zAlreadyCalledTests.setUp  s*    %*%7%9%9"4     rA   c                8    t          j        | j                   d S r<   )r   r   r  r   s    r?   tearDownzAlreadyCalledTests.tearDown  s    4566666rA   r7   r8   r9   c                    d S r<   rK   r   s      r?   r   zAlreadyCalledTests._callback      rA   c                    d S r<   rK   r   s      r?   r   zAlreadyCalledTests._errback  r  rA   r)  rM   c                0    |                     d           d S )Nr   rQ   r>  s     r?   _call_1zAlreadyCalledTests._call_1      	

7rA   c                0    |                     d           d S )NtwicerQ   r>  s     r?   _call_2zAlreadyCalledTests._call_2  r  rA   c                b    |                     t          t                                           d S r<   r   r0   rn   r>  s     r?   _err_1zAlreadyCalledTests._err_1  &    			',..))*****rA   c                b    |                     t          t                                           d S r<   r  r>  s     r?   _err_2zAlreadyCalledTests._err_2!  r  rA   c                    t                      }|                    | j        | j                   |                     |           |                     t          j        | j        |           d S r<   )	r!   r   r   r   r  r  r   AlreadyCalledErrorr  r>  s     r?   testAlreadyCalled_CCz'AlreadyCalledTests.testAlreadyCalled_CC$  sU    #::	t~t}555Q%2DL!DDDDDrA   c                    t                      }|                    | j        | j                   |                     |           |                     t          j        | j        |           d S r<   )	r!   r   r   r   r  r  r   r  r  r>  s     r?   testAlreadyCalled_CEz'AlreadyCalledTests.testAlreadyCalled_CE*  sU    #::	t~t}555Q%2DKCCCCCrA   c                    t                      }|                    | j        | j                   |                     |           |                     t          j        | j        |           d S r<   )	r!   r   r   r   r  r  r   r  r  r>  s     r?   testAlreadyCalled_EEz'AlreadyCalledTests.testAlreadyCalled_EE0  sU    #::	t~t}555A%2DKCCCCCrA   c                    t                      }|                    | j        | j                   |                     |           |                     t          j        | j        |           d S r<   )	r!   r   r   r   r  r  r   r  r  r>  s     r?   testAlreadyCalled_ECz'AlreadyCalledTests.testAlreadyCalled_EC6  sU    #::	t~t}555A%2DL!DDDDDrA   linetyper   funclinesr  r_  r;  c                    d}|D ]7}|                     d|z            r|                    d|z            r|dz  }8|                     ||k               d S )Nr   z %s:z %sr]   )r   endswithrc   )rf   r  r   r!  r_  countlines          r?   _countzAlreadyCalledTests._count<  sm     	 	Dv011 dmmEDL6Q6Q 
)*****rA   r   r  callerinvoker1invoker2c                   |j         d                             d          }|                     d||d           |                     dd|d           |                     dd|d           |                     dd|d           |                     dd|d           |                     d	||d           |                     d	||d           d S )
Nr   
Cr]   r  r  r  r  I)r7   splitr&  )rf   r   r'  r(  r)  r!  s         r?   _checkzAlreadyCalledTests._checkC  s    q	%% 	C***CE1---CE1---C5!,,,C5!,,,C5!,,,C5!,,,,,rA   c                L   t                      }|                    | j        | j                   |                     |           	 |                     |           |                     d           d S # t          j        $ r#}| 	                    |ddd           Y d }~d S d }~ww xY w)N2second callback failed to raise AlreadyCalledErrortestAlreadyCalledDebug_CCr  r  )
r!   r   r   r   r  r  r-  r   r  r/  rf   r)  r   s      r?   r2  z,AlreadyCalledTests.testAlreadyCalledDebug_CCQ  s    #::	t~t}555Q	LLLOOO IIJKKKKK ' 	N 	N 	NKK6	9MMMMMMMMM	N   A1 1B# BB#c                L   t                      }|                    | j        | j                   |                     |           	 |                     |           |                     d           d S # t          j        $ r#}| 	                    |ddd           Y d }~d S d }~ww xY w)N1second errback failed to raise AlreadyCalledErrortestAlreadyCalledDebug_CEr  r  )
r!   r   r   r   r  r  r-  r   r  r/  r3  s      r?   r7  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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)Nr1  testAlreadyCalledDebug_ECr  r  )
r!   r   r   r   r  r  r-  r   r  r/  r3  s      r?   r9  z,AlreadyCalledTests.testAlreadyCalledDebug_ECg  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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)Nr6  testAlreadyCalledDebug_EEr  r  )
r!   r   r   r   r  r  r-  r   r  r/  r3  s      r?   r;  z,AlreadyCalledTests.testAlreadyCalledDebug_EEr  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r4  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)NFr1  )r   r   r!   r   r   r   r  r  r-  r  r	  r7   r3  s      r?   testNoDebuggingz"AlreadyCalledTests.testNoDebugging}  s    5!!!#::	t~t}555Q	LLLOOO IIJKKKKK ' 	% 	% 	%QV$$$$$$$$$	%s   B B9B44B9c                   t          j        d           t                      }|                    d            t          j        d           |                    d            t          j        d           t                      }|                    d            t          j        d           |                    d            d S )NFc                    d S r<   rK   r  s    r?   r   z8AlreadyCalledTests.testSwitchDebugging.<locals>.<lambda>      d rA   Tc                    d S r<   rK   r@  s    r?   r   z8AlreadyCalledTests.testSwitchDebugging.<locals>.<lambda>  rA  rA   )r   r   r!   addBothrR   r>  s     r?   testSwitchDebuggingz&AlreadyCalledTests.testSwitchDebugging  s     	5!!!$JJ			""###4   	

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

44   			""#####rA   Nr   r  )r)  rM   r4   rN   )
r  r   r   r   r!  r  r_  r;  r4   rN   )
r   r  r'  r   r(  r   r)  r   r4   rN   )rH   rI   rJ   r   r  r   r   r  r  r  r  r  r  r  r  r&  r/  r2  r7  r9  r;  r=  rD  rK   rA   r?   r  r  
  s       ! ! ! !7 7 7 7            + + + ++ + + +E E E ED D D DD D D DE E E E+ + + +- - - -	L 	L 	L 	L	K 	K 	K 	K	L 	L 	L 	L	K 	K 	K 	K
L 
L 
L 
L$ $ $ $ $ $rA   r  c                      e Zd ZddZddZddZddZddZddZddZ	ddZ
ddZddZddZddZddZddZddZddZddZddZddZdS )DeferredCancellerTestsr4   rN   c                >    d | _         d | _        d | _        d| _        d S r   )r   r   r   cancellerCallCountr   s    r?   r   zDeferredCancellerTests.setUp  s(    .215/3"#rA   c                <    |                      | j        d           d S )Nro  )assertInrH  r   s    r?   r  zDeferredCancellerTests.tearDown  s    d-v66666rA   datar   c                    || _         |S r<   r   rf   rK  s     r?   r   z DeferredCancellerTests._callback  s    #rA   c                    || _         d S r<   r   rM  s     r?   r   z!DeferredCancellerTests._callback2  s     $rA   rr   r0   c                    || _         d S r<   r   )rf   rr   s     r?   r   zDeferredCancellerTests._errback  s    #rA   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   ra   r   r   r   r   r   r>  s     r?   test_noCancellerz'DeferredCancellerTests.test_noCanceller  s}    
 %JJ	t~t}555	


"...,153GHHH$./////rA   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   rR   r  r   r  r   r  r>  s     r?   !test_raisesAfterCancelAndCallbackz8DeferredCancellerTests.test_raisesAfterCancelAndCallback  s     %JJ	t~t}555	


 	


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


 	
		)++ 	%2AJEEE%2AIy{{KKKKKrA   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   rR   rq   rf   r)  currentFailures      r?   5test_noCancellerMultipleCancelsAfterCancelAndCallbackzLDeferredCancellerTests.test_noCancellerMultipleCancelsAfterCancelAndCallback  sv    
 %JJ	t~t}555	


,	

4	


nd&9:::::rA   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   ra   r   r   r   r   rF   rq   rW  s      r?   4test_noCancellerMultipleCancelsAfterCancelAndErrbackzKDeferredCancellerTests.test_noCancellerMultipleCancelsAfterCancelAndErrback  s    
 %JJ	t~t}555	


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


nd&9:::::rA   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   ra   r   r   r   rq   rW  s      r?   test_noCancellerMultipleCancelz5DeferredCancellerTests.test_noCancellerMultipleCancel  s     %JJ	t~t}555	


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


nd&9:::::rA   c                    d	 fd}t          |          }|                     j         j                   |                                  j        J                       j        j        t          j	                    j        }|                                  
                    | j                                         j        d           dS )
a,  
        Verify that calling cancel multiple times on a deferred with a
        canceller that does not errback results in a
        L{defer.CancelledError} and that subsequent calls to cancel do not
        cause an error and that after all that, the canceller was only
        called once.
        r)  r  r4   rN   c                (    xj         dz  c_         d S rK  rH  r)  rf   s    r?   r   zCDeferredCancellerTests.test_cancellerMultipleCancel.<locals>.cancel	      ##q(####rA   r  Nr]   r)  r  r4   rN   )r!   r   r   r   r   r   ra   r   r   r   rq   rH  )rf   r   r)  rX  s   `   r?   test_cancellerMultipleCancelz3DeferredCancellerTests.test_cancellerMultipleCancel	  s    	) 	) 	) 	) 	) 	) %v666	t~t}555	


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


nd&9:::0!44444rA   c                    d	 fd}t          |          }|                     j         j                   |                                                       j        d            j        J                       j        j        t          j
                                        t          j        |j        d                                t          j        |j        t                                 dS )
z
        Verify that a L{Deferred} calls its specified canceller when
        it is cancelled, and that further call/errbacks raise
        L{defer.AlreadyCalledError}.
        r)  r  r4   rN   c                (    xj         dz  c_         d S rK  r`  ra  s    r?   r   z;DeferredCancellerTests.test_simpleCanceller.<locals>.cancel)	  rb  rA   r  r]   Nrc  )r!   r   r   r   r   ra   rH  r   r   r   r   r  r  rR   r   r  rf   r   r)  s   `  r?   test_simpleCancellerz+DeferredCancellerTests.test_simpleCanceller"	  s    	) 	) 	) 	) 	) 	) %v666	t~t}555	


0!444"...,153GHHH 	%2AJEEE%2AIy{{KKKKKrA   c                     d fd}t          |                               j         j                                                    dS )	zQ
        Verify that a canceller is given the correct deferred argument.
        r   r  r4   rN   c                4                         |            d S r<   )rq   )r   r)  rf   s    r?   r   z8DeferredCancellerTests.test_cancellerArg.<locals>.cancel<	  s    MM"a     rA   r  N)r   r  r4   rN   )r!   r   r   r   r   rg  s   ` @r?   test_cancellerArgz(DeferredCancellerTests.test_cancellerArg7	  sb    
	! 	! 	! 	! 	! 	! 	! %v666	t~t}555	




rA   c                f    d
 fd}t          |          }|                     j         j                   |                    d           |                                                       j        d                                 j	                                         j
        d           d	S )zo
        Test that cancelling a deferred after it has been callbacked does
        not cause an error.
        r)  r  r4   rN   c                j    xj         dz  c_         |                     t                                 d S rK  rH  r   rF   ra  s    r?   r   z?DeferredCancellerTests.test_cancelAfterCallback.<locals>.cancelI	  3    ##q(##IIlnn%%%%%rA   r  zbiff!r   Nrc  )r!   r   r   r   rR   r   ra   rH  r   r   r   rg  s   `  r?   test_cancelAfterCallbackz/DeferredCancellerTests.test_cancelAfterCallbackC	  s    	& 	& 	& 	& 	& 	& $f555	t~t}555	

7	


0!444$-...-w77777rA   c                    d	 fd}t          |          }|                     j         j                   |                    t                                 |                                                       j        d            j	        J                       j	        j
        t
                                           j                   dS )
z
        Test that cancelling a L{Deferred} after it has been errbacked does
        not result in a L{defer.CancelledError}.
        r)  r  r4   rN   c                j    xj         dz  c_         |                     t                                 d S rK  rn  ra  s    r?   r   z>DeferredCancellerTests.test_cancelAfterErrback.<locals>.cancel[	  ro  rA   r  r   Nrc  )r!   r   r   r   r   rF   r   ra   rH  r   r   r   r   rg  s   `  r?   test_cancelAfterErrbackz.DeferredCancellerTests.test_cancelAfterErrbackU	  s    	& 	& 	& 	& 	& 	& %v666	t~t}555			,..!!!	


0!444"...,1<@@@$./////rA   c                .    d	 fd}t          |          }|                     j         j                   |                                                       j        d            j        J                       j        j        t                     dS )
z?
        Test a canceller which errbacks its deferred.
        r)  r  r4   rN   c                j    xj         dz  c_         |                     t                                 d S rK  rn  ra  s    r?   r   zADeferredCancellerTests.test_cancellerThatErrbacks.<locals>.cancelm	  ro  rA   r  r]   Nrc  )
r!   r   r   r   r   ra   rH  r   r   rF   rg  s   `  r?   test_cancellerThatErrbacksz1DeferredCancellerTests.test_cancellerThatErrbacksh	  s    
	& 	& 	& 	& 	& 	& %v666	t~t}555	


0!444"...,1<@@@@@rA   c                <    d
 fd}t          |          }|                     j         j                   |                                                       j        d                                 j        d                                 j	                   d	S )z<
        Test a canceller which calls its deferred.
        r)  r  r4   rN   c                R    xj         dz  c_         |                     d           d S )Nr]   hello!)rH  rR   ra  s    r?   r   zBDeferredCancellerTests.test_cancellerThatCallbacks.<locals>.cancel}	  s/    ##q(##JJx     rA   r  r]   ry  Nrc  )
r!   r   r   r   r   ra   rH  r   r   r   rg  s   `  r?   test_cancellerThatCallbacksz2DeferredCancellerTests.test_cancellerThatCallbacksx	  s    
	! 	! 	! 	! 	! 	! %v666	t~t}555	


0!444-x888$-.....rA   c                    d fd}d fd}t          |          t          |          }|                    d           |                    fd	           |                                 |                     j         j                                         j        d
            j	        J                       j	        j
        t          j                   dS )z
        Verify that a Deferred, a, which is waiting on another Deferred, b,
        returned from one of its callbacks, will propagate
        L{defer.CancelledError} when a is cancelled.
        r)  r  r4   rN   c                (    xj         dz  c_         d S rK  r`  ra  s    r?   innerCancelzEDeferredCancellerTests.test_cancelNestedDeferred.<locals>.innerCancel	  rb  rA   c                2                         d           d S NF)rc   ra  s    r?   r   z@DeferredCancellerTests.test_cancelNestedDeferred.<locals>.cancel	  s    OOE"""""rA   r  Nc                    S r<   rK   )rK  r:  s    r?   r   zBDeferredCancellerTests.test_cancelNestedDeferred.<locals>.<lambda>	  s    1 rA   r]   rc  )r!   rR   r   r   r   r   r   ra   rH  r   r   r   r   )rf   r}  r   r  r:  s   `   @r?   test_cancelNestedDeferredz0DeferredCancellerTests.test_cancelNestedDeferred	  s    	) 	) 	) 	) 	) 	)	# 	# 	# 	# 	# 	# %{;;;$v666	

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


	t~t}5550!444 "...,153GHHHHHrA   Nr   )rK  r   r4   r   )rK  r   r4   rN   )rr   r0   r4   rN   )rH   rI   rJ   r   r  r   r   r   rQ  rS  rU  rY  r[  r]  rd  rh  rk  rp  rs  rv  rz  r  rK   rA   r?   rF  rF    s       $ $ $ $7 7 7 7   % % % %$ $ $ $
0 
0 
0 
0L L L L"L L L L"; ; ; ;; ; ; ;$; ; ; ;5 5 5 5,L L L L*
 
 
 
8 8 8 8$0 0 0 0&A A A A / / / / I I I I I IrA   rF  c                  j    e Zd ZdZddZddZddZddZdd	Zdd
Z	ddZ
ddZddZddZddZdS )LogTestsz+
    Test logging of unhandled errors.
    r4   rN   c                P    g | _         t          j        | j         j                   dS )z<
        Add a custom observer to observer logging.
        N)cr-   r   r`   r   s    r?   r   zLogTests.setUp	  s%     (*&&&&&rA   c                B    t          j        | j        j                   dS )z&
        Remove the observer.
        N)r-   removeObserverr  r`   r   s    r?   r  zLogTests.tearDown	  s     	46=)))))rA   List[Dict[str, Any]]c                $    d | j         D             S )Nc                "    g | ]}|d          
|S )isErrorrK   )ro  r   s     r?   rp  z*LogTests._loggedErrors.<locals>.<listcomp>	  s!    222aQy\2222rA   r  r   s    r?   _loggedErrorszLogTests._loggedErrors	  s    22462222rA   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   r]   rX  N)r  ra   rb   rW  r9  rp   )rf   c2s     r?   r/  zLogTests._check	  sj     !!R!$$$
1i/000011111rA   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 r5  rK   r  s    r?   r   z(LogTests.test_errorLog.<locals>.<lambda>	  
    a rA   r]   N)r!   r   rR   gccollectr/  r   s    r?   test_errorLogzLogTests.test_errorLog	  sJ     	

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

rA   c                p    dd} |             t          j                     |                                  dS )zD
        Same as L{test_errorLog}, but with an inner frame.
        r4   rN   c                 x    t                      } |                     d            |                     d           d S )Nc                    ddz  S r5  rK   r  s    r?   r   z`LogTests.test_errorLogWithInnerFrameRef.<locals>._subErrorLogWithInnerFrameRef.<locals>.<lambda>	  
    AF rA   r]   )r!   r   rR   r  s    r?   _subErrorLogWithInnerFrameRefzNLogTests.test_errorLogWithInnerFrameRef.<locals>._subErrorLogWithInnerFrameRef	  s4    'zzAMM**+++JJqMMMMMrA   Nr   r  r  r/  )rf   r  s     r?   test_errorLogWithInnerFrameRefz'LogTests.test_errorLogWithInnerFrameRef	  sC    
	 	 	 	
 	&%'''

rA   c                p    dd} |             t          j                     |                                  dS )zQ
        Same as L{test_errorLogWithInnerFrameRef}, plus create a cycle.
        r4   rN   c                     t                      } | fdd}|                     |           | | _        |                     d           d S )	Nr  r;  r)  Deferred[int]r4   c                    ddz  S r5  rK   )r  r)  s     r?   unusedCyclezgLogTests.test_errorLogWithInnerFrameCycle.<locals>._subErrorLogWithInnerFrameCycle.<locals>.unusedCycle	  s    AvrA   r]   )r  r;  r)  r  r4   r;  )r!   r   _drR   )r)  r  s     r?   _subErrorLogWithInnerFrameCyclezRLogTests.test_errorLogWithInnerFrameCycle.<locals>._subErrorLogWithInnerFrameCycle	  sU    'zzA78      MM+&&&ADJJqMMMMMrA   Nr   r  )rf   r  s     r?    test_errorLogWithInnerFrameCyclez)LogTests.test_errorLogWithInnerFrameCycle	  sC    
		 		 		 		 	(')))

rA   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 r5  rK   r  s    r?   r   z.LogTests.test_errorLogNoRepr.<locals>.<lambda>	  r  rA   r]   r   r  Nr    Expected message starting with: )r!   r   rR   r  r  r/  ra   rb   r  r-   r|   rc   r   )rf   msgr_  s      r?   test_errorLogNoReprzLogTests.test_errorLogNoRepr	  s    
 	

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

CKK(((#DF2J//0NN8$$;x;;	
 	
 	
 	
 	
rA   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.
        r4   rN   c                     t                      } d| _        |                     d            |                     d           d S )NTc                    ddz  S r5  rK   r  s    r?   r   z?LogTests.test_errorLogDebugInfo.<locals>.doit.<locals>.<lambda>

  r  rA   r]   )r!   debugr   rR   r  s    r?   doitz-LogTests.test_errorLogDebugInfo.<locals>.doit
  s;    'zzAAGMM**+++JJqMMMMMrA   r   r  Nz
(debug:  Ir  r   )
r  r  r/  ra   rb   r  r-   r|   rc   r   )rf   r  r  r_  s       r?   test_errorLogDebugInfozLogTests.test_errorLogDebugInfo 
  s    	 	 	 	 	

CKK(((#DF2J//NN8$$;x;;	
 	
 	
 	
 	
rA   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-  rn   r@  s    r?   r   z3LogTests.test_chainedErrorCleanup.<locals>.<lambda>!
  s    %*\&-A-A"B"B rA   Nr]   r   )r!   r   rR   r   r`   ra   rb   rW  r  r  r  r  )rf   r)  rl  rv   s       r?   test_chainedErrorCleanupz!LogTests.test_chainedErrorCleanup
  s     %JJ	BBCCC	

4 ! "	w~v}555"%%%Va(((q	y!!! VQ 	
++--r22222rA   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<   rK   )r]  goods    r?   r   z6LogTests.test_errorClearedByChaining.<locals>.<lambda>A
  s    t rA   )	r   r-  r  r!   r_   r  r  ra   r  )rf   badr  s     @r?   test_errorClearedByChainingz$LogTests.test_errorClearedByChaining6
  s     ).
9W3E3E(F(F)1++++,,, s

++--r22222rA   Nr   )r4   r  )rH   rI   rJ   rj   r   r  r  r/  r  r  r  r  r  r  r  rK   rA   r?   r  r  	  s         ' ' ' '* * * *3 3 3 32 2 2 2         (
 
 
 
&
 
 
 
43 3 3 383 3 3 3 3 3rA   r  c                  .    e Zd Zd
dZd
dZddZd
dZd	S )DeferredListEmptyTestsr4   rN   c                    d| _         d S r   )callbackRanr   s    r?   r   zDeferredListEmptyTests.setUpJ
  s    rA   c                X    t          g           }|                    | j                   dS )zTesting empty DeferredList.N)r#   r   cb_empty)rf   r   s     r?   testDeferredListEmptyz,DeferredListEmptyTests.testDeferredListEmptyM
  s)    9Eb9I9I
t}%%%%%rA   resList[Tuple[bool, object]]c                @    d| _         |                     g |           d S rK  )r  ra   )rf   r  s     r?   r  zDeferredListEmptyTests.cb_emptyR
  s&    S!!!!!rA   c                <    |                      | j        d           d S )NzCallback was never run.)rc   r  r   s    r?   r  zDeferredListEmptyTests.tearDownV
  s     (*CDDDDDrA   Nr   )r  r  r4   rN   )rH   rI   rJ   r   r  r  r  rK   rA   r?   r  r  I
  sj           & & & &
" " " "E E E E E ErA   r  c                  n    e Zd ZddZddZddZddZdd	Zdd
ZddZ	ddZ
ddZddZddZddZdS )OtherPrimitivesTestsrs   r8   r4   rN   c                &    | xj         dz  c_         d S rK  counterrf   rs   s     r?   _incrzOtherPrimitivesTests._incr[
  s    rA   c                    d| _         d S r   r  r   s    r?   r   zOtherPrimitivesTests.setUp^
  s    rA   c                  
 t                      }|                                                    | j                   |                     |j                   |                     | j        d           |                                                    | j                   |                     |j                   |                     | j        d           |                                 |                     |j                   |                     | j        d           |                                 | 	                    |j                   |                     | j        d           | 
                    t          |j                   t                      }t                      }t                      }d 
dd
fd}|                    |||          }|                     |j                   |                     
|           |                    |           |                                                    | j                   |                     |j                   |                     | j        d           |                    |           |                     
|           |                     |j                   |                     | j        d	           |                                                    |          }|                                 |                     
t&                     |                     t)          t&          
          j        t,          j                   |                                 | 	                    |j                   dd}dd}	t1          |                    |          t          t2                              t1          |                    |	          t          t2                              d S )Nr]   r   resultValuer8   returnValuer4   c                    | |S r<   rK   )r  r  rs   s     r?   helperz-OtherPrimitivesTests.testLock.<locals>.helper|
  s     FrA   )r  r  r  r  c                 *    t          j        d          S r  r   r&  rK   rA   r?   r  z1OtherPrimitivesTests.testLock.<locals>.returnsInt
  s    =###rA   r;  c                 
   K   dS rK  rK   rK   rA   r?   returnsCoroIntz5OtherPrimitivesTests.testLock.<locals>.returnsCoroInt
  r  rA   r<   )r  r8   r  r8   r4   r8   )r4   r  r  )r$   acquirer   r  rc   lockedra   r  releaser	  r  r  runr8   r!   rR   rC  r   r:  r0   r   r   r   r   r   r;  )rf   lockfirstUniquesecondUniquecontrolDeferredr  resultDeferredr)  r  r  rs   s             @r?   testLockzOtherPrimitivesTests.testLocka
  sg   ~~""4:...$$$q)))""4:...$$$q)))$$$q)))%%%q))))TX...hhxx,4JJ#'	 	 	 	 	 	 	
  " 
 
 	$$$---""6***""4:...$$$q)))  ......$$$q)))LLNN""6**	


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




rA   c                    t                      }|                                 |                                }|                                 |                     |t          j                   dS )z
        When canceling a L{Deferred} from a L{DeferredLock} that does not
        yet have the lock (i.e., the L{Deferred} has not fired), the cancel
        should cause a L{defer.CancelledError} failure.
        N)r$   r  r   ri   r   r   )rf   r  r)  s      r?   test_cancelLockBeforeAcquiredz2OtherPrimitivesTests.test_cancelLockBeforeAcquired
  sS     ~~LLNN	


##Au';<<<<<rA   c                $   d}t          |          }t                      d dfd}g }t                      }|                    ||          }|                    |j                   |                    | j                   |                     |g            |                     |                               d            | 	                    |
                                           |                     | j        d           d	| _        t          dd|z             D ]I}|                                                    | j                   |                     | j        |           Jg dfd}dfd}	|                                                    ||	          }
|
                                 |                     dg           |                                                    | j                   |                     | j        |           |                                 |                     | j        |dz              t          dd|z             D ]4}|                                 |                     | j        |dz              5d S )Nr  argr8   r4   r  c                    | S r<   rK   )r  r  	helperArgs    r?   r  z2OtherPrimitivesTests.testSemaphore.<locals>.helper
  s    I""rA   )r  r]   r   rB  rN   c                2                         d           d S r  r   rB  r%  s    r?   r-  z0OtherPrimitivesTests.testSemaphore.<locals>.fail
  s    NN5!!!!!rA   c                2                         d           d S r  r   r  s    r?   r&  z3OtherPrimitivesTests.testSemaphore.<locals>.succeed
  s    NN4     rA   T)r  r8   r4   r  )rB  r8   r4   rN   )r&   r!   r8   r  r   r`   r  ra   rR   r   rz  r  ry  r  r   r   r  )rf   Nsemr  rl  uniqueObjectr  ir-  r&  r)  r  r  r%  s              @@@r?   testSemaphorez"OtherPrimitivesTests.testSemaphore
  s   ""*2** 		# 	# 	# 	# 	# 	# 	#
 !#xx\::""7>222""4:..."%%%L111  &&&'++--(((q)))q!a% 	. 	.AKKMM%%dj111T\1----	" 	" 	" 	" 	" 	"	! 	! 	! 	! 	! 	! KKMM&&tW55	


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




rA   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.
        r]   N)r&   r  r   ri   r   r   )rf   r  r)  s      r?   "test_cancelSemaphoreBeforeAcquiredz7OtherPrimitivesTests.test_cancelSemaphoreBeforeAcquired  sW      ""KKMM	


##Au';<<<<<rA   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   r]   r   )size)backlog)r%   ry  getr   r`   r  r   QueueUnderflowputra   listQueueOverflow)rf   r  Mqueuegottenr  s         r?   	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(((%.	:::::rA   c                     d fd}t                      }|                                }|                    |           |                    d           |                                 dS )z
        When canceling a L{Deferred} from a L{DeferredQueue} that already has
        a result, the cancel should have no effect.
        r2   r0   r4   rN   c                2                         d           d S r  r  r  s    r?   r  zOOtherPrimitivesTests.test_cancelQueueAfterSynchronousGet.<locals>.failOnErrback=  r  rA   Nr  )r%   r   r_   r  r   )rf   r  r  r)  s   `   r?   #test_cancelQueueAfterSynchronousGetz8OtherPrimitivesTests.test_cancelQueueAfterSynchronousGet7  sk    	2 	2 	2 	2 	2 	2 &3__IIKK	]###		$	




rA   c                l    t                                                      }|                                                      |t          j                   d fd}|                    |           g }|                    |j                                        t          |          d           dS )	a  
        When canceling a L{Deferred} from a L{DeferredQueue} that does not
        have a result (i.e., the L{Deferred} has not fired), the cancel
        causes a L{defer.CancelledError} failure. If the queue has a result
        later on, it doesn't try to fire the deferred.
        ignorer8   r4   r  c                                         d                                                                j        d           S r<   )r  r   r   rq   )r  r  rf   s    r?   r   z9OtherPrimitivesTests.test_cancelQueueAfterGet.<locals>.cbR  s4     IIdOOO99;;**4=$???rA   r]   N)r  r8   r4   r  )
r%   r   r   ri   r   r   r   r`   ra   rb   )rf   r)  r   doner  s   `   @r?   test_cancelQueueAfterGetz-OtherPrimitivesTests.test_cancelQueueAfterGetF  s     &3__IIKK	


##Au';<<<	@ 	@ 	@ 	@ 	@ 	@ 	@ 	
b	dk"""TA&&&&&rA   Nr  r   )rH   rI   rJ   r  r   r  r  r  r  r  r  r  r  r  r  rK   rA   r?   r  r  Z
  s             @= @= @= @=D   
= 
= 
= 
=12 12 12 12f= = = =   
= 
= 
= 
=#; #; #; #;J   ' ' ' ' ' 'rA   r  c                  Z    e Zd ZdZddZddZddZddZdd	Zdd
Z	ddZ
ddZddZdS )DeferredFilesystemLockTestsz8
    Test the behavior of L{DeferredFilesystemLock}
    r4   rN   c                    t                      | _        t          |                                 | j                  | _        d S )N)	scheduler)r,   clockr"   mktempr  r   s    r?   r   z!DeferredFilesystemLockTests.setUpc  s/    WW
*4;;==DJOOO			rA   r  c                8    | j                             d          S )zI
        Test that the lock can be acquired when no lock is held
        r]   timeout)r  deferUntilLockedr   s    r?   test_waitUntilLockedWithNoLockz:DeferredFilesystemLockTests.test_waitUntilLockedWithNoLockg  s     y))!)444rA   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  r]   r  )rc   r  r  assertFailurer   TimeoutErrorr  pumpr>  s     r?   %test_waitUntilLockedWithTimeoutLockedzADeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutLockedm  sr    
 		(()))I&&s&331e0111
b!!!rA   c                R    d	 fd}                       j                                                    j                            d j        j                    j                            d          }|                    |            j                            dgdz             |S )
z|
        Test that a lock can be acquired while a lock is held
        but the lock is unlocked before our timeout.
        r2   r0   r4   rN   c                p    |                      t          j                                       d           d S )NzShould not have timed out)rW  r   r  r-  r  s    r?   	onTimeoutzVDeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutUnlocked.<locals>.onTimeout  s1    FF5%&&&II122222rA   r]   r  r  r  )rc   r  r  	callLaterunlockr  r_   r  )rf   r#  r)  s   `  r?   'test_waitUntilLockedWithTimeoutUnlockedzCDeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutUnlocked{  s    	3 	3 	3 	3 	3 	3 		(()))
Q	 0111I&&r&22	Y
b!!!rA   c                    t          |                                           }|                     |j        t                     dS )zE
        Test that the default scheduler is set up properly.
        N)r"   r  ra   
_schedulerr   rf   r  s     r?   test_defaultSchedulerz1DeferredFilesystemLockTests.test_defaultScheduler  s7     &dkkmm44'22222rA   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.
        r]   )	r  r  r$  r%  r  r  r   AlreadyTryingToLockErroradvancerF  s      r?   test_concurrentUsagez0DeferredFilesystemLockTests.test_concurrentUsage  s    
 		
Q	 0111Y''))Y''))2u=>>>
1	rA   c                     d fd} j                                            j                            d j         j                    j                                         }|                    |            j                            d           |S )zO
        Test that a DeferredFilesystemLock can be used multiple times
        r  r8   r4   r  c                l    j                                          j                                         }|S r<   )r  r%  r  )r  r)  rf   s     r?   lockAquiredzDDeferredFilesystemLockTests.test_multipleUsages.<locals>.lockAquired  s/    I	**,,AHrA   r]   )r  r8   r4   r  )r  r  r$  r%  r  r   r-  )rf   r1  r)  s   `  r?   test_multipleUsagesz/DeferredFilesystemLockTests.test_multipleUsages  s    
	 	 	 	 	 	
 		
Q	 0111I&&((	k"""
1rA   c                ~   | j                                           | j                                         }| j         j        }|J |                                 |                     |                                           |                     | j         j                   |                     |t          j	                   dS )z
        When cancelling a L{Deferred} returned by
        L{DeferredFilesystemLock.deferUntilLocked}, the
        L{DeferredFilesystemLock._tryLockCall} is cancelled.
        N)
r  r  _tryLockCallr   r	  activer   r  r   r   )rf   rL   tryLockCalls      r?   test_cancelDeferUntilLockedz7DeferredFilesystemLockTests.test_cancelDeferUntilLocked  s     		9--//i,&&&++--...$)0111Xu';<<<<<rA   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.
        r]   r  N)
r  r  _timeoutCallr   r	  r5  r   r  r   r   )rf   rL   timeoutCalls      r?   &test_cancelDeferUntilLockedWithTimeoutzBDeferredFilesystemLockTests.test_cancelDeferUntilLockedWithTimeout  s     		9--a-88i,&&&++--...$)0111Xu';<<<<<rA   Nr   )r4   r  )rH   rI   rJ   rj   r   r  r   r&  r*  r.  r2  r7  r;  rK   rA   r?   r  r  ^  s         P P P P5 5 5 5      (3 3 3 3   "   (= = = == = = = = =rA   r  vr8   tfloatr   c                    dS )zj
    Private function to be used to pass as an alternate onTimeoutCancel value
    to timeoutDeferred
    
OVERRIDDENrK   )r<  r=  s     r?   _overrideFuncrA    s	    
 <rA   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}
    r4   rN   c                   t                      }|                    dt                                 |                    d            |                    d           |                     d|                     |                     dS )zu
        L{Deferred.addTimeout} returns its own L{Deferred} so it
        can be called in a callback chain.
        r  c                    dS )Nr  rK   r6  s    r?   r   z?DeferredAddTimeoutTests.test_timeoutChainable.<locals>.<lambda>  s     rA   Nr  )r!   
addTimeoutr,   r   rR   ra   r  r>  s     r?   test_timeoutChainablez-DeferredAddTimeoutTests.test_timeoutChainable  sw    
 %JJ	Q   	&&'''	

4!5!5a!8!899999rA   c                   t                      }t                      }|                    d|           t                      dd	fd}|                    |           |                    d           |                     d           |                     d           |                    d           |                                dS )
z
        The L{Deferred} callbacks with the result if it succeeds before
        the timeout. No cancellation happens after the callback either,
        which could also cancel inner deferreds.
        r  Nrl  r   r4   r  c                    | S r<   rK   rl  dCallbackedinnerDeferreds    r?   
onCallbackzKDeferredAddTimeoutTests.test_successResultBeforeTimeout.<locals>.onCallback      !K  rA   r  rl  r   r4   r  )	r,   r!   rF  r   rR   assertIsNotra   r-  r  rf   r  r)  rM  rK  rL  s       @@r?   test_successResultBeforeTimeoutz7DeferredAddTimeoutTests.test_successResultBeforeTimeout  s     #::	R )1

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

9 	{+++i000 	bM*****rA   c                   t                      }t                      }|                    d|t                     t                      dd
fd}|                    |           |                    d           |                     d           |                     d           |                    d	           | 	                               dS )a  
        The L{Deferred} callbacks with the result if it succeeds before
        the timeout, even if a custom C{onTimeoutCancel} function is provided.
        No cancellation happens after the callback either, which could also
        cancel inner deferreds.
        r  onTimeoutCancelNrl  r   r4   r  c                    | S r<   rK   rJ  s    r?   rM  zQDeferredAddTimeoutTests.test_successResultBeforeTimeoutCustom.<locals>.onCallback$  rN  rA   r  rO  )
r,   r!   rF  rA  r   rR   rP  ra   r-  r  rQ  s       @@r?   %test_successResultBeforeTimeoutCustomz=DeferredAddTimeoutTests.test_successResultBeforeTimeoutCustom  s     #::	R>>> )1

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

9 	{+++i000 	bM*****rA   c                   t                      }t                      }|                    d|           t                      dt          d          }d
fd}|                    |           |                    |           J |                     t                     |                     j	        |           |
                    d	           |                                dS )z
        The L{Deferred} errbacks with the failure if it fails before the
        timeout. No cancellation happens after the errback either, which
        could also cancel inner deferreds.
        r  Nr-  r2   r0   r4   r  c                    | S r<   rK   r2   
dErrbackedrL  s    r?   	onErrbackzDDeferredAddTimeoutTests.test_failureBeforeTimeout.<locals>.onErrbackF      J  rA   r  r2   r0   r4   r  )r,   r!   rF  rY  r_   r   r:  r0   rq   re   r-  r  rf   r  r)  rr   r\  r[  rL  s        @@r?   test_failureBeforeTimeoutz1DeferredAddTimeoutTests.test_failureBeforeTimeout6  s     $JJ	R )1

(,
6""	! 	! 	! 	! 	! 	! 	!
 	
Y			% %%%j'222j&... 	bM*****rA   c                   t                      }t                      }|                    d|t                     t                      dt	          d          }dfd	}|                    |           |                    |           J |                     t                     | 	                    j
        |           |                    d
           |                                dS )a  
        The L{Deferred} errbacks with the failure if it fails before the
        timeout, even if using a custom C{onTimeoutCancel} function.
        No cancellation happens after the errback either, which could also
        cancel inner deferreds.
        r  rT  Nr-  r2   r0   r4   r  c                    | S r<   rK   rZ  s    r?   r\  zJDeferredAddTimeoutTests.test_failureBeforeTimeoutCustom.<locals>.onErrbackj  r]  rA   r  r^  )r,   r!   rF  rA  rY  r_   r   r:  r0   rq   re   r-  r  r_  s        @@r?   test_failureBeforeTimeoutCustomz7DeferredAddTimeoutTests.test_failureBeforeTimeoutCustomY  s     $JJ	R>>> )1

(,
6""	! 	! 	! 	! 	! 	! 	!
 	
Y			% %%%j'222j&... 	bM*****rA   c                    t                      }t                      }|                    d|           |                     |           |                    d           |                     |t          j                   dS )z
        The L{Deferred} by default errbacks with a L{defer.TimeoutError}
        if it times out before callbacking or errbacking.
        r  r  N)r,   r!   rF  r  r-  r  r   r  rf   r  r)  s      r?   test_timedOutz%DeferredAddTimeoutTests.test_timedOut}  so    
 $JJ	RAbQ 233333rA   c                   t                      }t                      }|                    d|t                     |                     |           |                    d           |                     d|                     |                     dS )a1  
        If a custom C{onTimeoutCancel] function is provided, the
        L{Deferred} returns the custom function's return value if the
        L{Deferred} times out before callbacking or errbacking.
        The custom C{onTimeoutCancel} function can return a result instead of
        a failure.
        r  rT  r  r@  Nr,   r!   rF  rA  r  r-  ra   r  re  s      r?   test_timedOutCustomz+DeferredAddTimeoutTests.test_timedOutCustom  s~     $JJ	R>>>Abt';';A'>'>?????rA   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!rQ   r  s    r?   r   zLDeferredAddTimeoutTests.test_timedOutProvidedCancelSuccess.<locals>.<lambda>  s    ajj9K.L.L rA   r  r  rl  N)r,   r!   rF  r  r-  ra   r  re  s      r?   "test_timedOutProvidedCancelSuccessz:DeferredAddTimeoutTests.test_timedOutProvidedCancelSuccess  s     #$L$LMM	RAb--a002DEEEEErA   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  rr   s    r?   r   zLDeferredAddTimeoutTests.test_timedOutProvidedCancelFailure.<locals>.<lambda>  s    qyy/?/? rA   r  r  N)	r,   rY  r!   rF  r  r-  r  rq   re   )rf   r  r)  r2   rr   s       @r?   "test_timedOutProvidedCancelFailurez:DeferredAddTimeoutTests.test_timedOutProvidedCancelFailure  s     7##$%?%?%?%?@@	RAb  J//agu%%%%%rA   c                   t                      }t                      }|                    d|           t                      dd	fd}|                    |           |                                 J |                     t                     |                     j        t          j
                   |                    d           |                                dS )
a-  
        If the L{Deferred} is manually cancelled before the timeout, it
        is not re-cancelled (no L{AlreadyCancelled} error, and also no
        canceling of inner deferreds), and the default C{onTimeoutCancel}
        function is not called, preserving the original L{CancelledError}.
        r  Nr2   r0   r4   r  c                    | S r<   rK   r2   	dCanceledrL  s    r?   r\  zCDeferredAddTimeoutTests.test_cancelBeforeTimeout.<locals>.onErrback      I  rA   r  r^  )r,   r!   rF  r_   r   r:  r0   rq   r   r   r   r-  r  rf   r  r)  r\  ru  rL  s       @@r?   test_cancelBeforeTimeoutz0DeferredAddTimeoutTests.test_cancelBeforeTimeout  s     $JJ	R )1

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


 $$$i111ine&:;;; 	bM*****rA   c                   t                      }t                      }|                    d|t                     t                      dd
fd}|                    |           |                                 J |                     t                     |                     j	        t          j                   |                    d	           |                                dS )a,  
        If the L{Deferred} is manually cancelled before the timeout, it
        is not re-cancelled (no L{AlreadyCancelled} error, and also no
        canceling of inner deferreds), and the custom C{onTimeoutCancel}
        function is not called, preserving the original L{CancelledError}.
        r  rT  Nr2   r0   r4   r  c                    | S r<   rK   rt  s    r?   r\  zIDeferredAddTimeoutTests.test_cancelBeforeTimeoutCustom.<locals>.onErrback  rv  rA   r  r^  )r,   r!   rF  rA  r_   r   r:  r0   rq   r   r   r   r-  r  rw  s       @@r?   test_cancelBeforeTimeoutCustomz6DeferredAddTimeoutTests.test_cancelBeforeTimeoutCustom  s     $JJ	R>>> )1

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


 $$$i111ine&:;;; 	bM*****rA   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 )Nro  )r   rY  r  s    r?   r   zVDeferredAddTimeoutTests.test_providedCancelCalledBeforeTimeoutCustom.<locals>.<lambda>  s    qyyG9L9L/M/M rA   r  rT  r  r@  Nrh  re  s      r?   ,test_providedCancelCalledBeforeTimeoutCustomzDDeferredAddTimeoutTests.test_providedCancelCalledBeforeTimeoutCustom  s    
 $%M%MNN	R>>>Abt';';A'>'>?????rA   c                   t                      }t                      }ddfd}|                    |           |                    d|           |                    d           J |                     t                     |                     j        t          j	                   | 
                    |t          j                   dS )	a  
        An errback added before a timeout is added errbacks with a
        L{defer.CancelledError} when the timeout fires.  If the
        errback returns the L{defer.CancelledError}, it is translated
        to a L{defer.TimeoutError} by the timeout implementation.
        Nr2   r0   r4   c                    | | S r<   rK   r2   r[  s    r?   r   zGDeferredAddTimeoutTests.test_errbackAddedBeforeTimeout.<locals>.errback      JHrA   r  r  r2   r0   r4   r0   )r,   r!   r_   rF  r-  r:  r0   re   r   r   r  r  rf   r  r)  r   r[  s       @r?   test_errbackAddedBeforeTimeoutz6DeferredAddTimeoutTests.test_errbackAddedBeforeTimeout  s     $JJ
	 	 	 	 	 	
 	
W	Rb%%%j'222j.0DEEEQ 233333rA   c                   t                      }t                      }dd	fd}|                    |           |                    d|           |                    d           J |                     t                     |                     j        t          j	                   | 
                    |           dS )
z
        An errback added before a timeout is added errbacks with a
        L{defer.CancelledError} when the timeout fires.  If the
        errback suppresses the L{defer.CancelledError}, the deferred
        successfully completes.
        Nr2   r0   r4   rN   c                J    | |                      t          j                   d S r<   )rW  r   r   r  s    r?   r   z]DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellation.<locals>.errback8  s#    JFF5'(((((rA   r  r  r  )r,   r!   r_   rF  r-  r:  r0   re   r   r   r  r  s       @r?   4test_errbackAddedBeforeTimeoutSuppressesCancellationzLDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellation,  s     $JJ
	) 	) 	) 	) 	) 	)
 	
W	Rb%%%j'222j.0DEEEQrA   c                   t                      }t                      }dd	fd}|                    |           |                    d|t                     |                    d           J |                     t                     |                     j        t          j
                   |                     d|                     |                     dS )
a  
        An errback added before a timeout is added with a custom
        timeout function errbacks with a L{defer.CancelledError} when
        the timeout fires.  The timeout function runs if the errback
        returns the L{defer.CancelledError}.
        Nr2   r0   r4   c                    | | S r<   rK   r  s    r?   r   zMDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutCustom.<locals>.errbackT  r  rA   r  r  r@  r  r,   r!   r_   rF  rA  r-  r:  r0   re   r   r   ra   r  r  s       @r?   $test_errbackAddedBeforeTimeoutCustomz<DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutCustomH  s     $JJ
	 	 	 	 	 	
 	
W	R...b%%%j'222j.0DEEEt';';A'>'>?????rA   c                   t                      }t                      }dd
fd}|                    |           |                    d|t                     |                    d           J |                     t                     |                     j        t          j
                   |                     d	|                     |                     dS )a  
        An errback added before a timeout is added with a custom
        timeout function errbacks with a L{defer.CancelledError} when
        the timeout fires.  The timeout function runs if the errback
        suppresses the L{defer.CancelledError}.
        Nr2   r0   r4   rN   c                    | d S r<   rK   r  s    r?   r   zcDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationCustom.<locals>.errbackp  s    JJJrA   r  r  r@  r  r  r  s       @r?   :test_errbackAddedBeforeTimeoutSuppressesCancellationCustomzRDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationCustomd  s     $JJ
	 	 	 	 	 	 	
W	R...b%%%j'222j.0DEEEt';';A'>'>?????rA   c                \   t                      }dt          fd          }dd
fd}|                    |           |                    d|           |                    d	           |                                |                     |                     |                     dS )a*  
        Given a deferred with a cancellation function that resumes the
        callback chain, a callback that is added to the deferred
        before a timeout is added to runs when the timeout fires.  The
        deferred completes successfully, without a
        L{defer.TimeoutError}.
        r%  c                .    |                                S r<   rQ   r)  r%  s    r?   r   zSDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout.<locals>.<lambda>      ajj.A.A rA   Nre   r   r4   c                    | | S r<   rK   re   rK  s    r?   rR   zSDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout.<locals>.callback      KLrA   r  r  re   r   r4   r   )r,   r!   r   rF  r-  ra   rq   r  rf   r  r)  rR   rK  r%  s       @@r?   )test_callbackAddedToCancelerBeforeTimeoutzADeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout  s     #$A$A$A$ABB	 	 	 	 	 	
 	
h	Rbg...gt33A6677777rA   c                j   t                      }dt          fd          }ddfd}|                    |           |                    d|t          	           |                    d
           |                                |                     d|                     |                     dS )ad  
        Given a deferred with a cancellation function that resumes the
        callback chain, a callback that is added to the deferred
        before a timeout is added to runs when the timeout fires.  The
        deferred completes successfully, without a
        L{defer.TimeoutError}.  The timeout's custom timeout function
        also runs.
        r%  c                .    |                                S r<   rQ   r  s    r?   r   zYDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom.<locals>.<lambda>  r  rA   Nre   r   r4   c                    | | S r<   rK   r  s    r?   rR   zYDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom.<locals>.callback  r  rA   r  rT  r  r@  r  )r,   r!   r   rF  rA  r-  ra   r  r  s       @@r?   /test_callbackAddedToCancelerBeforeTimeoutCustomzGDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom  s     #$A$A$A$ABB	 	 	 	 	 	
 	
h	R>>>bg...t';';A'>'>?????rA   Nr   )rH   rI   rJ   rj   rG  rR  rW  r`  rc  rf  ri  rm  rq  rx  r{  r~  r  r  r  r  r  r  rK   rA   r?   rC  rC    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@ @ @ @ @ @rA   rC  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}.
    r4   rN   c                l    t                      }t          |          }|                     ||           dS )zK
        L{ensureDeferred} will pass through a Deferred unchanged.
        N)r!   r*   rq   rF  s      r?   test_passesThroughDeferredsz/EnsureDeferredTests.test_passesThroughDeferreds  s4     &ZZBb"rA   c                    |                      t          j                  5  t          d           ddd           dS # 1 swxY w Y   dS )zr
        Passing L{ensureDeferred} a non-coroutine and a non-Deferred will
        raise a L{ValueError}.
        	somethingN)r  r   r  r*   r   s    r?   'test_willNotAllowNonDeferredOrCoroutinez;EnsureDeferredTests.test_willNotAllowNonDeferredOrCoroutine  s    
 u788 	( 	(;'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(s   =AAc                L   dd} |            }|                      |t          j                   t          |          }t	          |t
          t                              |                      |t
                     |                     |          }|                     |d           dS )zM
        L{ensureDeferred} will turn a coroutine into a L{Deferred}.
        r4   r   c                 B   K   t          j        d          } |  d {V }|S NrF  r  r)  r  s     r?   r  z=EnsureDeferredTests.test_ensureDeferredCoroutine.<locals>.run  s+      e$$A''''''CJrA   rF  N)r4   r   )	r:  typesCoroutineTyper*   r   r!   r   r  ra   rf   r  rB  r)  r  s        r?   test_ensureDeferredCoroutinez0EnsureDeferredTests.test_ensureDeferredCoroutine      
	 	 	 	 CEEa!4555 1Ax}%%%a*** ""1%%e$$$$$rA   c                L   dd} |            }|                      |t          j                   t          |          }t	          |t
          t                              |                      |t
                     |                     |          }|                     |d           dS )zX
        L{ensureDeferred} will turn a yield-from coroutine into a L{Deferred}.
        r4   #Generator[Deferred[str], None, str]c               3  h   K   t          j        d          } t          t          | E d {V           }|S r  )r   r&  r   r   r  s     r?   r  z=EnsureDeferredTests.test_ensureDeferredGenerator.<locals>.run  s5      e$$As\\\\\\++CJrA   rF  N)r4   r  )	r:  r  GeneratorTyper*   r   r!   r   r  ra   r  s        r?   test_ensureDeferredGeneratorz0EnsureDeferredTests.test_ensureDeferredGenerator  r  rA   Nr   )rH   rI   rJ   rj   r  r  r  r  rK   rA   r?   r  r    sn            ( ( ( (% % % %.% % % % % %rA   r  c                      e Zd ZdZddZdS )TimeoutErrorTestsz1
    L{twisted.internet.defer} timeout code.
    r4   rN   c                   t                      }t          j        |           |                     |t          j                   |                     | j        g          }|                     t          |          d           | 	                    |d         d         t                     |                     |d         d         d           dS )zB
        L{twisted.internet.defer.timeout} is deprecated.
        r]   r   rG  rH  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  rI  test_deprecatedTimeoutra   rb   rq   rJ  )rf   rL   warningsShowns      r?   r  z(TimeoutErrorTests.test_deprecatedTimeout  s     &.ZZh8U%7888**D,G+HII]++Q///mA&z24FGGG!Y'M	
 	
 	
 	
 	
rA   Nr   )rH   rI   rJ   rj   r  rK   rA   r?   r  r    s2         
 
 
 
 
 
rA   r  loopr   c                b    |                      | j                   |                                  dS )z
    Tickle an asyncio event loop to call all of the things scheduled with
    call_soon, inasmuch as this can be done via the public API.

    @param loop: The asyncio event loop to flush the previously-called
        C{call_soon} entries from.
    N)	call_soonstoprun_foreverr  s    r?   callAllSoonCallsr    s0     	NN49rA   c                  N    e Zd ZddZddZddZddZddZdd	Zdd
Z	ddZ
dS )DeferredFutureAdapterTestsr4   r   c                V    t                      }|                     |j                   |S )zU
        Create a new event loop that will be closed at the end of the test.
        )_new_event_loopr   closer  s     r?   newLoopz"DeferredFutureAdapterTests.newLoop   s(     !""%%%rA   rN   c                   t                      }|                                 }|                    |          }|                     |                                d           |                    d           t          |           |                     |                     |          d           |                     |                                d           dS )zw
        L{Deferred.asFuture} returns a L{asyncio.Future} which fires when
        the given L{Deferred} does.
        Fr  N)	r!   r  asFuturera   r  rR   r  r  rs   )rf   r)  r  aFutures       r?   test_asFuturez(DeferredFutureAdapterTests.test_asFuture(  s    
 $::||~~**T""///	

2--a00$777))2.....rA   c                   ddfd}t          |          }|                                 }|                    |          }|                                 t	          |           |                                |                     |                     |          d           |                     t          |j
                   dS )	z
        L{Deferred.asFuture} returns a L{asyncio.Future} which, when
        cancelled, will cancel the original L{Deferred}.
        Fdprimer  r4   rN   c                    dd S r  rK   )r  r  s    r?   cancelerzFDeferredFutureAdapterTests.test_asFutureCancelFuture.<locals>.canceler>  s    FFFrA   Nr  r  r4   rN   )r!   r  r  r   r  rc   ra   r  r  r   rs   )rf   r  r)  r  r  r  s        @r?   test_asFutureCancelFuturez4DeferredFutureAdapterTests.test_asFutureCancelFuture6  s     	 	 	 	 	 	 %X..||~~**T""--a00$777.'.99999rA   c                V   dd}t          |          }|                                 }|                    |          }|                                 t	          |           |                     |                     |          d           |                     t          |j	                   dS )z
        While Futures don't support succeeding in response to cancellation,
        Deferreds do; if a Deferred is coerced into a success by a Future
        cancellation, that should just be ignored.
        r  r  r4   rN   c                0    |                      d           d S )Nr  rQ   )r  s    r?   r  zGDeferredFutureAdapterTests.test_asFutureSuccessCancel.<locals>.cancelerR  s    OOArA   Nr  )
r!   r  r  r   r  ra   r  r  r   rs   )rf   r  r)  r  r  s        r?   test_asFutureSuccessCancelz5DeferredFutureAdapterTests.test_asFutureSuccessCancelK  s    	 	 	 	 %X..||~~**T""--a00$777.'.99999rA   c                P   t                      }t          t                                }|                                 }|                    |          }t          |           |                    |           t          |           |                     t          |j                   dS )z
        L{Deferred.asFuture} makes a L{asyncio.Future} fire with an
        exception when the given L{Deferred} does.
        N)	r!   r0   r9  r  r  r  r   r  rs   )rf   r)  
theFailurer  futures        r?   test_asFutureFailurez/DeferredFutureAdapterTests.test_asFutureFailure]  s    
 %JJ.0011
||~~D!!			*+V];;;;;rA   c                :   |                                  }t          |          }t          j        |          }|                     |           |                    d           t          |           |                     |                     |          d           dS )zx
        L{Deferred.fromFuture} returns a L{Deferred} that fires
        when the given L{asyncio.Future} does.
        r  rP  N)	r  r   r!   
fromFuturer  
set_resultr  ra   r  )rf   r  r  r)  s       r?   test_fromFuturez*DeferredFutureAdapterTests.test_fromFuturek  s    
 ||~~%4000((A1--a00!44444rA   c                V   |                                  }t          |          }t          j        |          }|                                 t          |           |                     t          |j                   | 	                    |          
                    t                     dS )z
        L{Deferred.fromFuture} makes a L{Deferred} fire with
        an L{asyncio.CancelledError} when the given
        L{asyncio.Future} is cancelled.
        r  N)r  r   r!   r  r   r  r  r   rs   r  rW  rf   r  	cancelledr)  s       r?   test_fromFutureFutureCancelledz9DeferredFutureAdapterTests.test_fromFutureFutureCancelledx  s     ||~~"(d"3"3"3		**.)*:;;;Q$$^44444rA   c                   |                                  }t          |          }t          j        |          }|                                 t          |           |                     |                                d           |                     t          |j
                   |                     |                              t                     dS )z
        L{Deferred.fromFuture} makes a L{Deferred} which, when
        cancelled, cancels the L{asyncio.Future} it was created from.
        r  TN)r  r   r!   r  r   r  ra   r  r  r   rs   r  rW  r  s       r?    test_fromFutureDeferredCancelledz;DeferredFutureAdapterTests.test_fromFutureDeferredCancelled  s    
 ||~~"(d"3"3"3		**	


,,..555.)*:;;;Q$$^44444rA   N)r4   r   r   )rH   rI   rJ   r  r  r  r  r  r  r  r  rK   rA   r?   r  r    s           / / / /: : : :*: : : :$< < < <5 5 5 55 5 5 55 5 5 5 5 5rA   r  c                  n    e Zd Zd
dZd
dZed
d            Zed
d            Zed
d            Zd
dZ	d	S )CoroutineContextVarsTestsr4   rN   c                t    t                      t          j        d                              d           t	                                          fd           t	                                          fd                               d            t          j        dfd            t          t          g t          d	         f                    t          j        d fd            }t          |t          g t          t          d                  f                                        d            |            }                    d                               d                               d                                                     |          d           d	S )z
        When an inlineCallbacks function is called, the context is taken from
        when it was first called. When it resumes, the same context is applied.
        testvarr]   c                .                         d          S r  setr7  vars    r?   r   zDCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.<lambda>      swwqzz rA   c                .                         d          S N   r  r  s    r?   r   zDCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.<lambda>      

 rA   c                    ddz  S r5  rK   r6  s    r?   r   zDCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.<lambda>  
    A rA   r4   #Generator[Deferred[Any], Any, None]c               3     K   t                      }                     d| j        d           | V                      d           d S Nr]   Tr  r!   r$  rR   r  r)  r  r  s    r?   yieldingDeferredzLCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.yieldingDeferred  sD      'zzAOOAqz4000GGGGGAJJJJJrA   N,Generator[Deferred[Any], Any, Literal[True]]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           dS )Nr   r]   T???? should have failedra   r   r   r&  r$  rR   r  )r  mutatingDeferredmutatingDeferredThatFailsrf   r  r  s   r?   testFunctionzHCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.testFunction  s`      SWWYY*** -""""" SWWYY*** OOA/8$???"""" SWWYY*** OOA8A4HHH;////   9:::  / / /  A...../ #"$$$$$SWWYY***4s   C" "2DDTr   r4   r  )r4   r  )r,   contextvars
ContextVarr  r!   r   r   inlineCallbacksr   r	   r   r-  ra   r  )rf   r  r)  r  r  r  r  r  s   `  @@@@@r?   test_withInlineCallbacksz2CoroutineContextVarsTests.test_withInlineCallbacks  s   
 +6+A)+L+L


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


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


 	aQrA   c                  K   t                      d}4 d{V  |                     j                                                   }|                    fd           |                     j                   |                     |j                   ddd          d{V  n# 1 d{V swxY w Y   |                     |j                   | d{V  |                     j                   dS )zQ
        L{DeferredLock} can be used as an asynchronous context manager.
        Nc                ,                                     S r<   r  )r7  r  s    r?   r   z>CoroutineContextVarsTests.test_asyncWithLock.<locals>.<lambda>  s    DLLNN rA   )r$   rc   r  r  r   r	  r  )rf   r)  r  s     @r?   test_asyncWithLockz,CoroutineContextVarsTests.test_asyncWithLock  sv     
 ~~ 	' 	' 	' 	' 	' 	' 	' 	'OODK(((AMM2222333OODK(((QX&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	!!!%%%%%s   A;B))
B36B3c                  K   t          d          4 d{V  |                     j        d           4 d{V  |                     j        d                                           }                                }|                     j        d           |                     |j                   |                     |j                   ddd          d{V  n# 1 d{V swxY w Y   |                     j        d           |                     |j                   |                    fd           |                    fd           | d{V  | d{V  |                     j        d           ddd          d{V  n# 1 d{V swxY w Y   |                     j        d           dS )z^
        L{DeferredSemaphore} can be used as an asynchronous context
        manager.
        r  Nr   r]   r   c                ,                                     S r<   r	  r7  r  s    r?   r   zCCoroutineContextVarsTests.test_asyncWithSemaphore.<locals>.<lambda>+      S[[]] rA   c                ,                                     S r<   r	  r  s    r?   r   zCCoroutineContextVarsTests.test_asyncWithSemaphore.<locals>.<lambda>,  r  rA   )r&   ra   tokensr  rc   r  r	  r   )rf   r   r   r  s      @r?   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$   assertRaisesRegexr  rc   r  r	  r)  s     r?   test_asyncWithLockExceptionz5CoroutineContextVarsTests.test_asyncWithLockException2  s7      ~~##I/HII 	; 	; ; ; ; ; ; ; ; ;,,, 9:::; ; ; ; ; ; ; ; ; ; ; ; ; ; ;	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	%%%%%s.   	A=*A
A)	)A=,A)	-A==BBc                    t                      t          j        d                              d           t	                                          fd           t	                                          fd                               d            dfdd fd
}                    d           t           |                      }                    d                               d                               d                                 	                    |          d           dS )z
        When a coroutine is called, the context is taken from when it was first
        called. When it resumes, the same context is applied.
        r  r]   c                .                         d          S r  r  r  s    r?   r   zJCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.<lambda>K  r  rA   c                .                         d          S r  r  r  s    r?   r   zJCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.<lambda>N  r  rA   c                    ddz  S r5  rK   r6  s    r?   r   zJCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.<lambda>O  r  rA   r4   rN   c                    K   t                      }                     d| j        d           |  d {V                      d           d S r  r  r  s    r?   asyncFuncAwaitingDeferredz[CoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.asyncFuncAwaitingDeferredQ  sL       (

AOOAqz4000GGGGGGGGGAJJJJJrA   r   c                   K                                                        d           t          j        d           d {V                                                       d                               dj        d            d {V                                                       d                               dj        d           	  d {V  t          d          # t          $ r+                                                      d           Y nw xY w               d {V                                                       d           dS )Nr   r]   r   Tr  r  r  )r  r  r  r  rf   r  s   r?   r  zNCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.testFunctionX  s     SWWYY*** -""""""""" SWWYY*** OOA/8$???"""""""" SWWYY*** OOA8A4HHH;////////   9:::  / / /  A...../ ,+---------SWWYY***4s   C. .2D#"D#r   TNr   )r4   r   )
r,   r  r  r  r!   r   r*   r-  ra   r  )rf   r  r)  r  r  r  r  r  s   `  @@@@@r?   test_contextvarsWithAsyncAwaitz8CoroutineContextVarsTests.test_contextvarsWithAsyncAwait?  s   
 +6+A)+L+L


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


<<>>** 	a 	a 	a--a00$77777rA   Nr   )
rH   rI   rJ   r  r  rD   r
  r  r  r  rK   rA   r?   r  r    s        O8 O8 O8 O8b       @ & & & &  ( ( ( (2 
& 
& 
& 
&I8 I8 I8 I8 I8 I8rA   r  c                  L    e Zd ZddZ ej        ed          dd            ZdS )InlineCallbackTestsr4   rN   c                    d dd| j         dt          g}|D ]-}|                     t          j        t          j        |           .dS )r  c                    | S r<   rK   r  s    r?   r   zIInlineCallbackTests.test_fromCoroutineRequiresCoroutine.<locals>.<lambda>  r  rA   r]   TNr  r  s      r?   r  z7InlineCallbackTests.test_fromCoroutineRequiresCoroutine  r  rA   r  c                ~   t                      }t          j        |          }dd}t          j         ||                    }|                     ||                     |                     t          j        |          }~~|                      |                       |                      |                       dS )z
        Tests that there is no circular dependency when using
        L{Deferred.fromCoroutine}, so that the machinery gets cleaned up
        immediately rather than waiting for a GC.
        r  r   r4   c                
   K   | S r<   rK   )r  s    r?   r   zCInlineCallbackTests.test_coroutineNoCircularReference.<locals>.func  s      HrA   N)r  r   r4   r   )r  r  r  r!   r=   ra   r  r   )rf   obj
objWeakRefr   funcDfuncDWeakRefs         r?   !test_coroutineNoCircularReferencez5InlineCallbackTests.test_coroutineNoCircularReference  s     [%%
	 	 	 	 &ttCyy11d22599:::{5))  	**,,''',,..)))))rA   Nr   )rH   rI   rJ   r  r  r  r.   r'  rK   rA   r?   r  r    s\        W W W W. V]59::* * * ;:* * *rA   r  )r2   r3   r4   r5   )rL   rM   r4   rN   )r  r;  r4   r  )r<  r8   r=  r>  r4   r   )r  r   r4   rN   )crj   
__future__r   r  rB   r  rK  ri  r  r1   r  rU  r  asyncior   r   r   r   r  typingr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   hamcrestr   r   r   
hypothesisr   hypothesis.strategiesr   typing_extensionsr   twisted.internetr   r   twisted.internet.deferr    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   twisted.internet.taskr,   twisted.pythonr-   twisted.python.compatr.   twisted.python.failurer0   twisted.trialrD   r  rF   rT   rU   rX   r^   rl   SynchronousTestCaser   r  r  r  r  r  rF  r  r  r  r  rA  rC  r  r  r  r  r  r  rK   rA   r?   <module>r7     s"    # " " " " "         				 				                                                         & 2 1 1 1 1 1 1 1 1 1       * * * * * * ) ) ) ) ) ) + + + + + + + +                            ( ' ' ' ' '       ' ' ' ' ' ' * * * * * * " " " " " "   	 	 	 	 	9 	 	 	) ) ) ) gm95554 4 4 4 4 4 4 4,*M *M *M *M *M! *M *M *MZ@9 @9 @9 @9 @9H02G @9 @9 @9F4          &~7 ~7 ~7 ~7 ~7, ~7 ~7 ~7B;$ ;$ ;$ ;$ ;$h2 ;$ ;$ ;$|L$ L$ L$ L$ L$5 L$ L$ L$^GI GI GI GI GIX9 GI GI GITc3 c3 c3 c3 c3x+ c3 c3 c3LE E E E EX9 E E E"A' A' A' A' A'879N A' A' A'Hz= z= z= z= z=("3 z= z= z=z   S@ S@ S@ S@ S@h: S@ S@ S@lA% A% A% A% A%(+ A% A% A%H
 
 
 
 
)+@ 
 
 
,	 	 	 	s5 s5 s5 s5 s5!2 s5 s5 s5ls8 s8 s8 s8 s8 1 s8 s8 s8l3* 3* 3* 3* 3*(6 3* 3* 3* 3* 3*rA   