Ë
    oj=.  ã                   ó\  — 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	 dd	lmZ dd
lmZ ddlmZ ddlmZ  G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Zy# e$ r dZY Œnw xY w)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                   ó,   ‡ — e Zd ZdZˆ fd„Zˆ fd„Zˆ xZS )ÚWarningCheckerTestCasezg
    A test case that will make sure that no warnings are left unchecked at the end of a test run.
    c                 óÐ   •— t         ‰| �  «        t        j                  j	                  dd«      j                  «       dk(  r&t        j                  «       r| j                  «        y y y )NÚCIÚ Útrue)	ÚsuperÚsetUpÚosÚenvironÚgetÚlowerr   Ú	isWindowsÚflushWarnings)ÚselfÚ	__class__s    €úF/usr/lib/python3/dist-packages/twisted/internet/test/test_posixbase.pyr   zWarningCheckerTestCase.setUp    sN   ø€ Ü‰‰Œô �:‰:�>‰>˜$ Ó#×)Ñ)Ó+¨vÒ5¼(×:LÑ:LÔ:NØ×ÑÕ ð ;OÐ5ó    c                 ó  •— 	 t         ‰| �  «        | j                  «       }t        j                  j                  dd«      j                  «       dk(  rt        j                  «       ry | j                  t        |«      dd|› �«       y # | j                  «       }t        j                  j                  dd«      j                  «       dk(  rt        j                  «       r Y y | j                  t        |«      dd|› �«       w xY 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   s     €r   r!   zWarningCheckerTestCase.tearDown+   sÕ   ø€ ð	Ü‰GÑÔà×)Ñ)Ó+ˆHÜ�z‰z�~‰~˜d BÓ'×-Ñ-Ó/°6Ò9¼h×>PÑ>PÔ>Rð Ø×ÑÜ�H“˜qÐ$LÈXÈJÐ"Wõøð ×)Ñ)Ó+ˆHÜ�z‰z�~‰~˜d BÓ'×-Ñ-Ó/°6Ò9¼h×>PÑ>PÔ>Rò Ø×ÑÜ�H“˜qÐ$LÈXÈJÐ"Wõús   ƒB ÂAD Ã  D )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   Ú__classcell__)r   s   @r   r   r      s   ø„ ñô	!÷ð r   r   c                   ó*   — e Zd Zd„ Zd„ Zd„ Zd„ Zd„ Zy)ÚTrivialReactorc                 óJ   — i | _         i | _        t        j                  | «       y ©N)Ú_readersÚ_writersr   Ú__init__©r   s    r   r0   zTrivialReactor.__init__>   s   € ØˆŒØˆŒÜ×!Ñ! $Õ'r   c                 ó"   — d| j                   |<   y ©NT©r.   ©r   Úreaders     r   Ú	addReaderzTrivialReactor.addReaderC   ó   € Ø $ˆ�‰�fÒr   c                 ó   — | j                   |= y r-   r4   r5   s     r   ÚremoveReaderzTrivialReactor.removeReaderF   ó   € Ø�M‰M˜&Ñ!r   c                 ó"   — d| j                   |<   y r3   ©r/   ©r   Úwriters     r   Ú	addWriterzTrivialReactor.addWriterI   r8   r   c                 ó   — | j                   |= y r-   r=   r>   s     r   ÚremoveWriterzTrivialReactor.removeWriterL   r;   r   N)r%   r&   r'   r0   r7   r:   r@   rB   © r   r   r+   r+   =   s   „ ò(ò
%ò"ò%ó"r   r+   c                   ó(   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zy)ÚPosixReactorBaseTestsz(
    Tests for L{PosixReactorBase}.
    c                 óÜ   — | j                  |j                  t        «       | j                  |j                  |j                  «       | j                  |j                  |j
                  «       y r-   )ÚassertIsInstanceÚwakerr   ÚassertInÚ_internalReadersr.   ©r   r   s     r   Ú_checkWakerz!PosixReactorBaseTests._checkWakerU   sF   € Ø×Ñ˜gŸm™m¬VÔ4Ø�‰�g—m‘m W×%=Ñ%=Ô>Ø�‰�g—m‘m W×%5Ñ%5Õ6r   c                 ó:   — t        «       }| j                  |«       y)z
        When L{PosixReactorBase} is instantiated, it creates a waker and adds
        it to its internal readers set.
        N)r+   rL   rK   s     r   Útest_wakerIsInternalReaderz0PosixReactorBaseTests.test_wakerIsInternalReaderZ   s   € ô
 !Ó"ˆØ×Ñ˜Õ!r   c                 óf  — t        «       }t        «       }|j                  j                  |«        |j                  |«        |j
                  |j                  |j                  «       | j                  |«       | j                  ||j                  «       | j                  ||j                  «       y)z‹
        Any L{IReadDescriptor}s in L{PosixReactorBase._internalReaders} are
        left alone by L{PosixReactorBase._removeAll}.
        N)
r+   ÚobjectrJ   Úaddr7   Ú
_removeAllr.   r/   rL   rI   )r   r   Úextras      r   Ú"test_removeAllSkipsInternalReadersz8PosixReactorBaseTests.test_removeAllSkipsInternalReadersb   s‹   € ô
 !Ó"ˆÜ“ˆØ× Ñ ×$Ñ$ UÔ+Øˆ×Ñ˜%Ô Øˆ×Ñ˜7×+Ñ+¨W×-=Ñ-=Ô>Ø×Ñ˜Ô!Ø�‰�e˜W×5Ñ5Ô6Ø�‰�e˜W×-Ñ-Õ.r   c                 ó€  — t        «       }t        «       }t        «       } |j                  |«        |j                  |«        |j                  |j
                  |j                  «      }| j                  t        |«      ||h«       | j                  ||j
                  «       | j                  ||j                  «       y)z†
        L{PosixReactorBase._removeAll} returns a list of removed
        L{IReadDescriptor} and L{IWriteDescriptor} objects.
        N)
r+   rP   r7   r@   rR   r.   r/   r"   ÚsetÚassertNotIn)r   r   r6   r?   Úremoveds        r   Ú'test_removeAllReturnsRemovedDescriptorsz=PosixReactorBaseTests.test_removeAllReturnsRemovedDescriptorsp   sŸ   € ô
 !Ó"ˆÜ“ˆÜ“ˆØˆ×Ñ˜&Ô!Øˆ×Ñ˜&Ô!Ø$�'×$Ñ$ W×%5Ñ%5°w×7GÑ7GÓHˆØ×Ñœ˜W›¨°Ð'7Ô8Ø×Ñ˜ ×!1Ñ!1Ô2Ø×Ñ˜ ×!1Ñ!1Õ2r   N)r%   r&   r'   r(   rL   rN   rT   rY   rC   r   r   rE   rE   P   s   „ ñò7ò
"ò/ó3r   rE   c                   ó,   — e Zd ZdZ eee«      sdZd„ Zy)ÚTCPPortTestsz1
    Tests for L{twisted.internet.tcp.Port}.
    zNon-posixbase reactorc                 ó�   — t        dt        «       «      }d|_        d„ |_        | j	                  |j                  «       t        «      S )zƒ
        L{Port.stopListening} returns a L{Deferred} which errbacks if
        L{Port.connectionLost} raises an exception.
        i90  Tc                 ó   — ddz  S )Né   r   rC   )Úreasons    r   ú<lambda>z8TCPPortTests.test_connectionLostFailed.<locals>.<lambda>�   s
   € ¨Q°!©V€ r   )r   r   Ú	connectedÚconnectionLostÚassertFailureÚstopListeningÚZeroDivisionError)r   Úports     r   Útest_connectionLostFailedz&TCPPortTests.test_connectionLostFailedˆ   s>   € ô
 �Eœ=›?Ó+ˆØˆŒÙ3ˆÔØ×!Ñ! $×"4Ñ"4Ó"6Ô8IÓJÐJr   N)	r%   r&   r'   r(   Ú
isinstancer   r   Úskiprg   rC   r   r   r[   r[   €   s   „ ññ �gÐ/Ô0Ø&ˆóKr   r[   c                   óH   — e Zd ZdZd„ Zdeddfd„Zdeddfd„Zd„ Zd	„ Z	d
„ Z
y)Ú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                 óZ   — t        j                  | «       t        «       | _        d| _        y )Néd   )r   r0   r   ÚiterationTimeoutÚnowr1   s    r   r0   zTimeoutReportReactor.__init__š   s!   € Ü×!Ñ! $Ô'Ü (£
ˆÔØˆ�r   r6   ÚreturnNc                  ó   — y)z—
        Ignore the reader.  This is necessary because the waker will be
        added.  However, we won't actually monitor it for any events.
        NrC   r5   s     r   r7   zTimeoutReportReactor.addReaderŸ   ó   � r   c                  ó   — y)z#
        See L{addReader}.
        NrC   r5   s     r   r:   z!TimeoutReportReactor.removeReader¥   rr   r   c                 ó   — 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.
        rC   r1   s    r   Ú	removeAllzTimeoutReportReactor.removeAllª   s	   € ð ˆ	r   c                 ó   — | j                   S )zx
        Override the real clock with a deterministic one that can be easily
        controlled in a unit test.
        )ro   r1   s    r   ÚsecondszTimeoutReportReactor.seconds²   s   € ð
 �x‰xˆr   c                 óR   — | j                   }|�d | _         |j                  |«       y y r-   )rn   Úcallback)r   ÚtimeoutÚds      r   ÚdoIterationz TimeoutReportReactor.doIteration¹   s+   € Ø×!Ñ!ˆØˆ=Ø$(ˆDÔ!Ø�J‰J�wÕð r   )r%   r&   r'   r(   r0   r   r7   r:   ru   rw   r|   rC   r   r   rk   rk   “   sA   „ ñòð
 ð °Dó ð ?ð °tó ò
òó r   rk   c                   ó@   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ Zd„ Z	d„ Z
d	„ Zy
)ÚIterationTimeoutTestsz�
    Tests for the timeout argument L{PosixReactorBase.run} calls
    L{PosixReactorBase.doIteration} with in the presence of various delayed
    calls.
    c                 óº   ‡— g }‰j                   j                  |j                  «       ‰j                   j                  ˆfd„«        ‰j                  «        |d   S )Nc                 ó&   •—  ‰j                   «       S r-   )Ústop)Úignoredr   s    €r   r`   z>IterationTimeoutTests._checkIterationTimeout.<locals>.<lambda>Ê   s   ø€ ¸\¸W¿\¹\»^€ r   r   )rn   ÚaddCallbackÚappendÚrun©r   r   rz   s    ` r   Ú_checkIterationTimeoutz,IterationTimeoutTests._checkIterationTimeoutÇ   sJ   ø€ ØˆØ× Ñ ×,Ñ,¨W¯^©^Ô<Ø× Ñ ×,Ñ,Ó-KÔLØˆ�‰ŒØ�q‰zÐr   c                 ó\   — t        «       }| j                  |«      }| j                  |«       y)zl
        If there are no delayed calls, C{doIteration} is called with a
        timeout of L{None}.
        N)rk   r‡   ÚassertIsNoner†   s      r   Útest_noCallsz"IterationTimeoutTests.test_noCallsÎ   s*   € ô
 'Ó(ˆØ×-Ñ-¨gÓ6ˆØ×Ñ˜'Õ"r   c                 ó†   — t        «       } |j                  dd„ «       | j                  |«      }| j                  |d«       y)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.
        rm   c                   ó   — y r-   rC   rC   r   r   r`   z8IterationTimeoutTests.test_delayedCall.<locals>.<lambda>Þ   rr   r   N©rk   Ú	callLaterr‡   r"   r†   s      r   Útest_delayedCallz&IterationTimeoutTests.test_delayedCall×   s>   € ô 'Ó(ˆØˆ×Ñ˜#™|Ô,Ø×-Ñ-¨gÓ6ˆØ×Ñ˜ #Õ&r   c                 ó°   — t        «       } |j                  dd„ «       |xj                  dz  c_        | j                  |«      }| j	                  |d«       y)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.
        rm   c                   ó   — y r-   rC   rC   r   r   r`   z7IterationTimeoutTests.test_timePasses.<locals>.<lambda>é   rr   r   é   éK   N)rk   rŽ   ro   r‡   r"   r†   s      r   Útest_timePassesz%IterationTimeoutTests.test_timePassesâ   sL   € ô 'Ó(ˆØˆ×Ñ˜#™|Ô,Ø�Š�rÑ�Ø×-Ñ-¨gÓ6ˆØ×Ñ˜ "Õ%r   c                 óÖ   — t        «       } |j                  dd„ «        |j                  dd„ «        |j                  dd„ «       | j                  |«      }| j                  |d«       y)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                   ó   — y r-   rC   rC   r   r   r`   zAIterationTimeoutTests.test_multipleDelayedCalls.<locals>.<lambda>õ   rr   r   é
   c                   ó   — y r-   rC   rC   r   r   r`   zAIterationTimeoutTests.test_multipleDelayedCalls.<locals>.<lambda>ö   rr   r   rm   c                   ó   — y r-   rC   rC   r   r   r`   zAIterationTimeoutTests.test_multipleDelayedCalls.<locals>.<lambda>÷   rr   r   Nr�   r†   s      r   Útest_multipleDelayedCallsz/IterationTimeoutTests.test_multipleDelayedCallsî   sb   € ô 'Ó(ˆØˆ×Ñ˜"™lÔ+Øˆ×Ñ˜"™lÔ+Øˆ×Ñ˜#™|Ô,Ø×-Ñ-¨gÓ6ˆØ×Ñ˜ "Õ%r   c                 óÒ   — t        «       } |j                  dd„ «      }|xj                  dz  c_        |j                  d«       | j	                  |«      }| j                  |d«       y)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.
        r–   c                   ó   — y r-   rC   rC   r   r   r`   z=IterationTimeoutTests.test_resetDelayedCall.<locals>.<lambda>  rr   r   r’   é   N)rk   rŽ   ro   Úresetr‡   r"   ©r   r   Úcallrz   s       r   Útest_resetDelayedCallz+IterationTimeoutTests.test_resetDelayedCallû   sY   € ô 'Ó(ˆØ ˆw× Ñ  ¡\Ó2ˆØ�Š�rÑ�Ø�
‰
�2ŒØ×-Ñ-¨gÓ6ˆØ×Ñ˜ "Õ%r   c                 óÒ   — t        «       } |j                  dd„ «      }|xj                  dz  c_        |j                  d«       | j	                  |«      }| j                  |d«       y)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.
        r–   c                   ó   — y r-   rC   rC   r   r   r`   z=IterationTimeoutTests.test_delayDelayedCall.<locals>.<lambda>  rr   r   r˜   é   é<   N)rk   rŽ   ro   Údelayr‡   r"   r    s       r   Útest_delayDelayedCallz+IterationTimeoutTests.test_delayDelayedCall  sY   € ô 'Ó(ˆØ ˆw× Ñ  ¡\Ó2ˆØ�Š�rÑ�Ø�
‰
�2ŒØ×-Ñ-¨gÓ6ˆØ×Ñ˜ "Õ%r   c                 ó¤   — t        «       } |j                  dd„ «      }|j                  «        | j                  |«      }| j	                  |«       y)zp
        If the only delayed call is canceled, L{None} is the timeout passed
        to C{doIteration}.
        r–   c                   ó   — y r-   rC   rC   r   r   r`   z>IterationTimeoutTests.test_cancelDelayedCall.<locals>.<lambda>  rr   r   N)rk   rŽ   Úcancelr‡   r‰   r    s       r   Útest_cancelDelayedCallz,IterationTimeoutTests.test_cancelDelayedCall  sG   € ô
 'Ó(ˆØ ˆw× Ñ  ¡\Ó2ˆØ�‰ŒØ×-Ñ-¨gÓ6ˆØ×Ñ˜'Õ"r   N)r%   r&   r'   r(   r‡   rŠ   r�   r”   r›   r¢   r¨   r¬   rC   r   r   r~   r~   À   s/   „ ñòò#ò	'ò
&ò&ò&ò&ó	#r   r~   c                   ó$   — e Zd ZdZe�eZd„ Zd„ Zy)ÚConnectedDatagramPortTestsz/
    Test connected datagram UNIX sockets.
    Nc                 ó|   ‡ — ˆ fd„}t        j                  dt        «       «      }||_        |j	                  d«       y)z�
        L{ConnectedDatagramPort} does not call the deprecated C{loseConnection}
        in L{ConnectedDatagramPort.connectionFailed}.
        c                  ó(   •— ‰ j                  d«       y)z€
            Dummy C{loseConnection} method. C{loseConnection} is deprecated and
            should not get called.
            z7loseConnection is deprecated and should not get called.N)Úfailr1   s   €r   ÚloseConnectionz`ConnectedDatagramPortTests.test_connectionFailedDoesntCallLoseConnection.<locals>.loseConnection0  s   ø€ ð
 �I‰IÐOÕPr   NÚgoodbye)r
   ÚConnectedDatagramPortr   r²   ÚconnectionFailed)r   r²   rf   s   `  r   Ú-test_connectionFailedDoesntCallLoseConnectionzHConnectedDatagramPortTests.test_connectionFailedDoesntCallLoseConnection*  s5   ø€ ô	Qô ×)Ñ)¨$´³Ó>ˆØ,ˆÔØ×Ñ˜iÕ(r   c                 óÀ   ‡ — d‰ _         ˆ fd„}t        j                  dt        «       «      }||_        |j                  d«       ‰ j                  ‰ j                   «       y)zÁ
        L{ConnectedDatagramPort} calls L{ConnectedDatagramPort.stopListening}
        instead of the deprecated C{loseConnection} in
        L{ConnectedDatagramPort.connectionFailed}.
        Fc                  ó   •— d‰ _         y)z8
            Dummy C{stopListening} method.
            TN)Úcalledr1   s   €r   rd   zYConnectedDatagramPortTests.test_connectionFailedCallsStopListening.<locals>.stopListeningC  s   ø€ ð ˆD�Kr   Nr³   )r¹   r
   r´   r   rd   rµ   Ú
assertTrue)r   rd   rf   s   `  r   Ú'test_connectionFailedCallsStopListeningzBConnectedDatagramPortTests.test_connectionFailedCallsStopListening;  sM   ø€ ð ˆŒô	ô ×)Ñ)¨$´³Ó>ˆØ*ˆÔØ×Ñ˜iÔ(Ø�‰˜Ÿ™Õ$r   )r%   r&   r'   r(   ÚskipSocketsri   r¶   r»   rC   r   r   r®   r®   "  s   „ ñð ÐØˆò)ó"%r   r®   c                   ó   — e Zd Zd„ Zy)Ú
WakerTestsc                 óê   — t        «       }| j                  «       }| j                  t        |«      d|«       |j	                  d«       | j                  «       }| j                  t        |«      d|«       y)zH
        No warnings are generated when constructing the waker.
        r   N)r   r   r"   r#   rb   )r   rH   r$   s      r   Ú test_noWakerConstructionWarningsz+WakerTests.test_noWakerConstructionWarningsP  sc   € ô “ˆà×%Ñ%Ó'ˆØ×Ñœ˜X›¨¨8Ô4ð 	×Ñ˜TÔ"Ø×%Ñ%Ó'ˆØ×Ñœ˜X›¨¨8Õ4r   N)r%   r&   r'   rÀ   rC   r   r   r¾   r¾   O  s   „ ó5r   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+   rE   r[   rk   r~   r®   r¾   rC   r   r   ú<module>rË      sÀ   ðñó 
å +Ý 7ß ?Ý 3Ý +Ý +à€ð>Ý%Ý2õ %Ý %ô˜Xô ôD"Ð%ô "ô&-3Ð2ô -3ô`KÐ)ô Kô&* Ð+ô * ôZ_#Ð2ô _#ôD*%Ð!7ô *%ôZ5Ð'õ 5øðw	 ò >Ø=‚Kð>ús   °B! Â!B+Â*B+