
    Vhb*                       d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZ dd	lmZmZmZmZmZ dd
lmZ  G d de      Z ee       G d d             Z G d de      Z G d de      Z G d de      Zy)z"
Tests for L{twisted.test.iosim}.
    )annotations)Literal)implementer)IPushProducer)ClientFactoryFactoryProtocol)Clock)MemoryReactor)ConnectionCompleterFakeTransportIOPumpconnectconnectedServerAndClient)TestCasec                  (    e Zd ZdZddZddZddZy)FakeTransportTestsz%
    Tests for L{FakeTransport}.
    c                    t        t               d      }t        t               d      }| j                  |j                  t               | j                  |j                  t               | j                  |j                  |j                         y)ze
        Each L{FakeTransport} receives a serial number that uniquely identifies
        it.
        TFN)r   objectassertIsInstanceserialintassertNotEqual)selfabs      G/home/dcms/DCMS/lib/python3.12/site-packages/twisted/test/test_iosim.pytest_connectionSerialz(FakeTransportTests.test_connectionSerial    s`    
 &(D)&(E*ahh,ahh,AHHahh/    c                    t        t               d      }|j                  d       |j                  g d       | j	                  dj                  |j                        d       y)zl
        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_writeSequencez%FakeTransportTests.test_writeSequence+   sG    
 &(E*		*+!((+W5r   c                    t        t               d      }|j                  d       |j                          |j                  d       | j	                  dj                  |j                        d       y)z
        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_writeAfterClosez'FakeTransportTests.test_writeAfterClose7   sS    
 &(E*				!((+Y7r   NreturnNone)__name__
__module____qualname____doc__r   r+   r.    r   r   r   r      s    	0
6
8r   r   c                  ,    e Zd ZdZdZddZddZddZy)StrictPushProducerz
    An L{IPushProducer} implementation which produces nothing but enforces
    preconditions on its state transition methods.
    runningc                F    | j                   dk(  rt        d      d| _         y )Nstoppedz)Cannot stop already-stopped IPushProducer_state
ValueErrorr   s    r   stopProducingz StrictPushProducer.stopProducingM   s!    ;;)#HIIr   c                b    | j                   dk7  rt        d| j                    d      d| _         y )Nr9   zCannot pause  IPushProducerpausedr<   r?   s    r   pauseProducingz!StrictPushProducer.pauseProducingR   s-    ;;)#}T[[MHIIr   c                b    | j                   dk7  rt        d| j                    d      d| _         y )NrC   zCannot resume rB   r9   r<   r?   s    r   resumeProducingz"StrictPushProducer.resumeProducingW   s-    ;;("~dkk].IJJr   Nr/   )r2   r3   r4   r5   r=   r@   rD   rF   r6   r   r   r8   r8   D   s    
 F 

 r   r8   c                      e Zd ZdZddZddZddZddZddZddZ	ddZ
dd	Zdd
ZddZddZddZddZddZddZddZddZddZddZy)StrictPushProducerTestsz*
    Tests for L{StrictPushProducer}.
    c                    t               S )zp
        @return: A new L{StrictPushProducer} which has not been through any state
            changes.
        )r8   r?   s    r   _initialz StrictPushProducerTests._initialb   s    
 "##r   c                :    t               }|j                          |S )z@
        @return: A new, stopped L{StrictPushProducer}.
        )r8   r@   r   producers     r   _stoppedz StrictPushProducerTests._stoppedi   s     &' r   c                :    t               }|j                          |S )z?
        @return: A new, paused L{StrictPushProducer}.
        )r8   rD   rL   s     r   _pausedzStrictPushProducerTests._pausedq   s     &'!r   c                Z    t               }|j                          |j                          |S )zY
        @return: A new L{StrictPushProducer} which has been paused and resumed.
        )r8   rD   rF   rL   s     r   _resumedz StrictPushProducerTests._resumedy   s)     &'!  "r   c                <    | j                  |j                  d       y)z
        Assert that the given producer is in the stopped state.

        @param producer: The producer to verify.
        @type producer: L{StrictPushProducer}
        r;   Nr'   r=   rL   s     r   assertStoppedz%StrictPushProducerTests.assertStopped        	)4r   c                <    | j                  |j                  d       y)z
        Assert that the given producer is in the paused state.

        @param producer: The producer to verify.
        @type producer: L{StrictPushProducer}
        rC   NrT   rL   s     r   assertPausedz$StrictPushProducerTests.assertPaused   s     	(3r   c                <    | j                  |j                  d       y)z
        Assert that the given producer is in the running state.

        @param producer: The producer to verify.
        @type producer: L{StrictPushProducer}
        r9   NrT   rL   s     r   assertRunningz%StrictPushProducerTests.assertRunning   rV   r   c                `    | j                  t        | j                         j                         y)zz
        L{StrictPushProducer.stopProducing} raises L{ValueError} if called when
        the producer is stopped.
        N)assertRaisesr>   rN   r@   r?   s    r   test_stopThenStopz)StrictPushProducerTests.test_stopThenStop   s     
 	*dmmo&C&CDr   c                `    | j                  t        | j                         j                         y)z{
        L{StrictPushProducer.pauseProducing} raises L{ValueError} if called when
        the producer is stopped.
        N)r\   r>   rN   rD   r?   s    r   test_stopThenPausez*StrictPushProducerTests.test_stopThenPause   s     
 	*dmmo&D&DEr   c                `    | j                  t        | j                         j                         y)z|
        L{StrictPushProducer.resumeProducing} raises L{ValueError} if called when
        the producer is stopped.
        N)r\   r>   rN   rF   r?   s    r   test_stopThenResumez+StrictPushProducerTests.test_stopThenResume   s     
 	*dmmo&E&EFr   c                f    | j                         }|j                          | j                  |       y)zn
        L{StrictPushProducer} is stopped if C{stopProducing} is called on a paused
        producer.
        N)rP   r@   rU   rL   s     r   test_pauseThenStopz*StrictPushProducerTests.test_pauseThenStop   s)    
 <<> 8$r   c                d    | j                         }| j                  t        |j                         y)zs
        L{StrictPushProducer.pauseProducing} raises L{ValueError} if called on a
        paused producer.
        N)rP   r\   r>   rD   rL   s     r   test_pauseThenPausez+StrictPushProducerTests.test_pauseThenPause   s%    
 <<>*h&=&=>r   c                f    | j                         }|j                          | j                  |       y)zp
        L{StrictPushProducer} is resumed if C{resumeProducing} is called on a
        paused producer.
        N)rP   rF   rZ   rL   s     r   test_pauseThenResumez,StrictPushProducerTests.test_pauseThenResume   s)    
 <<>  "8$r   c                f    | j                         }|j                          | j                  |       y)zo
        L{StrictPushProducer} is stopped if C{stopProducing} is called on a
        resumed producer.
        N)rR   r@   rU   rL   s     r   test_resumeThenStopz+StrictPushProducerTests.test_resumeThenStop   )    
 ==? 8$r   c                f    | j                         }|j                          | j                  |       y)zo
        L{StrictPushProducer} is paused if C{pauseProducing} is called on a
        resumed producer.
        N)rR   rD   rX   rL   s     r   test_resumeThenPausez,StrictPushProducerTests.test_resumeThenPause   )    
 ==?!(#r   c                d    | j                         }| j                  t        |j                         y)zu
        L{StrictPushProducer.resumeProducing} raises L{ValueError} if called on a
        resumed producer.
        N)rR   r\   r>   rF   rL   s     r   test_resumeThenResumez-StrictPushProducerTests.test_resumeThenResume   %    
 ==?*h&>&>?r   c                f    | j                         }|j                          | j                  |       y)zn
        L{StrictPushProducer} is stopped if C{stopProducing} is called in the
        initial state.
        N)rJ   r@   rU   rL   s     r   	test_stopz!StrictPushProducerTests.test_stop   rj   r   c                f    | j                         }|j                          | j                  |       y)zn
        L{StrictPushProducer} is paused if C{pauseProducing} is called in the
        initial state.
        N)rJ   rD   rX   rL   s     r   
test_pausez"StrictPushProducerTests.test_pause   rm   r   c                d    | j                         }| j                  t        |j                         y)zz
        L{StrictPushProducer} raises L{ValueError} if C{resumeProducing} is called
        in the initial state.
        N)rJ   r\   r>   rF   rL   s     r   test_resumez#StrictPushProducerTests.test_resume   rp   r   N)r0   r8   )rM   r8   r0   r1   r/   )r2   r3   r4   r5   rJ   rN   rP   rR   rU   rX   rZ   r]   r_   ra   rc   re   rg   ri   rl   ro   rr   rt   rv   r6   r   r   rH   rH   ]   sk    $545EFG%?%%$@%$@r   rH   c                  0    e Zd ZdZddZddZddZddZy)	IOPumpTestsz
    Tests for L{IOPump}.
    c                "   t               }t        |d      }t               }t        |d      }t        ||||d      }t               }||d|   }|j	                  |d       |j                          | j                  d|j                         y)	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)	streamingr9   N)r	   r   r   r8   registerProducerpumpr'   r=   )	r   modeserverProtoserverTransportclientProtoclientTransportr   rM   victims	            r   _testStreamingProducerz"IOPumpTests._testStreamingProducer  s     j'dCj'eD
 &'%%
  	D9		HOO4r   c                (    | j                  d       y)z
        L{IOPump.pump} does not call C{resumeProducing} on a L{IPushProducer}
        (stream producer) registered with the server transport.
        r|   r   Nr   r?   s    r   test_serverStreamingProducerz(IOPumpTests.test_serverStreamingProducer)      
 	###2r   c                (    | j                  d       y)z
        L{IOPump.pump} does not call C{resumeProducing} on a L{IPushProducer}
        (stream producer) registered with the client transport.
        r}   r   Nr   r?   s    r   test_clientStreamingProducerz(IOPumpTests.test_clientStreamingProducer0  r   r   c                    g t               }t        t        t        |      \  }}}|j                  dfd       | j	                         |j                          | j                         y)zE
        L{IOPump.pump} advances time in the given L{Clock}.
        )clockr   c                 &     j                  d      S )NT)append)time_passeds   r   <lambda>z/IOPumpTests.test_timeAdvances.<locals>.<lambda>>  s    ;#5#5d#; r   N)r
   r   r	   	callLaterassertFalser   
assertTrue)r   r   _r   r   s       @r   test_timeAdvanceszIOPumpTests.test_timeAdvances7  sW     -hN
1d;<%		$r   N)r   zLiteral['server', 'client']r0   r1   r/   )r2   r3   r4   r5   r   r   r   r   r6   r   r   rx   rx     s    !5F33
%r   rx   c                       e Zd ZdZddZddZy)ConnectionCompleterTestszr
    L{ConnectionCompleter} can connect a connecting client and a listening
    server in a L{MemoryReactor}.
    c                j    t        t                     }| j                  |j                         d       y)z|
        When no connection for its reactor is pending,
        L{ConnectionCompleter.succeedOnce} returns C{None}.
        N)r   r   assertIssucceedOnce)r   ccs     r   test_noConnectionz*ConnectionCompleterTests.test_noConnectionJ  s%    
 !1bnn&-r   c                   t               }|j                  dt        j                  t                     |j                  ddt        j                  t                     t        |      }| j                  |j                         t               y)z
        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_someConnectionz,ConnectionCompleterTests.test_someConnectionR  s`    
 ob'--h78{B(A(A((KL %bnn.7r   Nr/   )r2   r3   r4   r5   r   r   r6   r   r   r   r   D  s    
.	8r   r   N)r5   
__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   r8   rH   rx   r   r6   r   r   <module>r      s    #  & 5 F F ' 2  ,&8 &8R ]     0a@h a@H@%( @%F8x 8r   