U
    lufÏA  ã                   @   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mZ d dlm	Z	m
Z
 d dlmZ d dlmZmZ d dlmZ d dlmZmZ d d	lmZmZmZ d d
lmZ d dlZejrÎd dlmZ d dlmZm Z m!Z! d\Z"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ƒZ(dS )é    )ÚclosingN©ÚFuture)Úbind_socketsÚResolver)ÚQueue)Ú	TCPClientÚ
_Connector)Ú	TCPServer)ÚAsyncTestCaseÚgen_test)ÚskipIfNoIPv6Úrefusing_portÚskipIfNonUnix)ÚTimeoutError)ÚIOStream)ÚListÚDictÚTuple)é   é   c                       s0   e Zd Z‡ fdd„Zdd„ Z‡ fdd„Z‡  ZS )ÚTestTCPServerc                    sD   t ƒ  ¡  g | _tƒ | _tdd|ƒ}|  |¡ |d  ¡ d | _d S )Nr   Ú	localhostr   )	ÚsuperÚ__init__Ústreamsr   Úqueuer   Zadd_socketsÚgetsocknameÚport)ÚselfÚfamilyZsockets©Ú	__class__© ú?/tmp/pip-unpacked-wheel-bmg6zs32/tornado/test/tcpclient_test.pyr   *   s    

zTestTCPServer.__init__c                 C   s   | j  |¡ | j |¡ d S ©N)r   Úappendr   Úput)r   ÚstreamÚaddressr#   r#   r$   Úhandle_stream2   s    zTestTCPServer.handle_streamc                    s"   t ƒ  ¡  | jD ]}| ¡  qd S r%   )r   Ústopr   Úclose©r   r(   r!   r#   r$   r+   6   s    

zTestTCPServer.stop)Ú__name__Ú
__module__Ú__qualname__r   r*   r+   Ú__classcell__r#   r#   r!   r$   r   )   s   r   c                       sÆ   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡ fdd„Zd	d
„ Zed&dd„ƒZ	dd„ Z
dd„ Zedd„ ƒZedd„ ƒZdd„ Zedd„ ƒZdd„ Zedd„ ƒZdd„ Zd d!„ Zed"d#„ ƒZed$d%„ ƒZ‡  ZS )'ÚTCPClientTestc                    s   t ƒ  ¡  d | _tƒ | _d S r%   )r   ÚsetUpÚserverr   Úclient©r   r!   r#   r$   r3   =   s    
zTCPClientTest.setUpc                 C   s0   |t jkrdtjkr|  d¡ t|ƒ| _| jjS )NZTRAVISz6dual-stack servers often have port conflicts on travis)ÚsocketÚ	AF_UNSPECÚosÚenvironÚskipTestr   r4   r   )r   r    r#   r#   r$   Ústart_serverB   s    

zTCPClientTest.start_serverc                 C   s   | j d k	r| j  ¡  d | _ d S r%   )r4   r+   r6   r#   r#   r$   Ústop_serverH   s    

zTCPClientTest.stop_serverc                    s    | j  ¡  |  ¡  tƒ  ¡  d S r%   )r5   r,   r=   r   ÚtearDownr6   r!   r#   r$   r>   M   s    
zTCPClientTest.tearDownc                 C   s:   | j  dd„ ¡}tdd„ |D ƒƒ}tj|kr6|  d¡ d S )Nc                   S   s   t ƒ  dd¡S )Nr   éP   )r   Úresolver#   r#   r#   r$   Ú<lambda>U   ó    z1TCPClientTest.skipIfLocalhostV4.<locals>.<lambda>c                 s   s   | ]}|d  V  qdS )r   Nr#   )Ú.0Úaddrr#   r#   r$   Ú	<genexpr>V   s     z2TCPClientTest.skipIfLocalhostV4.<locals>.<genexpr>z"localhost does not resolve to ipv6)Úio_loopZrun_syncÚsetr7   ÚAF_INET6r;   )r   ÚaddrinfoZfamiliesr#   r#   r$   ÚskipIfLocalhostV4R   s    
zTCPClientTest.skipIfLocalhostV4Nc           	   	   c   sx   |   |¡}| jj|||||dV }| jd k	s0t‚| jj ¡ V }t|ƒ( | d¡ | 	d¡V }|  
|d¡ W 5 Q R X d S )N)Ú	source_ipÚsource_portÚafs   helloé   )r<   r5   Úconnectr4   ÚAssertionErrorr   Úgetr   ÚwriteÚ
read_bytesÚassertEqual)	r   r    ÚhostrK   rL   r   r(   Zserver_streamÚdatar#   r#   r$   Údo_test_connectZ   s    
û

zTCPClientTest.do_test_connectc                 C   s   |   tjd¡ d S ©Nú	127.0.0.1©rW   r7   ÚAF_INETr6   r#   r#   r$   Útest_connect_ipv4_ipv4k   s    z$TCPClientTest.test_connect_ipv4_ipv4c                 C   s   |   tjd¡ d S ©Nr   rZ   r6   r#   r#   r$   Útest_connect_ipv4_dualn   s    z$TCPClientTest.test_connect_ipv4_dualc                 C   s   |   ¡  |  tjd¡ d S ©Nz::1)rJ   rW   r7   rH   r6   r#   r#   r$   Útest_connect_ipv6_ipv6q   s    z$TCPClientTest.test_connect_ipv6_ipv6c                 C   s4   |   ¡  t ¡ j d¡r"|  d¡ |  tjd¡ d S )NZTwistedResolverz3TwistedResolver does not support multiple addressesr   )	rJ   r   Zconfigured_classr.   Úendswithr;   rW   r7   rH   r6   r#   r#   r$   Útest_connect_ipv6_dualv   s    
z$TCPClientTest.test_connect_ipv6_dualc                 C   s   |   tjd¡ d S rX   ©rW   r7   r8   r6   r#   r#   r$   Útest_connect_unspec_ipv4}   s    z&TCPClientTest.test_connect_unspec_ipv4c                 C   s   |   ¡  |  tjd¡ d S r_   )rJ   rW   r7   r8   r6   r#   r#   r$   Útest_connect_unspec_ipv6€   s    z&TCPClientTest.test_connect_unspec_ipv6c                 C   s   |   tjd¡ d S r]   rc   r6   r#   r#   r$   Útest_connect_unspec_dual…   s    z&TCPClientTest.test_connect_unspec_dualc              	   c   s>   t ƒ \}}|  |¡ |  t¡ | j d|¡V  W 5 Q R X d S rX   )r   Z
addCleanupÚassertRaisesÚIOErrorr5   rO   )r   Zcleanup_funcr   r#   r#   r$   Útest_refused_ipv4ˆ   s    

zTCPClientTest.test_refused_ipv4c                 C   s   | j tj| jtjddd dS )z8Fail when trying to use the source IP Address '8.8.8.8'.rY   z8.8.8.8©rK   N)rg   r7   ÚerrorrW   r[   r6   r#   r#   r$   Útest_source_ip_fail   s    ûz!TCPClientTest.test_source_ip_failc                 C   s   | j tjddd dS )z=Success when trying to use the source IP Address '127.0.0.1'.rY   rj   NrZ   r6   r#   r#   r$   Útest_source_ip_success™   s    z$TCPClientTest.test_source_ip_successc                 C   s4   t  ¡ dkr|  d¡ | jtj| jtjddd dS )z&Fail when trying to use source port 1.Úrootzrunning as rootrY   r   )rL   N)ÚgetpassÚgetuserr;   rg   r7   rk   rW   r[   r6   r#   r#   r$   Útest_source_port_fail   s    
ûz#TCPClientTest.test_source_port_failc              	   c   sH   d}G dd„ dt ƒ}|  t¡  t|ƒ djdd|dV  W 5 Q R X d S )Ngš™™™™™©?c                   @   s   e Zd Zdd„ ZdS )z;TCPClientTest.test_connect_timeout.<locals>.TimeoutResolverc                 _   s   t ƒ S r%   r   )r   ÚargsÚkwargsr#   r#   r$   r@   ±   s    zCTCPClientTest.test_connect_timeout.<locals>.TimeoutResolver.resolveN)r.   r/   r0   r@   r#   r#   r#   r$   ÚTimeoutResolver°   s   rt   )Úresolverz1.2.3.4i90  )Útimeout)r   rg   r   r   rO   )r   rv   rt   r#   r#   r$   Útest_connect_timeout¬   s      ÿz"TCPClientTest.test_connect_timeout)NN)r.   r/   r0   r3   r<   r=   r>   rJ   r   rW   r\   r^   r   r`   rb   rd   re   rf   ri   rl   rm   r   rq   rw   r1   r#   r#   r!   r$   r2   <   s2   





r2   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚTestConnectorSplitc                 C   sB   t  tdftdfg¡\}}|  |tdftdfg¡ |  |g ¡ d S )NÚaÚb)r	   ÚsplitÚAF1rT   ©r   ZprimaryZ	secondaryr#   r#   r$   Útest_one_family»   s    z"TestConnectorSplit.test_one_familyc                 C   sZ   t  tdftdftdftdfg¡\}}|  |tdftdfg¡ |  |tdftdfg¡ d S ©Nry   rz   ÚcÚd)r	   r{   r|   ÚAF2rT   r}   r#   r#   r$   Ú
test_mixedÁ   s
    ÿzTestConnectorSplit.test_mixedN)r.   r/   r0   r~   rƒ   r#   r#   r#   r$   rx   º   s   rx   c                       sà   e Zd ZG dd„ deƒZ‡ fdd„Z‡ f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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d)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Z‡  ZS )1ÚConnectorTestc                   @   s   e Zd Zdd„ Zdd„ ZdS )zConnectorTest.FakeStreamc                 C   s
   d| _ d S )NF©Úclosedr6   r#   r#   r$   r   Ë   s    z!ConnectorTest.FakeStream.__init__c                 C   s
   d| _ d S )NTr…   r6   r#   r#   r$   r,   Î   s    zConnectorTest.FakeStream.closeN)r.   r/   r0   r   r,   r#   r#   r#   r$   Ú
FakeStreamÊ   s   r‡   c                    s8   t ƒ  ¡  i | _i | _tdftdftdftdfg| _d S r   )r   r3   Úconnect_futuresr   r|   r‚   rI   r6   r!   r#   r$   r3   Ñ   s
    
ÿzConnectorTest.setUpc                    s*   | j  ¡ D ]}|  |j¡ q
tƒ  ¡  d S r%   )r   ÚvaluesÚassertFalser†   r   r>   r-   r!   r#   r$   r>   Ù   s    zConnectorTest.tearDownc                 C   s.   t  ¡ }|| j|< tƒ }|| j||f< ||fS r%   )r„   r‡   r   r   rˆ   )r   rM   rD   r(   Úfuturer#   r#   r$   Úcreate_streamà   s
    
zConnectorTest.create_streamc                 G   s   |   t| j ¡ ƒt|ƒ¡ d S r%   )rT   Úsortedrˆ   Úkeys)r   rŽ   r#   r#   r$   Úassert_pendingç   s    zConnectorTest.assert_pendingc                 C   sX   | j  ||f¡}|r&| | j| ¡ n| j |¡ | tƒ ¡ | j | j¡ |  	¡  d S r%   )
rˆ   ÚpopZ
set_resultr   Zset_exceptionrh   rF   Zadd_callbackr+   Úwait)r   rM   rD   Úsuccessr‹   r#   r#   r$   Úresolve_connectê   s    zConnectorTest.resolve_connectc                 C   s   |j D ]}|  |j¡ qd S r%   )r   Ú
assertTruer†   )r   Úconnr(   r#   r#   r$   Úassert_connector_streams_closedõ   s    
z-ConnectorTest.assert_connector_streams_closedc                 C   s,   t || jƒ}|jd| j ¡ d d}||fS )Ni  )Úconnect_timeout)r	   rŒ   ÚstartrF   Útime)r   rI   r•   r‹   r#   r#   r$   Ústart_connectù   s    zConnectorTest.start_connectc                 C   sZ   |   | j¡\}}|  t| j ¡ ƒtdfg¡ |  tdd¡ |  | ¡ td| j	d f¡ d S )Nry   T)
rš   rI   rT   Úlistrˆ   rŽ   r|   r“   Úresultr   ©r   r•   r‹   r#   r#   r$   Útest_immediate_successÿ   s    z$ConnectorTest.test_immediate_successc                 C   sB   |   tdfg¡\}}|  tdf¡ |  tdd¡ |  t|j¡ d S )Nry   F©rš   r|   r   r“   rg   rh   rœ   r   r#   r#   r$   Útest_immediate_failure  s    z$ConnectorTest.test_immediate_failurec                 C   sr   |   tdftdfg¡\}}|  tdf¡ |  tdd¡ |  tdf¡ |  tdd¡ |  | ¡ td| jd f¡ d S ©Nry   rz   FT)rš   r|   r   r“   rT   rœ   r   r   r#   r#   r$   Útest_one_family_second_try  s    z(ConnectorTest.test_one_family_second_tryc                 C   sd   |   tdftdfg¡\}}|  tdf¡ |  tdd¡ |  tdf¡ |  tdd¡ |  t|j¡ d S )Nry   rz   FrŸ   r   r#   r#   r$   Ú"test_one_family_second_try_failure  s    z0ConnectorTest.test_one_family_second_try_failurec                 C   sˆ   |   tdftdfg¡\}}|  tdf¡ | ¡  |  tdf¡ |  tdd¡ |  tdf¡ |  tdd¡ |  | ¡ td| jd f¡ d S r¡   )rš   r|   r   Ú
on_timeoutr“   rT   rœ   r   r   r#   r#   r$   Ú"test_one_family_second_try_timeout  s    z0ConnectorTest.test_one_family_second_try_timeoutc                 C   s|   |   | j¡\}}|  tdf¡ |  tdd¡ |  tdftdf¡ |  tdd¡ |  tdd¡ |  | ¡ td| jd f¡ d S )Nry   Frz   r€   T)	rš   rI   r   r|   r“   r‚   rT   rœ   r   r   r#   r#   r$   Ú#test_two_families_immediate_failure(  s    z1ConnectorTest.test_two_families_immediate_failurec                 C   s~   |   | j¡\}}|  tdf¡ | ¡  |  tdftdf¡ |  tdd¡ |  | ¡ td| j	d f¡ |  tdd¡ |  ¡  d S )Nry   r€   TF)
rš   rI   r   r|   r¤   r‚   r“   rT   rœ   r   r   r#   r#   r$   Útest_two_families_timeout1  s    z'ConnectorTest.test_two_families_timeoutc                 C   sŠ   |   | j¡\}}|  tdf¡ | ¡  |  tdftdf¡ |  tdd¡ |  | ¡ td| j	d f¡ |  tdd¡ |  
| j	 d¡j¡ d S )Nry   r€   T)rš   rI   r   r|   r¤   r‚   r“   rT   rœ   r   r”   r   r†   r   r#   r#   r$   Útest_success_after_timeout<  s    z(ConnectorTest.test_success_after_timeoutc                 C   sÂ   |   | j¡\}}|  tdf¡ | ¡  |  tdftdf¡ |  tdd¡ |  tdftdf¡ |  tdd¡ |  tdf¡ |  tdd¡ |  tdf¡ |  | ¡ ¡ |  tdd¡ |  	t
|j¡ d S )Nry   r€   Fr   rz   )rš   rI   r   r|   r¤   r‚   r“   rŠ   Údonerg   rh   rœ   r   r#   r#   r$   Útest_all_failG  s    zConnectorTest.test_all_failc                 C   s’   |   tdftdfg¡\}}|  tdf¡ | ¡  | j tdf¡ |  | j d¡j¡ | 	¡  |  ¡  |  
t|jƒd¡ |  |¡ |  t|j¡ d S )Nry   rz   r   )rš   r|   r   Úon_connect_timeoutrˆ   r   r”   r   r†   r¤   rT   Úlenr–   rg   r   rœ   r   r#   r#   r$   Ú-test_one_family_timeout_after_connect_timeoutW  s    
z;ConnectorTest.test_one_family_timeout_after_connect_timeoutc                 C   s–   |   tdftdfg¡\}}|  tdf¡ |  tdd¡ | ¡  |  ¡  |  | jd jd¡ |  t|jƒd¡ |  	|¡ |  | 
¡ td| jd f¡ d S )Nry   rz   TFr   )rš   r|   r   r“   r«   rT   r   r†   r¬   r–   rœ   r   r#   r#   r$   Ú.test_one_family_success_before_connect_timeoutg  s    
z<ConnectorTest.test_one_family_success_before_connect_timeoutc                 C   s¦   |   tdftdfg¡\}}|  tdf¡ |  tdd¡ |  tdf¡ | ¡  | j tdf¡ |  | j d¡j	¡ |  ¡  |  
t|jƒd¡ |  |¡ |  t|j¡ d S ©Nry   rz   Fr   )rš   r|   r   r“   r«   rˆ   r   r”   r   r†   rT   r¬   r–   rg   r   rœ   r   r#   r#   r$   Ú0test_one_family_second_try_after_connect_timeoutt  s    
z>ConnectorTest.test_one_family_second_try_after_connect_timeoutc                 C   s   |   tdftdfg¡\}}|  tdf¡ |  tdd¡ |  tdf¡ |  tdd¡ | ¡  |  ¡  |  t|jƒd¡ |  |¡ |  	t
|j¡ d S r¯   )rš   r|   r   r“   r«   rT   r¬   r   r–   rg   rh   rœ   r   r#   r#   r$   Ú9test_one_family_second_try_failure_before_connect_timeout  s    
zGConnectorTest.test_one_family_second_try_failure_before_connect_timeoutc                 C   sÀ   |   | j¡\}}|  tdf¡ | ¡  |  tdftdf¡ | ¡  | j tdf¡ |  	| j
 d¡j¡ | j tdf¡ |  	| j
 d¡j¡ |  ¡  |  t|j
ƒd¡ |  |¡ |  t|j¡ d S )Nry   r€   r   )rš   rI   r   r|   r¤   r‚   r«   rˆ   r   r”   r   r†   rT   r¬   r–   rg   r   rœ   r   r#   r#   r$   Ú.test_two_family_timeout_before_connect_timeout  s    
z<ConnectorTest.test_two_family_timeout_before_connect_timeoutc                 C   s°   |   | j¡\}}|  tdf¡ | ¡  |  tdftdf¡ |  tdd¡ | j tdf¡ |  	| j
 d¡j¡ |  ¡  |  t|j
ƒd¡ |  |¡ |  | ¡ td| j
d f¡ d S )Nry   r€   Tr   )rš   rI   r   r|   r¤   r‚   r“   rˆ   r   r”   r   r†   rT   r¬   r–   rœ   r   r#   r#   r$   Ú%test_two_family_success_after_timeoutœ  s    
z3ConnectorTest.test_two_family_success_after_timeoutc                 C   s   |   | j¡\}}|  tdf¡ | ¡  | j tdf¡ |  | j d¡j	¡ |  ¡  | 
¡  |  ¡  |  t|jƒd¡ |  |¡ |  t|j¡ d S )Nry   r   )rš   rI   r   r|   r«   rˆ   r   r”   r   r†   r¤   rT   r¬   r–   rg   r   rœ   r   r#   r#   r$   Ú-test_two_family_timeout_after_connect_timeoutª  s    
z;ConnectorTest.test_two_family_timeout_after_connect_timeout)r.   r/   r0   Úobjectr‡   r3   r>   rŒ   r   r“   r–   rš   rž   r    r¢   r£   r¥   r¦   r§   r¨   rª   r­   r®   r°   r±   r²   r³   r´   r1   r#   r#   r!   r$   r„   É   s0   	r„   ))Ú
contextlibr   ro   r9   r7   ZunittestZtornado.concurrentr   Ztornado.netutilr   r   Ztornado.queuesr   Ztornado.tcpclientr   r	   Ztornado.tcpserverr
   Ztornado.testingr   r   Ztornado.test.utilr   r   r   Ztornado.genr   ÚtypingÚTYPE_CHECKINGZtornado.iostreamr   r   r   r   r|   r‚   r   r2   ZTestCaserx   r„   r#   r#   r#   r$   Ú<module>   s*   ~