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   