U
    d+                     @   sJ  d dl mZmZ d dl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"G dd deZ#e$dkrFe%  dS )    )genioloop)
HTTPServer)Event)ignore_deprecation)AsyncHTTPTestCaseAsyncTestCasebind_unused_portgen_testApplicationNc              	   c   sD   t j| }|t j| < z
d V  W 5 |d kr4t j| = n
|t j| < X d S N)osenvironget)namevalue	old_value r   =/tmp/pip-unpacked-wheel-fekwu36z/tornado/test/testing_test.pyset_environ   s    


r   c                   @   s   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 5 Q R X | j | d | j tdd& | | j |   W 5 Q R X W 5 Q R X 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_calls3   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-   B   s    
zLeakTest.tearDownc                    s8   t    fdd}| j| | j| j |   d S )Nc                      s.   z   I d H  W n tjk
r(   Y nX d S r   )r!   asyncioZCancelledErrorr   eventr   r   callbackN   s    z0LeakTest.test_leaked_coroutine.<locals>.callback)r   r   Zadd_callbackr    r!   )r#   r4   r   r2   r   test_leaked_coroutineG   s
    zLeakTest.test_leaked_coroutine)r(   r)   r*   r-   r5   __classcell__r   r   r/   r   r+   A   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                    sD   t    t \}}t }t|f|  }|| || _|| _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   Z   s    


zAsyncHTTPTestCaseTest.setUpc                 C   s   t  S r   r   r&   r   r   r   get_appd   s    zAsyncHTTPTestCaseTest.get_appc                 C   s(   d}|  |}| |jj| | d S )Nz/path)fetchassertEqualrequesturlget_urlr#   pathresponser   r   r   test_fetch_segmentg   s    
z(AsyncHTTPTestCaseTest.test_fetch_segmentc                 C   s(   d| j  }| |}| |jj| d S )Nzhttp://localhost:%d/path)r9   r?   r@   rA   rB   rD   r   r   r   test_fetch_full_http_urll   s    

z.AsyncHTTPTestCaseTest.test_fetch_full_http_urlc                    s   | j   t   d S r   )r:   r    r,   r-   r&   r/   r   r   r-   t   s    
zAsyncHTTPTestCaseTest.tearDown)	r(   r)   r*   r8   r>   rG   rH   r-   r6   r   r   r/   r   r7   Y   s
   
r7   c                   @   sH   e Zd Zdd Zee dkddd Zdd Z	d	d
 Z
dd ZdS )AsyncTestCaseWrapperTestc                 C   sV   G dd dt }|d}t }|| | t|jd | d|jd d  d S )Nc                   @   s   e Zd Zdd ZdS )zAAsyncTestCaseWrapperTest.test_undecorated_generator.<locals>.Testc                 s   s
   d V  d S r   r   r&   r   r   r   test_gen|   s    zJAsyncTestCaseWrapperTest.test_undecorated_generator.<locals>.Test.test_genN)r(   r)   r*   rJ   r   r   r   r   Test{   s   rK   rJ   r   should be decoratedr   r   unittest
TestResultrunr@   lenerrorsassertInr#   rK   testresultr   r   r   test_undecorated_generatorz   s    
z3AsyncTestCaseWrapperTest.test_undecorated_generatorPyPyz+pypy destructor warnings cannot be silencedc              	   C   st   G dd dt }|d}t }t  td || W 5 Q R X | t|j	d | 
d|j	d d  d S )Nc                   @   s   e Zd Zdd ZdS )zAAsyncTestCaseWrapperTest.test_undecorated_coroutine.<locals>.Testc                    s   d S r   r   r&   r   r   r   	test_coro   s    zKAsyncTestCaseWrapperTest.test_undecorated_coroutine.<locals>.Test.test_coroN)r(   r)   r*   rY   r   r   r   r   rK      s   rK   rY   ignorer   rL   r   )r   rN   rO   warningscatch_warningssimplefilterrP   r@   rQ   rR   rS   rT   r   r   r   test_undecorated_coroutine   s    

z3AsyncTestCaseWrapperTest.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 )zKAsyncTestCaseWrapperTest.test_undecorated_generator_with_skip.<locals>.Testzdon't run thisc                 s   s
   d V  d S r   r   r&   r   r   r   rJ      s    zTAsyncTestCaseWrapperTest.test_undecorated_generator_with_skip.<locals>.Test.test_genN)r(   r)   r*   rN   skiprJ   r   r   r   r   rK      s   rK   rJ   r   r   )r   rN   rO   rP   r@   rQ   rR   skippedrT   r   r   r   $test_undecorated_generator_with_skip   s    
z=AsyncTestCaseWrapperTest.test_undecorated_generator_with_skipc                 C   sV   G dd dt }|d}t }|| | t|jd | d|jd d  d S )Nc                   @   s   e Zd Zdd ZdS )z8AsyncTestCaseWrapperTest.test_other_return.<locals>.Testc                 S   s   dS )N*   r   r&   r   r   r   test_other_return   s    zJAsyncTestCaseWrapperTest.test_other_return.<locals>.Test.test_other_returnN)r(   r)   r*   rc   r   r   r   r   rK      s   rK   rc   r   z%Return value from test method ignoredr   rM   rT   r   r   r   rc      s    
z*AsyncTestCaseWrapperTest.test_other_returnc                 C   s4   G dd dt }|d}| t|j|jj d S )Nc                   @   s   e Zd Zdd ZdS )z2AsyncTestCaseWrapperTest.test_unwrap.<locals>.Testc                 S   s   d S r   r   r&   r   r   r   test_foo   s    z;AsyncTestCaseWrapperTest.test_unwrap.<locals>.Test.test_fooN)r(   r)   r*   rd   r   r   r   r   rK      s   rK   rd   )r   assertIsinspectunwraprd   Zorig_method)r#   rK   rU   r   r   r   test_unwrap   s    
z$AsyncTestCaseWrapperTest.test_unwrapN)r(   r)   r*   rW   rN   ZskipIfplatformpython_implementationr^   ra   rc   rh   r   r   r   r   rI   y   s   

rI   c                   @   s   e Zd Zdd ZdS )SetUpTearDownTestc                    s`   g  t  }G  fdddt j}G  fdddt|}|d| dddg}| |  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                    s     d d S )Nr8   appendr&   eventsr   r   r8      s    zDSetUpTearDownTest.test_set_up_tear_down.<locals>.SetUpTearDown.setUpc                    s     d d S )Nr-   rl   r&   rn   r   r   r-      s    zGSetUpTearDownTest.test_set_up_tear_down.<locals>.SetUpTearDown.tearDownN)r(   r)   r*   r8   r-   r   rn   r   r   SetUpTearDown   s   rp   c                       s   e Zd Z fddZdS )z<SetUpTearDownTest.test_set_up_tear_down.<locals>.InheritBothc                    s     d d S NrU   rl   r&   rn   r   r   rU      s    zASetUpTearDownTest.test_set_up_tear_down.<locals>.InheritBoth.testN)r(   r)   r*   rU   r   rn   r   r   InheritBoth   s   rr   rU   r8   r-   N)rN   rO   TestCaser   rP   r@   )r#   rV   rp   rr   expectedr   rn   r   test_set_up_tear_down   s    	
z'SetUpTearDownTest.test_set_up_tear_downN)r(   r)   r*   ru   r   r   r   r   rk      s   rk   c                   @   s   e Zd Zdd ZdS )"AsyncHTTPTestCaseSetUpTearDownTestc                 C   sN   t  }G dd dt}|d}|| | t|d | t|d d S )Nc                   @   s   e Zd Zdd Zdd ZdS )zeAsyncHTTPTestCaseSetUpTearDownTest.test_tear_down_releases_app_and_http_server.<locals>.SetUpTearDownc                 S   s   t  S r   r   r&   r   r   r   r>      s    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   rU      s    zjAsyncHTTPTestCaseSetUpTearDownTest.test_tear_down_releases_app_and_http_server.<locals>.SetUpTearDown.testN)r(   r)   r*   r>   rU   r   r   r   r   rp      s   rp   rU   rw   rx   )rN   rO   r   rP   assertFalserz   )r#   rV   rp   rU   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   rv      s   rv   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   )ry   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   sZ   t dddd }z||  | d W n& tjk
rN   | dt  Y nX d| _d S )Nr%   r   c                 s   s   t dV  d S Nr   r   sleepr&   r   r   r   rU      s    z"GenTest.test_timeout.<locals>.testdid not get expected exceptionzgen.sleep(1)T)r
   failr   TimeoutErrorrS   	traceback
format_excr~   r#   rU   r   r   r   test_timeout   s    
zGenTest.test_timeoutc                 C   s$   t dddd }||  d| _d S )Nr   r   c                 s   s   t dV  d S )Nr%   r   r&   r   r   r   rU     s    z%GenTest.test_no_timeout.<locals>.testTr
   r~   r   r   r   r   test_no_timeout  s    
zGenTest.test_no_timeoutc              	   C   s:   t dddd }tdd ||  W 5 Q R X d| _d S )Ng      ?r   c                 s   s   t dV  d S )Ng      ?r   r&   r   r   r   test_long_timeout  s    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   sR   t dddd }tdd& | tj ||  W 5 Q R X W 5 Q R X d| _d S )Nr   r   c                 s   s   t dV  d S r   r   r&   r   r   r   test_short_timeout$  s    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rU   )r@   r   r   )r#   argsr   r   r   test_with_args0  s    z5GenTest.test_with_method_args.<locals>.test_with_argsrU   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 rq   )ZassertDictEqualr   r   )r#   kwargsr   r   r   test_with_kwargs9  s    z9GenTest.test_with_method_kwargs.<locals>.test_with_kwargsrU   r   Tr   )r#   r   r   r   r   test_with_method_kwargs8  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   rU   B  s    z+GenTest.test_native_coroutine.<locals>.test)r
   r   r   r   r   test_native_coroutineA  s    
zGenTest.test_native_coroutinec                 C   sJ   t dddd }z||  | d W n tjk
rD   d| _Y nX d S )Nr%   r   c                    s   t dI d H  d S r   r   r&   r   r   r   rU   J  s    z3GenTest.test_native_coroutine_timeout.<locals>.testr   T)r
   r   r   r   r~   r   r   r   r   test_native_coroutine_timeoutH  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}   c                       s8   e Zd Zdd Z fddZ fddZdd Z  ZS )	GetNewIOLoopTestc                 C   s
   t j S r   )r   ZIOLoopcurrentr&   r   r   r   get_new_ioloopV  s    zGetNewIOLoopTest.get_new_ioloopc              	      s`   t  0 zt | _W n tk
r0   d | _Y nX W 5 Q R X t | _t| j t 	  d S r   )
r   r1   get_event_loop	orig_loopRuntimeErrorZnew_event_loopnew_loopset_event_loopr,   r8   r&   r/   r   r   r8   Z  s    
zGetNewIOLoopTest.setUpc                    s6   t    | t   t| j | j	  d S r   )
r,   r-   r{   r1   r   Z	is_closedr   r   r   closer&   r/   r   r   r-   f  s    
zGetNewIOLoopTest.tearDownc                 C   s   |  | jj| j d S r   )re   r   Zasyncio_loopr   r&   r   r   r   	test_loopm  s    zGetNewIOLoopTest.test_loop)r(   r)   r*   r   r8   r-   r   r6   r   r   r/   r   r   U  s   r   __main__)&Ztornador   r   Ztornado.httpserverr   Ztornado.locksr   Ztornado.test.utilr   Ztornado.testingr   r   r	   r
   Ztornado.webr   r1   
contextlibrf   r.   r   ri   r   rN   r[   contextmanagerr   r   r+   r7   rs   rI   rk   rv   r}   r   r(   mainr   r   r   r   <module>   s4   
! Cj
