o
    Qœ_ó  ã                   @   s�   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	m
Z
mZ d dlmZmZ G dd„ deƒZG d	d
„ d
ejƒZG dd„ dejƒZdS )é    N)ÚThreadPoolExecutor)Úgen)ÚIOLoop)ÚAsyncIOLoopÚto_asyncio_futureÚAnyThreadEventLoopPolicy)ÚAsyncTestCaseÚgen_testc                   @   s<   e Zd Zdd„ Zdd„ Zedd„ ƒZedd„ ƒZd	d
„ ZdS )ÚAsyncIOLoopTestc                 C   s
   t ƒ }|S ©N)r   )ÚselfÚio_loop© r   ú;/usr/lib/python3/dist-packages/tornado/test/asyncio_test.pyÚget_new_ioloop   s   zAsyncIOLoopTest.get_new_ioloopc                 C   s   t  ¡  | j¡ |  ¡  d S r   )ÚasyncioÚget_event_loopÚ	call_soonÚstopÚwait©r   r   r   r   Útest_asyncio_callback    s   z%AsyncIOLoopTest.test_asyncio_callbackc                 c   sH   � t tdƒr
tj}nttdƒ}|t ¡  d dd„ ¡ƒV }|  |d¡ d S )NÚensure_futureÚasyncc                   S   ó   dS ©Né*   r   r   r   r   r   Ú<lambda>1   ó    z5AsyncIOLoopTest.test_asyncio_future.<locals>.<lambda>r   )Úhasattrr   r   Úgetattrr   Úrun_in_executorÚassertEqual)r   r   Úxr   r   r   Útest_asyncio_future%   s   €

ÿz#AsyncIOLoopTest.test_asyncio_futurec                 c   s(   � t jdd„ ƒ}|ƒ V }|  |d¡ d S )Nc                  s   s$   � t  ¡ } |  d dd„ ¡E d H }|S )Nc                   S   r   r   r   r   r   r   r   r   :   r   zDAsyncIOLoopTest.test_asyncio_yield_from.<locals>.f.<locals>.<lambda>)r   r   r!   )Ú
event_loopr#   r   r   r   Úf7   s   €z2AsyncIOLoopTest.test_asyncio_yield_from.<locals>.fr   )r   Ú	coroutiner"   )r   r&   Úresultr   r   r   Útest_asyncio_yield_from5   s
   €
z'AsyncIOLoopTest.test_asyncio_yield_fromc                    sº   t jdd„ ƒ‰‡fdd„‰ ‡fdd„}‡ fdd„}|  | j ˆ ¡d	¡ |  | j |¡d	¡ |  | j |¡d	¡ |  t ¡  ˆ ƒ ¡d	¡ |  t ¡  |ƒ ¡d	¡ |  t ¡  |ƒ ¡d	¡ d S )
Nc                   s   s   � t jV  t  d¡‚r   )r   ÚmomentÚReturnr   r   r   r   Útornado_coroutineE   s   €
z?AsyncIOLoopTest.test_asyncio_adapter.<locals>.tornado_coroutinec                   “   s   �ˆ ƒ I d H S r   r   r   ©r,   r   r   Ú native_coroutine_without_adapterJ   s   €zNAsyncIOLoopTest.test_asyncio_adapter.<locals>.native_coroutine_without_adapterc                   “   ó   �t ˆ ƒ ƒI d H S r   ©r   r   r-   r   r   Únative_coroutine_with_adapterM   ó   €zKAsyncIOLoopTest.test_asyncio_adapter.<locals>.native_coroutine_with_adapterc                   “   r/   r   r0   r   )r.   r   r   Únative_coroutine_with_adapter2Q   r2   zLAsyncIOLoopTest.test_asyncio_adapter.<locals>.native_coroutine_with_adapter2r   )r   r'   r"   r   Úrun_syncr   r   Úrun_until_complete)r   r1   r3   r   )r.   r,   r   Útest_asyncio_adapter@   s4   
ÿüÿüÿüz$AsyncIOLoopTest.test_asyncio_adapterN)	Ú__name__Ú
__module__Ú__qualname__r   r   r	   r$   r)   r6   r   r   r   r   r
      s    


r
   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚLeakTestc                 C   s&   t ƒ  ¡  t ¡ | _t t ¡ ¡ d S r   )r   Úcloser   Úget_event_loop_policyÚorig_policyÚset_event_loop_policyÚDefaultEventLoopPolicyr   r   r   r   ÚsetUpp   s   

zLeakTest.setUpc                 C   s   t  ¡  ¡  t  | j¡ d S r   )r   r   r;   r>   r=   r   r   r   r   ÚtearDownx   s   zLeakTest.tearDownc                 C   sD   t tjƒ}tdƒD ]	}tƒ }| ¡  q	t tjƒ| }|  |d¡ d S )Né
   r   )Úlenr   Ú_ioloop_for_asyncioÚranger   r;   r"   ©r   Ú
orig_countÚiÚloopÚ	new_countr   r   r   Útest_ioloop_close_leak|   s   

zLeakTest.test_ioloop_close_leakc                 C   sf   t tjƒ}tdƒD ]}t ¡ }| tj¡ | |j¡ | 	¡  | 
¡  q	t tjƒ| }|  |d¡ d S )NrB   é   )rC   r   rD   rE   r   Únew_event_loopr   Úcurrentr   Úrun_foreverr;   r"   rF   r   r   r   Útest_asyncio_close_leak…   s   

z LeakTest.test_asyncio_close_leakN)r7   r8   r9   r@   rA   rK   rP   r   r   r   r   r:   o   s
    	r:   c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚAnyThreadEventLoopPolicyTestc                 C   s   t  ¡ | _tdƒ| _d S )NrL   )r   r<   r=   r   Úexecutorr   r   r   r   r@   •   s   
z"AnyThreadEventLoopPolicyTest.setUpc                 C   s   t  | j¡ | j ¡  d S r   )r   r>   r=   rR   Úshutdownr   r   r   r   rA   ™   s   z%AnyThreadEventLoopPolicyTest.tearDownc                 C   s   dd„ }| j  |¡}| ¡ S )Nc                  S   s   t  ¡ } |  ¡  | S )a#  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   r;   )rI   r   r   r   Úget_and_close_event_loopž   s   zWAnyThreadEventLoopPolicyTest.get_event_loop_on_thread.<locals>.get_and_close_event_loop)rR   Úsubmitr(   )r   rU   Úfuturer   r   r   Úget_event_loop_on_thread�   s   z5AnyThreadEventLoopPolicyTest.get_event_loop_on_threadc                 C   sV   |   ttf| j |¡j¡ t tƒ ¡ |  	| j |¡ ¡ |¡ | j dd„ ¡ ¡  d S )Nc                   S   s   t  ¡  ¡ S r   rT   r   r   r   r   r   ¸   s    z>AnyThreadEventLoopPolicyTest.run_policy_test.<locals>.<lambda>)
ÚassertRaisesÚRuntimeErrorÚAssertionErrorrR   rV   r(   r   r>   r   ÚassertIsInstance)r   ÚaccessorÚexpected_typer   r   r   Úrun_policy_test­   s   ÿz,AnyThreadEventLoopPolicyTest.run_policy_testc                 C   s   |   tjtj¡ d S r   )r_   r   r   ÚAbstractEventLoopr   r   r   r   Útest_asyncio_accessorº   s   z2AnyThreadEventLoopPolicyTest.test_asyncio_accessorc                 C   s   |   tjt¡ d S r   )r_   r   rN   r   r   r   r   Útest_tornado_accessor½   s   z2AnyThreadEventLoopPolicyTest.test_tornado_accessorN)	r7   r8   r9   r@   rA   rX   r_   ra   rb   r   r   r   r   rQ   ”   s    rQ   )r   ÚunittestÚconcurrent.futuresr   Útornador   Útornado.ioloopr   Útornado.platform.asyncior   r   r   Útornado.testingr   r	   r
   ÚTestCaser:   rQ   r   r   r   r   Ú<module>   s   T%