o
    Qœ_×)  ã                   @   s8  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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rše "¡  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   ú;/usr/lib/python3/dist-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ç{®Gáz„?é   ©ÚtimeoutÚASYNC_TEST_TIMEOUTz0.01)Úio_loopÚtimeÚadd_timeoutÚstopÚwaitÚassertRaisesÚ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_calls1   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ÚgcÚcollectr'   ©Ú	__class__r   r   r.   @   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!   ÚasyncioÚCancelledErrorr   ©Úeventr   r   ÚcallbackL   s   €ÿz0LeakTest.test_leaked_coroutine.<locals>.callback)r   r   Úadd_callbackr    r!   )r$   r7   r   r5   r   Útest_leaked_coroutineE   s
   zLeakTest.test_leaked_coroutine)r)   r*   r+   r.   r9   Ú__classcell__r   r   r1   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   Úget_httpserver_optionsÚ
add_socketÚsecond_portÚsecond_server)r$   ÚsockÚportÚappÚserverr1   r   r   r<   X   s   



zAsyncHTTPTestCaseTest.setUpc                 C   ó   t ƒ S r   r
   r'   r   r   r   Úget_appb   ó   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_segmente   s   
z(AsyncHTTPTestCaseTest.test_fetch_segmentc                 C   s(   d| j  }|  |¡}|  |jj|¡ d S )Nzhttp://localhost:%d/path)r?   rH   rI   rJ   rK   rM   r   r   r   Útest_fetch_full_http_urlj   s   

z.AsyncHTTPTestCaseTest.test_fetch_full_http_urlc                    s   | j  ¡  tƒ  ¡  d S r   )r@   r    r-   r.   r'   r1   r   r   r.   r   s   
zAsyncHTTPTestCaseTest.tearDown)	r)   r*   r+   r<   rF   rP   rQ   r.   r:   r   r   r1   r   r;   W   s    
r;   c                   @   s@   e Zd Zdd„ Ze e ¡ dkd¡dd„ ƒZdd„ Z	d	d
„ Z
dS )ÚAsyncTestCaseWrapperTestc                 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 )zAAsyncTestCaseWrapperTest.test_undecorated_generator.<locals>.Testc                 s   ó   � d V  d S r   r   r'   r   r   r   Útest_genz   s   €
zJAsyncTestCaseWrapperTest.test_undecorated_generator.<locals>.Test.test_genN)r)   r*   r+   rV   r   r   r   r   ÚTesty   ó    rW   rV   r   úshould be decoratedr   ©r   ÚunittestÚ
TestResultÚrunrI   ÚlenÚerrorsÚassertIn©r$   rW   ÚtestÚresultr   r   r   Útest_undecorated_generatorx   ó   
z3AsyncTestCaseWrapperTest.test_undecorated_generatorÚPyPyz+pypy destructor warnings cannot be silencedc                 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                   @   rT   )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+   rg   r   r   r   r   rW   ˆ   rX   rW   rg   Úignorer   rY   r   )r   r[   r\   ÚwarningsÚcatch_warningsÚsimplefilterr]   rI   r^   r_   r`   ra   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   rU   r   r   r'   r   r   r   rV   ™   ó   €
zTAsyncTestCaseWrapperTest.test_undecorated_generator_with_skip.<locals>.Test.test_genN)r)   r*   r+   r[   ÚskiprV   r   r   r   r   rW   ˜   s    rW   rV   r   r   )r   r[   r\   r]   rI   r^   r_   Úskippedra   r   r   r   Ú$test_undecorated_generator_with_skip—   s   
z=AsyncTestCaseWrapperTest.test_undecorated_generator_with_skipc                 C   rS   )Nc                   @   rT   )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+   rr   r   r   r   r   rW   ¤   rX   rW   rr   r   z%Return value from test method ignoredr   rZ   ra   r   r   r   rr   £   re   z*AsyncTestCaseWrapperTest.test_other_returnN)r)   r*   r+   rd   r[   ÚskipIfÚplatformÚpython_implementationrl   rp   rr   r   r   r   r   rR   w   s    
þ
rR   c                   @   rT   )Ú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 )Nr<   ©Úappendr'   ©Úeventsr   r   r<   ½   ó   zDSetUpTearDownTest.test_set_up_tear_down.<locals>.SetUpTearDown.setUpc                    rw   )Nr.   rx   r'   rz   r   r   r.   À   r|   zGSetUpTearDownTest.test_set_up_tear_down.<locals>.SetUpTearDown.tearDownN)r)   r*   r+   r<   r.   r   rz   r   r   ÚSetUpTearDown¼   s    r}   c                       s   e Zd Z‡ fdd„ZdS )z<SetUpTearDownTest.test_set_up_tear_down.<locals>.InheritBothc                    rw   ©Nrb   rx   r'   rz   r   r   rb   Ä   r|   zASetUpTearDownTest.test_set_up_tear_down.<locals>.InheritBoth.testN)r)   r*   r+   rb   r   rz   r   r   ÚInheritBothÃ   s    r   rb   )r<   rb   r.   N)r[   r\   ÚTestCaser   r]   rI   )r$   rc   r}   r   Úexpectedr   rz   r   Útest_set_up_tear_down°   s   	z'SetUpTearDownTest.test_set_up_tear_downN)r)   r*   r+   r‚   r   r   r   r   rv   ¯   rX   rv   c                   @   rT   )Ú"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   rE   r   r
   r'   r   r   r   rF   Ñ   rG   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   rb   Ô   s   zjAsyncHTTPTestCaseSetUpTearDownTest.test_tear_down_releases_app_and_http_server.<locals>.SetUpTearDown.testN)r)   r*   r+   rF   rb   r   r   r   r   r}   Ð   s    r}   rb   r„   r…   )r[   r\   r   r]   ÚassertFalser‡   )r$   rc   r}   rb   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ƒ   Ì   rX   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-   r<   Úfinishedr'   r1   r   r   r<   ß   s   

zGenTest.setUpc                    s   |   | j¡ tƒ  ¡  d S r   )r†   r‹   r-   r.   r'   r1   r   r   r.   ã   s   zGenTest.tearDownc                 C   s
   d| _ d S ©NT©r‹   r'   r   r   r   Ú	test_syncç   ó   
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   rb   ò   ó   €z"GenTest.test_timeout.<locals>.testúdid not get expected exceptionzgen.sleep(1)T)r	   Úfailr   ÚTimeoutErrorr`   Ú	tracebackÚ
format_excr‹   ©r$   rb   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   rb     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©rb   )rI   r   r�   )r$   Úargsr   r   r   Útest_with_args#  s   €z5GenTest.test_with_method_args.<locals>.test_with_argsrb   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 r~   )ÚassertDictEqualr   r�   )r$   Úkwargsr   r   r   Útest_with_kwargs,  s   €z9GenTest.test_with_method_kwargs.<locals>.test_with_kwargsrb   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   rb   5  rm   z+GenTest.test_native_coroutine.<locals>.test)r	   rœ   r   r   r   Útest_native_coroutine4  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   rb   =  s   €z3GenTest.test_native_coroutine_timeout.<locals>.testr—   T)r	   r˜   r   r™   r‹   rœ   r   r   r   Útest_native_coroutine_timeout;  s   
ÿz%GenTest.test_native_coroutine_timeout)r)   r*   r+   r<   r.   r	   rŽ   r‘   r�   rŸ   r¢   r¤   r¨   r¬   r­   r®   r:   r   r   r1   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   ÚIOLoopÚcurrentr'   r   r   r   Úget_new_ioloopI  r�   zGetNewIOLoopTest.get_new_ioloopc                    s.   t  ¡ | _t  ¡ | _t  | j¡ tƒ  ¡  d S r   )r3   Úget_event_loopÚ	orig_loopÚnew_event_loopÚnew_loopÚset_event_loopr-   r<   r'   r1   r   r   r<   M  s   

zGetNewIOLoopTest.setUpc                    s6   t ƒ  ¡  |  t ¡  ¡ ¡ t | j¡ | j 	¡  d S r   )
r-   r.   rˆ   r3   r³   Ú	is_closedr·   r´   r¶   Úcloser'   r1   r   r   r.   U  s   
zGetNewIOLoopTest.tearDownc                 C   s   |   | jj| j¡ d S r   )ÚassertIsr   Úasyncio_loopr¶   r'   r   r   r   Ú	test_loop\  s   zGetNewIOLoopTest.test_loop)r)   r*   r+   r²   r<   r.   r¼   r:   r   r   r1   r   r¯   H  s
    r¯   Ú__main__)#Útornador   r   Útornado.httpserverr   Útornado.locksr   Útornado.testingr   r   r   r	   Útornado.webr   r3   Ú
contextlibr/   r   rt   rš   r[   ri   Úcontextmanagerr   r   r,   r;   r€   rR   rv   rƒ   rŠ   r¯   r)   Úmainr   r   r   r   Ú<module>   s4    
! 8jÿ