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_cooperator.pyc


wGNc@sdZddlmZmZmZddlmZdefdYZdefdYZ	dej
fd	YZd
efdYZ
dej
fd
YZdej
fdYZdS(s_
This module contains tests for L{twisted.internet.task.Cooperator} and
related functionality.
i(treactortdeferttask(tunittesttFakeDelayedCallcBs eZdZdZdZRS(sA
    Fake delayed call which lets us simulate the scheduler.
    cCs||_t|_dS(s+
        A function to run, later.
        N(tfunctFalset	cancelled(tselfR((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt__init__s	cCs
t|_dS(s.
        Don't run my function later.
        N(tTrueR(R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytcancels(t__name__t
__module__t__doc__R	R(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRs	t
FakeSchedulercBs)eZdZdZdZdZRS(s/
    A fake scheduler for testing against.
    cCs
g|_dS(sD
        Create a fake scheduler with a list of work to do.
        N(twork(R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR	&scCs t|}|jj||S(s;
        Schedule a unit of work to be done later.
        (RRtappend(Rtthunktunit((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt__call__-scCs>|jg}|_x$|D]}|js|jqqWdS(sL
        Do all of the work that is currently available to be done.
        N(RRR(RRR((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytpump6s
	(RR
RR	RR(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR"s			tTestCooperatorcBszeZdZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZ
dZRS(
tdonecCs|jtj|jS(N(ttrapRtSchedulerStoppedtRESULT(Rterr((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytebIterDscCs|jdS(N(tfail(Rtign((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytcbIterIscs+fddjfdS(sU
        Test that Cooperators refuse new tasks when they have been stopped.
        csdtj}|j|jtd|}|jj|jj|jfdS(Ncsj|jS(N(tassertEqualR(tresult(R(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt<lambda>Ws((	Rt
Cooperatortstopt	coiteratetitertaddCallbackRt
addErrbackR(tstufftctd(R(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttestwithQs
cstjS(N(RtDeferred(R(R,(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR"YsN(tNoneR'(R((RR,s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttestStoppedRejectsNewTasksMscstj}fdd_|j}|jj|jj|jfd}|j||S(sq
        Test that a running iterator will not run to completion when the
        cooperator is stopped.
        c3s&xtdD]_jVq
WdS(Ni(trangetvalue((tmyiter(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR2bsics*j|jjjddS(Ni(R RR1(R!(R2R(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt	doassertsjs(	RR#R1R%R'RR(RR$(RR*R+R3((RR2s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttestStopRunning\s	

cstjtjfd}tjj|}fd}j||jj|jj|jfdS(s
        An iterator run with L{Cooperator.coiterate} paused on a L{Deferred}
        yielded by that iterator will fire its own L{Deferred} (the one
        returned by C{coiterate}) when L{Cooperator.stop} is called.
        c3s)tjdjdVjdS(Ni(Rt	callLatertcallbackR.R((ttestControlDtoutstandingDR(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR2yscsjjddS(Ntarglebargle(R$R6(R(R*R8(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt	stopAndGos
csj|jS(N(R R(R!(R(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR"s(	RR-RR#R%R'RR(R(RR2R+R:((R*RR8R7s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttestStopOutstandingqs
cCs7tj}d}|j|}|j|tS(Ncss
tdS(N(tRuntimeError(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR2s(RR#R%t
assertFailureR<(RR*R2R+((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttestUnexpectedErrors	cCs7d}tj}|j|}|j|tS(Ncss.tj}tjd|jt|VdS(Ni(RR-RR5terrbackR<(tD((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR2s(RR#R%R=R<(RR2R*R+((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt testUnexpectedErrorActuallyLaters	cCs7d}tj}|j|}|j|tS(NcsstjtVdS(N(RRR<(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR2s(RR#R%R=R<(RR2R*R+((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt#testUnexpectedErrorNotActuallyLaters	csgfd}ddddgtj}g}x*D]"}|j|j||q@Wtj|jfd
S(
Nc3s'x |D]}j|dVqWdS(N(RR.(tthingstth(tL(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR2s

tabciiitdefiiics%jtttdS(N((R ttupletsumtzip(R(tgroupsOfThingsRER(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR"s(iii(iii(RR#RR%RtDeferredListR'(RR2R*ttasksR)((RKRREs@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttestCooperations
 csgfd}dddYtjd}|j|j|j|jj|j|j|j	j
|jtddS(Nc3sEx>tdD]0}j||dkr8t_n|Vq
WdS(Nidi	(R0RR
tstopped(ti(toutputt_TPF(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR2s

RRcBseZeZdZRS(cSs|jS(N(RO(R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRs(RR
RROR(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRRstterminationPredicateFactoryi
((
RR#R%R(Rt_delayedCallRt_tickR$t
failUnlessROR R0(RR2R*((RRRQs@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttestResourceExhaustions


csgdd
dYfd}tjd|ddjtd}gfd}|j|d}xZs|d	7}x'rjdj|d	7}qW|d
krjdqqWdS(s*
        If a callback to a deferred returned by coiterate calls coiterate on
        the same Cooperator, we should make sure to only do the minimal amount
        of scheduling work.  (This test was added to demonstrate a specific bug
        that was found while writing the scheduler.)
        tFakeCallcBseZdZdZRS(cSs
||_dS(N(R(RR((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR	scSsd|jfS(Ns
<FakeCall %r>(R(R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt__repr__s(RR
R	RY(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRXs	cs1jtj|dS(Ni(tfailIftreprR(tf(RRXtcalls(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytschedst	schedulerRScSsdS(NcSstS(N(R
(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR"s((((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR"scs#jtdjjdS(N((R%R&taddBothR(R(R*R(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytanotherTasksiii2sCooperator took too longN(((RR#R%R&R'tpopRR(RR^R+RaR((R*R]RXRRs@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttestCallbackReCoiterates 
	
	cs3dgfd}tjd|}|jtddg}|jtddg}|jdj|j|j|jdj	t
|j|jd|j|jdj	t|j|jd|jtddg}|jdj	t
|j|jddS(s
        If the last task in a Cooperator is removed, the scheduled call for
        the next tick is cancelled, since it is no longer necessary.

        This behavior is useful for tests that want to assert they have left
        no reactor state behind when they're done.
        cst|d<dS(Ni(R(R\(R](s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR^sR_iiiN(
R.RR#t	cooperateR&R RRUR$RRRTR
(RR^tcoopttask1ttask2ttask3((R]s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt'test_removingLastTaskStopsScheduledCalls	

(RR
RRRR/R4R;R>RARBRNRWRcRi(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRAs												(tUnhandledExceptioncBseZdZRS(s0
    An exception that should go unhandled.
    (RR
R(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRj!st
AliasTestscBseZdZdZRS(sg
    Integration test to verify that the global singleton aliases do what
    they're supposed to.
    csMtjfd}|}tj|}|j|tjjS(sZ
        L{twisted.internet.task.cooperate} ought to run the generator that it is
        c3s dVdVdVjddS(Niiityay(R6((R+(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytdoit3s(RR-RRdtassertInt_theCooperatort_tasks(RRmtitttheTask((R+s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_cooperate.s	(RR
RRs(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRk(st
RunStateTestscBseZdZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZ
dZd
ZdZdZdZdZdZdZdZRS(s
    Tests to verify the behavior of L{CooperativeTask.pause},
    L{CooperativeTask.resume}, L{CooperativeTask.stop}, exhausting the
    underlying iterator, and their interactions with each other.
    cCs}t|_t|_t|_g|_t|_tjd|jdd|_	|j	j
|j|_|j	jdS(s
        Create a cooperator with a fake scheduler and a termination predicate
        that ensures only one unit of work will take place per tick.
        R_RScSsdS(NcSstS(N(R
(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR"Ts((((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR"TsN(
Rt_doDeferNextt_doStopNextt
_doDieNextRRR_RR#t
cooperatorRdtworkertstart(R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytsetUpFs						ccsd}xtr|d7}|jrOt|_tj}|jj||Vq	|jr\dS|jrqt	q	|jj||Vq	WdS(s
        This is a sample generator which yields Deferreds when we are testing
        deferral and an ascending integer count otherwise.
        iiN(
R
RuRRR-RRRvRwRj(RRPR+((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRyYs	
				cCs|`|`dS(s
        Drop references to interesting parts of the fixture to allow Deferred
        errors to be noticed when things start failing.
        N(RR_(R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttearDownoscCs
t|_dS(s@
        Defer the next result from my worker iterator.
        N(R
Ru(R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt	deferNextxscCs
t|_dS(sm
        Make the next result from my worker iterator be completion (raising
        StopIteration).
        N(R
Rv(R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytstopNextscCsd}t|_dS(sk
        Make the next result from my worker iterator be raising an
        L{UnhandledException}.
        cSs|jtdS(N(RRjR.(tfailure((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytignoreUnhandleds
N(R
Rw(RR((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytdieNexts	cCs|jj|j|jdg|jj|j|jddg|jj|jj|j|jddg|jj|j|jddg|jj|j|jdddgdS(s
        Cooperators should stop running their tasks when they're paused, and
        start again when they're resumed.
        iiiN(R_RR RRtpausetresume(R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_pauseResumes





cCsP|jtj|jj|jj|jj|jtj|jjdS(s
        L{CooperativeTask.resume} should raise a L{TaskNotPaused} exception if
        it was not paused; e.g. if L{CooperativeTask.pause} was not invoked
        more times than L{CooperativeTask.resume} on that object.
        N(tassertRaisesRt	NotPausedRR(R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_resumeNotPauseds

cCs|jj|jj|j|jg|jj|jj|j|jg|jj|jj|j|jg|jj|jj|j|jdgdS(s~
        Pauses on tasks should behave like a stack. If a task is paused twice,
        it needs to be resumed twice.
        iN(RRR_RR RR(R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_pauseTwices







cCs1|j|jj|jt|jd|jt|jdtj	|jj|jt|jd|j
j|jj|jt|jd|j
j|jj|jt|jd|jdj
d|jj|jt|jd|j|jdddS(s
        C{pause()}ing a task while it is waiting on an outstanding
        L{defer.Deferred} should put the task into a state where the
        outstanding L{defer.Deferred} must be called back I{and} the task is
        C{resume}d before it will continue processing.
        iisSTUFF!iN(R}R_RR tlenRRVt
isinstanceRR-RRRR6(R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_pauseWhileDeferreds 

 





cs/|jj}|jj}ggg}g}fd}fd}|j||j||j|j|j|j|j|jj|jtd|jtd|j	d|jj
|j	d|jj
|j|dg|j|dgdS(s/
        L{CooperativeTask.whenDone} returns a Deferred which fires when the
        Cooperator's iterator is exhausted.  It returns a new Deferred each
        time it is called; callbacks added to other invocations will not modify
        the value that subsequent invocations will fire with.
        csj|dS(Ni(R(R!(tresults1(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytcallbackOnes
csj|dS(Ni(R(R!(tresults2(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytcallbackTwos
iiiN(RtwhenDoneR'RR~R_RR RtassertIdenticalt	_iterator(Rt	deferred1t	deferred2tfinal1tfinal2RR((RRs@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt
test_whenDones(



cCss|jj}g}|j|j|j|jj|jt|d|j|dj	t
t
dS(s
        L{CooperativeTask.whenDone} returns a L{defer.Deferred} that will fail
        when the iterable's C{next} method raises an exception, with that
        exception.
        iiN(RRR(RRR_RR RtcheckRj(RRtresults((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_whenDoneErrors

cCso|jj}g}|j|j|jj|jt|d|j|djtjtjdS(s
        L{CooperativeTask.whenDone} returns a L{defer.Deferred} that fails with
        L{TaskStopped} when the C{stop} method is called on that
        L{CooperativeTask}.
        iiN(	RRR(RR$R RRtTaskStopped(RRterrors((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_whenDoneStop s
cCsS|j|jjg}|jjj|j|j||jjgdS(s
        L{CooperativeTask.whenDone} will return a L{defer.Deferred} that will
        succeed immediately if its iterator has already completed.
        N(	R~R_RRRR'RR R(RR((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_whenDoneAlreadyDone.s


cCs|jj|jj|jt|jd|jtj|jj|jtj|jj	|jj|j|jgdS(s
        C{stop()}ping a task should cause it to be removed from the run just as
        C{pause()}ing, with the distinction that C{resume()} will raise a
        L{TaskStopped} exception.
        iN(
RR$R_RR RRRRR(R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_stopStops:s


cCsK|jj|jj|jj|jj|j|jgdS(s
        C{resume()}ing a paused, stopped task should be a no-op; it should not
        raise an exception, because it's paused, but neither should it actually
        do more work from the task.
        N(RRR$RR_RR R(R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_pauseStopResumeJs




cCs|j|jj|jj}|j|jjdg}|j|j	|jj|jj
|jj|jd|jj|j|dg|j|jgdS(s
        As a corrolary of the interaction of C{pause()} and C{unpause()},
        C{stop()}ping a task which is waiting on a L{Deferred} should cause the
        task to gracefully shut down, meaning that it should not be unpaused
        when the deferred fires.
        iiN(
R}R_RRRbR Rt_pauseCountR`RR$R6R.(RR+R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_stopDeferredWs






cCs4|j|jj|jtj|jjdS(sx
        C{stop()}ping a L{CooperativeTask} whose iterator has been exhausted
        should raise L{TaskDone}.
        N(R~R_RRRtTaskDoneR$(R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_stopExhaustedrs

cCs4|j|jj|jtj|jjdS(s
        C{stop()}ping a L{CooperativeTask} whose iterator has encountered an
        error should raise L{TaskFailed}.
        N(RR_RRRt
TaskFailedR$(R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_stopErrored|s

csegfd}jjj|jjjjjjdgdS(s
        If a callback of a L{Deferred} from L{CooperativeTask.whenDone} calls
        C{Cooperator.stop} on its L{CooperativeTask._cooperator}, the
        L{Cooperator} will stop, but the L{CooperativeTask} whose callback is
        calling C{stop} should already be considered 'stopped' by the time the
        callback is running, and therefore removed from the
        L{CoooperativeTask}.
        cs+j|jjjddS(NR(RRxR$(R!(RtcallbackPhases(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytstopits

RN(RRR'R~R_RR R(RR((RRs@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_stopCooperatorReentrancys	

(RR
RR{RyR|R}R~RRRRRRRRRRRRRRR(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRt?s*												,					
		
	
N(Rttwisted.internetRRRt
twisted.trialRtobjectRRtTestCaseRt	ExceptionRjRkRt(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt<module>s