
    hs                    F   S r SSKrSSK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  SSKrSSKJrJrJrJrJr  SSK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`S jr$ " S S\RJ                  5      r& " S S\RN                  5      r( " S S5      r) " S S\)\RN                  5      r* " S S\)\RV                  5      r, " S S\#5      r- " S S\#5      r. " S S\R^                  5      r0 " S  S!\,5      r1 " S" S#\#5      r2 " S$ S%\#5      r3 " S& S'\#5      r4 " S( S)\RJ                  5      r5 " S* S+\RJ                  5      r6 " S, S-\RV                  5      r7 " S. S/\#5      r8 " S0 S1\RJ                  5      r9 " S2 S3\RJ                  5      r: " S4 S5\RJ                  5      r; " S6 S7\;5      r< " S8 S95      r= " S: S;\#\=5      r> " S< S=\R~                  5      r@ " S> S?\R                  5      rB " S@ SA\#5      rC " SB SC\RJ                  5      rD " SD SE\RJ                  5      rE " SF SG\RV                  5      rF " SH SI\R~                  5      rG " SJ SK\R                  5      rH " SL SM\#5      rI\" \5       " SN SO\5      5       rJ " SP SQ\RN                  5      rK " SR SS\#5      rL " ST SU\#5      rM " SV SW\#5      rN " SX SY\#5      rO " SZ S[\#5      rP " S\ S]\#5      rQ SSKRrR\RR                  " \RR                  5      S   S^-   rU\V" \>S_\U5        g! \W a     gf = f)az.
Tests for implementations of L{IReactorTCP}.
    Nwraps)Optional)skipIf)implementer)defererror
interfacesprotocolreactor)IPv4Address)IHalfCloseableProtocolIPullProducer)AccumulatingProtocol)policies)errmsg)platform)SkipTestTestCasec                    ^ ^^ SSK Jn  [        R                  " 5       mUU 4S jnUR	                  U5      mU4S jnTR                  U5        TR                  U5      nUR                  TR                  5        T$ )z
Poor excuse for an event notification helper.  This polls a condition and
calls back a Deferred when it is seen to be true.

Do not use this function.
r   )taskc                  F   > T" 5       n U (       a  TR                  U 5        g g Ncallback)resd	predicates    \/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/test/test_tcp.pycheckloopUntil.<locals>.check)   s    kJJsO     c                 (   > TR                  5         U $ r   )stop)resultcalls    r    r%   loopUntil.<locals>.stop0   s    		r#   )	twisted.internetr   r   DeferredLoopingCalladdCallbackstart
addErrbackerrback)r   intervalr   r!   r%   d2r'   r   s   `     @@r    	loopUntilr2      sa     &A
 E"D MM$	H	BMM!))Hr#   c                        \ rS rSrS rS rSrg)ClosingProtocol:   c                 N    [        S5        U R                  R                  5         g )NzClosingProtocol.connectionMader   	transportloseConnectionselfs    r    connectionMadeClosingProtocol.connectionMade;   s    ,-%%'r#   c                 X    [        S5        UR                  [        R                  5        g )NzClosingProtocol.connectionLost)r   trapr	   ConnectionDoner;   reasons     r    connectionLostClosingProtocol.connectionLost?   s    ,-E(()r#    N__name__
__module____qualname____firstlineno__r<   rC   __static_attributes__rE   r#   r    r4   r4   :   s    (*r#   r4   c                   R    \ rS rSr% SrSrSr\\R                     \
S'   S rS rSrg)ClosingFactoryD   z'
Factory that closes port immediately.
Nportc                 T    U R                   R                  5       U l        [        5       $ r   )rO   stopListening_cleanerUpperr4   )r;   conns     r    buildProtocolClosingFactory.buildProtocolL   s     !YY446  r#   c                     U R                   c-  U R                  (       d   eU R                  R                  5       $ U R                   $ )z<
Clean-up for tests to wait for the port to stop listening.
)rR   rO   rQ   r:   s    r    cleanUpClosingFactory.cleanUpP   s=     % 99999**,,!!!r#   )rR   )rG   rH   rI   rJ   __doc__rR   rO   r   r
   IListeningPort__annotations__rT   rW   rK   rE   r#   r    rM   rM   D   s-     M04D(:,,
-4!	"r#   rM   c                       \ rS rSr% Sr\rSr\\	R                  \      \S'   Sr\\	R                  \      \S'   Sr\\   \S'   SrS rS	 rS
rg)MyProtocolFactoryMixin\   a  
Mixin for factories which create L{AccumulatingProtocol} instances.

@type protocolFactory: no-argument callable
@ivar protocolFactory: Factory for protocols - takes the place of the
    typical C{protocol} attribute of factories (but that name is used by
    this class for something else).

@type protocolConnectionMade: L{None} or L{defer.Deferred}
@ivar protocolConnectionMade: When an instance of L{AccumulatingProtocol}
    is connected, if this is not L{None}, the L{Deferred} will be called
    back with the protocol instance and the attribute set to L{None}.

@type protocolConnectionLost: L{None} or L{defer.Deferred}
@ivar protocolConnectionLost: When an instance of L{AccumulatingProtocol}
    is created, this will be set as its C{closedDeferred} attribute and
    then this attribute will be set to L{None} so the L{defer.Deferred} is
    not used by more than one protocol.

@ivar protocol: The most recently created L{AccumulatingProtocol} instance
    which was returned from C{buildProtocol}.

@type called: C{int}
@ivar called: A counter which is incremented each time C{buildProtocol}
    is called.

@ivar peerAddresses: A C{list} of the addresses passed to C{buildProtocol}.
NprotocolConnectionMadeprotocolConnectionLostlastProtocolr   c                     / U l         g r   )peerAddressesr:   s    r    __init__MyProtocolFactoryMixin.__init__   s
    r#   c                     U R                   R                  U5        U =R                  S-  sl        U R                  5       nXl        U R
                  Ul        SU l        X l        U$ )zQ
Create a L{AccumulatingProtocol} and set it up to be able to perform
callbacks.
   N)rc   appendcalledprotocolFactoryfactoryr`   closedDeferredra   r;   addrps      r    rT   $MyProtocolFactoryMixin.buildProtocol   sZ    
 	!!$'q  "	66&*#r#   )ra   rc   r`   )rG   rH   rI   rJ   rY   r   rj   r_   r   r   r*   r[   r`   ra   ri   rd   rT   rK   rE   r#   r    r]   r]   \   sa    : +OMQHU^^4H%IJQMQHU^^4H%IJQ37L(/07F r#   r]   c                       \ rS rSrSrSrg)MyServerFactory   zA
Server factory which creates L{AccumulatingProtocol} instances.
rE   N)rG   rH   rI   rJ   rY   rK   rE   r#   r    rr   rr      s    r#   rr   c                       \ rS rSr% SrSrSr\R                  S   \	S'   \R                  S   \	S'   S r
S rS	 rS
 rSrg)MyClientFactory   zA
Client factory which creates L{AccumulatingProtocol} instances.
r   NdeferredfailDeferredc                     [         R                  U 5        [        R                  " 5       U l        [        R                  " 5       U l        g r   )r]   rd   r   r*   rw   rx   r:   s    r    rd   MyClientFactory.__init__   s-    ''-(!NN,r#   c                 T    SU l         X l        U R                  R                  S 5        g Nrg   )failedrB   rx   r   r;   	connectorrB   s      r    clientConnectionFailed&MyClientFactory.clientConnectionFailed   s"    ""4(r#   c                 F    X l         U R                  R                  S 5        g r   )
lostReasonrw   r   r~   s      r    clientConnectionLost$MyClientFactory.clientConnectionLost   s     t$r#   c                     SU l         g r|   stoppedr:   s    r    stopFactoryMyClientFactory.stopFactory   s	    r#   )rw   rx   r}   r   rB   r   )rG   rH   rI   rJ   rY   r}   r   r   r*   r[   rd   r   r   r   rK   rE   r#   r    ru   ru      sE     FGnnT""..&&-
)
%r#   ru   c                   P    \ 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)ListeningTests   c                     [        5       n[        R                  " SUSS9nU R                  UR                  5        U R                  [        R                  R                  U5      5        g)zN
L{IReactorTCP.listenTCP} returns an object which provides
L{IListeningPort}.
r   	127.0.0.1	interfaceN)	rr   r   	listenTCP
addCleanuprQ   
assertTruer
   rZ   
providedByr;   fp1s      r    test_listenListeningTests.test_listen   sO    
 q!{;(()
11<<R@Ar#   c                    ^^ [        5       m[        R                  " STSS9nUR                  5       R                  mUU4S jn[
        R                  " UR                  5      nUR                  U5        U$ )z
The L{IListeningPort} returned by L{IReactorTCP.listenTCP} can be
stopped with its C{stopListening} method.  After the L{Deferred} it
(optionally) returns has been called back, the port number can be bound
to a new server.
r   r   r   c                 P   > [         R                  " TTSS9nUR                  5       $ )Nr   r   )r   r   rQ   )ignoredrO   r   ns     r    cbStopListening9ListeningTests.testStopListening.<locals>.cbStopListening   s&    $$Q[AD%%''r#   )	rr   r   r   getHostrO   r   maybeDeferredrQ   r,   )r;   rO   r   r   r   r   s       @@r    testStopListening ListeningTests.testStopListening   s`       A=LLN	(
  2 23	o&r#   c                 b    [        5       n[        R                  " SUSS9nUR                  5       $ Nr   r   r   )rr   r   r   rQ   r   s      r    testNumberedInterface$ListeningTests.testNumberedInterface   s-    q!{;!!r#   c                 Z  ^ ^^ [        5       n[        R                  " SU5      m[        TR	                  5       R
                  5      mT R                  [        T5      R                  T5      S:H  5        UUU 4S jn[        R                  " TR                  5      nUR                  U5      $ )Nr   c                 ^   > TR                  [        T5      R                  T5      S:g  5        g )Nr   )assertFalsereprfind)ignro   portNor;   s    r    stoppedListening5ListeningTests.testPortRepr.<locals>.stoppedListening   s#    T!W\\&1R78r#   )rr   r   r   strr   rO   r   r   r   r   r   rQ   r,   )r;   r   r   r   ro   r   s   `   @@r    testPortReprListeningTests.testPortRepr   s~    a#QYY[%%&af-34	9 0}}-..r#   c                   ^ ^ [        5       n[        R                  " 5       =o!l        [        R
                  " SU5      mT R                  TR                  5        [        5       n[        R                  " 5       =oCl        [        R                  " STR                  5       R                  U5      nT R                  UR                  5        UU 4S jn[        R                  " X$/5      R                  U5      $ )zi
Check that the repr string of the server transport get the good port
number if the server listens on 0.
r   r   c                    > U u  pTR                  5       R                  nTR                  [        UR                  5      SU S35        UR                  R                  5         UR                  R                  5         g )Nz<AccumulatingProtocol #0 on >)r   rO   assertEqualr   r8   r9   )r&   serverProtoclientProto
portNumberrO   r;   s       r    r!   -ListeningTests.test_serverRepr.<locals>.check   sj    '-$K,,J[**+.zl!< !!002!!002r#   )rr   r   r*   r_   r   r   r   rQ   ru   
connectTCPr   rO   
disconnectgatherResultsr,   )r;   serverserverConnMadeclientclientConnMader   r!   rO   s   `      @r    test_serverReprListeningTests.test_serverRepr   s    
 !"9>9II6  F+**+ "9>9II6&&{DLLN4G4GP		,,-	3 ""N#CDPPQVWWr#   c                    ^ ^^^ [        5       m[        R                  " STSS9mT R                  TR                  5        UUU U4S jnS m[
        R                  " TR                  5      nUR                  U5        U$ )zv
Stop and then try to restart a L{tcp.Port}: after a restart, the
server should be able to handle client connections.
r   r   r   c                   > TR                  5         [        5       n[        R                  " 5       Tl        [        R                  " 5       Ul        [
        R                  " STR                  5       R                  U5      nTR                  UR                  5        [        R                  " TR                  UR                  /5      R                  T5      $ Nr   )startListeningru   r   r*   r_   r   r   r   rO   r   r   r   r,   r   r   r   closerO   r;   serverFactorys      r    r   =ListeningTests.test_restartListening.<locals>.cbStopListening
  s    !$&F38>>3CM0,1NN,<F)**;8K8KVTIOOI001&&55v7T7TUk% !r#   c                 t    U u  pUR                   R                  5         UR                   R                  5         g r   r8   r9   r&   r   r   s      r    r   3ListeningTests.test_restartListening.<locals>.close  .    '-$K!!002!!002r#   )rr   r   r   r   rQ   r   r   r,   )r;   r   r   r   rO   r   s   `  @@@r    test_restartListening$ListeningTests.test_restartListening  sk    
 ()  M[I**+
	! 
	!	3
  2 23	o&r#   c                     [        5       nS nX!l        [        R                  " SUSS9nU R	                  UR                  5       [        5      $ )z
If the server factory raises an exception in C{stopFactory}, the
deferred returned by L{tcp.Port.stopListening} should fail with the
corresponding error.
c                      [        S5      eNzAn errorRuntimeErrorrE   r#   r    raiseException8ListeningTests.test_exceptInStop.<locals>.raiseException'      z**r#   r   r   r   )rr   r   r   r   assertFailurerQ   r   r;   r   r   rO   s       r    test_exceptInStop ListeningTests.test_exceptInStop  sI     ()	+ %3!  M[I!!$"4"4"6EEr#   c                   ^ ^^^ [        5       mS nUTl        [        R                  " STSS9mT R	                  TR
                  5        UUU U4S jnS mT R                  TR                  5       [        5      R                  U5      $ )z
Even if the server factory raise an exception in C{stopFactory}, the
corresponding C{tcp.Port} instance should be in a sane state and can
be restarted.
c                      [        S5      er   r   rE   r#   r    r   >ListeningTests.test_restartAfterExcept.<locals>.raiseException7  r   r#   r   r   r   c                   > T? TR                  5         [        5       n[        R                  " 5       Tl        [        R                  " 5       Ul        [        R                  " STR                  5       R                  U5      nTR                  UR                  5        [        R                  " TR
                  UR
                  /5      R                  T5      $ r   )r   r   ru   r   r*   r_   r   r   r   rO   r   r   r   r,   r   s      r    r   ?ListeningTests.test_restartAfterExcept.<locals>.cbStopListening>  s    )!$&F38>>3CM0,1NN,<F)**;8K8KVTIOOI001&&55v7T7TUk% !r#   c                 t    U u  pUR                   R                  5         UR                   R                  5         g r   r   r   s      r    r   5ListeningTests.test_restartAfterExcept.<locals>.closeK  r   r#   )	rr   r   r   r   r   rQ   r   r   r,   )r;   r   r   r   rO   r   s   `  @@@r    test_restartAfterExcept&ListeningTests.test_restartAfterExcept/  s     ()	+ %3!  M[I**+	! 	!	3
 !!$"4"4"6EQQ
 	
r#   c                   ^ [        5       n[        R                  " SUSS9nUR                  5       R                  nUR                  S5        [        5       m[        R                  " 5       Ul	        [        R                  " 5       Tl	        [        R                  " SUT5        U4S jnTR                  R                  U5      $ )z
If C{connectionLost} is called directly on a port object, it succeeds
(and doesn't expect the presence of a C{deferred} attribute).

C{connectionLost} is called by L{reactor.disconnectAll} at shutdown.
r   r   r   Nc                 X   > TR                   R                  [        R                  5        g r   rB   r?   r	   ConnectionRefusedError)r   r   s    r    r!   ;ListeningTests.test_directConnectionLostCall.<locals>.checke  s    MMu;;<r#   )rr   r   r   r   rO   rC   ru   r   r*   r_   r   rx   r,   )r;   r   rO   r   r!   r   s        @r    test_directConnectionLostCall,ListeningTests.test_directConnectionLostCallT  s     ()  M[I\\^((
D! "/4~~/?,(-(8%;
F;	= ""..u55r#   c                     [        5       nS nX!l        [        R                  " SUSS9nU R	                  [
        UR                  S5        g)z
If C{connectionLost} is called directory on a port object and that the
server factory raises an exception in C{stopFactory}, the exception is
passed through to the caller.

C{connectionLost} is called by L{reactor.disconnectAll} at shutdown.
c                      [        S5      er   r   rE   r#   r    r   FListeningTests.test_exceptInConnectionLostCall.<locals>.raiseExceptiont  r   r#   r   r   r   N)rr   r   r   r   assertRaisesr   rC   r   s       r    test_exceptInConnectionLostCall.ListeningTests.test_exceptInConnectionLostCallj  sE     ()	+ %3!  M[I,(;(;TBr#   rE   N)rG   rH   rI   rJ   r   r   r   r   r   r   r   r   r   r   rK   rE   r#   r    r   r      s9    B("
/X6<F #
J6,Cr#   r   c                   N    \ 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)LoopbackTestsi|  z
Test loopback connections.
c                 J  ^ ^^ [        5       n[        R                  " SUSS9mTUl        T R	                  UR
                  5        TR                  5       R                  n[        5       m[        R                  " SUT5        UUU 4S jnTR                  R                  U5      $ )zb
A port created with L{IReactorTCP.listenTCP} can be connected to with
L{IReactorTCP.connectTCP}.
r   r   r   c                    > TR                  TR                  R                  5        TR                  TR                  5        TR                  R                  [        R                  5        g r   )r   ra   madedisconnectedr   r?   r	   r@   )xclientFrO   r;   s    r    r!   <LoopbackTests.test_closePortInProtocolFactory.<locals>.check  sG    OOG00556OOD--.##E$8$89r#   )rM   r   r   rO   r   rW   r   ru   r   rw   r,   )r;   r   r   r!   r   rO   s   `   @@r    test_closePortInProtocolFactory-LoopbackTests.test_closePortInProtocolFactory  s    
   A=		"\\^((
!#;
G<	:
 ++E22r#   c                 H    [        USS 5      " [        R                  5        g )Nr?   c                     g r   rE   )r   s    r    <lambda>,LoopbackTests._trapCnxDone.<locals>.<lambda>  s    tr#   )getattrr	   r@   )r;   objs     r    _trapCnxDoneLoopbackTests._trapCnxDone  s    V^,U-A-ABr#   c                   ^ [        5       n[        R                  " 5       nX2l        [        R
                  " SUSS9nU R                  UR                  5        UR                  5       R                  n[        5       n[        R                  " 5       nXvl        [        R                  " SXV5        [        R                  " X7/5      nU4S jn	UR                  U	5        U$ )zq
Invoke the given callback with a client protocol and a server protocol
which have been connected to each other.
r   r   r   c                    > U u  pT" X5        UR                   R                  5         UR                   R                  5         g r   r   )r&   serverProtocolclientProtocolr   s      r    	connected>LoopbackTests._connectedClientAndServerTest.<locals>.connected  s7    -3*N^4$$335$$335r#   )rr   r   r*   r_   r   r   r   rQ   r   rO   ru   r   r   r,   )
r;   r   r   r   rO   r   r   r   	connsMader  s
    `        r    _connectedClientAndServerTest+LoopbackTests._connectedClientAndServerTest  s    
 ())/=,  M[I**+\\^((
!#))7&;
<''(HI		6 	i(r#   c                 2   ^  U 4S jnT R                  U5      $ )z
The transport of a protocol connected with L{IReactorTCP.connectTCP} or
L{IReactor.TCP.listenTCP} can have its I{TCP_NODELAY} state inspected
and manipulated with L{ITCPTransport.getTcpNoDelay} and
L{ITCPTransport.setTcpNoDelay}.
c                 6  > X4 H  nUR                   nTR                  UR                  5       S5        UR                  S5        TR                  UR                  5       S5        UR                  S5        TR                  UR                  5       S5        M     g Nr   rg   )r8   r   getTcpNoDelaysetTcpNoDelayr  r  ro   r8   r;   s       r    r!   ,LoopbackTests.test_tcpNoDelay.<locals>.check  s    $5KK	  !8!8!:A>''*  !8!8!:A>''*  !8!8!:A> 6r#   r  r;   r!   s   ` r    test_tcpNoDelayLoopbackTests.test_tcpNoDelay  s    	? 11%88r#   c                 2   ^  U 4S jnT R                  U5      $ )z
The transport of a protocol connected with L{IReactorTCP.connectTCP} or
L{IReactor.TCP.listenTCP} can have its I{SO_KEEPALIVE} state inspected
and manipulated with L{ITCPTransport.getTcpKeepAlive} and
L{ITCPTransport.setTcpKeepAlive}.
c                 6  > X4 H  nUR                   nTR                  UR                  5       S5        UR                  S5        TR                  UR                  5       S5        UR                  S5        TR                  UR                  5       S5        M     g r  )r8   r   getTcpKeepAlivesetTcpKeepAliver  s       r    r!   .LoopbackTests.test_tcpKeepAlive.<locals>.check  s    $5KK	  !:!:!<a@))!,  !:!:!<a@))!,  !:!:!<a@ 6r#   r  r  s   ` r    test_tcpKeepAliveLoopbackTests.test_tcpKeepAlive  s    	A 11%88r#   c                    ^ [        5       m[        R                  " SSTSS9  U4S jnTR                  R	                  U5      $ )Nr   E      )timeoutc                 X   > TR                   R                  [        R                  5        g r   r   )r   r   s    r    r!   (LoopbackTests.testFailing.<locals>.check  s    NN < <=r#   )ru   r   r   rx   r,   )r;   r!   r   s     @r    testFailingLoopbackTests.testFailing  s=    !#;GQ?	> ##//66r#   c                    ^ ^^^ / m[        S5       HK  n[        R                  " 5       nUR                  S5        UR                  S5        TR	                  U5        MM     [
        R                  " T5        [        R                  " [        [        R                  5      mUU UU4S jmT" 5       nT R                  U[        R                  5        U 4S jnUR                  U5        U4S jnUR                  U5        U$ )zv
Assert that the error number of the ConnectionRefusedError is
ECONNREFUSED, and not some other socket related error.

   r   r   rg   c                     > UUU4S jn TR                  5       nUR                  5       u  p#UR                  5         TR                  X#5      nUR	                  U 5        U$ )Nc                 x   > U R                   R                  5         T(       a  T" 5       $ TR                  S5        g)z<
Darn.  Kill it and try again, if there are any tries left.
z?Could not fail to connect - could not test errno for that case.N)r8   r9   fail)protor;   serverSocketstryConnectFailures    r    r  ]LoopbackTests.test_connectionRefusedErrorNumber.<locals>.tryConnectFailure.<locals>.connected  s0     ..0 ,..		Ur#   )popgetsocknamer   r   r,   )	r  serverSocket
serverHost
serverPortconnectDeferredclientCreatorr;   r5  r6  s	        r    r6  JLoopbackTests.test_connectionRefusedErrorNumber.<locals>.tryConnectFailure  sY    	 ),,.L%1%=%=%?"J +66zNO''	2""r#   c                 Z   > TR                  U R                  [        R                  5        g r   )r   osErrorerrnoECONNREFUSED)excr;   s    r    connRefusedDLoopbackTests.test_connectionRefusedErrorNumber.<locals>.connRefused  s    S[[%*<*<=r#   c                 d   > T(       a'  TR                  5       R                  5         T(       a  M'  U $ r   )r8  r   )passthroughr5  s    r    cleanup@LoopbackTests.test_connectionRefusedErrorNumber.<locals>.cleanup#  s(    !!#))+  -r#   )rangesocketbindlistenrh   randomshuffler   ClientCreatorr   Protocolr   r	   r   r,   addBoth)	r;   ir:  refusedDeferredrE  rI  r>  r5  r6  s	   `     @@@r    !test_connectionRefusedErrorNumber/LoopbackTests.test_connectionRefusedErrorNumber  s    2 rA!==?L./"  .	 
 	}% ..w8I8IJ	# 	#( ,-?E,H,HI	> 	##K0	
 	(r#   c                 v    U R                  [        R                  [        R                  SS[        5       5        g)z]
Connecting to a named service which does not exist raises
L{error.ServiceNameUnknownError}.
r   thisbetternotexistN)r   r	   ServiceNameUnknownErrorr   r   ru   r:   s    r    test_connectByServiceFail'LoopbackTests.test_connectByServiceFail+  s0    
 	)) 	
r#   c                    ^ ^^	 [        5       m	[        R                  " 5       nUT	l        [        R
                  " ST	SS9nT R                  UR                  5        UR                  5       R                  m[        5       n[        R                  " 5       nXCl        U4S jnT R                  [        SU5        [        R                  " SSU5        [        R                  " X/5      nU U	4S jnUR                  U5        U$ )z
L{IReactorTCP.connectTCP} accepts the name of a service instead of a
port number and connects to the port number associated with that
service, as defined by L{socket.getservbyname}.
r   r   r   c                 "   > U S:X  a  US:X  a  T$ g)Nhttptcpr/  rE   )serviceNameprotocolNamer   s     r    fakeGetServicePortByNameELoopbackTests.test_connectByService.<locals>.fakeGetServicePortByNameH  s    f$)>!!r#   getservbynamer_  c                    > U u  pTR                  TR                  S5        UR                  R                  5         UR                  R                  5         g )Nz7Server factory was not called upon to build a protocol.)r   ri   r8   r9   )r&   r  r  r;   r   s      r    r  6LoopbackTests.test_connectByService.<locals>.connectedS  sI    -3*NOO$$I $$335$$335r#   )rr   r   r*   r_   r   r   r   rQ   r   rO   ru   patchrL  r   r   r,   )
r;   r   rO   clientFactoryr   rc  connMader  r   r   s
   `       @@r    test_connectByService#LoopbackTests.test_connectByService8  s     ())/=,  M[I**+\\^((
'))/=,	
 	

6?,DE;>&&'GH	6 	Y'r#   rE   N)rG   rH   rI   rJ   rY   r  r	  r  r  r$  r,  rV  r[  rk  rK   rE   r#   r    r   r   |  s7    3(C89&9&7EN
%r#   r   c                   (    \ rS rSrSrSrS rS rSrg)StartStopFactoryi`  r   c                 b    U R                   (       d  U R                  (       a  [        eSU l         g r|   startedr   r   r:   s    r    startFactoryStartStopFactory.startFactoryd      <<4<<r#   c                 b    U R                   (       a  U R                  (       a  [        eSU l        g r|   rp  r:   s    r    r   StartStopFactory.stopFactoryi  s    ||t||r#   )rq  r   N)	rG   rH   rI   rJ   rq  r   rr  r   rK   rE   r#   r    rn  rn  `  s    GG
r#   rn  c                   .    \ rS rSrSrSrS rS rS rSr	g)ClientStartStopFactoryio  r   c                 l    [         R                  " U /UQ70 UD6  [        R                  " 5       U l        g r   )ru   rd   r   r*   whenStopped)r;   akws      r    rd   ClientStartStopFactory.__init__s  s)      00R0 >>+r#   c                 b    U R                   (       d  U R                  (       a  [        eSU l         g r|   rp  r:   s    r    rr  #ClientStartStopFactory.startFactoryw  rt  r#   c                     U R                   (       a  U R                  (       a  [        eSU l        U R                  R	                  S5        g )Nrg   T)rq  r   r   rz  r   r:   s    r    r   "ClientStartStopFactory.stopFactory|  s0    ||t||!!$'r#   )rq  r   rz  N)
rG   rH   rI   rJ   rq  r   rd   rr  r   rK   rE   r#   r    rx  rx  o  s    GG,
(r#   rx  c                   $    \ rS rSrSrS rS rSrg)FactoryTestsi  zTests for factories.c                   ^ ^^	 [        5       m[        R                  " STSS9nT R                  UR                  5        T R                  TR                  TR                  4S5        [        R                  " STSS9n[        R                  " STSS9m	T R                  TR                  TR                  4S5        [        R                  " UR                  5      n[        R                  " UR                  5      n[        R                  " X4/5      nUU	U 4S jnUR                  U5        UU 4S jnUR                  U5        U$ )z
The factory passed to L{IReactorTCP.listenTCP} should be started only
when it transitions from being used on no ports to being used on one
port and should be stopped only when it transitions from being used on
one port to being used on no ports.
r   r   r   rg   r   c                 t   > TR                  TR                  TR                  4S5        TR                  5       $ )Nr  )r   rq  r   rQ   )r   r   p3r;   s    r    cbClosed3FactoryTests.test_serverStartStop.<locals>.cbClosed  s/    aii3V<##%%r#   c                 V   > TR                  TR                  TR                  4S5        g )N)rg   rg   )r   rq  r   )r   r   r;   s    r    cbClosedAll6FactoryTests.test_serverStartStop.<locals>.cbClosedAll  s     aii3V<r#   )rn  r   r   r   rQ   r   rq  r   r   r   r   r,   )
r;   r   p2d1r1   rl   r  r  r   r  s
   `       @@r    test_serverStartStop!FactoryTests.test_serverStartStop  s	     q!{;(()!))QYY/8 q!{;q!{;!))QYY/8   !1!12  !1!12,,bX6	&
 	""8,	= 	"";/r#   c                 R  ^ [        5       n[        R                  " SUSS9nX!l        U R	                  UR
                  5        UR                  5       R                  n[        5       m[        R                  " SUT5        U R                  TR                  5        [        U4S j5      $ )z
The factory passed to L{IReactorTCP.connectTCP} should be started when
the connection attempt starts and stopped when it is over.
r   r   r   c                     > T R                   $ r   r   )rk   s   r    r  3FactoryTests.test_clientStartStop.<locals>.<lambda>  s	    r#   )rM   r   r   rO   r   rW   r   rx  r   r   rq  r2   )r;   r   ro   r   rk   s       @r    test_clientStartStop!FactoryTests.test_clientStartStop  s}    
 ak:		"YY[%%
(*;
G<(011r#   rE   N)rG   rH   rI   rJ   rY   r  r  rK   rE   r#   r    r  r    s    +Z2r#   r  c                   *    \ rS rSrSrS rS rS rSrg)CannotBindTestsi  zQ
Tests for correct behavior when a reactor cannot bind to the required TCP
port.
c                    [        5       n[        R                  " SUSS9nU R                  UR                  5        UR                  5       R                  nUR                  5       nU R                  UR                  S5        U R                  UR                  S5        U R                  UR                  U5        U R                  [        R                  [        R                  X1SS9  g)zk
L{IReactorTCP.listenTCP} raises L{error.CannotListenError} if the
address to listen on is already in use.
r   r   r   TCPN)rr   r   r   r   rQ   r   rO   r   typehostr   r	   CannotListenError)r;   r   r   r   dests        r    test_cannotBindCannotBindTests.test_cannotBind  s    
 q!{;(()JJLzz|E*K0A& 	##W%6%6 	 	
r#   c                 4   ^^ [        T5      UU4S j5       nU$ )z>Returns closure that when called calls f and then callbacks d.c                  :   > T" U 0 UD6nTR                  S5        U$ )N r   )argsr|  rtnr   r   s      r    newf/CannotBindTests._fireWhenDoneFunc.<locals>.newf  s     T.R.CJJrNJr#   r   )r;   r   r   r  s    `` r    _fireWhenDoneFunc!CannotBindTests._fireWhenDoneFunc  s"     
q	 
	
 r#   c                 |  ^ ^^^^^^	^
 [         R                  " 5       n[        5       nT R                  XR                  5      Ul        [
        R                  " SUSS9m
T R                  T
R                  5        UU
U 4S jnUU 4S jmUUU	U
U 4S jmU 4S jmU 4S jmUU 4S	 jm	U 4S
 jmUR                  U5        U$ )z
L{IReactorTCP.connectTCP} calls C{Factory.clientConnectionFailed} with
L{error.ConnectBindError} if the bind address specified is already in
use.
r   r   r   c                   > [         R                  " 5       n[        5       nTR                  XR                  5      Ul        [
        R                  " STR                  5       R                  USS9  UR                  TU5        U$ )Nr   r0  bindAddress)
r   r*   ru   r  rT   r   r   r   rO   r,   )resultsr   cf1_conmadero   r;   s      r    	_connect12CannotBindTests.test_clientBind.<locals>._connect1  si     A!#C $ 6 6q:K:K LCQYY[--s@P MM(C(Hr#   c                    > [         R                  " 5       nTR                  X!R                  R                  5      UR                  l        UR                  TU5        U$ r   )r   r*   r  ra   r<   r,   )r  r  r   _check1connect2r;   s      r    r  1CannotBindTests.test_clientBind.<locals>._conmade  sL     A.2.D.D##22/C+ MM/3/Hr#   c                   > TR                  UR                  R                  S5        [        R                  " 5       n[        R                  " 5       nUR                  R
                  R                  5       R                  n[        5       nTR                  X%R                  5      Ul
        TR                  X5R                  5      Ul        [        R                  " ST
R                  5       R                  USU4S9  UR                  TX5        UR                  TX5        [        R                  " X#/5      nUR                  T	X5        U$ )Nrg   r   r  )r   ra   r   r   r*   r8   r   rO   ru   r  r   r   r   r   r,   DeferredList)r  r  r  r1   rO   cf2dl_check2failed_check2stopped_stopro   r;   s          r    r  8CannotBindTests.test_clientBind.<locals>._check1connect2  s    S--22A6!B!B##--557<<D!#C)-)?)?..*C& #44RICOQYY[--sd@S NN=#3NN>34##RH-BNN5#+Ir#   c                   > TR                  UR                  S5        UR                  R                  [        R
                  5        TR                  UR                  R                  [        R
                  5      5        U $ r|   )r   r}   rB   r?   r	   ConnectBindErrorr   r!   r  r  r  r;   s      r    r  6CannotBindTests.test_clientBind.<locals>._check2failed  sS    SZZ+JJOOE223OOCJJ,,U-C-CDENr#   c                 @   > TR                  UR                  S5        U $ r|   r   r   r  s      r    r  7CannotBindTests.test_clientBind.<locals>._check2stopped!  s    S[[!,Nr#   c                    > [         R                  " 5       nUR                  TU5        TR                  X1R                  5      Ul        UR
                  R                  R                  5         U$ r   )r   r*   r,   r  r   ra   r8   r9   )r  r  r  r   _check1cleanupr;   s       r    r  .CannotBindTests.test_clientBind.<locals>._stop%  sP     AMM.#."44QHCO&&557Hr#   c                 >   > TR                  UR                  S5        g r|   r  )r  r  r;   s     r    r  7CannotBindTests.test_clientBind.<locals>._check1cleanup,  s    S[[!,r#   )
r   r*   rr   r  rr  r   r   r   rQ   r,   )r;   theDeferredsfr  r  r  r  r  r  r  ro   s   `   @@@@@@@r    test_clientBindCannotBindTests.test_clientBind  s     nn&00ooNa{;(			 	(				- 		*r#   rE   N)	rG   rH   rI   rJ   rY   r  r  r  rK   rE   r#   r    r  r    s    

(	Gr#   r  c                       \ rS rSrSrS rSrg)LocalRemoteAddressTestsi3  zc
Tests for correct getHost/getPeer values and that the correct address is
passed to buildProtocol.
c                   ^ ^^^^	 [        5       n[        R                  " 5       Ul        UR                  m	[        R
                  " SUSS9mT R                  TR                  5        TR                  5       R                  n[        5       m[        R                  " 5       =nTl        [        R                  " SUT5      mUUU 4S jnUR                  U5        UU	4S jnUR                  U5        U$ )zg
L{IListeningPort.getHost} returns the same address as a client
connection's L{ITCPTransport.getPeer}.
r   r   r   c                    > TR                  TR                  5       /TR                  5        TR                  TR                  5       TR                  R                  R                  5       5        g r   )r   r   rc   ra   r8   getPeer)r   ri  rO   r;   s    r    r!   7LocalRemoteAddressTests.test_hostAddress.<locals>.checkI  sP    dlln-}/J/JK : : D D L L Nr#   c                 (   > TR                  5         T$ r   )r   )r   r   serverConnectionLosts    r    rI  9LocalRemoteAddressTests.test_hostAddress.<locals>.cleanupQ  s       "''r#   )rr   r   r*   r`   r   r   r   rQ   r   rO   ru   r_   r   r,   )
r;   r   r   onConnectionr!   rI  ri  r   rO   r  s
   `     @@@@r    test_hostAddress(LocalRemoteAddressTests.test_hostAddress9  s    
 ()/4~~/?,,CC  M[I**+LLN')>Cnn>NN};&&{A}E		 	  '	( 	  )r#   rE   N)rG   rH   rI   rJ   rY   r  rK   rE   r#   r    r  r  3  s    
!r#   r  c                       \ rS rSrS rSrg)WriterProtocoli]  c                    U R                   R                  S5        / SQnU R                   R                  U5        U R                   R                  5       nUR                  S:w  a  [        SU 35        SU R                  l        U R                   R                  5       nUR                  S:w  a  [        SU 35        SU R                  l        SU R                  l	        U R                   R                  5         g )N   Hello Cleveland!
)   Goodbye    cruel    world   
r  z!getPeer returned non-TCP socket: rg   z!getHost returned non-TCP socket: )r8   writewriteSequencer  r  r   rk   problemr   doner9   )r;   seqpeeruss       r    r<   WriterProtocol.connectionMade^  s     	237$$S)~~%%'993D6:;#$DLL ^^##%77e3B489#$DLL %%'r#   rE   NrG   rH   rI   rJ   r<   rK   rE   r#   r    r  r  ]  s    (r#   r  c                        \ rS rSrS rS rSrg)ReaderProtocolit  c                 B    U R                   =R                  U-  sl        g r   )rk   datar;   r  s     r    dataReceivedReaderProtocol.dataReceivedu  s    T!r#   c                 &    SU R                   l        g r|   rk   r  rA   s     r    rC   ReaderProtocol.connectionLostx      r#   rE   NrG   rH   rI   rJ   r  rC   rK   rE   r#   r    r  r  t  s    "r#   r  c                        \ rS rSrS rS rSrg)WriterClientFactoryi|  c                      SU l         SU l        g )Nr   r#   )r  r  r:   s    r    rd   WriterClientFactory.__init__}  s    		r#   c                 2    [        5       nXl        X l        U$ r   )r  rk   ra   rm   s      r    rT   !WriterClientFactory.buildProtocol  s    	r#   )r  r  ra   NrG   rH   rI   rJ   rd   rT   rK   rE   r#   r    r  r  |  s    r#   r  c                   $    \ rS rSrSrS rS rSrg)WriteDataTestsi  zk
Test that connected TCP sockets can actually write data. Try to exercise
the entire ITransport interface.
c                   ^ ^^ [         R                  " 5       m[        Tl         STl        STl        [        T5      n[        R                  " SUSS9nT R                  UR                  5        UR                  5       R                  n[        5       m[        T5      n[        R                  " SX45        UUU 4S jn[        R                  " UR                   UR                   /5      nUR#                  U5      $ )zj
L{ITCPTransport.write} and L{ITCPTransport.writeSequence} send bytes to
the other end of the connection.
r   r   r   c                   > TR                  TR                  S5        TR                  TR                  S:H  S5        TR                  TR                  S5        SR                  / SQ5      nTR                  TR                  U:H  S5        g )N&writer didn't finish, it probably diedr   zwriter indicated an error$client didn't see connection droppedr#   )r  r  r  r  r  z.client didn't receive all the data it expected)r   r  r  joinr  )r   expectedr   r   r;   s     r    r!   )WriteDataTests.test_writer.<locals>.check  sn    OOAFF$LMOOAIIN,GHOOGLL*PQxxPH OO(@r#   )r   Factoryr  r  r  WiredFactoryr   r   r   rQ   r   rO   r  r   r   r   onDisconnectr,   )	r;   wrappedFro   r   wrappedClientFr!   r   r   r   s	   `      @@r    test_writerWriteDataTests.test_writer  s    
 #
	?a[A(IIK%'%g.;:
	 !6!68S8S TU}}U##r#   c                 v  ^^	^
^ [         R                  R                  S:X  a  [        S5      e[        R
                  " 5       m
[        R
                  " 5       m " U
U4S jS[        R                  5      n[        R                  " 5       nXl        [         R                  " SUSS9nU R                  UR                  5        UR                  5       n[        [        5       " S S	5      5       m " UU
U4S
 jS[        R                  5      n[        5       m	UT	l        T	R"                  nU	4S jnUR%                  U5        ['        SUR(                   SUR*                   35        [         R,                  " UR(                  UR*                  T	5        ['        S5        U R/                  U[0        R2                  5      $ )z
A TCP transport which is written to after the connection has been shut
down should notify its protocol that the connection has been lost, even
if the TCP transport is not actively being monitored for read events
(ie, pauseProducing was called on it).
IOCPReactora(  iocpreactor does not, in fact, stop reading immediately after pauseProducing is called. This results in a bonus disconnection notification. Under some circumstances, it might be possible to not receive this notifications (specifically, pauseProducing, deliver some data, proceed with this test). c                   2   > \ rS rSrSrU 4S jrU4S jrSrg)JWriteDataTests.test_writeAfterShutdownWithoutReading.<locals>.Disconnecteri  z
Protocol for the server side of the connection which disconnects
itself in a callback on clientPaused and publishes notification
when its connection is actually lost.
c                 L   >^  [        S5        U 4S jnTR                  U5        g)z;
Set up a callback on clientPaused to lose the connection.
zDisconnector.connectionMadec                 f   > [        S5        TR                  R                  5         [        S5        g )Nz&Disconnector.connectionMade disconnectzloseConnection calledr7   )r   r;   s    r    r   mWriteDataTests.test_writeAfterShutdownWithoutReading.<locals>.Disconnecter.connectionMade.<locals>.disconnect  s%    @ANN113/0r#   N)r   r,   )r;   r   clientPauseds   ` r    r<   YWriteDataTests.test_writeAfterShutdownWithoutReading.<locals>.Disconnecter.connectionMade  s#     121
 ((4r#   c                 T   > [        S5        TR                  S5        [        S5        g)zD
Notify observers that the server side of the connection has
ended.
zDisconnecter.connectionLostNzserverLost called back)r   r   )r;   rB   
serverLosts     r    rC   YWriteDataTests.test_writeAfterShutdownWithoutReading.<locals>.Disconnecter.connectionLost  s%    
 12##D),-r#   rE   N)rG   rH   rI   rJ   rY   r<   rC   rK   )r  r  s   r    Disconnecterr    s    5. .r#   r  r   r   r   c                   *    \ rS rSrSrS rS rS rSrg)FWriteDataTests.test_writeAfterShutdownWithoutReading.<locals>.Infinitei  z
A producer which will write to its consumer as long as
resumeProducing is called.

@ivar consumer: The L{IConsumer} which will be written to.
c                     Xl         g r   consumer)r;   r  s     r    rd   OWriteDataTests.test_writeAfterShutdownWithoutReading.<locals>.Infinite.__init__  s     (r#   c                 f    [        S5        U R                  R                  S5        [        S5        g )NzInfinite.resumeProducing   xz*Infinite.resumeProducing wrote to consumer)r   r  r  r:   s    r    resumeProducingVWriteDataTests.test_writeAfterShutdownWithoutReading.<locals>.Infinite.resumeProducing  s&    ./##D)@Ar#   c                     [        S5        g )NzInfinite.stopProducing)r   r:   s    r    stopProducingTWriteDataTests.test_writeAfterShutdownWithoutReading.<locals>.Infinite.stopProducing  s    ,-r#   r  N)	rG   rH   rI   rJ   rY   rd   r"  r%  rK   rE   r#   r    Infiniter    s    )B
.r#   r'  c                   *   > \ rS rSrSrU UU4S jrSrg)MWriteDataTests.test_writeAfterShutdownWithoutReading.<locals>.UnreadingWriteri  z[
Trivial protocol which pauses its transport immediately and then
writes some bytes to it.
c                    >^  [        S5        T R                  R                  5         TR                  S 5        [        S5        UU 4S jnTR	                  U5        g )NzUnreadingWriter.connectionMadezclientPaused called backc                    > [        S5        T" TR                  5      n[        S5        TR                  R                  US5        [        S5        g )Nz$UnreadingWriter.connectionMade writez5UnreadingWriter.connectionMade write created producerFz8UnreadingWriter.connectionMade write registered producer)r   r8   registerProducer)r   producerr'  r;   s     r    r  kWriteDataTests.test_writeAfterShutdownWithoutReading.<locals>.UnreadingWriter.connectionMade.<locals>.write  sB    >?  (7HOPNN33HeDRSr#   )r   r8   pauseProducingr   r,   )r;   r  r'  r  r  s   ` r    r<   \WriteDataTests.test_writeAfterShutdownWithoutReading.<locals>.UnreadingWriter.connectionMade  sI    45--/%%d+./T$ &&u-r#   rE   N)rG   rH   rI   rJ   rY   r<   rK   )r'  r  r  s   r    UnreadingWriterr)    s    
. .r#   r1  c                 2   > [        S5        TR                  $ )NcbClientLost)r   r   )r   r   s    r    r3  JWriteDataTests.test_writeAfterShutdownWithoutReading.<locals>.cbClientLost,  s    $$$r#   zConnecting to :zReturning Deferred)r   	__class__rG   r   r   r*   r   rR  ServerFactoryr   r   rQ   r   r   r   ru   rj   rw   r,   r   r  rO   r   r   r	   ConnectionLost)r;   r  r   rO   rn   r1  r   r3  r'  r   r  r  s           @@@@r    %test_writeAfterShutdownWithoutReading4WriteDataTests.test_writeAfterShutdownWithoutReading  se    %%6?  ~~' ^^%
	. 	.8,, 	.< '')&  FkB**+||~	]	#	. 	. 
$	.&	. 	.h// 	.B !"!0%	% 	((6nTYYKq45499dii8 	 !!!"68L8LMMr#   rE   N)rG   rH   rI   rJ   rY   r  r9  rK   rE   r#   r    r  r    s    
$@INr#   r  c                       \ rS rSrS rSrg)ConnectionLosingProtocoli:  c                     U R                   R                  S5        U R                   R                  5         U R                  R	                  5         U R                  R
                  R                  U R                   5        g )N   1)r8   r  r9   master_connectionMadeportsrh   r:   s    r    r<   'ConnectionLosingProtocol.connectionMade;  sP    T"%%'##%  0r#   rE   Nr  rE   r#   r    r<  r<  :  s    1r#   r<  c                        \ rS rSrS rS rSrg)NoopProtocoliB  c                     [         R                  " 5       U l        U R                  R                  R                  U R                  5        g r   )r   r*   r   r?  serverConnsrh   r:   s    r    r<   NoopProtocol.connectionMadeC  s,    !&&tvv.r#   c                 :    U R                   R                  S5        g NT)r   r   rA   s     r    rC   NoopProtocol.connectionLostG  s    r#   )r   NrF   rE   r#   r    rD  rD  B  s    /r#   rD  c                   $    \ rS rSrSrS rS rSrg)ConnectionLostNotifyingProtocoliK  aG  
Protocol which fires a Deferred which was previously passed to
its initializer when the connection is lost.

@ivar onConnectionLost: The L{Deferred} which will be fired in
    C{connectionLost}.

@ivar lostConnectionReason: L{None} until the connection is lost, then a
    reference to the reason passed to C{connectionLost}.
c                     S U l         Xl        g r   lostConnectionReasononConnectionLost)r;   rP  s     r    rd   (ConnectionLostNotifyingProtocol.__init__W  s    $(! 0r#   c                 F    Xl         U R                  R                  U 5        g r   )rO  rP  r   rA   s     r    rC   .ConnectionLostNotifyingProtocol.connectionLost[  s    $*!&&t,r#   rN  N)rG   rH   rI   rJ   rY   rd   rC   rK   rE   r#   r    rL  rL  K  s    	1-r#   rL  c                       \ rS rSrSrS rSrg)HandleSavingProtocoli`  z
Protocol which grabs the platform-specific socket handle and
saves it as an attribute on itself when the connection is
established.
c                 j    UR                  5       U l        [        R                  R	                  X5      $ )zD
Save the platform-specific socket handle for future
introspection.
)	getHandlehandler   rR  makeConnection)r;   r8   s     r    rY  #HandleSavingProtocol.makeConnectiong  s+    
  ))+  //@@r#   )rX  N)rG   rH   rI   rJ   rY   rY  rK   rE   r#   r    rU  rU  `  s    Ar#   rU  c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
ProperlyCloseFilesMixinip  z9
Tests for platform resources properly being cleaned up.
c                     [        5       e)z
Bind a server port to which connections will be made.  The server
should use the given protocol factory.

@return: The L{IListeningPort} for the server created.
NotImplementedErrorr;   addressr   rk   s       r    createServer$ProperlyCloseFilesMixin.createServeru       "##r#   c                     [        5       e)z
Establish a connection to the given address using the given
L{ClientCreator} instance.

@return: A Deferred which will fire with the connected protocol instance.
r^  r;   ra  r   r>  s       r    connectClient%ProperlyCloseFilesMixin.connectClient~  rd  r#   c                     [        5       e)zm
Return the exception class which will be raised when an operation is
attempted on a closed platform handle.
r^  r:   s    r    getHandleExceptionType.ProperlyCloseFilesMixin.getHandleExceptionType  s    
 "##r#   c                     [         R                  " 5       (       a$  [        R                  " [        R
                  5      $ [        R                  " [        R                  5      $ )z
Return a L{hamcrest.core.matcher.Matcher} that matches the
errno expected to result from writing to a closed platform
socket handle.
)r   	isWindowshamcrestequal_torB  WSAENOTSOCKEBADFr:   s    r    getHandleErrorCodeMatcher1ProperlyCloseFilesMixin.getHandleErrorCodeMatcher  s?     $$U%6%677  --r#   c                    ^ ^^	^
 [         R                  " 5       m	[        R                  " 5       nU	4S jUl        T R	                  SSU5      m
[         R                  " 5       mT
R                  5       n[        R                  " [        U4S j5      nT R                  UR                  UR                  U5      nUU	4S jnUR                  U5        U 4S jnUR                  U5        U
4S jnUR                  U5        U$ )zX
Test that lost connections properly have their underlying socket
resources cleaned up.
c                     > [        T 5      $ r   )rL  )onServerConnectionLosts   r    r  AProperlyCloseFilesMixin.test_properlyCloseFiles.<locals>.<lambda>  s    )H"*
r#   r   r   c                     > [        T 5      $ r   )rU  )onClientConnectionLosts   r    r  rw    s    12HIr#   c                    > U R                   R                  S5        U R                   R                  5         [        R                  " TT/5      $ )z
Disconnect the client.  Return a Deferred which fires when both
the client and the server have received disconnect notification.
s0   some bytes to make sure the connection is set up)r8   r  r9   r   r   )r   ry  rv  s    r    clientConnectedHProperlyCloseFilesMixin.test_properlyCloseFiles.<locals>.clientConnected  sD    
 ""#VW++-&&(>@V'WXXr#   c                   > U u  pUR                   R                  [        R                  5      (       d  [	        UR                   S5        UR                   R                  [        R                  5      (       d  [	        UR                   S5        TR                  5       nTR                  TR                  5       UR                  R                  S5      n[        R                  " UR                  S   U5        g)zH
Verify that the underlying platform socket handle has been
cleaned up.
z,Client lost connection for unexpected reasonz,Server lost connection for unexpected reasons   bytesr   N)rO  r!   r	   ConnectionClosedr   rr  r   rj  rX  sendrn  assert_thatr  )r&   r   r   errorCodeMatcher	exceptionr;   s        r    clientDisconnectedKProperlyCloseFilesMixin.test_properlyCloseFiles.<locals>.clientDisconnected  s    
 $NF..44U5K5KLL//B ..44U5K5KLL//B  $==?))++-v}}/A/A8I   q! r#   c                 t   >^  [         R                  " TR                  5      nUR                  U 4S j5        U$ )zT
Shut down the server port.  Return a Deferred which fires when
this has completed.
c                    > T$ r   rE   )r   rH  s    r    r  RProperlyCloseFilesMixin.test_properlyCloseFiles.<locals>.cleanup.<locals>.<lambda>  s    ;r#   )r   r   rQ   r,   )rH  r&   r<  s   ` r    rI  @ProperlyCloseFilesMixin.test_properlyCloseFiles.<locals>.cleanup  s/    
 (()A)ABF67Mr#   )r   r*   r   r7  rb  r   rQ  r   rg  r  rO   r,   rS  )r;   r   
serverAddrr>  clientDeferredr{  r  rI  ry  rv  r<  s   `       @@@r    test_properlyCloseFiles/ProperlyCloseFilesMixin.test_properlyCloseFiles  s    
 "'!1 ..0"
 &&{A}E
!&!1'')
 ..I
 ++OOZ__m
	Y 	""?3	2 	""#56	 	w'r#   rE   N)rG   rH   rI   rJ   rY   rb  rg  rj  rr  r  rK   rE   r#   r    r\  r\  p  s!    $$$
.Fr#   r\  c                   *    \ rS rSrSrS rS rS rSrg)ProperlyCloseFilesTestsi  z
Test that the sockets created by L{IReactorTCP.connectTCP} are cleaned up
when the connection they are associated with is closed.
c                 ,    [         R                  " X#US9$ )z5
Create a TCP server using L{IReactorTCP.listenTCP}.
r   )r   r   r`  s       r    rb  $ProperlyCloseFilesTests.createServer  s       HHr#   c                 $    UR                  X5      $ )z6
Create a TCP client using L{IReactorTCP.connectTCP}.
)r   rf  s       r    rg  %ProperlyCloseFilesTests.connectClient  s     ''<<r#   c                 "    [         R                  $ )z
Return L{socket.error} as the expected error type which will be
raised by a write to the low-level socket object after it has been
closed.
)rL  r	   r:   s    r    rj  .ProperlyCloseFilesTests.getHandleExceptionType  s     ||r#   rE   N)	rG   rH   rI   rJ   rY   rb  rg  rj  rK   rE   r#   r    r  r    s    
I=r#   r  c                   &    \ rS rSrS rS rS rSrg)WiredForDeferredsi  c                 D    [         R                  R                  XU5        g r   )r   ProtocolWrapperrd   )r;   rk   wrappedProtocols      r    rd   WiredForDeferreds.__init__  s      ))$Ir#   c                     [         R                  R                  U 5        U R                  R                  R                  S 5        g r   )r   r  r<   rk   	onConnectr   r:   s    r    r<    WiredForDeferreds.connectionMade  s.      //5''-r#   c                     [         R                  R                  X5        U R                  R                  R                  S 5        g r   )r   r  rC   rk   r  r   rA   s     r    rC    WiredForDeferreds.connectionLost  s.      //=!!**40r#   rE   N)rG   rH   rI   rJ   rd   r<   rC   rK   rE   r#   r    r  r    s    J.1r#   r  c                       \ rS rSr\rS rSrg)r  i  c                     [         R                  R                  X5        [        R                  " 5       U l        [        R                  " 5       U l        g r   )r   WrappingFactoryrd   r   r*   r  r  r;   wrappedFactorys     r    rd   WiredFactory.__init__  s3      ))$?)!NN,r#   )r  r  N)rG   rH   rI   rJ   r  r   rd   rK   rE   r#   r    r  r    s     H-r#   r  c                   *    \ rS rSrSrS rS rS rSrg)AddressTestsi  zJ
Tests for address-related interactions with client and server protocols.
c                     " S S[         R                  5      n[        5       U l        [        R
                  " 5       =U l        U R                  l        [        R
                  " 5       =U l        U R                  l	        U" U R                  5      U l
        [        5       U l        [        R
                  " 5       =U l        U R                  l        [        R
                  " 5       =U l        U R                  l	        U" U R                  5      U l        [         R"                  " SU R                  SS9U l        [         R&                  " U R$                  R)                  5       R*                  U R$                  R)                  5       R$                  U R                  5      U l        [        R.                  " U R                  U R                  /5      $ )a	  
Create a port and connected client/server pair which can be used
to test factory behavior related to addresses.

@return: A L{defer.Deferred} which will be called back when both the
    client and server protocols have received their connection made
    callback.
c                   $    \ rS rSrSrS rS rSrg).AddressTests.setUp.<locals>.RememberingWrapperi#  a0  
Simple wrapper factory which records the addresses which are
passed to its L{buildProtocol} method and delegates actual
protocol creation to another factory.

@ivar addresses: A list of the objects passed to buildProtocol.
@ivar factory: The wrapped factory to which protocol creation is
    delegated.
c                     / U l         Xl        g r   	addressesrk   )r;   rk   s     r    rd   7AddressTests.setUp.<locals>.RememberingWrapper.__init__.  s    !#&r#   c                 n    U R                   R                  U5        U R                  R                  U5      $ )zX
Append the given address to C{self.addresses} and forward
the call to C{self.factory}.
)r  rh   rk   rT   )r;   rn   s     r    rT   <AddressTests.setUp.<locals>.RememberingWrapper.buildProtocol5  s+    
 %%d+||11$77r#   r  N)rG   rH   rI   rJ   rY   rd   rT   rK   rE   r#   r    RememberingWrapperr  #  s    '8r#   r  r   r   r   )r   ClientFactoryrr   r   r   r*   r   r_   serverConnLostr`   serverWrapperru   r   r   clientConnLostclientWrapperr   r   rO   r   r   r  r   r   )r;   r  s     r    setUpAddressTests.setUp  sA   	8!7!7 	8: &'CH>>CSSdkk@CH>>CSSdkk@ 0< &'CH>>CSSdkk@CH>>CSSdkk@/<%%a););{S	 ++II$$dii&7&7&9&>&>@R@R
 ""D$7$79L9L#MNNr#   c                     U R                   R                  5         [        R                  " U R                  U R
                  [        R                  " U R                  R                  5      /5      $ )zN
Disconnect the client/server pair and shutdown the port created in
L{setUp}.
)	r   r   r   r   r  r  r   rO   rQ   r:   s    r    tearDownAddressTests.tearDownT  sV    
 	!!#""######DII$;$;<
 	
r#   c                    U R                   R                  R                  R                  5       nU R                  R                  R                  R                  5       nU R                  U R                  R                  [        SUR                  UR                  5      /5        U R                  U R                  R                  [        SUR                  UR                  5      /5        g)a5  
L{ClientFactory.buildProtocol} should be invoked with the address of
the server to which a connection has been established, which should
be the same as the address reported by the C{getHost} method of the
transport of the server protocol and as the C{getPeer} method of the
transport of the client protocol.
r  N)r   ra   r8   r   r   r  r   r  r  r   r  rO   )r;   r;  
clientPeers      r    test_buildProtocolClient%AddressTests.test_buildProtocolClientb  s     [[--77??A
[[--77??A
((
AB	
 	((
AB	
r#   )
r   r  r   r  r   rO   r   r  r   r  N)	rG   rH   rI   rJ   rY   r  r  r  rK   rE   r#   r    r  r    s    9Ov

r#   r  c                       \ rS rSrS rSrg)LargeBufferWriterProtocoliw  c                    ^  T R                   R                  ST R                  R                  S-
  -  5        U 4S jn[        R
                  " SU5        g )N   Xrg   c                     > T R                   R                  S5        ST R                  l        T R                   R	                  5         g )Nr  rg   )r8   r  rk   r  r9   r:   s   r    finish8LargeBufferWriterProtocol.connectionMade.<locals>.finish  s1    NN  & !DLLNN))+r#   gMbP?)r8   r  rk   lenr   	callLater)r;   r  s   ` r    r<   (LargeBufferWriterProtocol.connectionMade}  s@    TT\\%5%5%9:;	,
 	%(r#   rE   Nr  rE   r#   r    r  r  w  s    )r#   r  c                        \ rS rSrS rS rSrg)LargeBufferReaderProtocoli  c                 T    U R                   =R                  [        U5      -  sl        g r   )rk   r  r  s     r    r  &LargeBufferReaderProtocol.dataReceived  s    CI%r#   c                 &    SU R                   l        g r|   r  rA   s     r    rC   (LargeBufferReaderProtocol.connectionLost  r  r#   rE   Nr  rE   r#   r    r  r    s    &r#   r  c                        \ rS rSrS rS rSrg)LargeBufferReaderClientFactoryi  c                      SU l         SU l        g Nr   )r  r  r:   s    r    rd   'LargeBufferReaderClientFactory.__init__  s    	r#   c                 2    [        5       nXl        X l        U$ r   )r  rk   ra   rm   s      r    rT   ,LargeBufferReaderClientFactory.buildProtocol  s    %'	r#   )r  ra   r  Nr  rE   r#   r    r  r    s    r#   r  c                       \ rS rSrSrS rSrg)FireOnClosei  zYA wrapper around a protocol that makes it fire a deferred when
connectionLost is called.
c                     [         R                  R                  X5        U R                  R                  R                  S 5        g r   )r   r  rC   rk   rw   r   rA   s     r    rC   FireOnClose.connectionLost  s.      //=&&t,r#   rE   N)rG   rH   rI   rJ   rY   rC   rK   rE   r#   r    r  r    s    -r#   r  c                       \ rS rSr\rS rSrg)FireOnCloseFactoryi  c                 v    [         R                  R                  X5        [        R                  " 5       U l        g r   )r   r  rd   r   r*   rw   r  s     r    rd   FireOnCloseFactory.__init__  s$      ))$?(r#   )rw   N)rG   rH   rI   rJ   r  r   rd   rK   rE   r#   r    r  r    s    H)r#   r  c                   "    \ rS rSrSrSrS rSrg)LargeBufferTestsi  z0Test that buffering large amounts of data works.i  c                   ^ ^^ [         R                  " 5       m[        Tl         STl        STl        T R
                  Tl        [        T5      n[        R                  " SUSS9nT R                  UR                  5        UR                  5       R                  n[        5       m[        T5      n[        R                  " SX45        [         R"                  " UR$                  UR$                  /5      nUUU 4S jnUR'                  U5      $ )Nr   r   r   c                 ~  > TR                  TR                  S5        TR                  TR                  TR                  :  STR                  TR                  4-  5        TR                  TR                  TR                  :*  STR                  TR                  4-  5        TR                  TR                  S5        g )Nr  z9client didn't receive all the data it expected (%d != %d)z8client did receive more data than it expected (%d != %d)r  )r   r  r  datalen)r   r   r   r;   s    r    r!   *LargeBufferTests.testWriter.<locals>.check  s    OOAFF$LMOOt||+ 'T\\:;
 OOt||+ 'T\\:;
 OOGLL*PQr#   )r   r  r  r  r  r  r  r  r   r   r   rQ   r   rO   r  r   r   r   rw   r,   )	r;   r	  ro   r   r
  r   r!   r   r   s	   `      @@r    
testWriterLargeBufferTests.testWriter  s    .
	%a(a[A(IIK02+G4;:!2!2N4K4K LM	R }}U##r#   rE   N)rG   rH   rI   rJ   rY   r  r  rK   rE   r#   r    r  r    s    :G$r#   r  c                   (    \ rS rSrSrSrS rS rSrg)MyHCProtocoli  Fc                 X    SU l         U R                  (       a  U R                  S 5        g g rI  )readHalfClosedwriteHalfClosedrC   r:   s    r    readConnectionLostMyHCProtocol.readConnectionLost  s&    "%  r#   c                 X    SU l         U R                  (       a  U R                  S 5        g g rI  )r  r  rC   r:   s    r    writeConnectionLost MyHCProtocol.writeConnectionLost  s&    #% r#   )r  r  N)	rG   rH   rI   rJ   r  r  r  r  rK   rE   r#   r    r  r    s    NO&&r#   r  c                   "    \ rS rSrSrSrS rSrg)MyHCFactoryi  r   Nc                 \    U =R                   S-  sl         [        5       nXl        X l        U$ r|   )ri   r  rk   ra   rm   s      r    rT   MyHCFactory.buildProtocol  s'    qN	r#   )ra   )rG   rH   rI   rJ   ri   r_   rT   rK   rE   r#   r    r  r    s    F!r#   r  c                   <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)HalfCloseTestsi  zTest half-closing connections.c                 d  ^ ^ [        5       =T l        n[        R                  " SUSS9=T l        mT R                  TR                  5        [        U4S j5      n[        R                  " [        [        5      T l        UR                  UU 4S j5        UR                  T R                  5        U$ )Nr   r   r   c                     > T R                   $ r   )r  )ro   s   r    r  &HalfCloseTests.setUp.<locals>.<lambda>  s	    akkr#   c                    > TR                   R                  TR                  5       R                  TR                  5       R                  5      $ r   )cfr   r   r  rO   )_ro   r;   s    r    r  r    s.     2 2199;3C3CQYY[EUEU Vr#   )r  r   r   r   ro   r   rQ   r2   r   rQ  r  r  r,   _setUp)r;   r   r   ro   s   `  @r    r  HalfCloseTests.setUp  s}     ]"&&q!{CC()*((,?	VW	dkk"r#   c                    ^  UT l         [        R                  " 5       =T l        T R                   l        T R                  T R                   R                  R                  S5        [        U 4S j5      $ )Nrg   c                  6   > [        T R                  SS 5      S L$ )Nra   )r  r   r:   s   r    r  'HalfCloseTests._setUp.<locals>.<lambda>  s    !Fd!Rr#   )	r   r   r*   clientProtoConnectionLostrl   r   r8   r  r2   r;   r   s   ` r    r  HalfCloseTests._setUp  sQ    FKnnFVV&)C..88!<RSSr#   c                 P  ^  T R                  T R                  R                  S5        T R                  R                  R	                  5         [
        R                  " T R                  R                  5      nUR                  U 4S j5        UR                  T R                  5        U$ )Nr   c                    > TR                   $ r   )r  )r   r;   s    r    r  )HalfCloseTests.tearDown.<locals>.<lambda>  s    $"@"@r#   )r   r   closedr8   r9   r   r   ro   rQ   r,   	_tearDown)r;   r   s   ` r    r  HalfCloseTests.tearDown  so    ++Q/,,. 4 45	@A	dnn%r#   c                   ^ ^ T R                  T R                  R                  S5        T R                  T R                  R                  R                  S5        [
        R                  " 5       mUU 4S jnUT R                  R                  l        T R                  R                  R                  R                  5         TR                  U 4S j5        T$ )Nrg   r   c                 ^   > STR                   R                  l        TR                  S 5        g r|   )r   ra   r  r   )rB   r   r;   s    r    _connectionLost1HalfCloseTests._tearDown.<locals>._connectionLost  s!    )*DFF&JJtr#   c                 d   > TR                  TR                  R                  R                  S5      $ r|   )r   r   ra   r  r   r;   s    r    r  *HalfCloseTests._tearDown.<locals>.<lambda>  s"     0 01D1D1K1KQ Or#   )r   r   r  r   ra   r   r*   rC   r8   r9   r,   )r;   r   r  r   s   `  @r    r	  HalfCloseTests._tearDown  s    ++Q/ 	,,33Q7NN	 .=*%%446	OPr#   c                   ^ ^^^ T R                   mT R                  mTR                  mTR                  S5        [	        U4S j5      nU4S jnUUU 4S jnUUU 4S jnUR                  U5      R                  U5      R                  U5      $ )N   helloc                  4   > [        T R                  5      S:H  $ r  )r  _tempDataBufferts   r    r  5HalfCloseTests.testCloseWriteCloser.<locals>.<lambda>&  s    c!"3"349r#   c                 B   > TR                  5         [        U4S j5      $ )Nc                     > T R                   $ r   )_writeDisconnectedr  s   r    r  HHalfCloseTests.testCloseWriteCloser.<locals>.loseWrite.<locals>.<lambda>*  s    Q%9%9r#   )loseWriteConnectionr2   )r   r  s    r    	loseWrite6HalfCloseTests.testCloseWriteCloser.<locals>.loseWrite(  s    !!#9::r#   c                    > TR                  TR                  5        TR                  TR                  5        TR                  TR                  5        [        U4S j5      $ )Nc                  0   > T R                   R                  $ r   )ra   r  r   s   r    r  DHalfCloseTests.testCloseWriteCloser.<locals>.check.<locals>.<lambda>0  s    Q^^%B%Br#   )r   r  r   r  r  r2   )r   r   r   r;   s    r    r!   2HalfCloseTests.testCloseWriteCloser.<locals>.check,  sG    V]]+OOF223V223BCCr#   c                   > TR                   R                  nU" S5        U" S5        TR                  S[        TR                   R                  5      5        TR                  TR
                  R                  S5        TR                  TR
                  R                  5        TR                  TR
                  R                  5        g )Nr  s   lalala fooled your   r  )r8   r  r   r  r  ra   r  r   r  r   r  )r   wr   r   r;   s     r    r  2HalfCloseTests.testCloseWriteCloser.<locals>.write2  s      &&AiL"#QF$4$4$D$D EFQ^^00(;Q^^223OOANN99:r#   )r   r   r8   r  r2   r,   )r;   r   r  r!   r  r   r   r  s   `    @@@r    testCloseWriteCloser#HalfCloseTests.testCloseWriteCloser   sn    FF	9:	;	D	; }}Y'33E:FFuMMr#   c                    ^ ^ T R                   mTR                  R                  R                  5         [        R
                  " [        U4S j5      [        U 4S j5      /5      nUR                  UU 4S j5        U$ )Nc                  0   > T R                   R                  $ r   )ra   r  r#  s   r    r  ;HalfCloseTests.testWriteCloseNotification.<locals>.<lambda>C  s    !.."@"@r#   c                  0   > T R                   R                  $ r   )r   r  r:   s   r    r  r-  D  s    $++"<"<r#   c                 P   > TR                  TR                  R                  S5      $ )NF)r   ra   r  )r  r   r;   s    r    r  r-  G  s     0 01N1NPU Vr#   )r   ra   r8   r  r   r   r2   r,   )r;   r   r   s   ` @r    testWriteCloseNotification)HalfCloseTests.testWriteCloseNotification=  s]    FF	  446@A<=
 	
VWr#   )r  r   r  r   ro   N)rG   rH   rI   rJ   rY   r  r  r  r	  r)  r0  rK   rE   r#   r    r  r    s%    (
T N:r#   r  c                   2    \ rS rSrS rS rS rS rS rSr	g)	0HalfCloseNoNotificationAndShutdownExceptionTestsiK  c                    [        5       =U l        n[        R                  " 5       U R                  l        [
        R                  " SUSS9=U l        n[        R                  " [
        [        5      R                  UR                  5       R                  UR                  5       R                  5      nUR                  U R                   5        U$ r   )rr   r   r   r*   r_   r   r   ro   r   rQ  r   r   r   r  rO   r,   
_gotClient)r;   r   ro   r   s       r    r  6HalfCloseNoNotificationAndShutdownExceptionTests.setUpL  s    $&&(-(8%&&q!{CC ""7,@ALLIIKaiik..
 	
doo&r#   c                 :    Xl         U R                  R                  $ r   )r   r   r_   r  s     r    r5  ;HalfCloseNoNotificationAndShutdownExceptionTests._gotClientX  s     vv,,,r#   c                 ~    U R                   R                  R                  5         U R                  R	                  5       $ r   )r   r8   r9   ro   rQ   r:   s    r    r  9HalfCloseNoNotificationAndShutdownExceptionTests.tearDown_  s+    ,,.vv##%%r#   c                   ^  T R                   R                  R                  S5        T R                   R                  R                  5         [        R
                  " 5       =T R                  R                  l        n[        R
                  " 5       =T R                   l        nUR                  U 4S j5        UR                  U 4S j5        [        R                  " X/5      $ )z
TCP protocols support half-close connections, but not all of them
support being notified of write closes.  In this case, test that
half-closing the connection causes the peer's connection to be
closed.
r  c                 d   > TR                  TR                  R                  R                  S5      $ )Nr  )r   r   ra   r  r  s    r    r  UHalfCloseNoNotificationAndShutdownExceptionTests.testNoNotification.<locals>.<lambda>n  s"     0 01D1D1I1I8 Tr#   c                 b   > TR                  TR                  R                  R                  5      $ r   r   r   ra   r  r  s    r    r  r=  o      0C0C0J0J Kr#   )r   r8   r  r  r   r*   r   ra   rl   r,   r   r;   r   r1   s   `  r    testNoNotificationCHalfCloseNoNotificationAndShutdownExceptionTests.testNoNotificationc  s     	##H-113161AA*Q*/..*::"R	TU	KL""A7++r#   c                   ^  T R                   R                  R                  R                  5         T R                  R                  R                  S5        T R                  R                  R                  5         [        R                  " 5       =T R                   R                  l	        n[        R                  " 5       =T R                  l	        nUR                  U 4S j5        [        R                  " X/5      $ )z`
If the other side has already closed its connection,
loseWriteConnection should pass silently.
r  c                 b   > TR                  TR                  R                  R                  5      $ r   r?  r  s    r    r  XHalfCloseNoNotificationAndShutdownExceptionTests.testShutdownException.<locals>.<lambda>|  r@  r#   )r   ra   r8   r9   r   r  r  r   r*   rl   r,   r   rA  s   `  r    testShutdownExceptionFHalfCloseNoNotificationAndShutdownExceptionTests.testShutdownExceptionr  s    
 	%%446##D)113161AA*Q*/..*::"R	KL""A7++r#   )r   r   ro   N)
rG   rH   rI   rJ   r  r5  r  rB  rG  rK   rE   r#   r    r3  r3  K  s    
-&,,r#   r3  c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
HalfCloseBuggyApplicationTestsi  zA
Test half-closing connections where notification code has bugs.
c                 B  ^  [        5       T l        [        R                  " 5       T R                  l        [
        R                  " ST R                  SS9T l        T R                  T R                  R                  5        T R                  R                  5       n[        R                  " [
        [        5      nUR                  UR                  UR                  5      nU 4S jnUR!                  U5        [        R"                  " T R                  R                  U/5      $ )zd
Set up a server and connect a client to it.  Return a Deferred which
only fires once this is done.
r   r   r   c                    > U Tl         g r   )r  )r  r;   s    r    	setClient7HalfCloseBuggyApplicationTests.setUp.<locals>.setClient  s    "0Dr#   )r  r   r   r*   r_   r   r   rO   r   rQ   r   r   rQ  r  r   r  r,   r   )r;   rn   creatorr  rM  s   `    r    r  $HalfCloseBuggyApplicationTests.setUp  s    
 )]49NN4D1%%a););{S			//0yy  "((,? ++DIItyyA	1 	""9-""66G
 	
r#   c                     [        S5      e)zH
Fake implementation of a callback which illegally raises an
exception.
zONO I AM BUGGY CODEr   )r;   r  s     r    aBug#HalfCloseBuggyApplicationTests.aBug  s    
 011r#   c                    ^  [         R                  " 5       =nT R                  l        T R                  R                  R                  5         U 4S jnUR                  U5        U$ )zf
Helper for testing that an exception is logged by the time the
client protocol loses its connection.
c                 f   > TR                  [        5      nTR                  [        U5      S5        g r|   )flushLoggedErrorsr   r   r  )r   errorsr;   s     r    r!   EHalfCloseBuggyApplicationTests._notificationRaisesTest.<locals>.check  s'    ++L9FS[!,r#   )r   r*   r  rl   r8   r  r,   )r;   r  r!   s   `  r    _notificationRaisesTest6HalfCloseBuggyApplicationTests._notificationRaisesTest  sQ    
 7<nn6FF$$3%%99;	- 	5!r#   c                 l    U R                   U R                  R                  l        U R	                  5       $ )z
If C{readConnectionLost} raises an exception when the transport
calls it to notify the protocol of that event, the exception should
be logged and the protocol should be disconnected completely.
)rR  r   ra   r  rY  r:   s    r    test_readNotificationRaises:HalfCloseBuggyApplicationTests.test_readNotificationRaises  s,     >BYY'':++--r#   c                 X    U R                   U R                  l        U R                  5       $ )z
If C{writeConnectionLost} raises an exception when the transport
calls it to notify the protocol of that event, the exception should
be logged and the protocol should be disconnected completely.
)rR  r  r  rY  r:   s    r    test_writeNotificationRaises;HalfCloseBuggyApplicationTests.test_writeNotificationRaises  s%     37))/++--r#   )r  rO   r   N)rG   rH   rI   rJ   rY   r  rR  rY  r\  r_  rK   rE   r#   r    rJ  rJ    s     
*2..r#   rJ  c                       \ rS rSrSrS rSrg)LogTestsi  z,
Test logging facility of TCP base classes.
c                 2  ^ ^ [        5       n[        5       n[        R                  " 5       Ul        [
        R                  " SUSS9nT R                  UR                  5        [
        R                  " UR                  5       R                  UR                  5       R                  U5      mT R                  TR                  5        T R                  TR                  R                   S5        UU 4S jnUR                  R#                  U5        UR                  $ )z`
Check that the log customization of the client transport happens
once the client is connected.
r   r   r   Uninitializedc                 R   > TR                  TR                  R                  S5        g )NzAccumulatingProtocol,client)r   r8   logstr)r   r   r;   s    r    cb+LogTests.test_logstrClientSetup.<locals>.cb  s     Y00779VWr#   )rr   ru   r   r*   r_   r   r   r   rQ   r   r   r  rO   r   r   r8   rf  r,   )r;   r   r   rO   rg  r   s   `    @r    test_logstrClientSetupLogTests.test_logstrClientSetup  s    
 !" "(-(8%  FkB**+&&t||~':':DLLN<O<OQWX		,,- 	,,33_E	X 	%%11"5,,,r#   rE   N)rG   rH   rI   rJ   rY   ri  rK   rE   r#   r    rb  rb    s    -r#   rb  c                   r    \ rS rSrSr\" \R                  R                  \	5      (       + S5      S 5       r
Srg)PauseProducingTestsi  z?
Test some behaviors of pausing the production of a transport.
z#Reactor not providing IReactorFDSetc                   ^ ^ [        5       n[        5       n[        R                  " 5       Ul        [
        R                  " SUSS9nT R                  UR                  5        [
        R                  " UR                  5       R                  UR                  5       R                  U5      nT R                  UR                  5        UU 4S jnU 4S jmUR                  R                  U5        UR                  $ )z
In C{connectionMade} of a client protocol, C{pauseProducing} used to be
ignored: this test is here to ensure it's not ignored.
r   r   r   c                   > U R                   nTR                  U[        R                  " 5       [        R                  " 5       -   5        U R                   R                  5         TR                  U[        R                  " 5       [        R                  " 5       -   5        [        R                  " 5       nUR                  T5        [        R                  " SUR                  U 5        U$ r  )r8   assertInr   
getReaders
getWritersr/  assertNotInr   r*   r,   r  r   )r4  trr   checkAfterConnectionMader;   s      r    checkInConnectionMadeVPauseProducingTests.test_pauseProducingInConnectionMade.<locals>.checkInConnectionMade  s    BMM"g002W5G5G5IIJOO**,R!3!3!58J8J8L!LM AMM23aU3Hr#   c                    > U R                   nTR                  U[        R                  " 5       [        R                  " 5       -   5        g r   )r8   rr  r   rp  rq  )r4  rs  r;   s     r    rt  YPauseProducingTests.test_pauseProducingInConnectionMade.<locals>.checkAfterConnectionMade  s3    BR!3!3!58J8J8L!LMr#   )rr   ru   r   r*   r_   r   r   r   rQ   r   r   r  rO   r   r,   )r;   r   r   rO   r   ru  rt  s   `     @r    #test_pauseProducingInConnectionMade7PauseProducingTests.test_pauseProducingInConnectionMade  s     !" "(-(8%  FkB**+&&t||~':':DLLN<O<OQWX		,,-			N
 	%%112GH,,,r#   rE   N)rG   rH   rI   rJ   rY   r   r
   IReactorFDSetr   r   ry  rK   rE   r#   r    rl  rl    s:     $$//88-!-	!-r#   rl  c                       \ rS rSrSrS rSrg)CallBackOrderTestsi  z%
Test the order of reactor callbacks
c                 (  ^ ^	 [        5       n[        R                  " 5       R                  U 4S j5      Ul        [        5       m	[        R                  " 5       T	l        [        R                  " 5       T	l        U	4S jnT	R                  R                  U5        [        R                  " SUSS9nT R                  UR                  5        [        R                  " UR                  5       R                  UR                  5       R                  T	5      nT R                  UR                  5        S nS nU 4S jn[        R                   " T	R                  R                  U5      T	R"                  R                  U5      /5      nUR                  U5      $ )	zT
Check that Protocol.connectionLost is called before factory's
clientConnectionLost
c                 N   > TR                  U R                  R                  5      $ r   )r   r8   r9   )r4  r;   s    r    r  3CallBackOrderTests.test_loseOrder.<locals>.<lambda>  s    $//%//*H*HIr#   c                 p   > [         R                  " STR                  R                  R                  5        g)z"
protocol.connectionMade callback
r   N)r   r  ra   r8   r9   )r   r   s    r    _cbCM0CallBackOrderTests.test_loseOrder.<locals>._cbCM"  s'     a!4!4!>!>!M!MNr#   r   r   r   c                     g)z'
factory.clientConnectionLost callback
CCLrE   r   s    r    _cbCCL1CallBackOrderTests.test_loseOrder.<locals>._cbCCL0  s     r#   c                     g)z"
protocol.connectionLost callback
CLrE   r  s    r    _cbCL0CallBackOrderTests.test_loseOrder.<locals>._cbCL6  s     r#   c                 .   > TR                  U SS/5        g )Nr  r  )r   )r   r;   s    r    	_cbGather4CallBackOrderTests.test_loseOrder.<locals>._cbGather<  s    S4-0r#   )rr   r   r*   r,   r_   ru   r`   r   r   r   rQ   r   r   r  rO   r   r   rw   )
r;   r   r  rO   r   r  r  r  r   r   s
   `        @r    test_loseOrder!CallBackOrderTests.test_loseOrder  s0   
 !"(-(8(D(DI)
% !"(-(8%(-(8%	O 	%%11%8  FkB**+&&t||~':':DLLN<O<OQWX		,,-			1 --99%@++F3
 }}Y''r#   rE   N)rG   rH   rI   rJ   rY   r  rK   rE   r#   r    r}  r}    s    1(r#   r}  r/  numberRounds)r   )XrY   rB  rO  rL  	functoolsr   typingr   unittestr   zope.interfacer   rn  r)   r   r	   r
   r   r   twisted.internet.addressr   twisted.internet.interfacesr   r   twisted.internet.testingr   twisted.protocolsr   twisted.python.logr   r   twisted.python.runtimer   twisted.trial.unittestr   r   r2   rR  r4   r7  rM   r]   rr   r  ru   r   r   r  rn  rx  r  r  r  r  r  r  r  r<  rD  rL  rU  r\  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r3  rJ  rb  rl  r}  resource	getrlimitRLIMIT_NOFILE	numRoundssetattrImportErrorrE   r#   r    <module>r     s  
       &  H H 0 M 9 & ' + 58*h'' *"X++ "04 4n,h.D.D ,h.D.D 8DCX DCNaH aHx'' (_ ((>28 >2Blh l^'h 'T(X&& (.X&& 	(00 	oNX oNd1x00 18$$ -h&7&7 -*A: A p pfh(? 6
100 
1-8++ -`
8 `
F) 1 1 )" 1 1 	X%;%; 	-(** -)11 )#$x #$L #$&' & %&"	((( 	VX Vr2,x 2,j@.X @.F-x ->*-( *-Z6( 6(r@ ""8#9#9:1=BI#^Y?	  		s   &L L L 