
    Vh                         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	e      Z G d
 de      Zy)z*
Tests for C{await} support in Deferreds.
    N)NoReturn)DeferredensureDeferredfailmaybeDeferredsucceed)Clock)Failure)TestCasec                       e Zd ZdZy)SampleExceptionz2
    A specific sample exception for testing.
    N)__name__
__module____qualname____doc__     V/home/dcms/DCMS/lib/python3.12/site-packages/twisted/internet/test/test_defer_await.pyr   r      s    r   r   c                   `    e Zd ZdZddZddZddZddZddZddZ	dd	Z
dd
ZddZddZy)
AwaitTestsz@
    Tests for using Deferreds in conjunction with PEP-492.
    Nc                 n    t               }|j                         }| j                  |t        |             y)z<
        C{Deferred.__await__} returns an iterable.
        N)r   	__await__assertEqualiter)selfdawaitedDeferreds      r   test_awaitReturnsIterablez$AwaitTests.test_awaitReturnsIterable$   s+     %J++-$*?@r   c                    dt         ffd}dt         fd |       }| j                  |t        j                         t	        j
                  |      }| j                  |t               | j                  |      }| j                  |d       y)zU
        L{Deferred.fromCoroutine} will turn a coroutine into a L{Deferred}.
        returnc                  `   K   t        d      } |  d {             d {   }|S 7 7 w)Nbarr   )r   resrun2s     r   runz2AwaitTests.test_deferredFromCoroutine.<locals>.run1   s.     AGG,CJ s   .*.,..c                  <   K   t        d      } |  d {   }|S 7 wNfoor#   r   r$   s     r   r%   z3AwaitTests.test_deferredFromCoroutine.<locals>.run27         A'CJ    r)   N)strassertIsInstancetypesCoroutineTyper   fromCoroutinesuccessResultOfr   )r   r&   rr   r$   r%   s        @r   test_deferredFromCoroutinez%AwaitTests.test_deferredFromCoroutine,   s~    
	3 		C 	 Ea!4!45 ""1%a* ""1%e$r   c                     dt         fd}t        j                   |             }| j                  |      }| j	                  |d       y)zc
        L{Deferred.fromCoroutine} allows a function to C{await} on a
        L{Deferred}.
        r    c                  <   K   t        d      } |  d {   }|S 7 wr(   r#   r*   s     r   r&   z"AwaitTests.test_basic.<locals>.runN   r+   r,   r)   N)r-   r   r1   r2   r   r   r&   r   r$   s       r   
test_basiczAwaitTests.test_basicH   sB    	3 	
 ""35)""1%e$r   c                 ~    dt         fd}t         |             }| j                  |      }| j                  |d       y)zS
        L{ensureDeferred} allows a function to C{await} on a L{Deferred}.
        r    c                  <   K   t        d      } |  d {   }|S 7 wr(   r#   r*   s     r   r&   z0AwaitTests.test_basicEnsureDeferred.<locals>.run\   r+   r,   r)   N)r-   r   r2   r   r7   s       r   test_basicEnsureDeferredz#AwaitTests.test_basicEnsureDeferredW   s<    
	3 	
 35!""1%e$r   c                    dt         fd}t        j                   |             }| j                  |      }| j	                  t        |j                        t               | j	                  |j                  j                  d       y)z
        An exception in a coroutine scheduled with L{Deferred.fromCoroutine}
        will cause the returned L{Deferred} to fire with a failure.
        r    c                  N   K   t        d      } |  d {    t        d      7 w)Nr)   Oh no!)r   
ValueError)r   s    r   r&   z&AwaitTests.test_exception.<locals>.runk   s&     AGGX&& s   %#%)r>   N)	r   r   r1   failureResultOfr   typevaluer?   argsr7   s       r   test_exceptionzAwaitTests.test_exceptione   sb    	'8 	'
 ""35)""1%cii*55r   c                    dd}t        |      dfd}| j                  t        j                   |                   }| j	                  d|j                                | j	                  d|j                                y)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.
        Nc                      t               N)r   r   r   r   raiseszCAwaitTests.test_synchronousDeferredFailureTraceback.<locals>.raises|   s    !##r   c                  $   K     d {   S 7 wrG   r   its   r   doomedzCAwaitTests.test_synchronousDeferredFailureTraceback.<locals>.doomed        8O8   , in doomed
z, in raises
r    N)r   r@   r   r1   assertIngetTraceback)r   rH   rL   failurerK   s       @r   (test_synchronousDeferredFailureTracebackz3AwaitTests.test_synchronousDeferredFailureTracebacku   sf    	$ 6"	 &&x'='=fh'GHow';';'=>ow';';'=>r   c                 ^   dt         fd}t               dfd}t        j                   |             }| j                  |       j	                   |              | j                  |      }| j                  d|j                                | j                  d|j                                y)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                  J    	 t               # t         $ r t               cY S w xY wrG   )r   r
   r   r   r   returnsFailurezEAwaitTests.test_asyncDeferredFailureTraceback.<locals>.returnsFailure   s'    !%''" !y !s   
 ""Nc                  $   K     d {   S 7 wrG   r   rJ   s   r   rL   z=AwaitTests.test_asyncDeferredFailureTraceback.<locals>.doomed   rM   rN   rO   z, in returnsFailure
rP   )r
   r   r1   assertNoResulterrbackr@   rQ   rR   )r   rW   rL   startedrS   rK   s        @r   "test_asyncDeferredFailureTracebackz-AwaitTests.test_asyncDeferredFailureTraceback   s    	! 	! &Z	 ((2G$


>#$&&w/ow';';'=>-w/C/C/EFr   c                    t               g dt        ffddt        ffd}t        j                   |             }j	                  d       | j                  ddg       j	                  d       | j                  g d       j	                  d       | j                  g d       j	                  d       | j                  g d	       | j                  |      }| j                  |d
       y)z
        A coroutine scheduled with L{Deferred.fromCoroutine} that awaits a
        L{Deferred} suspends its execution until the inner L{Deferred} fires.
        r    c                     K   j                  d       t               } j                  d| j                  d       |  d {    j                  d       y7 w)N         Yay!appendr   	callLatercallback)r   reactorsectionss    r   runonez'AwaitTests.test_twoDeep.<locals>.runone   sK     OOA'zAaQ/GGOOA s   >AAAc                     K   j                  d                d {   } j                  d       t               }j                  d|j                  d       | d {    j                  d       | S 7 Y7 w)Nr`         rc   )resultr   rg   ri   rh   s     r   r&   z$AwaitTests.test_twoDeep.<locals>.run   sh     OOA!8^FOOA'zAaQ/GGOOAM $ s"   A=A9AA=!A;"A=;A=g?r`   r_   皙?)r`   r_   ra   rk   )r`   r_   ra   rk   rl   rb   N)r	   r-   r   r1   advancer   r2   )r   r&   r   r$   rg   ri   rh   s       @@@r   test_twoDeepzAwaitTests.test_twoDeep   s    
 '	c 		3 	 ""35)Aq6*<0<0?3""1%f%r   c                      dt         f fd} j                  t        j                   |                   } j	                  |d       y)zO
        Awaiting an already failed Deferred will raise the exception.
        r    c                     K   	 t        t        d             d {    y7 # t        $ r&} j                  | j                  d       Y d } ~ yd } ~ ww xY ww)NBoom)rs   r`   r   )r   r?   r   rC   )er   s    r   testz%AwaitTests.test_reraise.<locals>.test   sO     :f-... 	 /   3s6   A$ "$ A$ 	AA	AAAr`   N)intr2   r   r1   r   )r   ru   r$   s   `  r   test_reraisezAwaitTests.test_reraise   s>    
	C 	 ""8#9#9$&#ABa r   c                     t               dfd}t        j                   |             }j                  d       | j	                  |      }| j                  |d       y)zd
        Awaiting a paused & chained Deferred will give the result when it has
        one.
        Nc                     K   t               } t               | j                  fd       | j                  d        j                  dj                  d       |  d {   S 7 w)Nc                     S rG   r   )ignoredd2s    r   <lambda>z7AwaitTests.test_chained.<locals>.test.<locals>.<lambda>   s    " r   r   bye)r   addCallbackrf   re   )r   r|   rg   s    @r   ru   z%AwaitTests.test_chained.<locals>.test   sM      (
A!)BMM,-JJtae47N7s   AA' A%!A'rn   r~   rP   )r	   r   r1   ro   r2   r   )r   ru   r   r$   rg   s       @r   test_chainedzAwaitTests.test_chained   sQ    
 '	 ""46*""1%e$r   rP   )r   r   r   r   r   r4   r8   r;   rD   rT   r\   rp   rw   r   r   r   r   r   r      s<    A%8%%6 ?(G2)&V! %r   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   <module>r      sA   
  &  ( * +i R% R%r   