ó
B±Nc           @   sÿ   d  Z  d d l m Z d d l m Z d d l m Z m Z d d l m	 Z	 d d l
 m Z m Z m Z d d l m Z m Z d d l m Z m Z d	 „  Z e Z d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s?   
Various helpers for tests for connection-oriented transports.
iÿÿÿÿ(   t   collect(   t   ref(   t   contextt   log(   t   fullyQualifiedName(   t   ILogContextt   msgt   err(   t   Deferredt   gatherResults(   t   ServerFactoryt   Protocolc         C   s   t  ƒ  } |  | _ | S(   s  
    Helper function which returns a L{ServerFactory} which will build instances
    of C{protocol}.

    @param protocol: A callable which returns an L{IProtocol} provider to be
        used to handle connections to the port the returned factory listens on.
    (   R
   t   protocol(   R   t   factory(    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt   serverFactoryFor   s    		t   _AcceptOneClientc           B   s    e  Z d  Z d „  Z d „  Z RS(   sM  
    This factory fires a L{Deferred} with a protocol instance shortly after it
    is constructed (hopefully long enough afterwards so that it has been
    connected to a transport).

    @ivar reactor: The reactor used to schedule the I{shortly}.
    @ivar result: A L{Deferred} which will be fired with the protocol instance.
    c         C   s   | |  _  | |  _ d  S(   N(   t   reactort   result(   t   selfR   R   (    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt   __init__)   s    	c         C   s2   t  j |  | ƒ } |  j j d |  j j | ƒ | S(   Ni    (   R
   t   buildProtocolR   t	   callLaterR   t   callback(   R   t   addrR   (    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyR   .   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyR       s   	t   ClosingLaterProtocolc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sÕ   
    ClosingLaterProtocol exchanges one byte with its peer and then disconnects
    itself.  This is mostly a work-around for the fact that connectionMade is
    called before the SSL handshake has completed.
    c         C   s   d  |  _ | |  _ d  S(   N(   t   Nonet   lostConnectionReasont   onConnectionLost(   R   R   (    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyR   ;   s    	c         C   s   t  d ƒ d  S(   Ns#   ClosingLaterProtocol.connectionMade(   R   (   R   (    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt   connectionMade@   s    c         C   s"   t  d | f ƒ |  j j ƒ  d  S(   Ns$   ClosingLaterProtocol.dataReceived %r(   R   t	   transportt   loseConnection(   R   t   bytes(    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt   dataReceivedD   s    c         C   s'   t  d ƒ | |  _ |  j j |  ƒ d  S(   Ns#   ClosingLaterProtocol.connectionLost(   R   R   R   R   (   R   t   reason(    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt   connectionLostI   s    
	(   R   R   R   R   R   R#   R%   (    (    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyR   5   s
   			t   ConnectionTestsMixinc           B   sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sg   
    This mixin defines test methods which should apply to most L{ITransport}
    implementations.
    c         C   s    t  d t |  j ƒ f ƒ ‚ d S(   s    
        Return an object providing L{IStreamServerEndpoint} for use in creating
        a server to use to establish the connection type to be tested.
        s#   %s.serverEndpoint() not implementedN(   t   NotImplementedErrorR   t	   __class__(   R   R   (    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt   serverEndpointU   s    c         C   s    t  d t |  j ƒ f ƒ ‚ d S(   s    
        Return an object providing L{IStreamClientEndpoint} for use in creating
        a client to use to establish the connection type to be tested.
        s#   %s.clientEndpoint() not implementedN(   R'   R   R(   (   R   R   t   serverAddress(    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt   clientEndpoint^   s    c            sh   t  ƒ  ‰ t ˆ  ˆ ƒ } | | _ ˆ j ˆ  ƒ } | j | ƒ } ‡  ‡ ‡ ‡ f d †  } | j | ƒ | S(   sl  
        Create a loopback connection of the type to be tested, using
        C{clientProtocol} and C{serverProtocol} to handle each end of the
        connection.

        @return: A L{Deferred} which fires when the connection is established.
            The result is a two-tuple of the client protocol instance and
            server protocol instance.
        c            s=   |  j  ƒ  } ˆ j ˆ  | ƒ } t | j t ˆ ƒ ƒ ˆ g ƒ S(   N(   t   getHostR+   R	   t   connectt
   factoryFor(   t   portt   addresst   client(   R   R   t   clientProtocolt   accepted(    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt   startedListeningv   s    (   R   R   R   R)   t   listent   addCallback(   R   R   R2   t   serverProtocolR   t   servert	   listeningR4   (    (   R   R   R2   R3   sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt   loopbackg   s    
		c            s¯   d t  f d „  ƒ  Y‰ ˆ j ƒ  ‰  ˆ j ˆ  ‡ f d †  ‡ f d †  ƒ } d „  } | j | ƒ ‡ f d †  } | j | ƒ | j t ƒ | j ‡  f d †  ƒ ˆ j ˆ  ƒ d S(	   s›   
        Client and server transports implement L{ILoggingContext.logPrefix} to
        return a message reflecting the protocol they are running.
        t   CustomLogPrefixProtocolc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         S   s   | |  _  t ƒ  |  _ d  S(   N(   t   _prefixR   t   system(   R   t   prefix(    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyR   …   s    	c         S   s   |  j  S(   N(   R<   (   R   (    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt	   logPrefix‰   s    c         S   s!   |  j  j t j t ƒ d ƒ d  S(   NR=   (   R=   R   R   t   getR   (   R   R"   (    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyR#   Œ   s    (   R   R   R   R?   R#   (    (    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyR;   „   s   		c              s
   ˆ  d ƒ S(   Ns   Custom Client(    (    (   R;   (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt   <lambda>’   s    c              s
   ˆ  d ƒ S(   Ns   Custom Server(    (    (   R;   (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyRA   “   s    c         S   sB   |  \ } } | j  j d ƒ | j  j d ƒ t | j | j g ƒ S(   Nt   foot   bar(   R    t   writeR	   R=   (   t   .0R1   R8   (    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt	   connected”   s    	c            s0   |  \ } } ˆ  j  d | ƒ ˆ  j  d | ƒ d  S(   Ns   Custom Clients   Custom Server(   t   assertIn(   RE   R1   R8   (   R   (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt	   gotSystemš   s    	c            s
   ˆ  j  ƒ  S(   N(   t   stop(   t   ignored(   R   (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyRA   Ÿ   s    N(   R   t   buildReactorR:   R6   t
   addErrbackR   t
   runReactor(   R   t   dRF   RH   (    (   R   R   R;   sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt   test_logPrefix   s    	c            s·   ˆ j  ƒ  ‰  g  ‰ t ƒ  ‰ ‡ f d †  } ˆ j ˆ  ƒ j t | ƒ ƒ } ‡  ‡ ‡ ‡ f d †  } | j | ƒ | j t ƒ | j ‡  f d †  ƒ ˆ j ˆ  ƒ ˆ j	 ˆ t
 t
 g ƒ d S(   s}   
        After a connection is disconnected, L{ITransport.write} and
        L{ITransport.writeSequence} are no-ops.
        c              s
   t  ˆ  ƒ S(   N(   R   (    (   t   serverConnectionLostDeferred(    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyRA   ­   s    c            s±   t  d |  j ƒ  f ƒ ˆ j ˆ |  j ƒ  ƒ } t ƒ  ‰  ‡  f d †  } | j t | ƒ ƒ } d „  } | j | ˆ  j ƒ ‡ f d †  } ˆ  j | ƒ ˆ j | ƒ t	 ˆ  ˆ g ƒ S(   Ns   Listening on %rc              s
   t  ˆ  ƒ S(   N(   R   (    (   t   lostConnectionDeferred(    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyRA   µ   s   c         S   s%   t  d |  f ƒ |  j j d ƒ d  S(   Ns   About to write to %rt   x(   R   R    RD   (   t   proto(    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyRD   ¸   s    c            sH   t  d |  f ƒ |  j j d ƒ |  j j d d g ƒ ˆ  j t ƒ d  S(   Ns   %r disconnecteds   some bytes to get lostt   somet   more(   R   R    RD   t   writeSequencet   appendt   True(   RS   (   t   finished(    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt   disconnected¾   s    (
   R   R,   R+   R   R-   R.   t   addCallbackst   errbackR6   R	   (   R/   t   endpointR   R1   RD   RZ   (   R   R   RY   RP   (   RQ   sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyR9   °   s    		c            s
   ˆ  j  ƒ  S(   N(   RI   (   RJ   (   R   (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyRA   Ì   s    N(   RK   R   R)   R5   R   R6   RL   R   RM   t   assertEqualRX   (   R   R   t   portDeferredR9   (    (   R   R   RY   RP   sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt   test_writeAfterDisconnect£   s    	c            sÄ   t  ƒ  ‰ t ˆ ƒ ‰ t ˆ ƒ } ˆ j ƒ  ‰  ˆ j ˆ  ƒ j t t ƒ ƒ } ‡  ‡ ‡ ‡ f d †  } | j | ƒ | j	 t
 ƒ | j ‡  f d †  ƒ ˆ j ˆ  ƒ d ‰ t ƒ  ˆ j d | ƒ  ƒ d S(   s’   
        After the connection a protocol is being used for is closed, the reactor
        discards all of its references to the protocol.
        c            sw   t  d |  j ƒ  f ƒ ˆ j ˆ  |  j ƒ  ƒ } | j t ‡ f d †  ƒ ƒ } d „  } | j | ƒ | j ˆ j ƒ ˆ S(   Ns   Listening on %rc              s   ˆ  S(   N(    (    (   R2   (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyRA   â   s    c         S   s"   t  d |  f ƒ |  j j ƒ  d  S(   Ns   About to disconnect %r(   R   R    R!   (   RS   (    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt
   disconnectã   s    (   R   R,   R+   R-   R.   R6   RL   R\   (   R/   R]   R1   Ra   (   R   R   RQ   R2   (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyR9   Þ   s    	c            s
   ˆ  j  ƒ  S(   N(   RI   (   RJ   (   R   (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyRA   ì   s    N(   R   R   R   RK   R)   R5   R   R   R6   RL   R   RM   R   R    t   assertIdentical(   R   t	   clientRefR_   R9   (    (   R   R   R2   RQ   sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt'   test_protocolGarbageAfterLostConnectionÒ   s    	(	   R   R   R   R)   R+   R:   RO   R`   Rd   (    (    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyR&   P   s   						$	/t   LogObserverMixinc           B   s   e  Z d  Z d „  Z RS(   sL   
    Mixin for L{TestCase} subclasses which want to observe log events.
    c         C   s0   g  } t  j | j ƒ |  j t  j | j ƒ | S(   N(   R   t   addObserverRW   t
   addCleanupt   removeObserver(   R   t   loggedMessages(    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt   observeü   s    (   R   R   R   Rj   (    (    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyRe   ø   s   N(   R   t   gcR    t   weakrefR   t   twisted.pythonR   R   t   twisted.python.reflectR   t   twisted.python.logR   R   R   t   twisted.internet.deferR   R	   t   twisted.internet.protocolR
   R   R   R.   R   R   t   objectR&   Re   (    (    (    sJ   /usr/lib/python2.7/dist-packages/twisted/internet/test/connectionmixins.pyt   <module>   s   	¨