o
    )Zh)                     @   s0  d dl mZmZ d dlmZ d dlmZ d dlmZm	Z	m
Z
mZ d dlmZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZejdd ZG d	d
 d
e	ZG dd de	ZG dd deZG dd dejZG dd dejZG dd dejZG dd de	Z e!dkre"  dS dS )    )genioloop)
HTTPServer)Event)AsyncHTTPTestCaseAsyncTestCasebind_unused_portgen_testApplicationNc              	   c   sb    t j| }|t j| < zd V  W |d u rt j| = d S |t j| < d S |d u r+t j| = w |t j| < w N)osenvironget)namevalue	old_value r   P/var/www/html/lang_env/lib/python3.10/site-packages/tornado/test/testing_test.pyset_environ   s   
r   c                   @      e Zd Zdd Zdd ZdS )AsyncTestCaseTestc              	   C   s   | j j}| j | d | j |   | j | d | j | | j | jdd W d    n1 s5w   Y  | j | d | j tdd+ | | j |   W d    n1 s`w   Y  W d    d S W d    d S 1 sxw   Y  d S )N{Gz?   timeoutASYNC_TEST_TIMEOUTz0.01)io_looptimeadd_timeoutstopwaitassertRaisesZfailureExceptionr   )selfr   r   r   r   test_wait_timeout    s   
"z#AsyncTestCaseTest.test_wait_timeoutc                 C   sP   | j | j  d | j | jdd | j | j  d | j | jdd dS )ze
        This test makes sure that a second call to wait()
        clears the first timeout.
        g        皙?r   g?g?N)r   r   r   r    r!   r#   r   r   r   test_subsequent_wait_calls2   s   z,AsyncTestCaseTest.test_subsequent_wait_callsN)__name__
__module____qualname__r$   r'   r   r   r   r   r      s    r   c                       s$   e Zd Z fddZdd Z  ZS )LeakTestc                    s   t    t  d S r   )supertearDowngcZcollectr&   	__class__r   r   r-   A   s   
zLeakTest.tearDownc                    s8   t    fdd}| j| | j| j |   d S )Nc                      s.   z
   I d H  W d S  tjy   Y d S w r   )r!   asyncioZCancelledErrorr   eventr   r   callbackM   s   z0LeakTest.test_leaked_coroutine.<locals>.callback)r   r   Zadd_callbackr    r!   )r#   r4   r   r2   r   test_leaked_coroutineF   s
   zLeakTest.test_leaked_coroutine)r(   r)   r*   r-   r5   __classcell__r   r   r/   r   r+   @   s    r+   c                       s@   e Zd Z fddZdd Zdd Zdd Z fd	d
Z  ZS )AsyncHTTPTestCaseTestc                    sH   t    t \}}t }t|fi |  }|| || _|| _d S r   )	r,   setUpr   r   r   Zget_httpserver_optionsZ
add_socketsecond_portsecond_server)r#   sockportZappserverr/   r   r   r8   Y   s   



zAsyncHTTPTestCaseTest.setUpc                 C      t  S r   r
   r&   r   r   r   get_appc      zAsyncHTTPTestCaseTest.get_appc                 C   s(   d}|  |}| |jj| | d S )Nz/path)fetchassertEqualrequesturlZget_urlr#   pathresponser   r   r   test_fetch_segmentf   s   
z(AsyncHTTPTestCaseTest.test_fetch_segmentc                 C   s(   d| j  }| |}| |jj| d S )Nzhttp://127.0.0.1:%d/path)r9   rA   rB   rC   rD   rE   r   r   r   test_fetch_full_http_urlk   s   

z.AsyncHTTPTestCaseTest.test_fetch_full_http_urlc                    s   | j   t   d S r   )r:   r    r,   r-   r&   r/   r   r   r-   s   s   
zAsyncHTTPTestCaseTest.tearDown)	r(   r)   r*   r8   r?   rH   rI   r-   r6   r   r   r/   r   r7   X   s    
r7   c                   @   sR   e Zd Zdd Zee dkdeej	dkddd Z
d	d
 Zdd ZdS )!AsyncTestCaseReturnAssertionsTestc                 C   V   G dd dt }|d}t }|| | t|jd | d|jd d  d S )Nc                   @      e Zd Zdd ZdS )zJAsyncTestCaseReturnAssertionsTest.test_undecorated_generator.<locals>.Testc                 s       d V  d S r   r   r&   r   r   r   test_gen   s   
zSAsyncTestCaseReturnAssertionsTest.test_undecorated_generator.<locals>.Test.test_genN)r(   r)   r*   rN   r   r   r   r   Test~       rO   rN   r   should be decoratedr   r   unittest
TestResultrunrB   lenerrorsassertInr#   rO   testresultr   r   r   test_undecorated_generator}      
z<AsyncTestCaseReturnAssertionsTest.test_undecorated_generatorPyPyz+pypy destructor warnings cannot be silenced)      z-py312 has its own check for test case returnsc                 C   s   G dd dt }|d}t }t  td || W d    n1 s)w   Y  | t|j	d | 
d|j	d d  d S )Nc                   @   rL   )zJAsyncTestCaseReturnAssertionsTest.test_undecorated_coroutine.<locals>.Testc                    s   d S r   r   r&   r   r   r   	test_coro   s   zTAsyncTestCaseReturnAssertionsTest.test_undecorated_coroutine.<locals>.Test.test_coroN)r(   r)   r*   ra   r   r   r   r   rO      rP   rO   ra   ignorer   rQ   r   )r   rS   rT   warningscatch_warningssimplefilterrU   rB   rV   rW   rX   rY   r   r   r   test_undecorated_coroutine   s   

z<AsyncTestCaseReturnAssertionsTest.test_undecorated_coroutinec                 C   sR   G dd dt }|d}t }|| | t|jd | t|jd d S )Nc                   @   s   e Zd Zeddd ZdS )zTAsyncTestCaseReturnAssertionsTest.test_undecorated_generator_with_skip.<locals>.Testzdon't run thisc                 s   rM   r   r   r&   r   r   r   rN         
z]AsyncTestCaseReturnAssertionsTest.test_undecorated_generator_with_skip.<locals>.Test.test_genN)r(   r)   r*   rS   skiprN   r   r   r   r   rO      s    rO   rN   r   r   )r   rS   rT   rU   rB   rV   rW   ZskippedrY   r   r   r   $test_undecorated_generator_with_skip   s   
zFAsyncTestCaseReturnAssertionsTest.test_undecorated_generator_with_skipc                 C   rK   )Nc                   @   rL   )zAAsyncTestCaseReturnAssertionsTest.test_other_return.<locals>.Testc                 S   s   dS )N*   r   r&   r   r   r   test_other_return   s   zSAsyncTestCaseReturnAssertionsTest.test_other_return.<locals>.Test.test_other_returnN)r(   r)   r*   rk   r   r   r   r   rO      rP   rO   rk   r   z%Return value from test method ignoredr   rR   rY   r   r   r   rk      r]   z3AsyncTestCaseReturnAssertionsTest.test_other_returnN)r(   r)   r*   r\   rS   ZskipIfplatformpython_implementationsysversion_inforf   ri   rk   r   r   r   r   rJ   x   s    
rJ   c                   @   rL   )SetUpTearDownTestc                    s^   g  t  }G  fdddt j}G  fdddt|}|d| g d}| |  dS )a  
        This test makes sure that AsyncTestCase calls super methods for
        setUp and tearDown.

        InheritBoth is a subclass of both AsyncTestCase and
        SetUpTearDown, with the ordering so that the super of
        AsyncTestCase will be SetUpTearDown.
        c                       s$   e Zd Z fddZ fddZdS )z>SetUpTearDownTest.test_set_up_tear_down.<locals>.SetUpTearDownc                         d d S )Nr8   appendr&   eventsr   r   r8         zDSetUpTearDownTest.test_set_up_tear_down.<locals>.SetUpTearDown.setUpc                    rq   )Nr-   rr   r&   rt   r   r   r-      rv   zGSetUpTearDownTest.test_set_up_tear_down.<locals>.SetUpTearDown.tearDownN)r(   r)   r*   r8   r-   r   rt   r   r   SetUpTearDown   s    rw   c                       s   e Zd Z fddZdS )z<SetUpTearDownTest.test_set_up_tear_down.<locals>.InheritBothc                    rq   NrZ   rr   r&   rt   r   r   rZ      rv   zASetUpTearDownTest.test_set_up_tear_down.<locals>.InheritBoth.testN)r(   r)   r*   rZ   r   rt   r   r   InheritBoth   s    ry   rZ   )r8   rZ   r-   N)rS   rT   TestCaser   rU   rB   )r#   r[   rw   ry   expectedr   rt   r   test_set_up_tear_down   s   	z'SetUpTearDownTest.test_set_up_tear_downN)r(   r)   r*   r|   r   r   r   r   rp      rP   rp   c                   @   rL   )"AsyncHTTPTestCaseSetUpTearDownTestc                 C   sN   t  }G dd dt}|d}|| | t|d | t|d d S )Nc                   @   r   )zeAsyncHTTPTestCaseSetUpTearDownTest.test_tear_down_releases_app_and_http_server.<locals>.SetUpTearDownc                 S   r>   r   r
   r&   r   r   r   r?      r@   zmAsyncHTTPTestCaseSetUpTearDownTest.test_tear_down_releases_app_and_http_server.<locals>.SetUpTearDown.get_appc                 S   s$   |  t| d |  t| d d S )N_apphttp_server)
assertTruehasattrr&   r   r   r   rZ      s   zjAsyncHTTPTestCaseSetUpTearDownTest.test_tear_down_releases_app_and_http_server.<locals>.SetUpTearDown.testN)r(   r)   r*   r?   rZ   r   r   r   r   rw      s    rw   rZ   r~   r   )rS   rT   r   rU   ZassertFalser   )r#   r[   rw   rZ   r   r   r   +test_tear_down_releases_app_and_http_server   s   
zNAsyncHTTPTestCaseSetUpTearDownTest.test_tear_down_releases_app_and_http_serverN)r(   r)   r*   r   r   r   r   r   r}      rP   r}   c                       s   e Zd Z fddZ fddZedd Ze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  ZS )GenTestc                    s   t    d| _d S )NF)r,   r8   finishedr&   r/   r   r   r8      s   

zGenTest.setUpc                    s   |  | j t   d S r   )r   r   r,   r-   r&   r/   r   r   r-      s   zGenTest.tearDownc                 C   s
   d| _ d S NTr   r&   r   r   r   	test_sync   s   
zGenTest.test_syncc                 c   s    t jV  d| _d S r   )r   momentr   r&   r   r   r   
test_async   s   
zGenTest.test_asyncc                 C   sX   t dddd }z||  | d W n tjy&   | dt  Y nw d| _d S )Nr%   r   c                 s       t dV  d S Nr   r   sleepr&   r   r   r   rZ         z"GenTest.test_timeout.<locals>.testdid not get expected exceptionzgen.sleep(1)T)r	   failr   TimeoutErrorrX   	traceback
format_excr   r#   rZ   r   r   r   test_timeout   s   

zGenTest.test_timeoutc                 C   s$   t dddd }||  d| _d S )Nr   r   c                 s   r   )Nr%   r   r&   r   r   r   rZ     r   z%GenTest.test_no_timeout.<locals>.testTr	   r   r   r   r   r   test_no_timeout  s   

zGenTest.test_no_timeoutc                 C   sN   t dddd }tdd ||  W d    n1 sw   Y  d| _d S )Ng      ?r   c                 s   r   )Ng      ?r   r&   r   r   r   test_long_timeout  r   zDGenTest.test_timeout_environment_variable.<locals>.test_long_timeoutr   0.1T)r	   r   r   )r#   r   r   r   r   !test_timeout_environment_variable  s   


z)GenTest.test_timeout_environment_variablec              	   C   sz   t dddd }tdd" | tj ||  W d    n1 s$w   Y  W d    n1 s3w   Y  d| _d S )Nr   r   c                 s   r   r   r   r&   r   r   r   test_short_timeout"  r   zHGenTest.test_no_timeout_environment_variable.<locals>.test_short_timeoutr   r   T)r	   r   r"   r   r   r   )r#   r   r   r   r   $test_no_timeout_environment_variable!  s   


z,GenTest.test_no_timeout_environment_variablec                 C   s    t dd }|| d d| _d S )Nc                 w   s    |  |d tjV  d S )NrZ   )rB   r   r   )r#   argsr   r   r   test_with_args.  s   z5GenTest.test_with_method_args.<locals>.test_with_argsrZ   Tr   )r#   r   r   r   r   test_with_method_args-  s   


zGenTest.test_with_method_argsc                 C   s"   t dd }|| dd d| _d S )Nc                 {   s    |  |ddi tjV  d S rx   )ZassertDictEqualr   r   )r#   kwargsr   r   r   test_with_kwargs7  s   z9GenTest.test_with_method_kwargs.<locals>.test_with_kwargsrZ   r   Tr   )r#   r   r   r   r   test_with_method_kwargs6  s   

zGenTest.test_with_method_kwargsc                 C   s   t dd }||  d S )Nc                    s   d| _ d S r   r   r&   r   r   r   rZ   @  rg   z+GenTest.test_native_coroutine.<locals>.test)r	   r   r   r   r   test_native_coroutine?  s   
zGenTest.test_native_coroutinec                 C   sH   t dddd }z||  | d W d S  tjy#   d| _Y d S w )Nr%   r   c                    s   t dI d H  d S r   r   r&   r   r   r   rZ   H  s   z3GenTest.test_native_coroutine_timeout.<locals>.testr   T)r	   r   r   r   r   r   r   r   r   test_native_coroutine_timeoutF  s   
z%GenTest.test_native_coroutine_timeout)r(   r)   r*   r8   r-   r	   r   r   r   r   r   r   r   r   r   r   r6   r   r   r/   r   r      s    

			r   __main__)#tornador   r   Ztornado.httpserverr   Ztornado.locksr   Ztornado.testingr   r   r   r	   Ztornado.webr   r1   
contextlibr.   r   rl   rn   r   rS   rc   contextmanagerr   r   r+   r7   rz   rJ   rp   r}   r   r(   mainr   r   r   r   <module>   s4    
! Bj