+
    Dfj                        R t ^ RIt^ RIHt ^ RIHt ^ RIHt ^ RIH	t	H
t
Ht ^ RIHt ^ RIHtHt ^ RIHtHtHtHtHt ^ R	IHtHt ^ R
IHt ^ RIHt ^ RIHtH t  ^ RI!H"t" ^ RI#H$t$H%t% R t&]! ]4       ! R R4      4       t']! ]]'4       ]! ]4       ! R R4      4       t(]! ]]'4        ! R R]%4      t) ! R R]%4      t* ! R R4      t+RR lt,] 3R lt-Rt. ! R R]$4      t/R# ) 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)StringTransport)formatEventglobalLogPublisher)Failure)SynchronousTestCaseTestCasec                     R# )zE
A fake target function for testing TimerService which does nothing.
N r       H/usr/lib/python3/dist-packages/twisted/application/test/test_internet.pyfakeTargetFunctionr   )   s     	r   c                   Z   a  ] tR t^0t o RtRtRtRt]! 4       t	^ t
R tR tR tR tRtV tR# )
FakeServeraA  
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.
Nc                $    \        4       V n        R # N)FakePortportselfs   &r   __init__FakeServer.__init__L   s    J	r   c                   a  S ;P                   ^,          un         VS n        \        V 3R lR7      S n        S P                  e&   S P                  P                  S P                  4       S P                  # )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                 :   < V P                  SP                  4      # r!   )errbackcancelException)dr%   s   &r   <lambda>#FakeServer.listen.<locals>.<lambda>Z   s    199T=Q=Q3Rr   )	canceller)listenAttemptsfactoryr
   resultfailImmediatelyr*   )r%   r1   s   f&r   listenFakeServer.listenO   sX     	q )RS+KK 4 45{{r   c                P    V P                   P                  V P                  4       R# )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)r2   callbackr#   r$   s   &r   startedListeningFakeServer.startedListening_   s     	TYY'r   c                P    V P                   P                  P                  R4       R# )z
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#   deferredr7   r$   s   &r   stoppedListeningFakeServer.stoppedListeningg   s     			##D)r   )r1   r#   r2   )__name__
__module____qualname____firstlineno____doc__r2   r1   r3   r	   r+   r0   r&   r4   r8   r<   __static_attributes____classdictcell____classdict__s   @r   r   r   0   sA     ( FGO$&ON (* *r   r   c                   :   a  ] tR t^tt o RtRtR tR tR tRt	V t
R# )r"   zg
Fake L{IListeningPort} implementation.

@ivar deferred: The L{Deferred} returned by C{stopListening}.
Nc                8    \        4       V n        V P                  # )zH
Stop listening.

@return: a L{Deferred} stored in L{FakePort.deferred}
)r
   r;   r$   s   &r   stopListeningFakePort.stopListening~   s     !
}}r   c                    R # r!   r   r$   s   &r   getHostFakePort.getHost       r   c                    R # r!   r   r$   s   &r   startListeningFakePort.startListening   rN   r   r;   )r>   r?   r@   rA   rB   r;   rI   rL   rP   rC   rD   rE   s   @r   r"   r"   t   s%      H r   r"   c                   p   a  ] tR t^t o RtR tR tRR ltR tR t	R t
R	 tR
 tR tR tR tR tRtV tR# )EndpointServiceTestsz,
Tests for L{twisted.application.internet}.
c                    \        4       V n        \        4       V n        \	        V P                  V P                  4      V n        R# )zX
Construct a stub server, a stub factory, and a
L{StreamServerEndpointService} to test.
N)r   
fakeServerr   r1   r   svcr$   s   &r   setUpEndpointServiceTests.setUp   s,    
 %,y.tMr   c                    V P                   P                  4        V P                  V P                  V P                  P                  4       R# )zo
L{StreamServerEndpointService.privilegedStartService} calls its
endpoint's C{listen} method with its factory.
N)rW   privilegedStartServiceassertIdenticalr1   rV   r$   s   &r   test_privilegedStartService0EndpointServiceTests.test_privilegedStartService   s2    
 	'')T\\4??+B+BCr   Nc                    \        4       V P                  n        RV P                  n        T P                  \         T;'       g    V P                  P                  4       R# )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)ZeroDivisionErrorrV   r3   rW   _raiseSynchronouslyassertRaisesstartService)r%   thunks   &&r   (test_synchronousRaiseRaisesSynchronously=EndpointServiceTests.test_synchronousRaiseRaisesSynchronously   sB      +<*=''+$+U-K-Kdhh6K6KLr   c                P    V P                  V P                  P                  4       R# )z
L{StreamServerEndpointService.privilegedStartService} should behave the
same as C{startService} with respect to
L{EndpointServiceTests.test_synchronousRaiseRaisesSynchronously}.
N)re   rW   r[   r$   s   &r   test_synchronousRaisePrivileged4EndpointServiceTests.test_synchronousRaisePrivileged   s     	55dhh6U6UVr   c                    V P                   P                  4        V P                  P                  P	                  \        4       4       V P                  \
        4      pV P                  \        V4      ^4       R# )z
L{StreamServerEndpointService.startService} and
L{StreamServerEndpointService.privilegedStartService} should both log
an exception when the L{Deferred} returned from their wrapped
L{IStreamServerEndpoint.listen} fails.
N)	rW   rc   rV   r2   r*   r`   flushLoggedErrorsassertEquallenr%   loggeds   & r   test_failReportsError*EndpointServiceTests.test_failReportsError   sU     	&&'8':;''(9:Va(r   c                   RV P                   n        V P                   P                  4        V P                  P                  P                  \        4       4       V P                  \        4      pV P                  \        V4      ^4       R# )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 asynchronously, even if
C{_raiseSynchronously} is set.
TN)
rW   ra   rc   rV   r2   r*   r`   rk   rl   rm   rn   s   & r   !test_asynchronousFailReportsError6EndpointServiceTests.test_asynchronousFailReportsError   sa     (,$&&'8':;''(9:Va(r   c                    \        4       V P                  n        V P                  P	                  4        V P                  \         4      pV P                  \        V4      ^4       R# )z
Without the C{_raiseSynchronously} compatibility flag, failing
immediately has the same behavior as failing later; it logs the error.
N)r`   rV   r3   rW   rc   rk   rl   rm   rn   s   & r    test_synchronousFailReportsError5EndpointServiceTests.test_synchronousFailReportsError   sH    
 +<*='''(9:Va(r   c                    V P                   P                  4        V P                  V P                  V P                  P                  4       V P                  V P                   P                  R4       R# )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)rW   rc   r\   r1   rV   rl   runningr$   s   &r   test_startServiceUnstarted/EndpointServiceTests.test_startServiceUnstarted   sL     	T\\4??+B+BC))40r   c                    V P                  4        V P                  P                  4        V P                  V P                  P
                  ^4       V P                  V P                  P                  R4       R# )z
L{StreamServerEndpointService.startService} sets the C{running} flag,
but nothing else, if the service has already been started.
TN)r]   rW   rc   rl   rV   r0   ry   r$   s   &r   test_startServiceStarted-EndpointServiceTests.test_startServiceStarted   sT    
 	((*77;))40r   c                   V P                   P                  4        V P                  P                  4        V P                   P	                  4        V P                   P                  4       p. pVP                  VP                  4       V P                  \        V4      ^ 4       V P                  P                  4        V P                  \        V4      ^4       V P                  V P                   P                  4       R# )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}.
N)rW   r[   rV   r8   rc   stopServiceaddCallbackappendrl   rm   r<   assertFalsery   r%   r2   ls   &  r   test_stopService%EndpointServiceTests.test_stopService   s     	'')((*%%'188$Q#((*Q#))*r   c                   V P                   P                  4        V P                   P                  4       p. pVP                  VP                  4       V P                  VR.4       V P                  V P                  \        4      . 4       R# )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)rW   r[   r   addBothr   rl   rk   r	   r   s   &  r   #test_stopServiceBeforeStartFinished8EndpointServiceTests.test_stopServiceBeforeStartFinished  sh     	'')%%'qxx TF#//?Dr   c                `   \        4       V P                  n        V P                  P	                  4        V P                  P                  4       p. pVP                  VP                  4       V P                  VR.4       V P                  \         4      pV P                  \        V4      ^4       R# )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}.
N)r`   rV   r+   rW   r[   r   r   r   rl   rk   rm   )r%   r2   r   stoppingErrorss   &   r    test_stopServiceCancelStartError5EndpointServiceTests.test_stopServiceCancelStartError  s     +<*=''')%%'188$TF#//0AB^,a0r   )r1   rV   rW   r!   )r>   r?   r@   rA   rB   rX   r]   re   rh   rp   rs   rv   rz   r}   r   r   r   rC   rD   rE   s   @r   rT   rT      sP     NDM(W
)))11+$E1 1r   rT   c                   `   a  ] tR tRt o RtR tR tR tR tR t	R t
R	 tR
 tR tR tRtV tR# )TimerServiceTestsi,  a	  
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}.
c                    \        ^V P                  4      V n        \        P                  ! 4       ;V n        V P                  n        \        4       V n        R# )z!
Set up a timer service to test.
N)r   calltimerr   r   clockr
   r;   r$   s   &r   rX   TimerServiceTests.setUp:  s8     "!TYY/
(,

4
TZZ% 
r   c                    V P                   # )zb
Function called by L{TimerService} being tested.

@returns: C{self.deferred}
@rtype: L{Deferred}
rR   r$   s   &r   r   TimerServiceTests.callB  s     }}r   c                   V P                   P                  4        V P                  V P                   P                  R4       V P	                  V P                   P
                  \        P                  4       V P                  V P                  V P                   P
                  P                  4       V P                  V P                   P
                  P                  R4       R# )zw
When L{TimerService.startService} is called, it marks itself
as running, creates a L{task.LoopingCall} and starts it.
zService is startedzLoopingCall is startedN)
r   rc   
assertTruery   assertIsInstance_loopr   LoopingCallr\   r   r$   s   &r   test_startService#TimerServiceTests.test_startServiceK  s    
 	

!

**,@Adjj..0@0@ATZZ)9)9)?)?@

((002JKr   c                    . pVP                   R/ 3V P                  n        V P                  P                  4        V P	                  R.V4       R# )zQ
When L{TimerService.startService} is called, it calls the function
immediately.
Nr!   )r   r   r   rc   rl   )r%   r2   s   & r   $test_startServiceRunsCallImmediately6TimerServiceTests.test_startServiceRunsCallImmediatelyV  sA    
 !=='26



!$(r   c                   a \         P                  ! 4       oV3R lpV P                  \        RV4       V P                  P                  4        V P                  SV P                  P                  P                  4       R# )z
L{TimerService.startService} uses L{internet._maybeGlobalReactor} to
choose the reactor to pass to L{task.LoopingCall}
uses the global reactor.
c                    < S# r!   r   )maybeReactor
otherClocks   &r   getOtherClockKTimerServiceTests.test_startServiceUsesGlobalReactor.<locals>.getOtherClockh  s	    r   _maybeGlobalReactorN)	r   r   patchr   r   rc   r\   r   r   )r%   r   r   s   & @r   "test_startServiceUsesGlobalReactor4TimerServiceTests.test_startServiceUsesGlobalReactor`  sX     ZZ\
	 	

82MB

!Z)9)9)?)?@r   c                d   V P                   P                  4        V P                   P                  4       pV P                  V4       V P	                  RV P                   P
                  4       V P                  P                  \        4       4       V P                  V P                  V4      R4       R# )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   rc   r   assertNoResultrl   ry   r;   r7   objectr\   successResultOfr%   r,   s   & r   test_stopServiceWaits'TimerServiceTests.test_stopServiceWaitso  s}    
 	

!JJ""$Atzz112vx(T11!4d;r   c                    V P                   P                  4        V P                  P                  \	        4       4       V P                   P                  4       pV P                  V P                  V4      R4       R# )z}
When L{TimerService.stopService} is called while a call isn't in progress.
the L{Deferred} returned has already been fired.
N)r   rc   r;   r7   r   r   r\   r   r   s   & r   test_stopServiceImmediately-TimerServiceTests.test_stopServiceImmediately{  sU    
 	

!vx(JJ""$T11!4d;r   c                h   V P                   P                  4        V P                  P                  \	        \        4       4      4       V P                  \
        4      pV P                  ^\        V4      4       V P                   P                  4       pV P                  V P                  V4      R4       R# )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.
N)r   rc   r;   r*   r   r`   rk   rl   rm   r   r\   r   )r%   errorsr,   s   &  r   test_failedCallLogsError*TimerServiceTests.test_failedCallLogsError  s    
 	

!g&7&9:;''(9:CK(JJ""$T11!4d;r   c                   \        ^\        4      pVP                  4        \        P                  ! V4      pVP                  4        \        P                  ! V4      p\        4       p\        VRV4      pV P                  WE4       R# )zY
When pickling L{internet.TimerService}, it won't pickle
L{internet.TimerService._loop}.
r   N
r   r   rc   pickledumpsr   loadsr   getattrr\   r%   r   dumpedTimerloadedTimernothingvalues   &     r   $test_pickleTimerServiceNotPickleLoop6TimerServiceTests.test_pickleTimerServiceNotPickleLoop  sh     Q 23ll5)ll;/(Wg6W,r   c                   \        ^\        4      pVP                  4        \        P                  ! V4      pVP                  4        \        P                  ! V4      p\        4       p\        VRV4      pV P                  WE4       R# )za
When pickling L{internet.TimerService}, it won't pickle
L{internet.TimerService._loopFinished}.
_loopFinishedNr   r   s   &     r   ,test_pickleTimerServiceNotPickleLoopFinished>TimerServiceTests.test_pickleTimerServiceNotPickleLoopFinished  sh     Q 23ll5)ll;/(_g>W,r   )r   r;   r   N)r>   r?   r@   rA   rB   rX   r   r   r   r   r   r   r   r   r   rC   rD   rE   s   @r   r   r   ,  sC     #	L)A
<<
<- - -r   r   c                   *   a  ] tR tRt o RtR tRtV tR# )ConnectInformationi  a  
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                0    . V n         . V n        . V n        R # r!   connectQueueconstructedProtocolspassedFactoriesr$   s   &r   r&   ConnectInformation.__init__  s    $&!!r   r   N)r>   r?   r@   rA   rB   r&   rC   rD   rE   s   @r   r   r     s     " "r   r   c                l   a a \        \        4       ! V V3R lR4      4       p\        4       oSV! 4       3# )a(  
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}.
c                   0   <a  ] tR tRt o VV3R ltRtV tR# ).endpointForTesting.<locals>.ClientTestEndpointi  c                   <a \        4       pSP                  P                  S4       VP                  VV3R  l4       pSP                  P                  V4       S'       d   VP                  R4       V# )c                    < SP                  R 4      pSP                  P                  V4       \        4       pVP	                  V4       V# r!   )buildProtocolr   r   r   makeConnection)ignoredprotocol	transportr1   infos   &  r   createProtocolNendpointForTesting.<locals>.ClientTestEndpoint.connect.<locals>.createProtocol  sB    "006))00:+-	''	2r   N)r
   r   r   r   r   r7   )r%   r1   r2   r   fireImmediatelyr   s   &f  r   connect6endpointForTesting.<locals>.ClientTestEndpoint.connect  s`    ZF  ''0     $$V,%Mr   r   N)r>   r?   r@   rA   r   rC   rD   )rF   r   r   s   @r   ClientTestEndpointr     s     	 	r   r   )r   r   r   )r   r   r   s   f @r   endpointForTestingr     s;     &'  ($ D#%%%r   c                x   aa . oSP                  SP                  4       V P                  VV3R l4       V3R l# )a  
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                  :   < S P                  SP                  4      # r!   )removeObserverr   )logPublisherlogss   r   r-   catchLogs.<locals>.<lambda>  s     ; ;DKK Hr   c                  D   < S U u. uF  p \        V 4      NK  	  up # u up i r!   )r   )eventr   s    r   r-   r     s    D9D5K&D99s   )addObserverr   
addCleanup)testCaser   r   s   &f@r   	catchLogsr     s1     DT[[)HI99r   g      Y@c                      a  ] tR tRt o RtRR]R3R ltR tR tR t	R	 t
R
 tR tR tR tR tR tR tR tV 3R lR ltR tR tR tR tR tR tR tR tR tR tR tR tR  t R! t!R" t"R# t#R$ t$R% t%R&t&V t'R'# )(ClientServiceTestsi  z
Tests for L{ClientService}.
TFc                n  a aaaaa / pVP                  \        4       R7       VP                  V4       VR,          o\        VR7      w  rx. ;oVn         ! VVV3R lR\        4      p	V	! 4       ;Vn        p
\        W3/ VB oVV V3R lpS P                  V4       V'       d   SP                  4        VS3# )a  
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   c                   >   <a a ] tR tRt o ZtV VV3R ltRtVtV ;t# )>ClientServiceTests.makeReconnector.<locals>.RememberingFactoryi2  c                \   < S'       d   R # \         SV `  V4      pSP                  V4       V# r!   )superr   r   )r%   addrr2   	__class__applicationProtocolsrefuseConnectionss   && r   r   LClientServiceTests.makeReconnector.<locals>.RememberingFactory.buildProtocol5  s+    $.t4$++F3r   r   )	r>   r?   r@   rA   r   r   rC   rD   __classcell__)r   rF   r   protocolTyper   s   @@r   RememberingFactoryr   2  s     #H r   r  c                     < R Sn         SP                  '       d   SP                  4        SP                  S P	                  4       . 4       R # r!   )	_protocolry   r   rl   getDelayedCalls)r   r%   services   r   stop0ClientServiceTests.makeReconnector.<locals>.stop@  s;     $G##% U224b9r   )	updater   r   r   r   applicationFactoryr   r   rc   )r%   r   rc   r  r   kwnkwcqendpointr  r1   r  r   r   r  s   f&&ff,      @@@r   makeReconnector"ClientServiceTests.makeReconnector  s    B 


!

2G)/J :<;r6	 	 	 +=*>>9S9	: 	  "7{r   c                x    V P                  RR7      w  rV P                  \        VP                  4      ^4       R# )z<
When the service is started, a connection attempt is made.
Fr   N)r  rl   rm   r   )r%   r  r  s   &  r   r   $ClientServiceTests.test_startServiceM  s2     **5*AR__-q1r   c                    V P                  4       w  rVP                  pV P                  VP                  ^ 4       VP                  ^ ,          pVP                  4        V P                  VP                  ^4       R# )z
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.
N)r  r
  rl   numPortsr   doStart)r%   r  r  firstAppFactoryfirstPassedFactorys   &    r   test_startStopFactory(ClientServiceTests.test_startStopFactoryT  sh     **,//1115//2""$1115r   c                D   V P                  4       w  rVP                  4       pV P                  V4       VP                  ^ ,          pV P	                  VP
                  P                  R4       VP                  \        \        4       4      4       V P                  V4       R# )z
When the service is stopped, no further connect attempts are made.  The
returned L{Deferred} fires when all outstanding connections have been
stopped.
TN)r  r   r   r   rl   r   disconnectingconnectionLostr   	Exceptionr   r%   r  r  r,   r   s   &    r   test_stopServiceWhileConnected1ClientServiceTests.test_stopServiceWhileConnectedb  s     **,!A**1-++994@	 45Qr   c                   V P                  4       w  rVP                  4       pV P                  V4       VP                  ^ ,          pV P	                  VP
                  P                  R4       VP                  4        V P                  V4       V P	                  \        VP                  4      ^4       VP                  \        \        R4      4      4       V P	                  \        VP                  4      ^4       R# )z
When L{ClientService} is restarted after having been connected, it
waits to start connecting until after having disconnected.
Tzsynthetic connectionLostN)r  r   r   r   rl   r   r  rc   rm   r  r   r  r  s   &    r   #test_startServiceWaitsForDisconnect6ClientServiceTests.test_startServiceWaitsForDisconnectp  s    
 **,!A**1-++994@AR445q9	2L(M NOR445q9r   c                h   V P                  RR7      w  rVP                  ^ ,          P                  R4       VP                  ^ ,          pVP	                  4       pV P                  V4       VP                  4       pV P                  V4       VP                  4        V P                  V4       V P                  V4       V P                  VP                  P                  R4       VP                  \        \        R4      4      4       V P                  V4       VP                  ^,          P                  R4       V P                  \        VP                  4      ^4       V P!                  V P                  V4      VP"                  ^,          4       VP	                  4       pV P                  V4       R# )z
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   NTzsynthetic connection lost)r  r   r7   r   r   r   whenConnectedrc   rl   r   r  r  r   r  r   rm   r\   r   )r%   r  r  firststoppednextProtocolsecondStoppeds   &      r   test_startServiceWhileStopping1ClientServiceTests.test_startServiceWhileStopping  sZ    **5*A
##D)''*%%'G$,,.L)L)G$66=WY/J%KLMW%
##D)R445q9  .0G0G0J	
  ++-M*r   c                   V P                  RR7      w  rVP                  4       pV P                  V4       V P                  VP	                  4       \
        4       VP                  4        VP                  R,          P                  R4       V P                  VP                  R,          V P                  VP	                  4       4      4       R# )z
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  r   r   failureResultOfr%  r	   rc   r   r7   r\   r   )r%   r  r  r'  s   &   r   test_startServiceWhileStopped0ClientServiceTests.test_startServiceWhileStopped  s     **5*A%%'W%W224nE
$$T*##B')=)=g>S>S>U)V	
r   c                4   \        \        \        4       ! R R\        4      4       pV P	                  VR7      w  r#VP
                  ^ ,          pV P                  \        P                  ! V4      4       V P                  \        P                  ! V4      4       R# )a  
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                       ] tR tRtRtRtR# )EClientServiceTests.test_interfacesForTransport.<locals>.FancyProtocoli  z!
Provider of various interfaces.
r   N)r>   r?   r@   rA   rB   rC   r   r   r   FancyProtocolr3    s    r   r4  )r  N)r   r   r   r   r  r   r   
providedBy)r%   r4  r  r  reactorFacings   &    r   test_interfacesForTransport.ClientServiceTests.test_interfacesForTransport  s~     
+-D	E	H 	 
F	
 ***F//2/::=IJ.99-HIr   c                   \        4       pV P                  RVR7      w  r#VP                  ^ ,          P                  \	        4       4       VP                  \        4       V P                  \        VP                  4      ^4       VP                  4       pVP                  ^,          P                  \	        4       4       V P                  V4       R# )zE
When the service is stopped while retrying, the retry is cancelled.
Fr   r   N)r   r  r   r*   r  advanceAT_LEAST_ONE_ATTEMPTrl   rm   r   r   r%   r   r  r  r,   s   &    r   test_stopServiceWhileRetrying0ClientServiceTests.test_stopServiceWhileRetrying  s     **5*N
""9;/*+R__-q1!
""9;/Qr   c                   \        4       pV P                  RVR7      w  r#V P                  \        VP                  4      ^4       V P                  VP                  ^ ,          4       VP                  4       pV P                  V4       R# )z^
When the service is stopped while initially connecting, the connection
attempt is cancelled.
Fr:  N)r   r  rl   rm   r   r   r   r   r=  s   &    r   test_stopServiceWhileConnecting2ClientServiceTests.test_stopServiceWhileConnecting  sn    
 **5*NR__-q1BOOA./!Qr   c                f   \        4       pV P                  VR7      w  r#VP                  4       pV P                  VP	                  4       . 4       V P                  V P                  V4      VP                  ^ ,          4       V P                  VP                  ^ ,          P                  VP                  ^ ,          4       V P                  \        VP                  ^ ,          4      \        VP                  ^ ,          4      4       V P                  R\        VP                  ^ ,          4      4       R# )za
When a client connects, the service keeps a reference to the new
protocol and resets the delay.
r   r  N)r   r  r%  rl   r  r\   r   r   r   r  assertInstrr   )r%   r   r  r  awaitingProtocols   &    r   test_clientConnected'ClientServiceTests.test_clientConnected  s    
 ***7"002..0"5  !12B4K4KA4N	
 	##A&00"2I2I!2L	
 	''*+''*+	
 	 ""1%&	
r   c                   \        4       pV P                  RVR7      w  r#V P                  \        VP                  4      ^4       VP                  ^ ,          P                  \        \        4       4      4       VP                  4       pV P                  V4       VP                  R 4       VP                  \        4       V P                  \        VP                  4      ^4       R# )zt
When a client connection fails, the service removes its reference
to the protocol and tries again after a timeout.
Fr:  c                 ,    V P                  \        4      # r!   )trapr	   )r   s   &r   r-   @ClientServiceTests.test_clientConnectionFailed.<locals>.<lambda>  s    n1Mr   N)r   r  rl   rm   r   r*   r   r  r%  r   
addErrbackr;  r<  r%   r   r  r  r%  s   &    r   test_clientConnectionFailed.ClientServiceTests.test_clientConnectionFailed  s    
 **5*NR__-q1
""79;#78--/M* 	  !MN*+R__-q1r   c                :   \        4       pV P                  VRR7      w  r#V P                  \        VP                  4      ^4       VP                  ^ ,          P                  R4       V P                  \        VP                  4      ^4       V P                  V P                  VP                  4       4      VP                  ^ ,          4       VP                  ^ ,          P                  \        \        4       4      4       VP                  \        4       V P                  \        VP                  4      ^4       VP                  ^,          P                  R4       V P                  V P                  VP                  4       4      VP                  ^,          4       R# )zf
When a client connection is lost, the service removes its reference
to the protocol and calls retry.
F)r   r   N)r   r  rl   rm   r   r7   r\   r   r%  r   r   r  r   r  r;  r<  )r%   r   r  r  s   &   r   test_clientConnectionLost,ClientServiceTests.test_clientConnectionLost  s0   
 ***NR__-q1
##D)R__-q1  !6!6!892;R;RST;U	
 	"11')+2FG*+R__-q1
##D)  !6!6!892;R;RST;U	
r   c                   < V ^8  d   QhRR/# )   returnNr   )formatrF   s   "r   __annotate__ClientServiceTests.__annotate__  s     $+ $+d $+r   c                  a a \        4       pS P                  V V3R l4       S P                  VRRR7      w  r#S P                  \	        VP
                  4      ^4       VP
                  P                  ^ 4      P                  R4       S P                  \	        VP
                  4      ^ 4       VP                  4       oS P                  S4       VP                  \        4       S P                  \	        VP
                  4      ^4       VP
                  P                  ^ 4      P                  R4       S P                  S4       R# )z
When the client factory refuses connections by returning None from
buildProtocol, the factory keeps attempting new connections.
c                  0   < S P                  S\        4      # r!   )r.  r	   )r%   r%  s   r   r-   AClientServiceTests.test_clientConnectionRefused.<locals>.<lambda>"  s     4 4]N Sr   FT)r   r   r   N)r   r   r  rl   rm   r   popr7   r%  r   r;  r<  rN  s   f   @r   test_clientConnectionRefused/ClientServiceTests.test_clientConnectionRefused  s    
 
 	ST**!"	 + 
 	R__-q1
A''-R__-q1--/M* 	*+R__-q1
A''-M*r   c                F   \        4       pV P                  VR7      w  r#VP                  4       pVP                  ^ ,          P	                  \        \        4       4      4       V P                  VP                  4       \        4       V P                  VP                  4       R# )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   N)r   r  r   r   r  r   IndentationErrorr.  r%  r	   r   calledr=  s   &    r   &test_clientConnectionLostWhileStopping9ClientServiceTests.test_clientConnectionLostWhileStopping>  sy     ***7!
"11':J:L2MNW224nE!r   c                   V P                  RRR7      w  rV P                  \        VP                  4      ^ 4       VP	                  4        V P                  \        VP                  4      ^4       \        V 4      pVP	                  4        V P                  \        VP                  4      ^4       V P                  RV! 4       ^ ,          4       R# )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   rc   z$Duplicate ClientService.startServiceN)r  rl   rm   r   rc   r   rD  )r%   r  r  messagess   &   r   test_startTwice"ClientServiceTests.test_startTwiceK  s     **5u*UR__-q1R__-q1T?R__-q1<hjmLr   c                @   \        4       pV P                  RVR7      w  r#VP                  4       pVP                  4       pVP                  ^R7      pV P                  V4       V P                  V4       V P                  V4       VP                  ^ ,          P                  R4       V P                  V4      pV P                  V4      pV P                  V4      p	V P                  Wx4       V P                  Wy4       V P                  WrP                  ^ ,          4       R# )zc
L{ClientService.whenConnected} returns a L{Deferred} that fires when a
connection is established.
Fr:  failAfterFailuresN)	r   r  r%  r   r   r7   r   r\   r   )
r%   r   r  r  abcresultAresultBresultCs
   &         r   test_whenConnectedLater*ClientServiceTests.test_whenConnectedLaterZ  s    
 **5*N!!#!!#!!A!6AAA
##D)&&q)&&q)&&q)W.W.W&=&=a&@Ar   c                Z   \        4       pV P                  RVR7      w  r#VP                  4       pVP                  ^R7      pVP                  ^R7      pVP                  ^R7      pV P                  V4       V P                  V4       V P                  V4       V P                  V4       \	        \        4       4      pVP                  ^ ,          P                  V4       V P                  V4       V P                  V P                  V\
        4      V4       V P                  V4       V P                  V4       VP                  \        4       V P                  \        VP                  4      ^4       V P                  V4       V P                  V4       V P                  V4       \	        \        4       4      p	VP                  ^,          P                  V	4       V P                  V4       V P                  V P                  V\
        4      V	4       V P                  V4       \        p
VP                  V
4       V P                  \        VP                  4      ^4       V P                  V4       V P                  V4       VP                  ^,          P                  R4       V P                  V4      pV P                  V4      pV P                  W4       V P                  WP                   ^ ,          4       VP                  ^R7      pV P                  V4      pV P                  W4       R# )zy
L{ClientService.whenConnected} returns a L{Deferred} that fails, if
asked, when some number of connections have failed.
Fr:  rj  N)r   r  r%  r   r   r  r   r*   r\   r.  r;  r<  rl   rm   r7   r   r   )r%   r   r  r  a0a1a2a3f1f2AT_LEAST_TWO_ATTEMPTSresultA0resultA3a4resultA4s   &              r   test_whenConnectedFails*ClientServiceTests.test_whenConnectedFailso  s   
 **5*N""$""Q"7""Q"7""Q"7BBBBY[!
""2&BT11"i@"EBB*+R__-q1BBBY[!
""2&BT11"i@"EB 4+,R__-q1BB
##D)''+''+X0X'>'>q'AB ""Q"7''+X0r   c                   \        4       pV P                  RVR7      w  r#VP                  4       pVP                  4       pVP                  ^R7      pV P                  V4       V P                  V4       V P                  V4       VP	                  4        VP                  \        4       V P                  V\        4       V P                  V\        4       V P                  V\        4       R# )zn
L{ClientService.whenConnected} returns a L{Deferred} that fails when
L{ClientService.stopService} is called.
Fr:  rj  N)	r   r  r%  r   r   r;  r<  r.  r	   )r%   r   r  r  rl  rm  rn  s   &      r   test_whenConnectedStopService0ClientServiceTests.test_whenConnectedStopService  s    
 **5*N!!#!!#!!A!6AAA*+Q/Q/Q/r   c                ,   \        4       pV P                  RVR7      w  r#VP                  P                  ^ 4      P	                  \        R4      4       VP                  4       pV P                  VP                  4       . 4       V P                  V4       R# )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:  no connectionN)
r   r  r   r]  r*   r  r   rl   r  r   r=  s   &    r   test_retryCancelled&ClientServiceTests.test_retryCancelled  sy     **5*N
A&&y'AB!..0"5Qr   c                    \        4       pV P                  RRVR7      w  r#VP                  4       pV P                  V P	                  V4      4       R# )z
Calling L{ClientService.stopService} before
L{ClientService.startService} returns a L{Deferred} that has
already fired with L{None}.
F)r   rc   r   Nr   r  r   assertIsNoner   )r%   r   _r  r,   s   &    r   "test_stopServiceBeforeStartService5ClientServiceTests.test_stopServiceBeforeStartService  sR     ))!U * 

 !$..q12r   c                   \        4       pV P                  RVR7      w  r#VP                  4       pVP                  ^ ,          P	                  \        R4      4       VP                  4        VP                  4       pV P                  V P                  V4      P                  \        4       V P                  V P                  V4      P                  \        4       R# )z
L{ClientService.whenConnected} returns a L{Deferred} that
errbacks with L{CancelledError} if
L{ClientService.stopService} is called between connection
attempts.
Fr:  r  N)r   r  r%  r   r*   r  r   r   r.  r   r	   )r%   r   r  r  beforeErrbackAndStopafterErrbackAndStops   &     r   'test_whenConnectedErrbacksOnStopService:ClientServiceTests.test_whenConnectedErrbacksOnStopService  s     **5*N&446 	""9_#=>%335  !56<<n	
 	  !45;;^	
r   c                r   \        4       pV P                  RVR7      w  r#VP                  ^ ,          P                  R4       VP	                  4       pVP	                  4       pVP
                  ^ ,          P                  \        \        4       4      4       V P                  V4       V P                  V4       R# )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:  N)
r   r  r   r7   r   r   r  r   ra  r   r%   r   r  r  firstStopDeferredsecondStopDeferreds   &     r   "test_stopServiceWhileDisconnecting5ClientServiceTests.test_stopServiceWhileDisconnecting  s     **5*N
##D) $//1$002 	"11':J:L2MN.//0r   c                   \        4       pV P                  RVR7      w  r#VP                  ^ ,          P                  R4       VP	                  4       pVP                  4        VP	                  4       pVP                  ^ ,          P                  \        \        4       4      4       V P                  V4       V P                  V4       R# )z
Calling L{ClientService.stopService} after calling a
reconnection attempt returns a L{Deferred} that fires when the
disconnection has completed.
Fr:  N)r   r  r   r7   r   rc   r   r  r   ra  r   r  s   &     r   test_stopServiceWhileRestarting2ClientServiceTests.test_stopServiceWhileRestarting  s     **5*N
##D) $//1$002 	"11':J:L2MN.//0r   c                   \        4       pV P                  RVR7      w  r#VP                  4       pVP                  4       pV P                  V P	                  V4      4       V P                  V P	                  V4      4       R# )zv
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  s   &     r    test_stopServiceOnStoppedService3ClientServiceTests.test_stopServiceOnStoppedService  st    
 ))%u)M
#//1$002$../@AB$../ABCr   c                v   a ^ .oV3R lpV P                  VRR7      w  r#V P                  ^S^ ,          4       R# )zn
The C{prepareConnection} callable is called after
L{ClientService.startService} once the connection is made.
c                 2   < S^ ;;,          ^,          uu&   R#     Nr   _protopreparess   &r   prepareConnection[ClientServiceTests.test_prepareConnectionCalledWhenServiceStarts.<locals>.prepareConnection2      QK1Kr   T)r  rc   N)r  rl   )r%   r  r  r  r  s   &   @r   -test_prepareConnectionCalledWhenServiceStarts@ClientServiceTests.test_prepareConnectionCalledWhenServiceStarts+  sE    
 3	 **/d + 
 	HQK(r   c                   a . oV3R lpV P                  VR7      w  r#V P                  VP                  ^ ,          S^ ,          4       R# )zN
The C{prepareConnection} callable is passed the connected protocol
instance.
c                 *   < SP                  V 4       R # r!   r   )protonewProtocolss   &r   r  VClientServiceTests.test_prepareConnectionCalledWithProtocol.<locals>.prepareConnectionA  s    &r   r  N)r  r\   r   )r%   r  r  r  r  s   &   @r   (test_prepareConnectionCalledWithProtocol;ClientServiceTests.test_prepareConnectionCalledWithProtocol:  sL    
 	' **/ + 
 	R44Q7aIr   c                  a ^ .oV3R lp\        4       pV P                  VRVR7      w  r4VP                  ^ ,          P                  \	        R4      4       V P                  ^ S^ ,          4       VP                  \        4       VP                  ^,          P                  R4       V P                  ^S^ ,          4       R# )zN
The C{prepareConnection} callback is invoked only once a connection is
made.
c                 2   < S^ ;;,          ^,          uu&   R# r  r   r  s   &r   r  ]ClientServiceTests.test_prepareConnectionCalledAfterConnectionMade.<locals>.prepareConnectionP  r  r   F)r  r   r   zconnection attempt failedN)	r   r  r   r*   r  rl   r;  r<  r7   r%   r  r   r  r  r  s   &    @r   /test_prepareConnectionCalledAfterConnectionMadeBClientServiceTests.test_prepareConnectionCalledAfterConnectionMadeI  s    
 3	 **/e + 
 	""9-H#IJHQK(*+
##D)HQK(r   c                J  a ^ .oV3R lp\        4       pV P                  WR7      w  r4V P                  ^S^ ,          4       VP                  ^ ,          P	                  \        \        4       4      4       VP                  \        4       V P                  ^S^ ,          4       R# )zi
The C{prepareConnection} callback is invoked each time a connection is
made, including on reconnection.
c                 2   < S^ ;;,          ^,          uu&   R# r  r   r  s   &r   r  UClientServiceTests.test_prepareConnectionCalledOnReconnect.<locals>.prepareConnectiong  r  r   r  r   N)	r   r  rl   r   r  r   ra  r;  r<  r  s   &    @r   'test_prepareConnectionCalledOnReconnect:ClientServiceTests.test_prepareConnectionCalledOnReconnect`  s    
 3	 **/ + 
 	HQK( 	"11':J:L2MN*+HQK(r   c                   a \        4       oV3R lpV P                  VR7      w  r#V P                  VP                  4       4      pV P	                  SV4       R# )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.
c                    < S# r!   r   )r  sentinels   &r   r  VClientServiceTests.test_prepareConnectionReturnValueIgnored.<locals>.prepareConnection  s    Or   r  N)r   r  r   r%  assertNotIdentical)r%   r  r  r  r2   r  s   &    @r   (test_prepareConnectionReturnValueIgnored;ClientServiceTests.test_prepareConnectionReturnValueIgnoredw  sR     8	 **=N*O%%g&;&;&=>&1r   c                   aa . o\        4       oVV3R lpV P                  VR7      w  r#VP                  4       pV P                  V4       SP	                  R4       V P                  VP                  ^ ,          V P                  V4      4       R# )zw
The C{prepareConnection} callable returns a deferred and calls to
L{ClientService.whenConnected} wait until it fires.
c                 *   < SP                  V 4       S# r!   r  )r  newProtocolDeferredr  s   &r   r  VClientServiceTests.test_prepareConnectionReturningADeferred.<locals>.prepareConnection  s    &&&r   r  N)r
   r  r%  r   r7   r\   r   r   )r%   r  r  r  whenConnectedDeferredr  r  s   &    @@r   (test_prepareConnectionReturningADeferred;ClientServiceTests.test_prepareConnectionReturningADeferred  s    
 &j	' **=N*O ' 5 5 712$$T*##A&(<(<=R(S	
r   c                b   \        4       pR pV P                  W!R7      w  r4VP                  ^R7      pV P                  V4       VP	                  \
        4       V P                  V4       VP	                  \
        4       V P                  \        V P                  V4      P                  4       R# )z[
The connection attempt counts as a failure when the
C{prepareConnection} callable throws.
c                     \        4       hr!   )ra  )r  s   &r   r  JClientServiceTests.test_prepareConnectionThrows.<locals>.prepareConnection  s    "$$r   r  rj  N)
r   r  r%  r   r;  r<  r\   ra  r.  type)r%   r   r  r  r  r  s   &     r   test_prepareConnectionThrows/ClientServiceTests.test_prepareConnectionThrows  s    
 	% **/ + 
 !( 5 5 5 J12*+12*+d223HINN	
r   r   N)(r>   r?   r@   rA   rB   r   r  r   r  r  r"  r*  r/  r7  r>  rA  rG  rO  rR  r^  rc  rg  rr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rC   rD   rE   s   @r   r   r     s      HT26 : +8
"J( 
 
02"
*$+ $+L"MB*81t0& 3
21.10D)J).).2&
.
 
r   r   )F)0rB   r   zope.interfacer   zope.interface.verifyr   twisted.applicationr   twisted.application.internetr   r   r   twisted.internetr   twisted.internet.deferr	   r
   twisted.internet.interfacesr   r   r   r   r   twisted.internet.protocolr   r   twisted.internet.taskr   twisted.internet.testingr   twisted.loggerr   r   twisted.python.failurer   twisted.trial.unittestr   r   r   r   r"   rT   r   r   r   r   r<  r   r   r   r   <module>r     s   
  & - ( 
 " ;  8 ' 4 : * @	 "#=* =* $=*@ !: . ^  6 !: .V18 V1rC- C-L" "( &F &8 :$  z

, z

r   