Your IP : 172.28.240.42


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


=Nc@sdZddlZddlZddlZddlmZddlmZmZm	Z	m
Z
mZmZddl
mZmZmZmZdejfdYZddd	YZd
ejfdYZdejfd
YZdZdejfdYZdejfdYZe	jeddkr^x/eeefD]Zde_qHWnddlZe	j eddkrxefD]Zde_qWndS(sC
Test methods in twisted.internet.threads and reactor thread APIs.
iN(tunittest(treactortdefert
interfacestthreadstprotocolterror(tfailuret
threadabletlogt
threadpooltReactorThreadsTestCasecBsheZdZdZdZdZdZdZdZdZ	dZ
d	Zd
ZRS(s.
    Tests for the reactor threading API.
    cCsJtjd|jtjjdtjd|jtjjddS(s:
        Try to change maximum number of threads.
        i"iN(RtsuggestThreadPoolSizetassertEqualR
tmax(tself((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyttest_suggestThreadPoolSizes

cCstjtjdS(s
        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.
        i(Rt
deferToThreadttimetsleep(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyt_waitForThread!scs"fd}jj|S(s
        Test callInThread functionality: set a C{threading.Event}, and check
        that it's not in the main thread.
        csqtjgfd}tj|jdjsZjdnjtgdS(Ncs!jtjjdS(N(tappendRtisInIOThreadtset((twaitertresult(s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pytthreadedFunc2sixsTimed out waiting for event.(	t	threadingtEventRtcallInThreadtwaittisSettfailR
tFalse(tignR(R(RRs=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pytcb/s

(RtaddCallback(RR#((Rs=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyttest_callInThread*s
cCsd}|jj|S(sk
        Test callFromThread functionality: from the main thread, and from
        another thread.
        cs`tj}tjfd}tj|tj|jdtj|gdtS(NcstjjddS(N(RtcallFromThreadtcallbacktNone((tfiredByOtherThread(s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyRHstfireOnOneErrback(	RtDeferredRRR&R'R(tDeferredListtTrue(R"tfiredByReactorThreadR((R)s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR#Ds
(RR$(RR#((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyttest_callFromThread?s	
cs"fd}jj|S(sT
        Try to make an overflow on the reactor waker using callFromThread.
        csd_tjfd}tj|jdjs]jdnjdk	r|t	jjSdS(NcsSxBtdD]4}ytjdWq
tj_Pq
Xq
WjdS(NicSsdS(N(R((((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyt<lambda>`s(txrangeRR&RtFailureR(ti(RR(s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pytthreadedFunction[s	ixsTimed out waiting for event(
R(RRRRRRRR R(R"R4(R(Rs=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR#Xs	


(RR$(RR#((Rs=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyttest_wakerOverflowTscsgtjggfd}fd}jj|j|S(sK
        Utility method to test L{threads.blockingCallFromThread}.
        cs>fd}tj|tjjjS(NcsTytjt}Wn tk
r8}j|nXj|jdS(N(RtblockingCallFromThreadRt	ExceptionRR(trte(terrorsRtresultstreactorFunc(s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyRus
(RRRRRt
getTimeout(R"R(R:RRR;R<(s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pytcb1ts	
cs&jsjdnfS(NsTimed out waiting for event(RR (R"(RRR:R;(s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pytcb2s(RRRR$taddBoth(RR<R>R?((R:RRR;R<s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyt_testBlockingCallFromThreadms
cs.d}fd}j|j|S(s
        Test blockingCallFromThread facility: create a thread, call a function
        in the reactor using L{threads.blockingCallFromThread}, and verify the
        result returned.
        cSs
tjdS(Ntfoo(Rtsucceed(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR<scsj|ddddS(NiRB(R
(tres(R(s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR#s(RAR$(RR<R#((Rs=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyttest_blockingCallFromThreads	cs.d}fd}j|j|S(sx
        Test blockingCallFromThread as above, but be sure the resulting
        Deferred is not already fired.
        cSs&tj}tjd|jd|S(Ng?tegg(RR+Rt	callLaterR'(td((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR<scsj|ddddS(NiRF(R
(RD(R(s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR#s(RAR$(RR<R#((Rs=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyt test_asyncBlockingCallFromThreads	cs.d}fd}j|j|S(s?
        Test error report for blockingCallFromThread.
        cSstjtdS(Ntbar(RR tRuntimeError(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR<scsAjt|ddtj|ddjdddS(NiiRJ(tassert_t
isinstanceRKR
targs(RD(R(s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR#s(RAR$(RR<R#((Rs=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyt test_errorBlockingCallFromThreads	cs.d}fd}j|j|S(s
        Test error report for blockingCallFromThread as above, but be sure the
        resulting Deferred is not already fired.
        cSs,tj}tjd|jtd|S(Ng?tspam(RR+RRGterrbackRK(RH((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR<scsAjt|ddtj|ddjdddS(NiiRP(RLRMRKR
RN(RD(R(s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR#s(RAR$(RR<R#((Rs=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyt%test_asyncErrorBlockingCallFromThreads	(
t__name__t
__module__t__doc__RRR%R/R5RARERIRORR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyRs	
							
		tCountercBseZdZdZdZRS(icCs?|jd}||jdkr2d|_tn||_dS(sA non thread-safe method.iN(tindextproblemt
ValueError(Rtnext((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pytadds

		(RSRTRWRXR[(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyRVstDeferredResultTestCasecBsDeZdZdZdZdZdZdZdZRS(s(
    Test twisted.internet.threads.
    cCstjddS(Ni(RR(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pytsetUpscCstjddS(Ni(RR(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyttearDownscsgdtjfd}tjgtD]}j|fif^qCtj|fifgS(Ni
cs'jtjddS(N(R
trangeR'R(((RHRtLtN(s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pytfinisheds(RR+RtcallMultipleInThreadR1RRR&(RRbR3((RHRR`Ras=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyttestCallMultiples+cCs5tjddddd}|j|jd|S(s
        L{threads.deferToThread} executes the function passed, and correctly
        handles the positional and keyword arguments given.
        icSs||S(N((txty((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR0siRfii(RRR$R
(RRH((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyttest_deferredResultscsDdtfdYfd}tj|}|j|S(s
        Check that L{threads.deferToThread} return a failure object
        with an appropriate exception instance when the called
        function raises an exception.
        tNewErrorcBseZRS((RSRT(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyRhscs
dS(N(((Rh(s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyt
raiseErrors(R7RRt
assertFailure(RRiRH((Rhs=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyttest_deferredFailurescCs2tjd}|jd|j|tS(s
        Check that a successfull L{threads.deferToThread} followed by a one
        that raises an exception correctly result as a failure.
        cSsdS(N(R((((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR0scSstjdS(NcSsddS(Nii((((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR0s(RR(R"((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR0s(RRR$RjtZeroDivisionError(RRH((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyt test_deferredFailureAfterSuccesss(	RSRTRUR]R^RdRgRkRm(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR\s				
	tDeferToThreadPoolTestCasecBs2eZdZdZdZdZdZRS(s=
    Test L{twisted.internet.threads.deferToThreadPool}.
    cCs&tjdd|_|jjdS(Nii(R
t
ThreadPoolttptstart(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR] scCs|jjdS(N(Rptstop(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR^%scCs>tjt|jddddd}|j|jd|S(s
        L{threads.deferToThreadPool} executes the function passed, and
        correctly handles the positional and keyword arguments given.
        icSs||S(N((ReRf((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR0/siRfii(RtdeferToThreadPoolRRpR$R
(RRH((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyRg)scsMdtfdYfd}tjt|j|}|j|S(s
        Check that L{threads.deferToThreadPool} return a failure object with an
        appropriate exception instance when the called function raises an
        exception.
        RhcBseZRS((RSRT(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyRh:scs
dS(N(((Rh(s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyRi<s(R7RRsRRpRj(RRiRH((Rhs=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyRk4s(RSRTRUR]R^RgRk(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyRns
			s_
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 xrange(100):
    time.sleep(0.0)
tThreadStartupProcessProtocolcBs,eZdZdZdZdZRS(cCs||_g|_g|_dS(N(Rbtoutterr(RRb((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyt__init__Ws		cCs|jj|dS(N(RuR(RRu((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pytoutReceived\scCs|jj|dS(N(RvR(RRv((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyterrReceived_scCs#|jj|j|j|fdS(N(RbR'RuRv(Rtreason((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pytprocessEndedbs(RSRTRwRxRyR{(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyRtVs			tStartupBehaviorTestCasecBseZdZdZRS(s
    Test cases for the behavior of the reactor threadpool near startup
    boundary conditions.

    In particular, this asserts that no threaded calls are attempted
    until the reactor starts up, that calls attempted before it starts
    are in fact executed once it has started, and that in both cases,
    the reactor properly cleans itself up (which is tested for
    somewhat implicitly, by requiring a child process be able to exit,
    something it cannot do unless the threadpool has been properly
    torn down).
    csj}t|d}|jtitjd6|jfd}fd}tjj	}tj
jtj
|d<tjj||}t|tjtjd|f||S(NtwRcss|\}}}|jtjr;jd||fn|rXtjd|fnj|d|fdS(Ns.Process did not exit cleanly (out: %s err: %s)s'Unexpected output on standard error: %ss(Expected no output, instead received:
%s(tcheckRtProcessTerminatedR R	tmsgtfailIf(t.0RuRvRz(R(s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pytprogramFinished|scs!|jtjjd|S(NtKILL(ttrapRtTimeoutErrort
signalProcess(Rv(tproto(s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pytprogramTimeouts
t
PYTHONPATHtpython(tmktemptfiletwritet_callBeforeStartupProgramRRTtclosetostenvirontcopytpathseptjointsystpathRR+taddCallbacksRttspawnProcesst
executable(RtprognametprogfileRRtenvRH((RRs=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyttestCallBeforeStartupUnexecutedvs
(RSRTRUR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyR|gss(No thread support, nothing to test here.s7No process support, cannot run subprocess thread tests.((!RURRRt
twisted.trialRttwisted.internetRRRRRRttwisted.pythonRRR	R
tTestCaseRRVR\RnRtProcessProtocolRtR|tIReactorThreadsR(tclstskipRtIReactorProcess(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_threads.pyt<module>s($."I8+