o
    b                     @   sV   d Z ddlZddlmZmZ ddlmZ ddlmZ G dd dZ	G dd	 d	eZ
dS )
z&
Test code for basic Factory classes.
    N)ProtocolReconnectingClientFactory)Clock)TestCasec                   @   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      d S N selfr	   r	   =/usr/lib/python3/dist-packages/twisted/test/test_factories.pystopConnecting      zFakeConnector.stopConnectingc                 C   r   r   r	   r
   r	   r	   r   connect   r   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   sJ   G dd d}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      t d)Nz%Shouldn't be called, we're connected.RuntimeErrorr
   r	   r	   r   r   )      z`ReconnectingFactoryTests.test_stopTryingWhenConnected.<locals>.NoConnectConnector.stopConnectingc                 S   r   )NzShouldn't be reconnecting.r   r
   r	   r	   r   r   ,   r   zYReconnectingFactoryTests.test_stopTryingWhenConnected.<locals>.NoConnectConnector.connectN)r   r   r   r   r   r	   r	   r	   r   NoConnectConnector(   s    r   N)r   r   protocol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   A   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   H   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%   getDelayedCalls)r   r&   r	   r#   r   test_stopTryingDoesNotReconnect8   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'   r0   r1   r	   r	   r   test_serializeWithClockd   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   
addCleanupr   r+   r-   r,   r3   r(   _callIDr.   retriesdelayinitialDelay
assertTruer   )r   factory
serializedunserializedr	   r	   r   $test_deserializationResetsParameterso   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.   lencalls)r   r'   r;   r	   r	   r   test_parametrizedClock   s
   z/ReconnectingFactoryTests.test_parametrizedClockN)
r   r   r   r   r!   r*   r2   r4   r>   rB   r	   r	   r	   r   r      s    #	r   )r   r+   twisted.internet.protocolr   r   twisted.internet.taskr   twisted.trial.unittestr   r   r   r	   r	   r	   r   <module>   s   