U
    Â“«jÝ$  ã                   @   sô   d 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 G d
d„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZeƒ  e ¡ ¡ eƒ  e ¡ ¡ eƒ  e ¡ ¡ eƒ  e ¡ ¡ dS )aD  
Tests for implementations of L{IReactorSocket}.

Generally only tests for failure cases are found here.  Success cases for
this interface are tested elsewhere.  For example, the success case for
I{AF_INET} is in L{twisted.internet.test.test_tcp}, since that case should
behave exactly the same as L{IReactorTCP.listenTCP}.
é    N)Úverify)ÚUnsupportedAddressFamily)ÚIReactorSocket)ÚDatagramProtocolÚServerFactory)ÚReactorBuilderÚneedsRunningReactor)Úerr)Úplatformc                   @   s   e Zd ZdZegZdd„ ZdS )Ú&IReactorSocketVerificationTestsBuilderaO  
    Builder for testing L{IReactorSocket} implementations for required
    methods and method signatures.

    L{ReactorBuilder} already runs L{IReactorSocket.providedBy} to
    ensure that these tests will only be run on reactor classes that
    claim to implement L{IReactorSocket}.

    These tests ensure that reactors which claim to provide the
    L{IReactorSocket} interface actually have all the required methods
    and that those methods have the expected number of arguments.

    These tests will be skipped for reactors which do not claim to
    provide L{IReactorSocket}.
    c                 C   s   |   ¡ }|  t t|¡¡ dS )zf
        The reactor instance returned by C{buildReactor} provides
        L{IReactorSocket}.
        N)ÚbuildReactorÚ
assertTruer   ZverifyObjectr   )ÚselfÚreactor© r   úE/tmp/pip-unpacked-wheel-59kc99xd/twisted/internet/test/test_socket.pyÚtest_provider-   s    z4IReactorSocketVerificationTestsBuilder.test_providerN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚrequiredInterfacesr   r   r   r   r   r      s   r   c                   @   s.   e Zd ZdZegZdd„ Zdd„ Zdd„ ZdS )	Ú!AdoptStreamPortErrorsTestsBuilderas  
    Builder for testing L{IReactorSocket.adoptStreamPort} implementations.

    Generally only tests for failure cases are found here.  Success cases for
    this interface are tested elsewhere.  For example, the success case for
    I{AF_INET} is in L{twisted.internet.test.test_tcp}, since that case should
    behave exactly the same as L{IReactorTCP.listenTCP}.
    c                 C   sp   |   ¡ }t ¡ }| ¡ }| ¡  |  tj|j|tjtƒ ¡}t	 
¡ rX|  |jd tj¡ n|  |jd tj¡ dS )z«
        An implementation of L{IReactorSocket.adoptStreamPort} raises
        L{socket.error} if passed an integer which is not associated with a
        socket.
        r   N)r   ÚsocketÚfilenoÚcloseÚassertRaisesÚerrorÚadoptStreamPortÚAF_INETr   r
   Ú	isWindowsÚassertEqualÚargsÚerrnoÚWSAENOTSOCKÚEBADF©r   r   Zprober   Úexcr   r   r   Útest_invalidDescriptorB   s    ûz8AdoptStreamPortErrorsTestsBuilder.test_invalidDescriptorc                 C   sR   |   ¡ }t ¡ }| d¡ | d¡ |  |j¡ d}|  t|j| 	¡ |t
ƒ ¡ dS )z¬
        An implementation of L{IReactorSocket.adoptStreamPort} raises
        L{UnsupportedAddressFamily} if passed an address family it does not
        support.
        ©z	127.0.0.1r   é   é  N)r   r   ÚbindÚlistenÚ
addCleanupr   r   r   r   r   r   ©r   r   ÚportÚ	arbitraryr   r   r   Útest_invalidAddressFamilyZ   s    

ûz;AdoptStreamPortErrorsTestsBuilder.test_invalidAddressFamilyc                    sœ   ˆ  ¡ ‰t ¡ ‰ˆ ˆj¡ ˆ d¡ ˆ d¡ ˆ d¡ ˆ ˆ ¡ ˆj	t
ƒ ¡}| ¡ ‰ ‡‡fdd„}ˆ  |¡ ˆ  td¡ tˆ‡ ‡fdd„ƒ ˆ ¡  d	S )
a9  
        When the L{IListeningPort} returned by
        L{IReactorSocket.adoptStreamPort} is stopped using
        C{stopListening}, the underlying socket is closed but not
        shutdown.  This allows another process which still has a
        reference to it to continue accepting connections over it.
        r)   r*   Fc                    sF   ˆ  tjˆ j¡}t ¡ r.ˆ |jd tj	¡ nˆ |jd tj
¡ d S )Nr   )r   r   r   Úacceptr
   r    r!   r"   r#   ÚWSAEWOULDBLOCKÚEAGAIN©Úignoredr'   ©Ú
portSocketr   r   r   Ústoppedˆ   s    zFAdoptStreamPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.stoppedz"Failed to accept on original port.c                      s   ˆ   ‡fdd„¡S )Nc                    s   ˆ   ¡ S ©N©Ústop©r7   ©r   r   r   Ú<lambda>–   ó    zYAdoptStreamPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>.<locals>.<lambda>©ÚaddCallbackr   ©Údr   r   r   r@   –   rA   zGAdoptStreamPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>N)r   r   r.   r   r,   r-   Úsetblockingr   r   Úfamilyr   ÚstopListeningrC   Ú
addErrbackr	   r   Úrun©r   r0   r:   r   ©rE   r9   r   r   r   Útest_stopOnlyClosesq   s(    


  ÿ

 ÿz5AdoptStreamPortErrorsTestsBuilder.test_stopOnlyClosesN©	r   r   r   r   r   r   r(   r2   rM   r   r   r   r   r   6   s
   	r   c                   @   s   e Zd ZdZegZdd„ ZdS )Ú'AdoptStreamConnectionErrorsTestsBuildera}  
    Builder for testing L{IReactorSocket.adoptStreamConnection}
    implementations.

    Generally only tests for failure cases are found here.  Success cases for
    this interface are tested elsewhere.  For example, the success case for
    I{AF_INET} is in L{twisted.internet.test.test_tcp}, since that case should
    behave exactly the same as L{IReactorTCP.listenTCP}.
    c                 C   s>   |   ¡ }t ¡ }|  |j¡ d}|  t|j| ¡ |tƒ ¡ dS )z²
        An implementation of L{IReactorSocket.adoptStreamConnection} raises
        L{UnsupportedAddressFamily} if passed an address family it does not
        support.
        r+   N)	r   r   r.   r   r   r   ZadoptStreamConnectionr   r   )r   r   Ú
connectionr1   r   r   r   r2   ©   s    ûzAAdoptStreamConnectionErrorsTestsBuilder.test_invalidAddressFamilyN)r   r   r   r   r   r   r2   r   r   r   r   rO   œ   s   
rO   c                   @   s.   e Zd ZdZegZdd„ Zdd„ Zdd„ ZdS )	Ú#AdoptDatagramPortErrorsTestsBuilderzR
    Builder for testing L{IReactorSocket.adoptDatagramPort} implementations.
    c                 C   sp   |   ¡ }t ¡ }| ¡ }| ¡  |  tj|j|tjtƒ ¡}t	 
¡ rX|  |jd tj¡ n|  |jd tj¡ dS )z­
        An implementation of L{IReactorSocket.adoptDatagramPort} raises
        L{socket.error} if passed an integer which is not associated with a
        socket.
        r   N)r   r   r   r   r   r   ÚadoptDatagramPortr   r   r
   r    r!   r"   r#   r$   r%   r&   r   r   r   r(   Æ   s    ûz:AdoptDatagramPortErrorsTestsBuilder.test_invalidDescriptorc                 C   sF   |   ¡ }t tjtj¡}|  |j¡ d}|  t|j| 	¡ |t
ƒ ¡ dS )z®
        An implementation of L{IReactorSocket.adoptDatagramPort} raises
        L{UnsupportedAddressFamily} if passed an address family it does not
        support.
        r+   N)r   r   r   Ú
SOCK_DGRAMr.   r   r   r   rR   r   r   r/   r   r   r   r2   Þ   s    ûz=AdoptDatagramPortErrorsTestsBuilder.test_invalidAddressFamilyc                    sš   ˆ  ¡ ‰t tjtj¡‰ˆ ˆj¡ ˆ d¡ ˆ d¡ ˆ ˆ 	¡ ˆj
tƒ ¡}| ¡ ‰ ‡‡fdd„}ˆ  |¡ ˆ  td¡ tˆ‡ ‡fdd„ƒ ˆ ¡  dS )	a7  
        When the L{IListeningPort} returned by
        L{IReactorSocket.adoptDatagramPort} is stopped using
        C{stopListening}, the underlying socket is closed but not
        shutdown.  This allows another process which still has a
        reference to it to continue reading and writing to it.
        r)   Fc                    sH   ˆ  tjˆ jd¡}t ¡ r0ˆ |jd tj	¡ nˆ |jd tj
¡ d S )Nr*   r   )r   r   r   Úrecvfromr
   r    r!   r"   r#   r4   r5   r6   r8   r   r   r:   	  s    zHAdoptDatagramPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.stoppedz Failed to read on original port.c                      s   ˆ   ‡fdd„¡S )Nc                    s   ˆ   ¡ S r;   r<   r>   r?   r   r   r@     rA   z[AdoptDatagramPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>.<locals>.<lambda>rB   r   rD   r   r   r@     rA   zIAdoptDatagramPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>N)r   r   r   rS   r.   r   r,   rF   rR   r   rG   r   rH   rC   rI   r	   r   rJ   rK   r   rL   r   rM   ó   s&    

  ÿ	
 ÿz7AdoptDatagramPortErrorsTestsBuilder.test_stopOnlyClosesNrN   r   r   r   r   rQ   ¿   s
   rQ   )r   r#   r   Zzope.interfacer   Ztwisted.internet.errorr   Ztwisted.internet.interfacesr   Ztwisted.internet.protocolr   r   Z#twisted.internet.test.reactormixinsr   r   Ztwisted.python.logr	   Ztwisted.python.runtimer
   r   r   rO   rQ   ÚglobalsÚupdateZmakeTestCaseClassesr   r   r   r   Ú<module>   s"   	f#]