
    Yf=.                        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 n# e$ r dZY nw xY w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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                   ,     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                            dd                                          dk    r)t          j                    r|                                  d S d S d S )NCI true)	supersetUposenvirongetlowerr   	isWindowsflushWarnings)self	__class__s    V/var/www/html/env/lib/python3.11/site-packages/twisted/internet/test/test_posixbase.pyr   zWarningCheckerTestCase.setUp    sq     :>>$##))++v55(:L:N:N5      6555    c                 p   	 t                                                       |                                 }t          j                            dd                                          dk    rt          j                    rd S | 	                    t          |          dd|            d S # |                                 }t          j                            dd                                          dk    rt          j                    r Y d S | 	                    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-   	GG))++Hz~~dB''--//699h>P>R>R9 Hq"WX"W"W     ))++Hz~~dB''--//699h>P>R>R9 Hq"WX"W"W   s    B+ +AD5(D5)__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dS )TrivialReactorc                 J    i | _         i | _        t          j        |            d S N)_readers_writersr   __init__r   s    r   r0   zTrivialReactor.__init__>   s'    !$'''''r   c                     d| j         |<   d S NTr.   r   readers     r   	addReaderzTrivialReactor.addReaderC        $fr   c                     | j         |= d S r-   r4   r5   s     r   removeReaderzTrivialReactor.removeReaderF       M&!!!r   c                     d| j         |<   d S r3   r/   r   writers     r   	addWriterzTrivialReactor.addWriterI   r8   r   c                     | j         |= d S 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dS )PosixReactorBaseTestsz(
    Tests for L{PosixReactorBase}.
    c                     |                      |j        t                     |                     |j        |j                   |                     |j        |j                   d S r-   )assertIsInstancewakerr   assertIn_internalReadersr.   r   r   s     r   _checkWakerz!PosixReactorBaseTests._checkWakerU   sR    gmV444gmW%=>>>gmW%566666r   c                 L    t                      }|                     |           dS )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                 ^   t                      }t                      }|j                            |            |j        |            |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+   objectrJ   addr7   
_removeAllr.   r/   rL   rI   )r   r   extras      r   "test_removeAllSkipsInternalReadersz8PosixReactorBaseTests.test_removeAllSkipsInternalReadersb   s    
 !"" $$U+++%   7+W-=>>>!!!eW5666eW-.....r   c                    t                      }t                      }t                      } |j        |            |j        |            |j        |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+   rP   r7   r@   rR   r.   r/   r"   setassertNotIn)r   r   r6   r?   removeds        r   'test_removeAllReturnsRemovedDescriptorsz=PosixReactorBaseTests.test_removeAllReturnsRemovedDescriptorsp   s    
 !""&!!!&!!!$'$W%5w7GHHW'7888!1222!122222r   N)r%   r&   r'   r(   rL   rN   rT   rY   rC   r   r   rE   rE   P   sZ         7 7 7
" " "/ / /3 3 3 3 3r   rE   c                   4    e Zd ZdZ eee          sdZd ZdS )TCPPortTestsz1
    Tests for L{twisted.internet.tcp.Port}.
    zNon-posixbase reactorc                     t          dt                                }d|_        d |_        |                     |                                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   sL    
 E=??++33!!$"4"4"6"68IJJJr   N)	r%   r&   r'   r(   
isinstancer   r   skiprg   rC   r   r   r[   r[      sN          :g/00 '&K K K K Kr   r[   c                   J    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
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                 b    t          j        |            t                      | _        d| _        d S )Nd   )r   r0   r   iterationTimeoutnowr1   s    r   r0   zTimeoutReportReactor.__init__   s*    !$''' (

r   r6   returnNc                     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.
        NrC   r5   s     r   r7   zTimeoutReportReactor.addReader         r   c                     dS )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r   c                 T    | j         }|d | _         |                    |           d S d S r-   )rn   callback)r   timeoutds      r   doIterationz TimeoutReportReactor.doIteration   s6    !=$(D!JJw =r   )r%   r&   r'   r(   r0   r   r7   r:   ru   rw   r|   rC   r   r   rk   rk      s           
 D    ? t    
             r   rk   c                   B    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
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                     g }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   sY     ,,W^<<< ,,-K-K-K-KLLLqzr   c                 v    t                      }|                     |          }|                     |           dS )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6'"""""r   c                     t                      } |j        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.
        rm   c                      d S r-   rC   rC   r   r   r`   z8IterationTimeoutTests.test_delayedCall.<locals>.<lambda>       t r   Nrk   	callLaterr   r"   r   s      r   test_delayedCallz&IterationTimeoutTests.test_delayedCall   sU     '((#||,,,--g66#&&&&&r   c                     t                      } |j        dd            |xj        dz  c_        |                     |          }|                     |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.
        rm   c                      d S r-   rC   rC   r   r   r`   z7IterationTimeoutTests.test_timePasses.<locals>.<lambda>   r   r      K   N)rk   r   ro   r   r"   r   s      r   test_timePassesz%IterationTimeoutTests.test_timePasses   se     '((#||,,,r--g66"%%%%%r   c                     t                      } |j        dd             |j        dd             |j        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                      d S r-   rC   rC   r   r   r`   zAIterationTimeoutTests.test_multipleDelayedCalls.<locals>.<lambda>       d r   
   c                      d S r-   rC   rC   r   r   r`   zAIterationTimeoutTests.test_multipleDelayedCalls.<locals>.<lambda>   r   r   rm   c                      d S r-   rC   rC   r   r   r`   zAIterationTimeoutTests.test_multipleDelayedCalls.<locals>.<lambda>   r   r   Nr   r   s      r   test_multipleDelayedCallsz/IterationTimeoutTests.test_multipleDelayedCalls   s     '(("ll+++"ll+++#||,,,--g66"%%%%%r   c                     t                      } |j        dd           }|xj        dz  c_        |                    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.
        r   c                      d S r-   rC   rC   r   r   r`   z=IterationTimeoutTests.test_resetDelayedCall.<locals>.<lambda>      T r   r      N)rk   r   ro   resetr   r"   r   r   callrz   s       r   test_resetDelayedCallz+IterationTimeoutTests.test_resetDelayedCall   ss     '(( w \\22r

2--g66"%%%%%r   c                     t                      } |j        dd           }|xj        dz  c_        |                    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.
        r   c                      d S r-   rC   rC   r   r   r`   z=IterationTimeoutTests.test_delayDelayedCall.<locals>.<lambda>  r   r   r      <   N)rk   r   ro   delayr   r"   r   s       r   test_delayDelayedCallz+IterationTimeoutTests.test_delayDelayedCall  ss     '(( w \\22r

2--g66"%%%%%r   c                     t                      } |j        dd           }|                                 |                     |          }|                     |           dS )zp
        If the only delayed call is canceled, L{None} is the timeout passed
        to C{doIteration}.
        r   c                      d S r-   rC   rC   r   r   r`   z>IterationTimeoutTests.test_cancelDelayedCall.<locals>.<lambda>  r   r   N)rk   r   cancelr   r   r   s       r   test_cancelDelayedCallz,IterationTimeoutTests.test_cancelDelayedCall  s_    
 '(( w \\22--g6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dS )ConnectedDatagramPortTestsz/
    Test connected datagram UNIX sockets.
    Nc                       fd}t          j        dt                                }||_        |                    d           dS )z
        L{ConnectedDatagramPort} does not call the deprecated C{loseConnection}
        in L{ConnectedDatagramPort.connectionFailed}.
        c                  2                          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)failr1   s   r   loseConnectionz`ConnectedDatagramPortTests.test_connectionFailedDoesntCallLoseConnection.<locals>.loseConnection0  s    
 IIOPPPPPr   Ngoodbye)r
   ConnectedDatagramPortr   r   connectionFailed)r   r   rf   s   `  r   -test_connectionFailedDoesntCallLoseConnectionzHConnectedDatagramPortTests.test_connectionFailedDoesntCallLoseConnection*  s[    	Q 	Q 	Q 	Q 	Q )$>>,i(((((r   c                      d _          fd}t          j        dt                                }||_        |                    d                                 j                    dS )z
        L{ConnectedDatagramPort} calls L{ConnectedDatagramPort.stopListening}
        instead of the deprecated C{loseConnection} in
        L{ConnectedDatagramPort.connectionFailed}.
        Fc                      d _         dS )z8
            Dummy C{stopListening} method.
            TN)calledr1   s   r   rd   zYConnectedDatagramPortTests.test_connectionFailedCallsStopListening.<locals>.stopListeningC  s     DKKKr   Nr   )r   r
   r   r   rd   r   
assertTrue)r   rd   rf   s   `  r   'test_connectionFailedCallsStopListeningzBConnectedDatagramPortTests.test_connectionFailedCallsStopListening;  ss     	 	 	 	 	 )$>>*i((($$$$$r   )r%   r&   r'   r(   skipSocketsri   r   r   rC   r   r   r   r   "  sI          ) ) )"% % % % %r   r   c                       e Zd Zd ZdS )
WakerTestsc                 ,   t                      }|                                 }|                     t          |          d|           |                    d           |                                 }|                     t          |          d|           dS )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  s     %%''X8444 	T"""%%''X844444r   N)r%   r&   r'   r   rC   r   r   r   r   O  s#        5 5 5 5 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 7 7 7 7 7 ? ? ? ? ? ? ? ? 3 3 3 3 3 3 + + + + + + + + + + + +>%%%%%%2222222 > > >=KKK> % $ $ $ $ $ % % % % % %    X   D" " " " "% " " "&-3 -3 -3 -3 -32 -3 -3 -3`K K K K K) K K K&*  *  *  *  * + *  *  * Z_# _# _# _# _#2 _# _# _#D*% *% *% *% *%!7 *% *% *%Z5 5 5 5 5' 5 5 5 5 5s   = AA