
    Vh                     b    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      Z	 G d d	e      Z
y)
z&
Test code for basic Factory classes.
    N)ProtocolReconnectingClientFactory)Clock)TestCasec                       e Zd ZdZd Zd Zy)FakeConnectorzP
    A fake connector class, to be used to mock connections failed or lost.
    c                      y N selfs    K/home/dcms/DCMS/lib/python3.12/site-packages/twisted/test/test_factories.pystopConnectingzFakeConnector.stopConnecting           c                      y r
   r   r   s    r   connectzFakeConnector.connect   r   r   N)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s    r   r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	ReconnectingFactoryTestsz1
    Tests for L{ReconnectingClientFactory}.
    c                      G d d      }t               }t        |_        |j                  d       |j	                          |j                   |       d       | j                  |j                         y)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                       e Zd Zd Zd Zy)QReconnectingFactoryTests.test_stopTryingWhenConnected.<locals>.NoConnectConnectorc                     t        d      )Nz%Shouldn't be called, we're connected.RuntimeErrorr   s    r   r   z`ReconnectingFactoryTests.test_stopTryingWhenConnected.<locals>.NoConnectConnector.stopConnecting)   s    "#JKKr   c                     t        d      )NzShouldn't be reconnecting.r   r   s    r   r   zYReconnectingFactoryTests.test_stopTryingWhenConnected.<locals>.NoConnectConnector.connect,   s    "#?@@r   N)r   r   r   r   r   r   r   r   NoConnectConnectorr   (   s    LAr   r!   N)r   r   protocolbuildProtocol
stopTryingclientConnectionLostassertFalsecontinueTrying)r   r!   cs      r   test_stopTryingWhenConnectedz5ReconnectingFactoryTests.test_stopTryingWhenConnected!   s\    	A 	A &'
			13T:))*r   c                 .    G fddt               }t               t               _         |       _        j                          | j                  j                  j                         | j                  j                  j                                y)z
        Calling stopTrying on a L{ReconnectingClientFactory} doesn't attempt a
        retry on any active connector.
        c                   "    e Zd ZdZ fdZd Zy)[ReconnectingFactoryTests.test_stopTryingDoesNotReconnect.<locals>.FactoryAwareFakeConnectorFc                 *    j                  | d       y)z
                Behave as though an ongoing connection attempt has now
                failed, and notify the factory of this.
                N)clientConnectionFailed)r   fs    r   r   zjReconnectingFactoryTests.test_stopTryingDoesNotReconnect.<locals>.FactoryAwareFakeConnector.stopConnectingA   s    
 ((t4r   c                     d| _         y)z|
                Record an attempt to reconnect, since this is what we
                are trying to avoid.
                TN)attemptedRetryr   s    r   r   zcReconnectingFactoryTests.test_stopTryingDoesNotReconnect.<locals>.FactoryAwareFakeConnector.connectH   s    
 '+#r   N)r   r   r   r1   r   r   )r/   s   r   FactoryAwareFakeConnectorr,   >   s    "N5+r   r2   N)	r   r   r   clock	connectorr$   r&   r1   getDelayedCalls)r   r2   r/   s     @r   test_stopTryingDoesNotReconnectz8ReconnectingFactoryTests.test_stopTryingDoesNotReconnect8   sl    	+ 	+" &'' 01	 	3340023r   c                     t               }t        j                  t        j                  |            }| j	                  |j
                  |j
                         y)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reconstituteds      r   test_serializeUnusedz-ReconnectingFactoryTests.test_serializeUnused[   s?    
 -.V\\(%;<**M,B,BCr   c                     t               }t               }||_        t        j                  t        j
                  |            }| j                  |j                         y)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   r3   r8   r9   r:   assertIsNone)r   r3   r=   r>   s       r   test_serializeWithClockz0ReconnectingFactoryTests.test_serializeWithClockd   sE    
 ,.V\\(%;<---.r   c                    t               }|j                  t               d       | j                  |j                         t        j                  |      }t        j                  |      }| j                  |j                         | j                  |j                         | j                  |j                  d       | j                  |j                  |j                         | j                  |j                          y)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$   r8   r:   r9   rA   r4   _callIDr;   retriesdelayinitialDelay
assertTruer'   )r   factory
serializedunserializeds       r   $test_deserializationResetsParametersz=ReconnectingFactoryTests.test_deserializationResetsParameterso   s     ,-&&}=**+\\'*
||J/,001,../--q1++W-A-AB334r   c                     t               }t               }||_        |j                  t	               d       | j                  t        |j                        d       y)z
        The clock used by L{ReconnectingClientFactory} can be parametrized, so
        that one can cleanly test reconnections.
        N   )r   r   r3   r%   r   r;   lencalls)r   r3   rJ   s      r   test_parametrizedClockz/ReconnectingFactoryTests.test_parametrizedClock   sE    
 +-$$]_d;U[[)1-r   N)
r   r   r   r   r)   r6   r?   rB   rM   rR   r   r   r   r   r      s'    +.!4FD	/5$
.r   r   )r   r8   twisted.internet.protocolr   r   twisted.internet.taskr   twisted.trial.unittestr   r   r   r   r   r   <module>rV      s0   
  I ' +	 	o.x o.r   