ó
[³XMc           @   sH  d  Z  d d l m Z d d l m Z d d l m Z d d l m Z m	 Z	 d d l
 m Z d Z y d d l m Z Wn e k
 r d Z n Xd d	 l m Z d d
 l m Z d d l m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s>   
Tests for L{twisted.internet.posixbase} and supporting code.
iÿÿÿÿ(   t   set(   t   TestCase(   t   Deferred(   t   PosixReactorBaset   _Waker(   t   ServerFactory(   t   unixs)   Platform does not support AF_UNIX sockets(   t   Port(   t   reactor(   t   ClientProtot   TrivialReactorc           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s#   i  |  _  i  |  _ t j |  ƒ d  S(   N(   t   _readerst   _writersR   t   __init__(   t   self(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyR      s    		c         C   s   t  |  j | <d  S(   N(   t   TrueR   (   R   t   reader(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt	   addReader    s    c         C   s   |  j  | =d  S(   N(   R   (   R   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   removeReader$   s    c         C   s   t  |  j | <d  S(   N(   R   R   (   R   t   writer(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt	   addWriter(   s    c         C   s   |  j  | =d  S(   N(   R   (   R   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   removeWriter,   s    (   t   __name__t
   __module__R   R   R   R   R   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyR
      s
   				t   PosixReactorBaseTestsc           B   sM   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   s(   
    Tests for L{PosixReactorBase}.
    c         C   sC   |  j  | j t ƒ |  j | j | j ƒ |  j | j | j ƒ d  S(   N(   t   assertIsInstancet   wakerR   t   assertInt   _internalReadersR   (   R   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   _checkWaker6   s    c         C   s   t  ƒ  } |  j | ƒ d S(   s   
        When L{PosixReactorBase} is instantiated, it creates a waker and adds
        it to its internal readers set.
        N(   R
   R   (   R   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   test_wakerIsInternalReader<   s    	c         C   s|   t  ƒ  } t ƒ  } | j j | ƒ | j | ƒ | j | j | j ƒ |  j | ƒ |  j	 | | j ƒ |  j	 | | j ƒ d S(   s‹   
        Any L{IReadDescriptors} in L{PosixReactorBase._internalReaders} are
        left alone by L{PosixReactorBase._removeAll}.
        N(
   R
   t   objectR   t   addR   t
   _removeAllR   R   R   R   (   R   R   t   extra(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt"   test_removeAllSkipsInternalReadersE   s    		c         C   s™   t  ƒ  } t ƒ  } t ƒ  } | j | ƒ | j | ƒ | j | j | j ƒ } |  j t | ƒ t | | g ƒ ƒ |  j	 | | j ƒ |  j	 | | j ƒ d S(   s†   
        L{PosixReactorBase._removeAll} returns a list of removed
        L{IReadDescriptor} and L{IWriteDescriptor} objects.
        N(
   R
   R   R   R   R!   R   R   t   assertEqualR    t   assertNotIn(   R   R   R   R   t   removed(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt'   test_removeAllReturnsRemovedDescriptorsT   s    			"c         C   sv   d d l  m } | j |  j |  j g ƒ } |  j t | ƒ d ƒ |  j | d d t ƒ |  j d | d d ƒ d S(	   sœ   
        L{twisted.internet.interfaces.IReactorArbitrary} is redundant with
        L{twisted.internet.interfaces.IReactorFDSet} and is deprecated.
        iÿÿÿÿ(   t
   interfacesi   i    t   categorysb   twisted.internet.interfaces.IReactorArbitrary was deprecated in Twisted 10.1.0: See IReactorFDSet.t   messageN(   t   twisted.internetR(   t   IReactorArbitraryt   flushWarningst"   test_IReactorArbitraryIsDeprecatedt   assertEqualst   lent   DeprecationWarning(   R   R(   t   warningsShown(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyR.   e   s    c         C   sˆ   d d	 d „  ƒ  Y} t  ƒ  } | j | ƒ |  j |  j g ƒ } |  j t | ƒ d ƒ |  j | d d t ƒ |  j d | d d ƒ d S(
   sÆ  
        L{PosixReactorBase} implements the deprecated L{IReactorArbitrary}, and
        L{PosixReactorBase.listenWith} is a part of that interface. To avoid
        unnecessary deprecation warnings when importing posixbase, the
        L{twisted.internet.interfaces._IReactorArbitrary} alias that doesn't
        have the deprecation warning is imported, and instead
        L{PosixReactorBase.listenWith} generates its own deprecation warning.
        t   fakePortc           B   s   e  Z d  „  Z d „  Z RS(   c         _   s   d  S(   N(    (   R   t   argst   kw(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyR      s    c         S   s   d  S(   N(    (   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   startListening„   s    (   R   R   R   R6   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyR3   €   s   	i   i    R)   s@   listenWith is deprecated since Twisted 10.1.  See IReactorFDSet.R*   N(    (   R
   t
   listenWithR-   t   test_listenWithIsDeprecatedR/   R0   R1   (   R   R3   R   t   warnings(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyR8   w   s    		c         C   sˆ   d d	 d „  ƒ  Y} t  ƒ  } | j | ƒ |  j |  j g ƒ } |  j t | ƒ d ƒ |  j | d d t ƒ |  j d | d d ƒ d S(
   sÈ  
        L{PosixReactorBase} implements the deprecated L{IReactorArbitrary}, and
        L{PosixReactorBase.connectWith} is a part of that interface. To avoid
        unnecessary deprecation warnings when importing posixbase, the
        L{twisted.internet.interfaces._IReactorArbitrary} alias that doesn't
        have the deprecation warning is imported, and instead
        L{PosixReactorBase.connectWith} generates its own deprecation warning.
        t   fakeConnectorc           B   s   e  Z d  „  Z d „  Z RS(   c         _   s   d  S(   N(    (   R   R4   R5   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyR      s    c         S   s   d  S(   N(    (   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   connect    s    (   R   R   R   R;   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyR:   œ   s   	i   i    R)   sA   connectWith is deprecated since Twisted 10.1.  See IReactorFDSet.R*   N(    (   R
   t   connectWithR-   t   test_connectWithIsDeprecatedR/   R0   R1   (   R   R:   R   R9   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyR=   “   s    		(
   R   R   t   __doc__R   R   R#   R'   R.   R8   R=   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyR   1   s   							t   TCPPortTestsc           B   s/   e  Z d  Z e e e ƒ s$ d Z n  d „  Z RS(   s1   
    Tests for L{twisted.internet.tcp.Port}.
    s   Non-posixbase reactorc         C   s=   t  d t ƒ  ƒ } t | _ d „  | _ |  j | j ƒ  t ƒ S(   sƒ   
        L{Port.stopListening} returns a L{Deferred} which errbacks if
        L{Port.connectionLost} raises an exception.
        i90  c         S   s   d d S(   Ni   i    (    (   t   reason(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   <lambda>¿   s    (   R   R   R   t	   connectedt   connectionLostt   assertFailuret   stopListeningt   ZeroDivisionError(   R   t   port(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   test_connectionLostFailed¸   s    	(   R   R   R>   t
   isinstanceR   R   t   skipRH   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyR?   °   s   	t   TimeoutReportReactorc           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sà   
    A reactor which is just barely runnable and which cannot monitor any
    readers or writers, and which fires a L{Deferred} with the timeout
    passed to its C{doIteration} method as soon as that method is invoked.
    c         C   s&   t  j |  ƒ t ƒ  |  _ d |  _ d  S(   Nid   (   R   R   R   t   iterationTimeoutt   now(   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyR   Ê   s    c         C   s   d S(   s—   
        Ignore the reader.  This is necessary because the waker will be
        added.  However, we won't actually monitor it for any events.
        N(    (   R   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyR   Ð   s    c         C   s   g  S(   s°   
        There are no readers or writers, so there is nothing to remove.
        This will be called when the reactor stops, though, so it must be
        implemented.
        (    (   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt	   removeAll×   s    c         C   s   |  j  S(   sx   
        Override the real clock with a deterministic one that can be easily
        controlled in a unit test.
        (   RM   (   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   secondsà   s    c         C   s2   |  j  } | d  k	 r. d  |  _  | j | ƒ n  d  S(   N(   RL   t   Nonet   callback(   R   t   timeoutt   d(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   doIterationè   s    		(   R   R   R>   R   R   RN   RO   RT   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyRK   Ä   s   					t   IterationTimeoutTestsc           B   sV   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   s   
    Tests for the timeout argument L{PosixReactorBase.run} calls
    L{PosixReactorBase.doIteration} with in the presence of various delayed
    calls.
    c            sD   g  } ˆ  j  j | j ƒ ˆ  j  j ‡  f d †  ƒ ˆ  j ƒ  | d S(   Nc            s
   ˆ  j  ƒ  S(   N(   t   stop(   t   ignored(   R   (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyRA   ù   s    i    (   RL   t   addCallbackt   appendt   run(   R   R   RR   (    (   R   sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   _checkIterationTimeoutö   s
    
c         C   s,   t  ƒ  } |  j | ƒ } |  j | d ƒ d S(   sl   
        If there are no delayed calls, C{doIteration} is called with a
        timeout of C{None}.
        N(   RK   R[   R/   RP   (   R   R   RR   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   test_noCallsþ   s    	c         C   s?   t  ƒ  } | j d d „  ƒ |  j | ƒ } |  j | d ƒ d S(   sÁ   
        If there is a delayed call, C{doIteration} is called with a timeout
        which is the difference between the current time and the time at
        which that call is to run.
        id   c           S   s   d  S(   N(   RP   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyRA     s    N(   RK   t	   callLaterR[   R/   (   R   R   RR   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   test_delayedCall  s    	c         C   sN   t  ƒ  } | j d d „  ƒ | j d 7_ |  j | ƒ } |  j | d ƒ d S(   s¯   
        If a delayed call is scheduled and then some time passes, the
        timeout passed to C{doIteration} is reduced by the amount of time
        which passed.
        id   c           S   s   d  S(   N(   RP   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyRA     s    i   iK   N(   RK   R]   RM   R[   R/   (   R   R   RR   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   test_timePasses  s
    	c         C   se   t  ƒ  } | j d d „  ƒ | j d d „  ƒ | j d d „  ƒ |  j | ƒ } |  j | d ƒ d S(   sÜ   
        If there are several delayed calls, C{doIteration} is called with a
        timeout which is the difference between the current time and the
        time at which the earlier of the two calls is to run.
        i2   c           S   s   d  S(   N(   RP   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyRA   (  s    i
   c           S   s   d  S(   N(   RP   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyRA   )  s    id   c           S   s   d  S(   N(   RP   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyRA   *  s    N(   RK   R]   R[   R/   (   R   R   RR   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   test_multipleDelayedCalls!  s    	c         C   s]   t  ƒ  } | j d d „  ƒ } | j d 7_ | j d ƒ |  j | ƒ } |  j | d ƒ d S(   sÀ   
        If a delayed call is reset, the timeout passed to C{doIteration} is
        based on the interval between the time when reset is called and the
        new delay of the call.
        i2   c           S   s   d  S(   N(   RP   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyRA   6  s    i   i   N(   RK   R]   RM   t   resetR[   R/   (   R   R   t   callRR   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   test_resetDelayedCall/  s    	c         C   s]   t  ƒ  } | j d d „  ƒ } | j d 7_ | j d ƒ |  j | ƒ } |  j | d ƒ d S(   sñ   
        If a delayed call is re-delayed, the timeout passed to
        C{doIteration} is based on the remaining time before the call would
        have been made and the additional amount of time passed to the delay
        method.
        i2   c           S   s   d  S(   N(   RP   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyRA   E  s    i
   i   i<   N(   RK   R]   RM   t   delayR[   R/   (   R   R   Rb   RR   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   test_delayDelayedCall=  s    	c         C   sK   t  ƒ  } | j d d „  ƒ } | j ƒ  |  j | ƒ } |  j | d ƒ d S(   sp   
        If the only delayed call is canceled, C{None} is the timeout passed
        to C{doIteration}.
        i2   c           S   s   d  S(   N(   RP   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyRA   R  s    N(   RK   R]   t   cancelR[   R/   RP   (   R   R   Rb   RR   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   test_cancelDelayedCallL  s
    	
(   R   R   R>   R[   R\   R^   R_   R`   Rc   Re   Rg   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyRU   ð   s   		
					t   ConnectedDatagramPortTestCasec           B   s5   e  Z d  Z e d k	 r! e Z n  d „  Z d „  Z RS(   s/   
    Test connected datagram UNIX sockets.
    c            s>   ‡  f d †  } t  j d t ƒ  ƒ } | | _ | j d ƒ d S(   s   
        L{ConnectedDatagramPort} does not call the deprecated C{loseConnection}
        in L{ConnectedDatagramPort.connectionFailed}.
        c              s   ˆ  j  d ƒ d S(   s€   
            Dummy C{loseConnection} method. C{loseConnection} is deprecated and
            should not get called.
            s7   loseConnection is deprecated and should not get called.N(   t   fail(    (   R   (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   loseConnectionf  s    t   goodbyeN(   R   t   ConnectedDatagramPortRP   R	   Rj   t   connectionFailed(   R   Rj   RG   (    (   R   sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt-   test_connectionFailedDoesntCallLoseConnectiona  s    	c            sZ   t  ˆ  _ ‡  f d †  } t j d t ƒ  ƒ } | | _ | j d ƒ ˆ  j ˆ  j t	 ƒ d S(   sÁ   
        L{ConnectedDatagramPort} calls L{ConnectedDatagramPort.stopListening}
        instead of the deprecated C{loseConnection} in
        L{ConnectedDatagramPort.connectionFailed}.
        c              s   t  ˆ  _ d S(   s8   
            Dummy C{stopListening} method.
            N(   R   t   called(    (   R   (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyRE   z  s    Rk   N(
   t   FalseRo   R   Rl   RP   R	   RE   Rm   R/   R   (   R   RE   RG   (    (   R   sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt'   test_connectionFailedCallsStopListeningr  s    		N(   R   R   R>   t   skipSocketsRP   RJ   Rn   Rq   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyRh   Y  s
   		N(   R>   t   twisted.python.compatR    t   twisted.trial.unittestR   t   twisted.internet.deferR   t   twisted.internet.posixbaseR   R   t   twisted.internet.protocolR   RP   Rr   R+   R   t   ImportErrort   twisted.internet.tcpR   R   t   twisted.test.test_unixR	   R
   R   R?   RK   RU   Rh   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_posixbase.pyt   <module>   s&   
,i