U
    �Acj�  ã                   @   s¨  d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	m
Z
mZ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jƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dej ƒZ!G dd„ dej"ƒZ"G dd„ dej ƒZ#G dd„ dej ƒZ$G dd„ dej ƒZ%G dd „ d ejej&ƒZ'G d!d"„ d"ej ƒZ(G d#d$„ d$ej ƒZ)G d%d&„ d&eƒZ*G d'd(„ d(ej+ƒZ,G d)d*„ d*ej ƒZ-dS )+z
Test code for policies.
é    N)ÚStringIO)Ú	InterfaceÚimplementedByÚimplementer)ÚaddressÚdeferÚprotocolÚreactorÚtask)ÚStringTransportÚ StringTransportWithDisconnection)Úpolicies)Úunittestc                   @   s8   e Zd Zd ZZdZdd„ Zdd„ Zdd„ Zd	d
„ Z	dS )ÚSimpleProtocolr   ó    c                 C   s   t  ¡ | _t  ¡ | _d S ©N)r   ÚDeferredÚ
dConnectedÚdDisconnected©Úself© r   ú>/tmp/pip-unpacked-wheel-nrvc_22y/twisted/test/test_policies.pyÚ__init__   s    
zSimpleProtocol.__init__c                 C   s   d| _ | j d¡ d S ©Né   Ú )Ú	connectedr   Úcallbackr   r   r   r   ÚconnectionMade   s    zSimpleProtocol.connectionMadec                 C   s   d| _ | j d¡ d S r   )Údisconnectedr   r   ©r   Úreasonr   r   r   ÚconnectionLost    s    zSimpleProtocol.connectionLostc                 C   s   |  j |7  _ d S r   )Úbuffer©r   Údatar   r   r   ÚdataReceived$   s    zSimpleProtocol.dataReceivedN)
Ú__name__Ú
__module__Ú__qualname__r   r    r$   r   r   r#   r'   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚSillyFactoryc                 C   s
   || _ d S r   ©Úp)r   r-   r   r   r   r   )   s    zSillyFactory.__init__c                 C   s   | j S r   r,   )r   Úaddrr   r   r   ÚbuildProtocol,   s    zSillyFactory.buildProtocolN)r(   r)   r*   r   r/   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 )ÚEchoProtocolFc                 C   s
   d| _ d S ©NT©Úpausedr   r   r   r   ÚpauseProducing3   s    zEchoProtocol.pauseProducingc                 C   s
   d| _ d S )NFr2   r   r   r   r   ÚresumeProducing6   s    zEchoProtocol.resumeProducingc                 C   s   d S r   r   r   r   r   r   ÚstopProducing9   s    zEchoProtocol.stopProducingc                 C   s   | j  |¡ d S r   )Ú	transportÚwriter%   r   r   r   r'   <   s    zEchoProtocol.dataReceivedN)r(   r)   r*   r3   r4   r5   r6   r'   r   r   r   r   r0   0   s
   r0   c                   @   s   e Zd ZdZeZdS )ÚServerz8
    A simple server factory using L{EchoProtocol}.
    N)r(   r)   r*   Ú__doc__r0   r   r   r   r   r   r9   @   s   r9   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚTestableThrottlingFactoryzH
    L{policies.ThrottlingFactory} using a L{task.Clock} for tests.
    c                 O   s   t jj| f|ž|Ž || _dS ©z˜
        @param clock: object providing a callLater method that can be used
            for tests.
        @type clock: C{task.Clock} or alike.
        N)r   ÚThrottlingFactoryr   Úclock©r   r>   ÚargsÚkwargsr   r   r   r   M   s    z"TestableThrottlingFactory.__init__c                 C   s   | j  ||¡S ©z0
        Forward to the testable clock.
        ©r>   Ú	callLater©r   ZperiodÚfuncr   r   r   rD   V   s    z#TestableThrottlingFactory.callLaterN©r(   r)   r*   r:   r   rD   r   r   r   r   r;   H   s   	r;   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚTestableTimeoutFactoryzE
    L{policies.TimeoutFactory} using a L{task.Clock} for tests.
    c                 O   s   t jj| f|ž|Ž || _dS r<   )r   ÚTimeoutFactoryr   r>   r?   r   r   r   r   b   s    zTestableTimeoutFactory.__init__c                 C   s   | j  ||¡S rB   rC   rE   r   r   r   rD   k   s    z TestableTimeoutFactory.callLaterNrG   r   r   r   r   rH   ]   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d„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"S )#ÚWrapperTestsz>
    Tests for L{WrappingFactory} and L{ProtocolWrapper}.
    c                 C   s8   t ƒ }t |¡}| t ddd¡¡}|  |jj|¡ dS )zf
        Make sure protocol.factory is the wrapped factory, not the wrapping
        factory.
        ÚTCPú	127.0.0.1é#   N)	r9   r   ÚWrappingFactoryr/   r   ÚIPv4AddressÚassertIsÚwrappedProtocolÚfactory)r   ÚfZwfr-   r   r   r   Útest_protocolFactoryAttributew   s    
z*WrapperTests.test_protocolFactoryAttributec                 C   sl   G dd„ dt ƒ}t|ƒG dd„ dƒƒ}ttjƒ t ¡ }t t d¡|¡}| |ƒ ¡ |  	| 
|j¡¡ dS )z²
        The transport wrapper passed to the wrapped protocol's
        C{makeConnection} provides the same interfaces as are provided by the
        original transport.
        c                   @   s   e Zd ZdS )z=WrapperTests.test_transportInterfaces.<locals>.IStubTransportN©r(   r)   r*   r   r   r   r   ÚIStubTransportˆ   s   rV   c                   @   s   e Zd ZdS )z<WrapperTests.test_transportInterfaces.<locals>.StubTransportNrU   r   r   r   r   ÚStubTransport‹   s   rW   N)r   r   r   r   ÚProtocolWrapperr   ÚProtocolrN   ÚmakeConnectionÚ
assertTrueZ
providedByr7   )r   rV   rW   ÚprotoÚwrapperr   r   r   Útest_transportInterfaces�   s    

z%WrapperTests.test_transportInterfacesc                 C   s$   t ƒ }t |¡}|  d| ¡ ¡ dS )zƒ
        L{WrappingFactory.logPrefix} is customized to mention both the original
        factory and the wrapping factory.
        zServer (WrappingFactory)N)r9   r   rN   ÚassertEqualÚ	logPrefix)r   ÚserverrR   r   r   r   Útest_factoryLogPrefixž   s    
z"WrapperTests.test_factoryLogPrefixc                 C   s2   G dd„ dƒ}|ƒ }t  |¡}|  d| ¡ ¡ dS )z—
        If the wrapped factory doesn't have a L{logPrefix} method,
        L{WrappingFactory.logPrefix} falls back to the factory class name.
        c                   @   s   e Zd ZdS )z=WrapperTests.test_factoryLogPrefixFallback.<locals>.NoFactoryNrU   r   r   r   r   Ú	NoFactory­   s   rc   zNoFactory (WrappingFactory)N)r   rN   r_   r`   )r   rc   ra   rR   r   r   r   Útest_factoryLogPrefixFallback§   s    
z*WrapperTests.test_factoryLogPrefixFallbackc                 C   s8   t ƒ }t |¡}| t ddd¡¡}|  d| ¡ ¡ dS )z{
        L{ProtocolWrapper.logPrefix} is customized to mention both the original
        protocol and the wrapper.
        rK   rL   rM   zEchoProtocol (ProtocolWrapper)N)r9   r   rN   r/   r   rO   r_   r`   )r   ra   rR   r   r   r   r   Útest_protocolLogPrefix´   s    
z#WrapperTests.test_protocolLogPrefixc                 C   sL   G dd„ dƒ}t ƒ }||_t |¡}| t ddd¡¡}|  d| ¡ ¡ dS )z™
        If the wrapped protocol doesn't have a L{logPrefix} method,
        L{ProtocolWrapper.logPrefix} falls back to the protocol class name.
        c                   @   s   e Zd ZdS )z?WrapperTests.test_protocolLogPrefixFallback.<locals>.NoProtocolNrU   r   r   r   r   Ú
NoProtocolÄ   s   rf   rK   rL   rM   zNoProtocol (ProtocolWrapper)N)	r9   r   r   rN   r/   r   rO   r_   r`   )r   rf   ra   rR   r   r   r   r   Útest_protocolLogPrefixFallback¾   s    
z+WrapperTests.test_protocolLogPrefixFallbackc                 C   s,   t  t  tƒ ¡t ¡ ¡}tƒ }| |¡ |S )zm
        Return L{policies.ProtocolWrapper} that has been connected to a
        L{StringTransport}.
        )r   rX   rN   r9   r   rY   r   rZ   ©r   r]   r7   r   r   r   Ú_getWrapperÍ   s    
 ÿ
zWrapperTests._getWrapperc                 C   s"   |   ¡ }|  | ¡ |j ¡ ¡ dS )zk
        L{policies.ProtocolWrapper.getHost} calls C{getHost} on the underlying
        transport.
        N)ri   r_   ÚgetHostr7   ©r   r]   r   r   r   Útest_getHostÙ   s    zWrapperTests.test_getHostc                 C   s"   |   ¡ }|  | ¡ |j ¡ ¡ dS )zk
        L{policies.ProtocolWrapper.getPeer} calls C{getPeer} on the underlying
        transport.
        N)ri   r_   ZgetPeerr7   rk   r   r   r   Útest_getPeerá   s    zWrapperTests.test_getPeerc                 C   s<   |   ¡ }tƒ }| |d¡ |  |jj|¡ |  |jj¡ dS )z}
        L{policies.ProtocolWrapper.registerProducer} calls C{registerProducer}
        on the underlying transport.
        TN)ri   ÚobjectÚregisterProducerrP   r7   Úproducerr[   Ú	streaming©r   r]   rp   r   r   r   Útest_registerProduceré   s
    z"WrapperTests.test_registerProducerc                 C   sB   |   ¡ }tƒ }| |d¡ | ¡  |  |jj¡ |  |jj¡ dS )z�
        L{policies.ProtocolWrapper.unregisterProducer} calls
        C{unregisterProducer} on the underlying transport.
        TN)ri   rn   ro   ZunregisterProducerÚassertIsNoner7   rp   rq   rr   r   r   r   Útest_unregisterProducerô   s    z$WrapperTests.test_unregisterProducerc                    s6   |   ¡ }g ‰ ‡ fdd„|j_| ¡  |  ˆ dg¡ dS )zw
        L{policies.ProtocolWrapper.stopConsuming} calls C{stopConsuming} on
        the underlying transport.
        c                      s
   ˆ   d¡S r1   ©Úappendr   ©Úresultr   r   Ú<lambda>  r   z1WrapperTests.test_stopConsuming.<locals>.<lambda>TN)ri   r7   ZstopConsumingr_   rk   r   rx   r   Útest_stopConsuming   s
    zWrapperTests.test_stopConsumingc                    sD   g ‰ G ‡ fdd„dƒ}t  |ƒ ¡}tƒ }| |¡ |  ˆ |g¡ dS )z}
        L{policies.WrappingFactory.startedConnecting} calls
        C{startedConnecting} on the underlying factory.
        c                       s   e Zd Z‡ fdd„ZdS )z4WrapperTests.test_startedConnecting.<locals>.Factoryc                    s   ˆ   |¡ d S r   rv   )r   Ú	connectorrx   r   r   ÚstartedConnecting  s    zFWrapperTests.test_startedConnecting.<locals>.Factory.startedConnectingN)r(   r)   r*   r}   r   rx   r   r   ÚFactory  s   r~   N)r   rN   rn   r}   r_   )r   r~   r]   r|   r   rx   r   Útest_startedConnecting  s    
z#WrapperTests.test_startedConnectingc                    sP   g ‰ G ‡ fdd„dƒ}t  |ƒ ¡}tƒ }tƒ }| ||¡ |  ˆ ||fg¡ dS )zƒ
        L{policies.WrappingFactory.clientConnectionLost} calls
        C{clientConnectionLost} on the underlying factory.
        c                       s   e Zd Z‡ fdd„ZdS )z7WrapperTests.test_clientConnectionLost.<locals>.Factoryc                    s   ˆ   ||f¡ d S r   rv   ©r   r|   r"   rx   r   r   ÚclientConnectionLost#  s    zLWrapperTests.test_clientConnectionLost.<locals>.Factory.clientConnectionLostN)r(   r)   r*   r�   r   rx   r   r   r~   "  s   r~   N)r   rN   rn   r�   r_   ©r   r~   r]   r|   r"   r   rx   r   Útest_clientConnectionLost  s    z&WrapperTests.test_clientConnectionLostc                    sP   g ‰ G ‡ fdd„dƒ}t  |ƒ ¡}tƒ }tƒ }| ||¡ |  ˆ ||fg¡ dS )z‡
        L{policies.WrappingFactory.clientConnectionFailed} calls
        C{clientConnectionFailed} on the underlying factory.
        c                       s   e Zd Z‡ fdd„ZdS )z9WrapperTests.test_clientConnectionFailed.<locals>.Factoryc                    s   ˆ   ||f¡ d S r   rv   r€   rx   r   r   ÚclientConnectionFailed4  s    zPWrapperTests.test_clientConnectionFailed.<locals>.Factory.clientConnectionFailedN)r(   r)   r*   r„   r   rx   r   r   r~   3  s   r~   N)r   rN   rn   r„   r_   r‚   r   rx   r   Útest_clientConnectionFailed,  s    z(WrapperTests.test_clientConnectionFailedc                 C   sR   t  t  tƒ ¡t ¡ ¡}tƒ }||_| |¡ |  |j	¡ | 
¡  |  |j	¡ dS )z¸
        L{policies.ProtocolWrapper.connectionLost} sets C{wrappedProtocol} to
        C{None} in order to break reference cycle between wrapper and wrapped
        protocols.
        N)r   rX   rN   r9   r   rY   r   rZ   ÚassertIsNotNonerQ   ÚloseConnectionrt   rh   r   r   r   Útest_breakReferenceCycle=  s    
 ÿ
z%WrapperTests.test_breakReferenceCycleN)r(   r)   r*   r:   rT   r^   rb   rd   re   rg   ri   rl   rm   rs   ru   r{   r   rƒ   r…   rˆ   r   r   r   r   rJ   r   s"   
	
rJ   c                   @   s   e Zd Zdd„ Zdd„ ZdS )rN   c                 C   s   |S r   r   )r   rS   r-   r   r   r   r   P  s    zWrappingFactory.protocolc                 C   s   t j | ¡ | j d ¡ d S r   )r   rN   ÚstartFactoryÚdeferredr   r   r   r   r   r‰   S  s    zWrappingFactory.startFactoryN)r(   r)   r*   r   r‰   r   r   r   r   rN   O  s   rN   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚThrottlingTestsz2
    Tests for L{policies.ThrottlingFactory}.
    c           	         s  t ƒ }dd„ tdƒD ƒ\‰ ‰‰‰t |d¡‰tˆƒ}t ¡ |_tj	d|dd�‰ˆ 
¡ j‰‡ ‡‡‡fdd	„}‡ ‡‡‡‡fd
d„}‡ fdd„}‡‡fdd„}‡‡fdd„}‡‡‡fdd„}|j |¡ |j |¡ |j |¡ |j |¡ |j |¡ |j |¡ |jS )z«
        Full test using a custom server limiting number of connections.

        FIXME: https://twistedmatrix.com/trac/ticket/10012
        This is a flaky test.
        c                 s   s   | ]}t ƒ V  qd S r   )r   )Ú.0Úir   r   r   Ú	<genexpr>e  s     z-ThrottlingTests.test_limit.<locals>.<genexpr>é   é   r   rL   )Z	interfacec                    sD   t  dˆtˆ ƒ¡ ˆ j ‡‡fdd„¡ ˆj ‡‡fdd„¡ ˆjS )NrL   c                    s   t  dˆtˆ ƒ¡S ©NrL   ©r	   Ú
connectTCPr+   ©Úr)Úc2Únr   r   rz   q  r   zAThrottlingTests.test_limit.<locals>._connect123.<locals>.<lambda>c                    s   t  dˆtˆ ƒ¡S r‘   r’   r”   )Úc3r—   r   r   rz   t  r   )r	   r“   r+   r   ÚaddCallbackr   ©Úresults)Úc1r–   r˜   r—   r   r   Ú_connect123n  s    ÿÿz/ThrottlingTests.test_limit.<locals>._connect123c                    s^   ˆ  dd„ ˆ ˆˆfD ƒdddg¡ ˆ  dd„ ˆ ˆˆfD ƒdddg¡ ˆ  tˆj ¡ ƒd¡ | S )Nc                 S   s   g | ]
}|j ‘qS r   )r   ©rŒ   Úcr   r   r   Ú
<listcomp>y  s     zAThrottlingTests.test_limit.<locals>._check123.<locals>.<listcomp>r   c                 S   s   g | ]
}|j ‘qS r   )r    rž   r   r   r   r    z  s     r   r�   )r_   ÚlenZ	protocolsÚkeysrš   )rœ   r–   r˜   r   ÚtServerr   r   Ú	_check123x  s    ""z-ThrottlingTests.test_limit.<locals>._check123c                    s   ˆ j  ¡  ˆ jS r   )r7   r‡   r   rš   )rœ   r   r   Ú_lose1~  s    
z*ThrottlingTests.test_limit.<locals>._lose1c                    s   t  dˆtˆ ƒ¡ ˆ jS r‘   )r	   r“   r+   r   rš   )Úc4r—   r   r   Ú	_connect4ƒ  s    z-ThrottlingTests.test_limit.<locals>._connect4c                    s    ˆ  ˆ jd¡ ˆ  ˆ jd¡ | S )Nr   r   )r_   r   r    rš   )r¦   r   r   r   Ú_check4‡  s    z+ThrottlingTests.test_limit.<locals>._check4c                    s4   ˆ ˆfD ]}|j  ¡  qt t ˆj¡ˆ jˆjg¡S r   )r7   r‡   r   ZDeferredListZmaybeDeferredÚstopListeningr   )r›   rŸ   )r–   r¦   r-   r   r   Ú_cleanupŒ  s    
ýÿz,ThrottlingTests.test_limit.<locals>._cleanup)r9   Úranger   r=   rN   r   r   rŠ   r	   Z	listenTCPrj   Úportr™   )	r   ra   ZwrapTServerr�   r¤   r¥   r§   r¨   rª   r   )rœ   r–   r˜   r¦   r—   r-   r   r£   r   Ú
test_limit]  s(    


zThrottlingTests.test_limitc                 C   sp   t ƒ }tt ¡ |ƒ}| t ddd¡¡}tƒ }||_| 	|¡ | 
dgd ¡ |  | ¡ d¡ |  |jd¡ dS )	zZ
        L{ThrottlingProtocol.writeSequence} is called on the underlying factory.
        rK   rL   r   ó   bytesr�   s   bytesbytesbytesbytesé   N)r9   r;   r
   ÚClockr/   r   rO   r   r   rZ   ÚwriteSequencer_   ÚvalueÚwrittenThisSecond)r   ra   r£   r   r7   r   r   r   Útest_writeSequenceŸ  s    
z"ThrottlingTests.test_writeSequencec                 C   sÞ   t ƒ }tt ¡ |dd�}| t ddd¡¡}tƒ }||_| 	|¡ |j
|_| d¡ | d¡ |  | ¡ d¡ |  |jd	¡ |  |j
j¡ |j d
¡ |  |jd¡ |  |j
j¡ |j d
¡ |  |jd¡ |  |j
j¡ dS )ze
        Check the writeLimit parameter: write data, and check for the pause
        status.
        é
   )Z
writeLimitrK   rL   r   ó
   0123456789ó
   abcdefghijó   0123456789abcdefghijr¯   çÍÌÌÌÌÌð?N)r9   r;   r
   r°   r/   r   rO   r   r   rZ   rQ   rp   r'   r_   r²   r³   ÚassertFalser3   r>   Úadvancer[   ©r   ra   r£   r¬   Útrr   r   r   Útest_writeLimit®  s$    


zThrottlingTests.test_writeLimitc                 C   sR  t ƒ }tt ¡ |dd�}| t ddd¡¡}tƒ }||_| 	|¡ | 
d¡ | 
d¡ |  | ¡ d¡ |  |jd	¡ |j d
¡ |  |jd¡ |  |jd¡ |j d
¡ |  |jd¡ |  |jd¡ | ¡  | 
d¡ | 
d¡ |  | ¡ d¡ |  |jd	¡ |j d
¡ |  |jd¡ |  |jd¡ |j d
¡ |  |jd¡ |  |jd¡ dS )zb
        Check the readLimit parameter: read data and check for the pause
        status.
        rµ   )Z	readLimitrK   rL   r   r¶   r·   r¸   r¯   r¹   r3   Z	producingN)r9   r;   r
   r°   r/   r   rO   r   r   rZ   r'   r_   r²   ZreadThisSecondr>   r»   ZproducerStateÚclearr¼   r   r   r   Útest_readLimitÌ  s6    




zThrottlingTests.test_readLimitN)r(   r)   r*   r:   r­   r´   r¾   rÀ   r   r   r   r   r‹   X  s
   Br‹   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚTimeoutProtocolTestsz0
    Tests for L{policies.TimeoutProtocol}.
    c                 C   sT   t  ¡ }t ¡ }t|_t||dƒ}| t ddd¡¡}t	ƒ }||_| 
|¡ ||fS )zÎ
        Helper to set up an already connected protocol to be tested.

        @return: A new protocol with its attached clock.
        @rtype: Tuple of (L{policies.TimeoutProtocol}, L{task.Clock})
        NrK   rL   é90  )r
   r°   r   ÚServerFactoryr   rH   r/   r   rO   r   rZ   )r   r>   ÚwrappedFactoryrR   r\   r7   r   r   r   ÚgetProtocolAndClockù  s    
z(TimeoutProtocolTests.getProtocolAndClockc                 C   sj   |   ¡ \}}| d¡ |  |j¡ |  |jj¡ | d¡ | ¡  |  	|j¡ | d¡ |  |jj¡ dS )z2
        Will cancel the ongoing timeout.
        é   r   N)
rÅ   Ú
setTimeoutr†   ÚtimeoutCallrº   rQ   r    r»   ÚcancelTimeoutrt   ©r   Úsutr>   r   r   r   Útest_cancelTimeout  s    


z'TimeoutProtocolTests.test_cancelTimeoutc                 C   s2   |   ¡ \}}|  |j¡ | ¡  |  |jj¡ dS )z<
        Does nothing if no timeout is already set.
        N)rÅ   rt   rÈ   rÉ   rº   rQ   r    rÊ   r   r   r   Útest_cancelTimeoutNoTimeout"  s    z0TimeoutProtocolTests.test_cancelTimeoutNoTimeoutc                 C   s>   |   ¡ \}}|j}| d¡ | d¡ |  |j¡ | ¡  dS )z@
        Does nothing if no timeout is already reached.
        rÆ   N)rÅ   rQ   rÇ   r»   r[   r    rÉ   )r   rË   r>   ÚwrappedProtor   r   r   Útest_cancelTimeoutAlreadyCalled.  s    

z4TimeoutProtocolTests.test_cancelTimeoutAlreadyCalledc                 C   s:   |   ¡ \}}| d¡ |j ¡  | ¡  |  |jj¡ dS )zm
        Does nothing if the timeout is cancelled from another part.
        Ex from another thread.
        rÆ   N)rÅ   rÇ   rÈ   ÚcancelrÉ   rº   rQ   r    rÊ   r   r   r   Ú"test_cancelTimeoutAlreadyCancelled<  s
    

z7TimeoutProtocolTests.test_cancelTimeoutAlreadyCancelledN)	r(   r)   r*   r:   rÅ   rÌ   rÍ   rÏ   rÑ   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 )ÚTimeoutFactoryTestsz/
    Tests for L{policies.TimeoutFactory}.
    c                 C   sn   t  ¡ | _t ¡ }t|_t| j|dƒ| _| j t	 
ddd¡¡| _tƒ | _| j| j_| j | j¡ | jj| _dS )zq
        Create a testable, deterministic clock, and a set of
        server factory/protocol/transport.
        rÆ   rK   rL   rÂ   N)r
   r°   r>   r   rÃ   r   rH   rR   r/   r   rO   r\   r   r7   rZ   rQ   rÎ   )r   rÄ   r   r   r   ÚsetUpQ  s    
ÿ
zTimeoutFactoryTests.setUpc                 C   sF   | j  dddddg¡ |  | jj¡ | j  ddg¡ |  | jj¡ dS )z¶
        Make sure that when a TimeoutFactory accepts a connection, it will
        time out that connection if no data is read or written within the
        timeout period.
        ç        ç      à?ç      ð?gš™™™™™Ù?çš™™™™™É?N)r>   Úpumprº   rÎ   r    r[   r   r   r   r   Útest_timeoutb  s    z TimeoutFactoryTests.test_timeoutc                 C   s    | j  dddg¡ |  | jj¡ | j d¡ | j  dddg¡ |  | jj¡ | j dgd ¡ | j  dddg¡ |  | jj¡ | j  ddg¡ |  | jj¡ dS )	z’
        Make sure that writing data to a transport from a protocol
        constructed by a TimeoutFactory resets the timeout countdown.
        rÔ   rÕ   rÖ   ó   bytes bytes bytesr®   rÆ   g       @N)	r>   rØ   rº   rÎ   r    r\   r8   r±   r[   r   r   r   r   Útest_sendAvoidsTimeoutp  s    z*TimeoutFactoryTests.test_sendAvoidsTimeoutc                 C   sp   | j  dddg¡ |  | jj¡ | j d¡ | j  dddg¡ |  | jj¡ | j  dddg¡ |  | jj¡ dS )zR
        Make sure that receiving data also resets the timeout countdown.
        rÔ   rÖ   rÕ   rÚ   N)r>   rØ   rº   rÎ   r    r\   r'   r[   r   r   r   r   Útest_receiveAvoidsTimeoutŒ  s    z-TimeoutFactoryTests.test_receiveAvoidsTimeoutN)r(   r)   r*   r:   rÓ   rÙ   rÛ   rÜ   r   r   r   r   rÒ   L  s
   rÒ   c                   @   sJ   e Zd ZdZdZdZdd„ Zdd„ Zdd	„ Zddd„Z	dd„ Z
dd„ Zd
S )ÚTimeoutTesterz”
    A testable protocol with timeout facility.

    @ivar timedOut: set to C{True} if a timeout has been detected.
    @type timedOut: C{bool}
    rÆ   Fc                 C   s
   || _ dS )zF
        Initialize the protocol with a C{task.Clock} object.
        N)r>   )r   r>   r   r   r   r   ¬  s    zTimeoutTester.__init__c                 C   s   |   | j¡ dS )z3
        Upon connection, set the timeout.
        N)rÇ   ÚtimeOutr   r   r   r   r   ²  s    zTimeoutTester.connectionMadec                 C   s   |   ¡  tj | |¡ dS )z,
        Reset the timeout on data.
        N)ZresetTimeoutr   rY   r'   r%   r   r   r   r'   ¸  s    zTimeoutTester.dataReceivedNc                 C   s   |   d¡ dS )zD
        On connection lost, cancel all timeout operations.
        N)rÇ   r!   r   r   r   r#   ¿  s    zTimeoutTester.connectionLostc                 C   s
   d| _ dS )zX
        Flags the timedOut variable to indicate the timeout of the connection.
        TN)ÚtimedOutr   r   r   r   ÚtimeoutConnectionÅ  s    zTimeoutTester.timeoutConnectionc                 O   s   | j j||f|ž|ŽS )zD
        Override callLater to use the deterministic clock.
        rC   )r   ÚtimeoutrF   r@   rA   r   r   r   rD   Ë  s    zTimeoutTester.callLater)N)r(   r)   r*   r:   rÞ   rß   r   r   r'   r#   rà   rD   r   r   r   r   rÝ   ¡  s   
rÝ   c                   @   sP   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S )ÚTimeoutMixinTestsz-
    Tests for L{policies.TimeoutMixin}.
    c                 C   s   t  ¡ | _t| jƒ| _dS )zY
        Create a testable, deterministic clock and a C{TimeoutTester} instance.
        N)r
   r°   r>   rÝ   r\   r   r   r   r   rÓ   ×  s    
zTimeoutMixinTests.setUpc                 C   s$   | j  d¡ |  t| jjƒd¡ dS )zš
        Test that the callLater of the clock is used instead of
        L{reactor.callLater<twisted.internet.interfaces.IReactorTime.callLater>}
        rµ   r   N)r\   rÇ   r_   r¡   r>   Zcallsr   r   r   r   Útest_overriddenCallLaterÞ  s    z*TimeoutMixinTests.test_overriddenCallLaterc                 C   sR   | j  tƒ ¡ | j ddddg¡ |  | j j¡ | j ddg¡ |  | j j¡ dS )zq
        Check that the protocol does timeout at the time specified by its
        C{timeOut} attribute.
        r   rÕ   rÖ   N)r\   rZ   r   r>   rØ   rº   rß   r[   r   r   r   r   rÙ   æ  s
    zTimeoutMixinTests.test_timeoutc                 C   s€   | j  tƒ ¡ | j ddddg¡ |  | j j¡ | j  d¡ | j ddddg¡ |  | j j¡ | j ddg¡ |  | j j¡ dS )zV
        Check that receiving data is delaying the timeout of the connection.
        r   rÕ   rÖ   s   hello thereN)	r\   rZ   r   r>   rØ   rº   rß   r'   r[   r   r   r   r   Útest_noTimeoutó  s    z TimeoutMixinTests.test_noTimeoutc                 C   sr   d| j _| j  tƒ ¡ | j  d¡ |  | j jd¡ | j ddg¡ |  | j j	¡ | j ddg¡ |  
| j j	¡ dS )zy
        Check that setting a new value for timeout cancel the previous value
        and install a new timeout.
        Nr   r   gÍÌÌÌÌÌì?r×   )r\   rÞ   rZ   r   rÇ   r_   r>   rØ   rº   rß   r[   r   r   r   r   Útest_resetTimeout  s    z#TimeoutMixinTests.test_resetTimeoutc                 C   sV   d| j _| j  tƒ ¡ | j  d¡ |  | j j¡ | j ddddg¡ |  | j j	¡ dS )zO
        Setting the timeout to L{None} cancel any timeout operations.
        é   Nr   )
r\   rÞ   rZ   r   rÇ   rt   r>   rØ   rº   rß   r   r   r   r   rÌ     s    z$TimeoutMixinTests.test_cancelTimeoutc                 C   sb   d| j _|  | j  d¡d¡ |  | j  d¡d¡ |  | j  d¡¡ |  | j jd¡ | j  d¡ dS )zM
        setTimeout should return the value of the previous timeout.
        ræ   rµ   Nr   )r\   rÞ   r_   rÇ   rt   r   r   r   r   Útest_setTimeoutReturn  s    z'TimeoutMixinTests.test_setTimeoutReturnc                 C   sJ   | j  d¡ | j ¡ d  ¡  |  | j j¡ | j  d¡ |  | j j¡ dS )zÑ
        When the timeout was already cancelled from an external place,
        calling setTimeout with C{None} to explicitly cancel it will clean
        up the timeout without raising any exception.
        rÆ   r   N)r\   rÇ   r>   ZgetDelayedCallsrÐ   r†   rÞ   rt   r   r   r   r   Ú%test_setTimeoutCancleAlreadyCancelled,  s
    z7TimeoutMixinTests.test_setTimeoutCancleAlreadyCancelledN)r(   r)   r*   r:   rÓ   rã   rÙ   rä   rå   rÌ   rç   rè   r   r   r   r   râ   Ò  s   râ   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú!LimitTotalConnectionsFactoryTestsz/Tests for policies.LimitTotalConnectionsFactoryc                 C   s‚   t  ¡ }tj|_|  d|j¡ | d ¡}|  d|j¡ | d ¡}|  d|j¡ | d ¡ |  d|j¡ | d ¡ |  d|j¡ d S )Nr   r   r�   )r   ÚLimitTotalConnectionsFactoryr   rY   r_   ÚconnectionCountr/   r#   )r   rR   Úp1Úp2r   r   r   ÚtestConnectionCounting@  s    



z8LimitTotalConnectionsFactoryTests.testConnectionCountingc                    sÚ   t  ¡ ‰ tjˆ _dˆ _ˆ  d ¡}|  |¡ |  dˆ j¡ |  	ˆ  d ¡¡ |  dˆ j¡ G ‡ fdd„dtjƒ}|ˆ _
dˆ _ˆ  d ¡}| d ¡ |  ˆ j¡ |  dˆ j¡ | d ¡ |  dˆ j¡ | d ¡ |  dˆ j¡ d S )Nr   c                       s   e Zd Z‡ fdd„ZdS )zRLimitTotalConnectionsFactoryTests.testConnectionLimiting.<locals>.OverflowProtocolc                    s
   dˆ _ d S r1   )Ú
overflowedr   ©rR   r   r   r   f  s    zaLimitTotalConnectionsFactoryTests.testConnectionLimiting.<locals>.OverflowProtocol.connectionMadeN)r(   r)   r*   r   r   rð   r   r   ÚOverflowProtocole  s   rñ   Fr�   r   )r   rê   r   rY   ZconnectionLimitr/   r†   r_   rë   rt   ZoverflowProtocolrï   rZ   r[   r#   )r   r-   rñ   Úopr   rð   r   ÚtestConnectionLimitingT  s&    





z8LimitTotalConnectionsFactoryTests.testConnectionLimitingN)r(   r)   r*   r:   rî   ró   r   r   r   r   ré   =  s   ré   c                   @   s   e Zd Zdd„ ZdS )ÚWriteSequenceEchoProtocolc                 C   s.   |  d¡dkr| j |g¡ nt | |¡ d S )Ns   vector!éÿÿÿÿ)Úfindr7   r±   r0   r'   )r   Úbytesr   r   r   r'   |  s    z&WriteSequenceEchoProtocol.dataReceivedN)r(   r)   r*   r'   r   r   r   r   rô   {  s   rô   c                   @   s   e Zd ZdZdd„ ZdS )ÚTestLoggingFactoryNc                 C   s    | j d kstdƒ‚tƒ | _ | j S )Nzopen() called too many times)ÚopenFileÚAssertionErrorr   )r   Únamer   r   r   Úopen†  s    zTestLoggingFactory.open)r(   r)   r*   rù   rü   r   r   r   r   rø   ƒ  s   rø   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚLoggingFactoryTestsz6
    Tests for L{policies.TrafficLoggingFactory}.
    c                 C   s  t ƒ }t|_tƒ }t|dƒ}| d¡}||_| |¡ |j ¡ }|  	d|¡ |  
| ¡ ¡ | d¡ |j ¡ }|  	d d¡|¡ |  	d d¡|¡ |  | ¡ d¡ | ¡  | d¡ |j ¡ }|  	d dg¡|¡ |  | ¡ d¡ | ¡  |j ¡ }|  	d	|¡ d
S )zQ
        Check the output produced by L{policies.TrafficLoggingFactory}.
        Útest©ú1.2.3.4i.  Ú*s   here are some bytesz	C 1: {!r}z	S 1: {!r}s"   prepare for vector! to the extremez
SV 1: {!r}ZConnectionDoneN)r9   rô   r   r   rø   r/   rZ   rù   ÚgetvalueZassertInrº   r²   r'   Úformatr_   r¿   r‡   )r   rÄ   ÚtrS   r-   Úvr   r   r   Útest_thingsGetLogged‘  s.    








z(LoggingFactoryTests.test_thingsGetLoggedc                 C   sn   t ƒ }t|dƒ}|  |jd¡ | d¡ |  |jd¡ d|_| d¡ |  |jd¡ | ¡  |  |jd¡ dS )zG
        Test counter management with the resetCounter method.
        rþ   r   rÿ   r   N)r   i/  r�   )r9   rø   r_   Ú_counterr/   rù   ZresetCounter)r   rÄ   rS   r   r   r   Útest_counter³  s    


z LoggingFactoryTests.test_counterc                    sž   g ‰ g ‰‡ ‡fdd„}|   td|¡ t ¡ }t|_t |d¡}| t 	ddd¡¡}| t 	ddd¡¡}d	i f}d
i f}|  
||gˆ ¡ |  
|j|jgˆ¡ dS )zÊ
        When the L{policies.TrafficLoggingFactory} builds a protocol, it
        automatically opens a unique log file for that protocol and attaches
        the logfile to the built protocol.
        c                     s,   ˆ   | |f¡ tƒ }| d |_ˆ  |¡ |S )zX
            Mock for the open call to prevent actually opening a log file.
            r   )rw   r   rû   )r@   rA   Úio©Z
open_callsZopen_rvaluesr   r   Úmocked_openÍ  s
    

zULoggingFactoryTests.test_loggingFactoryOpensLogfileAutomatically.<locals>.mocked_openrü   rþ   rK   rL   rÂ   i:0  )ztest-1Úw)ztest-2r  N)ÚpatchÚbuiltinsr   rÃ   r   r   ÚTrafficLoggingFactoryr/   r   rO   r_   Úlogfile)r   r  rÄ   rR   Zfirst_protoZsecond_protoZ
first_callZsecond_callr   r
  r   Ú,test_loggingFactoryOpensLogfileAutomaticallyÄ  s"    
ÿÿz@LoggingFactoryTests.test_loggingFactoryOpensLogfileAutomaticallyN)r(   r)   r*   r:   r  r  r  r   r   r   r   rý   Œ  s   "rý   ).r:   r  r	  r   Zzope.interfacer   r   r   Ztwisted.internetr   r   r   r	   r
   Ztwisted.internet.testingr   r   Ztwisted.protocolsr   Ztwisted.trialr   rY   r   ZClientFactoryr+   r0   rÃ   r9   r=   r;   rI   rH   ZTestCaserJ   rN   r‹   rÁ   rÒ   ZTimeoutMixinrÝ   râ   ré   rô   r  rø   rý   r   r   r   r   Ú<module>   s4    ^	 XU1k>	