U
    luf¡  ã                   @   s(  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mZm	Z	m
Z
 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 G d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deeƒZ G dd„ deƒZ!e"dkr$e #¡  dS )é    )ÚfuturesN)ÚFutureÚrun_on_executorÚ"future_set_result_unless_cancelled)Úutf8Ú
to_unicode)Úgen)ÚIOStream)Ú	TCPServer)ÚAsyncTestCaseÚbind_unused_portÚgen_testc                   @   s   e Zd Zdd„ ZdS )ÚMiscFutureTestc                 C   sv   t ƒ }t|dƒ |  | ¡ d¡ |  | ¡ ¡ t ƒ }| ¡  | ¡ }t|dƒ |  | ¡ |¡ |sr|  | ¡ d¡ d S ©Né*   )r   r   ÚassertEqualÚresultZassertFalseZ	cancelledÚcancel)ÚselfZfutZis_cancelled© r   ú@/tmp/pip-unpacked-wheel-bmg6zs32/tornado/test/concurrent_test.pyÚ'test_future_set_result_unless_cancelled#   s    

z6MiscFutureTest.test_future_set_result_unless_cancelledN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   "   s   r   c                   @   s   e Zd Zejdd„ ƒZdS )Ú	CapServerc                 c   sN   |  d¡V }t|ƒ}|| ¡ kr,| d¡ n| td| ¡  ƒ¡ | ¡  d S )Nó   
s   error	already capitalized
zok	%s)Ú
read_untilr   ÚupperÚwriter   Úclose)r   ÚstreamÚaddressÚdatar   r   r   Úhandle_stream7   s    zCapServer.handle_streamN)r   r   r   r   Ú	coroutiner$   r   r   r   r   r   6   s   r   c                   @   s   e Zd ZdS )ÚCapErrorN)r   r   r   r   r   r   r   r&   C   s   r&   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚBaseCapClientc                 C   s
   || _ d S ©N©Úport)r   r*   r   r   r   Ú__init__H   s    zBaseCapClient.__init__c                 C   sD   t  dt|ƒ¡}|d kr tdƒ‚| ¡ \}}|dkr8|S t|ƒ‚d S )Nz
(.*)	(.*)
zdid not matchÚok)ÚreÚmatchr   Ú	ExceptionÚgroupsr&   )r   r#   ÚmÚstatusÚmessager   r   r   Úprocess_responseK   s    zBaseCapClient.process_responseN)r   r   r   r+   r4   r   r   r   r   r'   G   s   r'   c                   @   s   e Zd Zejdd„ ƒZdS )ÚGeneratorCapClientc                 c   s€   t  d¡ tt ¡ ƒ}t  d¡ | d| jf¡V  | t|d ƒ¡ t  d¡ | d¡V }t  d¡ | 	¡  t
 |  |¡¡‚d S )NÚ
capitalizeZ
connectingz	127.0.0.1Ú
Úreadingr   Z	returning)ÚloggingÚdebugr	   ÚsocketÚconnectr*   r   r   r   r    r   ZReturnr4   )r   Zrequest_datar!   r#   r   r   r   r6   W   s    



zGeneratorCapClient.capitalizeN)r   r   r   r   r%   r6   r   r   r   r   r5   V   s   r5   c                       sl   e Zd ZdZ‡ fdd„Z‡ fdd„Zejdœdd„Zejdœd	d
„Z	ejdœdd„Z
ejdœdd„Z‡  ZS )ÚClientTestMixinNc                    s<   t ƒ  ¡  tƒ | _tƒ \}}| j |g¡ | j|d| _d S )Nr)   )ÚsuperÚsetUpr   Úserverr   Zadd_socketsÚclient_classÚclient)r   Úsockr*   ©Ú	__class__r   r   r?   h   s
    

zClientTestMixin.setUpc                    s   | j  ¡  tƒ  ¡  d S r(   )r@   Ústopr>   ÚtearDown©r   rD   r   r   rG   o   s    
zClientTestMixin.tearDownrH   c                 C   s8   | j  d¡}| j || j¡ |  ¡  |  | ¡ d¡ d S ©NZhelloÚHELLO)rB   r6   Úio_loopÚ
add_futurerF   Úwaitr   r   ©r   Úfuturer   r   r   Útest_futures   s    zClientTestMixin.test_futurec                 C   s8   | j  d¡}| j || j¡ |  ¡  |  td|j¡ d S )NrJ   úalready capitalized)	rB   r6   rK   rL   rF   rM   ÚassertRaisesRegexr&   r   rN   r   r   r   Útest_future_errory   s    z!ClientTestMixin.test_future_errorc                    s"   t j‡ fdd„ƒ}ˆ j |¡ d S )Nc                  3   s   ˆ j  d¡V } ˆ  | d¡ d S rI   )rB   r6   r   )r   rH   r   r   Úf€   s    z)ClientTestMixin.test_generator.<locals>.f©r   r%   rK   Zrun_sync©r   rT   r   rH   r   Útest_generator   s    zClientTestMixin.test_generatorc                    s"   t j‡ fdd„ƒ}ˆ j |¡ d S )Nc                	   3   s*   ˆ   td¡ ˆ j d¡V  W 5 Q R X d S )NrQ   rJ   )rR   r&   rB   r6   r   rH   r   r   rT   ˆ   s    z/ClientTestMixin.test_generator_error.<locals>.frU   rV   r   rH   r   Útest_generator_error‡   s    z$ClientTestMixin.test_generator_error)r   r   r   rA   r?   rG   ÚtypingÚAnyrP   rS   rW   rX   Ú__classcell__r   r   rD   r   r=   e   s   r=   c                   @   s   e Zd ZeZdS )ÚGeneratorClientTestN)r   r   r   r5   rA   r   r   r   r   r\      s   r\   c                   @   s<   e Zd Zedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZd	S )
ÚRunOnExecutorTestc                 c   s0   G dd„ dt ƒ}|ƒ }| ¡ V }|  |d¡ d S )Nc                   @   s    e Zd Zdd„ Zedd„ ƒZdS )z1RunOnExecutorTest.test_no_calling.<locals>.Objectc                 S   s   t j d¡| _d S ©Né   ©r   ÚthreadÚThreadPoolExecutorÚexecutorrH   r   r   r   r+   ˜   s    z:RunOnExecutorTest.test_no_calling.<locals>.Object.__init__c                 S   s   dS r   r   rH   r   r   r   rT   ›   s    z3RunOnExecutorTest.test_no_calling.<locals>.Object.fN©r   r   r   r+   r   rT   r   r   r   r   ÚObject—   s   re   r   ©ÚobjectrT   r   ©r   re   ÚoÚanswerr   r   r   Útest_no_calling•   s    
z!RunOnExecutorTest.test_no_callingc                 c   s0   G dd„ dt ƒ}|ƒ }| ¡ V }|  |d¡ d S )Nc                   @   s"   e Zd Zdd„ Zeƒ dd„ ƒZdS )z8RunOnExecutorTest.test_call_with_no_args.<locals>.Objectc                 S   s   t j d¡| _d S r^   r`   rH   r   r   r   r+   ¦   s    zARunOnExecutorTest.test_call_with_no_args.<locals>.Object.__init__c                 S   s   dS r   r   rH   r   r   r   rT   ©   s    z:RunOnExecutorTest.test_call_with_no_args.<locals>.Object.fNrd   r   r   r   r   re   ¥   s   re   r   rf   rh   r   r   r   Útest_call_with_no_args£   s    
z(RunOnExecutorTest.test_call_with_no_argsc                 c   s0   G dd„ dt ƒ}|ƒ }| ¡ V }|  |d¡ d S )Nc                   @   s&   e Zd Zdd„ Zedddd„ ƒZdS )z9RunOnExecutorTest.test_call_with_executor.<locals>.Objectc                 S   s   t j d¡| _d S r^   )r   ra   rb   Ú_Object__executorrH   r   r   r   r+   ´   s    zBRunOnExecutorTest.test_call_with_executor.<locals>.Object.__init__rm   )rc   c                 S   s   dS r   r   rH   r   r   r   rT   ·   s    z;RunOnExecutorTest.test_call_with_executor.<locals>.Object.fNrd   r   r   r   r   re   ³   s   re   r   rf   rh   r   r   r   Útest_call_with_executor±   s    
z)RunOnExecutorTest.test_call_with_executorc                 #   s:   G dd„ dt ƒ}|ƒ ‰ ‡ fdd„}|ƒ V }|  |d¡ d S )Nc                   @   s"   e Zd Zdd„ Zeƒ dd„ ƒZdS )z2RunOnExecutorTest.test_async_await.<locals>.Objectc                 S   s   t j d¡| _d S r^   r`   rH   r   r   r   r+   Â   s    z;RunOnExecutorTest.test_async_await.<locals>.Object.__init__c                 S   s   dS r   r   rH   r   r   r   rT   Å   s    z4RunOnExecutorTest.test_async_await.<locals>.Object.fNrd   r   r   r   r   re   Á   s   re   c                  “   s   ˆ   ¡ I d H } | S r(   )rT   )rj   ©ri   r   r   rT   Ë   s    z-RunOnExecutorTest.test_async_await.<locals>.fr   )rg   r   )r   re   rT   r   r   ro   r   Útest_async_await¿   s
    z"RunOnExecutorTest.test_async_awaitN)r   r   r   r   rk   rl   rn   rp   r   r   r   r   r]   ”   s   


r]   Ú__main__)$Z
concurrentr   r9   r-   r;   rY   ZunittestZtornado.concurrentr   r   r   Ztornado.escaper   r   Ztornador   Ztornado.iostreamr	   Ztornado.tcpserverr
   Ztornado.testingr   r   r   r   r   r/   r&   rg   r'   r5   r=   r\   r]   r   Úmainr   r   r   r   Ú<module>   s*   +?
