o
    ~îj  ã                   @   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 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 d dlmZ G d	d
„ d
eƒZeG dd„ dejƒƒZdS )é    N)Úgen)ÚIOStream)Úapp_log)Ú	TCPServer)ÚskipIfNonUnix)ÚAsyncTestCaseÚ	ExpectLogÚbind_unused_portÚgen_test)ÚTuplec                   @   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   úe/raid/bulk_storage/M2HATS/real-image-venv/lib/python3.10/site-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   3   s   €
zSTCPServerTest.test_handle_stream_native_coroutine.<locals>.TestServer.handle_streamN)r   r   r   r   r   r   r   r   r   2   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_twiceA   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 r0   )r&   r$   r   ©r'   r   r   r   O   s   €zETCPServerTest.test_stop_in_callback.<locals>.TestServer.handle_streamNr   r   r2   r   r   r   N   s    r   r   é(   c                 S   s   g | ]}t t ¡ ƒ‘qS r   )r   r!   )Ú.0Úir   r   r   Ú
<listcomp>Y   s    z7TCPServerTest.test_stop_in_callback.<locals>.<listcomp>c                 3   s6   � z|   ˆ¡V  W n
 ty   Y d S w ˆ  | ¡ d S r0   )r"   ÚOSErrorÚappend)Úc)Úconnected_clientsÚserver_addrr   r   r"   \   s   €ÿz4TCPServerTest.test_stop_in_callback.<locals>.connectc                    s   g | ]}ˆ |ƒ‘qS r   r   )r4   r9   )r"   r   r   r6   e   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Úclientsr9   r   )r"   r:   r'   r;   r   Útest_stop_in_callbackH   s0   €

ÿ€
ÿ
ÿz#TCPServerTest.test_stop_in_callbackN)r   r   r   r
   r+   r/   r1   rA   r   r   r   r   r      s    

r   c                   @   sF   e Zd Zdedeeef fdd„Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dS )ÚTestMultiprocessÚcodeÚreturnc              
   C   sh   zt jtjdgd|ddd�}W n t jy- } ztd|j› d|j› d|j› �ƒ|‚d }~ww |j|jfS )Nz-Werror::DeprecationWarningTÚutf8)Úcapture_outputÚinputÚencodingÚcheckzProcess returned z stdout=z stderr=)	Ú
subprocessÚrunÚsysÚ
executableÚCalledProcessErrorÚRuntimeErrorÚ
returncodeÚstdoutÚstderr)r   rC   r.   Úer   r   r   Úrun_subproc~   s"   
ûÿþ€ÿzTestMultiprocess.run_subprocc                 C   ó>   t  d¡}|  |¡\}}|  d t|ƒ¡d¡ |  |d¡ d S )Na  
            import asyncio
            from tornado.tcpserver import TCPServer

            async def main():
                server = TCPServer()
                server.listen(0, address='127.0.0.1')

            asyncio.run(main())
            print('012', end='')
        Ú Ú012©ÚtextwrapÚdedentrT   r-   ÚjoinÚsorted©r   rC   ÚoutÚerrr   r   r   Útest_listen_single�   s   ÿz#TestMultiprocess.test_listen_singlec                 C   rU   )NaÀ  
            import warnings

            from tornado.ioloop import IOLoop
            from tornado.process import task_id
            from tornado.tcpserver import TCPServer

            warnings.simplefilter("ignore", DeprecationWarning)

            server = TCPServer()
            server.bind(0, address='127.0.0.1')
            server.start(3)
            IOLoop.current().run_sync(lambda: None)
            print(task_id(), end='')
        rV   rW   rX   r]   r   r   r   Útest_bind_start¡   ó   ÿz TestMultiprocess.test_bind_startc                 C   rU   )Na  
            import asyncio
            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)
            async def post_fork_main():
                server = TCPServer()
                server.add_sockets(sockets)
            asyncio.run(post_fork_main())
            print(task_id(), end='')
        rV   rW   rX   r]   r   r   r   Útest_add_sockets·   rb   z!TestMultiprocess.test_add_socketsc                 C   rU   )NaÍ  
            import asyncio
            import socket
            from tornado.netutil import bind_sockets
            from tornado.process import task_id, fork_processes
            from tornado.tcpserver import TCPServer

            # Pick an unused port which we will be able to bind to multiple times.
            (sock,) = bind_sockets(0, address='127.0.0.1',
                family=socket.AF_INET, reuse_port=True)
            port = sock.getsockname()[1]

            fork_processes(3)

            async def main():
                server = TCPServer()
                server.listen(port, address='127.0.0.1', reuse_port=True)
            asyncio.run(main())
            print(task_id(), end='')
            rV   rW   rX   r]   r   r   r   Útest_listen_multi_reuse_portÍ   s   ÿz-TestMultiprocess.test_listen_multi_reuse_portN)
r   r   r   Ústrr   rT   r`   ra   rc   rd   r   r   r   r   rB   w   s    rB   )r!   rJ   rL   rY   ÚunittestÚtornador   Útornado.iostreamr   Útornado.logr   Útornado.tcpserverr   Útornado.test.utilr   Útornado.testingr   r   r	   r
   Útypingr   r   ÚTestCaserB   r   r   r   r   Ú<module>   s    f