Ë
    ojM£  ã                   ó¾  — d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZmZ dd	lmZmZmZmZmZ dd
lmZmZ ddlmZ ddlmZ ddlmZm Z  ddl!m"Z" ddl#m$Z$m%Z% d„ Z& ee«       G d„ d«      «       Z' eee'«        ee«       G d„ d«      «       Z( eee'«        G d„ de%«      Z) G d„ de%«      Z* G d„ d«      Z+d d„Z,e fd„Z-dZ. G d„ de$«      Z/y)!zœ
Tests for (new code in) L{twisted.application.internet}.

@var AT_LEAST_ONE_ATTEMPT: At least enough seconds for L{ClientService} to make
    one attempt.
é    N)Úimplementer)ÚverifyClass)Úinternet)ÚClientServiceÚStreamServerEndpointServiceÚTimerService)Útask)ÚCancelledErrorÚDeferred)ÚIFileDescriptorReceiverÚIHalfCloseableProtocolÚIListeningPortÚIStreamClientEndpointÚIStreamServerEndpoint)ÚFactoryÚProtocol)ÚClock)ÚStringTransport)ÚformatEventÚglobalLogPublisher)ÚFailure)ÚSynchronousTestCaseÚTestCasec                   ó   — y)zM
    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                   óF   — e Zd ZdZdZdZdZ e«       ZdZ	d„ Z
d„ Zd„ Zd„ Zy)Ú
FakeServeraq  
    In-memory implementation of L{IStreamServerEndpoint}.

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

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

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

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

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

    @ivar failImmediately: If set, the exception to fail the L{Deferred}
        returned from C{listen} before it is returned.
    Nr   c                 ó"   — t        «       | _        y ©N)ÚFakePortÚport©Úselfs    r   Ú__init__zFakeServer.__init__L   s   € Ü“Jˆ�	r   c                 óÞ   ‡ — ‰ xj                   dz  c_         |‰ _        t        ˆ fd„¬«      ‰ _        ‰ j                  �%‰ j                  j                  ‰ j                  «       ‰ j                  S )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                 ó:   •— | j                  ‰j                  «      S r"   )ÚerrbackÚcancelException)Údr&   s    €r   ú<lambda>z#FakeServer.listen.<locals>.<lambda>Z   s   ø€ °1·9±9¸T×=QÑ=QÓ3R€ r   )Ú	canceller)ÚlistenAttemptsÚfactoryr   ÚresultÚfailImmediatelyr+   )r&   r1   s   ` r   ÚlistenzFakeServer.listenO   sX   ø€ ð 	×Ò˜qÑ ÕØˆŒÜÓ)RÔSˆŒØ×ÑÐ+Ø�K‰K×Ñ × 4Ñ 4Ô5Ø�{‰{Ðr   c                 óN   — | j                   j                  | j                  «       y)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   ÚstartedListeningzFakeServer.startedListening_   s   € ð 	�‰×Ñ˜TŸY™YÕ'r   c                 óN   — | j                   j                  j                  d«       y)a  
        Test code should invoke this method after causing C{stopListening} to
        be invoked on the port fired from the L{Deferred} returned from
        C{listen} in order to cause the L{Deferred} returned from
        C{stopListening} to fire.
        N)r$   Údeferredr6   r%   s    r   ÚstoppedListeningzFakeServer.stoppedListeningg   s   € ð 	�	‰	×Ñ×#Ñ# DÕ)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r2   r1   r3   r
   r,   r0   r'   r4   r7   r:   r   r   r   r    r    0   s9   „ ñð( €FØ€GØ€OÙ$Ó&€OØ€Nòòò (ó*r   r    c                   ó&   — e Zd ZdZdZd„ Zd„ Zd„ Zy)r#   zs
    Fake L{IListeningPort} implementation.

    @ivar deferred: The L{Deferred} returned by C{stopListening}.
    Nc                 ó8   — t        «       | _        | j                  S )z`
        Stop listening.

        @return: a L{Deferred} stored in L{FakePort.deferred}
        )r   r9   r%   s    r   ÚstopListeningzFakePort.stopListening~   s   € ô !›
ˆŒØ�}‰}Ðr   c                  ó   — y r"   r   r%   s    r   ÚgetHostzFakePort.getHost‡   ó   € àr   c                  ó   — y r"   r   r%   s    r   ÚstartListeningzFakePort.startListening‹   rD   r   )r;   r<   r=   r>   r9   rA   rC   rF   r   r   r   r#   r#   t   s   „ ñð €Hòòór   r#   c                   óZ   — e Zd ZdZd„ Zd„ Zdd„Zd„ Zd„ Zd„ Z	d	„ Z
d
„ Zd„ Zd„ Zd„ Zd„ Zy)ÚEndpointServiceTestsz4
    Tests for L{twisted.application.internet}.
    c                 óŠ   — t        «       | _        t        «       | _        t	        | j                  | j                  «      | _        y)zp
        Construct a stub server, a stub factory, and a
        L{StreamServerEndpointService} to test.
        N)r    Ú
fakeServerr   r1   r   Úsvcr%   s    r   ÚsetUpzEndpointServiceTests.setUp˜   s,   € ô
 %›,ˆŒÜ“yˆŒÜ.¨t¯©ÀÇÁÓMˆ�r   c                 ó˜   — | j                   j                  «        | j                  | j                  | j                  j                  «       y)z‡
        L{StreamServerEndpointService.privilegedStartService} calls its
        endpoint's C{listen} method with its factory.
        N)rK   ÚprivilegedStartServiceÚassertIdenticalr1   rJ   r%   s    r   Útest_privilegedStartServicez0EndpointServiceTests.test_privilegedStartService¡   s2   € ð
 	�‰×'Ñ'Ô)Ø×Ñ˜TŸ\™\¨4¯?©?×+BÑ+BÕCr   Nc                 ó´   — t        «       | j                  _        d| j                  _        | j                  t         |xs | j                  j                  «       y)a@  
        L{StreamServerEndpointService.startService} should raise synchronously
        if the L{Deferred} returned by its wrapped
        L{IStreamServerEndpoint.listen} has already fired with an errback and
        the L{StreamServerEndpointService}'s C{_raiseSynchronously} flag has
        been set.  This feature is necessary to preserve compatibility with old
        behavior of L{twisted.internet.strports.service}, which is to return a
        service which synchronously raises an exception from C{startService}
        (so that, among other things, twistd will not start running).  However,
        since L{IStreamServerEndpoint.listen} may fail asynchronously, it is a
        bad idea to rely on this behavior.

        @param thunk: If specified, a callable to execute in place of
            C{startService}.
        TN)ÚZeroDivisionErrorrJ   r3   rK   Ú_raiseSynchronouslyÚassertRaisesÚstartService)r&   Úthunks     r   Ú(test_synchronousRaiseRaisesSynchronouslyz=EndpointServiceTests.test_synchronousRaiseRaisesSynchronously©   s?   € ô  +<Ó*=ˆ�‰Ô'Ø'+ˆ�‰Ô$Ø×ÑÔ+¨UÒ-K°d·h±h×6KÑ6KÕLr   c                 óN   — | j                  | j                  j                  «       y)zÓ
        L{StreamServerEndpointService.privilegedStartService} should behave the
        same as C{startService} with respect to
        L{EndpointServiceTests.test_synchronousRaiseRaisesSynchronously}.
        N)rW   rK   rN   r%   s    r   Útest_synchronousRaisePrivilegedz4EndpointServiceTests.test_synchronousRaisePrivileged½   s   € ð 	×5Ñ5°d·h±h×6UÑ6UÕVr   c                 óò   — | j                   j                  «        | j                  j                  j	                  t        «       «       | j                  t
        «      }| j                  t        |«      d«       y)a  
        L{StreamServerEndpointService.startService} and
        L{StreamServerEndpointService.privilegedStartService} should both log
        an exception when the L{Deferred} returned from their wrapped
        L{IStreamServerEndpoint.listen} fails.
        r)   N)	rK   rU   rJ   r2   r+   rR   ÚflushLoggedErrorsÚassertEqualÚlen©r&   Úloggeds     r   Útest_failReportsErrorz*EndpointServiceTests.test_failReportsErrorÅ   sU   € ð 	�‰×ÑÔØ�‰×Ñ×&Ñ&Ô'8Ó':Ô;Ø×'Ñ'Ô(9Ó:ˆØ×Ñœ˜V› aÕ(r   c                 ó  — d| j                   _        | j                   j                  «        | j                  j                  j                  t        «       «       | j                  t        «      }| j                  t        |«      d«       y)aB  
        L{StreamServerEndpointService.startService} and
        L{StreamServerEndpointService.privilegedStartService} should both log
        an exception when the L{Deferred} returned from their wrapped
        L{IStreamServerEndpoint.listen} fails asynchronously, even if
        C{_raiseSynchronously} is set.
        Tr)   N)
rK   rS   rU   rJ   r2   r+   rR   r[   r\   r]   r^   s     r   Ú!test_asynchronousFailReportsErrorz6EndpointServiceTests.test_asynchronousFailReportsErrorÑ   sa   € ð (,ˆ�‰Ô$Ø�‰×ÑÔØ�‰×Ñ×&Ñ&Ô'8Ó':Ô;Ø×'Ñ'Ô(9Ó:ˆØ×Ñœ˜V› aÕ(r   c                 óÊ   — t        «       | j                  _        | j                  j	                  «        | j                  t         «      }| j                  t        |«      d«       y)zŸ
        Without the C{_raiseSynchronously} compatibility flag, failing
        immediately has the same behavior as failing later; it logs the error.
        r)   N)rR   rJ   r3   rK   rU   r[   r\   r]   r^   s     r   Ú test_synchronousFailReportsErrorz5EndpointServiceTests.test_synchronousFailReportsErrorß   sH   € ô
 +<Ó*=ˆ�‰Ô'Ø�‰×ÑÔØ×'Ñ'Ô(9Ó:ˆØ×Ñœ˜V› aÕ(r   c                 óä   — | j                   j                  «        | j                  | j                  | j                  j                  «       | j                  | j                   j                  d«       y)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)rK   rU   rO   r1   rJ   r\   Úrunningr%   s    r   Útest_startServiceUnstartedz/EndpointServiceTests.test_startServiceUnstartedé   sL   € ð 	�‰×ÑÔØ×Ñ˜TŸ\™\¨4¯?©?×+BÑ+BÔCØ×Ñ˜Ÿ™×)Ñ)¨4Õ0r   c                 óð   — | j                  «        | j                  j                  «        | j                  | j                  j
                  d«       | j                  | j                  j                  d«       y)zš
        L{StreamServerEndpointService.startService} sets the C{running} flag,
        but nothing else, if the service has already been started.
        r)   TN)rP   rK   rU   r\   rJ   r0   rf   r%   s    r   Útest_startServiceStartedz-EndpointServiceTests.test_startServiceStartedó   sT   € ð
 	×(Ñ(Ô*Ø�‰×ÑÔØ×Ñ˜Ÿ™×7Ñ7¸Ô;Ø×Ñ˜Ÿ™×)Ñ)¨4Õ0r   c                 óø  — | j                   j                  «        | j                  j                  «        | j                   j	                  «        | j                   j                  «       }g }|j                  |j                  «       | j                  t        |«      d«       | j                  j                  «        | j                  t        |«      d«       | j                  | j                   j                  «       y)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)rK   rN   rJ   r7   rU   ÚstopServiceÚaddCallbackÚappendr\   r]   r:   ÚassertFalserf   ©r&   r2   Úls      r   Útest_stopServicez%EndpointServiceTests.test_stopServiceý   s­   € ð 	�‰×'Ñ'Ô)Ø�‰×(Ñ(Ô*à�‰×ÑÔØ—‘×%Ñ%Ó'ˆØˆØ×Ñ˜1Ÿ8™8Ô$Ø×Ñœ˜Q› Ô#Ø�‰×(Ñ(Ô*Ø×Ñœ˜Q› Ô#Ø×Ñ˜Ÿ™×)Ñ)Õ*r   c                 ó  — | j                   j                  «        | j                   j                  «       }g }|j                  |j                  «       | j                  |dg«       | j                  | j                  t        «      g «       y)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)rK   rN   rk   ÚaddBothrm   r\   r[   r
   ro   s      r   Ú#test_stopServiceBeforeStartFinishedz8EndpointServiceTests.test_stopServiceBeforeStartFinished  sh   € ð 	�‰×'Ñ'Ô)Ø—‘×%Ñ%Ó'ˆØˆØ�‰�q—x‘xÔ Ø×Ñ˜˜T˜FÔ#Ø×Ñ˜×/Ñ/´Ó?ÀÕDr   c                 ó^  — t        «       | j                  _        | j                  j	                  «        | j                  j                  «       }g }|j                  |j                  «       | j                  |dg«       | j                  t         «      }| j                  t        |«      d«       y)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)   )rR   rJ   r,   rK   rN   rk   rl   rm   r\   r[   r]   )r&   r2   rp   ÚstoppingErrorss       r   Ú test_stopServiceCancelStartErrorz5EndpointServiceTests.test_stopServiceCancelStartError  s„   € ô +<Ó*=ˆ�‰Ô'Ø�‰×'Ñ'Ô)Ø—‘×%Ñ%Ó'ˆØˆØ×Ñ˜1Ÿ8™8Ô$Ø×Ñ˜˜T˜FÔ#Ø×/Ñ/Ô0AÓBˆØ×Ñœ˜^Ó,¨aÕ0r   r"   )r;   r<   r=   r>   rL   rP   rW   rY   r`   rb   rd   rg   ri   rq   rt   rw   r   r   r   rH   rH   “   sH   „ ñòNòDóMò(Wò
)ò)ò)ò1ò1ò+ò$Eó1r   rH   c                   óL   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ Zd„ Z	d„ Z
d	„ Zd
„ Zd„ Zy)ÚTimerServiceTestsa)  
    Tests for L{twisted.application.internet.TimerService}.

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

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

    @type deferred: L{Deferred}
    @ivar deferred: deferred returned by L{TimerServiceTests.call}.
    c                 ó¬   — t        d| j                  «      | _        t        j                  «       x| _        | j                  _        t        «       | _        y)z1
        Set up a timer service to test.
        é   N)r   ÚcallÚtimerr	   r   Úclockr   r9   r%   s    r   rL   zTimerServiceTests.setUp:  s8   € ô " ! T§Y¡YÓ/ˆŒ
Ü(,¯
©
«Ð4ˆŒ
�T—Z‘ZÔ%Ü ›
ˆ�r   c                 ó   — | j                   S )z‚
        Function called by L{TimerService} being tested.

        @returns: C{self.deferred}
        @rtype: L{Deferred}
        )r9   r%   s    r   r|   zTimerServiceTests.callB  s   € ð �}‰}Ðr   c                 óÀ  — | j                   j                  «        | j                  | j                   j                  d«       | j	                  | j                   j
                  t        j                  «       | j                  | j                  | j                   j
                  j                  «       | j                  | j                   j
                  j                  d«       y)z�
        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}   rU   Ú
assertTruerf   ÚassertIsInstanceÚ_loopr	   ÚLoopingCallrO   r~   r%   s    r   Útest_startServicez#TimerServiceTests.test_startServiceK  s�   € ð
 	�
‰
×ÑÔ!Ø�‰˜Ÿ
™
×*Ñ*Ð,@ÔAØ×Ñ˜dŸj™j×.Ñ.´×0@Ñ0@ÔAØ×Ñ˜TŸZ™Z¨¯©×)9Ñ)9×)?Ñ)?Ô@Ø�‰˜Ÿ
™
×(Ñ(×0Ñ0Ð2JÕKr   c                 óž   — g }|j                   di f| j                  _        | j                  j                  «        | j	                  dg|«       y)zi
        When L{TimerService.startService} is called, it calls the function
        immediately.
        r"   N)rm   r}   r|   rU   r\   )r&   r2   s     r   Ú$test_startServiceRunsCallImmediatelyz6TimerServiceTests.test_startServiceRunsCallImmediatelyV  sA   € ð
 ˆØ!Ÿ=™=¨'°2Ð6ˆ�
‰
ŒØ�
‰
×ÑÔ!Ø×Ñ˜$˜ Õ(r   c                 óú   ‡— t        j                  «       Šˆfd„}| j                  t        d|«       | j                  j                  «        | j                  ‰| j                  j                  j                  «       y)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   ÚgetOtherClockzKTimerServiceTests.test_startServiceUsesGlobalReactor.<locals>.getOtherClockh  s	   ø€ ØÐr   Ú_maybeGlobalReactorN)	r	   r   Úpatchr   r}   rU   rO   rƒ   r~   )r&   rŒ   r‹   s     @r   Ú"test_startServiceUsesGlobalReactorz4TimerServiceTests.test_startServiceUsesGlobalReactor`  sX   ø€ ô —Z‘Z“\ˆ
ô	ð 	�
‰
”8Ð2°MÔBØ�
‰
×ÑÔ!Ø×Ñ˜Z¨¯©×)9Ñ)9×)?Ñ)?Õ@r   c                 ób  — | j                   j                  «        | j                   j                  «       }| j                  |«       | j	                  d| j                   j
                  «       | j                  j                  t        «       «       | j                  | j                  |«      d«       y)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}   rU   rk   ÚassertNoResultr\   rf   r9   r6   ÚobjectrO   ÚsuccessResultOf©r&   r-   s     r   Útest_stopServiceWaitsz'TimerServiceTests.test_stopServiceWaitso  s}   € ð
 	�
‰
×ÑÔ!Ø�J‰J×"Ñ"Ó$ˆØ×Ñ˜AÔØ×Ñ˜˜tŸz™z×1Ñ1Ô2Ø�‰×Ñœv›xÔ(Ø×Ñ˜T×1Ñ1°!Ó4°dÕ;r   c                 óô   — | j                   j                  «        | j                  j                  t	        «       «       | j                   j                  «       }| j                  | j                  |«      d«       y)z•
        When L{TimerService.stopService} is called while a call isn't in progress.
        the L{Deferred} returned has already been fired.
        N)r}   rU   r9   r6   r’   rk   rO   r“   r”   s     r   Útest_stopServiceImmediatelyz-TimerServiceTests.test_stopServiceImmediately{  sU   € ð
 	�
‰
×ÑÔ!Ø�‰×Ñœv›xÔ(Ø�J‰J×"Ñ"Ó$ˆØ×Ñ˜T×1Ñ1°!Ó4°dÕ;r   c                 óf  — | j                   j                  «        | j                  j                  t	        t        «       «      «       | j                  t
        «      }| j                  dt        |«      «       | j                   j                  «       }| j                  | j                  |«      d«       y)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}   rU   r9   r+   r   rR   r[   r\   r]   rk   rO   r“   )r&   Úerrorsr-   s      r   Útest_failedCallLogsErrorz*TimerServiceTests.test_failedCallLogsError…  s�   € ð
 	�
‰
×ÑÔ!Ø�‰×ÑœgÔ&7Ó&9Ó:Ô;Ø×'Ñ'Ô(9Ó:ˆØ×Ñ˜œC ›KÔ(Ø�J‰J×"Ñ"Ó$ˆØ×Ñ˜T×1Ñ1°!Ó4°dÕ;r   c                 ó
  — t        dt        «      }|j                  «        t        j                  |«      }|j                  «        t        j                  |«      }t        «       }t        |d|«      }| j                  ||«       y)zq
        When pickling L{internet.TimerService}, it won't pickle
        L{internet.TimerService._loop}.
        r)   rƒ   N©
r   r   rU   ÚpickleÚdumpsrk   Úloadsr’   ÚgetattrrO   ©r&   r}   ÚdumpedTimerÚloadedTimerÚnothingÚvalues         r   Ú$test_pickleTimerServiceNotPickleLoopz6TimerServiceTests.test_pickleTimerServiceNotPickleLoop‘  sj   € ô ˜QÔ 2Ó3ˆØ×ÑÔÜ—l‘l 5Ó)ˆØ×ÑÔÜ—l‘l ;Ó/ˆÜ“(ˆÜ˜ W¨gÓ6ˆØ×Ñ˜W eÕ,r   c                 ó
  — t        dt        «      }|j                  «        t        j                  |«      }|j                  «        t        j                  |«      }t        «       }t        |d|«      }| j                  ||«       y)zy
        When pickling L{internet.TimerService}, it won't pickle
        L{internet.TimerService._loopFinished}.
        r)   Ú_loopFinishedNrœ   r¡   s         r   Ú,test_pickleTimerServiceNotPickleLoopFinishedz>TimerServiceTests.test_pickleTimerServiceNotPickleLoopFinished¡  sj   € ô ˜QÔ 2Ó3ˆØ×ÑÔÜ—l‘l 5Ó)ˆØ×ÑÔÜ—l‘l ;Ó/ˆÜ“(ˆÜ˜ _°gÓ>ˆØ×Ñ˜W eÕ,r   N)r;   r<   r=   r>   rL   r|   r…   r‡   r�   r•   r—   rš   r¦   r©   r   r   r   ry   ry   ,  s;   „ ñò#òò	Lò)òAò
<ò<ò
<ò-ó -r   ry   c                   ó   — e Zd ZdZd„ Zy)ÚConnectInformationaË  
    Information about C{endpointForTesting}

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

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

    @ivar passedFactories: a L{list} of L{IProtocolFactory}; the ones actually
        passed to the underlying endpoint / i.e. the reactor.
    c                 ó.   — g | _         g | _        g | _        y r"   )ÚconnectQueueÚconstructedProtocolsÚpassedFactoriesr%   s    r   r'   zConnectInformation.__init__À  s   € ØˆÔØ$&ˆÔ!Ø!ˆÕr   N)r;   r<   r=   r>   r'   r   r   r   r«   r«   ²  s   „ ñó"r   r«   c                 ój   ‡ ‡— t        t        «       G ˆ ˆfd„d«      «       }t        «       Š‰ |«       fS )aD  
    Make a sample endpoint for testing.

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

    @return: a 2-tuple of C{(information, endpoint)}, where C{information} is a
        L{ConnectInformation} describing the operations in progress on
        C{endpoint}.
    c                   ó   •— e Zd Zˆ ˆfd„Zy)ú.endpointForTesting.<locals>.ClientTestEndpointc                 óÚ   •‡— t        «       }‰j                  j                  ‰«       |j                  ˆˆfd„«       }‰j                  j                  |«       ‰r|j                  d «       |S )Nc                 ó–   •— ‰j                  d «      }‰j                  j                  |«       t        «       }|j	                  |«       |S r"   )ÚbuildProtocolr®   rm   r   ÚmakeConnection)ÚignoredÚprotocolÚ	transportr1   Úinfos      €€r   ÚcreateProtocolzNendpointForTesting.<locals>.ClientTestEndpoint.connect.<locals>.createProtocolØ  sB   ø€ à"×0Ñ0°Ó6�Ø×)Ñ)×0Ñ0°Ô:Ü+Ó-�	Ø×'Ñ'¨	Ô2Ø�r   )r   r¯   rm   rl   r­   r6   )r&   r1   r2   r»   ÚfireImmediatelyrº   s    `  €€r   Úconnectz6endpointForTesting.<locals>.ClientTestEndpoint.connectÔ  s`   ù€ Ü“ZˆFØ× Ñ ×'Ñ'¨Ô0à×Ñô ó  ð ð ×Ñ×$Ñ$ VÔ,ÙØ—‘ Ô%ØˆMr   N)r;   r<   r=   r½   )r¼   rº   s   €€r   ÚClientTestEndpointr²   Ò  s   ø„ ö	r   r¾   )r   r   r«   )r¼   r¾   rº   s   ` @r   ÚendpointForTestingr¿   Æ  s;   ù€ ô Ô&Ó'÷ó ó (ðô$ Ó€DØÑ#Ó%Ð%Ð%r   c                 ót   ‡‡— g Š‰j                  ‰j                  «       | j                  ˆˆfd„«       ˆfd„S )a6  
    Catch the global log stream.

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

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

    @return: a 0-argument callable that returns a list of textual log messages
        for comparison.
    @rtype: L{list} of L{unicode}
    c                  ó:   •— ‰ j                  ‰j                  «      S r"   )ÚremoveObserverrm   )ÚlogPublisherÚlogss   €€r   r.   zcatchLogs.<locals>.<lambda>÷  s   ø€  × ;Ñ ;¸D¿K¹KÓ H€ r   c                  ó@   •— ‰D � cg c]  } t        | «      ‘Œ c} S c c} w r"   )r   )ÚeventrÄ   s    €r   r.   zcatchLogs.<locals>.<lambda>ø  s   ø€ °DÖ9¨5”K Õ&Ò9€ ùÒ9s   †)ÚaddObserverrm   Ú
addCleanup)ÚtestCaserÃ   rÄ   s    `@r   Ú	catchLogsrÊ   é  s1   ù€ ð €DØ×Ñ˜TŸ[™[Ô)Ø×ÑÔHÔIÛ9Ð9r   g      Y@c                   óÒ   — e Zd ZdZddefd„Zd„ Zd„ Zd„ Zd„ Z	d„ Z
d	„ Zd
„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Z d„ Z!d „ Z"d!„ Z#y")#ÚClientServiceTestsz%
    Tests for L{ClientService}.
    Tc                 óV  ‡ ‡‡‡‡— i }|j                  t        «       ¬«       |j                  |«       |d   Št        |¬«      \  }}g xŠ|_         G ˆˆfd„dt        «      } |«       x|_        }	t        ||	fi |¤ŽŠˆˆ ˆfd„}
‰ j                  |
«       |r‰j                  «        |‰fS )aL  
        Create a L{ClientService} along with a L{ConnectInformation} indicating
        the connections in progress on its endpoint.

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

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

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

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

        @return: a 2-tuple of L{ConnectInformation} (for information about test
            state) and L{ClientService} (the system under test).  The
            L{ConnectInformation} has 2 additional attributes;
            C{applicationFactory} and C{applicationProtocols}, which refer to
            the unwrapped protocol factory and protocol instances passed in to
            L{ClientService} respectively.
        ©r~   r~   ©r¼   c                   ó(   •‡ — e Zd ZW °Zˆ ˆfd„Zˆ xZS )ú>ClientServiceTests.makeReconnector.<locals>.RememberingFactoryc                 óH   •— t         ‰| �  |«      }‰j                  |«       |S r"   )Úsuperrµ   rm   )r&   Úaddrr2   Ú	__class__ÚapplicationProtocolss      €€r   rµ   zLClientServiceTests.makeReconnector.<locals>.RememberingFactory.buildProtocol0  s%   ø€ Ü™Ñ.¨tÓ4�Ø$×+Ñ+¨FÔ3Ø�r   )r;   r<   r=   r¸   rµ   Ú__classcell__)rÕ   rÖ   ÚprotocolTypes   @€€r   ÚRememberingFactoryrÑ   -  s   ù„ Ù#ˆH÷ñ r   rÙ   c                  óŒ   •— d ‰_         ‰j                  r‰j                  «        ‰j                  ‰ j	                  «       g «       y r"   )Ú	_protocolrf   rk   r\   ÚgetDelayedCalls)r~   r&   Úservices   €€€r   Ústopz0ClientServiceTests.makeReconnector.<locals>.stop9  s9   ø€ Ø $ˆGÔØ�ŠØ×#Ñ#Ô%ð ×Ñ˜U×2Ñ2Ó4°bÕ9r   )	Úupdater   r¿   rÖ   r   ÚapplicationFactoryr   rÈ   rU   )r&   r¼   rU   rØ   ÚkwÚnkwÚcqÚendpointrÙ   r1   rÞ   rÖ   r~   rÝ   s   `  `       @@@r   ÚmakeReconnectorz"ClientServiceTests.makeReconnector  s§   ü€ ð8 ˆØ�
‰
œ›ˆ
Ô!Ø�
‰
�2ŒØ�G‘ˆÜ)¸/ÔJ‰ˆˆHð :<Ð;Ð˜rÔ6÷	¤ô 	ñ +=Ó*>Ð>ˆÔ ä ¨'Ñ9°SÑ9ˆö	:ð 	�‰˜ÔÙØ× Ñ Ô"Ø�7ˆ{Ðr   c                 óx   — | j                  d¬«      \  }}| j                  t        |j                  «      d«       y)zL
        When the service is started, a connection attempt is made.
        FrÏ   r)   N)rå   r\   r]   r­   )r&   rã   rÝ   s      r   r…   z$ClientServiceTests.test_startServiceF  s4   € ð ×*Ñ*¸5Ð*ÓA‰ˆˆGØ×Ñœ˜RŸ_™_Ó-¨qÕ1r   c                 óð   — | j                  «       \  }}|j                  }| j                  |j                  d«       |j                  d   }|j                  «        | j                  |j                  d«       y)a!  
        Although somewhat obscure, L{IProtocolFactory} includes both C{doStart}
        and C{doStop} methods; ensure that when these methods are called on the
        factory that was passed to the reactor, the factory that was passed
        from the application receives them.
        r   r)   N)rå   rà   r\   ÚnumPortsr¯   ÚdoStart)r&   rã   rÝ   ÚfirstAppFactoryÚfirstPassedFactorys        r   Útest_startStopFactoryz(ClientServiceTests.test_startStopFactoryM  sj   € ð ×*Ñ*Ó,‰ˆˆGØ×/Ñ/ˆØ×Ñ˜×1Ñ1°1Ô5Ø×/Ñ/°Ñ2ÐØ×"Ñ"Ô$Ø×Ñ˜×1Ñ1°1Õ5r   c                 ó<  — | j                  «       \  }}|j                  «       }| j                  |«       |j                  d   }| j	                  |j
                  j                  d«       |j                  t        t        «       «      «       | j                  |«       y)z¸
        When the service is stopped, no further connect attempts are made.  The
        returned L{Deferred} fires when all outstanding connections have been
        stopped.
        r   TN)rå   rk   r‘   r®   r\   r¹   ÚdisconnectingÚconnectionLostr   Ú	Exceptionr“   ©r&   rã   rÝ   r-   r¸   s        r   Útest_stopServiceWhileConnectedz1ClientServiceTests.test_stopServiceWhileConnected[  s�   € ð ×*Ñ*Ó,‰ˆˆGØ×ÑÓ!ˆØ×Ñ˜AÔØ×*Ñ*¨1Ñ-ˆØ×Ñ˜×+Ñ+×9Ñ9¸4Ô@Ø×Ñ¤¬	«Ó 4Ô5Ø×Ñ˜QÕr   c                 óð  — | j                  «       \  }}|j                  «       }| j                  |«       |j                  d   }| j	                  |j
                  j                  d«       |j                  «        | j                  |«       | j	                  t        |j                  «      d«       |j                  t        t        «       «      «       | j	                  t        |j                  «      d«       y)z—
        When L{ClientService} is restarted after having been connected, it
        waits to start connecting until after having disconnected.
        r   Tr)   r{   N)rå   rk   r‘   r®   r\   r¹   rî   rU   r]   rï   r   rð   rñ   s        r   Ú#test_startServiceWaitsForDisconnectz6ClientServiceTests.test_startServiceWaitsForDisconnecti  sÃ   € ð
 ×*Ñ*Ó,‰ˆˆGØ×ÑÓ!ˆØ×Ñ˜AÔØ×*Ñ*¨1Ñ-ˆØ×Ñ˜×+Ñ+×9Ñ9¸4Ô@Ø×ÑÔØ×Ñ˜AÔØ×Ñœ˜R×4Ñ4Ó5°qÔ9Ø×Ñ¤¬	«Ó 4Ô5Ø×Ñœ˜R×4Ñ4Ó5°qÕ9r   c                 óF  — | j                  d¬«      \  }}|j                  d   j                  d«       |j                  d   }|j	                  «       }| j                  |«       |j                  «       }| j                  |«       |j                  «        | j                  |«       | j                  |«       | j                  |j                  j                  d«       |j                  t        t        «       «      «       | j                  |«       |j                  d   j                  d«       | j                  t        |j                  «      d«       | j!                  | j                  |«      |j"                  d   «       |j	                  «       }| j                  |«       y)a%  
        When L{ClientService} is stopping - that is,
        L{ClientService.stopService} has been called, but the L{Deferred} it
        returns has not fired yet - calling L{startService} will cause a new
        connection to be made, and new calls to L{whenConnected} to succeed.
        FrÏ   r   NTr)   r{   )rå   r­   r6   r®   rk   r‘   ÚwhenConnectedrU   r\   r¹   rî   rï   r   rð   r“   r]   rO   rÖ   )r&   rã   rÝ   ÚfirstÚstoppedÚnextProtocolÚsecondStoppeds          r   Útest_startServiceWhileStoppingz1ClientServiceTests.test_startServiceWhileStoppingy  sX  € ð ×*Ñ*¸5Ð*ÓA‰ˆˆGØ
�‰˜Ñ×#Ñ# DÔ)Ø×'Ñ'¨Ñ*ˆØ×%Ñ%Ó'ˆØ×Ñ˜GÔ$Ø×,Ñ,Ó.ˆØ×Ñ˜LÔ)Ø×ÑÔØ×Ñ˜LÔ)Ø×Ñ˜GÔ$Ø×Ñ˜Ÿ™×6Ñ6¸Ô=Ø×ÑœW¤Y£[Ó1Ô2Ø×Ñ˜WÔ%Ø
�‰˜Ñ×#Ñ# DÔ)Ø×Ñœ˜R×4Ñ4Ó5°qÔ9Ø×ÑØ× Ñ  Ó.°×0GÑ0GÈÑ0Jô	
ð  ×+Ñ+Ó-ˆØ×Ñ˜MÕ*r   c                 óŒ  — | j                  d¬«      \  }}|j                  «       }| j                  |«       | j                  |j	                  «       t
        «       |j                  «        |j                  d   j                  d«       | j                  |j                  d   | j                  |j	                  «       «      «       y)a  
        When L{ClientService} is stopped - that is,
        L{ClientService.stopService} has been called and the L{Deferred} it
        returns has fired - calling L{startService} will cause a new connection
        to be made, and new calls to L{whenConnected} to succeed.
        FrÏ   éÿÿÿÿN)rå   rk   r“   ÚfailureResultOfrö   r
   rU   r­   r6   rO   rÖ   )r&   rã   rÝ   rø   s       r   Útest_startServiceWhileStoppedz0ClientServiceTests.test_startServiceWhileStopped•  s¥   € ð ×*Ñ*¸5Ð*ÓA‰ˆˆGØ×%Ñ%Ó'ˆØ×Ñ˜WÔ%Ø×Ñ˜W×2Ñ2Ó4´nÔEØ×ÑÔØ
�‰˜Ñ×$Ñ$ TÔ*Ø×ÑØ×#Ñ# BÑ'¨×)=Ñ)=¸g×>SÑ>SÓ>UÓ)Võ	
r   c                 ó(  — t        t        t        «       G d„ dt        «      «       }| j	                  |¬«      \  }}|j
                  d   }| j                  t        j                  |«      «       | j                  t        j                  |«      «       y)aC  
        If the protocol objects returned by the factory given to
        L{ClientService} provide special "marker" interfaces for their
        transport - L{IHalfCloseableProtocol} or L{IFileDescriptorReceiver} -
        those interfaces will be provided by the protocol objects passed on to
        the reactor.
        c                   ó   — e Zd ZdZy)úEClientServiceTests.test_interfacesForTransport.<locals>.FancyProtocolz9
            Provider of various interfaces.
            N)r;   r<   r=   r>   r   r   r   ÚFancyProtocolr  ¯  s   „ òr   r  )rØ   r   N)r   r   r   r   rå   r®   r�   Ú
providedBy)r&   r  rã   rÝ   ÚreactorFacings        r   Útest_interfacesForTransportz.ClientServiceTests.test_interfacesForTransport¦  s€   € ô 
Ô+Ô-DÓ	Eô	œHó 	ó 
Fð	ð
 ×*Ñ*¸Ð*ÓF‰ˆˆGØ×/Ñ/°Ñ2ˆØ�‰Ô/×:Ñ:¸=ÓIÔJØ�‰Ô.×9Ñ9¸-ÓHÕIr   c                 ó’  — t        «       }| j                  d|¬«      \  }}|j                  d   j                  t	        «       «       |j                  t        «       | j                  t        |j                  «      d«       |j                  «       }|j                  d   j                  t	        «       «       | j                  |«       y)zU
        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_ATTEMPTr\   r]   rk   r“   ©r&   r~   rã   rÝ   r-   s        r   Útest_stopServiceWhileRetryingz0ClientServiceTests.test_stopServiceWhileRetryingº  s˜   € ô “ˆØ×*Ñ*¸5ÈÐ*ÓN‰ˆˆGØ
�‰˜Ñ×"Ñ"¤9£;Ô/Ø�‰Ô*Ô+Ø×Ñœ˜RŸ_™_Ó-¨qÔ1Ø×ÑÓ!ˆØ
�‰˜Ñ×"Ñ"¤9£;Ô/Ø×Ñ˜QÕr   c                 ó  — t        «       }| j                  d|¬«      \  }}| j                  t        |j                  «      d«       | j                  |j                  d   «       |j                  «       }| j                  |«       y)zv
        When the service is stopped while initially connecting, the connection
        attempt is cancelled.
        Fr  r)   r   N)r   rå   r\   r]   r­   r‘   rk   r“   r  s        r   Útest_stopServiceWhileConnectingz2ClientServiceTests.test_stopServiceWhileConnectingÇ  sp   € ô
 “ˆØ×*Ñ*¸5ÈÐ*ÓN‰ˆˆGØ×Ñœ˜RŸ_™_Ó-¨qÔ1Ø×Ñ˜BŸO™O¨AÑ.Ô/Ø×ÑÓ!ˆØ×Ñ˜QÕr   c                 óþ   — t        «       }| j                  |¬«      \  }}|j                  «       }| j                  |j	                  «       g «       | j                  | j                  |«      |j                  d   «       y)zy
        When a client connects, the service keeps a reference to the new
        protocol and resets the delay.
        rÎ   r   N)r   rå   rö   r\   rÜ   rO   r“   rÖ   )r&   r~   rã   rÝ   ÚawaitingProtocols        r   Útest_clientConnectedz'ClientServiceTests.test_clientConnectedÓ  st   € ô
 “ˆØ×*Ñ*°Ð*Ó7‰ˆˆGØ"×0Ñ0Ó2ÐØ×Ñ˜×.Ñ.Ó0°"Ô5Ø×ÑØ× Ñ Ð!1Ó2°B×4KÑ4KÈAÑ4Nõ	
r   c                 óÆ  — t        «       }| j                  d|¬«      \  }}| j                  t        |j                  «      d«       |j                  d   j                  t        t        «       «      «       |j                  «       }| j                  |«       |j                  d„ «       |j                  t        «       | j                  t        |j                  «      d«       y)zŒ
        When a client connection fails, the service removes its reference
        to the protocol and tries again after a timeout.
        Fr  r)   r   c                 ó,   — | j                  t        «      S r"   )Útrapr
   )r·   s    r   r.   z@ClientServiceTests.test_clientConnectionFailed.<locals>.<lambda>í  s   € °·±¼nÓ1M€ r   r{   N)r   rå   r\   r]   r­   r+   r   rð   rö   r‘   Ú
addErrbackr	  r
  )r&   r~   rã   rÝ   rö   s        r   Útest_clientConnectionFailedz.ClientServiceTests.test_clientConnectionFailedà  s®   € ô
 “ˆØ×*Ñ*¸5ÈÐ*ÓN‰ˆˆGØ×Ñœ˜RŸ_™_Ó-¨qÔ1Ø
�‰˜Ñ×"Ñ"¤7¬9«;Ó#7Ô8Ø×-Ñ-Ó/ˆØ×Ñ˜MÔ*ð 	× Ñ Ñ!MÔNØ�‰Ô*Ô+Ø×Ñœ˜RŸ_™_Ó-¨qÕ1r   c                 ó  — t        «       }| j                  |d¬«      \  }}| j                  t        |j                  «      d«       |j                  d   j                  d«       | j                  t        |j                  «      d«       | j                  | j                  |j                  «       «      |j                  d   «       |j                  d   j                  t        t        «       «      «       |j                  t        «       | j                  t        |j                  «      d«       |j                  d   j                  d«       | j                  | j                  |j                  «       «      |j                  d   «       y)z~
        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å   r\   r]   r­   r6   rO   r“   rö   rÖ   r®   rï   r   rð   r	  r
  )r&   r~   rã   rÝ   s       r   Útest_clientConnectionLostz,ClientServiceTests.test_clientConnectionLostñ  s2  € ô
 “ˆØ×*Ñ*°ÈÐ*ÓN‰ˆˆGØ×Ñœ˜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   c                 ó>  — t        «       }| j                  |¬«      \  }}|j                  «       }|j                  d   j	                  t        t        «       «      «       | j                  |j                  «       t        «       | j                  |j                  «       y)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å   rk   r®   rï   r   ÚIndentationErrorrþ   rö   r
   r�   Úcalledr  s        r   Ú&test_clientConnectionLostWhileStoppingz9ClientServiceTests.test_clientConnectionLostWhileStopping  s{   € ô “ˆØ×*Ñ*°Ð*Ó7‰ˆˆGØ×ÑÓ!ˆØ
×Ñ Ñ"×1Ñ1´'Ô:JÓ:LÓ2MÔNØ×Ñ˜W×2Ñ2Ó4´nÔEØ�‰˜Ÿ™Õ!r   c                 ó˜  — | j                  dd¬«      \  }}| j                  t        |j                  «      d«       |j	                  «        | j                  t        |j                  «      d«       t        | «      }|j	                  «        | j                  t        |j                  «      d«       | j                  d |«       d   «       y)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¼   rU   r   r)   z$Duplicate ClientService.startServiceN)rå   r\   r]   r­   rU   rÊ   ÚassertIn)r&   rã   rÝ   Úmessagess       r   Útest_startTwicez"ClientServiceTests.test_startTwice  sž   € ð ×*Ñ*¸5ÈuÐ*ÓU‰ˆˆGØ×Ñœ˜RŸ_™_Ó-¨qÔ1Ø×ÑÔØ×Ñœ˜RŸ_™_Ó-¨qÔ1Ü˜T“?ˆØ×ÑÔØ×Ñœ˜RŸ_™_Ó-¨qÔ1Ø�‰Ð<¹h»jÈ¹mÕLr   c                 ó6  — t        «       }| j                  d|¬«      \  }}|j                  «       }|j                  «       }|j                  d¬«      }| j                  |«       | j                  |«       | j                  |«       |j                  d   j                  d«       | j                  |«      }| j                  |«      }| j                  |«      }	| j                  ||«       | j                  ||	«       | j                  ||j                  d   «       y)z{
        L{ClientService.whenConnected} returns a L{Deferred} that fires when a
        connection is established.
        Fr  r)   ©ÚfailAfterFailuresr   N)	r   rå   rö   r‘   r­   r6   r“   rO   rÖ   )
r&   r~   rã   rÝ   ÚaÚbÚcÚresultAÚresultBÚresultCs
             r   Útest_whenConnectedLaterz*ClientServiceTests.test_whenConnectedLater"  sÿ   € ô
 “ˆØ×*Ñ*¸5ÈÐ*ÓN‰ˆˆGØ×!Ñ!Ó#ˆØ×!Ñ!Ó#ˆØ×!Ñ!°AÐ!Ó6ˆØ×Ñ˜AÔØ×Ñ˜AÔØ×Ñ˜AÔØ
�‰˜Ñ×#Ñ# DÔ)Ø×&Ñ& qÓ)ˆØ×&Ñ& qÓ)ˆØ×&Ñ& qÓ)ˆØ×Ñ˜W gÔ.Ø×Ñ˜W gÔ.Ø×Ñ˜W b×&=Ñ&=¸aÑ&@ÕAr   c                 ó@  — t        «       }| j                  d|¬«      \  }}|j                  «       }|j                  d¬«      }|j                  d¬«      }|j                  d¬«      }| j                  |«       | j                  |«       | j                  |«       | j                  |«       t	        t        «       «      }|j                  d   j                  |«       | j                  |«       | j                  | j                  |t
        «      |«       | j                  |«       | j                  |«       |j                  t        «       | j                  t        |j                  «      d«       | j                  |«       | j                  |«       | j                  |«       t	        t        «       «      }	|j                  d   j                  |	«       | j                  |«       | j                  | j                  |t
        «      |	«       | j                  |«       t        }
|j                  |
«       | j                  t        |j                  «      d«       | j                  |«       | j                  |«       |j                  d   j                  d«       | j                  |«      }| j                  |«      }| j                  ||«       | j                  ||j                   d   «       |j                  d¬«      }| j                  |«      }| j                  ||«       y)	z‘
        L{ClientService.whenConnected} returns a L{Deferred} that fails, if
        asked, when some number of connections have failed.
        Fr  r)   r"  r{   é   r   N)r   rå   rö   r‘   r   rð   r­   r+   rO   rþ   r	  r
  r\   r]   r6   r“   rÖ   )r&   r~   rã   rÝ   Úa0Úa1Úa2Úa3Úf1Úf2ÚAT_LEAST_TWO_ATTEMPTSÚresultA0ÚresultA3Úa4ÚresultA4s                  r   Útest_whenConnectedFailsz*ClientServiceTests.test_whenConnectedFails7  s˜  € ô
 “ˆØ×*Ñ*¸5ÈÐ*ÓN‰ˆˆGØ×"Ñ"Ó$ˆØ×"Ñ"°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 xÔ0Ø×Ñ˜X r×'>Ñ'>¸qÑ'AÔBð ×"Ñ"°QÐ"Ó7ˆØ×'Ñ'¨Ó+ˆØ×Ñ˜X xÕ0r   c                 óÜ  — t        «       }| j                  d|¬«      \  }}|j                  «       }|j                  «       }|j                  d¬«      }| j                  |«       | j                  |«       | j                  |«       |j	                  «        |j                  t        «       | j                  |t        «       | j                  |t        «       | j                  |t        «       y)z†
        L{ClientService.whenConnected} returns a L{Deferred} that fails when
        L{ClientService.stopService} is called.
        Fr  r)   r"  N)	r   rå   rö   r‘   rk   r	  r
  rþ   r
   )r&   r~   rã   rÝ   r$  r%  r&  s          r   Útest_whenConnectedStopServicez0ClientServiceTests.test_whenConnectedStopServiceq  sÆ   € ô
 “ˆØ×*Ñ*¸5ÈÐ*ÓN‰ˆˆGØ×!Ñ!Ó#ˆØ×!Ñ!Ó#ˆØ×!Ñ!°AÐ!Ó6ˆØ×Ñ˜AÔØ×Ñ˜AÔØ×Ñ˜AÔØ×ÑÔØ�‰Ô*Ô+Ø×Ñ˜Q¤Ô/Ø×Ñ˜Q¤Ô/Ø×Ñ˜Q¤Õ/r   c                 ó  — t        «       }| j                  d|¬«      \  }}|j                  d   j                  t	        d«      «       |j                  «       }| j                  |j                  «       g «       | j                  |«       y)zÎ
        When L{ClientService.stopService} is called while waiting between
        connection attempts, the pending reconnection attempt is cancelled and
        the service is stopped immediately.
        Fr  r   úno connectionN)	r   rå   r­   r+   rð   rk   r\   rÜ   r“   r  s        r   Útest_retryCancelledz&ClientServiceTests.test_retryCancelled„  su   € ô “ˆØ×*Ñ*¸5ÈÐ*ÓN‰ˆˆGØ
�‰˜Ñ×"Ñ"¤9¨_Ó#=Ô>Ø×ÑÓ!ˆØ×Ñ˜×.Ñ.Ó0°"Ô5Ø×Ñ˜QÕr   c                 ó¦   — t        «       }| j                  dd|¬«      \  }}|j                  «       }| j                  | j	                  |«      «       y)z¦
        Calling L{ClientService.stopService} before
        L{ClientService.startService} returns a L{Deferred} that has
        already fired with L{None}.
        F)r¼   rU   r~   N©r   rå   rk   ÚassertIsNoner“   )r&   r~   Ú_rÝ   r-   s        r   Ú"test_stopServiceBeforeStartServicez5ClientServiceTests.test_stopServiceBeforeStartService‘  sT   € ô “ˆØ×)Ñ)Ø!°¸Uð *ó 
‰
ˆˆ7ð ×ÑÓ!ˆØ×Ñ˜$×.Ñ.¨qÓ1Õ2r   c                 ó®  — t        «       }| j                  d|¬«      \  }}|j                  «       }|j                  d   j	                  t        d«      «       |j                  «        |j                  «       }| j                  | j                  |«      j                  t        «       | j                  | j                  |«      j                  t        «       y)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ð   rk   r‚   rþ   r¥   r
   )r&   r~   rã   rÝ   ÚbeforeErrbackAndStopÚafterErrbackAndStops         r   Ú'test_whenConnectedErrbacksOnStopServicez:ClientServiceTests.test_whenConnectedErrbacksOnStopServicež  s¸   € ô “ˆØ×*Ñ*¸5ÈÐ*ÓN‰ˆˆGØ&×4Ñ4Ó6Ðð 	�‰˜Ñ×"Ñ"¤9¨_Ó#=Ô>à×ÑÔØ%×3Ñ3Ó5Ðà×ÑØ× Ñ Ð!5Ó6×<Ñ<¼nô	
ð 	×ÑØ× Ñ Ð!4Ó5×;Ñ;¼^õ	
r   c                 ób  — t        «       }| j                  d|¬«      \  }}|j                  d   j                  d«       |j	                  «       }|j	                  «       }|j
                  d   j                  t        t        «       «      «       | j                  |«       | j                  |«       y)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­   r6   rk   r®   rï   r   r  r“   ©r&   r~   rã   rÝ   ÚfirstStopDeferredÚsecondStopDeferreds         r   Ú"test_stopServiceWhileDisconnectingz5ClientServiceTests.test_stopServiceWhileDisconnecting·  s�   € ô “ˆØ×*Ñ*¸5ÈÐ*ÓN‰ˆˆGà
�‰˜Ñ×#Ñ# DÔ)ð $×/Ñ/Ó1Ðà$×0Ñ0Ó2Ðð 	×Ñ Ñ"×1Ñ1´'Ô:JÓ:LÓ2MÔNà×ÑÐ.Ô/Ø×ÑÐ/Õ0r   c                 ó‚  — t        «       }| j                  d|¬«      \  }}|j                  d   j                  d«       |j	                  «       }|j                  «        |j	                  «       }|j                  d   j                  t        t        «       «      «       | j                  |«       | j                  |«       y)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­   r6   rk   rU   r®   rï   r   r  r“   rH  s         r   Útest_stopServiceWhileRestartingz2ClientServiceTests.test_stopServiceWhileRestartingÎ  s©   € ô “ˆØ×*Ñ*¸5ÈÐ*ÓN‰ˆˆGà
�‰˜Ñ×#Ñ# DÔ)ð $×/Ñ/Ó1Ðà×ÑÔà$×0Ñ0Ó2Ðð 	×Ñ Ñ"×1Ñ1´'Ô:JÓ:LÓ2MÔNà×ÑÐ.Ô/Ø×ÑÐ/Õ0r   c                 ó  — t        «       }| j                  d|¬«      \  }}|j                  «       }|j                  «       }| j                  | j	                  |«      «       | j                  | j	                  |«      «       y)zŽ
        Calling L{ClientService.stopService} on a stopped service
        returns a L{Deferred} that has already fired with L{None}.
        Fr  Nr?  )r&   r~   rA  rÝ   rI  rJ  s         r   Ú test_stopServiceOnStoppedServicez3ClientServiceTests.test_stopServiceOnStoppedServiceæ  sv   € ô
 “ˆØ×)Ñ)¸%ÀuÐ)ÓM‰
ˆˆ7Ø#×/Ñ/Ó1ÐØ$×0Ñ0Ó2Ðà×Ñ˜$×.Ñ.Ð/@ÓAÔBØ×Ñ˜$×.Ñ.Ð/AÓBÕCr   c                 ól   ‡— dgŠˆfd„}| j                  |d¬«      \  }}| j                  d‰d   «       y)z†
        The C{prepareConnection} callable is called after
        L{ClientService.startService} once the connection is made.
        r   c                 ó    •— ‰dxx   dz  cc<   y ©Nr   r)   r   ©Ú_protoÚpreparess    €r   ÚprepareConnectionz[ClientServiceTests.test_prepareConnectionCalledWhenServiceStarts.<locals>.prepareConnectionú  ó   ø€ Ø�Q‹K˜1ÑŒKr   T)rV  rU   r)   N)rå   r\   )r&   rV  rã   rÝ   rU  s       @r   Ú-test_prepareConnectionCalledWhenServiceStartsz@ClientServiceTests.test_prepareConnectionCalledWhenServiceStartsó  sG   ø€ ð
 �3ˆô	ð ×*Ñ*Ø/¸dð +ó 
‰ˆˆGð 	×Ñ˜˜H Q™KÕ(r   c                 ó‚   ‡— g Šˆfd„}| j                  |¬«      \  }}| j                  |j                  d   ‰d   «       y)zf
        The C{prepareConnection} callable is passed the connected protocol
        instance.
        c                 ó(   •— ‰j                  | «       y r"   ©rm   )ÚprotoÚnewProtocolss    €r   rV  zVClientServiceTests.test_prepareConnectionCalledWithProtocol.<locals>.prepareConnection	  s   ø€ Ø×Ñ Õ&r   ©rV  r   N)rå   rO   r®   )r&   rV  rã   rÝ   r]  s       @r   Ú(test_prepareConnectionCalledWithProtocolz;ClientServiceTests.test_prepareConnectionCalledWithProtocol  sN   ø€ ð
 ˆô	'ð ×*Ñ*Ø/ð +ó 
‰ˆˆGð 	×Ñ˜R×4Ñ4°QÑ7¸Àa¹ÕIr   c                 ó`  ‡— dgŠˆfd„}t        «       }| j                  |d|¬«      \  }}|j                  d   j                  t	        d«      «       | j                  d‰d   «       |j                  t        «       |j                  d   j                  d«       | j                  d‰d   «       y)zf
        The C{prepareConnection} callback is invoked only once a connection is
        made.
        r   c                 ó    •— ‰dxx   dz  cc<   y rR  r   rS  s    €r   rV  z]ClientServiceTests.test_prepareConnectionCalledAfterConnectionMade.<locals>.prepareConnection  rW  r   F)rV  r¼   r~   zconnection attempt failedr)   N)	r   rå   r­   r+   rð   r\   r	  r
  r6   ©r&   rV  r~   rã   rÝ   rU  s        @r   Ú/test_prepareConnectionCalledAfterConnectionMadezBClientServiceTests.test_prepareConnectionCalledAfterConnectionMade  s¥   ø€ ð
 �3ˆô	ô “ˆØ×*Ñ*Ø/ÀÈeð +ó 
‰ˆˆGð 	�‰˜Ñ×"Ñ"¤9Ð-HÓ#IÔJØ×Ñ˜˜H Q™KÔ(à�‰Ô*Ô+Ø
�‰˜Ñ×#Ñ# DÔ)à×Ñ˜˜H Q™KÕ(r   c                 ó2  ‡— dgŠˆfd„}t        «       }| j                  ||¬«      \  }}| j                  d‰d   «       |j                  d   j	                  t        t        «       «      «       |j                  t        «       | j                  d‰d   «       y)z�
        The C{prepareConnection} callback is invoked each time a connection is
        made, including on reconnection.
        r   c                 ó    •— ‰dxx   dz  cc<   y rR  r   rS  s    €r   rV  zUClientServiceTests.test_prepareConnectionCalledOnReconnect.<locals>.prepareConnection/  rW  r   ©rV  r~   r)   r{   N)	r   rå   r\   r®   rï   r   r  r	  r
  rb  s        @r   Ú'test_prepareConnectionCalledOnReconnectz:ClientServiceTests.test_prepareConnectionCalledOnReconnect(  s“   ø€ ð
 �3ˆô	ô “ˆØ×*Ñ*Ø/°uð +ó 
‰ˆˆGð 	×Ñ˜˜H Q™KÔ(ð 	×Ñ Ñ"×1Ñ1´'Ô:JÓ:LÓ2MÔNØ�‰Ô*Ô+à×Ñ˜˜H Q™KÕ(r   c                 ó°   ‡— t        «       Šˆfd„}| j                  |¬«      \  }}| j                  |j                  «       «      }| j	                  ‰|«       y)a?  
        The C{prepareConnection} return value is ignored when it does not
        indicate a failure. Even though the callback participates in the
        internal new-connection L{Deferred} chain for error propagation
        purposes, any successful result does not affect the ultimate return
        value.
        c                 ó   •— ‰S r"   r   )r\  Úsentinels    €r   rV  zVClientServiceTests.test_prepareConnectionReturnValueIgnored.<locals>.prepareConnectionJ  s   ø€ ØˆOr   r^  N)r’   rå   r“   rö   ÚassertNotIdentical)r&   rV  rã   rÝ   r2   rj  s        @r   Ú(test_prepareConnectionReturnValueIgnoredz;ClientServiceTests.test_prepareConnectionReturnValueIgnored?  sT   ø€ ô “8ˆô	ð ×*Ñ*Ð=NÐ*ÓO‰ˆˆGà×%Ñ% g×&;Ñ&;Ó&=Ó>ˆØ×Ñ ¨&Õ1r   c                 ó  ‡‡— g Št        «       Šˆˆfd„}| j                  |¬«      \  }}|j                  «       }| j                  |«       ‰j	                  d«       | j                  |j                  d   | j                  |«      «       y)z�
        The C{prepareConnection} callable returns a deferred and calls to
        L{ClientService.whenConnected} wait until it fires.
        c                 ó*   •— ‰j                  | «       ‰S r"   r[  )r\  ÚnewProtocolDeferredr]  s    €€r   rV  zVClientServiceTests.test_prepareConnectionReturningADeferred.<locals>.prepareConnectionZ  s   ø€ Ø×Ñ Ô&Ø&Ð&r   r^  Nr   )r   rå   rö   r‘   r6   rO   rÖ   r“   )r&   rV  rã   rÝ   ÚwhenConnectedDeferredro  r]  s        @@r   Ú(test_prepareConnectionReturningADeferredz;ClientServiceTests.test_prepareConnectionReturningADeferredR  s‡   ù€ ð
 ˆÜ&›jÐõ	'ð ×*Ñ*Ð=NÐ*ÓO‰ˆˆGà '× 5Ñ 5Ó 7ÐØ×ÑÐ1Ô2à×$Ñ$ TÔ*à×ÑØ×#Ñ# AÑ&¨×(<Ñ(<Ð=RÓ(Sõ	
r   c                 ód  — t        «       }d„ }| j                  ||¬«      \  }}|j                  d¬«      }| j                  |«       |j	                  t
        «       | j                  |«       |j	                  t
        «       | j                  t        | j                  |«      j                  «       y)zs
        The connection attempt counts as a failure when the
        C{prepareConnection} callable throws.
        c                 ó   — t        «       ‚r"   )r  )rT  s    r   rV  zJClientServiceTests.test_prepareConnectionThrows.<locals>.prepareConnectionp  s   € Ü"Ó$Ð$r   rf  r{   r"  N)
r   rå   rö   r‘   r	  r
  rO   r  rþ   Útype)r&   r~   rV  rã   rÝ   rp  s         r   Útest_prepareConnectionThrowsz/ClientServiceTests.test_prepareConnectionThrowsi  s£   € ô
 “ˆò	%ð ×*Ñ*Ø/°uð +ó 
‰ˆˆGð !(× 5Ñ 5ÈÐ 5Ó JÐØ×ÑÐ1Ô2à�‰Ô*Ô+Ø×ÑÐ1Ô2à�‰Ô*Ô+Ø×ÑÜ˜d×2Ñ2Ð3HÓI×NÑNõ	
r   N)$r;   r<   r=   r>   r   rå   r…   rì   rò   rô   rû   rÿ   r  r  r  r  r  r  r  r   r*  r8  r:  r=  rB  rF  rK  rM  rO  rX  r_  rc  rg  rl  rq  ru  r   r   r   rÌ   rÌ   þ  s³   „ ñð
 #°ÀHóAòF2ò6ò ò:ò +ò8
ò"Jò( ò
 ò
ò2ò"
ò*"òMòBò*81òt0ò& ò3ò
ò21ò.1ò0Dò)òJò)ò.)ò.2ò&
ó.
r   rÌ   )F)0r>   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#   rH   ry   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ó :ð$ Ð ôB

Ð,õ B

r   