+
    Dfj@                     P   R t . ROt^ RIt^ RIt^ RIt^ RIHtHtHtH	t	H
t
HtHtHt ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ R	IHt ^ R
IHt ^ RIHtHt ^ RIHtH t  ]'       d   ^ RI!H"t"  ^ RI!H#t$ ]$t# ! R R]&4      t'R t(RR lt) ! R R4      t*R R lt+]+t,R#   ]% d    Rt# L2i ; i)a  
Utilities for unit testing reactor implementations.

The main feature of this module is L{ReactorBuilder}, a base class for use when
writing interface/blackbox tests for reactor implementations.  Test case classes
for reactor features should subclass L{ReactorBuilder} instead of
L{SynchronousTestCase}.  All of the features of L{SynchronousTestCase} will be
available.  Additionally, the tests will automatically be applied to all
available reactor implementations.
TestTimeoutErrorReactorBuilderN)TYPE_CHECKINGCallableDictOptionalSequenceTypeUnioncast)	Interfacelog)_fullyQualifiedName)Failure)namedAny)platform)SkipTestSynchronousTestCase)DEFAULT_TIMEOUT_DURATIONacquireAttributeasyncioreactor)processc                       ] tR t^/tRtRtR# )r   za
The reactor was still running after the timeout period elapsed in
L{ReactorBuilder.runReactor}.
 N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       E/usr/lib/python3/dist-packages/twisted/internet/test/reactormixins.pyr   r   /   s    r"   c                (    V P                  V4       R# )a  
Various functions within these tests need an already-running reactor at
some point.  They need to stop the reactor when the test has completed, and
that means calling reactor.stop().  However, reactor.stop() raises an
exception if the reactor isn't already running, so if the L{Deferred} that
a particular API under test returns fires synchronously (as especially an
endpoint's C{connect()} method may do, if the connect is to a local
interface address) then the test won't be able to stop the reactor being
tested and finish.  So this calls C{thunk} only once C{reactor} is running.

(This is just an alias for
L{twisted.internet.interfaces.IReactorCore.callWhenRunning} on the given
reactor parameter, in order to centrally reference the above paragraph and
repeating it everywhere as a comment.)

@param reactor: the L{twisted.internet.interfaces.IReactorCore} under test

@param thunk: a 0-argument callable, which eventually finishes the test in
    question, probably in a L{Deferred} callback.
N)callWhenRunning)reactorthunks   &&r#   needsRunningReactorr(   6   s    * E"r"   c                   aa Vf   ^ RI Hp R.oVV3R lpVP                  V4       V P                  VP                  V4       R# )aT  
Stop the reactor as soon as any error is logged on the given publisher.

This is beneficial for tests which will wait for a L{Deferred} to fire
before completing (by passing or failing).  Certain implementation bugs may
prevent the L{Deferred} from firing with any result at all (consider a
protocol's {dataReceived} method that raises an exception: this exception
is logged but it won't ever cause a L{Deferred} to fire).  In that case the
test would have to complete by timing out which is a much less desirable
outcome than completing as soon as the unexpected error is encountered.

@param case: A L{SynchronousTestCase} to use to clean up the necessary log
    observer when the test is over.
@param reactor: The reactor to stop.
@param publisher: A L{LogPublisher} to watch for errors.  If L{None}, the
    global log publisher will be watched.
Nr   c                    < S'       d<   V P                  R 4      '       d#   SP                  4        SP                  4        R# R# R# )isErrorN)getpopstop)eventr&   runnings   &r#   stopIfError stopOnError.<locals>.stopIfErrord   s-    uyy++KKMLLN ,7r"   )twisted.pythonr   addObserver
addCleanupremoveObserver)caser&   	publisherr1   r0   s   &f& @r#   stopOnErrorr9   N   s>    $ 3fG
 +&OOI,,k:r"   c                     a  ] tR t^mt o RtR.t]P                  ! 4       '       d   ]P                  . RO4       M]P                  R.4       ]P                  R4       ]P                  R4       ]P                  ! 4       '       d   ]P                  R4       M<]P                  RR.4       ]P                  ! 4       '       g   ]P                  R	.4       R
tR
tR
t/ tR tR tR tR tR tRR lt]V 3R lR l4       tV 3R ltRtV tR
# )r   a  
L{SynchronousTestCase} mixin which provides a reactor-creation API.  This
mixin defines C{setUp} and C{tearDown}, so mix it in before
L{SynchronousTestCase} or call its methods from the overridden ones in the
subclass.

@cvar skippedReactors: A dict mapping FQPN strings of reactors for
    which the tests defined by this class will be skipped to strings
    giving the skip message.
@cvar requiredInterfaces: A C{list} of interfaces which the reactor must
    provide or these tests will be skipped.  The default, L{None}, means
    that no interfaces are required.
@ivar reactorFactory: A no-argument callable which returns the reactor to
    use for testing.
@ivar originalHandler: The SIGCHLD handler which was installed when setUp
    ran and which will be re-installed when tearDown runs.
@ivar _reactors: A list of FQPN strings giving the reactors for which
    L{SynchronousTestCase}s will be created.
z,twisted.internet.selectreactor.SelectReactorz$twisted.internet.gireactor.GIReactorz:twisted.internet.test.reactormixins.AsyncioSelectorReactorz6twisted.internet._threadedselect.ThreadedSelectReactorz$twisted.internet.cfreactor.CFReactorz(twisted.internet.pollreactor.PollReactorz*twisted.internet.epollreactor.EPollReactorz(twisted.internet.kqreactor.KQueueReactorNc                    \         P                  ! 4       '       g;   \        P                  ! \        P                  \        P                  4      V n        R# R# )z
Clear the SIGCHLD handler, if there is one, to ensure an environment
like the one which exists prior to a call to L{reactor.run}.
N)r   	isWindowssignalSIGCHLDSIG_DFLoriginalHandlerselfs   &r#   setUpReactorBuilder.setUp   s3    
 !!###)==#PD  $r"   c                   V P                   e0   \        P                  ! \        P                  V P                   4       \        e   \        P                  ! 4       p\        P
                  '       d   \        P                  ! R\        P
                  : 24       \        P                  4        \        P                  ! R4       \        P                  ! 4       V,
          ^<8  g   K  \        P
                   F(  p\        P                  ! V\        P                  4       K*  	  \        R\        P
                  : 24      hR# R# )zd
Restore the original SIGCHLD handler and reap processes as long as
there seem to be any remaining.
Nz/ReactorBuilder.tearDown reaping some processes gMbP?z-Timeout waiting for child processes to exit: )r@   r=   r>   r   timereapProcessHandlersr   msgreapAllProcessessleeposkillSIGKILL	Exception)rB   beginpids   &  r#   tearDownReactorBuilder.tearDown   s    
 +MM&..$*>*>?IIKE---225 ((* 

5!99;&+&::V^^4  ;#"669  . r"   c                   VP                  4        \        VRR4      eP   VP                   F%  pVP                  V4       VP	                  R4       K'  	  VP                  P                  4        VP                  4        VP                  4       pV F  pVP                  4        K  	  ^ RI	H
p VP                  Vn        VP                  Vn        R# )zx
Clean up any resources which may have been allocated for the given
reactor by its creation or by a test which used it.
_internalReadersNr&   )_uninstallHandlergetattrrT   removeReaderconnectionLostcleardisconnectAllgetDelayedCallscanceltwisted.internetr&   _originalReactorDict__dict___originalReactorClass	__class__)rB   r&   readercallscglobalReactors   &&    r#   _unbuildReactorReactorBuilder._unbuildReactor   s     	!!#7.5A!22$$V,%%d+ 3 $$**, 	 '')AHHJ  	>!(!=!=")"?"?r"   c                    ^ RI Hp ^ RIHp \	        W4      '       d   V P
                  VJ d   \        R4      h T P
                  f   Q hT P                  4       pXP                  Tn	        TP                  Tn        TP                  Tn        TP                  Tn
        T P                  e   T P                   Uu. uF  pTP                  T4      '       d   K  TNK   	  ppT'       d_   T P                  T4       \        \        TP                  4      : RRP!                  T Uu. uF  p\        T4      NK  	  up4      : 24      hT P/                  T P                  T4       T#   \         d     EL9i ; iu upi u upi   \"         dJ    \$        P&                  ! RR4       T P)                  4        \        \+        4       P-                  4       4      hi ; i)z;
Create and return a reactor using C{self.reactorFactory}.
rU   )	CFReactorzuCFReactor uses APIs which manipulate global state, so it's not safe to run its own reactor-builder tests under itselfNz does not provide ,zFailed to install reactor)r^   r&   twisted.internet.cfreactorrj   
isinstancereactorFactoryr   ImportErrorr`   r_   rb   ra   requiredInterfaces
providedByrg   fullyQualifiedNamejoinBaseExceptionr   errflushLoggedErrorsr   getErrorMessager5   )rB   rf   rj   r&   requiredmissingxs   &      r#   buildReactorReactorBuilder.buildReactor  s   	A<
 =44''94# 
"	&&222))+G+8+A+AG(,9,C,CG) &-%5%5M"&-&7&7M# &&2 %)$;$;$;#..w7 H$;  
 ((1" /w/@/@AHHW%MW&8&;W%MN  	,,g6a  		B &N/  		8 GGD56""$7944677		8s0   E% A$F ;E7E7%E<%E43E4AGc                8    \        V P                  R\        4      # )z}
Determine how long to run the test before considering it failed.

@return: A C{int} or C{float} giving a number of seconds.
timeout)r   _parentsr   rA   s   &r#   
getTimeoutReactorBuilder.getTimeout;  s      y:RSSr"   c                   aa Vf   V P                  4       p. oVV3R lpSP                  W#4      pSP                  4        S'       d   \        RV R24      hVP	                  4        R# )a^  
Run the reactor for at most the given amount of time.

@param reactor: The reactor to run.

@type timeout: C{int} or C{float}
@param timeout: The maximum amount of time, specified in seconds, to
    allow the reactor to run.  If the reactor is still running after
    this much time has elapsed, it will be stopped and an exception
    raised.  If L{None}, the default test method timeout imposed by
    Trial will be used.  This depends on the L{IReactorTime}
    implementation of C{reactor} for correct operation.

@raise TestTimeoutError: If the reactor is still running after
    C{timeout} seconds.
Nc                  J   < SP                  R 4       S P                  4        R # N)appendr.   )r&   timedOuts   r#   r.   'ReactorBuilder.runReactor.<locals>.stopY  s    OOD!LLNr"   zreactor still running after z seconds)r   	callLaterrunr   r]   )rB   r&   r~   r.   timedOutCallr   s   &f&  @r#   
runReactorReactorBuilder.runReactorC  s_    " ?oo'G	 ((7"%A'(#STT!r"   c          	      ~   < V ^8  d   QhRS[ R,          RS[S[S[S[ R,          S[ S[,          3,          3,          /# )   clsr   return)r	   r   strr
   r   )format__classdict__s   "r#   __annotate__ReactorBuilder.__annotate__e  sC      "#	c5./6I1JJKK	Lr"   c                  a a / pS P                    F  oSP                  R4      R,          pS P                  R,           V,           R,           P                  RR4      p ! V V3R lRS \        4      pW4n        RP                  S P                  P                  4       ^ R V.,           4      Vn        WAVP                  &   K  	  V# )z
Create a L{SynchronousTestCase} subclass which mixes in C{cls} for each
known reactor and return a dict mapping their names to them.
.Tests_c                      < ] tR tRt Z P                  t Z Z P                  9   d    Z P                   Z,          t ]!  Z4      tRtR#   ] d    ]	! 4       P                  4       t RtR# i ; i)4ReactorBuilder.makeTestCaseClasses.<locals>.testcaseis  r   N)r   r   r   r   skippedReactorsskipr   rn   rt   r   rw   r!   )r   r&   s   r#   testcaser   s  sX     ^^
c111..w7D7%-g%6N$ 7"9446D7s   	A A-,A-r   )	_reactorssplitr   replacer   rs   r   )r   classesshortReactorNamenamer   r&   s   f    @r#   makeTestCaseClasses"ReactorBuilder.makeTestCaseClassesd  s      	 }}G&}}S1"5LL3&)99GCLLSRUVD7 73 3 7 !%$'HHS-=-=-C-C-Ea-Ktf-T$UH!)1H%%& %  r"   c                   < V ^8  d   Qh/ S[ S[. S[3,          ,          ;R&   S[ S[S[S[,          ,          ,          ;R&   S[S[S[3,          ;R&   # )r   rn   rp   r   )r   r   objectr   r	   r   r   r   )r   r   s   "r#   r   r   m   sV     J Xb&j129K P !$y/!:;BQ R #s(^(S r"   )r@   )z,twisted.internet.gireactor.PortableGIReactorz/twisted.internet.win32eventreactor.Win32Reactorz0twisted.internet.iocpreactor.reactor.IOCPReactorr   )r   r   r   r   r    r   r   r<   extendr   isMacOSXisLinuxrn   r@   rp   r   rC   rQ   rg   r{   r   r   classmethodr   __annotate_func__r!   __classdictcell__)r   s   @r#   r   r   m   s.    , 	7I
  		
 	6	
 	UVQRCD>@ ##%%    C 6:NO>B&(OQ:#@J7rT"B  q  r"   c                (    V ^8  d   QhR\         RR/# )r   rB   r   z%asyncioreactor.AsyncioSelectorReactor)r   )r   s   "r#   r   r     s     7 7 7,S 7r"   c                   aaa ^ RI HpHpHo ^ RIHp \        \        V 4      pV! 4       oV! 4       oS! S4       VP                  VVV3R l4       pVP                  ! S4      # )a  
Make a new asyncio reactor associated with a new event loop.

The test suite prefers this constructor because having a new event loop
for each reactor provides better test isolation.  The real constructor
prefers to re-use (or create) a global loop because of how this interacts
with other asyncio-based libraries and applications (though maybe it
shouldn't).

@param self: The L{ReactorBuilder} subclass this is being called on.  We
    don't use this parameter but we get called with it anyway.
)get_event_loopnew_event_loopset_event_loopr   c                  8   < S P                  4        S! S4       R # r   )close)looporiginalLoopr   s   r#   cleanUp'asyncioSelectorReactor.<locals>.cleanUp  s    

|$r"   )
asyncior   r   r   r^   r   r   r   r5   AsyncioSelectorReactor)	rB   r   r   r   
asTestCaser   r   r   r   s	   &     @@@r#   asyncioSelectorReactorr     s_     GF/)40J!#LD4% % 0066r"   )r   r   r(   r   )-r    __all__rK   r=   rF   typingr   r   r   r   r   r	   r
   r   zope.interfacer   r3   r   twisted.python.deprecater   rr   twisted.python.failurer   twisted.python.reflectr   twisted.python.runtimer   twisted.trial.unittestr   r   twisted.trial.utilr   r   r^   r   r   _processro   rN   r   r(   r9   r   r   r   r   r"   r#   <module>r      s   	 H 	   W W W $  N * + + @ I 04 Gy #0;>R Rj7@ 0 s  Gs   *B 	B%$B%