U
    
W[                     @   sh   d Z ddlmZmZ ddlZddlmZ ddlmZ ddl	m
Z
mZ G dd deZG d	d
 d
eZdS )z&
Test code for basic Factory classes.
    )divisionabsolute_importN)TestCase)Clock)ReconnectingClientFactoryProtocolc                   @   s    e Zd ZdZdd Zdd ZdS )FakeConnectorzP
    A fake connector class, to be used to mock connections failed or lost.
    c                 C   s   d S N selfr
   r
   =/usr/lib/python3/dist-packages/twisted/test/test_factories.pystopConnecting   s    zFakeConnector.stopConnectingc                 C   s   d S r	   r
   r   r
   r
   r   connect   s    zFakeConnector.connectN)__name__
__module____qualname____doc__r   r   r
   r
   r
   r   r      s   r   c                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )ReconnectingFactoryTestsz1
    Tests for L{ReconnectingClientFactory}.
    c                 C   sL   G dd dt }t }t|_|d |  || d | |j dS )z
        If a L{ReconnectingClientFactory} has C{stopTrying} called while it is
        connected, it does not subsequently attempt to reconnect if the
        connection is later lost.
        c                   @   s   e Zd Zdd Zdd ZdS )zQReconnectingFactoryTests.test_stopTryingWhenConnected.<locals>.NoConnectConnectorc                 S   s   t dd S )Nz%Shouldn't be called, we're connected.RuntimeErrorr   r
   r
   r   r   ,   s    z`ReconnectingFactoryTests.test_stopTryingWhenConnected.<locals>.NoConnectConnector.stopConnectingc                 S   s   t dd S )NzShouldn't be reconnecting.r   r   r
   r
   r   r   .   s    zYReconnectingFactoryTests.test_stopTryingWhenConnected.<locals>.NoConnectConnector.connectN)r   r   r   r   r   r
   r
   r
   r   NoConnectConnector+   s   r   N)	objectr   r   ZprotocolZbuildProtocol
stopTryingclientConnectionLostassertFalsecontinueTrying)r   r   cr
   r
   r   test_stopTryingWhenConnected%   s    
z5ReconnectingFactoryTests.test_stopTryingWhenConnectedc                    sT   G  fdddt }t  t  _|  _   |  jj |  j  dS )z
        Calling stopTrying on a L{ReconnectingClientFactory} doesn't attempt a
        retry on any active connector.
        c                       s$   e Zd ZdZ fddZdd ZdS )z[ReconnectingFactoryTests.test_stopTryingDoesNotReconnect.<locals>.FactoryAwareFakeConnectorFc                    s     | d dS )z
                Behave as though an ongoing connection attempt has now
                failed, and notify the factory of this.
                N)clientConnectionFailedr   fr
   r   r   C   s    zjReconnectingFactoryTests.test_stopTryingDoesNotReconnect.<locals>.FactoryAwareFakeConnector.stopConnectingc                 S   s
   d| _ dS )z|
                Record an attempt to reconnect, since this is what we
                are trying to avoid.
                TN)attemptedRetryr   r
   r
   r   r   J   s    zcReconnectingFactoryTests.test_stopTryingDoesNotReconnect.<locals>.FactoryAwareFakeConnector.connectN)r   r   r   r"   r   r   r
   r    r
   r   FactoryAwareFakeConnector@   s   r#   N)	r   r   r   clock	connectorr   r   r"   ZgetDelayedCalls)r   r#   r
   r    r   test_stopTryingDoesNotReconnect;   s    z8ReconnectingFactoryTests.test_stopTryingDoesNotReconnectc                 C   s*   t  }tt|}| |j|j dS )z
        A L{ReconnectingClientFactory} which hasn't been used for anything
        can be pickled and unpickled and end up with the same state.
        N)r   pickleloadsdumpsassertEqual__dict__)r   originalreconstitutedr
   r
   r   test_serializeUnused^   s    z-ReconnectingFactoryTests.test_serializeUnusedc                 C   s2   t  }t }||_tt|}| |j dS )z
        The clock attribute of L{ReconnectingClientFactory} is not serialized,
        and the restored value sets it to the default value, the reactor.
        N)r   r   r$   r'   r(   r)   assertIsNone)r   r$   r,   r-   r
   r
   r   test_serializeWithClockh   s
    z0ReconnectingFactoryTests.test_serializeWithClockc                 C   sz   t  }|t d | |j t|}t|}| |j	 | |j
 | |jd | |j|j | |j dS )z
        A L{ReconnectingClientFactory} which is unpickled does not have an
        L{IConnector} and has its reconnecting timing parameters reset to their
        initial values.
        Nr   )r   r   r   Z
addCleanupr   r'   r)   r(   r/   r%   Z_callIDr*   ZretriesZdelayZinitialDelayZ
assertTruer   )r   factoryZ
serializedZunserializedr
   r
   r   $test_deserializationResetsParameterst   s    

z=ReconnectingFactoryTests.test_deserializationResetsParametersc                 C   s6   t  }t }||_|t d | t|jd dS )z
        The clock used by L{ReconnectingClientFactory} can be parametrized, so
        that one can cleanly test reconnections.
        N   )r   r   r$   r   r   r*   lenZcalls)r   r$   r1   r
   r
   r   test_parametrizedClock   s
    z/ReconnectingFactoryTests.test_parametrizedClockN)
r   r   r   r   r   r&   r.   r0   r2   r5   r
   r
   r
   r   r       s   #
r   )r   Z
__future__r   r   r'   Ztwisted.trial.unittestr   Ztwisted.internet.taskr   Ztwisted.internet.protocolr   r   r   r   r   r
   r
   r
   r   <module>   s   