U
    P|jÎ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#k
rà   dZ!Y nX 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eeef ejej	ddœdd„Z
dd	„ Zd
d„ Zdd„ Zedœdd„Zeeef ejej	ddœ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   úC/tmp/pip-unpacked-wheel-audpp6lv/twisted/internet/test/test_base.pyÚ<lambda>1   ó    z&FakeReactor.__init__.<locals>.<lambda>)	r   Ú_clockZ	callLaterr   r   ÚstartZgetThreadPoolr   Ú_threadCallsr   r   r   r   Ú__init__+   s    

zFakeReactor.__init__N)ÚcallableÚargsÚkwargsÚreturnc                 O   s   | j  |||f¡ d S r   )r    Úput©r   r"   r#   r$   r   r   r   ÚcallFromThread5   s    zFakeReactor.callFromThreadc                 C   s   | j  ¡ \}}}|||Ž 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   s   d S r   r   r   r   r   r   ÚgetDelayedCallsA   s    zFakeReactor.getDelayedCalls)r%   c                 C   s   d S r   r   r   r   r   r   ÚsecondsE   s    zFakeReactor.secondsc                 O   s   d S r   r   r'   r   r   r   ÚcallInThreadI   s    zFakeReactor.callInThreadc                 C   s   d S r   r   )r   Úsizer   r   r   ÚsuggestThreadPoolSizeO   s    z!FakeReactor.suggestThreadPoolSize)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   r   r   Úobjectr#   r$   r(   r+   r-   r.   Úfloatr/   r0   r2   r   r   r   r   r   $   s"   
  þ
  þr   c                   @   s0   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
resolvedTor@   Ú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ƒ‚d S )NzENOBUFS (this is a funny joke))ÚOSErrorr<   r   r   r   r@   †   s    z=ThreadedResolverTests.test_failure.<locals>.fakeGetHostByNamerA   ú	some.namerB   N)r   rC   r-   rD   rE   r
   rF   ÚassertFailurer   rG   r;   r+   rH   Úlenr   rI   rJ   ©r   rK   rL   r@   Z
failedWithrM   rN   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   ˆ   ¡ ‚d S r   )r)   r<   ©Úresultr   r   r@   ¦   s    z=ThreadedResolverTests.test_timeout.<locals>.fakeGetHostByNamerA   rQ   rB   zThe I/O was errorfulN)r   rC   r-   r   rD   rE   r
   rF   rR   r   rG   r;   r   rI   rH   rS   r&   ÚIOErrorrT   r   rW   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©rJ   r   r   rF   Ã   s    
zOThreadedResolverTests.test_resolverGivenStr.<locals>.FakeResolver.getHostByNameN)r   )r3   r4   r5   rF   r   r[   r   r   ÚFakeResolverÁ   s   r\   c                   @   s   e Zd Zdd„ ZdS )zFThreadedResolverTests.test_resolverGivenStr.<locals>.JustEnoughReactorc                 S   s   d S r   r   r   r   r   r   ÚinstallWakerÈ   s    zSThreadedResolverTests.test_resolverGivenStr.<locals>.JustEnoughReactor.installWakerN)r3   r4   r5   r]   r   r   r   r   ÚJustEnoughReactorÇ   s   r^   zexample.exampleu   vÃ¤Ã¤ntynyt.exampleu   Ñ€Ñ„.examplezxn----7sbb4ac0ad0be6cf.xn--p1aié   r   rB   zxn--vntynyt-5waa.exampleé   zxn--p1ai.exampleé   é   N)r   r   r	   ZinstallResolverr   r   ZnameResolverZresolveHostNamerH   rS   ÚlistÚmapÚtypeÚstr)r   r\   r^   ZfakerL   Zrecr   r[   r   Útest_resolverGivenStrº   s(    

z+ThreadedResolverTests.test_resolverGivenStrN)r3   r4   r5   r6   rO   rU   rZ   rg   r   r   r   r   r9   T   s
   #!r9   c                   C   s   dS )z8
    Function used by L{DelayedCallTests.test_str}.
    Nr   r   r   r   r   ÚnothingÞ   s    rh   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   s   d S r   r   )Úcallr   r   r   Únoopñ   s    z0DelayedCallMixin._getDelayedCallAt.<locals>.noopc                   S   s   d S r   r   r   r   r   r   r   ô   r   z4DelayedCallMixin._getDelayedCallAt.<locals>.<lambda>r   N)r   )r   Útimerl   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   rB   N)rn   Ú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.
        é   ©ra   ÚAr_   Nc                   S   s   dS ©Ng      ø?r   r   r   r   r   r     r   z+DelayedCallMixin.test_str.<locals>.<lambda>z?<DelayedCall 0x%x [10.5s] called=0 cancelled=0 nothing(3, A=5)>)r   rh   rH   rf   Ú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}.
        é   )é   ru   é	   Nc                   S   s   dS )Ngš™™™™™ù?r   r   r   r   r   r     r   z,DelayedCallMixin.test_repr.<locals>.<lambda>)r   rh   rH   rf   Úreprrx   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©ro   rp   Ú
assertTrueÚassertFalse©r   ro   rp   r   r   r   Útest_lt  s
    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  s
    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*  s
    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  s
    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‚   ro   rp   r�   r   r   r   r   Útest_eqA  s    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�   ro   rp   r‚   r   r   r   r   Útest_neK  s    zDelayedCallMixin.test_neN)r3   r4   r5   r6   rn   rq   rz   r   r„   r…   r†   r‡   rˆ   r‰   r   r   r   r   ri   ä   s   
ri   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚDelayedCallNoDebugTestsrj   c                 C   s   |   tdd¡ t | ¡ dS )z!
        Turn debug off.
        ÚdebugFN©rD   r   ri   rq   r   r   r   r   rq   [  s    zDelayedCallNoDebugTests.setUpc              	   C   s>   t dtdddidddd„ ƒ}d t|ƒ¡}|  t|ƒ|¡ dS )	rr   rs   rt   ru   r_   Nc                   S   s   dS rv   r   r   r   r   r   r   h  r   z2DelayedCallNoDebugTests.test_str.<locals>.<lambda>zA<DelayedCall 0x{:x} [10.5s] called=0 cancelled=0 nothing(3, A=5)>)r   rh   Úformatrw   rH   rf   )r   ry   Úexpectedr   r   r   rz   b  s    ÿÿz DelayedCallNoDebugTests.test_strc              	   C   s6   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.
        ra   c                   S   s   d S r   r   r   r   r   r   r   v  r   z<DelayedCallNoDebugTests.test_switchToDebug.<locals>.<lambda>r   c                   S   s   dS ©Nr`   r   r   r   r   r   r   v  r   rB   útraceback at creationN)r   rh   r‹   ZassertNotInr~   rx   r   r   r   Útest_switchToDebugo  s    z*DelayedCallNoDebugTests.test_switchToDebugN)r3   r4   r5   r6   rq   rz   r‘   r   r   r   r   rŠ   V  s   rŠ   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚDelayedCallDebugTestsrj   c                 C   s   |   tdd¡ t | ¡ dS )z 
        Turn debug on.
        r‹   TNrŒ   r   r   r   r   rq   €  s    zDelayedCallDebugTests.setUpc              	   C   s>   t dtdddidddd„ ƒ}d t|ƒ¡}|  t|ƒ|¡ dS )	rr   rs   rt   ru   r_   Nc                   S   s   dS rv   r   r   r   r   r   r   �  r   z0DelayedCallDebugTests.test_str.<locals>.<lambda>z\<DelayedCall 0x{:x} \[10.5s\] called=0 cancelled=0 nothing\(3, A=5\)

traceback at creation:)r   rh   r�   rw   ÚassertRegexrf   )r   ry   ZexpectedRegexpr   r   r   rz   ‡  s    þÿzDelayedCallDebugTests.test_strc              	   C   s6   t ddd„ di ttdd„ ƒ}d|_|  dt|ƒ¡ dS )	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).
        ra   c                   S   s   d S r   r   r   r   r   r   r   œ  r   z<DelayedCallDebugTests.test_switchFromDebug.<locals>.<lambda>r   c                   S   s   dS r�   r   r   r   r   r   r   œ  r   r   r�   N)r   rh   r‹   ZassertInr~   rx   r   r   r   Útest_switchFromDebug•  s    z*DelayedCallDebugTests.test_switchFromDebugN)r3   r4   r5   r6   rq   rz   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   s   dS )z*
        Required method, unused.
        Nr   r   r   r   r   r]   ¨  s    z*TestSpySignalCapturingReactor.installWakerN)r3   r4   r5   r6   r]   r   r   r   r   r•   ¡  s   r•   zsignal module not availablec                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )Ú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   rL   r   r   r   Útest_exitSignalDefaultsToNoneµ  s    z4ReactorBaseSignalTests.test_exitSignalDefaultsToNonec                 C   s(   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¼  s    z)ReactorBaseSignalTests.test_captureSIGINTc                 C   s(   t ƒ }| tjd¡ |  tj|j¡ dS )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Å  s    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)r3   r4   r5   r6   r™   r�   rŸ   r¢   r   r   r   r   r–   ®  s
   		r–   )-r6   rE   Ú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   r9   rh   ri   rŠ   r’   r•   r–   r   r   r   r   Ú<module>   s>   
/ r%&
