U
    
W[ۂ                     @   s  d Z ddlmZmZ ddlmZmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZmZmZmZmZ dd	lmZ zdd
lZW n ek
r   dd
lZY nX 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j0Z1G d+d, d,ej%Z2d
S )-z
Test code for policies.
    )divisionabsolute_import)	InterfaceimplementerimplementedBy)NativeStringIO)unittest)StringTransport) StringTransportWithDisconnection)protocolreactoraddressdefertask)policiesNc                   @   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   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   4   s    zSillyFactory.__init__c                 C   s   | j S r   r.   )r   Zaddrr   r   r   buildProtocol7   s    zSillyFactory.buildProtocolN)r*   r+   r,   r   r0   r   r   r   r   r-   2   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   pauseProducing>   s    zEchoProtocol.pauseProducingc                 C   s
   d| _ d S )NFr3   r   r   r   r   resumeProducingA   s    zEchoProtocol.resumeProducingc                 C   s   d S r   r   r   r   r   r   stopProducingD   s    zEchoProtocol.stopProducingc                 C   s   | j | d S r   )	transportwriter'   r   r   r   r)   G   s    zEchoProtocol.dataReceivedN)r*   r+   r,   r4   r5   r6   r7   r)   r   r   r   r   r1   ;   s
   r1   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__r1   r   r   r   r   r   r:   L   s   r:   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   Y   s    z"TestableThrottlingFactory.__init__c                 C   s   | j ||S z0
        Forward to the testable clock.
        r?   	callLaterr   Zperiodfuncr   r   r   rE   c   s    z#TestableThrottlingFactory.callLaterNr*   r+   r,   r;   r   rE   r   r   r   r   r<   T   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   p   s    zTestableTimeoutFactory.__init__c                 C   s   | j ||S rC   rD   rF   r   r   r   rE   z   s    z TestableTimeoutFactory.callLaterNrH   r   r   r   r   rI   k   s   
rI   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)	r:   r   WrappingFactoryr0   r   IPv4AddressassertIswrappedProtocolfactory)r   fZwfr/   r   r   r   test_protocolFactoryAttribute   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   rW   c                   @   s   e Zd ZdS )z<WrapperTests.test_transportInterfaces.<locals>.StubTransportNrV   r   r   r   r   StubTransport   s   rX   N)r   r   r   r   ProtocolWrapperr   ProtocolrO   makeConnection
assertTrueZ
providedByr8   )r   rW   rX   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)r:   r   rO   assertEqual	logPrefix)r   serverrS   r   r   r   test_factoryLogPrefix   s    
z"WrapperTests.test_factoryLogPrefixc                 C   s4   G dd dt }| }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>.NoFactoryNrV   r   r   r   r   	NoFactory   s   rd   zNoFactory (WrappingFactory)N)objectr   rO   r`   ra   )r   rd   rb   rS   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.
        rL   rM   rN   zEchoProtocol (ProtocolWrapper)N)r:   r   rO   r0   r   rP   r`   ra   )r   rb   rS   r   r   r   r   test_protocolLogPrefix   s    
z#WrapperTests.test_protocolLogPrefixc                 C   sN   G dd dt }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>.NoProtocolNrV   r   r   r   r   
NoProtocol   s   rh   rL   rM   rN   zNoProtocol (ProtocolWrapper)N)
re   r:   r   r   rO   r0   r   rP   r`   ra   )r   rh   rb   rS   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   rY   rO   r:   r   rZ   r	   r[   r   r^   r8   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)rk   r`   getHostr8   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)rk   r`   ZgetPeerr8   rm   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)rk   re   registerProducerrQ   r8   producerr\   	streamingr   r^   rq   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)rk   re   rp   ZunregisterProducerassertIsNoner8   rq   rr   rs   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 r2   appendr   resultr   r   <lambda>!  r   z1WrapperTests.test_stopConsuming.<locals>.<lambda>TN)rk   r8   ZstopConsumingr`   rm   r   ry   r   test_stopConsuming  s
    zWrapperTests.test_stopConsumingc                    sF   g  G  fdddt }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   rw   )r   	connectorry   r   r   startedConnecting-  s    zFWrapperTests.test_startedConnecting.<locals>.Factory.startedConnectingN)r*   r+   r,   r~   r   ry   r   r   Factory,  s   r   N)re   r   rO   r~   r`   )r   r   r^   r}   r   ry   r   test_startedConnecting&  s    
z#WrapperTests.test_startedConnectingc                    sR   g  G  fdddt }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   rw   r   r}   r$   ry   r   r   clientConnectionLost=  s    zLWrapperTests.test_clientConnectionLost.<locals>.Factory.clientConnectionLostN)r*   r+   r,   r   r   ry   r   r   r   <  s   r   N)re   r   rO   r   r`   r   r   r^   r}   r$   r   ry   r   test_clientConnectionLost6  s    z&WrapperTests.test_clientConnectionLostc                    sR   g  G  fdddt }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   rw   r   ry   r   r   clientConnectionFailedN  s    zPWrapperTests.test_clientConnectionFailed.<locals>.Factory.clientConnectionFailedN)r*   r+   r,   r   r   ry   r   r   r   M  s   r   N)re   r   rO   r   r`   r   r   ry   r   test_clientConnectionFailedG  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.
        :return:
        N)r   rY   rO   r:   r   rZ   r
   r[   assertIsNotNonerR   loseConnectionru   rj   r   r   r   test_breakReferenceCycleX  s    
z%WrapperTests.test_breakReferenceCycleN)r*   r+   r,   r;   rU   r_   rc   rf   rg   ri   rk   rn   ro   rt   rv   r|   r   r   r   r   r   r   r   r   rK      s"   
		rK   c                   @   s   e Zd Zdd Zdd ZdS )rO   c                 C   s   |S r   r   )srT   r/   r   r   r   r{   l  r   zWrappingFactory.<lambda>c                 C   s   t j|  | jd  d S r   )r   rO   startFactorydeferredr    r   r   r   r   r   n  s    zWrappingFactory.startFactoryN)r*   r+   r,   r   r   r   r   r   r   rO   k  s   rO   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 )zQ
        Full test using a custom server limiting number of connections.
        c                 S   s   g | ]
}t  qS r   )r   ).0ir   r   r   
<listcomp>~  s     z.ThrottlingTests.test_limit.<locals>.<listcomp>      r   rM   )Z	interfacec                    sD   t dt   jfdd jfdd jS )NrM   c                    s   t dt S NrM   r   
connectTCPr-   r)c2nr   r   r{     r   zAThrottlingTests.test_limit.<locals>._connect123.<locals>.<lambda>c                    s   t dt S r   r   r   )c3r   r   r   r{     r   )r   r   r-   r   addCallbackr   results)c1r   r   r   r   r   _connect123  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   r     s     zAThrottlingTests.test_limit.<locals>._check123.<locals>.<listcomp>r   c                 S   s   g | ]
}|j qS r   )r"   r   r   r   r   r     s     r   r   )r`   lenZ	protocolskeysr   )r   r   r   r   tServerr   r   	_check123  s    ""z-ThrottlingTests.test_limit.<locals>._check123c                    s    j    jS r   )r8   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   )r8   r   r   ZDeferredListZmaybeDeferredZstopListeningr   )r   r   )r   r   r/   r   r   _cleanup  s    
z,ThrottlingTests.test_limit.<locals>._cleanup)r:   ranger   r>   rO   r   r   r   r   Z	listenTCPrl   portr   )	r   rb   ZwrapTServerr   r   r   r   r   r   r   )r   r   r   r   r   r/   r   r   r   
test_limity  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.
        rL   rM   r      bytesr   s   bytesbytesbytesbytes   N)r:   r<   r   Clockr0   r   rP   r
   r   r[   writeSequencer`   valuewrittenThisSecond)r   rb   r   r   r8   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
writeLimitrL   rM   r   
   0123456789
   abcdefghij   0123456789abcdefghijr   ?N)r:   r<   r   r   r0   r   rP   r
   r   r[   rR   rq   r)   r`   r   r   assertFalser4   r?   advancer\   r   rb   r   r   Z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	readLimitrL   rM   r   r   r   r   r   r   r4   Z	producingN)r:   r<   r   r   r0   r   rP   r
   r   r[   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   t  s
   ;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})
        NrL   rM   90  )r   r   r   ServerFactoryr   rI   r0   r   rP   r
   r[   )r   r?   wrappedFactoryrS   r]   r8   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   rR   r"   r   cancelTimeoutru   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   ru   r   r   r   rR   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   rR   r   r   r\   r"   r   )r   r   r?   wrappedProtor   r   r   test_cancelTimeoutAlreadyCalledK  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   rR   r"   r   r   r   r   "test_cancelTimeoutAlreadyCancelledZ  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   rL   rM   r   N)r   r   r?   r   r   r   rI   rS   r0   r   rP   r]   r
   r8   r[   rR   r   )r   r   r   r   r   setUpo  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_timeout  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]   r9   r   r\   r   r   r   r   test_sendAvoidsTimeout  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   j  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   rZ   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.
        rD   )r   ZtimeoutrG   rA   rB   r   r   r   rE     s    zTimeoutTester.callLater)N)r*   r+   r,   r;   r   r   r   r!   r)   r%   r   rE   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]   r[   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]   r[   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   r[   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   r[   r	   r   ru   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   ru   r   r   r   r   test_setTimeoutReturnJ  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   ru   r   r   r   r   %test_setTimeoutCancleAlreadyCancelledY  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   rZ   r`   connectionCountr0   r%   )r   rS   Zp1Zp2r   r   r   testConnectionCountingm  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 r2   )
overflowedr   rS   r   r   r!     s    zaLimitTotalConnectionsFactoryTests.testConnectionLimiting.<locals>.OverflowProtocol.connectionMadeN)r*   r+   r,   r!   r   r   r   r   OverflowProtocol  s   r   Fr   r   )r   r   r   rZ   ZconnectionLimitr0   r   r`   r   ru   ZoverflowProtocolr   r[   r\   r%   )r   r/   r   opr   r   r   testConnectionLimiting  s&    





z8LimitTotalConnectionsFactoryTests.testConnectionLimitingN)r*   r+   r,   r;   r   r   r   r   r   r   r   k  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!)findr8   r   r1   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f | | | d |  |j }| 	d
| dS )zQ
        Check the output produced by L{policies.TrafficLoggingFactory}.
        test1.2.3.4i.  *   here are some byteszC 1: %r)r   zS 1: %rs"   prepare for vector! to the extremezSV 1: %rZConnectionDoneN)r:   r   r   r
   r   r0   r[   r   getvalueZassertInr   r   r)   r`   r   r   )r   r   trT   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   )r:   r   r`   Z_counterr0   r   ZresetCounter)r   r   rT   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   )rx   r   r   )rA   rB   ioZ
open_callsZopen_rvaluesr   r   mocked_open  s
    

zULoggingFactoryTests.test_loggingFactoryOpensLogfileAutomatically.<locals>.mocked_openr   r   rL   rM   r   i:0  )ztest-1w)ztest-2r  N)Zpatchbuiltinsr   r   r   r   TrafficLoggingFactoryr0   r   rP   r`   Zlogfile)r   r  r   rS   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   )3r;   Z
__future__r   r   Zzope.interfacer   r   r   Ztwisted.python.compatr   Ztwisted.trialr   Ztwisted.test.proto_helpersr	   r
   Ztwisted.internetr   r   r   r   r   Ztwisted.protocolsr   r  ImportErrorZ__builtin__rZ   r   ZClientFactoryr-   r1   r   r:   r>   r<   rJ   rI   ZTestCaserK   rO   r   r   r   ZTimeoutMixinr   r   r   r   r	  r   r   r   r   r   r   <module>   s>   	 j	 ]X6s<	