U
    
W[2  ã                
   @   sJ  d Z ddlZddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZ dd	lmZ zPdd
lmZmZmZ ddlmZmZmZ ddlmZ ddlmZ ddl m!Z! W n e"k
rÜ   dZ#Y nX zeeeƒ $¡  W n, e
k
r Z% ze&e%ƒZ'W 5 dZ%[%X Y nX dZ'G dd„ dej(ƒZ)G dd„ dej(ƒZ*dS )z,
Tests for L{twisted.internet.iocpreactor}.
é    N)Úarray)Úpack)ÚAF_INET6ÚAF_INETÚSOCK_STREAMÚ
SOL_SOCKETÚerrorÚsocket)ÚverifyClass)Úunittest)Úmsg)ÚIPushProducer)ÚiocpsupportÚtcpÚudp)ÚIOCPReactorÚEVENTS_PER_LOOPÚ
KEY_NORMAL)ÚIReadWriteHandle)ÚSO_UPDATE_ACCEPT_CONTEXT)Ú
FileHandlez%This test only applies to IOCPReactorc                   @   s6   e Zd ZdZdd„ Zdd„ Zdd„ Zedk	r2ee_dS )	ÚSupportTestszn
    Tests for L{twisted.internet.iocpreactor.iocpsupport}, low-level reactor
    implementation helpers.
    c              
   C   s>  t d|f ƒ t|tƒ}|  |j¡ | d¡ | d¡ t|tƒ}|  |j¡ | d¡ z| || 	¡ d f¡ W n8 t
k
rª } z|  |jtjtjf¡ W 5 d}~X Y nX t|tƒ}|  |j¡ tddƒ}|  dt | ¡ | ¡ |d¡¡ | tttd	| ¡ ƒ¡ |  || ¡ dd
… | 	¡ dd
… ft | ¡ |¡¡ dS )a  
        Create a C{SOCK_STREAM} connection to localhost using a socket with an
        address family of C{family} and assert that the result of
        L{iocpsupport.get_accept_addrs} is consistent with the result of
        C{socket.getsockname} and C{socket.getpeername}.
        zfamily = %r)Ú r   é   FNÚBs                                                                                                                                                                                                                                                                   r   ÚPé   )r   r	   r   Z
addCleanupÚcloseZbindZlistenZsetblockingZconnectZgetsocknamer   ZassertInÚerrnoZEINPROGRESSZEWOULDBLOCKr   ÚassertEqualÚ_iocpZacceptÚfilenoZ
setsockoptr   r   r   ZgetpeernameZget_accept_addrs)ÚselfZfamilyZ	localhostÚportZclientÚeZserverZbuff© r%   úA/usr/lib/python3/dist-packages/twisted/internet/test/test_iocp.pyÚ_acceptAddressTest(   s8    




(

 ÿ  ÿ þzSupportTests._acceptAddressTestc                 C   s   |   td¡ dS )a  
        L{iocpsupport.get_accept_addrs} returns a three-tuple of address
        information about the socket associated with the file descriptor passed
        to it.  For a connection using IPv4:

          - the first element is C{AF_INET}
          - the second element is a two-tuple of a dotted decimal notation IPv4
            address and a port number giving the peer address of the connection
          - the third element is the same type giving the host address of the
            connection
        z	127.0.0.1N)r'   r   ©r"   r%   r%   r&   Útest_ipv4AcceptAddressH   s    z#SupportTests.test_ipv4AcceptAddressc                 C   s   |   td¡ dS )a‰  
        Like L{test_ipv4AcceptAddress}, but for IPv6 connections.  In this case:

          - the first element is C{AF_INET6}
          - the second element is a two-tuple of a hexadecimal IPv6 address
            literal and a port number giving the peer address of the connection
          - the third element is the same type giving the host address of the
            connection
        z::1N)r'   r   r(   r%   r%   r&   Útest_ipv6AcceptAddressW   s    
z#SupportTests.test_ipv6AcceptAddressN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   r)   r*   Úipv6SkipÚskipr%   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 )
ÚIOCPReactorTestsc                 C   s"   t ƒ }| ¡  |  | d¡¡ dS )zd
        Test reactor behavior (doIteration) when there are no pending time
        events.
        N)r   ZwakeUpZassertFalseÚdoIteration)r"   Úirr%   r%   r&   Útest_noPendingTimerEventsh   s    z*IOCPReactorTests.test_noPendingTimerEventsc                 C   s(   |   tttjƒ¡ |   tttjƒ¡ dS )zY
        Verify that IOCP socket-representing classes implement IReadWriteHandle
        N)Ú
assertTruer
   r   r   Z
Connectionr   ZPortr(   r%   r%   r&   Útest_reactorInterfacesr   s    z'IOCPReactorTests.test_reactorInterfacesc                 C   s   |   tttƒ¡ dS )zH
        Verify that L{Filehandle} implements L{IPushProducer}.
        N)r5   r
   r   r   r(   r%   r%   r&   Útest_fileHandleInterfacesz   s    z*IOCPReactorTests.test_fileHandleInterfacesc                 C   s‚   G dd„ dƒ}t ƒ }|ƒ }t |j|¡}ttd ƒD ]}|j dt|¡ q4| 	d¡ |  
|jt¡ | 	d¡ |  
|jtd ¡ dS )z†
        Verify that we don't lose an event when more than EVENTS_PER_LOOP
        events occur in the same reactor iteration
        c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )z;IOCPReactorTests.test_maxEventsPerIteration.<locals>.FakeFDr   c                 S   s   dS )NÚFakeFDr%   r(   r%   r%   r&   Ú	logPrefixˆ   s    zEIOCPReactorTests.test_maxEventsPerIteration.<locals>.FakeFD.logPrefixc                 S   s   |  j d7  _ d S )Nr   )Úcounter)r"   ZrcÚbytesZevtr%   r%   r&   ÚcbŠ   s    z>IOCPReactorTests.test_maxEventsPerIteration.<locals>.FakeFD.cbN)r+   r,   r-   r:   r9   r<   r%   r%   r%   r&   r8   †   s   r8   r   r   N)r   r    ZEventr<   Úranger   r#   Z	postEventr   r2   r   r:   )r"   r8   r3   ÚfdZeventÚ_r%   r%   r&   Útest_maxEventsPerIteration   s    

z+IOCPReactorTests.test_maxEventsPerIterationN)r+   r,   r-   r4   r6   r7   r@   r%   r%   r%   r&   r1   g   s   
r1   )+r.   r   r   Zstructr   r	   r   r   r   r   r   Zzope.interface.verifyr
   Ztwisted.trialr   Ztwisted.python.logr   Ztwisted.internet.interfacesr   Ztwisted.internet.iocpreactorr   r    r   r   Z$twisted.internet.iocpreactor.reactorr   r   r   Z'twisted.internet.iocpreactor.interfacesr   Z"twisted.internet.iocpreactor.constr   Z%twisted.internet.iocpreactor.abstractr   ÚImportErrorr0   r   r$   Ústrr/   ZTestCaser   r1   r%   r%   r%   r&   Ú<module>   s.    
D