U
    n5{i°  ã                   @   s|   d Z ddlZddlmZ ddlmZmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZ G dd	„ d	eƒZG d
d„ deƒZdS )z*
Tests for C{await} support in Deferreds.
é    N)ÚNoReturn)ÚDeferredÚensureDeferredÚfailÚmaybeDeferredÚsucceed)ÚClock)ÚFailure)ÚTestCasec                   @   s   e Zd ZdZdS )ÚSampleExceptionz2
    A specific sample exception for testing.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úJ/tmp/pip-unpacked-wheel-mnhg7r0q/twisted/internet/test/test_defer_await.pyr      s   r   c                   @   sœ   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„Zddœdd„Z	ddœdd„Z
ddœdd„Zddœdd„Zddœdd„ZdS )Ú
AwaitTestsz@
    Tests for using Deferreds in conjunction with PEP-492.
    N©Úreturnc                 C   s"   t ƒ }| ¡ }|  |t|ƒ¡ dS )z<
        C{Deferred.__await__} returns an iterable.
        N)r   Ú	__await__ÚassertEqualÚiter)ÚselfÚdZawaitedDeferredr   r   r   Útest_awaitReturnsIterable$   s    z$AwaitTests.test_awaitReturnsIterablec                    sd   t dœ‡ fdd„}t dœdd„‰ |ƒ }|  |tj¡ t |¡}|  |t¡ |  |¡}|  |d¡ dS )zU
        L{Deferred.fromCoroutine} will turn a coroutine into a L{Deferred}.
        r   c                  “   s"   t dƒ} | I d H  ˆ ƒ I d H }|S )NÚbar©r   ©r   Úres©Úrun2r   r   Úrun1   s    
z2AwaitTests.test_deferredFromCoroutine.<locals>.runc                  Ó   s   t dƒ} | I d H }|S ©NÚfoor   r   r   r   r   r    7   s    
z3AwaitTests.test_deferredFromCoroutine.<locals>.run2r#   N)ÚstrZassertIsInstanceÚtypesÚCoroutineTyper   ÚfromCoroutineÚsuccessResultOfr   )r   r!   Úrr   r   r   r   r   Útest_deferredFromCoroutine,   s    

z%AwaitTests.test_deferredFromCoroutinec                 C   s4   t dœdd„}t |ƒ ¡}|  |¡}|  |d¡ dS )zc
        L{Deferred.fromCoroutine} allows a function to C{await} on a
        L{Deferred}.
        r   c                  Ó   s   t dƒ} | I d H }|S r"   r   r   r   r   r   r!   N   s    
z"AwaitTests.test_basic.<locals>.runr#   N)r$   r   r'   r(   r   ©r   r!   r   r   r   r   r   Ú
test_basicH   s    
zAwaitTests.test_basicc                 C   s2   t dœdd„}t|ƒ ƒ}|  |¡}|  |d¡ dS )zS
        L{ensureDeferred} allows a function to C{await} on a L{Deferred}.
        r   c                  Ó   s   t dƒ} | I d H }|S r"   r   r   r   r   r   r!   \   s    
z0AwaitTests.test_basicEnsureDeferred.<locals>.runr#   N)r$   r   r(   r   r+   r   r   r   Útest_basicEnsureDeferredW   s    

z#AwaitTests.test_basicEnsureDeferredc                 C   sJ   t dœdd„}t |ƒ ¡}|  |¡}|  t|jƒt¡ |  |jjd¡ dS )zš
        An exception in a coroutine scheduled with L{Deferred.fromCoroutine}
        will cause the returned L{Deferred} to fire with a failure.
        r   c                  Ó   s   t dƒ} | I d H  tdƒ‚d S )Nr#   úOh no!)r   Ú
ValueError©r   r   r   r   r!   k   s    
z&AwaitTests.test_exception.<locals>.run)r.   N)	r   r   r'   ÚfailureResultOfr   ÚtypeÚvaluer/   Úargsr+   r   r   r   Útest_exceptione   s
    
zAwaitTests.test_exceptionc                    s^   ddœdd„}t |ƒ‰ ddœ‡ fdd„}|  t |ƒ ¡¡}|  d| ¡ ¡ |  d| ¡ ¡ dS )	zé
        When a Deferred is awaited upon that has already failed with a Failure
        that has a traceback, both the place that the synchronous traceback
        comes from and the awaiting line are shown in the traceback.
        Nr   c                   S   s
   t ƒ ‚d S ©N)r   r   r   r   r   Úraises|   s    zCAwaitTests.test_synchronousDeferredFailureTraceback.<locals>.raisesc                   “   s
   ˆ I d H S r6   r   r   ©Úitr   r   Údoomed�   s    zCAwaitTests.test_synchronousDeferredFailureTraceback.<locals>.doomedú, in doomed
z, in raises
)r   r1   r   r'   ÚassertInÚgetTraceback)r   r7   r:   Úfailurer   r8   r   Ú(test_synchronousDeferredFailureTracebacku   s    z3AwaitTests.test_synchronousDeferredFailureTracebackc                    sv   t dœdd„}tƒ ‰ ddœ‡ fdd„}t |ƒ ¡}|  |¡ ˆ  |ƒ ¡ |  |¡}|  d| ¡ ¡ |  d| ¡ ¡ dS )	zâ
        When a Deferred is awaited upon that later fails with a Failure that
        has a traceback, both the place that the synchronous traceback comes
        from and the awaiting line are shown in the traceback.
        r   c                   S   s*   z
t ƒ ‚W n t k
r$   tƒ  Y S X d S r6   )r   r	   r   r   r   r   ÚreturnsFailure�   s    
zEAwaitTests.test_asyncDeferredFailureTraceback.<locals>.returnsFailureNc                   “   s
   ˆ I d H S r6   r   r   r8   r   r   r:   ˜   s    z=AwaitTests.test_asyncDeferredFailureTraceback.<locals>.doomedr;   z, in returnsFailure
)r	   r   r'   ZassertNoResultZerrbackr1   r<   r=   )r   r@   r:   Ústartedr>   r   r8   r   Ú"test_asyncDeferredFailureTraceback‰   s    

z-AwaitTests.test_asyncDeferredFailureTracebackc                    sÐ   t ƒ ‰ g ‰tdœ‡ ‡fdd„‰tdœ‡ ‡‡fdd„}t |ƒ ¡}ˆ  d¡ |  ˆddg¡ ˆ  d	¡ |  ˆddd
dg¡ ˆ  d¡ |  ˆddd
dg¡ ˆ  d	¡ |  ˆddd
ddg¡ |  |¡}|  |d¡ dS )z¢
        A coroutine scheduled with L{Deferred.fromCoroutine} that awaits a
        L{Deferred} suspends its execution until the inner L{Deferred} fires.
        r   c                  “   s8   ˆ  d¡ tƒ } ˆ  d| jd¡ | I d H  ˆ  d¡ dS )Né   é   é   úYay!©Úappendr   Ú	callLaterÚcallbackr0   )ÚreactorÚsectionsr   r   Úrunoneª   s    


z'AwaitTests.test_twoDeep.<locals>.runonec                  “   sN   ˆ  d¡ ˆƒ I d H } ˆ  d¡ tƒ }ˆ  d|jd¡ |I d H  ˆ  d¡ | S )NrD   é   é   rG   )Úresultr   ©rK   rM   rL   r   r   r!   ²   s    



z$AwaitTests.test_twoDeep.<locals>.rungÍÌÌÌÌÌì?rD   rC   çš™™™™™¹?rE   rN   rO   rF   N)r   r$   r   r'   Úadvancer   r(   r+   r   rQ   r   Útest_twoDeep¢   s    





zAwaitTests.test_twoDeepc                    s4   t dœ‡ fdd„}ˆ  t |ƒ ¡¡}ˆ  |d¡ dS )zO
        Awaiting an already failed Deferred will raise the exception.
        r   c               
   “   sR   zt tdƒƒI d H  W n6 tk
rL }  zˆ  | jd¡ W Y ¢dS d } ~ X Y nX dS )NÚBoom)rU   rD   r   )r   r/   r   r4   )Úe©r   r   r   ÚtestÒ   s    z%AwaitTests.test_reraise.<locals>.testrD   N)Úintr(   r   r'   r   )r   rX   r   r   rW   r   Útest_reraiseÍ   s    zAwaitTests.test_reraisec                    sH   t ƒ ‰ ddœ‡ fdd„}t |ƒ ¡}ˆ  d¡ |  |¡}|  |d¡ dS )zd
        Awaiting a paused & chained Deferred will give the result when it has
        one.
        Nr   c                  “   sB   t ƒ } t ƒ ‰ |  ‡ fdd„¡ |  d ¡ ˆ dˆ jd¡ | I d H S )Nc                    s   ˆ S r6   r   )Zignored©Zd2r   r   Ú<lambda>ç   ó    z7AwaitTests.test_chained.<locals>.test.<locals>.<lambda>r   Úbye)r   ZaddCallbackrJ   rI   r0   ©rK   r[   r   rX   ä   s    
z%AwaitTests.test_chained.<locals>.testrR   r^   )r   r   r'   rS   r(   r   )r   rX   r   r   r   r_   r   Útest_chainedÝ   s    	

zAwaitTests.test_chained)r   r   r   r   r   r*   r,   r-   r5   r?   rB   rT   rZ   r`   r   r   r   r   r      s   +r   )r   r%   Ztyping_extensionsr   Ztwisted.internet.deferr   r   r   r   r   Ztwisted.internet.taskr   Ztwisted.python.failurer	   Ztwisted.trial.unittestr
   Ú	Exceptionr   r   r   r   r   r   Ú<module>   s   