ó
    Ö›‘h¬  ã                   óÂ  • S r SSKrSSKJr  SSKJr  SSKJr  SSKJ	r	J
r
Jr  SSKJr  SSKJrJr  SS	KJrJrJrJrJr  SS
KJrJr  SSKJr  SSKJr  SSKJrJ r   SSK!J"r"  SSK#J$r$J%r%  S r&\" \5       " S S5      5       r'\" \\'5        \" \5       " S S5      5       r(\" \\'5         " S S\%5      r) " S S\%5      r* " S S5      r+S S jr,\ 4S jr-Sr. " S S\$5      r/g)!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                  ó   • g)zE
A fake target function for testing TimerService which does nothing.
N© r   ó    ÚjC:\Users\psant\Downloads\Noiseback\school\venv\Lib\site-packages\twisted/application/test/test_internet.pyÚfakeTargetFunctionr   )   s   € ð 	r   c                   óN   • \ rS rSrSrSrSrSr\" 5       r	Sr
S rS rS rS rS	rg)
Ú
FakeServeré0   aA  
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.
Nr   c                 ó"   • [        5       U l        g ©N)ÚFakePortÚport©Úselfs    r   Ú__init__ÚFakeServer.__init__L   s   € Ü“Jˆ�	r   c                 óÜ   ^ • T =R                   S-  sl         UT l        [        U 4S jS9T l        T R                  b%  T R                  R                  T R                  5        T R                  $ )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                 ó:   >• U R                  TR                  5      $ r#   )ÚerrbackÚcancelException)Údr'   s    €r   Ú<lambda>Ú#FakeServer.listen.<locals>.<lambda>Z   s   ø€ °1·9±9¸T×=QÑ=QÔ3Rr   )Ú	canceller)ÚlistenAttemptsÚfactoryr   ÚresultÚfailImmediatelyr-   )r'   r4   s   ` r   ÚlistenÚFakeServer.listenO   sX   ø€ ð 	×Ò˜qÑ ÕØˆŒÜÔ)RÑSˆŒØ×ÑÑ+Ø�K‰K×Ñ × 4Ñ 4Ô5Ø�{‰{Ðr   c                 óN   • U R                   R                  U R                  5        g)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)r5   Úcallbackr%   r&   s    r   ÚstartedListeningÚFakeServer.startedListening_   s   € ð 	�‰×Ñ˜TŸY™YÕ'r   c                 óN   • U R                   R                  R                  S5        g)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%   Údeferredr:   r&   s    r   ÚstoppedListeningÚFakeServer.stoppedListeningg   s   € ð 	�	‰	×Ñ×#Ñ# DÕ)r   )r4   r%   r5   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r5   r4   r6   r
   r.   r3   r(   r7   r;   r?   Ú__static_attributes__r   r   r   r    r    0   s9   † ñð( €FØ€GØ€OÙ$Ó&€OØ€Nòòò (õ*r   r    c                   ó.   • \ rS rSrSrSrS rS rS rSr	g)r$   ét   zg
Fake L{IListeningPort} implementation.

@ivar deferred: The L{Deferred} returned by C{stopListening}.
Nc                 ó8   • [        5       U l        U R                  $ )zH
Stop listening.

@return: a L{Deferred} stored in L{FakePort.deferred}
)r   r>   r&   s    r   ÚstopListeningÚFakePort.stopListening~   s   € ô !›
ˆŒØ�}‰}Ðr   c                 ó   • g r#   r   r&   s    r   ÚgetHostÚFakePort.getHost‡   ó   € àr   c                 ó   • g r#   r   r&   s    r   ÚstartListeningÚFakePort.startListening‹   rO   r   ©r>   )
rA   rB   rC   rD   rE   r>   rJ   rM   rQ   rF   r   r   r   r$   r$   t   s   † ñð €Hòòõr   r$   c                   ód   • \ rS rSrSrS rS rSS jrS rS r	S	 r
S
 rS rS rS rS rS rSrg)ÚEndpointServiceTestsé“   z,
Tests for L{twisted.application.internet}.
c                 óŠ   • [        5       U l        [        5       U l        [	        U R                  U R                  5      U l        g)zX
Construct a stub server, a stub factory, and a
L{StreamServerEndpointService} to test.
N)r    Ú
fakeServerr   r4   r   Úsvcr&   s    r   ÚsetUpÚEndpointServiceTests.setUp˜   s,   € ô
 %›,ˆŒÜ“yˆŒÜ.¨t¯©ÀÇÁÓMˆ�r   c                 ó˜   • U R                   R                  5         U R                  U R                  U R                  R                  5        g)zo
L{StreamServerEndpointService.privilegedStartService} calls its
endpoint's C{listen} method with its factory.
N)rY   ÚprivilegedStartServiceÚassertIdenticalr4   rX   r&   s    r   Útest_privilegedStartServiceÚ0EndpointServiceTests.test_privilegedStartService¡   s2   € ð
 	�‰×'Ñ'Ô)Ø×Ñ˜TŸ\™\¨4¯?©?×+BÑ+BÕCr   Nc                 ó¾   • [        5       U R                  l        SU R                  l        U R                  [         U=(       d    U R                  R                  5        g)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)ÚZeroDivisionErrorrX   r6   rY   Ú_raiseSynchronouslyÚassertRaisesÚstartService)r'   Úthunks     r   Ú(test_synchronousRaiseRaisesSynchronouslyÚ=EndpointServiceTests.test_synchronousRaiseRaisesSynchronously©   s?   € ô  +<Ó*=ˆ�‰Ô'Ø'+ˆ�‰Ô$Ø×ÑÔ+¨U×-K°d·h±h×6KÑ6KÕLr   c                 óN   • U R                  U R                  R                  5        g)z³
L{StreamServerEndpointService.privilegedStartService} should behave the
same as C{startService} with respect to
L{EndpointServiceTests.test_synchronousRaiseRaisesSynchronously}.
N)rg   rY   r]   r&   s    r   Útest_synchronousRaisePrivilegedÚ4EndpointServiceTests.test_synchronousRaisePrivileged½   s   € ð 	×5Ñ5°d·h±h×6UÑ6UÕVr   c                 óò   • U R                   R                  5         U R                  R                  R	                  [        5       5        U R                  [
        5      nU R                  [        U5      S5        g)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.
r+   N)	rY   re   rX   r5   r-   rb   ÚflushLoggedErrorsÚassertEqualÚlen©r'   Úloggeds     r   Útest_failReportsErrorÚ*EndpointServiceTests.test_failReportsErrorÅ   sU   € ð 	�‰×ÑÔØ�‰×Ñ×&Ñ&Ô'8Ó':Ô;Ø×'Ñ'Ô(9Ó:ˆØ×Ñœ˜V› aÕ(r   c                 ó  • SU R                   l        U R                   R                  5         U R                  R                  R                  [        5       5        U R                  [        5      nU R                  [        U5      S5        g)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.
Tr+   N)
rY   rc   re   rX   r5   r-   rb   rm   rn   ro   rp   s     r   Ú!test_asynchronousFailReportsErrorÚ6EndpointServiceTests.test_asynchronousFailReportsErrorÑ   sa   € ð (,ˆ�‰Ô$Ø�‰×ÑÔØ�‰×Ñ×&Ñ&Ô'8Ó':Ô;Ø×'Ñ'Ô(9Ó:ˆØ×Ñœ˜V› aÕ(r   c                 óÊ   • [        5       U R                  l        U R                  R	                  5         U R                  [         5      nU R                  [        U5      S5        g)z‡
Without the C{_raiseSynchronously} compatibility flag, failing
immediately has the same behavior as failing later; it logs the error.
r+   N)rb   rX   r6   rY   re   rm   rn   ro   rp   s     r   Ú test_synchronousFailReportsErrorÚ5EndpointServiceTests.test_synchronousFailReportsErrorß   sH   € ô
 +<Ó*=ˆ�‰Ô'Ø�‰×ÑÔØ×'Ñ'Ô(9Ó:ˆØ×Ñœ˜V› aÕ(r   c                 óä   • U R                   R                  5         U R                  U R                  U R                  R                  5        U R                  U R                   R                  S5        g)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)rY   re   r^   r4   rX   rn   Úrunningr&   s    r   Útest_startServiceUnstartedÚ/EndpointServiceTests.test_startServiceUnstartedé   sL   € ð 	�‰×ÑÔØ×Ñ˜TŸ\™\¨4¯?©?×+BÑ+BÔCØ×Ñ˜Ÿ™×)Ñ)¨4Õ0r   c                 óð   • U R                  5         U R                  R                  5         U R                  U R                  R
                  S5        U R                  U R                  R                  S5        g)z‚
L{StreamServerEndpointService.startService} sets the C{running} flag,
but nothing else, if the service has already been started.
r+   TN)r_   rY   re   rn   rX   r3   r{   r&   s    r   Útest_startServiceStartedÚ-EndpointServiceTests.test_startServiceStartedó   sT   € ð
 	×(Ñ(Ô*Ø�‰×ÑÔØ×Ñ˜Ÿ™×7Ñ7¸Ô;Ø×Ñ˜Ÿ™×)Ñ)¨4Õ0r   c                 óø  • U R                   R                  5         U R                  R                  5         U R                   R	                  5         U R                   R                  5       n/ nUR                  UR                  5        U R                  [        U5      S5        U R                  R                  5         U R                  [        U5      S5        U R                  U R                   R                  5        g)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}.
r   r+   N)rY   r]   rX   r;   re   ÚstopServiceÚaddCallbackÚappendrn   ro   r?   ÚassertFalser{   ©r'   r5   Úls      r   Útest_stopServiceÚ%EndpointServiceTests.test_stopServiceý   s­   € ð 	�‰×'Ñ'Ô)Ø�‰×(Ñ(Ô*à�‰×ÑÔØ—‘×%Ñ%Ó'ˆØˆØ×Ñ˜1Ÿ8™8Ô$Ø×Ñœ˜Q› Ô#Ø�‰×(Ñ(Ô*Ø×Ñœ˜Q› Ô#Ø×Ñ˜Ÿ™×)Ñ)Õ*r   c                 ó  • U R                   R                  5         U R                   R                  5       n/ nUR                  UR                  5        U R                  US/5        U R                  U R                  [        5      / 5        g)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)rY   r]   r‚   ÚaddBothr„   rn   rm   r
   r†   s      r   Ú#test_stopServiceBeforeStartFinishedÚ8EndpointServiceTests.test_stopServiceBeforeStartFinished  sh   € ð 	�‰×'Ñ'Ô)Ø—‘×%Ñ%Ó'ˆØˆØ�‰�q—x‘xÔ Ø×Ñ˜˜T˜FÔ#Ø×Ñ˜×/Ñ/´Ó?ÀÕDr   c                 ó^  • [        5       U R                  l        U R                  R	                  5         U R                  R                  5       n/ nUR                  UR                  5        U R                  US/5        U R                  [         5      nU R                  [        U5      S5        g)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+   )rb   rX   r.   rY   r]   r‚   rƒ   r„   rn   rm   ro   )r'   r5   r‡   ÚstoppingErrorss       r   Ú test_stopServiceCancelStartErrorÚ5EndpointServiceTests.test_stopServiceCancelStartError  s„   € ô +<Ó*=ˆ�‰Ô'Ø�‰×'Ñ'Ô)Ø—‘×%Ñ%Ó'ˆØˆØ×Ñ˜1Ÿ8™8Ô$Ø×Ñ˜˜T˜FÔ#Ø×/Ñ/Ô0AÓBˆØ×Ñœ˜^Ó,¨aÕ0r   )r4   rX   rY   r#   )rA   rB   rC   rD   rE   rZ   r_   rg   rj   rr   ru   rx   r|   r   rˆ   rŒ   r�   rF   r   r   r   rU   rU   “   sH   † ñòNòDôMò(Wò
)ò)ò)ò1ò1ò+ò$Eõ1r   rU   c                   óT   • \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rSrg)Ú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                 ó®   • [        SU R                  5      U l        [        R                  " 5       =U l        U R                  l        [        5       U l        g)z!
Set up a timer service to test.
é   N)r   ÚcallÚtimerr	   r   Úclockr   r>   r&   s    r   rZ   ÚTimerServiceTests.setUp:  s8   € ô " ! T§Y¡YÓ/ˆŒ
Ü(,¯
ª
«Ð4ˆŒ
�T—Z‘ZÔ%Ü ›
ˆ�r   c                 ó   • U R                   $ )zb
Function called by L{TimerService} being tested.

@returns: C{self.deferred}
@rtype: L{Deferred}
rS   r&   s    r   r–   ÚTimerServiceTests.callB  s   € ð �}‰}Ðr   c                 óÀ  • U R                   R                  5         U R                  U R                   R                  S5        U R	                  U R                   R
                  [        R                  5        U R                  U R                  U R                   R
                  R                  5        U R                  U R                   R
                  R                  S5        g)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—   re   Ú
assertTruer{   ÚassertIsInstanceÚ_loopr	   ÚLoopingCallr^   r˜   r&   s    r   Útest_startServiceÚ#TimerServiceTests.test_startServiceK  s�   € ð
 	�
‰
×ÑÔ!Ø�‰˜Ÿ
™
×*Ñ*Ð,@ÔAØ×Ñ˜dŸj™j×.Ñ.´×0@Ñ0@ÔAØ×Ñ˜TŸZ™Z¨¯©×)9Ñ)9×)?Ñ)?Ô@Ø�‰˜Ÿ
™
×(Ñ(×0Ñ0Ð2JÕKr   c                 óž   • / nUR                   S0 4U R                  l        U R                  R                  5         U R	                  S/U5        g)zQ
When L{TimerService.startService} is called, it calls the function
immediately.
r#   N)r„   r—   r–   re   rn   )r'   r5   s     r   Ú$test_startServiceRunsCallImmediatelyÚ6TimerServiceTests.test_startServiceRunsCallImmediatelyV  sA   € ð
 ˆØ!Ÿ=™=¨'°2Ð6ˆ�
‰
ŒØ�
‰
×ÑÔ!Ø×Ñ˜$˜ Õ(r   c                 óþ   ^• [         R                  " 5       mU4S jnU R                  [        SU5        U R                  R                  5         U R                  TU R                  R                  R                  5        g)z‘
L{TimerService.startService} uses L{internet._maybeGlobalReactor} to
choose the reactor to pass to L{task.LoopingCall}
uses the global reactor.
c                 ó   >• T$ r#   r   )ÚmaybeReactorÚ
otherClocks    €r   ÚgetOtherClockÚKTimerServiceTests.test_startServiceUsesGlobalReactor.<locals>.getOtherClockh  s	   ø€ ØÐr   Ú_maybeGlobalReactorN)	r	   r   Úpatchr   r—   re   r^   rŸ   r˜   )r'   rª   r©   s     @r   Ú"test_startServiceUsesGlobalReactorÚ4TimerServiceTests.test_startServiceUsesGlobalReactor`  sX   ø€ ô —Z’Z“\ˆ
õ	ð 	�
‰
”8Ð2°MÔBØ�
‰
×ÑÔ!Ø×Ñ˜Z¨¯©×)9Ñ)9×)?Ñ)?Õ@r   c                 ób  • U R                   R                  5         U R                   R                  5       nU R                  U5        U R	                  SU R                   R
                  5        U R                  R                  [        5       5        U R                  U R                  U5      S5        g)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—   re   r‚   ÚassertNoResultrn   r{   r>   r:   Úobjectr^   ÚsuccessResultOf©r'   r/   s     r   Útest_stopServiceWaitsÚ'TimerServiceTests.test_stopServiceWaitso  s}   € ð
 	�
‰
×ÑÔ!Ø�J‰J×"Ñ"Ó$ˆØ×Ñ˜AÔØ×Ñ˜˜tŸz™z×1Ñ1Ô2Ø�‰×Ñœv›xÔ(Ø×Ñ˜T×1Ñ1°!Ó4°dÕ;r   c                 óô   • U R                   R                  5         U R                  R                  [	        5       5        U R                   R                  5       nU R                  U R                  U5      S5        g)z}
When L{TimerService.stopService} is called while a call isn't in progress.
the L{Deferred} returned has already been fired.
N)r—   re   r>   r:   r²   r‚   r^   r³   r´   s     r   Útest_stopServiceImmediatelyÚ-TimerServiceTests.test_stopServiceImmediately{  sU   € ð
 	�
‰
×ÑÔ!Ø�‰×Ñœv›xÔ(Ø�J‰J×"Ñ"Ó$ˆØ×Ñ˜T×1Ñ1°!Ó4°dÕ;r   c                 óf  • U R                   R                  5         U R                  R                  [	        [        5       5      5        U R                  [
        5      nU R                  S[        U5      5        U R                   R                  5       nU R                  U R                  U5      S5        g)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—   re   r>   r-   r   rb   rm   rn   ro   r‚   r^   r³   )r'   Úerrorsr/   s      r   Útest_failedCallLogsErrorÚ*TimerServiceTests.test_failedCallLogsError…  s�   € ð
 	�
‰
×ÑÔ!Ø�‰×ÑœgÔ&7Ó&9Ó:Ô;Ø×'Ñ'Ô(9Ó:ˆØ×Ñ˜œC ›KÔ(Ø�J‰J×"Ñ"Ó$ˆØ×Ñ˜T×1Ñ1°!Ó4°dÕ;r   c                 ó  • [        S[        5      nUR                  5         [        R                  " U5      nUR                  5         [        R                  " U5      n[        5       n[        USU5      nU R                  XE5        g)zY
When pickling L{internet.TimerService}, it won't pickle
L{internet.TimerService._loop}.
r+   rŸ   N©
r   r   re   ÚpickleÚdumpsr‚   Úloadsr²   Úgetattrr^   ©r'   r—   ÚdumpedTimerÚloadedTimerÚnothingÚvalues         r   Ú$test_pickleTimerServiceNotPickleLoopÚ6TimerServiceTests.test_pickleTimerServiceNotPickleLoop‘  sh   € ô ˜QÔ 2Ó3ˆØ×ÑÔÜ—l’l 5Ó)ˆØ×ÑÔÜ—l’l ;Ó/ˆÜ“(ˆÜ˜ W¨gÓ6ˆØ×Ñ˜WÕ,r   c                 ó  • [        S[        5      nUR                  5         [        R                  " U5      nUR                  5         [        R                  " U5      n[        5       n[        USU5      nU R                  XE5        g)za
When pickling L{internet.TimerService}, it won't pickle
L{internet.TimerService._loopFinished}.
r+   Ú_loopFinishedNr¿   rÄ   s         r   Ú,test_pickleTimerServiceNotPickleLoopFinishedÚ>TimerServiceTests.test_pickleTimerServiceNotPickleLoopFinished¡  sh   € ô ˜QÔ 2Ó3ˆØ×ÑÔÜ—l’l 5Ó)ˆØ×ÑÔÜ—l’l ;Ó/ˆÜ“(ˆÜ˜ _°gÓ>ˆØ×Ñ˜WÕ,r   )r˜   r>   r—   N)rA   rB   rC   rD   rE   rZ   r–   r¡   r¤   r®   rµ   r¸   r¼   rÉ   rÍ   rF   r   r   r   r“   r“   ,  s;   † ñò#òò	Lò)òAò
<ò<ò
<ò-õ -r   r“   c                   ó   • \ rS rSrSrS rSrg)Ú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                 ó.   • / U l         / U l        / U l        g r#   ©ÚconnectQueueÚconstructedProtocolsÚpassedFactoriesr&   s    r   r(   ÚConnectInformation.__init__À  s   € ØˆÔØ$&ˆÔ!Ø!ˆÕr   rÒ   N)rA   rB   rC   rD   rE   r(   rF   r   r   r   rÐ   rÐ   ²  s   † ñõ"r   rÐ   c                 ól   ^ ^• [        [        5       " U U4S jS5      5       n[        5       mTU" 5       4$ )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                   ó$   >• \ rS rSrU U4S jrSrg)Ú.endpointForTesting.<locals>.ClientTestEndpointiÒ  c                 óæ   >^• [        5       nTR                  R                  T5        UR                  UU4S j5       nTR                  R                  U5        T(       a  UR                  S 5        U$ )Nc                 ó–   >• TR                  S 5      nTR                  R                  U5        [        5       nUR	                  U5        U$ r#   )ÚbuildProtocolrÔ   r„   r   ÚmakeConnection)ÚignoredÚprotocolÚ	transportr4   Úinfos      €€r   ÚcreateProtocolÚNendpointForTesting.<locals>.ClientTestEndpoint.connect.<locals>.createProtocolØ  sB   ø€ à"×0Ñ0°Ó6�Ø×)Ñ)×0Ñ0°Ô:Ü+Ó-�	Ø×'Ñ'¨	Ô2Ø�r   )r   rÕ   r„   rƒ   rÓ   r:   )r'   r4   r5   râ   ÚfireImmediatelyrá   s    `  €€r   ÚconnectÚ6endpointForTesting.<locals>.ClientTestEndpoint.connectÔ  s`   ù€ Ü“ZˆFØ× Ñ ×'Ñ'¨Ô0à×Ñõ ó  ð ð ×Ñ×$Ñ$ VÔ,ÞØ—‘ Ô%ØˆMr   r   N)rA   rB   rC   rD   rå   rF   )rä   rá   s   €€r   ÚClientTestEndpointrÙ   Ò  s   ø† ÷	ñ 	r   rç   )r   r   rÐ   )rä   rç   rá   s   ` @r   ÚendpointForTestingrè   Æ  s;   ù€ ô Ô&Ó'÷ô ó (ðô$ Ó€DØÑ#Ó%Ð%Ð%r   c                 óx   ^^• / mTR                  TR                  5        U R                  UU4S j5        U4S j$ )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                  ó:   >• T R                  TR                  5      $ r#   )ÚremoveObserverr„   )ÚlogPublisherÚlogss   €€r   r0   ÚcatchLogs.<locals>.<lambda>÷  s   ø€  × ;Ñ ;¸D¿K¹KÔ Hr   c                  óF   >• T V s/ s H  n [        U 5      PM     sn $ s  sn f r#   )r   )Úeventrí   s    €r   r0   rî   ø  s   ø€ ±DÓ9²D¨5”K Ö&±DÒ9ùÒ9s   †)ÚaddObserverr„   Ú
addCleanup)ÚtestCaserì   rí   s    `@r   Ú	catchLogsrô   é  s1   ù€ ð €DØ×Ñ˜TŸ[™[Ô)Ø×ÑÕHÔIÜ9Ð9r   g      Y@c                   óè   • \ rS rSrSrSS\S4S jrS rS rS r	S	 r
S
 rS rS rS rS rS rS rS rS'S jrS rS rS rS rS rS rS rS rS rS rS rS rS  r S! r!S" r"S# r#S$ r$S% r%S&r&g)(ÚClientServiceTestsiþ  z
Tests for L{ClientService}.
TFc                 óX  ^ ^^^^^• 0 nUR                  [        5       S9  UR                  U5        US   m[        US9u  px/ =mUl         " UUU4S jS[        5      n	U	" 5       =Ul        n
[        XŠ40 UD6mUU U4S jnT R                  U5        U(       a  TR                  5         UT4$ )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                   ó4   >^ • \ rS rSr YrU UU4S jrSrU =r$ )Ú>ClientServiceTests.makeReconnector.<locals>.RememberingFactoryi2  c                 óX   >• T(       a  g [         TU ]  U5      nTR                  U5        U$ r#   )ÚsuperrÜ   r„   )r'   Úaddrr5   Ú	__class__ÚapplicationProtocolsÚrefuseConnectionss      €€€r   rÜ   ÚLClientServiceTests.makeReconnector.<locals>.RememberingFactory.buildProtocol5  s+   ø€ Þ$ØÜ™Ñ.¨tÓ4�Ø$×+Ñ+¨FÔ3Ø�r   r   )rA   rB   rC   rD   rß   rÜ   rF   Ú__classcell__)rÿ   r   ÚprotocolTyper  s   @€€€r   ÚRememberingFactoryrû   2  s   ù† Ù#ˆH÷õ r   r  c                  ó–   >• S Tl         TR                  (       a  TR                  5         TR                  T R	                  5       / 5        g r#   )Ú	_protocolr{   r‚   rn   ÚgetDelayedCalls)r˜   r'   Úservices   €€€r   ÚstopÚ0ClientServiceTests.makeReconnector.<locals>.stop@  s9   ø€ Ø $ˆGÔØ��Ø×#Ñ#Ô%ð ×Ñ˜U×2Ñ2Ó4°bÕ9r   )	Úupdater   rè   r   r   ÚapplicationFactoryr   rò   re   )r'   rä   re   r  r  ÚkwÚnkwÚcqÚendpointr  r4   r
  r   r˜   r	  s   `  ``       @@@r   ÚmakeReconnectorÚ"ClientServiceTests.makeReconnector  s©   ý€ ðB ˆØ�
‰
œ›ˆ
Ñ!Ø�
‰
�2ŒØ�G‘ˆÜ)¸/ÑJ‰ˆð :<Ð;Ð˜rÔ6÷	ñ 	¤ô 	ñ +=Ó*>Ð>ˆÔ ä Ñ9°SÑ9ˆ÷	:ð 	�‰˜ÔÞØ× Ñ Ô"Ø�7ˆ{Ðr   c                 óp   • U R                  SS9u  pU R                  [        UR                  5      S5        g)z<
When the service is started, a connection attempt is made.
Frù   r+   N)r  rn   ro   rÓ   )r'   r  r	  s      r   r¡   Ú$ClientServiceTests.test_startServiceM  s2   € ð ×*Ñ*¸5Ð*ÐA‰ˆØ×Ñœ˜RŸ_™_Ó-¨qÕ1r   c                 óî   • U R                  5       u  pUR                  nU R                  UR                  S5        UR                  S   nUR                  5         U R                  UR                  S5        g)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.
r   r+   N)r  r  rn   ÚnumPortsrÕ   ÚdoStart)r'   r  r	  ÚfirstAppFactoryÚfirstPassedFactorys        r   Útest_startStopFactoryÚ(ClientServiceTests.test_startStopFactoryT  sh   € ð ×*Ñ*Ó,‰ˆØ×/Ñ/ˆØ×Ñ˜×1Ñ1°1Ô5Ø×/Ñ/°Ñ2ÐØ×"Ñ"Ô$Ø×Ñ˜×1Ñ1°1Õ5r   c                 ó:  • U R                  5       u  pUR                  5       nU R                  U5        UR                  S   nU R	                  UR
                  R                  S5        UR                  [        [        5       5      5        U R                  U5        g)z˜
When the service is stopped, no further connect attempts are made.  The
returned L{Deferred} fires when all outstanding connections have been
stopped.
r   TN)r  r‚   r±   rÔ   rn   rà   ÚdisconnectingÚconnectionLostr   Ú	Exceptionr³   ©r'   r  r	  r/   rß   s        r   Útest_stopServiceWhileConnectedÚ1ClientServiceTests.test_stopServiceWhileConnectedb  s   € ð ×*Ñ*Ó,‰ˆØ×ÑÓ!ˆØ×Ñ˜AÔØ×*Ñ*¨1Ñ-ˆØ×Ñ˜×+Ñ+×9Ñ9¸4Ô@Ø×Ñ¤¬	«Ó 4Ô5Ø×Ñ˜QÕr   c                 óð  • U R                  5       u  pUR                  5       nU R                  U5        UR                  S   nU R	                  UR
                  R                  S5        UR                  5         U R                  U5        U R	                  [        UR                  5      S5        UR                  [        [        S5      5      5        U R	                  [        UR                  5      S5        g)z
When L{ClientService} is restarted after having been connected, it
waits to start connecting until after having disconnected.
r   Tr+   zsynthetic connectionLostr•   N)r  r‚   r±   rÔ   rn   rà   r  re   ro   r  r   r   r!  s        r   Ú#test_startServiceWaitsForDisconnectÚ6ClientServiceTests.test_startServiceWaitsForDisconnectp  sÅ   € ð
 ×*Ñ*Ó,‰ˆØ×ÑÓ!ˆØ×Ñ˜AÔØ×*Ñ*¨1Ñ-ˆØ×Ñ˜×+Ñ+×9Ñ9¸4Ô@Ø×ÑÔØ×Ñ˜AÔØ×Ñœ˜R×4Ñ4Ó5°qÔ9Ø×Ñ¤¬	Ð2LÓ(MÓ NÔOØ×Ñœ˜R×4Ñ4Ó5°qÕ9r   c                 ó@  • U R                  SS9u  pUR                  S   R                  S5        UR                  S   nUR	                  5       nU R                  U5        UR                  5       nU R                  U5        UR                  5         U R                  U5        U R                  U5        U R                  UR                  R                  S5        UR                  [        [        S5      5      5        U R                  U5        UR                  S   R                  S5        U R                  [        UR                  5      S5        U R!                  U R                  U5      UR"                  S   5        UR	                  5       nU R                  U5        g)	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ù   r   NTzsynthetic connection lostr+   r•   )r  rÓ   r:   rÔ   r‚   r±   ÚwhenConnectedre   rn   rà   r  r  r   r   r³   ro   r^   r   )r'   r  r	  ÚfirstÚstoppedÚnextProtocolÚsecondStoppeds          r   Útest_startServiceWhileStoppingÚ1ClientServiceTests.test_startServiceWhileStopping€  sZ  € ð ×*Ñ*¸5Ð*ÐA‰ˆØ
�‰˜Ñ×#Ñ# DÔ)Ø×'Ñ'¨Ñ*ˆØ×%Ñ%Ó'ˆØ×Ñ˜GÔ$Ø×,Ñ,Ó.ˆØ×Ñ˜LÔ)Ø×ÑÔØ×Ñ˜LÔ)Ø×Ñ˜GÔ$Ø×Ñ˜Ÿ™×6Ñ6¸Ô=Ø×ÑœW¤YÐ/JÓ%KÓLÔMØ×Ñ˜WÔ%Ø
�‰˜Ñ×#Ñ# DÔ)Ø×Ñœ˜R×4Ñ4Ó5°qÔ9Ø×ÑØ× Ñ  Ó.°×0GÑ0GÈÑ0Jô	
ð  ×+Ñ+Ó-ˆØ×Ñ˜MÕ*r   c                 ó„  • U R                  SS9u  pUR                  5       nU R                  U5        U R                  UR	                  5       [
        5        UR                  5         UR                  S   R                  S5        U R                  UR                  S   U R                  UR	                  5       5      5        g)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
   re   rÓ   r:   r^   r   )r'   r  r	  r*  s       r   Útest_startServiceWhileStoppedÚ0ClientServiceTests.test_startServiceWhileStoppedœ  s£   € ð ×*Ñ*¸5Ð*ÐA‰ˆØ×%Ñ%Ó'ˆØ×Ñ˜WÔ%Ø×Ñ˜W×2Ñ2Ó4´nÔEØ×ÑÔØ
�‰˜Ñ×$Ñ$ TÔ*Ø×ÑØ×#Ñ# BÑ'¨×)=Ñ)=¸g×>SÑ>SÓ>UÓ)Võ	
r   c                 ó$  • [        [        [        5       " S S[        5      5       nU R	                  US9u  p#UR
                  S   nU R                  [        R                  " U5      5        U R                  [        R                  " U5      5        g)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                   ó   • \ rS rSrSrSrg)ÚEClientServiceTests.test_interfacesForTransport.<locals>.FancyProtocoli¶  z!
Provider of various interfaces.
r   N)rA   rB   rC   rD   rE   rF   r   r   r   ÚFancyProtocolr6  ¶  s   † ôr   r7  )r  r   N)r   r   r   r   r  rÔ   r�   Ú
providedBy)r'   r7  r  r	  ÚreactorFacings        r   Útest_interfacesForTransportÚ.ClientServiceTests.test_interfacesForTransport­  s~   € ô 
Ô+Ô-DÓ	Eô	œHó 	ó 
Fð	ð
 ×*Ñ*¸Ð*ÐF‰ˆØ×/Ñ/°Ñ2ˆØ�‰Ô/×:Ò:¸=ÓIÔJØ�‰Ô.×9Ò9¸-ÓHÕIr   c                 óŠ  • [        5       nU R                  SUS9u  p#UR                  S   R                  [	        5       5        UR                  [        5        U R                  [        UR                  5      S5        UR                  5       nUR                  S   R                  [	        5       5        U R                  U5        g)zE
When the service is stopped while retrying, the retry is cancelled.
F©rä   r˜   r   r•   r+   N)r   r  rÓ   r-   r   ÚadvanceÚAT_LEAST_ONE_ATTEMPTrn   ro   r‚   r³   ©r'   r˜   r  r	  r/   s        r   Útest_stopServiceWhileRetryingÚ0ClientServiceTests.test_stopServiceWhileRetryingÁ  s–   € ô “ˆØ×*Ñ*¸5ÈÐ*ÐN‰ˆØ
�‰˜Ñ×"Ñ"¤9£;Ô/Ø�‰Ô*Ô+Ø×Ñœ˜RŸ_™_Ó-¨qÔ1Ø×ÑÓ!ˆØ
�‰˜Ñ×"Ñ"¤9£;Ô/Ø×Ñ˜QÕr   c                 ó  • [        5       nU R                  SUS9u  p#U R                  [        UR                  5      S5        U R                  UR                  S   5        UR                  5       nU R                  U5        g)z^
When the service is stopped while initially connecting, the connection
attempt is cancelled.
Fr=  r+   r   N)r   r  rn   ro   rÓ   r±   r‚   r³   r@  s        r   Útest_stopServiceWhileConnectingÚ2ClientServiceTests.test_stopServiceWhileConnectingÎ  sn   € ô
 “ˆØ×*Ñ*¸5ÈÐ*ÐN‰ˆØ×Ñœ˜RŸ_™_Ó-¨qÔ1Ø×Ñ˜BŸO™O¨AÑ.Ô/Ø×ÑÓ!ˆØ×Ñ˜QÕr   c                 ó.  • [        5       nU R                  US9u  p#UR                  5       nU R                  UR	                  5       / 5        U R                  U R                  U5      UR                  S   5        U R                  UR                  S   R                  UR                  S   5        U R                  [        UR                  S   5      [        UR                  S   5      5        U R                  S[        UR                  S   5      5        g)za
When a client connects, the service keeps a reference to the new
protocol and resets the delay.
rø   r   r  N)r   r  r(  rn   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Ó2°B×4KÑ4KÈAÑ4Nô	
ð 	×ÑØ×#Ñ# AÑ&×0Ñ0°"×2IÑ2IÈ!Ñ2Lô	
ð 	�‰Ü�×'Ñ'¨Ñ*Ó+Ü�×'Ñ'¨Ñ*Ó+ô	
ð 	�‰Ø Ü�×"Ñ" 1Ñ%Ó&õ	
r   c                 ó¾  • [        5       nU R                  SUS9u  p#U R                  [        UR                  5      S5        UR                  S   R                  [        [        5       5      5        UR                  5       nU R                  U5        UR                  S 5        UR                  [        5        U R                  [        UR                  5      S5        g)zt
When a client connection fails, the service removes its reference
to the protocol and tries again after a timeout.
Fr=  r+   r   c                 ó,   • U R                  [        5      $ r#   )Útrapr
   )rÞ   s    r   r0   Ú@ClientServiceTests.test_clientConnectionFailed.<locals>.<lambda>ÿ  s   € °·±¼nÔ1Mr   r•   N)r   r  rn   ro   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Ø
�‰˜Ñ×"Ñ"¤7¬9«;Ó#7Ô8Ø×-Ñ-Ó/ˆØ×Ñ˜MÔ*ð 	× Ñ Ñ!MÔNØ�‰Ô*Ô+Ø×Ñœ˜RŸ_™_Ó-¨qÕ1r   c                 ó
  • [        5       nU R                  USS9u  p#U R                  [        UR                  5      S5        UR                  S   R                  S5        U R                  [        UR                  5      S5        U R                  U R                  UR                  5       5      UR                  S   5        UR                  S   R                  [        [        5       5      5        UR                  [        5        U R                  [        UR                  5      S5        UR                  S   R                  S5        U R                  U R                  UR                  5       5      UR                  S   5        g)zf
When a client connection is lost, the service removes its reference
to the protocol and calls retry.
F)r˜   rä   r+   r   Nr•   )r   r  rn   ro   rÓ   r:   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Ó9¸2×;RÑ;RÐSTÑ;Uô	
ð 	×Ñ Ñ"×1Ñ1´'¼)»+Ó2FÔGØ�‰Ô*Ô+Ø×Ñœ˜RŸ_™_Ó-¨qÔ1Ø
�‰˜Ñ×#Ñ# DÔ)Ø×ÑØ× Ñ  ×!6Ñ!6Ó!8Ó9¸2×;RÑ;RÐSTÑ;Uõ	
r   Nc                 ó‚  ^ ^• [        5       nT R                  U U4S j5        T R                  USSS9u  p#T R                  [	        UR
                  5      S5        UR
                  R                  S5      R                  S5        T R                  [	        UR
                  5      S5        UR                  5       mT R                  T5        UR                  [        5        T R                  [	        UR
                  5      S5        UR
                  R                  S5      R                  S5        T R                  T5        g)z�
When the client factory refuses connections by returning None from
buildProtocol, the factory keeps attempting new connections.
c                  ó0   >• T R                  T[        5      $ r#   )r1  r
   )r'   r(  s   €€r   r0   ÚAClientServiceTests.test_clientConnectionRefused.<locals>.<lambda>"  s   ø€  × 4Ñ 4°]ÄNÔ Sr   FT)r˜   rä   r  r+   r   N)r   rò   r  rn   ro   rÓ   Úpopr:   r(  r±   r>  r?  rQ  s   `   @r   Útest_clientConnectionRefusedÚ/ClientServiceTests.test_clientConnectionRefused  sû   ù€ ô
 “ˆð
 	�‰ÕSÔTØ×*Ñ*ØØ!à"ð	 +ð 
‰ˆð 	×Ñœ˜RŸ_™_Ó-¨qÔ1à
�‰×Ñ˜AÓ×'Ñ'¨Ô-à×Ñœ˜RŸ_™_Ó-¨qÔ1à×-Ñ-Ó/ˆà×Ñ˜MÔ*ð 	�‰Ô*Ô+à×Ñœ˜RŸ_™_Ó-¨qÔ1à
�‰×Ñ˜AÓ×'Ñ'¨Ô-à×Ñ˜MÕ*r   c                 ó6  • [        5       nU R                  US9u  p#UR                  5       nUR                  S   R	                  [        [        5       5      5        U R                  UR                  5       [        5        U R                  UR                  5        g)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ø   r   N)r   r  r‚   rÔ   r  r   ÚIndentationErrorr1  r(  r
   r�   Úcalledr@  s        r   Ú&test_clientConnectionLostWhileStoppingÚ9ClientServiceTests.test_clientConnectionLostWhileStopping>  sy   € ô “ˆØ×*Ñ*°Ð*Ð7‰ˆØ×ÑÓ!ˆØ
×Ñ Ñ"×1Ñ1´'Ô:JÓ:LÓ2MÔNØ×Ñ˜W×2Ñ2Ó4´nÔEØ�‰˜Ÿ™Õ!r   c                 ó�  • U R                  SSS9u  pU R                  [        UR                  5      S5        UR	                  5         U R                  [        UR                  5      S5        [        U 5      nUR	                  5         U R                  [        UR                  5      S5        U R                  SU" 5       S   5        g)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ä   re   r   r+   z$Duplicate ClientService.startServiceN)r  rn   ro   rÓ   re   rô   rG  )r'   r  r	  Úmessagess       r   Útest_startTwiceÚ"ClientServiceTests.test_startTwiceK  sœ   € ð ×*Ñ*¸5ÈuÐ*ÐU‰ˆØ×Ñœ˜RŸ_™_Ó-¨qÔ1Ø×ÑÔØ×Ñœ˜RŸ_™_Ó-¨qÔ1Ü˜T“?ˆØ×ÑÔØ×Ñœ˜RŸ_™_Ó-¨qÔ1Ø�‰Ð<¹h»jÈ¹mÕLr   c                 ó"  • [        5       nU R                  SUS9u  p#UR                  5       nUR                  5       nUR                  SS9nU R                  U5        U R                  U5        U R                  U5        UR                  S   R                  S5        U R                  U5      nU R                  U5      nU R                  U5      n	U R                  Xx5        U R                  Xy5        U R                  XrR                  S   5        g)zc
L{ClientService.whenConnected} returns a L{Deferred} that fires when a
connection is established.
Fr=  r+   ©ÚfailAfterFailuresr   N)	r   r  r(  r±   rÓ   r:   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                 ó  • [        5       nU R                  SUS9u  p#UR                  5       nUR                  SS9nUR                  SS9nUR                  SS9nU R                  U5        U R                  U5        U R                  U5        U R                  U5        [	        [        5       5      nUR                  S   R                  U5        U R                  U5        U R                  U R                  U[
        5      U5        U R                  U5        U R                  U5        UR                  [        5        U R                  [        UR                  5      S5        U R                  U5        U R                  U5        U R                  U5        [	        [        5       5      n	UR                  S   R                  U	5        U R                  U5        U R                  U R                  U[
        5      U	5        U R                  U5        [        n
UR                  U
5        U R                  [        UR                  5      S5        U R                  U5        U R                  U5        UR                  S   R                  S5        U R                  U5      nU R                  U5      nU R                  X¼5        U R                  X²R                   S   5        UR                  SS9nU R                  U5      nU R                  X¾5        g)	zy
L{ClientService.whenConnected} returns a L{Deferred} that fails, if
asked, when some number of connections have failed.
Fr=  r+   rg  r•   é   r   N)r   r  r(  r±   r   r   rÓ   r-   r^   r1  r>  r?  rn   ro   r:   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                 óÎ  • [        5       nU R                  SUS9u  p#UR                  5       nUR                  5       nUR                  SS9nU R                  U5        U R                  U5        U R                  U5        UR	                  5         UR                  [        5        U R                  U[        5        U R                  U[        5        U R                  U[        5        g)zn
L{ClientService.whenConnected} returns a L{Deferred} that fails when
L{ClientService.stopService} is called.
Fr=  r+   rg  N)	r   r  r(  r±   r‚   r>  r?  r1  r
   )r'   r˜   r  r	  ri  rj  rk  s          r   Útest_whenConnectedStopServiceÚ0ClientServiceTests.test_whenConnectedStopService©  sÄ   € ô
 “ˆØ×*Ñ*¸5ÈÐ*ÐN‰ˆØ×!Ñ!Ó#ˆØ×!Ñ!Ó#ˆØ×!Ñ!°AÐ!Ð6ˆØ×Ñ˜AÔØ×Ñ˜AÔØ×Ñ˜AÔØ×ÑÔØ�‰Ô*Ô+Ø×Ñ˜Q¤Ô/Ø×Ñ˜Q¤Ô/Ø×Ñ˜Q¤Õ/r   c                 ó$  • [        5       nU R                  SUS9u  p#UR                  R                  S5      R	                  [        S5      5        UR                  5       nU R                  UR                  5       / 5        U R                  U5        g)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)
r   r  rÓ   rZ  r-   r   r‚   rn   r  r³   r@  s        r   Útest_retryCancelledÚ&ClientServiceTests.test_retryCancelled¼  sy   € ô “ˆØ×*Ñ*¸5ÈÐ*ÐN‰ˆØ
�‰×Ñ˜AÓ×&Ñ&¤y°Ó'AÔBØ×ÑÓ!ˆØ×Ñ˜×.Ñ.Ó0°"Ô5Ø×Ñ˜QÕr   c                 óž   • [        5       nU R                  SSUS9u  p#UR                  5       nU R                  U R	                  U5      5        g)z†
Calling L{ClientService.stopService} before
L{ClientService.startService} returns a L{Deferred} that has
already fired with L{None}.
F)rä   re   r˜   N©r   r  r‚   ÚassertIsNoner³   )r'   r˜   Ú_r	  r/   s        r   Ú"test_stopServiceBeforeStartServiceÚ5ClientServiceTests.test_stopServiceBeforeStartServiceÉ  sR   € ô “ˆØ×)Ñ)Ø!°¸Uð *ð 
‰
ˆð ×ÑÓ!ˆØ×Ñ˜$×.Ñ.¨qÓ1Õ2r   c                 ó¦  • [        5       nU R                  SUS9u  p#UR                  5       nUR                  S   R	                  [        S5      5        UR                  5         UR                  5       nU R                  U R                  U5      R                  [        5        U R                  U R                  U5      R                  [        5        g)z¢
L{ClientService.whenConnected} returns a L{Deferred} that
errbacks with L{CancelledError} if
L{ClientService.stopService} is called between connection
attempts.
Fr=  r   r„  N)r   r  r(  rÓ   r-   r   r‚   rž   r1  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                 óZ  • [        5       nU R                  SUS9u  p#UR                  S   R                  S5        UR	                  5       nUR	                  5       nUR
                  S   R                  [        [        5       5      5        U R                  U5        U R                  U5        g)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=  r   N)
r   r  rÓ   r:   r‚   rÔ   r  r   r^  r³   ©r'   r˜   r  r	  ÚfirstStopDeferredÚsecondStopDeferreds         r   Ú"test_stopServiceWhileDisconnectingÚ5ClientServiceTests.test_stopServiceWhileDisconnectingï  s›   € ô “ˆØ×*Ñ*¸5ÈÐ*ÐN‰ˆà
�‰˜Ñ×#Ñ# DÔ)ð $×/Ñ/Ó1Ðà$×0Ñ0Ó2Ðð 	×Ñ Ñ"×1Ñ1´'Ô:JÓ:LÓ2MÔNà×ÑÐ.Ô/Ø×ÑÐ/Õ0r   c                 óz  • [        5       nU R                  SUS9u  p#UR                  S   R                  S5        UR	                  5       nUR                  5         UR	                  5       nUR                  S   R                  [        [        5       5      5        U R                  U5        U R                  U5        g)z’
Calling L{ClientService.stopService} after calling a
reconnection attempt returns a L{Deferred} that fires when the
disconnection has completed.
Fr=  r   N)r   r  rÓ   r:   r‚   re   rÔ   r  r   r^  r³   r“  s         r   Útest_stopServiceWhileRestartingÚ2ClientServiceTests.test_stopServiceWhileRestarting  s§   € ô “ˆØ×*Ñ*¸5ÈÐ*ÐN‰ˆà
�‰˜Ñ×#Ñ# DÔ)ð $×/Ñ/Ó1Ðà×ÑÔà$×0Ñ0Ó2Ðð 	×Ñ Ñ"×1Ñ1´'Ô:JÓ:LÓ2MÔNà×ÑÐ.Ô/Ø×ÑÐ/Õ0r   c                 óü   • [        5       nU R                  SUS9u  p#UR                  5       nUR                  5       nU R                  U R	                  U5      5        U R                  U R	                  U5      5        g)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_stopServiceOnStoppedServiceÚ3ClientServiceTests.test_stopServiceOnStoppedService  st   € ô
 “ˆØ×)Ñ)¸%ÀuÐ)ÐM‰
ˆØ#×/Ñ/Ó1ÐØ$×0Ñ0Ó2Ðà×Ñ˜$×.Ñ.Ð/@ÓAÔBØ×Ñ˜$×.Ñ.Ð/AÓBÕCr   c                 óf   ^• S/mU4S jnU R                  USS9u  p#U R                  STS   5        g)zn
The C{prepareConnection} callable is called after
L{ClientService.startService} once the connection is made.
r   c                 ó    >• TS==   S-  ss'   g ©Nr   r+   r   ©Ú_protoÚpreparess    €r   ÚprepareConnectionÚ[ClientServiceTests.test_prepareConnectionCalledWhenServiceStarts.<locals>.prepareConnection2  ó   ø€ Ø�Q‹K˜1ÑŒKr   T)r¤  re   r+   N)r  rn   )r'   r¤  r  r	  r£  s       @r   Ú-test_prepareConnectionCalledWhenServiceStartsÚ@ClientServiceTests.test_prepareConnectionCalledWhenServiceStarts+  sE   ø€ ð
 �3ˆõ	ð ×*Ñ*Ø/¸dð +ð 
‰ˆð 	×Ñ˜˜H Q™KÕ(r   c                 ó|   ^• / mU4S jnU R                  US9u  p#U R                  UR                  S   TS   5        g)zN
The C{prepareConnection} callable is passed the connected protocol
instance.
c                 ó(   >• TR                  U 5        g r#   ©r„   )ÚprotoÚnewProtocolss    €r   r¤  ÚVClientServiceTests.test_prepareConnectionCalledWithProtocol.<locals>.prepareConnectionA  s   ø€ Ø×Ñ Õ&r   ©r¤  r   N)r  r^   rÔ   )r'   r¤  r  r	  r­  s       @r   Ú(test_prepareConnectionCalledWithProtocolÚ;ClientServiceTests.test_prepareConnectionCalledWithProtocol:  sL   ø€ ð
 ˆõ	'ð ×*Ñ*Ø/ð +ð 
‰ˆð 	×Ñ˜R×4Ñ4°QÑ7¸Àa¹ÕIr   c                 óZ  ^• S/mU4S jn[        5       nU R                  USUS9u  p4UR                  S   R                  [	        S5      5        U R                  STS   5        UR                  [        5        UR                  S   R                  S5        U R                  STS   5        g)zN
The C{prepareConnection} callback is invoked only once a connection is
made.
r   c                 ó    >• TS==   S-  ss'   g r   r   r¡  s    €r   r¤  Ú]ClientServiceTests.test_prepareConnectionCalledAfterConnectionMade.<locals>.prepareConnectionP  r¦  r   F)r¤  rä   r˜   zconnection attempt failedr+   N)	r   r  rÓ   r-   r   rn   r>  r?  r:   ©r'   r¤  r˜   r  r	  r£  s        @r   Ú/test_prepareConnectionCalledAfterConnectionMadeÚBClientServiceTests.test_prepareConnectionCalledAfterConnectionMadeI  s£   ø€ ð
 �3ˆõ	ô “ˆØ×*Ñ*Ø/ÀÈeð +ð 
‰ˆð 	�‰˜Ñ×"Ñ"¤9Ð-HÓ#IÔJØ×Ñ˜˜H Q™KÔ(à�‰Ô*Ô+Ø
�‰˜Ñ×#Ñ# DÔ)à×Ñ˜˜H Q™KÕ(r   c                 ó*  ^• S/mU4S jn[        5       nU R                  XS9u  p4U R                  STS   5        UR                  S   R	                  [        [        5       5      5        UR                  [        5        U R                  STS   5        g)zi
The C{prepareConnection} callback is invoked each time a connection is
made, including on reconnection.
r   c                 ó    >• TS==   S-  ss'   g r   r   r¡  s    €r   r¤  ÚUClientServiceTests.test_prepareConnectionCalledOnReconnect.<locals>.prepareConnectiong  r¦  r   ©r¤  r˜   r+   r•   N)	r   r  rn   rÔ   r  r   r^  r>  r?  rµ  s        @r   Ú'test_prepareConnectionCalledOnReconnectÚ:ClientServiceTests.test_prepareConnectionCalledOnReconnect`  s�   ø€ ð
 �3ˆõ	ô “ˆØ×*Ñ*Ø/ð +ð 
‰ˆð 	×Ñ˜˜H Q™KÔ(ð 	×Ñ Ñ"×1Ñ1´'Ô:JÓ:LÓ2MÔNØ�‰Ô*Ô+à×Ñ˜˜H Q™KÕ(r   c                 óª   ^• [        5       mU4S jnU R                  US9u  p#U R                  UR                  5       5      nU R	                  TU5        g)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                 ó   >• T$ r#   r   )r¬  Úsentinels    €r   r¤  ÚVClientServiceTests.test_prepareConnectionReturnValueIgnored.<locals>.prepareConnection‚  s   ø€ ØˆOr   r¯  N)r²   r  r³   r(  ÚassertNotIdentical)r'   r¤  r  r	  r5   rÀ  s        @r   Ú(test_prepareConnectionReturnValueIgnoredÚ;ClientServiceTests.test_prepareConnectionReturnValueIgnoredw  sR   ø€ ô “8ˆõ	ð ×*Ñ*Ð=NÐ*ÐO‰ˆà×%Ñ% g×&;Ñ&;Ó&=Ó>ˆØ×Ñ ¨&Õ1r   c                 ó  ^^• / m[        5       mUU4S jnU R                  US9u  p#UR                  5       nU R                  U5        TR	                  S5        U R                  UR                  S   U R                  U5      5        g)zw
The C{prepareConnection} callable returns a deferred and calls to
L{ClientService.whenConnected} wait until it fires.
c                 ó*   >• TR                  U 5        T$ r#   r«  )r¬  ÚnewProtocolDeferredr­  s    €€r   r¤  ÚVClientServiceTests.test_prepareConnectionReturningADeferred.<locals>.prepareConnection’  s   ø€ Ø×Ñ Ô&Ø&Ð&r   r¯  Nr   )r   r  r(  r±   r:   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                 óT  • [        5       nS nU R                  X!S9u  p4UR                  SS9nU R                  U5        UR	                  [
        5        U R                  U5        UR	                  [
        5        U R                  [        U R                  U5      R                  5        g)z[
The connection attempt counts as a failure when the
C{prepareConnection} callable throws.
c                 ó   • [        5       er#   )r^  )r¢  s    r   r¤  ÚJClientServiceTests.test_prepareConnectionThrows.<locals>.prepareConnection¨  s   € Ü"Ó$Ð$r   r»  r•   rg  N)
r   r  r(  r±   r>  r?  r^   r^  r1  Ú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Ñ2Ð3HÓI×NÑNõ	
r   r   )ÚreturnN)'rA   rB   rC   rD   rE   r   r  r¡   r  r"  r%  r-  r2  r:  rA  rD  rJ  rR  rU  r[  r`  rd  ro  r~  r�  r…  r‹  r�  r–  r™  rœ  r§  r°  r¶  r¼  rÃ  rÊ  rÐ  rF   r   r   r   rö   rö   þ  s¾   † ñð ØØØôHòT2ò6ò ò:ò +ò8
ò"Jò( ò
 ò
ò02ò"
ô*$+òL"òMòBò*81òt0ò& ò3ò
ò21ò.1ò0Dò)òJò)ò.)ò.2ò&
õ.
r   rö   )F)0rE   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$   rU   r“   rÐ   rè   rô   r?  rö   r   r   r   Ú<module>rà     sô   ðñ
ó å &Ý -å (÷ñ õ
 "ß ;÷õ ÷ 8Ý 'Ý 4ß :Ý *ß @ò	ñ Ð"Ó#÷=*ð =*ó $ð=*ñ@ Ð! :Ô .ñ ˆ^Ó÷ð ó ðñ6 Ð! :Ô .ôV1˜8ô V1ôrC-˜ô C-÷L"ñ "ô( &ðF &8ô :ð$ Ð ôz

Ð,õ z

r   