AnonSec Shell
Server IP : 209.38.156.173  /  Your IP : 216.73.216.122   [ Reverse IP ]
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux lakekumayuhotel 5.15.0-136-generic #147-Ubuntu SMP Sat Mar 15 15:53:30 UTC 2025 x86_64
User : root ( 0)
PHP Version : 8.1.2-1ubuntu2.22
Disable Function : NONE
Domains : 2 Domains
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /usr/lib/python3/dist-packages/twisted/application/test/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME ]     [ BACKUP SHELL ]     [ JUMPING ]     [ MASS DEFACE ]     [ SCAN ROOT ]     [ SYMLINK ]     

Current File : /usr/lib/python3/dist-packages/twisted/application/test/__pycache__/test_internet.cpython-310.pyc
o

�bO��@stdZddlZddlmZddlmZddlmZddlm	Z	m
Z
mZddlm
Z
ddlmZmZdd	lmZmZmZmZmZdd
lmZmZddlmZddlmZmZdd
lm Z ddl!m"Z"ddl#m$Z$m%Z%dd�Z&ee�Gdd�d��Z'eee'�ee�Gdd�d��Z(eee'�Gdd�de%�Z)Gdd�de%�Z*Gdd�d�Z+d$dd�Z,efdd �Z-d!Z.Gd"d#�d#e$�Z/dS)%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.
�N)�implementer)�verifyClass)�internet)�
ClientService�StreamServerEndpointService�TimerService)�task)�CancelledError�Deferred)�IFileDescriptorReceiver�IHalfCloseableProtocol�IListeningPort�IStreamClientEndpoint�IStreamServerEndpoint)�Factory�Protocol)�Clock)�formatEvent�globalLogPublisher)�Failure)�StringTransport)�SynchronousTestCase�TestCasecCsdS)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__LszFakeServer.__init__csF�jd7_|�_t�fdd�d��_�jdur �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>Z�z#FakeServer.listen.<locals>.<lambda>)�	cancellerN)�listenAttempts�factoryr
�result�failImmediatelyr$)r!r+rr r�listenOs	
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,�callbackrr rrr�startedListening_�zFakeServer.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�deferredr/r rrr�stoppedListeninggszFakeServer.stoppedListening)�__name__�
__module__�__qualname__�__doc__r,r+r-r	r%r*r"r.r0r3rrrrr0src@s,eZdZdZdZdd�Zdd�Zdd�ZdS)	rzs
    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}
        )r
r2r rrr�
stopListening~szFakePort.stopListeningcC�dSrrr rrr�getHost��zFakePort.getHostcCr9rrr rrr�startListening�r;zFakePort.startListening)r4r5r6r7r2r8r:r<rrrrrts	rc@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?�_raiseSynchronously�assertRaises�startService)r!�thunkrrr�(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)rIr?rAr rrr�test_synchronousRaisePrivileged�r1z4EndpointServiceTests.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?rGr>r,r$rD�flushLoggedErrors�assertEqual�len�r!�loggedrrr�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?rErGr>r,r$rDrKrLrMrNrrr�!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)rDr>r-r?rGrKrLrMrNrrr� 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?rGrBr+r>rL�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)rCr?rGrLr>r*rSr 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?rAr>r0rG�stopService�addCallback�appendrLrMr3�assertFalserS�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?rArV�addBothrXrLrKr	rZrrr�#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#)rDr>r%r?rArVrWrXrLrKrM)r!r,r[�stoppingErrorsrrr� test_stopServiceCancelStartErrors



z5EndpointServiceTests.test_stopServiceCancelStartErrorr)r4r5r6r7r@rCrIrJrPrQrRrTrUr\r^r`rrrrr=�s	




r=c@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�clockr
r2r rrrr@:szTimerServiceTests.setUpcCs|jS)z�
        Function called by L{TimerService} being tested.

        @returns: C{self.deferred}
        @rtype: L{Deferred}
        )r2r rrrrcBszTimerServiceTests.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)
rdrG�
assertTruerS�assertIsInstance�_loopr�LoopingCallrBrer rrr�test_startServiceKs

z#TimerServiceTests.test_startServicecCs0g}|jdif|j_|j��|�dg|�dS)zi
        When L{TimerService.startService} is called, it calls the function
        immediately.
        rN)rXrdrcrGrL)r!r,rrr�$test_startServiceRunsCallImmediatelyVs
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.
        c��Srr)�maybeReactor��
otherClockrr�
getOtherClockh�zKTimerServiceTests.test_startServiceUsesGlobalReactor.<locals>.getOtherClock�_maybeGlobalReactorN)	rr�patchrrdrGrBrhre)r!rprrnr�"test_startServiceUsesGlobalReactor`s

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)rdrGrV�assertNoResultrLrSr2r/�objectrB�successResultOf�r!r&rrr�test_stopServiceWaitsos


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)rdrGr2r/rvrVrBrwrxrrr�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)rdrGr2r$rrDrKrLrMrVrBrw)r!�errorsr&rrr�test_failedCallLogsError�s


z*TimerServiceTests.test_failedCallLogsErrorcC�Ptdt�}|��t�|�}|��t�|�}t�}t|d|�}|�	||�dS)zq
        When pickling L{internet.TimerService}, it won't pickle
        L{internet.TimerService._loop}.
        r#rhN�
rrrG�pickle�dumpsrV�loadsrv�getattrrB�r!rd�dumpedTimer�loadedTimer�nothing�valuerrr�$test_pickleTimerServiceNotPickleLoop��


z6TimerServiceTests.test_pickleTimerServiceNotPickleLoopcCr})zy
        When pickling L{internet.TimerService}, it won't pickle
        L{internet.TimerService._loopFinished}.
        r#�
_loopFinishedNr~r�rrr�,test_pickleTimerServiceNotPickleLoopFinished�r�z>TimerServiceTests.test_pickleTimerServiceNotPickleLoopFinishedN)r4r5r6r7r@rcrjrkrtryrzr|r�r�rrrrra,s
	

rac@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"�s
zConnectInformation.__init__N)r4r5r6r7r"rrrrr��s
r�Fcs,tt�G��fdd�d��}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)�
buildProtocolr�rXr�makeConnection)�ignored�protocol�	transport)r+�inforr�createProtocol�s


zNendpointForTesting.<locals>.ClientTestEndpoint.connect.<locals>.createProtocol)r
r�rXrWr�r/)r!r+r,r���fireImmediatelyr�)r+r�connect�s
z6endpointForTesting.<locals>.ClientTestEndpoint.connectN)r4r5r6r�rr�rr�ClientTestEndpoint�sr�)rrr�)r�r�rr�r�endpointForTesting�s
r�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)�removeObserverrXr��logPublisher�logsrrr'�r(zcatchLogs.<locals>.<lambda>csdd��D�S)NcSsg|]}t|��qSr)r)�.0�eventrrr�
<listcomp>�sz/catchLogs.<locals>.<lambda>.<locals>.<listcomp>rr)r�rrr'�s)�addObserverrX�
addCleanup)�testCaser�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}.
    Tcs�i}|jt�d�|�|�|d�t|d�\}}g�|_G��fdd�dt�}|�|_}	t||	fi|������fdd�}
��|
�|rK���|�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.
        �rere�r�cs"eZdZ�Z��fdd�Z�ZS)z>ClientServiceTests.makeReconnector.<locals>.RememberingFactorycst��|�}��|�|Sr)�superr�rX)r!�addrr,)�	__class__�applicationProtocolsrrr�0s
zLClientServiceTests.makeReconnector.<locals>.RememberingFactory.buildProtocol)r4r5r6r�r��
__classcell__r)r��protocolType)r�r�RememberingFactory-sr�cs(d�_�jr
��������g�dSr)�	_protocolrSrVrL�getDelayedCallsr)rer!�servicerr�stop9sz0ClientServiceTests.makeReconnector.<locals>.stop)	�updaterr�r�r�applicationFactoryrr�rG)r!r�rGr��kw�nkw�cq�endpointr�r+r�r)r�rer�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�rLrMr�)r!r�r�rrrrjFsz$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�rL�numPortsr��doStart)r!r�r��firstAppFactory�firstPassedFactoryrrr�test_startStopFactoryMs
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�rVrur�rLr��
disconnecting�connectionLostr�	Exceptionrw�r!r�r�r&r�rrr�test_stopServiceWhileConnected[s

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#rbN)r�rVrur�rLr�r�rGrMr�rr�r�rrr�#test_startServiceWaitsForDisconnectis


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#rb)r�r�r/r�rVru�
whenConnectedrGrLr�r�r�rr�rwrMrBr�)r!r�r��first�stopped�nextProtocol�
secondStoppedrrr�test_startServiceWhileStoppingys(





�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�rVrw�failureResultOfr�r	rGr�r/rBr�)r!r�r�r�rrr�test_startServiceWhileStopped�s
�z0ClientServiceTests.test_startServiceWhileStoppedcCsXttt�Gdd�dt��}|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)rrrrr�r�rf�
providedBy)r!r�r�r��
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�rerrbr#N)rr�r�r$r��advance�AT_LEAST_ONE_ATTEMPTrLrMrVrw�r!rer�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�rLrMr�rurVrwr�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�rLr�rBrwr�)r!rer�r��awaitingProtocolrrr�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)�trapr	)r�rrrr'�s
z@ClientServiceTests.test_clientConnectionFailed.<locals>.<lambda>rbN)
rr�rLrMr�r$rr�r�ru�
addErrbackr�r�)r!rer�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)rer�r#rNrb)rr�rLrMr�r/rBrwr�r�r�r�rr�r�r�)r!rer�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�rVr�r�r�IndentationErrorr�r�r	rf�calledr�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�rGrr#z$Duplicate ClientService.startServiceN)r�rLrMr�rGr��assertIn)r!r�r��messagesrrr�test_startTwicesz"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#��failAfterFailuresrN)	rr�r�rur�r/rwrBr�)
r!rer�r��a�b�c�resultA�resultB�resultCrrr�test_whenConnectedLater"s





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�rb�rN)rr�r�rurr�r�r$rBr�r�r�rLrMr/rwr�)r!rer�r��a0�a1�a2�a3�f1�f2�AT_LEAST_TWO_ATTEMPTS�resultA0�resultA3�a4�resultA4rrr�test_whenConnectedFails7sN




















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�rurVr�r�r�r	)r!rer�r�r�r�r�rrr�test_whenConnectedStopServiceqs



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�r�r$r�rVrLr�rwr�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�rGreN�rr�rV�assertIsNonerw)r!re�_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�r�r$r�rVrgr�r�r	)r!rer�r��beforeErrbackAndStop�afterErrbackAndStoprrr�'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�r�r/rVr�r�rr�rw�r!rer�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�r�r/rVrGr�r�rr�rwrrrr�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!rerr�rrrrr� 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.
        rc��dd7<dS�Nrr#r��_proto��preparesrr�prepareConnection��z[ClientServiceTests.test_prepareConnectionCalledWhenServiceStarts.<locals>.prepareConnectionT)rrGr#N)r�rL�r!rr�r�rrr�-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�rX��proto��newProtocolsrrr	szVClientServiceTests.test_prepareConnectionCalledWithProtocol.<locals>.prepareConnection�rrN)r�rBr�rrrr�(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.
        rcrr
rrrrrrrz]ClientServiceTests.test_prepareConnectionCalledAfterConnectionMade.<locals>.prepareConnectionF)rr�rezconnection attempt failedr#N)	rr�r�r$r�rLr�r�r/�r!rrer�r�rrr�/test_prepareConnectionCalledAfterConnectionMades
�
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.
        rcrr
rrrrrr/rzUClientServiceTests.test_prepareConnectionCalledOnReconnect.<locals>.prepareConnection�rrer#rbN)	rr�rLr�r�rr�r�r�rrrr�'test_prepareConnectionCalledOnReconnect(s
�
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.
        crlrrr��sentinelrrrJrqzVClientServiceTests.test_prepareConnectionReturnValueIgnored.<locals>.prepareConnectionrN)rvr�rwr��assertNotIdentical)r!rr�r�r,rr!r�(test_prepareConnectionReturnValueIgnored?s
	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��|��Srrr��newProtocolDeferredrrrrZs
zVClientServiceTests.test_prepareConnectionReturningADeferred.<locals>.prepareConnectionrNr)r
r�r�rur/rBr�rw)r!rr�r��whenConnectedDeferredrr%r�(test_prepareConnectionReturningADeferredRs

�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.
        cSst��r)r�rrrrrpszJClientServiceTests.test_prepareConnectionThrows.<locals>.prepareConnectionrrbr�N)
rr�r�rur�r�rBr�r��type)r!rerr�r�r'rrr�test_prepareConnectionThrowsis
�



�z/ClientServiceTests.test_prepareConnectionThrowsN)$r4r5r6r7rr�rjr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrr	r
rrrrr r$r(r*rrrrr��sD
�C


:


r�)F)0r7r�zope.interfacer�zope.interface.verifyr�twisted.applicationr�twisted.application.internetrrr�twisted.internetr�twisted.internet.deferr	r
�twisted.internet.interfacesrrr
rr�twisted.internet.protocolrr�twisted.internet.taskr�twisted.loggerrr�twisted.python.failurer�twisted.test.proto_helpersr�twisted.trial.unittestrrrrrr=rar�r�r�r�r�rrrr�<module>s>
@

#

Anon7 - 2022
AnonSec Team