
\c           @` s	  d  d l  m Z m Z m Z d  d l Z d  d l m Z d  d l Z d  d l m	 Z	 d  d l
 Z
 d  d l Z d  d l m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l Z d  d l Z y d  d l Z Wn e  k
 rud Z n Xd  d l" Z" d  d l# Z# d  d l$ Z$ d  d l% m& Z& m' Z' m( Z( d  d l) m* Z* m+ Z+ d  d	 l, m- Z- d  d
 l. m/ Z/ d d l0 m1 Z1 m2 Z2 m3 Z3 d d l4 m5 Z5 m6 Z6 m7 Z7 m8 Z8 d d l9 m: Z: d d l; m< Z< d d l= m> Z> d d l? m@ Z@ mA ZA mB ZB d d lC m Z d d lD mE ZE d d lF mG ZG d d lH mI ZI d d lJ mK ZK mL ZL mM ZM mN ZN mO ZO mP ZP mQ ZQ mR ZR mS ZS mT ZT d d lU mV ZV mW ZW mX ZX y d  d lY Z$ Wn e  k
 r;n Xe jZ e[  Z\ d   e j] j^ j_ j`   D Za e< d    jb   e" jc d d  d    Zd e" jc d d  d    Ze e" jc d d  d    Zf e* jg d    Zh e" jc d     Zi e" jc d!    Zj e" jc d"    Zk e d#    Zl e d$    Zm e jn e$ j= j=  Zo d%   Zp d&   Zq d'   Zr d(   Zs d)   Zt d*   Zu d+   Zv d,   Zw d-   Zx d.   Zy d/   Zz d0 d1  Z{ d0 d2  Z| d0 d3  Z} d d4  Z~ d0 d5  Z d0 d6  Z d7   Z e d8  Z e j e  Z e j   Z d9 e f d:     YZ d;   Z d<   Z e* jg d0 d=   Z e d>  Z e j d k re dA  e st  n d Z i  Z e* jg dB    Z dC   Z dD   Z dE   Z e d dF   Z e" jc dG    Z e" jc dH    Z e" jc dI    Z e" jc dJ    Z e" jc dK    Z0 e" jc dL    Z e d d d dM   Z e" jc dN    Z e" jc dO    Z e" jc dP    ZH e dQ e i  d i  dR   Z e* jg d? d dS   Z e* jg d? d dT   Z dU   Z dV dW  Z d dX l m Z d dY lU mV ZV e* jg d eV i  i  dZ   Z e* jg d[    Z d d g d\ dV d eV e i  i  i  d i  e d]  Z e d^  Z d_   Z e d`    Z d@ da  Z d db dc  Z e* jg d db dd   Z e' de    Z e   rdf   Z n e" j j dg  Z e* jg d d dh   Z e* jg d d e di   Z e* jg d d dj dk   Z e* jg d d dj dl   Z e* jg d d dm   Z e* jg d d dn   Z e* jg d d do   Z e e j d dp   Z e dq    Z e dr    Z e ds    Z e dt    Z e j j e j j e j j e  du   Z dv   Z dw   Z dx   Z dy   Z dz   Z d{ d| d} d~  Z d{ d| d} d  Z d   Z d   Z d S(   i    (   t   print_functiont   divisiont   absolute_importN(   t   contextmanager(   t	   timedelta(   t   glob(   t   sleep(   t   merget   memoizet   assoc(   t   gent   queues(   t   TimeoutError(   t   IOLoopi   (   t   default_clientt   _global_clientst   Client(   t   PY3t   Emptyt   WINDOWSt   PY2(   t   Comm(   t   offload(   t   initialize_logging(   t   connectt   rpct   CommClosedError(   t   time(   t   _cleanup_dangling(   t   enable_proctitle_on_children(   t   Security(
   t   ignoringt
   log_errorst
   mp_contextt   get_ipt   get_ipv6t   DequeHandlert   reset_logger_lockst   synct   iscoroutinefunctiont   thread_state(   t   Workert   TOTAL_MEMORYt   _global_workersc         C` s4   i  |  ]* \ } } t  | t j  r | j |  q S(    (   t
   isinstancet   loggingt   Loggert   level(   t   .0t   namet   logger(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pys
   <dictcomp>L   s   		c           C` s   d  S(   N(   t   None(    (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   <lambda>R   t    t   scopet   sessionc         C` s)   |  j  d  j d  } | j d  | S(   Nt   datas   file.pys   print('hello world!')(   t   mktempt   joint   write(   t   tmpdir_factoryt
   local_file(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   valid_python_scriptU   s    c         C` s8   |  j  d  j d  } d } | j d j |   | S(   NR8   s   distributed_script.pys   from distributed import Clients   e = Client('127.0.0.1:8989')s   print(e)s   
(   s   from distributed import Clients   e = Client('127.0.0.1:8989')s   print(e)(   R9   R:   R;   (   R<   R=   t   lines(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   client_contract_script\   s      c         C` s)   |  j  d  j d  } | j d  | S(   NR8   s   file.pys   a+1(   R9   R:   R;   (   R<   R=   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   invalid_python_scripth   s    c          C` s4   x- t  D]% }  |    }  |  j d t d t  q Wd  S(   Nt   reportt   executor_wait(   R+   t   closet   False(   t   w(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   cleanup_global_workerso   s    	c       	   #` s{  t  2t j   t    }  |  j  t j       j      f d   } | |  _ |  Vy' t |  t	 d d |  j
 |  j  WnD t k
 r } t j d t |   s   q n t j k
 r n X  j   Wd  QXt  2t   } x3 t t  rt d  t   | d k  s t  q Wt   t rmxC t j   j d t  D]& } t t j   | j   Wd  QXq@Wn  t j   d  S(   Nc           ` s'     j    z    Wd    j   Xd  S(   N(   t   cleart   set(    (   t
   is_stoppedt
   orig_start(    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   start   s    
t   callback_timeoutg      ?s   IOLoop is clos(ed|ing)g?i
   t	   recursive(   R+   R   RH   t   pristine_loopRL   t	   threadingt   EventRI   R&   RG   t   add_callbackt   stopt   RuntimeErrort   ret   matcht   strR
   R   t   waitR   R   t   AssertionErrorR   R   t   psutilt   Processt   childrent   TrueR   t   NoSuchProcesst	   terminate(   t   loopRL   t   et   child(    (   RJ   RK   s5   lib/python2.7/site-packages/distributed/utils_test.pyR`   v   s:    
	
			
c       	   c` s   t     }  t j d |  j d d  } t | _ | j   t j   } |  j | j  | j	   |  V|  j |  j
  | j d d  Wd  QXd  S(   Nt   targetR1   s   test IOLoopt   timeouti   (   RO   RP   t   ThreadRL   R]   t   daemonRQ   RR   RI   RX   RS   R:   (   R`   t   threadt   loop_started(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   loop_in_thread   s    	

c          c` s4   d d  l  }  |  j j   } | V| j d d  d  S(   Ni    t   linger(   t   zmqt   Contextt   instancet   destroy(   Rk   t   ctx(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   zmq_ctx   s    c       	   c` s   t  j   t  j   t    }  |  j   t  j   |  k s? t  z	 |  VWd  y |  j d t  Wn t t	 f k
 rx n Xt  j   t  j   Xd  S(   Nt   all_fds(
   R   t   clear_instancet   clear_currentt   make_currentt   currentRY   RD   R]   t   KeyErrort
   ValueError(   R`   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyRO      s    

	
	
c       	   #` s   d d  l  }  d d l m } |  j     i    _ d    _   f d   } |  j d |  # |  j d |  
   VWd  QXWd  QXx! | j j	   D] } | j
   q W| j j   d  S(   Ni    (   t   remote_magicc           ` s     S(   N(    (    (   t   ip(    s5   lib/python2.7/site-packages/distributed/utils_test.pyR"      s    s   IPython.get_ipythons&   distributed._ipython_utils.get_ipython(   t   mockt   distributed._ipython_utilsRx   t   Mockt   user_nsR3   t   kernelt   patcht   _clientst   valuest   stop_channelsRH   (   Rz   Rx   R"   t   kc(    (   Ry   s5   lib/python2.7/site-packages/distributed/utils_test.pyt   mock_ipython   s    		 c           C` s0   t  j j j   t  j j j t j t   d  S(   N(   t   daskt   configRH   t   updatet   copyt   deepcopyt   original_config(    (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   reset_config   s    c         ` s,   t  s
   St j      f d    } | S(   s   
    A decorator to disable debug facilities during timing-sensitive tests.
    Warning: this doesn't affect already created IOLoops.
    c          ` s`   t  j j d  } | d  k	 r+ t  j d =n  z   |  |   SWd  | d  k	 r[ | t  j d <n  Xd  S(   Nt   PYTHONASYNCIODEBUG(   t   ost   environt   getR3   (   t   argst   kwargst   old_asyncio_debug(   t   func(    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   wrapped   s    (   R   t	   functoolst   wraps(   R   R   (    (   R   s5   lib/python2.7/site-packages/distributed/utils_test.pyt   nodebug   s    c         C` s5   t  j j d  |  _ |  j d k	 r1 t  j d =n  d S(   sa   
    A setup_module() that you can install in a test module to disable
    debug facilities.
    R   N(   R   R   R   t   _old_asyncio_debugR3   (   t   module(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   nodebug_setup_module  s    c         C` s&   |  j  d k	 r" |  j  t j d <n  d S(   se   
    A teardown_module() that you can install in a test module to reenable
    debug facilities.
    R   N(   R   R3   R   R   (   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   nodebug_teardown_module  s    c         C` s   |  d S(   Ni   (    (   t   x(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   inc  s    c         C` s   |  d S(   Ni   (    (   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   dec  s    c         C` s   |  | S(   N(    (   R   t   y(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   mul"  s    c         C` s   |  | S(   N(    (   R   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   div&  s    c         C` s"   |  d k r t  |  d  St Sd  S(   Ni    i   (   t   deepR]   (   t   n(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyR   *  s    c         C` s   t  d   d  S(   Ns   hello!(   RT   (   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   throws1  s    c         C` s   |  d S(   Ni   (    (   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   double5  s    g{Gz?c         C` s   t  |  |  d S(   Ni   (   R   (   R   t   delay(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   slowinc9  s    
c         C` s   t  |  |  d S(   Ni   (   R   (   R   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   slowdec>  s    
c         C` s   t  |  d |  S(   Ni   (   R   (   R   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt
   slowdoubleC  s    
c         C` s)   d d l  m  } t |   |  |  d S(   Ni    (   t   randomi   (   R   R   (   R   t   scaleR   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt	   randomincH  s    c         C` s   t  |  |  | S(   N(   R   (   R   R   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   slowaddO  s    
c         C` s   t  |  t |   S(   N(   R   t   sum(   t   seqR   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   slowsumT  s    
c          O` s>   | j  d d  } t |  t |   d k r6 |  d S|  Sd  S(   NR   g{Gz?i   i    (   R   R   t   len(   R   R   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   slowidentityY  s
    
c         C` s'   |   |  } x |   | k r" q Wd S(   s*   
    Burn CPU for *duration* seconds.
    N(    (   t   durationt   timert   deadline(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   run_forb  s    t   _ModuleSlotc           B` s   e  Z d    Z d   Z RS(   c         C` s   | |  _  | |  _ d  S(   N(   t   modnamet   slotname(   t   selfR   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   __init__q  s    	c         C` s   t  t j |  j |  j  S(   N(   t   getattrt   syst   modulesR   R   (   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyR   u  s    (   t   __name__t
   __module__R   R   (    (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyR   p  s   	c         ` s4   t  t d   t t       f d   } | S(   si   
    Return a function that returns a result (or raises an exception)
    from *items* at each call.
    t   _varying_dictc          ` si    j    }  |   } | t    k r1 t  n4   | } | d |   <t | t  ra |  n | Sd  S(   Ni   (   R   R   t
   IndexErrorR,   t	   Exception(   t   dctt   iR   (   t   itemst   keyt   slot(    s5   lib/python2.7/site-packages/distributed/utils_test.pyR     s    
	
	(   R   R   t   nextt   _varying_key_gen(   R   R   (    (   R   R   R   s5   lib/python2.7/site-packages/distributed/utils_test.pyt   varyingy  s    	c         C` s   d   } | t  t |   f S(   si   
    Like *varying*, but return the full specification for a map() call
    on multiple items lists.
    c         _` s   |  | |   S(   N(    (   R   R   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   apply  s    (   t   mapR   (   t
   itemslistsR   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   map_varying  s    	c         c` s%   t  j |  Vt  j |  d   d  S(   Ni   (   R
   R   t   Return(   R   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   geninc  s    c         B` sA   | r e  j |   }  n  e |  d d  }  e   } |  | | Ud  S(   Ns	   <dynamic>t   exec(   t   textwrapt   dedentt   compilet   globals(   t   codeR   t   ns(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   compile_snippet  s
    	i   i   sp   
        async def asyncinc(x, delay=0.02):
            await gen.sleep(delay)
            return x + 1
        c         #` s   y t     WnG t k
 rW t j    t    <t j    f d    } |   n X j   V} | d k rz t  n t j	 |   d S(   sR   
    Read one message at a time from a comm that reads lists of
    messages.
    c          3` si   xN t  rP y   j   V}  Wn t k
 r. Pn Xx |  D] }  j |  q6 Wq W j d   t   =d  S(   N(   R]   t   readR   t
   put_nowaitR3   t   _readone_queues(   t   messagest   msg(   t   commt   q(    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   background_read  s    	N(
   R   Rv   R   t   QueueR
   t	   coroutineR   R3   R   R   (   R   R   R   (    (   R   R   s5   lib/python2.7/site-packages/distributed/utils_test.pyt   readone  s    	c      
   K` s   d d l  m } t   q } | d t |  } | j d  } x$ t |  D] } |  j | j  qJ Wz | j   Wd  | j d t  XWd  QXd  S(   Ni    (   t	   Schedulert   validates	   127.0.0.1Rq   (	   t   distributedR   RO   R]   RL   t   ranget   putt   addressRD   (   R   t   nputsR   R   R`   t	   schedulert   doneR   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   run_scheduler  s    c         ` s   d d l  m } t   t    t    } | j   } | | d t |   | j   f d    |  j   j	  z) t
 j   f d    } | j |  Wd  | j d t  XWd  QXWd  QXd  S(   Ni    (   R)   R   c           ` s     j  d  S(   Ni    (   t   _start(    (   t   worker(    s5   lib/python2.7/site-packages/distributed/utils_test.pyR4     R5   c           3` s     j  j   Vd  S(   N(   t   _closedRX   (    (   R   (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   wait_until_closed  s    Rq   (   R   R)   R%   R    RO   R   R]   t   run_syncR   R   R
   R   RD   (   R   t   scheduler_qR   R)   R`   t   scheduler_addrR   (    (   R   s5   lib/python2.7/site-packages/distributed/utils_test.pyt
   run_worker  s    
c         ` s   d d l  m } t    t    } | j   } | | d t |   | j   f d    |  j   j  z | j	   Wd  | j   j
  | j
 d t  XWd  QXWd  QXd  S(   Ni    (   t   NannyR   c           ` s     j  d  S(   Ni    (   R   (    (   R   (    s5   lib/python2.7/site-packages/distributed/utils_test.pyR4     R5   Rq   (   R   R   R    RO   R   R]   R   R   R   RL   RD   (   R   R   R   R   R`   R   (    (   R   s5   lib/python2.7/site-packages/distributed/utils_test.pyt	   run_nanny  s    
c         #` s{   t  t j      r8 t r8 t j   t  t j    n  d  V  f d    t j     f d    } |  j |  d  S(   Nc           ` s"   t  j d t t j     d  S(   Ns!   some RPCs left active by test: %s(   t   pytestt   failRI   R   t   active(    (   t   active_before(    s5   lib/python2.7/site-packages/distributed/utils_test.pyR   $  s    c           3` s$   t    f d   d  d  Vd  S(   Nc           ` s   t  t t j     d k S(   Ni    (   R   RI   R   R   (    (   R   (    s5   lib/python2.7/site-packages/distributed/utils_test.pyR4   ,  R5   Rd   t	   fail_func(   t   async_wait_for(    (   R   t   active_rpc_timeoutR   (    s5   lib/python2.7/site-packages/distributed/utils_test.pyRX   )  s    (	   RI   R   R   R   t   gct   collectR
   R   R   (   R`   R   RX   (    (   R   R   R   s5   lib/python2.7/site-packages/distributed/utils_test.pyt   check_active_rpc  s    
c         c` s'   t     \ } } | | f VWd  QXd  S(   N(   t   cluster(   R`   R   t   workers(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   cluster_fixture4  s    c         C` s   |  \ } } | S(   N(    (   R  R   R  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   s:  s    c         C` s   |  \ } } | d S(   Ni    (    (   R  R   R  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   a@  s    c         C` s   |  \ } } | d S(   Ni   (    (   R  R   R  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   bF  s    c         c` s4   | \ } } t  | d d |   } | VWd  QXd  S(   NR   R`   (   R   (   R`   R  R   R  t   client(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyR	  L  s    c         c` s4   | \ } } t  | d d |   } | VWd  QXd  S(   NR   R`   (   R   (   R`   R  R   R  R	  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   client_secondaryS  s    c         k` su   | p t    } t |  p i  d |  }  t | p3 i  d |  } t d |  d | |   \ } } | | f VWd  QXd  S(   Nt   securityt   worker_kwargst   scheduler_kwargs(   t   tls_only_securityR	   R  (   R  R  R  R   R  R  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   tls_cluster_contextZ  s    	c         c` s-   t  d |   \ } } | | f VWd  QXd  S(   NR  (   R  (   R`   R  R   R  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   tls_clusterh  s    c         c` s:   |  \ } } t  | d d | d |  } | VWd  QXd  S(   NR   R  R`   (   R   (   R  R`   R  R  R  R	  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt
   tls_clientn  s    c           C` s   t    S(   N(   R  (    (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyR  u  s    i   c         #` sl  t  j   } t   t j j   x0 t j   D]" \ } } t j	 |  j
 |  q- Wt   t   >} t | |  &| r t }	 n t }	 t j   }
 t j d t d |
 |  d f d |  } | j |  t | _ | j   g   x t |   D] } t j   } d t j   } t i d d 6| d 6t d 6|  } t j d |	 d | |
 f d |  } | j |   j i | d	 6| d
 6| d 6 q Wx  D] } | d	 j   qWy/ x(  D]  } | d
 j d d  | d <qWWn# t k
 rt  j! j" d   n X|
 j    t#   } z y$ | d } i | j$ d  d 6  Wn t% k
 rXi    n Xt&     ^ } xT t r| j' | j(  } t) |  |  k rPn  t#   | d k rnt" d   qnqnWWd  QXi  d 6g   D]+ } i | d d 6t  j* | d	  d	 6^ qf VWd  t+ j, d  | j'    f d    | j'    f d    | j-   |
 j.   |
 j/ j.   |
 j0 j.   xL  D]D } | d	 j-   | d
 j.   | d
 j/ j.   | d
 j0 j.   qW| j1 d  ~ x5 g   D] } | d	 ^ qD] } | j1 d d  qWt2 t3   ~ ~ ~ Wd  QX 2x4 t4 d  D]& } t2 t5   t6 j7 |  Wd  QXqAWXy t8   } Wn t9 k
 rn X| j.   Wd  QXWd  QXt#   } x9 t: |  rt; d  t#   | d k  st< d   qWx} t d  D]- } t= d   t j D  rPqt; d  qWg  t j D] } | j>   s0| ^ q0} t j j   t? d |  d  S(   NRc   R   i   R   s   _test_worker-%st   ncorest	   local_dirt   memory_limitt   proct   queuet   dirRd   i   R   s   Worker failed to start in testR  R	  t   connection_argss   Timeout on cluster creations   Closing out test clusterc          ` s-   t  g   D] }  |  d ^ q
 d d d   S(   NR   Rd   g      ?t
   rpc_kwargs(   t   disconnect_all(   RF   (   R  R  (    s5   lib/python2.7/site-packages/distributed/utils_test.pyR4     s   c           ` s   t   d d d   S(   NRd   g      ?R  (   t
   disconnect(    (   R  t   saddr(    s5   lib/python2.7/site-packages/distributed/utils_test.pyR4     R5   i   s   _test_worker-*g{Gz?s%   Workers still around after one secondc         s` s   |  ] } | j    Vq d  S(   N(   t   closed(   R0   t   c(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pys	   <genexpr>  s    g?s   Unclosed Comms(@   t   weakreft   WeakSetR   R   t
   _instancesRH   t   logging_levelsR   R-   t	   getLoggert   setLevelR   RO   R  R   R   R!   R   R[   R   t   addR]   Rf   RL   R   t   uuidt   uuid4R   R*   t   appendR   R   R   t   xfailR   R   t   get_connection_argsRv   R   R   R  R   t   refR2   t   debugR_   RD   t   _readert   _writerR:   R   t   UnboundLocalErrorR   t   OSErrort   shutilt   rmtreeR   Rw   t   listR   RY   t   allR  t   print(   t   nworkerst   nannyR  R   R  t   wsR1   R/   R`   t   _run_workerR   R   R   R   t   fnR   R  R   RL   R  R  R  RF   R	  R  t   L(    (   R  R  R  s5   lib/python2.7/site-packages/distributed/utils_test.pyR  z  s    				
		&"	

	
;

$	
#(c         #` s[    p	 i   t  j    f d    } t t  " t  j t d |  |    VWd  QXd  S(   Nc          3` sC   t  t t  . t      }  |  j d t  VWd  QXWd  QXd  S(   NRD   (   R   t   EnvironmentErrorR   R   R_   R]   (   RF   (   t   addrR  (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   do_disconnect  s    t   seconds(   R
   R   R   R   t   with_timeoutR   (   R=  Rd   R  R>  (    (   R=  R  s5   lib/python2.7/site-packages/distributed/utils_test.pyR  
  s    c         c` s(   g  |  D] } t  | | |  ^ q Vd  S(   N(   R  (   t	   addressesRd   R  R=  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyR    s    c         C` sO   y1 t  j j d  s0 t  j j d  |   }  n  Wn t k
 rD n Xt |   S(   Ns	   --runslows   need --runslow option to run(   R   R   t	   getoptiont   markt   skipt   AttributeErrorR   (   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   slow  s    i
   c         ` s     f d   } | S(   sn    Coroutine test

    @gen_test(timeout=5)
    def test_foo():
        yield ...  # use tornado coroutines
    c         ` s      f d   } | S(   Nc          ` s_   t    P }  t    r!   } n t j    } z |  j | d  Wd  |  j   XWd  QXd  S(   NRd   (   RO   R'   R
   R   R   RS   (   R`   t   cor(   R   Rd   (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt	   test_func1  s    	(    (   R   RH  (   Rd   (   R   s5   lib/python2.7/site-packages/distributed/utils_test.pyt   _0  s    (    (   Rd   RI  (    (   Rd   s5   lib/python2.7/site-packages/distributed/utils_test.pyt   gen_test(  s    (   R   (   R)   c         c` s  t  d | d t d | |  } | j |  } g  t |   D]e \ }	 }
 | | j d |
 d d |	 d | d | d t t |
  d k r t | |
 d  n | ^ q: } g  t |  |  D] \ }
 } | j |
 d  ^ q Vt	   } x t | j
  t |   k  st d	   | j j   D  rt j d
  Vt	   | d k r g  | D] } | j d d  ^ qGV| j d t  Vt d   q q Wt j | | f   d  S(   NR`   R   R  R  i   R1   i   i    c         s` s   |  ] } | j  d  k Vq d  S(   N(   R   R3   (   R0   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pys	   <genexpr>d  s    g{Gz?i   Rd   t   fasts   Cluster creation timeout(   R   R]   RL   t	   enumerateR   R   R   t   zipR   R   R  t   anyt   stream_commsR   R
   R   RD   R   R   (   R  R   R`   R  R)   R  R  R  R   R   t   ncoreR  RF   RL   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   start_clusterE  s    
	u4	!$c         c` sV   t  j d  t j d    } g  | D] } | |  ^ q& V|  j   V|  j   d  S(   Ns   Closing out test clusterc         s` s.   t  t t t   |  j d t  VWd  QXd  S(   NRB   (   R   R   R   R<  RD   RE   (   RF   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt
   end_workerr  s    (   R2   R,  R
   R   RD   RS   (   R  R  RR  RF   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   end_clustern  s
    s	   127.0.0.1c         ` sd   d d l  m   t i t d 6d d 6             	 
   f d   } | S(   Ni    (   R   R  i   t   death_timeoutc         ` sX   t     s t j      n            	 
    f d   } | S(   Nc          ` s  t  2t j   t j j   t t j  }  t   t	 j
 j i d d 6 x0 t j   D]" \ } } t j |  j |  qU Wd  } g   t   ,  t     i t j      	   
       f d    }   j | d  r  d n  } Wd  QXxN  D]F } t | d d   ry | j j   Wn t k
 rPn X| ` qqWt j   xI t  D]A } |   } | j d t d t  | j d	 k ro| j   qoqoWt  2Wd  QXt rt r rt    } x t! rg  t j j   D]K \ } } | |  k rd
 | j" k rd | j" k rd | j" k r| ^ q}	 |	 sNPn
 t# d  t    | d k rd d l$ m% }
 |	 d } t j | } |
 j& t' j(   |  } t st) | | f   qqWn  t*   t+ t,   t- `. Wd  QX| S(   Nt   5ss!   distributed.comm.timeouts.connectc    	      3` s{  t  j j   ct }  x t d  D]y } y5 t    d 
 d  d 	 d  V\ }  } Wn& t k
 r } t j d d t	 q& X|  (|  g  } Pq& W|  t k r t d   n   r   |  j
 d	  d 
 d
 t	  V} | g | } n  zQ  |   }  r,t j t d   |  } n  | V} |  j rI|  j   n  Wd   r| j d k r| j d |  j d k  Vn  t |    Vt j t d d  t    VXy t   V} Wn t k
 rn X| j d t	  Vx t d  D]1 } t d   t j D  rPqt j d  VqWg  t j D] } | j   s*| ^ q*} t j j   t d |  t j |   Wd  QXd  S(   Ni   R  R)   R  R  s%   Failed to start gen_cluster, retryingt   exc_infos   Could not start clusterR`   t   asynchronousR?  t   closingR  RK  i   c         s` s   |  ] } | j    Vq d  S(   N(   R  (   R0   R  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pys	   <genexpr>  s    g?s   Unclosed Comms(   RX  R  (   R   R   RI   RE   R   RQ  R   R2   t   errorR]   R   R
   R@  R   R   t   validate_statet   statust   _closeRS  RG   R   Rw   R4  R   R!  R   R  RH   R5  R   (	   R  R   R8  Ra   R   R  t   futuret   resultR;  (   R   R)   R	  t   client_kwargsR   R   R`   R  R   R  R  Rd   R  R  (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   coro  sn    	
	(Rd   i   R8   RB   RC   t   runningt   Threadeds   watch messages   TCP-Executorg{Gz?i   i    (   t   profile(/   R+   R   RH   R   R!  RI   RP   t   _activeR   R   R   R"  R   R-   R#  R$  R3   RO   R  R
   R   R   R   R8   R<  R$   t   clear_all_instancesRD   RE   R[  R   R   R   R]   R1   R   R   Rc  t
   call_stackR   t   _current_framesRY   R   R   RE  R(   t   on_event_loop_thread(   t   active_threads_startR1   R/   R^  R`  RF   RL   t   tt   vt   badRc  t   tidRg   t   call_stacks(   R   R)   R   t   check_new_threadsR	  R_  R   R   R  R   R  R  Rd   R  (   R`   R  s5   lib/python2.7/site-packages/distributed/utils_test.pyRH    sf    
?H%
	
		

"(   R'   R
   R   (   R   RH  (   R   R)   R   Ro  R	  R_  R   R  R   R  R  Rd   R  (   R   s5   lib/python2.7/site-packages/distributed/utils_test.pyRI    s    6(   R   R   R   R*   (   R  R   Rd   R  R)   R	  R  R  R_  R   R   Ro  RI  (    (   R   R)   R   Ro  R	  R_  R   R  R   R  R  Rd   R  s5   lib/python2.7/site-packages/distributed/utils_test.pyt   gen_cluster|  s
    3c         C` s(   y |    t  SWn | k
 r# t SXd  S(   N(   RE   R]   (   R   t   exc(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   raises/  s
    c         C` s   |  j    d  k r t j j d  r7 |  j t j  n |  j t j  zf t j	 d d k rm |  j
 d  n? t   } x3 |  j    d  k r t   | d k  r t d  qy WWd  t t   |  j   Wd  QXXn  d  S(   Nt   wini    i   i
   g{Gz?(   t   pollR3   R   t   platformt
   startswitht   send_signalt   signalt   CTRL_BREAK_EVENTt   SIGINTt   version_infoRX   R   R   R   R0  t   kill(   R  RL   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   terminate_process7  s    	(c      
   k` sm  t  j | d <t  j | d <t j j d  r< t  j | d <n  t } t |   }  t j j d  r t j	 j
 t j d |  d  |  d <n6 t j	 j
 t j j d d  t j d	 |  d  |  d <t  j |  |  } z* y	 | VWn t k
 r t }   n XWd  z t |  Wd  | j   \ } } | rgt d
 |  d d  t | j    t d  t | j    n  XXd  S(   Nt   stdoutt   stderrRs  t   creationflagst   Scriptsi    t   DESTDIRR5   t   bins+   

Print from stderr
  %s
=================
s&   

Print from stdout
=================
(   t
   subprocesst   PIPER   Ru  Rv  t   CREATE_NEW_PROCESS_GROUPRE   R3  R   t   pathR:   t   prefixR   R   t   PopenR   R]   R}  t   communicateR5  t   decode(   R   R   t   dump_stdoutR  t   outt   err(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   popenJ  s2    &	- 	
c         C` s   t  |  t  s t  t   | } xq t r | t   } | d k  rZ t d |  f   n  y t j |  d | } Wn t k
 r q% X| j	   Pq% Wd  S(   Ni    s   Failed to connect to %sRd   (
   R,   t   tupleRY   R   R]   RT   t   sockett   create_connectionR<  RD   (   R   Rd   R   t   sock(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   wait_for_porto  s    	
gMbP?c         C` sg   t    | } xS |    sb t |  t    | k r | d  k	 rH |   n  t j d | f  q q Wd  S(   Ns&   condition not reached until %s seconds(   R   R   R3   R   R   (   t	   predicateRd   R   t   periodR   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   wait_for  s    

c         c` sk   t    | } xW |    sf t j |  Vt    | k r | d  k	 rL |   n  t j d | f  q q Wd  S(   Ns&   condition not reached until %s seconds(   R   R
   R   R3   R   R   (   R  Rd   R   R  R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyR     s    
c          C` s   d }  } zl yO t j t j t j  }  |  j d  |  j d  t j |  j   d   } Wn t k
 rp t	 SXt
 SWd | d k	 r | j   n  |  d k	 r |  j   n  Xd S(   s   
    Return whether IPv6 is locally functional.  This doesn't guarantee IPv6
    is properly configured outside of localhost.
    s   ::i    i   i   N(   s   ::i    (   R3   R  t   AF_INET6t   SOCK_STREAMt   bindt   listenR  t   getsocknameR<  RE   R]   RD   (   t   servt   cli(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   has_ipv6  s    
 c         C` s   |  S(   N(    (   RH  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   requires_ipv6  s    s   ipv6 requiredc         c` s<   | d k r d } n  t |  d | d | V} | j   d S(   sh   
    Check that it is possible to connect to the distributed *addr*
    within the given *timeout*.
    g      ?Rd   R  N(   R3   R   t   abort(   R=  Rd   R  R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   assert_can_connect  s    	c      
   c` sR   | d k r d } n  t j |  ( t |  d | d | V} | j   Wd QXd S(   sj   
    Check that it is impossible to connect to the distributed *addr*
    within the given *timeout*.
    g      ?Rd   R  N(   R3   R   Rr  R   R  (   R=  Rd   R  t   exception_classR   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   assert_cannot_connect  s
    	t   tcpc         c` s   | | f } t  d | |  f |  t  d | t   |  f |  g } t   r | t  d | |  f |  t  d | t   |  f |  g 7} n  | Vd S(   sT   
    Check that the local *port* is reachable from all IPv4 and IPv6 addresses.
    s   %s://127.0.0.1:%ds
   %s://%s:%ds   %s://[::1]:%ds   %s://[%s]:%dN(   R  R"   R  R#   (   t   portRd   R  t   protocolR   t   futures(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt&   assert_can_connect_from_everywhere_4_6  s    "	&c         c` s   | | f } t  d | |  f |  t  d | t   |  f |  g } t   r | t d | |  f |  t d | t   |  f |  g 7} n  | Vd S(   sK   
    Check that the local *port* is reachable from all IPv4 addresses.
    s   %s://127.0.0.1:%ds
   %s://%s:%ds   %s://[::1]:%ds   %s://[%s]:%dN(   R  R"   R  R  R#   (   R  Rd   R  R  R   R  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt$   assert_can_connect_from_everywhere_4  s    "	&c         c` s   | | f } t  d |  |  g } t   d k rW | t d t   |  f |  g 7} n  t   r | t d |  |  t d t   |  f |  g 7} n  | Vd S(   sR   
    Check that the local *port* is only reachable from local IPv4 addresses.
    s   tcp://127.0.0.1:%ds	   127.0.0.1s   tcp://%s:%ds   tcp://[::1]:%ds   tcp://[%s]:%dN(   R  R"   R  R  R#   (   R  Rd   R  R   R  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   assert_can_connect_locally_4  s    &	#c         c` s|   t    s t  | | f } t d |  |  t d t   |  f |  t d |  |  t d t   |  f |  g } | Vd S(   sK   
    Check that the local *port* is reachable from all IPv6 addresses.
    s   tcp://127.0.0.1:%ds   tcp://%s:%ds   tcp://[::1]:%ds   tcp://[%s]:%dN(   R  RY   R  R"   R  R#   (   R  Rd   R  R   R  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt$   assert_can_connect_from_everywhere_6
  s    c         c` s   t    s t  | | f } t d |  |  t d t   |  f |  t d |  |  g } t   d k r | t d t   |  f |  g 7} n  | Vd S(   sR   
    Check that the local *port* is only reachable from local IPv6 addresses.
    s   tcp://127.0.0.1:%ds   tcp://%s:%ds   tcp://[::1]:%ds   ::1s   tcp://[%s]:%dN(   R  RY   R  R"   R  R#   (   R  Rd   R  R   R  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   assert_can_connect_locally_6  s    &c         c` s   t  |  t  r! t j |   }  n  |  j } |  j } | d k	 rU |  j } | |  _ n  t j	   } t j
 |  g |  j (|  j |  z	 | VWd | |  j (|  j |  | d k	 r | |  _ n  Xd S(   s*   Capture output from the given Logger.
    N(   R,   RW   R-   R#  R/   t   handlersR3   t	   propagatet   sixt   StringIOt   StreamHandlerR$  (   R2   R/   R  t
   orig_levelt   orig_handlerst   orig_propagatet   sio(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   captured_logger+  s     	
		
c         c` sM   t  |  t j  s t  |  j } t j   |  _ z |  j VWd | |  _ Xd S(   s9   Capture output from the given logging.StreamHandler.
    N(   R,   R-   R  RY   t   streamR  R  (   t   handlert   orig_stream(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   captured_handlerB  s    	c         c` s   d d l  m } t j  j  } | j   } zC | j   | j | j    t j  j | |   t |  d VWd | j   | j |  t |  Xd S(   s6   
    Temporarily change configuration dictionary.
    i   (   t   defaultsN(   R   R  R   R   RH   R   R   (   t
   new_configR  R   t   orig_config(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyR  O  s    

	
c         c` sM   t  j j   } t  j j |   z	 d  VWd  t  j j   t  j j |  Xd  S(   N(   R   R   R   R   RH   (   t   changest   saved_environ(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   new_environmentd  s    	c         c` s   d d l  } t j j d  } t j d d  \ } } zo t j | d   } | j | j |    Wd QX| t j d <z	 d VWd | r | t j d <n
 t j d =XWd t j	 |  Xd S(   sH   
    Temporarily change configuration file to match dictionary *c*.
    i    Nt   DASK_CONFIGR  s   dask-configRF   (
   t   yamlR   R   R   t   tempfilet   mkstempt   fdopenR;   t   dumpt   remove(   R  R  t   old_filet   fdR  t   f(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   new_config_fileo  s    	t   testsc         C` s7   t  j j t |   } t  j j |  s3 t |   | S(   s;   
    Get the path to one of the test TLS certificates.
    (   R   R  R:   t	   certs_dirt   existsRY   (   t   filenameR  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   get_cert  s    c          C` sZ   t  d  }  t  d  } i i |  d 6i | d 6d 6i | d 6d 6i | d 6d 6d 6} | S(	   s=   
    A functional TLS configuration with our test certs.
    s   tls-ca-cert.pems   tls-key-cert.pems   ca-filet   certR	  R   R   t   tls(   R  (   t   ca_filet   keycertR  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt
   tls_config  s    c          C` s   t    }  t |  d <|  S(   sg   
    A functional TLS configuration with our test certs, disallowing
    plain TCP communications.
    s   require-encryption(   R  R]   (   R  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   tls_only_config  s    	
c          C` s#   t  t     t   }  Wd QX|  S(   s:   
    A Security object with proper TLS configuration.
    N(   R  R  R   (   t   sec(    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   tls_security  s    c          C` s2   t  t     t   }  Wd QX|  j s. t  |  S(   sg   
    A Security object with proper TLS configuration and disallowing plain
    TCP communications.
    N(   R  R  R   t   require_encryptionRY   (   R  (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyR    s    s   tls-cert.pems   tls-key.pems   tls-ca-cert.pemc         C` sV   t  j t  j j d t |  } t | _ t  j | _ | j	 t |   t |   | S(   Nt   cafile(
   t   sslt   create_default_contextt   Purposet   CLIENT_AUTHR  RE   t   check_hostnamet   CERT_REQUIREDt   verify_modet   load_cert_chain(   t   certfilet   keyfileR  Ro   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   get_server_ssl_context  s
    !	c         C` sV   t  j t  j j d t |  } t | _ t  j | _ | j	 t |   t |   | S(   NR  (
   R  R  R  t   SERVER_AUTHR  RE   R  R  R  R  (   R  R  R  Ro   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   get_client_ssl_context  s
    !	c         C` s   t  j d  } yG | j |   \ } } | | k  rU | j |  | t | |  f  n  Wn* t k
 r } t  j d | | f  n Xd  S(   Nt   resources.   rlimit too low (%s) and can't be increased: %s(   R   t   importorskipt	   getrlimitt	   setrlimitt   maxR   RD  (   t   limitt   desiredR  t   softt   hardRa   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   bump_rlimit  s    &c          K` s/   |  j  d d d g  t d d d t   |   S(	   NR  s   tls://127.0.0.1i   i   R   R  (   s   tls://127.0.0.1i   (   s   tls://127.0.0.1i   (   t
   setdefaultRp  R  (   R   (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   gen_tls_cluster  s    (   i   i   (   s	   127.0.0.1i   (   s	   127.0.0.1i   (   t
   __future__R    R   R   t   collectionst
   contextlibR   R   t   datetimeR   R   R   R   t	   itertoolsR-   t   logging.configR   RZ   RU   R1  Rx  R  R  R   R  R   RP   R   R   R&  R  R  t   ImportErrorR3   R   R  R   t   toolzR   R   R	   t   tornadoR
   R   t   tornado.genR   t   tornado.ioloopR   R	  R   R   R   t   compatibilityR   R   R   R   R   R   t
   comm.utilsR   R   R   t   coreR   R   R   t   metricst   processR   t	   proctitleR   R  R   t   utilsR   R    R!   R"   R#   R$   R%   R&   R'   R(   R   R)   R*   R+   t
   dask.arrayR#  R   R2   t   roott   managert
   loggerDictR   R"  R^  t   fixtureR>   R@   RA   R   RG   R`   Ri   Rp   RO   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   defaultdictt   intR   t   countR   t   objectR   R   R   R   R]   R   R{  t   asyncincRY   R   R   R   R   R   R  R  R  R  R  R
  R  R  R  RE   R  R  R  RF  RJ  R   R   RQ  RS  Rp  R   Rr  R}  R  R  R  R   R  R  RC  RD  R  R<  R  R  R  R  R  R  t   INFOR  R  R  R  R  R  t   abspathR:   t   dirnamet   __file__R  R  R  R  R  R  R  R  R  R  (    (    (    s5   lib/python2.7/site-packages/distributed/utils_test.pyt   <module>   sp  
"F1				
											
			 				!		%

	-				
			
		
