o
    9Bib*                     @  s   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 deZeeG dd dZG dd deZG dd deZG dd deZdS )z"
Tests for L{twisted.test.iosim}.
    )annotations)Literal)implementer)IPushProducer)ClientFactoryFactoryProtocol)Clock)MemoryReactor)ConnectionCompleterFakeTransportIOPumpconnectconnectedServerAndClient)TestCasec                   @  s.   e Zd ZdZdddZdddZddd	Zd
S )FakeTransportTestsz%
    Tests for L{FakeTransport}.
    returnNonec                 C  sH   t t d}t t d}| |jt | |jt | |j|j dS )ze
        Each L{FakeTransport} receives a serial number that uniquely identifies
        it.
        TFN)r   objectassertIsInstanceserialintassertNotEqual)selfab r   W/var/www/html/Trade-python/venv/lib/python3.10/site-packages/twisted/test/test_iosim.pytest_connectionSerial    s
   z(FakeTransportTests.test_connectionSerialc                 C  s<   t t d}|d |g d | d|jd dS )zl
        L{FakeTransport.writeSequence} will write a sequence of L{bytes} to the
        transport.
        F   a)   b   c   d    s   abcdN)r   r   writewriteSequenceassertEqualjoinstreamr   r   r   r   r   test_writeSequence+   s   
z%FakeTransportTests.test_writeSequencec                 C  s@   t t d}|d |  |d | d|jd dS )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)   r   r   r   test_writeAfterClose7   s
   

z'FakeTransportTests.test_writeAfterCloseNr   r   )__name__
__module____qualname____doc__r   r*   r,   r   r   r   r   r      s
    

r   c                   @  s2   e Zd ZdZdZdddZdddZdd	d
ZdS )StrictPushProducerz
    An L{IPushProducer} implementation which produces nothing but enforces
    preconditions on its state transition methods.
    runningr   r   c                 C  s   | j dkr	tdd| _ d S )Nstoppedz)Cannot stop already-stopped IPushProducer_state
ValueErrorr   r   r   r   stopProducingM   s   

z StrictPushProducer.stopProducingc                 C  &   | j dkrtd| j  dd| _ d S )Nr3   zCannot pause  IPushProducerpausedr5   r8   r   r   r   pauseProducingR      

z!StrictPushProducer.pauseProducingc                 C  r:   )Nr<   zCannot resume r;   r3   r5   r8   r   r   r   resumeProducingW   r>   z"StrictPushProducer.resumeProducingNr-   )r.   r/   r0   r1   r6   r9   r=   r?   r   r   r   r   r2   D   s    

r2   c                   @  s   e Zd ZdZd-ddZd-ddZd-dd	Zd-d
dZd.ddZd.ddZ	d.ddZ
d/ddZd/ddZd/ddZd/ddZd/ddZd/ddZd/d d!Zd/d"d#Zd/d$d%Zd/d&d'Zd/d(d)Zd/d*d+Zd,S )0StrictPushProducerTestsz*
    Tests for L{StrictPushProducer}.
    r   r2   c                 C  s   t  S )zp
        @return: A new L{StrictPushProducer} which has not been through any state
            changes.
        )r2   r8   r   r   r   _initialb   s   z StrictPushProducerTests._initialc                 C     t  }|  |S )z@
        @return: A new, stopped L{StrictPushProducer}.
        )r2   r9   r   producerr   r   r   _stoppedi      z StrictPushProducerTests._stoppedc                 C  rB   )z?
        @return: A new, paused L{StrictPushProducer}.
        )r2   r=   rC   r   r   r   _pausedq   rF   zStrictPushProducerTests._pausedc                 C  s   t  }|  |  |S )zY
        @return: A new L{StrictPushProducer} which has been paused and resumed.
        )r2   r=   r?   rC   r   r   r   _resumedy   s   z StrictPushProducerTests._resumedrD   r   c                 C     |  |jd dS )z
        Assert that the given producer is in the stopped state.

        @param producer: The producer to verify.
        @type producer: L{StrictPushProducer}
        r4   Nr&   r6   rC   r   r   r   assertStopped      z%StrictPushProducerTests.assertStoppedc                 C  rI   )z
        Assert that the given producer is in the paused state.

        @param producer: The producer to verify.
        @type producer: L{StrictPushProducer}
        r<   NrJ   rC   r   r   r   assertPaused   rL   z$StrictPushProducerTests.assertPausedc                 C  rI   )z
        Assert that the given producer is in the running state.

        @param producer: The producer to verify.
        @type producer: L{StrictPushProducer}
        r3   NrJ   rC   r   r   r   assertRunning   rL   z%StrictPushProducerTests.assertRunningc                 C     |  t|  j dS )zz
        L{StrictPushProducer.stopProducing} raises L{ValueError} if called when
        the producer is stopped.
        N)assertRaisesr7   rE   r9   r8   r   r   r   test_stopThenStop      z)StrictPushProducerTests.test_stopThenStopc                 C  rO   )z{
        L{StrictPushProducer.pauseProducing} raises L{ValueError} if called when
        the producer is stopped.
        N)rP   r7   rE   r=   r8   r   r   r   test_stopThenPause   rR   z*StrictPushProducerTests.test_stopThenPausec                 C  rO   )z|
        L{StrictPushProducer.resumeProducing} raises L{ValueError} if called when
        the producer is stopped.
        N)rP   r7   rE   r?   r8   r   r   r   test_stopThenResume   rR   z+StrictPushProducerTests.test_stopThenResumec                 C     |   }|  | | dS )zn
        L{StrictPushProducer} is stopped if C{stopProducing} is called on a paused
        producer.
        N)rG   r9   rK   rC   r   r   r   test_pauseThenStop      z*StrictPushProducerTests.test_pauseThenStopc                 C     |   }| t|j dS )zs
        L{StrictPushProducer.pauseProducing} raises L{ValueError} if called on a
        paused producer.
        N)rG   rP   r7   r=   rC   r   r   r   test_pauseThenPause      z+StrictPushProducerTests.test_pauseThenPausec                 C  rU   )zp
        L{StrictPushProducer} is resumed if C{resumeProducing} is called on a
        paused producer.
        N)rG   r?   rN   rC   r   r   r   test_pauseThenResume   rW   z,StrictPushProducerTests.test_pauseThenResumec                 C  rU   )zo
        L{StrictPushProducer} is stopped if C{stopProducing} is called on a
        resumed producer.
        N)rH   r9   rK   rC   r   r   r   test_resumeThenStop   rW   z+StrictPushProducerTests.test_resumeThenStopc                 C  rU   )zo
        L{StrictPushProducer} is paused if C{pauseProducing} is called on a
        resumed producer.
        N)rH   r=   rM   rC   r   r   r   test_resumeThenPause   rW   z,StrictPushProducerTests.test_resumeThenPausec                 C  rX   )zu
        L{StrictPushProducer.resumeProducing} raises L{ValueError} if called on a
        resumed producer.
        N)rH   rP   r7   r?   rC   r   r   r   test_resumeThenResume   rZ   z-StrictPushProducerTests.test_resumeThenResumec                 C  rU   )zn
        L{StrictPushProducer} is stopped if C{stopProducing} is called in the
        initial state.
        N)rA   r9   rK   rC   r   r   r   	test_stop   rW   z!StrictPushProducerTests.test_stopc                 C  rU   )zn
        L{StrictPushProducer} is paused if C{pauseProducing} is called in the
        initial state.
        N)rA   r=   rM   rC   r   r   r   
test_pause   rW   z"StrictPushProducerTests.test_pausec                 C  rX   )zz
        L{StrictPushProducer} raises L{ValueError} if C{resumeProducing} is called
        in the initial state.
        N)rA   rP   r7   r?   rC   r   r   r   test_resume   rZ   z#StrictPushProducerTests.test_resumeN)r   r2   )rD   r2   r   r   r-   )r.   r/   r0   r1   rA   rE   rG   rH   rK   rM   rN   rQ   rS   rT   rV   rY   r[   r\   r]   r^   r_   r`   ra   r   r   r   r   r@   ]   s*    




	
	
	
	



	

	
	
	

		r@   c                   @  s8   e Zd ZdZdddZddd	Zdd
dZdddZdS )IOPumpTestsz
    Tests for L{IOPump}.
    modeLiteral['server', 'client']r   r   c           	      C  sr   t  }t|dd}t  }t|dd}t||||dd}t }||d| }|j|dd |  | d|j dS )	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)	streamingr3   N)r   r   r   r2   registerProducerpumpr&   r6   )	r   rc   serverProtoserverTransportclientProtoclientTransportrk   rD   victimr   r   r   _testStreamingProducer  s(   z"IOPumpTests._testStreamingProducerc                 C     | j dd dS )z
        L{IOPump.pump} does not call C{resumeProducing} on a L{IPushProducer}
        (stream producer) registered with the server transport.
        rg   rc   Nrq   r8   r   r   r   test_serverStreamingProducer)     z(IOPumpTests.test_serverStreamingProducerc                 C  rr   )z
        L{IOPump.pump} does not call C{resumeProducing} on a L{IPushProducer}
        (stream producer) registered with the client transport.
        rh   rs   Nrt   r8   r   r   r   test_clientStreamingProducer0  rv   z(IOPumpTests.test_clientStreamingProducerc                   sR   g  t  }ttt|d\}}}|d fdd |   |  |   dS )zE
        L{IOPump.pump} advances time in the given L{Clock}.
        )clockr   c                     s
     dS )NT)appendr   time_passedr   r   <lambda>>  s   
 z/IOPumpTests.test_timeAdvances.<locals>.<lambda>N)r	   r   r   	callLaterassertFalserk   
assertTrue)r   rx   _rk   r   rz   r   test_timeAdvances7  s   
zIOPumpTests.test_timeAdvancesN)rc   rd   r   r   r-   )r.   r/   r0   r1   rq   ru   rw   r   r   r   r   r   rb     s    

#
rb   c                   @  s$   e Zd ZdZd	ddZd	ddZdS )
ConnectionCompleterTestszr
    L{ConnectionCompleter} can connect a connecting client and a listening
    server in a L{MemoryReactor}.
    r   r   c                 C  s   t t }| | d dS )z|
        When no connection for its reactor is pending,
        L{ConnectionCompleter.succeedOnce} returns C{None}.
        N)r   r
   assertIssucceedOnce)r   ccr   r   r   test_noConnectionJ  s   
z*ConnectionCompleterTests.test_noConnectionc                 C  sH   t  }|dtt |ddtt t|}| |	 t
 dS )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   r   r   r   test_someConnectionR  s
   z,ConnectionCompleterTests.test_someConnectionNr-   )r.   r/   r0   r1   r   r   r   r   r   r   r   D  s    
r   N)r1   
__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   r2   r@   rb   r   r   r   r   r   <module>   s"   ) %C