Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib64/python3.7/site-packages/twisted/test/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib64/python3.7/site-packages/twisted/test/__pycache__/test_defer.cpython-37.pyc

B

;��]ϖ�@s.dZddlmZmZddlZddlZddlZddlZddlm	Z	m
Z
mZddlm
Z
mZddlmZddlmZmZddlmZe
r�dd	lmZmZmZdZnd
Zer�ddlmZee����d�Z d
e!diZ"e	�#e j$e"�e"dZ%nGdd�dej&�Z%Gdd�de'�Z(dd�Z)dd�Z*Gdd�de+�Z,Gdd�dej&�Z-Gdd�dej.e,�Z/Gdd�dej.�Z0Gdd �d ej.�Z1Gd!d"�d"ej.�Z2Gd#d$�d$ej.�Z3Gd%d&�d&ej.�Z4Gd'd(�d(ej.e,�Z5Gd)d*�d*ej&�Z6d+d,�Z7Gd-d.�d.ej.�Z8Gd/d0�d0ej&�Z9Gd1d2�d2ej&e,�Z:d3d4�Z;Gd5d6�d6ej&�Z<dS)7z+
Test cases for L{twisted.internet.defer}.
�)�division�absolute_importN)�compat�failure�log)�_PY3�	_PY35PLUS)�unittest)�defer�reactor)�Clock)�new_event_loop�Future�CancelledErrorz'asyncio not available before python 3.4)�FilePathztest_defer.py.3only�__name__z.3only�DeferredTestsAsyncc@seZdZdZdZdd�ZdS)rz�
        A dummy class to show that this test file was discovered but the tests
        are unable to be ran in this version of Python.
        z.async/await is not available before Python 3.5cCsdS)zq
            A skipped test to show that this was not ran because the Python is
            too old.
            N�)�selfrr�J/opt/alt/python37/lib64/python3.7/site-packages/twisted/test/test_defer.py�test_notAvailable-sz$DeferredTestsAsync.test_notAvailableN)r�
__module__�__qualname__�__doc__�skiprrrrrr&sc@seZdZdS)�GenericErrorN)rrrrrrrr5srcOs*yddWntj||�}YnX|S)z�
    Make a L{failure.Failure} of a divide-by-zero error.

    @param args: Any C{*args} are passed to Failure's constructor.
    @param kwargs: Any C{**kwargs} are passed to Failure's constructor.
    �r)r�Failure)�args�kwargs�frrr�getDivisionFailure:s
r!cCs|�d�dS)z�
    A fake L{defer.Deferred} canceller which callbacks the L{defer.Deferred}
    with C{str} "Callback Result" when cancelling it.

    @param deferred: The cancelled L{defer.Deferred}.
    zCallback ResultN)�callback)�deferredrrr�fakeCallbackCancellerIsr$c@seZdZdZdd�ZdS)�ImmediateFailureMixinz+
    Add additional assertion methods.
    cCs>g}|�|j�|�t|�d�|�|d�|��|djS)z�
        Assert that the given Deferred current result is a Failure with the
        given exception.

        @return: The exception instance in the Deferred.
        rr)�
addErrback�append�assertEqual�len�
assertTrue�check�value)rr#�	exception�failuresrrr�assertImmediateFailureYs
z,ImmediateFailureMixin.assertImmediateFailureN)rrrrr/rrrrr%Tsr%c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�	UtilTestsz&
    Tests for utility functions.
    cCs0t�t��}t�|�}|�t�|�||�dS)z<
        L{defer.logError} returns the given error.
        N)rr�RuntimeErrorr
�logError�flushLoggedErrors�assertIs)r�error�resultrrr�test_logErrorReturnsErrorms

z#UtilTests.test_logErrorReturnsErrorcCs2t�t��}t�|�|�t�}|�||g�dS)z9
        L{defer.logError} logs the given error.
        N)rrr1r
r2r3r()rr5�errorsrrr�test_logErrorLogsErrorxs

z UtilTests.test_logErrorLogsErrorcsRg��fdd�}t�|�t�t��}t�|�|�t�|��d�	d��dS)zR
        The text logged by L{defer.logError} has no repr of the failure.
        cs��t�|��dS)N)r'r�textFromEventDict)Z	eventDict)�outputrr�emit�sz4UtilTests.test_logErrorLogsErrorNoRepr.<locals>.emitrzUnhandled Error
Traceback N)
r�addObserverrrr1r
r2r3r*�
startswith)rr<r5r)r;r�test_logErrorLogsErrorNoRepr�s


z&UtilTests.test_logErrorLogsErrorNoReprN)rrrrr7r9r?rrrrr0hsr0c@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Zd9d:�Zd;d<�Z d=d>�Z!d?d@�Z"dAdB�Z#dCdD�Z$dEdF�Z%dGdH�Z&dIdJ�Z'dKdL�Z(dMdN�Z)dOdP�Z*dQdR�Z+dSdT�Z,dUdV�Z-dWdX�Z.dYdZ�Z/d[d\�Z0d]d^�Z1d_d`�Z2dadb�Z3dcdd�Z4dedf�Z5dgdh�Z6didj�Z7dkdl�Z8dmdn�Z9dodp�Z:dqdr�Z;dsdt�Z<dudv�Z=dwdx�Z>dydz�Z?d{d|�Z@d}d~�ZAdd��ZBd�d��ZCd�d��ZDd�d��ZEd�d��ZFd�d��ZGd�S)��
DeferredTestscCs(d|_d|_d|_|�tjt���dS)N)�callbackResults�errbackResults�callback2Results�
addCleanupr
�setDebugging�getDebugging)rrrr�setUp�szDeferredTests.setUpcOs||f|_|dS)Nr)rA)rr�kwrrr�	_callback�s
zDeferredTests._callbackcOs||f|_dS)N)rC)rrrHrrr�
_callback2�szDeferredTests._callback2cOs||f|_dS)N)rB)rrrHrrr�_errback�szDeferredTests._errbackcCs@t��}|�|j�|�d�|�|j�|�|jdif�dS)N�hello)rL)	r
�Deferred�addCallbackrIr"�assertIsNonerBr(rA)rr#rrr�testCallbackWithoutArgs�s

z%DeferredTests.testCallbackWithoutArgscCsBt��}|�|jd�|�d�|�|j�|�|jdif�dS)N�worldrL)rLrQ)	r
rMrNrIr"rOrBr(rA)rr#rrr�testCallbackWithArgs�s

z"DeferredTests.testCallbackWithArgscCsHt��}|j|jdd�|�d�|�|j�|�|jdddif�dS)NrQ)rQrL)rL)	r
rMrNrIr"rOrBr(rA)rr#rrr�testCallbackWithKwArgs�s
z$DeferredTests.testCallbackWithKwArgscCs^t��}|�|j�|�|j�|�d�|�|j�|�|j	dif�|�|j
dif�dS)NrL)rL)r
rMrNrIrJr"rOrBr(rArC)rr#rrr�testTwoCallbacks�s

zDeferredTests.testTwoCallbackscCs�t��}t��}t��}t�|||g�}g}|fdd�}dd�}|�||�|�d�|�|�|�td��|�d�|�|d|d	dt	|d	d	j
�f|d
gtjdftjdftjdfg�dS)NcSs|�|�dS)N)�extend)�
resultListr6rrr�cb�sz*DeferredTests.testDeferredList.<locals>.cbcSsdS)Nr)�errrrr�catch�sz-DeferredTests.testDeferredList.<locals>.catch�1�2�3rr�)
r
rM�DeferredList�addCallbacksr"r&�errbackrr(�strr,�SUCCESS�FAILURE)r�defr1�defr2�defr3�dlr6rWrYrrr�testDeferredList�s$



zDeferredTests.testDeferredListcCsfg}|fdd�}t�g�}|�|�|�|gg�g|dd�<tjgdd�}|�|�|�|g�dS)NcSs|�|�dS)N)r')rVr6rrrrW�sz/DeferredTests.testEmptyDeferredList.<locals>.cbr)�fireOnOneCallback)r
r^r_r()rr6rWrgrrr�testEmptyDeferredList�s


z#DeferredTests.testEmptyDeferredListc	Cs�t��}t��}t��}tj|||gdd�}g}|�|j�dd�}|�|�|�d�|�|g�|�td��|�t	|�d�|d}|�
t|jtj
�d|jf�|j}|�|jjt�|�|jjjd	�|�|jd�dS)
Nr)�fireOnOneErrbackcSsdS)Nr)rXrrrrYsz;DeferredTests.testDeferredListFireOnOneError.<locals>.catchrZz	from def2rzHissubclass(aFailure.type, defer.FirstError) failed: failure's type is %r)z	from def2)r
rMr^r&r'r"r(r`rr)r*�
issubclass�type�
FirstErrorr,�
subFailurer�index)	rrdrerfrgr6rYZaFailure�
firstErrorrrr�testDeferredListFireOnOneError�s(

z,DeferredTests.testDeferredListFireOnOneErrorcCs�t��}t�|g�}g}|�|j�g}|�|j�|�td��|�d|dj	j
d�|�dt|��|�d|dddj	j
d�dS)N�Bangrr)r
rMr^r&r'rNr`rr(r,rr))r�d1rg�	errorTrapr6rrr�!testDeferredListDontConsumeErrorssz/DeferredTests.testDeferredListDontConsumeErrorscCs�t��}tj|gdd�}g}|�|j�g}|�|j�|�td��|�g|�|�dt	|��|�d|dddj
jd�dS)NT)�
consumeErrorsrsrr)r
rMr^r&r'rNr`rr(r)r,r)rrtrgrur6rrr�testDeferredListConsumeErrors-sz+DeferredTests.testDeferredListConsumeErrorscCsbt��}t��}|�td��tj||gdd�}g}|�|j�|�dt|��|�dd��dS)NrsT)rkrcSsdS)Nr)�errr�<lambda>H�zWDeferredTests.testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds.<locals>.<lambda>)	r
rMr`rr^r&r'r(r))rrt�d2rgr6rrr�7testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds<szEDeferredTests.testDeferredListFireOnOneErrorWithAlreadyFiredDeferredscCsht��}t��}|�td��|�d�t�||g�}g}|�|j�|�dt	|��|�
dd��dS)Nrsr]rcSsdS)Nr)ryrrrrzYr{zIDeferredTests.testDeferredListWithAlreadyFiredDeferreds.<locals>.<lambda>)r
rMr`rr"r^rNr'r(r)r&)rrtr|rgr6rrr�)testDeferredListWithAlreadyFiredDeferredsJs
z7DeferredTests.testDeferredListWithAlreadyFiredDeferredscCsFt��}t��}t�||g�}|��|�|tj�|�|tj�dS)zw
        When cancelling an unfired L{defer.DeferredList}, cancel every
        L{defer.Deferred} in the list.
        N)r
rMr^�cancel�failureResultOfr)r�deferredOne�deferredTwo�deferredListrrr�test_cancelDeferredList\sz%DeferredTests.test_cancelDeferredListcCs�t�t�}t��}t�||g�}|��|�|tj�|�|�}|�|dd�|�	|ddd�|�
|dd�|�|dd�tj��dS)z�
        When cancelling an unfired L{defer.DeferredList} without the
        C{fireOnOneCallback} and C{fireOnOneErrback} flags set, the
        L{defer.DeferredList} will be callback with a C{list} of
        (success, result) C{tuple}s.
        rrzCallback ResultN)r
rMr$r^rr�r�successResultOfr*r(�assertFalser+)rr�r�r�r6rrr�test_cancelDeferredListCallbackis

z-DeferredTests.test_cancelDeferredListCallbackcCsJt��}t��}tj||gdd�}|��|�|tj�|�|tj�dS)z�
        When cancelling an unfired L{defer.DeferredList} with the flag
        C{fireOnOneCallback} set, cancel every L{defer.Deferred} in the list.
        T)riN)r
rMr^rr�r)rr�r�r�rrr�,test_cancelDeferredListWithFireOnOneCallback|s
z:DeferredTests.test_cancelDeferredListWithFireOnOneCallbackcCsTt�t�}t��}tj||gdd�}|��|�|tj�|�|�}|�|d�dS)a,
        When cancelling an unfired L{defer.DeferredList} with the flag
        C{fireOnOneCallback} set, if one of the L{defer.Deferred} callbacks
        in its canceller, the L{defer.DeferredList} will callback with the
        result and the index of the L{defer.Deferred} in a C{tuple}.
        T)ri)zCallback ResultrN)	r
rMr$r^rr�rr�r()rr�r�r�r6rrr�?test_cancelDeferredListWithFireOnOneCallbackAndDeferredCallback�s


zMDeferredTests.test_cancelDeferredListWithFireOnOneCallbackAndDeferredCallbackcCsrt��}t��}tj||gdd�}|��|�|tj�|�|tj�|�|tj�}|j}|�|j	�
tj��dS)z�
        When cancelling an unfired L{defer.DeferredList} with the flag
        C{fireOnOneErrback} set, cancel every L{defer.Deferred} in the list.
        T)rkN)r
rMr^rr�rrnr,r*ror+)rr�r�r�ZdeferredListFailurerqrrr�+test_cancelDeferredListWithFireOnOneErrback�s
z9DeferredTests.test_cancelDeferredListWithFireOnOneErrbackcCs�t�t�}t�t�}tj||gdd�}|��|�|�}|�|dd�|�|ddd�|�|dd�|�|ddd�dS)a
        When cancelling an unfired L{defer.DeferredList} with the flag
        C{fireOnOneErrback} set, if all the L{defer.Deferred} callbacks
        in its canceller, the L{defer.DeferredList} will callback with a
        C{list} of (success, result) C{tuple}s.
        T)rkrrzCallback ResultN)r
rMr$r^rr�r*r()rr�r�r�r6rrr�?test_cancelDeferredListWithFireOnOneErrbackAllDeferredsCallback�s



zMDeferredTests.test_cancelDeferredListWithFireOnOneErrbackAllDeferredsCallbackcCsft��}t��}||g}t�|�}t��}|�|�|��|�|tj�|�|tj�|�|�dS)zs
        Cancelling a L{defer.DeferredList} will cancel the original
        L{defer.Deferred}s passed in.
        N)r
rMr^r'rr�r�assertNoResult)rr�r�ZargumentListr�Z
deferredThreerrr�,test_cancelDeferredListWithOriginalDeferreds�s

z:DeferredTests.test_cancelDeferredListWithOriginalDeferredscCs\dd�}t�|�}t��}t�||g�}|��|�|tj�|�t�}|�t	|�d�dS)z�
        Cancelling a L{defer.DeferredList} will cancel every L{defer.Deferred}
        in the list even exceptions raised from the C{cancel} method of the
        L{defer.Deferred}s.
        cSstd��dS)z�
            A L{defer.Deferred} canceller that raises an exception.

            @param deferred: The cancelled L{defer.Deferred}.
            �testN)r1)r#rrr�cancellerRaisesException�szTDeferredTests.test_cancelDeferredListWithException.<locals>.cancellerRaisesExceptionrN)
r
rMr^rr�rr3r1r(r))rr�r�r�r�r8rrr�$test_cancelDeferredListWithException�s

z2DeferredTests.test_cancelDeferredListWithExceptioncCsBt��}t��}tj||gdd�}|�d�|��|�|�dS)z�
        When a L{defer.DeferredList} has fired because one L{defer.Deferred} in
        the list fired with a non-failure result, the cancellation will do
        nothing instead of cancelling the rest of the L{defer.Deferred}s.
        T)riN)r
rMr^r"rr�)rr�r�r�rrr�)test_cancelFiredOnOneCallbackDeferredList�s

z7DeferredTests.test_cancelFiredOnOneCallbackDeferredListcCs`t��}t��}tj||gdd�}|�td��|��|�|�|�|t�|�|tj�dS)z�
        When a L{defer.DeferredList} has fired because one L{defer.Deferred} in
        the list fired with a failure result, the cancellation will do
        nothing instead of cancelling the rest of the L{defer.Deferred}s.
        T)rkr�N)	r
rMr^r`rrr�r�rn)rr�r�r�rrr�(test_cancelFiredOnOneErrbackDeferredList�s

z6DeferredTests.test_cancelFiredOnOneErrbackDeferredListcCs,g}t�d�}|�|j�|�|dg�dS)N�success)r
�succeedrNr'r()r�l�drrr�testImmediateSuccesss
z"DeferredTests.testImmediateSuccesscCs8g}t�td��}|�|j�|�t|dj�d�dS)N�failr)r
r�rr&r'r(rar,)rr�r�rrr�testImmediateFailuresz"DeferredTests.testImmediateFailurecCsTg}t�td��}|��|�|j�|�|g�|��|�t|dj	�d�dS)Nr�r)
r
r�r�pauser&r'r(�unpauserar,)rr�r�rrr�testPausedFailureszDeferredTests.testPausedFailurecCsxg}t���dd���|j�}|�d�|�|djt�g}t���dd���|j�}|�d�|�|djt�dS)NcSsddS)Nrrr)�_rrrrzr{z2DeferredTests.testCallbackErrors.<locals>.<lambda>rrcSst�t��S)N)rr�ZeroDivisionError)r�rrrrz$r{)	r
rMrNr&r'r"�assertIsInstancer,r�)rr�r�rrr�testCallbackErrorss

z DeferredTests.testCallbackErrorscCs(t��}|��|�|j�|��dS)N)r
rMr�rNrIr�)rr�rrr�testUnpauseBeforeCallback(sz'DeferredTests.testUnpauseBeforeCallbackcCs�t��}t��}|��|�|fdd��|�|j�|�d�|jdksRtd��|�d�|jdksntd��|��|jdddks�td|jf��dS)	NcSs|S)Nr)�rr|rrrrz2r{z2DeferredTests.testReturnDeferred.<locals>.<lambda>rz Should not have been called yet.r]z&Still should not have been called yet.rz/Result should have been from second deferred:%s)	r
rMr�rNrIr"rA�AssertionErrorr�)rr�r|rrr�testReturnDeferred.s

z DeferredTests.testReturnDeferredcszt�}t�����|����t��}|��fdd��|�d�g}|�|j�|�|g����|�||g�dS)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.
        cs�S)Nr)�ignored)�pausedrrrzGr{zDDeferredTests.test_chainedPausedDeferredWithResult.<locals>.<lambda>N)	�objectr
rMr"r�rNr'r(r�)r�expectedZchainedr6r)r�r�$test_chainedPausedDeferredWithResult<s

z2DeferredTests.test_chainedPausedDeferredWithResultcs`t��}t���|��fdd��|�d�|����d�g}��|j�|�|dg�dS)z�
        A paused Deferred encountered while pushing a result forward through a
        chain does not prevent earlier Deferreds from continuing to execute
        their callbacks.
        cs�S)Nr)r�)�secondrrrzYr{z:DeferredTests.test_pausedDeferredChained.<locals>.<lambda>N)r
rMrNr"r�r'r()r�firstr6r)r�r�test_pausedDeferredChainedQs

z(DeferredTests.test_pausedDeferredChainedcCs�g}t�t�d�t�d�g��|j�|�|ddgg�g}t�d�t�t��g}t�|��|j�|�t	|�d�|�
|dtj�|d�dd��dS)Nrr]rcSsdS)Nrr)ryrrrrznr{z2DeferredTests.test_gatherResults.<locals>.<lambda>)
r
�
gatherResultsr�rNr'r(r��
ValueErrorr&r)r�rr)rr�rgrrr�test_gatherResultsbs"z DeferredTests.test_gatherResultscCs�t�d�}t�td��}tj||gdd�}g}|�|j�g}|�|j�|�t|�t|�fd�|�	|dj
tj�|dj
j}|�	|j
t�dS)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}.
        rzoh noesT)rw)rrrN)
r
r�r�r1r�r&r'r(r)r�r,rnro)rZdgoodZdbadr�ZunconsumedErrorsZgatheredErrorsrqrrr�#test_gatherResultsWithConsumeErrorsqs
z1DeferredTests.test_gatherResultsWithConsumeErrorscCsnt��}t��}t�||g�}|��|�|tj�|�|tj�|�|tj�}|j}|�|j	�
tj��dS)z�
        When cancelling the L{defer.gatherResults} call, all the
        L{defer.Deferred}s in the list will be cancelled.
        N)r
rMr�rr�rrnr,r*ror+)rr�r�r6ZgatherResultsFailurerqrrr�test_cancelGatherResults�sz&DeferredTests.test_cancelGatherResultscCsXt�t�}t�t�}t�||g�}|��|�|�}|�|dd�|�|dd�dS)a
        When cancelling the L{defer.gatherResults} call, if all the
        L{defer.Deferred}s callback in their canceller, the L{defer.Deferred}
        returned by L{defer.gatherResults} will be callbacked with the C{list}
        of the results.
        rzCallback ResultrN)r
rMr$r�rr�r()rr�r�r6ZcallbackResultrrr�0test_cancelGatherResultsWithAllDeferredsCallback�s


z>DeferredTests.test_cancelGatherResultsWithAllDeferredsCallbackcCsHgg}}t�dd�d�}|�|j|j�|�|g�|�|dg�dS)z�
        L{defer.maybeDeferred} should retrieve the result of a synchronous
        function and pass it to its resulting L{defer.Deferred}.
        cSs|dS)N�r)�xrrrrz�r{z6DeferredTests.test_maybeDeferredSync.<locals>.<lambda>�
�N)r
�
maybeDeferredr_r'r()r�S�Er�rrr�test_maybeDeferredSync�s

z$DeferredTests.test_maybeDeferredSyncc
Cs�gg}}yddWn*tk
r@}zt|�}Wdd}~XYnXt�dd�d�}|�|j|j�|�|g�|�t|�d�|�t|dj�|�dS)z�
        L{defer.maybeDeferred} should catch exception raised by a synchronous
        function and errback its resulting L{defer.Deferred} with it.
        �10r�NcSs|dS)Nr�r)r�rrrrz�r{z;DeferredTests.test_maybeDeferredSyncError.<locals>.<lambda>rr)	�	TypeErrorrar
r�r_r'r(r)r,)rr�r�ryr�r�rrr�test_maybeDeferredSyncError�s
z)DeferredTests.test_maybeDeferredSyncErrorcsFt���t��fdd��}��d�g}|�|j�|�|dg�dS)z�
        L{defer.maybeDeferred} should let L{defer.Deferred} instance pass by
        so that original result is the same.
        cs�S)Nrr)r�rrrz�r{z7DeferredTests.test_maybeDeferredAsync.<locals>.<lambda>ZSuccessN)r
rMr�r"rNr'r()rr|r6r)r�r�test_maybeDeferredAsync�s
z%DeferredTests.test_maybeDeferredAsynccs<t���t��fdd��}��t�t���|�|t�dS)z�
        L{defer.maybeDeferred} should let L{defer.Deferred} instance pass by
        so that L{failure.Failure} returned by the original instance is the
        same.
        cs�S)Nrr)r�rrrz�r{z<DeferredTests.test_maybeDeferredAsyncError.<locals>.<lambda>N)r
rMr�r`rrr1r/)rr|r)r�r�test_maybeDeferredAsyncError�sz*DeferredTests.test_maybeDeferredAsyncErrorcs�g�g�t������fdd�}t�d�}|�|���d�|��j����j�|��j�|�g��|��ddddg�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.
        csR��d|f�t�d�}��fdd�}�fdd�}|�|��|�|��j�|S)Nzstart-of-cb�innercs��d��S)N)�
firstCallbackr�)r')r6)r��resultsrrr��s
zMDeferredTests.test_innerCallbacksPreserved.<locals>.cb.<locals>.firstCallbackcs��d|f�|dS)N�secondCallbackr])r')r6)r�rrr��szNDeferredTests.test_innerCallbacksPreserved.<locals>.cb.<locals>.secondCallback)r'r
r�rNr&)r6r�r�r�)r.r�r�rrrW�s
z6DeferredTests.test_innerCallbacksPreserved.<locals>.cb�outer�orange)zstart-of-cbr�)r�r�)r�r��orangeorangeN)r
rMr�rNr"r'r&r()rrWr�r)r.r�r�r�test_innerCallbacksPreserved�s"


z*DeferredTests.test_innerCallbacksPreservedcs�g�g�t������fdd�}t�d�}|�|�|��j�|�ddg����d�|�g��|��ddddgfg�dS)	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.
        cs^��d|f�t��}��fdd�}�fdd�}|�|�|�|�|��j�|�d�|S)NrWcs��d|f�t��g�S)Nr�)r'r
r�)r�)�ar�rrr�szNDeferredTests.test_continueCallbackNotFirst.<locals>.cb.<locals>.firstCallbackcs��d|f�dS)Nr�)r')r6)r�rrr�szODeferredTests.test_continueCallbackNotFirst.<locals>.cb.<locals>.secondCallback)r'r
rMrNr&r")r6r�r�r�)r�r.r�rrrW
s


z7DeferredTests.test_continueCallbackNotFirst.<locals>.cbr�)rWr�)r�N�withersr�N)r
rMr�rNr&r'r(r")rrWr�r)r�r.r�r�test_continueCallbackNotFirsts


z+DeferredTests.test_continueCallbackNotFirstcs�g�g�t������fdd�}t��}|�|�|��fdd��|��j�|�d�|�ddg����d�|�g��|��ddd	dgfd
g�dS)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.
        cs^��d|f�t��}��fdd�}�fdd�}|�|�|�|�|��j�|�d�|S)NrWcs��d|f�t��g�S)Nr�)r'r
r�)r�)r�r�rrr�9szLDeferredTests.test_callbackOrderPreserved.<locals>.cb.<locals>.firstCallbackcs��d|f�dS)Nr�)r')r6)r�rrr�=szMDeferredTests.test_callbackOrderPreserved.<locals>.cb.<locals>.secondCallback)r'r
rMrNr&r")r6r�r�r�)r�r.r�rrrW5s


z5DeferredTests.test_callbackOrderPreserved.<locals>.cbcs
��d�S)N�final)r')r�)r�rrrzHr{z;DeferredTests.test_callbackOrderPreserved.<locals>.<lambda>r�)rWr�)r�Nr�r�r�N)r
rMrNr&r'r"r()rrWr�r)r�r.r�r�test_callbackOrderPreserved+s"


z)DeferredTests.test_callbackOrderPreservedcsht���g��fdd���fdd�}���fdd�}��|���|���d�|��dd	d
g�dS)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.
        cs��d�dS)N�)r')r6)�calledrr�	callback3\sz;DeferredTests.test_reentrantRunCallbacks.<locals>.callback3cs��d�dS)Nr])r')r6)r�rr�	callback2^sz;DeferredTests.test_reentrantRunCallbacks.<locals>.callback2cs��d�����dS)Nr)r'rN)r6)r�r�r#rr�	callback1`s
z;DeferredTests.test_reentrantRunCallbacks.<locals>.callback1Nrr]r�)r
rMrNr"r()rr�r�r)r�r�r#r�test_reentrantRunCallbacksUs


z(DeferredTests.test_reentrantRunCallbackscsRt���g��fdd������fdd�}��|���d����ddg�dS)z�
        A callback added to a L{Deferred} by a callback on that L{Deferred}
        should not be executed until the running callback returns.
        cs��d�dS)Nr])r')r6)r�rrr�psz;DeferredTests.test_nonReentrantCallbacks.<locals>.callback2cs&��d��������dg�dS)Nr)r'rNr()r6)r�r�r#rrrr�rs

z;DeferredTests.test_nonReentrantCallbacks.<locals>.callback1Nrr])r
rMrNr"r()rr�r)r�r�r#rr�test_nonReentrantCallbacksis

z(DeferredTests.test_nonReentrantCallbackscsZd�t����fdd����fdd�}��|���d�|��t�}|�|j�f�dS)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.
        zcallback raised exceptioncst���dS)N)�	Exception)r6)�exceptionMessagerrr��szFDeferredTests.test_reentrantRunCallbacksWithFailure.<locals>.callback2cs����dS)N)rN)r6)r�r#rrr��szFDeferredTests.test_reentrantRunCallbacksWithFailure.<locals>.callback1N)r
rMrNr"r/r�r(r)rr�r-r)r�r#r�r�%test_reentrantRunCallbacksWithFailure{s

z3DeferredTests.test_reentrantRunCallbacksWithFailurecsrt�}t�|��t��}|��fdd��|�d�g}��|j�|�|d�|�|j�|�|d|�dS)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}.
        cs�S)Nr)�ign)r�rrrz�r{z=DeferredTests.test_synchronousImplicitChain.<locals>.<lambda>Nrr)	r�r
r�rMrNr"r'rOr4)rr6r�r�r)r�r�test_synchronousImplicitChain�s

z+DeferredTests.test_synchronousImplicitChaincs�t���t��}|��fdd��|�d�g}��|j�g}|�|j�|�|g�|�|g�t�}��|�|�|dg�|�||g�dS)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}.
        cs�S)Nr)r�)r�rrrz�r{z>DeferredTests.test_asynchronousImplicitChain.<locals>.<lambda>N)r
rMrNr"r'r(r�)rr��firstResult�secondResultr6r)r�r�test_asynchronousImplicitChain�s

z,DeferredTests.test_asynchronousImplicitChaincCs`t�td��}t��}|�|fdd��|�d�g}|�|j�|�|d�|�|t�dS)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.
        zFirst Deferred's FailurecSs|S)Nr)r�r�rrrrz�r{zBDeferredTests.test_synchronousImplicitErrorChain.<locals>.<lambda>Nr)	r
r�r1rMrNr"r'rOr/)rr�r�r�rrr�"test_synchronousImplicitErrorChain�s
z0DeferredTests.test_synchronousImplicitErrorChaincs�t���t��}|��fdd��|�d�g}|�|j�g}��|j�g}|�|j�|�|g�|�|g���td��|�	|d�
t��|�|dg�|�t|�d�dS)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.
        cs�S)Nr)r�)r�rrrz�r{zCDeferredTests.test_asynchronousImplicitErrorChain.<locals>.<lambda>NzFirst Deferred's Failurerr)r
rMrNr"r&r'r(r`r1r*r+r))rr�ZsecondErrorr�r�r)r�r�#test_asynchronousImplicitErrorChain�s 	
z1DeferredTests.test_asynchronousImplicitErrorChaincs�t���t������fdd��t��}|��fdd��g}|�|j�t�}��d�|�d�|�|g���|�|�||g�dS)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.
        cs�S)Nr)r�)r�rrrz�r{zGDeferredTests.test_doubleAsynchronousImplicitChaining.<locals>.<lambda>cs�S)Nr)r�)r�rrrz�r{N)r
rMrNr'r�r"r()r�thirdZthirdResultr6r)r�r�r�'test_doubleAsynchronousImplicitChaining�s


z5DeferredTests.test_doubleAsynchronousImplicitChainingcs�g�g�t������fdd�}t�d�}|�|�|��j�|��ddg���d����j�|��j�|�g�d�|��dddd	g�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.
        csR��d|f�t�d�}��fdd�}�fdd�}|�|��|�|��j�|S)Nzstart-of-cbr�cs��d��S)N)r�r�)r')r6)r�r�rrr�s
zXDeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb.<locals>.firstCallbackcs��d|f�|dS)Nr�r])r')r6)r�rrr�!szYDeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb.<locals>.secondCallback)r'r
r�rNr&)r6r�r�r�)r.r�r�rrrWs
zADeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cbr�)zstart-of-cbr�)r�r�r�z&Got errbacks but wasn't expecting any.)r�r�r�N)r
rMr�rNr'r(r"r&)rrWr�r)r.r�r�r�'test_nestedAsynchronousChainedDeferredss(



z5DeferredTests.test_nestedAsynchronousChainedDeferredscs�g�g�t������fdd�}t�d�}|�|�|��j�|��ddg���d�|��j����j�|�g�d�|��ddddgfddgg�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.
        csV��d|f�t�d�}��fdd�}�fdd�}|�|�|�|�|��j�|S)Nzstart-of-cbr�cs��d|f���dd��S)Nr�cSs|gS)Nr)r�rrrrz_r{z|DeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.firstCallback.<locals>.<lambda>)r'rN)r�)r�r�rrr�YszjDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.firstCallbackcs��d|f�|dS)Nr�r])r')r6)r�rrr�aszkDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.secondCallback)r'r
r�rNr&)r6r�r�r�)r.r�r�rrrWUs


zSDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cbr�)zstart-of-cbr�)r�r�r�z&Got errbacks but wasn't expecting any.r�N)r
rMr�rNr'r(r"r&)rrWr�r)r.r�r�r�9test_nestedAsynchronousChainedDeferredsWithExtraCallbacksFs(



zGDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbackscCs,t��}t��}|�|�|�|j|�dS)zT
        When we chain a L{Deferred}, that chaining is recorded explicitly.
        N)r
rM�
chainDeferredr4�
_chainedTo)rr��brrr�&test_chainDeferredRecordsExplicitChain�s
z4DeferredTests.test_chainDeferredRecordsExplicitChaincCs4t��}t��}|�|�|�d�|�|j�dS)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
rMr�r"rOr�)rr�r�rrr�%test_explicitChainClearedWhenResolved�s
	

z3DeferredTests.test_explicitChainClearedWhenResolvedcs>t��}t���|��fdd��|�d�|�|j��dS)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.
        cs�S)Nr)r�)r�rrrz�r{zFDeferredTests.test_chainDeferredRecordsImplicitChain.<locals>.<lambda>N)r
rMrNr"r4r�)rr�r)r�r�&test_chainDeferredRecordsImplicitChain�s

z4DeferredTests.test_chainDeferredRecordsImplicitChaincs�t����fdd�}��|���d�|�|g�}|�t|�d�|d}|�|dt�d}|�t	�
||d�d	||df�d
S)z�
        When a Deferred is returned from a callback directly attached to that
        same Deferred, a warning is emitted.
        cs�S)Nr)r6)r�rr�circularCallback�szADeferredTests.test_circularChainWarning.<locals>.circularCallback�foorr�categoryz1Callback returned the Deferred it was attached to�messagez
Expected match: %r
Got: %rN)r
rMrNr"�
flushWarningsr(r)�DeprecationWarningr*�re�search)rr�Zcircular_warnings�warning�patternr)r�r�test_circularChainWarning�s

z'DeferredTests.test_circularChainWarningcs`|�ttdtj�tjdtd�t����fdd�}��|���	d�|�
��}|�t�dS)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.
        �filtersr5)r�cs�S)Nr)r6)r�rrr��szCDeferredTests.test_circularChainException.<locals>.circularCallbackr�N)rD�setattr�warningsr��filterwarningsr�r
rMrNr"r��trap)rr�rr)r�r�test_circularChainException�s


z)DeferredTests.test_circularChainExceptioncCs*t��}t|�}|�t|�d|f�dS)z�
        The C{repr()} of a L{Deferred} contains the class name and a
        representation of the internal Python ID.
        z<Deferred at 0x%x>N)r
rM�idr(�repr)rr��addressrrr�	test_repr�szDeferredTests.test_reprcCs0t��}|�d�|�t|�dt|�f�dS)zb
        If a L{Deferred} has been fired, then its C{repr()} contains its
        result.
        r�z+<Deferred at 0x%x current result: 'orange'>N)r
rMr"r(r�r�)rr�rrr�test_reprWithResult�s

z!DeferredTests.test_reprWithResultcCs>t��}t��}|�|�|�t|�dt|�t|�f�dS)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}.
        z.<Deferred at 0x%x waiting on Deferred at 0x%x>N)r
rMr�r(r�r�)rr�r�rrr�test_reprWithChaining�s
z#DeferredTests.test_reprWithChainingcs$�fdd�}��|d�|d��dS)zw
        The depth of the call stack does not grow as more L{Deferred} instances
        are chained together.
        cs�g��fdd�}t��}dd�t|�D�}|dd�}t���|��}|�|fdd��|�|�x&|r�|��}|�|fdd��|}qdW|��fdd��|�d�x|D]}|�d�q�W���g���d��d	S)
Ncs��tt����dS)N)r'r)�	traceback�
extract_stack)r�)�stackrr�recordStackDepthszVDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.recordStackDepthcSsg|]}t���qSr)r
rM)�.0r�rrr�
<listcomp>szPDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.<listcomp>cSs|S)Nr)r�r�rrrrz	r{zNDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.<lambda>cSs|S)Nr)r�r�rrrrzr{cs�S)Nr)r�)�lastrrrzr{r)r
rM�range�poprNr"r()ZhowManyr�topZinnerDeferredsZoriginalInnersr�ZnewInner)r)rrr�chainDeferreds�s(



z<DeferredTests.test_boundedStackDepth.<locals>.chainDeferredsrr]N)r()rr	r)rr�test_boundedStackDepth�s#z$DeferredTests.test_boundedStackDepthcs|t��}t���t���|��fdd�����fdd����d�|�d���d�g}��|j�|�|dg�dS)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.
        cs�S)Nr)r�)r�rrrz2r{zZDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled.<locals>.<lambda>cs�S)Nr)r�)r�rrrz3r{N)r
rMrNr"r'r()rr��Lr)r�r�r�:test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled$s


zHDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalledcCs�t�d�t��}g}td�}y|�Wn|��YnX|�|j�|d}|�|j|�|j	ddd�\}}|�g|�|�g|�dS)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.
        Frsr���N)
r
rErMrr`r&r'r(r,�frames)rr�r��excr��localz�globalzrrr�test_errbackWithNoArgsNoDebug<s
z+DeferredTests.test_errbackWithNoArgsNoDebugcCs�t�d�t��}g}td�}y|�Wn|��YnX|�|j�|d}|�|j|�|j	ddd�\}}|�
g|�|�
g|�dS)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.
        Trsrr
N)r
rErMrr`r&r'r(r,r�assertNotEqual)rr�r�rr�rrrrr�test_errbackWithNoArgsRs
z$DeferredTests.test_errbackWithNoArgscCsxt��}|�d�t�d�dd�}|�|�g}|�|j�|d}|jddd�\}}|�g|�|�g|�dS)z�
        An error raised by a callback creates a Failure.  The Failure captures
        locals and globals if and only if C{Deferred.debug} is set.
        NFcSstd��dS)Nrs)r)r�rrr�
raiseErrorpszHDeferredTests.test_errorInCallbackDoesNotCaptureVars.<locals>.raiseErrorrr
)	r
rMr"rErNr&r'rr()rr�rr�r�rrrrr�&test_errorInCallbackDoesNotCaptureVarshs


z4DeferredTests.test_errorInCallbackDoesNotCaptureVarscCsxt��}|�d�t�d�dd�}|�|�g}|�|j�|d}|jddd�\}}|�g|�|�g|�dS)z�
        An error raised by a callback creates a Failure.  The Failure captures
        locals and globals if and only if C{Deferred.debug} is set.
        NTcSstd��dS)Nrs)r)r�rrrr�szODeferredTests.test_errorInCallbackCapturesVarsWhenDebugging.<locals>.raiseErrorrr
)	r
rMr"rErNr&r'rr)rr�rr�r�rrrrr�-test_errorInCallbackCapturesVarsWhenDebugging{s


z;DeferredTests.test_errorInCallbackCapturesVarsWhenDebuggingcCs8t�}t��}y|��Wn|��YnXdd�}t�|�}|�|�}t�|�	��}t
r�|�t|�d�|�
d|dd�|�d|dd�|�d|dd�nL|�t|�d�|�
d|d	d�|�d|d	d�|�d|d	d�|�
d|dd�|�d
|dd�|�d|dd�dS)
z�
        L{defer.inlineCallbacks} that re-raise tracebacks into their deferred
        should not lose their tracebacks.
        css
|VdS)Nr)r�rrr�ic�sz8DeferredTests.test_inlineCallbacksTracebacks.<locals>.icr�Z
test_deferr]rr!z1/0r�test_inlineCallbacksTracebackszf.raiseException()N)r!r
rMZraiseExceptionr`ZinlineCallbacksr�r��
extract_tbZgetTracebackObjectrr(r)�assertIn)rr r�rZ
newFailure�tbrrrr�s,

z,DeferredTests.test_inlineCallbacksTracebacksN)HrrrrGrIrJrKrPrRrSrTrhrjrrrvrxr}r~r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r
rrrrrrrrrrr@�s�(
	
%)* ;A

+r@c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�FirstErrorTestsz"
    Tests for L{FirstError}.
    cCsPtd�}y|�Wnt��}YnXt�|d�}|�t|�dt|�f�dS)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}.
        z	some textr�zFirstError[#3, %s]N)r�rrr
rnr(r�)rrr r5rrrr��szFirstErrorTests.test_reprcCsPtd�}y|�Wnt��}YnXt�|d�}|�t|�dt|�f�dS)z�
        The str of a L{FirstError} instance includes the str of the
        sub-failure and the index which corresponds to the L{FirstError}.
        z	some textr�zFirstError[#5, %s]N)r�rrr
rnr(ra)rrr r5rrr�test_str�szFirstErrorTests.test_strcCs�yddWnt��}YnXt�|d�}t�|d�}ytd��Wnt��}YnXt�|d�}|�||k�|�||k�|�||k�|�||k�|�|dk�dS)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.
        rr�
Zbar�	r�N)rrr
rnr�r*r�)rZfirstFailure�oneZ
anotherOneZ
secondFailureZanotherrrr�test_comparison�s zFirstErrorTests.test_comparisonN)rrrrr�rr"rrrrr�src@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)S)*�AlreadyCalledTestscCst��|_t�d�dS)NT)r
rF�_deferredWasDebuggingrE)rrrrrG�s
zAlreadyCalledTests.setUpcCst�|j�dS)N)r
rEr$)rrrr�tearDown�szAlreadyCalledTests.tearDowncOsdS)Nr)rrrHrrrrI�szAlreadyCalledTests._callbackcOsdS)Nr)rrrHrrrrK�szAlreadyCalledTests._errbackcCs|�d�dS)NrL)r")rr�rrr�_call_1szAlreadyCalledTests._call_1cCs|�d�dS)NZtwice)r")rr�rrr�_call_2szAlreadyCalledTests._call_2cCs|�t�t���dS)N)r`rrr1)rr�rrr�_err_1szAlreadyCalledTests._err_1cCs|�t�t���dS)N)r`rrr1)rr�rrr�_err_2szAlreadyCalledTests._err_2cCs8t��}|�|j|j�|�|�|�tj|j|�dS)N)	r
rMr_rIrKr&�assertRaises�AlreadyCalledErrorr')rr�rrr�testAlreadyCalled_CC
s
z'AlreadyCalledTests.testAlreadyCalled_CCcCs8t��}|�|j|j�|�|�|�tj|j|�dS)N)	r
rMr_rIrKr&r*r+r))rr�rrr�testAlreadyCalled_CEs
z'AlreadyCalledTests.testAlreadyCalled_CEcCs8t��}|�|j|j�|�|�|�tj|j|�dS)N)	r
rMr_rIrKr(r*r+r))rr�rrr�testAlreadyCalled_EEs
z'AlreadyCalledTests.testAlreadyCalled_EEcCs8t��}|�|j|j�|�|�|�tj|j|�dS)N)	r
rMr_rIrKr(r*r+r')rr�rrr�testAlreadyCalled_ECs
z'AlreadyCalledTests.testAlreadyCalled_ECcCsHd}x0|D](}|�d|�r
|�d|�r
|d7}q
W|�||k�dS)Nrz %s:z %sr)r>�endswithr*)rZlinetype�func�linesr��count�linerrr�_count#s
zAlreadyCalledTests._countcCs�|jd�d�}|�d||d�|�dd|d�|�dd|d�|�dd|d�|�dd|d�|�d	||d�|�d	||d�dS)
Nr�
�Crr&r'r(r)�I)r�splitr5)rry�callerZinvoker1Zinvoker2r2rrr�_check+szAlreadyCalledTests._checkc
Cstt��}|�|j|j�|�|�y|�|�Wn4tjk
rd}z|�|ddd�Wdd}~XYnX|�	d�dS)N�testAlreadyCalledDebug_CCr&r'z2second callback failed to raise AlreadyCalledError)
r
rMr_rIrKr&r'r+r;r�)rr�ryrrrr<9s
"z,AlreadyCalledTests.testAlreadyCalledDebug_CCc
Cstt��}|�|j|j�|�|�y|�|�Wn4tjk
rd}z|�|ddd�Wdd}~XYnX|�	d�dS)N�testAlreadyCalledDebug_CEr&r)z1second errback failed to raise AlreadyCalledError)
r
rMr_rIrKr&r)r+r;r�)rr�ryrrrr=Ds
"z,AlreadyCalledTests.testAlreadyCalledDebug_CEc
Cstt��}|�|j|j�|�|�y|�|�Wn4tjk
rd}z|�|ddd�Wdd}~XYnX|�	d�dS)N�testAlreadyCalledDebug_ECr(r'z2second callback failed to raise AlreadyCalledError)
r
rMr_rIrKr(r'r+r;r�)rr�ryrrrr>Os
"z,AlreadyCalledTests.testAlreadyCalledDebug_ECc
Cstt��}|�|j|j�|�|�y|�|�Wn4tjk
rd}z|�|ddd�Wdd}~XYnX|�	d�dS)N�testAlreadyCalledDebug_EEr(r)z1second errback failed to raise AlreadyCalledError)
r
rMr_rIrKr(r)r+r;r�)rr�ryrrrr?Zs
"z,AlreadyCalledTests.testAlreadyCalledDebug_EEc
Cszt�d�t��}|�|j|j�|�|�y|�|�Wn0tjk
rj}z|�	|j
�Wdd}~XYnX|�d�dS)NFz2second callback failed to raise AlreadyCalledError)r
rErMr_rIrKr&r'r+r�rr�)rr�ryrrr�testNoDebugginges

z"AlreadyCalledTests.testNoDebuggingcCslt�d�t��}|�dd��t�d�|�d�t�d�t��}|�d�t�d�|�dd��dS)NFcSsdS)Nr)r�rrrrzwr{z8AlreadyCalledTests.testSwitchDebugging.<locals>.<lambda>TcSsdS)Nr)r�rrrrzr{)r
rErM�addBothr")rr�rrr�testSwitchDebuggingrs





z&AlreadyCalledTests.testSwitchDebuggingN)rrrrGr%rIrKr&r'r(r)r,r-r.r/r5r;r<r=r>r?r@rBrrrrr#�s(
r#c@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'S)(�DeferredCancellerTestscCsd|_d|_d|_d|_dS)Nr)rArBrC�cancellerCallCount)rrrrrG�szDeferredCancellerTests.setUpcCs|�|jd�dS)N)rr)rrD)rrrrr%�szDeferredCancellerTests.tearDowncCs
||_|S)N)rA)r�datarrrrI�sz DeferredCancellerTests._callbackcCs
||_dS)N)rC)rrErrrrJ�sz!DeferredCancellerTests._callback2cCs
||_dS)N)rB)rrErrrrK�szDeferredCancellerTests._errbackcCsBt��}|�|j|j�|��|�|jjtj	�|�
|j�dS)z
        A L{defer.Deferred} without a canceller must errback with a
        L{defer.CancelledError} and not callback.
        N)r
rMr_rIrKrr(rBrmrrOrA)rr�rrr�test_noCanceller�s
z'DeferredCancellerTests.test_noCancellercCsTt��}|�|j|j�|��|�d�|�tj|jd�|�tj|j	t
��dS)z�
        A L{defer.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
rMr_rIrKrr"r*r+r`r�)rr�rrr�!test_raisesAfterCancelAndCallback�s
z8DeferredCancellerTests.test_raisesAfterCancelAndCallbackcCsVt��}|�|j|j�|��|�t��|�tj	|j
d�|�tj	|jt��dS)z�
        A L{defer.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
rMr_rIrKrr`r�r*r+r")rr�rrr� test_raisesAfterCancelAndErrback�sz7DeferredCancellerTests.test_raisesAfterCancelAndErrbackcCsJt��}|�|j|j�|��|j}|�d�|��|�||j�dS)z�
        A L{Deferred} without a canceller, when cancelled and then
        callbacked, ignores multiple cancels thereafter.
        N)	r
rMr_rIrKrrBr"r4)rr��currentFailurerrr�5test_noCancellerMultipleCancelsAfterCancelAndCallback�s
zLDeferredCancellerTests.test_noCancellerMultipleCancelsAfterCancelAndCallbackcCspt��}|�|j|j�|��|�|jjtj	�|j}|�
t��|�|jjtj	�|��|�||j�dS)z�
        A L{defer.Deferred} without a canceller, when cancelled and then
        errbacked, ignores multiple cancels thereafter.
        N)
r
rMr_rIrKrr(rBrmrr`rr4)rr�rIrrr�4test_noCancellerMultipleCancelsAfterCancelAndErrback�szKDeferredCancellerTests.test_noCancellerMultipleCancelsAfterCancelAndErrbackcCsRt��}|�|j|j�|��|�|jjtj	�|j}|��|�
||j�dS)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
rMr_rIrKrr(rBrmrr4)rr�rIrrr�test_noCancellerMultipleCancel�sz5DeferredCancellerTests.test_noCancellerMultipleCancelcsp�fdd�}tj|d�}|��j�j�|�����jjtj	��j}|����
|�j����jd�dS)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.
        cs�jd7_dS)Nr)rD)r�)rrrrszCDeferredCancellerTests.test_cancellerMultipleCancel.<locals>.cancel)�	cancellerrN)r
rMr_rIrKrr(rBrmrr4rD)rrr�rIr)rr�test_cancellerMultipleCancel�sz3DeferredCancellerTests.test_cancellerMultipleCancelcsz�fdd�}tj|d�}|��j�j�|�����jd����jj	tj
���tj|j
d���tj|jt��dS)z�
        Verify that a L{defer.Deferred} calls its specified canceller when
        it is cancelled, and that further call/errbacks raise
        L{defer.AlreadyCalledError}.
        cs�jd7_dS)Nr)rD)r�)rrrrsz;DeferredCancellerTests.test_simpleCanceller.<locals>.cancel)rMrN)r
rMr_rIrKrr(rDrBrmrr*r+r"r`r�)rrr�r)rr�test_simpleCancellersz+DeferredCancellerTests.test_simpleCancellercs6��fdd�}tj|d�����j�j����dS)zQ
        Verify that a canceller is given the correct deferred argument.
        cs��|��dS)N)r4)rt)r�rrrr+sz8DeferredCancellerTests.test_cancellerArg.<locals>.cancel)rMN)r
rMr_rIrKr)rrr)r�rr�test_cancellerArg'sz(DeferredCancellerTests.test_cancellerArgcsf�fdd�}tj|d�}|��j�j�|�d�|�����jd���	�j
����jd�dS)zo
        Test that cancelling a deferred after it has been callbacked does
        not cause an error.
        cs�jd7_|�t��dS)Nr)rDr`r)r�)rrrr7sz?DeferredCancellerTests.test_cancelAfterCallback.<locals>.cancel)rMzbiff!rN)r
rMr_rIrKr"rr(rDrOrBrA)rrr�r)rr�test_cancelAfterCallback2s
z/DeferredCancellerTests.test_cancelAfterCallbackcsj�fdd�}tj|d�}|��j�j�|�t��|�����j	d����j
jt����j
�dS)z�
        Test that cancelling a L{Deferred} after it has been errbacked does
        not result in a L{defer.CancelledError}.
        cs�jd7_|�t��dS)Nr)rDr`r)r�)rrrrHsz>DeferredCancellerTests.test_cancelAfterErrback.<locals>.cancel)rMrN)r
rMr_rIrKr`rrr(rDrBrmrOrA)rrr�r)rr�test_cancelAfterErrbackCsz.DeferredCancellerTests.test_cancelAfterErrbackcsR�fdd�}tj|d�}|��j�j�|�����jd����jj	t
�dS)z?
        Test a canceller which errbacks its deferred.
        cs�jd7_|�t��dS)Nr)rDr`r)r�)rrrrXszADeferredCancellerTests.test_cancellerThatErrbacks.<locals>.cancel)rMrN)r
rMr_rIrKrr(rDrBrmr)rrr�r)rr�test_cancellerThatErrbacksTsz1DeferredCancellerTests.test_cancellerThatErrbackscs\�fdd�}tj|d�}|��j�j�|�����jd����jd���	�j
�dS)z<
        Test a canceller which calls its deferred.
        cs�jd7_|�d�dS)Nrzhello!)rDr")r�)rrrrfszBDeferredCancellerTests.test_cancellerThatCallbacks.<locals>.cancel)rMrzhello!N)r
rMr_rIrKrr(rDrArOrB)rrr�r)rr�test_cancellerThatCallbacksbsz2DeferredCancellerTests.test_cancellerThatCallbackscs��fdd�}�fdd�}tj|d��tj|d�}|�d�|��fdd��|��|��j�j����j	d	����j
jtj�dS)
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.
        cs�jd7_dS)Nr)rD)r�)rrr�innerCancelwszEDeferredCancellerTests.test_cancelNestedDeferred.<locals>.innerCancelcs��d�dS)NF)r*)r�)rrrrysz@DeferredCancellerTests.test_cancelNestedDeferred.<locals>.cancel)rMNcs�S)Nr)rE)r�rrrzr{zBDeferredCancellerTests.test_cancelNestedDeferred.<locals>.<lambda>r)
r
rMr"rNrr_rIrKr(rDrBrmr)rrUrr�r)r�rr�test_cancelNestedDeferredqs
z0DeferredCancellerTests.test_cancelNestedDeferredN)rrrrGr%rIrJrKrFrGrHrJrKrLrNrOrPrQrRrSrTrVrrrrrC�s&rCc@sheZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�ZdS)�LogTestsz+
    Test logging of unhandled errors.
    cCsg|_t�|jj�dS)z<
        Add a custom observer to observer logging.
        N)�crr=r')rrrrrG�szLogTests.setUpcCst�|jj�dS)z&
        Remove the observer.
        N)rZremoveObserverrXr')rrrrr%�szLogTests.tearDowncCsdd�|jD�S)NcSsg|]}|dr|�qS)ZisErrorr)rryrrrr�sz*LogTests._loggedErrors.<locals>.<listcomp>)rX)rrrr�
_loggedErrors�szLogTests._loggedErrorscCs8|��}|�t|�d�|dd�t�|�t�dS)zV
        Check the output of the log observer to see if the error is present.
        r]rrN)rYr(r)r�r�r3)rZc2rrrr;�szLogTests._checkcCs,t���dd���d�t��|��dS)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.
        cSsddS)Nrrr)r�rrrrz�r{z(LogTests.test_errorLog.<locals>.<lambda>rN)r
rMrNr"�gc�collectr;)rrrr�
test_errorLog�szLogTests.test_errorLogcCs"dd�}|�t��|��dS)zD
        Same as L{test_errorLog}, but with an inner frame.
        cSs$t��}|�dd��|�d�dS)NcSsddS)Nrrr)r�rrrrz�r{z`LogTests.test_errorLogWithInnerFrameRef.<locals>._subErrorLogWithInnerFrameRef.<locals>.<lambda>r)r
rMrNr")r�rrr�_subErrorLogWithInnerFrameRef�szNLogTests.test_errorLogWithInnerFrameRef.<locals>._subErrorLogWithInnerFrameRefN)rZr[r;)rr]rrr�test_errorLogWithInnerFrameRef�sz'LogTests.test_errorLogWithInnerFrameRefcCs"dd�}|�t��|��dS)zQ
        Same as L{test_errorLogWithInnerFrameRef}, plus create a cycle.
        cSs.t��}|�|fdd��||_|�d�dS)NcSsddS)Nrrr)r�r�rrrrz�r{zdLogTests.test_errorLogWithInnerFrameCycle.<locals>._subErrorLogWithInnerFrameCycle.<locals>.<lambda>r)r
rMrN�_dr")r�rrr�_subErrorLogWithInnerFrameCycle�szRLogTests.test_errorLogWithInnerFrameCycle.<locals>._subErrorLogWithInnerFrameCycleN)rZr[r;)rr`rrr� test_errorLogWithInnerFrameCycle�sz)LogTests.test_errorLogWithInnerFrameCyclecCsjt���dd���d�t��|��|�dt|j	��t
�|j	d�}d}|�|�
|�d�|��dS)	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.
        cSsddS)Nrrr)r�rrrrz�r{z.LogTests.test_errorLogNoRepr.<locals>.<lambda>rr]���zUnhandled Error
Traceback z%Expected message starting with: {0!r}N)r
rMrNr"rZr[r;r(r)rXrr:r*r>�format)r�msgr�rrr�test_errorLogNoRepr�szLogTests.test_errorLogNoReprcCs`dd�}|�t��|��|�dt|j��t�|jd�}d}|�|�	|�d�
|��dS)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.
        cSs*t��}d|_|�dd��|�d�dS)NTcSsddS)Nrrr)r�rrrrz�r{z?LogTests.test_errorLogDebugInfo.<locals>.doit.<locals>.<lambda>r)r
rM�debugrNr")r�rrr�doit�sz-LogTests.test_errorLogDebugInfo.<locals>.doitr]rbz
(debug:  Iz%Expected message starting with: {0!r}N)rZr[r;r(r)rXrr:r*r>rc)rrgrdr�rrr�test_errorLogDebugInfo�szLogTests.test_errorLogDebugInfocCs�t��}|�dd��|�d�g}g}|�|j|j�|�|g�|�t|�d�|d�t	�~~~t
��|�|��g�dS)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.
        cSst�td��S)NZzoop)r
r�r1)r�rrrrzr{z3LogTests.test_chainedErrorCleanup.<locals>.<lambda>Nrr)
r
rMrNr"r_r'r(r)r�r�rZr[rY)rr�r�r8rrr�test_chainedErrorCleanup�s
z!LogTests.test_chainedErrorCleanupcsLt�td��}t���|��fdd��d�}t��|�|��g�dS)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.
        zoh nocs�S)Nr)r�)�goodrrrz!r{z6LogTests.test_errorClearedByChaining.<locals>.<lambda>N)	r
r�r�rMr&rZr[r(rY)r�badr)rjr�test_errorClearedByChainingsz$LogTests.test_errorClearedByChainingN)rrrrrGr%rYr;r\r^rarerhrirlrrrrrW�s	

rWc@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�DeferredListEmptyTestscCs
d|_dS)Nr)�callbackRan)rrrrrG+szDeferredListEmptyTests.setUpcCst�g�}|�|j�dS)zTesting empty DeferredList.N)r
r^rN�cb_empty)rrgrrr�testDeferredListEmpty.s
z,DeferredListEmptyTests.testDeferredListEmptycCsd|_|�g|�dS)Nr)rnr()r�resrrrro3szDeferredListEmptyTests.cb_emptycCs|�|jd�dS)NzCallback was never run.)r*rn)rrrrr%7szDeferredListEmptyTests.tearDownN)rrrrGrpror%rrrrrm*srmc@sleZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�ZdS)�OtherPrimitivesTestscCs|jd7_dS)Nr)�counter)rr6rrr�_incr=szOtherPrimitivesTests._incrcCs
d|_dS)Nr)rs)rrrrrG@szOtherPrimitivesTests.setUpcs�t��}|����j���|j����jd�|����j���|j����jd�|�	���|j����jd�|�	���
|j����jd���t|j
�t�}t�}t����fdd�}|j
|�|d�}��|j����j|�|��fdd��|����j���|j����jd���|����j|���|j����jd�|����fd	d��}|�����jjtj�|�	���
|j�dS)
Nrr]cs
||_�S)N)r�)rr�)�controlDeferredrr�helper[sz-OtherPrimitivesTests.testLock.<locals>.helper)rr�cst�d|�S)Nr6)r�)r�)rrrrzcr{z/OtherPrimitivesTests.testLock.<locals>.<lambda>r�cst�d|�S)Nr6)r�)r�)rrrrznr{)r
�DeferredLock�acquirerNrtr*�lockedr(rs�releaser�r*r��runr�rMr�r"r6rArrmr)r�lockZfirstUniqueZsecondUniquerv�resultDeferredr�r)rurr�testLockCsD
zOtherPrimitivesTests.testLockcs2�fdd�}t��}|��}|�|�|��dS)z�
        When canceling a L{Deferred} from a L{DeferredLock} that already
        has the lock, the cancel should have no effect.
        cs��d�dS)NzUnexpected errback call!)r�)r�)rrr�_failOnErrback{szIOtherPrimitivesTests.test_cancelLockAfterAcquired.<locals>._failOnErrbackN)r
rwrxr&r)rrr|r�r)rr�test_cancelLockAfterAcquiredvs

z1OtherPrimitivesTests.test_cancelLockAfterAcquiredcCs2t��}|��|��}|��|�|tj�dS)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
rwrxrr/r)rr|r�rrr�test_cancelLockBeforeAcquired�s
z2OtherPrimitivesTests.test_cancelLockBeforeAcquiredcs�d}t�|�}t����fdd�}g}t�}|j|||d�}|�|j�|�|j�|�|g�|�|j	|���
d�|�|���|�|j
d�d|_
x4tdd|�D]"}|���|j�|�|j
|�q�Wg��fdd�}�fd	d
�}	|���||	�}
|
��|��dg�|���|j�|�|j
|�|��|�|j
|d�x2tdd|�D] }|��|�|j
|d��qZWdS)Nrcs
||_�S)N)�arg)rr�)rurrrv�sz2OtherPrimitivesTests.testSemaphore.<locals>.helper)rr�rrcs��d�dS)NF)r')r�)r�rrr��sz0OtherPrimitivesTests.testSemaphore.<locals>.failcs��d�dS)NT)r')r�)r�rrr��sz3OtherPrimitivesTests.testSemaphore.<locals>.succeedT)r
�DeferredSemaphorerMr�r{rNr'rtr(r�r"rOrrsrrxr_rrz)r�N�semrvr�ZuniqueObjectr}�ir�r�r�r)rur�r�
testSemaphore�s>

z"OtherPrimitivesTests.testSemaphorecCs$|�ttjd�|�ttjd�dS)zz
        If the token count passed to L{DeferredSemaphore} is less than one
        then L{ValueError} is raised.
        rrbN)r*r�r
r�)rrrr�test_semaphoreInvalidTokens�sz0OtherPrimitivesTests.test_semaphoreInvalidTokenscs4�fdd�}t�d�}|��}|�|�|��dS)z�
        When canceling a L{Deferred} from a L{DeferredSemaphore} that
        already has the semaphore, the cancel should have no effect.
        cs��d�dS)NzUnexpected errback call!)r�)r�)rrrr�szNOtherPrimitivesTests.test_cancelSemaphoreAfterAcquired.<locals>._failOnErrbackrN)r
r�rxr&r)rrr�r�r)rr�!test_cancelSemaphoreAfterAcquired�s


z6OtherPrimitivesTests.test_cancelSemaphoreAfterAcquiredcCs4t�d�}|��|��}|��|�|tj�dS)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.
        rN)r
r�rxrr/r)rr�r�rrr�"test_cancelSemaphoreBeforeAcquired�s

z7OtherPrimitivesTests.test_cancelSemaphoreBeforeAcquiredc	Cs�d\}}t�||�}g}x t|�D]}|���|j�q"W|�tj|j�x2t|�D]&}|�|�|�	|t
t|d���qTWx2t|�D]&}|�||�|�	|t
t|���q�W|�tj|jd�g}x>t|�D]2}|���|j�|�	|t
t|||d���q�Wt��}g}x"t|�D]}|���|j��qWxt|�D]}|�|��qBW|�	|t
t|���tjdd�}|�tj|jd�tjdd�}|�tj|j�dS)N)r]r]rr)�size)Zbacklog)r
�
DeferredQueuer�getrNr'r*ZQueueUnderflow�putr(�listZ
QueueOverflow)rr��M�queueZgottenr�rrr�	testQueue�s8
"zOtherPrimitivesTests.testQueuecs<�fdd�}t��}|��}|�|�|�d�|��dS)z�
        When canceling a L{Deferred} from a L{DeferredQueue} that already has
        a result, the cancel should have no effect.
        cs��d�dS)NzUnexpected errback call!)r�)r�)rrrr
	szPOtherPrimitivesTests.test_cancelQueueAfterSynchronousGet.<locals>._failOnErrbackN)r
r�r�r&r�r)rrr�r�r)rr�#test_cancelQueueAfterSynchronousGet	s

z8OtherPrimitivesTests.test_cancelQueueAfterSynchronousGetcsbt������}|����|tj���fdd�}|�|�g}|�|j���t	|�d�dS)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.
        cs��d������jd�S)N)r�r�rNr4)�ignore)r�rrrrW"	s
z9OtherPrimitivesTests.test_cancelQueueAfterGet.<locals>.cbrN)
r
r�r�rr/rrNr'r(r))rr�rW�doner)r�rr�test_cancelQueueAfterGet	s
z-OtherPrimitivesTests.test_cancelQueueAfterGetN)rrrrtrGr~r�r�r�r�r�r�r�r�r�rrrrrr<s3

.	
&rrc@sXeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�ZdS)�DeferredFilesystemLockTestsz8
    Test the behavior of L{DeferredFilesystemLock}
    cCs"t�|_tj|��|jd�|_dS)N)Z	scheduler)r�clockr
�DeferredFilesystemLock�mktempr|)rrrrrG3	s
z!DeferredFilesystemLockTests.setUpcCs|jjdd�}|S)zI
        Test that the lock can be acquired when no lock is held
        r)�timeout)r|�deferUntilLocked)rr�rrr�test_waitUntilLockedWithNoLock9	sz:DeferredFilesystemLockTests.test_waitUntilLockedWithNoLockcCsB|�|j���|jjdd�}|�|tj�|j�dgd�|S)zs
        Test that the lock can not be acquired when the lock is held
        for longer than the timeout.
        g@)r�rr�)r*r|r��
assertFailurer
�TimeoutErrorr��pump)rr�rrr�%test_waitUntilLockedWithTimeoutLockedB	s
zADeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutLockedcs\�fdd�}���j����j�d�jj��jjdd�}|�|��j�dgd�|S)z|
        Test that a lock can be acquired while a lock is held
        but the lock is unlocked before our timeout.
        cs|�tj���d�dS)NzShould not have timed out)r�r
r�r�)r )rrr�	onTimeoutV	szVDeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutUnlocked.<locals>.onTimeoutrr�)r�)r*r|r��	callLater�unlockr�r&r�)rr�r�r)rr�'test_waitUntilLockedWithTimeoutUnlockedQ	s
zCDeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutUnlockedcCs t�|���}|�|jt�dS)zE
        Test that the default scheduler is set up properly.
        N)r
r�r�r(Z
_schedulerr)rr|rrr�test_defaultSchedulere	sz1DeferredFilesystemLockTests.test_defaultSchedulercCsN|j��|j�d|jj�|j��}|j��}|�|tj�|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
ZAlreadyTryingToLockError�advance)rr�r|rrr�test_concurrentUsagen	s


z0DeferredFilesystemLockTests.test_concurrentUsagecsL�fdd�}�j���j�d�jj��j��}|�|��j�d�|S)zO
        Test that a DeferredFilesystemLock can be used multiple times
        cs�j���j��}|S)N)r|r�r�)r�r�)rrr�lockAquired�	s

zDDeferredFilesystemLockTests.test_multipleUsages.<locals>.lockAquiredr)r|r�r�r�r�rNr�)rr�r�r)rr�test_multipleUsages�	s


z/DeferredFilesystemLockTests.test_multipleUsagescCsR|j��|j��}|jj}|��|�|���|�|jj�|�|tj	�dS)z�
        When cancelling a L{defer.Deferred} returned by
        L{defer.DeferredFilesystemLock.deferUntilLocked}, the
        L{defer.DeferredFilesystemLock._tryLockCall} is cancelled.
        N)
r|r�Z_tryLockCallrr��activerOr�r
r)rr#ZtryLockCallrrr�test_cancelDeferUntilLocked�	s

z7DeferredFilesystemLockTests.test_cancelDeferUntilLockedcCsV|j��|jjdd�}|jj}|��|�|���|�|jj�|�|tj	�dS)z�
        When cancel a L{defer.Deferred} returned by
        L{defer.DeferredFilesystemLock.deferUntilLocked}, if the timeout is
        set, the timeout call will be cancelled.
        r)r�N)
r|r�Z_timeoutCallrr�r�rOr�r
r)rr#ZtimeoutCallrrr�&test_cancelDeferUntilLockedWithTimeout�	s
zBDeferredFilesystemLockTests.test_cancelDeferUntilLockedWithTimeoutN)
rrrrrGr�r�r�r�r�r�r�r�rrrrr�.	s		r�cCsdS)zj
    Private function to be used to pass as an alternate onTimeoutCancel value
    to timeoutDeferred
    �
OVERRIDDENr)�v�trrr�
_overrideFunc�	sr�c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&S)'�DeferredAddTimeoutTestsz7
    Tests for the function L{Deferred.addTimeout}
    cCs<t���dt���dd��}|�d�|�d|�|��dS)z�
        L{defer.Deferred.addTimeout} returns its own L{defer.Deferred} so it
        can be called in a callback chain.
        r�cSsdS)Nr�r)r�rrrrz�	r{z?DeferredAddTimeoutTests.test_timeoutChainable.<locals>.<lambda>Nr�)r
rM�
addTimeoutrrNr"r(r�)rr�rrr�test_timeoutChainable�	s
z-DeferredAddTimeoutTests.test_timeoutChainablecs�t�}t��}|�d|�t���dg���fdd�}|�|�|�d�|�d�d�|��dd�|�d�|�	��dS)z�
        The L{defer.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�Ncs|�d<�S)Nrr)r�)�dCallbacked�
innerDeferredrr�
onCallback�	szKDeferredAddTimeoutTests.test_successResultBeforeTimeout.<locals>.onCallbackr�rr�)
rr
rMr�rNr"�assertIsNotr(r�r�)rr�r�r�r)r�r�r�test_successResultBeforeTimeout�	s


z7DeferredAddTimeoutTests.test_successResultBeforeTimeoutcs�t�}t��}|jd|td�t���dg���fdd�}|�|�|�d�|�d�d�|��dd�|�	d�|�
��dS)	a
        The L{defer.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�)�onTimeoutCancelNcs|�d<�S)Nrr)r�)r�r�rrr��	szQDeferredAddTimeoutTests.test_successResultBeforeTimeoutCustom.<locals>.onCallbackr�rr�)rr
rMr�r�rNr"r�r(r�r�)rr�r�r�r)r�r�r�%test_successResultBeforeTimeoutCustom�	s


z=DeferredAddTimeoutTests.test_successResultBeforeTimeoutCustomcs�t�}t��}|�d|�t���dg�td�}��fdd�}|�|�|�|�|��dtj	�|�
�dj|�|�d�|�
��dS)z�
        The L{defer.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�cs|�d<�S)Nrr)r )�
dErrbackedr�rr�	onErrback
szDDeferredAddTimeoutTests.test_failureBeforeTimeout.<locals>.onErrbackrr�)rr
rMr�r�r&r`r�rrr4r,r�r�)rr�r�r5r�r)r�r�r�test_failureBeforeTimeout
s


z1DeferredAddTimeoutTests.test_failureBeforeTimeoutcs�t�}t��}|jd|td�t���dg�td�}��fdd�}|�|�|�|�|��dt	j
�|��dj|�|�
d�|���dS)	a
        The L{defer.Deferred} errbacks with the failure if it fails before the
        timeout, even if using a custom C{onTimeoutCancel} function.
        No cancellation happens after the errback either, which could also
        cancel inner deferreds.
        r�)r�Nr�cs|�d<�S)Nrr)r )r�r�rrr�@
szJDeferredAddTimeoutTests.test_failureBeforeTimeoutCustom.<locals>.onErrbackrr�)rr
rMr�r�r�r&r`r�rrr4r,r�r�)rr�r�r5r�r)r�r�r�test_failureBeforeTimeoutCustom/
s


z7DeferredAddTimeoutTests.test_failureBeforeTimeoutCustomcCs@t�}t��}|�d|�|�|�|�d�|�|tj�dS)z�
        The L{defer.Deferred} by default errbacks with a L{defer.TimeoutError}
        if it times out before callbacking or errbacking.
        r�r�N)rr
rMr�r�r�r�r�)rr�r�rrr�
test_timedOutR
s

z%DeferredAddTimeoutTests.test_timedOutcCsHt�}t��}|jd|td�|�|�|�d�|�d|�|��dS)a=
        If a custom C{onTimeoutCancel] function is provided, the
        L{defer.Deferred} returns the custom function's return value if the
        L{defer.Deferred} times out before callbacking or errbacking.
        The custom C{onTimeoutCancel} function can return a result instead of
        a failure.
        r�)r�r�r�N)	rr
rMr�r�r�r�r(r�)rr�r�rrr�test_timedOutCustoma
s

z+DeferredAddTimeoutTests.test_timedOutCustomcCsJt�}t�dd��}|�d|�|�|�|�d�|�|�|�d�dS)z�
        If a cancellation function is provided when the L{defer.Deferred} is
        initialized, the L{defer.Deferred} returns the cancellation value's
        non-failure return value when the L{defer.Deferred} times out.
        cSs
|�d�S)NzI was cancelled!)r")rXrrrrzz
r{zLDeferredAddTimeoutTests.test_timedOutProvidedCancelSuccess.<locals>.<lambda>r�r�zI was cancelled!N)rr
rMr�r�r�r(r�)rr�r�rrr�"test_timedOutProvidedCancelSuccesss
s

z:DeferredAddTimeoutTests.test_timedOutProvidedCancelSuccesscs^t�}td��t��fdd��}|�d|�|�|�|�d�|�|t�}|�|j	��dS)z�
        If a cancellation function is provided when the L{defer.Deferred} is
        initialized, the L{defer.Deferred} returns the cancellation value's
        non-L{CanceledError} failure when the L{defer.Deferred} times out.
        zwhat!cs
|���S)N)r`)rX)r5rrrz�
r{zLDeferredAddTimeoutTests.test_timedOutProvidedCancelFailure.<locals>.<lambda>r�r�N)
rr�r
rMr�r�r�r�r4r,)rr�r�r r)r5r�"test_timedOutProvidedCancelFailure�
s

z:DeferredAddTimeoutTests.test_timedOutProvidedCancelFailurecs�t�}t��}|�d|�t���dg���fdd�}|�|�|��|��dtj�|�	�dj
tj�|�d�|�
��dS)a3
        If the L{defer.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�Ncs|�d<�S)Nrr)r )�	dCanceledr�rrr��
szCDeferredAddTimeoutTests.test_cancelBeforeTimeout.<locals>.onErrbackrr�)rr
rMr�r&rr�rrr4rmrr�r�)rr�r�r�r)r�r�r�test_cancelBeforeTimeout�
s

z0DeferredAddTimeoutTests.test_cancelBeforeTimeoutcs�t�}t��}|jd|td�t���dg���fdd�}|�|�|��|��dtj	�|�
�djtj�|�
d�|���dS)a2
        If the L{defer.Deferred} is manually cancelled before the timeout, it
        is not re-cancelled (no L{AlreadyCancelled} error, and also no
        canceling of inner deferreds), and the custom C{onTimeoutCancel}
        function is not called, preserving the original L{CancelledError}.
        r�)r�Ncs|�d<�S)Nrr)r )r�r�rrr��
szIDeferredAddTimeoutTests.test_cancelBeforeTimeoutCustom.<locals>.onErrbackrr�)rr
rMr�r�r&rr�rrr4rmrr�r�)rr�r�r�r)r�r�r�test_cancelBeforeTimeoutCustom�
s

z6DeferredAddTimeoutTests.test_cancelBeforeTimeoutCustomcCsNt�}t�dd��}|jd|td�|�|�|�d�|�d|�|��dS)z{
        A custom translation function can handle a L{defer.Deferred} with a
        custom cancellation function.
        cSs|�td��S)Nzwhat!)r`r�)rXrrrrz�
r{zVDeferredAddTimeoutTests.test_providedCancelCalledBeforeTimeoutCustom.<locals>.<lambda>r�)r�r�r�N)	rr
rMr�r�r�r�r(r�)rr�r�rrr�,test_providedCancelCalledBeforeTimeoutCustom�
s

zDDeferredAddTimeoutTests.test_providedCancelCalledBeforeTimeoutCustomcsxt�}t��}dg��fdd�}|�|�|�d|�|�d�|��dtj�|��dj	tj
�|�|tj�dS)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.
        Ncs|�d<|S)Nrr)r )r�rrr`�
szGDeferredAddTimeoutTests.test_errbackAddedBeforeTimeout.<locals>.errbackr�r�r)
rr
rMr&r�r�r�rrr,rr�r�)rr�r�r`r)r�r�test_errbackAddedBeforeTimeout�
s

z6DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutcstt�}t��}dg��fdd�}|�|�|�d|�|�d�|��dtj�|��dj	tj
�|�|�dS)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.
        Ncs|�d<|�tj�dS)Nr)r�r
r)r )r�rrr`sz]DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellation.<locals>.errbackr�r�r)rr
rMr&r�r�r�rrr,rr�)rr�r�r`r)r�r�4test_errbackAddedBeforeTimeoutSuppressesCancellations

zLDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationcs~t�}t��}dg��fdd�}|�|�|�d|t�|�d�|��dtj	�|��dj
tj�|�d|�
|��dS)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}.
        Ncs|�d<|S)Nrr)r )r�rrr`)szMDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutCustom.<locals>.errbackr�r�rr�)rr
rMr&r�r�r�r�rrr,rr(r�)rr�r�r`r)r�r�$test_errbackAddedBeforeTimeoutCustoms

z<DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutCustomcs~t�}t��}dg��fdd�}|�|�|�d|t�|�d�|��dtj	�|��dj
tj�|�d|�
|��dS)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}.
        Ncs|�d<dS)Nrr)r )r�rrr`DszcDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationCustom.<locals>.errbackr�r�rr�)rr
rMr&r�r�r�r�rrr,rr(r�)rr�r�r`r)r�r�:test_errbackAddedBeforeTimeoutSuppressesCancellationCustom8s

zRDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationCustomcstt�}d�t��fdd��}dg��fdd�}|�|�|�d|�|�d�|��d	��|��|�|��dS)
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�cs
|���S)N)r")r�)r�rrrz\r{zSDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout.<locals>.<lambda>Ncs|�d<|S)Nrr)r,)r�rrr"`szSDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout.<locals>.callbackr�r�r)	rr
rMrNr�r�r(r4r�)rr�r�r"r)r�r�r�)test_callbackAddedToCancelerBeforeTimeoutRs

zADeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutcsxt�}d�t��fdd��}dg��fdd�}|�|�|jd|td�|�d	�|��d
��|�d|�|��dS)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�cs
|���S)N)r")r�)r�rrrzyr{zYDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom.<locals>.<lambda>Ncs|�d<|S)Nrr)r,)r�rrr"}szYDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom.<locals>.callbackr�)r�r�rr�)	rr
rMrNr�r�r�r(r�)rr�r�r"r)r�r�r�/test_callbackAddedToCancelerBeforeTimeoutCustomns	

zGDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustomN)rrrrr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr��	s&
!""#""r�c@s eZdZdZdd�Zdd�ZdS)�EnsureDeferredTestsz=
    Tests for L{twisted.internet.defer.ensureDeferred}.
    cCs"t��}t�|�}|�||�dS)zQ
        L{defer.ensureDeferred} will pass through a Deferred unchanged.
        N)r
rM�ensureDeferredr4)rr�r|rrr�test_passesThroughDeferreds�s
z/EnsureDeferredTests.test_passesThroughDeferredsc	Cs$|�t��t�d�WdQRXdS)zx
        Passing L{defer.ensureDeferred} a non-coroutine and a non-Deferred will
        raise a L{ValueError}.
        Z	somethingN)r*r�r
r�)rrrr�'test_willNotAllowNonDeferredOrCoroutine�sz;EnsureDeferredTests.test_willNotAllowNonDeferredOrCoroutineN)rrrrr�r�rrrrr��s	r�c@seZdZdZdd�ZdS)�TimeoutErrorTestsz1
    L{twisted.internet.defer} timeout code.
    cCsjt��}t�|�|�|tj�|�|jg�}|�t|�d�|�	|ddt
�|�|ddd�dS)zB
        L{twisted.internet.defer.timeout} is deprecated.
        rrr�r�z~twisted.internet.defer.timeout was deprecated in Twisted 17.1.0; please use twisted.internet.defer.Deferred.addTimeout insteadN)r
rMr�r�r�r��test_deprecatedTimeoutr(r)r4r�)rr#Z
warningsShownrrrr��s

z(TimeoutErrorTests.test_deprecatedTimeoutN)rrrrr�rrrrr��sr�cCs|�|j�|��dS)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)Z	call_soon�stopZrun_forever)�looprrr�callAllSoonCalls�sr�c@sHeZdZeZdd�Zdd�Zdd�Zdd�Zd	d
�Z	dd�Z
d
d�ZdS)�DeferredFutureAdapterTestscCs`t��}t�}|�|�}|�|��d�|�d�t|�|�|�|�d�|�|�	�d�dS)z�
        L{defer.Deferred.asFuture} returns a L{asyncio.Future} which fires when
        the given L{defer.Deferred} does.
        FrN)
r
rMr
�asFuturer(r�r"r�r�r6)rr�r��aFuturerrr�
test_asFuture�s

z(DeferredFutureAdapterTests.test_asFuturecsn�fdd��d�_t���}t�}|�|�}|��t|�|��jd�|�|�|�d�|�	t
|j�dS)z�
        L{defer.Deferred.asFuture} returns a L{asyncio.Future} which, when
        cancelled, will cancel the original L{defer.Deferred}.
        cs
d�_dS)NT)r�)�dprime)�cancelerrrr��szFDeferredFutureAdapterTests.test_asFutureCancelFuture.<locals>.cancelerFTN)r�r
rMr
r�rr�r(r�r*rr6)rr�r�r�r)r�r�test_asFutureCancelFuture�s

z4DeferredFutureAdapterTests.test_asFutureCancelFuturecCsVdd�}t�|�}t�}|�|�}|��t|�|�|�|�d�|�t	|j
�dS)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.
        cSs|�d�dS)Nr )r")r�rrrr��szGDeferredFutureAdapterTests.test_asFutureSuccessCancel.<locals>.cancelerN)r
rMr
r�rr�r(r�r*rr6)rr�r�r�r�rrr�test_asFutureSuccessCancel�s

z5DeferredFutureAdapterTests.test_asFutureSuccessCancelcCsPt��}t�t��}t�}|�|�}t|�|�|�t|�|�	t|j
�dS)z�
        L{defer.Deferred.asFuture} makes a L{asyncio.Future} fire with an
        exception when the given L{defer.Deferred} does.
        N)r
rMrrr�r
r�r�r`r*r6)rr�Z
theFailurer��futurerrr�test_asFutureFailure�s

z/DeferredFutureAdapterTests.test_asFutureFailurecCsNt�}t|d�}tj�|�}|�|�|�d�t|�|�|�	|�d�dS)z�
        L{defer.Deferred.fromFuture} returns a L{defer.Deferred} that fires
        when the given L{asyncio.Future} does.
        )r��N)
r
rr
rM�
fromFuturer��
set_resultr�r(r�)rr�r�r�rrr�test_fromFutures


z*DeferredFutureAdapterTests.test_fromFuturecCsNt�}t|d�}tj�|�}|��t|�|�t|j	�|�
|��t�dS)z�
        L{defer.Deferred.fromFuture} makes a L{defer.Deferred} fire with
        an L{asyncio.CancelledError} when the given
        L{asyncio.Future} is cancelled.
        )r�N)r
rr
rMr�rr�r*rr6r�r�)rr��	cancelledr�rrr�test_fromFutureFutureCancelleds
z9DeferredFutureAdapterTests.test_fromFutureFutureCancelledcCs^t�}t|d�}tj�|�}|��t|�|�|��d�|�	t
|j�|�|��
t
�dS)z�
        L{defer.Deferred.fromFuture} makes a L{defer.Deferred} which, when
        cancelled, cancels the L{asyncio.Future} it was created from.
        )r�TN)r
rr
rMr�rr�r(r�r*rr6r�r�)rr�r�r�rrr� test_fromFutureDeferredCancelled(s
z;DeferredFutureAdapterTests.test_fromFutureDeferredCancelledN)rrr�	asyncSkiprr�r�r�r�r�r�r�rrrrr��sr�)=r�
__future__rrr�rZr�r�Ztwisted.pythonrrrZtwisted.python.compatrrZ
twisted.trialr	Ztwisted.internetr
rZtwisted.internet.taskrZasyncior
rrr�Ztwisted.python.filepathr�__file__�parent�child�_pathrZ_gZexecfile�pathrZTestCaser�rr!r$r�r%r0ZSynchronousTestCaser@rr#rCrWrmrrr�r�r�r�r�r�r�rrrr�<module>sv
.$D	!s	S