o
    b                  
   @   sp  d 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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 dd	lmZ dd
lmZ 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  ddl!m"Z"m#Z#m$Z$ W n e%y   ej&dkr{ dZ'Y nw z	eee(  W n e)y Z* zdZ+e,e*Z-W Y dZ*[*n	dZ*[*ww dZ+dZ-G dd deZ.G dd deZ/dS )z,
Tests for L{twisted.internet.iocpreactor}.
    N)array)AF_INETAF_INET6SOCK_STREAM
SOL_SOCKETsocket)pack)skipIf)verifyClass)IPushProducer)msg)TestCase)iocpsupporttcpudp)
FileHandle)SO_UPDATE_ACCEPT_CONTEXT)IReadWriteHandle)EVENTS_PER_LOOP
KEY_NORMALIOCPReactorwin32z%This test only applies to IOCPReactorTF c                   @   s2   e Zd ZdZdd Zdd Zeeedd Z	dS )	SupportTestszn
    Tests for L{twisted.internet.iocpreactor.iocpsupport}, low-level reactor
    implementation helpers.
    c           
      C   s  t d| t|t}| |j |d |d t|t}| |j |d z|||	 d f W n t
yV } z| |jtjtjf W Y d}~nd}~ww t|t}| |j tdd}| dt| | |d ttd	D ]7}z|tttd
|  W  n& t
y }	 z|	jttdkr |dkr W Y d}	~	nd}	~	ww td q}| || 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}.

        A port starts listening (is bound) at the low-level socket without
        calling accept() yet.
        A client is then connected.
        After the client is connected IOCP accept() is called, which is the
        target of these tests.

        Most of the time, the socket is ready instantly, but sometimes
        the socket is not ready right away after calling IOCP accept().
        It should not take more than 5 seconds for a socket to be ready, as
        the client connection is already made over the loopback interface.

        These are flaky tests.
        Tweak the failure rate by changing the number of retries and the
        wait/sleep between retries.

        If you will need to update the retries to wait more than 5 seconds
        for the port to be available, then there might a bug in the code and
        not the test (or a very, very busy VM running the tests).
        z	family = )r   r      FNBs                                                                                                                                                                                                                                                                   r      PWSAENOTCONNg?   )r   r   r   
addCleanupclosebindlistensetblockingconnectgetsocknameOSErrorassertInerrnoEINPROGRESSEWOULDBLOCKr   assertEqual_iocpacceptfilenoreversedrange
setsockoptr   r   r   getattrtimesleepgetpeernameget_accept_addrs)
selffamily	localhostportclienteserverbuffattemptsRemainingsocketError rB   A/usr/lib/python3/dist-packages/twisted/internet/test/test_iocp.py_acceptAddressTest7   sJ   




"

  zSupportTests._acceptAddressTestc                 C      |  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)rD   r   r8   rB   rB   rC   test_ipv4AcceptAddress      z#SupportTests.test_ipv4AcceptAddressc                 C   rE   )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)rD   r   rF   rB   rB   rC   test_ipv6AcceptAddress   rH   z#SupportTests.test_ipv6AcceptAddressN)
__name__
__module____qualname____doc__rD   rG   r	   ipv6Skipipv6SkipReasonrI   rB   rB   rB   rC   r   1   s    R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   wakeUpassertFalsedoIteration)r8   irrB   rB   rC   test_noPendingTimerEvents   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   
Connectionr   PortrF   rB   rB   rC   test_reactorInterfaces   s   z'IOCPReactorTests.test_reactorInterfacesc                 C   s   |  ttt dS )zH
        Verify that L{Filehandle} implements L{IPushProducer}.
        N)rV   r
   r   r   rF   rB   rB   rC   test_fileHandleInterfaces   s   z*IOCPReactorTests.test_fileHandleInterfacesc                 C   s   G dd d}t  }| }t|j|}ttd D ]
}|jdt| q|	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FakeFDrB   rF   rB   rB   rC   	logPrefix   s   zEIOCPReactorTests.test_maxEventsPerIteration.<locals>.FakeFD.logPrefixc                 S   s   |  j d7  _ d S )Nr   )counter)r8   rcbytesevtrB   rB   rC   cb   s   z>IOCPReactorTests.test_maxEventsPerIteration.<locals>.FakeFD.cbN)rJ   rK   rL   r]   r\   ra   rB   rB   rB   rC   r[      s    r[   r   r   N)r   r-   Eventra   r1   r   r;   	postEventr   rS   r,   r]   )r8   r[   rT   fdevent_rB   rB   rC   test_maxEventsPerIteration   s   	

z+IOCPReactorTests.test_maxEventsPerIterationN)rJ   rK   rL   rU   rY   rZ   rg   rB   rB   rB   rC   rP      s
    	rP   )0rM   r)   sysr4   r   r   r   r   r   r   structr   unittestr	   zope.interface.verifyr
   twisted.internet.interfacesr   twisted.python.logr   twisted.trial.unittestr   twisted.internet.iocpreactorr   r-   r   r   %twisted.internet.iocpreactor.abstractr   "twisted.internet.iocpreactor.constr   'twisted.internet.iocpreactor.interfacesr   $twisted.internet.iocpreactor.reactorr   r   r   ImportErrorplatformskipr!   r'   r=   rN   strrO   r   rP   rB   rB   rB   rC   <module>   sD   
u