
    hb*                       S r SSKJr  SSKJr  SSK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  SS	KJrJrJrJrJr  SS
KJr   " S S\5      r\" \5       " S S5      5       r " S S\5      r " S S\5      r " S S\5      rg)z"
Tests for L{twisted.test.iosim}.
    )annotations)Literal)implementer)IPushProducer)ClientFactoryFactoryProtocol)Clock)MemoryReactor)ConnectionCompleterFakeTransportIOPumpconnectconnectedServerAndClient)TestCasec                  6    \ rS rSrSrSS jrSS jrSS jrSrg)	FakeTransportTests   z
Tests for L{FakeTransport}.
c                    [        [        5       S5      n[        [        5       S5      nU R                  UR                  [        5        U R                  UR                  [        5        U R                  UR                  UR                  5        g)zM
Each L{FakeTransport} receives a serial number that uniquely identifies
it.
TFN)r   objectassertIsInstanceserialintassertNotEqual)selfabs      ^/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/test/test_iosim.pytest_connectionSerial(FakeTransportTests.test_connectionSerial    s`    
 &(D)&(E*ahh,ahh,AHHahh/    c                    [        [        5       S5      nUR                  S5        UR                  / SQ5        U R	                  SR                  UR                  5      S5        g)zT
L{FakeTransport.writeSequence} will write a sequence of L{bytes} to the
transport.
F   a)   b   c   dr!   s   abcdN)r   r   writewriteSequenceassertEqualjoinstreamr   r   s     r   test_writeSequence%FakeTransportTests.test_writeSequence+   sG    
 &(E*		*+!((+W5r!   c                    [        [        5       S5      nUR                  S5        UR                  5         UR                  S5        U R	                  SR                  UR                  5      S5        g)zp
L{FakeTransport.write} will accept writes after transport was closed,
but the data will be silently discarded.
Fs   befores   afterr!   N)r   r   r'   loseConnectionr)   r*   r+   r,   s     r   test_writeAfterClose'FakeTransportTests.test_writeAfterClose7   sS    
 &(E*				!((+Y7r!    NreturnNone)	__name__
__module____qualname____firstlineno____doc__r   r-   r1   __static_attributes__r3   r!   r   r   r      s    	0
6
8r!   r   c                  :    \ rS rSrSrSrS	S jrS	S jrS	S jrSr	g)
StrictPushProducerD   zw
An L{IPushProducer} implementation which produces nothing but enforces
preconditions on its state transition methods.
runningc                H    U R                   S:X  a  [        S5      eSU l         g )Nstoppedz)Cannot stop already-stopped IPushProducer_state
ValueErrorr   s    r   stopProducing StrictPushProducer.stopProducingM   s!    ;;)#HIIr!   c                d    U R                   S:w  a  [        SU R                    S35      eSU l         g )Nr@   zCannot pause  IPushProducerpausedrC   rF   s    r   pauseProducing!StrictPushProducer.pauseProducingR   s-    ;;)#}T[[MHIIr!   c                d    U R                   S:w  a  [        SU R                    S35      eSU l         g )NrK   zCannot resume rJ   r@   rC   rF   s    r   resumeProducing"StrictPushProducer.resumeProducingW   s-    ;;("~dkk].IJJr!   )rD   Nr4   )
r7   r8   r9   r:   r;   rD   rG   rL   rO   r<   r3   r!   r   r>   r>   D   s    
 F 

 r!   r>   c                      \ rS rSrSrSS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSrg)StrictPushProducerTests]   z"
Tests for L{StrictPushProducer}.
c                    [        5       $ )zX
@return: A new L{StrictPushProducer} which has not been through any state
    changes.
)r>   rF   s    r   _initial StrictPushProducerTests._initialb   s    
 "##r!   c                :    [        5       nUR                  5         U$ )z0
@return: A new, stopped L{StrictPushProducer}.
)r>   rG   r   producers     r   _stopped StrictPushProducerTests._stoppedi   s     &' r!   c                :    [        5       nUR                  5         U$ )z/
@return: A new, paused L{StrictPushProducer}.
)r>   rL   rX   s     r   _pausedStrictPushProducerTests._pausedq   s     &'!r!   c                Z    [        5       nUR                  5         UR                  5         U$ )zI
@return: A new L{StrictPushProducer} which has been paused and resumed.
)r>   rL   rO   rX   s     r   _resumed StrictPushProducerTests._resumedy   s)     &'!  "r!   c                <    U R                  UR                  S5        g)z
Assert that the given producer is in the stopped state.

@param producer: The producer to verify.
@type producer: L{StrictPushProducer}
rB   Nr)   rD   rX   s     r   assertStopped%StrictPushProducerTests.assertStopped        	)4r!   c                <    U R                  UR                  S5        g)z
Assert that the given producer is in the paused state.

@param producer: The producer to verify.
@type producer: L{StrictPushProducer}
rK   Nrc   rX   s     r   assertPaused$StrictPushProducerTests.assertPaused   s     	(3r!   c                <    U R                  UR                  S5        g)z
Assert that the given producer is in the running state.

@param producer: The producer to verify.
@type producer: L{StrictPushProducer}
r@   Nrc   rX   s     r   assertRunning%StrictPushProducerTests.assertRunning   rf   r!   c                `    U R                  [        U R                  5       R                  5        g)zb
L{StrictPushProducer.stopProducing} raises L{ValueError} if called when
the producer is stopped.
N)assertRaisesrE   rZ   rG   rF   s    r   test_stopThenStop)StrictPushProducerTests.test_stopThenStop   s     
 	*dmmo&C&CDr!   c                `    U R                  [        U R                  5       R                  5        g)zc
L{StrictPushProducer.pauseProducing} raises L{ValueError} if called when
the producer is stopped.
N)rn   rE   rZ   rL   rF   s    r   test_stopThenPause*StrictPushProducerTests.test_stopThenPause   s     
 	*dmmo&D&DEr!   c                `    U R                  [        U R                  5       R                  5        g)zd
L{StrictPushProducer.resumeProducing} raises L{ValueError} if called when
the producer is stopped.
N)rn   rE   rZ   rO   rF   s    r   test_stopThenResume+StrictPushProducerTests.test_stopThenResume   s     
 	*dmmo&E&EFr!   c                f    U R                  5       nUR                  5         U R                  U5        g)zV
L{StrictPushProducer} is stopped if C{stopProducing} is called on a paused
producer.
N)r]   rG   rd   rX   s     r   test_pauseThenStop*StrictPushProducerTests.test_pauseThenStop   s)    
 <<> 8$r!   c                d    U R                  5       nU R                  [        UR                  5        g)z[
L{StrictPushProducer.pauseProducing} raises L{ValueError} if called on a
paused producer.
N)r]   rn   rE   rL   rX   s     r   test_pauseThenPause+StrictPushProducerTests.test_pauseThenPause   s%    
 <<>*h&=&=>r!   c                f    U R                  5       nUR                  5         U R                  U5        g)zX
L{StrictPushProducer} is resumed if C{resumeProducing} is called on a
paused producer.
N)r]   rO   rk   rX   s     r   test_pauseThenResume,StrictPushProducerTests.test_pauseThenResume   s)    
 <<>  "8$r!   c                f    U R                  5       nUR                  5         U R                  U5        g)zW
L{StrictPushProducer} is stopped if C{stopProducing} is called on a
resumed producer.
N)r`   rG   rd   rX   s     r   test_resumeThenStop+StrictPushProducerTests.test_resumeThenStop   )    
 ==? 8$r!   c                f    U R                  5       nUR                  5         U R                  U5        g)zW
L{StrictPushProducer} is paused if C{pauseProducing} is called on a
resumed producer.
N)r`   rL   rh   rX   s     r   test_resumeThenPause,StrictPushProducerTests.test_resumeThenPause   )    
 ==?!(#r!   c                d    U R                  5       nU R                  [        UR                  5        g)z]
L{StrictPushProducer.resumeProducing} raises L{ValueError} if called on a
resumed producer.
N)r`   rn   rE   rO   rX   s     r   test_resumeThenResume-StrictPushProducerTests.test_resumeThenResume   %    
 ==?*h&>&>?r!   c                f    U R                  5       nUR                  5         U R                  U5        g)zV
L{StrictPushProducer} is stopped if C{stopProducing} is called in the
initial state.
N)rU   rG   rd   rX   s     r   	test_stop!StrictPushProducerTests.test_stop   r   r!   c                f    U R                  5       nUR                  5         U R                  U5        g)zV
L{StrictPushProducer} is paused if C{pauseProducing} is called in the
initial state.
N)rU   rL   rh   rX   s     r   
test_pause"StrictPushProducerTests.test_pause   r   r!   c                d    U R                  5       nU R                  [        UR                  5        g)zb
L{StrictPushProducer} raises L{ValueError} if C{resumeProducing} is called
in the initial state.
N)rU   rn   rE   rO   rX   s     r   test_resume#StrictPushProducerTests.test_resume   r   r!   r3   N)r5   r>   )rY   r>   r5   r6   r4   )r7   r8   r9   r:   r;   rU   rZ   r]   r`   rd   rh   rk   ro   rr   ru   rx   r{   r~   r   r   r   r   r   r   r<   r3   r!   r   rR   rR   ]   sk    $545EFG%?%%$@%$@r!   rR   c                  @    \ rS rSrSrS	S jrS
S jrS
S jrS
S jrSr	g)IOPumpTestsi  z
Tests for L{IOPump}.
c                
   [        5       n[        USS9n[        5       n[        USS9n[        UUUUSS9n[        5       nUUS.U   nUR	                  USS9  UR                  5         U R                  SUR                  5        g)	a  
Connect a couple protocol/transport pairs to an L{IOPump} and then pump
it.  Verify that a streaming producer registered with one of the
transports does not receive invalid L{IPushProducer} method calls and
ends in the right state.

@param mode: C{u"server"} to test a producer registered with the
    server transport.  C{u"client"} to test a producer registered with
    the client transport.
T)isServerF)greet)serverclient)	streamingr@   N)r	   r   r   r>   registerProducerpumpr)   rD   )	r   modeserverProtoserverTransportclientProtoclientTransportr   rY   victims	            r   _testStreamingProducer"IOPumpTests._testStreamingProducer  s     j'dCj'eD
 &'%%
  	D9		HOO4r!   c                "    U R                  SS9  g)z
L{IOPump.pump} does not call C{resumeProducing} on a L{IPushProducer}
(stream producer) registered with the server transport.
r   r   Nr   rF   s    r   test_serverStreamingProducer(IOPumpTests.test_serverStreamingProducer)      
 	###2r!   c                "    U R                  SS9  g)z
L{IOPump.pump} does not call C{resumeProducing} on a L{IPushProducer}
(stream producer) registered with the client transport.
r   r   Nr   rF   s    r   test_clientStreamingProducer(IOPumpTests.test_clientStreamingProducer0  r   r!   c                   ^ / m[        5       n[        [        [        US9u    p#UR                  SU4S j5        U R	                  T5        UR                  5         U R                  T5        g)z5
L{IOPump.pump} advances time in the given L{Clock}.
)clockr   c                 &   > T R                  S5      $ )NT)append)time_passeds   r   <lambda>/IOPumpTests.test_timeAdvances.<locals>.<lambda>>  s    ;#5#5d#;r!   N)r
   r   r	   	callLaterassertFalser   
assertTrue)r   r   _r   r   s       @r   test_timeAdvancesIOPumpTests.test_timeAdvances7  sU     -hN
1;<%		$r!   r3   N)r   zLiteral['server', 'client']r5   r6   r4   )
r7   r8   r9   r:   r;   r   r   r   r   r<   r3   r!   r   r   r     s    !5F33
%r!   r   c                  ,    \ rS rSrSrSS jrSS jrSrg)ConnectionCompleterTestsiD  zf
L{ConnectionCompleter} can connect a connecting client and a listening
server in a L{MemoryReactor}.
c                j    [        [        5       5      nU R                  UR                  5       S5        g)zd
When no connection for its reactor is pending,
L{ConnectionCompleter.succeedOnce} returns C{None}.
N)r   r   assertIssucceedOnce)r   ccs     r   test_noConnection*ConnectionCompleterTests.test_noConnectionJ  s%    
 !1bnn&-r!   c                    [        5       nUR                  S[        R                  " [        5      5        UR                  SS[        R                  " [        5      5        [        U5      nU R                  UR                  5       [        5        g)zw
When a connection to a matched host and port is pending,
L{ConnectionCompleter} returns an L{IOPump} connecting them.
P   z	127.0.0.1N)r   	listenTCPr   forProtocolr	   
connectTCPr   r   r   r   r   )r   memr   s      r   test_someConnection,ConnectionCompleterTests.test_someConnectionR  s`    
 ob'--h78{B(A(A((KL %bnn.7r!   r3   Nr4   )r7   r8   r9   r:   r;   r   r   r<   r3   r!   r   r   r   D  s    
.	8r!   r   N)r;   
__future__r   typingr   zope.interfacer   twisted.internet.interfacesr   twisted.internet.protocolr   r   r	   twisted.internet.taskr
   twisted.internet.testingr   twisted.test.iosimr   r   r   r   r   twisted.trial.unittestr   r   r>   rR   r   r   r3   r!   r   <module>r      s    #  & 5 F F ' 2  ,&8 &8R ]     0a@h a@H@%( @%F8x 8r!   