%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/twisted/test/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/twisted/test/__pycache__/test_defer.cpython-312.pyc

�

Ϫ�f{=���dZddlmZddlZddlZddlZddlZddlZddlZddl	Z
ddlZddlZddl
mZmZmZmZddlmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"ddl#m$Z$m%Z%m&Z&ddl'm(Z(ddl)m*Z*dd	lm+Z+dd
l,m-Z-m.Z.ddl/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;ddl<m=Z=dd
l>m?Z?ddl@mAZAddlBmCZCddlDm	Z					d?d�ZEGd�deF�ZGd@d�ZHdAd�ZIe deF��ZJGd�d�ZKGd�de	j��ZMGd�de	j�eK�ZOGd�d�ZPdBd �ZQGd!�d"e	j��ZRGd#�d$e	j��ZSGd%�d&e	j��ZTGd'�d(e	j��ZUGd)�d*e	j��ZVGd+�d,e	j��ZWGd-�d.e	j�eK�ZXGd/�d0e	j��ZYdCd1�ZZGd2�d3e	j��Z[Gd4�d5e	j��Z\Gd6�d7e	j�eK�Z]dDd8�Z^Gd9�d:e	j��Z_Gd;�d<e	j��Z`Gd=�d>e	j��Zay)Ez+
Test cases for L{twisted.internet.defer}.
�)�annotationsN)�AbstractEventLoop�CancelledError�Future�new_event_loop)�Any�Callable�	Coroutine�Dict�	Generator�List�Mapping�NoReturn�Optional�Set�Tuple�Type�TypeVar�Union�cast)�assert_that�empty�equal_to)�given)�integers)�assert_type)�defer�reactor)�_T�Deferred�DeferredFilesystemLock�DeferredList�DeferredLock�
DeferredQueue�DeferredSemaphore�FailureGroup�_DeferredListResultListT�_DeferredListSingleResultT�ensureDeferred�race)�Clock)�log)�_PYPY��Failure)�unittestc�D��tj��d�fd��}|S)Nc�>���|i|��}tj|�S�N)r �
fromCoroutine)�args�kwargs�coro�fs   ��9/usr/lib/python3/dist-packages/twisted/test/test_defer.py�wrapperz!ensuringDeferred.<locals>.wrapperIs#����$�!�&�!���%�%�d�+�+�)r5�objectr6r<�returnzDeferred[_T])�	functools�wraps)r8r:s` r9�ensuringDeferredr@Fs'����_�_�Q��,��,��Nr;c��eZdZy)�GenericErrorN��__name__�
__module__�__qualname__�r;r9rBrBQs��r;rBc�H�	ddzS#t$rt|i|��}Y|SwxYw)z�
    Make a L{Failure} of a divide-by-zero error.

    @param args: Any C{*args} are passed to Failure's constructor.
    @param kwargs: Any C{**kwargs} are passed to Failure's constructor.
    �r)�
BaseExceptionr/)r5r6r8s   r9�getDivisionFailurerKUs:��%�	�A��
�H���%��T�$�V�$���H�%�s�	�!�!c�&�|jd�y)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��deferreds r9�fakeCallbackCancellerrRcs��
���'�(r;�_ExceptionT)�boundc�$�eZdZdZ						dd�Zy)�ImmediateFailureMixinz+
    Add additional assertion methods.
    c�&�ttj|�}g}|j|j�|jt
|�d�|j|dj|��tt|dj�S)z�
        Assert that the given Deferred current result is a Failure with the
        given exception.

        @return: The exception instance in the Deferred.
        rIr)rr0�TestCase�
addErrback�append�assertEqual�len�
assertTrue�checkrS�value)�selfrQ�	exception�testCase�failuress     r9�assertImmediateFailurez,ImmediateFailureMixin.assertImmediateFailureusu����)�)�4�0��"$�����H�O�O�,����S��]�A�.����H�Q�K�-�-�i�8�9��K��!��!2�!2�3�3r;N)rQ�
Deferred[Any]razType[_ExceptionT]r=rS)rDrErF�__doc__rdrGr;r9rVrVps$���4�%�4�2C�4�	�4r;rVc�(�eZdZdZdd�Zdd�Zdd�Zy)�	UtilTestsz&
    Tests for utility functions.
    c��tt��}tj|�}|j	t�|j||�y)z<
        L{defer.logError} returns the given error.
        N)r/�RuntimeErrorr�logError�flushLoggedErrors�assertIs)r`�error�results   r9�test_logErrorReturnsErrorz#UtilTests.test_logErrorReturnsError�s:�����'������&�����|�,��
�
�e�V�$r;c��tt��}tj|�|j	t�}|j||g�y)z9
        L{defer.logError} logs the given error.
        N)r/rjrrkrlr[)r`rn�errorss   r9�test_logErrorLogsErrorz UtilTests.test_logErrorLogsError�s>�����'��
���u���'�'��5������%��)r;c���g�d�fd�}tj|�tt��}t	j
|�|j
t�|j�djd��y)zR
        The text logged by L{defer.logError} has no repr of the failure.
        c�Z��tj|�}|�J��j|�yr3)r,�textFromEventDictrZ)�	eventDict�text�outputs  �r9�emitz4UtilTests.test_logErrorLogsErrorNoRepr.<locals>.emit�s+����(�(��3�D��#�#�#��M�M�$�r;r�Unhandled Error
Traceback N)rwzDict[str, Any]r=�None)	r,�addObserverr/rjrrkrlr]�
startswith)r`rzrnrys   @r9�test_logErrorLogsErrorNoReprz&UtilTests.test_logErrorLogsErrorNoRepr�s_�����	 �
	��������'��
���u�����|�,�����q�	�,�,�-J�K�Lr;N�r=r|)rDrErFrfrprsrrGr;r9rhrh�s���%�*�Mr;rhc�0�eZdZdWd�ZdXd�ZdYd�ZdZd�ZdWd�ZdWd�ZdWd�Z	dWd�Z
dWd	�ZdWd
�ZdWd�Z
dWd�ZdWd
�ZdWd�ZdWd�ZdWd�ZdWd�ZdWd�ZdWd�ZdWd�ZdWd�ZdWd�ZdWd�ZdWd�ZdWd�ZdWd�ZdWd�ZdWd�ZdWd�ZdWd�Z dWd�Z!dWd �Z"dWd!�Z#dWd"�Z$dWd#�Z%dWd$�Z&dWd%�Z'dWd&�Z(dWd'�Z)dWd(�Z*dWd)�Z+dWd*�Z,dWd+�Z-dWd,�Z.dWd-�Z/dWd.�Z0dWd/�Z1dWd0�Z2dWd1�Z3dWd2�Z4dWd3�Z5dWd4�Z6dWd5�Z7dWd6�Z8dWd7�Z9dWd8�Z:dWd9�Z;dWd:�Z<dWd;�Z=dWd<�Z>dWd=�Z?dWd>�Z@dWd?�ZAdWd@�ZBdWdA�ZCdWdB�ZDdWdC�ZEdWdD�ZFdWdE�ZGdWdF�ZHdWdG�ZIdWdH�ZJdWdI�ZKdWdJ�ZLdWdK�ZMdWdL�ZNdWdM�ZOdWdN�ZPdWdO�ZQdWdP�ZRdWdQ�ZSeTj�eVdR�dWdS��ZWeTj�eVdR�dWdT��ZXeTj�eVdR�dWdU��ZYyV)[�
DeferredTestsc��d|_d|_d|_|jtj
t	j��yr3)�callbackResults�callback2Results�errbackResults�
addCleanupr�setDebugging�getDebugging�r`s r9�setUpzDeferredTests.setUp�sH��
�	
��

�	
��

�	
��
	
����*�*�E�,>�,>�,@�Ar;c��||f|_|dS�Nr�r��r`r5r6s   r9�	_callbackzDeferredTests._callback�s��#�V�|����A�w�r;c��||f|_yr3�r�r�s   r9�
_callback2zDeferredTests._callback2�s�� $�f���r;c��||f|_yr3�r�r�s   r9�_errbackzDeferredTests._errback�s��"�F�l��r;c���t�}|j|j�|jd�|j	|j
�|j
|jdif�y�N�hello�r��r �addCallbackr�rO�assertIsNoner�r[r��r`rQs  r9�testCallbackWithoutArgsz%DeferredTests.testCallbackWithoutArgs�sX��"*�*�����T�^�^�,����'�"����$�-�-�.�����-�-�
�B�/?�@r;c���t�}|j|jd�|jd�|j	|j
�|j
|jdif�y)N�worldr�)r�r�r�r�s  r9�testCallbackWithArgsz"DeferredTests.testCallbackWithArgs�s[��"*�*�����T�^�^�W�5����'�"����$�-�-�.�����-�-�0B�B�/G�Hr;c���t�}|j|jd��|jd�|j	|j
�|j
|jdddif�y)Nr�)r�r�r�r�r�s  r9�testCallbackWithKwArgsz$DeferredTests.testCallbackWithKwArgs�sb��"*�*�����T�^�^�7��;����'�"����$�-�-�.�����-�-�
�W�g�<N�/O�Pr;c�T�t�}|j|j�|j|j�|j	d�|j|j�|j|jdif�|j|jdif�yr�)
r r�r�r�rOr�r�r[r�r�r�s  r9�testTwoCallbackszDeferredTests.testTwoCallbacks�s���"*�*�����T�^�^�,����T�_�_�-����'�"����$�-�-�.�����-�-�
�B�/?�@�����.�.��R�0@�Ar;c�$�td�}t�}|j|jt	t
dtfd��|j|�|j|j�|j|j�|j�J�|jt|jd�d�|j|jddj|�|j|jdi�y)zQ
        If given None for an errback, addCallbacks uses a pass-through.
        �oopsie.NrrI)rBr �addCallbacksr�rr	r<�errbackrYr�r�r�r�r[r\r_�r`rnrQs   r9�test_addCallbacksNoneErrbackz*DeferredTests.test_addCallbacksNoneErrback�s����X�&��#+�:�����d�n�n�d�8�C��K�3H�$�.O�P���������D�M�M�*����$�.�.�/��"�"�.�.�.�����T�0�0��3�4�a�8�����,�,�Q�/��2�8�8�%�@�����,�,�Q�/��4r;c
�`�t�}|j|j|jt	t
td�t	tttfd�di�|jd�|j|j�|j|jdif�y)zR
        If given None as a callback args and kwargs, () and {} are used.
        NrGr�r�)r r�r�r�rrr<r�strrOr�r�r[r�r�s  r9�!test_addCallbacksNoneCallbackArgsz/DeferredTests.test_addCallbacksNoneCallbackArgs�s���#+�*������N�N��M�M���v���%����f��%�t�,���
	
�	���'�"����$�-�-�.�����-�-�
�B�/?�@r;c�r�td�}t�}|j|j|jditttd�ttttfd��|j|�|j|j�|j|j�|j�J�|jt!|jd�d�|j|jddj"|�|j|jdi�y)zQ
        If given None as a errback args and kwargs, () and {} are used.
        r�rGNrrI)rBr r�r�r�rrr<rr�r�rYr�r�r�r[r\r_r�s   r9� test_addCallbacksNoneErrbackArgsz.DeferredTests.test_addCallbacksNoneErrbackArgs
s���X�&��#+�:������N�N��M�M�����v���%����f��%�t�,�
	
�	��������D�M�M�*����$�.�.�/��"�"�.�.�.�����T�0�0��3�4�a�8�����,�,�Q�/��2�8�8�%�@�����,�,�Q�/��4r;c�z�tttttt
ff}t
�}t
�}t
�}t|||g�}g}|fd	d�}d
d�}|j||�|jd�|j|�|jtd��|jd�|j|d|ddt	tt
|dd�j�f|dgt j"dft j$dft j"dfg�y)Nc�&�|j|�yr3)�extend��
resultListros  r9�cbz*DeferredTests.testDeferredList.<locals>.cb)s���M�M�*�%r;c��yr3rG��errs r9�catchz-DeferredTests.testDeferredList.<locals>.catch,���r;�1�2�3rrI�)r��
ResultListror�r=r|�r�r/r=r|)r
r�boolrr�r/r r"r�rOrYr�rBr[rr_r�SUCCESS�FAILURE)	r`r��defr1�defr2�defr3�dlror�r�s	         r9�testDeferredListzDeferredTests.testDeferredList s���%��e�C��L�&9� 9�:�;�
�'�z��'�z��'�z��
�5�%��/�
0����<B�	&�	�	����B��
���s��
�����	�
�
�l�3�'�(�
���s������q�	����1��s�4�����1��#>�#D�#D�E�F��q�	�	
��m�m�S�
!�E�M�M�3�#7�%�-�-��9M�N�	
r;c���g}|f					dd�}tg�}|j|�|j|gg�g|ddtgd��}|j|�|j|g�y)Nc�&�|j|�yr3�rZr�s  r9r�z/DeferredTests.testEmptyDeferredList.<locals>.cbGs��
�M�M�*�%r;T��fireOnOneCallback)r�z_DeferredListResultListT[None]roz$List[_DeferredListResultListT[None]]r=r|)r"r�r[)r`ror��dl1�dl2s     r9�testEmptyDeferredListz#DeferredTests.testEmptyDeferredListDs���79��<B�	&�6�	&�8�	&��	&�9E�R�8H�����������"��&���q�	�:F��$�;
��	����������$r;c��t�}t�}t�}t|||gd��}g}|j|j�d
d�}|j|�|j	d�|j|g�|j
td��|jt|�d�|d}|jt|jtj�d|j���|j}|j|jjt�|j|jjj d	�|j|j"d�y)NT��fireOnOneErrbackc��yr3rGr�s r9r�z;DeferredTests.testDeferredListFireOnOneError.<locals>.catchbr�r;r��	from def2rIrzFissubclass(aFailure.type, defer.FirstError) failed: failure's type is )r�r�)r r"rYrZrOr[r�rBr\r]�
issubclass�typer�
FirstErrorr_�
subFailurer5�index)	r`r�r�r�r�ror��aFailure�
firstErrors	         r9�testDeferredListFireOnOneErrorz,DeferredTests.testDeferredListFireOnOneErrorXs3��'�z��'�z��'�z��
�5�%��/�$�
G�� "��
�
�
�f�m�m�$�	�	�����	���s�������$�	�
�
�l�;�/�0�����V��a�(��!�9��	
����x�}�}�e�&6�&6�7�&.�m�m�
6�	
��^�^�
�	
����.�.�3�3�\�B�����.�.�4�4�9�9�>�J�����)�)�1�-r;c���t�}t|g�}g}|j|j�g}|j	|j�|jt
d��|jd|djjd�|jdt|��|dd}|�J�|jd|djjd�y)N�BangrrI)r r"rYrZr�r�rBr[r_r5r\�r`�d1r��	errorTrap�resultLists�firstResults      r9�!testDeferredListDontConsumeErrorsz/DeferredTests.testDeferredListDontConsumeErrors�s���%�Z��
�2�$�
��#%�	�
�
�
�i�&�&�'�<>��
���{�)�)�*�
�
�
�<��'�(������1��!3�!3�!8�!8��!;�<�����C��,�-�!�!�n�Q�'���&�&�&������Q��!5�!5�!:�!:�1�!=�>r;c��t�}t|gd��}g}|j|j�g}|j	|j�|jt
d��|jg|�|jdt|��|dd}|�J�|jd|djjd�y)NT��
consumeErrorsr�rIr)r r"rYrZr�r�rBr[r\r_r5r�s      r9�testDeferredListConsumeErrorsz+DeferredTests.testDeferredListConsumeErrors�s���%�Z��
�2�$�d�
3��#%�	�
�
�
�i�&�&�'�<>��
���{�)�)�*�
�
�
�<��'�(�����Y�'�����C��,�-�!�!�n�Q�'���&�&�&������Q��!5�!5�!:�!:�1�!=�>r;c��t�}t�}|jtd��t||gd��}g}|j	|j
�|j
dt|��|j	d��y)Nr�Tr�rIc��yr3rG��es r9�<lambda>zWDeferredTests.testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds.<locals>.<lambda>���r;)r r�rBr"rYrZr[r\�r`r��d2r�ros     r9�7testDeferredListFireOnOneErrorWithAlreadyFiredDeferredszEDeferredTests.testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds�sh��%�Z��%�Z��
�
�
�<��'�(��2�r�(�T�
:�� "��
�
�
�f�m�m�$�����C��K�(�
�
�
�n�%r;c�0�t�}t�}|jtd��|jd�t	||g�}g}|j|j�|jdt|��|jd��y)Nr�r�rIc��yr3rGr�s r9r�zIDeferredTests.testDeferredListWithAlreadyFiredDeferreds.<locals>.<lambda>�r�r;)
r r�rBrOr"r�rZr[r\rYr�s     r9�)testDeferredListWithAlreadyFiredDeferredsz7DeferredTests.testDeferredListWithAlreadyFiredDeferreds�sq��$�J��$�J��
�
�
�<��'�(�
���A���2�r�(�
#��/1��
���v�}�}�%�����C��K�(�
�
�
�n�%r;c���t�}t�}t||g�}|j�|j|tj
�|j|tj
�y)zk
        When cancelling an unfired L{DeferredList}, cancel every
        L{Deferred} in the list.
        N�r r"�cancel�failureResultOfrr�r`�deferredOne�deferredTwo�deferredLists    r9�test_cancelDeferredListz%DeferredTests.test_cancelDeferredList�sY��
'/�j��&.�j��#�[�+�$>�?���������[�%�*>�*>�?����[�%�*>�*>�?r;c���tt�}t�}t||g�}|j�|j	|t
j�|j|�}|j|dd�|j|ddd�|j|dd�|j|ddjt
j��y)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.
        rrIrMN)r rRr"r�r�rr�successResultOfr]r[�assertFalser^�r`r�r�r�ros     r9�test_cancelDeferredListCallbackz-DeferredTests.test_cancelDeferredListCallback�s���&.�.C�%D��%-�Z��#�[�+�$>�?���������[�%�*>�*>�?��%�%�l�3������q�	�!��%�������1��'8�9�������1��&�����q�	�!��*�*�5�+?�+?�@�Ar;c���t�}t�}t||gd��}|j�|j|tj
�|j|tj
�y)z�
        When cancelling an unfired L{DeferredList} with the flag
        C{fireOnOneCallback} set, cancel every L{Deferred} in the list.
        Tr�Nr�r�s    r9�,test_cancelDeferredListWithFireOnOneCallbackz:DeferredTests.test_cancelDeferredListWithFireOnOneCallback�s\��
'/�j��&.�j��#�[�+�$>�RV�W���������[�%�*>�*>�?����[�%�*>�*>�?r;c��tt�}t�}t||gd��}|j�|j	|t
j�|j|�}|j|d�y)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�)rMrN)	r rRr"r�r�rrrr[rs     r9�?test_cancelDeferredListWithFireOnOneCallbackAndDeferredCallbackzMDeferredTests.test_cancelDeferredListWithFireOnOneCallbackAndDeferredCallback�sk��&.�.C�%D��%-�Z��#�[�+�$>�RV�W���������[�%�*>�*>�?��%�%�l�3������!7�8r;c��t�}t�}t||gd��}|j�|j|tj
�|j|tj
�|j|tj�}|j}|j|jjtj
��y)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�rrr�r_r]r�r^)r`r�r�r��deferredListFailurer�s      r9�+test_cancelDeferredListWithFireOnOneErrbackz9DeferredTests.test_cancelDeferredListWithFireOnOneErrback�s���
'/�j��&.�j��#�[�+�$>�QU�V���������[�%�*>�*>�?����[�%�*>�*>�?�"�2�2�<��AQ�AQ�R��(�.�.�
����
�-�-�3�3�E�4H�4H�I�Jr;c�\�tt�}tt�}t||gd��}|j�|j	|�}|j|dd�|j
|ddd�|j|dd�|j
|ddd�y)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�rrIrMN)r rRr"r�rr]r[rs     r9�?test_cancelDeferredListWithFireOnOneErrbackAllDeferredsCallbackzMDeferredTests.test_cancelDeferredListWithFireOnOneErrbackAllDeferredsCallback
s���&.�.C�%D��%-�.C�%D��#�[�+�$>�QU�V�������%�%�l�3������q�	�!��%�������1��'8�9�����q�	�!��%�������1��'8�9r;c�B�t�}t�}||g}t|�}t�}|j|�|j�|j	|t
j�|j	|t
j�|j|�y)zg
        Cancelling a L{DeferredList} will cancel the original
        L{Deferred}s passed in.
        N)r r"rZr�r�rr�assertNoResult)r`r�r��argumentListr��
deferredThrees      r9�,test_cancelDeferredListWithOriginalDeferredsz:DeferredTests.test_cancelDeferredListWithOriginalDeferredss���
'/�j��&.�j��#�[�1��#�L�1��(0�
�
����M�*��������[�%�*>�*>�?����[�%�*>�*>�?����M�*r;c��dd�}t|�}t�}t||g�}|j�|j|tj
�|j
t�}|jt|�d�y)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.
        c��td��)z�
            A L{Deferred} canceller that raises an exception.

            @param deferred: The cancelled L{Deferred}.
            �test)rjrPs r9�cancellerRaisesExceptionzTDeferredTests.test_cancelDeferredListWithException.<locals>.cancellerRaisesException2s���v�&�&r;rIN)rQ�Deferred[object]r=r|)
r r"r�r�rrrlrjr[r\)r`rr�r�r�rrs      r9�$test_cancelDeferredListWithExceptionz2DeferredTests.test_cancelDeferredListWithException+sp��	'�'/�/G�&H��&.�j��#�[�+�$>�?���������[�%�*>�*>�?��'�'��5������V��a�(r;c��t�}t�}t||gd��}|jd�|j�|j	|�y)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"rOr�rr�s    r9�)test_cancelFiredOnOneCallbackDeferredListz7DeferredTests.test_cancelFiredOnOneCallbackDeferredListBsL��'/�j��&.�j��#�[�+�$>�RV�W�����T�"��������K�(r;c�,�t�}t�}t||gd��}|jtd��|j	�|j|�|j
|t�|j
|tj�y)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�rN)	r r"r�rBr�rr�rr�r�s    r9�(test_cancelFiredOnOneErrbackDeferredListz6DeferredTests.test_cancelFiredOnOneErrbackDeferredListOsw��'/�j��&.�j��#�[�+�$>�QU�V�����L��0�1��������K�(����[�,�7����\�5�+;�+;�<r;c��g}tjd�}|j|j�|j	|dg�y)N�success)r�succeedr�rZr[�r`�l�ds   r9�testImmediateSuccessz"DeferredTests.testImmediateSuccess^s7���� �=�=��3��	�
�
�a�h�h������Y�K�(r;c���g}tjtd��}|j|j�|jt
|dj�d�y�N�failr)rr'rBrYrZr[r�r_r!s   r9�testImmediateFailurez"DeferredTests.testImmediateFailuredsF����!�J�J�|�F�';�<��	���Q�X�X������Q�q�T�Z�Z��&�1r;c�.�g}tjtd��}|j�|j	|j
�|j
|g�|j�|j
t|dj�d�yr&)
rr'rB�pauserYrZr[�unpauser�r_r!s   r9�testPausedFailurezDeferredTests.testPausedFailurejsh�����J�J�|�F�+�,��	���	�	���Q�X�X������B��	�	�	������Q�q�T�Z�Z��&�1r;c��g}t�}|jd��j|j�|j	d�|j|djt�g}tt�}|jd��j|j�|j	d�|j|djt�y)Nc��ddzS�NrIrrG��_s r9r�z2DeferredTests.testCallbackErrors.<locals>.<lambda>vs
���Q��r;rIrc�(�tt��Sr3)r/�ZeroDivisionErrorr0s r9r�z2DeferredTests.testCallbackErrors.<locals>.<lambda>{s���):�)<�!=�r;)	r r�rYrZrO�assertIsInstancer_r3�int)r`r"r#r�s    r9�testCallbackErrorsz DeferredTests.testCallbackErrorsss�����#�:��	�
�
�&�'�2�2�1�8�8�<�	�
�
�1�
����a��d�j�j�*;�<���
�c�]�_��
���=�>�I�I�!�(�(�S�
���A�����a��d�j�j�*;�<r;c��t�}|j�|j|j�|j	�yr3)r r*r�r�r+�r`r#s  r9�testUnpauseBeforeCallbackz'DeferredTests.testUnpauseBeforeCallbacks+��$�J��	���	�	�
�
�d�n�n�%�	�	�	�r;c�V��t�}t���j�|j�fd��|j|j�|j	d�|j|jd��j	d�|j|jd��j�|j|jd�|j�Jd��|j|jd	d	dd
j|j��y)z�
        Callbacks passed to L{Deferred.addCallback} can return Deferreds and
        the next callback will not be run until that Deferred fires.
        c����Sr3rG)�rr�s �r9r�z<DeferredTests.test_callbackReturnsDeferred.<locals>.<lambda>�s����r;rIz Should not have been called yet.r�z&Still should not have been called yet.zShould have been called nowNz%make that legible to the type checkerrz/Result should have been from second deferred:{})r r*r�r�rOr�r�r+�assertIsNotNone�assertEquals�format�r`r�r�s  @r9�test_callbackReturnsDeferredz*DeferredTests.test_callbackReturnsDeferred�s����
%�J��$�J��
���
�
���|�$�
���t�~�~�&�
���A�����$�.�.�0R�S�
���A������ � �"J�	
�	�
�
�����T�1�1�3P�Q��#�#�/�X�1X�X�/����� � ��#�A�&�
�=�D�D��$�$�
�	
r;c����t�}d�t��d��fd�}|jd�|j|j	|j|��d�tt�}d�|jd�|j|j|j|�j|�t�j��y)zF
        Callbacks passed to addCallback may return Failures.
        Fc�(���rt��S|dzS�NrIr.)ro�rte�
shouldFails ��r9�	maybeFailzADeferredTests.test_callbackMaybeReturnsFailure.<locals>.maybeFail�s�����s�|�#���z�!r;��TN)ror5r=zUnion[int, Failure])
r rjrOr[rr�r5rmr�r_)r`r#rGrErFs   @@r9� test_callbackMaybeReturnsFailurez.DeferredTests.test_callbackMaybeReturnsFailure�s����$�:���
��n��	"�	
�
�
�1�
�����-�-�a�m�m�I�.F�G��K��S�M�O���
�	�
�
�1�
��
�
�� � ��
�
�i�(�4�4�Y�?��
��e��
	
r;c�L���t�}t��g�d�fd�}d�fd�}|j|�j|�|jt	��|j|�|j
�g��jd�|j
�dg�y)zo
        Errbacks passed to L{Deferred.addErrback} can return Deferreds just as
        callbacks can.
        roc����Sr3rG)ror�s �r9�asyncErrbackz?DeferredTests.test_errbackReturnsDeferred.<locals>.asyncErrback�s����Ir;c�(���j|�yr3r�)ro�resultValuess �r9�syncCallbackz?DeferredTests.test_errbackReturnsDeferred.<locals>.syncCallback�s�������'r;N)ror/r=�
Deferred[str])rozUnion[str, int]r=r|)r rYr�r��
ValueErrorrr[rO)r`r#rMrPr�rOs    @@r9�test_errbackReturnsDeferredz)DeferredTests.test_errbackReturnsDeferred�s����
$�:��$�J����	�	(�	
���\�"�.�.�|�<�	�	�	�*�,�����A������r�*�
���H�������z�2r;c�r��t�}t���j|��j�t�}|j	�fd��|jd�g}|j	|j
�|j
|g��j�|j
||g�y)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����Sr3rG)�ignored�pauseds �r9r�zDDeferredTests.test_chainedPausedDeferredWithResult.<locals>.<lambda>�s���F�r;N)r<r rOr*r�rZr[r+)r`�expected�chainedrorWs    @r9�$test_chainedPausedDeferredWithResultz2DeferredTests.test_chainedPausedDeferredWithResult�s�����8��#+�:������!�����"*�*�����2�3������!�����F�M�M�*������$���������(��,r;c���t�}t��|j�fd��|jd�|j��jd�g}�j|j�|j|dg�y)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����Sr3rG�rV�seconds �r9r�z:DeferredTests.test_pausedDeferredChained.<locals>.<lambda>�����&�r;N)r r�rOr*rZr[)r`�firstror^s   @r9�test_pausedDeferredChainedz(DeferredTests.test_pausedDeferredChained�sl���!)�
��!)���
���0�1�
���t��
���
�����������6�=�=�)�����$��(r;c�4�g}tjtjd�tjd�g�j|j�|j|ddgg�g}tjd�tjt��g}tj|�j|j�|jt|�d�|j|dt�|djd��y)NrIr�rc��yrDrGr�s r9r�z2DeferredTests.test_gatherResults.<locals>.<lambda>r�r;)r�
gatherResultsr r�rZr[r'rRrYr\r4r/)r`�resultsrrr�s    r9�test_gatherResultsz DeferredTests.test_gatherResults�s���#%��
���U�]�]�1�-�u�}�}�Q�/?�@�A�M�M��N�N�	
�	
����A�q�6�(�+� "���m�m�A���
�
�:�<� 8�
9��
���B��*�*�6�=�=�9�����V��a�(����f�Q�i��1�
�1�����%r;c�(�tjd�}tjtd��}tj||gd��}g}|j|j�g}|j|j�|jt|�t|�fd�|j|djtj�|djj}|j|jt�y)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}.
        rIzoh noesTr��rrIrN)
rr r'rjrdrYrZr[r\r4r_r�r�)r`�dgood�dbadr#�unconsumedErrors�gatheredErrorsr�s       r9�#test_gatherResultsWithConsumeErrorsz1DeferredTests.test_gatherResultsWithConsumeErrorss����
�
�a� ���z�z�,�y�1�2�������
�T�B��*,�����(�/�/�0�(*��	���^�*�*�+����#�.�/��^�1D�E�v�N����n�Q�/�5�5�u�7G�7G�H�#�A�&�,�,�7�7�
����j�.�.��=r;c���t�}t�}tj||g�}|j�|j	|tj
�|j	|tj
�|j	|tj�}|j}|j|jjtj
��y)z~
        When cancelling the L{defer.gatherResults} call, all the
        L{Deferred}s in the list will be cancelled.
        N)r rrdr�r�rr�r_r]r�r^)r`r�r�ro�gatherResultsFailurer�s      r9�test_cancelGatherResultsz&DeferredTests.test_cancelGatherResultss���
'/�j��&.�j���$�$�k�;�%?�@���
�
�����[�%�*>�*>�?����[�%�*>�*>�?�#�3�3�F�E�<L�<L�M��)�/�/�
����
�-�-�3�3�E�4H�4H�I�Jr;c��tt�}tt�}tj||g�}|j	�|j|�}|j
|dd�|j
|dd�y)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.
        rrMrIN)r rRrrdr�rr[)r`r�r�ro�callbackResults     r9�0test_cancelGatherResultsWithAllDeferredsCallbackz>DeferredTests.test_cancelGatherResultsWithAllDeferredsCallback+st��&.�.C�%D��%-�.C�%D���$�$�k�;�%?�@���
�
���-�-�f�5�������*�,=�>������*�,=�>r;c�"��t��g}g}tj�fd��}|j|j|j�|j|g�|jt
|�d�|j|d��y)z�
        L{defer.maybeDeferred} should retrieve the result of a synchronous
        function and pass it to its resulting L{Deferred}.
        c����Sr3rG�ros�r9r�z6DeferredTests.test_maybeDeferredSync.<locals>.<lambda>Bs����r;rIrN)r<r�
maybeDeferredr�rZr[r\�assertIdentical)r`rerrr#ros    @r9�test_maybeDeferredSyncz$DeferredTests.test_maybeDeferredSync:st���
��� "�� "������/��	���w�~�~�v�}�}�5������$�����W��q�)����W�Q�Z��0r;c��dd�}g}g}tj|d�}t|tt�|j|j|j�|j|g�|j|dg�y)zV
        L{defer.maybeDeferred} should pass arguments to the called function.
        c��|dzS)N�rG��xs r9�plusFivez>DeferredTests.test_maybeDeferredSyncWithArgs.<locals>.plusFiveMs���q�5�Lr;�
�N)r~r5r=r5)rrwrr r5r�rZr[)r`rrerrr#s     r9�test_maybeDeferredSyncWithArgsz,DeferredTests.test_maybeDeferredSyncWithArgsHsk��
	� �� "������"�-���A�x��}�%�	���w�~�~�v�}�}�5������$�����2�$�'r;c�^��td��d�fd�}g}g}tj|�j|j|j�|j|g�|jt
|�d�|jt|dj�t���y)z�
        L{defer.maybeDeferred} should catch an exception raised by a synchronous
        function and errback its resulting L{Deferred} with it.
        zthat value is unacceptablec�����r3rG�rXs�r9�raisesExceptionzFDeferredTests.test_maybeDeferredSyncException.<locals>.raisesException_s����Nr;rIrN)r=r)	rRrrwr�rZr[r\r�r_)r`r�rerrrXs    @r9�test_maybeDeferredSyncExceptionz-DeferredTests.test_maybeDeferredSyncExceptionXs����
�:�;��	� �� "��
���O�,�9�9�'�.�.�&�-�-�X�����"�%�����V��a�(�����V�A�Y�_�_�-�s�8�}�=r;c�L��	ddzg}g}tj�fd��}|j	|j
|j
�|j
|g�|j
t|�d�|j|d��y#t$r
t��Y��wxYw)zw
        L{defer.maybeDeferred} should handle a L{Failure} returned by a
        function and errback with it.
        �10r|c����Sr3rGr�s�r9r�z=DeferredTests.test_maybeDeferredSyncFailure.<locals>.<lambda>us����r;rIrN)	�	TypeErrorr/rrwr�rZr[r\rx)r`rerrr#rXs    @r9�test_maybeDeferredSyncFailurez+DeferredTests.test_maybeDeferredSyncFailureis����
	!��1�H� �� "����� 0�1��	���w�~�~�v�}�}�5�����"�%�����V��a�(����V�A�Y��1���	!��y�H�	!�s�B
�
B#�"B#c����t��tj�fd��}�jd�g}|j	|j
�|j
|dg�y)z}
        L{defer.maybeDeferred} should let L{Deferred} instance pass by
        so that original result is the same.
        c����Sr3rG�r�s�r9r�z7DeferredTests.test_maybeDeferredAsync.<locals>.<lambda>�s����r;�SuccessN)r rrwrOr�rZr[)r`r�ror�s   @r9�test_maybeDeferredAsyncz%DeferredTests.test_maybeDeferredAsync{sO���
%�J��
�
 �
 ��
,��
���I����
���v�}�}�%�����)��-r;c���t��tj�fd��}�jt	t���|j
|t
�y)z�
        L{defer.maybeDeferred} should let L{Deferred} instance pass by
        so that L{Failure} returned by the original instance is the
        same.
        c����Sr3rGr�s�r9r�z<DeferredTests.test_maybeDeferredAsyncError.<locals>.<lambda>�s����r;N)r rrwr�r/rjrd)r`r�r�s  @r9�test_maybeDeferredAsyncErrorz*DeferredTests.test_maybeDeferredAsyncError�s?���&�Z��"�0�0��<��
�
�
�7�<�>�*�+��#�#�B��5r;c��dd�}tj|�}t|tt�|j|j
|�d�y)��
        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.
        c��K�y�w�NrIrGrGr;r9r8z;DeferredTests.test_maybeDeferredCoroutineSuccess.<locals>.f��	�������rIN�r=r5)rrwrr r5r[r)r`r8�coroutineDeferreds   r9�"test_maybeDeferredCoroutineSuccessz0DeferredTests.test_maybeDeferredCoroutineSuccess�sE��	�"�/�/��2���%�x��}�5�����-�-�.?�@�!�Dr;c����dd��d�fd�}dd�}tj|�}t|tt�|j|�}|j
|j|�d�y)	r�c��K�y�wr�rGrGr;r9r8zCDeferredTests.test_maybeDeferredCoroutineSuccessIndirect.<locals>.f�r�r�c�����Sr3rG�r8s�r9�gzCDeferredTests.test_maybeDeferredCoroutineSuccessIndirect.<locals>.g��
����3�Jr;c��|dzSrDrGrvs r9�
typedCallbackzODeferredTests.test_maybeDeferredCoroutineSuccessIndirect.<locals>.typedCallback�s���A�:�r;�Nr�)r=z"Coroutine[Deferred[int], Any, Any])ror5r=r5)rrwrr rr�r[r)r`r�r�r��modifiedDeferredr8s     @r9�*test_maybeDeferredCoroutineSuccessIndirectz8DeferredTests.test_maybeDeferredCoroutineSuccessIndirect�sa���	�
	�
	�"�/�/��2���%�x��}�5�,�8�8��G������-�-�.>�?��Cr;c����Gd�dt��d�fd��d�fd�}t|jtj|��j
t
���y)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��eZdZy)�GDeferredTests.test_maybeDeferredCoroutineFailure.<locals>.SomeExceptionNrCrGr;r9�
SomeExceptionr��s��r;r�c���K�����wr3rG)r�s�r9r8z;DeferredTests.test_maybeDeferredCoroutineFailure.<locals>.f�s������/�!�s�c�����Sr3rGr�s�r9r�z;DeferredTests.test_maybeDeferredCoroutineFailure.<locals>.g�r�r;Nr�)r=zCoroutine[None, None, None])�	Exceptionrr�rrwr�r)r`r�r�r8s  @@r9�"test_maybeDeferredCoroutineFailurez0DeferredTests.test_maybeDeferredCoroutineFailure�sJ���	�I�	�	"�
	�	�� � ��!4�!4�Q�!7�8�=�=��]�#�	
r;c�����g�g�t��d���fd�}tjd�}|j|��j	d�|j�j
��j
�j
�|j
�j
�|jg��|j�gd��y)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.
        c�����jd|f�tjd�}d��fd�}d�fd�}|j|�j|�|j	�j�|S)N�start-of-cb�innerc�*���jd��S�N��
firstCallbackr�r��ror�res ��r9r�zMDeferredTests.test_innerCallbacksPreserved.<locals>.cb.<locals>.firstCallback�s������9�:��r;c�4���jd|f�|dzS�N�secondCallbackr�r��rores �r9r�zNDeferredTests.test_innerCallbacksPreserved.<locals>.cb.<locals>.secondCallback�������� 0�&�9�:���z�!r;�ror�r=rQ�ror�r=r��rZrr r�rY�ror#r�r�rcr�res    ���r9r�z6DeferredTests.test_innerCallbacksPreserved.<locals>.cb�sZ����N�N�M�6�2�3��
�
�g�&�A�
�
"�
�M�M�-�(�4�4�^�D�
�L�L����)��Hr;�outer�orange��r�r�r�)r�r��orangeorangeNr�)r rr r�rOrZrYr[�r`r�r�rcr�res   @@@r9�test_innerCallbacksPreservedz*DeferredTests.test_innerCallbacksPreserved�s����68��"$��'�z��	� �
�
�g�&��
���"��
���x� �
���'�.�.�)�
������)�
������)�����X�&�����
�	
r;c�N����g�g�t��d���fd�}tjd�j|�j	�j
�|j
ddg���jd�|j
g��|j
�ddddgfg�y)	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.
        c�����jd|f�t�}d��fd�}d�fd�}|j|�j|�j�j�}|j	d�|S)Nr�c�V���jd|f�tj�g�S�Nr��rZrrd�ro�ares ��r9r�zNDeferredTests.test_continueCallbackNotFirst.<locals>.cb.<locals>.firstCallback�'��������8�9��*�*�A�3�/�/r;c�,���jd|f�y�Nr�r�r�s �r9r�zODeferredTests.test_continueCallbackNotFirst.<locals>.cb.<locals>.secondCallback������� 0�&�9�:r;�ror|r=�Deferred[List[str]]�ro�	List[str]r=r|�rZr r�rYrO�ror#r�r��returnerr�rcres     ���r9r�z7DeferredTests.test_continueCallbackNotFirst.<locals>.cb�b����N�N�D�&�>�*� (�
�A�
0�
;��
�
�m�,���^�,���H�O�O�,�
�

�J�J�t���Or;r��r�r��r�N�withersr�N�ror�r=�Deferred[None])r rr r�rYrZr[rO)r`r�r�rcres  @@@r9�test_continueCallbackNotFirstz+DeferredTests.test_continueCallbackNotFirst�s����BD��"$��#�:��	�&	�
�
�g��*�*�2�.�9�9�(�/�/�J����/�+B�C�W�M�	�
�
�9������X�&�����
�5�8H�9�+�7V�W�	
r;c�����g�g�t��d
���fd�}t�}|j|�|j�fd��|j�j�|j	d�|jddg���j	d�|jg��|j�ddddgfdg�y	)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.
        c�����jd|f�t�}d��fd�}d�fd�}|j|�j|�j�j�}|j	d�|S)Nr�c�V���jd|f�tj�g�Sr�r�r�s ��r9r�zLDeferredTests.test_callbackOrderPreserved.<locals>.cb.<locals>.firstCallback1r�r;c�,���jd|f�yr�r�r�s �r9r�zMDeferredTests.test_callbackOrderPreserved.<locals>.cb.<locals>.secondCallback5r�r;r�r�r�r�s     ���r9r�z5DeferredTests.test_callbackOrderPreserved.<locals>.cb-r�r;c�&���jd�S)N��finalNr�)r~res �r9r�z;DeferredTests.test_callbackOrderPreserved.<locals>.<lambda>Bs���G�N�N�?�$C�r;r�r�r�r�r�r�Nr�)r r�rYrZrOr[)r`r�r�r�rcres   @@@r9�test_callbackOrderPreservedz)DeferredTests.test_callbackOrderPreserved#s����BD��"$��#�:��	�& (�z��
���"��
���C�D�
������)�
���w�����/�+B�C�W�M�	�
�
�9������X�&������'�!�I�;�/��	
�	
r;c������t��g�d�fd��d�fd�}d���fd�}�j|��j|��jd�|j�gd��y)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.
        c�(���jd�y�N�r��ro�calleds �r9�	callback3z;DeferredTests.test_reentrantRunCallbacks.<locals>.callback3Z�����M�M�!�r;c�(���jd�y�Nr�r�r�s �r9�	callback2z;DeferredTests.test_reentrantRunCallbacks.<locals>.callback2]r�r;c�J���jd��j��yrD�rZr�)ror�r�rQs ���r9�	callback1z;DeferredTests.test_reentrantRunCallbacks.<locals>.callback1`s����M�M�!��� � ��+r;N)rIr�r��ror|r=r|�r r�rOr[)r`r�r�r�r�rQs   @@@r9�test_reentrantRunCallbacksz(DeferredTests.test_reentrantRunCallbacksRs\���
$,�:����	�	�	,�	���Y�'����Y�'����$�������+r;c������t��g�d�fd��d����fd�}�j|��jd��j�ddg�y)z�
        A callback added to a L{Deferred} by a callback on that L{Deferred}
        should not be executed until the running callback returns.
        c�(���jd�yr�r�r�s �r9r�z;DeferredTests.test_nonReentrantCallbacks.<locals>.callback2qr�r;c�p���jd��j���j�dg�yrD)rZr�r[)ror�r�rQr`s ����r9r�z;DeferredTests.test_nonReentrantCallbacks.<locals>.callback1ts/����M�M�!��� � ��+����V�a�S�)r;NrIr�r�r�)r`r�r�r�rQs` @@@r9�test_nonReentrantCallbacksz(DeferredTests.test_nonReentrantCallbacksisR���
$,�:����	�	*�	*�
	���Y�'����$������!�Q��(r;c������d�t��d�fd��d��fd�}�j|��jd�|j�t�}|j|j�f�y)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 exceptionc���t���r3�r�)ro�exceptionMessages �r9r�zFDeferredTests.test_reentrantRunCallbacksWithFailure.<locals>.callback2�s����,�-�-r;c�(���j��yr3)r�)ror�rQs ��r9r�zFDeferredTests.test_reentrantRunCallbacksWithFailure.<locals>.callback1�s���� � ��+r;N�ror<r=r|)r r�rOrdr�r[r5)r`r�rar�rQrs   @@@r9�%test_reentrantRunCallbacksWithFailurez3DeferredTests.test_reentrantRunCallbacksWithFailure}sd���7��#+�:��	.�	,�	���Y�'����$���/�/��)�D�	�������*:�)<�=r;c�d��t�}tj|��t�}|j	�fd��|jd�g}�j	|j�|j|d�|j	|j�|j|d|�y)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����Sr3rG��ignr`s �r9r�z=DeferredTests.test_synchronousImplicitChain.<locals>.<lambda>�����u�r;NrrI)	r<rr r r�rOrZr�rm)r`ror^rer`s    @r9�test_synchronousImplicitChainz+DeferredTests.test_synchronousImplicitChain�s��������
�
�f�%��!)������,�-������*,��
���'�.�.�)����'�!�*�%����7�>�>�*��
�
�g�a�j�&�)r;c���t��t�}|j�fd��|jd�g}�j|j�g}|j|j�|j	|g�|j	|g�t�}�j|�|j	|dg�|j	||g�y)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����Sr3rGr	s �r9r�z>DeferredTests.test_asynchronousImplicitChain.<locals>.<lambda>�rr;N)r r�rOrZr[r<)r`r^r��secondResultror`s     @r9�test_asynchronousImplicitChainz,DeferredTests.test_asynchronousImplicitChain�s����#+�*��#+�:�����,�-������$&��
���+�,�,�-�%'�����<�.�.�/�����b�)�����r�*����
���v������t�f�-������x�0r;c�2�tjtd��}|fdd�}t�}|j	|�|jd�g}|j	|j�|j|d�|j|t�y)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 Failurec��|Sr3rG)r1r`s  r9r�z<DeferredTests.test_synchronousImplicitErrorChain.<locals>.cb�s���Lr;Nr)r1r|r`r�r=r�)	rr'rjr r�rOrZr�rd)r`r`r�r^r�s     r9�"test_synchronousImplicitErrorChainz0DeferredTests.test_synchronousImplicitErrorChain�s~���
�
�<�(B�C�D��05�	�"*������2�������"$��
���+�,�,�-����+�a�.�)��#�#�F�L�9r;c�L��t��t�}|j�fd��|jd�g}|j|j�g}�j|j�g}|j|j�|j|g�|j|g��j
td��|j|djt��|j|dg�|jt|�d�y)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����Sr3rGr	s �r9r�zCDeferredTests.test_asynchronousImplicitErrorChain.<locals>.<lambda>�rr;NrrrI)r r�rOrYrZr[r�rjr]r^r\)r`r^�secondErrorr�rr`s     @r9�#test_asynchronousImplicitErrorChainz1DeferredTests.test_asynchronousImplicitErrorChain�s����!)�
��!)������,�-������%'�����+�,�,�-�"$��
���+�,�,�-�#%�����<�.�.�/�����b�)�����r�*�
�
�
�l�#=�>�?�����A��,�,�\�:�;�����t�f�-�����\�*�A�.r;c����t��t���j�fd��t�}|j�fd��g}|j|j�t�}�j	d�|j	d�|j|g��j	|�|j||g�y)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����Sr3rGr	s �r9r�zGDeferredTests.test_doubleAsynchronousImplicitChaining.<locals>.<lambda>�rr;c����Sr3rG)r
r^s �r9r�zGDeferredTests.test_doubleAsynchronousImplicitChaining.<locals>.<lambda>�s���f�r;N)r r�rZr<rOr[)r`�third�thirdResultror`r^s    @@r9�'test_doubleAsynchronousImplicitChainingz5DeferredTests.test_doubleAsynchronousImplicitChaining�s����#+�*��#+�:�����,�-�"*�*��
���,�-�$&��
���+�,�,�-���������
���t��	
����b�)�	���v������v�h�/r;c�����g�g�t��d	���fd�}tjd�j|�j�j�}|j�ddg��j
d��j�j�|j�j�|jg�d�|j�gd��y)
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.
        c�����jd|f�tjd�}d��fd�}d�fd�}|j|�j|�|j	�j�|S)Nr�r�c�*���jd��Sr�r�r�s ��r9r�zXDeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb.<locals>.firstCallbacks������9�:��r;c�4���jd|f�|dzSr�r�r�s �r9r�zYDeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cb.<locals>.secondCallback$r�r;r�r�r�r�s    ���r9r�zADeferredTests.test_nestedAsynchronousChainedDeferreds.<locals>.cbsZ����N�N�M�6�2�3��
�
�g�&�A�
�
"�
�M�M�-�(�4�4�^�D�
�L�L����)��Hr;r�r�r�r��&Got errbacks but wasn't expecting any.r�Nr��r rr r�rZr[rOrYr�s   @@@r9�'test_nestedAsynchronousChainedDeferredsz5DeferredTests.test_nestedAsynchronousChainedDeferredss����68��"$�� (�z��	�,
�M�M�'�"�.�.�r�2�>�>�w�~�~�N�	�
	
����.�0J�K�	
�	���x� �	������)�
������)�����X�'O�P�����
�	
r;c������g�g�t��d	���fd�}tjd�}|j|�|j�j�|j�ddg��j
d�|j�j��j�j�|jg�d�|j�ddddgfddgg�y)
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.
        c�����jd|f�tjd�}d��fd�}d�fd�}|j|�j|�j	�j�S)Nr�r�c�T���jd|f�dd�}�j|�S)Nr�c��|gSr3rGrvs r9�	transformz}DeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.firstCallback.<locals>.transformes	��"�8�Or;)ror�r=r�r�)ror*r�res  ��r9r�zjDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.firstCallback^s,��������8�9�$��(�(��3�3r;c�4���jd|f�|dzSr�r�r�s �r9r�zkDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cb.<locals>.secondCallbackjr�r;)ror�r=r�)ror�r=r�r�r�s    ���r9r�zSDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacks.<locals>.cbZsW����N�N�M�6�2�3��
�
�g�&�A�

4�
"�
�
�
�m�,���^�,���H�O�O�,�
r;r�r�r�r�r#r�N)ror�r=zDeferred[Optional[List[str]]]r$r�s   @@@r9�9test_nestedAsynchronousChainedDeferredsWithExtraCallbackszGDeferredTests.test_nestedAsynchronousChainedDeferredsWithExtraCallbacksKs���� ��"$�� (�z��	�:�
�
�g�&��
���"��
���'�.�.�)�	
����.�0J�K�	
�	���y�!�	������)�
������)�����X�'O�P�����(�*�!�I�;�/��I�&�	
�	
r;c��t�}t�}|j|�|j|j|�y)zT
        When we chain a L{Deferred}, that chaining is recorded explicitly.
        N)r �
chainDeferredrm�
_chainedTo�r`r��bs   r9�&test_chainDeferredRecordsExplicitChainz4DeferredTests.test_chainDeferredRecordsExplicitChain�s0��%�J��$�J��	������
�
�a�l�l�A�&r;c��t�}t�}|j|�|jd�|j|j�y)aR
        Any recorded chaining is cleared once the chaining is resolved, since
        it no longer exists.

        In other words, if one L{Deferred} is recorded as depending on the
        result of another, and I{that} L{Deferred} has fired, then the
        dependency is resolved and we no longer benefit from recording it.
        N)r r.rOr�r/r0s   r9�%test_explicitChainClearedWhenResolvedz3DeferredTests.test_explicitChainClearedWhenResolved�s<��%�J��$�J��	�����	�
�
�4�����!�,�,�'r;c���t�}t��|j�fd��|jd�|j|j��y)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����Sr3rG)rVr1s �r9r�zFDeferredTests.test_chainDeferredRecordsImplicitChain.<locals>.<lambda>�s���a�r;N)r r�rOrmr/r0s  @r9�&test_chainDeferredRecordsImplicitChainz4DeferredTests.test_chainDeferredRecordsImplicitChain�s>���%�J��$�J��	�
�
�'�(�	�
�
�4���
�
�a�l�l�A�&r;c�|��t��d
�fd�}�j|��jd�|j|g�}|j	t|�d�|d}|j	|dt�d}|jtj||d�dj||d��y	)z�
        When a Deferred is returned from a callback directly attached to that
        same Deferred, a warning is emitted.
        c����Sr3rG�ror#s �r9�circularCallbackzADeferredTests.test_circularChainWarning.<locals>.circularCallback������Hr;�foorIr�categoryz1Callback returned the Deferred it was attached to�messagez
Expected match: {!r}
Got: {!r}Nr�)r r�rO�
flushWarningsr[r\�DeprecationWarningr]�re�searchr?)r`r;�circular_warnings�warning�patternr#s     @r9�test_circularChainWarningz'DeferredTests.test_circularChainWarning�s����
$�:��	�	
�
�
�&�'�	�
�
�5�� �.�.�0@�/A�B������.�/��3�#�A�&�������,�.@�A�E������I�I�g�w�y�1�2�/�6�6�w��	�@R�S�	
r;c�@��|jttdtj�tjdt
��t
��d�fd�}�j|��jd�|j��}|jt
�y)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.
        �filtersrn)r>c����Sr3rGr:s �r9r;zCDeferredTests.test_circularChainException.<locals>.circularCallback�r<r;r=Nr�)r��setattr�warningsrI�filterwarningsrAr r�rOr��trap)r`r;�failurer#s   @r9�test_circularChainExceptionz)DeferredTests.test_circularChainException�sz���	
���������		
�	����2D�E�#�:��	�	
�
�
�&�'�	�
�
�5���&�&�q�)�����'�(r;c�n�t�}t|�}|jt|�d|d�d��y)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 �idr[�repr)r`r#�addresss   r9�	test_reprzDeferredTests.test_repr�s3��
%�J���Q�%������a��O�G�A�;�a�"@�Ar;c��t�}|jd�|jt|�dt	|�d�d��y)zb
        If a L{Deferred} has been fired, then its C{repr()} contains its
        result.
        r�rRr~z current result: 'orange'>N)r rOr[rUrTr8s  r9�test_reprWithResultz!DeferredTests.test_reprWithResult�s;��
$�:��	�
�
�8������a��O�B�q�E�!�9�<V�"W�Xr;c	��t�}t�}|j|�|jt|�dt	|�d�dt	|�d�d��y)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}.
        rRr~z waiting on Deferred at 0xrSN)r r.r[rUrTr0s   r9�test_reprWithChainingz#DeferredTests.test_reprWithChaining�sR��%�J��$�J��	����������G��b��e�A�Y�&@��A��q�	��K�	
r;c�N��d�fd�}�j|d�|d��y)zw
        The depth of the call stack does not grow as more L{Deferred} instances
        are chained together.
        c�>��
��g�d�fd�}t�}t|�D�cgc]}t���}}|dd}t��
|j�}|f					dd�}|j|�|j|�|r1|j�}|f					dd�}	|j|	�|}|r�1|j�
fd��|j	d�|D]}|j	d���j�g��
j	d��dScc}w)Nc�^���jttj���yr3)rZr\�	traceback�
extract_stack)rV�stacks �r9�recordStackDepthzVDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.recordStackDepths������S��!8�!8�!:�;�<r;c��|Sr3rG�rVr�s  r9�cbInnerzMDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.cbInners	���r;c��|Sr3rGrds  r9�
cbNewInnerzPDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.cbNewInner%s	��!�Lr;c����Sr3rG)r
�lasts �r9r�zNDeferredTests.test_boundedStackDepth.<locals>.chainDeferreds.<locals>.<lambda>-s���$�r;r�rVr<r=r|)rVr<r�r�r=r�)r �range�popr�rOr[)
�howManyrb�toprV�innerDeferreds�originalInnersr�re�newInnerrgrirar`s
          @@�r9�chainDeferredsz<DeferredTests.test_boundedStackDepth.<locals>.chainDeferredssI����E�
=�#+�*�C�*/��.�4�&��
�4�N�4�,�A�.�N�#+�:�D�"�&�&�(�E�:?�
��
�(6�
��
�

�O�O�G�$��O�O�,�-� �)�-�-�/��>F�!�#�!�,:�!�#�!�
�!�!�*�-� ��!�
���.�/��L�L���'�
%�����t�$�
%�

���U�B�'�
�M�M�$����8�O��Q4s�DrIr�N)rmr5r=r5)r[)r`rrs` r9�test_boundedStackDepthz$DeferredTests.test_boundedStackDepths%���/	�f	
�����*�N�1�,=�>r;c�Z���t�}t��t��|j�fd���j�fd���jd�|jd��jd�g}�j|j�|j	|dg�y)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����Sr3rGr]s �r9r�zZDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled.<locals>.<lambda>Nr_r;c����Sr3rG)rVrs �r9r�zZDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled.<locals>.<lambda>Os���5�r;N)r r�rOrZr[)r`r`rer^rs   @@r9�:test_resultOfDeferredResultOfDeferredOfFiredDeferredCalledzHDeferredTests.test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled@s����!)�
��!)��� (�
��
���0�1����0�1������
���t��
���t�� �����7�>�>�*�����4�&�)r;c��tjd�t�}g}td�}	|�#t$r|j�YnwxYw|j
|j�|d}|j|j|�|jddd\}}|jg|�|jg|�y)z�
        C{Deferred.errback()} creates a failure from the current Python
        exception.  When Deferred.debug is not set no globals or locals are
        captured in that failure.
        Fr�r���N)rr�r rBrJr�rYrZr[r_�frames�r`r#r"�excr'�localz�globalzs       r9�test_errbackWithNoArgsNoDebugz+DeferredTests.test_errbackWithNoArgsNoDebugWs���	���5�!�$�J�����6�"��	��I���	�
�I�I�K�	��	���Q�X�X����t��������S�)��+�+�a�.���-��������V�$�����W�%��0�A�Ac��tjd�t�}g}td�}	|�#t$r|j�YnwxYw|j
|j�|d}|j|j|�|jddd\}}|jg|�|jg|�y)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�rryN)rr�r rBrJr�rYrZr[r_rz�assertNotEqualr{s       r9�test_errbackWithNoArgsz$DeferredTests.test_errbackWithNoArgsls���	���4� �$�J�����6�"��	��I���	�
�I�I�K�	��	���Q�X�X����t��������S�)��+�+�a�.���-�������B��'����B��(r�c�D�t�}|jd�tjd�dd�}|j	|�g}|j|j�|d}|jddd\}}|jg|�|jg|�y)��
        An error raised by a callback creates a Failure.  The Failure captures
        locals and globals if and only if C{Deferred.debug} is set.
        NFc��td���Nr��rB�rVs r9�
raiseErrorzHDeferredTests.test_errorInCallbackDoesNotCaptureVars.<locals>.raiseError��
���v�&�&r;rryrj)	r rOrr�r�rYrZrzr[�r`r#r�r"r'r}r~s       r9�&test_errorInCallbackDoesNotCaptureVarsz4DeferredTests.test_errorInCallbackDoesNotCaptureVars�s���
%�J��	�
�
�4��
���5�!�	'�	
�
�
�j�!���	���Q�X�X����t���+�+�a�.���-��������V�$�����W�%r;c�D�t�}|jd�tjd�dd�}|j	|�g}|j|j�|d}|jddd\}}|jg|�|jg|�y)r�NTc��td��r�r�r�s r9r�zODeferredTests.test_errorInCallbackCapturesVarsWhenDebugging.<locals>.raiseError�r�r;rryrj)	r rOrr�r�rYrZrzr�r�s       r9�-test_errorInCallbackCapturesVarsWhenDebuggingz;DeferredTests.test_errorInCallbackCapturesVarsWhenDebugging�s���
%�J��	�
�
�4��
���4� �	'�	
�
�
�j�!���	���Q�X�X����t���+�+�a�.���-�������B��'����B��(r;c�~�t�}t�}	|j�dd�}tj|�|j|�}tj|j��}|jt|�d�|jd|dd�|jd|dd�|jd|dd�|jd|dd�|jd|dd�|jd	|dd�y
#t$r|j	�Y��wxYw)��
        L{defer.inlineCallbacks} that re-raise tracebacks into their deferred
        should not lose their tracebacks.
        c3�K�|��y�w)aJ
            This is never called.
            It is only used as the decorated function.
            The resulting function is never called in this test.
            This is used to make sure that if we wrap
            an already failed deferred, inlineCallbacks
            will not add any extra traceback frames.
            NrG�r#s r9�icz8DeferredTests.test_inlineCallbacksTracebacks.<locals>.ic�s
�����G���r��
test_deferr�rrK�1 / 0�test_inlineCallbacksTracebacks�f.raiseException()N�r#r<r=zGenerator[Any, Any, None]�rKr �raiseExceptionrJr�r�inlineCallbacksr�r_�
extract_tb�getTracebackObjectr[r\�assertIn�r`r8r#r��
newFailure�tbs      r9r�z,DeferredTests.test_inlineCallbacksTracebacks�s��

� ��$�J��	�
����		�	���b�!��)�)�!�,�
�
�
!�
!�*�"?�"?�"A�
B������R��!�$��
�
�l�B�q�E�!�H�-����-�r�!�u�Q�x�8�����"�Q�%��(�+��
�
�l�B�q�E�!�H�-����9�2�a�5��8�D����-�r�!�u�Q�x�8��3�	�
�I�I�K�	���D�D<�;D<c��dd�}tj|��}t|tt�|j	d|j|��y)zM
        It can discover the return type of a first level coroutine.
        c��K�y�wrDrGrGr;r9�
returnsIntz4DeferredTests.test_fromCoroutine.<locals>.returnsInt�r�r�rINr�)r r4rr5r[r)r`r�r#s   r9�test_fromCoroutinez DeferredTests.test_fromCoroutine�sD��
	�
�"�"�:�<�0���A�x��}�%�����D�0�0��3�4r;c��d�dd|jdtg}|D]1}|jtjtj
|��3y)��
        L{Deferred.fromCoroutine} requires a coroutine object or a generator,
        and will reject things that are not that.
        c��|Sr3rGr}s r9r�zCDeferredTests.test_fromCoroutineRequiresCoroutine.<locals>.<lambda>����a�r;rITN��#test_fromCoroutineRequiresCoroutiner�assertRaises�NotACoroutineErrorr r4�r`�thingsThatAreNotCoroutines�things   r9r�z1DeferredTests.test_fromCoroutineRequiresCoroutine��V��
�
���4�4���
&
�"�0�	W�E����e�6�6��8N�8N�PU�V�	Wr;�GC works differently on PyPy.c��t�}tj|�}t|�}||_tj|�}|jd�~~|j
|��|j
|��y)z|
        A circular reference between a `Deferred` and its canceller
        is broken when the deferred is called.
        N)�DummyCanceller�weakref�refr rQrOr�)r`�	canceller�
weakCancellerrQ�weakDeferreds     r9�*test_canceller_circular_reference_callbackz8DeferredTests.test_canceller_circular_reference_callback�st��#�$�	����I�.�
�#+�9�"5��%�	���{�{�8�,�����$��
��	
���-�/�*����,�.�)r;c�X�t�}tj|�}t|�}||_tj|�}tt
d��}|j|�|j|�~~|j|��|j|��y)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 rQr/r�r�r�r�)r`r�r�rQr�rOs      r9�)test_canceller_circular_reference_errbackz7DeferredTests.test_canceller_circular_reference_errback
s���#�$�	����I�.�
�#+�9�"5��%�	���{�{�8�,���)�$@�A�B������!�	
���X�&���	
���-�/�*����,�.�)r;c���t�}tj|�}t|�}||_|jd��|j
d�~~|j|��y)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�Sr3)r r0s r9r�zKDeferredTests.test_canceller_circular_reference_non_final.<locals>.<lambda>>s��x�z�r;N)r�r�r�r rQr�rOr�)r`r�r�rQs    r9�+test_canceller_circular_reference_non_finalz9DeferredTests.test_canceller_circular_reference_non_final-sc��#�$�	����I�.�
�#+�9�"5��%�	�����1�2����$����	
���-�/�*r;Nr�)r5r<r6r<r=r�r5r<r6r<r=r|)r5r/r6r<r=r|)ZrDrErFr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrr	rrrrrrr$r(r,r6r9rArJrSrZrarfrmrprsryr�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrrr%r,r2r4r7rGrPrWrYr[rsrwrr�r�r�r�r�r��pyunit�skipIfr-r�r�r�rGr;r9r�r��s��B��-�+�A�I�Q�B�
5�A�"5�,"
�H%�('.�R?�"?�"&�&�"
@�B�$
@�
9�
K�:�"+� )�.)�
=�)�2�2�
=��
�6
�<3�,-�()� &� >�(
K�
?�1�(� >�"2�$
.�	6�E�D�2
�.*
�X%
�N-
�^,�.)�(>�(*�&1�4:�(/�:0�>;
�zF
�P'�
(�
'�
�.)�.B�Y�
�9?�v*�.&�*)�*&�()�("9�H
5�W�.�V�]�]�5�9�:�*�;�*�8�V�]�]�5�9�:�*�;�*�>�V�]�]�5�9�:�+�;�+r;r�c�(�eZdZUdZdZded<dd�Zy)r�z�
    A callable that does nothing.
    It is intended to be used just to get an object reference
    to support GC related tests.
    NzOptional[Deferred[Any]]rQc��y)zV
        This is not expected to be called as part of the current test suite.
        NrGr�s  r9�__call__zDummyCanceller.__call__Rr�r;)rQrer=r|)rDrErFrfrQ�__annotations__r�rGr;r9r�r�Is���)-�H�%�,�r;r�c���dg|z�g}t|�D]%}|fd�fd�
}|jt|����'�|fS)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.
    rc� ���|xxdz
cc<yrDrG)r#�n�cancelledStates  �r9r�z_setupRaceState.<locals>.cancelcs����1���"�r;�r�)r#rr�r5r=r|)rkrZr )�numDeferreds�dsr�r�r�s    @r9�_setupRaceStater�XsR����S�<�'�N�!#�B�
�<�
 �.��12�	#�	�	�	�(�V�,�-�.��2��r;c��eZdZdZeedd��edd����dd��Zeedd��edd����dd��Zeedd��edd����dd��Zdd	�Z	eed
d����dd��Z
y
)�	RaceTestsz
    Tests for L{race}.
    rr�)�	min_value�	max_value)�beforeWinner�afterWinnerc��t|dz|z�\}}t|�}t�}||j|�t	|j|�t
||f��dg|zdgzdg|zz}t	|t
|��y)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.
        rIrN)r�r*r<rOrrr)r`r�r�r�r��
raceResultrX�expectedCancelledStates        r9�test_successzRaceTests.test_successps��� -�\�A�-=��-K�L�����"�X�
��8��
�<��!�!�(�+�	�� � ��,��l�H�-�.�	
�
#$��|�!3�q�c�!9�Q�C�+�<M�!M�����+�,�	
r;c	�v�t|dz|z�\}}ttd��}t|�}|D]}|j	|��|j|t�}t|jjt|gt|�z��t|tdgt|�z��y)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.
        rIr�rN)r�r/r�r*r�r�r&rr_rcrr\)	r`r�r�r�r�rOr�r#�
actualFailures	         r9�test_failurezRaceTests.test_failure�s��� -�\�A�-=��-K�L�����)�$@�A�B���"�X�
��	�A�
�I�I�g��	��,�,�Z��F�
�����(�(��g�Y��R��(�)�	
�	���a�S�3�r�7�]�#�	
r;c�,�t|dz|z�D�cgc]}t���}}t|�}||jd�||dzjd�|j	|�t|j
�t��ycc}w)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.
        r�NrI)rkr r*rOrrrlr)r`r�r�r�r�r�s      r9�test_resultAfterCancelz RaceTests.test_resultAfterCancel�s���!&�l�Q�&6��&D� E�$
��H�J�$
��$
��"�X�
�
�<��!�!�$�'�
�<�!���%�%�d�+����Z�(��D�*�*�,�e�g�6��$
s�Bc���t�}|td���g}t�}t|�}|j|�t	|j|�t
d|f��y)z~
        If one of the input Deferreds has a cancel function that fires it
        with success, nothing bad happens.
        c�4�|jt��Sr3)rOr<r�s r9r�z1RaceTests.test_resultFromCancel.<locals>.<lambda>�s�����F�H�)=�r;r�rN)r r<r*rOrrr)r`�winnerr�rXr�s     r9�test_resultFromCancelzRaceTests.test_resultFromCancel�sZ��
$,�:����=�>�&
���8���"�X�
�����!��D�(�(��4�h��8�}�6M�Nr;rI)r�c��t|�\}}t|�}|j�t|t	dg|z��|j|t�y)zu
        If the result of L{race} is cancelled then all of the L{Deferred}s
        passed in are cancelled.
        rIN)r�r*r�rrr�r&)r`r�r�r�r�s     r9�test_cancelzRaceTests.test_cancel�sN��-�\�:�����"�X�
������N�H�a�S�<�-?�$@�A����Z��6r;N)r�r5r�r5r=r|r�)r�r5r=r|)rDrErFrfrrr�r�r�r�r�rGr;r9r�r�ks�������Q�7��q�A�6��
�	�
�>���Q�7��q�A�6��
�	�
�:���Q�7��q�A�6��7�	�7�&O� ���Q�7��7��7r;r�c�(�eZdZdZdd�Zdd�Zdd�Zy)�FirstErrorTestsz"
    Tests for L{FirstError}.
    c���td�}	|�#t$r
t�}YnwxYwtj|d�}|jt
|�dt
|��d��y)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�zFirstError[#3, �]N)rRrJr/rr�r[rU�r`r|r8rns    r9rWzFirstErrorTests.test_repr�sc��
��%��	��I���	��	�A�	��� � ��A�&������e����S�	�{�!�&D�E���%�%c���td�}	|�#t$r
t�}YnwxYwtj|d�}|jt
|�dt
|��d��y)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|zFirstError[#5, r�N)rRrJr/rr�r[r�r�s    r9�test_strzFirstErrorTests.test_str�sc��
��%��	��I���	��	�A�	��� � ��A�&������U���s�1�v�h�a�%@�Ar�c���	ddztjd�}tj|d�}	t	d��#t$r
t�}Y�MwxYw#t$r
t�}YnwxYwtj|d�}|j||k(�|j
||k(�|j||k7�|j
||k7�|j
|dk(�y)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.
        rIr�
�bar�	r�N)rJr/rr�rRr]r)r`�firstFailure�one�
anotherOne�
secondFailure�anothers      r9�test_comparisonzFirstErrorTests.test_comparison
s���	%�
��F����|�R�0���%�%�l�B�7�
�	&��U�#�#���	%�"�9�L�	%���	&�#�I�M�	&���"�"�=�!�4������z�)�*�������(�����w��'�����
�*�+�������#s �?�A�A�A�A.�-A.Nr�)rDrErFrfrWr�r�rGr;r9r�r��s���F�B�$r;r�c��eZdZdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Z	dd�Z
dd	�Zdd
�Zdd�Z
dd�Zdd
�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zy)�AlreadyCalledTestsc�`�tj�|_tjd�y�NT)rr��_deferredWasDebuggingr�r�s r9r�zAlreadyCalledTests.setUp+s!��%*�%7�%7�%9��"�
���4� r;c�B�tj|j�yr3)rr�rr�s r9�tearDownzAlreadyCalledTests.tearDown/s��
���4�5�5�6r;c��yr3rGr�s   r9r�zAlreadyCalledTests._callback2���r;c��yr3rGr�s   r9r�zAlreadyCalledTests._errback5rr;c�&�|jd�y)Nr�rNr8s  r9�_call_1zAlreadyCalledTests._call_18���	�
�
�7�r;c�&�|jd�y)N�twicerNr8s  r9�_call_2zAlreadyCalledTests._call_2;rr;c�H�|jtt���yr3�r�r/rjr8s  r9�_err_1zAlreadyCalledTests._err_1>���	�	�	�'�,�.�)�*r;c�H�|jtt���yr3r
r8s  r9�_err_2zAlreadyCalledTests._err_2Arr;c���t�}|j|j|j�|j	|�|jtj|j|�yr3)	r r�r�r�rr�r�AlreadyCalledErrorrr8s  r9�testAlreadyCalled_CCz'AlreadyCalledTests.testAlreadyCalled_CCDsF��#�:��	���t�~�~�t�}�}�5����Q�����%�2�2�D�L�L�!�Dr;c���t�}|j|j|j�|j	|�|jtj|j|�yr3)	r r�r�r�rr�rrrr8s  r9�testAlreadyCalled_CEz'AlreadyCalledTests.testAlreadyCalled_CEJsF��#�:��	���t�~�~�t�}�}�5����Q�����%�2�2�D�K�K��Cr;c���t�}|j|j|j�|j	|�|jtj|j|�yr3)	r r�r�r�rr�rrrr8s  r9�testAlreadyCalled_EEz'AlreadyCalledTests.testAlreadyCalled_EEPsF��#�:��	���t�~�~�t�}�}�5����A�����%�2�2�D�K�K��Cr;c���t�}|j|j|j�|j	|�|jtj|j|�yr3)	r r�r�r�rr�rrrr8s  r9�testAlreadyCalled_ECz'AlreadyCalledTests.testAlreadyCalled_ECVsF��#�:��	���t�~�~�t�}�}�5����A�����%�2�2�D�L�L�!�Dr;c��d}|D]1}|jd|z�s�|jd|z�s�-|dz
}�3|j||k(�y)Nrz %s:z %srI)r~�endswithr])r`�linetype�func�linesrX�count�lines       r9�_countzAlreadyCalledTests._count\sR�����	�D����v��0�1�d�m�m�E�D�L�6Q���
��	�	
�����)�*r;c�X�|jdjd�}|jd||d�|jdd|d�|jdd|d�|jdd|d�|jdd|d�|jd	||d�|jd	||d�y)
Nr�
�CrIrrrr�I)r5�splitr")r`r��caller�invoker1�invoker2rs      r9�_checkzAlreadyCalledTests._checkcs������q�	����%��	
���C����*����C��E�1�-����C��E�1�-����C��5�!�,����C��5�!�,����C��5�!�,����C��5�!�,r;c�4�t�}|j|j|j�|j	|�	|j|�|j
d�y#tj$r}|j|ddd�Yd}~yd}~wwxYw)N�2second callback failed to raise AlreadyCalledError�testAlreadyCalledDebug_CCrr)
r r�r�r�rrr'rrr+�r`r#r�s   r9r.z,AlreadyCalledTests.testAlreadyCalledDebug_CCqsz��#�:��	���t�~�~�t�}�}�5����Q��	L��L�L��O�
�I�I�J�K���'�'�	N��K�K��6�	�9�M�M��	N���A&�&B�9B�Bc�4�t�}|j|j|j�|j	|�	|j|�|j
d�y#tj$r}|j|ddd�Yd}~yd}~wwxYw)N�1second errback failed to raise AlreadyCalledError�testAlreadyCalledDebug_CErr)
r r�r�r�rrr'rrr+r/s   r9r3z,AlreadyCalledTests.testAlreadyCalledDebug_CE|sz��#�:��	���t�~�~�t�}�}�5����Q��	K��K�K��N�
�I�I�I�J���'�'�	M��K�K��6�	�8�L�L��	M�r0c�4�t�}|j|j|j�|j	|�	|j|�|j
d�y#tj$r}|j|ddd�Yd}~yd}~wwxYw)Nr-�testAlreadyCalledDebug_ECrr)
r r�r�r�rrr'rrr+r/s   r9r5z,AlreadyCalledTests.testAlreadyCalledDebug_EC�sz��#�:��	���t�~�~�t�}�}�5����A��	L��L�L��O�
�I�I�J�K���'�'�	M��K�K��6��)�L�L��	M�r0c�4�t�}|j|j|j�|j	|�	|j|�|j
d�y#tj$r}|j|ddd�Yd}~yd}~wwxYw)Nr2�testAlreadyCalledDebug_EErr)
r r�r�r�rrr'rrr+r/s   r9r7z,AlreadyCalledTests.testAlreadyCalledDebug_EE�sz��#�:��	���t�~�~�t�}�}�5����A��	K��K�K��N�
�I�I�I�J���'�'�	L��K�K��6��(�K�K��	L�r0c�l�tjd�t�}|j|j|j
�|j
|�	|j|�|jd�y#tj$r%}|j|j�Yd}~yd}~wwxYw)NFr-)rr�r r�r�r�rrr'rrr5r/s   r9�testNoDebuggingz"AlreadyCalledTests.testNoDebugging�s���
���5�!�#�:��	���t�~�~�t�}�}�5����Q��	L��L�L��O�
�I�I�J�K���'�'�	%����Q�V�V�$�$��	%�s�A;�;B3�B.�.B3c�`�tjd�t�}|jd��tjd�|j	d�tjd�t�}|j	d�tjd�|jd��y)NFc��yr3rG�r
s r9r�z8AlreadyCalledTests.testSwitchDebugging.<locals>.<lambda>�r�r;Tc��yr3rGr<s r9r�z8AlreadyCalledTests.testSwitchDebugging.<locals>.<lambda>�r�r;)rr�r �addBothrOr8s  r9�testSwitchDebuggingz&AlreadyCalledTests.testSwitchDebugging�s|��	���5�!�$�J��	�	�	�"�#�
���4� �	�
�
�4��
���5�!��J��	�
�
�4��
���4� �	�	�	�"�#r;Nr�r�)r#rQr=r|)
rr�rr�rr�rXr5r=r|)
r�r�r(r�r)r�r*r�r=r|)rDrErFr�rr�r�rrrrrrrrr"r+r.r3r5r7r9r?rGr;r9r�r�*so��!�7�
�
���+�+�E�D�D�E�+�-�	L�	K�	L�	K�
L�
$r;r�c��eZdZdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Z	dd�Z
dd	�Zdd
�Zdd�Z
dd�Zdd
�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zy)�DeferredCancellerTestsc�<�d|_d|_d|_d|_yr�)r�r�r��cancellerCallCountr�s r9r�zDeferredCancellerTests.setUp�s"��.2���15���/3���"#��r;c�<�|j|jd�y)Nrh)r�rCr�s r9rzDeferredCancellerTests.tearDown�s���
�
�d�-�-�v�6r;c��||_|Sr3r��r`�datas  r9r�z DeferredCancellerTests._callback�s��#����r;c��||_yr3r�rFs  r9r�z!DeferredCancellerTests._callback2�s
�� $��r;c��||_yr3r�)r`rns  r9r�zDeferredCancellerTests._errback�s
��#��r;c�>�t�}|j|j|j�|j	�|j
�J�|j
|j
jtj�|j|j�y)zy
        A L{Deferred} without a canceller must errback with a
        L{defer.CancelledError} and not callback.
        N)r r�r�r�r�r�r[r�rrr�r�r8s  r9�test_noCancellerz'DeferredCancellerTests.test_noCanceller�sr��
%�J��	���t�~�~�t�}�}�5�	���
��"�"�.�.�.�����,�,�1�1�5�3G�3G�H����$�.�.�/r;c�b�t�}|j|j|j�|j	�|jd�|j
tj|j
d�|j
tj|jt��y)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�rOr�rrr�r�r8s  r9�!test_raisesAfterCancelAndCallbackz8DeferredCancellerTests.test_raisesAfterCancelAndCallback�st��%�J��	���t�~�~�t�}�}�5�	���
�	
�
�
�4��	
���%�2�2�A�J�J��E����%�2�2�A�I�I�y�{�Kr;c�r�t�}|j|j|j�|j	�|jt
��|jtj|jd�|jtj|j
t
��y)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�rrrOr8s  r9� test_raisesAfterCancelAndErrbackz7DeferredCancellerTests.test_raisesAfterCancelAndErrback�sv��%�J��	���t�~�~�t�}�}�5�	���
�	
�	�	�)�+��	
���%�2�2�A�J�J��E����%�2�2�A�I�I�y�{�Kr;c��t�}|j|j|j�|j	�|j
}|j
d�|j	�|j||j
�y)z�
        A L{Deferred} without a canceller, when cancelled and then
        callbacked, ignores multiple cancels thereafter.
        N)r r�r�r�r�r�rOrm�r`r#�currentFailures   r9�5test_noCancellerMultipleCancelsAfterCancelAndCallbackzLDeferredCancellerTests.test_noCancellerMultipleCancelsAfterCancelAndCallback�s^��
%�J��	���t�~�~�t�}�}�5�	���
��,�,��	�
�
�4��	���
��
�
�n�d�&9�&9�:r;c��t�}|j|j|j�|j	�|j
�J�|j
|j
jtj�|j
}|jt��|j
|j
jtj�|j	�|j||j
�y)z�
        A L{Deferred} without a canceller, when cancelled and then
        errbacked, ignores multiple cancels thereafter.
        N)
r r�r�r�r�r�r[r�rrr�rBrmrQs   r9�4test_noCancellerMultipleCancelsAfterCancelAndErrbackzKDeferredCancellerTests.test_noCancellerMultipleCancelsAfterCancelAndErrback	s���
%�J��	���t�~�~�t�}�}�5�	���
��"�"�.�.�.�����,�,�1�1�5�3G�3G�H��,�,��	�	�	�,�.�!�����,�,�1�1�5�3G�3G�H�	���
��
�
�n�d�&9�&9�:r;c�x�t�}|j|j|j�|j	�|j
�J�|j
|j
jtj�|j
}|j	�|j||j
�y)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�r[r�rrrmrQs   r9�test_noCancellerMultipleCancelz5DeferredCancellerTests.test_noCancellerMultipleCancel	s���%�J��	���t�~�~�t�}�}�5�	���
��"�"�.�.�.�����,�,�1�1�5�3G�3G�H��,�,��	���
��
�
�n�d�&9�&9�:r;c����d�fd�}t|��}|j�j�j�|j	��j
�J��j
�j
jtj��j
}|j	��j|�j
��j
�jd�y)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.
        c�0���xjdz
c_yrD�rC�r#r`s �r9r�zCDeferredCancellerTests.test_cancellerMultipleCancel.<locals>.cancel5	�����#�#�q�(�#r;r�NrI�r#rr=r|)r r�r�r�r�r�r[r�rrrmrC)r`r�r#rRs`   r9�test_cancellerMultipleCancelz3DeferredCancellerTests.test_cancellerMultipleCancel,	s����	)�%�v�6��	���t�~�~�t�}�}�5�	���
��"�"�.�.�.�����,�,�1�1�5�3G�3G�H��,�,��	���
��
�
�n�d�&9�&9�:�����0�0�!�4r;c���d�fd�}t|��}|j�j�j�|j	��j�jd��j�J��j�jjtj��jtj|jd��jtj|jt��y)z�
        Verify that a L{Deferred} calls its specified canceller when
        it is cancelled, and that further call/errbacks raise
        L{defer.AlreadyCalledError}.
        c�0���xjdz
c_yrDrZr[s �r9r�z;DeferredCancellerTests.test_simpleCanceller.<locals>.cancelI	r\r;r�rINr])r r�r�r�r�r[rCr�r�rrr�rrOr�r��r`r�r#s`  r9�test_simpleCancellerz+DeferredCancellerTests.test_simpleCancellerB	s����	)�%�v�6��	���t�~�~�t�}�}�5�	���
�����0�0�!�4��"�"�.�.�.�����,�,�1�1�5�3G�3G�H�	
���%�2�2�A�J�J��E����%�2�2�A�I�I�y�{�Kr;c����d��fd�}t|����j�j�j��j	�y)zQ
        Verify that a canceller is given the correct deferred argument.
        c�*���j|��yr3)rm)r�r#r`s ��r9r�z8DeferredCancellerTests.test_cancellerArg.<locals>.cancel\	s����M�M�"�a� r;r�N)r�rr=r|)r r�r�r�r�ras` @r9�test_cancellerArgz(DeferredCancellerTests.test_cancellerArgW	s3���
	!�%�v�6��	���t�~�~�t�}�}�5�	���
r;c�^��d�fd�}t|��}|j�j�j�|j	d�|j��j
�jd��j�j��j
�jd�y)zo
        Test that cancelling a deferred after it has been callbacked does
        not cause an error.
        c�b���xjdz
c_|jt��yrD�rCr�rBr[s �r9r�z?DeferredCancellerTests.test_cancelAfterCallback.<locals>.canceli	�"����#�#�q�(�#�
�I�I�l�n�%r;r�zbiff!rNr])r r�r�r�rOr�r[rCr�r�r�ras`  r9�test_cancelAfterCallbackz/DeferredCancellerTests.test_cancelAfterCallbackc	s���	&�$�f�5��	���t�~�~�t�}�}�5�	�
�
�7��	���
�����0�0�!�4����$�-�-�.�����-�-�w�7r;c���d�fd�}t|��}|j�j�j�|j	t��|j
��j�jd��j�J��j�jjt
��j�j�y)z�
        Test that cancelling a L{Deferred} after it has been errbacked does
        not result in a L{defer.CancelledError}.
        c�b���xjdz
c_|jt��yrDrhr[s �r9r�z>DeferredCancellerTests.test_cancelAfterErrback.<locals>.cancel{	rir;r�rNr])
r r�r�r�r�rBr�r[rCr�r�r�r�ras`  r9�test_cancelAfterErrbackz.DeferredCancellerTests.test_cancelAfterErrbacku	s����	&�%�v�6��	���t�~�~�t�}�}�5�	�	�	�,�.�!�	���
�����0�0�!�4��"�"�.�.�.�����,�,�1�1�<�@����$�.�.�/r;c�>��d�fd�}t|��}|j�j�j�|j	��j�jd��j�J��j�jjt�y)z?
        Test a canceller which errbacks its deferred.
        c�b���xjdz
c_|jt��yrDrhr[s �r9r�zADeferredCancellerTests.test_cancellerThatErrbacks.<locals>.cancel�	rir;r�rINr])
r r�r�r�r�r[rCr�r�rBras`  r9�test_cancellerThatErrbacksz1DeferredCancellerTests.test_cancellerThatErrbacks�	sw���
	&�%�v�6��	���t�~�~�t�}�}�5�	���
�����0�0�!�4��"�"�.�.�.�����,�,�1�1�<�@r;c�<��d�fd�}t|��}|j�j�j�|j	��j�jd��j�jd��j�j�y)z<
        Test a canceller which calls its deferred.
        c�R���xjdz
c_|jd�y)NrI�hello!)rCrOr[s �r9r�zBDeferredCancellerTests.test_cancellerThatCallbacks.<locals>.cancel�	s ����#�#�q�(�#�
�J�J�x� r;r�rIrsNr])
r r�r�r�r�r[rCr�r�r�ras`  r9�test_cancellerThatCallbacksz2DeferredCancellerTests.test_cancellerThatCallbacks�	ss���
	!�%�v�6��	���t�~�~�t�}�}�5�	���
�����0�0�!�4�����-�-�x�8����$�-�-�.r;c�����d�fd�}d�fd�}t|���t|��}|jd�|j�fd��|j�|j	�j
�j��j�jd��j�J��j�jjtj�y)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.
        c�0���xjdz
c_yrDrZr[s �r9�innerCancelzEDeferredCancellerTests.test_cancelNestedDeferred.<locals>.innerCancel�	r\r;c�(���jd�y�NF)r]r[s �r9r�z@DeferredCancellerTests.test_cancelNestedDeferred.<locals>.cancel�	s����O�O�E�"r;r�Nc����Sr3rG)rGr1s �r9r�zBDeferredCancellerTests.test_cancelNestedDeferred.<locals>.<lambda>�	s���1�r;rIr])
r rOr�r�r�r�r�r[rCr�r�rr)r`rwr�r�r1s`   @r9�test_cancelNestedDeferredz0DeferredCancellerTests.test_cancelNestedDeferred�	s����	)�	#�%�{�;��$�v�6��	�
�
�4��	�
�
�n�%�	���
�	���t�~�~�t�}�}�5�����0�0�!�4��"�"�.�.�.�����,�,�1�1�5�3G�3G�Hr;Nr�)rGr�r=r�)rGr�r=r|)rnr/r=r|)rDrErFr�rr�r�r�rKrMrOrSrUrWr^rbrerjrmrprtr{rGr;r9rArA�sf��$�7��%�$�
0�L�"L�"
;�;�$
;�5�,L�*
�8�$0�&A� /� Ir;rAc�h�eZdZdZdd�Zdd�Zdd�Zdd�Zdd�Zdd�Z	dd�Z
dd	�Zdd
�Zdd�Z
dd�Zy
)�LogTestsz+
    Test logging of unhandled errors.
    c�d�g|_tj|jj�y)z<
        Add a custom observer to observer logging.
        N)�cr,r}rZr�s r9r�zLogTests.setUp�	s��(*���������
�
�&r;c�V�tj|jj�y)z&
        Remove the observer.
        N)r,�removeObserverrrZr�s r9rzLogTests.tearDown�	s��	���4�6�6�=�=�)r;c�L�|jD�cgc]
}|ds�	|��c}Scc}w)N�isError�r)r`r�s  r9�
_loggedErrorszLogTests._loggedErrors�	s ���6�6�2�a�Q�y�\��2�2��2s�
!�!c��|j�}|jt|�d�|ddjt�|jt�y)zV
        Check the output of the log observer to see if the error is present.
        r�rIrON)r�r[r\rNr3rl)r`�c2s  r9r+zLogTests._check�	sM���
�
�
!������R��!�$�
�1��i����/�0����0�1r;c��t�jd��jd�tj�|j�y)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��ddzSr/rGr}s r9r�z(LogTests.test_errorLog.<locals>.<lambda>�	�
���a��r;rIN)r r�rO�gc�collectr+r�s r9�
test_errorLogzLogTests.test_errorLog�	s2��	�
���/�0�9�9�!�<�
�
�
�����
r;c�b�dd�}|�tj�|j�y)zD
        Same as L{test_errorLog}, but with an inner frame.
        c�^�t�}|jd��|jd�y)Nc��ddzSr/rGr}s r9r�z`LogTests.test_errorLogWithInnerFrameRef.<locals>._subErrorLogWithInnerFrameRef.<locals>.<lambda>�	�
��A��F�r;rI)r r�rOr�s r9�_subErrorLogWithInnerFrameRefzNLogTests.test_errorLogWithInnerFrameRef.<locals>._subErrorLogWithInnerFrameRef�	s!��'�z�A�
�M�M�*�+�
�J�J�q�Mr;Nr��r�r�r+)r`r�s  r9�test_errorLogWithInnerFrameRefz'LogTests.test_errorLogWithInnerFrameRef�	s!��
	�
	&�'�
�
�
�����
r;c�b�dd�}|�tj�|j�y)zQ
        Same as L{test_errorLogWithInnerFrameRef}, plus create a cycle.
        c�v�t�}|fdd�}|j|�||_|jd�y)Nc��ddzSr/rG)r~r#s  r9�unusedCyclezgLogTests.test_errorLogWithInnerFrameCycle.<locals>._subErrorLogWithInnerFrameCycle.<locals>.unusedCycle
s���A�v�
r;rI)r~r5r#�
Deferred[int]r=r5)r r��_drO)r#r�s  r9�_subErrorLogWithInnerFrameCyclezRLogTests.test_errorLogWithInnerFrameCycle.<locals>._subErrorLogWithInnerFrameCycle�	s1��'�z�A�78�
�
�M�M�+�&��A�D�
�J�J�q�Mr;Nr�r�)r`r�s  r9� test_errorLogWithInnerFrameCyclez)LogTests.test_errorLogWithInnerFrameCycle�	s!��
		�	(�)�
�
�
�����
r;c��t�jd��jd�tj�|j�|j
dt|j��tj|jd�}|�J�d}|j|j|�d|���y)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��ddzSr/rGr}s r9r�z.LogTests.test_errorLogNoRepr.<locals>.<lambda>
r�r;rIr����Nr{� Expected message starting with: )
r r�rOr�r�r+r[r\rr,rvr]r~)r`�msgrXs   r9�test_errorLogNoReprzLogTests.test_errorLogNoRepr
s���
	�
���/�0�9�9�!�<�
�
�
�����
�����C����K�(��#�#�D�F�F�2�J�/������0������N�N�8�$�.�x�l�;�	
r;c�D�dd�}|�tj�|j�|jdt	|j
��t
j|j
d�}|�J�d}|j|j|�d|���y)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.
        c�l�t�}d|_|jd��|jd�y)NTc��ddzSr/rGr}s r9r�z?LogTests.test_errorLogDebugInfo.<locals>.doit.<locals>.<lambda>*
r�r;rI)r �debugr�rOr�s r9�doitz-LogTests.test_errorLogDebugInfo.<locals>.doit'
s(��'�z�A��A�G�
�M�M�*�+�
�J�J�q�Mr;r�r�Nz
(debug:  Ir�r�)
r�r�r+r[r\rr,rvr]r~)r`r�r�rXs    r9�test_errorLogDebugInfozLogTests.test_errorLogDebugInfo 
s���	�	
��
�
�
�����
�����C����K�(��#�#�D�F�F�2�J�/������������N�N�8�$�.�x�l�;�	
r;c��t�}|jd��|jd�g}g}|j|j|j�|j|g�|jt
|�d�|djt�~~~tj�|j|j�g�y)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�>�tjtd��S)N�zoop)rr'rjr<s r9r�z3LogTests.test_chainedErrorCleanup.<locals>.<lambda>A
s��%�*�*�\�&�-A�"B�r;NrIr)r r�rOr�rZr[r\rNr�r�r�r�)r`r#rerrs    r9�test_chainedErrorCleanupz!LogTests.test_chainedErrorCleanup:
s���%�J��	�
�
�B�C�	�
�
�4��!�� "��	���w�~�~�v�}�}�5�����"�%�����V��a�(��q�	���y�!�
�V�Q�	�
�
������+�+�-�r�2r;c���tjtd��}t��|�J�|j	�fd��dx�}tj�|j|j�g�y)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 noNc����Sr3rG)rV�goods �r9r�z6LogTests.test_errorClearedByChaining.<locals>.<lambda>a
s���t�r;)	rr'r�r rYr�r�r[r�)r`�badr�s  @r9�test_errorClearedByChainingz$LogTests.test_errorClearedByChainingV
sd���).�
�
�9�W�3E�(F��)1����������+�,����s�
�
�
������+�+�-�r�2r;Nr�)r=zList[Dict[str, Any]])rDrErFrfr�rr�r+r�r�r�r�r�r�r�rGr;r9r}r}�	s>���'�*�3�2����(
�&
�43�83r;r}c�,�eZdZdd�Zdd�Zdd�Zdd�Zy)�DeferredListEmptyTestsc��d|_yr�)�callbackRanr�s r9r�zDeferredListEmptyTests.setUpj
s
����r;c�P�tg�}|j|j�y)zTesting empty DeferredList.N)r"r��cb_empty)r`r�s  r9�testDeferredListEmptyz,DeferredListEmptyTests.testDeferredListEmptym
s��9E�b�9I��
���t�}�}�%r;c�6�d|_|jg|�yrD)r�r[)r`�ress  r9r�zDeferredListEmptyTests.cb_emptyr
s���������S�!r;c�<�|j|jd�y)NzCallback was never run.)r]r�r�s r9rzDeferredListEmptyTests.tearDownv
s������(�(�*C�Dr;Nr�)r�zList[Tuple[bool, object]]r=r|)rDrErFr�r�r�rrGr;r9r�r�i
s���&�
"�Er;r�c�l�eZdZdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Z	dd�Z
dd	�Zdd
�Zdd�Z
dd�Zy
)�OtherPrimitivesTestsc�.�|xjdz
c_yrD��counter�r`ros  r9�_incrzOtherPrimitivesTests._incr{
s�������r;c��d|_yr�r�r�s r9r�zOtherPrimitivesTests.setUp~
s	����r;c�T�
�t�}|j�j|j�|j	|j
�|j
|jd�|j�j|j�|j	|j
�|j
|jd�|j�|j	|j
�|j
|jd�|j�|j|j
�|j
|jd�|jt|j�t�}t�}t�}d�
dd	�
fd�
}|j|||��}|j	|j
�|j
�
|�|j|�|j�j|j�|j	|j
�|j
|jd�|j|�|j
�
|�|j	|j
�|j
|jd�|j�j!|�}|j#�|j%�
t&�|j
t)t&�
�j*t,j.�|j�|j|j
�d
d�}dd�}	t1|j|�tt2�t1|j|	�tt2�y)NrIr�c���|�|Sr3rG)�resultValue�returnValueros  �r9�helperz-OtherPrimitivesTests.testLock.<locals>.helper�
s��� �F��r;)r�r�r�c�,�tjd�Sr��rr rGr;r9r�z1OtherPrimitivesTests.testLock.<locals>.returnsInt�
s���=�=��#�#r;c��K�y�wrDrGrGr;r9�returnsCoroIntz5OtherPrimitivesTests.testLock.<locals>.returnsCoroInt�
r�r�r3)r�r<r�r<r=r<)r=r�r�)r#�acquirer�r�r]�lockedr[r��releaserr�r��runr<r rOr>r�r4r/rr�rrrr5)r`�lock�firstUnique�secondUnique�controlDeferredr��resultDeferredr#r�r�ros          @r9�testLockzOtherPrimitivesTests.testLock�
s�����~������"�"�4�:�:�.�������$�������q�)�����"�"�4�:�:�.�������$�������q�)�����������$�������q�)�����������%�������q�)����)�T�X�X�.��h���x��,4�J��#'��	�
������"�
��	
������$������-��"�"�6�*�����"�"�4�:�:�.�������$�������q�)�� � ��.������.�������$�������q�)��L�L�N�"�"�6�*��	���
����f�g�.�����g�v�.�3�3�U�5I�5I�J�����������%�	$�	�	�D�H�H�Z�(�(�3�-�8��D�H�H�^�,�h�s�m�<r;c���d�fd�}t�}|j�}|j|�|j�y)z�
        When canceling a L{Deferred} from a L{DeferredLock} that already
        has the lock, the cancel should have no effect.
        c�(���jd�y�NzUnexpected errback call!�r'�r8r`s �r9�
failOnErrbackzHOtherPrimitivesTests.test_cancelLockAfterAcquired.<locals>.failOnErrback�
�����I�I�0�1r;N�r8r/r=r|)r#r�rYr�)r`r�r�r#s`   r9�test_cancelLockAfterAcquiredz1OtherPrimitivesTests.test_cancelLockAfterAcquired�
s1���	2��~���L�L�N��	���]�#�	���
r;c��t�}|j�|j�}|j�|j|tj
�y)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�rdrr)r`r�r#s   r9�test_cancelLockBeforeAcquiredz2OtherPrimitivesTests.test_cancelLockBeforeAcquired�
s>���~�������L�L�N��	���
��#�#�A�u�';�';�<r;c����
�d}t|�}t��d�d	��fd�}g}t�}|j||��}|j	|j
�|j	|j�|j|g�|j�|��jd�|j|j��|j|jd�d|_tdd|z�D]G}|j�j	|j�|j|j|��Ig�
d
�
fd�}d
�
fd�}	|j�j||	�}
|
j�|j�
dg�|j�j	|j�|j|j|�|j!�|j|j|dz�tdd|z�D]1}|j!�|j|j|dz��3y)Nr�c���|��Sr3rG)�argr��	helperArgs ��r9r�z2OtherPrimitivesTests.testSemaphore.<locals>.helper�
s����I�"�"r;)r�rIrc�(���jd�yryr��r<rs �r9r'z0OtherPrimitivesTests.testSemaphore.<locals>.fail�
s����N�N�5�!r;c�(���jd�yr�r�r�s �r9r z3OtherPrimitivesTests.testSemaphore.<locals>.succeed�
s����N�N�4� r;T)r�r<r=r�)r<r<r=r|)r%r r<r�r�rZr�r[rOr�rlr�rkr�r�r�r�)r`�N�semr�re�uniqueObjectr��ir'r r#r�r�rs           @@@r9�
testSemaphorez"OtherPrimitivesTests.testSemaphore�
s��������"��*2�*�� �	�	#�
!#���x������\��:���"�"�7�>�>�2��"�"�4�:�:�.�����"�%�����L�1�� � ��&����'�+�+�-�(�������q�)�����q�!�a�%��	.�A��K�K�M�%�%�d�j�j�1����T�\�\�1�-�	.���	"�	!�
�K�K�M�&�&�t�W�5��	���
�����4�&�)����
�!�!�$�*�*�-�������q�)����
�������q�1�u�-��q�!�a�%��	2�A��K�K�M����T�\�\�1�q�5�1�	2r;c�p�|jttd�|jttd�y)zz
        If the token count passed to L{DeferredSemaphore} is less than one
        then L{ValueError} is raised.
        rr�N)r�rRr%r�s r9�test_semaphoreInvalidTokensz0OtherPrimitivesTests.test_semaphoreInvalidTokenss*��
	
���*�&7��;����*�&7��<r;c���d�fd�}td�}|j�}|j|�|j�y)z�
        When canceling a L{Deferred} from a L{DeferredSemaphore} that
        already has the semaphore, the cancel should have no effect.
        c�(���jd�yr�r�r�s �r9r�zMOtherPrimitivesTests.test_cancelSemaphoreAfterAcquired.<locals>.failOnErrbackr�r;rINr�)r%r�rYr�)r`r�r�r#s`   r9�!test_cancelSemaphoreAfterAcquiredz6OtherPrimitivesTests.test_cancelSemaphoreAfterAcquireds4���	2� ��"���K�K�M��	���]�#�	���
r;c��td�}|j�|j�}|j�|j|tj
�y)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.
        rIN)r%r�r�rdrr)r`r�r#s   r9�"test_cancelSemaphoreBeforeAcquiredz7OtherPrimitivesTests.test_cancelSemaphoreBeforeAcquired&sA�� ��"�����
��K�K�M��	���
��#�#�A�u�';�';�<r;c� �d\}}t||�}g}t|�D]+}|j�j|j��-|jtj|j�t|�D]:}|j|�|j|tt|dz����<t|�D]:}|j||z�|j|tt|����<|jtj|jd�g}t|�D]V}|j�j|j�|j|tt|||zdz����Xt�}g}t|�D]+}|j�j|j��-t|�D]}|j|��|j|tt|���td��}|jtj|jd�td��}|jtj|j�y)N)r�r�rIr)�size)�backlog)r$rk�getr�rZr�r�QueueUnderflow�putr[�list�
QueueOverflow)r`r��M�queue�gottenr�s      r9�	testQueuezOtherPrimitivesTests.testQueue2s�����1�$1�!�Q�$7�����q��	3�A��I�I�K�#�#�F�M�M�2�	3����%�.�.��	�	�:��q��	9�A��I�I�a�L����V�T�%��A��,�%7�8�	9��q��	5�A��I�I�a�!�e�����V�T�%��(�^�4�	5�	
���%�-�-�u�y�y�$�?����q��	@�A��I�I�K�#�#�F�M�M�2����V�T�%��1�q�5�1�9�*=�%>�?�	@�������q��	3�A��I�I�K�#�#�F�M�M�2�	3��q��	�A��I�I�a�L�	������e�A�h��0��1�%�����%�-�-�u�y�y�$�?��a�(�����%�.�.��	�	�:r;c���d�fd�}t�}|j�}|j|�|jd�|j	�y)z�
        When canceling a L{Deferred} from a L{DeferredQueue} that already has
        a result, the cancel should have no effect.
        c�(���jd�yr�r�r�s �r9r�zOOtherPrimitivesTests.test_cancelQueueAfterSynchronousGet.<locals>.failOnErrback]r�r;Nr�)r$r�rYr�r�)r`r�r�r#s`   r9�#test_cancelQueueAfterSynchronousGetz8OtherPrimitivesTests.test_cancelQueueAfterSynchronousGetWs<���	2�&3�_���I�I�K��	���]�#�
�	�	�$��	���
r;c�<���t���j�}|j��j|tj
�d��fd�}|j
|�g}|j
|j��jt|�d�y)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.
        c�z���jd��j�j�jd�Sr3)r�r�r�rm)�ignorer�r`s ��r9r�z9OtherPrimitivesTests.test_cancelQueueAfterGet.<locals>.cbrs-���
�I�I�d�O��9�9�;�*�*�4�=�=�$�?�?r;rIN)rr<r=r�)
r$r�r�rdrrr�rZr[r\)r`r#r��doner�s`   @r9�test_cancelQueueAfterGetz-OtherPrimitivesTests.test_cancelQueueAfterGetfss���&3�_���I�I�K��	���
��#�#�A�u�';�';�<�	@�	
�
�
�b����	�
�
�d�k�k�"�����T��A�&r;Nrr�)rDrErFr�r�r�r�r�r�r�r�r�rrrrGr;r9r�r�z
sB����@=�D�
=�12�f=��
=�#;�J
�'r;r�c�X�eZdZdZdd�Zd
d�Zd
d�Zd
d�Zdd�Zd
d�Z	d
d�Z
dd	�Zdd
�Zy)�DeferredFilesystemLockTestsz8
    Test the behavior of L{DeferredFilesystemLock}
    c�v�t�|_t|j�|j��|_y)N)�	scheduler)r+�clockr!�mktempr�r�s r9r�z!DeferredFilesystemLockTests.setUp�s#���W��
�*�4�;�;�=�D�J�J�O��	r;c�:�|jjd��S)zI
        Test that the lock can be acquired when no lock is held
        rI��timeout)r��deferUntilLockedr�s r9�test_waitUntilLockedWithNoLockz:DeferredFilesystemLockTests.test_waitUntilLockedWithNoLock�s���y�y�)�)�!�)�4�4r;c��|j|jj��|jjd��}|j|tj
�|jjdgdz�|S)zs
        Test that the lock can not be acquired when the lock is held
        for longer than the timeout.
        g@rrIr�)r]r�r�
assertFailurer�TimeoutErrorr
�pumpr8s  r9�%test_waitUntilLockedWithTimeoutLockedzADeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutLocked�sc��
	
����	�	���(�)��I�I�&�&�s�&�3�����1�e�0�0�1��
�
�����b��!��r;c�^��d�fd�}�j�jj���jjd�jj��jjd��}|j
|��jjdgdz�|S)z|
        Test that a lock can be acquired while a lock is held
        but the lock is unlocked before our timeout.
        c�f��|jtj��jd�y)NzShould not have timed out)rNrrr'r�s �r9�	onTimeoutzVDeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutUnlocked.<locals>.onTimeout�s"���
�F�F�5�%�%�&��I�I�1�2r;rIr�rr�)r]r�r
�	callLater�unlockrrYr)r`rr#s`  r9�'test_waitUntilLockedWithTimeoutUnlockedzCDeferredFilesystemLockTests.test_waitUntilLockedWithTimeoutUnlocked�s}���	3�	
����	�	���(�)��
�
���Q��	�	� 0� 0�1��I�I�&�&�r�&�2��	���Y���
�
�����b��!��r;c�v�t|j��}|j|jt�y)zE
        Test that the default scheduler is set up properly.
        N)r!rr[�
_schedulerr�r`r�s  r9�test_defaultSchedulerz1DeferredFilesystemLockTests.test_defaultScheduler�s(��&�d�k�k�m�4��������'�2r;c�x�|jj�|jjd|jj�|jj	�}|jj	�}|j|tj�|jjd�|S)z|
        Test that an appropriate exception is raised when attempting
        to use deferUntilLocked concurrently.
        rI)	r�r
rrrrr�AlreadyTryingToLockError�advancer@s   r9�test_concurrentUsagez0DeferredFilesystemLockTests.test_concurrentUsage�s���
	
�	�	�����
�
���Q��	�	� 0� 0�1�
�Y�Y�
'�
'�
)��
�Y�Y�
'�
'�
)�����2�u�=�=�>��
�
���1���	r;c�4��d�fd�}�jj��jjd�jj��jj	�}|j|��jj
d�|S)zO
        Test that a DeferredFilesystemLock can be used multiple times
        c�p���jj��jj�}|Sr3)r�rr)r
r#r`s  �r9�lockAquiredzDDeferredFilesystemLockTests.test_multipleUsages.<locals>.lockAquired�s*����I�I�����	�	�*�*�,�A��Hr;rI)r
r<r=r�)r�r
rrrr�r%)r`r)r#s`  r9�test_multipleUsagesz/DeferredFilesystemLockTests.test_multipleUsages�sk���
	�
	
�	�	�����
�
���Q��	�	� 0� 0�1��I�I�&�&�(��	�
�
�k�"��
�
���1���r;c��|jj�|jj�}|jj}|�J�|j�|j	|j��|j
|jj�|j|tj�y)z�
        When cancelling a L{Deferred} returned by
        L{DeferredFilesystemLock.deferUntilLocked}, the
        L{DeferredFilesystemLock._tryLockCall} is cancelled.
        N)
r�r�_tryLockCallr�r�activer�r�rr)r`rQ�tryLockCalls   r9�test_cancelDeferUntilLockedz7DeferredFilesystemLockTests.test_cancelDeferUntilLocked�s���	
�	�	�����9�9�-�-�/���i�i�,�,���&�&�&���������+�+�-�.����$�)�)�0�0�1����X�u�';�';�<r;c��|jj�|jjd��}|jj}|�J�|j�|j	|j��|j
|jj�|j|tj�y)z�
        When cancel a L{Deferred} returned by
        L{DeferredFilesystemLock.deferUntilLocked}, if the timeout is
        set, the timeout call will be cancelled.
        rIrN)
r�r�_timeoutCallr�rr-r�r�rr)r`rQ�timeoutCalls   r9�&test_cancelDeferUntilLockedWithTimeoutzBDeferredFilesystemLockTests.test_cancelDeferUntilLockedWithTimeout�s���	
�	�	�����9�9�-�-�a�-�8���i�i�,�,���&�&�&���������+�+�-�.����$�)�)�0�0�1����X�u�';�';�<r;Nr�)r=r�)
rDrErFrfr�rrrr"r&r*r/r3rGr;r9r
r
~s5���P�5���(3��"�(
=�
=r;r
c��y)zj
    Private function to be used to pass as an alternate onTimeoutCancel value
    to timeoutDeferred
    �
OVERRIDDENrG)�v�ts  r9�
_overrideFuncr8�s��
r;c��eZdZdZdd�Zdd�Zdd�Zdd�Zdd�Zdd�Z	dd�Z
dd	�Zdd
�Zdd�Z
dd�Zdd
�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zy)�DeferredAddTimeoutTestsz7
    Tests for the function L{Deferred.addTimeout}
    c���t�}|jdt��|jd��|j	d�|jd|j
|��y)zu
        L{Deferred.addTimeout} returns its own L{Deferred} so it
        can be called in a callback chain.
        r|c��y)NrrGr0s r9r�z?DeferredAddTimeoutTests.test_timeoutChainable.<locals>.<lambda>r�r;Nr)r �
addTimeoutr+r�rOr[rr8s  r9�test_timeoutChainablez-DeferredAddTimeoutTests.test_timeoutChainablesO��
%�J��	���Q��� �	�
�
�&�'�	�
�
�4�������!5�!5�a�!8�9r;c�J���t�}t�}|jd|�t��d�d��fd�}|j|�|j	d�|jd��|j
�d�|jd�|j��y)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�Nrec���|��Sr3rG�re�dCallbacked�
innerDeferreds ��r9�
onCallbackzKDeferredAddTimeoutTests.test_successResultBeforeTimeout.<locals>.onCallback"����!�K� � r;r��rer�r=r�)	r+r r=r�rO�assertIsNotr[r%r�r`r
r#rDrBrCs    @@r9�test_successResultBeforeTimeoutz7DeferredAddTimeoutTests.test_successResultBeforeTimeouts�������#�:��	���R���)1�
�
�%)��	!�
	
�
�
�j�!�	�
�
�9��	
����{�+�����i�0�	�
�
�b�����M�*r;c�V���t�}t�}|jd|t��t��d�d��fd�}|j	|�|jd�|j
d��|j�d�|jd�|j��y)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���onTimeoutCancelNrec���|��Sr3rGrAs ��r9rDzQDeferredAddTimeoutTests.test_successResultBeforeTimeoutCustom.<locals>.onCallbackDrEr;r�rF)
r+r r=r8r�rOrGr[r%rrHs    @@r9�%test_successResultBeforeTimeoutCustomz=DeferredAddTimeoutTests.test_successResultBeforeTimeoutCustom4s�������#�:��	���R��
��>�)1�
�
�%)��	!�
	
�
�
�j�!�	�
�
�9��	
����{�+�����i�0�	�
�
�b�����M�*r;c����t�}t�}|jd|�t��d�td�}d��fd�}|j	|�|j|���J�|j
�t�|j�j|�|jd�|j��y)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'c���|��Sr3rG�r8�
dErrbackedrCs ��r9�	onErrbackzDDeferredAddTimeoutTests.test_failureBeforeTimeout.<locals>.onErrbackf�����J� � r;r��r8r/r=r�)r+r r=rRrYr�r4r/rmr_r%r�r`r
r#rnrSrRrCs     @@r9�test_failureBeforeTimeoutz1DeferredAddTimeoutTests.test_failureBeforeTimeoutVs�������$�J��	���R���)1�
�
�(,�
��6�"��	!�
	
���Y��	�	�	�%���%�%�%����j�'�2��
�
�j�&�&��.�	�
�
�b�����M�*r;c����t�}t�}|jd|t��t��d�t	d�}d��fd�}|j|�|j
|���J�|j�t�|j�j|�|jd�|j��y)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�rKNr'c���|��Sr3rGrQs ��r9rSzJDeferredAddTimeoutTests.test_failureBeforeTimeoutCustom.<locals>.onErrback�rTr;r�rU)
r+r r=r8rRrYr�r4r/rmr_r%rrVs     @@r9�test_failureBeforeTimeoutCustomz7DeferredAddTimeoutTests.test_failureBeforeTimeoutCustomys�������$�J��	���R��
��>�)1�
�
�(,�
��6�"��	!�
	
���Y��	�	�	�%���%�%�%����j�'�2��
�
�j�&�&��.�	�
�
�b�����M�*r;c���t�}t�}|jd|�|j|�|j	d�|j|tj�y)z�
        The L{Deferred} by default errbacks with a L{defer.TimeoutError}
        if it times out before callbacking or errbacking.
        r�r�N)r+r r=rr%r�rr�r`r
r#s   r9�
test_timedOutz%DeferredAddTimeoutTests.test_timedOut�sP��
���$�J��	���R������A��
�
�
�b�����Q�� 2� 2�3r;c���t�}t�}|jd|t��|j	|�|jd�|j
d|j|��y)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�rKr�r5N�r+r r=r8rr%r[rr\s   r9�test_timedOutCustomz+DeferredAddTimeoutTests.test_timedOutCustom�sY�����$�J��	���R��
��>����A��
�
�
�b������t�';�';�A�'>�?r;c���t�}td��}|jd|�|j|�|j	d�|j|j
|�d�y)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�$�|jd�S)N�I was cancelled!rNr�s r9r�zLDeferredAddTimeoutTests.test_timedOutProvidedCancelSuccess.<locals>.<lambda>�s��a�j�j�9K�.L�r;r�r�rcN)r+r r=rr%r[rr\s   r9�"test_timedOutProvidedCancelSuccessz:DeferredAddTimeoutTests.test_timedOutProvidedCancelSuccess�sZ�����#�$L�M��	���R������A��
�
�
�b������-�-�a�0�2D�Er;c���t�}td��t�fd��}|jd|�|j	|�|jd�|j
|t�}|j|j��y)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�&��|j��Sr3)r�)rrns �r9r�zLDeferredAddTimeoutTests.test_timedOutProvidedCancelFailure.<locals>.<lambda>�s���q�y�y��/?�r;r�r�N)	r+rRr r=rr%r�rmr_)r`r
r#r8rns    @r9�"test_timedOutProvidedCancelFailurez:DeferredAddTimeoutTests.test_timedOutProvidedCancelFailure�sm�������7�#��$�%?�@��	���R������A��
�
�
�b��� � ��J�/���
�
�a�g�g�u�%r;c����t�}t�}|jd|�t��d�d��fd�}|j|�|j	���J�|j�t�|j�jtj�|jd�|j��y)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�Nc���|��Sr3rG�r8�	dCanceledrCs ��r9rSzCDeferredAddTimeoutTests.test_cancelBeforeTimeout.<locals>.onErrback������I� � r;r�rU)
r+r r=rYr�r4r/rmr�rrr%r�r`r
r#rSrlrCs    @@r9�test_cancelBeforeTimeoutz0DeferredAddTimeoutTests.test_cancelBeforeTimeout�s�������$�J��	���R���)1�
�
��	�	!�
	
���Y��	���
��$�$�$����i��1��
�
�i�n�n�e�&:�&:�;�	�
�
�b�����M�*r;c����t�}t�}|jd|t��t��d�d��fd�}|j	|�|j���J�|j
�t�|j�jtj�|jd�|j��y)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�rKNc���|��Sr3rGrks ��r9rSzIDeferredAddTimeoutTests.test_cancelBeforeTimeoutCustom.<locals>.onErrback
rmr;r�rU)r+r r=r8rYr�r4r/rmr�rrr%rrns    @@r9�test_cancelBeforeTimeoutCustomz6DeferredAddTimeoutTests.test_cancelBeforeTimeoutCustom�s�������$�J��	���R��
��>�)1�
�
��	�	!�
	
���Y��	���
��$�$�$����i��1��
�
�i�n�n�e�&:�&:�;�	�
�
�b�����M�*r;c���t�}td��}|jd|t��|j	|�|jd�|j
d|j|��y)zu
        A custom translation function can handle a L{Deferred} with a
        custom cancellation function.
        c�6�|jtd��S)Nrf)r�rRr�s r9r�zVDeferredAddTimeoutTests.test_providedCancelCalledBeforeTimeoutCustom.<locals>.<lambda>(
s��q�y�y��G�9L�/M�r;r�rKr�r5Nr_r\s   r9�,test_providedCancelCalledBeforeTimeoutCustomzDDeferredAddTimeoutTests.test_providedCancelCalledBeforeTimeoutCustom"
s]��
���$�%M�N��	���R��
��>����A��
�
�
�b������t�';�';�A�'>�?r;c�n��t�}t�}d�d�fd�}|j|�|jd|�|j	d���J�|j�t�|j�jtj�|j|tj�y)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.
        Nc���|�|Sr3rG�r8rRs �r9r�zGDeferredAddTimeoutTests.test_errbackAddedBeforeTimeout.<locals>.errback<
�
����J��Hr;r�r��r8r/r=r/)r+r rYr=r%r4r/r_rrr�r�r`r
r#r�rRs    @r9�test_errbackAddedBeforeTimeoutz6DeferredAddTimeoutTests.test_errbackAddedBeforeTimeout0
s�������$�J���
�	�
	
���W��	���R���
�
�
�b���%�%�%����j�'�2����j�.�.��0D�0D�E����Q�� 2� 2�3r;c�P��t�}t�}d�d�fd�}|j|�|jd|�|j	d���J�|j�t�|j�jtj�|j|�y)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.
        Nc�H��|�|jtj�yr3)rNrrrxs �r9r�z]DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellation.<locals>.errbackX
s����J�
�F�F�5�'�'�(r;r�r�r�)r+r rYr=r%r4r/r_rrrr{s    @r9�4test_errbackAddedBeforeTimeoutSuppressesCancellationzLDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationL
s�������$�J���
�	)�
	
���W��	���R���
�
�
�b���%�%�%����j�'�2����j�.�.��0D�0D�E����Q�r;c�z��t�}t�}d�d�fd�}|j|�|jd|t�|jd���J�|j
�t�|j
�jtj�|jd|j|��y)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}.
        Nc���|�|Sr3rGrxs �r9r�zMDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutCustom.<locals>.errbackt
ryr;r�r�r5rz�
r+r rYr=r8r%r4r/r_rrr[rr{s    @r9�$test_errbackAddedBeforeTimeoutCustomz<DeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutCustomh
s�������$�J���
�	�
	
���W��	���R��
�.�
�
�
�b���%�%�%����j�'�2����j�.�.��0D�0D�E�����t�';�';�A�'>�?r;c�z��t�}t�}d�d�fd�}|j|�|jd|t�|jd���J�|j
�t�|j
�jtj�|jd|j|��y)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}.
        Nc�
��|�yr3rGrxs �r9r�zcDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationCustom.<locals>.errback�
s����Jr;r�r�r5r�r�r{s    @r9�:test_errbackAddedBeforeTimeoutSuppressesCancellationCustomzRDeferredAddTimeoutTests.test_errbackAddedBeforeTimeoutSuppressesCancellationCustom�
s�������$�J���
�	�	
���W��	���R��
�.�
�
�
�b���%�%�%����j�'�2����j�.�.��0D�0D�E�����t�';�';�A�'>�?r;c����t�}d�t�fd��}d�d�fd�}|j|�|jd|�|j	d�|j���|j
�|j|��y)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}.
        rc�&��|j��Sr3rN�r#rs �r9r�zSDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout.<locals>.<lambda>�
����a�j�j��.A�r;Nc���|�|Sr3rG�r_rBs �r9rOzSDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout.<locals>.callback�
�
����K��Lr;r�r��r_r�r=r�)r+r r�r=r%r[rmr�r`r
r#rOrBrs    @@r9�)test_callbackAddedToCancelerBeforeTimeoutzADeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeout�
sw��������#�$A�B����	�
	
�
�
�h��	���R���
�
�
�b������g�.��
�
�g�t�3�3�A�6�7r;c�&���t�}d�t�fd��}d�d	�fd�}|j|�|jd|t��|jd�|j
���|j
d|j|��y)
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.
        rc�&��|j��Sr3rNr�s �r9r�zYDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom.<locals>.<lambda>�
r�r;Nc���|�|Sr3rGr�s �r9rOzYDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom.<locals>.callback�
r�r;r�rKr�r5r�)r+r r�r=r8r%r[rr�s    @@r9�/test_callbackAddedToCancelerBeforeTimeoutCustomzGDeferredAddTimeoutTests.test_callbackAddedToCancelerBeforeTimeoutCustom�
s}��������#�$A�B����	�
	
�
�
�h��	���R��
��>�
�
�
�b������g�.�����t�';�';�A�'>�?r;Nr�)rDrErFrfr>rIrNrWrZr]r`rdrhrorrrur|rr�r�r�r�rGr;r9r:r:sm���	:�+�B +�D!+�F"+�H4�@�"
F�&�"!+�F!+�F@�4�8 �8@�8@�68�8@r;r:c�0�eZdZdZdd�Zdd�Zdd�Zdd�Zy)�EnsureDeferredTestsz&
    Tests for L{ensureDeferred}.
    c�R�t�}t|�}|j||�y)zK
        L{ensureDeferred} will pass through a Deferred unchanged.
        N)r r)rmr@s   r9�test_passesThroughDeferredsz/EnsureDeferredTests.test_passesThroughDeferreds�
s#��&�Z��
�B�
���
�
�b�"�r;c��|jtj�5td�ddd�y#1swYyxYw)zr
        Passing L{ensureDeferred} a non-coroutine and a non-Deferred will
        raise a L{ValueError}.
        �	somethingN)r�rr�r)r�s r9�'test_willNotAllowNonDeferredOrCoroutinez;EnsureDeferredTests.test_willNotAllowNonDeferredOrCoroutine�
s5��
�
�
�u�7�7�
8�	(��;�'�	(�	(�	(�s�5�>c��dd�}|�}|j|tj�t|�}t	|t
t�|j|t
�|j|�}|j|d�y)zM
        L{ensureDeferred} will turn a coroutine into a L{Deferred}.
        c��PK�tjd�}|�d{���}|S7��w�Nr=r��r#r�s  r9r�z=EnsureDeferredTests.test_ensureDeferredCoroutine.<locals>.run�
s%�����
�
�e�$�A��'�C��J��s�&�$�&r=N)r=r�)	r4�types�
CoroutineTyper)rr r�rr[�r`r�r<r#r�s     r9�test_ensureDeferredCoroutinez0EnsureDeferredTests.test_ensureDeferredCoroutine�
�r��
	�
�E�����a��!4�!4�5�
�1����A�x��}�%����a��*��"�"�1�%������e�$r;c��dd�}|�}|j|tj�t|�}t	|t
t�|j|t
�|j|�}|j|d�y)zX
        L{ensureDeferred} will turn a yield-from coroutine into a L{Deferred}.
        c3�lK�tjd�}tt|Ed{����}|S7�
�wr�)rr rr�r�s  r9r�z=EnsureDeferredTests.test_ensureDeferredGenerator.<locals>.run
s,�����
�
�e�$�A��s��\�+�C��J�*�s�%4�2
�4r=N)r=z#Generator[Deferred[str], None, str])	r4r��
GeneratorTyper)rr r�rr[r�s     r9�test_ensureDeferredGeneratorz0EnsureDeferredTests.test_ensureDeferredGeneratorr�r;Nr�)rDrErFrfr�r�r�r�rGr;r9r�r��
s����(�%�.%r;r�c��eZdZdZdd�Zy)�TimeoutErrorTestsz1
    L{twisted.internet.defer} timeout code.
    c�X�t�}tj|�|j|tj�|j|jg�}|jt|�d�|j|ddt�|j|ddd�y)zB
        L{twisted.internet.defer.timeout} is deprecated.
        rIrr>r?z~twisted.internet.defer.timeout was deprecated in Twisted 17.1.0; please use twisted.internet.defer.Deferred.addTimeout insteadN)r rrrrr@�test_deprecatedTimeoutr[r\rmrA)r`rQ�
warningsShowns   r9r�z(TimeoutErrorTests.test_deprecatedTimeout"s���&.�Z��
�
�
�h�����8�U�%7�%7�8��*�*�D�,G�,G�+H�I�
�����]�+�Q�/��
�
�m�A�&�z�2�4F�G�����!��Y�'�
M�	
r;Nr�)rDrErFrfr�rGr;r9r�r�s���
r;r�c�Z�|j|j�|j�y)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��loops r9�callAllSoonCallsr�3s ��	�N�N�4�9�9�����r;c�L�eZdZd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Z	dd�Z
y	)�DeferredFutureAdapterTestsc�P�t�}|j|j�|S)zU
        Create a new event loop that will be closed at the end of the test.
        )�_new_event_loopr��closer�s  r9�newLoopz"DeferredFutureAdapterTests.newLoop@s!��!�"��������%��
r;c�T�t�}|j�}|j|�}|j|j	�d�|jd�t
|�|j|j|�d�|j|j�d�y)zw
        L{Deferred.asFuture} returns a L{asyncio.Future} which fires when
        the given L{Deferred} does.
        Fr�N)	r r��asFuturer[rrOr�rro)r`r#r��aFutures    r9�
test_asFuturez(DeferredFutureAdapterTests.test_asFutureHs��
$�:���|�|�~���*�*�T�"����������/�	�
�
�2���������-�-�a�0�$�7�������)�2�.r;c�H��d�d�fd�}t|�}|j�}|j|�}|j�t	|�|j��|j
|j|�d�|jt|j�y)z�
        L{Deferred.asFuture} returns a L{asyncio.Future} which, when
        cancelled, will cancel the original L{Deferred}.
        Fc�
��d�yr�rG)�dprimer�s �r9�cancelerzFDeferredFutureAdapterTests.test_asFutureCancelFuture.<locals>.canceler^s����Fr;N�r�rr=r|)r r�r�r�r�r]r[rr�rro)r`r�r#r�r�r�s     @r9�test_asFutureCancelFuturez4DeferredFutureAdapterTests.test_asFutureCancelFutureVs������	�%�X�.���|�|�~���*�*�T�"������������������-�-�a�0�$�7����.�'�.�.�9r;c��dd�}t|�}|j�}|j|�}|j�t	|�|j|j
|�d�|jt|j�y)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.
        c�&�|jd�y)Nr�rN)r�s r9r�zGDeferredFutureAdapterTests.test_asFutureSuccessCancel.<locals>.cancelerrs���O�O�A�r;Nr�)
r r�r�r�r�r[rr�rro)r`r�r#r�r�s     r9�test_asFutureSuccessCancelz5DeferredFutureAdapterTests.test_asFutureSuccessCancelksm��	�%�X�.���|�|�~���*�*�T�"�������������-�-�a�0�$�7����.�'�.�.�9r;c��t�}tt��}|j�}|j	|�}t|�|j
|�t|�|jt|j�y)z�
        L{Deferred.asFuture} makes a L{asyncio.Future} fire with an
        exception when the given L{Deferred} does.
        N)	r r/r3r�r�r�r�r�ro)r`r#�
theFailurer��futures     r9�test_asFutureFailurez/DeferredFutureAdapterTests.test_asFutureFailure}sc��
%�J���.�0�1�
��|�|�~�����D�!�����	�	�	�*��������+�V�]�]�;r;c��|j�}t|��}tj|�}|j	|�|jd�t
|�|j|j|�d�y)zx
        L{Deferred.fromFuture} returns a L{Deferred} that fires
        when the given L{asyncio.Future} does.
        r�rIN)	r�rr �
fromFuturer�
set_resultr�r[r)r`r�r�r#s    r9�test_fromFuturez*DeferredFutureAdapterTests.test_fromFuture�sh��
�|�|�~��%�4�0������(�����A�����1���������-�-�a�0�!�4r;c�$�|j�}t|��}tj|�}|j	�t|�|j
t|j�|j|�jt�y)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�rr r�r�r�r�rror�rN�r`r��	cancelledr#s    r9�test_fromFutureFutureCancelledz9DeferredFutureAdapterTests.test_fromFutureFutureCancelled�sl���|�|�~��"(�d�"3�	����	�*������������.�)�*:�*:�;����Q��$�$�^�4r;c�d�|j�}t|��}tj|�}|j	�t|�|j
|j�d�|jt|j�|j|�jt�y)z�
        L{Deferred.fromFuture} makes a L{Deferred} which, when
        cancelled, cancels the L{asyncio.Future} it was created from.
        r�TN)
r�rr r�r�r�r[r�r�rror�rNr�s    r9� test_fromFutureDeferredCancelledz;DeferredFutureAdapterTests.test_fromFutureDeferredCancelled�s���
�|�|�~��"(�d�"3�	����	�*��	���
��������,�,�.��5����.�)�*:�*:�;����Q��$�$�^�4r;N)r=rr�)rDrErFr�r�r�r�r�r�r�r�rGr;r9r�r�?s*���/�:�*:�$<�5�5�5r;r�c�Z�eZdZdd�Zdd�Zedd��Zedd��Zedd��Zdd�Z	y)	�CoroutineContextVarsTestsc���������t��tjd���jd�t	���j�fd��t	���j�fd���jd��tjd��fd���t�tgtdf�tjd������fd��}t|tgtdf��jd	�|�}�jd��jd��jd��j�j|�d
�y)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.
        �testvarrIc�&���jd�Sr���set�r1�vars �r9r�zDCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.<lambda>�����s�w�w�q�z�r;c�&���jd�S�N�r�r�s �r9r�zDCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.<lambda>���������
�r;c��ddzSr/rGr0s r9r�zDCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.<lambda>��
���A��r;c3��K�t�}�jd|jd�|���jd�y�w�NrITr��r rrOr��r#r
r�s ��r9�yieldingDeferredzLCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.yieldingDeferred�s2�����'�z�A��O�O�A�q�z�z�4�0��G��G�G�A�J�s�>ANc3�r�K��j�j�d�tjd����j�j�d��j	d�j
d�����j�j�d��j	d�j
d�	���t
d��#t$r#�j�j�d�YnwxYw�����j�j�d�tjd�y�w)Nr�rIT�???? should have failed)r[r�rr rrOr�r�)r
�mutatingDeferred�mutatingDeferredThatFailsr`r�r�s������r9�testFunctionzHCoroutineContextVarsTests.test_withInlineCallbacks.<locals>.testFunction�s�����
���S�W�W�Y��*��-�-��"�"�
���S�W�W�Y��*�
�O�O�A�/�8�8�$�?�"�"�
���S�W�W�Y��*�
�O�O�A�8�A�A�4�H�
;�/�/� � 9�:�:���
/�� � �����A�.�
/��#�$�$����S�W�W�Y��*����d�#�s+�B6D7�:C	�>D7�	)C5�2D7�4C5�5AD7r�T�r=z#Generator[Deferred[Any], Any, None])
r+�contextvars�
ContextVarr�r r�rr�rr	r%r[r)r`r�r#r
r�r�r�r�s`  @@@@@r9�test_withInlineCallbacksz2CoroutineContextVarsTests.test_withInlineCallbacks�s)���
���+6�+A�+A�)�+L������
�,4�:���$�$�%9�:�3;�:�!�!�-�-�.B�C�!�-�-�o�>�	�	�	�	�
�	�	�$�h�r�8�D�>�/A�&B�C�
�	�	�"	$�"	$�
�"	$�H	�L�(�2�x��~�+=�">�?�����
��N��	�
�
�a��	�
�
�a��	�
�
�a������-�-�a�0�$�7r;c������t��tjd��tjd���fd��}�jd�|�}�j
d��j|�y)z�
        When an inlineCallbacks function resumes, we should be able to reset() a
        contextvar that was set when it was first called.
        r�c3�(�K��jd�}t�}�jd|jd�|���j	�j�d��j
|��j	�j�d�y�w)Nr�rITr�)r�r rrOr[r��reset)�tokenr#r
r`r�s  ���r9r�zQCoroutineContextVarsTests.test_resetWithInlineCallbacks.<locals>.yieldingDeferredsp������G�G�A�J�E� (�z�A��O�O�A�q�z�z�4�0��G����S�W�W�Y��*��I�I�e�����S�W�W�Y��*�s�BBr�rINr�)r+r�r�rr�r�r%r)r`r�r#r
r�s`  @@r9�test_resetWithInlineCallbacksz7CoroutineContextVarsTests.test_resetWithInlineCallbacksse���
���+6�+A�+A�)�+L��	�	�	�	+�
�	+�	����
����	�
�
�a�����Q�r;c���K�t��d}�4�d{���|j�j��j�}|j	�fd��|j�j�|j|j�ddd��d{���|j|j�|�d{���|j�j�y7��7�G#1�d{���7swY�WxYw7�9�w)zQ
        L{DeferredLock} can be used as an asynchronous context manager.
        Nc�$���j�Sr3�r�)r1r�s �r9r�z>CoroutineContextVarsTests.test_asyncWithLock.<locals>.<lambda>1s
���D�L�L�N�r;)r#r]r�r�r�rr�)r`r#r�s  @r9�test_asyncWithLockz,CoroutineContextVarsTests.test_asyncWithLock's������
�~�����	'�	'��O�O�D�K�K�(�����A�
�M�M�2�3��O�O�D�K�K�(����Q�X�X�&�	'�	'�	
������!����������%�	'��	'��	'�	'�	'��	�sQ�C<�C!�C<�A6C%�C<�C#�$C<�C:�!C<�#C<�%C7�+C.�,C7�3C<c���K�td���4�d{���|j�jd��4�d{���|j�jd��j�}�j�}|j�jd�|j	|j
�|j
|j
�ddd��d{���|j�jd�|j	j
�j�fd��|j�fd��|�d{���|�d{���|j�jd�ddd��d{���|j�jd�y7���7��b7��#1�d{���7swY��xYw7�t7�l7�B#1�d{���7swY�RxYw�w)z^
        L{DeferredSemaphore} can be used as an asynchronous context
        manager.
        r�Nr�rIrc�$���j�Sr3r��r1r�s �r9r�zCCoroutineContextVarsTests.test_asyncWithSemaphore.<locals>.<lambda>K�
���S�[�[�]�r;c�$���j�Sr3r�r�s �r9r�zCCoroutineContextVarsTests.test_asyncWithSemaphore.<locals>.<lambda>Lrr;)r%r[�tokensr�r]r�rr�)r`r�r�r�s   @r9�test_asyncWithSemaphorez1CoroutineContextVarsTests.test_asyncWithSemaphore8sn����� ��"���	,�	,����S�Z�Z��+��
,�
,�� � ����Q�/��[�[�]���[�[�]��� � ����Q�/�����	�	�*�� � ����+�

,�
,�
���S�Z�Z��+��O�O�B�I�I�&��N�N�2�3��N�N�2�3��H�H��H�H����S�Z�Z��+�	,�	,� 	
������Q�'�!	,��
,��
,��
,�
,�
,��
����	,��	,�	,�	,�s��G�F�G�#F>�F�F>�BF#�F>�F!�A(F>�F8�	F>�
F:� F>�.G�9F<�:"G�F>�!F>�#F5	�)F,�*F5	�1F>�:F>�<G�>G�G�G�Gc��@K�t�}|jtd�5|4�d{���|j|j�td��7�*#1�d{���7swYnxYw	ddd�n#1swYnxYw|j|j�y�w)zw
        C{DeferredLock} correctly propagates exceptions when
        used as an asynchronous context manager.
        zsome specific exceptionN)r#�assertRaisesRegexr�r]r�rr!s  r9�test_asyncWithLockExceptionz5CoroutineContextVarsTests.test_asyncWithLockExceptionRs������~��
�
#�
#�I�/H�
I�	;��
;�
;�������,�� 9�:�:�
;��
;�
;�
;��
;�	;�	;�	;��	
������%�sI�!B�A6�A�A6�'A�A6�A)	�A �A)	�%A6�-	B�6A?�;#Bc��������t��tjd���jd�t	���j�fd��t	���j�fd���jd��d��fd��d������fd�}�jd�t
|��}�jd��jd��jd��j�j|�d	�y
)
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�rIc�&���jd�Sr�r�r�s �r9r�zJCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.<lambda>kr�r;c�&���jd�Sr�r�r�s �r9r�zJCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.<lambda>nr�r;c��ddzSr/rGr0s r9r�zJCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.<lambda>or�r;c���K�t�}�jd|jd�|�d{����jd�y7��wr�r�r�s ��r9�asyncFuncAwaitingDeferredz[CoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.asyncFuncAwaitingDeferredqs9����� (�
�A��O�O�A�q�z�z�4�0��G�G��G�G�A�J�
�s�-A	�A�A	c���K��j�j�d�tjd��d{����j�j�d��j	d�j
d���d{����j�j�d��j	d�j
d�	��d{���t
d��7��7�Y7�#t$r#�j�j�d�YnwxYw���d{���7�j�j�d�y�w)Nr�rIrTr�r�)r[r�rr rrOr�)rr
r�r�r`r�s������r9r�zNCoroutineContextVarsTests.test_contextvarsWithAsyncAwait.<locals>.testFunctionxs��������S�W�W�Y��*��-�-��"�"�"�
���S�W�W�Y��*�
�O�O�A�/�8�8�$�?�"�"�"�
���S�W�W�Y��*�
�O�O�A�8�A�A�4�H�
;�/�/�/� � 9�:�:�)
#��
#��0���
/�� � �����A�.�
/��,�-�-�-����S�W�W�Y��*��se�9E�C�AE�C�AE�C!�C�C!�E�E�C!�!)D
�
E�D
�

E�D�&Er�TNr�)r=r�)
r+r�r�r�r r�r)r%r[r)r`r�r#rr
r�r�r�s`  @@@@@r9�test_contextvarsWithAsyncAwaitz8CoroutineContextVarsTests.test_contextvarsWithAsyncAwait_s����
���+6�+A�+A�)�+L������
�,4�:���$�$�%9�:�4<�J�!�!�-�-�.B�C�!�-�-�o�>�	�!	�!	�H	����
��<�>�*��	�
�
�a��	�
�
�a��	�
�
�a������-�-�a�0�$�7r;Nr�)
rDrErFr�r�r@r�rrrrGr;r9r�r��sQ��O8�b �@�&��&� �(��(�2�
&��
&�I8r;r�c��eZdZdd�Zdd�Zdd�Zejed�dd��Z	ejed�dd��Z
y)	�InlineCallbackTestsc�~�t�}t�}	|j�dd�}tj|�|j|�}tj|j��}|jt|�d�|jd|dd�|jd|dd�|jd|dd�|jd|dd�|jd|dd�|jd	|dd�y
#t$r|j	�Y��wxYw)r�c3�K�|��y�wr3rGr�s r9r�z>InlineCallbackTests.test_inlineCallbacksTracebacks.<locals>.ic�s�����G�r�r�r�r�rrKr�r�r�Nr�r�r�s      r9r�z2InlineCallbackTests.test_inlineCallbacksTracebacks�s��

� ��$�J��	�
����	�	���b�!��)�)�!�,�
�
�
!�
!�*�"?�"?�"A�
B������R��!�$��
�
�l�B�q�E�!�H�-����-�r�!�u�Q�x�8�����"�Q�%��(�+��
�
�l�B�q�E�!�H�-����9�2�a�5��8�D����-�r�!�u�Q�x�8��#�	�
�I�I�K�	�r�c��d�dd|jdtg}|D]1}|jtjtj
|��3y)r�c��|Sr3rGr}s r9r�zIInlineCallbackTests.test_fromCoroutineRequiresCoroutine.<locals>.<lambda>�r�r;rITNr�r�s   r9r�z7InlineCallbackTests.test_fromCoroutineRequiresCoroutine�r�r;c����g}g}t|j��t|j��tjd��fd��}|�}|j	��|j	��|j	|�|j|g�|j|g�|j
�|j|�g�|j|g�|j	|�|j
�|j|�}|j|jtj�|j|�g�y)a�
        Cancelling an L{defer.inlineCallbacks} correctly handles the function
        catching the L{defer.CancelledError}.

        The desired behavior is:
            1. If the function is waiting on an inner deferred, that inner
               deferred is cancelled, and a L{defer.CancelledError} is raised
               within the function.
            2. If the function catches that exception, execution continues, and
               the deferred returned by the function is not resolved.
            3. Cancelling the deferred again cancels any deferred the function
               is waiting on, and the exception is raised.
        c3�>�K�	������y#t$rY�wxYw�wr3r)r�r�s��r9�testFunczHInlineCallbackTests.test_inlineCallbacksCancelCaptured.<locals>.testFunc�s+�����
����H���
��
�s����	���N)r=z)Generator[Deferred[object], object, None])
r rZrr�rr[r�r�r�r)r`�canceller1Calls�canceller2Callsr�funcDrOr�r�s      @@r9�"test_inlineCallbacksCancelCapturedz6InlineCallbackTests.test_inlineCallbacksCancelCaptured�s���35��24��'��(>�(>�?��'��(>�(>�?��	�	�	�	�
�	��
�����B�����B�����E�"�����"�-�����"�-�
	��������2�$�/�����"�-����E�"�
	�����&�&�u�-��������u�';�';�<�����2�$�/r;r�c�H�t�}tj|�}tjdd��}||�}|j||j
|��tj|�}~~|j|��|j|��y)a�
        When using L{defer.inlineCallbacks}, after the function exits, it will
        not keep references to the function itself or the arguments.

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

        The GC on PyPy works differently (del doesn't immediately deallocate the
        object), so we skip the test.
        c3�K�|��|S�wr3rG�r�s r9rzIInlineCallbackTests.test_inlineCallbacksNoCircularReference.<locals>.func's�����G��H�s�	N�r�rr=r)r�r�r�rr�r[rr��r`�obj�
objWeakRefrr�funcDWeakRefs      r9�'test_inlineCallbacksNoCircularReferencez;InlineCallbackTests.test_inlineCallbacksNoCircularReferences�������[�[��%�
�	�	�	�	�
�	�
�S�	������d�2�2�5�9�:��{�{�5�)��
��	
���*�,�'����,�.�)r;c�H�t�}tj|�}dd�}tj||��}|j||j
|��tj|�}~~|j|��|j|��y)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.
        c��K�|S�wr3rGrs r9rzCInlineCallbackTests.test_coroutineNoCircularReference.<locals>.funcFs�����H�s�Nr)r�r�r�r r4r[rr�r s      r9�!test_coroutineNoCircularReferencez5InlineCallbackTests.test_coroutineNoCircularReference:s�������[�[��%�
�	��&�&�t�C�y�1������d�2�2�5�9�:��{�{�5�)��
��	
���*�,�'����,�.�)r;Nr�)rDrErFr�r�rr�r�r-r$r'rGr;r9rr�sX��9�8W�.50�n�V�]�]�5�9�:�!*�;�!*�F�V�]�]�5�9�:�*�;�*r;r)r8z0Callable[..., Coroutine[Deferred[Any], Any, _T]]r=zCallable[..., Deferred[_T]])r5r<r6r<r=r/)rQrQr=r|)r�r5r=z(tuple[list[int], list[Deferred[object]]])r6r<r7�floatr=r�)r�rr=r|)brf�
__future__rr�r>r�rBr_r�r0r�rLr��asynciorrrrr��typingrr	r
rrr
rrrrrrrrr�hamcrestrrr�
hypothesisr�hypothesis.strategiesrr�twisted.internetrr�twisted.internet.deferrr r!r"r#r$r%r&r'r(r)r*�twisted.internet.taskr+�twisted.pythonr,�twisted.python.compatr-�twisted.python.failurer/�
twisted.trialr@r�rBrKrRrSrVrXrh�SynchronousTestCaser�r�r�r�r�r�rAr}r�r�r
r8r:r�r�r�r�r�rrGr;r9�<module>r7s8���#���	�	�������������$2�1��*��+�
�
�
�
�(��'�*�"��7�� ��	�9�	�
�)��m�9�5��4�4�,*M��!�!�*M�ZS+�H�0�0�2G�S+�l4���&~7��,�,�~7�B;$�h�2�2�;$�|L$��5�5�L$�^GI�X�9�9�GI�Tc3�x�+�+�c3�LE�X�9�9�E�"A'�8�7�7�9N�A'�Hz=�(�"3�"3�z=�z�S@�h�:�:�S@�lA%�(�+�+�A%�H
��)�)�+@�
�,	�s5��!2�!2�s5�ls8�� 1� 1�s8�lj*�(�6�6�j*r;

Zerion Mini Shell 1.0