ó
ÐH/\c           @   sf  d  d l  Z  d  d l Z d  d l m Z d  d l m Z d  d l m Z d  d l m Z d  d l m	 Z	 d  d l
 m Z d  d l m Z d  d	 l m Z e e d
 ƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e e j f d „  ƒ  YZ d e e j f d „  ƒ  YZ d e f d „  ƒ  YZ e d ƒ d e e j f d „  ƒ  Yƒ Z e d k rbe j ƒ  n  d S(   iÿÿÿÿN(   t   gettrace(   t   settrace(   t   get_original(   t   thread_mod_name(   t   NativeStrIO(   t   skipOnPyPyOnWindows(   t   _monitor(   t   configt	   get_identt   MockHubc           B   sM   e  Z d Z d Z d  „  Z d „  Z e Z d „  Z e	 d „  ƒ Z
 d „  Z RS(   c         C   s%   t  ƒ  |  _ t ƒ  |  _ t |  _ d  S(   N(   R   t   thread_identR   t   exception_streamt   Falset   dead(   t   self(    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   __init__   s    c         C   s   |  j  S(   N(   R   (   R   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   __bool__   s    c         G   s   ‚  d  S(   N(    (   R   t   args(    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   handle_error#   s    c         C   s   |  S(   N(    (   R   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   loop&   s    c         C   s   d S(   s   mock loop.reinitN(    (   R   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   reinit*   s    N(   t   __name__t
   __module__t   Nonet   _threadpoolt	   _resolverR   R   t   __nonzero__R   t   propertyR   R   (    (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyR	      s   			t%   _AbstractTestPeriodicMonitoringThreadc           B   s   e  Z d  „  Z d „  Z RS(   c            s±   t  t ˆ  ƒ j ƒ  t j ˆ  _ t j ˆ  _ d „  t _ d ˆ  _ ‡  f d †  } | t _ t	 ƒ  ˆ  _
 t j ˆ  j
 ƒ ˆ  _ ˆ  j ˆ  j
 _ ˆ  j j ƒ  ˆ  _ t ˆ  j ƒ ˆ  _ d  S(   Nc         S   s
   t  j ƒ  S(   N(   t   gct   collect(   t   _s(    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   <lambda>6   s    Iï¾­Þ    c            s   ˆ  j  } ˆ  j  d 7_  | S(   Ni   (   t   tid(   t   _ft   _at   r(   R   (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   start_new_thread8   s    	(   t   superR   t   setUpt   monitorR%   t   _orig_start_new_threadt   thread_sleept   _orig_thread_sleepR!   R	   t   hubt   PeriodicMonitoringThreadt   pmtt   periodic_monitoring_threadt   monitoring_functionst   pmt_default_funcst   lent   len_pmt_default_funcs(   R   R%   (    (   R   s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyR'   2   s    		c         C   sy   |  j  t _ |  j t _ |  j j j } |  j j ƒ  t	 ƒ  | k sX t
 t	 ƒ  | f ƒ ‚ t d  ƒ t t |  ƒ j ƒ  d  S(   N(   R)   R(   R%   R+   R*   R.   t   _greenlet_tracert   previous_trace_functiont   killR    t   AssertionErrorR   R   R&   R   t   tearDown(   R   t   prev(    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyR8   D   s    $
(   R   R   R'   R8   (    (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyR   -   s   	t   TestPeriodicMonitoringThreadc           B   sn   e  Z d  „  Z e d ƒ d „  ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z RS(   c         C   s3   |  j  d |  j j ƒ |  j  t ƒ  |  j j ƒ d  S(   NIï¾­Þ    (   t   assertEqualR.   t   monitor_thread_identR    R4   (   R   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_constructorQ   s    s%   psutil doesn't install on PyPy on Winc         C   s9   |  j  j ƒ  } |  j | ƒ |  j | |  j  j ƒ  ƒ d  S(   N(   R.   t   _get_processt   assertIsNotNonet   assertIs(   R   t   proc(    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_get_processU   s    c         C   sc   |  j  |  j |  j j ƒ |  ` t j ƒ  |  j |  j j ƒ |  j |  j j ƒ |  j t ƒ  ƒ d  S(   N(	   R@   R,   R.   R   R   t   assertIsNonet   assertFalset
   should_runR    (   R   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_hub_wref[   s    
c         C   s/  |  j  t |  j j d  d ƒ |  j  t |  j j d „  d ƒ d „  } |  j j | d ƒ |  j |  j d t |  j j ƒ  ƒ ƒ |  j d |  j j ƒ  d j	 ƒ |  j j | d ƒ |  j |  j d t |  j j ƒ  ƒ ƒ |  j d |  j j ƒ  d j	 ƒ |  j j | d  ƒ |  j |  j t |  j j ƒ  ƒ ƒ d  S(   Ni   c           S   s   d  S(   N(   R   (    (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyR    j   s    iÿÿÿÿc           S   s   d S(   s   Does nothingN(    (    (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   fl   s    i   (
   t   assertRaisest
   ValueErrorR.   t   add_monitoring_functionR   R;   R3   R2   R0   t   period(   R   RG   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_add_monitoring_functiong   s    	& & c         C   sÖ   |  j  |  j j ƒ  d j |  j j ƒ  ƒ d |  j _ |  j  |  j j |  j j ƒ  ƒ d |  j j ƒ  d _ d |  j _ |  j j ƒ  |  j  |  j j ƒ  d j |  j j ƒ  ƒ |  j  |  j j ƒ  d j |  j j ƒ d  S(   Ni    iÿÿÿÿ(   R;   R.   R0   RK   t   calculate_sleep_timet   _calculated_sleep_timet   inactive_sleep_time(   R   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_calculate_sleep_time}   s     	c            sC   ‡  f d †  } ˆ  j  j | d ƒ ˆ  j  ƒ  ˆ  j ˆ  j  j ƒ d  S(   Nc            s   d  }  d  ˆ  _ t j ƒ  d  S(   N(   R   R,   R   R   (   t   _hub(   R   (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyRG   ™   s    	gš™™™™™¹?(   R.   RJ   RD   RE   (   R   RG   (    (   R   s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_call_destroyed_hub–   s    
c         C   s=   d „  } |  j  j | d ƒ |  j  ƒ  |  j |  j  j ƒ d  S(   Nc         S   s   t  |  _ d  S(   N(   t   TrueR   (   R,   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyRG   ¥   s    gš™™™™™¹?(   R.   RJ   RD   RE   (   R   RG   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_call_dead_hub¢   s    	
c         C   s*   d „  } |  j  j | d ƒ |  j  ƒ  d  S(   Nc         S   s   t  ƒ  ‚ d  S(   N(   t
   SystemExit(   RQ   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyRG   ­   s    gš™™™™™¹?(   R.   RJ   (   R   RG   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_call_SystemExit«   s    	c            s\   d t  f d „  ƒ  Y‰  ‡  f d †  } |  j j | d ƒ |  j ˆ  ƒ  |  j ƒ  Wd  QXd  S(   Nt   MyExceptionc           B   s   e  Z RS(    (   R   R   (    (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyRW   ´   s   c            s   ˆ  ƒ  ‚ d  S(   N(    (   RQ   (   RW   (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyRG   ·   s    gš™™™™™¹?(   t	   ExceptionR.   RJ   RH   (   R   RG   (    (   RW   s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_call_other_error³   s
    c         C   s{   d d  l  } d d l m } d |  j _ |  j j } | |  j ƒ |  j | j ƒ  |  j j ƒ |  j | d |  j j ƒ d  S(   Niÿÿÿÿ(   R   i   (	   t   ost
   gevent.hubR   R.   t   pidR<   R,   R;   t   getpid(   R   RZ   R   t   old_tid(    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_hub_reinit¾   s    (   R   R   R=   R   RB   RF   RL   RP   RR   RT   RV   RY   R_   (    (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyR:   N   s   									t   TestPeriodicMonitorBlockingc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c            s³   |  j  j ƒ  |  j t ƒ  ƒ g  ‰  ‡  f d †  } t | ƒ t j |  j ƒ |  _  |  j t ƒ  |  j  j	 ƒ |  j
 |  j  j	 j | ƒ |  j  j	 d d ƒ |  j d d f g ˆ  ƒ d  S(   Nc             s   ˆ  j  |  ƒ d  S(   N(   t   append(   R   (   t   called(    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyRG   Ó   s    t   eventR   (   R   (   R   (   R.   R6   RC   R    R   R(   R-   R,   R;   R4   R@   R5   (   R   RG   (    (   Rb   s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_previous_traceÎ   s    
c         C   sþ   |  j  d |  j j j ƒ |  j j d d  ƒ |  j  d |  j j j ƒ |  j |  j j j ƒ t ƒ  } t ƒ  } |  j j d | | f ƒ |  j  d |  j j j ƒ |  j | |  j j j ƒ |  j j d d ƒ |  j  d |  j j j ƒ |  j |  j j j ƒ d  S(   Ni    t   unknowni   t   switchi   i   (    (	   R;   R.   R4   t   greenlet_switch_counterR   RC   t   active_greenlett   objectR@   (   R   t   origint   target(    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test__greenlet_tracerà   s    		c         C   s  d d l  m } d d l  m } d d l m } g  } | j | j ƒ |  j |  j j |  j	 ƒ ƒ t
 ƒ  } t
 ƒ  } |  j j d | | f ƒ |  j |  j j |  j	 ƒ ƒ |  j | ƒ |  j |  j j |  j	 ƒ ƒ |  j | ƒ | | | d ƒ | 2|  j j ƒ  |  j |  j j |  j	 ƒ ƒ |  j | ƒ |  j j ƒ  |  j |  j j |  j	 ƒ ƒ d |  j	 _ |  j |  j j |  j	 ƒ ƒ d  S(   Niÿÿÿÿ(   t   subscribers(   t   IEventLoopBlocked(   t   verifyObjectRf   i    (   t   gevent.eventsRm   Rn   t   zope.interface.verifyRo   Ra   RD   R.   t   monitor_blockingR,   Ri   R4   t
   assertTruet    ignore_current_greenlet_blockingt!   monitor_current_greenlet_blockingR
   (   R   Rm   Rn   Ro   t   eventsRj   Rk   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_monitor_blockingó   s,    		(   R   R   Rd   Rl   Rw   (    (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyR`   Ë   s   		t   MockProcessc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   rss(   R   Ry   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyR     s    c         C   s   |  S(   N(    (   R   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   memory_full_info   s    (   R   R   R   Rz   (    (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyRx     s   	s%   psutil doesn't install on PyPy on Wint   TestPeriodicMonitorMemoryc           B   sM   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   i    c            sA   t  t ˆ  ƒ j ƒ  t j ˆ  _ d  t _ ‡  f d †  ˆ  j _ d  S(   Nc              s   t  ˆ  j ƒ S(   N(   Rx   Ry   (    (   R   (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyR    /  s    (	   R&   R{   R'   t   GEVENT_CONFIGt   max_memory_usaget   _old_maxR   R.   R>   (   R   (    (   R   s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyR'   *  s    	c         C   s#   |  j  t _ t t |  ƒ j ƒ  d  S(   N(   R~   R|   R}   R&   R{   R8   (   R   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyR8   1  s    c         C   sM   |  j  |  j j ƒ  ƒ |  j j ƒ  |  j |  j d t |  j j ƒ  ƒ ƒ d  S(   Ni   (   Rs   R.   t   can_monitor_memory_usaget   install_monitor_memory_usageR;   R3   R2   R0   (   R   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_can_monitor_and_install5  s    c         C   s   d d  l  } d „  |  j _ |  j |  j j ƒ  ƒ | j d t ƒ  } |  j j ƒ  Wd  QX|  j d t	 | ƒ ƒ |  j
 t j | d j ƒ d  S(   Niÿÿÿÿc           S   s   d  S(   N(   R   (    (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyR    A  s    t   recordi   i    (   t   warningsR.   R>   RD   R   t   catch_warningsRS   R€   R;   R2   R@   R(   t   MonitorWarningt   category(   R   Rƒ   t   ws(    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_cannot_monitor_and_install?  s    c         C   s    |  j  d |  j j d  ƒ ƒ d  S(   Niÿÿÿÿ(   R;   R.   t   monitor_memory_usageR   (   R   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_monitor_no_allowedK  s    c         C   s  d d l  m } d |  _ d t _ |  j j d  ƒ } |  j | | j	 ƒ |  j
 d | j ƒ |  j
 d | j ƒ |  j | j t ƒ |  j j d  ƒ } |  j | ƒ d |  _ |  j j d  ƒ } |  j | | j	 ƒ |  j
 d | j ƒ d |  _ |  j j d  ƒ } |  j | | j ƒ |  j
 d | j ƒ t | ƒ |  j j d  ƒ } |  j | ƒ d |  _ |  j j d  ƒ } |  j | | j	 ƒ |  j
 d | j ƒ d  S(   Niÿÿÿÿ(   Rv   i   i   i   (   t   geventRv   Ry   R|   R}   R.   R‰   R   t   assertIsInstancet   MemoryUsageThresholdExceededR;   t	   mem_usaget   max_allowedt   memory_infoRx   RC   t   MemoryUsageUnderThresholdt   repr(   R   Rv   Rc   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_monitor_greaterN  s2    				
	c         C   s5   d |  _  d t _ |  j j d  ƒ } |  j | ƒ d  S(   Ni   i
   (   Ry   R|   R}   R.   R‰   R   RC   (   R   Rc   (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   test_monitor_initial_belowy  s    		(
   R   R   Ry   R'   R8   R   Rˆ   RŠ   R“   R”   (    (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyR{   $  s   			
			+t   __main__(   R   t   unittestt   greenletR    R   t   gevent.monkeyR   t   gevent._compatR   R   t   gevent.testing.skippingR   R‹   R   R(   R   R|   R   Ri   R	   R   t   TestCaseR:   R`   Rx   R{   R   t   main(    (    (    s:   lib/python2.7/site-packages/gevent/tests/test___monitor.pyt   <module>   s,   !|O	[