
    h                     @   S r SSKrSSKJr  SSKJrJrJr  SSKJ	r	J
r
JrJrJr  SSKJrJr  SSKJr  SSKJr   " S	 S
\R*                  5      r " S S\R.                  5      r " S S\R*                  5      r " S S\R4                  5      r " S S\R8                  5      r " S S\R<                  5      r " S S\R@                  5      r! " S S\RD                  5      r" " S S\R@                  5      r# " S S\R@                  5      r$ " S S\R@                  5      r% " S S \R*                  \RL                  5      r' " S! S"\R@                  5      r( " S# S$\R@                  5      r) " S% S&\5      r* " S' S(\RV                  5      r, " S) S*\R@                  5      r-g)+z
Test code for policies.
    N)StringIO)	InterfaceimplementedByimplementer)addressdeferprotocolreactortask)StringTransport StringTransportWithDisconnection)policies)unittestc                   8    \ rS rSrS=rrSrS rS rS r	S r
Srg	)
SimpleProtocol   r       c                 l    [         R                  " 5       U l        [         R                  " 5       U l        g N)r   Deferred
dConnecteddDisconnectedselfs    a/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/test/test_policies.py__init__SimpleProtocol.__init__   s    ..*"^^-r   c                 H    SU l         U R                  R                  S5        g N    )	connectedr   callbackr   s    r   connectionMadeSimpleProtocol.connectionMade   s      $r   c                 H    SU l         U R                  R                  S5        g r   )disconnectedr   r#   r   reasons     r   connectionLostSimpleProtocol.connectionLost    s    ##B'r   c                 .    U =R                   U-  sl         g r   )bufferr   datas     r   dataReceivedSimpleProtocol.dataReceived$   s    tr   )r"   r   r   r'   N)__name__
__module____qualname____firstlineno__r"   r'   r-   r   r$   r*   r0   __static_attributes__ r   r   r   r      s%      IF.%(r   r   c                        \ rS rSrS rS rSrg)SillyFactory(   c                     Xl         g r   p)r   r=   s     r   r   SillyFactory.__init__)   s    r   c                     U R                   $ r   r<   )r   addrs     r   buildProtocolSillyFactory.buildProtocol,   s    vvr   r<   N)r2   r3   r4   r5   r   rA   r6   r7   r   r   r9   r9   (   s    r   r9   c                   0    \ rS rSrSrS rS rS rS rSr	g)	EchoProtocol0   Fc                     SU l         g NTpausedr   s    r   pauseProducingEchoProtocol.pauseProducing3   s	    r   c                     SU l         g )NFrH   r   s    r   resumeProducingEchoProtocol.resumeProducing6   s	    r   c                     g r   r7   r   s    r   stopProducingEchoProtocol.stopProducing9   s    r   c                 :    U R                   R                  U5        g r   )	transportwriter.   s     r   r0   EchoProtocol.dataReceived<   s    T"r   rH   N)
r2   r3   r4   r5   rI   rJ   rM   rP   r0   r6   r7   r   r   rD   rD   0   s    F#r   rD   c                       \ rS rSrSr\rSrg)Server@   z0
A simple server factory using L{EchoProtocol}.
r7   N)r2   r3   r4   r5   __doc__rD   r	   r6   r7   r   r   rW   rW   @   s     Hr   rW   c                   $    \ rS rSrSrS rS rSrg)TestableThrottlingFactoryH   z@
L{policies.ThrottlingFactory} using a L{task.Clock} for tests.
c                 X    [         R                  R                  " U /UQ70 UD6  Xl        gzx
@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   r`   argskwargss       r   r   "TestableThrottlingFactory.__init__M   s'     	""++DB4B6B
r   c                 8    U R                   R                  X5      $ z 
Forward to the testable clock.
r`   	callLaterr   periodfuncs      r   rh   #TestableThrottlingFactory.callLaterV        zz##F11r   r`   Nr2   r3   r4   r5   rY   r   rh   r6   r7   r   r   r[   r[   H       2r   r[   c                   $    \ rS rSrSrS rS rSrg)TestableTimeoutFactory]   z=
L{policies.TimeoutFactory} using a L{task.Clock} for tests.
c                 X    [         R                  R                  " U /UQ70 UD6  Xl        gr^   )r   TimeoutFactoryr   r`   ra   s       r   r   TestableTimeoutFactory.__init__b   s'     	((???
r   c                 8    U R                   R                  X5      $ rf   rg   ri   s      r   rh    TestableTimeoutFactory.callLaterk   rm   r   rn   Nro   r7   r   r   rr   rr   ]   rp   r   rr   c                   x    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rSrg)WrapperTestsr   z6
Tests for L{WrappingFactory} and L{ProtocolWrapper}.
c                     [        5       n[        R                  " U5      nUR                  [        R
                  " SSS5      5      nU R                  UR                  R                  U5        g)zN
Make sure protocol.factory is the wrapped factory, not the wrapping
factory.
TCP	127.0.0.1#   N)	rW   r   WrappingFactoryrA   r   IPv4AddressassertIswrappedProtocolfactory)r   fwfr=   s       r   test_protocolFactoryAttribute*WrapperTests.test_protocolFactoryAttributew   sT    
 H%%a(W00RHIa''//3r   c                     " S S[         5      n[        U5       " S S5      5       n[        [        R                  5        [
        R                  " 5       n[        R                  " [        R                  " S5      U5      nUR                  U" 5       5        U R                  UR                  UR                  5      5        g)z
The transport wrapper passed to the wrapped protocol's
C{makeConnection} provides the same interfaces as are provided by the
original transport.
c                       \ rS rSrSrg)=WrapperTests.test_transportInterfaces.<locals>.IStubTransport   r7   Nr2   r3   r4   r5   r6   r7   r   r   IStubTransportr          r   r   c                       \ rS rSrSrg)<WrapperTests.test_transportInterfaces.<locals>.StubTransport   r7   Nr   r7   r   r   StubTransportr      s    r   r   N)r   r   r   r   ProtocolWrapperr	   Protocolr   makeConnection
assertTrue
providedByrS   )r   r   r   protowrappers        r   test_transportInterfaces%WrapperTests.test_transportInterfaces   s    	Y 	 
^	$	 	 
%	 	h../!!#**8+C+CD+I5Q}/11%//BCr   c                     [        5       n[        R                  " U5      nU R                  SUR	                  5       5        g)zk
L{WrappingFactory.logPrefix} is customized to mention both the original
factory and the wrapping factory.
zServer (WrappingFactory)N)rW   r   r   assertEqual	logPrefix)r   serverr   s      r   test_factoryLogPrefix"WrapperTests.test_factoryLogPrefix   s5    
 **623W5F5F5HIr   c                      " S S5      nU" 5       n[         R                  " U5      nU R                  SUR                  5       5        g)z
If the wrapped factory doesn't have a L{logPrefix} method,
L{WrappingFactory.logPrefix} falls back to the factory class name.
c                       \ rS rSrSrg)=WrapperTests.test_factoryLogPrefixFallback.<locals>.NoFactory   r7   Nr   r7   r   r   	NoFactoryr      r   r   r   zNoFactory (WrappingFactory)N)r   r   r   r   )r   r   r   r   s       r   test_factoryLogPrefixFallback*WrapperTests.test_factoryLogPrefixFallback   s?    	 	 **6268I8I8KLr   c                     [        5       n[        R                  " U5      nUR                  [        R
                  " SSS5      5      nU R                  SUR                  " 5       5        g)zc
L{ProtocolWrapper.logPrefix} is customized to mention both the original
protocol and the wrapper.
r}   r~   r   zEchoProtocol (ProtocolWrapper)N)rW   r   r   rA   r   r   r   r   )r   r   r   r	   s       r   test_protocolLogPrefix#WrapperTests.test_protocolLogPrefix   sU    
 **62(()<)<UKQS)TU98;M;M;OPr   c                      " S S5      n[        5       nXl        [        R                  " U5      nUR	                  [
        R                  " SSS5      5      nU R                  SUR                  " 5       5        g)z
If the wrapped protocol doesn't have a L{logPrefix} method,
L{ProtocolWrapper.logPrefix} falls back to the protocol class name.
c                       \ rS rSrSrg)?WrapperTests.test_protocolLogPrefixFallback.<locals>.NoProtocol   r7   Nr   r7   r   r   
NoProtocolr      r   r   r   r}   r~   r   zNoProtocol (ProtocolWrapper)N)	rW   r	   r   r   rA   r   r   r   r   )r   r   r   r   r	   s        r   test_protocolLogPrefixFallback+WrapperTests.test_protocolLogPrefixFallback   sd    	 	 $**62(()<)<UKQS)TU79K9K9MNr   c                     [         R                  " [         R                  " [        5       5      [        R
                  " 5       5      n[        5       nUR                  U5        U$ )zU
Return L{policies.ProtocolWrapper} that has been connected to a
L{StringTransport}.
)r   r   r   rW   r	   r   r   r   r   r   rS   s      r   _getWrapperWrapperTests._getWrapper   sL    
 **$$VX.0A0A0C
 $%	y)r   c                     U R                  5       nU R                  UR                  5       UR                  R                  5       5        g)zS
L{policies.ProtocolWrapper.getHost} calls C{getHost} on the underlying
transport.
N)r   r   getHostrS   r   r   s     r   test_getHostWrapperTests.test_getHost   8    
 ""$*G,=,=,E,E,GHr   c                     U R                  5       nU R                  UR                  5       UR                  R                  5       5        g)zS
L{policies.ProtocolWrapper.getPeer} calls C{getPeer} on the underlying
transport.
N)r   r   getPeerrS   r   s     r   test_getPeerWrapperTests.test_getPeer   r   r   c                     U R                  5       n[        5       nUR                  US5        U R                  UR                  R
                  U5        U R                  UR                  R                  5        g)ze
L{policies.ProtocolWrapper.registerProducer} calls C{registerProducer}
on the underlying transport.
TN)r   objectregisterProducerr   rS   producerr   	streamingr   r   r   s      r   test_registerProducer"WrapperTests.test_registerProducer   s[    
 ""$8  40g''00(;))334r   c                    U R                  5       n[        5       nUR                  US5        UR                  5         U R	                  UR
                  R                  5        U R	                  UR
                  R                  5        g)zi
L{policies.ProtocolWrapper.unregisterProducer} calls
C{unregisterProducer} on the underlying transport.
TN)r   r   r   unregisterProducerassertIsNonerS   r   r   r   s      r   test_unregisterProducer$WrapperTests.test_unregisterProducer   si    
 ""$8  40""$'++445'++556r   c                    ^ U R                  5       n/ mU4S jUR                  l        UR                  5         U R                  TS/5        g)z_
L{policies.ProtocolWrapper.stopConsuming} calls C{stopConsuming} on
the underlying transport.
c                  &   > T R                  S5      $ rG   appendresults   r   <lambda>1WrapperTests.test_stopConsuming.<locals>.<lambda>  s    &--2Er   TN)r   rS   stopConsumingr   )r   r   r   s     @r   test_stopConsumingWrapperTests.test_stopConsuming   sD    
 ""$*E'$(r   c                    ^ / m " U4S jS5      n[         R                  " U" 5       5      n[        5       nUR                  U5        U R	                  TU/5        g)ze
L{policies.WrappingFactory.startedConnecting} calls
C{startedConnecting} on the underlying factory.
c                   "   > \ rS rSrU 4S jrSrg)4WrapperTests.test_startedConnecting.<locals>.Factoryi  c                 (   > TR                  U5        g r   r   )r   	connectorr   s     r   startedConnectingFWrapperTests.test_startedConnecting.<locals>.Factory.startedConnecting  s    i(r   r7   N)r2   r3   r4   r5   r   r6   r   s   r   Factoryr     s    ) )r   r   N)r   r   r   r   r   )r   r   r   r   r   s       @r   test_startedConnecting#WrapperTests.test_startedConnecting  sO    
 	) 	) **795H	!!),)-r   c                    ^ / m " U4S jS5      n[         R                  " U" 5       5      n[        5       n[        5       nUR                  X45        U R	                  TX44/5        g)zk
L{policies.WrappingFactory.clientConnectionLost} calls
C{clientConnectionLost} on the underlying factory.
c                   "   > \ rS rSrU 4S jrSrg)7WrapperTests.test_clientConnectionLost.<locals>.Factoryi"  c                 *   > TR                  X45        g r   r   r   r   r)   r   s      r   clientConnectionLostLWrapperTests.test_clientConnectionLost.<locals>.Factory.clientConnectionLost#      y12r   r7   N)r2   r3   r4   r5   r   r6   r   s   r   r   r   "      3 3r   r   N)r   r   r   r   r   r   r   r   r   r)   r   s        @r   test_clientConnectionLost&WrapperTests.test_clientConnectionLost  sZ    
 	3 	3 **795H	$$Y79"5!67r   c                    ^ / m " U4S jS5      n[         R                  " U" 5       5      n[        5       n[        5       nUR                  X45        U R	                  TX44/5        g)zo
L{policies.WrappingFactory.clientConnectionFailed} calls
C{clientConnectionFailed} on the underlying factory.
c                   "   > \ rS rSrU 4S jrSrg)9WrapperTests.test_clientConnectionFailed.<locals>.Factoryi3  c                 *   > TR                  X45        g r   r   r   s      r   clientConnectionFailedPWrapperTests.test_clientConnectionFailed.<locals>.Factory.clientConnectionFailed4  r   r   r7   N)r2   r3   r4   r5   r   r6   r   s   r   r   r   3  r   r   r   N)r   r   r   r   r   r   s        @r   test_clientConnectionFailed(WrapperTests.test_clientConnectionFailed,  sZ    
 	3 	3 **795H	&&y99"5!67r   c                 ^   [         R                  " [         R                  " [        5       5      [        R
                  " 5       5      n[        5       nXl        UR                  U5        U R                  UR                  5        UR                  5         U R                  UR                  5        g)z
L{policies.ProtocolWrapper.connectionLost} sets C{wrappedProtocol} to
C{None} in order to break reference cycle between wrapper and wrapped
protocols.
N)r   r   r   rW   r	   r   r   r   assertIsNotNoner   loseConnectionr   r   s      r   test_breakReferenceCycle%WrapperTests.test_breakReferenceCycle=  s     **$$VX.0A0A0C
 56	$y)W445  "'112r   r7   N)r2   r3   r4   r5   rY   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r6   r7   r   r   rz   rz   r   s^    4D:JMQO
II	5
7	). 8"8"3r   rz   c                        \ rS rSrS rS rSrg)r   iO  c                     U$ r   r7   )r   r   r=   s      r   r	   WrappingFactory.protocolP  s    r   c                 x    [         R                  R                  U 5        U R                  R	                  S 5        g r   )r   r   startFactorydeferredr#   r   s    r   r  WrappingFactory.startFactoryS  s(      --d3t$r   r7   N)r2   r3   r4   r5   r	   r  r6   r7   r   r   r   r   O  s    %r   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	ThrottlingTestsiX  z*
Tests for L{policies.ThrottlingFactory}.
c                   ^ ^	^
^^^^^ [        5       nS [        S5       5       u  m	m
mm[        R                  " US5      m[	        T5      n[
        R                  " 5       Ul        [        R                  " SUSS9mTR                  5       R                  mU	U
UU4S jnU	U
UU U4S jnU	4S	 jnUU4S
 jnUU 4S jnU
UU4S jnUR                  R                  U5        UR                  R                  U5        UR                  R                  U5        UR                  R                  U5        UR                  R                  U5        UR                  R                  U5        UR                  $ )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   6   #    U  H  n[        5       v   M     g 7fr   )r   ).0is     r   	<genexpr>-ThrottlingTests.test_limit.<locals>.<genexpr>e  s     =Hq.**Hs         r   r~   )	interfacec                    > [         R                  " ST[        T5      5        TR                  R	                  UU4S j5        TR                  R	                  UU4S j5        TR
                  $ )Nr~   c                 F   > [         R                  " ST[        T5      5      $ Nr~   r
   
connectTCPr9   )rc2ns    r   r   AThrottlingTests.test_limit.<locals>._connect123.<locals>.<lambda>q      ',,[!\"=MNr   c                 F   > [         R                  " ST[        T5      5      $ r  r  )r  c3r  s    r   r   r  t  r  r   )r
   r  r9   r   addCallbackr   )resultsc1r  r  r  s    r   _connect123/ThrottlingTests.test_limit.<locals>._connect123n  sT    {A|B/?@MM%%N MM%%N ###r   c                 F  > TR                  TTT4 Vs/ s H  oR                  PM     sn/ SQ5        TR                  TTT4 Vs/ s H  oR                  PM     sn/ SQ5        TR                  [        TR                  R                  5       5      S5        U $ s  snf s  snf )N)r    r    r    )r   r   r    r  )r   r"   r'   len	protocolskeys)r  cr  r  r  r   tServers     r   	_check123-ThrottlingTests.test_limit.<locals>._check123x  s    BB<@<akk<@)Lr2rlClnnlCYOS!2!2!7!7!9:A>N ACs   BBc                 P   > TR                   R                  5         TR                  $ r   )rS   r   r   )r  r  s    r   _lose1*ThrottlingTests.test_limit.<locals>._lose1~  s    LL'')###r   c                 ^   > [         R                  " ST[        T5      5        TR                  $ r  )r
   r  r9   r   )r  c4r  s    r   	_connect4-ThrottlingTests.test_limit.<locals>._connect4  s$    {A|B/?@== r   c                 x   > TR                  TR                  S5        TR                  TR                  S5        U $ )Nr    r   )r   r"   r'   )r  r,  r   s    r   _check4+ThrottlingTests.test_limit.<locals>._check4  s0    R\\1-R__a0Nr   c                    > TT4 H  nUR                   R                  5         M     [        R                  " [        R                  " TR
                  5      TR                  TR                  /5      $ r   )rS   r   r   DeferredListmaybeDeferredstopListeningr   )r  r$  r  r,  r=   s     r   _cleanup,ThrottlingTests.test_limit.<locals>._cleanup  s]    V**, %%''8$$$$ r   )rW   ranger   r_   r   r   r   r  r
   	listenTCPr   portr  )r   r   wrapTServerr  r&  r)  r-  r0  r6  r  r  r  r,  r  r=   r%  s   `        @@@@@@@r   
test_limitThrottlingTests.test_limit]  s"    =E!H=BB,,VQ7%g.$~~/ aDIIK	$ 	$	 		$
	!	
		 	((5((3((0((3((1((2###r   c                    [        5       n[        [        R                  " 5       U5      nUR	                  [
        R                  " SSS5      5      n[        5       nX4l        UR                  " U5        UR                  " S/S-  5        U R                  UR                  5       S5        U R                  UR                  S5        g)	zJ
L{ThrottlingProtocol.writeSequence} is called on the underlying factory.
r}   r~   r      bytesr  s   bytesbytesbytesbytesr   N)rW   r[   r   ClockrA   r   r   r   r	   r   writeSequencer   valuewrittenThisSecond)r   r   r%  r	   rS   s        r   test_writeSequence"ThrottlingTests.test_writeSequence  s     +DJJL&A(()<)<UKQR)ST46	%	*zA~.*,CD22B7r   c                 |   [        5       n[        [        R                  " 5       USS9nUR	                  [
        R                  " SSS5      5      n[        5       nX4l        UR                  U5        UR                  Ul        UR                  S5        UR                  S5        U R                  UR                  5       S5        U R                  UR                  S	5        U R!                  UR                  R"                  5        UR$                  R'                  S
5        U R                  UR                  S5        U R)                  UR                  R"                  5        UR$                  R'                  S
5        U R                  UR                  S5        U R!                  UR                  R"                  5        g)zM
Check the writeLimit parameter: write data, and check for the pause
status.

   )
writeLimitr}   r~   r   
   0123456789
   abcdefghij   0123456789abcdefghijr   ?N)rW   r[   r   r@  rA   r   r   r   r	   r   r   r   r0   r   rB  rC  assertFalserI   r`   advancer   r   r   r%  r:  trs        r   test_writeLimitThrottlingTests.test_writeLimit  sM   
 +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                    [        5       n[        [        R                  " 5       USS9nUR	                  [
        R                  " SSS5      5      n[        5       nX4l        UR                  U5        UR                  S5        UR                  S5        U R                  UR                  5       S5        U R                  UR                  S	5        UR                  R                  S
5        U R                  UR                  S5        U R                  UR                   S5        UR                  R                  S
5        U R                  UR                  S5        U R                  UR                   S5        UR#                  5         UR                  S5        UR                  S5        U R                  UR                  5       S5        U R                  UR                  S	5        UR                  R                  S
5        U R                  UR                  S5        U R                  UR                   S5        UR                  R                  S
5        U R                  UR                  S5        U R                  UR                   S5        g)zJ
Check the readLimit parameter: read data and check for the pause
status.
rG  )	readLimitr}   r~   r   rI  rJ  rK  r   rL  rI   	producingN)rW   r[   r   r@  rA   r   r   r   r	   r   r0   r   rB  readThisSecondr`   rN  producerStateclearrO  s        r   test_readLimit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   r7   N)
r2   r3   r4   r5   rY   r<  rD  rQ  rY  r6   r7   r   r   r  r  X  s    @$D86<%8r   r  c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
TimeoutProtocolTestsi  z(
Tests for L{policies.TimeoutProtocol}.
c                    [         R                  " 5       n[        R                  " 5       n[        Ul        [        XS5      nUR                  [        R                  " SSS5      5      n[        5       nXEl        UR                  U5        XA4$ )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})
Nr}   r~   90  )r   r@  r	   ServerFactoryr   rr   rA   r   r   r   r   )r   r`   wrappedFactoryr   r   rS   s         r   getProtocolAndClock(TimeoutProtocolTests.getProtocolAndClock  st     

!//1"0(E%%g&9&9%e&TU46	"Y'~r   c                    U R                  5       u  pUR                  S5        U R                  UR                  5        U R	                  UR
                  R                  5        UR                  S5        UR                  5         U R                  UR                  5        UR                  S5        U R	                  UR
                  R                  5        g)z"
Will cancel the ongoing timeout.
   r    N)
ra  
setTimeoutr   timeoutCallrM  r   r'   rN  cancelTimeoutr   r   sutr`   s      r   test_cancelTimeout'TimeoutProtocolTests.test_cancelTimeout  s     --/
qS__-,,99:a#//* 	a,,99:r   c                     U R                  5       u  pU R                  UR                  5        UR                  5         U R	                  UR
                  R                  5        g)z,
Does nothing if no timeout is already set.
N)ra  r   rf  rg  rM  r   r'   rh  s      r   test_cancelTimeoutNoTimeout0TimeoutProtocolTests.test_cancelTimeoutNoTimeout"  sN     --/
#//* 	,,99:r   c                     U R                  5       u  pUR                  nUR                  S5        UR                  S5        U R	                  UR
                  5        UR                  5         g)z0
Does nothing if no timeout is already reached.
rd  N)ra  r   re  rN  r   r'   rg  )r   ri  r`   wrappedProtos       r   test_cancelTimeoutAlreadyCalled4TimeoutProtocolTests.test_cancelTimeoutAlreadyCalled.  sW     --/
**qa112 	r   c                     U R                  5       u  pUR                  S5        UR                  R                  5         UR	                  5         U R                  UR                  R                  5        g)zU
Does nothing if the timeout is cancelled from another part.
Ex from another thread.
rd  N)ra  re  rf  cancelrg  rM  r   r'   rh  s      r   "test_cancelTimeoutAlreadyCancelled7TimeoutProtocolTests.test_cancelTimeoutAlreadyCancelled<  sX    
 --/
q  	,,99:r   r7   N)r2   r3   r4   r5   rY   ra  rj  rm  rq  ru  r6   r7   r   r   r\  r\    s     ,;&
;;r   r\  c                   0    \ rS rSrSrS rS rS rS rSr	g)	TimeoutFactoryTestsiL  z'
Tests for L{policies.TimeoutFactory}.
c                    [         R                  " 5       U l        [        R                  " 5       n[
        Ul        [        U R                  US5      U l        U R                  R                  [        R                  " SSS5      5      U l        [        5       U l        U R                  U R                  l        U R                  R                  U R                  5        U R                  R                  U l        g)zY
Create a testable, deterministic clock, and a set of
server factory/protocol/transport.
rd  r}   r~   r^  N)r   r@  r`   r	   r_  r   rr   r   rA   r   r   r   r   rS   r   r   rp  )r   r`  s     r   setUpTimeoutFactoryTests.setUpQ  s    
 ZZ\
!//1"0-djj.!L\\//{E:

 :;"&**

!!$..1 JJ66r   c                    U R                   R                  / SQ5        U R                  U R                  R                  5        U R                   R                  SS/5        U R                  U R                  R                  5        g)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)r`   pumprM  rp  r'   r   r   s    r   test_timeout TimeoutFactoryTests.test_timeoutb  s]     	

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

c
#))667r   c                    U R                   R                  / SQ5        U R                  U R                  R                  5        U R
                  R                  S5        U R                   R                  / SQ5        U R                  U R                  R                  5        U R
                  R                  S/S-  5        U R                   R                  / SQ5        U R                  U R                  R                  5        U R                   R                  SS/5        U R                  U R                  R                  5        g)	zz
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?  rd  r}  g       @N)	r`   r  rM  rp  r'   r   rT   rA  r   r   s    r   test_sendAvoidsTimeout*TimeoutFactoryTests.test_sendAvoidsTimeoutp  s     	

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

-. 	

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

  (a0 	

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

c
#))667r   c                    U R                   R                  / SQ5        U R                  U R                  R                  5        U R
                  R                  S5        U R                   R                  / SQ5        U R                  U R                  R                  5        U R                   R                  / SQ5        U R                  U R                  R                  5        g)zB
Make sure that receiving data also resets the timeout countdown.
)r}  r  r~  r  r  N)r`   r  rM  rp  r'   r   r0   r   r   s    r   test_receiveAvoidsTimeout-TimeoutFactoryTests.test_receiveAvoidsTimeout  s    
 	

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

 45 	

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

())667r   )r`   r   r   rS   rp  N)
r2   r3   r4   r5   rY   rz  r  r  r  r6   r7   r   r   rx  rx  L  s    7"8888r   rx  c                   H    \ rS rSrSrSrSrS rS rS r	SS	 jr
S
 rS rSrg)TimeoutTesteri  z
A testable protocol with timeout facility.

@ivar timedOut: set to C{True} if a timeout has been detected.
@type timedOut: C{bool}
rd  Fc                     Xl         g)z6
Initialize the protocol with a C{task.Clock} object.
Nrn   )r   r`   s     r   r   TimeoutTester.__init__  s	     
r   c                 :    U R                  U R                  5        g)z#
Upon connection, set the timeout.
N)re  timeOutr   s    r   r$   TimeoutTester.connectionMade  s     	%r   c                 b    U R                  5         [        R                  R                  X5        g)z
Reset the timeout on data.
N)resetTimeoutr	   r   r0   r.   s     r   r0   TimeoutTester.dataReceived  s$     	&&t2r   Nc                 &    U R                  S5        g)z4
On connection lost, cancel all timeout operations.
N)re  r(   s     r   r*   TimeoutTester.connectionLost  s     	r   c                     SU l         g)zH
Flags the timedOut variable to indicate the timeout of the connection.
TN)timedOutr   s    r   timeoutConnectionTimeoutTester.timeoutConnection  s     r   c                 B    U R                   R                  " X/UQ70 UD6$ )z4
Override callLater to use the deterministic clock.
rg   )r   timeoutrk   rb   rc   s        r   rh   TimeoutTester.callLater  s#     zz##GCDCFCCr   )r`   r  r   )r2   r3   r4   r5   rY   r  r  r   r$   r0   r*   r  rh   r6   r7   r   r   r  r    s2     GH&3Dr   r  c                   H    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rSrg)TimeoutMixinTestsi  z%
Tests for L{policies.TimeoutMixin}.
c                 l    [         R                  " 5       U l        [        U R                  5      U l        g)zI
Create a testable, deterministic clock and a C{TimeoutTester} instance.
N)r   r@  r`   r  r   r   s    r   rz  TimeoutMixinTests.setUp  s!     ZZ\
"4::.
r   c                     U R                   R                  S5        U R                  [        U R                  R
                  5      S5        g)z
Test that the callLater of the clock is used instead of
L{reactor.callLater<twisted.internet.interfaces.IReactorTime.callLater>}
rG  r    N)r   re  r   r!  r`   callsr   s    r   test_overriddenCallLater*TimeoutMixinTests.test_overriddenCallLater  s5    
 	

b!TZZ--.2r   c                 R   U R                   R                  [        5       5        U R                  R	                  / SQ5        U R                  U R                   R                  5        U R                  R	                  SS/5        U R                  U R                   R                  5        g)zY
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   r`   r  rM  r  r   r   s    r   r  TimeoutMixinTests.test_timeout  sn    
 	

!!/"34 	

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

C!

++,r   c                    U R                   R                  [        5       5        U R                  R	                  / SQ5        U R                  U R                   R                  5        U R                   R                  S5        U R                  R	                  / SQ5        U R                  U R                   R                  5        U R                  R	                  SS/5        U R                  U R                   R                  5        g)zF
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   r`   r  rM  r  r0   r   r   s    r   test_noTimeout TimeoutMixinTests.test_noTimeout  s     	

!!/"34

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

/

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

C!

++,r   c                    SU R                   l        U R                   R                  [        5       5        U R                   R	                  S5        U R                  U R                   R                  S5        U R                  R                  SS/5        U R                  U R                   R                  5        U R                  R                  SS/5        U R                  U R                   R                  5        g)za
Check that setting a new value for timeout cancel the previous value
and install a new timeout.
Nr    r   g?r  )r   r  r   r   re  r   r`   r  rM  r  r   r   s    r   test_resetTimeout#TimeoutMixinTests.test_resetTimeout  s    
 "



!!/"34

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

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

C!

++,r   c                 p   SU R                   l        U R                   R                  [        5       5        U R                   R	                  S5        U R                  U R                   R                  5        U R                  R                  / SQ5        U R                  U R                   R                  5        g)z?
Setting the timeout to L{None} cancel any timeout operations.
   N)r   r  r  r  )
r   r  r   r   re  r   r`   r  rM  r  r   s    r   rj  $TimeoutMixinTests.test_cancelTimeout  sw     



!!/"34

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

%,,-r   c                    SU R                   l        U R                  U R                   R                  S5      S5        U R                  U R                   R                  S5      S5        U R	                  U R                   R                  S5      5        U R                  U R                   R                  S5        U R                   R                  S5        g)z=
setTimeout should return the value of the previous timeout.
r  rG  Nr    )r   r  r   re  r   r   s    r   test_setTimeoutReturn'TimeoutMixinTests.test_setTimeoutReturn  s     

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

d#r   c                 Z   U R                   R                  S5        U R                  R                  5       S   R	                  5         U R                  U R                   R                  5        U R                   R                  S5        U R                  U R                   R                  5        g)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.
rd  r   N)r   re  r`   getDelayedCallsrt  r   r  r   r   s    r   %test_setTimeoutCancleAlreadyCancelled7TimeoutMixinTests.test_setTimeoutCancleAlreadyCancelled,  sx     	

a  	

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

d#$**,,-r   )r`   r   N)r2   r3   r4   r5   rY   rz  r  r  r  r  rj  r  r  r6   r7   r   r   r  r    s/    /3--- .$.r   r  c                   $    \ rS rSrSrS rS rSrg)!LimitTotalConnectionsFactoryTestsi=  z/Tests for policies.LimitTotalConnectionsFactoryc                    [         R                  " 5       n[        R                  Ul        U R	                  SUR
                  5        UR                  S 5      nU R	                  SUR
                  5        UR                  S 5      nU R	                  SUR
                  5        UR                  S 5        U R	                  SUR
                  5        UR                  S 5        U R	                  SUR
                  5        g )Nr   r    r  )r   LimitTotalConnectionsFactoryr	   r   r   connectionCountrA   r*   )r   r   p1p2s       r   testConnectionCounting8LimitTotalConnectionsFactoryTests.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                   ^ [         R                  " 5       m[        R                  Tl        STl        TR                  S 5      nU R                  U5        U R                  STR                  5        U R                  TR                  S 5      5        U R                  STR                  5         " U4S jS[        R                  5      nUTl
        STl        TR                  S 5      nUR                  S 5        U R                  TR                  5        U R                  STR                  5        UR                  S 5        U R                  STR                  5        UR                  S 5        U R                  STR                  5        g )Nr    c                   "   > \ rS rSrU 4S jrSrg)RLimitTotalConnectionsFactoryTests.testConnectionLimiting.<locals>.OverflowProtocolie  c                    > STl         g rG   )
overflowed)r   r   s    r   r$   aLimitTotalConnectionsFactoryTests.testConnectionLimiting.<locals>.OverflowProtocol.connectionMadef  s    %)"r   r7   N)r2   r3   r4   r5   r$   r6   )r   s   r   OverflowProtocolr  e  s    * *r   r  Fr  r   )r   r  r	   r   connectionLimitrA   r   r   r  r   overflowProtocolr  r   r   r*   )r   r=   r  opr   s       @r   testConnectionLimiting8LimitTotalConnectionsFactoryTests.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   r7   N)r2   r3   r4   r5   rY   r  r  r6   r7   r   r   r  r  =  s    95($5r   r  c                       \ rS rSrS rSrg)WriteSequenceEchoProtocoli{  c                     UR                  S5      S:w  a  U R                  R                  U/5        g [        R	                  X5        g )Ns   vector!)findrS   rA  rD   r0   )r   bytess     r   r0   &WriteSequenceEchoProtocol.dataReceived|  s5    ::j!R'NN((%1%%d2r   r7   N)r2   r3   r4   r5   r0   r6   r7   r   r   r  r  {  s    3r   r  c                       \ rS rSrSrS rSrg)TestLoggingFactoryi  Nc                 `    U R                   b   S5       e[        5       U l         U R                   $ )Nzopen() called too many times)openFiler   )r   names     r   openTestLoggingFactory.open  s+    }}$D&DD$ 
}}r   )r  )r2   r3   r4   r5   r  r  r6   r7   r   r   r  r    s    Hr   r  c                   *    \ rS rSrSrS rS rS rSrg)LoggingFactoryTestsi  z.
Tests for L{policies.TrafficLoggingFactory}.
c                    [        5       n[        Ul        [        5       n[	        US5      nUR                  S5      nXBl        UR                  U5        UR                  R                  5       nU R                  SU5        U R                  UR                  5       5        UR                  S5        UR                  R                  5       nU R                  SR                  S5      U5        U R                  SR                  S5      U5        U R                  UR                  5       S5        UR                  5         UR                  S5        UR                  R                  5       nU R                  SR                  S/5      U5        U R                  UR                  5       S5        UR!                  5         UR                  R                  5       nU R                  S	U5        g
)zA
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)rW   r  r	   r   r  rA   r   r  getvalueassertInrM  rB  r0   formatr   rX  r   )r   r`  tr   r=   vs         r   test_thingsGetLogged(LoggingFactoryTests.test_thingsGetLogged  sq     ";,.~v6OO-.
	JJ!c1#	-.JJ!k(()?@!Dk(()?@!D$:;			<=JJ!l))+P*QRTUV$IJ	JJ!&*r   c                    [        5       n[        US5      nU R                  UR                  S5        UR	                  S5        U R                  UR                  S5        SUl        UR	                  S5        U R                  UR                  S5        UR                  5         U R                  UR                  S5        g)z7
Test counter management with the resetCounter method.
r  r   r  r    N)r  i/  r  )rW   r  r   _counterrA   r  resetCounter)r   r`  r   s      r   test_counter LoggingFactoryTests.test_counter  s      ~v6Q'	)*Q'
	)*Q'	Q'r   c                   ^^	 / m/ m	UU	4S jnU R                  [        SU5        [        R                  " 5       n[        Ul        [
        R                  " US5      nUR                  [        R                  " SSS5      5      nUR                  [        R                  " SSS5      5      nS0 4nS	0 4nU R                  Xg/T5        U R                  UR                  UR                  /T	5        g
)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                  v   > TR                  X45        [        5       nU S   Ul        TR                  U5        U$ )z@
Mock for the open call to prevent actually opening a log file.
r   )r   r   r  )rb   rc   io
open_callsopen_rvaluess      r   mocked_openULoggingFactoryTests.test_loggingFactoryOpensLogfileAutomatically.<locals>.mocked_open  s:     tn-B1gBG#Ir   r  r  r}   r~   r^  i:0  )ztest-1w)ztest-2r  N)patchbuiltinsr	   r_  r   r   TrafficLoggingFactoryrA   r   r   r   logfile)
r   r  r`  r   first_protosecond_proto
first_callsecond_callr  r  s
           @@r   ,test_loggingFactoryOpensLogfileAutomatically@LoggingFactoryTests.test_loggingFactoryOpensLogfileAutomatically  s     
	 	

8V[1!//1"000H++{E:
 ,,{E:
 &r*
&+*2J?+--|/C/CDlSr   r7   N)	r2   r3   r4   r5   rY   r  r  r  r6   r7   r   r   r  r    s     +D("$Tr   r  ).rY   r  r  r   zope.interfacer   r   r   twisted.internetr   r   r	   r
   r   twisted.internet.testingr   r   twisted.protocolsr   twisted.trialr   r   r   ClientFactoryr9   rD   r_  rW   r_   r[   ru   rr   TestCaserz   r   r  r\  rx  TimeoutMixinr  r  r  r  r  r  r  r7   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   