ϪfMdZddlZddlmZddlmZddlmZddlm Z m Z m Z ddl m Z ddlmZmZdd lmZmZmZmZmZdd lmZmZdd lmZdd lmZdd lmZm Z ddl!m"Z"ddl#m$Z$m%Z%dZ&eeGddZ'eee'eeGddZ(eee'Gdde%Z)Gdde%Z*GddZ+d dZ,e fdZ-dZ.Gdde$Z/y)!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) ClientServiceStreamServerEndpointService TimerService)task)CancelledErrorDeferred)IFileDescriptorReceiverIHalfCloseableProtocolIListeningPortIStreamClientEndpointIStreamServerEndpoint)FactoryProtocol)Clock)StringTransport) formatEventglobalLogPublisher)Failure)SynchronousTestCaseTestCasecy)zM A fake target function for testing TimerService which does nothing. NrH/usr/lib/python3/dist-packages/twisted/application/test/test_internet.pyfakeTargetFunctionr)s rcFeZdZdZdZdZdZeZdZ dZ dZ dZ dZ y) 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. Nrc"t|_yN)FakePortportselfs r__init__zFakeServer.__init__Ls J rcxjdz c_|_tfd_j%jj jjS)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} c:|jjSr")errbackcancelException)dr&s rz#FakeServer.listen..Zs199T=Q=Q3Rr) canceller)listenAttemptsfactoryr resultfailImmediatelyr+)r&r1s` rlistenzFakeServer.listenOsX q  )RS    + KK   4 4 5{{rcN|jj|jy)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)r2callbackr$r%s rstartedListeningzFakeServer.startedListening_s TYY'rcN|jjjdy)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$deferredr6r%s rstoppedListeningzFakeServer.stoppedListeninggs ##D)r)__name__ __module__ __qualname____doc__r2r1r3r r,r0r'r4r7r:rrrr r 0s9(FGO$&ON (*rr c&eZdZdZdZdZdZdZy)r#zs Fake L{IListeningPort} implementation. @ivar deferred: The L{Deferred} returned by C{stopListening}. Nc8t|_|jS)z` Stop listening. @return: a L{Deferred} stored in L{FakePort.deferred} )r r9r%s r stopListeningzFakePort.stopListening~s !  }}rcyr"rr%s rgetHostzFakePort.getHost rcyr"rr%s rstartListeningzFakePort.startListeningrDr)r;r<r=r>r9rArCrFrrrr#r#ts H  rr#cZeZdZdZdZdZddZdZdZdZ d Z d Z d Z d Z d ZdZy)EndpointServiceTestsz4 Tests for L{twisted.application.internet}. ct|_t|_t |j|j|_y)zp Construct a stub server, a stub factory, and a L{StreamServerEndpointService} to test. N)r fakeServerrr1rsvcr%s rsetUpzEndpointServiceTests.setUps, %,y .t Mrc|jj|j|j|jjy)z L{StreamServerEndpointService.privilegedStartService} calls its endpoint's C{listen} method with its factory. N)rKprivilegedStartServiceassertIdenticalr1rJr%s rtest_privilegedStartServicez0EndpointServiceTests.test_privilegedStartServices2 '') T\\4??+B+BCrNct|j_d|j_|j t|xs|jj y)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)ZeroDivisionErrorrJr3rK_raiseSynchronously assertRaises startService)r&thunks r(test_synchronousRaiseRaisesSynchronouslyz=EndpointServiceTests.test_synchronousRaiseRaisesSynchronouslys? +<*=''+$ +U-Kdhh6K6KLrcN|j|jjy)z L{StreamServerEndpointService.privilegedStartService} should behave the same as C{startService} with respect to L{EndpointServiceTests.test_synchronousRaiseRaisesSynchronously}. N)rWrKrNr%s rtest_synchronousRaisePrivilegedz4EndpointServiceTests.test_synchronousRaisePrivilegeds 55dhh6U6UVrc|jj|jjj t |j t }|jt|dy)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) rKrUrJr2r+rRflushLoggedErrors assertEquallenr&loggeds rtest_failReportsErrorz*EndpointServiceTests.test_failReportsErrorsU  &&'8':;''(9: Va(rcd|j_|jj|jjj t |jt }|jt|dy)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) rKrSrUrJr2r+rRr[r\r]r^s r!test_asynchronousFailReportsErrorz6EndpointServiceTests.test_asynchronousFailReportsErrorsa(,$  &&'8':;''(9: Va(rct|j_|jj |j t}|j t|dy)z Without the C{_raiseSynchronously} compatibility flag, failing immediately has the same behavior as failing later; it logs the error. r)N)rRrJr3rKrUr[r\r]r^s r test_synchronousFailReportsErrorz5EndpointServiceTests.test_synchronousFailReportsErrorsH +<*=' ''(9: Va(rc|jj|j|j|jj|j |jj dy)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)rKrUrOr1rJr\runningr%s rtest_startServiceUnstartedz/EndpointServiceTests.test_startServiceUnstartedsL  T\\4??+B+BC ))40rc|j|jj|j|jj d|j|jj dy)z L{StreamServerEndpointService.startService} sets the C{running} flag, but nothing else, if the service has already been started. r)TN)rPrKrUr\rJr0rfr%s rtest_startServiceStartedz-EndpointServiceTests.test_startServiceStartedsT ((*  77; ))40rc|jj|jj|jj |jj }g}|j |j|jt|d|jj|jt|d|j|jjy)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) rKrNrJr7rU stopService addCallbackappendr\r]r: assertFalserfr&r2ls rtest_stopServicez%EndpointServiceTests.test_stopServices '') ((* %%' 188$ Q# ((* Q# ))*rc|jj|jj}g}|j|j|j |dg|j |j tgy)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)rKrNrkaddBothrmr\r[r ros r#test_stopServiceBeforeStartFinishedz8EndpointServiceTests.test_stopServiceBeforeStartFinishedsh '')%%' qxx  TF# //?Drc^t|j_|jj |jj }g}|j |j|j|dg|jt}|jt|dy)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)) rRrJr,rKrNrkrlrmr\r[r])r&r2rpstoppingErrorss r test_stopServiceCancelStartErrorz5EndpointServiceTests.test_stopServiceCancelStartErrors +<*=' '')%%' 188$ TF#//0AB ^,a0rr")r;r<r=r>rLrPrWrYr`rbrdrgrirqrtrwrrrrHrHsHNDM(W ) ))11+$ E 1rrHcLeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z y ) 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}. ctd|j|_tjx|_|j_t |_y)z1 Set up a timer service to test. N)rcalltimerr rclockr r9r%s rrLzTimerServiceTests.setUp:s8"!TYY/ (, 4 TZZ%  rc|jS)z Function called by L{TimerService} being tested. @returns: C{self.deferred} @rtype: L{Deferred} )r9r%s rr|zTimerServiceTests.callBs}}rc|jj|j|jjd|j |jj t j|j|j|jj j|j|jj jdy)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) r}rU assertTruerfassertIsInstance_loopr LoopingCallrOr~r%s rtest_startServicez#TimerServiceTests.test_startServiceKs !  **,@A djj..0@0@A TZZ)9)9)?)?@  ((002JKrcg}|jdif|j_|jj|j dg|y)zi When L{TimerService.startService} is called, it calls the function immediately. r"N)rmr}r|rUr\)r&r2s r$test_startServiceRunsCallImmediatelyz6TimerServiceTests.test_startServiceRunsCallImmediatelyVsA !=='26  ! $(rctjfd}|jtd||jj |j |jjjy)z L{TimerService.startService} uses L{internet._maybeGlobalReactor} to choose the reactor to pass to L{task.LoopingCall} uses the global reactor. cSr"r) maybeReactor otherClocks r getOtherClockzKTimerServiceTests.test_startServiceUsesGlobalReactor..getOtherClockhs  r_maybeGlobalReactorN) r rpatchrr}rUrOrr~)r&rrs @r"test_startServiceUsesGlobalReactorz4TimerServiceTests.test_startServiceUsesGlobalReactor`sX ZZ\   82MB ! Z)9)9)?)?@rcb|jj|jj}|j||j d|jj |j jt|j|j|dy)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) r}rUrkassertNoResultr\rfr9r6objectrOsuccessResultOfr&r-s rtest_stopServiceWaitsz'TimerServiceTests.test_stopServiceWaitsos} ! JJ " " $ A tzz112 vx( T11!4d;rc|jj|jjt |jj }|j |j|dy)z When L{TimerService.stopService} is called while a call isn't in progress. the L{Deferred} returned has already been fired. N)r}rUr9r6rrkrOrrs rtest_stopServiceImmediatelyz-TimerServiceTests.test_stopServiceImmediately{sU ! vx( JJ " " $ T11!4d;rcf|jj|jjt t |j t }|jdt||jj}|j|j|dy)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) r}rUr9r+rrRr[r\r]rkrOr)r&errorsr-s rtest_failedCallLogsErrorz*TimerServiceTests.test_failedCallLogsErrors ! g&7&9:;''(9: CK( JJ " " $ T11!4d;rc tdt}|jtj|}|j tj |}t}t|d|}|j||y)zq When pickling L{internet.TimerService}, it won't pickle L{internet.TimerService._loop}. r)rN rrrUpickledumpsrkloadsrgetattrrOr&r} dumpedTimer loadedTimernothingvalues r$test_pickleTimerServiceNotPickleLoopz6TimerServiceTests.test_pickleTimerServiceNotPickleLoopsjQ 23 ll5)  ll;/ ( Wg6 We,rc tdt}|jtj|}|j tj |}t}t|d|}|j||y)zy When pickling L{internet.TimerService}, it won't pickle L{internet.TimerService._loopFinished}. r) _loopFinishedNrrs r,test_pickleTimerServiceNotPickleLoopFinishedz>TimerServiceTests.test_pickleTimerServiceNotPickleLoopFinishedsjQ 23 ll5)  ll;/ ( _g> We,rN)r;r<r=r>rLr|rrrrrrrrrrrryry,s; # L) A << <- -rryceZdZdZdZy)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. c.g|_g|_g|_yr") connectQueueconstructedProtocolspassedFactoriesr%s rr'zConnectInformation.__init__s$&!!rN)r;r<r=r>r'rrrrrs  "rrcjttGfdd}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}. ceZdZfdZy).endpointForTesting..ClientTestEndpointct}jj|jfd}jj|r|j d|S)Ncjd}jj|t}|j ||Sr") buildProtocolrrmrmakeConnection)ignoredprotocol transportr1infos rcreateProtocolzNendpointForTesting..ClientTestEndpoint.connect..createProtocolsB"006))00:+- '' 2r)r rrmrlrr6)r&r1r2rfireImmediatelyrs ` rconnectz6endpointForTesting..ClientTestEndpoint.connects`ZF  ' ' 0          $ $V ,%MrN)r;r<r=r)rrsrClientTestEndpointrs rr)rrr)rrrs` @rendpointForTestingrs;&'($  D #% %%rctgjj|jfdfdS)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} c:jjSr")removeObserverrm) logPublisherlogssrr.zcatchLogs..s ; ;DKK Hrc@Dcgc] }t|c}Scc}wr")r)eventrs rr.zcatchLogs..sD95K&99s) addObserverrm addCleanup)testCaserrs `@r catchLogsrs1 DT[[) HI 99rgY@ceZdZdZddefdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!d Z"d!Z#y")#ClientServiceTestsz% Tests for L{ClientService}. Tc V i}|jt|j||d t|\}}gx |_G fddt}|x|_} t || fi| fd} j| |r j| 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~rc(eZdZWZfdZxZS)>ClientServiceTests.makeReconnector..RememberingFactorycHt||}j||Sr")superrrm)r&addrr2 __class__applicationProtocolss rrzLClientServiceTests.makeReconnector..RememberingFactory.buildProtocol0s%.t4$++F3 r)r;r<r=rr __classcell__)rr protocolTypes@rRememberingFactoryr-s#H  rrcd_jrjjj gyr") _protocolrfrkr\getDelayedCalls)r~r&servicesrstopz0ClientServiceTests.makeReconnector..stop9s9 $G ##%   U224b 9r) updaterrrrapplicationFactoryrrrU)r&rrUrkwnkwcqendpointrr1rrr~rs` ` @@@rmakeReconnectorz"ClientServiceTests.makeReconnectors8  ! 2G )/J H:<;r6  +=*>>'9S9 :    "7{rcx|jd\}}|jt|jdy)zL When the service is started, a connection attempt is made. Frr)N)rr\r]r)r&rrs rrz$ClientServiceTests.test_startServiceFs4**5*A G R__-q1rc|j\}}|j}|j|jd|jd}|j |j|jdy)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)rrr\numPortsrdoStart)r&rrfirstAppFactoryfirstPassedFactorys rtest_startStopFactoryz(ClientServiceTests.test_startStopFactoryMsj**, G// 1115//2""$ 1115rc<|j\}}|j}|j||jd}|j |j j d|jtt|j|y)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) rrkrrr\r disconnectingconnectionLostr Exceptionrr&rrr-rs rtest_stopServiceWhileConnectedz1ClientServiceTests.test_stopServiceWhileConnected[s **, G    ! A**1- ++994@ 45 Qrc|j\}}|j}|j||jd}|j |j j d|j|j||j t|jd|jtt|j t|jdy)z When L{ClientService} is restarted after having been connected, it waits to start connecting until after having disconnected. rTr)r{N) rrkrrr\rrrUr]rrrrs r#test_startServiceWaitsForDisconnectz6ClientServiceTests.test_startServiceWaitsForDisconnectis **, G    ! A**1- ++994@ A R445q9 45 R445q9rcF|jd\}}|jdjd|jd}|j }|j ||j }|j ||j|j ||j ||j|jjd|jtt|j||jdjd|jt|jd|j!|j||j"d|j }|j |y)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. FrrNTr)r{)rrr6rrkr whenConnectedrUr\rrrrrrr]rOr)r&rrfirststopped nextProtocol secondStoppeds rtest_startServiceWhileStoppingz1ClientServiceTests.test_startServiceWhileStoppingysX**5*A G ##D)''*%%' G$,,.  L) L) G$ 66= WY[12 W% ##D) R445q9    .0G0G0J  ++-  M*rc|jd\}}|j}|j||j|j t |j |jdjd|j|jd|j|j y)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. FrN) rrkrfailureResultOfrr rUrr6rOr)r&rrrs rtest_startServiceWhileStoppedz0ClientServiceTests.test_startServiceWhileStoppeds**5*A G%%' W% W224nE $$T*   # #B ')=)=g>S>S>U)V rc(tttGddt}|j |\}}|j d}|j tj||j tj|y)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. ceZdZdZy)EClientServiceTests.test_interfacesForTransport..FancyProtocolz9 Provider of various interfaces. N)r;r<r=r>rrr FancyProtocolrs rr)rrN)rr r rrrr providedBy)r&rrr reactorFacings rtest_interfacesForTransportz.ClientServiceTests.test_interfacesForTransports +-D E H  F  ** *F G//2  /::=IJ .99-HIrct}|jd|\}}|jdjt |j t |jt|jd|j}|jdjt |j|y)zU When the service is stopped while retrying, the retry is cancelled. Frr~rr{r)N) rrrr+radvanceAT_LEAST_ONE_ATTEMPTr\r]rkrr&r~rrr-s rtest_stopServiceWhileRetryingz0ClientServiceTests.test_stopServiceWhileRetryings**5*N G ""9;/ *+ R__-q1    ! ""9;/ Qrc t}|jd|\}}|jt|jd|j |jd|j }|j|y)zv When the service is stopped while initially connecting, the connection attempt is cancelled. Frr)rN)rrr\r]rrrkrr s rtest_stopServiceWhileConnectingz2ClientServiceTests.test_stopServiceWhileConnectingsp **5*N G R__-q1 BOOA./    ! Qrct}|j|\}}|j}|j|j g|j |j ||jdy)zy When a client connects, the service keeps a reference to the new protocol and resets the delay. rrN)rrrr\rrOrr)r&r~rrawaitingProtocols rtest_clientConnectedz'ClientServiceTests.test_clientConnectedst ***7 G"002 ..0"5   !1 2B4K4KA4N rct}|jd|\}}|jt|jd|jdj t t|j}|j||jd|jt|jt|jdy)z When a client connection fails, the service removes its reference to the protocol and tries again after a timeout. Frr)rc,|jtSr")trapr )rs rr.z@ClientServiceTests.test_clientConnectionFailed..sn1Mrr{N) rrr\r]rr+rrrr addErrbackr r )r&r~rrrs rtest_clientConnectionFailedz.ClientServiceTests.test_clientConnectionFaileds **5*N G R__-q1 ""79;#78--/  M*   !MN *+ R__-q1rct}|j|d\}}|jt|jd|jdj d|jt|jd|j |j|j|jd|jdjtt|jt|jt|jd|jdj d|j |j|j|jdy)z~ When a client connection is lost, the service removes its reference to the protocol and calls retry. F)r~rr)rNr{)rrr\r]rr6rOrrrrrrrr r )r&r~rrs rtest_clientConnectionLostz,ClientServiceTests.test_clientConnectionLosts2 ***N G R__-q1 ##D) R__-q1   !6!6!8 92;R;RST;U  "11')+2FG *+ R__-q1 ##D)   !6!6!8 92;R;RST;U rc>t}|j|\}}|j}|jdj t t |j|jt|j|jy)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. rrN) rrrkrrrIndentationErrorrrr rcalledr s r&test_clientConnectionLostWhileStoppingz9ClientServiceTests.test_clientConnectionLostWhileStoppings{ ***7 G    ! "11':J:L2MN W224nE !rc|jdd\}}|jt|jd|j |jt|jdt |}|j |jt|jd|j d|dy)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)rrUrr)z$Duplicate ClientService.startServiceN)rr\r]rrUrassertIn)r&rrmessagess rtest_startTwicez"ClientServiceTests.test_startTwices **5u*U G R__-q1 R__-q1T? R__-q1 '>q'AB " "Q " 7''+ Xx0rct}|jd|\}}|j}|j}|jd}|j||j||j||j |j t |j|t|j|t|j|ty)z L{ClientService.whenConnected} returns a L{Deferred} that fails when L{ClientService.stopService} is called. Frr)r"N) rrrrrkr r rr )r&r~rrr$r%r&s rtest_whenConnectedStopServicez0ClientServiceTests.test_whenConnectedStopServiceqs **5*N G  ! ! #  ! ! #  ! !A ! 6 A A A *+ Q/ Q/ Q/rct}|jd|\}}|jdjt d|j }|j |jg|j|y)z When L{ClientService.stopService} is called while waiting between connection attempts, the pending reconnection attempt is cancelled and the service is stopped immediately. Frr no connectionN) rrrr+rrkr\rrr s rtest_retryCancelledz&ClientServiceTests.test_retryCancelledsu **5*N G ""9_#=>    ! ..0"5 Qrct}|jdd|\}}|j}|j|j |y)z Calling L{ClientService.stopService} before L{ClientService.startService} returns a L{Deferred} that has already fired with L{None}. F)rrUr~Nrrrk assertIsNoner)r&r~_rr-s r"test_stopServiceBeforeStartServicez5ClientServiceTests.test_stopServiceBeforeStartServicesT ))!U* 7    ! $..q12rct}|jd|\}}|j}|jdj t d|j |j}|j|j|jt|j|j|jty)z L{ClientService.whenConnected} returns a L{Deferred} that errbacks with L{CancelledError} if L{ClientService.stopService} is called between connection attempts. Frrr<N) rrrrr+rrkrrrr )r&r~rrbeforeErrbackAndStopafterErrbackAndStops r'test_whenConnectedErrbacksOnStopServicez:ClientServiceTests.test_whenConnectedErrbacksOnStopServices**5*N G&446 ""9_#=>%335   !5 6 < .prepareConnection QK1 KrT)rVrUr)N)rr\)r&rVrrrUs @r-test_prepareConnectionCalledWhenServiceStartsz@ClientServiceTests.test_prepareConnectionCalledWhenServiceStartssG 3 **/d+ G HQK(rcgfd}|j|\}}|j|jddy)zf The C{prepareConnection} callable is passed the connected protocol instance. c(j|yr"rm)proto newProtocolss rrVzVClientServiceTests.test_prepareConnectionCalledWithProtocol..prepareConnection s    &rrVrN)rrOr)r&rVrrr]s @r(test_prepareConnectionCalledWithProtocolz;ClientServiceTests.test_prepareConnectionCalledWithProtocolsN   '**/+ G R44Q7aIrc`dgfd}t}|j|d|\}}|jdjt d|j dd|j t|jdjd|j ddy)zf The C{prepareConnection} callback is invoked only once a connection is made. rc dxxdz cc<yrRrrSs rrVz]ClientServiceTests.test_prepareConnectionCalledAfterConnectionMade..prepareConnectionrWrF)rVrr~zconnection attempt failedr)N) rrrr+rr\r r r6r&rVr~rrrUs @r/test_prepareConnectionCalledAfterConnectionMadezBClientServiceTests.test_prepareConnectionCalledAfterConnectionMades 3 **/e+ G ""9-H#IJ HQK( *+ ##D) HQK(rc2dgfd}t}|j||\}}|jdd|jdj t t |jt|jddy)z The C{prepareConnection} callback is invoked each time a connection is made, including on reconnection. rc dxxdz cc<yrRrrSs rrVzUClientServiceTests.test_prepareConnectionCalledOnReconnect..prepareConnection/rWrrVr~r)r{N) rrr\rrrrr r rbs @r'test_prepareConnectionCalledOnReconnectz:ClientServiceTests.test_prepareConnectionCalledOnReconnect(s 3 **/u+ G HQK( "11':J:L2MN *+ HQK(rctfd}|j|\}}|j|j}|j |y)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. cSr"r)r\sentinels rrVzVClientServiceTests.test_prepareConnectionReturnValueIgnored..prepareConnectionJsOrr^N)rrrrassertNotIdentical)r&rVrrr2rjs @r(test_prepareConnectionReturnValueIgnoredz;ClientServiceTests.test_prepareConnectionReturnValueIgnored?sT8 **=N*O G%%g&;&;&=> &1rcgtfd}|j|\}}|j}|j|j d|j |j d|j|y)z The C{prepareConnection} callable returns a deferred and calls to L{ClientService.whenConnected} wait until it fires. c*j|Sr"r[)r\newProtocolDeferredr]s rrVzVClientServiceTests.test_prepareConnectionReturningADeferred..prepareConnectionZs    && &rr^Nr)r rrrr6rOrr)r&rVrrwhenConnectedDeferredror]s @@r(test_prepareConnectionReturningADeferredz;ClientServiceTests.test_prepareConnectionReturningADeferredRs  &j '**=N*O G ' 5 5 7 12$$T*   # #A &(<(<=R(S rcdt}d}|j||\}}|jd}|j||j t |j||j t |j t|j|jy)zs The connection attempt counts as a failure when the C{prepareConnection} callable throws. ctr")r)rTs rrVzJClientServiceTests.test_prepareConnectionThrows..prepareConnectionps "$ $rrfr{r"N) rrrrr r rOrrtype)r&r~rVrrrps rtest_prepareConnectionThrowsz/ClientServiceTests.test_prepareConnectionThrowsis  %**/u+ G!( 5 5 5 J 12 *+ 12 *+  d223HINN rN)$r;r<r=r>rrrrrrrrrr rrrrrr r*r8r:r=rBrFrKrMrOrXr_rcrgrlrqrurrrrrs #HAF2 6  : +8 "J(      2" * " MB*81t0&   3 21.10 D ) J).).2& . rr)F)0r>rzope.interfacerzope.interface.verifyrtwisted.applicationrtwisted.application.internetrrrtwisted.internetr twisted.internet.deferr r twisted.internet.interfacesr r rrrtwisted.internet.protocolrrtwisted.internet.taskrtwisted.internet.testingrtwisted.loggerrrtwisted.python.failurertwisted.trial.unittestrrrr r#rHryrrrr rrrrrs &-( ";8'4:*@  "#=*=*$=*@ !:. ^   6 !:.V18V1rC-C-L""( &F&8:$B  ,B  r