ϪfdZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z ddl mZGd d eZej#ej%y) z2 Tests for implementations of L{IReactorThreads}. N)ref)IReactorThreads)ReactorBuilder) isInIOThread) ThreadPool)VersioncReZdZdZefZdZdZdZdZ dZ dZ dZ d Z d Zd Zy ) ThreadTestsBuilderzD Builder for defining tests relating to L{IReactorThreads}. cg|jj|jt|j j dfd}j ||jd|jdd|j dd|jjdy ) z C{reactor.getThreadPool()} returns an instance of L{ThreadPool} which starts when C{reactor.run()} is called and stops before it returns. z(Pool should not start before reactor.runcjjjjjyN)appendstartedjoinedstop)poolreactorstatesD/usr/lib/python3/dist-packages/twisted/internet/test/test_threads.pyfz0ThreadTestsBuilder.test_getThreadPool..f's, LL & LL % LLNrz#Pool should start after reactor.runz-Pool should not be joined before reactor.stopz0Pool should be stopped after reactor.run returnsN) buildReactor getThreadPoolassertIsInstancer assertFalsercallWhenRunning runReactor assertTruer)selfrrrrs @@@rtest_getThreadPoolz%ThreadTestsBuilder.test_getThreadPools ##%$$& dJ/ 'QR  " # a"GH q#RS  %WXrc|j}|jd|j}|j|jdy)zm C{reactor.suggestThreadPoolSize()} sets the maximum size of the reactor threadpool. N)rsuggestThreadPoolSizer assertEqualmax)r!rrs rtest_suggestThreadPoolSizez-ThreadTestsBuilder.test_suggestThreadPoolSize4sB ##%%%b)$$& 2&rc|jfd}jdj|j}|j dj}|j ||z dky)aR A function scheduled with L{IReactorThreads.callFromThread} invoked from a delayed call is run immediately in the next reactor iteration. When invoked from the reactor thread, previous implementations of L{IReactorThreads.callFromThread} would skip the pipe/socket based wake up step, assuming the reactor would wake up on its own. However, this resulted in the reactor not noticing an insert into the thread queue at the right time (in this case, after the thread queue has been processed for that reactor iteration). c&jyr )r)rsr threadCallzAThreadTestsBuilder.test_delayedCallFromThread..threadCallLs LLNrr<N)r callLatercallFromThreadsecondsrr )r!r+beforeafterrs @rtest_delayedCallFromThreadz-ThreadTestsBuilder.test_delayedCallFromThread>so##%  !W33Z@" $!$ +,rc|jgfd}jdjj||j d|j t jgy)z A function scheduled with L{IReactorThreads.callFromThread} invoked from another thread is run in the reactor thread. cljtjjyr )r threadingcurrent_threadr)rresultsrr+z:ThreadTestsBuilder.test_callFromThread..threadCallps! MM)224 5 LLNrrN)rr. callInThreadr/rr&r6r7)r!r+rr8s @@rtest_callFromThreadz&ThreadTestsBuilder.test_callFromThreadhsi ##%  !W1173I3I:V # )":":"rc|j}t|j}|j|j|j |t j|j|y)a0 When the reactor stops, L{ReactorBase._stopThreadPool} drops the reactor's direct reference to its internal threadpool and removes the associated startup and shutdown triggers. This is the case of the thread pool being created before the reactor is run. N) rrrrrrgccollect assertIsNone)r!r threadpools rtest_stopThreadPoolz&ThreadTestsBuilder.test_stopThreadPoolys\##%..01  -    *,'rc|jgfd}j||jtj|j dy)a We must handle the case of shutting down the thread pool when it was started after the reactor was run in a special way. Some implementation background: The thread pool is started with callWhenRunning, which only returns a system trigger ID when it is invoked before the reactor is started. This is the case of the thread pool being created after the reactor is started. cvjtjjyr )rrrr)rthreadPoolRefssracquireThreadPoolz[ThreadTestsBuilder.test_stopThreadPoolWhenStartedAfterReactorRan..acquireThreadPools(  ! !#g&;&;&="> ? LLNrrN)rrrr=r>r?)r!rErrDs @@r-test_stopThreadPoolWhenStartedAfterReactorRanz@ThreadTestsBuilder.test_stopThreadPoolWhenStartedAfterReactorRans\##%   12    +.+-.rc|j}t|j}|jdt j |j |y)a[ When the reactor has its shutdown event fired before it is run, the thread pool is completely destroyed. For what it's worth, the reason we support this behavior at all is because Trial does this. This is the case of the thread pool being created without the reactor being started at al. shutdownN)rrrfireSystemEventr=r>r?)r!r threadPoolRefs r,test_cleanUpThreadPoolEvenBeforeReactorIsRunz?ThreadTestsBuilder.test_cleanUpThreadPoolEvenBeforeReactorIsRunsL##%G1134  +  -/*rcg|jfd}j||j|jdgy)z The reactor registers itself as the I/O thread when it runs so that L{twisted.python.threadable.isInIOThread} returns C{True} if it is called in the thread the reactor is running in. cXjtjyr )rrrrresultssrcheckz3ThreadTestsBuilder.test_isInIOThread..checks NN<> * LLNrTN)rrrr&r!rPrrOs @@rtest_isInIOThreadz$ThreadTestsBuilder.test_isInIOThreadsK ##%  &   $)rcg|jfd}j||j|jdgy)z The reactor registers itself as the I/O thread when it runs so that L{twisted.python.threadable.isInIOThread} returns C{False} if it is called in a different thread than the reactor is running in. cnjtjjyr )rrr/rrNsrrPz6ThreadTestsBuilder.test_isNotInIOThread..checks# NN<> *  " "7<< 0rFN)rr:rr&rQs @@rtest_isNotInIOThreadz'ThreadTestsBuilder.test_isNotInIOThreadsK ##% 1 U#   %'*rcb|jtdddddftjy)NTwistedrrzthreading.current_thread)versionr)callDeprecatedrr currentThread)r!s r&test_threadPoolCurrentThreadDeprecatedz9ThreadTestsBuilder.test_threadPoolCurrentThreadDeprecateds6  2q!,*&&  rN)__name__ __module__ __qualname____doc__rrequiredInterfacesr"r(r3r;rArFrKrRrUr\rrr r sF*+Y2'(-T?"( /0+"*"+" rr )r`r=r6weakrefrtwisted.internet.interfacesr#twisted.internet.test.reactormixinsrtwisted.python.threadablertwisted.python.threadpoolrtwisted.python.versionsrr globalsupdatemakeTestCaseClassesrbrrrlsO  7>20+G G T #779:r