
    h7                     r   S r SSKJr  SSKJrJrJr  SSKJr  SSK	J
r
JrJr  SSKJr  SSKJrJr  SSKJr   " S	 S
\R(                  5      r " S S\5      r " S S5      r " S S\\R0                  5      r " S S\\R0                  5      r " S S\\R0                  5      r " S S\R0                  5      rg)z.
Test case for L{twisted.protocols.loopback}.
    )implementer)defer
interfacesreactor)Deferred)IAddressIPullProducerIPushProducer)Protocol)basicloopback)unittestc                   ,    \ rS rSrS rS rS rS rSrg)SimpleProtocol   c                 T    [         R                  " 5       U l        / U l        / U l        g N)r   r   connlinesconnLostselfs    a/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/test/test_loopback.py__init__SimpleProtocol.__init__   s    NN$	
    c                 :    U R                   R                  S 5        g r   )r   callbackr   s    r   connectionMadeSimpleProtocol.connectionMade   s    		4 r   c                 :    U R                   R                  U5        g r   )r   appendr   lines     r   lineReceivedSimpleProtocol.lineReceived   s    

$r   c                 :    U R                   R                  U5        g r   )r   r"   )r   reasons     r   connectionLostSimpleProtocol.connectionLost   s    V$r   )r   r   r   N)	__name__
__module____qualname____firstlineno__r   r   r%   r)   __static_attributes__ r   r   r   r      s    
! %r   r   c                       \ rS rSrSrS rSrg)DoomProtocol#   r   c                    U =R                   S-  sl         U R                   S:  a  U R                  SU R                   4-  5        [        R                  X5        U R                  S   S:X  a  U R
                  R                  5         g g )N      s   Hello %d   Hello 3)isendLiner   r%   r   	transportloseConnectionr#   s     r   r%   DoomProtocol.lineReceived&   sf    !66A: MM+	12##D/::b>Z'NN))+ (r   r0   N)r+   r,   r-   r.   r9   r%   r/   r0   r   r   r2   r2   #   s    	A,r   r2   c                        \ rS rSrS rS rSrg)LoopbackTestCaseMixin1   c                    ^ ^^ [        5       m[        5       mU4S jnTR                  R                  U5        UUU 4S jn[        R                  " T R
                  TT5      nUR                  U5        U$ )Nc                 \   > TR                  S5        TR                  R                  5         g )N   THIS IS LINE ONE!)r:   r;   r<   resultss    r   	sendALine<LoopbackTestCaseMixin.testRegularFunction.<locals>.sendALine6   s     JJ+,KK&&(r   c                    > TR                  TR                  S/5        TR                  [        TR                  5      S5        TR                  [        TR                  5      S5        g )NrC   r5   assertEqualr   lenr   ignoredcrF   r   s    r   check8LoopbackTestCaseMixin.testRegularFunction.<locals>.check<   sK    QWW';&<=S_a0S_a0r   )r   r   addCallbackr   maybeDeferredloopbackFuncr   rG   rP   drO   rF   s   `   @@r   testRegularFunction)LoopbackTestCaseMixin.testRegularFunction2   s^    	) 	
9%	1
  1 11a8	er   c                    ^ ^^ [        5       m[        5       mU4S jnTR                  R                  U5        UUU 4S jn[        R                  " T R
                  TT5      nUR                  U5        U$ )Nc                 (   > TR                  S5        g )N	   DOOM LINE)r:   rD   s    r   rG   =LoopbackTestCaseMixin.testSneakyHiddenDoom.<locals>.sendALineI   s    JJ|$r   c                   > TR                  TR                  / SQ5        TR                  TR                  / SQ5        TR                  [        TR                  5      S5        TR                  [        TR                  5      S5        g )N)   Hello 1   Hello 2r8   )r[   r^   r_   r8   r5   rJ   rM   s    r   rP   9LoopbackTestCaseMixin.testSneakyHiddenDoom.<locals>.checkN   sb    QWW&JKK S_a0S_a0r   )r2   r   rR   r   rS   rT   rU   s   `   @@r   testSneakyHiddenDoom*LoopbackTestCaseMixin.testSneakyHiddenDoomE   s\    NN	% 	
9%	1  1 11a8	er   r0   N)r+   r,   r-   r.   rW   ra   r/   r0   r   r   r?   r?   1   s    &r   r?   c                       \ rS rSr\" \R                  5      rS rS r	S r
S rSS jrS rS rS	 rS
 rSS jrS rS rS rS rS rS rS rS rSrg)LoopbackAsyncTests[   c                      " S S[         5      nU" 5       nU" 5       n[        R                  " X#5        U R                  UR                  5        U R                  UR                  5        g)ze
Test that the client and server protocol both have makeConnection
invoked on them by loopbackAsync.
c                       \ rS rSrSrS rSrg)<LoopbackAsyncTests.test_makeConnection.<locals>.TestProtocold   Nc                     Xl         g r   r;   r   r;   s     r   makeConnectionKLoopbackAsyncTests.test_makeConnection.<locals>.TestProtocol.makeConnectiong       !*r   rk   )r+   r,   r-   r.   r;   rm   r/   r0   r   r   TestProtocolrh   d   s    I+r   rp   N)r   r   loopbackAsyncassertIsNotNoner;   )r   rp   serverclients       r   test_makeConnection&LoopbackAsyncTests.test_makeConnection^   sT    	+8 	+ v.V--.V--.r   c                   ^ ^  " S S[         5      nU(       a"  U" 5       n[        5       =oTl        [        5       nO![        5       nU" 5       n[        5       =oVl        [        R                  " XF5        UU 4S jnUR                  U5      $ )z:
Test one of the permutations of client/server host/peer.
c                       \ rS rSrS rSrg)6LoopbackAsyncTests._hostpeertest.<locals>.TestProtocolu   c                 f    [         R                  " X5        U R                  R                  U5        g r   )r   rm   onConnectionr   rl   s     r   rm   ELoopbackAsyncTests._hostpeertest.<locals>.TestProtocol.makeConnectionv   s$    ''8!!**95r   r0   N)r+   r,   r-   r.   rm   r/   r0   r   r   rp   ry   u   s    6r   rp   c                 r   > [        U T5      " 5       nTR                  [        R                  " U5      5        g r   )getattr
assertTruer   
providedBy)r;   hostgetr   s     r   	connected3LoopbackAsyncTests._hostpeertest.<locals>.connected   s)    9c*,DOOH//56r   )r   r   r|   r   rq   rR   )r   r   
testServerrp   rs   rV   rt   r   s   ``      r   _hostpeertest LoopbackAsyncTests._hostpeertestp   sn    
	68 	6
 !^F&.j0A#ZFZF!^F&.j0A#v.	7 }}Y''r   c                 &    U R                  SS5      $ )zl
Test that the server gets a transport with a properly functioning
implementation of L{ITransport.getHost}.
getHostTr   r   s    r   test_serverHost"LoopbackAsyncTests.test_serverHost   s    
 !!)T22r   c                 &    U R                  SS5      $ )z7
Like C{test_serverHost} but for L{ITransport.getPeer}
getPeerTr   r   s    r   test_serverPeer"LoopbackAsyncTests.test_serverPeer   s     !!)T22r   c                 &    U R                  SS5      $ )zl
Test that the client gets a transport with a properly functioning
implementation of L{ITransport.getHost}.
r   Fr   )r   r   s     r   test_clientHost"LoopbackAsyncTests.test_clientHost   s    
 !!)U33r   c                 &    U R                  SS5      $ )z8
Like C{test_clientHost} but for L{ITransport.getPeer}.
r   Fr   r   s    r   test_clientPeer"LoopbackAsyncTests.test_clientPeer   s     !!)U33r   c                    ^  " S S[         5      n " U4S jS[         5      nU(       a  U" 5       nU" 5       n[        5       =ovl        OU" 5       n[        5       =oul        U" 5       n[        R                  " XV5        U$ )z
Test one of the permutations of write/writeSequence client/server.

@param write: The name of the method to test, C{"write"} or
    C{"writeSequence"}.
c                       \ rS rSrSrS rSrg)9LoopbackAsyncTests._greetingtest.<locals>.GreeteeProtocol   r   c                     U =R                   U-  sl         U R                   S:X  a  U R                  R                  S 5        g g N   bytes)bytesreceivedr   r   r   s     r   dataReceivedFLoopbackAsyncTests._greetingtest.<locals>.GreeteeProtocol.dataReceived   s3    

e#
::)MM**40 *r   r0   Nr+   r,   r-   r.   r   r   r/   r0   r   r   GreeteeProtocolr      s    E1r   r   c                   "   > \ rS rSrU 4S jrSrg)9LoopbackAsyncTests._greetingtest.<locals>.GreeterProtocol   c                    > TS:X  a  U R                   R                  S5        g U R                   R                  SS/5        g )Nwriter   s   byts   es)r;   r   writeSequencer   r   s    r   r   HLoopbackAsyncTests._greetingtest.<locals>.GreeterProtocol.connectionMade   s3    G#NN((2NN00&%Ar   r0   Nr+   r,   r-   r.   r   r/   r   s   r   GreeterProtocolr      s    B Br   r   )r   r   r   r   rq   )r   r   r   r   r   rs   rt   rV   s    `      r   _greetingtest LoopbackAsyncTests._greetingtest   sm    	1h 	1	Bh 	B $&F$&F"**,A$&F"**,A$&Fv.r   c                 &    U R                  SS5      $ )zl
Test that on a connection where the client speaks first, the server
receives the bytes sent by the client.
r   Fr   r   s    r   test_clientGreeting&LoopbackAsyncTests.test_clientGreeting   s    
 !!'511r   c                 &    U R                  SS5      $ )zb
Like C{test_clientGreeting}, but use C{writeSequence} instead of
C{write} to issue the greeting.
r   Fr   r   s    r   test_clientGreetingSequence.LoopbackAsyncTests.test_clientGreetingSequence   s    
 !!/599r   c                 &    U R                  SS5      $ )zl
Test that on a connection where the server speaks first, the client
receives the bytes sent by the server.
r   Tr   r   s     r   test_serverGreeting&LoopbackAsyncTests.test_serverGreeting   s    
 !!'400r   c                 &    U R                  SS5      $ )zb
Like C{test_serverGreeting}, but use C{writeSequence} instead of
C{write} to issue the greeting.
r   Tr   r   s    r   test_serverGreetingSequence.LoopbackAsyncTests.test_serverGreetingSequence   s    
 !!/488r   c                 $  ^^^^ [        SS5       Vs/ s H	  nSU4-  PM     snm " UU4S jS[        5      n " UUU4S jS[        5      nU" 5       mU" 5       m[        5       Tl        [        R
                  " TT5        TR                  $ s  snf )Nr   
   s   %dc                   $   > \ rS rSrU U4S jrSrg);LoopbackAsyncTests._producertest.<locals>.ProducingProtocol   c                 |   > T" [        T5      5      U l        U R                  R                  U R                  5        g r   )listproducerstartr;   )r   producerClass	toProduces    r   r   JLoopbackAsyncTests._producertest.<locals>.ProducingProtocol.connectionMade   s)     -d9o >##DNN3r   )r   Nr   )r   r   s   r   ProducingProtocolr      s    4 4r   r   c                   *   > \ rS rSrSrU UU4S jrSrg);LoopbackAsyncTests._producertest.<locals>.ReceivingProtocol   r   c                    > U =R                   U-  sl         U R                   SR                  T5      :X  a  U R                  R                  TT45        g g )Nr   )r   joinr   r   )r   datart   rs   r   s     r   r   HLoopbackAsyncTests._producertest.<locals>.ReceivingProtocol.dataReceived   sB    

d"
::)!44MM**FF+;< 5r   r0   Nr   )rt   rs   r   s   r   ReceivingProtocolr      s    E= =r   r   )ranger   r   r   r   rq   )r   r   r9   r   r   rt   rs   r   s    `   @@@r   _producertest LoopbackAsyncTests._producertest   s    +0B<8<aUaT\<8		4 	4 	4
	= 	= 	= #$"$"*vv.) 9s   Bc                    ^  [        [        5       " S S5      5       nT R                  U5      nU 4S jnUR                  U5        U$ )z?
Test a push producer registered against a loopback transport.
c                   0    \ rS rSrSrS rS rS rS rSr	g)	:LoopbackAsyncTests.test_pushProducer.<locals>.PushProducer   Fc                     Xl         g r   r   r   r   s     r   r   CLoopbackAsyncTests.test_pushProducer.<locals>.PushProducer.__init__  ro   r   c                     SU l         g NT)resumedr   s    r   resumeProducingJLoopbackAsyncTests.test_pushProducer.<locals>.PushProducer.resumeProducing  s	    #r   c                 T    Xl         UR                  U S5        U R                  5         g r   )consumerregisterProducer_produceAndScheduler   r   s     r   r   @LoopbackAsyncTests.test_pushProducer.<locals>.PushProducer.start
  s#     ())$5((*r   c                    U R                   (       aV  U R                  R                  U R                   R                  S5      5        [        R
                  " SU R                  5        g U R                  R                  5         g Nr   )r   r   r   popr   	callLaterr   unregisterProducerr   s    r   r   NLoopbackAsyncTests.test_pushProducer.<locals>.PushProducer._produceAndSchedule  sP    >>MM''(:(:1(=>%%a)A)ABMM446r   )r   r   r   N)
r+   r,   r-   r.   r   r   r   r   r   r/   r0   r   r   PushProducerr      s    G+$+
7r   r   c                 Z   > U u  pTR                  UR                  R                  S5        g )Nz0Streaming producer should not have been resumed.)assertFalser   r   )resultsrt   rs   r   s      r   finished6LoopbackAsyncTests.test_pushProducer.<locals>.finished  s)    &V''Br   )r   r
   r   rR   )r   r   rV   r   s   `   r   test_pushProducer$LoopbackAsyncTests.test_pushProducer   sK    
 
]	#	7 	7 
$	7* |,	 	
hr   c                 \    [        [        5       " S S5      5       nU R                  U5      $ )z?
Test a pull producer registered against a loopback transport.
c                   &    \ rS rSrS rS rS rSrg):LoopbackAsyncTests.test_pullProducer.<locals>.PullProduceri'  c                     Xl         g r   r   r   s     r   r   CLoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.__init__)  ro   r   c                 H    Xl         U R                   R                  U S5        g )NF)r   r   r   s     r   r   @LoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.start,  s     (..tU;r   c                     U R                   R                  U R                  R                  S5      5        U R                  (       d  U R                   R	                  5         g g r   )r   r   r   r   r   r   s    r   r   JLoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.resumeProducing0  s?    ##DNN$6$6q$9:~~MM446 &r   )r   r   N)r+   r,   r-   r.   r   r   r   r/   r0   r   r   PullProducerr   '  s    +<7r   r  )r   r	   r   )r   r  s     r   test_pullProducer$LoopbackAsyncTests.test_pullProducer"  s3    
 
]	#	7 	7 
$	7 !!,//r   c                    ^ ^  " S S[         5      n " S S[         5      nU" 5       nU" 5       m[        R                  " TU5      nUU 4S jnUR                  U5        U$ )z
L{loopback.loopbackAsync} does not call a protocol's C{dataReceived}
method while that protocol's transport's C{write} method is higher up
on the stack.
c                       \ rS rSrS rSrg)9LoopbackAsyncTests.test_writeNotReentrant.<locals>.Serveri>  c                 :    U R                   R                  S5        g r   )r;   r   r   s     r   r   FLoopbackAsyncTests.test_writeNotReentrant.<locals>.Server.dataReceived?  s    $$X.r   r0   N)r+   r,   r-   r.   r   r/   r0   r   r   Serverr  >  s    /r   r
  c                   *    \ rS rSrSrS rS rS rSrg)9LoopbackAsyncTests.test_writeNotReentrant.<locals>.ClientiB  Fc                 F    [         R                  " SU R                  5        g r   )r   r   gor   s    r   r   HLoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.connectionMadeE  s    !!!TWW-r   c                 H    U R                   R                  S5        SU l        g )N   fooT)r;   r   readyr   s    r   r  <LoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.goH  s    $$V,!
r   c                 Z    U R                   U l        U R                  R                  5         g r   )r  wasReadyr;   r<   r   s     r   r   FLoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.dataReceivedL  s     $

--/r   )r  r  N)	r+   r,   r-   r.   r  r   r  r   r/   r0   r   r   Clientr  B  s    E."0r   r  c                 <   > TR                  TR                  5        g r   )r   r  )rN   rt   r   s    r   
cbFinished=LoopbackAsyncTests.test_writeNotReentrant.<locals>.cbFinishedT  s    OOFOO,r   )r   r   rq   rR   )r   r
  r  rs   rV   r  rt   s   `     @r   test_writeNotReentrant)LoopbackAsyncTests.test_writeNotReentrant7  sU    	/X 	/	0X 	0 ""662	- 	
j!r   c                   ^ ^^^ / mU4S jn[        5       m[        5       m[        R                  " TTU5      nT R                  T/ 5        TR                  R                  S5        TR                  R                  S5        TR                  R                  S5        TR                  R                  S5        TR                  R                  5         UUU U4S jnUR                  U5        U$ )z
The callable passed as the value for the C{pumpPolicy} parameter to
L{loopbackAsync} is called with a L{_LoopbackQueue} of pending bytes
and a protocol to which they should be delivered.
c                    > / nU (       a(  UR                  U R                  5       5        U (       a  M(  TR                  X45        g r   )r"   r   )queuetargetr   	pumpCallss      r   dummyPolicy7LoopbackAsyncTests.test_pumpPolicy.<locals>.dummyPolicyb  s4    EUYY[) %f_-r   r     bar   baz   quuxc                 >   > TR                  TT/ SQ4TSS/4/5        g )N)r%  r&  Nr  r$  )rK   )rN   rt   r!  r   rs   s    r   
cbComplete6LoopbackAsyncTests.test_pumpPolicy.<locals>.cbCompletet  s.     12Vff=M4NO	r   )r   r   rq   rK   r;   r   r<   rR   )r   r"  r   r(  rt   r!  rs   s   `   @@@r   test_pumpPolicy"LoopbackAsyncTests.test_pumpPolicyZ  s     		. ))&&+FB'v&v&v&w''')	 	 	Z(r   c                 "   / n[        5       nUR                  Ul        [        R                  " 5       nUR                  S5        UR                  S5        UR                  S5        [        R                  " X25        U R                  USS/5        g)z
L{identityPumpPolicy} is a pump policy which calls the target's
C{dataReceived} method one for each string in the queue passed to it.
r  r$  N)r   r"   r   r   _LoopbackQueueputidentityPumpPolicyrK   r   r   rt   r  s       r   test_identityPumpPolicy*LoopbackAsyncTests.test_identityPumpPolicy  sp    
 #ll'')		&		&		$##E2 01r   c                     / n[        5       nUR                  Ul        [        R                  " 5       nUR                  S5        UR                  S5        UR                  S5        [        R                  " X25        U R                  US/5        g)z
L{collapsingPumpPolicy} is a pump policy which calls the target's
C{dataReceived} only once with all of the strings in the queue passed
to it joined together.
r  r$  Ns   foobar)r   r"   r   r   r-  r.  collapsingPumpPolicyrK   r0  s       r   test_collapsingPumpPolicy,LoopbackAsyncTests.test_collapsingPumpPolicy  sm     #ll'')		&		&		$%%e4,r   r0   N)r   r   )r+   r,   r-   r.   staticmethodr   rq   rT   ru   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r*  r1  r5  r/   r0   r   r   rd   rd   [   sp     6 67L/$(63344!F2:19.%N0*!F#J2"-r   rd   c                   8    \ rS rSr\" \R                  5      rSrg)LoopbackTCPTestsi  r0   N)	r+   r,   r-   r.   r7  r   loopbackTCPrT   r/   r0   r   r   r9  r9    s     4 45Lr   r9  c                   j    \ rS rSr\" \R                  5      r\R                  " \
S5      c  SrSrgSrg)LoopbackUNIXTestsi  Nz-Current reactor does not support UNIX socketsr0   )r+   r,   r-   r.   r7  r   loopbackUNIXrT   r   IReactorUNIXr   skipr/   r0   r   r   r<  r<    s/     5 56Lw-5> 6r   r<  c                   :    \ rS rSrSr " S S\5      rS rS rSr	g)	LoopbackRelayTesti  z6
Test for L{twisted.protocols.loopback.LoopbackRelay}
c                   "    \ rS rSrSrSrS rSrg)LoopbackRelayTest.Receiveri  z6
Simple Receiver class used for testing LoopbackRelay
r   c                 .    U =R                   U-  sl         g)z)Accumulate received data for verificationN)r   )r   r   s     r   r   'LoopbackRelayTest.Receiver.dataReceived  s    IIIr   r0   N)r+   r,   r-   r.   __doc__r   r   r/   r0   r   r   ReceiverrC    s    	 	r   rG  c                 $   U R                  5       n[        R                  " U5      nUR                  S5        UR                  S5        U R	                  UR
                  S5        UR                  5         U R	                  UR
                  S5        g)z8Test to verify that the write function works as expecteds   abcs   defr   s   abcdefN)rG  r   LoopbackRelayr   rK   r   clearBufferr   receiverrelays      r   
test_writeLoopbackRelayTest.test_write  si    ==?&&x0FF,	2r   c                 ,   U R                  5       n[        R                  " U5      nUR                  / SQ5        UR                  / SQ5        U R	                  UR
                  S5        UR                  5         U R	                  UR
                  S5        g)z@Test to verify that the writeSequence function works as expected)s   The s   quick s   brown s   fox )s   jumps s   over s   the lazy dogr   s+   The quick brown fox jumps over the lazy dogN)rG  r   rI  r   rK   r   rJ  rK  s      r   test_writeSequence$LoopbackRelayTest.test_writeSequence  sp    ==?&&x0DEBC,(VWr   r0   N)
r+   r,   r-   r.   rF  r   rG  rN  rQ  r/   r0   r   r   rA  rA    s    	8 	3Xr   rA  N)rF  zope.interfacer   twisted.internetr   r   r   twisted.internet.deferr   twisted.internet.interfacesr   r	   r
   twisted.internet.protocolr   twisted.protocolsr   r   twisted.trialr   LineReceiverr   r2   r?   TestCaserd   r9  r<  rA  r0   r   r   <module>r\     s   
 ' 7 7 + N N . - "%U'' % ,> ,' 'TE-.0A0A E-P
6,h.?.? 6?-x/@/@ ?"X)) "Xr   