ó
[³XMc           @   sÃ   d  Z  d d l m Z m Z m Z d d l m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ	 d e j
 f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e j
 f d „  ƒ  YZ d e j
 f d „  ƒ  YZ d S(   s_   
This module contains tests for L{twisted.internet.task.Cooperator} and
related functionality.
iÿÿÿÿ(   t   reactort   defert   task(   t   unittestt   FakeDelayedCallc           B   s    e  Z d  Z d „  Z d „  Z RS(   sA   
    Fake delayed call which lets us simulate the scheduler.
    c         C   s   | |  _  t |  _ d S(   s+   
        A function to run, later.
        N(   t   funct   Falset	   cancelled(   t   selfR   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   __init__   s    	c         C   s   t  |  _ d S(   s.   
        Don't run my function later.
        N(   t   TrueR   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   cancel   s    (   t   __name__t
   __module__t   __doc__R	   R   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR      s   	t   FakeSchedulerc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s/   
    A fake scheduler for testing against.
    c         C   s   g  |  _  d S(   sD   
        Create a fake scheduler with a list of work to do.
        N(   t   work(   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR	   &   s    c         C   s    t  | ƒ } |  j j | ƒ | S(   s;   
        Schedule a unit of work to be done later.
        (   R   R   t   append(   R   t   thunkt   unit(    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   __call__-   s    c         C   s>   |  j  g  } |  _  x$ | D] } | j s | j ƒ  q q Wd S(   sL   
        Do all of the work that is currently available to be done.
        N(   R   R   R   (   R   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   pump6   s    	(   R   R   R   R	   R   R   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR   "   s   			t   TestCooperatorc           B   sq   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z RS(   t   donec         C   s   | j  t j ƒ |  j S(   N(   t   trapR   t   SchedulerStoppedt   RESULT(   R   t   err(    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   ebIterD   s    c         C   s   |  j  ƒ  d  S(   N(   t   fail(   R   t   ign(    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   cbIterI   s    c            s+   ‡  f d †  ‰ ˆ d ƒ j ‡ f d †  ƒ S(   sU   
        Test that Cooperators refuse new tasks when they have been stopped.
        c            sd   t  j ƒ  } | j ƒ  | j t d ƒ |  ƒ } | j ˆ  j ƒ | j ˆ  j ƒ | j ‡  f d †  ƒ S(   Nc            s   ˆ  j  |  ˆ  j ƒ S(   N(   t   assertEqualsR   (   t   result(   R   (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   <lambda>W   s    (    (	   R   t
   Cooperatort   stopt	   coiteratet   itert   addCallbackR   t
   addErrbackR   (   t   stufft   ct   d(   R   (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   testwithQ   s    
c            s   ˆ  t  j ƒ  ƒ S(   N(   R   t   Deferred(   R   (   R,   (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR"   Y   s    N(   t   NoneR'   (   R   (    (   R   R,   s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   testStoppedRejectsNewTasksM   s    c            sƒ   t  j ƒ  } ‡ f d †  ‰ d ˆ _ | j ˆ ƒ  ƒ } | j ˆ  j ƒ | j ˆ  j ƒ | j ƒ  ‡ ‡  f d †  } | j | ƒ | S(   sq   
        Test that a running iterator will not run to completion when the
        cooperator is stopped.
        c           3   s&   x t  d ƒ D] ˆ  _ ˆ  j Vq Wd  S(   Ni   (   t   ranget   value(    (   t   myiter(    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR2   b   s    iÿÿÿÿc            s*   ˆ j  |  ˆ j ƒ ˆ j  ˆ  j d ƒ d  S(   Niÿÿÿÿ(   R    R   R1   (   R!   (   R2   R   (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt	   doassertsj   s    (	   R   R#   R1   R%   R'   R   R(   R   R$   (   R   R*   R+   R3   (    (   R   R2   s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   testStopRunning\   s    	
c            s    t  j ƒ  ‰ t  j ƒ  ‰ ‡ ‡ ‡ f d †  } t j ƒ  ‰  ˆ  j | ƒ  ƒ } ‡  ‡ f d †  } ˆ j | ƒ | j ˆ j ƒ | j ˆ j ƒ | j ‡ f d †  ƒ 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.
        c           3   s)   t  j d ˆ  j d  ƒ ˆ Vˆ j ƒ  d  S(   Ni    (   R    t	   callLatert   callbackR.   R   (    (   t   testControlDt   outstandingDR   (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR2   y   s    c            s   ˆ  j  ƒ  ˆ j d ƒ d  S(   Nt   arglebargle(   R$   R6   (   R   (   R*   R8   (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt	   stopAndGo   s    
c            s   ˆ  j  |  ˆ  j ƒ S(   N(   R    R   (   R!   (   R   (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR"   ˆ   s    (	   R   R-   R   R#   R%   R'   R   R(   R   (   R   R2   R+   R:   (    (   R*   R   R8   R7   s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   testStopOutstandingq   s    c         C   s7   t  j ƒ  } d „  } | j | ƒ  ƒ } |  j | t ƒ S(   Nc           s   s   t  ƒ  ‚ d  S(   N(   t   RuntimeError(    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR2      s    (   R   R#   R%   t   assertFailureR<   (   R   R*   R2   R+   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   testUnexpectedError‹   s    	c         C   s7   d „  } t  j ƒ  } | j | ƒ  ƒ } |  j | t ƒ S(   Nc          s   s.   t  j ƒ  }  t j d |  j t ƒ  ƒ |  Vd  S(   Ni    (   R   R-   R    R5   t   errbackR<   (   t   D(    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR2   —   s    (   R   R#   R%   R=   R<   (   R   R2   R*   R+   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt    testUnexpectedErrorActuallyLater–   s    	c         C   s7   d „  } t  j ƒ  } | j | ƒ  ƒ } |  j | t ƒ S(   Nc           s   s   t  j t ƒ  ƒ Vd  S(   N(   R   R   R<   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR2   ¢   s    (   R   R#   R%   R=   R<   (   R   R2   R*   R+   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt#   testUnexpectedErrorNotActuallyLater¡   s    	c            s‹   g  ‰ ‡ f d †  } d d d d g ‰  t  j ƒ  } g  } x* ˆ  D]" } | j | j | | ƒ ƒ ƒ q@ Wt j | ƒ j ‡  ‡ ‡ f d
 †  ƒ S(   Nc         3   s'   x  |  D] } ˆ  j  | ƒ d  Vq Wd  S(   N(   R   R.   (   t   thingst   th(   t   L(    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR2   ¬   s    t   abci   i   i   t   defi   i   i   c            s%   ˆ j  t ˆ ƒ t t ˆ  Œ  d ƒ ƒ S(   N(    (   R    t   tuplet   sumt   zip(   R   (   t   groupsOfThingsRE   R   (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR"   ¹   s    (   i   i   i   (   i   i   i   (   R   R#   R   R%   R   t   DeferredListR'   (   R   R2   R*   t   tasksR)   (    (   RK   R   RE   s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   testCooperationª   s     c            s¤   g  ‰ ‡ ‡  f d †  } d d d „  ƒ  Y‰  t  j d ˆ  ƒ } | j | ƒ  ƒ j |  j ƒ | j j ƒ  | j ƒ  | j ƒ  |  j	 ˆ  j
 ƒ |  j ˆ t d ƒ ƒ d  S(   Nc          3   sE   x> t  d ƒ D]0 }  ˆ  j |  ƒ |  d k r8 t ˆ _ n  |  Vq Wd  S(   Nid   i	   (   R0   R   R
   t   stopped(   t   i(   t   outputt   _TPF(    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR2   ¾   s
    RR   c           B   s   e  Z e Z d  „  Z RS(   c         S   s   |  j  S(   N(   RO   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR   Ç   s    (   R   R   R   RO   R   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRR   Å   s   t   terminationPredicateFactoryi
   (    (   R   R#   R%   R(   R   t   _delayedCallR   t   _tickR$   t
   failUnlessRO   R    R0   (   R   R2   R*   (    (   RR   RQ   s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   testResourceExhaustion¼   s    

c            sê   g  ‰ d d d „  ƒ  Y‰ ‡ ‡ ‡ f d †  } t  j d | d d „  ƒ ‰  ˆ  j t d ƒ ƒ } g  ‰ ‡  ‡ f d †  } | j | ƒ d } xZ ˆ så | d	 7} x' ˆ rÅ ˆ j d ƒ j ƒ  | d	 7} qŸ W| d
 k rŒ ˆ j d ƒ 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.)
        t   FakeCallc           B   s   e  Z d  „  Z d „  Z RS(   c         S   s   | |  _  d  S(   N(   R   (   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR	   ß   s    c         S   s   d |  j  f S(   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.pyRX   Þ   s   	c            s1   ˆ  j  ˆ t ˆ ƒ ƒ ˆ j ˆ |  ƒ ƒ ˆ d S(   Niÿÿÿÿ(   t   failIft   reprR   (   t   f(   R   RX   t   calls(    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   schedå   s    t	   schedulerRS   c           S   s   d „  S(   Nc           S   s   t  S(   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"   ê   s    c            s#   ˆ  j  t d ƒ ƒ j ˆ j ƒ d  S(   N(    (   R%   R&   t   addBothR   (   R   (   R*   R   (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   anotherTaskî   s    i    i   i2   s   Cooperator took too longN(    (    (   R   R#   R%   R&   R'   t   popR   R   (   R   R^   R+   Ra   R   (    (   R*   R]   RX   R   R   s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   testCallbackReCoiterateÕ   s     	
	(   R   R   R   R   R   R/   R4   R;   R>   RA   RB   RN   RW   Rc   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR   A   s   											t   UnhandledExceptionc           B   s   e  Z d  Z RS(   s0   
    An exception that should go unhandled.
    (   R   R   R   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRd   þ   s   t
   AliasTestsc           B   s   e  Z d  Z d „  Z RS(   sg   
    Integration test to verify that the global singleton aliases do what
    they're supposed to.
    c            sM   t  j ƒ  ‰  ‡  f d †  } | ƒ  } t j | ƒ } |  j | t j j ƒ ˆ  S(   sZ   
        L{twisted.internet.task.cooperate} ought to run the generator that it is
        c           3   s    d Vd Vd Vˆ  j  d ƒ d  S(   Ni   i   i   t   yay(   R6   (    (   R+   (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   doit  s    (   R   R-   R   t	   cooperatet   assertInt   _theCooperatort   _tasks(   R   Rg   t   itt   theTask(    (   R+   s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   test_cooperate  s    	(   R   R   R   Rn   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRe     s   t   RunStateTestsc           B   sÂ   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   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.
    c         C   s}   t  |  _ t  |  _ t  |  _ g  |  _ t ƒ  |  _ t j d |  j d d „  ƒ |  _	 |  j	 j
 |  j ƒ  ƒ |  _ |  j	 j ƒ  d S(   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_   RS   c           S   s   d „  S(   Nc           S   s   t  S(   N(   R
   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR"   1  s    (    (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyR"   1  s    N(   R   t   _doDeferNextt   _doStopNextt
   _doDieNextR   R   R_   R   R#   t
   cooperatorRh   t   workert   start(   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   setUp#  s    						c         c   sŽ   d } x t  r‰ | d 7} |  j rO t |  _ t j ƒ  } |  j j | ƒ | Vq	 |  j r\ d S|  j rq t	 ƒ  ‚ q	 |  j j | ƒ | Vq	 Wd S(   s’   
        This is a sample generator which yields Deferreds when we are testing
        deferral and an ascending integer count otherwise.
        i    i   N(
   R
   Rp   R   R   R-   R   R   Rq   Rr   Rd   (   R   RP   R+   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRt   6  s    	
				c         C   s   |  `  |  ` d S(   s   
        Drop references to interesting parts of the fixture to allow Deferred
        errors to be noticed when things start failing.
        N(   R   R_   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   tearDownL  s    c         C   s   t  |  _ d S(   s@   
        Defer the next result from my worker iterator.
        N(   R
   Rp   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt	   deferNextU  s    c         C   s   t  |  _ d S(   sm   
        Make the next result from my worker iterator be completion (raising
        StopIteration).
        N(   R
   Rq   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   stopNext\  s    c         C   s   d „  } t  |  _ d S(   sk   
        Make the next result from my worker iterator be raising an
        L{UnhandledException}.
        c         S   s   |  j  t ƒ d  S(   N(   R   Rd   R.   (   t   failure(    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   ignoreUnhandledi  s    N(   R
   Rr   (   R   R{   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   dieNextd  s    	c         C   sÏ   |  j  j ƒ  |  j |  j d g ƒ |  j  j ƒ  |  j |  j d d g ƒ |  j j ƒ  |  j  j ƒ  |  j |  j d d g ƒ |  j j ƒ  |  j |  j d d g ƒ |  j  j ƒ  |  j |  j d d d g ƒ d S(   s€   
        Cooperators should stop running their tasks when they're paused, and
        start again when they're resumed.
        i   i   i   N(   R_   R   R    R   R   t   pauset   resume(   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   test_pauseResumeo  s    c         C   sP   |  j  t j |  j j ƒ |  j j ƒ  |  j j ƒ  |  j  t j |  j j ƒ d S(   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   assertRaisesR   t	   NotPausedR~   R}   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   test_resumeNotPaused†  s    c         C   s»   |  j  j ƒ  |  j j ƒ  |  j |  j g  ƒ |  j  j ƒ  |  j j ƒ  |  j |  j g  ƒ |  j  j ƒ  |  j j ƒ  |  j |  j g  ƒ |  j  j ƒ  |  j j ƒ  |  j |  j d g ƒ d S(   s~   
        Pauses on tasks should behave like a stack. If a task is paused twice,
        it needs to be resumed twice.
        i   N(   R   R}   R_   R   R    R   R~   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   test_pauseTwice’  s    c         C   s1  |  j  ƒ  |  j j ƒ  |  j t |  j ƒ d ƒ |  j t |  j d t j	 ƒ ƒ |  j j ƒ  |  j t |  j ƒ d ƒ |  j
 j ƒ  |  j j ƒ  |  j t |  j ƒ d ƒ |  j
 j ƒ  |  j j ƒ  |  j t |  j ƒ d ƒ |  j d j d ƒ |  j j ƒ  |  j t |  j ƒ d ƒ |  j |  j d d ƒ d S(   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.
        i   i    s   STUFF!i   N(   Rx   R_   R   R    t   lenR   RV   t
   isinstanceR   R-   R   R}   R~   R6   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   test_pauseWhileDeferred©  s     
 c            s/  |  j  j ƒ  } |  j  j ƒ  } g  ‰ g  ‰  g  } g  } ‡ f d †  } ‡  f d †  } | j | ƒ | j | ƒ | j | j ƒ | j | j ƒ |  j ƒ  |  j j ƒ  |  j t ˆ ƒ d ƒ |  j t ˆ  ƒ d ƒ |  j	 ˆ d |  j  j
 ƒ |  j	 ˆ  d |  j  j
 ƒ |  j | d g ƒ |  j | d g ƒ d S(   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  |  ƒ d S(   Ni   (   R   (   R!   (   t   results1(    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   callbackOneÑ  s    c            s   ˆ  j  |  ƒ d S(   Ni   (   R   (   R!   (   t   results2(    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   callbackTwoÕ  s    i   i    i   N(   R   t   whenDoneR'   R   Ry   R_   R   R    R„   t   assertIdenticalt	   _iterator(   R   t	   deferred1t	   deferred2t   final1t   final2Rˆ   RŠ   (    (   R‰   R‡   s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   test_whenDoneÂ  s(    
c         C   ss   |  j  j ƒ  } g  } | j | j ƒ |  j ƒ  |  j j ƒ  |  j t | ƒ d ƒ |  j | d j	 t
 ƒ t
 ƒ d S(   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.
        i   i    N(   R   R‹   R(   R   R|   R_   R   R    R„   t   checkRd   (   R   RŽ   t   results(    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   test_whenDoneErrorî  s    
c         C   so   |  j  j ƒ  } g  } | j | j ƒ |  j  j ƒ  |  j t | ƒ d ƒ |  j | d j t  j ƒ t  j ƒ d S(   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}.
        i   i    N(	   R   R‹   R(   R   R$   R    R„   R“   t   TaskStopped(   R   RŽ   t   errors(    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   test_whenDoneStopý  s    c         C   sS   |  j  ƒ  |  j j ƒ  g  } |  j j ƒ  j | j ƒ |  j | |  j j g ƒ d S(   sš   
        L{CooperativeTask.whenDone} will return a L{defer.Deferred} that will
        succeed immediately if its iterator has already completed.
        N(	   Ry   R_   R   R   R‹   R'   R   R    R   (   R   R”   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   test_whenDoneAlreadyDone  s
    
c         C   s‰   |  j  j ƒ  |  j j ƒ  |  j t |  j ƒ d ƒ |  j t  j |  j  j ƒ |  j t  j |  j  j	 ƒ |  j j ƒ  |  j |  j g  ƒ d S(   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.
        i    N(
   R   R$   R_   R   R    R„   R   R€   R–   R}   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   test_stopStops  s    c         C   sK   |  j  j ƒ  |  j  j ƒ  |  j  j ƒ  |  j j ƒ  |  j |  j g  ƒ d S(   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(   R   R}   R$   R~   R_   R   R    R   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   test_pauseStopResume'  s
    c         C   s½   |  j  ƒ  |  j j ƒ  |  j j ƒ  } |  j |  j j d ƒ g  } | j | j	 ƒ |  j j ƒ  |  j j
 ƒ  |  j j ƒ  | j d ƒ |  j j ƒ  |  j | d g ƒ |  j |  j g  ƒ d S(   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.
        i   i   N(   Rx   R_   R   R   Rb   R    R   t   _pauseCountR`   R   R$   R6   R.   (   R   R+   R”   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   test_stopDeferred4  s    
c         C   s4   |  j  ƒ  |  j j ƒ  |  j t j |  j j ƒ d S(   sx   
        C{stop()}ping a L{CooperativeTask} whose iterator has been exhausted
        should raise L{TaskDone}.
        N(   Ry   R_   R   R€   R   t   TaskDoneR$   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   test_stopExhaustedO  s    
c         C   s4   |  j  ƒ  |  j j ƒ  |  j t j |  j j ƒ d S(   s€   
        C{stop()}ping a L{CooperativeTask} whose iterator has encountered an
        error should raise L{TaskFailed}.
        N(   R|   R_   R   R€   R   t
   TaskFailedR$   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   test_stopErroredY  s    
c            se   g  ‰ ‡  ‡ f d †  } ˆ  j  j ƒ  j | ƒ ˆ  j ƒ  ˆ  j j ƒ  ˆ  j ˆ ˆ  j  j d g ƒ d S(   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  |  ƒ ˆ  j j ƒ  ˆ j  d ƒ d  S(   NR   (   R   Rs   R$   (   R!   (   R   t   callbackPhases(    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   stopitm  s    R   N(   R   R‹   R'   Ry   R_   R   R    R   (   R   R£   (    (   R   R¢   s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   test_stopCooperatorReentrancyc  s    	
(   R   R   R   Rv   Rt   Rw   Rx   Ry   R|   R   R‚   Rƒ   R†   R’   R•   R˜   R™   Rš   R›   R   RŸ   R¡   R¤   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyRo     s*   												,							
	
N(   R   t   twisted.internetR    R   R   t   twisted.trialR   t   objectR   R   t   TestCaseR   t	   ExceptionRd   Re   Ro   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/test/test_cooperator.pyt   <module>   s   ½