
    Vh=.                     \   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    T/home/dcms/DCMS/lib/python3.12/site-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zz~~dB'--/69h>P>P>R Hq$LXJ"W ))+Hzz~~dB'--/69h>P>P>R Hq$LXJ"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       MM&!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    gmmV4gmmW%=%=>gmmW%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-=-=>!eW556eW--.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%5w7G7G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"68I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 )Nd   )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    
 xxr   c                 R    | j                   }|d | _         |j                  |       y y r-   )rn   callback)r   timeoutds      r   doIterationz TimeoutReportReactor.doIteration   s+    !!=$(D!JJw 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qz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    
 II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     DKr   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' 5w	  >=K>s   B! !B+*B+