HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //proc/self/root/lib/python3/dist-packages/twisted/test/__pycache__/test_policies.cpython-38.pyc
U


W[ۂ�@s�dZddlmZmZddlmZmZmZddlm	Z	ddl
mZddlm
Z
ddlmZddlmZmZmZmZmZdd	lmZzdd
lZWnek
r�dd
lZYnXGdd�dej�ZGd
d�dej�ZGdd�dej�ZGdd�dej�Z Gdd�dej!�Z"Gdd�dej#�Z$Gdd�dej%�Z&Gdd�dej'�Z'Gdd�dej%�Z(Gdd�dej%�Z)Gdd �d ej%�Z*Gd!d"�d"ejej+�Z,Gd#d$�d$ej%�Z-Gd%d&�d&ej%�Z.Gd'd(�d(e�Z/Gd)d*�d*ej0�Z1Gd+d,�d,ej%�Z2d
S)-z
Test code for policies.
�)�division�absolute_import)�	Interface�implementer�
implementedBy)�NativeStringIO)�unittest)�StringTransport)� StringTransportWithDisconnection)�protocol�reactor�address�defer�task)�policiesNc@s8eZdZdZZdZdd�Zdd�Zdd�Zd	d
�Z	dS)�SimpleProtocolr�cCst��|_t��|_dS�N)r�Deferred�
dConnected�
dDisconnected��self�r�</usr/lib/python3/dist-packages/twisted/test/test_policies.py�__init__!s
zSimpleProtocol.__init__cCsd|_|j�d�dS�N��)�	connectedr�callbackrrrr�connectionMade%szSimpleProtocol.connectionMadecCsd|_|j�d�dSr)�disconnectedrr �r�reasonrrr�connectionLost)szSimpleProtocol.connectionLostcCs|j|7_dSr)�buffer�r�datarrr�dataReceived-szSimpleProtocol.dataReceivedN)
�__name__�
__module__�__qualname__rr"r&rr!r%r)rrrrrsrc@seZdZdd�Zdd�ZdS)�SillyFactorycCs
||_dSr��p)rr/rrrr4szSillyFactory.__init__cCs|jSrr.)rZaddrrrr�
buildProtocol7szSillyFactory.buildProtocolN)r*r+r,rr0rrrrr-2sr-c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�EchoProtocolFcCs
d|_dS�NT��pausedrrrr�pauseProducing>szEchoProtocol.pauseProducingcCs
d|_dS)NFr3rrrr�resumeProducingAszEchoProtocol.resumeProducingcCsdSrrrrrr�
stopProducingDszEchoProtocol.stopProducingcCs|j�|�dSr)�	transport�writer'rrrr)GszEchoProtocol.dataReceivedN)r*r+r,r4r5r6r7r)rrrrr1;s
r1c@seZdZdZeZdS)�Serverz8
    A simple server factory using L{EchoProtocol}.
    N)r*r+r,�__doc__r1rrrrrr:Lsr:c@s eZdZdZdd�Zdd�ZdS)�TestableThrottlingFactoryzH
    L{policies.ThrottlingFactory} using a L{task.Clock} for tests.
    cOstjj|f|�|�||_dS�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�rr?�args�kwargsrrrrYsz"TestableThrottlingFactory.__init__cCs|j�||�S�z0
        Forward to the testable clock.
        �r?�	callLater�rZperiod�funcrrrrEcsz#TestableThrottlingFactory.callLaterN�r*r+r,r;rrErrrrr<Ts
r<c@s eZdZdZdd�Zdd�ZdS)�TestableTimeoutFactoryzE
    L{policies.TimeoutFactory} using a L{task.Clock} for tests.
    cOstjj|f|�|�||_dSr=)r�TimeoutFactoryrr?r@rrrrpszTestableTimeoutFactory.__init__cCs|j�||�SrCrDrFrrrrEzsz TestableTimeoutFactory.callLaterNrHrrrrrIks
rIc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"S)#�WrapperTestsz>
    Tests for L{WrappingFactory} and L{ProtocolWrapper}.
    cCs8t�}t�|�}|�t�ddd��}|�|jj|�dS)zf
        Make sure protocol.factory is the wrapped factory, not the wrapping
        factory.
        �TCP�	127.0.0.1�#N)	r:r�WrappingFactoryr0r
�IPv4Address�assertIs�wrappedProtocol�factory)r�fZwfr/rrr�test_protocolFactoryAttribute�s
z*WrapperTests.test_protocolFactoryAttributecCslGdd�dt�}t|�Gdd�d��}ttj�t��}t�t�d�|�}|�|��|�	|�
|j��dS)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eZdZdS)z=WrapperTests.test_transportInterfaces.<locals>.IStubTransportN�r*r+r,rrrr�IStubTransport�srWc@seZdZdS)z<WrapperTests.test_transportInterfaces.<locals>.StubTransportNrVrrrr�
StubTransport�srXN)rrrr�ProtocolWrapperr�ProtocolrO�makeConnection�
assertTrueZ
providedByr8)rrWrX�proto�wrapperrrr�test_transportInterfaces�s

z%WrapperTests.test_transportInterfacescCs$t�}t�|�}|�d|���dS)z�
        L{WrappingFactory.logPrefix} is customized to mention both the original
        factory and the wrapping factory.
        zServer (WrappingFactory)N)r:rrO�assertEqual�	logPrefix)r�serverrSrrr�test_factoryLogPrefix�s
z"WrapperTests.test_factoryLogPrefixcCs4Gdd�dt�}|�}t�|�}|�d|���dS)z�
        If the wrapped factory doesn't have a L{logPrefix} method,
        L{WrappingFactory.logPrefix} falls back to the factory class name.
        c@seZdZdS)z=WrapperTests.test_factoryLogPrefixFallback.<locals>.NoFactoryNrVrrrr�	NoFactory�srdzNoFactory (WrappingFactory)N)�objectrrOr`ra)rrdrbrSrrr�test_factoryLogPrefixFallback�s
z*WrapperTests.test_factoryLogPrefixFallbackcCs8t�}t�|�}|�t�ddd��}|�d|���dS)z{
        L{ProtocolWrapper.logPrefix} is customized to mention both the original
        protocol and the wrapper.
        rLrMrNzEchoProtocol (ProtocolWrapper)N)r:rrOr0r
rPr`ra)rrbrSrrrr�test_protocolLogPrefix�s
��z#WrapperTests.test_protocolLogPrefixcCsNGdd�dt�}t�}||_t�|�}|�t�ddd��}|�d|�	��dS)z�
        If the wrapped protocol doesn't have a L{logPrefix} method,
        L{ProtocolWrapper.logPrefix} falls back to the protocol class name.
        c@seZdZdS)z?WrapperTests.test_protocolLogPrefixFallback.<locals>.NoProtocolNrVrrrr�
NoProtocol�srhrLrMrNzNoProtocol (ProtocolWrapper)N)
rer:rrrOr0r
rPr`ra)rrhrbrSrrrr�test_protocolLogPrefixFallback�s
��z+WrapperTests.test_protocolLogPrefixFallbackcCs,t�t�t��t���}t�}|�|�|S)zm
        Return L{policies.ProtocolWrapper} that has been connected to a
        L{StringTransport}.
        )rrYrOr:rrZr	r[�rr^r8rrr�_getWrapper�s�
zWrapperTests._getWrappercCs"|��}|�|��|j���dS)zk
        L{policies.ProtocolWrapper.getHost} calls C{getHost} on the underlying
        transport.
        N)rkr`�getHostr8�rr^rrr�test_getHost�szWrapperTests.test_getHostcCs"|��}|�|��|j���dS)zk
        L{policies.ProtocolWrapper.getPeer} calls C{getPeer} on the underlying
        transport.
        N)rkr`ZgetPeerr8rmrrr�test_getPeer�szWrapperTests.test_getPeercCs<|��}t�}|�|d�|�|jj|�|�|jj�dS)z}
        L{policies.ProtocolWrapper.registerProducer} calls C{registerProducer}
        on the underlying transport.
        TN)rkre�registerProducerrQr8�producerr\�	streaming�rr^rqrrr�test_registerProducers
z"WrapperTests.test_registerProducercCsB|��}t�}|�|d�|��|�|jj�|�|jj�dS)z�
        L{policies.ProtocolWrapper.unregisterProducer} calls
        C{unregisterProducer} on the underlying transport.
        TN)rkrerpZunregisterProducer�assertIsNoner8rqrrrsrrr�test_unregisterProducer
sz$WrapperTests.test_unregisterProducercs6|��}g��fdd�|j_|��|��dg�dS)zw
        L{policies.ProtocolWrapper.stopConsuming} calls C{stopConsuming} on
        the underlying transport.
        cs
��d�Sr2��appendr��resultrr�<lambda>!rz1WrapperTests.test_stopConsuming.<locals>.<lambda>TN)rkr8Z
stopConsumingr`rmrryr�test_stopConsumings
zWrapperTests.test_stopConsumingcsFg�G�fdd�dt�}t�|��}t�}|�|�|��|g�dS)z}
        L{policies.WrappingFactory.startedConnecting} calls
        C{startedConnecting} on the underlying factory.
        cseZdZ�fdd�ZdS)z4WrapperTests.test_startedConnecting.<locals>.Factorycs��|�dSrrw)r�	connectorryrr�startedConnecting-szFWrapperTests.test_startedConnecting.<locals>.Factory.startedConnectingN)r*r+r,r~rryrr�Factory,srN)rerrOr~r`)rrr^r}rryr�test_startedConnecting&s
z#WrapperTests.test_startedConnectingcsRg�G�fdd�dt�}t�|��}t�}t�}|�||�|��||fg�dS)z�
        L{policies.WrappingFactory.clientConnectionLost} calls
        C{clientConnectionLost} on the underlying factory.
        cseZdZ�fdd�ZdS)z7WrapperTests.test_clientConnectionLost.<locals>.Factorycs��||f�dSrrw�rr}r$ryrr�clientConnectionLost=szLWrapperTests.test_clientConnectionLost.<locals>.Factory.clientConnectionLostN)r*r+r,r�rryrrr<srN)rerrOr�r`�rrr^r}r$rryr�test_clientConnectionLost6sz&WrapperTests.test_clientConnectionLostcsRg�G�fdd�dt�}t�|��}t�}t�}|�||�|��||fg�dS)z�
        L{policies.WrappingFactory.clientConnectionFailed} calls
        C{clientConnectionFailed} on the underlying factory.
        cseZdZ�fdd�ZdS)z9WrapperTests.test_clientConnectionFailed.<locals>.Factorycs��||f�dSrrwr�ryrr�clientConnectionFailedNszPWrapperTests.test_clientConnectionFailed.<locals>.Factory.clientConnectionFailedN)r*r+r,r�rryrrrMsrN)rerrOr�r`r�rryr�test_clientConnectionFailedGsz(WrapperTests.test_clientConnectionFailedcCsRt�t�t��t���}t�}||_|�|�|�|j	�|�
�|�|j	�dS)z�
        L{policies.ProtocolWrapper.connectionLost} sets C{wrappedProtocol} to
        C{None} in order to break reference cycle between wrapper and wrapped
        protocols.
        :return:
        N)rrYrOr:rrZr
r[�assertIsNotNonerR�loseConnectionrurjrrr�test_breakReferenceCycleXs�
z%WrapperTests.test_breakReferenceCycleN)r*r+r,r;rUr_rcrfrgrirkrnrortrvr|r�r�r�r�rrrrrK�s"


		
rKc@seZdZdd�Zdd�ZdS)rOcCs|Srr)�srTr/rrrr{lrzWrappingFactory.<lambda>cCstj�|�|j�d�dSr)rrO�startFactory�deferredr rrrrr�nszWrappingFactory.startFactoryN)r*r+r,rr�rrrrrOksrOc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�ThrottlingTestsz2
    Tests for L{policies.ThrottlingFactory}.
    c	st�}dd�td�D�\����t�|d��t��}t��|_tj	d|dd����
�j�����fdd	�}�����fd
d�}�fdd
�}��fdd�}��fdd�}���fdd�}|j�|�|j�|�|j�|�|j�|�|j�|�|j�|�|jS)zQ
        Full test using a custom server limiting number of connections.
        cSsg|]
}t��qSr)r)�.0�irrr�
<listcomp>~sz.ThrottlingTests.test_limit.<locals>.<listcomp>��rrM)Z	interfacecsDt�d�t����j���fdd���j���fdd���jS)NrMcst�d�t���S�NrM�r�
connectTCPr-��r)�c2�nrrr{�rzAThrottlingTests.test_limit.<locals>._connect123.<locals>.<lambda>cst�d�t���Sr�r�r�)�c3r�rrr{�r)rr�r-r�addCallbackr��results)�c1r�r�r�rr�_connect123�s��z/ThrottlingTests.test_limit.<locals>._connect123cs^��dd����fD�dddg���dd����fD�dddg���t�j���d�|S)NcSsg|]
}|j�qSr)r�r��crrrr��szAThrottlingTests.test_limit.<locals>._check123.<locals>.<listcomp>rcSsg|]
}|j�qSr)r"r�rrrr��srr�)r`�lenZ	protocols�keysr�)r�r�r�r�tServerrr�	_check123�s""z-ThrottlingTests.test_limit.<locals>._check123cs�j���jSr)r8r�rr�)r�rr�_lose1�s
z*ThrottlingTests.test_limit.<locals>._lose1cst�d�t����jSr�)rr�r-rr�)�c4r�rr�	_connect4�sz-ThrottlingTests.test_limit.<locals>._connect4cs ���jd����jd�|S)Nrr)r`rr"r�)r�rrr�_check4�sz+ThrottlingTests.test_limit.<locals>._check4cs4��fD]}|j��qt�t��j��j�jg�Sr)r8r�rZDeferredListZ
maybeDeferredZ
stopListeningr)r�r�)r�r�r/rr�_cleanup�s
�z,ThrottlingTests.test_limit.<locals>._cleanup)
r:�rangerr>rOrrr�rZ	listenTCPrl�portr�)	rrbZwrapTServerr�r�r�r�r�r�r)r�r�r�r�r�r/rr�r�
test_limitys(

zThrottlingTests.test_limitcCspt�}tt��|�}|�t�ddd��}t�}||_|�	|�|�
dgd�|�|��d�|�|j
d�dS)	zZ
        L{ThrottlingProtocol.writeSequence} is called on the underlying factory.
        rLrMr�bytesr�sbytesbytesbytesbytes�N)r:r<r�Clockr0r
rPr
rr[�
writeSequencer`�value�writtenThisSecond)rrbr�rr8rrr�test_writeSequence�s�
z"ThrottlingTests.test_writeSequencecCs�t�}tt��|dd�}|�t�ddd��}t�}||_|�	|�|j
|_|�d�|�d�|�
|��d�|�
|jd	�|�|j
j�|j�d
�|�
|jd�|�|j
j�|j�d
�|�
|jd�|�|j
j�dS)ze
        Check the writeLimit parameter: write data, and check for the pause
        status.
        �
)Z
writeLimitrLrMr�
0123456789�
abcdefghij�0123456789abcdefghijr���������?N)r:r<rr�r0r
rPr
rr[rRrqr)r`r�r��assertFalser4r?�advancer\�rrbr�r�Ztrrrr�test_writeLimit�s$


zThrottlingTests.test_writeLimitcCsRt�}tt��|dd�}|�t�ddd��}t�}||_|�	|�|�
d�|�
d�|�|��d�|�|j
d	�|j�d
�|�|j
d�|�|jd�|j�d
�|�|j
d�|�|jd�|��|�
d�|�
d�|�|��d�|�|j
d	�|j�d
�|�|j
d�|�|jd�|j�d
�|�|j
d�|�|jd�d
S)zb
        Check the readLimit parameter: read data and check for the pause
        status.
        r�)Z	readLimitrLrMrr�r�r�r�r�r4Z	producingN)r:r<rr�r0r
rPr
rr[r)r`r�ZreadThisSecondr?r�Z
producerState�clearr�rrr�test_readLimit�s6




zThrottlingTests.test_readLimitN)r*r+r,r;r�r�r�r�rrrrr�ts
;r�c@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�TimeoutProtocolTestsz0
    Tests for L{policies.TimeoutProtocol}.
    cCsTt��}t��}t|_t||d�}|�t�ddd��}t	�}||_|�
|�||fS)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})
        NrLrM�90)rr�r�
ServerFactoryrrIr0r
rPr
r[)rr?�wrappedFactoryrSr]r8rrr�getProtocolAndClocks�
z(TimeoutProtocolTests.getProtocolAndClockcCsj|��\}}|�d�|�|j�|�|jj�|�d�|��|�	|j�|�d�|�|jj�dS)z2
        Will cancel the ongoing timeout.
        �rN)
r��
setTimeoutr��timeoutCallr�rRr"r��
cancelTimeoutru�r�sutr?rrr�test_cancelTimeout*s


z'TimeoutProtocolTests.test_cancelTimeoutcCs2|��\}}|�|j�|��|�|jj�dS)z<
        Does nothing if no timeout is already set.
        N)r�rur�r�r�rRr"r�rrr�test_cancelTimeoutNoTimeout>sz0TimeoutProtocolTests.test_cancelTimeoutNoTimeoutcCs>|��\}}|j}|�d�|�d�|�|j�|��dS)z@
        Does nothing if no timeout is already reached.
        r�N)r�rRr�r�r\r"r�)rr�r?�wrappedProtorrr�test_cancelTimeoutAlreadyCalledKs

z4TimeoutProtocolTests.test_cancelTimeoutAlreadyCalledcCs:|��\}}|�d�|j��|��|�|jj�dS)zm
        Does nothing if the timeout is cancelled from another part.
        Ex from another thread.
        r�N)r�r�r��cancelr�r�rRr"r�rrr�"test_cancelTimeoutAlreadyCancelledZs


z7TimeoutProtocolTests.test_cancelTimeoutAlreadyCancelledN)	r*r+r,r;r�r�r�r�r�rrrrr�
s
r�c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�TimeoutFactoryTestsz/
    Tests for L{policies.TimeoutFactory}.
    cCsnt��|_t��}t|_t|j|d�|_|j�t	�
ddd��|_t�|_
|j|j
_|j�|j
�|jj|_dS)zq
        Create a testable, deterministic clock, and a set of
        server factory/protocol/transport.
        r�rLrMr�N)rr�r?rr�rrIrSr0r
rPr]r
r8r[rRr�)rr�rrr�setUpos
�
zTimeoutFactoryTests.setUpcCsF|j�dddddg�|�|jj�|j�ddg�|�|jj�dS)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\rrrr�test_timeout�sz TimeoutFactoryTests.test_timeoutcCs�|j�dddg�|�|jj�|j�d�|j�dddg�|�|jj�|j�dgd�|j�dddg�|�|jj�|j�ddg�|�|jj�dS)	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]r9r�r\rrrr�test_sendAvoidsTimeout�sz*TimeoutFactoryTests.test_sendAvoidsTimeoutcCsp|j�dddg�|�|jj�|j�d�|j�dddg�|�|jj�|j�dddg�|�|jj�dS)zR
        Make sure that receiving data also resets the timeout countdown.
        r�r�r�r�N)r?r�r�r�r"r]r)r\rrrr�test_receiveAvoidsTimeout�sz-TimeoutFactoryTests.test_receiveAvoidsTimeoutN)r*r+r,r;r�r�r�r�rrrrr�js
r�c@sJeZdZdZdZdZdd�Zdd�Zdd	�Zddd�Z	d
d�Z
dd�Zd
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�FcCs
||_dS)zF
        Initialize the protocol with a C{task.Clock} object.
        N)r?)rr?rrrr�szTimeoutTester.__init__cCs|�|j�dS)z3
        Upon connection, set the timeout.
        N)r��timeOutrrrrr!�szTimeoutTester.connectionMadecCs|��tj�||�dS)z,
        Reset the timeout on data.
        N)ZresetTimeoutrrZr)r'rrrr)�szTimeoutTester.dataReceivedNcCs|�d�dS)zD
        On connection lost, cancel all timeout operations.
        N)r�r#rrrr%�szTimeoutTester.connectionLostcCs
d|_dS)zX
        Flags the timedOut variable to indicate the timeout of the connection.
        TN)�timedOutrrrr�timeoutConnection�szTimeoutTester.timeoutConnectioncOs|jj||f|�|�S)zD
        Override callLater to use the deterministic clock.
        rD)rZtimeoutrGrArBrrrrE�szTimeoutTester.callLater)N)r*r+r,r;r�r�rr!r)r%r�rErrrrr��s
r�c@sPeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�ZdS)�TimeoutMixinTestsz-
    Tests for L{policies.TimeoutMixin}.
    cCst��|_t|j�|_dS)zY
        Create a testable, deterministic clock and a C{TimeoutTester} instance.
        N)rr�r?r�r]rrrrr��s
zTimeoutMixinTests.setUpcCs$|j�d�|�t|jj�d�dS)z�
        Test that the callLater of the clock is used instead of
        L{reactor.callLater<twisted.internet.interfaces.IReactorTime.callLater>}
        r�rN)r]r�r`r�r?Zcallsrrrr�test_overriddenCallLatersz*TimeoutMixinTests.test_overriddenCallLatercCsR|j�t��|j�ddddg�|�|jj�|j�ddg�|�|jj�dS)zq
        Check that the protocol does timeout at the time specified by its
        C{timeOut} attribute.
        rr�r�N)r]r[r	r?r�r�r�r\rrrrr�s
zTimeoutMixinTests.test_timeoutcCs�|j�t��|j�ddddg�|�|jj�|j�d�|j�ddddg�|�|jj�|j�ddg�|�|jj�dS)zV
        Check that receiving data is delaying the timeout of the connection.
        rr�r�shello thereN)	r]r[r	r?r�r�r�r)r\rrrr�test_noTimeoutsz TimeoutMixinTests.test_noTimeoutcCsrd|j_|j�t��|j�d�|�|jjd�|j�ddg�|�|jj	�|j�ddg�|�
|jj	�dS)zy
        Check that setting a new value for timeout cancel the previous value
        and install a new timeout.
        Nrrg�������?r�)r]r�r[r	r�r`r?r�r�r�r\rrrr�test_resetTimeout+sz#TimeoutMixinTests.test_resetTimeoutcCsVd|j_|j�t��|j�d�|�|jj�|j�ddddg�|�|jj	�dS)zO
        Setting the timeout to L{None} cancel any timeout operations.
        �Nr)
r]r�r[r	r�rur?r�r�r�rrrrr�<sz$TimeoutMixinTests.test_cancelTimeoutcCsbd|j_|�|j�d�d�|�|j�d�d�|�|j�d��|�|jjd�|j�d�dS)zM
        setTimeout should return the value of the previous timeout.
        r�r�Nr)r]r�r`r�rurrrr�test_setTimeoutReturnJsz'TimeoutMixinTests.test_setTimeoutReturncCsJ|j�d�|j��d��|�|jj�|j�d�|�|jj�dS)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�rN)r]r�r?ZgetDelayedCallsr�r�r�rurrrr�%test_setTimeoutCancleAlreadyCancelledYs
z7TimeoutMixinTests.test_setTimeoutCancleAlreadyCancelledN)r*r+r,r;r�r�r�r�r�r�r�r�rrrrr��s	r�c@s eZdZdZdd�Zdd�ZdS)�!LimitTotalConnectionsFactoryTestsz/Tests for policies.LimitTotalConnectionsFactorycCs�t��}tj|_|�d|j�|�d�}|�d|j�|�d�}|�d|j�|�d�|�d|j�|�d�|�d|j�dS)Nrrr�)r�LimitTotalConnectionsFactoryrrZr`�connectionCountr0r%)rrSZp1Zp2rrr�testConnectionCountingms



z8LimitTotalConnectionsFactoryTests.testConnectionCountingcs�t���tj�_d�_��d�}|�|�|�d�j�|�	��d��|�d�j�G�fdd�dtj�}|�_
d�_��d�}|�d�|�
�j�|�d�j�|�d�|�d�j�|�d�|�d�j�dS)NrcseZdZ�fdd�ZdS)zRLimitTotalConnectionsFactoryTests.testConnectionLimiting.<locals>.OverflowProtocolcs
d�_dSr2)�
overflowedr�rSrrr!�szaLimitTotalConnectionsFactoryTests.testConnectionLimiting.<locals>.OverflowProtocol.connectionMadeN)r*r+r,r!rr�rr�OverflowProtocol�sr�Fr�r)rr�rrZZconnectionLimitr0r�r`r�ruZoverflowProtocolr�r[r\r%)rr/r��oprr�r�testConnectionLimiting�s&





z8LimitTotalConnectionsFactoryTests.testConnectionLimitingN)r*r+r,r;r�r�rrrrr�ksr�c@seZdZdd�ZdS)�WriteSequenceEchoProtocolcCs.|�d�dkr|j�|g�nt�||�dS)Nsvector!���)�findr8r�r1r))r�bytesrrrr)�sz&WriteSequenceEchoProtocol.dataReceivedN)r*r+r,r)rrrrr��sr�c@seZdZdZdd�ZdS)�TestLoggingFactoryNcCs |jdkstd��t�|_|jS)Nzopen() called too many times)�openFile�AssertionErrorr)r�namerrr�open�szTestLoggingFactory.open)r*r+r,r�r�rrrrr��sr�c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�LoggingFactoryTestsz6
    Tests for L{policies.TrafficLoggingFactory}.
    cCst�}t|_t�}t|d�}|�d�}||_|�|�|j��}|�	d|�|�
|���|�d�|j��}|�	dd|�|�	dd|�|�
|��d�|��|�d�|j��}|�	d	dgf|�|�
|��d�|��|j��}|�	d
|�dS)zQ
        Check the output produced by L{policies.TrafficLoggingFactory}.
        �test��1.2.3.4i.�*�here are some byteszC 1: %r)r�zS 1: %rs"prepare for vector! to the extremezSV 1: %rZConnectionDoneN)r:r�rr
r�r0r[r��getvalueZassertInr�r�r)r`r�r�)rr��trTr/�vrrr�test_thingsGetLogged�s.








z(LoggingFactoryTests.test_thingsGetLoggedcCsnt�}t|d�}|�|jd�|�d�|�|jd�d|_|�d�|�|jd�|��|�|jd�dS)zG
        Test counter management with the resetCounter method.
        r�rr�rN)r�i/r�)r:r�r`Z_counterr0r�ZresetCounter)rr�rTrrr�test_counter�s


z LoggingFactoryTests.test_countercs�g�g���fdd�}|�td|�t��}t|_t�|d�}|�t�	ddd��}|�t�	ddd��}d	if}d
if}|�
||g��|�
|j|jg��dS)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.
        cs,��||f�t�}|d|_��|�|S)zX
            Mock for the open call to prevent actually opening a log file.
            r)rxrr�)rArB�io�Z
open_callsZopen_rvaluesrr�mocked_open�s


zULoggingFactoryTests.test_loggingFactoryOpensLogfileAutomatically.<locals>.mocked_openr�r�rLrMr�i:0)ztest-1�w)ztest-2rN)Zpatch�builtinsrr�rr�TrafficLoggingFactoryr0r
rPr`Zlogfile)rrr�rSZfirst_protoZsecond_protoZ
first_callZsecond_callrrr�,test_loggingFactoryOpensLogfileAutomatically�s,

�
�
�z@LoggingFactoryTests.test_loggingFactoryOpensLogfileAutomaticallyN)r*r+r,r;rrr
rrrrr��s#r�)3r;Z
__future__rrZzope.interfacerrrZtwisted.python.compatrZ
twisted.trialrZtwisted.test.proto_helpersr	r
Ztwisted.internetrrr
rrZtwisted.protocolsrr�ImportErrorZ__builtin__rZrZ
ClientFactoryr-r1r�r:r>r<rJrIZTestCaserKrOr�r�r�ZTimeoutMixinr�r�r�r�r	r�r�rrrr�<module>s>	j	]X6s<