o
    >h                     @   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   /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/internet/test/test_defer_await.pyr      s    r   c                   @   st   e Zd ZdZdddZdddZddd	Zdd
dZdddZdddZ	dddZ
dddZdddZdddZdS )
AwaitTestsz@
    Tests for using Deferreds in conjunction with PEP-492.
    returnNc                 C   s"   t  }| }| |t| dS )z<
        C{Deferred.__await__} returns an iterable.
        N)r   	__await__assertEqualiter)selfdawaitedDeferredr   r   r   test_awaitReturnsIterable$   s   z$AwaitTests.test_awaitReturnsIterablec                    sd   dt f fdd}dt f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                        t d} | I d H }|S Nfoor   r   r   r   r   r    7      
z3AwaitTests.test_deferredFromCoroutine.<locals>.run2r$   N)strassertIsInstancetypesCoroutineTyper   fromCoroutinesuccessResultOfr   )r   r!   rr   r   r   r   r   test_deferredFromCoroutine,   s   

z%AwaitTests.test_deferredFromCoroutinec                 C   s4   dt fdd}t| }| |}| |d dS )zc
        L{Deferred.fromCoroutine} allows a function to C{await} on a
        L{Deferred}.
        r   c                     r"   r#   r   r   r   r   r   r!   N   r%   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   dt fdd}t| }| |}| |d dS )zS
        L{ensureDeferred} allows a function to C{await} on a L{Deferred}.
        r   c                     r"   r#   r   r   r   r   r   r!   \   r%   z0AwaitTests.test_basicEnsureDeferred.<locals>.runr$   N)r&   r   r+   r   r.   r   r   r   test_basicEnsureDeferredW   s   

z#AwaitTests.test_basicEnsureDeferredc                 C   sJ   dt f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)Nr$   Oh no!)r   
ValueErrorr   r   r   r   r!   k   s   
z&AwaitTests.test_exception.<locals>.run)r1   N)	r   r   r*   failureResultOfr   typevaluer2   argsr.   r   r   r   test_exceptione   s
   
zAwaitTests.test_exceptionc                    sV   d	dd}t | 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.
        r   Nc                   S   s   t  N)r   r   r   r   r   raises|   s   zCAwaitTests.test_synchronousDeferredFailureTraceback.<locals>.raisesc                          I d H S r9   r   r   itr   r   doomed      
zCAwaitTests.test_synchronousDeferredFailureTraceback.<locals>.doomed, in doomed
z, in raises
r   N)r   r4   r   r*   assertIngetTraceback)r   r:   r>   failurer   r<   r   (test_synchronousDeferredFailureTracebacku   s   
z3AwaitTests.test_synchronousDeferredFailureTracebackc                    sr   dt fdd}t  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   t y   t  Y S w r9   )r   r	   r   r   r   r   returnsFailure   s
   
zEAwaitTests.test_asyncDeferredFailureTraceback.<locals>.returnsFailureNc                      r;   r9   r   r   r<   r   r   r>      r?   z=AwaitTests.test_asyncDeferredFailureTraceback.<locals>.doomedr@   z, in returnsFailure
rA   )r	   r   r*   assertNoResulterrbackr4   rB   rC   )r   rF   r>   startedrD   r   r<   r   "test_asyncDeferredFailureTraceback   s   

z-AwaitTests.test_asyncDeferredFailureTracebackc                    s   t   g dtf fdddtf fdd}t| } d | ddg  d	 | g d
  d | g d
  d	 | g d | |}| |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                     s:    d t }  d| jd | I d H   d dS )N         Yay!appendr   	callLatercallbackr3   )reactorsectionsr   r   runone   s   


z'AwaitTests.test_twoDeep.<locals>.runonec                     sP    d  I d H }  d t } d|jd |I d H   d | S )NrL         rO   )resultr   rS   rU   rT   r   r   r!      s   



z$AwaitTests.test_twoDeep.<locals>.rung?rL   rK   皙?)rL   rK   rM   rV   )rL   rK   rM   rV   rW   rN   N)r   r&   r   r*   advancer   r+   r.   r   rY   r   test_twoDeep   s   





zAwaitTests.test_twoDeepc                    s4   dt f fdd} t| } |d dS )zO
        Awaiting an already failed Deferred will raise the exception.
        r   c               
      sP   zt tdI d H  W dS  ty' }  z | jd W Y d } ~ dS d } ~ ww )NBoom)r]   rL   r   )r   r2   r   r7   )er   r   r   test   s   z%AwaitTests.test_reraise.<locals>.testrL   N)intr+   r   r*   r   )r   r`   r   r   r_   r   test_reraise   s   zAwaitTests.test_reraisec                    sD   t   d fdd}t| } d | |}| |d dS )zd
        Awaiting a paused & chained Deferred will give the result when it has
        one.
        r   Nc                     sD   t  } t   |  fdd | d  d jd | I d H S )Nc                    s    S r9   r   )ignoredd2r   r   <lambda>   s    z7AwaitTests.test_chained.<locals>.test.<locals>.<lambda>r   bye)r   addCallbackrR   rQ   r3   rS   rd   r   r`      s   

z%AwaitTests.test_chained.<locals>.testrZ   rg   rA   )r   r   r*   r[   r+   r   )r   r`   r   r   r   ri   r   test_chained   s   	

zAwaitTests.test_chainedrA   )r   r   r   r   r   r-   r/   r0   r8   rE   rJ   r\   rb   rj   r   r   r   r   r      s    








+r   )r   r(   typing_extensionsr   twisted.internet.deferr   r   r   r   r   twisted.internet.taskr   twisted.python.failurer	   twisted.trial.unittestr
   	Exceptionr   r   r   r   r   r   <module>   s   