
[XMc           @   sQ  d  Z  d d l Z d d l Z d d l Z y d d l Z Wn e k
 rS d Z n Xd d l m Z d d l	 m
 Z
 d d l m Z 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 m Z d d	 l m Z m Z d
 e f d     YZ d e f d     YZ e j j  e  sMd Z! e! e _" e! e _" n  d S(   s   
Whitebox tests for TCP APIs.
iN(   t   TestCase(   t   log(   t   ECONNABORTEDt   ENOMEMt   ENFILEt   EMFILEt   ENOBUFSt   EINPROGRESSt   Port(   t   ServerFactory(   t   platform(   t   maybeDeferredt   gatherResults(   t   reactort
   interfacest   PlatformAssumptionsTestCasec           B   sV   e  Z d  Z d Z d   Z d   Z d   Z d   Z e j	   d k rT d e _
 n  RS(   s4   
    Test assumptions about platform behaviors.
    i    c         C   sZ   g  |  _  t d  k	 rV t j t j  |  _ t j t j d |  j d f  d |  _ n  d  S(   Ni   i   i   (   t   openSocketst   resourcet   Nonet	   getrlimitt   RLIMIT_NOFILEt   originalFileLimitt	   setrlimitt   socketLimit(   t   self(    (    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyt   setUp   s
    	 c         C   s{   x  |  j  r" |  j  j   j   q Wt d  k	 rw t j t j  d } t |  j d |  } t j	 t j | | f  n  d  S(   Ni   i    (
   R   t   popt   closeR   R   R   R   t   minR   R   (   R   t   currentHardLimitt   newSoftLimit(    (    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyt   tearDown'   s    c         C   s    t  j    } |  j j |  | S(   s|   
        Create and return a new socket object, also tracking it so it can be
        closed in the test tear down.
        (   t   socketR   t   append(   R   t   s(    (    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyR    4   s    c         C   s  |  j    } | j d  | j   d } | j d  |  j    } | j t  xk t |  j  D]M } y |  j    Wq_ t  j k
 r } | j	 d t
 t f k r Pq   q_ Xq_ W|  j d  |  j | j d | f  d t f  |  j t  j | j  } |  j | j	 d t
 t f  d S(   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.1i    i   i   s7   Could provoke neither EMFILE nor ENOBUFS from platform.N(   s	   127.0.0.1i    (   R    t   bindt   getsocknamet   listent   setblockingt   Falset   xrangeR   t   errort   argsR   R   t   failt   assertInt
   connect_exR   t   assertRaisest   accept(   R   t   portt   serverPortNumbert   clientt   it   et   exc(    (    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyt   test_acceptOutOfFiles>   s$    t   win32sh   Windows requires an unacceptably large amount of resources to provoke this behavior in the naive manner.(   t   __name__t
   __module__t   __doc__R   R   R   R    R6   R
   t   getTypet   skip(    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyR      s   			
	%t   SelectReactorTestCasec           B   s   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 e j   d	 k rr d
 e
 _ n  d   Z e j   d	 k r d e _ n  RS(   s7   
    Tests for select-specific failure conditions.
    c         C   s)   g  |  _  g  |  _ t j |  j j  d  S(   N(   t   portst   messagesR   t   addObserverR!   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyR   o   s    		c         C   s<   t  j |  j j  t g  |  j D] } t | j  ^ q   S(   N(   R   t   removeObserverR?   R!   R   R>   R   t   stopListening(   R   t   p(    (    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyR   u   s    c         C   s3   t  | | d | } | j   |  j j |  | 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   t   startListeningR>   R!   (   R   t
   portNumbert   factoryRD   RC   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyR0   |   s    
c   
         s   d t  f   f d     Y} t   } |  j d | d d } | j } z |   | _ | j   d } t j   } | | f } xD |  j D]" }	 |	 j d  | f k r Pq q W|  j	 d |  j f  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              s   e  Z d  Z   f d   Z RS(   sI   
            Pretend to be a socket in an overloaded system.
            c            s   t  j   t j      d  S(   N(   R    R)   t   ost   strerror(   R   (   t   socketErrorNumber(    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyR/      s    (   R8   R9   R:   R/   (    (   RK   (    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyRH      s   i    RD   s	   127.0.0.1s$   Could not accept new connection (%s)t   messages+   Log event for failed accept not found in %rN(
   t   objectR	   R0   R    t   doReadt   errnot	   errorcodeR?   t   getR+   (
   R   RK   RH   RG   R0   t   originalSockett   expectedFormatt   expectedErrorCodet   expectedMessaget   msg(    (   RK   sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyt   _acceptFailureTest   s     		
	c         C   s   |  j  t  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.
        (   RW   R   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyt   test_tooManyFilesFromAccept   s    	c         C   s   |  j  t  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.
        (   RW   R   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyt   test_noBufferSpaceFromAccept   s    c         C   s   |  j  t  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.
        (   RW   R   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyt    test_connectionAbortedFromAccept   s    c         C   s   |  j  t  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.
        (   RW   R   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyt   test_noFilesFromAccept   s    R7   s(   Windows accept(2) cannot generate ENFILEc         C   s   |  j  t  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).
        (   RW   R   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyt   test_noMemoryFromAccept   s    s(   Windows accept(2) cannot generate ENOMEM(   R8   R9   R:   R   R   R0   RW   RX   RY   RZ   R[   R
   R;   R<   R\   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyR=   j   s   				(						s?   This test only applies to reactors that implement IReactorFDset(#   R:   RO   R    RI   R   t   ImportErrorR   t   twisted.trial.unittestR    t   twisted.pythonR   t   twisted.internet.tcpR   R   R   R   R   R   R   t   twisted.internet.protocolR	   t   twisted.python.runtimeR
   t   twisted.internet.deferR   R   t   twisted.internetR   R   R   R=   t   IReactorFDSett
   providedByt   skipMsgR<   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/test/test_tcp_internals.pyt   <module>   s$   $
4Q	