o
    Òó·iÝ$  ã                   @   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                   @   ó   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   úgC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\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                   @   ó.   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   ór   |   ¡ }t ¡ }| ¡ }| ¡  |  tj|j|tjtƒ ¡}t	 
¡ r-|  |jd tj¡ dS |  |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   ó   û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                    sH   ˆ  tjˆ j¡}t ¡ rˆ |jd tj	¡ d S ˆ |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                      ó   ˆ   ‡fdd„¡S )Nc                    ó   ˆ   ¡ S ©N©Ústop©r;   ©r   r   r   Ú<lambda>–   ó    zYAdoptStreamPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>.<locals>.<lambda>©ÚaddCallbackr   ©Údr   r   r   rF   –   ó    zGAdoptStreamPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>N)r   r   r2   r   r0   r1   Úsetblockingr!   r   Úfamilyr   ÚstopListeningrI   Ú
addErrbackr	   r   Úrun©r   r4   r>   r   ©rK   r=   r   r   r   Útest_stopOnlyClosesq   s"   


ÿ

ÿz5AdoptStreamPortErrorsTestsBuilder.test_stopOnlyClosesN©	r   r   r   r   r   r   r+   r6   rT   r   r   r   r   r   6   s    	r   c                   @   r   )Ú'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   r2   r   r   r   ZadoptStreamConnectionr   r   )r   r   Ú
connectionr5   r   r   r   r6   ©   s   ûzAAdoptStreamConnectionErrorsTestsBuilder.test_invalidAddressFamilyN)r   r   r   r   r   r   r6   r   r   r   r   rV   œ   s    
rV   c                   @   r   )	Ú#AdoptDatagramPortErrorsTestsBuilderzR
    Builder for testing L{IReactorSocket.adoptDatagramPort} implementations.
    c                 C   r   )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+   Æ   r,   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_DGRAMr2   r   r   r   rY   r   r   r3   r   r   r   r6   Þ   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                    sJ   ˆ  tjˆ jd¡}t ¡ rˆ |jd tj	¡ d S ˆ |jd tj
¡ d S )Nr.   r   )r   r   r    Úrecvfromr
   r#   r$   r%   r&   r8   r9   r:   r<   r   r   r>   	  s   zHAdoptDatagramPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.stoppedz Failed to read on original port.c                      r?   )Nc                    r@   rA   rB   rD   rE   r   r   rF     rG   z[AdoptDatagramPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>.<locals>.<lambda>rH   r   rJ   r   r   rF     rL   zIAdoptDatagramPortErrorsTestsBuilder.test_stopOnlyCloses.<locals>.<lambda>N)r   r   r"   rZ   r2   r   r0   rM   rY   r   rN   r   rO   rI   rP   r	   r   rQ   rR   r   rS   r   rT   ó   s    

ÿ
	ÿz7AdoptDatagramPortErrorsTestsBuilder.test_stopOnlyClosesNrU   r   r   r   r   rX   ¿   s    rX   )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   rV   rX   ÚglobalsÚupdateZmakeTestCaseClassesr   r   r   r   Ú<module>   s$   	f#]