a
    ze                     @   s   d dl Z d dlZd dlZd dlmZ d dlZd dlmZ dd ZG dd dZ	G dd	 d	ej
ZG d
d deZG dd deZG dd deZedkre  dS )    N)mock)utilsc                   C   s   t d  d S N)asyncioZset_event_loop_policy r   r   3/usr/lib/python3.9/test/test_asyncio/test_pep492.pytearDownModule   s    r   c                   @   s.   e Zd Zdd Zd
ddZdd Zdd	 ZdS )FakeCoroc                 C   s   d S r   r   )selfvaluer   r   r   send   s    zFakeCoro.sendNc                 C   s   d S r   r   )r
   typvaltbr   r   r   throw   s    zFakeCoro.throwc                 C   s   d S r   r   r
   r   r   r   close   s    zFakeCoro.closec                 c   s
   d V  d S r   r   r   r   r   r   	__await__   s    zFakeCoro.__await__)NN)__name__
__module____qualname__r   r   r   r   r   r   r   r   r	      s   
r	   c                       s   e Zd Z fddZ  ZS )BaseTestc                    sH   t    t | _t | j_t | j_d| jjj	_
| | j d S )Nr   )supersetUpr   ZBaseEventLooploopr   ZMockZ_process_events	_selectorselectZreturn_valueZset_event_loopr   	__class__r   r   r   "   s    

zBaseTest.setUp)r   r   r   r   __classcell__r   r   r   r   r       s   r   c                   @   s   e Zd Zdd Zdd ZdS )	LockTestsc                    s     tD tj jdtj jdtj jdtj jdg}W d    n1 sT0    Y   fdd}|D ]"} j||  	|
  qnd S )Nr   c              	      s   t dI d H   |   | 4 I d H N} |d   |   t dI d H   |   W d   I d H  q1 I d H s0    Y   |   d S )N{Gz?)r   sleepassertFalselockedZassertIs
assertTrue)lock_lockr   r   r   test6   s    8z7LockTests.test_context_manager_async_with.<locals>.testassertWarnsDeprecationWarningr   Lockr   	Condition	SemaphoreBoundedSemaphorerun_until_completer$   r%   r
   Z
primitivesr)   Z	primitiver   r   r   test_context_manager_async_with-   s    "
z)LockTests.test_context_manager_async_withc                    s     tD tj jdtj jdtj jdtj jdg}W d    n1 sT0    Y   fdd}|D ]"} j||  	|
  qnd S )Nr!   c              	      sx   t dI d H   |    td: | I d H  W d    n1 sL0    Y  W d    n1 sj0    Y  d S )Nr"   z#can't be used in 'await' expression)r   r#   r$   r%   ZassertRaisesRegex	TypeError)r'   r   r   r   r)   M   s    z7LockTests.test_context_manager_with_await.<locals>.testr*   r2   r   r   r   test_context_manager_with_awaitD   s    "
z)LockTests.test_context_manager_with_awaitN)r   r   r   r3   r5   r   r   r   r   r    +   s   r    c                   @   s   e Zd Zdd ZdS )StreamReaderTestsc                    sR   d}t j| jd  |     fdd}| j| }| |g d d S )Ns   line1
line2
line3r!   c                     s&   g }  2 z3 d H W }|  | q6 | S r   )append)datalinestreamr   r   readere   s    z/StreamReaderTests.test_readline.<locals>.reader)s   line1
s   line2
s   line3)r   StreamReaderr   Z	feed_dataZfeed_eofr1   assertEqual)r
   ZDATAr<   r8   r   r:   r   test_readline^   s    
zStreamReaderTests.test_readlineN)r   r   r   r?   r   r   r   r   r6   \   s   r6   c                   @   sL   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )CoroutineTestsc                 C   sL   dd }| }z|  t| W |  n
|  0 |  tt  d S )Nc                      s   d S r   r   r   r   r   r   foor       z,CoroutineTests.test_iscoroutine.<locals>.foo)r&   r   Ziscoroutiner   r	   )r
   rA   fr   r   r   test_iscoroutineq   s    zCoroutineTests.test_iscoroutinec                 C   s   dd }|  t| d S )Nc                      s   d S r   r   r   r   r   r   rA   }   rB   z4CoroutineTests.test_iscoroutinefunction.<locals>.foo)r&   r   Ziscoroutinefunction)r
   rA   r   r   r   test_iscoroutinefunction|   s    z'CoroutineTests.test_iscoroutinefunctionc                    sn   G dd d |  t" tj fdd}W d    n1 s@0    Y  | }| |d d |  d S )Nc                   @   s   e Zd Zdd ZdS )zCCoroutineTests.test_function_returning_awaitable.<locals>.Awaitablec                 S   s   dS )N)spamr   r   r   r   r   r      s    zMCoroutineTests.test_function_returning_awaitable.<locals>.Awaitable.__await__N)r   r   r   r   r   r   r   r   	Awaitable   s   rG   c                      s     S r   r   r   rG   r   r   func   s    z>CoroutineTests.test_function_returning_awaitable.<locals>.funcrF   )r+   r,   r   	coroutiner>   r   r   )r
   rI   coror   rH   r   !test_function_returning_awaitable   s    ,z0CoroutineTests.test_function_returning_awaitablec                    sX   dd   fdd}| j | }| |d | j d | j | }| |d d S )Nc                      s   dS )NrF   r   r   r   r   r   bar   s    z5CoroutineTests.test_async_def_coroutines.<locals>.barc                      s     I d H S r   r   r   rM   r   r   rA      s    z5CoroutineTests.test_async_def_coroutines.<locals>.foorF   T)r   r1   r>   	set_debug)r
   rA   r8   r   rN   r   test_async_def_coroutines   s    z(CoroutineTests.test_async_def_coroutinesc                    sJ    fdd}  t d  jd  j|    t d d S )Nc                      s     t dk d S Nr   )r&   sys#get_coroutine_origin_tracking_depthr   r   r   r   start   s    zOCoroutineTests.test_debug_mode_manages_coroutine_origin_tracking.<locals>.startr   T)r>   rR   rS   r   rO   r1   )r
   rT   r   r   r   1test_debug_mode_manages_coroutine_origin_tracking   s
    z@CoroutineTests.test_debug_mode_manages_coroutine_origin_trackingc                    sF   dd t jfdd  fdd}j| }|d d S )Nc                   s   s   dE d H  dS )Nr   rF   r   r   r   r   r   gen   s    
z0CoroutineTests.test_types_coroutine.<locals>.genc                      s     S r   r   r   )rV   r   r   rI      s    z1CoroutineTests.test_types_coroutine.<locals>.funcc                     s     }  | tj | I d H S r   )ZassertIsInstancetypes_GeneratorWrapper)wrapper)rI   r
   r   r   rK      s    z1CoroutineTests.test_types_coroutine.<locals>.cororF   )rW   rJ   r   r1   r>   )r
   rK   r8   r   )rI   rV   r
   r   test_types_coroutine   s    z#CoroutineTests.test_types_coroutinec                    s4   d   fdd fdd}j |  d S )Nc                     s4    j dd} z| d jjd W d } nd } 0 d S )N   )limitr   rA   )Z	get_stackr>   f_codeco_name)rC   )Tr
   r   r   rA      s    z1CoroutineTests.test_task_print_stack.<locals>.fooc                      s    t j jd  I d H  d S )Nr!   )r   Zensure_futurer   r   r_   rA   r
   r   r   runner   s    z4CoroutineTests.test_task_print_stack.<locals>.runner)r   r1   r
   ra   r   r`   r   test_task_print_stack   s    z$CoroutineTests.test_task_print_stackc                    sb   dd   fdd}j d jtdd j |  W d    n1 sT0    Y  d S )Nc                      s   t dI d H  d S )Ng?)r   r#   r   r   r   r   afunc   s    z/CoroutineTests.test_double_await.<locals>.afuncc                     sH     } j | }z&tdI d H  | I d H  W |  n
|  0 d S rQ   )r   Zcreate_taskr   r#   cancel)rK   trd   r
   r   r   ra      s    z0CoroutineTests.test_double_await.<locals>.runnerTz"coroutine is being awaited already)msg)r   rO   ZassertRaisesRuntimeErrorr1   rb   r   rg   r   test_double_await   s    	z CoroutineTests.test_double_awaitN)r   r   r   rD   rE   rL   rP   rU   rZ   rc   rj   r   r   r   r   r@   o   s   	r@   __main__)rR   rW   Zunittestr   r   Ztest.test_asyncior   Z
test_utilsr   r	   ZTestCaser   r    r6   r@   r   mainr   r   r   r   <module>   s   1n