ó
[³XMc           @   sÊ   d  Z  d d l 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 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 S(   s&   
Test code for basic Factory classes.
iÿÿÿÿN(   t   TestCase(   t   reactort   defer(   t   Clock(   t   Factoryt   ReconnectingClientFactory(   t   Int16StringReceivert   Inc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s   i  |  _  d  S(   N(   t   msgs(   t   self(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyt   __init__   s    c         C   s   |  j  j d 7_ d  S(   Ni   (   t   factoryt   connections(   R	   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyt   connectionMade   s    c         C   s<   t  j | ƒ \ } } | |  j | <|  j t  j | ƒ ƒ d  S(   N(   t   picklet   loadsR   t
   sendStringt   dumps(   R	   t   msgt   n(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyt   stringReceived   s    c         C   sN   |  j  j j |  j ƒ t |  j  j ƒ |  j  j k rJ |  j  j j d  ƒ n  d  S(   N(	   R   t   allMessagest   appendR   t   lent   goalt   dt   callbackt   None(   R	   t   reason(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyt   connectionLost   s    (   t   __name__t
   __module__R
   R   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyR      s   			t   Outc           B   sR   e  Z e g  e d  ƒ D] Z e d e f ^ q ƒ Z d „  Z d „  Z d „  Z RS(   i
   t   Xc         C   s   t  j j ƒ  |  _ d  S(   N(   R    R   t   copy(   R	   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyR
   )   s    c         C   sA   x: |  j  j ƒ  D]) } |  j t j | |  j  | f ƒ ƒ q Wd  S(   N(   R   t   keysR   R   R   (   R	   t   i(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyR   ,   s    c         C   sj   t  j | ƒ } |  j | =|  j sf |  j j ƒ  |  j j d 8_ |  j j d k rf |  j j ƒ  qf n  d  S(   Ni   i    (   R   R   R   t	   transportt   loseConnectionR   t   howManyTimest
   stopTrying(   R	   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyR   0   s    
	(	   R   R   t   dictt   ranget   xR   R
   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyR    &   s   /		t   FakeConnectorc           B   s    e  Z d  Z d „  Z d „  Z RS(   sP   
    A fake connector class, to be used to mock connections failed or lost.
    c         C   s   d  S(   N(    (   R	   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyt   stopConnecting@   s    c         C   s   d  S(   N(    (   R	   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyt   connectD   s    (   R   R   t   __doc__R-   R.   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyR,   ;   s   	t   ReconnectingFactoryTestCasec           B   sV   e  Z d  Z d „  Z d e _ d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   s1   
    Tests for L{ReconnectingClientFactory}.
    c         C   sË   t  ƒ  } t | _ d | _ g  | _ d | _ t j ƒ  | _ t	 ƒ  } d | _
 | _ t | _ d | _ t j d | ƒ } |  j | j ƒ | j ƒ  j } t j d | | ƒ | j j |  j | | ƒ | j S(   Ni    i   gš™™™™™É?s	   127.0.0.1(   R   R   t   protocolR   R   R   R   t   DeferredR   R   t   initialDelayt   delayR    R'   R   t	   listenTCPt
   addCleanupt   stopListeningt   getHostt   portt
   connectTCPt   addCallbackt   _testStopTrying_1(   R	   t   ft   cR9   t   PORT(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyt   testStopTryingN   s     								i
   c         C   st   |  j  t | j ƒ d d | j ƒ |  j  | j d d | j ƒ |  j  | j t j g d ƒ |  j | j d ƒ d  S(   Ni   s   not enough messages -- %ss-   Number of successful connections incorrect %ds&   stopTrying never called or ineffective(   t   assertEqualsR   R   R   R    R   t   failIft   continueTrying(   R	   t   resR=   R>   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyR<   e   s    c            st   d t  f ‡  f d †  ƒ  Y} t ƒ  ‰  t ƒ  ˆ  _ | ƒ  ˆ  _ ˆ  j ƒ  |  j ˆ  j j ƒ |  j ˆ  j j ƒ  ƒ d S(   s   
        Calling stopTrying on a L{ReconnectingClientFactory} doesn't attempt a
        retry on any active connector.
        t   FactoryAwareFakeConnectorc              s&   e  Z e Z ‡  f d  †  Z d „  Z RS(   c            s   ˆ  j  |  d ƒ d S(   s   
                Behave as though an ongoing connection attempt has now
                failed, and notify the factory of this.
                N(   t   clientConnectionFailedR   (   R	   (   R=   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyR-   w   s    c         S   s   t  |  _ d S(   s|   
                Record an attempt to reconnect, since this is what we
                are trying to avoid.
                N(   t   Truet   attemptedRetry(   R	   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyR.   ~   s    (   R   R   t   FalseRH   R-   R.   (    (   R=   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyRE   t   s   N(	   R,   R   R   t   clockt	   connectorR(   t   assertFalseRH   t   getDelayedCalls(   R	   RE   (    (   R=   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyt   test_stopTryingDoesNotReconnecto   s    	
c         C   s;   t  ƒ  } t j t j | ƒ ƒ } |  j | j | j ƒ d S(   s™   
        A L{ReconnectingClientFactory} which hasn't been used for anything
        can be pickled and unpickled and end up with the same state.
        N(   R   R   R   R   t   assertEqualt   __dict__(   R	   t   originalt   reconstituted(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyt   test_serializeUnused’   s    	c         C   sJ   t  ƒ  } t ƒ  } | | _ t j t j | ƒ ƒ } |  j | j d ƒ d S(   s¢   
        The clock attribute of L{ReconnectingClientFactory} is not serialized,
        and the restored value sets it to the default value, the reactor.
        N(   R   R   RJ   R   R   R   t   assertIdenticalR   (   R	   RJ   RQ   RR   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyt   test_serializeWithClockœ   s
    			c         C   s°   t  ƒ  } | j t ƒ  d ƒ |  j | j ƒ t j | ƒ } t j | ƒ } |  j	 | j
 d ƒ |  j	 | j d ƒ |  j	 | j d ƒ |  j	 | j | j ƒ |  j	 | j t ƒ d S(   s¼   
        A L{ReconnectingClientFactory} which is unpickled does not have an
        L{IConnector} and has its reconnecting timing parameters reset to their
        initial values.
        i    N(   R   RF   R,   R   R6   R(   R   R   R   RO   RK   t   _callIDt   retriesR4   R3   RC   RG   (   R	   R   t
   serializedt   unserialized(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyt$   test_deserializationResetsParameters¨   s    	c         C   sK   t  ƒ  } t ƒ  } | | _ | j t ƒ  d ƒ |  j t | j ƒ d ƒ d S(   s‰   
        The clock used by L{ReconnectingClientFactory} can be parametrized, so
        that one can cleanly test reconnections.
        i   N(	   R   R   RJ   t   clientConnectionLostR,   R   RA   R   t   calls(   R	   RJ   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyt   test_parametrizedClock»   s
    			(   R   R   R/   R@   t   timeoutR<   RN   RS   RU   RZ   R]   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyR0   I   s   			
	#	
		(   R/   R   t   twisted.trial.unittestR    t   twisted.internetR   R   t   twisted.internet.taskR   t   twisted.internet.protocolR   R   t   twisted.protocols.basicR   R   R    t   objectR,   R0   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_factories.pyt   <module>   s   