Ϫf3dZddlZddlZddlZddlmZddlmZmZm Z m Z m Z m Z ddl mZmZmZmZddlmZ ddlZee j,e d dGdd eZGd d Zee j,e d dGd d eZGddeZdZGdde j8Zee j,e d dee j<e d dGddeZy#e$rYwxYw)zC Test methods in twisted.internet.threads and reactor thread APIs. N)skipIf)defererror interfacesprotocolreactorthreads)failurelog threadable threadpool)TestCasez(No thread support, nothing to test here.cLeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z y ) ReactorThreadsTestsz. Tests for the reactor threading API. ctjd|jtjjdtjd|jtjjdy)z: Try to change maximum number of threads. "N)rsuggestThreadPoolSize assertEqualr maxselfs ;/usr/lib/python3/dist-packages/twisted/test/test_threads.pytest_suggestThreadPoolSizez.ReactorThreadsTests.test_suggestThreadPoolSize"sX %%b) ++//4%%a( ++//3cJtjtjdS)z The reactor's threadpool is only available when the reactor is running, so to have a sane behavior during the tests we make a dummy L{threads.deferToThread} call. r)r deferToThreadtimesleeprs r_waitForThreadz"ReactorThreadsTests._waitForThread+s $$TZZ33rcLfd}jj|S)z Test callInThread functionality: set a C{threading.Event}, and check that it's not in the main thread. ctjgfd}tj|j dj sj dyjdgy)NcljtjjyN)appendr isInIOThreadset)resultwaitersr threadedFunczGReactorThreadsTests.test_callInThread..cb..threadedFunc=s! j5578 rxzTimed out waiting for event.F) threadingEventr callInThreadwaitisSetfailr)ignr*r(r)rs @@rcbz1ReactorThreadsTests.test_callInThread..cb9s]__&FF    . KK <<> 89  %1rr addCallbackrr3s` rtest_callInThreadz%ReactorThreadsTests.test_callInThread3s$ 2""$0044rcFd}|jj|S)zk Test callFromThread functionality: from the main thread, and from another thread. ctj}tjfd}tj|tj|j dtj |gdS)NcFtjjdyr$)rcallFromThreadcallback)firedByOtherThreadsrr*zIReactorThreadsTests.test_callFromThread..cb..threadedFuncTs&&'9'B'BDIrT)fireOnOneErrback)rDeferredrr.r;r< DeferredList)r2firedByReactorThreadr*r=s @rr3z3ReactorThreadsTests.test_callFromThread..cbPsh#(>>#3 !&!1  J   .  " "#7#@#@$ G%%%'9:T rr4r6s rtest_callFromThreadz'ReactorThreadsTests.test_callFromThreadJs# ""$0044rcLfd}jj|S)zT Try to make an overflow on the reactor waker using callFromThread. c.d_tjfd}tj|j dj sjdjtjjSy)NctdD]} tjdj y#t$rt j _Y6wxYw)Nicyr$rGrrz^ReactorThreadsTests.test_wakerOverflow..cb..threadedFunction..nr)rangerr; BaseExceptionr Failurer')irr)s rthreadedFunctionzLReactorThreadsTests.test_wakerOverflow..cb..threadedFunctionisXvA..|<  )'.'8 s:"A A r+Timed out waiting for event) r r,r-rr.r/r0r1r)r2rNr)rs @rr3z2ReactorThreadsTests.test_wakerOverflow..cbespDL__&F   !1 2 KK <<> 78||'zz$,,//(rr4r6s` rtest_wakerOverflowz&ReactorThreadsTests.test_wakerOverflow`s$  0,""$0044rctjggfd}fd}jj|j |S)zK Utility method to test L{threads.blockingCallFromThread}. cfd}tj|tjjj S)Nc tjt}j|j y#t$r}j|Yd}~0d}~wwxYwr$)r blockingCallFromThreadrr% Exceptionr')reerrors reactorFuncresultsr)s rr*zRReactorThreadsTests._testBlockingCallFromThread..cb1..threadedFuncsR&66w LANN1% !%MM!$$%s? A#AA#)rr.r rr/ getTimeout)r2r*rXrYrZrr)s rcb1z.cb1s6    .((doo6GH HrcNjsjdfS)NrO)r0r1)r2rXrZrr)s rcb2z.cb2s#<<> 78F? "r)r,r-r r5addBoth)rrYr\r^rXrZr)s`` @@@r_testBlockingCallFromThreadz/ReactorThreadsTests._testBlockingCallFromThread}sS" I I # ""$005==cBBrcTd}fd}j|j|S)z Test blockingCallFromThread facility: create a thread, call a function in the reactor using L{threads.blockingCallFromThread}, and verify the result returned. c,tjdS)Nfoo)rsucceedrGrrrYzDReactorThreadsTests.test_blockingCallFromThread..reactorFuncs==' 'rc6j|dddy)Nrrcrresrs rr3z;ReactorThreadsTests.test_blockingCallFromThread..cb   SVAY .rr`r5rrYr3s` rtest_blockingCallFromThreadz/ReactorThreadsTests.test_blockingCallFromThreads+ ( /// <HHLLrcTd}fd}j|j|S)zx Test blockingCallFromThread as above, but be sure the resulting Deferred is not already fired. cptj}tjd|jd|S)N皙?egg)rr?r callLaterr<ds rrYzIReactorThreadsTests.test_asyncBlockingCallFromThread..reactorFuncs) A   c1::u 5Hrc6j|dddy)Nrrprfrgs rr3z@ReactorThreadsTests.test_asyncBlockingCallFromThread..cbrirrjrks` r test_asyncBlockingCallFromThreadz4ReactorThreadsTests.test_asyncBlockingCallFromThreads+    /// <HHLLrcTd}fd}j|j|S)z? Test error report for blockingCallFromThread. c>tjtdS)Nbar)rr1 RuntimeErrorrGrrrYzIReactorThreadsTests.test_errorBlockingCallFromThread..reactorFuncs::l512 2rcj|ddtj|ddjddy)NrrxassertIsInstanceryrargsrgs rr3z@ReactorThreadsTests.test_errorBlockingCallFromThread..cbs<  ! !#a&)\ :   SVAY^^A. 6rrjrks` r test_errorBlockingCallFromThreadz4ReactorThreadsTests.test_errorBlockingCallFromThreads+  3 7// <HHLLrcTd}fd}j|j|S)z Test error report for blockingCallFromThread as above, but be sure the resulting Deferred is not already fired. ctj}tjd|jt d|S)Nrospam)rr?rrqerrbackryrrs rrYzNReactorThreadsTests.test_asyncErrorBlockingCallFromThread..reactorFuncs. A   c199l6.B CHrcj|ddtj|ddjddy)Nr{rrr|rgs rr3zEReactorThreadsTests.test_asyncErrorBlockingCallFromThread..cbs<  ! !#a&)\ :   SVAY^^A. 7rrjrks` r%test_asyncErrorBlockingCallFromThreadz9ReactorThreadsTests.test_asyncErrorBlockingCallFromThreads+    8// <HHLLrN)__name__ __module__ __qualname____doc__rr r7rBrPr`rlrurrrGrrrrs> 445.5,5:C8 MM MMrrceZdZdZdZdZy)Counterrcn|jdz}||jdzk7r d|_t||_y)zA non thread-safe method.r{N)indexproblem ValueError)rnexts raddz Counter.adds5zzA~ 4::> !DL   rN)rrrrrrrGrrrrs EG rrc4eZdZdZdZdZdZdZdZdZ y) DeferredResultTestsz( Test twisted.internet.threads. c.tjdy)Nrrrs rsetUpzDeferredResultTests.setUp%%a(rc.tjdy)Nrrrs rtearDownzDeferredResultTests.tearDownrrcgdtjfd}tjt Dcgc]}j |fifc}t j|fifgzScc}w)zW L{threads.callMultipleInThread} calls multiple functions in a thread. cpjttjdyr$)rlistrJr<)LNrsrsrfinishedz7DeferredResultTests.test_callMultiple..finisheds'   QU1X / JJt r)rr?r callMultipleInThreadrJr%rr;)rrrMrrrss` @@@rtest_callMultiplez%DeferredResultTests.test_callMultiplesu   NN   $$+08 4aahhb ! 4&& R89 :  5sA7crtjdddd}|j|jd|S)z L{threads.deferToThread} executes the function passed, and correctly handles the positional and keyword arguments given. c ||zSr$rGxys rrHz9DeferredResultTests.test_deferredResult..s Qrrr)r rr5rrrss rtest_deferredResultz'DeferredResultTests.test_deferredResult s2  ! !"6Q ? d&&*rczGddtfd}tj|}|j|S)z Check that L{threads.deferToThread} return a failure object with an appropriate exception instance when the called function raises an exception. c eZdZy):DeferredResultTests.test_deferredFailure..NewErrorNrrrrGrrNewErrorr rrcr$rGrsr raiseErrorz.raiseError * r)rUr r assertFailurerrrsrs @rtest_deferredFailurez(DeferredResultTests.test_deferredFailures9 y    ! !* -!!!X..rc~tjd}|jd|j|tS)z Check that a successful L{threads.deferToThread} followed by a one that raises an exception correctly result as a failure. cyr$rGrGrrrHzFDeferredResultTests.test_deferredFailureAfterSuccess..3rIrc.tjdS)Nc ddzS)Nr{rrGrGrrrHzXDeferredResultTests.test_deferredFailureAfterSuccess....4s Qr)r r)r2s rrHzFDeferredResultTests.test_deferredFailureAfterSuccess..4s'"7"7"Gr)r rr5rZeroDivisionErrorrs r test_deferredFailureAfterSuccessz4DeferredResultTests.test_deferredFailureAfterSuccess$s5  ! !, / GH!!!%677rN) rrrrrrrrrrrGrrrrs% ))$/ 8rrc(eZdZdZdZdZdZdZy)DeferToThreadPoolTestsz= Test L{twisted.internet.threads.deferToThreadPool}. cntjdd|_|jjy)Nrr)r ThreadPooltpstartrs rrzDeferToThreadPoolTests.setUp=s#''1-  rc8|jjyr$)rstoprs rrzDeferToThreadPoolTests.tearDownAs  rctjt|jdddd}|j |j d|S)z L{threads.deferToThreadPool} executes the function passed, and correctly handles the positional and keyword arguments given. c ||zSr$rGrs rrHz.Is q1urrrrrr)r deferToThreadPoolrrr5rrs rrz*DeferToThreadPoolTests.test_deferredResultDs;  % %gtww8LaST U d&&*rcGddtfd}tjt|j|}|j |S)z Check that L{threads.deferToThreadPool} return a failure object with an appropriate exception instance when the called function raises an exception. c eZdZy)=DeferToThreadPoolTests.test_deferredFailure..NewErrorNrrGrrrrTrrrcr$rGrsrrz?DeferToThreadPoolTests.test_deferredFailure..raiseErrorWrr)rUr rrrrrs @rrz+DeferToThreadPoolTests.test_deferredFailureMsA y    % %gtww C!!!X..rN)rrrrrrrrrGrrrr8s/rra_ import time import %(reactor)s %(reactor)s.install() from twisted.internet import reactor def threadedCall(): print('threaded call') reactor.callInThread(threadedCall) # Spin very briefly to try to give the thread a chance to run, if it # is going to. Is there a better way to achieve this behavior? for i in range(100): time.sleep(0.0) c$eZdZdZdZdZdZy)ThreadStartupProcessProtocolc.||_g|_g|_yr$)routerr)rrs r__init__z%ThreadStartupProcessProtocol.__init__rs  rc:|jj|yr$)rr%)rrs r outReceivedz(ThreadStartupProcessProtocol.outReceivedw rc:|jj|yr$)rr%)rrs r errReceivedz(ThreadStartupProcessProtocol.errReceivedzrrch|jj|j|j|fyr$)rr<rr)rreasons r processEndedz)ThreadStartupProcessProtocol.processEnded}s# $((F;.programFinishedsl!' S#v||E334 ?uF3%qQR?uEF   S$KC5"Q Rrch|jtjjd|S)NKILL)trapr TimeoutError signalProcess)rprotos rprogramTimeoutzLStartupBehaviorTests.testCallBeforeStartupUnexecuted..programTimeouts( HHU'' (    'Jr PYTHONPATHpython)mktempopenwrite_callBeforeStartupProgramrrosenvironcopypathsepjoinsyspathrr? addCallbacksr spawnProcess executable)rprognameprogfilerrenvrsrs` @rtestCallBeforeStartupUnexecutedz4StartupBehaviorTests.testCallBeforeStartupUnexecuteds;;= (C  XH NN4 7CUCU7VV W X S  jjooJJOOCHH5L NN  ) )/> J,Q/UCNNXx4H#N- X Xs )DD N)rrrrrrGrrrrs  rr) rrrrunittestrtwisted.internetrrrrrr twisted.pythonr r r r twisted.trial.unittestrr, ImportErrorIReactorThreadsrrrrrProcessProtocolrIReactorProcessrrGrrrsO   QQ??+  " ""7D11.xM(xM xMv$" ""7D11.G8(G8 G8T#/X#/L& =8#;#; = " ""7D11." ""7D11=&8&  &i   sDD  D