o
    Óó·ib*  ã                   @  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ÚintZassertNotEqual)ÚselfÚaÚb© r   ú]C:\Users\psant\Downloads\PAF backend\school\venv\Lib\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   ÚwriteZ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#   Z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 )Nr0   zCannot pause ú IPushProducerÚpausedr2   r5   r   r   r   ÚpauseProducingR   ó   

z!StrictPushProducer.pauseProducingc                 C  r7   )Nr9   zCannot resume r8   r0   r2   r5   r   r   r   ÚresumeProducingW   r;   z"StrictPushProducer.resumeProducingNr*   )r+   r,   r-   r.   r3   r6   r:   r<   r   r   r   r   r/   D   s    

r/   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   r/   c                 C  s   t ƒ S )zp
        @return: A new L{StrictPushProducer} which has not been through any state
            changes.
        N)r/   r5   r   r   r   Ú_initialb   s   z StrictPushProducerTests._initialc                 C  ó   t ƒ }| ¡  |S )z@
        @return: A new, stopped L{StrictPushProducer}.
        N)r/   r6   ©r   Úproducerr   r   r   Ú_stoppedi   ó   z StrictPushProducerTests._stoppedc                 C  r?   )z?
        @return: A new, paused L{StrictPushProducer}.
        N)r/   r:   r@   r   r   r   Ú_pausedq   rC   zStrictPushProducerTests._pausedc                 C  s   t ƒ }| ¡  | ¡  |S )zY
        @return: A new L{StrictPushProducer} which has been paused and resumed.
        N)r/   r:   r<   r@   r   r   r   Ú_resumedy   s   z StrictPushProducerTests._resumedrA   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}
        r1   N©r$   r3   r@   r   r   r   ÚassertStopped‚   ó   z%StrictPushProducerTests.assertStoppedc                 C  rF   )z¨
        Assert that the given producer is in the paused state.

        @param producer: The producer to verify.
        @type producer: L{StrictPushProducer}
        r9   NrG   r@   r   r   r   ÚassertPaused‹   rI   z$StrictPushProducerTests.assertPausedc                 C  rF   )z©
        Assert that the given producer is in the running state.

        @param producer: The producer to verify.
        @type producer: L{StrictPushProducer}
        r0   NrG   r@   r   r   r   ÚassertRunning”   rI   z%StrictPushProducerTests.assertRunningc                 C  ó   |   t|  ¡ j¡ dS )zz
        L{StrictPushProducer.stopProducing} raises L{ValueError} if called when
        the producer is stopped.
        N)ÚassertRaisesr4   rB   r6   r5   r   r   r   Útest_stopThenStop�   ó   z)StrictPushProducerTests.test_stopThenStopc                 C  rL   )z{
        L{StrictPushProducer.pauseProducing} raises L{ValueError} if called when
        the producer is stopped.
        N)rM   r4   rB   r:   r5   r   r   r   Útest_stopThenPause¤   rO   z*StrictPushProducerTests.test_stopThenPausec                 C  rL   )z|
        L{StrictPushProducer.resumeProducing} raises L{ValueError} if called when
        the producer is stopped.
        N)rM   r4   rB   r<   r5   r   r   r   Útest_stopThenResume«   rO   z+StrictPushProducerTests.test_stopThenResumec                 C  ó   |   ¡ }| ¡  |  |¡ dS )zn
        L{StrictPushProducer} is stopped if C{stopProducing} is called on a paused
        producer.
        N)rD   r6   rH   r@   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)rD   rM   r4   r:   r@   r   r   r   Útest_pauseThenPause»   ó   z+StrictPushProducerTests.test_pauseThenPausec                 C  rR   )zp
        L{StrictPushProducer} is resumed if C{resumeProducing} is called on a
        paused producer.
        N)rD   r<   rK   r@   r   r   r   Útest_pauseThenResumeÃ   rT   z,StrictPushProducerTests.test_pauseThenResumec                 C  rR   )zo
        L{StrictPushProducer} is stopped if C{stopProducing} is called on a
        resumed producer.
        N)rE   r6   rH   r@   r   r   r   Útest_resumeThenStopÌ   rT   z+StrictPushProducerTests.test_resumeThenStopc                 C  rR   )zo
        L{StrictPushProducer} is paused if C{pauseProducing} is called on a
        resumed producer.
        N)rE   r:   rJ   r@   r   r   r   Útest_resumeThenPauseÕ   rT   z,StrictPushProducerTests.test_resumeThenPausec                 C  rU   )zu
        L{StrictPushProducer.resumeProducing} raises L{ValueError} if called on a
        resumed producer.
        N)rE   rM   r4   r<   r@   r   r   r   Útest_resumeThenResumeÞ   rW   z-StrictPushProducerTests.test_resumeThenResumec                 C  rR   )zn
        L{StrictPushProducer} is stopped if C{stopProducing} is called in the
        initial state.
        N)r>   r6   rH   r@   r   r   r   Ú	test_stopæ   rT   z!StrictPushProducerTests.test_stopc                 C  rR   )zn
        L{StrictPushProducer} is paused if C{pauseProducing} is called in the
        initial state.
        N)r>   r:   rJ   r@   r   r   r   Ú
test_pauseï   rT   z"StrictPushProducerTests.test_pausec                 C  rU   )zz
        L{StrictPushProducer} raises L{ValueError} if C{resumeProducing} is called
        in the initial state.
        N)r>   rM   r4   r<   r@   r   r   r   Útest_resumeø   rW   z#StrictPushProducerTests.test_resumeN)r   r/   )rA   r/   r   r   r*   )r+   r,   r-   r.   r>   rB   rD   rE   rH   rJ   rK   rN   rP   rQ   rS   rV   rX   rY   rZ   r[   r\   r]   r^   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)ZisServerF)Zgreet)ÚserverÚclient)Z	streamingr0   N)r   r   r   r/   ZregisterProducerÚpumpr$   r3   )	r   r`   ZserverProtoZserverTransportZclientProtoZclientTransportrd   rA   Z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.
        rb   ©r`   N©re   r5   r   r   r   Útest_serverStreamingProducer)  ó   z(IOPumpTests.test_serverStreamingProducerc                 C  rf   )z—
        L{IOPump.pump} does not call C{resumeProducing} on a L{IPushProducer}
        (stream producer) registered with the client transport.
        rc   rg   Nrh   r5   r   r   r   Útest_clientStreamingProducer0  rj   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   ©Ztime_passedr   r   Ú<lambda>>  s   
 z/IOPumpTests.test_timeAdvances.<locals>.<lambda>N)r	   r   r   Z	callLaterZassertFalserd   Ú
assertTrue)r   rl   Ú_rd   r   rn   r   Útest_timeAdvances7  s   
zIOPumpTests.test_timeAdvancesN)r`   ra   r   r   r*   )r+   r,   r-   r.   re   ri   rk   rr   r   r   r   r   r_     s    

#
r_   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
   Z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
   Z	listenTCPr   ZforProtocolr   Z
connectTCPr   r   r   rt   r   )r   Zmemru   r   r   r   Útest_someConnectionR  s
   z,ConnectionCompleterTests.test_someConnectionNr*   )r+   r,   r-   r.   rv   rx   r   r   r   r   rs   D  s    
rs   N)r.   Ú
__future__r   Útypingr   Zzope.interfacer   Ztwisted.internet.interfacesr   Ztwisted.internet.protocolr   r   r   Ztwisted.internet.taskr	   Ztwisted.internet.testingr
   Ztwisted.test.iosimr   r   r   r   r   Ztwisted.trial.unittestr   r   r/   r=   r_   rs   r   r   r   r   Ú<module>   s"   ) %C