o
    >h9                     @   s~  d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZmZmZ dd
lmZ ddlmZ ddlmZmZmZ ddlmZ ddlmZ ddlmZm Z  zddl!Z"W n e#yo   dZ!Y nw e"Z!edZ$e	eeG dd dZ%G dd de Z&dd Z'G dd dZ(G dd de(e Z)G dd de(e Z*G dd deZ+ee! dG d d! d!e Z,dS )"z%
Tests for L{twisted.internet.base}.
    N)Queue)Callable)skipIf)implementer)	ParamSpec)FirstOneWins)DelayedCallReactorBaseThreadedResolver)Deferred)DNSLookupError)IReactorThreadsIReactorTimeIResolverSimple)Clock)
ThreadPool)SkipTestTestCase_Pc                   @   s   e Zd ZdZdd Zdeeef dejdej	ddfd	d
Z
dd Zdd Zdd ZdefddZdeeef dejdej	ddfddZdd ZdS )FakeReactorzl
    A fake reactor implementation which just supports enough reactor APIs for
    L{ThreadedResolver}.
    c                    s>   t   _ jj _t  _ j   fdd _t  _d S )Nc                      s    j S N)_threadpool selfr   /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/internet/test/test_base.py<lambda>1   s    z&FakeReactor.__init__.<locals>.<lambda>)	r   _clock	callLaterr   r   startgetThreadPoolr   _threadCallsr   r   r   r   __init__+   s   

zFakeReactor.__init__callableargskwargsreturnNc                 O   s   | j |||f d S r   )r!   putr   r#   r$   r%   r   r   r   callFromThread5   s   zFakeReactor.callFromThreadc                 C   s"   | j  \}}}||i | d S r   )r!   get)r   fr$   r%   r   r   r   _runThreadCalls:   s   zFakeReactor._runThreadCallsc                 C   s   | j   d S r   )r   stopr   r   r   r   _stop>   s   zFakeReactor._stopc                 C      d S r   r   r   r   r   r   getDelayedCallsA      zFakeReactor.getDelayedCallsc                 C   r/   r   r   r   r   r   r   secondsE   r1   zFakeReactor.secondsc                 O   r/   r   r   r(   r   r   r   callInThreadI   s   zFakeReactor.callInThreadc                 C   r/   r   r   )r   sizer   r   r   suggestThreadPoolSizeO   r1   z!FakeReactor.suggestThreadPoolSize)__name__
__module____qualname____doc__r"   r   r   objectr$   r%   r)   r,   r.   r0   floatr2   r3   r5   r   r   r   r   r   $   s4    




r   c                   @   0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )ThreadedResolverTestsz(
    Tests for L{ThreadedResolver}.
    c                    s   d d}d}t  }| |j g g } fdd}| td| t|}|||f}||j |	  | 
|g | 
| g |j|d  | 
|jjg  dS )	z
        L{ThreadedResolver.getHostByName} returns a L{Deferred} which fires
        with the value returned by the call to L{socket.gethostbyname} in the
        threadpool of the reactor passed to L{ThreadedResolver.__init__}.
        z	10.0.0.17zfoo.bar.example.com   c                    s    |   S r   )appendnameiplookedUpr   r   fakeGetHostByNamei   s   
z=ThreadedResolverTests.test_success.<locals>.fakeGetHostByNamegethostbyname   N)r   
addCleanupr.   patchsocketr
   getHostByNameaddCallbackr?   r,   assertEqualr   advancecalls)r   rA   timeoutreactor
resolvedTorE   resolverdr   rB   r   test_successY   s"   z"ThreadedResolverTests.test_successc                 C   s   d}t  }| |j dd }| td| g }t|}|d|f}| |t |	|j
 |  | t|d |j|d  | |jjg  dS )z
        L{ThreadedResolver.getHostByName} returns a L{Deferred} which fires a
        L{Failure} if the call to L{socket.gethostbyname} raises an exception.
        r>   c                 S   s   t d)NzENOBUFS (this is a funny joke))OSErrorr@   r   r   r   rE         z=ThreadedResolverTests.test_failure.<locals>.fakeGetHostByNamerF   	some.namerG   N)r   rH   r.   rI   rJ   r
   rK   assertFailurer   rL   r?   r,   rM   lenr   rN   rO   r   rP   rQ   rE   
failedWithrS   rT   r   r   r   test_failure|   s   z"ThreadedResolverTests.test_failurec                    s   d}t  }| |j t   fdd}| td| g }t|}|d|f}| |t	 |
|j |j|d  | |g  |jd | t|d  td dS )	z
        If L{socket.gethostbyname} does not complete before the specified
        timeout elapsed, the L{Deferred} returned by
        L{ThreadedResolver.getHostByName} fails with L{DNSLookupError}.
        
   c                    s      r   )r*   r@   resultr   r   rE      rW   z=ThreadedResolverTests.test_timeout.<locals>.fakeGetHostByNamerF   rX   rG   zThe I/O was errorfulN)r   rH   r.   r   rI   rJ   r
   rK   rY   r   rL   r?   r   rN   rM   rZ   r'   IOErrorr[   r   r_   r   test_timeout   s    z"ThreadedResolverTests.test_timeoutc                    s  g  t tG  fddd}G dd dt}| }| }|| tt }|j|d |j|d |j|d |j|d |j|d | t	 d	 | t
tt tgd	  | d d
  | d d  | d d  | d d  | d d  dS )zm
        L{ThreadedResolver.getHostByName} is passed L{str}, encoded using IDNA
        if required.
        c                       s   e Zd Zd fdd	ZdS )zAThreadedResolverTests.test_resolverGivenStr.<locals>.FakeResolverr   c                    s     | t S r   )r?   r   )r   rA   timeoutsrO   r   r   rK      s   
zOThreadedResolverTests.test_resolverGivenStr.<locals>.FakeResolver.getHostByNameN)r   )r6   r7   r8   rK   r   rd   r   r   FakeResolver   s    re   c                   @   s   e Zd Zdd ZdS )zFThreadedResolverTests.test_resolverGivenStr.<locals>.JustEnoughReactorc                 S   r/   r   r   r   r   r   r   installWaker      zSThreadedResolverTests.test_resolverGivenStr.<locals>.JustEnoughReactor.installWakerN)r6   r7   r8   rf   r   r   r   r   JustEnoughReactor   s    rh   zexample.exampleu   vääntynyt.exampleu   рф.examplezxn----7sbb4ac0ad0be6cf.xn--p1ai   r   rG   zxn--vntynyt-5waa.example   zxn--p1ai.example      N)r   r   r	   installResolverr   r   nameResolverresolveHostNamerM   rZ   listmaptypestr)r   re   rh   fakerQ   recr   rd   r   test_resolverGivenStr   s(   

z+ThreadedResolverTests.test_resolverGivenStrN)r6   r7   r8   r9   rU   r]   rb   rv   r   r   r   r   r=   T   s    #!r=   c                   C      dS )z8
    Function used by L{DelayedCallTests.test_str}.
    Nr   r   r   r   r   nothing       rx   c                   @   s`   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d Zdd ZdS )DelayedCallMixin
    L{DelayedCall}
    c                 C   s    dd }t |dd di ||dS )z
        Get a L{DelayedCall} instance at a given C{time}.

        @param time: The absolute time at which the returned L{DelayedCall}
            will be scheduled.
        c                 S   r/   r   r   )callr   r   r   noop   rg   z0DelayedCallMixin._getDelayedCallAt.<locals>.noopc                   S   r/   r   r   r   r   r   r   r      ry   z4DelayedCallMixin._getDelayedCallAt.<locals>.<lambda>r   N)r   )r   timer}   r   r   r   _getDelayedCallAt   s   z"DelayedCallMixin._getDelayedCallAtc                 C   s   |  d| _|  d| _dS )zb
        Create two L{DelayedCall} instanced scheduled to run at different
        times.
        r   rG   N)r   zerooner   r   r   r   setUp   s   zDelayedCallMixin.setUpc              	   C   s:   t dtdddidddd }| t|dt|f  dS )	
        The string representation of a L{DelayedCall} instance, as returned by
        L{str}, includes the unsigned id of the instance, as well as its state,
        the function to be called, and the function arguments.
           rk   Ari   Nc                   S   rw   Ng      ?r   r   r   r   r   r     ry   z+DelayedCallMixin.test_str.<locals>.<lambda>z?<DelayedCall 0x%x [10.5s] called=0 cancelled=0 nothing(3, A=5)>)r   rx   rM   rs   idr   dcr   r   r   test_str   s   zDelayedCallMixin.test_strc              	   C   s4   t dtdddidddd }| t|t| dS )z
        The string representation of a L{DelayedCall} instance, as returned by
        {repr}, is identical to that returned by L{str}.
           )   r   	   Nc                   S   rw   )Ng?r   r   r   r   r   r     ry   z,DelayedCallMixin.test_repr.<locals>.<lambda>)r   rx   rM   rs   reprr   r   r   r   	test_repr  s   zDelayedCallMixin.test_reprc                 C   sJ   | j | j}}| ||k  | ||k  | ||k  | ||k  dS )z
        For two instances of L{DelayedCall} C{a} and C{b}, C{a < b} is true
        if and only if C{a} is scheduled to run before C{b}.
        Nr   r   
assertTrueassertFalser   r   r   r   r   r   test_lt  
   zDelayedCallMixin.test_ltc                 C   sJ   | j | j}}| ||k | ||k | ||k | ||k dS )z
        For two instances of L{DelayedCall} C{a} and C{b}, C{a <= b} is true
        if and only if C{a} is scheduled to run before C{b} or at the same
        time as C{b}.
        Nr   r   r   r   r   test_le  
   zDelayedCallMixin.test_lec                 C   sJ   | j | j}}| ||k | ||k | ||k | ||k dS )z
        For two instances of L{DelayedCall} C{a} and C{b}, C{a > b} is true
        if and only if C{a} is scheduled to run after C{b}.
        Nr   r   r   r   r   test_gt*  r   zDelayedCallMixin.test_gtc                 C   sJ   | j | j}}| ||k | ||k | ||k | ||k dS )z
        For two instances of L{DelayedCall} C{a} and C{b}, C{a > b} is true
        if and only if C{a} is scheduled to run after C{b} or at the same
        time as C{b}.
        Nr   r   r   r   r   test_ge5  r   zDelayedCallMixin.test_gec                 C   s:   |  | j| jk | | j| jk | | j| jk dS )zD
        A L{DelayedCall} instance is only equal to itself.
        N)r   r   r   r   r   r   r   r   test_eqA     zDelayedCallMixin.test_eqc                 C   s:   |  | j| jk | | j| jk | | j| jk dS )zM
        A L{DelayedCall} instance is not equal to any other object.
        N)r   r   r   r   r   r   r   r   test_neK  r   zDelayedCallMixin.test_neN)r6   r7   r8   r9   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rz      s    
rz   c                   @   (   e Zd ZdZdd Zdd Zdd ZdS )	DelayedCallNoDebugTestsr{   c                 C      |  tdd t|  dS )z!
        Turn debug off.
        debugFNrI   r   rz   r   r   r   r   r   r   [     zDelayedCallNoDebugTests.setUpc              	   C   >   t dtdddidddd }dt|}| t|| dS )	r   r   r   r   ri   Nc                   S   rw   r   r   r   r   r   r   r   h  ry   z2DelayedCallNoDebugTests.test_str.<locals>.<lambda>zA<DelayedCall 0x{:x} [10.5s] called=0 cancelled=0 nothing(3, A=5)>)r   rx   formatr   rM   rs   )r   r   expectedr   r   r   r   b  s
   
z DelayedCallNoDebugTests.test_strc              	   C   6   t ddd di ttdd }d|_| dt| dS )	z
        If L{DelayedCall.debug} changes from C{0} to C{1} between
        L{DelayeCall.__init__} and L{DelayedCall.__repr__} then
        L{DelayedCall.__repr__} returns a string that does not include the
        creator stack.
        rk   c                   S   r/   r   r   r   r   r   r   r   v  ry   z<DelayedCallNoDebugTests.test_switchToDebug.<locals>.<lambda>r   c                   S   rw   Nrj   r   r   r   r   r   r   v  ry   rG   traceback at creationN)r   rx   r   assertNotInr   r   r   r   r   test_switchToDebugo     z*DelayedCallNoDebugTests.test_switchToDebugN)r6   r7   r8   r9   r   r   r   r   r   r   r   r   V  s
    r   c                   @   r   )	DelayedCallDebugTestsr{   c                 C   r   )z 
        Turn debug on.
        r   TNr   r   r   r   r   r     r   zDelayedCallDebugTests.setUpc              	   C   r   )	r   r   r   r   ri   Nc                   S   rw   r   r   r   r   r   r   r     ry   z0DelayedCallDebugTests.test_str.<locals>.<lambda>z\<DelayedCall 0x{:x} \[10.5s\] called=0 cancelled=0 nothing\(3, A=5\)

traceback at creation:)r   rx   r   r   assertRegexrs   )r   r   expectedRegexpr   r   r   r     s
   
zDelayedCallDebugTests.test_strc              	   C   r   )	a  
        If L{DelayedCall.debug} changes from C{1} to C{0} between
        L{DelayeCall.__init__} and L{DelayedCall.__repr__} then
        L{DelayedCall.__repr__} returns a string that includes the creator
        stack (we captured it, we might as well display it).
        rk   c                   S   r/   r   r   r   r   r   r   r     ry   z<DelayedCallDebugTests.test_switchFromDebug.<locals>.<lambda>r   c                   S   rw   r   r   r   r   r   r   r     ry   r   r   N)r   rx   r   assertInr   r   r   r   r   test_switchFromDebug  r   z*DelayedCallDebugTests.test_switchFromDebugN)r6   r7   r8   r9   r   r   r   r   r   r   r   r   {  s
    r   c                   @   s   e Zd ZdZdd ZdS )TestSpySignalCapturingReactorza
    Subclass of ReactorBase to capture signals delivered to the
    reactor for inspection.
    c                 C   rw   )z*
        Required method, unused.
        Nr   r   r   r   r   rf     ry   z*TestSpySignalCapturingReactor.installWakerN)r6   r7   r8   r9   rf   r   r   r   r   r     s    r   zsignal module not availablec                   @   r<   )ReactorBaseSignalTestszE
    Tests to exercise ReactorBase's signal exit reporting path.
    c                 C   s   t  }| d|j dS )zI
        The default value of the _exitSignal attribute is None.
        N)r   assertIs_exitSignalr   rQ   r   r   r   test_exitSignalDefaultsToNone  s   z4ReactorBaseSignalTests.test_exitSignalDefaultsToNonec                 C   (   t  }|tjd | tj|j dS )zn
        ReactorBase's SIGINT handler saves the value of SIGINT to the
        _exitSignal attribute.
        N)r   sigIntsignalSIGINTassertEqualsr   r   r   r   r   test_captureSIGINT     z)ReactorBaseSignalTests.test_captureSIGINTc                 C   r   )zp
        ReactorBase's SIGTERM handler saves the value of SIGTERM to the
        _exitSignal attribute.
        N)r   sigTermr   SIGTERMr   r   r   r   r   r   test_captureSIGTERM  r   z*ReactorBaseSignalTests.test_captureSIGTERMc                 C   s:   t tds	tdt }|tjd | tj|j dS )zr
        ReactorBase's SIGBREAK handler saves the value of SIGBREAK to the
        _exitSignal attribute.
        SIGBREAKz$signal module does not have SIGBREAKN)hasattrr   r   r   sigBreakr   r   r   r   r   r   r   test_captureSIGBREAK  s
   
z+ReactorBaseSignalTests.test_captureSIGBREAKN)r6   r7   r8   r9   r   r   r   r   r   r   r   r   r     s    		r   )-r9   rJ   queuer   typingr   unittestr   zope.interfacer   typing_extensionsr   twisted.internet._resolverr   twisted.internet.baser   r	   r
   twisted.internet.deferr   twisted.internet.errorr   twisted.internet.interfacesr   r   r   twisted.internet.taskr   twisted.python.threadpoolr   twisted.trial.unittestr   r   r   _signalImportErrorr   r   r=   rx   rz   r   r   r   r   r   r   r   r   <module>   sB   / r%&
