
    Vh                     @   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mZ ddl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j4                        Z G d dej8                        Z G d dej<                        Z G d dej@                        Z! G d dejD                        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jL                        Z' G d! d"ej@                        Z( G d# d$ej@                        Z) G d% d&e      Z* G d' d(ejV                        Z, G d) d*ej@                        Z-y)+z
Test code for policies.
    N)StringIO)	InterfaceimplementedByimplementer)addressdeferprotocolreactortask)StringTransport StringTransportWithDisconnection)policies)unittestc                   0    e Zd ZdxZZdZd Zd Zd Zd Z	y)SimpleProtocolr       c                 h    t        j                         | _        t        j                         | _        y N)r   Deferred
dConnecteddDisconnectedselfs    J/home/dcms/DCMS/lib/python3.12/site-packages/twisted/test/test_policies.py__init__zSimpleProtocol.__init__   s    ..*"^^-r   c                 H    d| _         | j                  j                  d       y N    )	connectedr   callbackr   s    r   connectionMadezSimpleProtocol.connectionMade   s      $r   c                 H    d| _         | j                  j                  d       y r   )disconnectedr   r!   r   reasons     r   connectionLostzSimpleProtocol.connectionLost    s    ##B'r   c                 .    | xj                   |z  c_         y 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y)SillyFactoryc                     || _         y r   p)r   r5   s     r   r   zSillyFactory.__init__)   s	    r   c                     | j                   S r   r4   )r   addrs     r   buildProtocolzSillyFactory.buildProtocol,   s    vvr   N)r-   r.   r/   r   r8   r0   r   r   r2   r2   (   s    r   r2   c                   (    e Zd ZdZd Zd Zd Zd Zy)EchoProtocolFc                     d| _         y NTpausedr   s    r   pauseProducingzEchoProtocol.pauseProducing3   s	    r   c                     d| _         y )NFr=   r   s    r   resumeProducingzEchoProtocol.resumeProducing6   s	    r   c                      y r   r0   r   s    r   stopProducingzEchoProtocol.stopProducing9   s    r   c                 :    | j                   j                  |       y 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:   0   s    F#r   r:   c                       e Zd ZdZeZy)Serverz8
    A simple server factory using L{EchoProtocol}.
    N)r-   r.   r/   __doc__r:   r	   r0   r   r   rH   rH   @   s     Hr   rH   c                       e Zd ZdZd Zd Zy)TestableThrottlingFactoryzH
    L{policies.ThrottlingFactory} using a L{task.Clock} for tests.
    c                 X    t        j                  j                  | g|i | || _        y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__M   s)     	""++DB4B6B
r   c                 :    | j                   j                  ||      S z0
        Forward to the testable clock.
        rO   	callLaterr   periodfuncs      r   rV   z#TestableThrottlingFactory.callLaterV        zz##FD11r   Nr-   r.   r/   rI   r   rV   r0   r   r   rK   rK   H       2r   rK   c                       e Zd ZdZd Zd Zy)TestableTimeoutFactoryzE
    L{policies.TimeoutFactory} using a L{task.Clock} for tests.
    c                 X    t        j                  j                  | g|i | || _        yrM   )r   TimeoutFactoryr   rO   rP   s       r   r   zTestableTimeoutFactory.__init__b   s)     	((???
r   c                 :    | j                   j                  ||      S rT   rU   rW   s      r   rV   z TestableTimeoutFactory.callLaterk   rZ   r   Nr[   r0   r   r   r^   r^   ]   r\   r   r^   c                   p    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y)WrapperTestsz>
    Tests for L{WrappingFactory} and L{ProtocolWrapper}.
    c                     t               }t        j                  |      }|j                  t	        j
                  ddd            }| j                  |j                  j                  |       y)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_protocolFactoryAttributew   sT    
 H%%a(W00RHIa''//3r   c                 ~    G d dt               }t        |       G d d             }t        t        j                         t        j                         }t        j                  t        j                  d      |      }|j                   |              | j                  |j                  |j                               y)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y)=WrapperTests.test_transportInterfaces.<locals>.IStubTransportNr-   r.   r/   r0   r   r   IStubTransportrr          r   rt   c                       e Zd Zy)<WrapperTests.test_transportInterfaces.<locals>.StubTransportNrs   r0   r   r   StubTransportrw      s    r   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+C+CD+I5Q}/11%//BCr   c                     t               }t        j                  |      }| j                  d|j	                                y)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   s5    
 **623W5F5F5HIr   c                      G d d      } |       }t        j                  |      }| j                  d|j                                y)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y)=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   s?    	 	 **6268I8I8KLr   c                     t               }t        j                  |      }|j                  t	        j
                  ddd            }| j                  d |j                                y)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   sX    
 **62(()<)<UKQS)TU9;M8;M;M;OPr   c                      G d d      }t               }||_        t        j                  |      }|j	                  t        j                  ddd            }| j                  d |j                                y)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y)?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   si    	 	 $**62(()<)<UKQS)TU79K9K9K9MNr   c                     t        j                  t        j                  t                     t	        j
                               }t               }|j                  |       |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   sL    
 **$$VX.0A0A0C
 $%	y)r   c                     | j                         }| j                  |j                         |j                  j                                y)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   8    
 ""$*G,=,=,E,E,GHr   c                     | j                         }| j                  |j                         |j                  j                                y)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                     | j                         }t               }|j                  |d       | j                  |j                  j
                  |       | j                  |j                  j                         y)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   s[    
 ""$8  40g''00(;))334r   c                    | j                         }t               }|j                  |d       |j                          | j	                  |j
                  j                         | j	                  |j
                  j                         y)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   si    
 ""$8  40""$'++445'++556r   c                     | j                         }g fd|j                  _        |j                          | j                  dg       y)zw
        L{policies.ProtocolWrapper.stopConsuming} calls C{stopConsuming} on
        the underlying transport.
        c                  &     j                  d      S r<   appendresults   r   <lambda>z1WrapperTests.test_stopConsuming.<locals>.<lambda>  s    &--2E r   TN)r   rE   stopConsumingr   )r   r   r   s     @r   test_stopConsumingzWrapperTests.test_stopConsuming   sD    
 ""$*E'$(r   c                     g  G fdd      }t        j                   |             }t               }|j                  |       | j	                  |g       y)z}
        L{policies.WrappingFactory.startedConnecting} calls
        C{startedConnecting} on the underlying factory.
        c                       e Zd Z fdZy)4WrapperTests.test_startedConnecting.<locals>.Factoryc                 (    j                  |       y 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  sO    
 	) 	) **795H	!!),)-r   c                     g  G fdd      }t        j                   |             }t               }t               }|j                  ||       | j	                  ||fg       y)z
        L{policies.WrappingFactory.clientConnectionLost} calls
        C{clientConnectionLost} on the underlying factory.
        c                       e Zd Z fdZy)7WrapperTests.test_clientConnectionLost.<locals>.Factoryc                 ,    j                  ||f       y r   r   r   r   r&   r   s      r   clientConnectionLostzLWrapperTests.test_clientConnectionLost.<locals>.Factory.clientConnectionLost#      y&12r   N)r-   r.   r/   r   r   s   r   r   r   "      3r   r   N)r   rh   r   r   r   r   r   r   r   r&   r   s        @r   test_clientConnectionLostz&WrapperTests.test_clientConnectionLost  s^    
 	3 	3 **795H	$$Y79f"5!67r   c                     g  G fdd      }t        j                   |             }t               }t               }|j                  ||       | j	                  ||fg       y)z
        L{policies.WrappingFactory.clientConnectionFailed} calls
        C{clientConnectionFailed} on the underlying factory.
        c                       e Zd Z fdZy)9WrapperTests.test_clientConnectionFailed.<locals>.Factoryc                 ,    j                  ||f       y r   r   r   s      r   clientConnectionFailedzPWrapperTests.test_clientConnectionFailed.<locals>.Factory.clientConnectionFailed4  r   r   N)r-   r.   r/   r   r   s   r   r   r   3  r   r   r   N)r   rh   r   r   r   r   s        @r   test_clientConnectionFailedz(WrapperTests.test_clientConnectionFailed,  s^    
 	3 	3 **795H	&&y&99f"5!67r   c                 Z   t        j                  t        j                  t                     t	        j
                               }t               }||_        |j                  |       | j                  |j                         |j                          | j                  |j                         y)z
        L{policies.ProtocolWrapper.connectionLost} sets C{wrappedProtocol} to
        C{None} in order to break reference cycle between wrapper and wrapped
        protocols.
        N)r   ry   rh   rH   r	   rz   r   r{   assertIsNotNonerk   loseConnectionr   r   s      r   test_breakReferenceCyclez%WrapperTests.test_breakReferenceCycle=  s     **$$VX.0A0A0C
 56	$	y)W445  "'11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   r   s^    4D:JMQO
II	5
7	). 8"8"3r   rc   c                       e Zd Zd Zd Zy)rh   c                     |S r   r0   )r   rm   r5   s      r   r	   zWrappingFactory.protocolP  s    r   c                 x    t         j                  j                  |        | j                  j	                  d        y r   )r   rh   startFactorydeferredr!   r   s    r   r   zWrappingFactory.startFactoryS  s(      --d3t$r   N)r-   r.   r/   r	   r   r0   r   r   rh   rh   O  s    %r   rh   c                   (    e Zd ZdZd Zd Zd Zd Zy)ThrottlingTestsz2
    Tests for L{policies.ThrottlingFactory}.
    c                    	
 t               }d t        d      D        \  	
t        j                  |d      t	              }t        j                         |_        t        j                  d|d      j                         j                  	
fd}	
 fd}	fd	}fd
} fd}
fd}|j                  j                  |       |j                  j                  |       |j                  j                  |       |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   0   K   | ]  }t                 y wr   )r   ).0is     r   	<genexpr>z-ThrottlingTests.test_limit.<locals>.<genexpr>e  s     =q.*=s         r   rf   )	interfacec                     t        j                  dt                     j                  j	                  fd       j                  j	                  fd       j
                  S )Nrf   c                 D    t        j                  dt                    S Nrf   r
   
connectTCPr2   )rc2ns    r   r   zAThrottlingTests.test_limit.<locals>._connect123.<locals>.<lambda>q      ',,[!\"=MN r   c                 D    t        j                  dt                    S r   r   )r   c3r   s    r   r   zAThrottlingTests.test_limit.<locals>._connect123.<locals>.<lambda>t  r   r   )r
   r   r2   r   addCallbackr   )resultsc1r   r   r   s    r   _connect123z/ThrottlingTests.test_limit.<locals>._connect123n  sT    {A|B/?@MM%%N MM%%N ###r   c                 >   j                  fD cg c]  }|j                   c}g d       j                  fD cg c]  }|j                   c}g d       j                  t        j                  j                               d       | S c c}w c c}w )N)r   r   r   )r   r   r   r   )r   r    r$   len	protocolskeys)r   cr   r   r   r   tServers     r   	_check123z-ThrottlingTests.test_limit.<locals>._check123x  s    BB<@aakk@)Lr2rlCannCYOS!2!2!7!7!9:A>N ACs   BBc                 P    j                   j                          j                  S r   )rE   r   r   )r   r   s    r   _lose1z*ThrottlingTests.test_limit.<locals>._lose1~  s    LL'')###r   c                 \    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                 x    j                  j                  d       j                  j                  d       | S )Nr   r   )r   r    r$   )r   r   r   s    r   _check4z+ThrottlingTests.test_limit.<locals>._check4  s0    R\\1-R__a0Nr   c                     fD ]  }|j                   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  sb    V -**,-%%''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!H=BB,,VQ7%g.$~~/ aDIIK	$	 		$
	!	
		 	((5((3((0((3((1((2###r   c                    t               }t        t        j                         |      }|j	                  t        j                  ddd            }t               }||_         |j                  |        |j                  dgdz         | j                  |j                         d       | j                  |j                  d       y)	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     +DJJL&A(()<)<UKQR)ST46	%		*zA~.*,CD22B7r   c                    t               }t        t        j                         |d      }|j	                  t        j                  ddd            }t               }||_        |j                  |       |j                  |_        |j                  d       |j                  d       | j                  |j                         d       | j                  |j                  d	       | j!                  |j                  j"                         |j$                  j'                  d
       | j                  |j                  d       | j)                  |j                  j"                         |j$                  j'                  d
       | j                  |j                  d       | j!                  |j                  j"                         y)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  sO   
 +DJJL&RP$$W%8%8Q%OP-/B,,-(-(%<=22B7--445
 	d#22A6,,334d#22A6--445r   c                    t               }t        t        j                         |d      }|j	                  t        j                  ddd            }t               }||_        |j                  |       |j                  d       |j                  d       | j                  |j                         d       | j                  |j                  d	       |j                  j                  d
       | j                  |j                  d       | j                  |j                   d       |j                  j                  d
       | j                  |j                  d       | j                  |j                   d       |j#                          |j                  d       |j                  d       | j                  |j                         d       | j                  |j                  d	       |j                  j                  d
       | j                  |j                  d       | j                  |j                   d       |j                  j                  d
       | j                  |j                  d       | j                  |j                   d       y)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  s   
 +DJJL&BO$$W%8%8Q%OP-/B-(-(%<=//4d#//3))84d#//3));7

-(-(%<=//4d#//3))84d#//3));7r   N)r-   r.   r/   rI   r  r
  r  r  r0   r   r   r   r   X  s    @$D86<%8r   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)TimeoutProtocolTestsz0
    Tests for L{policies.TimeoutProtocol}.
    c                    t        j                         }t        j                         }t        |_        t        ||d      }|j                  t        j                  ddd            }t               }||_        |j                  |       ||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  sz     

!//1"0(E%%g&9&9%e&TU46	"	Y'u~r   c                    | j                         \  }}|j                  d       | j                  |j                         | j	                  |j
                  j                         |j                  d       |j                          | j                  |j                         |j                  d       | j	                  |j
                  j                         y)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9:a#//* 	a,,99:r   c                     | j                         \  }}| j                  |j                         |j                          | j	                  |j
                  j                         y)z<
        Does nothing if no timeout is already set.
        N)r$  r   r(  r)  r  rk   r$   r*  s      r   test_cancelTimeoutNoTimeoutz0TimeoutProtocolTests.test_cancelTimeoutNoTimeout"  sP     --/
U#//* 	,,99:r   c                     | j                         \  }}|j                  }|j                  d       |j                  d       | j	                  |j
                         |j                          y)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_cancelTimeoutAlreadyCalled.  sY     --/
U**qa112 	r   c                     | j                         \  }}|j                  d       |j                  j                          |j	                          | j                  |j                  j                         y)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<  sZ    
 --/
Uq  	,,99:r   N)	r-   r.   r/   rI   r$  r,  r.  r1  r4  r0   r   r   r  r    s     ,;&
;;r   r  c                   (    e Zd ZdZd Zd Zd Zd Zy)TimeoutFactoryTestsz/
    Tests for L{policies.TimeoutFactory}.
    c                    t        j                         | _        t        j                         }t
        |_        t        | j                  |d      | _        | j                  j                  t        j                  ddd            | _        t               | _        | j                  | j                  _        | j                  j                  | j                         | j                  j                  | _        y)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   r0  )r   r#  s     r   setUpzTimeoutFactoryTests.setUpQ  s    
 ZZ\
!//1"0-djj.!L\\//{E:

 :;"&**

!!$..1 JJ66r   c                    | j                   j                  g d       | j                  | j                  j                         | j                   j                  ddg       | j                  | j                  j                         y)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  r0  r$   r|   r   s    r   test_timeoutz TimeoutFactoryTests.test_timeoutb  s]     	

12**778 	

c
#))667r   c                    | j                   j                  g d       | j                  | j                  j                         | j
                  j                  d       | j                   j                  g d       | j                  | j                  j                         | j
                  j                  dgdz         | j                   j                  g d       | j                  | j                  j                         | j                   j                  ddg       | j                  | j                  j                         y)	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   r>  r  r0  r$   r~   rF   r  r|   r   s    r   test_sendAvoidsTimeoutz*TimeoutFactoryTests.test_sendAvoidsTimeoutp  s     	

(**778 	

-. 	

(**778 	

  (a0 	

(**778 	

c
#))667r   c                    | j                   j                  g d       | j                  | j                  j                         | j
                  j                  d       | j                   j                  g d       | j                  | j                  j                         | j                   j                  g d       | j                  | j                  j                         y)zR
        Make sure that receiving data also resets the timeout countdown.
        )r:  r<  r;  rA  rB  N)rO   r>  r  r0  r$   r~   r,   r|   r   s    r   test_receiveAvoidsTimeoutz-TimeoutFactoryTests.test_receiveAvoidsTimeout  s    
 	

(**778 	

 45 	

(**778 	

())667r   N)r-   r.   r/   rI   r8  r?  rC  rE  r0   r   r   r6  r6  L  s    7"8888r   r6  c                   >    e Zd ZdZdZdZd Zd Zd ZddZ	d	 Z
d
 Zy)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                     || _         y)zF
        Initialize the protocol with a C{task.Clock} object.
        N)rO   )r   rO   s     r   r   zTimeoutTester.__init__  s     
r   c                 :    | j                  | j                         y)z3
        Upon connection, set the timeout.
        N)r'  timeOutr   s    r   r"   zTimeoutTester.connectionMade  s     	%r   c                 d    | j                          t        j                  j                  | |       y)z,
        Reset the timeout on data.
        N)resetTimeoutr	   rz   r,   r*   s     r   r,   zTimeoutTester.dataReceived  s&     	&&tT2r   Nc                 &    | j                  d       y)zD
        On connection lost, cancel all timeout operations.
        N)r'  r%   s     r   r'   zTimeoutTester.connectionLost  s     	r   c                     d| _         y)zX
        Flags the timedOut variable to indicate the timeout of the connection.
        TN)timedOutr   s    r   timeoutConnectionzTimeoutTester.timeoutConnection  s     r   c                 D     | j                   j                  ||g|i |S )zD
        Override callLater to use the deterministic clock.
        rU   )r   timeoutrY   rQ   rR   s        r   rV   zTimeoutTester.callLater  s'     $tzz##GTCDCFCCr   r   )r-   r.   r/   rI   rJ  rO  r   r"   r,   r'   rP  rV   r0   r   r   rG  rG    s2     GH&3Dr   rG  c                   @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)TimeoutMixinTestsz-
    Tests for L{policies.TimeoutMixin}.
    c                 j    t        j                         | _        t        | j                        | _        y)zY
        Create a testable, deterministic clock and a C{TimeoutTester} instance.
        N)r   r  rO   rG  r~   r   s    r   r8  zTimeoutMixinTests.setUp  s!     ZZ\
"4::.
r   c                     | j                   j                  d       | j                  t        | j                  j
                        d       y)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  s5    
 	

b!TZZ--.2r   c                 R   | j                   j                  t                      | j                  j	                  g d       | j                  | j                   j                         | j                  j	                  ddg       | j                  | j                   j                         y)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   r>  r  rO  r|   r   s    r   r?  zTimeoutMixinTests.test_timeout  sn    
 	

!!/"34 	

*+,,-

C!

++,r   c                    | j                   j                  t                      | j                  j	                  g d       | j                  | j                   j                         | j                   j                  d       | j                  j	                  g d       | j                  | j                   j                         | j                  j	                  ddg       | j                  | j                   j                         y)zV
        Check that receiving data is delaying the timeout of the connection.
        rZ  s   hello there)r   r<  r<  r;  r   r<  N)	r~   r{   r   rO   r>  r  rO  r,   r|   r   s    r   test_noTimeoutz TimeoutMixinTests.test_noTimeout  s     	

!!/"34

*+,,-

/

*+,,-

C!

++,r   c                    d| j                   _        | j                   j                  t                      | j                   j	                  d       | j                  | j                   j                  d       | j                  j                  ddg       | j                  | j                   j                         | j                  j                  ddg       | j                  | j                   j                         y)zy
        Check that setting a new value for timeout cancel the previous value
        and install a new timeout.
        Nr   r   g?r=  )r~   rJ  r{   r   r'  r   rO   r>  r  rO  r|   r   s    r   test_resetTimeoutz#TimeoutMixinTests.test_resetTimeout  s    
 "



!!/"34

a ++Q/

C!,,-

C!

++,r   c                 p   d| j                   _        | j                   j                  t                      | j                   j	                  d       | j                  | j                   j                         | j                  j                  g d       | j                  | j                   j                         y)zO
        Setting the timeout to L{None} cancel any timeout operations.
           N)r   r`  r`  r`  )
r~   rJ  r{   r   r'  r   rO   r>  r  rO  r   s    r   r,  z$TimeoutMixinTests.test_cancelTimeout  sw     



!!/"34

d#$**,,-

%,,-r   c                    d| j                   _        | j                  | j                   j                  d      d       | j                  | j                   j                  d      d       | j	                  | j                   j                  d             | j                  | j                   j                  d       | j                   j                  d       y)zM
        setTimeout should return the value of the previous timeout.
        r`  r  Nr   )r~   rJ  r   r'  r   r   s    r   test_setTimeoutReturnz'TimeoutMixinTests.test_setTimeoutReturn  s     

..r2A6..t4b9$**//23++Q/ 	

d#r   c                 Z   | j                   j                  d       | j                  j                         d   j	                          | j                  | j                   j                         | j                   j                  d       | j                  | j                   j                         y)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   getDelayedCallsr3  r   rJ  r   r   s    r   %test_setTimeoutCancleAlreadyCancelledz7TimeoutMixinTests.test_setTimeoutCancleAlreadyCancelled,  sx     	

a  	

""$Q'..0TZZ//0

d#$**,,-r   N)r-   r.   r/   rI   r8  rX  r?  r\  r^  r,  rb  re  r0   r   r   rT  rT    s/    /3--- .$.r   rT  c                       e Zd ZdZd Zd Zy)!LimitTotalConnectionsFactoryTestsz/Tests for policies.LimitTotalConnectionsFactoryc                    t        j                         }t        j                  |_        | j	                  d|j
                         |j                  d       }| j	                  d|j
                         |j                  d       }| j	                  d|j
                         |j                  d        | j	                  d|j
                         |j                  d        | j	                  d|j
                         y )Nr   r   r   )r   LimitTotalConnectionsFactoryr	   rz   r   connectionCountr8   r'   )r   rl   p1p2s       r   testConnectionCountingz8LimitTotalConnectionsFactoryTests.testConnectionCounting@  s    779#,, 	G334 ""4(G334""4(G334 	$G334
$G334r   c                    t        j                         t        j                  _        d_        j                  d       }| j                  |       | j                  dj                         | j                  j                  d              | j                  dj                          G fddt        j                        }|_
        d_        j                  d       }|j                  d        | j                  j                         | j                  dj                         |j                  d        | j                  dj                         |j                  d        | j                  dj                         y )Nr   c                       e Zd Z fdZy)RLimitTotalConnectionsFactoryTests.testConnectionLimiting.<locals>.OverflowProtocolc                     d_         y r<   )
overflowed)r   rl   s    r   r"   zaLimitTotalConnectionsFactoryTests.testConnectionLimiting.<locals>.OverflowProtocol.connectionMadef  s    %)"r   N)r-   r.   r/   r"   )rl   s   r   OverflowProtocolrp  e  s    *r   rs  Fr   r   )r   ri  r	   rz   connectionLimitr8   r   r   rj  r   overflowProtocolrr  r{   r|   r'   )r   r5   rs  oprl   s       @r   testConnectionLimitingz8LimitTotalConnectionsFactoryTests.testConnectionLimitingT  sD   779#,,"# !!$'QG334 	'//56G334	*x00 	* $4 "
 ""4(
$**+G334 	
G334
$G334r   N)r-   r.   r/   rI   rm  rw  r0   r   r   rg  rg  =  s    95($5r   rg  c                       e Zd Zd Zy)WriteSequenceEchoProtocolc                     |j                  d      dk7  r| j                  j                  |g       y t        j	                  | |       y )Ns   vector!)findrE   r  r:   r,   )r   bytess     r   r,   z&WriteSequenceEchoProtocol.dataReceived|  s7    ::j!R'NN((%1%%dE2r   N)r-   r.   r/   r,   r0   r   r   ry  ry  {  s    3r   ry  c                       e Zd ZdZd Zy)TestLoggingFactoryNc                 ^    | j                   J d       t               | _         | j                   S )Nzopen() called too many times)openFiler   )r   names     r   openzTestLoggingFactory.open  s+    }}$D&D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y)LoggingFactoryTestsz6
    Tests for L{policies.TrafficLoggingFactory}.
    c                    t               }t        |_        t               }t	        |d      }|j                  d      }||_        |j                  |       |j                  j                         }| j                  d|       | j                  |j                                |j                  d       |j                  j                         }| j                  dj                  d      |       | j                  dj                  d      |       | j                  |j                         d       |j                          |j                  d       |j                  j                         }| j                  dj                  dg      |       | j                  |j                         d       |j!                          |j                  j                         }| j                  d	|       y
)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   ry  r	   r   r  r8   r{   r  getvalueassertInr  r  r,   formatr   r  r   )r   r#  trm   r5   vs         r   test_thingsGetLoggedz(LoggingFactoryTests.test_thingsGetLogged  ss     ";,.~v6OO-.
	JJ!c1#	-.JJ!k(()?@!Dk(()?@!D$:;			<=JJ!l))+P*QRTUV$IJ	JJ!&*r   c                    t               }t        |d      }| j                  |j                  d       |j	                  d       | j                  |j                  d       d|_        |j	                  d       | j                  |j                  d       |j                          | j                  |j                  d       y)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Q'	)*Q'
	)*Q'	Q'r   c                   	 g g 		fd}| j                  t        d|       t        j                         }t        |_        t        j                  |d      }|j                  t        j                  ddd            }|j                  t        j                  ddd            }di f}d	i f}| j                  ||g       | j                  |j                  |j                  g	       y
)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                  x    j                  | |f       t               }| d   |_        j                  |       |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  s<     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"000H++{E:
 ,,{E:
 &r*
&+*k2J?+--|/C/CDlSr   N)r-   r.   r/   rI   r  r  r  r0   r   r   r  r    s     +D("$Tr   r  ).rI   r  r  r   zope.interfacer   r   r   twisted.internetr   r   r	   r
   r   twisted.internet.testingr   r   twisted.protocolsr   twisted.trialr   rz   r   ClientFactoryr2   r:   r"  rH   rN   rK   r`   r^   TestCaserc   rh   r   r  r6  TimeoutMixinrG  rT  rg  ry  r  r  r  r0   r   r   <module>r     sr  
   @ @ D D V & "X&& (8)) #8$$ # X## 2 : : 2*2X44 2*Z38$$ Z3z%h.. %Y8h'' Y8xU;8,, U;pR8(++ R8j.DH%%x'<'< .Dbh.)) h.V;5(9(9 ;5|3 377 \T(++ \Tr   