o
    ¼>h=.  ã                   @   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yG   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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                    s<   t ƒ  ¡  tj dd¡ ¡ dkrt ¡ r|  ¡  d S d S d S )NÚCIÚ Útrue)	ÚsuperÚsetUpÚosÚenvironÚgetÚlowerr   Ú	isWindowsÚflushWarnings©Úself©Ú	__class__© ú„/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/internet/test/test_posixbase.pyr       s   
ÿzWarningCheckerTestCase.setUpc                    s    z)t ƒ  ¡  W |  ¡ }tj dd¡ ¡ dkrt ¡ rd S |  	t
|ƒdd|› ¡ d S |  ¡ }tj dd¡ ¡ dkrCt ¡ rC   Y d S |  	t
|ƒdd|› ¡ w )Nr   r   r   r   z'Warnings found at the end of the test:
)r   ÚtearDownr   r   r   r   r   r   r   ÚassertEqualÚlen)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   ó   d| j |< d S ©NT©r+   ©r   Úreaderr   r   r   Ú	addReaderC   ó   zTrivialReactor.addReaderc                 C   ó   | j |= d S r*   r0   r1   r   r   r   ÚremoveReaderF   ó   zTrivialReactor.removeReaderc                 C   r.   r/   ©r,   ©r   Úwriterr   r   r   Ú	addWriterI   r4   zTrivialReactor.addWriterc                 C   r5   r*   r8   r9   r   r   r   ÚremoveWriterL   r7   zTrivialReactor.removeWriterN)r$   r%   r&   r-   r3   r6   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   Ú_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)   rC   rB   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)   ÚobjectrA   Úaddr3   Ú
_removeAllr+   r,   rC   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)   rE   r3   r;   rG   r+   r,   r!   ÚsetÚassertNotIn)r   r   r2   r:   Úremovedr   r   r   Ú'test_removeAllReturnsRemovedDescriptorsp   s   

z=PosixReactorBaseTests.test_removeAllReturnsRemovedDescriptorsN)r$   r%   r&   r'   rC   rD   rI   rM   r   r   r   r   r=   P   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>   ó    z8TCPPortTests.test_connectionLostFailed.<locals>.<lambda>)r   r   Ú	connectedÚconnectionLostÚassertFailureÚstopListeningÚZeroDivisionError)r   Úportr   r   r   Útest_connectionLostFailedˆ   s   
z&TCPPortTests.test_connectionLostFailedN)	r$   r%   r&   r'   Ú
isinstancer   r   ÚskiprY   r   r   r   r   rN   €   s
    
rN   c                   @   sT   e Zd ZdZdd„ Zdeddfdd„Zdeddf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__r2   ÚreturnNc                 C   ó   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   r1   r   r   r   r3   Ÿ   ó    zTimeoutReportReactor.addReaderc                 C   ra   )z#
        See L{addReader}.
        Nr   r1   r   r   r   r6   ¥   rb   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.
        )r_   r   r   r   r   Úseconds²   s   zTimeoutReportReactor.secondsc                 C   s&   | j }|d urd | _ | |¡ d S d S r*   )r^   Úcallback)r   ÚtimeoutÚdr   r   r   ÚdoIteration¹   s
   þz TimeoutReportReactor.doIteration)r$   r%   r&   r'   r-   r   r3   r6   rc   rd   rh   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)Úignoredr   r   r   rQ   Ê   rR   z>IterationTimeoutTests._checkIterationTimeout.<locals>.<lambda>r   )r^   ÚaddCallbackÚappendÚrun©r   r   rf   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\   rp   ÚassertIsNonero   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   ó   d S r*   r   r   r   r   r   rQ   Þ   rb   z8IterationTimeoutTests.test_delayedCall.<locals>.<lambda>N©r\   Ú	callLaterrp   r!   ro   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   rs   r*   r   r   r   r   r   rQ   é   rb   z7IterationTimeoutTests.test_timePasses.<locals>.<lambda>é   éK   N)r\   ru   r_   rp   r!   ro   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   rs   r*   r   r   r   r   r   rQ   õ   rb   zAIterationTimeoutTests.test_multipleDelayedCalls.<locals>.<lambda>é
   c                   S   rs   r*   r   r   r   r   r   rQ   ö   rb   r]   c                   S   rs   r*   r   r   r   r   r   rQ   ÷   rb   Nrt   ro   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.
        rz   c                   S   rs   r*   r   r   r   r   r   rQ     rb   z=IterationTimeoutTests.test_resetDelayedCall.<locals>.<lambda>rw   é   N)r\   ru   r_   Úresetrp   r!   ©r   r   Úcallrf   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.
        rz   c                   S   rs   r*   r   r   r   r   r   rQ     rb   z=IterationTimeoutTests.test_delayDelayedCall.<locals>.<lambda>r{   é   é<   N)r\   ru   r_   Údelayrp   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}.
        rz   c                   S   rs   r*   r   r   r   r   r   rQ     rb   z>IterationTimeoutTests.test_cancelDelayedCall.<locals>.<lambda>N)r\   ru   Úcancelrp   rq   r   r   r   r   Útest_cancelDelayedCall  s
   
z,IterationTimeoutTests.test_cancelDelayedCallN)r$   r%   r&   r'   rp   rr   rv   ry   r|   r   r…   r‡   r   r   r   r   ri   À   s    	ri   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   ÚloseConnection0  s   z`ConnectedDatagramPortTests.test_connectionFailedDoesntCallLoseConnection.<locals>.loseConnectionNÚgoodbye)r	   ÚConnectedDatagramPortr
   rŠ   ÚconnectionFailed)r   rŠ   rX   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   rV   C  s   
zYConnectedDatagramPortTests.test_connectionFailedCallsStopListening.<locals>.stopListeningNr‹   )r   r	   rŒ   r
   rV   r   Ú
assertTrue)r   rV   rX   r   r   r   Ú'test_connectionFailedCallsStopListening;  s   
zBConnectedDatagramPortTests.test_connectionFailedCallsStopListening)r$   r%   r&   r'   ÚskipSocketsr[   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"   rT   )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   Útwisted.internet.deferr   Útwisted.internet.interfacesr   Útwisted.internet.posixbaser   r   Útwisted.internet.protocolr   Útwisted.python.runtimer   Útwisted.trial.unittestr   r’   Útwisted.internetr	   Útwisted.test.test_unixr
   ÚImportErrorr   Útwisted.internet.tcpr   r   r)   r=   rN   r\   ri   rˆ   r“   r   r   r   r   Ú<module>   s2   ÿ"0-b-