o
    Òó·iÎ9  ã                   @   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   úeC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/internet/test/test_base.pyÚ<lambda>1   s    z&FakeReactor.__init__.<locals>.<lambda>)	r   Ú_clockZ	callLaterr   r   ÚstartZ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   r/   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   r/   z!FakeReactor.suggestThreadPoolSize)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r   r   Úobjectr"   r#   r'   r*   r,   r.   Úfloatr0   r1   r3   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©ÚipZlookedUpr   r   ÚfakeGetHostByNamei   s   
z=ThreadedResolverTests.test_success.<locals>.fakeGetHostByNameÚgethostbynameé   N)r   Ú
addCleanupr,   ÚpatchÚsocketr
   ÚgetHostByNameÚaddCallbackr=   r*   ÚassertEqualr   ÚadvanceÚcalls)r   r?   ÚtimeoutÚreactorZ
resolvedTorB   ÚresolverÚdr   r@   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   rB   †   ó   z=ThreadedResolverTests.test_failure.<locals>.fakeGetHostByNamerC   ú	some.namerD   N)r   rE   r,   rF   rG   r
   rH   ÚassertFailurer   rI   r=   r*   rJ   Úlenr   rK   rL   ©r   rM   rN   rB   Z
failedWithrO   rP   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   rB   ¦   rS   z=ThreadedResolverTests.test_timeout.<locals>.fakeGetHostByNamerC   rT   rD   zThe I/O was errorfulN)r   rE   r,   r   rF   rG   r
   rH   rU   r   rI   r=   r   rK   rJ   rV   r%   ÚIOErrorrW   r   rZ   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   r?   Ztimeouts©rL   r   r   rH   Ã   s   
zOThreadedResolverTests.test_resolverGivenStr.<locals>.FakeResolver.getHostByNameN)r   )r4   r5   r6   rH   r   r^   r   r   ÚFakeResolverÁ   s    r_   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)r4   r5   r6   r`   r   r   r   r   ÚJustEnoughReactorÇ   s    rb   zexample.exampleu   vÃ¤Ã¤ntynyt.exampleu   Ñ€Ñ„.examplezxn----7sbb4ac0ad0be6cf.xn--p1aié   r   rD   zxn--vntynyt-5waa.exampleé   zxn--p1ai.exampleé   é   N)r   r   r	   ZinstallResolverr   r   ZnameResolverZresolveHostNamerJ   rV   ÚlistÚmapÚtypeÚstr)r   r_   rb   ZfakerN   Zrecr   r^   r   Útest_resolverGivenStrº   s(   

z+ThreadedResolverTests.test_resolverGivenStrN)r4   r5   r6   r7   rQ   rX   r]   rk   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Þ   ó    rm   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ñ   ra   z0DelayedCallMixin._getDelayedCallAt.<locals>.noopc                   S   r-   r   r   r   r   r   r   r   ô   rn   z4DelayedCallMixin._getDelayedCallAt.<locals>.<lambda>r   N)r   )r   Útimerr   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   rD   N)rt   Ú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.
        é   ©re   ÚArc   Nc                   S   rl   ©Ng      ø?r   r   r   r   r   r     rn   z+DelayedCallMixin.test_str.<locals>.<lambda>z?<DelayedCall 0x%x [10.5s] called=0 cancelled=0 nothing(3, A=5)>)r   rm   rJ   rj   Ú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   rl   )Ngš™™™™™ù?r   r   r   r   r   r     rn   z,DelayedCallMixin.test_repr.<locals>.<lambda>)r   rm   rJ   rj   Ú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©ru   rv   Ú
assertTrueÚassertFalse©r   ru   rv   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ˆ   ru   rv   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‡   ru   rv   rˆ   r   r   r   r   Útest_neK  r‘   zDelayedCallMixin.test_neN)r4   r5   r6   r7   rt   rw   r€   r…   rŠ   rŒ   rŽ   r�   r�   r’   r   r   r   r   ro   ä   s    
ro   c                   @   ó(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚDelayedCallNoDebugTestsrp   c                 C   ó   |   tdd¡ t | ¡ dS )z!
        Turn debug off.
        ÚdebugFN©rF   r   ro   rw   r   r   r   r   rw   [  ó   zDelayedCallNoDebugTests.setUpc              	   C   ó>   t dtdddidddd„ ƒ}d t|ƒ¡}|  t|ƒ|¡ dS )	rx   ry   rz   r{   rc   Nc                   S   rl   r|   r   r   r   r   r   r   h  rn   z2DelayedCallNoDebugTests.test_str.<locals>.<lambda>zA<DelayedCall 0x{:x} [10.5s] called=0 cancelled=0 nothing(3, A=5)>)r   rm   Úformatr}   rJ   rj   )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.
        re   c                   S   r-   r   r   r   r   r   r   r   v  rn   z<DelayedCallNoDebugTests.test_switchToDebug.<locals>.<lambda>r   c                   S   rl   ©Nrd   r   r   r   r   r   r   v  rn   rD   útraceback at creationN)r   rm   r–   ZassertNotInr„   r~   r   r   r   Útest_switchToDebugo  ó   z*DelayedCallNoDebugTests.test_switchToDebugN)r4   r5   r6   r7   rw   r€   rŸ   r   r   r   r   r”   V  s
    r”   c                   @   r“   )	ÚDelayedCallDebugTestsrp   c                 C   r•   )z 
        Turn debug on.
        r–   TNr—   r   r   r   r   rw   €  r˜   zDelayedCallDebugTests.setUpc              	   C   r™   )	rx   ry   rz   r{   rc   Nc                   S   rl   r|   r   r   r   r   r   r   �  rn   z0DelayedCallDebugTests.test_str.<locals>.<lambda>z\<DelayedCall 0x{:x} \[10.5s\] called=0 cancelled=0 nothing\(3, A=5\)

traceback at creation:)r   rm   rš   r}   ÚassertRegexrj   )r   r   Z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).
        re   c                   S   r-   r   r   r   r   r   r   r   œ  rn   z<DelayedCallDebugTests.test_switchFromDebug.<locals>.<lambda>r   c                   S   rl   r�   r   r   r   r   r   r   œ  rn   r   rž   N)r   rm   r–   ZassertInr„   r~   r   r   r   Útest_switchFromDebug•  r    z*DelayedCallDebugTests.test_switchFromDebugN)r4   r5   r6   r7   rw   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   rl   )z*
        Required method, unused.
        Nr   r   r   r   r   r`   ¨  rn   z*TestSpySignalCapturingReactor.installWakerN)r4   r5   r6   r7   r`   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¤   ZassertIsÚ_exitSignal©r   rN   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¤   Z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¤   Z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¤   ZsigBreakr±   r¬   r¦   r§   r   r   r   Útest_captureSIGBREAKÎ  s
   
z+ReactorBaseSignalTests.test_captureSIGBREAKN)r4   r5   r6   r7   r¨   r­   r°   r³   r   r   r   r   r¥   ®  s    		r¥   )-r7   rG   Úqueuer   Útypingr   Zunittestr   Zzope.interfacer   Ztyping_extensionsr   Ztwisted.internet._resolverr   Ztwisted.internet.baser   r	   r
   Ztwisted.internet.deferr   Ztwisted.internet.errorr   Ztwisted.internet.interfacesr   r   r   Ztwisted.internet.taskr   Ztwisted.python.threadpoolr   Ztwisted.trial.unittestr   r   rª   Ú_signalÚImportErrorr   r   r;   rm   ro   r”   r¡   r¤   r¥   r   r   r   r   Ú<module>   sB   ÿ/ r%&
