o
    ¯bt5  ã                   @   sn  d Z ddl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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"yk   dZ Y nw e!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)ÚAnyÚCallable)ÚskipIf)Úimplementer)ÚFirstOneWins)ÚDelayedCallÚReactorBaseÚThreadedResolver)ÚDeferred)ÚDNSLookupError)ÚIReactorThreadsÚIReactorTimeÚIResolverSimple)ÚClock)Ú
ThreadPool)ÚSkipTestÚTestCasec                   @   sl   e Zd ZdZdd„ Zdedef fdd„Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdedef 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   úA/usr/lib/python3/dist-packages/twisted/internet/test/test_base.pyÚ<lambda>-   s    z&FakeReactor.__init__.<locals>.<lambda>)	r   Ú_clockÚ	callLaterr   r   ÚstartÚgetThreadPoolr   Ú_threadCallsr   r   r   r   Ú__init__'   s   

zFakeReactor.__init__Úcallable.c                 O   s   | j  |||f¡ d S r   )r    Úput©r   r"   ÚargsÚkwargsr   r   r   ÚcallFromThread1   s   zFakeReactor.callFromThreadc                 C   s"   | j  ¡ \}}}||i |¤Ž d S r   )r    Úget)r   Úfr%   r&   r   r   r   Ú_runThreadCalls4   s   zFakeReactor._runThreadCallsc                 C   s   | j  ¡  d S r   )r   Ústopr   r   r   r   Ú_stop8   s   zFakeReactor._stopc                 C   ó   d S r   r   r   r   r   r   ÚgetDelayedCalls;   ó   zFakeReactor.getDelayedCallsc                 C   r-   r   r   r   r   r   r   Úseconds?   r/   zFakeReactor.secondsc                 O   r-   r   r   r$   r   r   r   ÚcallInThreadC   r/   zFakeReactor.callInThreadc                 C   r-   r   r   )r   Úsizer   r   r   ÚsuggestThreadPoolSizeG   r/   z!FakeReactor.suggestThreadPoolSizeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   r   r   r'   r*   r,   r.   r0   r1   r3   r   r   r   r   r       s    
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   ÚfakeGetHostByNamea   s   
z=ThreadedResolverTests.test_success.<locals>.fakeGetHostByNameÚgethostbynameé   N)r   Ú
addCleanupr,   ÚpatchÚsocketr
   ÚgetHostByNameÚaddCallbackr;   r*   ÚassertEqualr   ÚadvanceÚcalls)r   r=   ÚtimeoutÚreactorÚ
resolvedTorA   ÚresolverÚdr   r>   r   Útest_successQ   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   rA   ~   ó   z=ThreadedResolverTests.test_failure.<locals>.fakeGetHostByNamerB   ú	some.namerC   N)r   rD   r,   rE   rF   r
   rG   ÚassertFailurer   rH   r;   r*   rI   Úlenr   rJ   rK   ©r   rL   rM   rA   Ú
failedWithrO   rP   r   r   r   Útest_failuret   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   rA   ž   rS   z=ThreadedResolverTests.test_timeout.<locals>.fakeGetHostByNamerB   rT   rC   zThe I/O was errorfulN)r   rD   r,   r   rE   rF   r
   rG   rU   r   rH   r;   r   rJ   rI   rV   r#   ÚIOErrorrW   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   r=   Útimeouts©rK   r   r   rG   »   s   
zOThreadedResolverTests.test_resolverGivenStr.<locals>.FakeResolver.getHostByNameN)r   )r4   r5   r6   rG   r   r`   r   r   ÚFakeResolver¹   s    ra   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   rb   r   r   r   r   ÚJustEnoughReactor¿   s    rd   zexample.exampleu   vÃ¤Ã¤ntynyt.exampleu   Ñ€Ñ„.examplezxn----7sbb4ac0ad0be6cf.xn--p1aié   r   rC   zxn--vntynyt-5waa.exampleé   zxn--p1ai.exampleé   é   N)r   r   r	   ÚinstallResolverr   r   ÚnameResolverÚresolveHostNamerI   rV   ÚlistÚmapÚtypeÚstr)r   ra   rd   ÚfakerM   Úrecr   r`   r   Útest_resolverGivenStr²   s(   

z+ThreadedResolverTests.test_resolverGivenStrN)r4   r5   r6   r7   rQ   rY   r^   rr   r   r   r   r   r9   L   s    #!r9   c                   C   ó   dS )z8
    Function used by L{DelayedCallTests.test_str}.
    Nr   r   r   r   r   ÚnothingÖ   ó    rt   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é   rc   z0DelayedCallMixin._getDelayedCallAt.<locals>.noopc                   S   r-   r   r   r   r   r   r   r   ì   ru   z4DelayedCallMixin._getDelayedCallAt.<locals>.<lambda>r   N)r   )r   Útimery   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   rC   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.
        é   ©rg   ÚAre   Nc                   S   rs   ©Ng      ø?r   r   r   r   r   r   ü   ru   z+DelayedCallMixin.test_str.<locals>.<lambda>z?<DelayedCall 0x%x [10.5s] called=0 cancelled=0 nothing(3, A=5)>)r   rt   rI   ro   Ú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   rs   )Ngš™™™™™ù?r   r   r   r   r   r     ru   z,DelayedCallMixin.test_repr.<locals>.<lambda>)r   rt   rI   ro   Ú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_ge-  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_eq9  ó   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_neC  r˜   zDelayedCallMixin.test_neN)r4   r5   r6   r7   r{   r~   r‡   rŒ   r‘   r“   r•   r–   r—   r™   r   r   r   r   rv   Ü   s    
rv   c                   @   ó    e Zd ZdZdd„ Zdd„ ZdS )ÚDelayedCallNoDebugTestsrw   c                 C   ó   |   tdd¡ t | ¡ dS )z!
        Turn debug off.
        ÚdebugFN©rE   r   rv   r~   r   r   r   r   r~   S  ó   zDelayedCallNoDebugTests.setUpc              	   C   ó>   t dtdddidddd„ ƒ}d t|ƒ¡}|  t|ƒ|¡ dS )	r   r€   r�   r‚   re   Nc                   S   rs   rƒ   r   r   r   r   r   r   `  ru   z2DelayedCallNoDebugTests.test_str.<locals>.<lambda>zA<DelayedCall 0x{:x} [10.5s] called=0 cancelled=0 nothing(3, A=5)>)r   rt   Úformatr„   rI   ro   )r   r†   Úexpectedr   r   r   r‡   Z  s
   
þz DelayedCallNoDebugTests.test_strN©r4   r5   r6   r7   r~   r‡   r   r   r   r   r›   N  ó    r›   c                   @   rš   )ÚDelayedCallDebugTestsrw   c                 C   rœ   )z 
        Turn debug on.
        r�   TNrž   r   r   r   r   r~   m  rŸ   zDelayedCallDebugTests.setUpc              	   C   r    )	r   r€   r�   r‚   re   Nc                   S   rs   rƒ   r   r   r   r   r   r   z  ru   z0DelayedCallDebugTests.test_str.<locals>.<lambda>z\<DelayedCall 0x{:x} \[10.5s\] called=0 cancelled=0 nothing\(3, A=5\)

traceback at creation:)r   rt   r¡   r„   ÚassertRegexro   )r   r†   ÚexpectedRegexpr   r   r   r‡   t  s
   
ýzDelayedCallDebugTests.test_strNr£   r   r   r   r   r¥   h  r¤   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   rs   )z*
        Required method, unused.
        Nr   r   r   r   r   rb   Š  ru   z*TestSpySignalCapturingReactor.installWakerN)r4   r5   r6   r7   rb   r   r   r   r   r¨   ƒ  s    r¨   zsignal module not availablec                   @   r8   )Ú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   rM   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)r4   r5   r6   r7   r­   r³   r·   r»   r   r   r   r   r©   �  s    		r©   )+r7   rF   Úqueuer   Útypingr   r   Úunittestr   Úzope.interfacer   Ú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   r9   rt   rv   r›   r¥   r¨   r©   r   r   r   r   Ú<module>   s>   ÿ+ r
