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: //usr/lib/python3/dist-packages/twisted/application/test/__pycache__/test_internet.cpython-38.pyc
U


W[d��@s�dZddlmZmZddlZddlmZddlmZddl	m
Z
mZddlm
Z
ddlmZmZdd	lmZdd
lmZmZmZddlmZmZddlmZmZmZmZmZdd
l m!Z!ddl"m#Z#ddl$m%Z%m&Z&ddl'm(Z(dd�Z)ee�Gdd�de*��Z+eee+�ee�Gdd�de*��Z,eee+�Gdd�de�Z-Gdd�de�Z.Gdd�de*�Z/d%dd�Z0e%fd d!�Z1d"Z2Gd#d$�d$e�Z3dS)&z�
Tests for (new code in) L{twisted.application.internet}.

@var AT_LEAST_ONE_ATTEMPT: At least enough seconds for L{ClientService} to make
    one attempt.
�)�absolute_import�divisionN)�implementer)�verifyClass)�Factory�Protocol)�Clock)�TestCase�SynchronousTestCase)�internet)�StreamServerEndpointService�TimerService�
ClientService)�Deferred�CancelledError)�IStreamServerEndpoint�IStreamClientEndpoint�IListeningPort�IHalfCloseableProtocol�IFileDescriptorReceiver)�task)�Failure)�globalLogPublisher�formatEvent)�StringTransportcCsdS)zM
    A fake target function for testing TimerService which does nothing.
    N�rrr�H/usr/lib/python3/dist-packages/twisted/application/test/test_internet.py�fakeTargetFunction$src@sFeZdZdZdZdZdZe�ZdZ	dd�Z
dd�Zdd	�Zd
d�Z
dS)�
FakeServeraq
    In-memory implementation of L{IStreamServerEndpoint}.

    @ivar result: The L{Deferred} resulting from the call to C{listen}, after
        C{listen} has been called.

    @ivar factory: The factory passed to C{listen}.

    @ivar cancelException: The exception to errback C{self.result} when it is
        cancelled.

    @ivar port: The L{IListeningPort} which C{listen}'s L{Deferred} will fire
        with.

    @ivar listenAttempts: The number of times C{listen} has been invoked.

    @ivar failImmediately: If set, the exception to fail the L{Deferred}
        returned from C{listen} before it is returned.
    NrcCst�|_dS�N)�FakePort�port��selfrrr�__init__GszFakeServer.__init__csF�jd7_|�_t�fdd�d��_�jdk	r@�j��j��jS)z�
        Return a Deferred and store it for future use.  (Implementation of
        L{IStreamServerEndpoint}).

        @param factory: the factory to listen with

        @return: a L{Deferred} stored in L{FakeServer.result}
        �cs|��j�Sr)�errback�cancelException)�dr"rr�<lambda>W�z#FakeServer.listen.<locals>.<lambda>)Z	cancellerN)�listenAttempts�factoryr�result�failImmediatelyr&)r#r,rr"r�listenKs	
�
zFakeServer.listencCs|j�|j�dS)z�
        Test code should invoke this method after causing C{listen} to be
        invoked in order to fire the L{Deferred} previously returned from
        C{listen}.
        N)r-�callbackr!r"rrr�startedListening]szFakeServer.startedListeningcCs|jj�d�dS)a
        Test code should invoke this method after causing C{stopListening} to
        be invoked on the port fired from the L{Deferred} returned from
        C{listen} in order to cause the L{Deferred} returned from
        C{stopListening} to fire.
        N)r!�deferredr0r"rrr�stoppedListeningfszFakeServer.stoppedListening)�__name__�
__module__�__qualname__�__doc__r-r,r.rr'r+r$r/r1r3rrrrr,s	rc@seZdZdZdZdd�ZdS)r zs
    Fake L{IListeningPort} implementation.

    @ivar deferred: The L{Deferred} returned by C{stopListening}.
    NcCst�|_|jS)z`
        Stop listening.

        @return: a L{Deferred} stored in L{FakePort.deferred}
        )rr2r"rrr�
stopListening|szFakePort.stopListening)r4r5r6r7r2r8rrrrr ssr c@sreZdZdZdd�Zdd�Zddd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�ZdS)�EndpointServiceTestsz4
    Tests for L{twisted.application.internet}.
    cCs$t�|_t�|_t|j|j�|_dS)zp
        Construct a stub server, a stub factory, and a
        L{StreamServerEndpointService} to test.
        N)r�
fakeServerrr,r�svcr"rrr�setUp�szEndpointServiceTests.setUpcCs |j��|�|j|jj�dS)z�
        L{StreamServerEndpointService.privilegedStartService} calls its
        endpoint's C{listen} method with its factory.
        N)r;�privilegedStartService�assertIdenticalr,r:r"rrr�test_privilegedStartService�s
z0EndpointServiceTests.test_privilegedStartServiceNcCs*t�|j_d|j_|�t|p"|jj�dS)a@
        L{StreamServerEndpointService.startService} should raise synchronously
        if the L{Deferred} returned by its wrapped
        L{IStreamServerEndpoint.listen} has already fired with an errback and
        the L{StreamServerEndpointService}'s C{_raiseSynchronously} flag has
        been set.  This feature is necessary to preserve compatibility with old
        behavior of L{twisted.internet.strports.service}, which is to return a
        service which synchronously raises an exception from C{startService}
        (so that, among other things, twistd will not start running).  However,
        since L{IStreamServerEndpoint.listen} may fail asynchronously, it is a
        bad idea to rely on this behavior.

        @param thunk: If specified, a callable to execute in place of
            C{startService}.
        TN)�ZeroDivisionErrorr:r.r;�_raiseSynchronouslyZassertRaises�startService)r#Zthunkrrr�(test_synchronousRaiseRaisesSynchronously�s
z=EndpointServiceTests.test_synchronousRaiseRaisesSynchronouslycCs|�|jj�dS)z�
        L{StreamServerEndpointService.privilegedStartService} should behave the
        same as C{startService} with respect to
        L{EndpointServiceTests.test_synchronousRaiseRaisesSynchronously}.
        N)rCr;r=r"rrr�test_synchronousRaisePrivileged�s�z4EndpointServiceTests.test_synchronousRaisePrivilegedcCs8|j��|jj�t��|�t�}|�t|�d�dS)a
        L{StreamServerEndpointService.startService} and
        L{StreamServerEndpointService.privilegedStartService} should both log
        an exception when the L{Deferred} returned from their wrapped
        L{IStreamServerEndpoint.listen} fails.
        r%N)	r;rBr:r-r&r@�flushLoggedErrors�assertEqual�len�r#Zloggedrrr�test_failReportsError�s

z*EndpointServiceTests.test_failReportsErrorcCs@d|j_|j��|jj�t��|�t�}|�t	|�d�dS)aB
        L{StreamServerEndpointService.startService} and
        L{StreamServerEndpointService.privilegedStartService} should both log
        an exception when the L{Deferred} returned from their wrapped
        L{IStreamServerEndpoint.listen} fails asynchronously, even if
        C{_raiseSynchronously} is set.
        Tr%N)
r;rArBr:r-r&r@rErFrGrHrrr�!test_asynchronousFailReportsError�s


z6EndpointServiceTests.test_asynchronousFailReportsErrorcCs2t�|j_|j��|�t�}|�t|�d�dS)z�
        Without the C{_raiseSynchronously} compatibility flag, failing
        immediately has the same behavior as failing later; it logs the error.
        r%N)r@r:r.r;rBrErFrGrHrrr� test_synchronousFailReportsError�s


z5EndpointServiceTests.test_synchronousFailReportsErrorcCs0|j��|�|j|jj�|�|jjd�dS)z�
        L{StreamServerEndpointService.startService} sets the C{running} flag,
        and calls its endpoint's C{listen} method with its factory, if it
        has not yet been started.
        TN)r;rBr>r,r:rF�runningr"rrr�test_startServiceUnstarted�s
z/EndpointServiceTests.test_startServiceUnstartedcCs6|��|j��|�|jjd�|�|jjd�dS)z�
        L{StreamServerEndpointService.startService} sets the C{running} flag,
        but nothing else, if the service has already been started.
        r%TN)r?r;rBrFr:r+rLr"rrr�test_startServiceStarted�s
z-EndpointServiceTests.test_startServiceStartedcCst|j��|j��|j��|j��}g}|�|j�|�t	|�d�|j�
�|�t	|�d�|�|jj�dS)z�
        L{StreamServerEndpointService.stopService} calls C{stopListening} on
        the L{IListeningPort} returned from its endpoint, returns the
        C{Deferred} from stopService, and sets C{running} to C{False}.
        rr%N)
r;r=r:r1rB�stopService�addCallback�appendrFrGr3ZassertFalserL�r#r-�lrrr�test_stopService�s




z%EndpointServiceTests.test_stopServicecCsH|j��|j��}g}|�|j�|�|dg�|�|�t�g�dS)z�
        L{StreamServerEndpointService.stopService} cancels the L{Deferred}
        returned by C{listen} if it has not yet fired.  No error will be logged
        about the cancellation of the listen attempt.
        N)r;r=rOZaddBothrQrFrErrRrrr�#test_stopServiceBeforeStartFinisheds

z8EndpointServiceTests.test_stopServiceBeforeStartFinishedcCsZt�|j_|j��|j��}g}|�|j�|�|dg�|�	t�}|�t
|�d�dS)z�
        L{StreamServerEndpointService.stopService} cancels the L{Deferred}
        returned by C{listen} if it has not fired yet.  An error will be logged
        if the resulting exception is not L{CancelledError}.
        Nr%)r@r:r'r;r=rOrPrQrFrErG)r#r-rSZstoppingErrorsrrr� test_stopServiceCancelStartErrors



z5EndpointServiceTests.test_stopServiceCancelStartError)N)r4r5r6r7r<r?rCrDrIrJrKrMrNrTrUrVrrrrr9�s
	


r9c@s`eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�TimerServiceTestsa)
    Tests for L{twisted.application.internet.TimerService}.

    @type timer: L{TimerService}
    @ivar timer: service to test

    @type clock: L{task.Clock}
    @ivar clock: source of time

    @type deferred: L{Deferred}
    @ivar deferred: deferred returned by L{TimerServiceTests.call}.
    cCs,td|j�|_t��|_|j_t�|_dS)z1
        Set up a timer service to test.
        �N)r
�call�timerrr�clockrr2r"rrrr<=szTimerServiceTests.setUpcCs|jS)z�
        Function called by L{TimerService} being tested.

        @returns: C{self.deferred}
        @rtype: L{Deferred}
        )r2r"rrrrYFszTimerServiceTests.callcCsV|j��|�|jjd�|�|jjtj�|�|j	|jjj	�|�|jjjd�dS)z�
        When L{TimerService.startService} is called, it marks itself
        as running, creates a L{task.LoopingCall} and starts it.
        zService is startedzLoopingCall is startedN)
rZrB�
assertTruerL�assertIsInstance�_looprZLoopingCallr>r[r"rrr�test_startServicePs

z#TimerServiceTests.test_startServicecCs0g}|jdif|j_|j��|�dg|�dS)zi
        When L{TimerService.startService} is called, it calls the function
        immediately.
        rN)rQrZrYrBrF)r#r-rrr�$test_startServiceRunsCallImmediately\s
z6TimerServiceTests.test_startServiceRunsCallImmediatelycsBt����fdd�}|�td|�|j��|��|jjj�dS)z�
        L{TimerService.startService} uses L{internet._maybeGlobalReactor} to
        choose the reactor to pass to L{task.LoopingCall}
        uses the global reactor.
        cs�Srr)ZmaybeReactor�Z
otherClockrr�
getOtherClocknszKTimerServiceTests.test_startServiceUsesGlobalReactor.<locals>.getOtherClockZ_maybeGlobalReactorN)	rrZpatchrrZrBr>r^r[)r#rbrrar�"test_startServiceUsesGlobalReactorgs

z4TimerServiceTests.test_startServiceUsesGlobalReactorcCsR|j��|j��}|�|�|�d|jj�|j�t��|�	|�
|�d�dS)z�
        When L{TimerService.stopService} is called while a call is in progress.
        the L{Deferred} returned doesn't fire until after the call finishes.
        TN)rZrBrO�assertNoResultrFrLr2r0�objectr>�successResultOf�r#r(rrr�test_stopServiceWaitsus


z'TimerServiceTests.test_stopServiceWaitscCs8|j��|j�t��|j��}|�|�|�d�dS)z�
        When L{TimerService.stopService} is called while a call isn't in progress.
        the L{Deferred} returned has already been fired.
        N)rZrBr2r0rerOr>rfrgrrr�test_stopServiceImmediately�s

z-TimerServiceTests.test_stopServiceImmediatelycCsV|j��|j�tt���|�t�}|�dt|��|j�	�}|�
|�|�d�dS)z�
        When function passed to L{TimerService} returns a deferred that errbacks,
        the exception is logged, and L{TimerService.stopService} doesn't raise an error.
        r%N)rZrBr2r&rr@rErFrGrOr>rf)r#�errorsr(rrr�test_failedCallLogsError�s


z*TimerServiceTests.test_failedCallLogsErrorcCsPtdt�}|��t�|�}|��t�|�}t�}t|d|�}|�	||�dS)zq
        When pickling L{internet.TimerService}, it won't pickle
        L{internet.TimerService._loop}.
        r%r^N�
r
rrB�pickle�dumpsrO�loadsre�getattrr>�r#rZZdumpedTimerZloadedTimerZnothing�valuerrr�$test_pickleTimerServiceNotPickleLoop�s


z6TimerServiceTests.test_pickleTimerServiceNotPickleLoopcCsPtdt�}|��t�|�}|��t�|�}t�}t|d|�}|�	||�dS)zy
        When pickling L{internet.TimerService}, it won't pickle
        L{internet.TimerService._loopFinished}.
        r%Z
_loopFinishedNrlrqrrr�,test_pickleTimerServiceNotPickleLoopFinished�s


z>TimerServiceTests.test_pickleTimerServiceNotPickleLoopFinishedN)r4r5r6r7r<rYr_r`rcrhrirkrsrtrrrrrW/s
	


rWc@seZdZdZdd�ZdS)�ConnectInformationa�
    Information about C{endpointForTesting}

    @ivar connectQueue: a L{list} of L{Deferred} returned from C{connect}.  If
        these are not already fired, you can fire them with no value and they
        will trigger building a factory.

    @ivar constructedProtocols: a L{list} of protocols constructed.

    @ivar passedFactories: a L{list} of L{IProtocolFactory}; the ones actually
        passed to the underlying endpoint / i.e. the reactor.
    cCsg|_g|_g|_dSr)�connectQueue�constructedProtocols�passedFactoriesr"rrrr$�szConnectInformation.__init__N)r4r5r6r7r$rrrrru�sruFcs.tt�G��fdd�dt��}t���|�fS)aD
    Make a sample endpoint for testing.

    @param fireImmediately: If true, fire all L{Deferred}s returned from
        C{connect} immedaitely.

    @return: a 2-tuple of C{(information, endpoint)}, where C{information} is a
        L{ConnectInformation} describing the operations in progress on
        C{endpoint}.
    cseZdZ��fdd�ZdS)z.endpointForTesting.<locals>.ClientTestEndpointcsDt�}�j���|j��fdd��}�j�|��r@|�d�|S)Ncs*��d�}�j�|�t�}|�|�|Sr)�
buildProtocolrwrQrZmakeConnection)�ignored�protocol�	transport)r,�inforr�createProtocol�s


zNendpointForTesting.<locals>.ClientTestEndpoint.connect.<locals>.createProtocol)rrxrQrPrvr0)r#r,r-r~��fireImmediatelyr})r,r�connect�s
z6endpointForTesting.<locals>.ClientTestEndpoint.connectN)r4r5r6r�rrrr�ClientTestEndpoint�sr�)rrreru)r�r�rrr�endpointForTesting�sr�cs0g����j�|���fdd���fdd�S)a6
    Catch the global log stream.

    @param testCase: The test case to add a cleanup to.

    @param logPublisher: the log publisher to add and remove observers for.

    @return: a 0-argument callable that returns a list of textual log messages
        for comparison.
    @rtype: L{list} of L{unicode}
    cs���j�Sr)ZremoveObserverrQr��logPublisher�logsrrr)�r*zcatchLogs.<locals>.<lambda>csdd��D�S)NcSsg|]}t|��qSr)r)�.0Zeventrrr�
<listcomp>sz/catchLogs.<locals>.<lambda>.<locals>.<listcomp>rr)r�rrr)r*)ZaddObserverrQ�
addCleanup)ZtestCaser�rr�r�	catchLogs�sr�gY@c@seZdZdZddefdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Zd9d:�Z d;d<�Z!d=d>�Z"d?d@�Z#dAS)B�ClientServiceTestsz%
    Tests for L{ClientService}.
    Tc
s�i}|jt�d�|�|�|d�t|d�\}}g�|_G���fdd�dtt����|_}t||f|�����fdd�}	��|	�|r���	�|�fS)aL
        Create a L{ClientService} along with a L{ConnectInformation} indicating
        the connections in progress on its endpoint.

        @param fireImmediately: Should all of the endpoint connection attempts
            fire synchronously?
        @type fireImmediately: L{bool}

        @param startService: Should the L{ClientService} be started before
            being returned?
        @type startService: L{bool}

        @param protocolType: a 0-argument callable returning a new L{IProtocol}
            provider to be used for application-level protocol connections.

        @param kw: Arbitrary keyword arguments to be passed on to
            L{ClientService}

        @return: a 2-tuple of L{ConnectInformation} (for information about test
            state) and L{ClientService} (the system under test).  The
            L{ConnectInformation} has 2 additional attributes;
            C{applicationFactory} and C{applicationProtocols}, which refer to
            the unwrapped protocol factory and protocol instances passed in to
            L{ClientService} respectively.
        �r[r[�r�cs$eZdZ�Z���fdd�Z�ZS)z>ClientServiceTests.makeReconnector.<locals>.RememberingFactorycst�|��|�}��|�|Sr)�superryrQ)r#Zaddrr-)�RememberingFactory�	__class__�applicationProtocolsrrry6s
zLClientServiceTests.makeReconnector.<locals>.RememberingFactory.buildProtocol)r4r5r6r{ry�
__classcell__r)r�r��protocolType)r�rr�4sr�cs(d�_�jr��������g�dSr)Z	_protocolrLrOrF�getDelayedCallsr)r[r#�servicerr�stop>sz0ClientServiceTests.makeReconnector.<locals>.stop)
�updaterr�r�rre�applicationFactoryrr�rB)
r#r�rBr��kwZnkw�cqZendpointr,r�r)r�r�r[r�r#r�r�makeReconnectors


z"ClientServiceTests.makeReconnectorcCs&|jdd�\}}|�t|j�d�dS)zL
        When the service is started, a connection attempt is made.
        Fr�r%N)r�rFrGrv)r#r�r�rrrr_Ksz$ClientServiceTests.test_startServicecCsD|��\}}|j}|�|jd�|jd}|��|�|jd�dS)a!
        Although somewhat obscure, L{IProtocolFactory} includes both C{doStart}
        and C{doStop} methods; ensure that when these methods are called on the
        factory that was passed to the reactor, the factory that was passed
        from the application receives them.
        rr%N)r�r�rFZnumPortsrxZdoStart)r#r�r�ZfirstAppFactoryZfirstPassedFactoryrrr�test_startStopFactorySs
z(ClientServiceTests.test_startStopFactorycCsV|��\}}|��}|�|�|jd}|�|jjd�|�tt	���|�
|�dS)z�
        When the service is stopped, no further connect attempts are made.  The
        returned L{Deferred} fires when all outstanding connections have been
        stopped.
        rTN)r�rOrdrwrFr|�
disconnecting�connectionLostr�	Exceptionrf�r#r�r�r(r{rrr�test_stopServiceWhileConnectedbs

z1ClientServiceTests.test_stopServiceWhileConnectedcCs�|��\}}|��}|�|�|jd}|�|jjd�|��|�|�|�t|j�d�|�	t
t���|�t|j�d�dS)z�
        When L{ClientService} is restarted after having been connected, it
        waits to start connecting until after having disconnected.
        rTr%rXN)r�rOrdrwrFr|r�rBrGr�rr�r�rrr�#test_startServiceWaitsForDisconnectqs


z6ClientServiceTests.test_startServiceWaitsForDisconnectcCs�|jdd�\}}|jd�d�|jd}|��}|�|�|��}|�|�|��|�|�|�|�|�|j	j
d�|�tt
���|�|�|jd�d�|�t|j�d�|�|�|�|jd�|��}|�|�dS)a%
        When L{ClientService} is stopping - that is,
        L{ClientService.stopService} has been called, but the L{Deferred} it
        returns has not fired yet - calling L{startService} will cause a new
        connection to be made, and new calls to L{whenConnected} to succeed.
        Fr�rNTr%rX)r�rvr0rwrOrd�
whenConnectedrBrFr|r�r�rr�rfrGr>r�)r#r�r��first�stoppedZnextProtocolZ
secondStoppedrrr�test_startServiceWhileStopping�s(





�z1ClientServiceTests.test_startServiceWhileStoppingcCsj|jdd�\}}|��}|�|�|�|��t�|��|jd�d�|�	|j
d|�|����dS)a
        When L{ClientService} is stopped - that is,
        L{ClientService.stopService} has been called and the L{Deferred} it
        returns has fired - calling L{startService} will cause a new connection
        to be made, and new calls to L{whenConnected} to succeed.
        Fr����N)r�rOrf�failureResultOfr�rrBrvr0r>r�)r#r�r�r�rrr�test_startServiceWhileStopped�s
�z0ClientServiceTests.test_startServiceWhileStoppedcCsZttt�Gdd�dtt��}|j|d�\}}|jd}|�t�|��|�t�|��dS)aC
        If the protocol objects returned by the factory given to
        L{ClientService} provide special "marker" interfaces for their
        transport - L{IHalfCloseableProtocol} or L{IFileDescriptorReceiver} -
        those interfaces will be provided by the protocol objects passed on to
        the reactor.
        c@seZdZdZdS)zEClientServiceTests.test_interfacesForTransport.<locals>.FancyProtocolz9
            Provider of various interfaces.
            N)r4r5r6r7rrrr�
FancyProtocol�sr�)r�rN)	rrrrrer�rwr\Z
providedBy)r#r�r�r�Z
reactorFacingrrr�test_interfacesForTransport�s
z.ClientServiceTests.test_interfacesForTransportcCsnt�}|jd|d�\}}|jd�t��|�t�|�t|j�d�|�	�}|jd�t��|�
|�dS)zU
        When the service is stopped while retrying, the retry is cancelled.
        F�r�r[rrXr%N)rr�rvr&r��advance�AT_LEAST_ONE_ATTEMPTrFrGrOrf�r#r[r�r�r(rrr�test_stopServiceWhileRetrying�s
z0ClientServiceTests.test_stopServiceWhileRetryingcCsPt�}|jd|d�\}}|�t|j�d�|�|jd�|��}|�|�dS)zv
        When the service is stopped while initially connecting, the connection
        attempt is cancelled.
        Fr�r%rN)rr�rFrGrvrdrOrfr�rrr�test_stopServiceWhileConnecting�sz2ClientServiceTests.test_stopServiceWhileConnectingcCsJt�}|j|d�\}}|��}|�|��g�|�|�|�|jd�dS)zy
        When a client connects, the service keeps a reference to the new
        protocol and resets the delay.
        r�rN)rr�r�rFr�r>rfr�)r#r[r�r�ZawaitingProtocolrrr�test_clientConnected�s�z'ClientServiceTests.test_clientConnectedcCs�t�}|jd|d�\}}|�t|j�d�|jd�tt���|��}|�	|�|�
dd��|�t�|�t|j�d�dS)	z�
        When a client connection fails, the service removes its reference
        to the protocol and tries again after a timeout.
        Fr�r%rcSs
|�t�Sr)Ztrapr)rzrrrr)�r*z@ClientServiceTests.test_clientConnectionFailed.<locals>.<lambda>rXN)
rr�rFrGrvr&rr�r�rdZ
addErrbackr�r�)r#r[r�r�r�rrr�test_clientConnectionFailed�s�


z.ClientServiceTests.test_clientConnectionFailedcCs�t�}|j|dd�\}}|�t|j�d�|jd�d�|�t|j�d�|�|�|���|j	d�|j
d�tt
���|�t�|�t|j�d�|jd�d�|�|�|���|j	d�dS)z~
        When a client connection is lost, the service removes its reference
        to the protocol and calls retry.
        F)r[r�r%rNrX)rr�rFrGrvr0r>rfr�r�rwr�rr�r�r�)r#r[r�r�rrr�test_clientConnectionLost�s�
�z,ClientServiceTests.test_clientConnectionLostcCsTt�}|j|d�\}}|��}|jd�tt���|�|��t	�|�
|j�dS)z�
        When a client connection is lost while the service is stopping, the
        protocol stopping deferred is called and the reference to the protocol
        is removed.
        r�rN)rr�rOrwr�r�IndentationErrorr�r�rr\Zcalledr�rrr�&test_clientConnectionLostWhileStoppingsz9ClientServiceTests.test_clientConnectionLostWhileStoppingcCsv|jddd�\}}|�t|j�d�|��|�t|j�d�t|�}|��|�t|j�d�|�d|�d�dS)z�
        If L{ClientService} is started when it's already started, it will log a
        complaint and do nothing else (in particular it will not make
        additional connections).
        F)r�rBrr%z$Duplicate ClientService.startServiceN)r�rFrGrvrBr�ZassertIn)r#r�r�Zmessagesrrr�test_startTwices�
z"ClientServiceTests.test_startTwicec
Cs�t�}|jd|d�\}}|��}|��}|jdd�}|�|�|�|�|�|�|jd�d�|�|�}|�|�}|�|�}	|�||�|�||	�|�||jd�dS)z{
        L{ClientService.whenConnected} returns a L{Deferred} that fires when a
        connection is established.
        Fr�r%�ZfailAfterFailuresrN)	rr�r�rdrvr0rfr>r�)
r#r[r�r��a�b�cZresultAZresultBZresultCrrr�test_whenConnectedLater0s





z*ClientServiceTests.test_whenConnectedLatercCs�t�}|jd|d�\}}|��}|jdd�}|jdd�}|jdd�}|�|�|�|�|�|�|�|�tt��}|jd�|�|�|�|�|�	|t�|�|�|�|�|�|�
t�|�t
|j�d�|�|�|�|�|�|�tt��}	|jd�|	�|�|�|�|�	|t�|	�|�|�t}
|�
|
�|�t
|j�d�|�|�|�|�|jd�d�|�|�}|�|�}|�||�|�||jd�|jdd�}
|�|
�}|�||�dS)	z�
        L{ClientService.whenConnected} returns a L{Deferred} that fails, if
        asked, when some number of connections have failed.
        Fr�r%r�rX�rN)rr�r�rdrr�rvr&r>r�r�r�rFrGr0rfr�)r#r[r�r�Za0Za1Za2Za3�f1�f2ZAT_LEAST_TWO_ATTEMPTSZresultA0ZresultA3Za4ZresultA4rrr�test_whenConnectedFailsFsN




















z*ClientServiceTests.test_whenConnectedFailscCs�t�}|jd|d�\}}|��}|��}|jdd�}|�|�|�|�|�|�|��|�t�|�|t�|�|t�|�|t�dS)z�
        L{ClientService.whenConnected} returns a L{Deferred} that fails when
        L{ClientService.stopService} is called.
        Fr�r%r�N)	rr�r�rdrOr�r�r�r)r#r[r�r�r�r�r�rrr�test_whenConnectedStopService�s



z0ClientServiceTests.test_whenConnectedStopServicecCsRt�}|jd|d�\}}|jd�td��|��}|�|��g�|�|�dS)z�
        When L{ClientService.stopService} is called while waiting between
        connection attempts, the pending reconnection attempt is cancelled and
        the service is stopped immediately.
        Fr�r�
no connectionN)	rr�rvr&r�rOrFr�rfr�rrr�test_retryCancelled�sz&ClientServiceTests.test_retryCancelledcCs6t�}|jdd|d�\}}|��}|�|�|��dS)z�
        Calling L{ClientService.stopService} before
        L{ClientService.startService} returns a L{Deferred} that has
        already fired with L{None}.
        F)r�rBr[N�rr�rOZassertIsNonerf)r#r[�_r�r(rrr�"test_stopServiceBeforeStartService�s�
z5ClientServiceTests.test_stopServiceBeforeStartServicecCspt�}|jd|d�\}}|��}|jd�td��|��|��}|�|�|�j	t
�|�|�|�j	t
�dS)z�
        L{ClientService.whenConnected} returns a L{Deferred} that
        errbacks with L{CancelledError} if
        L{ClientService.stopService} is called between connection
        attempts.
        Fr�rr�N)rr�r�rvr&r�rOr]r�rrr)r#r[r�r�ZbeforeErrbackAndStopZafterErrbackAndStoprrr�'test_whenConnectedErrbacksOnStopService�s�
��z:ClientServiceTests.test_whenConnectedErrbacksOnStopServicecCsft�}|jd|d�\}}|jd�d�|��}|��}|jd�tt���|�	|�|�	|�dS)z�
        Calling L{ClientService.stopService} twice after it has
        connected (that is, stopping it while it is disconnecting)
        returns a L{Deferred} each time that fires when the
        disconnection has completed.
        Fr�rN)
rr�rvr0rOrwr�rr�rf�r#r[r�r��firstStopDeferred�secondStopDeferredrrr�"test_stopServiceWhileDisconnecting�s�

z5ClientServiceTests.test_stopServiceWhileDisconnectingcCsnt�}|jd|d�\}}|jd�d�|��}|��|��}|jd�tt	���|�
|�|�
|�dS)z�
        Calling L{ClientService.stopService} after calling a
        reconnection attempt returns a L{Deferred} that fires when the
        disconnection has completed.
        Fr�rN)rr�rvr0rOrBrwr�rr�rfr�rrr�test_stopServiceWhileRestarting�s�

z2ClientServiceTests.test_stopServiceWhileRestartingcCsLt�}|jd|d�\}}|��}|��}|�|�|��|�|�|��dS)z�
        Calling L{ClientService.stopService} on a stopped service
        returns a L{Deferred} that has already fired with L{None}.
        Fr�Nr�)r#r[r�r�r�r�rrr� test_stopServiceOnStoppedService�s�
z3ClientServiceTests.test_stopServiceOnStoppedServicecs8dg��fdd�}|j|dd�\}}|�d�d�dS)z�
        The C{prepareConnection} callable is called after
        L{ClientService.startService} once the connection is made.
        rcs�dd7<dS�Nrr%r�Z_proto�Zpreparesrr�prepareConnectionsz[ClientServiceTests.test_prepareConnectionCalledWhenServiceStarts.<locals>.prepareConnectionT)r�rBr%N)r�rF�r#r�r�r�rr�r�-test_prepareConnectionCalledWhenServiceStartss�
z@ClientServiceTests.test_prepareConnectionCalledWhenServiceStartscs:g��fdd�}|j|d�\}}|�|jd�d�dS)zf
        The C{prepareConnection} callable is passed the connected protocol
        instance.
        cs��|�dSr�rQ��proto��newProtocolsrrr�"szVClientServiceTests.test_prepareConnectionCalledWithProtocol.<locals>.prepareConnection�r�rN)r�r>rwr�rr�r�(test_prepareConnectionCalledWithProtocols�
z;ClientServiceTests.test_prepareConnectionCalledWithProtocolcs~dg��fdd�}t�}|j|d|d�\}}|jd�td��|�d�d�|�t�|jd�d�|�d�d�dS)	zf
        The C{prepareConnection} callback is invoked only once a connection is
        made.
        rcs�dd7<dSr�rr�r�rrr�2sz]ClientServiceTests.test_prepareConnectionCalledAfterConnectionMade.<locals>.prepareConnectionF)r�r�r[zconnection attempt failedr%N)	rr�rvr&r�rFr�r�r0�r#r�r[r�r�rr�r�/test_prepareConnectionCalledAfterConnectionMade+s�

zBClientServiceTests.test_prepareConnectionCalledAfterConnectionMadecsndg��fdd�}t�}|j||d�\}}|�d�d�|jd�tt���|�t�|�d�d�dS)z�
        The C{prepareConnection} callback is invoked each time a connection is
        made, including on reconnection.
        rcs�dd7<dSr�rr�r�rrr�JszUClientServiceTests.test_prepareConnectionCalledOnReconnect.<locals>.prepareConnection�r�r[r%rXN)	rr�rFrwr�rr�r�r�r�rr�r�'test_prepareConnectionCalledOnReconnectCs�

z:ClientServiceTests.test_prepareConnectionCalledOnReconnectcs@t���fdd�}|j|d�\}}|�|���}|��|�dS)a?
        The C{prepareConnection} return value is ignored when it does not
        indicate a failure. Even though the callback participates in the
        internal new-connection L{Deferred} chain for error propagation
        purposes, any successful result does not affect the ultimate return
        value.
        cs�Srrr���sentinelrrr�eszVClientServiceTests.test_prepareConnectionReturnValueIgnored.<locals>.prepareConnectionr�N)rer�rfr�ZassertNotIdentical)r#r�r�r�r-rr�r�(test_prepareConnectionReturnValueIgnoredZs
	z;ClientServiceTests.test_prepareConnectionReturnValueIgnoredcs`g�t����fdd�}|j|d�\}}|��}|�|���d�|�|jd|�|��dS)z�
        The C{prepareConnection} callable returns a deferred and calls to
        L{ClientService.whenConnected} wait until it fires.
        cs��|��Srr�r��ZnewProtocolDeferredr�rrr�vs
zVClientServiceTests.test_prepareConnectionReturningADeferred.<locals>.prepareConnectionr�Nr)rr�r�rdr0r>r�rf)r#r�r�r��whenConnectedDeferredrr�r�(test_prepareConnectionReturningADeferredns

�z;ClientServiceTests.test_prepareConnectionReturningADeferredcCslt�}dd�}|j||d�\}}|jdd�}|�|�|�t�|�|�|�t�|�t|�|�j	�dS)zs
        The connection attempt counts as a failure when the
        C{prepareConnection} callable throws.
        cSs
t��dSr)r�r�rrrr��szJClientServiceTests.test_prepareConnectionThrows.<locals>.prepareConnectionr�rXr�N)
rr�r�rdr�r�r>r�r��type)r#r[r�r�r�r�rrr�test_prepareConnectionThrows�s�





�z/ClientServiceTests.test_prepareConnectionThrowsN)$r4r5r6r7rr�r_r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr�sD�
@

;r�)F)4r7Z
__future__rrrmZzope.interfacerZzope.interface.verifyrZtwisted.internet.protocolrrZtwisted.internet.taskrZtwisted.trial.unittestr	r
Ztwisted.applicationrZtwisted.application.internetrr
rZtwisted.internet.deferrrZtwisted.internet.interfacesrrrrrZtwisted.internetrZtwisted.python.failurerZtwisted.loggerrrZtwisted.test.proto_helpersrrrerr r9rWrur�r�r�r�rrrr�<module>s>B

'