Ϫf9dZddlZddlmZddlmZddlmZddlm Z ddlm Z ddl m Z dd l mZmZmZdd lmZdd lmZdd lmZmZmZdd lmZddlmZddlmZmZ ddl Z!e!Z e dZ#e eeGddZ$GddeZ%dZ&GddZ'Gdde'eZ(Gdde'eZ)GddeZ*ee dGdd eZ+y#e"$rdZ Y{wxYw)!z% Tests for L{twisted.internet.base}. N)Queue)Callable)skipIf) implementer) ParamSpec) FirstOneWins) DelayedCall ReactorBaseThreadedResolver)Deferred)DNSLookupError)IReactorThreads IReactorTimeIResolverSimple)Clock) ThreadPool)SkipTestTestCase_PceZdZdZdZdeeefdejdejddfdZ d Z d Z d Z defd Zdeeefdejdejddfd ZdZy) FakeReactorzl A fake reactor implementation which just supports enough reactor APIs for L{ThreadedResolver}. ct_jj_t_jj fd_t_y)NcjSN) _threadpoolselfsA/usr/lib/python3/dist-packages/twisted/internet/test/test_base.pyz&FakeReactor.__init__..1s T%5%5) r_clock callLaterrrstart getThreadPoolr _threadCallsrs`r__init__zFakeReactor.__init__+sKg ..%<  5!Gr callableargskwargsreturnNc@|jj|||fyr)r%putrr'r(r)s rcallFromThreadzFakeReactor.callFromThread5s xv67r cP|jj\}}}||i|yr)r%get)rfr(r)s r_runThreadCallszFakeReactor._runThreadCalls:s)++//14 46r c8|jjyr)rstoprs r_stopzFakeReactor._stop>s r cyrrs rgetDelayedCallszFakeReactor.getDelayedCallsA r cyrr7rs rsecondszFakeReactor.secondsEr9r cyrr7r-s r callInThreadzFakeReactor.callInThreadIs r cyrr7)rsizes rsuggestThreadPoolSizez!FakeReactor.suggestThreadPoolSizeOr9r )__name__ __module__ __qualname____doc__r&rrobjectr(r)r.r2r5r8floatr;r=r@r7r rrr$s $8 V,857WW8HJ 8 8       V, 57WW HJ    r rc(eZdZdZdZdZdZdZy)ThreadedResolverTestsz( Tests for L{ThreadedResolver}. c dd}d}t}|j|jg g} fd}|jtd|t |}|j ||f}|j|j|j|j |g|j|g|jj|dz|j|jjgy)z L{ThreadedResolver.getHostByName} returns a L{Deferred} which fires with the value returned by the call to L{socket.gethostbyname} in the threadpool of the reactor passed to L{ThreadedResolver.__init__}. z 10.0.0.17zfoo.bar.example.comc*j|Sr)append)nameiplookedUps rfakeGetHostByNamez=ThreadedResolverTests.test_success..fakeGetHostByNameis OOD !Ir gethostbynameN)r addCleanupr5patchsocketr getHostByName addCallbackrLr2 assertEqualr!advancecalls) rrMtimeoutreactor resolvedTorPresolverdrNrOs @@r test_successz"ThreadedResolverTests.test_successYs $-  &   6?,=>#G,  " "4' 4 j''(! D6* bT* w{+ --r2r cd}t}|j|jd}|jtd|g}t |}|j d|f}|j|t|j|j|j|jt|d|jj|dz|j|jj gy)z L{ThreadedResolver.getHostByName} returns a L{Deferred} which fires a L{Failure} if the call to L{socket.gethostbyname} raises an exception. rJctd)NzENOBUFS (this is a funny joke))OSError)rMs rrPz=ThreadedResolverTests.test_failure..fakeGetHostByNames:; ;r rQ some.namerRN)rrSr5rTrUr rV assertFailurer rWrLr2rXlenr!rYrZ)rr[r\rP failedWithr^r_s r test_failurez"ThreadedResolverTests.test_failure|s -  & < 6?,=> #G,  " "; ; 1n- j''(! Z!, w{+ --r2r cBd}t}|j|jtfd}|j t d|g}t |}|jd|f}|j|t|j|j|jj|dz |j|g|jjd|jt|dj!t#dy)z If L{socket.gethostbyname} does not complete before the specified timeout elapsed, the L{Deferred} returned by L{ThreadedResolver.getHostByName} fails with L{DNSLookupError}. c$jr)r0)rMresults rrPz=ThreadedResolverTests.test_timeout..fakeGetHostByNames**, r rQrdrRzThe I/O was errorfulN)rrSr5rrTrUr rVrer rWrLr!rYrXrfr,IOError)rr[r\rPrgr^r_rls @r test_timeoutz"ThreadedResolverTests.test_timeouts -  &  6?,=> #G,  " "; ; 1n- j''(w{+ R(q! Z!,  7123r cJgttGfdd}Gddt}|}|}|j|t t }|j j|d|j j|d|j j|d|j j|d|j j|d|jtd |jttttgd z|jdd |jdd |jd d |jdd|jddy)zm L{ThreadedResolver.getHostByName} is passed L{str}, encoded using IDNA if required. ceZdZdfd Zy)AThreadedResolverTests.test_resolverGivenStr..FakeResolverc:j|tSr)rLr )rrMtimeoutsrZs rrVzOThreadedResolverTests.test_resolverGivenStr..FakeResolver.getHostByNames T"z!r N)r7)rArBrCrV)rZsr FakeResolverrqs "r rtceZdZdZy)FThreadedResolverTests.test_resolverGivenStr..JustEnoughReactorcyrr7rs r installWakerzSThreadedResolverTests.test_resolverGivenStr..JustEnoughReactor.installWakersr N)rArBrCrxr7r rJustEnoughReactorrvs r ryzexample.exampleuvääntynyt.exampleu рф.examplezxn----7sbb4ac0ad0be6cf.xn--p1airrRzxn--vntynyt-5waa.examplezxn--p1ai.exampleN)rrr installResolverrr nameResolverresolveHostNamerXrflistmaptypestr)rrtryfaker\recrZs @rtest_resolverGivenStrz+ThreadedResolverTests.test_resolverGivenStrso  _ % " " & "    ~#%%8:&,,S2CD,,S2CD,,S2KL,,S2HI,,S2ST UQ' c$./#; *E!H5 *E!H5 3U1X> +U1X6 :E!HEr N)rArBrCrDr`rhrnrr7r rrHrHTs!3F3:4B!Fr rHcy)z8 Function used by L{DelayedCallTests.test_str}. Nr7r7r rnothingrr cLeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z y ) DelayedCallMixin L{DelayedCall} c ,d}t|ddi||dS)z Get a L{DelayedCall} instance at a given C{time}. @param time: The absolute time at which the returned L{DelayedCall} will be scheduled. cyrr7)calls rnoopz0DelayedCallMixin._getDelayedCallAt..noops r cyrr7r7r rrz4DelayedCallMixin._getDelayedCallAt..rr r7N)r )rtimers r_getDelayedCallAtz"DelayedCallMixin._getDelayedCallAts  4r2tT4HHr c\|jd|_|jd|_y)zb Create two L{DelayedCall} instanced scheduled to run at different times. rrRN)rzerooners rsetUpzDelayedCallMixin.setUps( **1- ))!,r c tdtdddiddd}|jt|dt |fzy) The string representation of a L{DelayedCall} instance, as returned by L{str}, includes the unsigned id of the instance, as well as its state, the function to be called, and the function arguments. r|ArzNcyNg?r7r7r rrz+DelayedCallMixin.test_str..rr z?)r rrXridrdcs rtest_strzDelayedCallMixin.test_strsD WdS!HdD+ N  G M"vi  r c |tdtdddiddd}|jt|t |y)z The string representation of a L{DelayedCall} instance, as returned by {repr}, is identical to that returned by L{str}. )r Ncy)Ng?r7r7r rrz,DelayedCallMixin.test_repr..rr )r rrXrreprrs r test_reprzDelayedCallMixin.test_repr s6 WdS!HdD+ N R$r(+r c|j|j}}|j||k|j||k|j||k|j||ky)z For two instances of L{DelayedCall} C{a} and C{b}, C{a < b} is true if and only if C{a} is scheduled to run before C{b}. Nrr assertTrue assertFalserrrs rtest_ltzDelayedCallMixin.test_lts[ IItxxc s # t$ % s#r c|j|j}}|j||k|j||k|j||k|j||ky)z For two instances of L{DelayedCall} C{a} and C{b}, C{a <= b} is true if and only if C{a} is scheduled to run before C{b} or at the same time as C{b}. Nrrs rtest_lezDelayedCallMixin.test_lesW IItxxc  $ %  % s #r c|j|j}}|j||kD|j||kD|j||kD|j||kDy)z For two instances of L{DelayedCall} C{a} and C{b}, C{a > b} is true if and only if C{a} is scheduled to run after C{b}. Nrrs rtest_gtzDelayedCallMixin.test_gt*s[ IItxxc d # $ % s#r c|j|j}}|j||k\|j||k\|j||k\|j||k\y)z For two instances of L{DelayedCall} C{a} and C{b}, C{a > b} is true if and only if C{a} is scheduled to run after C{b} or at the same time as C{b}. Nrrs rtest_gezDelayedCallMixin.test_ge5sW IItxxc t $ %  % s #r c|j|j|jk(|j|j|jk(|j|j|jk(y)zD A L{DelayedCall} instance is only equal to itself. N)rrrrrs rtest_eqzDelayedCallMixin.test_eqAsQ dhh./  TYY./ DHH,-r c|j|j|jk7|j|j|jk7|j|j|jk7y)zM A L{DelayedCall} instance is not equal to any other object. N)rrrrrs rtest_nezDelayedCallMixin.test_neKsS  TXX-. dii/0 TXX-.r N)rArBrCrDrrrrrrrrrrr7r rrrs: I-  , $ $ $ $./r rc"eZdZdZdZdZdZy)DelayedCallNoDebugTestsrc\|jtddtj|y)z! Turn debug off. debugFNrTr rrrs rrzDelayedCallNoDebugTests.setUp[s" ;/t$r c tdtdddiddd}djt|}|j t ||y)rrrrrzNcyrr7r7r rrz2DelayedCallNoDebugTests.test_str..hrr zA)r rformatrrXr)rrexpecteds rrz DelayedCallNoDebugTests.test_strbsK WdS!HdD+ N %vbf~  R(+r c ~tdddittd}d|_|jdt |y)z If L{DelayedCall.debug} changes from C{0} to C{1} between L{DelayeCall.__init__} and L{DelayedCall.__repr__} then L{DelayedCall.__repr__} returns a string that does not include the creator stack. r|cyrr7r7r rrz.vrr r7cyNr{r7r7r rrz.vrr rRtraceback at creationN)r rr assertNotInrrs rtest_switchToDebugz*DelayedCallNoDebugTests.test_switchToDebugos6L"b'7I N 0$r(;r N)rArBrCrDrrrr7r rrrVs% , .rr z\.rr r7cyrr7r7r rrz.rr rrN)r rrassertInrrs rtest_switchFromDebugz*DelayedCallDebugTests.test_switchFromDebugs4L"b'7I N -tBx8r N)rArBrCrDrrrr7r rrr{s% 2 9r rceZdZdZdZy)TestSpySignalCapturingReactorza Subclass of ReactorBase to capture signals delivered to the reactor for inspection. cy)z* Required method, unused. Nr7rs rrxz*TestSpySignalCapturingReactor.installWakerrr N)rArBrCrDrxr7r rrrs   r rzsignal module not availablec(eZdZdZdZdZdZdZy)ReactorBaseSignalTestszE Tests to exercise ReactorBase's signal exit reporting path. cPt}|jd|jy)zI The default value of the _exitSignal attribute is None. N)rassertIs _exitSignalrr\s rtest_exitSignalDefaultsToNonez4ReactorBaseSignalTests.test_exitSignalDefaultsToNones 01 dG//0r ct}|jtjd|j tj|j y)zn ReactorBase's SIGINT handler saves the value of SIGINT to the _exitSignal attribute. N)rsigIntsignalSIGINT assertEqualsrrs rtest_captureSIGINTz)ReactorBaseSignalTests.test_captureSIGINTs8 01v}}d+ &--)<)<=r ct}|jtjd|j tj|j y)zp ReactorBase's SIGTERM handler saves the value of SIGTERM to the _exitSignal attribute. N)rsigTermrSIGTERMrrrs rtest_captureSIGTERMz*ReactorBaseSignalTests.test_captureSIGTERMs8 01- &..'*=*=>r cttds tdt}|j tj d|j tj |jy)zr ReactorBase's SIGBREAK handler saves the value of SIGBREAK to the _exitSignal attribute. SIGBREAKz$signal module does not have SIGBREAKN)hasattrrrrsigBreakrrrrs rtest_captureSIGBREAKz+ReactorBaseSignalTests.test_captureSIGBREAKsP vz*AB B/1$/ &//7+>+>?r N)rArBrCrDrrrrr7r rrrs1>? @r r),rDrUqueuertypingrunittestrzope.interfacerrtwisted.internet._resolverrtwisted.internet.baser r r twisted.internet.deferr twisted.internet.errorr twisted.internet.interfacesrrrtwisted.internet.taskrtwisted.python.threadpoolrtwisted.trial.unittestrrr_signal ImportErrorrrrHrrrrrrr7r rrs&3LL+1VV'05Ft_ \?+, , ,, ^GFHGFT o/o/d"<."