o
    ¯b¹4  ã                   @   sÆ   d Z ddlZddl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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„ ZG dd„ deƒZeeƒG dd„ dƒƒZeƒ  e ¡ ¡ dS )z0
Tests for implementations of L{IReactorFDSet}.
é    N)ÚskipIf)Úimplementer)ÚFileDescriptor)ÚIReactorFDSetÚIReadDescriptor)ÚEINPROGRESSÚEWOULDBLOCK)ÚReactorBuilder)Úplatform)ÚSkipTestc               
   C   sÊ   t   ¡ } |  d¡ |  d¡ zQt   ¡ }z7| d¡ z| d|  ¡ d f¡ W n tyB } z|jd tt	fvr8‚ W Y d }~nd }~ww |  
¡ \}}W n tyV   | ¡  ‚ w W |  ¡  ||fS |  ¡  w )N)ú	127.0.0.1r   é   Fr   r   )ÚsocketÚbindÚlistenÚsetblockingÚconnectÚgetsocknameÚOSErrorÚargsr   r   ÚacceptÚBaseExceptionÚclose)ÚserverSocketÚclientÚeÚserverÚaddr© r   úB/usr/lib/python3/dist-packages/twisted/internet/test/test_fdset.pyÚ
socketpair   s0   


ÿ€ÿþÿ
þr    c                   @   s”   e Zd ZdZeg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d„ Zdd„ Zee ¡ dƒdd„ ƒZdd„ ZdS ) ÚReactorFDSetTestsBuilderz>
    Builder defining tests relating to L{IReactorFDSet}.
    c                 C   s*   t ƒ \}}|  |j¡ |  |j¡ ||fS )zL
        Return the two sockets which make up a new TCP connection.
        )r    Ú
addCleanupr   )Úselfr   r   r   r   r   Ú_connectedPair8   s   
z'ReactorFDSetTestsBuilder._connectedPairc                 C   s.   |   ¡ }|  ¡ \}}t|ƒ}|j|_|||fS ©N)ÚbuildReactorr$   r   Úfileno)r#   Úreactorr   r   Úfdr   r   r   Ú_simpleSetupA   s
   
z%ReactorFDSetTestsBuilder._simpleSetupc                    sD   |   ¡ \‰‰ }‡ ‡fdd„}|ˆ _ˆ ˆ ¡ | d¡ |  ˆ¡ dS )z©
        C{reactor.addReader()} accepts an L{IReadDescriptor} provider and calls
        its C{doRead} method when there may be data available on its C{fileno}.
        c                      ó   ˆ  ˆ ¡ ˆ ¡  d S r%   )ÚremoveReaderÚstopr   ©r)   r(   r   r   ÚremoveAndStopR   ó   
z>ReactorFDSetTestsBuilder.test_addReader.<locals>.removeAndStopó   xN)r*   ÚdoReadÚ	addReaderÚsendallÚ
runReactor©r#   r   r/   r   r.   r   Útest_addReaderK   s   

z'ReactorFDSetTestsBuilder.test_addReaderc                    s`   ˆ   ¡ \}}}‡ fdd„}||_| |¡ | |¡ | d¡ | d|jd|j¡ ˆ  |¡ dS )zÈ
        L{reactor.removeReader()} accepts an L{IReadDescriptor} provider
        previously passed to C{reactor.addReader()} and causes it to no longer
        be monitored for input events.
        c                      ó   ˆ   d¡ d S ©NzdoRead should not be called©Úfailr   ©r#   r   r   r;   f   ó   z8ReactorFDSetTestsBuilder.test_removeReader.<locals>.failr1   r   N)r*   r2   r3   r,   r4   Ú	callLaterr-   r5   ©r#   r(   r)   r   r;   r   r<   r   Útest_removeReader^   s   


z*ReactorFDSetTestsBuilder.test_removeReaderc                    s:   |   ¡ \‰‰ }‡ ‡fdd„}|ˆ _ˆ ˆ ¡ |  ˆ¡ dS )z³
        C{reactor.addWriter()} accepts an L{IWriteDescriptor} provider and
        calls its C{doWrite} method when it may be possible to write to its
        C{fileno}.
        c                      r+   r%   )ÚremoveWriterr-   r   r.   r   r   r/   ~   r0   z>ReactorFDSetTestsBuilder.test_addWriter.<locals>.removeAndStopN)r*   ÚdoWriteÚ	addWriterr5   r6   r   r.   r   Útest_addWriterv   s
   
z'ReactorFDSetTestsBuilder.test_addWriterc                 C   sª   |   ¡ }t|d| d ƒ}t|d| ƒ}t|d| ƒ}|  ¡ \}}|  ||ƒ ¡ |  ||ƒ ¡ ||ƒ |  ||ƒ ¡ |  ||ƒ ¡ ||ƒ |  ||ƒ ¡ |  ||ƒ ¡ dS )z=
        Helper for getReaders and getWriters tests.
        ÚgetÚsÚaddÚremoveN)r&   Úgetattrr$   ÚassertNotInÚassertIn)r#   Úkindr(   rE   rG   rH   r   r   r   r   r   Ú
_getFDTestˆ   s   z#ReactorFDSetTestsBuilder._getFDTestc                 C   ó   |   d¡ dS )z¡
        L{IReactorFDSet.getReaders} reflects the additions and removals made
        with L{IReactorFDSet.addReader} and L{IReactorFDSet.removeReader}.
        ÚReaderN©rM   r<   r   r   r   Útest_getReadersž   ó   z(ReactorFDSetTestsBuilder.test_getReadersc                    sV   ˆ   ¡ \}}}‡ fdd„}||_| |¡ | |¡ | d|jd|j¡ ˆ  |¡ dS )zÊ
        L{reactor.removeWriter()} accepts an L{IWriteDescriptor} provider
        previously passed to C{reactor.addWriter()} and causes it to no longer
        be monitored for outputability.
        c                      r8   ©NzdoWrite should not be calledr:   r   r<   r   r   r;   ­   r=   z8ReactorFDSetTestsBuilder.test_removeWriter.<locals>.failr   N)r*   rB   rC   rA   r>   r-   r5   r?   r   r<   r   Útest_removeWriter¥   s   

z*ReactorFDSetTestsBuilder.test_removeWriterc                 C   rN   )z¡
        L{IReactorFDSet.getWriters} reflects the additions and removals made
        with L{IReactorFDSet.addWriter} and L{IReactorFDSet.removeWriter}.
        ÚWriterNrP   r<   r   r   r   Útest_getWriters¼   rR   z(ReactorFDSetTestsBuilder.test_getWritersc                    sˆ   ˆ   ¡ }ˆ  ¡ \}}}‡ fdd„|_‡ fdd„|_| d¡ | |¡ | |¡ | ¡ }| d|jd|j	¡ ˆ  
|¡ ˆ  ||g¡ dS )z¯
        C{reactor.removeAll()} removes all registered L{IReadDescriptor}
        providers and all registered L{IWriteDescriptor} providers and returns
        them.
        c                      ó
   ˆ   d¡S r9   r:   r   r<   r   r   Ú<lambda>Í   ó   
 z9ReactorFDSetTestsBuilder.test_removeAll.<locals>.<lambda>c                      rW   rS   r:   r   r<   r   r   rX   Î   rY   r1   r   N)r&   r*   r2   rB   r4   r3   rC   Ú	removeAllr>   r-   r5   ÚassertEqual)r#   r(   r)   r   Úremovedr   r<   r   Útest_removeAllÃ   s   



z'ReactorFDSetTestsBuilder.test_removeAllc                 C   s8   |   ¡ }t|ƒ}| |j¡ |  |¡ |  |jg ¡ dS )z†
        A descriptor's C{fileno} method should not be called after the
        descriptor has been removed from the reactor.
        N)r&   ÚRemovingDescriptorÚcallWhenRunningÚstartr5   r[   Úcalls)r#   r(   Ú
descriptorr   r   r   Útest_removedFromReactorà   s
   
z0ReactorFDSetTestsBuilder.test_removedFromReactorc                    sd   |   ¡ ‰|  ¡ \‰ ‰G ‡ ‡‡fdd„dtƒ}|ˆƒ}ˆ |¡ ˆ  d¡ |  ˆ¡ |  |jd¡ dS )zp
        If L{FileDescriptor.fileno} returns C{-1}, the descriptor is removed
        from the reactor.
        c                       s:   e Zd Z” ¡ ZdZdd„ Z‡ ‡fdd„Z‡fdd„ZdS )	zWReactorFDSetTestsBuilder.test_negativeOneFileDescriptor.<locals>.DisappearingDescriptoró    c                 S   s   | j S r%   )Ú_filenor<   r   r   r   r'   ù   s   z^ReactorFDSetTestsBuilder.test_negativeOneFileDescriptor.<locals>.DisappearingDescriptor.filenoc                    s(   d| _ |  jˆ d¡7  _ˆ  d¡ d S )Néÿÿÿÿr   ó   y)re   Ú	_receivedÚrecvÚsendr<   )r   r   r   r   r2   ü   s   z^ReactorFDSetTestsBuilder.test_negativeOneFileDescriptor.<locals>.DisappearingDescriptor.doReadc                    s   ˆ   ¡  d S r%   ©r-   ©r#   Úreason©r(   r   r   ÚconnectionLost  s   zfReactorFDSetTestsBuilder.test_negativeOneFileDescriptor.<locals>.DisappearingDescriptor.connectionLostN)Ú__name__Ú
__module__Ú__qualname__r'   re   rh   r2   ro   r   ©r   r(   r   r   r   ÚDisappearingDescriptorô   s    rt   r1   N)r&   r$   r   r3   rj   r5   r[   rh   )r#   rt   rb   r   rs   r   Útest_negativeOneFileDescriptorë   s   


z7ReactorFDSetTestsBuilder.test_negativeOneFileDescriptorz*Cannot duplicate socket filenos on Windowsc                    sˆ   ˆ  ¡ ‰ ˆ jj}|dv rt|›dƒ‚ˆ ¡ \}‰G ‡ ‡fdd„dtƒ}ˆ  |ƒ ¡ ‡‡fdd„}ˆ  d|¡ ˆ ˆ ¡ ˆ 	t
j¡ dS )	a  
        The file descriptor underlying a FileDescriptor may be closed and
        replaced by another at some point.  Bytes which arrive on the new
        descriptor must not be delivered to the FileDescriptor which was
        originally registered with the original descriptor of the same number.

        Practically speaking, this is difficult or impossible to detect.  The
        implementation relies on C{fileno} raising an exception if the original
        descriptor has gone away.  If C{fileno} continues to return the original
        file descriptor value, the reactor may deliver events from that
        descriptor.  This is a best effort attempt to ease certain debugging
        situations.  Applications should not rely on it intentionally.
        )ÚEPollReactorÚKQueueReactorÚ	CFReactorÚAsyncioSelectorReactorz$ cannot detect lost file descriptorsc                       s0   e Zd ZdZ‡fdd„Zdd„ Z‡ fdd„ZdS )	z@ReactorFDSetTestsBuilder.test_lostFileDescriptor.<locals>.VictimaŠ  
            This L{FileDescriptor} will have its socket closed out from under it
            and another socket will take its place.  It will raise a
            socket.error from C{fileno} after this happens (because socket
            objects remember whether they have been closed), so as long as the
            reactor calls the C{fileno} method the problem will be detected.
            c                    s   ˆ   ¡ S r%   )r'   r<   )r   r   r   r'   3  ó   zGReactorFDSetTestsBuilder.test_lostFileDescriptor.<locals>.Victim.filenoc                 S   s   t dƒ‚)Nz$Victim.doRead should never be called)Ú	Exceptionr<   r   r   r   r2   6  rz   zGReactorFDSetTestsBuilder.test_lostFileDescriptor.<locals>.Victim.doReadc                    s   ˆ   ¡  dS )zÊ
                When the problem is detected, the reactor should disconnect this
                file descriptor.  When that happens, stop the reactor so the
                test ends.
                Nrk   rl   rn   r   r   ro   9  s   zOReactorFDSetTestsBuilder.test_lostFileDescriptor.<locals>.Victim.connectionLostN)rp   rq   rr   Ú__doc__r'   r2   ro   r   )r(   r   r   r   ÚVictim*  s
    r}   c                     s:   ˆ   ¡ \} }ˆ ¡ }ˆ ¡  t | ¡ |¡ |  d¡ d S )Nr1   )r$   r'   r   ÚosÚdup2rj   )ÚnewCÚnewSr'   )r#   r   r   r   ÚmessItUpH  s
   zBReactorFDSetTestsBuilder.test_lostFileDescriptor.<locals>.messItUpr   N)r&   Ú	__class__rp   r   r$   r   r3   r>   r5   ÚflushLoggedErrorsr   Úerror)r#   Únamer   r}   r‚   r   )r(   r#   r   r   Útest_lostFileDescriptor
  s   

z0ReactorFDSetTestsBuilder.test_lostFileDescriptorc                 C   sŠ   |   ¡ }G dd„ dtƒ}|  ¡ \}}||ƒ}|j|_||ƒ}|j|_| |¡ | |¡ | |j¡ |  |¡ |  	|j
¡ |  	|j
¡ dS )z‡
        Any file descriptors added to the reactor have their C{connectionLost}
        called when C{reactor.stop} is called.
        c                   @   s   e Zd Zdd„ Zdd„ ZdS )zSReactorFDSetTestsBuilder.test_connectionLostOnShutdown.<locals>.DoNothingDescriptorc                 S   ó   d S r%   r   r<   r   r   r   r2   _  ó   zZReactorFDSetTestsBuilder.test_connectionLostOnShutdown.<locals>.DoNothingDescriptor.doReadc                 S   rˆ   r%   r   r<   r   r   r   rB   b  r‰   z[ReactorFDSetTestsBuilder.test_connectionLostOnShutdown.<locals>.DoNothingDescriptor.doWriteN)rp   rq   rr   r2   rB   r   r   r   r   ÚDoNothingDescriptor^  s    rŠ   N)r&   r   r$   r'   r3   rC   r_   r-   r5   Ú
assertTrueÚdisconnected)r#   r(   rŠ   r   r   Úfd1Úfd2r   r   r   Útest_connectionLostOnShutdownW  s   


z6ReactorFDSetTestsBuilder.test_connectionLostOnShutdownN)rp   rq   rr   r|   r   ÚrequiredInterfacesr$   r*   r7   r@   rD   rM   rQ   rT   rV   r]   rc   ru   r   r
   Ú	isWindowsr‡   r   r   r   r   r   r!   1   s$    	

Lr!   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S )r^   a˜  
    A read descriptor which removes itself from the reactor as soon as it
    gets a chance to do a read and keeps track of when its own C{fileno}
    method is called.

    @ivar insideReactor: A flag which is true as long as the reactor has
        this descriptor as a reader.

    @ivar calls: A list of the bottom of the call stack for any call to
        C{fileno} when C{insideReactor} is false.
    c                 C   s$   || _ d| _g | _tƒ \| _| _d S ©NF)r(   ÚinsideReactorra   r    ÚreadÚwrite)r#   r(   r   r   r   Ú__init__‚  s   zRemovingDescriptor.__init__c                 C   s"   d| _ | j | ¡ | j d¡ d S )NTó   a)r“   r(   r3   r•   rj   r<   r   r   r   r`   ˆ  s   zRemovingDescriptor.startc                 C   s   dS )NÚfoor   r<   r   r   r   Ú	logPrefix  r‰   zRemovingDescriptor.logPrefixc                 C   s4   | j  | ¡ d| _| j  ¡  | j ¡  | j ¡  d S r’   )r(   r,   r“   r-   r”   r   r•   r<   r   r   r   r2     s
   

zRemovingDescriptor.doReadc                 C   s,   | j s| j tjddd d… ¡ | j ¡ S )Né   )Úlimitrf   )r“   ra   ÚappendÚ	tracebackÚextract_stackr”   r'   r<   r   r   r   r'   —  s   
zRemovingDescriptor.filenoc                 C   rˆ   r%   r   rl   r   r   r   ro   œ  s   z!RemovingDescriptor.connectionLostN)
rp   rq   rr   r|   r–   r`   r™   r2   r'   ro   r   r   r   r   r^   t  s    r^   )r|   r~   r   r   Úunittestr   Úzope.interfacer   Útwisted.internet.abstractr   Útwisted.internet.interfacesr   r   Útwisted.internet.tcpr   r   Ú#twisted.internet.test.reactormixinsr	   Útwisted.python.runtimer
   Útwisted.trial.unittestr   r    r!   r^   ÚglobalsÚupdateÚmakeTestCaseClassesr   r   r   r   Ú<module>   s&     E.