ó †šQc@`sÙdZddlmZmZddlmZmZmZddlm Z de fd„ƒYZ de fd„ƒYZ d e j fd „ƒYZd efd „ƒYZd e j fd„ƒYZde j fd„ƒYZdS(s_ This module contains tests for L{twisted.internet.task.Cooperator} and related functionality. i(tdivisiontabsolute_import(treactortdeferttask(tunittesttFakeDelayedCallcB`s eZdZd„Zd„ZRS(sA Fake delayed call which lets us simulate the scheduler. cC`s||_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 cC`s 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 FakeSchedulercB`s)eZdZd„Zd„Zd„ZRS(s/ A fake scheduler for testing against. cC`s 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 (scC`s t|ƒ}|jj|ƒ|S(s; Schedule a unit of work to be done later. (RRtappend(R tthunktunit((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt__call__/s cC`s>|jg}|_x$|D]}|js|jƒqqWdS(sL Do all of the work that is currently available to be done. N(RR R(R RR((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytpump8s  (RRRR RR(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR$s  tTestCooperatorcB`sžeZdZd„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z d „Z d „Z d „Z d „Zd „Zd„Zd„Zd„ZRS(tdonecC`s|jtjƒ|jS(N(ttrapRtSchedulerStoppedtRESULT(R terr((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytebIterFscC`s|jƒdS(N(tfail(R tign((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytcbIterKsc`s+‡fd†‰ˆdƒj‡fd†ƒS(sU Test that Cooperators refuse new tasks when they have been stopped. c`sdtjƒ}|jƒ|jtdƒ|ƒ}|jˆjƒ|jˆjƒ|j‡fd†ƒS(Nc`sˆj|ˆjƒS(N(t assertEqualR(tresult(R (s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytYs(( Rt Cooperatortstopt coiteratetitert addCallbackR!t addErrbackR(tstufftctd(R (s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttestwithSs   c`sˆtjƒƒS(N(RtDeferred(R (R.(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR$[sN(tNoneR)(R ((R R.s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttestStoppedRejectsNewTasksOsc`sƒtjƒ}‡fd†‰dˆ_|jˆƒƒ}|jˆjƒ|jˆjƒ|jƒ‡‡fd†}|j|ƒ|S(sq Test that a running iterator will not run to completion when the cooperator is stopped. c3`s&xtdƒD]ˆ_ˆjVq WdS(Ni(trangetvalue((tmyiter(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR4dsiÿÿÿÿc`s*ˆj|ˆjƒˆjˆjdƒdS(Niÿÿÿÿ(R"RR3(R#(R4R (s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt doassertsls( RR%R3R'R)R!R*RR&(R R,R-R5((R4R s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttestStopRunning^s    c`s tjƒ‰tjƒ‰‡‡‡fd†}tjƒ‰ˆj|ƒƒ}‡‡fd†}ˆj|ƒ|jˆjƒ|jˆjƒ|j‡fd†ƒS(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. c3`s)tjdˆjdƒˆVˆjƒdS(Ni(Rt callLatertcallbackR0R((t outstandingDR t testControlD(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR4{sc`sˆjƒˆjdƒdS(Nt arglebargle(R&R8(R (R,R9(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt stopAndGos c`sˆj|ˆjƒS(N(R"R(R#(R (s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR$Šs( RR/RR%R'R)R!R*R(R R4R-R<((R,R9R R:s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttestStopOutstandingss    cC`s7tjƒ}d„}|j|ƒƒ}|j|tƒS(Ncs`s tƒ‚dS(N(t RuntimeError(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR4s(RR%R't assertFailureR>(R R,R4R-((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttestUnexpectedErrors  cC`s7d„}tjƒ}|j|ƒƒ}|j|tƒS(Ncs`s.tjƒ}tjd|jtƒƒ|VdS(Ni(RR/RR7terrbackR>(tD((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR4™s (RR%R'R?R>(R R4R,R-((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt testUnexpectedErrorActuallyLater˜s  cC`s7d„}tjƒ}|j|ƒƒ}|j|tƒS(Ncs`stjtƒƒVdS(N(RRR>(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR4¤s(RR%R'R?R>(R R4R,R-((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt#testUnexpectedErrorNotActuallyLater£s  c`s‹g‰‡fd†}dd dd g‰tjƒ}g}x*ˆD]"}|j|j||ƒƒƒq@Wtj|ƒj‡‡‡fd †ƒS( Nc3`s'x |D]}ˆj|ƒdVqWdS(N(RR0(tthingstth(tL(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR4®s  tabciiitdefiiic`s%ˆjtˆƒttˆŒdƒƒS(N((R"ttupletsumtzip(R (RGtgroupsOfThingsR (s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR$»s(iii(iii(RR%RR'Rt DeferredListR)(R R4R,ttasksR+((RGRMR s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttestCooperation¬s   c`sªg‰‡‡fd†}ddd„ƒY‰tjdˆƒ}|j|ƒƒj|jƒ|jjƒ|jƒ|jƒ|j ˆj ƒ|j ˆt t dƒƒƒdS(Nc3`sEx>tdƒD]0}ˆj|ƒ|dkr8tˆ_n|Vq WdS(Nidi (R2RR tstopped(ti(t_TPFtoutput(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR4Às    RScB`seZeZd„ZRS(cS`s|jS(N(RQ(R ((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRÉs(RRRRQR(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRSÇstterminationPredicateFactoryi ((RR%R'R*Rt _delayedCallR t_tickR&t failUnlessRQR"tlistR2(R R4R,((RSRTs@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttestResourceExhaustion¾s   c`sêg‰dd d„ƒY‰‡‡‡fd†}tjd|dd„ƒ‰ˆjtdƒƒ}g‰‡‡fd†}|j|ƒd}xZˆså|d 7}x'ˆrňjdƒjƒ|d 7}qŸW|d krŒˆjd ƒqŒqŒWd 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.) tFakeCallcB`seZd„Zd„ZRS(cS`s ||_dS(N(R(R R((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR áscS`sd|jfS(Ns (R(R ((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt__repr__äs(RRR R\(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR[às c`s1ˆjˆtˆƒƒˆjˆ|ƒƒˆdS(Niÿÿÿÿ(tfailIftreprR(tf(R[tcallsR (s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytschedçst schedulerRUcS`sd„S(NcS`stS(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$ìsc`s#ˆjtdƒƒjˆjƒdS(N((R'R(taddBothR(R (R,R(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt anotherTaskðsiii2sCooperator took too longN(((RR%R'R(R)tpopRR(R RaR-RdR((R[R,R`RR s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttestCallbackReCoiterate×s      c`s1dg‰‡fd†}tjd|ƒ}|jtddgƒƒ}|jtddgƒƒ}|jˆdj|jƒ|jƒ|jˆdj t ƒ|j|j ˆdƒ|jƒ|jˆdj t ƒ|j|j dƒ|jtddgƒƒ|jˆdj t ƒ|j|j ˆdƒdS(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. c`st|ƒˆd<ˆdS(Ni(R(R_(R`(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRasRbiiiN( R0RR%t cooperateR(R"RRWR&R RRVR (R Ratcoopttask1ttask2((R`s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt'test_removingLastTaskStopsScheduledCallÿs   cC`s tjƒ}|j|jƒdS(sm L{Cooperator.running} reports C{True} if the L{Cooperator} was started on creation. N(RR%t assertTruetrunning(R R,((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_runningWhenStarted"s cC`s&tjdtƒ}|j|jƒdS(sk L{Cooperator.running} reports C{False} if the L{Cooperator} has not been started. tstartedN(RR%Rt assertFalseRm(R R,((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_runningWhenNotStarted+scC`s@tjdtƒ}|jƒ|j|jƒ|j|jƒdS(sb L{Cooperator.running} reports C{True} when the L{Cooperator} is running. RoN(RR%Rtstartt addCleanupR&RlRm(R R,((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_runningWhenRunning4s cC`s:tjdtƒ}|jƒ|jƒ|j|jƒdS(sj L{Cooperator.running} reports C{False} after the L{Cooperator} has been stopped. RoN(RR%RRrR&RpRm(R R,((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_runningWhenStopped?s  (RRRRR!R1R6R=R@RCRDRPRZRfRkRnRqRtRu(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRCs"        ( # tUnhandledExceptioncB`seZdZRS(s0 An exception that should go unhandled. (RRR(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRvKst AliasTestscB`seZdZd„ZRS(sg Integration test to verify that the global singleton aliases do what they're supposed to. c`sMtjƒ‰‡fd†}|ƒ}tj|ƒ}|j|tjjƒˆS(sZ L{twisted.internet.task.cooperate} ought to run the generator that it is c3`s dVdVdVˆjdƒdS(Niiityay(R8((R-(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytdoit]s(RR/RRgtassertInt_theCooperatort_tasks(R RytitttheTask((R-s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_cooperateXs   (RRRR(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRwRst RunStateTestscB`sÂeZdZd„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z d „Z d „Z d „Z d „Zd „Zd„Zd„Zd„Zd„Zd„Zd„Zd„ZRS(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. cC`s}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. RbRUcS`sd„S(NcS`stS(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$~sN( Rt _doDeferNextt _doStopNextt _doDieNextRRRbRR%t cooperatorRgtworkerRr(R ((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytsetUpps       cc`sŽd}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 RRRR/RRR‚RƒRv(R RRR-((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR…ƒs        cC`s|`|`dS(s Drop references to interesting parts of the fixture to allow Deferred errors to be noticed when things start failing. N(RRb(R ((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttearDown™scC`s t|_dS(s@ Defer the next result from my worker iterator. N(R R(R ((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt deferNext¢scC`s t|_dS(sm Make the next result from my worker iterator be completion (raising StopIteration). N(R R‚(R ((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytstopNext©scC`sd„}t|_dS(sk Make the next result from my worker iterator be raising an L{UnhandledException}. cS`s|jtƒdS(N(RRvR0(tfailure((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytignoreUnhandled¶s N(R Rƒ(R R‹((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytdieNext±s cC`sÏ|jjƒ|j|jdgƒ|jjƒ|j|jddgƒ|jjƒ|jjƒ|j|jddgƒ|jjƒ|j|jddgƒ|jjƒ|j|jdddgƒdS(s€ Cooperators should stop running their tasks when they're paused, and start again when they're resumed. iiiN(RbRR"RRtpausetresume(R ((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_pauseResume¼s      cC`sP|jtj|jjƒ|jjƒ|jjƒ|jtj|jjƒdS(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 NotPausedRŽR(R ((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_resumeNotPausedÓs  cC`s»|jjƒ|jjƒ|j|jgƒ|jjƒ|jjƒ|j|jgƒ|jjƒ|jjƒ|j|jgƒ|jjƒ|jjƒ|j|jdgƒdS(s~ Pauses on tasks should behave like a stack. If a task is paused twice, it needs to be resumed twice. iN(RRRbRR"RRŽ(R ((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_pauseTwiceßs        cC`s1|jƒ|jjƒ|jt|jƒdƒ|jt|jdtj ƒƒ|jjƒ|jt|jƒdƒ|j j ƒ|jjƒ|jt|jƒdƒ|j j ƒ|jjƒ|jt|jƒdƒ|jdj dƒ|jjƒ|jt|jƒdƒ|j|jddƒdS(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ˆRbRR"tlenRRXt isinstanceRR/RRRŽR8(R ((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_pauseWhileDeferredös          c`s/|jjƒ}|jjƒ}g‰g‰g}g}‡fd†}‡fd†}|j|ƒ|j|ƒ|j|jƒ|j|jƒ|jƒ|jjƒ|jtˆƒdƒ|jtˆƒdƒ|j ˆd|jj ƒ|j ˆd|jj ƒ|j|dgƒ|j|dgƒdS(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. c`sˆj|ƒdS(Ni(R(R#(tresults1(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt callbackOnes c`sˆj|ƒdS(Ni(R(R#(tresults2(s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt callbackTwo"s iiiN( RtwhenDoneR)RR‰RbRR"R”tassertIdenticalt _iterator(R t deferred1t deferred2tfinal1tfinal2R˜Rš((R—R™s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt test_whenDones(    cC`ss|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( RR›R*RRŒRbRR"R”tcheckRv(R Ržtresults((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_whenDoneError;s  cC`so|jjƒ}g}|j|jƒ|jjƒ|jt|ƒdƒ|j|djtjƒtjƒdS(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( RR›R*RR&R"R”R£t TaskStopped(R Ržterrors((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_whenDoneStopJs  cC`sS|jƒ|jjƒg}|jjƒj|jƒ|j||jjgƒdS(sš L{CooperativeTask.whenDone} will return a L{defer.Deferred} that will succeed immediately if its iterator has already completed. N( R‰RbRRR›R)RR"R(R R¤((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_whenDoneAlreadyDoneXs   cC`s‰|jjƒ|jjƒ|jt|jƒdƒ|jtj|jjƒ|jtj|jj ƒ|jjƒ|j|jgƒdS(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&RbRR"R”RRR¦R(R ((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_stopStopsds   cC`sK|jjƒ|jjƒ|jjƒ|jjƒ|j|jgƒdS(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&RŽRbRR"R(R ((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_pauseStopResumets     cC`s½|jƒ|jjƒ|jjƒ}|j|jjdƒg}|j|j ƒ|jjƒ|jj ƒ|jjƒ|j dƒ|jjƒ|j|dgƒ|j|jgƒdS(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ˆRbRRReR"Rt _pauseCountRcRR&R8R0(R R-R¤((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_stopDeferreds       cC`s4|jƒ|jjƒ|jtj|jjƒdS(sx C{stop()}ping a L{CooperativeTask} whose iterator has been exhausted should raise L{TaskDone}. N(R‰RbRRRtTaskDoneR&(R ((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_stopExhaustedœs  cC`s4|jƒ|jjƒ|jtj|jjƒdS(s€ C{stop()}ping a L{CooperativeTask} whose iterator has encountered an error should raise L{TaskFailed}. N(RŒRbRRRt TaskFailedR&(R ((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_stopErrored¦s  c`seg‰‡‡fd†}ˆjjƒj|ƒˆjƒˆjjƒˆjˆˆjjdgƒdS(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}. c`s+ˆj|ƒˆjjƒˆjdƒdS(NR(RR„R&(R#(tcallbackPhasesR (s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytstopitºs  RN(RR›R)R‰RbRR"R(R R³((R²R s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyttest_stopCooperatorReentrancy°s   (RRRR†R…R‡RˆR‰RŒRR’R“R–R¢R¥R¨R©RªR«R­R¯R±R´(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR€is*        ,     N(Rt __future__RRttwisted.internetRRRt twisted.trialRtobjectRRtTestCaseRt ExceptionRvRwR€(((s@/usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pytsÿ