๓
จ[c           @` sf  d  d l  m Z m Z m Z d  d l m Z d  d l m Z d  d l m	 Z	 d  d l
 m Z m Z d  d l m Z m Z m Z m Z y d  d l m Z Wn e k
 rฑ d Z n! Xd  d l m Z m Z m Z e e j e d k d	  d
 e f d     Y Z e j e d k d	  d e j f d     Y Z e j e d k d	  d e j f d     Y Z d S(   i    (   t   absolute_importt   divisiont   print_function(   t   ThreadPoolExecutor(   t   gen(   t   IOLoop(   t   AsyncTestCaset   gen_test(   t   unittestt   skipBefore33t   skipBefore35t	   exec_test(   t   asyncio(   t   AsyncIOLoopt   to_asyncio_futuret   AnyThreadEventLoopPolicys   asyncio module not presentt   AsyncIOLoopTestc           B` sM   e  Z d    Z d   Z e d    Z e e d     Z e d    Z	 RS(   c         C` s   t    } | S(   N(   R   (   t   selft   io_loop(    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   get_new_ioloop!   s    	c         C` s$   t  j   j |  j  |  j   d  S(   N(   R   t   get_event_loopt	   call_soont   stopt   wait(   R   (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   test_asyncio_callback%   s    c         c` s`   t  t d  r t j } n t t d  } | t j   j d  d     V} |  j | d  d  S(   Nt   ensure_futuret   asyncc           S` s   d S(   Ni*   (    (    (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   <lambda>6   s    i*   (   t   hasattrR   R   t   getattrR   t   run_in_executort   Nonet   assertEqual(   R   R   t   x(    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   test_asyncio_future*   s    c         c` s:   t  t   t   d  } | d   V} |  j | d  d  S(   Nsภ   
        @gen.coroutine
        def f():
            event_loop = asyncio.get_event_loop()
            x = yield from event_loop.run_in_executor(None, lambda: 42)
            return x
        t   fi*   (   R   t   globalst   localsR    (   R   t	   namespacet   result(    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   test_asyncio_yield_from9   s    	c         C` s$  t  j d    } t t   t   d  d } t t   t   d  d } t t   t   d  d } |  j |  j j |  d  |  j |  j j |  d  |  j |  j j |  d  |  j t j	   j
 |    d  |  j t j	   j
 |    d  |  j t j	   j
 |    d  d  S(	   Nc           s` s   t  j Vt  j d   d  S(   Ni*   (   R   t   momentt   Return(    (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   tornado_coroutineN   s    sl   
        async def native_coroutine_without_adapter():
            return await tornado_coroutine()
        t    native_coroutine_without_adapters|   
        async def native_coroutine_with_adapter():
            return await to_asyncio_future(tornado_coroutine())
        t   native_coroutine_with_adapters   
        async def native_coroutine_with_adapter2():
            return await to_asyncio_future(native_coroutine_without_adapter())
        t   native_coroutine_with_adapter2i*   (   R   t	   coroutineR   R$   R%   R    R   t   run_syncR   R   t   run_until_complete(   R   R+   R,   R-   R.   (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   test_asyncio_adapterH   s8    			(
   t   __name__t
   __module__R   R   R   R"   R	   R(   R
   R2   (    (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyR      s   		t   LeakTestc           B` s,   e  Z d    Z d   Z d   Z d   Z RS(   c         C` s3   t    j   t j   |  _ t j t j    d  S(   N(   R   t   closeR   t   get_event_loop_policyt   orig_policyt   set_event_loop_policyt   DefaultEventLoopPolicy(   R   (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   setUp   s    c         C` s$   t  j   j   t  j |  j  d  S(   N(   R   R   R6   R9   R8   (   R   (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   tearDown   s    c         C` s`   t  t j  } x' t d  D] } t   } | j   q Wt  t j  | } |  j | d  d  S(   Ni
   i    (   t   lenR   t   _ioloop_for_asynciot   rangeR   R6   R    (   R   t
   orig_countt   it   loopt	   new_count(    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   test_ioloop_close_leak   s    	c         C` s   t  t j  } xT t d  D]F } t j   } | j t j  | j | j  | j	   | j
   q Wt  t j  | } |  j | d  d  S(   Ni
   i   (   R=   R   R>   R?   R   t   new_event_loopR   t   currentR   t   run_foreverR6   R    (   R   R@   RA   RB   RC   (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   test_asyncio_close_leak   s    
(   R3   R4   R;   R<   RD   RH   (    (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyR5   }   s   				t   AnyThreadEventLoopPolicyTestc           B` s>   e  Z d    Z d   Z d   Z d   Z d   Z d   Z RS(   c         C` s"   t  j   |  _ t d  |  _ d  S(   Ni   (   R   R7   R8   R   t   executor(   R   (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyR;   ฅ   s    c         C` s!   t  j |  j  |  j j   d  S(   N(   R   R9   R8   RJ   t   shutdown(   R   (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyR<   ฉ   s    c         C` s%   d   } |  j  j |  } | j   S(   Nc          S` s   t  j   }  |  j   |  S(   s#  Get the event loop. Close it if one is returned.

            Returns the (closed) event loop. This is a silly thing
            to do and leaves the thread in a broken state, but it's
            enough for this test. Closing the loop avoids resource
            leak warnings.
            (   R   R   R6   (   RB   (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   get_and_close_event_loopฎ   s    
(   RJ   t   submitR'   (   R   RL   t   future(    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   get_event_loop_on_threadญ   s    	c         C` st   |  j  t t f |  j j |  j  t j t    |  j	 |  j j |  j   |  |  j j d    j   d  S(   Nc           S` s   t  j   j   S(   N(   R   R   R6   (    (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyR   ศ   s    (
   t   assertRaisest   RuntimeErrort   AssertionErrorRJ   RM   R'   R   R9   R   t   assertIsInstance(   R   t   accessort   expected_type(    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   run_policy_testผ   s    c         C` s   |  j  t j t j  d  S(   N(   RV   R   R   t   AbstractEventLoop(   R   (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   test_asyncio_accessorส   s    c         C` s   |  j  t j t  d  S(   N(   RV   R   RF   (   R   (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   test_tornado_accessorอ   s    (   R3   R4   R;   R<   RO   RV   RX   RY   (    (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyRI   ฃ   s   					N(   t
   __future__R    R   R   t   concurrent.futuresR   t   tornadoR   t   tornado.ioloopR   t   tornado.testingR   R   t   tornado.test.utilR   R	   R
   R   t   tornado.platform.asyncioR   t   ImportErrorR   R   R   R   t   skipIfR   t   TestCaseR5   RI   (    (    (    s8   lib/python2.7/site-packages/tornado/test/asyncio_test.pyt   <module>   s"   "
]%