
    h                     8   S r SSKrSSKrSSKrSSKJr  SSKJrJrJ	r	J
r
JrJrJr  SSKJrJr  SSKJr  SSKJr  SSKJr   SS	KJr  \r\(       a  \R4                  (       d  Sr " S
 S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rSr  " S S\RB                  5      r"\" \RF                  " \S5      (       + S5       " S S\5      5       r$\" \RJ                  " \S5      (       + S5       " S S\5      5       r& " S S\RN                  5      r( " S S \RR                  5      r* " S! S"\5      r+ " S# S$5      r, " S% S&\RZ                  5      r. " S' S(\,5      r/ " S) S*\5      r0 " S+ S,\5      r1 " S- S.\5      r2g! \ a    Sr GNNf = f)/zB
Tests for lots of functionality provided by L{twisted.internet}.
    N)skipIf)abstractbasedefererror
interfacesprotocolreactor)Deferredpassthru)	Connector)util)TestCase)sslc                       \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rS rS rS rS rS rSrg)ThreePhaseEventTests   zI
Tests for the private implementation helpers for system event triggers.
c                 f    S U l         [        5       U l        [        R                  " 5       U l        g)zB
Create a trigger, an argument, and an event to be used by tests.
c                     g N )xs    a/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/test/test_internet.py<lambda>,ThreePhaseEventTests.setUp.<locals>.<lambda>(   s        N)triggerobjectargr   _ThreePhaseEventeventselfs    r   setUpThreePhaseEventTests.setUp$   s$     &8**,
r   c                     U R                  [        U R                  R                  SU R                  U R
                  5        g)z\
L{_ThreePhaseEvent.addTrigger} should raise L{KeyError} when called
with an invalid phase.
xxxN)assertRaisesKeyErrorr!   
addTriggerr   r   r"   s    r   test_addInvalidPhase)ThreePhaseEventTests.test_addInvalidPhase,   s/    
 	djj++UDLL$((	
r   c                     U R                   R                  SU R                  U R                  5        U R	                  U R                   R
                  U R                  U R                  40 4/5        g)z
L{_ThreePhaseEvent.addTrigger} should accept C{'before'} as a phase, a
callable, and some arguments and add the callable with the arguments to
the before list.
beforeN)r!   r*   r   r   assertEqualr.   r"   s    r   test_addBeforeTrigger*ThreePhaseEventTests.test_addBeforeTrigger5   P     	

hdhh?**dllTXXK-L,MNr   c                     U R                   R                  SU R                  U R                  5        U R	                  U R                   R
                  U R                  U R                  40 4/5        g)z
L{_ThreePhaseEvent.addTrigger} should accept C{'during'} as a phase, a
callable, and some arguments and add the callable with the arguments to
the during list.
duringN)r!   r*   r   r   r/   r4   r"   s    r   test_addDuringTrigger*ThreePhaseEventTests.test_addDuringTrigger>   r2   r   c                     U R                   R                  SU R                  U R                  5        U R	                  U R                   R
                  U R                  U R                  40 4/5        g)z
L{_ThreePhaseEvent.addTrigger} should accept C{'after'} as a phase, a
callable, and some arguments and add the callable with the arguments to
the after list.
afterN)r!   r*   r   r   r/   r8   r"   s    r   test_addAfterTrigger)ThreePhaseEventTests.test_addAfterTriggerG   sP     	

gt||TXX>))T\\DHH;,K+LMr   c                     U R                   R                  SU R                  U R                  5      nU R                   R	                  U5        U R                  U R                   R                  / 5        g)z
L{_ThreePhaseEvent.removeTrigger} should accept an opaque object
previously returned by L{_ThreePhaseEvent.addTrigger} and remove the
associated trigger.
r.   N)r!   r*   r   r   removeTriggerr/   r.   r#   handles     r   test_removeTrigger'ThreePhaseEventTests.test_removeTriggerP   sP     &&xtxxH

  (**B/r   c                 j    U R                  [        U R                  R                  [	        5       5        g)z
L{_ThreePhaseEvent.removeTrigger} should raise L{ValueError} when given
an object not previously returned by L{_ThreePhaseEvent.addTrigger}.
N)r(   
ValueErrorr!   r<   r   r"   s    r   test_removeNonexistentTrigger2ThreePhaseEventTests.test_removeNonexistentTriggerZ   s"    
 	*djj&>&>Ir   c                     U R                   R                  SU R                  U R                  5      nU R                   R	                  U5        U R                  [        U R                   R                  U5        g)z
L{_ThreePhaseEvent.removeTrigger} should raise L{ValueError} the second
time it is called with an object returned by
L{_ThreePhaseEvent.addTrigger}.
r.   N)r!   r*   r   r   r<   r(   rB   r=   s     r   test_removeRemovedTrigger.ThreePhaseEventTests.test_removeRemovedTriggera   sR     &&xtxxH

  (*djj&>&>Gr   c                     U R                  [        U R                  R                  SU R                  U R
                  40 45        g)z
L{_ThreePhaseEvent.removeTrigger} should raise L{ValueError} if it is
given a trigger handle which resembles a valid trigger handle aside
from its phase being incorrect.
r'   N)r(   r)   r!   r<   r   r   r"   s    r   test_removeAlmostValidTrigger2ThreePhaseEventTests.test_removeAlmostValidTriggerk   s7     	djj..txxkSU0V	
r   c                 \   / nU R                   R                  SUR                  S5        U R                   R                  SUR                  S5        U R                   R                  SUR                  S5        U R                   R                  SUR                  S5        U R                   R                  SUR                  S5        U R                   R                  SUR                  S	5        U R                  U/ 5        U R                   R	                  5         U R                  U/ S
Q5        g)zl
L{_ThreePhaseEvent.fireEvent} should call I{before}, I{during}, and
I{after} phase triggers in that order.
r8   firstr8   r4   rM   r4   r.   rM   r.   secondr.   rQ   r4   rQ   r8   )rO   rP   rN   rR   rL   rS   N)r!   r*   appendr/   	fireEventr#   eventss     r   test_fireEvent#ThreePhaseEventTests.test_fireEventu   s    
 

gv}}6HI

h7JK

h7JK

h7KL

h7KL

gv}}6IJ$


	
r   c                   ^ / n[        5       mU R                  R                  SU4S j5        U R                  R                  SUR                  S5        U R                  R                  SUR                  S5        U R	                  U/ 5        U R                  R                  5         U R	                  U/ 5        TR                  S5        U R	                  USS/5        g)z
L{_ThreePhaseEvent.fireEvent} should wait for any L{Deferred} returned
by a I{before} phase trigger before proceeding to I{during} events.
r.   c                     > T $ r   r   beforeResults   r   r   >ThreePhaseEventTests.test_asynchronousBefore.<locals>.<lambda>       r   r4   r8   N)r   r!   r*   rT   r/   rU   callback)r#   rW   r]   s     @r   test_asynchronousBefore,ThreePhaseEventTests.test_asynchronousBefore   s    
 z

h(<=

hx@

gv}}g>$

$d#(G!45r   c                   ^ / n " S S[         5      mU4S jnU R                  R                  SU5        U R                  R                  SUR                  S5        U R                  R                  SUR                  S5        U R                  R	                  5         U R                  USS/5        U R                  T5      nU R                  [        U5      S5        g)zy
If a before-phase trigger raises a synchronous exception, it should be
logged and the remaining triggers should be run.
c                       \ rS rSrSrg)HThreePhaseEventTests.test_beforeTriggerException.<locals>.DummyException   r   N__name__
__module____qualname____firstlineno____static_attributes__r   r   r   DummyExceptionre          r   rm   c                     > T " 5       er   r   rm   s   r   raisingTriggerHThreePhaseEventTests.test_beforeTriggerException.<locals>.raisingTrigger        ""r   r.   r4      N	Exceptionr!   r*   rT   rU   r/   flushLoggedErrorslenr#   rW   rq   errorsrm   s       @r   test_beforeTriggerException0ThreePhaseEventTests.test_beforeTriggerException   s    
 	Y 		# 	

h7

hx@

hx@

(H!56''7Va(r   c                   ^ / n " S S[         5      mU4S jnU R                  R                  SU5        U R                  R                  SUR                  S5        U R                  R                  SUR                  S5        U R                  R	                  5         U R                  USS/5        U R                  T5      nU R                  [        U5      S5        g)zy
If a during-phase trigger raises a synchronous exception, it should be
logged and the remaining triggers should be run.
c                       \ rS rSrSrg)HThreePhaseEventTests.test_duringTriggerException.<locals>.DummyException   r   Nrg   r   r   r   rm   r      rn   r   rm   c                     > T " 5       er   r   rp   s   r   rq   HThreePhaseEventTests.test_duringTriggerException.<locals>.raisingTrigger   rs   r   r4   r8   rt   Nru   ry   s       @r   test_duringTriggerException0ThreePhaseEventTests.test_duringTriggerException   s    
 	Y 		# 	

h7

hx@

gv}}g>

(G!45''7Va(r   c                 v  ^ ^ / nUU 4S jnT R                   R                  SUR                  S5      mT R                   R                  SU5        T R                   R                  SUR                  S5        T R                  [        S[
        T R                   R                  5        T R                  USS/5        g)z|
If a before-phase trigger tries to remove another before-phase trigger
which has already run, a warning should be emitted.
c                  <   > TR                   R                  T 5        g r   r!   r<   beforeHandler#   s   r   r<   WThreePhaseEventTests.test_synchronousRemoveAlreadyExecutedBefore.<locals>.removeTrigger   s    JJ$$\2r   r.   rO   rP   zdRemoving already-fired system event triggers will raise an exception in a future version of Twisted.N)r!   r*   rT   assertWarnsDeprecationWarning__file__rU   r/   )r#   rW   r<   r   s   `  @r   +test_synchronousRemoveAlreadyExecutedBefore@ThreePhaseEventTests.test_synchronousRemoveAlreadyExecutedBefore   s    
 	3 zz,,fmm%8
 	

h6

h7KL8JJ  	
 	"57K!LMr   c                 D  ^ ^ / nT R                   R                  SUU 4S j5        T R                   R                  SUR                  S5      mT R                   R                  SUR                  S5        T R                   R                  5         T R	                  US/5        g)z~
If a before-phase trigger removes another before-phase trigger which
has not yet run, the removed trigger should not be run.
r.   c                  :   > TR                   R                  T 5      $ r   r   r   s   r   r   JThreePhaseEventTests.test_synchronousRemovePendingBefore.<locals>.<lambda>       

0H0H0Vr   rO   rP   Nr!   r*   rT   rU   r/   )r#   rW   r   s   ` @r   #test_synchronousRemovePendingBefore8ThreePhaseEventTests.test_synchronousRemovePendingBefore       
 

h(VWzz,,fmm%8
 	

h7KL

"6!78r   c                 D  ^ ^ / nT R                   R                  SUU 4S j5        T R                   R                  SUR                  S5      mT R                   R                  SUR                  S5        T R                   R                  5         T R	                  US/5        g)zg
If a before-phase trigger removes a during-phase trigger, the
during-phase trigger should not be run.
r.   c                  :   > TR                   R                  T 5      $ r   r   duringHandler#   s   r   r   JThreePhaseEventTests.test_synchronousBeforeRemovesDuring.<locals>.<lambda>   r   r   r4   r8   Nr   r#   rW   r   s   ` @r   #test_synchronousBeforeRemovesDuring8ThreePhaseEventTests.test_synchronousBeforeRemovesDuring   sw    
 

h(VWzz,,Xv}}hO

gv}}g>

'+r   c                   ^ / n[        5       mU R                  R                  SU4S j5        U R                  R                  SUR                  S5      nU R                  R                  SUR                  S5        U R                  R	                  5         U R                  R                  U5        TR                  S5        U R                  US/5        g)z
If a before-phase trigger returns a L{Deferred} and later removes a
during-phase trigger before the L{Deferred} fires, the during-phase
trigger should not be run.
r.   c                     > T $ r   r   r\   s   r   r   KThreePhaseEventTests.test_asynchronousBeforeRemovesDuring.<locals>.<lambda>  r_   r   r4   r8   N)r   r!   r*   rT   rU   r<   r`   r/   )r#   rW   r   r]   s      @r   $test_asynchronousBeforeRemovesDuring9ThreePhaseEventTests.test_asynchronousBeforeRemovesDuring   s     z

h(<=zz,,Xv}}hO

gv}}g>



  .d#'+r   c                 &  ^ ^^ / mU4S jnT R                   R                  SU5        T R                   R                  SUU 4S j5        T R                   R                  SU5      mT R                   R                  5         T R                  TS/5        g)z
If a before-phase trigger removes a during-phase trigger which is
identical to an already-executed before-phase trigger aside from their
phases, no warning should be emitted and the during-phase trigger
should not be run.
c                  (   > T R                  S5        g )Nr   rT   rW   s   r   r   ]ThreePhaseEventTests.test_synchronousBeforeRemovesConspicuouslySimilarDuring.<locals>.trigger  s    MM)$r   r.   c                  :   > TR                   R                  T 5      $ r   r   )duringTriggerr#   s   r   r   ^ThreePhaseEventTests.test_synchronousBeforeRemovesConspicuouslySimilarDuring.<locals>.<lambda>  s    

0H0H0Wr   r4   r   N)r!   r*   rU   r/   )r#   r   r   rW   s   ` @@r   7test_synchronousBeforeRemovesConspicuouslySimilarDuringLThreePhaseEventTests.test_synchronousBeforeRemovesConspicuouslySimilarDuring  sr     	% 	

h0

h(WX

--h@

)-r   c                 D  ^ ^ / nT R                   R                  SUU 4S j5        T R                   R                  SUR                  S5      mT R                   R                  SUR                  S5        T R                   R                  5         T R	                  US/5        g)z~
If a during-phase trigger removes another during-phase trigger which
has not yet run, the removed trigger should not be run.
r4   c                  :   > TR                   R                  T 5      $ r   r   r   s   r   r   JThreePhaseEventTests.test_synchronousRemovePendingDuring.<locals>.<lambda>%  r   r   rN   rR   Nr   r   s   ` @r   #test_synchronousRemovePendingDuring8ThreePhaseEventTests.test_synchronousRemovePendingDuring  r   r   c                    / nU R                   R                  SUR                  S5        U R                   R                  SUR                  S5        U R                   R                  SUR                  S5        U R                   R                  5         U R                   R                  5         U R	                  U/ SQ5        g)zU
A trigger should only be called on the first call to
L{_ThreePhaseEvent.fireEvent}.
r.   r4   r8   )r.   r4   r8   Nr   rV   s     r   test_triggersRunOnce)ThreePhaseEventTests.test_triggersRunOnce-  s    
 

hx@

hx@

gv}}g>



!>?r   c                    ^ ^ / mUU 4S jnT R                   R                  STR                  S5        T R                   R                  SU5        T R                   R                  5         T R	                  TSS/5        g)z
The temporary list L{_ThreePhaseEvent.finishedBefore} should be emptied
and the state reset to C{'BASE'} before the first during-phase trigger
executes.
c                     > T R                  S5        TR                  TR                  R                  / 5        TR                  TR                  R                  S5        g )Nr4   BASE)rT   r/   r!   finishedBeforestate)rW   r#   s   r   r   NThreePhaseEventTests.test_finishedBeforeTriggersCleared.<locals>.duringTriggerB  sC    MM(#TZZ66;TZZ--v6r   r.   r4   Nr   )r#   r   rW   s   ` @r   "test_finishedBeforeTriggersCleared7ThreePhaseEventTests.test_finishedBeforeTriggersCleared:  sd     	7
 	

hx@

h6

(H!56r   )r   r!   r   N)rh   ri   rj   rk   __doc__r$   r+   r0   r5   r9   r?   rC   rF   rI   rX   ra   r{   r   r   r   r   r   r   r   r   r   rl   r   r   r   r   r      sw    -
OON0JH

66")*)*N09
,, .$9@7r   r   c                   ~    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rS rSrg)SystemEventTestsiM  a  
Tests for the reactor's implementation of the C{fireSystemEvent},
C{addSystemEventTrigger}, and C{removeSystemEventTrigger} methods of the
L{IReactorCore} interface.

@ivar triggers: A list of the handles to triggers which have been added to
    the reactor.
c                     / U l         g)z9
Create an empty list in which to store trigger handles.
Ntriggersr"   s    r   r$   SystemEventTests.setUpW  s     r   c                     U R                   (       aE  U R                   R                  5       n [        R                  " U5        U R                   (       a  MD  gg! [        [
        4 a     N'f = f)z1
Remove all remaining triggers from the reactor.
N)r   popr
   removeSystemEventTriggerrB   r)   r#   r   s     r   tearDownSystemEventTests.tearDown]  sT     mmmm'')G009 mmm ) s   A A+*A+c                 j    [         R                  " XU5      nU R                  R                  U5        U$ )zC
Add a trigger to the reactor and remember it in C{self.triggers}.
)r
   addSystemEventTriggerr   rT   )r#   r!   phasefuncts        r   r*   SystemEventTests.addTriggerh  s-     ))%=Qr   c                 f    [         R                  " U5        U R                  R                  U5        g)zL
Remove a trigger by its handle from the reactor and from
C{self.triggers}.
N)r
   r   r   remover   s     r   r<   SystemEventTests.removeTriggerp  s$    
 	((1W%r   c                    ^ Sn/ mU4S jnU R                  XU5        U R                  T/ 5        [        R                  " U5        U R                  TS /5        g )Ntestc                  (   > T R                  S 5        g r   r   r   s   r   r   <SystemEventTests._addSystemEventTriggerTest.<locals>.trigger|      MM$r   r*   r/   r
   fireSystemEvent)r#   r   	eventTyper   rW   s       @r   _addSystemEventTriggerTest+SystemEventTests._addSystemEventTriggerTestx  sR    		  	'2$	*$(r   c                 &    U R                  S5        g)z
L{IReactorCore.addSystemEventTrigger} should accept the C{'before'}
phase and not call the given object until the right event is fired.
r.   Nr   r"   s    r   test_beforePhase!SystemEventTests.test_beforePhase      
 	''1r   c                 &    U R                  S5        g)z
L{IReactorCore.addSystemEventTrigger} should accept the C{'during'}
phase and not call the given object until the right event is fired.
r4   Nr   r"   s    r   test_duringPhase!SystemEventTests.test_duringPhase  r   r   c                 &    U R                  S5        g)z
L{IReactorCore.addSystemEventTrigger} should accept the C{'after'}
phase and not call the given object until the right event is fired.
r8   Nr   r"   s    r   test_afterPhase SystemEventTests.test_afterPhase  s    
 	''0r   c                 P    SnU R                  [        U R                  SUS 5        g)zp
L{IReactorCore.addSystemEventTrigger} should reject phases other than
C{'before'}, C{'during'}, or C{'after'}.
r   r'   c                      g r   r   r   r   r   r   4SystemEventTests.test_unknownPhase.<locals>.<lambda>  s    tr   N)r(   r)   r*   )r#   r   s     r   test_unknownPhase"SystemEventTests.test_unknownPhase  s#    
 	(DOOUI|Tr   c                    ^ Sn/ mU4S jnU4S jnU R                  SX5        U R                  SX5        U R                  T/ 5        [        R                  " U5        U R                  TSS/5        g)z
L{IReactorCore.addSystemEventTrigger} should call triggers added to the
C{'before'} phase before it calls triggers added to the C{'during'}
phase.
r   c                  (   > T R                  S5        g Nr.   r   r   s   r   beforeTriggerASystemEventTests.test_beforePreceedsDuring.<locals>.beforeTrigger      MM(#r   c                  (   > T R                  S5        g Nr4   r   r   s   r   r   ASystemEventTests.test_beforePreceedsDuring.<locals>.duringTrigger  r   r   r.   r4   Nr   )r#   r   r   r   rW   s       @r   test_beforePreceedsDuring*SystemEventTests.test_beforePreceedsDuring  sj     		$	$ 	););$	*(H!56r   c                    ^ Sn/ mU4S jnU4S jnU R                  SX5        U R                  SX5        U R                  T/ 5        [        R                  " U5        U R                  TSS/5        g)z
L{IReactorCore.addSystemEventTrigger} should call triggers added to the
C{'during'} phase before it calls triggers added to the C{'after'}
phase.
r   c                  (   > T R                  S5        g r   r   r   s   r   r   @SystemEventTests.test_duringPreceedsAfter.<locals>.duringTrigger  r   r   c                  (   > T R                  S5        g )Nr8   r   r   s   r   afterTrigger?SystemEventTests.test_duringPreceedsAfter.<locals>.afterTrigger      MM'"r   r4   r8   Nr   )r#   r   r   r   rW   s       @r   test_duringPreceedsAfter)SystemEventTests.test_duringPreceedsAfter  sj     		$	# 	);9$	*(G!45r   c                 @  ^^ [        5       mSn/ mU4S jnU4S jnU R                  SX5        U R                  SX5        U R                  T/ 5        [        R                  " U5        U R                  T/ 5        TR                  S5        U R                  TS/5        g)z
If a trigger added to the C{'before'} phase of an event returns a
L{Deferred}, the C{'during'} phase should be delayed until it is called
back.
r   c                     > T $ r   r   )triggerDeferreds   r   r   BSystemEventTests.test_beforeReturnsDeferred.<locals>.beforeTrigger  s	    ""r   c                  (   > T R                  S5        g r   r   r   s   r   r   BSystemEventTests.test_beforeReturnsDeferred.<locals>.duringTrigger  r   r   r.   r4   Nr   r*   r/   r
   r   r`   )r#   r   r   r   rW   r  s       @@r   test_beforeReturnsDeferred+SystemEventTests.test_beforeReturnsDeferred  s     #*		#	$ 	););$	*$  &(,r   c                   ^^^ [        5       m[        5       mSn/ mU4S jnU4S jnU4S jnU R                  SX5        U R                  SX5        U R                  SX5        U R                  T/ 5        [        R                  " U5        U R                  T/ 5        TR                  S5        U R                  T/ 5        TR                  S5        U R                  TS/5        g)z
If more than one trigger added to the C{'before'} phase of an event
return L{Deferred}s, the C{'during'} phase should be delayed until they
are all called back.
r   c                     > T $ r   r   firstDeferreds   r   firstBeforeTriggerNSystemEventTests.test_multipleBeforeReturnDeferred.<locals>.firstBeforeTrigger  	      r   c                     > T $ r   r   )secondDeferreds   r   secondBeforeTriggerOSystemEventTests.test_multipleBeforeReturnDeferred.<locals>.secondBeforeTrigger  s	    !!r   c                  (   > T R                  S5        g r   r   r   s   r   r   ISystemEventTests.test_multipleBeforeReturnDeferred.<locals>.duringTrigger  r   r   r.   r4   Nr	  )r#   r   r  r  r   rW   r  r  s        @@@r   !test_multipleBeforeReturnDeferred2SystemEventTests.test_multipleBeforeReturnDeferred  s     !
!		!	"	$ 	)@)A);$	*$t$$%(,r   c                   ^^^ Sn/ m[        5       m[        5       mU4S jnU4S jnUU4S jnU4S jnU R                  SX5        U R                  SX5        U R                  SX5        U R                  SX5        U R                  T/ 5        [        R                  " U5        U R                  TS/5        TR                  S5        U R                  TSS/5        g)	a1  
If a trigger added to the C{'before'} phase of an event calls back a
L{Deferred} returned by an earlier trigger in the C{'before'} phase of
the same event, the remaining C{'before'} triggers for that event
should be run and any further L{Deferred}s waited on before proceeding
to the C{'during'} events.
r   c                     > T $ r   r   r  s   r   r  aSystemEventTests.test_subsequentBeforeTriggerFiresPriorBeforeDeferred.<locals>.firstBeforeTrigger  r  r   c                  (   > T R                  S 5        g r   )r`   r  s   r   r  bSystemEventTests.test_subsequentBeforeTriggerFiresPriorBeforeDeferred.<locals>.secondBeforeTrigger  s    ""4(r   c                  *   > T R                  S5        T$ r   r   rW   r  s   r   thirdBeforeTriggeraSystemEventTests.test_subsequentBeforeTriggerFiresPriorBeforeDeferred.<locals>.thirdBeforeTrigger  s    MM(#!!r   c                  (   > T R                  S5        g r   r   r   s   r   r   \SystemEventTests.test_subsequentBeforeTriggerFiresPriorBeforeDeferred.<locals>.duringTrigger  r   r   r.   r4   Nr	  )	r#   r   r  r  r"  r   rW   r  r  s	         @@@r   4test_subsequentBeforeTriggerFiresPriorBeforeDeferredESystemEventTests.test_subsequentBeforeTriggerFiresPriorBeforeDeferred  s     	 
!	!	)	"	$ 	)@)A)@);$	*(,%(H!56r   c                   ^ Sn/ mU4S jnU4S jnU R                  SX5        U R                  U R                  SX5      5        U R                  T/ 5        [        R                  " U5        U R                  TS/5        g)zl
A trigger removed with L{IReactorCore.removeSystemEventTrigger} should
not be called when the event fires.
r   c                  (   > T R                  S5        g )NrM   r   r   s   r   r  JSystemEventTests.test_removeSystemEventTrigger.<locals>.firstBeforeTrigger.  r   r   c                  (   > T R                  S5        g )NrQ   r   r   s   r   r  KSystemEventTests.test_removeSystemEventTrigger.<locals>.secondBeforeTrigger1  r   r   r.   rM   N)r*   r<   r/   r
   r   )r#   r   r  r  rW   s       @r   test_removeSystemEventTrigger.SystemEventTests.test_removeSystemEventTrigger&  sr    
 		#	$ 	)@4??8YTU$	*'+r   c           	      H   U R                  SSS 5      nU R                  U5        U R                  [        [        R
                  S5        U R                  [        [        R
                  U5        U R                  [        [        R
                  US   SUS   SS -   45        g)a  
Passing an object to L{IReactorCore.removeSystemEventTrigger} which was
not returned by a previous call to
L{IReactorCore.addSystemEventTrigger} or which has already been passed
to C{removeSystemEventTrigger} should result in L{TypeError},
L{KeyError}, or L{ValueError} being raised.
r4   r   c                      g r   r   r   r   r   r   KSystemEventTests.test_removeNonExistentSystemEventTrigger.<locals>.<lambda>B  s    dr   Nr   )r'   rt   )r*   r<   r(   	TypeErrorr
   r   rB   r)   )r#   bs     r   (test_removeNonExistentSystemEventTrigger9SystemEventTests.test_removeNonExistentSystemEventTrigger:  s     OOHfl;1)W%E%EtL*g&F&FJg661x!A$qr(?R8S	
r   c                 `  ^^^	 / mSn[        5       mUU4S jnU4S jnSn[        5       m	UU	4S jnU4S jnU R                  SX5        U R                  SX5        U R                  SXE5        U R                  SXF5        U R                  T/ 5        [        R                  " U5        U R                  TS	/5        [        R                  " U5        U R                  TS	S
/5        TR                  S5        U R                  T/ SQ5        T	R                  S5        U R                  T/ SQ5        g)z
L{IReactorCore.fireSystemEvent} should behave the same way for a
particular system event regardless of whether Deferreds are being
waited on for a different system event.
zfirst-eventc                  *   > T R                  S5        T$ )Nr.   rM   r   )rW   r  s   r   beforeFirstEventQSystemEventTests.test_interactionBetweenDifferentEvents.<locals>.beforeFirstEventU  s    MM-.  r   c                  (   > T R                  S5        g )Nr8   rM   r   r   s   r   afterFirstEventPSystemEventTests.test_interactionBetweenDifferentEvents.<locals>.afterFirstEventY  s    MM,-r   zsecond-eventc                  *   > T R                  S5        T$ )Nr.   rQ   r   r!  s   r   beforeSecondEventRSystemEventTests.test_interactionBetweenDifferentEvents.<locals>.beforeSecondEvent_  s    MM./!!r   c                  (   > T R                  S5        g )Nr8   rQ   r   r   s   r   afterSecondEventQSystemEventTests.test_interactionBetweenDifferentEvents.<locals>.afterSecondEventc  s    MM-.r   r.   r8   r8  r@  N)r8  r@  r<  )r8  r@  r<  rD  r	  )
r#   
firstEventr9  r=  secondEventrA  rE  rW   r  r  s
          @@@r   &test_interactionBetweenDifferentEvents7SystemEventTests.test_interactionBetweenDifferentEventsJ  s    "
 
	!	. %!	"	/ 	*?=+A?$ 	
+"5!67 	,"57K!LM 	t$S	

 	%	
r   r   N)rh   ri   rj   rk   r   r$   r   r*   r<   r   r   r   r   r   r   r  r
  r  r&  r-  r4  rI  rl   r   r   r   r   r   M  s^    	&
)221U7*6*-0->"7H,(
 >
r   r   c                   `    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rSrg)	TimeTestsi  z1
Tests for the IReactorTime part of the reactor.
c                 \    [         R                  " 5       nU R                  X-
  U-   U5        g)a  
L{twisted.internet.reactor.seconds} should return something
like a number.

1. This test specifically does not assert any relation to the
   "system time" as returned by L{time.time} or
   L{twisted.python.runtime.seconds}, because at some point we
   may find a better option for scheduling calls than
   wallclock-time.
2. This test *also* does not assert anything about the type of
   the result, because operations may not return ints or
   floats: For example, datetime-datetime == timedelta(0).
N)r
   secondsr/   )r#   nows     r   test_secondsTimeTests.test_seconds  s&     ooS#.r   c                    [         R                  nS [         l         [         R                  " SS 5      nU R                  UR	                  5       S5        U[         l        UR                  5         g! U[         l        f = f)z
L{reactor.callLater<twisted.internet.interfaces.IReactorTime.callLater>}
should use the reactor's seconds factory
to produce the time at which the DelayedCall will be called.
c                      gNd   r   r   r   r   r   ITimeTests.test_callLaterUsesReactorSecondsInDelayedCall.<locals>.<lambda>      #r      c                      g r   r   r   r   r   r   rV        r   i   N)r
   rN  	callLaterr/   getTimecancelr#   osecondscalls      r   -test_callLaterUsesReactorSecondsInDelayedCall7TimeTests.test_callLaterUsesReactorSecondsInDelayedCall  \     ??%	'$$Q5DT\\^S1&GO 'GO   8A2 2A?c                    [         R                  nS [         l         [         R                  " SS 5      nU R                  UR                  5       S5        U[         l        UR	                  5         g! U[         l        f = f)z
L{reactor.callLater<twisted.internet.interfaces.IReactorTime.callLater>}
should propagate its own seconds factory
to the DelayedCall to use as its own seconds factory.
c                      grT  r   r   r   r   r   WTimeTests.test_callLaterUsesReactorSecondsAsDelayedCallSecondsFactory.<locals>.<lambda>  rW  r   rX  c                      g r   r   r   r   r   r   rh    rZ  r   rU  N)r
   rN  r\  r/   r^  r_  s      r   ;test_callLaterUsesReactorSecondsAsDelayedCallSecondsFactoryETimeTests.test_callLaterUsesReactorSecondsAsDelayedCallSecondsFactory  rd  re  c                     [        5       n[        R                  " SUR                  S5        UR	                  U R
                  S5        U$ )zK
Test that a DelayedCall really calls the function it is
supposed to call.
r   N)r   r
   r\  r`   addCallbackr/   r#   ds     r   test_callLaterTimeTests.test_callLater  s:    
 J!QZZ.	d&&-r   c                     [         R                  " S[        [        5      nU R                  UR                  5        UR
                  nUR                  S5        U R                  UR
                  U5        g)zC
A L{DelayedCall} that is reset will be scheduled at the new time.
   rt   N)r
   r\  r   
addCleanupr^  timeresetassertNotEqual)r#   ra  origTimes      r   test_callLaterResetTimeTests.test_callLaterReset  sP       Hh7$99

1DIIx0r   c                    ^ ^^ / mU4S jn[         R                  " SU5      nUR                  5         [        5       mUUU 4S jn[         R                  " S[         R                  SU5        T$ )z<
Test that when a DelayedCall is cancelled it does not run.
c                  (   > T R                  S 5        g r   r   )calleds   r   function2TimeTests.test_cancelDelayedCall.<locals>.function  r   r   r   c                     >  TR                  T / 5        TR                  S 5        g ! [         a    TR                  5          g f = fr   )r/   r`   BaseExceptionerrback)r}  ro  r#   s   r   check/TimeTests.test_cancelDelayedCall.<locals>.check  s@    !  , 

4  ! 		s   ' AA)r
   r\  r^  r   )r#   r~  ra  r  r}  ro  s   `   @@r   test_cancelDelayedCall TimeTests.test_cancelDelayedCall  s[     	    H- J	! 	!W..59r   c                     [         R                  " SS 5      nUR                  5         U R                  [        R
                  UR                  5        g)zg
Test that cancelling a DelayedCall which has already been cancelled
raises the appropriate exception.
r   c                      g r   r   r   r   r   r   ;TimeTests.test_cancelCancelledDelayedCall.<locals>.<lambda>  s    Dr   N)r
   r\  r^  r(   r   AlreadyCancelled)r#   ra  s     r   test_cancelCancelledDelayedCall)TimeTests.test_cancelCancelledDelayedCall  s9    
   L1%00$++>r   c                 ^   ^ ^^ [        5       mUUU 4S jn[        R                  " SU5      mT$ )z
Test that cancelling a DelayedCall in the DelayedCall's function as
that function is being invoked by the DelayedCall raises the
appropriate exception.
c                     >  TR                  [        R                  T R                  5        TR	                  S 5        g ! [
         a    TR                  5          g f = fr   r(   r   AlreadyCalledr^  r`   r  r  ra  ro  r#   s   r   later@TimeTests.test_cancelCalledDelayedCallSynchronous.<locals>.later  J    !!!%"5"5t{{C 

4  ! 		   *? AAr   r   r
   r\  )r#   r  ra  ro  s   ` @@r   'test_cancelCalledDelayedCallSynchronous1TimeTests.test_cancelCalledDelayedCallSynchronous  s*     J	!   E*r   c                 l   ^ ^^^ [        5       mUUU 4S jmU4S jn[        R                  " SU5      mT$ )zd
Test that cancelling a DelayedCall after it has run its function
raises the appropriate exception.
c                     >  TR                  [        R                  T R                  5        TR	                  S 5        g ! [
         a    TR                  5          g f = fr   r  r  s   r   r  ATimeTests.test_cancelCalledDelayedCallAsynchronous.<locals>.check  r  r  c                  4   > [         R                  " ST 5        g Nr   )r
   r\  )r  s   r   r  ATimeTests.test_cancelCalledDelayedCallAsynchronous.<locals>.later  s    a'r   r   r  )r#   r  ra  r  ro  s   ` @@@r   (test_cancelCalledDelayedCallAsynchronous2TimeTests.test_cancelCalledDelayedCallAsynchronous
  s/    
 J	!	(   E*r   c                     [         R                  " SS 5      n U R                  UR                  5       [        R                  " 5       S-   -
  S:  5        UR                  5         g ! UR                  5         f = f)N
   c                      g r   r   r   r   r   r   -TimeTests.testCallLaterTime.<locals>.<lambda>   s    $r   rt   )r
   r\  
assertTruer]  ru  r^  rn  s     r   testCallLaterTimeTimeTests.testCallLaterTime  sS    b,/	OOAIIK499;+;<q@AHHJAHHJs   ;A& &A8c                   ^ [         R                  " SS SSS9m[        T5        TR                  S5        [        T5        TR	                  5         [        T5        [         R                  " SS /[        S5      Q7SSS	0S
4[         /06m[        T5        TR	                  5         [        T5        U4S jn[        5       R                  U5      n[         R                  " SUR                  S 5      m[        T5        U$ )Nr   c                     g r   r   )r   ys     r   r   :TimeTests.testDelayedCallStringification.<locals>.<lambda>)  s    tr   r   r  )r  rX  c                      g r   r   r   r   r   r   r  1      tr   helloworldy              $@c                    > [        T5        g r   )str)ignoreddcs    r   
calledBack<TimeTests.testDelayedCallStringification.<locals>.calledBack7  s
    Gr   )	r
   r\  r  rv  r^  ranger   rm  r`   )r#   r  ro  r  s      @r   testDelayedCallStringification(TimeTests.testDelayedCallStringification&  s     q"3SB?B
B
		B|
FKBi
")7!3S 97C
 	B
		B	 J"":.q!**d3Br   c           	          S n[         R                  " SS S0 S S U5      nU R                  UR                  5       S5        UR	                  S5        U R                  UR                  5       S5        g	)
zw
Test that the C{seconds} argument to DelayedCall gets used instead of
the default timing function, if it is not None.
c                      g)Nr  r   r   r   r   rN  9TimeTests.testDelayedCallSecondsOverride.<locals>.secondsE  s    r   rX  c                      g r   r   r   r   r   r   :TimeTests.testDelayedCallSecondsOverride.<locals>.<lambda>I  r  r   r   c                     g r   r   r  s    r   r   r  I  rZ  r   c                     g r   r   r  s    r   r   r  I  s    r         N)r   DelayedCallr/   r]  rv  )r#   rN  r  s      r   testDelayedCallSecondsOverride(TimeTests.testDelayedCallSecondsOverride?  sa    	 |R_ow
 	q)
r*r   r   N)rh   ri   rj   rk   r   rP  rb  rj  rp  ry  r  r  r  r  r  r  r  rl   r   r   r   rL  rL    sC    /"16?&*2+r   rL  c                   v    \ rS rSr\" \R                  " \S5      (       + S5      S 5       rS r	S r
S rS rS	rg)
!CallFromThreadStopsAndWakeUpTestsiP  Nz-Nothing to wake up for without thread supportc                 T   ^ [        5       mU4S jn[        R                  " U5        T$ )Nc                  t   > [         R                  " S5        [        R                  " T R                  S 5        g )Ng?)ru  sleepr
   callFromThreadr`   )ro  s   r   wake:CallFromThreadStopsAndWakeUpTests.testWakeUp.<locals>.wakeY  s"    JJsO""1::t4r   )r   r
   callInThread)r#   r  ro  s     @r   
testWakeUp,CallFromThreadStopsAndWakeUpTests.testWakeUpQ  s&     J	5
 	T"r   c                     SU l         g )NTstoppedr"   s    r   _stopCallFromThreadCallback=CallFromThreadStopsAndWakeUpTests._stopCallFromThreadCallbacka  s	    r   c                     [         R                  " U R                  U5        [         R                  " SU R                  5        g r  )r
   r  _callFromThreadCallback2r\  r  rn  s     r   _callFromThreadCallback9CallFromThreadStopsAndWakeUpTests._callFromThreadCallbackd  s.    t<<a@!T==>r   c                      U R                  U R                  5        UR                  S 5        g ! [         a    UR	                  5          g f = fr   )r  r  r`   r  r  rn  s     r   r  :CallFromThreadStopsAndWakeUpTests._callFromThreadCallback2h  s?    	OODLL)
 JJt	  	IIK	s   / AAc                     SU l         [        R                  " 5       n[        R                  " U R
                  U5        U$ )z
Ensure that callFromThread from inside a callFromThread
callback doesn't sit in an infinite loop and lets other
things happen too.
F)r  r   r   r
   r  r  rn  s     r   testCallFromThreadStops9CallFromThreadStopsAndWakeUpTests.testCallFromThreadStopsq  s2     NNt;;Q?r   r  )rh   ri   rj   rk   r   r   IReactorThreadsr
   r  r  r  r  r  rl   r   r   r   r  r  P  sC    &&w557
	
?	r   r  c                   J    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rSrg)DelayedTestsi}  c                 b    SU l         SU l        0 U l        [        R                  " 5       U l        g r  )finishedcountertimersr   r   deferredr"   s    r   r$   DelayedTests.setUp~  s%    (r   c                 f    U R                   R                  5        H  nUR                  5         M     g r   )r  valuesr^  )r#   r   s     r   r   DelayedTests.tearDown  s"    ##%AHHJ &r   c                    U R                   R                  5       n[        [        R                  " 5       5      n/ nU H  nXB;  d  M
  UR                  U5        M     U(       a  SU l        U R                  US[        U5      -   5        g )Nrt   z7Should have been missing no calls, instead was missing )	r  r  listr
   getDelayedCallsrT   r  assertFalserepr)r#   l1l2missingr  s        r   checkTimersDelayedTests.checkTimers  sw    [[!'))+, B|r"  DM7m	
r   c                 >    U R                   U	 U R                  5         g r   )r  r  r#   tags     r   r`   DelayedTests.callback  s    KKr   c                 ^    U R                  U5        U R                  SU R                   5        g )N   )r`   addTimerr  s     r   rm  DelayedTests.addCallback  s     cb$--(r   c                 j    SU l         U R                  U5        U R                  R                  S 5        g )Nrt   )r  r`   r  r  s     r   doneDelayedTests.done  s'    ct$r   c                     [         R                  " US-  X R                  5      U R                  U R                  '   U =R                  S-  sl        U R	                  5         g )N{Gz?rt   )r
   r\  r  r  r  )r#   whenr`   s      r   r  DelayedTests.addTimer  sH    $+$5$54K<<%
DLL! 	r   c                   ^  [        [        S5      (       d  g T R                  5         T R                  ST R                  5        T R                  ST R
                  5        T R                  ST R
                  5        T R                  nT R                  ST R
                  5        T R                  ST R                  5        T R                  ST R
                  5        T R                  U   R                  5         T R                  U	 T R                  5         T R                  R                  U 4S j5        T R                  $ )	Nr  #                  c                 $   > TR                  5       $ r   )r  )r   r#   s    r   r   2DelayedTests.testGetDelayedCalls.<locals>.<lambda>  s    D,<,<,>r   )hasattrr
   r  r  r  r`   r  rm  r  r^  r  )r#   whichs   ` r   testGetDelayedCalls DelayedTests.testGetDelayedCalls  s    w 122 	b$))$b$--(b$--(b$--(b$**+b$--(E!!#KK!!">?}}r   c                    ^ ^ [         R                  " ST R                  R                  S5      mT R	                  TR                  5       5        UU 4S jnT R                  R                  U5        T R                  $ )z=
L{IDelayedCall.active} returns False once the call has run.
r  Tc                 F   > TR                  TR                  5       5        U $ r   )r  active)successdcallr#   s    r   checkDeferredCall3DelayedTests.test_active.<locals>.checkDeferredCall  s    U\\^,Nr   )r
   r\  r  r`   r  r  rm  )r#   r  r  s   ` @r   test_activeDelayedTests.test_active  sY     !!$(>(>E'	 	!!"34}}r   )r  r  r  r  N)rh   ri   rj   rk   r$   r   r  r`   rm  r  r  r  r  rl   r   r   r   r  r  }  s/    )
*)%
,r   r  a  
import %(reactor)s
%(reactor)s.install()
from twisted.internet import reactor

class Foo:
    def __init__(self):
        reactor.callWhenRunning(self.start)
        self.timer = reactor.callLater(3, self.failed)
    def start(self):
        reactor.resolve('localhost').addBoth(self.done)
    def done(self, res):
        print('done', res)
        reactor.stop()
    def failed(self):
        print('failed')
        self.timer = None
        reactor.stop()
f = Foo()
reactor.run()
c                   2    \ rS rSrS rS rS rS rS rSr	g)	ChildResolveProtocoli  c                     Xl         g r   )onCompletion)r#   r  s     r   __init__ChildResolveProtocol.__init__  s    (r   c                      / U l         / U l        g r   )outputr   r"   s    r   connectionMade#ChildResolveProtocol.connectionMade  s    
r   c                 :    U R                   R                  U5        g r   )r!  rT   )r#   outs     r   outReceived ChildResolveProtocol.outReceived  s    3r   c                 :    U R                   R                  U5        g r   )r   rT   )r#   errs     r   errReceived ChildResolveProtocol.errReceived  s    

#r   c                 t    U R                   R                  XR                  U R                  45        S U l         g r   )r  r`   r!  r   )r#   reasons     r   processEnded!ChildResolveProtocol.processEnded  s+    ""FKK#DE r   )r   r  r!  N)
rh   ri   rj   rk   r  r"  r&  r*  r.  rl   r   r   r   r  r    s    ) !r   r  z8cannot run test: reactor doesn't support IReactorProcessc                       \ rS rSrS rSrg)ResolveTestsi  c                 J  ^  [         R                  R                  T R                  5       5      n[	        US5       n[
        R                  nUR                  [        SU0-  5        S S S 5        [         R                  R                  5       n[         R                  R                  [        R                  5      US'   [        5       n[        U5      n[
        R                   " U[        R"                  SSU4U5        U 4S jnUR%                  U5        U$ ! , (       d  f       N= f)Nwr
   
PYTHONPATHpythonz-uc           	         > U u  pnSR                  U5      nS[        R                  R                  S5      -   nX$:w  a@  TR	                  SR                  UR                  5       USR                  U5      5      5        g g )Nr   s   done 127.0.0.1asciizThe child process failed to produce the desired results:
   Reason for termination was: {!r}
   Output stream was: {!r}
   Error stream was: {!r}
)joinoslinesepencodefailformatgetErrorMessage)resultr-  r!  r   expected_outputr#   s        r   
cbFinished1ResolveTests.testChildResolve.<locals>.cbFinished  ss    &,#VU XXf%F/"**2C2CG2LLO(		6
 fV335vsxxO )r   )r9  pathabspathmktempopenr
   ri   writeresolve_helperenvironcopypathsepr8  sysr   r  spawnProcess
executablerm  )r#   
helperPath
helperFilereactorNameenvhelperDeferredhelperProtorA  s   `       r   testChildResolveResolveTests.testChildResolve  s     WW__T[[]3
*c"j!,,K^y+.FFG	 # jjooJJOOCHH5L!*>:(D*)Es	
	" 	"":.E #"s   +D
D"r   N)rh   ri   rj   rk   rU  rl   r   r   r   r1  r1    s    
'r   r1  z&Nothing to test without thread supportc                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
CallFromThreadTestsi0  z%
Task scheduling from threads tests.
c                 0    SU l         [        5       U l        g r  )r  r   r  r"   s    r   r$   CallFromThreadTests.setUp9  s     
r   c                 0    [         R                  " U0 UD6  g)z
Override in subclasses.
N)r
   r  )r#   argskwargss      r   scheduleCallFromThreadTests.schedule=  s     	//r   c                 n   ^  U 4S jn[        S5       H  nT R                  U5        M     T R                  $ )zj
L{IReactorThreads.callFromThread} can be used to schedule a large
number of calls in the reactor thread.
c                     > T =R                   S-  sl         T R                   S:X  a  T R                  R                  S5        g g )Nrt   rU  Tr  r  r`   r"   s   r   addAndMaybeFinishVCallFromThreadTests.test_lotsOfThreadsAreScheduledCorrectly.<locals>.addAndMaybeFinishI  s4    LLAL||s"&&t, #r   rU  )r  r^  r  )r#   rc  is   `  r   'test_lotsOfThreadsAreScheduledCorrectly;CallFromThreadTests.test_lotsOfThreadsAreScheduledCorrectlyC  s1    	-
 sAMM+,  }}r   c                 x  ^ ^ / mUU 4S jnT R                   R                  U5        T R                  TR                  S5        T R                  TR                  S5        T R                  TR                  S5        T R                  [        R
                  T R                   R                  S5        T R                   $ )z?
Callbacks should be invoked in the order they were scheduled.
c                 .   > TR                  T/ SQ5        g )N)rt   rs  r  )r/   )_orderr#   s    r   r  ECallFromThreadTests.test_threadsAreRunInScheduledOrder.<locals>.checkY  s    UI.r   rt   rs  r  N)r  rm  r^  rT   r
   r  r`   )r#   r  rk  s   ` @r   "test_threadsAreRunInScheduledOrder6CallFromThreadTests.test_threadsAreRunInScheduledOrderS  s     	/ 	!!%(ellA&ellA&ellA&g,,dmm.D.DdK}}r   c                    ^  U 4S jnT R                  U5        T R                  T R                  S5        T R                  $ )zE
Scheduled tasks should not be run until the reactor starts running.
c                  J   > ST l         T R                  R                  S5        g )Nrt   Trb  r"   s   r   incAndFinishUCallFromThreadTests.test_scheduledThreadsNotRunUntilReactorRuns.<locals>.incAndFinishi  s    DLMM""4(r   r   )r^  r/   r  r  )r#   rq  s   ` r   +test_scheduledThreadsNotRunUntilReactorRuns?CallFromThreadTests.test_scheduledThreadsNotRunUntilReactorRunsd  s6    
	) 	l# 	q)}}r   )r  r  N)rh   ri   rj   rk   r   r$   r^  rf  rm  rs  rl   r   r   r   rX  rX  0  s     
#0 "r   rX  c                       \ rS rSrSrSrg)
MyProtocoliu  z
Sample protocol.
r   N)rh   ri   rj   rk   r   rl   r   r   r   rv  rv  u  s    r   rv  c                       \ rS rSrSr\rSrg)	MyFactoryi{  z
Sample factory.
r   N)rh   ri   rj   rk   r   rv  r	   rl   r   r   r   rx  rx  {  s     Hr   rx  c                       \ rS rSrS rSrg)ProtocolTestsi  c                     [        5       nUR                  S 5      nU R                  UR                  U5        U R	                  X!R
                  5        g r   )rx  buildProtocolr/   factoryassertIsInstancer	   )r#   r}  r	   s      r   testFactoryProtocolTests.testFactory  sC    +((.))73h(8(89r   r   N)rh   ri   rj   rk   r  rl   r   r   r   rz  rz    s    :r   rz  c                   0    \ rS rSrSrS rS rS rS rSr	g)	DummyProduceri  ao  
Very uninteresting producer implementation used by tests to ensure the
right methods are called by the consumer with which it is registered.

@type events: C{list} of C{str}
@ivar events: The producer/consumer related events which have happened to
this producer.  Strings in this list may be C{'resume'}, C{'stop'}, or
C{'pause'}.  Elements are added as they occur.
c                     / U l         g r   r   r"   s    r   r  DummyProducer.__init__  s	    r   c                 :    U R                   R                  S5        g )NresumerW   rT   r"   s    r   resumeProducingDummyProducer.resumeProducing  s    8$r   c                 :    U R                   R                  S5        g )Nstopr  r"   s    r   stopProducingDummyProducer.stopProducing  s    6"r   c                 :    U R                   R                  S5        g )Npauser  r"   s    r   pauseProducingDummyProducer.pauseProducing  s    7#r   r   N)
rh   ri   rj   rk   r   r  r  r  r  rl   r   r   r   r  r    s    %#$r   r  c                   0    \ rS rSrSrSrSrS rS r\r	Sr
g)	SillyDescriptori  z
A descriptor whose data buffer gets filled very fast.

Useful for testing FileDescriptor's IConsumer interface, since
the data buffer fills as soon as at least four characters are
written to it, and gets emptied in a single doWrite() cycle.
r  Tc                     [        U5      $ )z
Always write all data.
)rx   )r#   datas     r   writeSomeDataSillyDescriptor.writeSomeData  s     4yr   c                     g)z!
Do nothing: bypass the reactor.
Nr   r"   s    r   startWritingSillyDescriptor.startWriting  s    r   r   N)rh   ri   rj   rk   r   
bufferSize	connectedr  r  stopWritingrl   r   r   r   r  r    s$     JI
 Kr   r  c                   8   ^  \ rS rSrSrU 4S jrU 4S jrSrU =r$ )ReentrantProduceri  a  
Similar to L{DummyProducer}, but with a resumeProducing method which calls
back into an L{IConsumer} method of the consumer against which it is
registered.

@ivar consumer: The consumer with which this producer has been or will
be registered.

@ivar methodName: The name of the method to call on the consumer inside
C{resumeProducing}.

@ivar methodArgs: The arguments to pass to the consumer method invoked in
C{resumeProducing}.
c                 F   > [         TU ]  5         Xl        X l        X0l        g r   )superr  consumer
methodName
methodArgs)r#   r  r  r  	__class__s       r   r  ReentrantProducer.__init__  s     $$r   c                 |   > [         TU ]  5         [        U R                  U R                  5      " U R
                  6   g r   )r  r  getattrr  r  r  )r#   r  s    r   r  !ReentrantProducer.resumeProducing  s)    !t/Ar   )r  r  r  )	rh   ri   rj   rk   r   r  r  rl   __classcell__)r  s   @r   r  r    s    %B Br   r  c                   H    \ 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)ProducerTestsi  z4
Test abstract.FileDescriptor's consumer interface.
c                    [         R                  " 5       nSUl        [        5       nUR	                  US5        U R                  UR                  S/5        U R                  [        UR                  [        5       S5        g)z
Verify that registering a non-streaming producer invokes its
resumeProducing() method and that you can only register one producer
at a time.
rt   r   r  N)	r   FileDescriptorr  r  registerProducerr/   rW   r(   RuntimeErrorr#   fddps      r   test_doubleProducer!ProducerTests.test_doubleProducer  sb     $$&_
B"XJ/,(;(;]_aPr   c                     [         R                  " 5       nSUl        [        5       nUR	                  US5        U R                  UR                  S/5        g)zt
Verify that registering a producer when the connection has already
been closed invokes its stopProducing() method.
rt   r   r  N)r   r  disconnectedr  r  r/   rW   r  s      r   test_unconnectedFileDescriptor,ProducerTests.test_unconnectedFileDescriptor  sF    
 $$&_
B"VH-r   c                 2   [        5       n[        5       nUR                  USS9  U R                  UR                  S/5        UR                  SS2	 US:X  a  UR                  / SQ5        OUR                  S5        U R                  UR                  / 5        g)z
Pull consumers don't get their C{pauseProducing} method called if the
descriptor buffer fills up.

@param _methodName: Either 'write', or 'writeSequence', indicating
    which transport method to write data to.
F	streamingr  NwriteSequence)   1   2   3   4s   1234)r  r  r  r/   rW   r  rG  )r#   r  
descriptorproducers       r   _dontPausePullConsumerTest(ProducerTests._dontPausePullConsumerTest  s     %&
 ?##H#>8*5OOA ($$%=>W%"-r   c                 $    U R                  S5      $ )z
Verify that FileDescriptor does not call producer.pauseProducing() on a
non-streaming pull producer in response to a L{IConsumer.write} call
which results in a full write buffer. Issue #2286.
rG  r  r"   s    r   !test_dontPausePullConsumerOnWrite/ProducerTests.test_dontPausePullConsumerOnWrite  s     ..w77r   c                 $    U R                  S5      $ )a  
Like L{test_dontPausePullConsumerOnWrite}, but for a call to
C{writeSequence} rather than L{IConsumer.write}.

C{writeSequence} is not part of L{IConsumer}, but
L{abstract.FileDescriptor} has supported consumery behavior in response
to calls to L{writeSequence} forever.
r  r  r"   s    r   )test_dontPausePullConsumerOnWriteSequence7ProducerTests.test_dontPausePullConsumerOnWriteSequence  s     ..??r   c                    [        5       nUS:X  a  / SQnOSn[        X!U5      nUR                  USS9  [        X!5      " U5        U R	                  UR
                  S/5        UR
                  S S 2	 UR                  5         U R	                  UR
                  SS/5        UR
                  S S 2	 UR                  5         U R	                  UR
                  SS/5        g )Nr  )   s   ps   ams   spamTr  r  r  )r  r  r  r  r/   rW   doWrite)r#   r  r  r  r  s        r   _reentrantStreamingProducerTest-ProducerTests._reentrantStreamingProducerTest  s    $&
(&DD$ZTB##H#= 	
'- 	7)4OOA 	8W*=>OOA 	8W*=>r   c                 $    U R                  S5      $ )zl
Verify that FileDescriptor tracks producer's paused state correctly.
Issue #811, fixed in revision r12857.
rG  r  r"   s    r   )test_reentrantStreamingProducerUsingWrite7ProducerTests.test_reentrantStreamingProducerUsingWrite?  s    
 33G<<r   c                 $    U R                  S5      $ )z
Like L{test_reentrantStreamingProducerUsingWrite}, but for calls to
C{writeSequence}.

C{writeSequence} is B{not} part of L{IConsumer}, however
C{abstract.FileDescriptor} has supported consumery behavior in response
to calls to C{writeSequence} forever.
r  r  r"   s    r   1test_reentrantStreamingProducerUsingWriteSequence?ProducerTests.test_reentrantStreamingProducerUsingWriteSequenceF  s     33ODDr   r   N)rh   ri   rj   rk   r   r  r  r  r  r  r  r  r  rl   r   r   r   r  r    s3    Q	...8	@?@=	Er   r  c                      \ rS rSr\" \R                  " \S5      (       + S5      S 5       r\" \R                  " \S5      (       + S5      S 5       r
\" \R                  " \S5      (       + S5      \" \(       + S5      \4S	 j5       5       rS
rg)PortStringificationTestsiR  NzIReactorTCP is neededc                    [         R                  " S[        R                  " 5       5      nUR	                  5       R
                  nU R                  [        U5      R                  [        U5      5      SSX!4-  5        UR                  5       $ Nr   %d not found in %s)
r
   	listenTCPr	   ServerFactorygetHostportrw  r  findstopListeningr#   pportNos      r   testTCP PortStringificationTests.testTCPS  sk    a!7!7!9:!!FKKF$b*>&*L	
   r   zIReactorUDP is neededc                    [         R                  " S[        R                  " 5       5      nUR	                  5       R
                  nU R                  [        U5      R                  [        U5      5      SSX!4-  5        UR                  5       $ r  )
r
   	listenUDPr	   DatagramProtocolr  r  rw  r  r  r  r  s      r   testUDP PortStringificationTests.testUDP\  sk    a!:!:!<=!!FKKF$b*>&*L	
   r   zIReactorSSL is neededzSSL support is missingc                 p   [         R                  " [        S5      n[        R                  " S[
        R                  " 5       UR                  X"5      5      nUR                  5       R                  nU R                  [        U5      R                  [        U5      5      SSXC4-  5        UR                  5       $ )Nz
server.pemr   r  r  )r   sibpathr   r
   	listenSSLr	   r  DefaultOpenSSLContextFactoryr  r  rw  r  r  r  )r#   r   pemr  r  s        r   testSSL PortStringificationTests.testSSLe  s     ll8\2x%%')I)I#)S
 !!FKKF$b*>&*L	
   r   r   )rh   ri   rj   rk   r   r   IReactorTCPr
   r  IReactorUDPr  IReactorSSLr   r  rl   r   r   r   r  r  R  s    
&&w557NO! P! 
&&w557NO! P! 
&&w557NOG-. 	! / P	!r   r  c                       \ rS rSrS rSrg)ConnectorReprTestsis  c                     [        SS[        5       S[        5       5      nSnU[        U5      UR                  5       4-  nU R	                  [        U5      U5        g )N	localhosti  r   zA<twisted.internet.tcp.Connector instance at 0x%x disconnected %s>)r   r   idgetDestinationr/   r  )r#   cexpects      r   test_tcp_repr ConnectorReprTests.test_tcp_reprt  sM    k3!VX>W2a5!"2"2"455a&)r   r   N)rh   ri   rj   rk   r  rl   r   r   r   r  r  s  s    *r   r  )3r   r9  rL  ru  unittestr   twisted.internetr   r   r   r   r   r	   r
   twisted.internet.deferr   r   twisted.internet.tcpr   twisted.pythonr   twisted.trial.unittestr   r   _sslImportError	supportedr   r   rL  r  r  rH  ProcessProtocolr  IReactorProcessr1  r  rX  Protocolrv  Factoryrx  rz  r  r  r  r  r  r  r  r   r   r   <module>r     s  
 
 
   X X X 5 *  +, Cs}}
Ck78 k7\	{
x {
|	B+ B+J* *ZW8 Wt.!833 !& ""7D11>(8 (	(V ""7D11,>( >	>B""    :H :$ $0h-- 4B B6wEH wEt!x !B* *{*  
Cs   F FF