Ϫf/@@dZgdZddlZddlZddlZddlmZmZmZm Z m Z m Z m Z m Z ddlmZddlmZddlmZddlmZdd lmZdd lmZdd lmZmZdd lmZm Z erdd l!m"Z" ddl!m#Z$e$Z#Gdde&Z'dZ(ddZ)GddZ*de+ddfdZ,e,Z-y#e%$rdZ#Y0wxYw)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. )TestTimeoutErrorReactorBuilderneedsRunningReactorN) TYPE_CHECKINGCallableDictOptionalSequenceTypeUnioncast) Interfacelog)_fullyQualifiedName)Failure)namedAny)platform)SkipTestSynchronousTestCase)DEFAULT_TIMEOUT_DURATIONacquireAttributeasyncioreactor)processceZdZdZy)rzm The reactor was still running after the timeout period elapsed in L{ReactorBuilder.runReactor}. N)__name__ __module__ __qualname____doc__E/usr/lib/python3/dist-packages/twisted/internet/test/reactormixins.pyrr/sr"rc&|j|y)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)reactorthunks r#rr6s* E"r"c|ddlm}dgfd}|j||j|j|y)a 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. Nrrcpr3|jdr!jjyyy)NisError)getpopstop)eventr&runnings r# stopIfErrorz stopOnError..stopIfErrords* uyy+ KKM LLN,7r")twisted.pythonr addObserver addCleanupremoveObserver)caser& publisherr0r/s ` @r# stopOnErrorr7Ns>$3fG +&OOI,,k:r"c eZdZUdZdgZej rejgdn|ejdgejdejrejdn6ejddgejsejd gd Z e e gefed <d Zd Ze eeeed <iZeeefed <dZdZdZdZdZddZededdeeeedee fffdZ!y )ra 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.SelectReactor)z,twisted.internet.gireactor.PortableGIReactorz/twisted.internet.win32eventreactor.Win32Reactorz0twisted.internet.iocpreactor.reactor.IOCPReactorz$twisted.internet.gireactor.GIReactorz:twisted.internet.test.reactormixins.AsyncioSelectorReactorz$twisted.internet.cfreactor.CFReactorz(twisted.internet.pollreactor.PollReactorz*twisted.internet.epollreactor.EPollReactorz(twisted.internet.kqreactor.KQueueReactorNreactorFactoryrequiredInterfacesskippedReactorsctjs8tjtjtj|_yy)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 isWindowssignalSIGCHLDSIG_DFLoriginalHandlerselfs r#setUpzReactorBuilder.setUps0 !!##)==#PD $r"cn|j.tjtj|jtt j}tj rt jdtj tjt jdt j|z dkDrUtj D]&}tj|tj(tdtj tj ryyy)z| 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: )rAr>r?rtimereapProcessHandlersrmsgreapAllProcessessleeposkillSIGKILL Exception)rCbeginpids r#tearDownzReactorBuilder.tearDowns    + MM&..$*>*> ?  IIKE--225((*  5!99;&+&::5V^^45#"669-- r"c|jt|ddM|jD]$}|j||j d&|jj |j |j}|D]}|jddl m }|j|_ |j|_y)z Clean up any resources which may have been allocated for the given reactor by its creation or by a test which used it. _internalReadersNrr&)_uninstallHandlergetattrrT removeReaderconnectionLostclear disconnectAllgetDelayedCallscanceltwisted.internetr&_originalReactorDict__dict___originalReactorClass __class__)rCr&readercallsc globalReactors r#_unbuildReactorzReactorBuilder._unbuildReactors !!# 7. 5 A!22 ,$$V,%%d+ ,  $ $ * * , '') A HHJ  >!(!=!= ")"?"? r"c h ddlm}ddlm}t ||r|j |ur t d |j J|j }j|_ |j|_ |j|_|j|_ |j|jDcgc]}|j|s|}}|rZ|j|t t|jddj!|Dcgc] }t|c}|j/|j||S#t$rYwxYwcc}wcc}w#t"$rHt%j&dd|j)t t+j-wxYw) zK Create and return a reactor using C{self.reactorFactory}. rrU) CFReactorzuCFReactor uses APIs which manipulate global state, so it's not safe to run its own reactor-builder tests under itselfNz does not provide ,zFailed to install reactor)r^r&twisted.internet.cfreactorri isinstancer9r ImportErrorr`r_rbrar: providedByrgfullyQualifiedNamejoin BaseExceptionrerrflushLoggedErrorsrgetErrorMessager3)rCrfrir&requiredmissingxs r# buildReactorzReactorBuilder.buildReactors  A < =)4''94# " &&2 22))+G+8+A+AG (,9,C,CG )&-%5%5M "&-&7&7M #&&2%)$;$; #..w7 ((1"/w/@/@AHHW%M&8&;%MN ,,g6a   B&N/ 8 GGD5 6  " " $794467 7 8s* EA"E 0E E EE AF1c8t|jdtS)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_parentsrrBs r# getTimeoutzReactorBuilder.getTimeout:s   y:RSSr"c||j}gfd}j||}jrtd|d|j y)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. NcHjdjyN)appendr-)r&timedOutsr#r-z'ReactorBuilder.runReactor..stopXs OOD ! LLNr"zreactor still running after z seconds)r| callLaterrunrr])rCr&rzr- timedOutCallrs ` @r# runReactorzReactorBuilder.runReactorBsa" ?oo'G (($7  "%A'(#ST T    !r"clsreturnc^i}jD]jdd}jdz|zdzjdd}Gfddt}||_dj j jdd|gz|_|||j<|S)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_ceZdZWjZWWjvrWjWZ eWZy#e$rejZYywxYw)4ReactorBuilder.makeTestCaseClasses..testcaseN) rrrr;skiprr9rqrrt)rr&sr#testcaserrsX ^^ c111..w7D7%-g%6N$7"9446D7s ?AArr) _reactorssplitrreplacerrpr)rclassesshortReactorNamenamerr&s` @r#makeTestCaseClassesz"ReactorBuilder.makeTestCaseClassescs  }} 2G&}}S1"5 LL3&)99GCLLSRUVD 73 3 7!%H $'HHS-=-=-C-C-Ea-Ktf-T$UH !)1GH%% & 2 r"r)"rrrr rrr=extendrisMacOSXisLinuxr9r robject__annotations__rAr:r r rr;rstrrDrRrgrxr|r classmethodr rrr!r"r#rrmsz, 7I x    6  UV 8      C D   >@  $8##%  C6:NHXb&j129O>B$y/!:;B&(OT#s(^(Q:#@J7rT"B " # c5./6I1JJKK Lr"rrCrz%asyncioreactor.AsyncioSelectorReactorcddlm}m}mddlm}t t|}|||jfd}|jS)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. r)get_event_loopnew_event_loopset_event_looprc6jyr)close)loop originalLooprsr#cleanUpz'asyncioSelectorReactor..cleanUps |$r") asynciorrrr^rr rr3AsyncioSelectorReactor) rCrrr asTestCaserrrrs @@@r#asyncioSelectorReactorrsaGF/)40J!#L  D4%% 1> 0 0 66r"r).r __all__rLr>rGtypingrrrr r r r r zope.interfacerr1rtwisted.python.deprecaterrotwisted.python.failurertwisted.python.reflectrtwisted.python.runtimertwisted.trial.unittestrrtwisted.trial.utilrrr^rr_processrmrOrrr7rrrrr!r"r#rs  H WWW$N*++@I04Gy#0;>QQh77,S7@0q Gs$BBB