a
    õ£BeM(  ã                   @   s6  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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�r2e #¡  dS )é    )ÚgenÚioloop)Ú
HTTPServer)ÚEvent)ÚAsyncHTTPTestCaseÚAsyncTestCaseÚbind_unused_portÚgen_test©ÚApplicationNc              	   c   s`   t j | ¡}|t j| < z&d V  W |d u r2t j| = q\|t j| < n|d u rPt j| = n
|t j| < 0 d S ©N)ÚosÚenvironÚget)ÚnameÚvalueÚ	old_value© r   ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/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 d   ƒ n1 sj0    Y  | j  |ƒ d | j¡ tddƒ�D |  | j¡� |  ¡  W d   ƒ n1 sÀ0    Y  W d   ƒ n1 sÞ0    Y  d S )Nç{®Gáz„?é   ©Ú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y&   Y n0 d S r   )r    ÚasyncioÚ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                    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   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://127.0.0.1:%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                   @   sZ   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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 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 sR0    Y  |  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)   r[   r   r   r   r   rK   �   s   rK   r[   Ú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   Z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)   rd   r   r   r   r   rK   ©   s   rK   rd   r   z%Return value from test method ignoredr   rM   rT   r   r   r   rd   ¨   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)   re   r   r   r   r   rK   ´   s   rK   re   )r   ZassertIsÚinspectÚunwrapre   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_implementationÚsysÚversion_infor`   rb   rd   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ƒ |¡ 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                    s   ˆ   d¡ d S )Nr8   ©Úappendr%   ©Úeventsr   r   r8   Í   s    zDSetUpTearDownTest.test_set_up_tear_down.<locals>.SetUpTearDown.setUpc                    s   ˆ   d¡ d S )Nr,   rn   r%   rp   r   r   r,   Ð   s    zGSetUpTearDownTest.test_set_up_tear_down.<locals>.SetUpTearDown.tearDownN)r'   r(   r)   r8   r,   r   rp   r   r   ÚSetUpTearDownÌ   s   rr   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   rn   r%   rp   r   r   rU   Ô   s    zASetUpTearDownTest.test_set_up_tear_down.<locals>.InheritBoth.testN)r'   r(   r)   rU   r   rp   r   r   ÚInheritBothÓ   s   rt   rU   )r8   rU   r,   N)rN   rO   ÚTestCaser   rP   r@   )r"   rV   rr   rt   Úexpectedr   rp   r   Útest_set_up_tear_downÀ   s    	z'SetUpTearDownTest.test_set_up_tear_downN)r'   r(   r)   rw   r   r   r   r   rm   ¿   s   rm   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   rr   à   s   rr   rU   ry   rz   )rN   rO   r   rP   ZassertFalser|   )r"   rV   rr   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   rx   Ü   s   rx   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yL   |  dt ¡ ¡ Y n0 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   sN   t dd�dd„ ƒ}tddƒ� || ƒ W d   ƒ n1 s:0    Y  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   sz   t dd�dd„ ƒ}tddƒ�D |  tj¡� || ƒ W d   ƒ n1 sH0    Y  W d   ƒ n1 sf0    Y  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    
D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_args3  s    z5GenTest.test_with_method_args.<locals>.test_with_argsrU   Tr�   )r"   r˜   r   r   r   Útest_with_method_args2  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 rs   )ZassertDictEqualr   rƒ   )r"   Úkwargsr   r   r   Útest_with_kwargs<  s    z9GenTest.test_with_method_kwargs.<locals>.test_with_kwargsrU   r–   Tr�   )r"   r›   r   r   r   Útest_with_method_kwargs;  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   E  s    z+GenTest.test_native_coroutine.<locals>.test)r	   r�   r   r   r   Útest_native_coroutineD  s    
zGenTest.test_native_coroutinec                 C   sH   t dd�dd„ ƒ}z|| ƒ |  d¡ W n tjyB   d| _Y n0 d S )Nr$   r   c                 Ó   s   t  d¡I d H  d S r…   r†   r%   r   r   r   rU   M  s    z3GenTest.test_native_coroutine_timeout.<locals>.testrˆ   T)r	   r‰   r   rŠ   r   r�   r   r   r   Útest_native_coroutine_timeoutK  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   r0   Ú
contextlibrf   r-   r   ri   rk   r‹   rN   r]   Úcontextmanagerr   r   r*   r7   ru   rI   rm   rx   r~   r'   Úmainr   r   r   r   Ú<module>   s2   
! Fj
