ó O)|Pc@`swdZddlmZmZddlZddlZddlZyddlZWnek ridZnXddl m Z ddl m Z ddlmZddlmZmZmZmZmZmZmZddlmZdd lmZdd lmZmZdd lm Z m!Z!d e fd „ƒYZ"de fd„ƒYZ#e!j$j%e ƒssdZ&e&e"_'e&e#_'ndS(s Whitebox tests for TCP APIs. i(tdivisiontabsolute_importN(t_PY3(tTestCase(tlog(t ECONNABORTEDtENOMEMtENFILEtEMFILEtENOBUFSt EINPROGRESStPort(t ServerFactory(tplatform(t maybeDeferredt gatherResults(treactort interfacestPlatformAssumptionsTestCasecB`seeZdZerdZndZd„Zd„Zd„Zd„Z e j ƒdkrcde _nRS( s4 Test assumptions about platform behaviors. s<Port when Python 3 supports twisted.internet.process (#5987)i cC`sg|_tdk r}ddlm}t|ƒƒd}tjtjƒ|_tj tj||jdfƒ|d|_ ndS(Ni(t _listOpenFDsiiid( t openSocketstresourcetNonettwisted.internet.processRtlent getrlimitt RLIMIT_NOFILEtoriginalFileLimitt setrlimitt socketLimit(tselfRtnewLimit((sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pytsetUp%s   cC`s{x |jr"|jjƒjƒqWtdk rwtjtjƒd}t|jd|ƒ}tj tj||fƒndS(Nii( RtpoptcloseRRRRtminRR(RtcurrentHardLimitt newSoftLimit((sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyttearDown5s   cC`s tjƒ}|jj|ƒ|S(s| Create and return a new socket object, also tracking it so it can be closed in the test tear down. (tsocketRtappend(Rts((sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyR'Bs cC`s|jƒ}|jdƒ|jƒd}|jdƒ|jƒ}|jtƒxkt|jƒD]M}y|jƒWq_tjk r«}|j dt t fkr¥Pq¬‚q_Xq_W|j dƒ|j |jd|fƒdtfƒ|jtj|jƒ}|j |j dt t fƒdS(s˜ Test that the platform accept(2) call fails with either L{EMFILE} or L{ENOBUFS} when there are too many file descriptors open. s 127.0.0.1iiis7Could provoke neither EMFILE nor ENOBUFS from platform.N(s 127.0.0.1i(R'tbindt getsocknametlistent setblockingtFalsetxrangeRterrortargsRR tfailtassertInt connect_exR t assertRaisestaccept(RtporttserverPortNumbertclienttitetexc((sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyttest_acceptOutOfFilesLs$        twin32shWindows requires an unacceptably large amount of resources to provoke this behavior in the naive manner.( t__name__t __module__t__doc__RtskipRR R&R'R=R tgetType(((sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyRs   %tSelectReactorTestCasecB`s›eZdZd„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z e j ƒd krrd e _ nd „Ze j ƒd kr™d e_ nRS( s7 Tests for select-specific failure conditions. cC`s)g|_g|_tj|jjƒdS(N(tportstmessagesRt addObserverR((R((sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyR }s  cC`s<tj|jjƒtg|jD]}t|jƒ^q ƒS(N(RtremoveObserverRFR(RRERt stopListening(Rtp((sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyR&ƒscC`s3t||d|ƒ}|jƒ|jj|ƒ|S(s Create, start, and return a new L{Port}, also tracking it so it can be stopped in the test tear down. t interface(R tstartListeningRER((Rt portNumbertfactoryRKRJ((sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyR7Šs c `sØdtf‡fd†ƒY}tƒ}|jd|ddƒ}|j}z|ƒ|_|jƒd}tjˆ}||f}xD|jD]"} | jdƒ|fkr‰Pq‰q‰W|j d|jfƒWd ||_Xd S( s¢ Test behavior in the face of an exception from C{accept(2)}. On any exception which indicates the platform is unable or unwilling to allocate further resources to us, the existing port should remain listening, a message should be logged, and the exception should not propagate outward from doRead. @param socketErrorNumber: The errno to simulate from accept. t FakeSocketc`seZdZ‡fd†ZRS(sI Pretend to be a socket in an overloaded system. c`stjˆtjˆƒƒ‚dS(N(R'R0toststrerror(R(tsocketErrorNumber(sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyR6¤s(R?R@RAR6((RR(sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyRO siRKs 127.0.0.1s$Could not accept new connection (%s)tmessages+Log event for failed accept not found in %rN( tobjectR R7R'tdoReadterrnot errorcodeRFtgetR2( RRRRORNR7toriginalSockettexpectedFormattexpectedErrorCodetexpectedMessagetmsg((RRsC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyt_acceptFailureTest•s        cC`s |jtƒS(s* C{accept(2)} can fail with C{EMFILE} when there are too many open file descriptors in the process. Test that this doesn't negatively impact any other existing connections. C{EMFILE} mainly occurs on Linux when the open file rlimit is encountered. (R^R(R((sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyttest_tooManyFilesFromAccept½s cC`s |jtƒS(sö Similar to L{test_tooManyFilesFromAccept}, but test the case where C{accept(2)} fails with C{ENOBUFS}. This mainly occurs on Windows and FreeBSD, but may be possible on Linux and other platforms as well. (R^R (R((sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyttest_noBufferSpaceFromAcceptÉscC`s |jtƒS(sù Similar to L{test_tooManyFilesFromAccept}, but test the case where C{accept(2)} fails with C{ECONNABORTED}. It is not clear whether this is actually possible for TCP connections on modern versions of Linux. (R^R(R((sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyt test_connectionAbortedFromAcceptÔscC`s |jtƒS(sà Similar to L{test_tooManyFilesFromAccept}, but test the case where C{accept(2)} fails with C{ENFILE}. This can occur on Linux when the system has exhausted (!) its supply of inodes. (R^R(R((sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyttest_noFilesFromAcceptßsR>s(Windows accept(2) cannot generate ENFILEcC`s |jtƒS(s³ Similar to L{test_tooManyFilesFromAccept}, but test the case where C{accept(2)} fails with C{ENOMEM}. On Linux at least, this can sensibly occur, even in a Python program (which eats memory like no ones business), when memory has become fragmented or low memory has been filled (d_alloc calls kmem_cache_alloc calls kmalloc - kmalloc only allocates out of low memory). (R^R(R((sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyttest_noMemoryFromAcceptìs s(Windows accept(2) cannot generate ENOMEM(R?R@RAR R&R7R^R_R`RaRbR RCRBRc(((sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyRDxs   (   s?This test only applies to reactors that implement IReactorFDset((RAt __future__RRRVR'RPRt ImportErrorRttwisted.python.compatRttwisted.trial.unittestRttwisted.pythonRttwisted.internet.tcpRRRRR R R ttwisted.internet.protocolR ttwisted.python.runtimeR ttwisted.internet.deferRRttwisted.internetRRRRDt IReactorFDSett providedBytskipMsgRB(((sC/usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyts($  4\ƒ