U
    
W[@%  ã                   @   sþ   d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZ dZzddlmZ dd	lmZ W n ek
r€   d
ZY nX 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eƒ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.posixbase} and supporting code.
é    )ÚdivisionÚabsolute_import)ÚTestCase)ÚDeferred)ÚPosixReactorBaseÚ_Waker)ÚServerFactoryN)Úunix)ÚClientProtoz)Platform does not support AF_UNIX sockets)ÚPort©Úreactorc                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚTrivialReactorc                 C   s   i | _ i | _t | ¡ d S ©N)Ú_readersÚ_writersr   Ú__init__©Úself© r   úF/usr/lib/python3/dist-packages/twisted/internet/test/test_posixbase.pyr      s    zTrivialReactor.__init__c                 C   s   d| j |< d S ©NT©r   ©r   Úreaderr   r   r   Ú	addReader#   s    zTrivialReactor.addReaderc                 C   s   | j |= d S r   r   r   r   r   r   ÚremoveReader'   s    zTrivialReactor.removeReaderc                 C   s   d| j |< d S r   ©r   ©r   Úwriterr   r   r   Ú	addWriter+   s    zTrivialReactor.addWriterc                 C   s   | j |= d S r   r   r   r   r   r   ÚremoveWriter/   s    zTrivialReactor.removeWriterN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r    r!   r   r   r   r   r      s
   r   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚPosixReactorBaseTestsz(
    Tests for L{PosixReactorBase}.
    c                 C   s2   |   |jt¡ |  |j|j¡ |  |j|j¡ d S r   )ZassertIsInstanceZwakerr   ÚassertInÚ_internalReadersr   ©r   r   r   r   r   Ú_checkWaker9   s    z!PosixReactorBaseTests._checkWakerc                 C   s   t ƒ }|  |¡ dS )z
        When L{PosixReactorBase} is instantiated, it creates a waker and adds
        it to its internal readers set.
        N)r   r)   r(   r   r   r   Útest_wakerIsInternalReader?   s    z0PosixReactorBaseTests.test_wakerIsInternalReaderc                 C   s\   t ƒ }tƒ }|j |¡ | |¡ | |j|j¡ |  |¡ |  	||j¡ |  	||j¡ dS )z‹
        Any L{IReadDescriptors} in L{PosixReactorBase._internalReaders} are
        left alone by L{PosixReactorBase._removeAll}.
        N)
r   Úobjectr'   Úaddr   Ú
_removeAllr   r   r)   r&   )r   r   Zextrar   r   r   Ú"test_removeAllSkipsInternalReadersH   s    

z8PosixReactorBaseTests.test_removeAllSkipsInternalReadersc                 C   sn   t ƒ }tƒ }tƒ }| |¡ | |¡ | |j|j¡}|  t|ƒt||gƒ¡ |  	||j¡ |  	||j¡ dS )z†
        L{PosixReactorBase._removeAll} returns a list of removed
        L{IReadDescriptor} and L{IWriteDescriptor} objects.
        N)
r   r+   r   r    r-   r   r   ÚassertEqualÚsetZassertNotIn)r   r   r   r   Zremovedr   r   r   Ú'test_removeAllReturnsRemovedDescriptorsW   s    

 ÿz=PosixReactorBaseTests.test_removeAllReturnsRemovedDescriptorsN)r"   r#   r$   Ú__doc__r)   r*   r.   r1   r   r   r   r   r%   4   s
   	r%   c                   @   s&   e Zd ZdZeeeƒsdZdd„ ZdS )ÚTCPPortTestsz1
    Tests for L{twisted.internet.tcp.Port}.
    zNon-posixbase reactorc                 C   s,   t dtƒ ƒ}d|_dd„ |_|  | ¡ t¡S )zƒ
        L{Port.stopListening} returns a L{Deferred} which errbacks if
        L{Port.connectionLost} raises an exception.
        i90  Tc                 S   s   dd S )Né   r   r   )Úreasonr   r   r   Ú<lambda>x   ó    z8TCPPortTests.test_connectionLostFailed.<locals>.<lambda>)r   r   Z	connectedZconnectionLostZassertFailureÚstopListeningÚZeroDivisionError)r   Úportr   r   r   Útest_connectionLostFailedq   s    
z&TCPPortTests.test_connectionLostFailedN)	r"   r#   r$   r2   Ú
isinstancer   r   Úskipr;   r   r   r   r   r3   i   s   
r3   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚTimeoutReportReactorzà
    A reactor which is just barely runnable and which cannot monitor any
    readers or writers, and which fires a L{Deferred} with the timeout
    passed to its C{doIteration} method as soon as that method is invoked.
    c                 C   s   t  | ¡ tƒ | _d| _d S )Néd   )r   r   r   ÚiterationTimeoutÚnowr   r   r   r   r   ƒ   s    
zTimeoutReportReactor.__init__c                 C   s   dS )z—
        Ignore the reader.  This is necessary because the waker will be
        added.  However, we won't actually monitor it for any events.
        Nr   r   r   r   r   r   ‰   s    zTimeoutReportReactor.addReaderc                 C   s   g S )z°
        There are no readers or writers, so there is nothing to remove.
        This will be called when the reactor stops, though, so it must be
        implemented.
        r   r   r   r   r   Ú	removeAll   s    zTimeoutReportReactor.removeAllc                 C   s   | j S )zx
        Override the real clock with a deterministic one that can be easily
        controlled in a unit test.
        )rA   r   r   r   r   Úseconds™   s    zTimeoutReportReactor.secondsc                 C   s"   | j }|d k	rd | _ | |¡ d S r   )r@   Úcallback)r   ÚtimeoutÚdr   r   r   ÚdoIteration¡   s    z TimeoutReportReactor.doIterationN)	r"   r#   r$   r2   r   r   rB   rC   rG   r   r   r   r   r>   }   s   	r>   c                   @   sP   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S )ÚIterationTimeoutTestsz
    Tests for the timeout argument L{PosixReactorBase.run} calls
    L{PosixReactorBase.doIteration} with in the presence of various delayed
    calls.
    c                    s6   g }ˆ j  |j¡ ˆ j  ‡ fdd„¡ ˆ  ¡  |d S )Nc                    s   ˆ   ¡ S r   )Ústop)Zignoredr   r   r   r6   ²   r7   z>IterationTimeoutTests._checkIterationTimeout.<locals>.<lambda>r   )r@   ZaddCallbackÚappendÚrun©r   r   rE   r   r   r   Ú_checkIterationTimeout¯   s
    z,IterationTimeoutTests._checkIterationTimeoutc                 C   s   t ƒ }|  |¡}|  |¡ dS )zl
        If there are no delayed calls, C{doIteration} is called with a
        timeout of L{None}.
        N)r>   rM   ÚassertIsNonerL   r   r   r   Útest_noCalls·   s    
z"IterationTimeoutTests.test_noCallsc                 C   s0   t ƒ }| ddd„ ¡ |  |¡}|  |d¡ dS )zÁ
        If there is a delayed call, C{doIteration} is called with a timeout
        which is the difference between the current time and the time at
        which that call is to run.
        r?   c                   S   s   d S r   r   r   r   r   r   r6   È   r7   z8IterationTimeoutTests.test_delayedCall.<locals>.<lambda>N©r>   Ú	callLaterrM   r/   rL   r   r   r   Útest_delayedCallÁ   s    
z&IterationTimeoutTests.test_delayedCallc                 C   s>   t ƒ }| ddd„ ¡ | jd7  _|  |¡}|  |d¡ dS )z¯
        If a delayed call is scheduled and then some time passes, the
        timeout passed to C{doIteration} is reduced by the amount of time
        which passed.
        r?   c                   S   s   d S r   r   r   r   r   r   r6   Ô   r7   z7IterationTimeoutTests.test_timePasses.<locals>.<lambda>é   éK   N)r>   rQ   rA   rM   r/   rL   r   r   r   Útest_timePassesÍ   s
    
z%IterationTimeoutTests.test_timePassesc                 C   sP   t ƒ }| ddd„ ¡ | ddd„ ¡ | ddd„ ¡ |  |¡}|  |d¡ dS )	zÜ
        If there are several delayed calls, C{doIteration} is called with a
        timeout which is the difference between the current time and the
        time at which the earlier of the two calls is to run.
        é2   c                   S   s   d S r   r   r   r   r   r   r6   á   r7   zAIterationTimeoutTests.test_multipleDelayedCalls.<locals>.<lambda>é
   c                   S   s   d S r   r   r   r   r   r   r6   â   r7   r?   c                   S   s   d S r   r   r   r   r   r   r6   ã   r7   NrP   rL   r   r   r   Útest_multipleDelayedCallsÚ   s    
z/IterationTimeoutTests.test_multipleDelayedCallsc                 C   sH   t ƒ }| ddd„ ¡}| jd7  _| d¡ |  |¡}|  |d¡ dS )zÀ
        If a delayed call is reset, the timeout passed to C{doIteration} is
        based on the interval between the time when reset is called and the
        new delay of the call.
        rV   c                   S   s   d S r   r   r   r   r   r   r6   ï   r7   z=IterationTimeoutTests.test_resetDelayedCall.<locals>.<lambda>rS   é   N)r>   rQ   rA   ÚresetrM   r/   ©r   r   ÚcallrE   r   r   r   Útest_resetDelayedCallè   s    

z+IterationTimeoutTests.test_resetDelayedCallc                 C   sH   t ƒ }| ddd„ ¡}| jd7  _| d¡ |  |¡}|  |d¡ dS )zñ
        If a delayed call is re-delayed, the timeout passed to
        C{doIteration} is based on the remaining time before the call would
        have been made and the additional amount of time passed to the delay
        method.
        rV   c                   S   s   d S r   r   r   r   r   r   r6   þ   r7   z=IterationTimeoutTests.test_delayDelayedCall.<locals>.<lambda>rW   é   é<   N)r>   rQ   rA   ZdelayrM   r/   r[   r   r   r   Útest_delayDelayedCallö   s    

z+IterationTimeoutTests.test_delayDelayedCallc                 C   s6   t ƒ }| ddd„ ¡}| ¡  |  |¡}|  |¡ dS )zp
        If the only delayed call is canceled, L{None} is the timeout passed
        to C{doIteration}.
        rV   c                   S   s   d S r   r   r   r   r   r   r6     r7   z>IterationTimeoutTests.test_cancelDelayedCall.<locals>.<lambda>N)r>   rQ   ZcancelrM   rN   r[   r   r   r   Útest_cancelDelayedCall  s
    
z,IterationTimeoutTests.test_cancelDelayedCallN)r"   r#   r$   r2   rM   rO   rR   rU   rX   r]   r`   ra   r   r   r   r   rH   ©   s   
rH   c                   @   s,   e Zd ZdZedk	reZdd„ Zdd„ ZdS )ÚConnectedDatagramPortTestsz/
    Test connected datagram UNIX sockets.
    Nc                    s.   ‡ fdd„}t  dtƒ ¡}||_| d¡ dS )z
        L{ConnectedDatagramPort} does not call the deprecated C{loseConnection}
        in L{ConnectedDatagramPort.connectionFailed}.
        c                      s   ˆ   d¡ dS )z€
            Dummy C{loseConnection} method. C{loseConnection} is deprecated and
            should not get called.
            z7loseConnection is deprecated and should not get called.N)Zfailr   r   r   r   ÚloseConnection  s    z`ConnectedDatagramPortTests.test_connectionFailedDoesntCallLoseConnection.<locals>.loseConnectionNÚgoodbye)r	   ÚConnectedDatagramPortr
   rc   ÚconnectionFailed)r   rc   r:   r   r   r   Ú-test_connectionFailedDoesntCallLoseConnection  s    zHConnectedDatagramPortTests.test_connectionFailedDoesntCallLoseConnectionc                    s@   dˆ _ ‡ fdd„}t dtƒ ¡}||_| d¡ ˆ  ˆ j ¡ dS )zÁ
        L{ConnectedDatagramPort} calls L{ConnectedDatagramPort.stopListening}
        instead of the deprecated C{loseConnection} in
        L{ConnectedDatagramPort.connectionFailed}.
        Fc                      s
   dˆ _ dS )z8
            Dummy C{stopListening} method.
            TN)Úcalledr   r   r   r   r8   3  s    zYConnectedDatagramPortTests.test_connectionFailedCallsStopListening.<locals>.stopListeningNrd   )rh   r	   re   r
   r8   rf   Z
assertTrue)r   r8   r:   r   r   r   Ú'test_connectionFailedCallsStopListening+  s    
zBConnectedDatagramPortTests.test_connectionFailedCallsStopListening)r"   r#   r$   r2   ÚskipSocketsr=   rg   ri   r   r   r   r   rb     s
   rb   )r2   Z
__future__r   r   Ztwisted.trial.unittestr   Ztwisted.internet.deferr   Ztwisted.internet.posixbaser   r   Ztwisted.internet.protocolr   rj   Ztwisted.internetr	   Ztwisted.test.test_unixr
   ÚImportErrorZtwisted.internet.tcpr   r   r   r%   r3   r>   rH   rb   r   r   r   r   Ú<module>   s&   
5,i