Ë
    ojW&  ã                  óÜ   — 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
 ddlmZ ddl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y)z"
Tests for L{twisted.test.iosim}.
é    )Úannotations)ÚLiteral)Úimplementer)ÚIPushProducer)ÚProtocol)ÚClock)ÚFakeTransportÚ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      ú9/usr/lib/python3/dist-packages/twisted/test/test_iosim.pyÚtest_connectionSerialz(FakeTransportTests.test_connectionSerial   s`   € ô
 œ&›( DÓ)ˆÜœ&›( EÓ*ˆØ×Ñ˜aŸh™h¬Ô,Ø×Ñ˜aŸh™h¬Ô,Ø×Ñ˜AŸH™H a§h¡hÕ/ó    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_writeAfterClose0   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.stopProducingF   s!   € Ø�;‰;˜)Ò#ÜÐHÓIÐIØˆ�r   c                ób   — | j                   dk7  rt        d| j                   › d�«      ‚d| _         y )Nr4   zCannot pause ú IPushProducerÚpausedr7   r:   s    r   ÚpauseProducingz!StrictPushProducer.pauseProducingK   s-   € Ø�;‰;˜)Ò#Ü˜}¨T¯[©[¨M¸ÐHÓIÐIØˆ�r   c                ób   — | j                   dk7  rt        d| j                   › d�«      ‚d| _         y )Nr>   zCannot resume r=   r4   r7   r:   s    r   ÚresumeProducingz"StrictPushProducer.resumeProducingP   s-   € Ø�;‰;˜(Ò"Ü˜~¨d¯k©k¨]¸.ÐIÓJÐJØˆ�r   Nr*   )r-   r.   r/   r0   r8   r;   r?   rA   r1   r   r   r3   r3   =   s   „ ñð
 €Fó ó
ô
 r   r3   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.
        )r3   r:   s    r   Ú_initialz StrictPushProducerTests._initial[   s   € ô
 "Ó#Ð#r   c                ó:   — t        «       }|j                  «        |S )z@
        @return: A new, stopped L{StrictPushProducer}.
        )r3   r;   ©r   Úproducers     r   Ú_stoppedz StrictPushProducerTests._stoppedb   s   € ô &Ó'ˆØ×ÑÔ Øˆr   c                ó:   — t        «       }|j                  «        |S )z?
        @return: A new, paused L{StrictPushProducer}.
        )r3   r?   rG   s     r   Ú_pausedzStrictPushProducerTests._pausedj   s   € ô &Ó'ˆØ×ÑÔ!Øˆr   c                óZ   — t        «       }|j                  «        |j                  «        |S )zY
        @return: A new L{StrictPushProducer} which has been paused and resumed.
        )r3   r?   rA   rG   s     r   Ú_resumedz StrictPushProducerTests._resumedr   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}
        r6   N©r"   r8   rG   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}
        r>   NrO   rG   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}
        r4   NrO   rG   s     r   ÚassertRunningz%StrictPushProducerTests.assertRunning�   rQ   r   c                ó`   — | j                  t        | j                  «       j                  «       y)zz
        L{StrictPushProducer.stopProducing} raises L{ValueError} if called when
        the producer is stopped.
        N)ÚassertRaisesr9   rI   r;   r:   s    r   Útest_stopThenStopz)StrictPushProducerTests.test_stopThenStop–   s    € ð
 	×Ñœ* d§m¡m£o×&CÑ&CÕDr   c                ó`   — | j                  t        | j                  «       j                  «       y)z{
        L{StrictPushProducer.pauseProducing} raises L{ValueError} if called when
        the producer is stopped.
        N)rW   r9   rI   r?   r:   s    r   Útest_stopThenPausez*StrictPushProducerTests.test_stopThenPause�   s    € ð
 	×Ñœ* d§m¡m£o×&DÑ&DÕEr   c                ó`   — | j                  t        | j                  «       j                  «       y)z|
        L{StrictPushProducer.resumeProducing} raises L{ValueError} if called when
        the producer is stopped.
        N)rW   r9   rI   rA   r:   s    r   Útest_stopThenResumez+StrictPushProducerTests.test_stopThenResume¤   s    € ð
 	×Ñœ* d§m¡m£o×&EÑ&EÕFr   c                óf   — | j                  «       }|j                  «        | j                  |«       y)zn
        L{StrictPushProducer} is stopped if C{stopProducing} is called on a paused
        producer.
        N)rK   r;   rP   rG   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)rK   rW   r9   r?   rG   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)rK   rA   rU   rG   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)rM   r;   rP   rG   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)rM   r?   rS   rG   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)rM   rW   r9   rA   rG   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)rE   r;   rP   rG   s     r   Ú	test_stopz!StrictPushProducerTests.test_stopß   re   r   c                óf   — | j                  «       }|j                  «        | j                  |«       y)zn
        L{StrictPushProducer} is paused if C{pauseProducing} is called in the
        initial state.
        N)rE   r?   rS   rG   s     r   Ú
test_pausez"StrictPushProducerTests.test_pauseè   rh   r   c                ód   — | j                  «       }| j                  t        |j                  «       y)zz
        L{StrictPushProducer} raises L{ValueError} if C{resumeProducing} is called
        in the initial state.
        N)rE   rW   r9   rA   rG   s     r   Útest_resumez#StrictPushProducerTests.test_resumeñ   rk   r   N)r+   r3   )rH   r3   r+   r,   r*   )r-   r.   r/   r0   rE   rI   rK   rM   rP   rS   rU   rX   rZ   r\   r^   r`   rb   rd   rg   rj   rm   ro   rq   r1   r   r   rC   rC   V   sk   „ ñó$óóóó5ó4ó5óEóFóGó%ó?ó%ó%ó$ó@ó%ó$ô@r   rC   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)Ú	streamingr4   N)r   r	   r
   r3   ÚregisterProducerÚpumpr"   r8   )	r   ÚmodeÚserverProtoÚserverTransportÚclientProtoÚclientTransportr{   rH   Úvictims	            r   Ú_testStreamingProducerz"IOPumpTests._testStreamingProducerÿ   s–   € ô “jˆÜ'¨¸dÔCˆä“jˆÜ'¨¸eÔDˆäØØØØØô
ˆô &Ó'ˆà%Ø%ñ
ð ñˆð 	×Ñ °DÐÔ9à�	‰	ŒØ×Ñ˜ H§O¡OÕ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.
        rw   ©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.
        rx   r„   Nr…   r:   s    r   Útest_clientStreamingProducerz(IOPumpTests.test_clientStreamingProducer)  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>7  s   ø€  ;×#5Ñ#5°dÓ#;€ r   N)r   r   r   Ú	callLaterÚassertFalser{   Ú
assertTrue)r   r‹   Ú_r{   rŽ   s       @r   Útest_timeAdvanceszIOPumpTests.test_timeAdvances0  sW   ø€ ð ˆÜ“ˆÜ-¬h¼ÈÔN‰
ˆˆ1ˆdØ�‰˜Ó;Ô<Ø×Ñ˜Ô%Ø�	‰	ŒØ�‰˜Õ$r   N)r|   zLiteral['server', 'client']r+   r,   r*   )r-   r.   r/   r0   r‚   r†   r‰   r”   r1   r   r   rs   rs   ú   s   „ ñó!5óF3ó3ô
%r   rs   N)r0   Ú
__future__r   Útypingr   Úzope.interfacer   Útwisted.internet.interfacesr   Útwisted.internet.protocolr   Útwisted.internet.taskr   Útwisted.test.iosimr	   r
   r   Útwisted.trial.unittestr   r   r3   rC   rs   r1   r   r   ú<module>r�      sq   ðñõ #å å &å 5Ý .Ý 'ß OÑ OÝ +ô&8˜ô &8ñR ˆ]Ó÷ ð  ó ð ô0a@˜hô a@ôH@%�(õ @%r   