Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/internet/test/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyc


ZOc@sdZeZddlmZddlZddlZddlmZddl	m
Z
ddlmZde
fdYZ
eje
jdS(	s2
Tests for implementations of L{IReactorThreads}.
i(trefN(tisInIOThread(tReactorBuilder(t
ThreadPooltThreadTestsBuildercBs_eZdZdZdZdZdZdZdZdZ	dZ
d	ZRS(
sD
    Builder for defining tests relating to L{IReactorThreads}.
    csg|jj|jt|jjdfd}j||jd|jdd|jdd|jj	dd	S(
s
        C{reactor.getThreadPool()} returns an instance of L{ThreadPool} which
        starts when C{reactor.run()} is called and stops before it returns.
        s(Pool should not start before reactor.runcs.jjjjjdS(N(tappendtstartedtjoinedtstop((tstatetreactortpool(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pytf#siis#Pool should start after reactor.runis-Pool should not be joined before reactor.stops0Pool should be stopped after reactor.run returnsN(
tbuildReactort
getThreadPooltassertIsInstanceRtassertFalseRtcallWhenRunningt
runReactort
assertTrueR(tselfR((R
R	RsF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyttest_getThreadPools 

cCs<|j}|jd|j}|j|jddS(sm
        C{reactor.suggestThreadPoolSize()} sets the maximum size of the reactor
        threadpool.
        iN(R
tsuggestThreadPoolSizeRtassertEqualtmax(RR
R((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyttest_suggestThreadPoolSize5s
cst|jfd}jdj|j}|jdj}|j||dkdS(sQ
        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 a insert into the thread queue at
        the right time (in this case, after the thread queue has been processed
        for that reactor iteration).
        csjdS(N(R((R
(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyt
threadCallNsii<iN(R
t	callLatertcallFromThreadtsecondsRR(RRtbeforetafter((R
sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyttest_delayedCallFromThread@scsm|jgfd}jdjj||jd|jtjgdS(s
        A function scheduled with L{IReactorThreads.callFromThread} invoked
        from another thread is run in the reactor thread.
        cs!jtjjdS(N(Rt	threadingt
currentThreadR((tresultR
(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyRssiiN(R
RtcallInThreadRRtassertEqualsR!R"(RR((R
R#sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyttest_callFromThreadks
cCs\|j}t|j}|j|j|j|tj|j|ddS(s0
        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(
R
RRRRRtgctcollecttassertIdenticaltNone(RR
t
threadpool((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyttest_stopThreadPool}s	

csc|jgfd}j||jtj|jdddS(s
        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.
        cs'jtjjdS(N(RRRR((tthreadPoolRefsR
(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pytacquireThreadPoolsiN(R
RRR'R(R)R*(RR.((R
R-sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyt-test_stopThreadPoolWhenStartedAfterReactorRans


cCsL|j}t|j}|jdtj|j|ddS(s[
        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.
        tshutdownN(R
RRtfireSystemEventR'R(R)R*(RR
t
threadPoolRef((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyt,test_cleanUpThreadPoolEvenBeforeReactorIsRuns


csUg|jfd}j||j|jtgdS(s
        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.
        csjtjdS(N(RRR((tresultsR
(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pytchecksN(R
RRRtTrue(RR5((R4R
sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyttest_isInIOThreads

csUg|jfd}j||j|jtgdS(s
        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.
        cs$jtjjdS(N(RRRR((R4R
(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyR5sN(R
R$RRtFalse(RR5((R4R
sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyttest_isNotInIOThreads

(t__name__t
__module__t__doc__RRR R&R,R/R3R7R9(((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyRs			+					(R<ttypet
__metaclass__tweakrefRR'R!ttwisted.python.threadableRt#twisted.internet.test.reactormixinsRttwisted.python.threadpoolRRtglobalstupdatetmakeTestCaseClasses(((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyt<module>s