U
    2ÜŸj=.  ã                   @   s.  d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	m
Z
 ddl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G dd„ deƒZG dd„ deƒZdS )z>
Tests for L{twisted.internet.posixbase} and supporting code.
é    N)ÚDeferred)ÚIReadDescriptor)ÚPosixReactorBaseÚ_Waker)ÚServerFactory)Úplatform)ÚTestCase)Úunix)ÚClientProtoz)Platform does not support AF_UNIX sockets©Úreactor)ÚPortc                       s,   e Zd ZdZ‡ fdd„Z‡ fdd„Z‡  ZS )ÚWarningCheckerTestCasezg
    A test case that will make sure that no warnings are left unchecked at the end of a test run.
    c                    s4   t ƒ  ¡  tj dd¡ ¡ dkr0t ¡ r0|  ¡  d S )NÚCIÚ Útrue)	ÚsuperÚsetUpÚosÚenvironÚgetÚlowerr   Ú	isWindowsÚflushWarnings©Úself©Ú	__class__© úH/tmp/pip-unpacked-wheel-4b529toj/twisted/internet/test/test_posixbase.pyr       s    
zWarningCheckerTestCase.setUpc                    s`   d zt	ƒ  
¡  W 5 |   ¡ }tj dd¡ ¡ dkr@t ¡ r@£  d S |  t|ƒdd|› �¡ X  d S )Nr   r   r   r   z'Warnings found at the end of the test:
)r   r   r   r   r   r   r   ÚassertEqualÚlenr   ÚtearDown)r   Úwarningsr   r   r   r"   +   s      ÿzWarningCheckerTestCase.tearDown)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r"   Ú__classcell__r   r   r   r   r      s   r   c                   @   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__r   r   r   r   r-   >   s    zTrivialReactor.__init__c                 C   s   d| j |< d S ©NT©r+   ©r   Úreaderr   r   r   Ú	addReaderC   s    zTrivialReactor.addReaderc                 C   s   | j |= d S r*   r/   r0   r   r   r   ÚremoveReaderF   s    zTrivialReactor.removeReaderc                 C   s   d| j |< d S r.   ©r,   ©r   Úwriterr   r   r   Ú	addWriterI   s    zTrivialReactor.addWriterc                 C   s   | j |= d S r*   r4   r5   r   r   r   ÚremoveWriterL   s    zTrivialReactor.removeWriterN)r$   r%   r&   r-   r2   r3   r7   r8   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assertIsInstanceÚwakerr   ÚassertInÚ_internalReadersr+   ©r   r   r   r   r   Ú_checkWakerU   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_wakerIsInternalReaderZ   s    z0PosixReactorBaseTests.test_wakerIsInternalReaderc                 C   s\   t ƒ }tƒ }|j |¡ | |¡ | |j|j¡ |  |¡ |  	||j¡ |  	||j¡ dS )z‹
        Any L{IReadDescriptor}s in L{PosixReactorBase._internalReaders} are
        left alone by L{PosixReactorBase._removeAll}.
        N)
r)   Úobjectr<   Úaddr2   Ú
_removeAllr+   r,   r>   r;   )r   r   Úextrar   r   r   Ú"test_removeAllSkipsInternalReadersb   s    

z8PosixReactorBaseTests.test_removeAllSkipsInternalReadersc                 C   sj   t ƒ }tƒ }tƒ }| |¡ | |¡ | |j|j¡}|  t|ƒ||h¡ |  	||j¡ |  	||j¡ dS )z†
        L{PosixReactorBase._removeAll} returns a list of removed
        L{IReadDescriptor} and L{IWriteDescriptor} objects.
        N)
r)   r@   r2   r7   rB   r+   r,   r    ÚsetZassertNotIn)r   r   r1   r6   Úremovedr   r   r   Ú'test_removeAllReturnsRemovedDescriptorsp   s    

z=PosixReactorBaseTests.test_removeAllReturnsRemovedDescriptorsN)r$   r%   r&   r'   r>   r?   rD   rG   r   r   r   r   r9   P   s
   r9   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>�   ó    z8TCPPortTests.test_connectionLostFailed.<locals>.<lambda>)r   r   Ú	connectedÚconnectionLostZassertFailureÚstopListeningÚZeroDivisionError)r   Úportr   r   r   Útest_connectionLostFailedˆ   s    
z&TCPPortTests.test_connectionLostFailedN)	r$   r%   r&   r'   Ú
isinstancer   r   ÚskiprR   r   r   r   r   rH   €   s   
rH   c                   @   sP   e Zd ZdZdd„ Zeddœdd„Zeddœ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__N)r1   Úreturnc                 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   r0   r   r   r   r2   Ÿ   s    zTimeoutReportReactor.addReaderc                 C   s   dS )z#
        See L{addReader}.
        Nr   r0   r   r   r   r3   ¥   s    z!TimeoutReportReactor.removeReaderc                 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.
        )rX   r   r   r   r   Úseconds²   s    zTimeoutReportReactor.secondsc                 C   s"   | j }|d k	rd | _ | |¡ d S r*   )rW   Úcallback)r   ÚtimeoutÚdr   r   r   ÚdoIteration¹   s    z TimeoutReportReactor.doIteration)r$   r%   r&   r'   r-   r   r2   r3   rZ   r[   r_   r   r   r   r   rU   “   s   rU   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   rK   Ê   rL   z>IterationTimeoutTests._checkIterationTimeout.<locals>.<lambda>r   )rW   ZaddCallbackÚappendÚrun©r   r   r]   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)rU   re   ÚassertIsNonerd   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.
        rV   c                   S   s   d S r*   r   r   r   r   r   rK   Þ   rL   z8IterationTimeoutTests.test_delayedCall.<locals>.<lambda>N©rU   Ú	callLaterre   r    rd   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.
        rV   c                   S   s   d S r*   r   r   r   r   r   rK   é   rL   z7IterationTimeoutTests.test_timePasses.<locals>.<lambda>é   éK   N)rU   ri   rX   re   r    rd   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   rK   õ   rL   zAIterationTimeoutTests.test_multipleDelayedCalls.<locals>.<lambda>é
   c                   S   s   d S r*   r   r   r   r   r   rK   ö   rL   rV   c                   S   s   d S r*   r   r   r   r   r   rK   ÷   rL   Nrh   rd   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.
        rn   c                   S   s   d S r*   r   r   r   r   r   rK     rL   z=IterationTimeoutTests.test_resetDelayedCall.<locals>.<lambda>rk   é   N)rU   ri   rX   Úresetre   r    ©r   r   Úcallr]   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.
        rn   c                   S   s   d S r*   r   r   r   r   r   rK     rL   z=IterationTimeoutTests.test_delayDelayedCall.<locals>.<lambda>ro   é   é<   N)rU   ri   rX   Údelayre   r    rs   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}.
        rn   c                   S   s   d S r*   r   r   r   r   r   rK     rL   z>IterationTimeoutTests.test_cancelDelayedCall.<locals>.<lambda>N)rU   ri   Úcancelre   rf   rs   r   r   r   Útest_cancelDelayedCall  s
    
z,IterationTimeoutTests.test_cancelDelayedCallN)r$   r%   r&   r'   re   rg   rj   rm   rp   ru   ry   r{   r   r   r   r   r`   À   s   	r`   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   ÚloseConnection0  s    z`ConnectedDatagramPortTests.test_connectionFailedDoesntCallLoseConnection.<locals>.loseConnectionNÚgoodbye)r	   ÚConnectedDatagramPortr
   r}   ÚconnectionFailed)r   r}   rQ   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   rO   C  s    zYConnectedDatagramPortTests.test_connectionFailedCallsStopListening.<locals>.stopListeningNr~   )r‚   r	   r   r
   rO   r€   Ú
assertTrue)r   rO   rQ   r   r   r   Ú'test_connectionFailedCallsStopListening;  s    
zBConnectedDatagramPortTests.test_connectionFailedCallsStopListening)r$   r%   r&   r'   ÚskipSocketsrT   r�   r„   r   r   r   r   r|   "  s
   r|   c                   @   s   e Zd Zdd„ ZdS )Ú
WakerTestsc                 C   sH   t ƒ }|  ¡ }|  t|ƒd|¡ | d¡ |  ¡ }|  t|ƒd|¡ dS )zH
        No warnings are generated when constructing the waker.
        r   N)r   r   r    r!   rN   )r   r:   r#   r   r   r   Ú test_noWakerConstructionWarningsP  s    
z+WakerTests.test_noWakerConstructionWarningsN)r$   r%   r&   r‡   r   r   r   r   r†   O  s   r†   ) r'   r   Ztwisted.internet.deferr   Ztwisted.internet.interfacesr   Ztwisted.internet.posixbaser   r   Ztwisted.internet.protocolr   Ztwisted.python.runtimer   Ztwisted.trial.unittestr   r…   Ztwisted.internetr	   Ztwisted.test.test_unixr
   ÚImportErrorr   Ztwisted.internet.tcpr   r   r)   r9   rH   rU   r`   r|   r†   r   r   r   r   Ú<module>   s.   
"0-b-