o
    ¯bö%  ã                   @   sü   d Z ddlmZ ddlmZ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y7   d	Z
Y nw 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dS )z>
Tests for L{twisted.internet.posixbase} and supporting code.
é    )ÚDeferred)ÚPosixReactorBaseÚ_Waker)ÚServerFactory)ÚTestCaseN)Úunix)ÚClientProtoz)Platform does not support AF_UNIX sockets©Úreactor)ÚPortc                   @   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   ó   d| j |< d S ©NT©r   ©r   Úreaderr   r   r   Ú	addReader   ó   zTrivialReactor.addReaderc                 C   ó   | j |= d S r   r   r   r   r   r   ÚremoveReader"   ó   zTrivialReactor.removeReaderc                 C   r   r   ©r   ©r   Úwriterr   r   r   Ú	addWriter%   r   zTrivialReactor.addWriterc                 C   r   r   r   r    r   r   r   ÚremoveWriter(   r   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   )ÚassertIsInstanceÚwakerr   ÚassertInÚ_internalReadersr   ©r   r
   r   r   r   Ú_checkWaker1   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_wakerIsInternalReader6   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
   Úextrar   r   r   Ú"test_removeAllSkipsInternalReaders>   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/   r   r"   r1   r   r   ÚassertEqualÚsetÚassertNotIn)r   r
   r   r!   Úremovedr   r   r   Ú'test_removeAllReturnsRemovedDescriptorsL   s   

z=PosixReactorBaseTests.test_removeAllReturnsRemovedDescriptorsN)r$   r%   r&   Ú__doc__r-   r.   r3   r8   r   r   r   r   r'   ,   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>k   ó    z8TCPPortTests.test_connectionLostFailed.<locals>.<lambda>)r   r   Ú	connectedÚconnectionLostÚassertFailureÚstopListeningÚZeroDivisionError)r   Úportr   r   r   Útest_connectionLostFailedd   s   
z&TCPPortTests.test_connectionLostFailedN)	r$   r%   r&   r9   Ú
isinstancer
   r   ÚskiprE   r   r   r   r   r:   \   s
    
r:   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   v   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   {   ó    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.
        )rK   r   r   r   r   Úseconds‰   s   zTimeoutReportReactor.secondsc                 C   s&   | j }|d urd | _ | |¡ d S d S r   )rJ   Úcallback)r   ÚtimeoutÚdr   r   r   ÚdoIteration�   s
   þz TimeoutReportReactor.doIterationN)	r$   r%   r&   r9   r   r   rM   rN   rR   r   r   r   r   rH   o   s    rH   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)Úignoredr	   r   r   r=   ¡   r>   z>IterationTimeoutTests._checkIterationTimeout.<locals>.<lambda>r   )rJ   ÚaddCallbackÚappendÚrun©r   r
   rP   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)rH   rZ   ÚassertIsNonerY   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.
        rI   c                   S   ó   d S r   r   r   r   r   r   r=   µ   rL   z8IterationTimeoutTests.test_delayedCall.<locals>.<lambda>N©rH   Ú	callLaterrZ   r4   rY   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.
        rI   c                   S   r]   r   r   r   r   r   r   r=   À   rL   z7IterationTimeoutTests.test_timePasses.<locals>.<lambda>é   éK   N)rH   r_   rK   rZ   r4   rY   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   r]   r   r   r   r   r   r   r=   Ì   rL   zAIterationTimeoutTests.test_multipleDelayedCalls.<locals>.<lambda>é
   c                   S   r]   r   r   r   r   r   r   r=   Í   rL   rI   c                   S   r]   r   r   r   r   r   r   r=   Î   rL   Nr^   rY   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.
        rd   c                   S   r]   r   r   r   r   r   r   r=   Ù   rL   z=IterationTimeoutTests.test_resetDelayedCall.<locals>.<lambda>ra   é   N)rH   r_   rK   ÚresetrZ   r4   ©r   r
   ÚcallrP   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.
        rd   c                   S   r]   r   r   r   r   r   r   r=   ç   rL   z=IterationTimeoutTests.test_delayDelayedCall.<locals>.<lambda>re   é   é<   N)rH   r_   rK   ÚdelayrZ   r4   ri   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}.
        rd   c                   S   r]   r   r   r   r   r   r   r=   ó   rL   z>IterationTimeoutTests.test_cancelDelayedCall.<locals>.<lambda>N)rH   r_   ÚcancelrZ   r[   ri   r   r   r   Útest_cancelDelayedCallí   s
   
z,IterationTimeoutTests.test_cancelDelayedCallN)r$   r%   r&   r9   rZ   r\   r`   rc   rf   rk   ro   rq   r   r   r   r   rS   —   s    	rS   c                   @   s,   e Zd ZdZedu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)Úfailr   r   r   r   ÚloseConnection  s   z`ConnectedDatagramPortTests.test_connectionFailedDoesntCallLoseConnection.<locals>.loseConnectionNÚgoodbye)r   ÚConnectedDatagramPortr   rt   ÚconnectionFailed)r   rt   rD   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   rB     s   
zYConnectedDatagramPortTests.test_connectionFailedCallsStopListening.<locals>.stopListeningNru   )ry   r   rv   r   rB   rw   Ú
assertTrue)r   rB   rD   r   r   r   Ú'test_connectionFailedCallsStopListening  s   
zBConnectedDatagramPortTests.test_connectionFailedCallsStopListening)r$   r%   r&   r9   ÚskipSocketsrG   rx   r{   r   r   r   r   rr   ù   s    rr   c                   @   s   e Zd Zdd„ ZdS )Ú
WakerTestsc                 C   s0   t d d�}|  ¡ }| d ¡ |  t|ƒd¡ d S )Nr	   r   )r   ÚflushWarningsr@   r4   Úlen)r   r)   Úwarningsr   r   r   Ú test_noWakerConstructionWarnings'  s   

z+WakerTests.test_noWakerConstructionWarningsN)r$   r%   r&   r�   r   r   r   r   r}   &  s    r}   )r9   Útwisted.internet.deferr   Útwisted.internet.posixbaser   r   Útwisted.internet.protocolr   Útwisted.trial.unittestr   r|   Útwisted.internetr   Útwisted.test.test_unixr   ÚImportErrorr
   Útwisted.internet.tcpr   r   r'   r:   rH   rS   rr   r}   r   r   r   r   Ú<module>   s*   ÿ0(b-