o
    Qœ_R  ã                   @   s¶   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 d dlm	Z	 d dl
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 G d	d
„ d
eƒZeG dd„ dejƒƒZdS )é    N)Úutf8Ú
to_unicode)Úgen)ÚIOStream)Úapp_log)Ú	TCPServer)ÚskipIfNonUnix)ÚAsyncTestCaseÚ	ExpectLogÚbind_unused_portÚgen_testc                   @   s8   e Zd Zedd„ ƒZedd„ ƒZdd„ Zedd„ ƒZd	S )
ÚTCPServerTestc                 c   sè   � G dd„ dt ƒ}d  }}zTtƒ \}}|ƒ }| |¡ tt ¡ ƒ}ttdƒ� | d|f¡V  | d¡V  | 	¡ V  t
jV  W d   ƒ n1 sHw   Y  W |d urV| ¡  |d ur`| ¡  d S d S |d urj| ¡  |d urs| ¡  w w )Nc                   @   s   e Zd Zejdd„ ƒZdS )zFTCPServerTest.test_handle_stream_coroutine_logging.<locals>.TestServerc                 s   s&   � |  tdƒ¡V  | ¡  dd  d S )Nó   helloé   r   )Ú
read_bytesÚlenÚclose©ÚselfÚstreamÚaddress© r   ú=/usr/lib/python3/dist-packages/tornado/test/tcpserver_test.pyÚhandle_stream   s   €zTTCPServerTest.test_handle_stream_coroutine_logging.<locals>.TestServer.handle_streamN©Ú__name__Ú
__module__Ú__qualname__r   Ú	coroutiner   r   r   r   r   Ú
TestServer   s    r   zException in callbackÚ	localhostr   )r   r   Ú
add_socketr   Úsocketr
   r   ÚconnectÚwriteÚread_until_closer   ÚmomentÚstopr   )r   r   ÚserverÚclientÚsockÚportr   r   r   Ú$test_handle_stream_coroutine_logging   s2   €



ü€ÿþ
ÿz2TCPServerTest.test_handle_stream_coroutine_loggingc                 c   sr   � G dd„ dt ƒ}tƒ \}}|ƒ }| |¡ tt ¡ ƒ}| d|f¡V  | ¡ V }|  |d¡ | ¡  | 	¡  d S )Nc                   @   s   e Zd Zdd„ ZdS )zETCPServerTest.test_handle_stream_native_coroutine.<locals>.TestServerc                 Ó   s   �|  d¡ | ¡  d S )Nó   data)r$   r   r   r   r   r   r   2   s   €
zSTCPServerTest.test_handle_stream_native_coroutine.<locals>.TestServer.handle_streamN)r   r   r   r   r   r   r   r   r   1   s    r   r    r-   )
r   r   r!   r   r"   r#   r%   ÚassertEqualr'   r   )r   r   r*   r+   r(   r)   Úresultr   r   r   Ú#test_handle_stream_native_coroutine-   s   €


z1TCPServerTest.test_handle_stream_native_coroutinec                 C   s.   t ƒ \}}tƒ }| |¡ | ¡  | ¡  d S ©N)r   r   r!   r'   )r   r*   r+   r(   r   r   r   Útest_stop_twice@   s
   

zTCPServerTest.test_stop_twicec              	   #   sÐ   � G ‡fdd„dt ƒ}tƒ \}}|ƒ ‰ˆ |¡ d|f‰d}dd„ t|ƒD ƒ}g ‰tj‡‡fdd„ƒ‰ ‡ fd	d„|D ƒV  |  tˆƒd
d¡ ztˆƒ|krR|  d¡ W ˆD ]}| 	¡  qUd S ˆD ]}| 	¡  q`w )Nc                       s   e Zd Zej‡ fdd„ƒZdS )z7TCPServerTest.test_stop_in_callback.<locals>.TestServerc                 3   s   � ˆ   ¡  | ¡ V  d S r1   )r'   r%   r   ©r(   r   r   r   N   s   €zETCPServerTest.test_stop_in_callback.<locals>.TestServer.handle_streamNr   r   r3   r   r   r   M   s    r   r    é(   c                 S   s   g | ]}t t ¡ ƒ‘qS r   )r   r"   )Ú.0Úir   r   r   Ú
<listcomp>X   s    z7TCPServerTest.test_stop_in_callback.<locals>.<listcomp>c                 3   s6   � z|   ˆ¡V  W n
 ty   Y d S w ˆ  | ¡ d S r1   )r#   ÚEnvironmentErrorÚappend)Úc)Úconnected_clientsÚserver_addrr   r   r#   [   s   €ÿz4TCPServerTest.test_stop_in_callback.<locals>.connectc                    s   g | ]}ˆ |ƒ‘qS r   r   )r5   r:   )r#   r   r   r7   d   s    r   zall clients failed connectingzHat least one client should fail connecting for the test to be meaningful)
r   r   r!   Úranger   r   ÚassertGreaterr   ÚskipTestr   )r   r   r*   r+   ÚNÚclientsr:   r   )r#   r;   r(   r<   r   Útest_stop_in_callbackG   s0   €

ÿ€
ÿ
ÿz#TCPServerTest.test_stop_in_callbackN)r   r   r   r   r,   r0   r2   rB   r   r   r   r   r      s    

r   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚTestMultiprocessc                 C   sp   t jtjt jt jd�}|j t|ƒ¡ |j ¡  | 	¡  |j
 ¡ }|j
 ¡  |jdkr4td|j|f ƒ‚t|ƒS )N)ÚstdinÚstdoutr   zProcess returned %d. stdout=%r)Ú
subprocessÚPopenÚsysÚ
executableÚPIPErD   r$   r   r   ÚwaitrE   ÚreadÚ
returncodeÚRuntimeErrorr   )r   ÚcodeÚprocrE   r   r   r   Úrun_subproc}   s   ÿ



ÿzTestMultiprocess.run_subprocc                 C   ó.   t  d¡}|  |¡}|  d t|ƒ¡d¡ d S )Na  
            from tornado.ioloop import IOLoop
            from tornado.tcpserver import TCPServer

            server = TCPServer()
            server.listen(0, address='127.0.0.1')
            IOLoop.current().run_sync(lambda: None)
            print('012', end='')
        Ú Ú012©ÚtextwrapÚdedentrQ   r.   ÚjoinÚsorted©r   rO   Úoutr   r   r   Útest_singleŒ   s
   ÿ
zTestMultiprocess.test_singlec                 C   rR   )Nab  
            from tornado.ioloop import IOLoop
            from tornado.process import task_id
            from tornado.tcpserver import TCPServer

            server = TCPServer()
            server.bind(0, address='127.0.0.1')
            server.start(3)
            IOLoop.current().run_sync(lambda: None)
            print(task_id(), end='')
        rS   rT   rU   rZ   r   r   r   Útest_simple�   s
   ÿ
zTestMultiprocess.test_simplec                 C   rR   )Na
  
            from tornado.ioloop import IOLoop
            from tornado.netutil import bind_sockets
            from tornado.process import fork_processes, task_id
            from tornado.ioloop import IOLoop
            from tornado.tcpserver import TCPServer

            sockets = bind_sockets(0, address='127.0.0.1')
            fork_processes(3)
            server = TCPServer()
            server.add_sockets(sockets)
            IOLoop.current().run_sync(lambda: None)
            print(task_id(), end='')
        rS   rT   rU   rZ   r   r   r   Útest_advanced®   s
   ÿ
zTestMultiprocess.test_advancedN)r   r   r   rQ   r\   r]   r^   r   r   r   r   rC   v   s
    rC   )r"   rF   rH   rV   ÚunittestÚtornado.escaper   r   Útornador   Útornado.iostreamr   Útornado.logr   Útornado.tcpserverr   Útornado.test.utilr   Útornado.testingr	   r
   r   r   r   ÚTestCaserC   r   r   r   r   Ú<module>   s    f