U
     T‚gW&  ã                   @  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
 ddlmZ ddl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dS )z"
Tests for L{twisted.test.iosim}.
é    )Úannotations)ÚLiteral)Úimplementer)ÚIPushProducer)ÚProtocol)ÚClock)ÚFakeTransportÚconnectÚconnectedServerAndClient)ÚTestCasec                   @  s:   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zd
S )ÚFakeTransportTestsz%
    Tests for L{FakeTransport}.
    ÚNone©Úreturnc                 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   ÚobjectZassertIsInstanceÚserialÚintZassertNotEqual)ÚselfÚaÚb© r   ú;/tmp/pip-unpacked-wheel-1_7di8ic/twisted/test/test_iosim.pyÚtest_connectionSerial   s
    z(FakeTransportTests.test_connectionSerialc                 C  s>   t tƒ dƒ}| d¡ | dddg¡ |  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_writeAfterClose0   s
    

z'FakeTransportTests.test_writeAfterCloseN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   r$   r   r   r   r   r      s   r   c                   @  s>   e Zd ZdZdZddœdd„Zddœdd„Zd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   ÚstopProducingF   s    
z StrictPushProducer.stopProducingc                 C  s&   | j dkrtd| j › d�ƒ‚d| _ d S )Nr*   zCannot pause ú IPushProducerÚpausedr,   r/   r   r   r   ÚpauseProducingK   s    
z!StrictPushProducer.pauseProducingc                 C  s&   | j dkrtd| j › d�ƒ‚d| _ d S )Nr2   zCannot resume r1   r*   r,   r/   r   r   r   ÚresumeProducingP   s    
z"StrictPushProducer.resumeProducingN)r%   r&   r'   r(   r-   r0   r3   r4   r   r   r   r   r)   =   s
   r)   c                   @  s   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zdddœdd„Zdddœdd„Z	dddœdd„Z
ddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœd d!„Zddœd"d#„Zddœd$d%„Zddœd&d'„Zddœd(d)„Zddœd*d+„Zd,S )-Ú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.
        )r)   r/   r   r   r   Ú_initial[   s    z StrictPushProducerTests._initialc                 C  s   t ƒ }| ¡  |S )z@
        @return: A new, stopped L{StrictPushProducer}.
        )r)   r0   ©r   Úproducerr   r   r   Ú_stoppedb   s    z StrictPushProducerTests._stoppedc                 C  s   t ƒ }| ¡  |S )z?
        @return: A new, paused L{StrictPushProducer}.
        )r)   r3   r7   r   r   r   Ú_pausedj   s    zStrictPushProducerTests._pausedc                 C  s   t ƒ }| ¡  | ¡  |S )zY
        @return: A new L{StrictPushProducer} which has been paused and resumed.
        )r)   r3   r4   r7   r   r   r   Ú_resumedr   s    z StrictPushProducerTests._resumedr   )r8   r   c                 C  s   |   |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}
        r+   N©r   r-   r7   r   r   r   ÚassertStopped{   s    z%StrictPushProducerTests.assertStoppedc                 C  s   |   |jd¡ dS )z¨
        Assert that the given producer is in the paused state.

        @param producer: The producer to verify.
        @type producer: L{StrictPushProducer}
        r2   Nr<   r7   r   r   r   ÚassertPaused„   s    z$StrictPushProducerTests.assertPausedc                 C  s   |   |jd¡ dS )z©
        Assert that the given producer is in the running state.

        @param producer: The producer to verify.
        @type producer: L{StrictPushProducer}
        r*   Nr<   r7   r   r   r   ÚassertRunning�   s    z%StrictPushProducerTests.assertRunningc                 C  s   |   t|  ¡ j¡ dS )zz
        L{StrictPushProducer.stopProducing} raises L{ValueError} if called when
        the producer is stopped.
        N)ÚassertRaisesr.   r9   r0   r/   r   r   r   Útest_stopThenStop–   s    z)StrictPushProducerTests.test_stopThenStopc                 C  s   |   t|  ¡ j¡ dS )z{
        L{StrictPushProducer.pauseProducing} raises L{ValueError} if called when
        the producer is stopped.
        N)r@   r.   r9   r3   r/   r   r   r   Útest_stopThenPause�   s    z*StrictPushProducerTests.test_stopThenPausec                 C  s   |   t|  ¡ j¡ dS )z|
        L{StrictPushProducer.resumeProducing} raises L{ValueError} if called when
        the producer is stopped.
        N)r@   r.   r9   r4   r/   r   r   r   Útest_stopThenResume¤   s    z+StrictPushProducerTests.test_stopThenResumec                 C  s   |   ¡ }| ¡  |  |¡ dS )zn
        L{StrictPushProducer} is stopped if C{stopProducing} is called on a paused
        producer.
        N)r:   r0   r=   r7   r   r   r   Útest_pauseThenStop«   s    z*StrictPushProducerTests.test_pauseThenStopc                 C  s   |   ¡ }|  t|j¡ dS )zs
        L{StrictPushProducer.pauseProducing} raises L{ValueError} if called on a
        paused producer.
        N)r:   r@   r.   r3   r7   r   r   r   Útest_pauseThenPause´   s    z+StrictPushProducerTests.test_pauseThenPausec                 C  s   |   ¡ }| ¡  |  |¡ dS )zp
        L{StrictPushProducer} is resumed if C{resumeProducing} is called on a
        paused producer.
        N)r:   r4   r?   r7   r   r   r   Útest_pauseThenResume¼   s    z,StrictPushProducerTests.test_pauseThenResumec                 C  s   |   ¡ }| ¡  |  |¡ dS )zo
        L{StrictPushProducer} is stopped if C{stopProducing} is called on a
        resumed producer.
        N)r;   r0   r=   r7   r   r   r   Útest_resumeThenStopÅ   s    z+StrictPushProducerTests.test_resumeThenStopc                 C  s   |   ¡ }| ¡  |  |¡ dS )zo
        L{StrictPushProducer} is paused if C{pauseProducing} is called on a
        resumed producer.
        N)r;   r3   r>   r7   r   r   r   Útest_resumeThenPauseÎ   s    z,StrictPushProducerTests.test_resumeThenPausec                 C  s   |   ¡ }|  t|j¡ dS )zu
        L{StrictPushProducer.resumeProducing} raises L{ValueError} if called on a
        resumed producer.
        N)r;   r@   r.   r4   r7   r   r   r   Útest_resumeThenResume×   s    z-StrictPushProducerTests.test_resumeThenResumec                 C  s   |   ¡ }| ¡  |  |¡ dS )zn
        L{StrictPushProducer} is stopped if C{stopProducing} is called in the
        initial state.
        N)r6   r0   r=   r7   r   r   r   Ú	test_stopß   s    z!StrictPushProducerTests.test_stopc                 C  s   |   ¡ }| ¡  |  |¡ dS )zn
        L{StrictPushProducer} is paused if C{pauseProducing} is called in the
        initial state.
        N)r6   r3   r>   r7   r   r   r   Ú
test_pauseè   s    z"StrictPushProducerTests.test_pausec                 C  s   |   ¡ }|  t|j¡ dS )zz
        L{StrictPushProducer} raises L{ValueError} if C{resumeProducing} is called
        in the initial state.
        N)r6   r@   r.   r4   r7   r   r   r   Útest_resumeñ   s    z#StrictPushProducerTests.test_resumeN)r%   r&   r'   r(   r6   r9   r:   r;   r=   r>   r?   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   r   r   r   r   r5   V   s(   										r5   c                   @  sJ   e Zd ZdZdddœdd„Zddœdd	„Zddœd
d„Zddœdd„ZdS )ÚIOPumpTestsz
    Tests for L{IOPump}.
    zLiteral[('server', 'client')]r   )Úmoder   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	streamingr*   N)r   r   r	   r)   ZregisterProducerÚpumpr   r-   )	r   rN   ZserverProtoZserverTransportZclientProtoZclientTransportrQ   r8   Zvictimr   r   r   Ú_testStreamingProducerÿ   s(    ûþýz"IOPumpTests._testStreamingProducerr   c                 C  s   | 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.
        rO   ©rN   N©rR   r/   r   r   r   Útest_serverStreamingProducer"  s    z(IOPumpTests.test_serverStreamingProducerc                 C  s   | j dd� dS )z—
        L{IOPump.pump} does not call C{resumeProducing} on a L{IPushProducer}
        (stream producer) registered with the client transport.
        rP   rS   NrT   r/   r   r   r   Útest_clientStreamingProducer)  s    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>7  r   z/IOPumpTests.test_timeAdvances.<locals>.<lambda>N)r   r
   r   Z	callLaterZassertFalserQ   Ú
assertTrue)r   rW   Ú_rQ   r   rY   r   Útest_timeAdvances0  s    
zIOPumpTests.test_timeAdvancesN)r%   r&   r'   r(   rR   rU   rV   r]   r   r   r   r   rM   ú   s
   #rM   N)r(   Ú
__future__r   Útypingr   Zzope.interfacer   Ztwisted.internet.interfacesr   Ztwisted.internet.protocolr   Ztwisted.internet.taskr   Ztwisted.test.iosimr   r	   r
   Ztwisted.trial.unittestr   r   r)   r5   rM   r   r   r   r   Ú<module>   s   ) %