o
    ¯b%  ã                   @   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 ddlmZ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)Úpolicies)ÚStringTransportÚ StringTransportWithDisconnection)Ú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   ú</usr/lib/python3/dist-packages/twisted/test/test_policies.pyÚ__init__   s   
zSimpleProtocol.__init__c                 C   ó   d| _ | j d¡ d S ©Né   Ú )Ú	connectedr   Úcallbackr   r   r   r   ÚconnectionMade   ó   zSimpleProtocol.connectionMadec                 C   r   r   )Údisconnectedr   r   ©r   Úreasonr   r   r   ÚconnectionLost!   r!   zSimpleProtocol.connectionLostc                 C   s   |  j |7  _ d S r   )Úbuffer©r   Údatar   r   r   ÚdataReceived%   ó   zSimpleProtocol.dataReceivedN)
Ú__name__Ú
__module__Ú__qualname__r   r"   r&   r   r    r%   r)   r   r   r   r   r      s    r   c                   @   ó   e Zd Zdd„ Zdd„ ZdS )ÚSillyFactoryc                 C   s
   || _ d S r   ©Úp)r   r1   r   r   r   r   *   ó   
zSillyFactory.__init__c                 C   s   | j S r   r0   )r   Úaddrr   r   r   ÚbuildProtocol-   s   zSillyFactory.buildProtocolN)r+   r,   r-   r   r4   r   r   r   r   r/   )   ó    r/   c                   @   ó0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚEchoProtocolFc                 C   ó
   d| _ d S ©NT©Úpausedr   r   r   r   ÚpauseProducing4   r2   zEchoProtocol.pauseProducingc                 C   r8   )NFr:   r   r   r   r   ÚresumeProducing7   r2   zEchoProtocol.resumeProducingc                 C   s   d S r   r   r   r   r   r   ÚstopProducing:   ó   zEchoProtocol.stopProducingc                 C   s   | j  |¡ d S r   )Ú	transportÚwriter'   r   r   r   r)   =   s   zEchoProtocol.dataReceivedN)r+   r,   r-   r;   r<   r=   r>   r)   r   r   r   r   r7   1   s    r7   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__r7   r   r   r   r   r   rB   A   s    rB   c                   @   ó    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   ó$   t jj| g|¢R i |¤Ž || _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   rI   ÚargsÚkwargsr   r   r   r   N   ó   
z"TestableThrottlingFactory.__init__c                 C   ó   | j  ||¡S ©z0
        Forward to the testable clock.
        ©rI   Ú	callLater©r   ÚperiodÚfuncr   r   r   rQ   W   ó   z#TestableThrottlingFactory.callLaterN©r+   r,   r-   rC   r   rQ   r   r   r   r   rE   I   ó    	rE   c                   @   rD   )ÚTestableTimeoutFactoryzE
    L{policies.TimeoutFactory} using a L{task.Clock} for tests.
    c                 O   rF   rG   )r   ÚTimeoutFactoryr   rI   rJ   r   r   r   r   c   rM   zTestableTimeoutFactory.__init__c                 C   rN   rO   rP   rR   r   r   r   rQ   l   rU   z TestableTimeoutFactory.callLaterNrV   r   r   r   r   rX   ^   rW   rX   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)	rB   r   ÚWrappingFactoryr4   r   ÚIPv4AddressÚassertIsÚwrappedProtocolÚfactory)r   ÚfÚwfr1   r   r   r   Útest_protocolFactoryAttributex   ó   
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                   @   ó   e Zd ZdS )z=WrapperTests.test_transportInterfaces.<locals>.IStubTransportN©r+   r,   r-   r   r   r   r   ÚIStubTransport‰   ó    ri   c                   @   rg   )z<WrapperTests.test_transportInterfaces.<locals>.StubTransportNrh   r   r   r   r   ÚStubTransportŒ   s    rk   N)r   r   r   r   ÚProtocolWrapperr   ÚProtocolr^   ÚmakeConnectionÚ
assertTrueÚ
providedByr@   )r   ri   rk   Ú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)rB   r   r^   ÚassertEqualÚ	logPrefix)r   Úserverrb   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                   @   rg   )z=WrapperTests.test_factoryLogPrefixFallback.<locals>.NoFactoryNrh   r   r   r   r   Ú	NoFactory®   rj   rx   zNoFactory (WrappingFactory)N)r   r^   rt   ru   )r   rx   rv   rb   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.
        r[   r\   r]   zEchoProtocol (ProtocolWrapper)N)rB   r   r^   r4   r   r_   rt   ru   )r   rv   rb   r   r   r   r   Útest_protocolLogPrefixµ   rf   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                   @   rg   )z?WrapperTests.test_protocolLogPrefixFallback.<locals>.NoProtocolNrh   r   r   r   r   Ú
NoProtocolÅ   rj   r{   r[   r\   r]   zNoProtocol (ProtocolWrapper)N)	rB   r   r   r^   r4   r   r_   rt   ru   )r   r{   rv   rb   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   rl   r^   rB   r   rm   r   rn   ©r   rr   r@   r   r   r   Ú_getWrapperÎ   s   ÿ
zWrapperTests._getWrapperc                 C   ó"   |   ¡ }|  | ¡ |j ¡ ¡ dS )zk
        L{policies.ProtocolWrapper.getHost} calls C{getHost} on the underlying
        transport.
        N)r~   rt   ÚgetHostr@   ©r   rr   r   r   r   Útest_getHostÚ   ó   zWrapperTests.test_getHostc                 C   r   )zk
        L{policies.ProtocolWrapper.getPeer} calls C{getPeer} on the underlying
        transport.
        N)r~   rt   ÚgetPeerr@   r   r   r   r   Útest_getPeerâ   rƒ   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)r~   ÚobjectÚregisterProducerr`   r@   Úproducerro   Ú	streaming©r   rr   rˆ   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)r~   r†   r‡   ÚunregisterProducerÚassertIsNoner@   rˆ   r‰   rŠ   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 r9   ©Úappendr   ©Úresultr   r   Ú<lambda>  s   
 z1WrapperTests.test_stopConsuming.<locals>.<lambda>TN)r~   r@   ÚstopConsumingrt   r   r   r‘   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                       ó   e Zd Z‡ fdd„ZdS )z4WrapperTests.test_startedConnecting.<locals>.Factoryc                    s   ˆ   |¡ d S r   r   )r   Ú	connectorr‘   r   r   ÚstartedConnecting  s   zFWrapperTests.test_startedConnecting.<locals>.Factory.startedConnectingN)r+   r,   r-   r˜   r   r‘   r   r   ÚFactory  ó    r™   N)r   r^   r†   r˜   rt   )r   r™   rr   r—   r   r‘   r   Útest_startedConnecting  s   
z#WrapperTests.test_startedConnectingc                    óP   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                       r–   )z7WrapperTests.test_clientConnectionLost.<locals>.Factoryc                    ó   ˆ   ||f¡ d S r   r   ©r   r—   r$   r‘   r   r   ÚclientConnectionLost$  r*   zLWrapperTests.test_clientConnectionLost.<locals>.Factory.clientConnectionLostN)r+   r,   r-   rŸ   r   r‘   r   r   r™   #  rš   r™   N)r   r^   r†   rŸ   rt   ©r   r™   rr   r—   r$   r   r‘   r   Útest_clientConnectionLost  ó   z&WrapperTests.test_clientConnectionLostc                    rœ   )z‡
        L{policies.WrappingFactory.clientConnectionFailed} calls
        C{clientConnectionFailed} on the underlying factory.
        c                       r–   )z9WrapperTests.test_clientConnectionFailed.<locals>.Factoryc                    r   r   r   rž   r‘   r   r   ÚclientConnectionFailed5  r*   zPWrapperTests.test_clientConnectionFailed.<locals>.Factory.clientConnectionFailedN)r+   r,   r-   r£   r   r‘   r   r   r™   4  rš   r™   N)r   r^   r†   r£   rt   r    r   r‘   r   Útest_clientConnectionFailed-  r¢   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.
        :return:
        N)r   rl   r^   rB   r   rm   r   rn   ÚassertIsNotNonera   ÚloseConnectionr   r}   r   r   r   Útest_breakReferenceCycle>  s   ÿ
z%WrapperTests.test_breakReferenceCycleN)r+   r,   r-   rC   re   rs   rw   ry   rz   r|   r~   r‚   r…   r‹   rŽ   r•   r›   r¡   r¤   r§   r   r   r   r   rZ   s   s$    
	
rZ   c                   @   r.   )r^   c                 C   s   |S r   r   )r   rc   r1   r   r   r   r   R  r?   zWrappingFactory.protocolc                 C   s   t j | ¡ | j d ¡ d S r   )r   r^   ÚstartFactoryÚdeferredr   r   r   r   r   r¨   U  s   zWrappingFactory.startFactoryN)r+   r,   r-   r   r¨   r   r   r   r   r^   Q  r5   r^   c                   @   r6   )Ú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>g  s   € z-ThrottlingTests.test_limit.<locals>.<genexpr>é   é   r   r\   )Ú	interfacec                    sD   t  dˆtˆ ƒ¡ ˆ j ‡‡fdd„¡ ˆj ‡‡fdd„¡ ˆjS )Nr\   c                    ó   t  dˆtˆ ƒ¡S ©Nr\   ©r	   Ú
connectTCPr/   ©Úr)Úc2Únr   r   r“   s  ó    zAThrottlingTests.test_limit.<locals>._connect123.<locals>.<lambda>c                    r±   r²   r³   rµ   )Úc3r¸   r   r   r“   v  r¹   )r	   r´   r/   r   ÚaddCallbackr   ©Úresults)Úc1r·   rº   r¸   r   r   Ú_connect123p  s   ÿÿz/ThrottlingTests.test_limit.<locals>._connect123c                    sZ   ˆ  dd„ ˆ ˆˆfD ƒg d¢¡ ˆ  dd„ ˆ ˆˆfD ƒg d¢¡ ˆ  tˆj ¡ ƒd¡ | S )Nc                 S   ó   g | ]}|j ‘qS r   )r   ©r«   Úcr   r   r   Ú
<listcomp>{  r¹   zAThrottlingTests.test_limit.<locals>._check123.<locals>.<listcomp>)r   r   r   c                 S   rÀ   r   )r"   rÁ   r   r   r   rÃ   |  r¹   )r   r   r   r¯   )rt   ÚlenÚ	protocolsÚkeysr¼   )r¾   r·   rº   r   ÚtServerr   r   Ú	_check123z  s     z-ThrottlingTests.test_limit.<locals>._check123c                    s   ˆ j  ¡  ˆ jS r   )r@   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   )rt   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   )r@   r¦   r   ÚDeferredListÚmaybeDeferredÚstopListeningr   )r½   rÂ   )r·   rÊ   r1   r   r   Ú_cleanupŽ  s   
ýÿz,ThrottlingTests.test_limit.<locals>._cleanup)rB   Úranger   rH   r^   r   r   r©   r	   Ú	listenTCPr€   Úportr»   )	r   rv   ÚwrapTServerr¿   rÈ   rÉ   rË   rÌ   rÐ   r   )r¾   r·   rº   rÊ   r¸   r1   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.
        r[   r\   r   ó   bytesr®   s   bytesbytesbytesbytesé   N)rB   rE   r
   ÚClockr4   r   r_   r   r   rn   ÚwriteSequencert   ÚvalueÚwrittenThisSecond)r   rv   rÇ   r   r@   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.
        é
   )Ú
writeLimitr[   r\   r   ó
   0123456789ó
   abcdefghijó   0123456789abcdefghijr×   çÍÌÌÌÌÌð?N)rB   rE   r
   rØ   r4   r   r_   r   r   rn   ra   rˆ   r)   rt   rÚ   rÛ   ÚassertFalser;   rI   Úadvancero   ©r   rv   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Ý   )Ú	readLimitr[   r\   r   rß   rà   rá   r×   râ   r;   Ú	producingN)rB   rE   r
   rØ   r4   r   r_   r   r   rn   r)   rt   rÚ   ÚreadThisSecondrI   rä   ÚproducerStateÚclearrå   r   r   r   Útest_readLimitÎ  s6   




zThrottlingTests.test_readLimitN)r+   r,   r-   rC   rÕ   rÜ   rç   rí   r   r   r   r   rª   Z  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})
        Nr[   r\   é90  )r
   rØ   r   ÚServerFactoryr   rX   r4   r   r_   r   rn   )r   rI   ÚwrappedFactoryrb   rq   r@   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ã   ra   r"   rä   ÚcancelTimeoutr   ©r   ÚsutrI   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ò   r   rõ   rö   rã   ra   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ò   ra   rô   rä   ro   r"   rö   )r   rø   rI   ÚwrappedProtor   r   r   Útest_cancelTimeoutAlreadyCalled0  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ã   ra   r"   r÷   r   r   r   Ú"test_cancelTimeoutAlreadyCancelled>  s
   

z7TimeoutProtocolTests.test_cancelTimeoutAlreadyCancelledN)	r+   r,   r-   rC   rò   rù   rú   rü   rþ   r   r   r   r   rî   ö  s    rî   c                   @   r6   )Ú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ó   r[   r\   rï   N)r
   rØ   rI   r   rð   r   rX   rb   r4   r   r_   rq   r   r@   rn   ra   rû   )r   rñ   r   r   r   ÚsetUpS  s   
ÿ
zTimeoutFactoryTests.setUpc                 C   s@   | j  g d¢¡ |  | 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.
        )ç        ç      à?ç      ð?r  gš™™™™™Ù?r  çš™™™™™É?N)rI   Úpumprã   rû   r"   ro   r   r   r   r   Útest_timeoutd  s   z TimeoutFactoryTests.test_timeoutc                 C   sš   | j  g d¢¡ |  | jj¡ | j d¡ | j  g d¢¡ |  | jj¡ | j dgd ¡ | j  g d¢¡ |  | 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 bytes©r  r  r  rÖ   ró   r  g       @N)	rI   r  rã   rû   r"   rq   rA   rÙ   ro   r   r   r   r   Útest_sendAvoidsTimeoutr  s   z*TimeoutFactoryTests.test_sendAvoidsTimeoutc                 C   sj   | j  g d¢¡ |  | jj¡ | j d¡ | j  g d¢¡ |  | jj¡ | j  g d¢¡ |  | jj¡ dS )zR
        Make sure that receiving data also resets the timeout countdown.
        )r  r  r  r  r  N)rI   r  rã   rû   r"   rq   r)   ro   r   r   r   r   Útest_receiveAvoidsTimeoutŽ  s   z-TimeoutFactoryTests.test_receiveAvoidsTimeoutN)r+   r,   r-   rC   r   r  r	  r
  r   r   r   r   rÿ   N  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)rI   )r   rI   r   r   r   r   ®  ó   
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)ÚresetTimeoutr   rm   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%   Á  rU   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Ç  r  zTimeoutTester.timeoutConnectionc                 O   s   | j j||g|¢R i |¤ŽS )zD
        Override callLater to use the deterministic clock.
        rP   )r   ÚtimeoutrT   rK   rL   r   r   r   rQ   Í  s   zTimeoutTester.callLaterr   )r+   r,   r-   rC   r  r  r   r    r)   r%   r  rQ   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Ø   rI   r  rq   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)rq   rô   rt   rÄ   rI   Úcallsr   r   r   r   Útest_overriddenCallLaterà  s   z*TimeoutMixinTests.test_overriddenCallLaterc                 C   sN   | j  tƒ ¡ | j g d¢¡ |  | 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  r  r   r  N)rq   rn   r   rI   r  rã   r  ro   r   r   r   r   r  è  s
   zTimeoutMixinTests.test_timeoutc                 C   sx   | j  tƒ ¡ | j g d¢¡ |  | j j¡ | j  d¡ | j g d¢¡ |  | j j¡ | j ddg¡ |  | j j¡ dS )zV
        Check that receiving data is delaying the timeout of the connection.
        r  s   hello there)r   r  r  r  r   r  N)	rq   rn   r   rI   r  rã   r  r)   ro   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  )rq   r  rn   r   rô   rt   rI   r  rã   r  ro   r   r   r   r   Útest_resetTimeout  s   z#TimeoutMixinTests.test_resetTimeoutc                 C   sR   d| j _| j  tƒ ¡ | j  d¡ |  | j j¡ | j g d¢¡ |  | j j	¡ dS )zO
        Setting the timeout to L{None} cancel any timeout operations.
        é   N)r   r  r  r  )
rq   r  rn   r   rô   r   rI   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   )rq   r  rt   rô   r   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)rq   rô   rI   ÚgetDelayedCallsrý   r¥   r  r   r   r   r   r   Ú%test_setTimeoutCancleAlreadyCancelled.  s
   z7TimeoutMixinTests.test_setTimeoutCancleAlreadyCancelledN)r+   r,   r-   rC   r   r  r  r  r  rù   r  r  r   r   r   r   r  Ô  s    r  c                   @   rD   )Ú!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   rm   rt   ÚconnectionCountr4   r%   )r   rb   Úp1Úp2r   r   r   ÚtestConnectionCountingB  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                       r–   )zRLimitTotalConnectionsFactoryTests.testConnectionLimiting.<locals>.OverflowProtocolc                    s
   dˆ _ d S r9   )Ú
overflowedr   ©rb   r   r   r    h  r2   zaLimitTotalConnectionsFactoryTests.testConnectionLimiting.<locals>.OverflowProtocol.connectionMadeN)r+   r,   r-   r    r   r#  r   r   ÚOverflowProtocolg  rš   r$  Fr¯   r   )r   r  r   rm   ÚconnectionLimitr4   r¥   rt   r  r   ÚoverflowProtocolr"  rn   ro   r%   )r   r1   r$  Úopr   r#  r   ÚtestConnectionLimitingV  s&   





z8LimitTotalConnectionsFactoryTests.testConnectionLimitingN)r+   r,   r-   rC   r!  r(  r   r   r   r   r  ?  s    r  c                   @   s   e Zd Zdd„ ZdS )ÚWriteSequenceEchoProtocolc                 C   s0   |  d¡dkr| j |g¡ d S t | |¡ d S )Ns   vector!éÿÿÿÿ)Úfindr@   rÙ   r7   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 u s	J dƒ‚tƒ | _ | j S )Nzopen() called too many times)ÚopenFiler   )r   Únamer   r   r   Úopenˆ  s   zTestLoggingFactory.open)r+   r,   r-   r.  r0  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}ÚConnectionDoneN)rB   r)  r   r   r-  r4   rn   r.  ÚgetvalueÚassertInrã   rÚ   r)   Úformatrt   rì   r¦   )r   rñ   Útrc   r1   Ú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.
        r2  r   r3  r   N)r4  i/  r¯   )rB   r-  rt   Ú_counterr4   r.  ÚresetCounter)r   rñ   rc   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   )r   r   r/  )rK   rL   Úio©Ú
open_callsÚopen_rvaluesr   r   Úmocked_openÏ  s
   

zULoggingFactoryTests.test_loggingFactoryOpensLogfileAutomatically.<locals>.mocked_openr0  r2  r[   r\   rï   i:0  )ztest-1Úw)ztest-2rE  N)ÚpatchÚbuiltinsr   rð   r   r   ÚTrafficLoggingFactoryr4   r   r_   rt   Úlogfile)r   rD  rñ   rb   Úfirst_protoÚsecond_protoÚ
first_callÚsecond_callr   rA  r   Ú,test_loggingFactoryOpensLogfileAutomaticallyÆ  s"   
ÿÿz@LoggingFactoryTests.test_loggingFactoryOpensLogfileAutomaticallyN)r+   r,   r-   rC   r<  r?  rN  r   r   r   r   r1  Ž  s
    "r1  ).rC   rG  r@  r   Úzope.interfacer   r   r   Útwisted.internetr   r   r   r	   r
   Útwisted.protocolsr   Útwisted.test.proto_helpersr   r   Útwisted.trialr   rm   r   ÚClientFactoryr/   r7   rð   rB   rH   rE   rY   rX   ÚTestCaserZ   r^   rª   rî   rÿ   ÚTimeoutMixinr  r  r  r)  rH  r-  r1  r   r   r   r   Ú<module>   s6    _	 XU1k>	