
    \d%                        d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	m
Z
mZmZmZ ddlmZ ddlmZmZ ddlmZ  G d	 d
ej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z G d dej                   Z! G d dej"                  Z" G d dej                   Z# G d dej                   Z$ G d dej                   Z% G d d ej        ej&                  Z' G d! d"ej                   Z( G d# d$ej                   Z) G d% d&e          Z* G d' d(ej+                  Z, G d) d*ej                   Z-dS )+z
Test code for policies.
    N)StringIO)	InterfaceimplementedByimplementer)addressdeferprotocolreactortask)policies)StringTransport StringTransportWithDisconnection)unittestc                   2    e Zd ZdxZZdZd Zd Zd Zd Z	dS )SimpleProtocolr       c                 f    t          j                    | _        t          j                    | _        d S N)r   Deferred
dConnecteddDisconnectedselfs    :lib/python3.11/site-packages/twisted/test/test_policies.py__init__zSimpleProtocol.__init__   s'    .**"^--r   c                 H    d| _         | j                            d           d S N    )	connectedr   callbackr   s    r   connectionMadezSimpleProtocol.connectionMade   s%      $$$$$r   c                 H    d| _         | j                            d           d S r   )disconnectedr   r!   r   reasons     r   connectionLostzSimpleProtocol.connectionLost!   s'    ##B'''''r   c                 &    | xj         |z  c_         d S r   )bufferr   datas     r   dataReceivedzSimpleProtocol.dataReceived%   s    tr   N)
__name__
__module____qualname__r    r$   r)   r   r"   r'   r,    r   r   r   r      s_          IF. . .% % %( ( (    r   r   c                       e Zd Zd Zd ZdS )SillyFactoryc                     || _         d S r   p)r   r5   s     r   r   zSillyFactory.__init__*   s    r   c                     | j         S r   r4   )r   addrs     r   buildProtocolzSillyFactory.buildProtocol-   s	    vr   N)r-   r.   r/   r   r8   r0   r   r   r2   r2   )   s2              r   r2   c                   *    e Zd ZdZd Zd Zd Zd ZdS )EchoProtocolFc                     d| _         d S NTpausedr   s    r   pauseProducingzEchoProtocol.pauseProducing4   s    r   c                     d| _         d S )NFr=   r   s    r   resumeProducingzEchoProtocol.resumeProducing7   s    r   c                     d S r   r0   r   s    r   stopProducingzEchoProtocol.stopProducing:   s    r   c                 :    | j                             |           d S r   )	transportwriter*   s     r   r,   zEchoProtocol.dataReceived=   s    T"""""r   N)r-   r.   r/   r>   r?   rA   rC   r,   r0   r   r   r:   r:   1   sU        F      # # # # #r   r:   c                       e Zd ZdZeZdS )Serverz8
    A simple server factory using L{EchoProtocol}.
    N)r-   r.   r/   __doc__r:   r	   r0   r   r   rH   rH   A   s          HHHr   rH   c                       e Zd ZdZd Zd ZdS )TestableThrottlingFactoryzH
    L{policies.ThrottlingFactory} using a L{task.Clock} for tests.
    c                 H    t          j        j        | g|R i | || _        dS z
        @param clock: object providing a callLater method that can be used
            for tests.
        @type clock: C{task.Clock} or alike.
        N)r   ThrottlingFactoryr   clockr   rO   argskwargss       r   r   z"TestableThrottlingFactory.__init__N   s3     	"+DB4BBB6BBB


r   c                 8    | j                             ||          S z0
        Forward to the testable clock.
        rO   	callLaterr   periodfuncs      r   rV   z#TestableThrottlingFactory.callLaterW        z##FD111r   Nr-   r.   r/   rI   r   rV   r0   r   r   rK   rK   I   <           2 2 2 2 2r   rK   c                       e Zd ZdZd Zd ZdS )TestableTimeoutFactoryzE
    L{policies.TimeoutFactory} using a L{task.Clock} for tests.
    c                 H    t          j        j        | g|R i | || _        dS rM   )r   TimeoutFactoryr   rO   rP   s       r   r   zTestableTimeoutFactory.__init__c   s3     	(???????


r   c                 8    | j                             ||          S rT   rU   rW   s      r   rV   z TestableTimeoutFactory.callLaterl   rZ   r   Nr[   r0   r   r   r^   r^   ^   r\   r   r^   c                   r    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd ZdS )WrapperTestsz>
    Tests for L{WrappingFactory} and L{ProtocolWrapper}.
    c                     t                      }t          j        |          }|                    t	          j        ddd                    }|                     |j        j        |           dS )zf
        Make sure protocol.factory is the wrapped factory, not the wrapping
        factory.
        TCP	127.0.0.1#   N)	rH   r   WrappingFactoryr8   r   IPv4AddressassertIswrappedProtocolfactory)r   fwfr5   s       r   test_protocolFactoryAttributez*WrapperTests.test_protocolFactoryAttributex   s_    
 HH%a((W0RHHIIa'/33333r   c                     G d dt                     }t          |           G d d                      }t          t          j                   t          j                    }t          j        t          j        d          |          }|                     |                       | 	                    |
                    |j                             dS )z
        The transport wrapper passed to the wrapped protocol's
        C{makeConnection} provides the same interfaces as are provided by the
        original transport.
        c                       e Zd ZdS )=WrapperTests.test_transportInterfaces.<locals>.IStubTransportNr-   r.   r/   r0   r   r   IStubTransportrr              Dr   rt   c                       e Zd ZdS )<WrapperTests.test_transportInterfaces.<locals>.StubTransportNrs   r0   r   r   StubTransportrw      s        Dr   rx   N)r   r   r   r   ProtocolWrapperr	   Protocolrh   makeConnection
assertTrue
providedByrE   )r   rt   rx   protowrappers        r   test_transportInterfacesz%WrapperTests.test_transportInterfaces   s    	 	 	 	 	Y 	 	 	 
^	$	$	 	 	 	 	 	 	 
%	$	 	h.///!##*8+CD+I+I5QQ}}///11%/BBCCCCCr   c                     t                      }t          j        |          }|                     d|                                           dS )z
        L{WrappingFactory.logPrefix} is customized to mention both the original
        factory and the wrapping factory.
        zServer (WrappingFactory)N)rH   r   rh   assertEqual	logPrefix)r   serverrl   s      r   test_factoryLogPrefixz"WrapperTests.test_factoryLogPrefix   sF    
 *6223W5F5F5H5HIIIIIr   c                      G d d          } |            }t          j        |          }|                     d|                                           dS )z
        If the wrapped factory doesn't have a L{logPrefix} method,
        L{WrappingFactory.logPrefix} falls back to the factory class name.
        c                       e Zd ZdS )=WrapperTests.test_factoryLogPrefixFallback.<locals>.NoFactoryNrs   r0   r   r   	NoFactoryr      ru   r   r   zNoFactory (WrappingFactory)N)r   rh   r   r   )r   r   r   rl   s       r   test_factoryLogPrefixFallbackz*WrapperTests.test_factoryLogPrefixFallback   sp    	 	 	 	 	 	 	 	 *62268I8I8K8KLLLLLr   c                     t                      }t          j        |          }|                    t	          j        ddd                    }|                     d |j                               dS )z{
        L{ProtocolWrapper.logPrefix} is customized to mention both the original
        protocol and the wrapper.
        re   rf   rg   zEchoProtocol (ProtocolWrapper)N)rH   r   rh   r8   r   ri   r   r   )r   r   rl   r	   s       r   test_protocolLogPrefixz#WrapperTests.test_protocolLogPrefix   si    
 *622(()<UKQS)T)TUU9;M8;M;O;OPPPPPr   c                 
    G d d          }t                      }||_        t          j        |          }|                    t          j        ddd                    }|                     d |j                               dS )z
        If the wrapped protocol doesn't have a L{logPrefix} method,
        L{ProtocolWrapper.logPrefix} falls back to the protocol class name.
        c                       e Zd ZdS )?WrapperTests.test_protocolLogPrefixFallback.<locals>.NoProtocolNrs   r0   r   r   
NoProtocolr      ru   r   r   re   rf   rg   zNoProtocol (ProtocolWrapper)N)	rH   r	   r   rh   r8   r   ri   r   r   )r   r   r   rl   r	   s        r   test_protocolLogPrefixFallbackz+WrapperTests.test_protocolLogPrefixFallback   s    	 	 	 	 	 	 	 	 $*622(()<UKQS)T)TUU79K9K9M9MNNNNNr   c                     t          j        t          j        t                                t	          j                              }t                      }|                    |           |S )zm
        Return L{policies.ProtocolWrapper} that has been connected to a
        L{StringTransport}.
        )r   ry   rh   rH   r	   rz   r   r{   r   r   rE   s      r   _getWrapperzWrapperTests._getWrapper   sY    
 *$VXX..0A0C0C
 
 $%%	y)))r   c                     |                                  }|                     |                                |j                                                   dS )zk
        L{policies.ProtocolWrapper.getHost} calls C{getHost} on the underlying
        transport.
        N)r   r   getHostrE   r   r   s     r   test_getHostzWrapperTests.test_getHost   J    
 ""$$**G,=,E,E,G,GHHHHHr   c                     |                                  }|                     |                                |j                                                   dS )zk
        L{policies.ProtocolWrapper.getPeer} calls C{getPeer} on the underlying
        transport.
        N)r   r   getPeerrE   r   s     r   test_getPeerzWrapperTests.test_getPeer   r   r   c                     |                                  }t                      }|                    |d           |                     |j        j        |           |                     |j        j                   dS )z}
        L{policies.ProtocolWrapper.registerProducer} calls C{registerProducer}
        on the underlying transport.
        TN)r   objectregisterProducerrj   rE   producerr|   	streamingr   r   r   s      r   test_registerProducerz"WrapperTests.test_registerProducer   sl    
 ""$$88  4000g'0(;;;)344444r   c                    |                                  }t                      }|                    |d           |                                 |                     |j        j                   |                     |j        j                   dS )z
        L{policies.ProtocolWrapper.unregisterProducer} calls
        C{unregisterProducer} on the underlying transport.
        TN)r   r   r   unregisterProducerassertIsNonerE   r   r   r   s      r   test_unregisterProducerz$WrapperTests.test_unregisterProducer   s    
 ""$$88  4000""$$$'+4555'+566666r   c                     |                                  }g fd|j        _        |                                 |                     dg           dS )zw
        L{policies.ProtocolWrapper.stopConsuming} calls C{stopConsuming} on
        the underlying transport.
        c                  .                          d          S r<   appendresults   r   <lambda>z1WrapperTests.test_stopConsuming.<locals>.<lambda>  s    &--2E2E r   TN)r   rE   stopConsumingr   )r   r   r   s     @r   test_stopConsumingzWrapperTests.test_stopConsuming  s_    
 ""$$*E*E*E*E'$(((((r   c                     g  G fdd          }t          j         |                      }t                      }|                    |           |                     |g           dS )z}
        L{policies.WrappingFactory.startedConnecting} calls
        C{startedConnecting} on the underlying factory.
        c                       e Zd Z fdZdS )4WrapperTests.test_startedConnecting.<locals>.Factoryc                 2                         |           d S r   r   )r   	connectorr   s     r   startedConnectingzFWrapperTests.test_startedConnecting.<locals>.Factory.startedConnecting  s    i(((((r   N)r-   r.   r/   r   r   s   r   Factoryr     s.        ) ) ) ) ) ) )r   r   N)r   rh   r   r   r   )r   r   r   r   r   s       @r   test_startedConnectingz#WrapperTests.test_startedConnecting  s    
 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) *779955HH	!!),,,)-----r   c                     g  G fdd          }t          j         |                      }t                      }t                      }|                    ||           |                     ||fg           dS )z
        L{policies.WrappingFactory.clientConnectionLost} calls
        C{clientConnectionLost} on the underlying factory.
        c                       e Zd Z fdZdS )7WrapperTests.test_clientConnectionLost.<locals>.Factoryc                 6                         ||f           d S r   r   r   r   r&   r   s      r   clientConnectionLostzLWrapperTests.test_clientConnectionLost.<locals>.Factory.clientConnectionLost$       y&122222r   N)r-   r.   r/   r   r   s   r   r   r   #  .        3 3 3 3 3 3 3r   r   N)r   rh   r   r   r   r   r   r   r   r&   r   s        @r   test_clientConnectionLostz&WrapperTests.test_clientConnectionLost  s    
 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 *779955HH	$$Y7779f"5!677777r   c                     g  G fdd          }t          j         |                      }t                      }t                      }|                    ||           |                     ||fg           dS )z
        L{policies.WrappingFactory.clientConnectionFailed} calls
        C{clientConnectionFailed} on the underlying factory.
        c                       e Zd Z fdZdS )9WrapperTests.test_clientConnectionFailed.<locals>.Factoryc                 6                         ||f           d S r   r   r   s      r   clientConnectionFailedzPWrapperTests.test_clientConnectionFailed.<locals>.Factory.clientConnectionFailed5  r   r   N)r-   r.   r/   r   r   s   r   r   r   4  r   r   r   N)r   rh   r   r   r   r   s        @r   test_clientConnectionFailedz(WrapperTests.test_clientConnectionFailed-  s    
 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 *779955HH	&&y&9999f"5!677777r   c                 r   t          j        t          j        t                                t	          j                              }t                      }||_        |                    |           |                     |j	                   |
                                 |                     |j	                   dS )z
        L{policies.ProtocolWrapper.connectionLost} sets C{wrappedProtocol} to
        C{None} in order to break reference cycle between wrapper and wrapped
        protocols.
        :return:
        N)r   ry   rh   rH   r	   rz   r   r{   assertIsNotNonerk   loseConnectionr   r   s      r   test_breakReferenceCyclez%WrapperTests.test_breakReferenceCycle>  s     *$VXX..0A0C0C
 
 566	$	y)))W4555  """'122222r   N)r-   r.   r/   rI   ro   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r0   r   r   rc   rc   s   s#        4 4 4D D D:J J JM M MQ Q QO O O
 
 
I I II I I	5 	5 	5
7 
7 
7	) 	) 	). . . 8 8 8"8 8 8"3 3 3 3 3r   rc   c                       e Zd Zd Zd ZdS )rh   c                     |S r   r0   )r   rm   r5   s      r   r	   zWrappingFactory.protocolR  s    r   c                 x    t           j                            |            | j                            d            d S r   )r   rh   startFactorydeferredr!   r   s    r   r   zWrappingFactory.startFactoryU  s5     --d333t$$$$$r   N)r-   r.   r/   r	   r   r0   r   r   rh   rh   Q  s2          % % % % %r   rh   c                   *    e Zd ZdZd Zd Zd Zd ZdS )ThrottlingTestsz2
    Tests for L{policies.ThrottlingFactory}.
    c                    	
 t                      }d t          d          D             \  	
t          j        |d          t	                    }t          j                    |_        t          j	        d|d          
                                j        	
fd}	
 fd}	fd	}fd
} fd}
fd}|j                            |           |j                            |           |j                            |           |j                            |           |j                            |           |j                            |           |j        S )z
        Full test using a custom server limiting number of connections.

        FIXME: https://twistedmatrix.com/trac/ticket/10012
        This is a flaky test.
        c              3   2   K   | ]}t                      V  d S r   )r   ).0is     r   	<genexpr>z-ThrottlingTests.test_limit.<locals>.<genexpr>g  s(      ==q.**======r         r   rf   )	interfacec                     t          j        dt                               j                            fd           j                            fd           j        S )Nrf   c                 J    t          j        dt                              S Nrf   r
   
connectTCPr2   )rc2ns    r   r   zAThrottlingTests.test_limit.<locals>._connect123.<locals>.<lambda>s      ',[!\"=M=MNN r   c                 J    t          j        dt                              S r   r   )r   c3r   s    r   r   zAThrottlingTests.test_limit.<locals>._connect123.<locals>.<lambda>v  r   r   )r
   r   r2   r   addCallbackr   )resultsc1r   r   r   s    r   _connect123z/ThrottlingTests.test_limit.<locals>._connect123p  s    {A|B/?/?@@@M%%NNNNN   M%%NNNNN   ##r   c                                         d fD             g d                                d fD             g d                                t          j                                                  d           | S )Nc                     g | ]	}|j         
S r0   )r    r   cs     r   
<listcomp>zAThrottlingTests.test_limit.<locals>._check123.<locals>.<listcomp>{  s    @@@aak@@@r   )r   r   r   c                     g | ]	}|j         
S r0   )r$   r   s     r   r   zAThrottlingTests.test_limit.<locals>._check123.<locals>.<listcomp>|  s    CCCanCCCr   )r   r   r   r   )r   len	protocolskeys)r   r   r   r   r   tServers    r   	_check123z-ThrottlingTests.test_limit.<locals>._check123z  s    @@BB<@@@)))LLLCCr2rlCCCYYYOOOS!2!7!7!9!9::A>>>Nr   c                 D    j                                          j        S r   )rE   r   r   )r   r   s    r   _lose1z*ThrottlingTests.test_limit.<locals>._lose1  s     L'')))##r   c                 X    t          j        dt                               j        S r   )r
   r   r2   r   )r   c4r   s    r   	_connect4z-ThrottlingTests.test_limit.<locals>._connect4  s(    {A|B/?/?@@@= r   c                 t                         j        d                                j        d           | S )Nr   r   )r   r    r$   )r   r   r   s    r   _check4z+ThrottlingTests.test_limit.<locals>._check4  s8    R\1---R_a000Nr   c                     fD ]}|j                                          t          j        t          j        j                  j        j        g          S r   )rE   r   r   DeferredListmaybeDeferredstopListeningr   )r   r   r   r   r5   s     r   _cleanupz,ThrottlingTests.test_limit.<locals>._cleanup  sc    V - -**,,,,%'88$$  r   )rH   ranger   rN   rh   r   r   r   r
   	listenTCPr   portr   )r   r   wrapTServerr   r   r   r   r   r  r   r   r   r   r   r5   r   s   `        @@@@@@@r   
test_limitzThrottlingTests.test_limit_  s    ==E!HH===BB,VQ77%g..$~// aDDDIIKK	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$	 	 	 	 	 	 	 	 		$ 	$ 	$ 	$ 	$
	! 	! 	! 	! 	! 	!	 	 	 	 	 	
		 		 		 		 		 		 		 	((555((333((000((333((111((222##r   c                    t                      }t          t          j                    |          }|                    t          j        ddd                    }t                      }||_         |j	        |            |j
        dgdz             |                     |                                d           |                     |j        d           dS )	zZ
        L{ThrottlingProtocol.writeSequence} is called on the underlying factory.
        re   rf   r      bytesr   s   bytesbytesbytesbytes   N)rH   rK   r   Clockr8   r   ri   r   r	   r{   writeSequencer   valuewrittenThisSecond)r   r   r   r	   rE   s        r   test_writeSequencez"ThrottlingTests.test_writeSequence  s     +DJLL&AA(()<UKQR)S)STT466	%		***zA~...**,CDDD2B77777r   c                    t                      }t          t          j                    |d          }|                    t          j        ddd                    }t                      }||_        |	                    |           |j
        |_        |                    d           |                    d           |                     |                                d           |                     |j        d	           |                     |j
        j                   |j                            d
           |                     |j        d           |                     |j
        j                   |j                            d
           |                     |j        d           |                     |j
        j                   dS )ze
        Check the writeLimit parameter: write data, and check for the pause
        status.
        
   )
writeLimitre   rf   r   
   0123456789
   abcdefghij   0123456789abcdefghijr	  ?N)rH   rK   r   r
  r8   r   ri   r   r	   r{   rk   r   r,   r   r  r  assertFalser>   rO   advancer|   r   r   r   r  trs        r   test_writeLimitzThrottlingTests.test_writeLimit  s   
 +DJLL&RPPP$$W%8Q%O%OPP-//B,-(((-(((%<===2B777-4555
 	d###2A666,3444d###2A666-455555r   c                 j   t                      }t          t          j                    |d          }|                    t          j        ddd                    }t                      }||_        |	                    |           |
                    d           |
                    d           |                     |                                d           |                     |j        d	           |j                            d
           |                     |j        d           |                     |j        d           |j                            d
           |                     |j        d           |                     |j        d           |                                 |
                    d           |
                    d           |                     |                                d           |                     |j        d	           |j                            d
           |                     |j        d           |                     |j        d           |j                            d
           |                     |j        d           |                     |j        d           dS )zb
        Check the readLimit parameter: read data and check for the pause
        status.
        r  )	readLimitre   rf   r   r  r  r  r	  r  r>   	producingN)rH   rK   r   r
  r8   r   ri   r   r	   r{   r,   r   r  readThisSecondrO   r  producerStateclearr  s        r   test_readLimitzThrottlingTests.test_readLimit  sb   
 +DJLL&BOOO$$W%8Q%O%OPP-//B-(((-(((%<===/444d###/333)8444d###/333);777



-(((-(((%<===/444d###/333)8444d###/333);77777r   N)r-   r.   r/   rI   r  r  r  r!  r0   r   r   r   r   Z  s^         @$ @$ @$D8 8 86 6 6<%8 %8 %8 %8 %8r   r   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )TimeoutProtocolTestsz0
    Tests for L{policies.TimeoutProtocol}.
    c                 6   t          j                    }t          j                    }t          |_        t          ||d          }|                    t          j        ddd                    }t                      }||_        |
                    |           ||fS )z
        Helper to set up an already connected protocol to be tested.

        @return: A new protocol with its attached clock.
        @rtype: Tuple of (L{policies.TimeoutProtocol}, L{task.Clock})
        Nre   rf   90  )r   r
  r	   ServerFactoryr   r^   r8   r   ri   r   r{   )r   rO   wrappedFactoryrl   r~   rE   s         r   getProtocolAndClockz(TimeoutProtocolTests.getProtocolAndClock  s     
!/11"0(EE%%g&9%e&T&TUU466	"	Y'''u~r   c                    |                                  \  }}|                    d           |                     |j                   |                     |j        j                   |                    d           |                                 | 	                    |j                   |                    d           |                     |j        j                   dS )z2
        Will cancel the ongoing timeout.
           r   N)
r(  
setTimeoutr   timeoutCallr  rk   r$   r  cancelTimeoutr   r   sutrO   s      r   test_cancelTimeoutz'TimeoutProtocolTests.test_cancelTimeout  s     --//
UqS_---,9:::a#/*** 	a,9:::::r   c                     |                                  \  }}|                     |j                   |                                 |                     |j        j                   dS )z<
        Does nothing if no timeout is already set.
        N)r(  r   r,  r-  r  rk   r$   r.  s      r   test_cancelTimeoutNoTimeoutz0TimeoutProtocolTests.test_cancelTimeoutNoTimeout$  sc     --//
U#/*** 	,9:::::r   c                     |                                  \  }}|j        }|                    d           |                    d           |                     |j                   |                                 dS )z@
        Does nothing if no timeout is already reached.
        r*  N)r(  rk   r+  r  r|   r$   r-  )r   r/  rO   wrappedProtos       r   test_cancelTimeoutAlreadyCalledz4TimeoutProtocolTests.test_cancelTimeoutAlreadyCalled0  st     --//
U*qa1222 	r   c                     |                                  \  }}|                    d           |j                                         |                                 |                     |j        j                   dS )zm
        Does nothing if the timeout is cancelled from another part.
        Ex from another thread.
        r*  N)r(  r+  r,  cancelr-  r  rk   r$   r.  s      r   "test_cancelTimeoutAlreadyCancelledz7TimeoutProtocolTests.test_cancelTimeoutAlreadyCancelled>  ss    
 --//
Uq    	,9:::::r   N)	r-   r.   r/   rI   r(  r0  r2  r5  r8  r0   r   r   r#  r#    si           ,; ; ;&
; 
; 
;  ; ; ; ; ;r   r#  c                   *    e Zd ZdZd Zd Zd Zd ZdS )TimeoutFactoryTestsz/
    Tests for L{policies.TimeoutFactory}.
    c                    t          j                    | _        t          j                    }t
          |_        t          | j        |d          | _        | j                            t          j
        ddd                    | _        t                      | _        | j        | j        _        | j                            | j                   | j        j        | _        dS )zq
        Create a testable, deterministic clock, and a set of
        server factory/protocol/transport.
        r*  re   rf   r%  N)r   r
  rO   r	   r&  r   r^   rl   r8   r   ri   r~   r   rE   r{   rk   r4  )r   r'  s     r   setUpzTimeoutFactoryTests.setUpS  s    
 Z\\
!/11"0-dj.!LL\//{E::
 

 :;;"&*
!!$.111 J6r   c                     | j                             g d           |                     | j        j                   | j                             ddg           |                     | j        j                   dS )z
        Make sure that when a TimeoutFactory accepts a connection, it will
        time out that connection if no data is read or written within the
        timeout period.
        )              ?      ?r@  g?r>  皙?N)rO   pumpr  r4  r$   r|   r   s    r   test_timeoutz TimeoutFactoryTests.test_timeoutd  sq     	
111222*7888 	
c
###)677777r   c                 N   | j                             g d           |                     | j        j                   | j                            d           | j                             g d           |                     | j        j                   | j                            dgdz             | j                             g d           |                     | j        j                   | j                             ddg           |                     | j        j                   dS )	z
        Make sure that writing data to a transport from a protocol
        constructed by a TimeoutFactory resets the timeout countdown.
        )r>  r?  r@     bytes bytes bytesr>  r@  r@  r  r*  r>  g       @N)	rO   rB  r  r4  r$   r~   rF   r  r|   r   s    r   test_sendAvoidsTimeoutz*TimeoutFactoryTests.test_sendAvoidsTimeoutr  s    	
(((*7888 	
-... 	
(((*7888 	
  (a000 	
(((*7888 	
c
###)677777r   c                    | j                             g d           |                     | j        j                   | j                            d           | j                             g d           |                     | j        j                   | j                             g d           |                     | j        j                   dS )zR
        Make sure that receiving data also resets the timeout countdown.
        )r>  r@  r?  rE  rF  N)rO   rB  r  r4  r$   r~   r,   r|   r   s    r   test_receiveAvoidsTimeoutz-TimeoutFactoryTests.test_receiveAvoidsTimeout  s    
 	
(((*7888 	
 4555 	
(((*7888 	
((()677777r   N)r-   r.   r/   rI   r<  rC  rG  rI  r0   r   r   r:  r:  N  sZ         7 7 7"8 8 88 8 888 8 8 8 8r   r:  c                   @    e Zd ZdZdZdZd Zd Zd ZddZ	d	 Z
d
 ZdS )TimeoutTesterz
    A testable protocol with timeout facility.

    @ivar timedOut: set to C{True} if a timeout has been detected.
    @type timedOut: C{bool}
    r*  Fc                     || _         dS )zF
        Initialize the protocol with a C{task.Clock} object.
        N)rO   )r   rO   s     r   r   zTimeoutTester.__init__  s     


r   c                 :    |                      | j                   dS )z3
        Upon connection, set the timeout.
        N)r+  timeOutr   s    r   r"   zTimeoutTester.connectionMade  s     	%%%%%r   c                 n    |                                   t          j                            | |           dS )z,
        Reset the timeout on data.
        N)resetTimeoutr	   rz   r,   r*   s     r   r,   zTimeoutTester.dataReceived  s5     	&&tT22222r   Nc                 0    |                      d           dS )zD
        On connection lost, cancel all timeout operations.
        N)r+  r%   s     r   r'   zTimeoutTester.connectionLost  s     	r   c                     d| _         dS )zX
        Flags the timedOut variable to indicate the timeout of the connection.
        TN)timedOutr   s    r   timeoutConnectionzTimeoutTester.timeoutConnection  s     r   c                 0     | j         j        ||g|R i |S )zD
        Override callLater to use the deterministic clock.
        rU   )r   timeoutrY   rQ   rR   s        r   rV   zTimeoutTester.callLater  s+     $tz#GTCDCCCFCCCr   r   )r-   r.   r/   rI   rN  rS  r   r"   r,   r'   rT  rV   r0   r   r   rK  rK    s          GH  & & &3 3 3     D D D D Dr   rK  c                   B    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
S )TimeoutMixinTestsz-
    Tests for L{policies.TimeoutMixin}.
    c                 h    t          j                    | _        t          | j                  | _        dS )zY
        Create a testable, deterministic clock and a C{TimeoutTester} instance.
        N)r   r
  rO   rK  r~   r   s    r   r<  zTimeoutMixinTests.setUp  s&     Z\\
"4:..


r   c                     | j                             d           |                     t          | j        j                  d           dS )z
        Test that the callLater of the clock is used instead of
        L{reactor.callLater<twisted.internet.interfaces.IReactorTime.callLater>}
        r  r   N)r~   r+  r   r   rO   callsr   s    r   test_overriddenCallLaterz*TimeoutMixinTests.test_overriddenCallLater  sC    
 	
b!!!TZ-..22222r   c                 >   | j                             t                                 | j                            g d           |                     | j         j                   | j                            ddg           |                     | j         j                   dS )zq
        Check that the protocol does timeout at the time specified by its
        C{timeOut} attribute.
        r   r?  r@  r@  r   r@  N)r~   r{   r   rO   rB  r  rS  r|   r   s    r   rC  zTimeoutMixinTests.test_timeout  s    
 	
!!/"3"3444 	
***+++,---
C!!!
+,,,,,r   c                    | j                             t                                 | j                            g d           |                     | j         j                   | j                             d           | j                            g d           |                     | j         j                   | j                            ddg           |                     | j         j                   dS )zV
        Check that receiving data is delaying the timeout of the connection.
        r^  s   hello there)r   r@  r@  r?  r   r@  N)	r~   r{   r   rO   rB  r  rS  r,   r|   r   s    r   test_noTimeoutz TimeoutMixinTests.test_noTimeout  s     	
!!/"3"3444
***+++,---
///
***+++,---
C!!!
+,,,,,r   c                    d| j         _        | j                             t                                 | j                             d           |                     | j         j        d           | j                            ddg           |                     | j         j	                   | j                            ddg           | 
                    | j         j	                   dS )zy
        Check that setting a new value for timeout cancel the previous value
        and install a new timeout.
        Nr   r   g?rA  )r~   rN  r{   r   r+  r   rO   rB  r  rS  r|   r   s    r   test_resetTimeoutz#TimeoutMixinTests.test_resetTimeout  s    
 "

!!/"3"3444
a   +Q///
C!!!,---
C!!!
+,,,,,r   c                 R   d| j         _        | j                             t                                 | j                             d           |                     | j         j                   | j                            g d           |                     | j         j	                   dS )zO
        Setting the timeout to L{None} cancel any timeout operations.
           N)r   rd  rd  rd  )
r~   rN  r{   r   r+  r   rO   rB  r  rS  r   s    r   r0  z$TimeoutMixinTests.test_cancelTimeout  s     

!!/"3"3444
d###$*,---
%%%,-----r   c                    d| j         _        |                     | j                             d          d           |                     | j                             d          d           |                     | j                             d                     |                     | j         j        d           | j                             d           dS )zM
        setTimeout should return the value of the previous timeout.
        rd  r  Nr   )r~   rN  r   r+  r   r   s    r   test_setTimeoutReturnz'TimeoutMixinTests.test_setTimeoutReturn   s     
..r22A666..t44b999$*//22333+Q/// 	
d#####r   c                 L   | j                             d           | j                                        d                                          |                     | j         j                   | j                             d           |                     | j         j                   dS )z
        When the timeout was already cancelled from an external place,
        calling setTimeout with C{None} to explicitly cancel it will clean
        up the timeout without raising any exception.
        r*  r   N)r~   r+  rO   getDelayedCallsr7  r   rN  r   r   s    r   %test_setTimeoutCancleAlreadyCancelledz7TimeoutMixinTests.test_setTimeoutCancleAlreadyCancelled.  s     	
a    	
""$$Q'..000TZ/000
d###$*,-----r   N)r-   r.   r/   rI   r<  r\  rC  r`  rb  r0  rf  ri  r0   r   r   rX  rX    s         / / /3 3 3- - -- - -- - - . . .$ $ $. . . . .r   rX  c                       e Zd ZdZd Zd ZdS )!LimitTotalConnectionsFactoryTestsz/Tests for policies.LimitTotalConnectionsFactoryc                    t          j                    }t          j        |_        |                     d|j                   |                    d           }|                     d|j                   |                    d           }|                     d|j                   |                    d            |                     d|j                   |                    d            |                     d|j                   d S )Nr   r   r   )r   LimitTotalConnectionsFactoryr	   rz   r   connectionCountr8   r'   )r   rl   p1p2s       r   testConnectionCountingz8LimitTotalConnectionsFactoryTests.testConnectionCountingB  s    799#, 	G3444 ""4((G3444""4((G3444 	$G3444
$G344444r   c                 <   t          j                    t          j        _        d_                            d           }|                     |           |                     dj                   | 	                                        d                      |                     dj                    G fddt          j                  }|_
        d_                            d           }|                    d            |                     j                   |                     dj                   |                    d            |                     dj                   |                    d            |                     dj                   d S )Nr   c                       e Zd Z fdZdS )RLimitTotalConnectionsFactoryTests.testConnectionLimiting.<locals>.OverflowProtocolc                     d_         d S r<   )
overflowed)r   rl   s    r   r"   zaLimitTotalConnectionsFactoryTests.testConnectionLimiting.<locals>.OverflowProtocol.connectionMadeh  s    %)"""r   N)r-   r.   r/   r"   )rl   s   r   OverflowProtocolrt  g  s.        * * * * * * *r   rw  Fr   r   )r   rm  r	   rz   connectionLimitr8   r   r   rn  r   overflowProtocolrv  r{   r|   r'   )r   r5   rw  oprl   s       @r   testConnectionLimitingz8LimitTotalConnectionsFactoryTests.testConnectionLimitingV  s   799#,"# !!$''QG3444 	'//55666G3444	* 	* 	* 	* 	* 	* 	*x0 	* 	* 	* $4 "
 ""4((
$*+++G3444 	
G3444
$G344444r   N)r-   r.   r/   rI   rq  r{  r0   r   r   rk  rk  ?  s8        995 5 5($5 $5 $5 $5 $5r   rk  c                       e Zd Zd ZdS )WriteSequenceEchoProtocolc                     |                     d          dk    r| j                            |g           d S t                              | |           d S )Ns   vector!)findrE   r  r:   r,   )r   bytess     r   r,   z&WriteSequenceEchoProtocol.dataReceived~  sS    ::j!!R''N((%11111%%dE22222r   N)r-   r.   r/   r,   r0   r   r   r}  r}  }  s#        3 3 3 3 3r   r}  c                       e Zd ZdZd ZdS )TestLoggingFactoryNc                 X    | j         
J d            t                      | _         | j         S )Nzopen() called too many times)openFiler   )r   names     r   openzTestLoggingFactory.open  s,    }$$&D$$$ 

}r   )r-   r.   r/   r  r  r0   r   r   r  r    s(        H    r   r  c                   $    e Zd ZdZd Zd Zd ZdS )LoggingFactoryTestsz6
    Tests for L{policies.TrafficLoggingFactory}.
    c                    t                      }t          |_        t                      }t	          |d          }|                    d          }||_        |                    |           |j                                        }| 	                    d|           | 
                    |                                           |                    d           |j                                        }| 	                    d                    d          |           | 	                    d                    d          |           |                     |                                d           |                                 |                    d           |j                                        }| 	                    d                    dg          |           |                     |                                d           |                                 |j                                        }| 	                    d	|           d
S )zQ
        Check the output produced by L{policies.TrafficLoggingFactory}.
        test1.2.3.4i.  *s   here are some bytesz	C 1: {!r}z	S 1: {!r}s"   prepare for vector! to the extremez
SV 1: {!r}ConnectionDoneN)rH   r}  r	   r   r  r8   r{   r  getvalueassertInr  r  r,   formatr   r   r   )r   r'  trm   r5   vs         r   test_thingsGetLoggedz(LoggingFactoryTests.test_thingsGetLogged  s     ";,..~v66OO-..
	J!!c1###	-...J!!k(()?@@!DDDk(()?@@!DDD$:;;;					<===J!!l))+P*QRRTUVVV$IJJJ	J!!&*****r   c                    t                      }t          |d          }|                     |j        d           |                    d           |                     |j        d           d|_        |                    d           |                     |j        d           |                                 |                     |j        d           dS )zG
        Test counter management with the resetCounter method.
        r  r   r  r   N)r  i/  r   )rH   r  r   _counterr8   r  resetCounter)r   r'  rm   s      r   test_counterz LoggingFactoryTests.test_counter  s      ~v66Q'''	)***Q'''
	)***Q'''	Q'''''r   c                   	 g g 		fd}|                      t          d|           t          j                    }t          |_        t          j        |d          }|                    t          j	        ddd                    }|                    t          j	        ddd                    }di f}d	i f}| 
                    ||g           | 
                    |j        |j        g	           d
S )z
        When the L{policies.TrafficLoggingFactory} builds a protocol, it
        automatically opens a unique log file for that protocol and attaches
        the logfile to the built protocol.
        c                                           | |f           t                      }| d         |_                             |           |S )zX
            Mock for the open call to prevent actually opening a log file.
            r   )r   r   r  )rQ   rR   io
open_callsopen_rvaluess      r   mocked_openzULoggingFactoryTests.test_loggingFactoryOpensLogfileAutomatically.<locals>.mocked_open  sJ     tVn---B1gBG###Ir   r  r  re   rf   r%  i:0  )ztest-1w)ztest-2r  N)patchbuiltinsr	   r&  r   r   TrafficLoggingFactoryr8   r   ri   r   logfile)
r   r  r'  rl   first_protosecond_proto
first_callsecond_callr  r  s
           @@r   ,test_loggingFactoryOpensLogfileAutomaticallyz@LoggingFactoryTests.test_loggingFactoryOpensLogfileAutomatically  s    
	 	 	 	 	 	 	

8V[111!/11"00HH++{E::
 
 ,,{E::
 
 &r*
&+*k2J???+-|/CDlSSSSSr   N)r-   r.   r/   rI   r  r  r  r0   r   r   r  r    sQ          +  +  +D( ( ("$T $T $T $T $Tr   r  ).rI   r  r  r   zope.interfacer   r   r   twisted.internetr   r   r	   r
   r   twisted.protocolsr   twisted.test.proto_helpersr   r   twisted.trialr   rz   r   ClientFactoryr2   r:   r&  rH   rN   rK   r`   r^   TestCaserc   rh   r   r#  r:  TimeoutMixinrK  rX  rk  r}  r  r  r  r0   r   r   <module>r     s   
        @ @ @ @ @ @ @ @ @ @ D D D D D D D D D D D D D D & & & & & & X X X X X X X X " " " " " "    X&   *    8)   # # # # #8$ # # #     X#   2 2 2 2 2 : 2 2 2*2 2 2 2 2X4 2 2 2*[3 [3 [3 [3 [38$ [3 [3 [3|% % % % %h. % % %Y8 Y8 Y8 Y8 Y8h' Y8 Y8 Y8xU; U; U; U; U;8, U; U; U;pR8 R8 R8 R8 R8(+ R8 R8 R8j.D .D .D .D .DH%x'< .D .D .Dbh. h. h. h. h.) h. h. h.V;5 ;5 ;5 ;5 ;5(9 ;5 ;5 ;5|3 3 3 3 3 3 3 3    7   \T \T \T \T \T(+ \T \T \T \T \Tr   