o
    Óó·i�  ã                   @   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   ú`C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-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   ÚpauseProducing3   r2   zEchoProtocol.pauseProducingc                 C   r8   )NFr:   r   r   r   r   ÚresumeProducing6   r2   zEchoProtocol.resumeProducingc                 C   s   d S r   r   r   r   r   r   ÚstopProducing9   ó   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   0   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   @   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   M   ó   
z"TestableThrottlingFactory.__init__c                 C   ó   | j  ||¡S ©z0
        Forward to the testable clock.
        N©rI   Ú	callLater©r   ZperiodÚfuncr   r   r   rQ   V   ó   z#TestableThrottlingFactory.callLaterN©r+   r,   r-   rC   r   rQ   r   r   r   r   rE   H   ó    	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   b   rM   zTestableTimeoutFactory.__init__c                 C   rN   rO   rP   rR   r   r   r   rQ   k   rT   z TestableTimeoutFactory.callLaterNrU   r   r   r   r   rW   ]   rV   rW   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   ÚfZwfr1   r   r   r   Útest_protocolFactoryAttributew   ó   
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ˆ   ó    rg   c                   @   re   )z<WrapperTests.test_transportInterfaces.<locals>.StubTransportNrf   r   r   r   r   ÚStubTransport‹   s    ri   N)r   r   r   r   ÚProtocolWrapperr   ÚProtocolr]   ÚmakeConnectionÚ
assertTrueZ
providedByr@   )r   rg   ri   Ú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   Úserverra   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                   @   re   )z=WrapperTests.test_factoryLogPrefixFallback.<locals>.NoFactoryNrf   r   r   r   r   Ú	NoFactory­   rh   ru   zNoFactory (WrappingFactory)N)r   r]   rq   rr   )r   ru   rs   ra   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.
        rZ   r[   r\   zEchoProtocol (ProtocolWrapper)N)rB   r   r]   r4   r   r^   rq   rr   )r   rs   ra   r   r   r   r   Útest_protocolLogPrefix´   rd   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                   @   re   )z?WrapperTests.test_protocolLogPrefixFallback.<locals>.NoProtocolNrf   r   r   r   r   Ú
NoProtocolÄ   rh   rx   rZ   r[   r\   zNoProtocol (ProtocolWrapper)N)	rB   r   r   r]   r4   r   r^   rq   rr   )r   rx   rs   ra   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}.
        N)r   rj   r]   rB   r   rk   r   rl   ©r   ro   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{   rq   ÚgetHostr@   ©r   ro   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{   rq   Z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@   Úproducerrm   Ú	streaming©r   ro   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ƒ   Z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@   ZstopConsumingrq   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’   rq   )r   r“   ro   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™   rq   ©r   r“   ro   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   ÚclientConnectionFailed4  r*   zPWrapperTests.test_clientConnectionFailed.<locals>.Factory.clientConnectionFailedN)r+   r,   r-   r�   r   rŒ   r   r   r“   3  r”   r“   N)r   r]   r‚   r�   rq   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.
        N)r   rj   r]   rB   r   rk   r   rl   ÚassertIsNotNoner`   ÚloseConnectionrˆ   rz   r   r   r   Útest_breakReferenceCycle=  s   ÿ
z%WrapperTests.test_breakReferenceCycleN)r+   r,   r-   rC   rc   rp   rt   rv   rw   ry   r{   r   r�   r‡   r‰   r�   r•   r›   rž   r¡   r   r   r   r   rY   r   s$    
	
rY   c                   @   r.   )r]   c                 C   s   |S r   r   )r   rb   r1   r   r   r   r   P  r?   zWrappingFactory.protocolc                 C   s   t j | ¡ | j d ¡ d S r   )r   r]   ÚstartFactoryÚdeferredr   r   r   r   r   r¢   S  s   zWrappingFactory.startFactoryN)r+   r,   r-   r   r¢   r   r   r   r   r]   O  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>e  s   € z-ThrottlingTests.test_limit.<locals>.<genexpr>é   é   r   r[   )Z	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Ž   q  ó    zAThrottlingTests.test_limit.<locals>._connect123.<locals>.<lambda>c                    rª   r«   r¬   r®   )Úc3r±   r   r   rŽ   t  r²   )r	   r­   r/   r   ÚaddCallbackr   ©Úresults)Úc1r°   r³   r±   r   r   Ú_connect123n  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>y  r²   zAThrottlingTests.test_limit.<locals>._check123.<locals>.<listcomp>)r   r   r   c                 S   r¹   r   )r"   rº   r   r   r   r¼   z  r²   )r   r   r   r©   )rq   ÚlenÚ	protocolsÚkeysrµ   )r·   r°   r³   r   ÚtServerr   r   Ú	_check123x  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   )rq   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   ZDeferredListZmaybeDeferredÚstopListeningr   )r¶   r»   )r°   rÃ   r1   r   r   Ú_cleanupŒ  s   
ýÿz,ThrottlingTests.test_limit.<locals>._cleanupN)rB   Úranger   rH   r]   r   r   r£   r	   Z	listenTCPr}   Úportr´   )	r   rs   Z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.
        rZ   r[   r   ó   bytesr¨   s   bytesbytesbytesbytesé   N)rB   rE   r
   ÚClockr4   r   r^   r   r   rl   ÚwriteSequencerq   ÚvalueÚwrittenThisSecond)r   rs   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.
        é
   )Z
writeLimitrZ   r[   r   ó
   0123456789ó
   abcdefghijó   0123456789abcdefghijrÌ   çÍÌÌÌÌÌð?N)rB   rE   r
   rÍ   r4   r   r^   r   r   rl   r`   r„   r)   rq   rÏ   rÐ   ÚassertFalser;   rI   Úadvancerm   ©r   rs   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	readLimitrZ   r[   r   rÓ   rÔ   rÕ   rÌ   rÖ   r;   Z	producingN)rB   rE   r
   rÍ   r4   r   r^   r   r   rl   r)   rq   rÏ   ZreadThisSecondrI   rØ   Z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¤   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})
        NrZ   r[   é90  )r
   rÍ   r   ÚServerFactoryr   rW   r4   r   r^   r   rl   )r   rI   ÚwrappedFactoryra   rn   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×   r`   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×   r`   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â   r`   rä   rØ   rm   r"   ræ   )r   rè   rI   Ú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×   r`   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ã   rZ   r[   rß   N)r
   rÍ   rI   r   rà   r   rW   ra   r4   r   r^   rn   r   r@   rl   r`   rë   )r   rá   r   r   r   ÚsetUpQ  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"   rm   r   r   r   r   Útest_timeoutb  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"   rn   rA   rÎ   rm   r   r   r   r   Útest_sendAvoidsTimeoutp  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"   rn   r)   rm   r   r   r   r   Útest_receiveAvoidsTimeoutŒ  s   z-TimeoutFactoryTests.test_receiveAvoidsTimeoutN)r+   r,   r-   rC   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)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)ZresetTimeoutr   rk   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%   ¿  rT   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.
        NrP   )r   ÚtimeoutrS   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û   rn   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)rn   rä   rq   r½   rI   Z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)rn   rl   r   rI   rõ   r×   rþ   rm   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)	rn   rl   r   rI   rõ   r×   rþ   r)   rm   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ô   )rn   rý   rl   r   rä   rq   rI   rõ   r×   rþ   rm   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  )
rn   rý   rl   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   )rn   rý   rq   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)rn   rä   rI   Z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   rk   rq   ÚconnectionCountr4   r%   )r   ra   Ú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                       r�   )zRLimitTotalConnectionsFactoryTests.testConnectionLimiting.<locals>.OverflowProtocolc                    s
   dˆ _ d S r9   )Ú
overflowedr   ©ra   r   r   r    f  r2   zaLimitTotalConnectionsFactoryTests.testConnectionLimiting.<locals>.OverflowProtocol.connectionMadeN)r+   r,   r-   r    r   r  r   r   ÚOverflowProtocole  r”   r  Fr©   r   )r   r
  r   rk   ZconnectionLimitr4   rŸ   rq   r  rˆ   ZoverflowProtocolr  rl   rm   r%   )r   r1   r  Úopr   r  r   ÚtestConnectionLimitingT  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  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)rB   r  r   r   r  r4   rl   r  ÚgetvalueZassertInr×   rÏ   r)   Úformatrq   rÜ   r    )r   rá   Útrb   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.
        r  r   r  r   N)r  i/  r©   )rB   r  rq   Ú_counterr4   r  ZresetCounter)r   rá   rb   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   N)r‹   r   r  )rK   rL   Úio©Z
open_callsZopen_rvaluesr   r   Úmocked_openÍ  s
   

zULoggingFactoryTests.test_loggingFactoryOpensLogfileAutomatically.<locals>.mocked_openr  r  rZ   r[   rß   i:0  )ztest-1Úw)ztest-2r+  N)ÚpatchÚbuiltinsr   rà   r   r   ÚTrafficLoggingFactoryr4   r   r^   rq   Úlogfile)r   r*  rá   ra   Zfirst_protoZsecond_protoZ
first_callZsecond_callr   r)  r   Ú,test_loggingFactoryOpensLogfileAutomaticallyÄ  s"   
ÿÿz@LoggingFactoryTests.test_loggingFactoryOpensLogfileAutomaticallyN)r+   r,   r-   rC   r%  r'  r0  r   r   r   r   r  Œ  s
    "r  ).rC   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   rk   r   ZClientFactoryr/   r7   rà   rB   rH   rE   rX   rW   ZTestCaserY   r]   r¤   rÞ   rï   ZTimeoutMixinrû   r  r	  r  r.  r  r  r   r   r   r   Ú<module>   s6    ^	 XU1k>	