ϪfdZddlZddlZddlZddlmZddlmZmZmZm Z mZddl m Z ddl m Z ddlmZddlmZdd lmZdd lmZ dd lmZmZmZdd lmZdd lmZddlm Z ddl!m"Z"m#Z#m$Z$ eeejQdZ)dZ*GddeZ.GddeZ/y#e%$rejLdk(rdZ'YLwxYw#e+$rZ,dZ)e-e,Z*YdZ,[,HdZ,[,wwxYw)z, Tests for L{twisted.internet.iocpreactor}. N)array)AF_INETAF_INET6 SOCK_STREAM SOL_SOCKETsocket)pack)skipIf) verifyClass) IPushProducer)msg)TestCase) iocpsupporttcpudp) FileHandle)SO_UPDATE_ACCEPT_CONTEXT)IReadWriteHandle)EVENTS_PER_LOOP KEY_NORMAL IOCPReactorwin32z%This test only applies to IOCPReactorFTc:eZdZdZdZdZeeedZ y) SupportTestszn Tests for L{twisted.internet.iocpreactor.iocpsupport}, low-level reactor implementation helpers. c td|t|t}|j|j|j d|j dt|t}|j|j|jd |j||jdft|t}|j|jtdd}|j!dt#j$|j'|j'|dt)t+d D]7} |j-t.t0t3d |j'n|j!||j;dd |jdd ft#j<|j'|y#t$rE}|j|jtjtjfYd}~ld}~wwxYw#t$r.} | jt5td k7r|dk(rYd} ~ nd} ~ wwxYwt7j8d 6)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 = )rrFNBsrP WSAENOTCONNg?)r rr addCleanupclosebindlisten setblockingconnect getsocknameOSErrorassertInerrno EINPROGRESS EWOULDBLOCKr assertEqual_iocpacceptfilenoreversedrange setsockoptrrr getattrtimesleep getpeernameget_accept_addrs) selffamily localhostportclienteserverbuffattemptsRemaining socketErrors A/usr/lib/python3/dist-packages/twisted/internet/test/test_iocp.py_acceptAddressTestzSupportTests._acceptAddressTest7s4 iz "#fk*  # ' A ,  %5! K NNIt'7'7'9!'<= > ,  %S+& ELL tTU!)%(!3   !! 8$sDKKM:R B  V'')"1-v/A/A/CBQ/G H  " "6==?D 9 S K MM!''E$5$5u7H7H#I J J K0 $$}(EE%)*  JJsOs0$G 3H1 H.):H))H.1 I(:$I##I(c0|jtdy)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)rFrr;s rEtest_ipv4AcceptAddressz#SupportTests.test_ipv4AcceptAddresss 5c0|jtdy)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)rFrrHs rEtest_ipv6AcceptAddressz#SupportTests.test_ipv6AcceptAddresss %0rJN) __name__ __module__ __qualname____doc__rFrIr ipv6Skipipv6SkipReasonrLrJrErr1s. P d 6 Hn% 1& 1rJrc$eZdZdZdZdZdZy)IOCPReactorTestscxt}|j|j|jdy)zd Test reactor behavior (doIteration) when there are no pending time events. N)rwakeUp assertFalse doIteration)r;irs rEtest_noPendingTimerEventsz*IOCPReactorTests.test_noPendingTimerEventss+ ]  -.rJc|jtttj|jttt j y)zY Verify that IOCP socket-representing classes implement IReadWriteHandle N) assertTruer rr ConnectionrPortrHs rEtest_reactorInterfacesz'IOCPReactorTests.test_reactorInterfacess4  $4cnnEF  $4chh?@rJcJ|jttty)zH Verify that L{Filehandle} implements L{IPushProducer}. N)r]r r rrHs rEtest_fileHandleInterfacesz*IOCPReactorTests.test_fileHandleInterfacess  M:>?rJcGdd}t}|}tj|j|}t t dzD]#}|j jdt|%|jd|j|jt |jd|j|jt dzy)z Verify that we don't lose an event when more than EVENTS_PER_LOOP events occur in the same reactor iteration ceZdZdZdZdZy);IOCPReactorTests.test_maxEventsPerIteration..FakeFDrcy)NFakeFDrSrHs rE logPrefixzEIOCPReactorTests.test_maxEventsPerIteration..FakeFD.logPrefixsrJc.|xjdz c_y)Nr)counter)r;rcbytesevts rEcbz>IOCPReactorTests.test_maxEventsPerIteration..FakeFD.cbs ! rJN)rMrNrOrjrhrnrSrJrErgresG  "rJrgrrN) rr0Eventrnr4rr> postEventrrYr/rj)r;rgrZfdevent_s rEtest_maxEventsPerIterationz+IOCPReactorTests.test_maxEventsPerIterations  " "] X BEE2&*+ 4A GG  aU 3 4 t _5 q _q%89rJN)rMrNrOr[r`rbrtrSrJrErUrUs/A@ :rJrU)0rPr,sysr7rrrrrrstructr unittestr zope.interface.verifyr twisted.internet.interfacesr twisted.python.logr twisted.trial.unittestrtwisted.internet.iocpreactorrr0rr%twisted.internet.iocpreactor.abstractr"twisted.internet.iocpreactor.constr'twisted.internet.iocpreactor.interfacesr$twisted.internet.iocpreactor.reactorrrr ImportErrorplatformskipr$rQrRr*r@strrrUrSrJrErs EE-5"+3KK@KH 8[!'') HNr18r1j.:x.:M3 ||w 2D 3HVNs*&B!/B>!B;:B;>C CC