ó
[³XMc           @   sx   d  Z  e Z d d l m Z d d l Z d d l m Z d d l m	 Z	 d e f d „  ƒ  YZ
 e ƒ  j e
 j ƒ  ƒ d S(   s2   
Tests for implementations of L{IReactorThreads}.
iÿÿÿÿ(   t   refN(   t   ReactorBuilder(   t
   ThreadPoolt   ThreadTestsBuilderc           B   sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sD   
    Builder for defining tests relating to L{IReactorThreads}.
    c            s²   g  ‰ |  j  ƒ  ‰  ˆ  j ƒ  ‰ |  j ˆ t ƒ |  j ˆ j d ƒ ‡ ‡  ‡ f d †  } ˆ  j | ƒ |  j ˆ  d ƒ |  j ˆ d d ƒ |  j ˆ d d ƒ |  j ˆ j	 d ƒ d	 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.runc              s.   ˆ  j  ˆ j ƒ ˆ  j  ˆ j ƒ ˆ j ƒ  d  S(   N(   t   appendt   startedt   joinedt   stop(    (   t   statet   reactort   pool(    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyt   f"   s    i   i    s#   Pool should start after reactor.runi   s-   Pool should not be joined before reactor.stops0   Pool should be stopped after reactor.run returnsN(
   t   buildReactort   getThreadPoolt   assertIsInstanceR   t   assertFalseR   t   callWhenRunningt
   runReactort
   assertTrueR   (   t   selfR   (    (   R	   R   R
   sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyt   test_getThreadPool   s     c         C   s<   |  j  ƒ  } | j d ƒ | j ƒ  } |  j | j d ƒ d S(   sm   
        C{reactor.suggestThreadPoolSize()} sets the maximum size of the reactor
        threadpool.
        i   N(   R   t   suggestThreadPoolSizeR   t   assertEqualt   max(   R   R	   R
   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyt   test_suggestThreadPoolSize4   s    c            st   |  j  ƒ  ‰  ‡  f d †  } ˆ  j d ˆ  j | ƒ ˆ  j ƒ  } |  j ˆ  d ƒ ˆ  j ƒ  } |  j | | d k  ƒ d S(   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).
        c              s   ˆ  j  ƒ  d  S(   N(   R   (    (   R	   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyt
   threadCallM   s    i    i<   i   N(   R   t	   callLatert   callFromThreadt   secondsR   R   (   R   R   t   beforet   after(    (   R	   sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyt   test_delayedCallFromThread?   s    c         C   s\   |  j  ƒ  } t | j ƒ  ƒ } | j | j ƒ |  j | ƒ t j ƒ  |  j | ƒ  d ƒ d S(   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   R    R   R   R   R   t   gct   collectt   assertIdenticalt   None(   R   R	   t
   threadpool(    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyt   test_stopThreadPoolj   s    	
c            sc   |  j  ƒ  ‰  g  ‰ ‡ ‡  f d †  } ˆ  j | ƒ |  j ˆ  ƒ t j ƒ  |  j ˆ d ƒ  d ƒ d S(   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.
        c              s'   ˆ  j  t ˆ j ƒ  ƒ ƒ ˆ j ƒ  d  S(   N(   R   R    R   R   (    (   t   threadPoolRefsR	   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyt   acquireThreadPool‰   s    i    N(   R   R   R   R    R!   R"   R#   (   R   R'   (    (   R	   R&   sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyt-   test_stopThreadPoolWhenStartedAfterReactorRan{   s    
c         C   sL   |  j  ƒ  } t | j ƒ  ƒ } | j d ƒ t j ƒ  |  j | ƒ  d ƒ d S(   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.
        t   shutdownN(   R   R    R   t   fireSystemEventR    R!   R"   R#   (   R   R	   t   threadPoolRef(    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyt,   test_cleanUpThreadPoolEvenBeforeReactorIsRun’   s
    
(	   t   __name__t
   __module__t   __doc__R   R   R   R%   R(   R,   (    (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyR      s   			+		(   R/   t   typet   __metaclass__t   weakrefR    R    t#   twisted.internet.test.reactormixinsR   t   twisted.python.threadpoolR   R   t   globalst   updatet   makeTestCaseClasses(    (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_threads.pyt   <module>   s   “