U
    ª€ljþA  ã                   @  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mZmZmZmZmZmZmZmZ ddlmZmZ dd	lmZmZmZmZ dd
lmZ ddlm Z  G dd„ de ƒZ!G dd„ de ƒZ"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.internet.testing}.
é    )Úannotations)ÚCallable)ÚverifyObject)ÚProtocol)ÚIPv4Address)	ÚIAddressÚ
IConnectorÚ	IConsumerÚIListeningPortÚIPushProducerÚIReactorSSLÚIReactorTCPÚIReactorUNIXÚ
ITransport)ÚClientFactoryÚFactory)ÚMemoryReactorÚNonStreamingProducerÚRaisingMemoryReactorÚStringTransport)ÚnamedAny)ÚTestCasec                   @  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„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dœd&d'„Zddœd(d)„Zd*S )+ÚStringTransportTestsz@
    Tests for L{twisted.internet.testing.StringTransport}.
    ÚNone©Úreturnc                 C  s   t ƒ | _d S ©N)r   Ú	transport©Úself© r    úF/tmp/pip-unpacked-wheel-aghyhbke/twisted/internet/test/test_testing.pyÚsetUp+   s    zStringTransportTests.setUpc                 C  s:   |   tt| jƒ¡ |   tt| jƒ¡ |   tt| jƒ¡ dS )zq
        L{StringTransport} instances provide L{ITransport}, L{IPushProducer},
        and L{IConsumer}.
        N)Ú
assertTruer   r   r   r   r	   r   r    r    r!   Útest_interfaces.   s    z$StringTransportTests.test_interfacesc                 C  s>   t ƒ }t ƒ }| j ||¡ |  | jj|¡ |  | jj|¡ dS )zz
        L{StringTransport.registerProducer} records the arguments supplied to
        it as instance attributes.
        N)Úobjectr   ÚregisterProducerÚassertIsÚproducerÚ	streaming©r   r(   r)   r    r    r!   Útest_registerProducer7   s
    z*StringTransportTests.test_registerProducerc                 C  sL   t ƒ }| j |d¡ |  t| jjt ƒ d¡ |  | jj|¡ |  | jj¡ dS )zy
        L{StringTransport.registerProducer} raises L{RuntimeError} if a
        producer is already registered.
        TFN)	r%   r   r&   ÚassertRaisesÚRuntimeErrorr'   r(   r#   r)   )r   r(   r    r    r!   Útest_disallowedRegisterProducerB   s       ÿz4StringTransportTests.test_disallowedRegisterProducerc                 C  sb   t ƒ }t ƒ }| j |d¡ | j ¡  |  | jj¡ | j |d¡ |  | jj|¡ |  | jj¡ dS )z±
        L{StringTransport.unregisterProducer} causes the transport to forget
        about the registered producer and makes it possible to register a new
        one.
        FTN)	r%   r   r&   ÚunregisterProducerÚassertIsNoner(   r'   r#   r)   )r   ZoldProducerZnewProducerr    r    r!   Útest_unregisterProducerO   s    
z,StringTransportTests.test_unregisterProducerc                 C  s   |   t| jj¡ dS )z€
        L{StringTransport.unregisterProducer} raises L{RuntimeError} if called
        when no producer is registered.
        N)r,   r-   r   r/   r   r    r    r!   Útest_invalidUnregisterProducer^   s    z3StringTransportTests.test_invalidUnregisterProducerc                 C  s   |   | jjd¡ dS )zO
        L{StringTransport.producerState} is initially C{'producing'}.
        Ú	producingN)ÚassertEqualr   ÚproducerStater   r    r    r!   Útest_initialProducerStatee   s    z.StringTransportTests.test_initialProducerStatec                 C  s   | j  ¡  |  | j jd¡ dS )zy
        L{StringTransport.pauseProducing} changes the C{producerState} of the
        transport to C{'paused'}.
        ZpausedN)r   ÚpauseProducingr4   r5   r   r    r    r!   Útest_pauseProducingk   s    
z(StringTransportTests.test_pauseProducingc                 C  s(   | j  ¡  | j  ¡  |  | j jd¡ dS )z}
        L{StringTransport.resumeProducing} changes the C{producerState} of the
        transport to C{'producing'}.
        r3   N)r   r7   ÚresumeProducingr4   r5   r   r    r    r!   Útest_resumeProducings   s    

z)StringTransportTests.test_resumeProducingc                 C  s   | j  ¡  |  | j jd¡ dS )z{
        L{StringTransport.stopProducing} changes the C{'producerState'} of the
        transport to C{'stopped'}.
        ÚstoppedN)r   ÚstopProducingr4   r5   r   r    r    r!   Útest_stopProducing|   s    
z'StringTransportTests.test_stopProducingc                 C  s   | j  ¡  |  t| j j¡ dS )zu
        L{StringTransport.pauseProducing} raises L{RuntimeError} if the
        transport has been stopped.
        N)r   r<   r,   r-   r7   r   r    r    r!   Ú test_stoppedTransportCannotPause„   s    
z5StringTransportTests.test_stoppedTransportCannotPausec                 C  s   | j  ¡  |  t| j j¡ dS )zv
        L{StringTransport.resumeProducing} raises L{RuntimeError} if the
        transport has been stopped.
        N)r   r<   r,   r-   r9   r   r    r    r!   Ú!test_stoppedTransportCannotResumeŒ   s    
z6StringTransportTests.test_stoppedTransportCannotResumec                 C  s   | j  ¡  |  t| j j¡ dS )zz
        L{StringTransport.pauseProducing} raises L{RuntimeError} if the
        transport is being disconnected.
        N)r   ÚloseConnectionr,   r-   r7   r   r    r    r!   Ú&test_disconnectingTransportCannotPause”   s    
z;StringTransportTests.test_disconnectingTransportCannotPausec                 C  s   | j  ¡  |  t| j j¡ dS )z{
        L{StringTransport.resumeProducing} raises L{RuntimeError} if the
        transport is being disconnected.
        N)r   r@   r,   r-   r9   r   r    r    r!   Ú'test_disconnectingTransportCannotResumeœ   s    
z<StringTransportTests.test_disconnectingTransportCannotResumec                 C  s*   |   | jj¡ | j ¡  |  | jj¡ dS )zv
        L{StringTransport.loseConnection} toggles the C{disconnecting} instance
        variable to C{True}.
        N)ÚassertFalser   Zdisconnectingr@   r#   r   r    r    r!   Ú$test_loseConnectionSetsDisconnecting¤   s    
z9StringTransportTests.test_loseConnectionSetsDisconnectingc                 C  s   t ƒ }|  t|ƒ ¡ |¡ dS )z�
        If a host address is passed to L{StringTransport.__init__}, that
        value is returned from L{StringTransport.getHost}.
        N)r%   r'   r   ÚgetHost©r   Úaddressr    r    r!   Útest_specifiedHostAddress­   s    z.StringTransportTests.test_specifiedHostAddressc                 C  s    t ƒ }|  t|d� ¡ |¡ dS )z�
        If a peer address is passed to L{StringTransport.__init__}, that
        value is returned from L{StringTransport.getPeer}.
        )ZpeerAddressN)r%   r'   r   ÚgetPeerrF   r    r    r!   Útest_specifiedPeerAddressµ   s    z.StringTransportTests.test_specifiedPeerAddressc                 C  s   t ƒ  ¡ }|  |t¡ dS )z•
        If no host address is passed to L{StringTransport.__init__}, an
        L{IPv4Address} is returned from L{StringTransport.getHost}.
        N)r   rE   ÚassertIsInstancer   rF   r    r    r!   Útest_defaultHostAddress½   s    
z,StringTransportTests.test_defaultHostAddressc                 C  s   t ƒ  ¡ }|  |t¡ dS )z•
        If no peer address is passed to L{StringTransport.__init__}, an
        L{IPv4Address} is returned from L{StringTransport.getPeer}.
        N)r   rI   rK   r   rF   r    r    r!   Útest_defaultPeerAddressÅ   s    
z,StringTransportTests.test_defaultPeerAddressN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r"   r$   r+   r.   r1   r2   r6   r8   r:   r=   r>   r?   rA   rB   rD   rH   rJ   rL   rM   r    r    r    r!   r   &   s(   			r   c                   @  sd   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„Zddœdd„Z	dS )ÚReactorTestszA
    Tests for L{MemoryReactor} and L{RaisingMemoryReactor}.
    r   r   c                 C  s(   t ƒ }tt|ƒ tt|ƒ tt|ƒ dS )zt
        L{MemoryReactor} provides all of the attributes described by the
        interfaces it advertises.
        N)r   r   r   r   r   )r   ÚmemoryReactorr    r    r!   Útest_memoryReactorProvidesÓ   s    

z'ReactorTests.test_memoryReactorProvidesc                 C  s(   t ƒ }tt|ƒ tt|ƒ tt|ƒ dS )z{
        L{RaisingMemoryReactor} provides all of the attributes described by the
        interfaces it advertises.
        N)r   r   r   r   r   )r   ZraisingReactorr    r    r!   Útest_raisingReactorProvidesÝ   s    

z(ReactorTests.test_raisingReactorProvidesc                 C  s¢   t ƒ }| ddtƒ ¡| ddtƒ d¡fD ]<}tt|ƒ | ¡ }tt|ƒ |  |j	d¡ |  |j
d¡ q(| dtƒ ¡}tt|ƒ | ¡ }tt|ƒ |  |jd¡ dS )a  
        L{MemoryReactor.connectTCP}, L{MemoryReactor.connectSSL}, and
        L{MemoryReactor.connectUNIX} will return an L{IConnector} whose
        C{getDestination} method returns an L{IAddress} with attributes which
        reflect the values passed.
        ztest.example.comi�   Ns
   /fake/path)r   Z
connectTCPr   Z
connectSSLr   r   ZgetDestinationr   r4   ÚhostÚportZconnectUNIXÚname)r   rS   Z	connectorrG   r    r    r!   Útest_connectDestinationç   s    þ



z$ReactorTests.test_connectDestinationc                 C  sž   t ƒ }| dtƒ ¡| dtƒ d¡fD ]<}tt|ƒ | ¡ }tt|ƒ |  |j	d¡ |  |j
d¡ q$| dtƒ ¡}tt|ƒ | ¡ }tt|ƒ |  |jd¡ dS )a–  
        L{MemoryReactor.listenTCP}, L{MemoryReactor.listenSSL} and
        L{MemoryReactor.listenUNIX} will return an L{IListeningPort} whose
        C{getHost} method returns an L{IAddress}; C{listenTCP} and C{listenSSL}
        will have a default host of C{'0.0.0.0'}, and a port that reflects the
        value passed, and C{listenUNIX} will have a name that reflects the path
        passed.
        i2   Nz0.0.0.0s   /path/to/socket)r   Z	listenTCPr   Z	listenSSLr   r
   rE   r   r4   rV   rW   Z
listenUNIXrX   )r   rS   rW   rG   r    r    r!   Útest_listenDefaultHostþ   s    	þ



z#ReactorTests.test_listenDefaultHostc                 C  sP   t ƒ }tƒ }| |¡ | |¡ |  | ¡ |g¡ | |¡ |  | ¡ g ¡ dS )z>
        Adding, removing, and listing readers works.
        N)r%   r   Z	addReaderr4   Z
getReadersZremoveReader)r   ÚreaderÚreactorr    r    r!   Útest_readers  s    


zReactorTests.test_readersc                 C  sP   t ƒ }tƒ }| |¡ | |¡ |  | ¡ |g¡ | |¡ |  | ¡ g ¡ dS )z>
        Adding, removing, and listing writers works.
        N)r%   r   Z	addWriterr4   Z
getWritersZremoveWriter)r   Úwriterr\   r    r    r!   Útest_writers'  s    


zReactorTests.test_writersN)
rN   rO   rP   rQ   rT   rU   rY   rZ   r]   r_   r    r    r    r!   rR   Î   s   

rR   c                   @  sN   e Zd ZdZddœdd„Zddddœd	d
„Zddœdd„Zdddœdd„ZdS )ÚTestConsumerzP
    A very basic test consumer for use with the NonStreamingProducerTests.
    r   r   c                 C  s   g | _ d | _d | _d S r   )Úwritesr(   ÚproducerStreamingr   r    r    r!   Ú__init__=  s    zTestConsumer.__init__r%   Úbool)r(   r)   r   c                 C  s   || _ || _dS )z×
        Registers a single producer with this consumer. Just keeps track of it.

        @param producer: The producer to register.
        @param streaming: Whether the producer is a streaming one or not.
        N©r(   rb   r*   r    r    r!   r&   B  s    zTestConsumer.registerProducerc                 C  s   d| _ d| _dS )zC
        Forget the producer we had previously registered.
        Nre   r   r    r    r!   r/   L  s    zTestConsumer.unregisterProducerÚbytes)Údatar   c                 C  s   | j  |¡ dS )zz
        Some data was written to the consumer: stores it for later use.

        @param data: The data to write.
        N)ra   Úappend)r   rg   r    r    r!   ÚwriteS  s    zTestConsumer.writeN)rN   rO   rP   rQ   rc   r&   r/   ri   r    r    r    r!   r`   8  s
   
r`   c                   @  s,   e Zd ZdZddœdd„Zddœdd„ZdS )	ÚNonStreamingProducerTestszF
    Tests for the L{NonStreamingProducer} to validate behaviour.
    r   r   c              
   C  s´   t ƒ }t|ƒ}| |d¡ |  |j|¡ |  |j|¡ |  |j¡ tdƒD ]}| 	¡  qJddddddd	d
ddg
}|  
|j¡ |  
|j¡ |  
|j¡ |  |j|¡ |  t|j	¡ dS )z�
        When the L{NonStreamingProducer} has resumeProducing called 10 times,
        it writes the counter each time and then fails.
        Fé
   ó   0ó   1ó   2ó   3ó   4ó   5ó   6ó   7ó   8ó   9N)r`   r   r&   r'   r(   ÚconsumerrC   rb   Úranger9   r0   r4   ra   r,   r-   )r   rv   r(   Ú_ZexpectedWritesr    r    r!   Útest_producesOnly10Timesa  s    
z2NonStreamingProducerTests.test_producesOnly10Timesc                 C  s4   t ƒ }t|ƒ}| |d¡ | ¡  |  t|j¡ dS )zb
        When the L{NonStreamingProducer} is paused, it raises a
        L{RuntimeError}.
        FN)r`   r   r&   r9   r,   r-   r7   )r   rv   r(   r    r    r!   Útest_cannotPauseProduction{  s
    z4NonStreamingProducerTests.test_cannotPauseProductionN)rN   rO   rP   rQ   ry   rz   r    r    r    r!   rj   \  s   rj   c                   @  s   e Zd Zeddœdd„ƒZ dS )Ú_SupportsNameÚstrr   c                 C  s   d S r   r    r   r    r    r!   rN   ‹  s    z_SupportsName.__name__N)rN   rO   rP   Úpropertyr    r    r    r!   r{   Š  s   r{   c                   @  sæ   e Zd ZdZd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dœd!d"„Zddœd#d$„Zd%S )&ÚDeprecationTestsz8
    Deprecations in L{twisted.test.proto_helpers}.
    zCallable[..., object]r{   r   )ÚtestÚobjr   c                 C  sd   d|j › �}|  |g¡}|  t|d d ¡ |  dt|ƒ¡ |  ||d d ¡ |  |t|ƒ¡ d S )Nztwisted.internet.testing.r   Úcategoryé   Úmessage)rN   ZflushWarningsr4   ÚDeprecationWarningÚlenZassertInr'   r   )r   r   r€   Únew_pathÚwarningsr    r    r!   Úhelper•  s    zDeprecationTests.helperr   c                 C  s   ddl m} |  | j|¡ d S )Nr   )ÚAccumulatingProtocol)Útwisted.test.proto_helpersr‰   rˆ   Útest_accumulatingProtocol)r   r‰   r    r    r!   r‹   �  s    z*DeprecationTests.test_accumulatingProtocolc                 C  s   ddl m} |  | j|¡ d S )Nr   )ÚLineSendingProtocol)rŠ   rŒ   rˆ   Útest_lineSendingProtocol)r   rŒ   r    r    r!   r�   ¢  s    z)DeprecationTests.test_lineSendingProtocolc                 C  s   ddl m} |  | j|¡ d S )Nr   )ÚFakeDatagramTransport)rŠ   rŽ   rˆ   Útest_fakeDatagramTransport)r   rŽ   r    r    r!   r�   §  s    z+DeprecationTests.test_fakeDatagramTransportc                 C  s   ddl m} |  | j|¡ d S )Nr   )r   )rŠ   r   rˆ   Útest_stringTransport)r   r   r    r    r!   r�   ¬  s    z%DeprecationTests.test_stringTransportc                 C  s   ddl m} |  | j|¡ d S )Nr   )Ú StringTransportWithDisconnection)rŠ   r‘   rˆ   Ú%test_stringTransportWithDisconnection)r   r‘   r    r    r!   r’   ±  s
     ÿz6DeprecationTests.test_stringTransportWithDisconnectionc                 C  s   ddl m} |  | j|¡ d S )Nr   )ÚStringIOWithoutClosing)rŠ   r“   rˆ   Útest_stringIOWithoutClosing)r   r“   r    r    r!   r”   ¸  s    z,DeprecationTests.test_stringIOWithoutClosingc                 C  s   ddl m} |  | j|¡ d S )Nr   )Ú_FakeConnector)rŠ   r•   rˆ   Útest__fakeConnector)r   r•   r    r    r!   r–   ½  s    z$DeprecationTests.test__fakeConnectorc                 C  s   ddl m} |  | j|¡ d S )Nr   )Ú	_FakePort)rŠ   r—   rˆ   Útest__fakePort)r   r—   r    r    r!   r˜   Â  s    zDeprecationTests.test__fakePortc                 C  s   ddl m} |  | j|¡ d S )Nr   )r   )rŠ   r   rˆ   Útest_memoryReactor)r   r   r    r    r!   r™   Ç  s    z#DeprecationTests.test_memoryReactorc                 C  s   ddl m} |  | j|¡ d S )Nr   )ÚMemoryReactorClock)rŠ   rš   rˆ   Útest_memoryReactorClock)r   rš   r    r    r!   r›   Ì  s    z(DeprecationTests.test_memoryReactorClockc                 C  s   ddl m} |  | j|¡ d S )Nr   )r   )rŠ   r   rˆ   Útest_raisingMemoryReactor)r   r   r    r    r!   rœ   Ñ  s    z*DeprecationTests.test_raisingMemoryReactorc                 C  s   ddl m} |  | j|¡ d S )Nr   )r   )rŠ   r   rˆ   Útest_nonStreamingProducer)r   r   r    r    r!   r�   Ö  s    z*DeprecationTests.test_nonStreamingProducerc                 C  s   ddl m} |  | j|¡ d S )Nr   )ÚwaitUntilAllDisconnected)rŠ   rž   rˆ   Útest_waitUntilAllDisconnected)r   rž   r    r    r!   rŸ   Û  s    z.DeprecationTests.test_waitUntilAllDisconnectedc                 C  s   ddl m} |  | j|¡ d S )Nr   )ÚEventLoggingObserver)rŠ   r    rˆ   Útest_eventLoggingObserver)r   r    r    r    r!   r¡   à  s    z*DeprecationTests.test_eventLoggingObserverN)rN   rO   rP   rQ   rˆ   r‹   r�   r�   r�   r’   r”   r–   r˜   r™   r›   rœ   r�   rŸ   r¡   r    r    r    r!   r~   �  s    r~   N)'rQ   Ú
__future__r   Útypingr   Zzope.interface.verifyr   Ztyping_extensionsr   Ztwisted.internet.addressr   Ztwisted.internet.interfacesr   r   r	   r
   r   r   r   r   r   Ztwisted.internet.protocolr   r   Ztwisted.internet.testingr   r   r   r   Ztwisted.python.reflectr   Ztwisted.trial.unittestr   r   rR   r`   rj   r{   r~   r    r    r    r!   Ú<module>   s"   , )j$.