a
    ze                    @   s  d 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
m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 Zdd Zejdd ZdSddZ dd Z!G dd dZ"G dd dZ#G dd dZ$dd Z%G dd dZ&e'e(ed ode(ed!d"G d#d$ d$e$e&ej)Z*e'e(ed oe(ed!d"e%G d%d& d&e$ej)Z+e'e(ed!d"e%G d'd( d(e$ej)Z,e'e(ed d"e%G d)d* d*e$ej)Z-e'e(ed!d"G d+d, d,e$ej)Z.e'e(ed d"G d-d. d.e$ej)Z/G d/d0 d0e$e&ej)Z0e%G d1d2 d2e$ej)Z1e'e(ed!d"G d3d4 d4ej)Z2G d5d6 d6Z3G d7d8 d8ej)e3Z4e'e(ed9d"G d:d; d;ej)e3Z5G d<d= d=Z6G d>d? d?e6ej)Z7e'e(ed!d"G d@dA dAe6ej)Z8G dBdC dCej)Z9G dDdE dEZ:G dFdG dGe:ej)Z;G dHdI dIe:ej)Z<G dJdK dKej)Z=G dLdM dMej)Z>G dNdO dOej)Z?G dPdQ dQej)Z@eAdRkreB  dS )TzTests for tasks.py.    N)mock)GenericAlias)
coroutines)futures)tasks)utils)support)assert_python_okc                   C   s   t d  d S N)asyncioZset_event_loop_policy r   r   2/usr/lib/python3.9/test/test_asyncio/test_tasks.pytearDownModule   s    r   c                      s   d S r
   r   r   r   r   r   coroutine_function!   s    r   c                 c   s0   t j}|j}z| |_d V  W ||_n||_0 d S r
   )r   r   _DEBUG)Zenabledr   Z	old_debugr   r   r   set_coroutine_debug%   s    r   Fc                 C   sR   |rd| }nd| }|d ur@|d }d| |||d |d f S d| ||f S d S )Nz%sz%s, definedz%coro=<%s() %s at %s> created at %s:%sr      zcoro=<%s() %s at %s>r   )qualnamestatesrcsource_traceback	generatorframer   r   r   format_coroutine1   s    
r   c                 C   s2   d}| j }|du rq"|} |d7 }qt| | j|fS )zP
    Return information about the innermost exception context in the chain.
    r   Nr   )__context__typeargs)excdepthcontextr   r   r   get_innermost_context>   s    
r!   c                   @   s   e Zd Zdd Zdd ZdS )Dummyc                 C   s   dS )Nz<Dummy>r   selfr   r   r   __repr__P   s    zDummy.__repr__c                 G   s   d S r
   r   )r$   r   r   r   r   __call__S   s    zDummy.__call__N)__name__
__module____qualname__r%   r&   r   r   r   r   r"   N   s   r"   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
CoroLikeObjectc                 C   s   t dd S N*   )StopIteration)r$   vr   r   r   sendX   s    zCoroLikeObject.sendc                 G   s   d S r
   r   )r$   r   r   r   r   throw[   s    zCoroLikeObject.throwc                 C   s   d S r
   r   r#   r   r   r   close^   s    zCoroLikeObject.closec                 C   s   | S r
   r   r#   r   r   r   	__await__a   s    zCoroLikeObject.__await__N)r'   r(   r)   r/   r0   r1   r2   r   r   r   r   r*   W   s   r*   c                       s  e Zd ZdZdZdddZdd Z f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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)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Z d;d< Z!d=d> Z"d?d@ Z#dAdB Z$dCdD Z%dEdF Z&dGdH Z'dIdJ Z(dKdL Z)dMdN Z*dOdP Z+dQdR Z,dSdT Z-dUdV Z.dWdX Z/dYdZ Z0d[d\ Z1d]d^ Z2d_d` Z3dadb Z4dcdd Z5dedf Z6dgdh Z7didj Z8dkdl Z9dmdn Z:dodp Z;dqdr Z<dsdt Z=dudv Z>dwdx Z?dydz Z@d{d| ZAd}d~ ZBdd ZCdd ZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdd ZSdd ZTdd ZUdd ZVdd ZWdd ZXdd ZYdd ZZdd Z[dd Z\dd Z]dd Z^dd Z_dd Z`dd Zadd Zbecdddd Zeecdd¡ddĄ ZfddƄ ZgddȄ Zhddʄ Zidd̄ Zjdd΄ ZkecddddЄ Zldd҄ ZmddԄ Znddք Zodd؄ Zpddڄ Zqdd܄ Zrddބ Zsdd Ztdd Zudd Zv  ZwS )BaseTaskTestsNTestTaskc                 C   s   | j j|||dS )N)loopname)	__class__Task)r$   r5   coror6   r   r   r   new_taskj   s    zBaseTaskTests.new_taskc                 C   s   | j j|dS Nr5   )r7   Futurer$   r5   r   r   r   
new_futurem   s    zBaseTaskTests.new_futurec                    s6   t       _ j j  fdd j_d S )Nc                      s      jS r
   )r?   r5   r   r#   r   r   <lambda>t       z%BaseTaskTests.setUp.<locals>.<lambda>)supersetUpnew_test_loopr5   set_task_factoryr:   create_futurer#   r7   r#   r   rC   p   s    

zBaseTaskTests.setUpc                 C   s,   | j jt }| |jtf | |t d S r
   )r7   r8   strassertEqual__args__assertIsInstancer   r$   taskr   r   r   test_generic_aliasv   s    z BaseTaskTests.test_generic_aliasc                 C   s   dd }|  | j| }| t|d | |jd  |d | |jd | tj	 | j
| W d    n1 s|0    Y  d S )Nc                      s   d S r
   r   r   r   r   r   r9   |   s    z;BaseTaskTests.test_task_cancel_message_getter.<locals>.coro_cancel_message
my message)r:   r5   
assertTruehasattrrI   rO   cancelassertRaisesr   CancelledErrorrun_until_completer$   r9   tr   r   r   test_task_cancel_message_getter{   s    
z-BaseTaskTests.test_task_cancel_message_getterc                 C   sr   dd }|  | j| }|d d|_| |jd | tj | j| W d    n1 sd0    Y  d S )Nc                      s   d S r
   r   r   r   r   r   r9      s    z;BaseTaskTests.test_task_cancel_message_setter.<locals>.cororP   zmy new message)	r:   r5   rS   rO   rI   rT   r   rU   rV   rW   r   r   r   test_task_cancel_message_setter   s    
z-BaseTaskTests.test_task_cancel_message_setterc                    sD   G dd d  fddj tjfddtdD   d S )Nc                   @   s   e Zd Zdd ZdS )z1BaseTaskTests.test_task_del_collect.<locals>.Evilc                 S   s   t   d S r
   )gccollectr#   r   r   r   __del__   s    z9BaseTaskTests.test_task_del_collect.<locals>.Evil.__del__N)r'   r(   r)   r]   r   r   r   r   Evil   s   r^   c                      s     S r
   r   r   )r^   r   r   run   s    z0BaseTaskTests.test_task_del_collect.<locals>.runc                    s   g | ]} j  qS r   )r:   r5   ).0_)r_   r$   r   r   
<listcomp>   s   z7BaseTaskTests.test_task_del_collect.<locals>.<listcomp>d   )r5   rV   r   gatherranger#   r   )r^   r_   r$   r   test_task_del_collect   s    z#BaseTaskTests.test_task_del_collectc                 C   sr   t  }| |}dd }zH| td  | j|| W d    n1 sN0    Y  W |  n
|  0 d S )Nc                    s   | I d H  d S r
   r   futr   r   r   r_      s    z1BaseTaskTests.test_other_loop_future.<locals>.runzTask .* got Future .* attached)r   new_event_loopr?   assertRaisesRegexRuntimeErrorr5   rV   r1   )r$   
other_looprh   r_   r   r   r   test_other_loop_future   s    
0z$BaseTaskTests.test_other_loop_futurec                    sZ    fdd}t j| | jd | td | j  W d    n1 sL0    Y  d S )Nc                      s    I d H  d S r
   r   r   rM   r   r   test   s    z6BaseTaskTests.test_task_awaits_on_itself.<locals>.testr<   zTask cannot await on itself)r   ensure_futurer5   rj   rk   rV   )r$   ro   r   rn   r   test_task_awaits_on_itself   s    z(BaseTaskTests.test_task_awaits_on_itselfc                 C   s   dd }|  | j| }| j| | |  | | d | |j| j | |	 | j t
 }| | |  || }| |j| || |  d S )Nc                      s   dS Nokr   r   r   r   r   notmuch   s    z.BaseTaskTests.test_task_class.<locals>.notmuchrs   )r:   r5   rV   rQ   donerI   resultassertIs_loopget_loopr   ri   set_event_loopr1   r$   rt   rX   r5   r   r   r   test_task_class   s    

zBaseTaskTests.test_task_classc                 C   s   |  t tjdd }W d    n1 s.0    Y  tj| | jd}| j| | |  | 	|
 d | |j| j t }| | tj| |d}| |j| || |  d S )Nc                   S   s   dS rr   r   r   r   r   r   rt      s    z;BaseTaskTests.test_ensure_future_coroutine.<locals>.notmuchr<   rs   )assertWarnsDeprecationWarningr   	coroutinerp   r5   rV   rQ   ru   rI   rv   rw   rx   ri   rz   r1   r{   r   r   r   test_ensure_future_coroutine   s    (

z*BaseTaskTests.test_ensure_future_coroutinec                 C   s   |  | j}|d t|}| j| | |  | |	 d | 
|| t }| | | t tj||d}W d    n1 s0    Y  |  tj|| jd}| 
|| d S )Nkor<   )r?   r5   
set_resultr   rp   rV   rQ   ru   rI   rv   rw   ri   rz   rT   
ValueErrorr1   )r$   Zf_origfr5   r   r   r   test_ensure_future_future   s    


,z'BaseTaskTests.test_ensure_future_futurec                 C   s   dd }|  | j| }t|}| j| | |  | | d | 	|| t
 }| | | t tj||d}W d    n1 s0    Y  |  tj|| jd}| 	|| d S )Nc                      s   dS rr   r   r   r   r   r   rt      s    z6BaseTaskTests.test_ensure_future_task.<locals>.notmuchrs   r<   )r:   r5   r   rp   rV   rQ   ru   rI   rv   rw   ri   rz   rT   r   r1   )r$   rt   Zt_origrX   r5   r   r   r   test_ensure_future_task   s    

,z%BaseTaskTests.test_ensure_future_taskc                 C   sz   G dd d}|  t tjdd }W d    n1 s<0    Y  t }| | tj|| |d}|| d S )Nc                   @   s   e Zd Zdd Zdd ZdS )z6BaseTaskTests.test_ensure_future_awaitable.<locals>.Awc                 S   s
   || _ d S r
   r9   r$   r9   r   r   r   __init__  s    z?BaseTaskTests.test_ensure_future_awaitable.<locals>.Aw.__init__c                 s   s   | j E d H S r
   r   r#   r   r   r   r2   
  s    z@BaseTaskTests.test_ensure_future_awaitable.<locals>.Aw.__await__N)r'   r(   r)   r   r2   r   r   r   r   Aw  s   r   c                   S   s   dS rr   r   r   r   r   r   r9     s    z8BaseTaskTests.test_ensure_future_awaitable.<locals>.coror<   )r}   r~   r   r   ri   rz   rp   rV   )r$   r   r9   r5   rh   r   r   r   test_ensure_future_awaitable  s    (

z*BaseTaskTests.test_ensure_future_awaitablec                 C   s8   |  t td W d    n1 s*0    Y  d S rr   )rT   	TypeErrorr   rp   r#   r   r   r   test_ensure_future_neither  s    z(BaseTaskTests.test_ensure_future_neitherc                 C   sZ   t  }| | j}| td t j||d W d    n1 sD0    Y  |  d S )NzRThe future belongs to a different loop than the one specified as the loop argumentr<   )r   ri   r?   r5   rj   r   rp   r1   )r$   r5   r   r   r   r   test_ensure_future_error_msg  s
    ,z*BaseTaskTests.test_ensure_future_error_msgc                    s@   d  fdd fdd fdd}j |  d S )Nc                      s     I d H  d S r
   r   r   )barr   r   foo(  s    z)BaseTaskTests.test_get_stack.<locals>.fooc                     sh    j dd} z| d jjd W d } nd } 0 t } jd|d |d | }	|d d S )Nr   )limitr   r   )r   filezfoo\(\) running)
Z	get_stackrI   f_codeco_nameioStringIOprint_stackseekreadassertRegex)r   r   tb)Tr$   r   r   r   +  s    
z)BaseTaskTests.test_get_stack.<locals>.barc                      s    t j jd  I d H  d S r;   )r   rp   r5   r   )r   r   r$   r   r   runner:  s    z,BaseTaskTests.test_get_stack.<locals>.runnerr5   rV   )r$   r   r   )r   r   r   r$   r   test_get_stack%  s
    zBaseTaskTests.test_get_stackc           	      C   sX  | j d dd }| |jd | |jd | |jt t|\}}d||f }| }d}| |jd | |j| | 	| j |}|
t  t|d||jdd	}| t|d
|  |  | t|d|  | tj| j j| t|d||j}| t|d|  | 	| j | }| j | t|d||j}| t|d|  d S )NFc                      s   dS Nabcr   r   r   r   r   rt   D  s    z-BaseTaskTests.test_task_repr.<locals>.notmuchrt   z#\w+.test_task_repr.<locals>.notmuch%s:%srunningTr   0<Task pending name='TestTask' %s cb=[<Dummy>()]>z3<Task cancelling name='TestTask' %s cb=[<Dummy>()]>ru   z#<Task cancelled name='TestTask' %s>z/<Task finished name='TestTask' %s result='abc'>)r5   	set_debugrI   r'   r   r)   r(   
test_utilsget_function_sourcer:   add_done_callbackr"   r   _source_tracebackreprrS   rT   r   rU   rV   )	r$   rt   filenamelinenor   gencoro_qualnamerX   r9   r   r   r   test_task_reprA  sT    



zBaseTaskTests.test_task_reprc                 C   s   dd }|  | j| d }|  | j| d }| t|t| tdt|}| | tdt|}| | | t|	dt|	d | j
| | j
| d S )Nc                      s   dS N{   r   r   r   r   r   rt   u  s    z;BaseTaskTests.test_task_repr_autogenerated.<locals>.notmuchz ^<Task pending name='Task-(\d+)'r   )r:   r5   ZassertNotEqualr   rematchassertIsNotNoneZ
assertLessintgrouprV   )r$   rt   t1t2Zmatch1Zmatch2r   r   r   test_task_repr_autogeneratedt  s    

 z*BaseTaskTests.test_task_repr_autogeneratedc                 C   sD   dd }|  | j| }|dh | | d | j| d S )Nc                      s   dS r   r   r   r   r   r   rt     s    z:BaseTaskTests.test_task_repr_name_not_str.<locals>.notmuch   z{6})r:   r5   set_namerI   get_namerV   )r$   rt   rX   r   r   r   test_task_repr_name_not_str  s
    z)BaseTaskTests.test_task_repr_name_not_strc                 C   s  | j d | t tjdd }W d    n1 s:0    Y  | |jd | |j	d | |j
t | }d}d}| |j| | |j	| tjrtjrt|\}}|jd }d||||d |d	 f }n|j}	d
||	j|	jf }| t|d|  | | j |}
|
t  tjr8dt| }n|j}	d|	j|	jf }t|d||
jtj d}| t|
d|  | j |
 d S )NFc                   S   s   dS r   r   r   r   r   r   rt     s    z<BaseTaskTests.test_task_repr_coro_decorator.<locals>.notmuchrt   z4\w+.test_task_repr_coro_decorator\.<locals>\.notmuchr   z0%s() running, defined at %s:%s, created at %s:%sr   r   z%s() running at %s:%sz<CoroWrapper %s>r   r   r   r   )r5   r   r}   r~   r   r   rI   r'   r   r)   r(   r   r   r   r   r   gi_codeco_filenameco_firstlinenor   r:   r   r"   r   rV   )r$   rt   r   Z	coro_namer   r   r   r   r9   coderX   r   r   r   r   test_task_repr_coro_decorator  sX    (

z+BaseTaskTests.test_task_repr_coro_decoratorc                 C   sv   | j d dd }| | j }| | j ||}t| j  | t|dt	t|  |
d  | j | d S )NFc                    s
   | I d H S r
   r   rg   r   r   r   wait_for  s    z7BaseTaskTests.test_task_repr_wait_for.<locals>.wait_forz<Task .* wait_for=%s>)r5   r   r?   r:   r   run_brieflyr   r   r   escaper   rV   )r$   r   rh   rM   r   r   r   test_task_repr_wait_for  s    

z%BaseTaskTests.test_task_repr_wait_forc              	   C   s   t d | jd dd }| t" tt|d}W d    n1 sP0    Y  | j	|d}d|_
| |jj W d    n1 s0    Y  t|j}d}| || d S )NTc                    s   t dI d H  d S Nr   r   sleep)xyr   r   r   func  s    z>BaseTaskTests.test_task_repr_partial_corowrapper.<locals>.funcr      FzL<coroutine object \w+\.test_task_repr_partial_corowrapper\.<locals>\.func at)r   r5   r   r}   r~   r   r   	functoolspartialcreate_task_log_destroy_pending
addCleanupZ_coror1   r   r   )r$   r   Zpartial_funcrM   Z	coro_reprexpectedr   r   r   "test_task_repr_partial_corowrapper  s    
0,
z0BaseTaskTests.test_task_repr_partial_corowrapperc                    s<    fdd}dd  dd | }|  | j|d d S )Nc                     s      I d H }  I d H }| | S r
   r   )abinner1inner2r   r   outer  s    z-BaseTaskTests.test_task_basics.<locals>.outerc                      s   dS r+   r   r   r   r   r   r     s    z.BaseTaskTests.test_task_basics.<locals>.inner1c                      s   dS )Ni  r   r   r   r   r   r     s    z.BaseTaskTests.test_task_basics.<locals>.inner2i  )rI   r5   rV   )r$   r   rX   r   r   r   test_task_basics  s
    zBaseTaskTests.test_task_basicsc                    s^   t     dd  fdd}z$ | } | W    n
   0 d S )Nc                      s   t d S r
   r   r   r   r   r   raise_error  s    zFBaseTaskTests.test_exception_chaining_after_await.<locals>.raise_errorc                     s   zt dW n ty }  z~  }z|I d H  W nT ty }  z<t| t | j}t||jft df W Y d } ~ n
d } ~ 0 0 W Y d } ~ n
d } ~ 0 0 d S )N   )r   )KeyError	Exceptionr:   rI   r   r   r   r   )r   rM   Zchainedr5   r   r$   r   r   r_     s    z>BaseTaskTests.test_exception_chaining_after_await.<locals>.run)r   ri   rz   r:   rV   r1   r$   r_   rM   r   r   r   #test_exception_chaining_after_await  s    
z1BaseTaskTests.test_exception_chaining_after_awaitc                    sp   d  t   dd  fdd}z$| }| W   n
  0  d d S )Nc                    s   | d S r
   r   )r   r   r   r   process_exc(  s    zYBaseTaskTests.test_exception_chaining_after_await_with_context_cycle.<locals>.process_excc                     s   zt dW nx ty }  z`| }z|I d H  W n4 tyn }  z| | ju  d | _W Y d } ~ n
d } ~ 0 0 W Y d } ~ n
d } ~ 0 0 d S )Nr   )r   r   r:   BaseExceptionr   )r   rM   Z	has_cycler5   r   r$   r   r   r_   +  s    
zQBaseTaskTests.test_exception_chaining_after_await_with_context_cycle.<locals>.runF)r   ri   rz   r:   rV   r1   rI   r   r   r   r   6test_exception_chaining_after_await_with_context_cycle!  s    
zDBaseTaskTests.test_exception_chaining_after_await_with_context_cyclec                    s    fdd}  |}dd } || }||j  tj || W d    n1 sd0    Y   |	   |
   |  d S )Nc                  3   s   d V }   d|  dV  d S N      $@r   assertAlmostEqualZwhenr#   r   r   r   B  s    z&BaseTaskTests.test_cancel.<locals>.genc                      s   t dI d H  dS )Nr      r   r   r   r   r   rM   I  s    z'BaseTaskTests.test_cancel.<locals>.task)rD   r:   	call_soonrS   rT   r   rU   rV   rQ   ru   	cancelledassertFalse)r$   r   r5   rM   rX   r   r#   r   test_cancel@  s    
(zBaseTaskTests.test_cancelc              
      s   g d}|D ]\ }j  d t  dd  fdd}| }tj}| W d    n1 s0    Y  |j}	|j
d t|}	|tj|df W d    q1 s0    Y  qd S )	N)r   r   )r
   r   )rP   r   )   r   cancel_argsc                      s   t dI d H  d S N
   r   r   r   r   r   r   c  s    zHBaseTaskTests.test_cancel_with_message_then_future_result.<locals>.sleepc                     sJ     } tdI d H  | j   t| gI d H \}}|   d S r   )r:   r   r   rS   waitrv   rM   ru   pendingr   r5   r$   r   r   r   r9   f  s
    
zGBaseTaskTests.test_cancel_with_message_then_future_result.<locals>.coror   r   subTestr   ri   rz   r:   rT   rU   rV   	exceptionrI   r   r!   r$   casesexpected_argsr9   rM   cmr   actualr   r   r   +test_cancel_with_message_then_future_resultU  s     
(
z9BaseTaskTests.test_cancel_with_message_then_future_resultc              
      s   g d}|D ]\ }j  d t  dd  fdd}| }tj}| W d    n1 s0    Y  |j}	|j
d t|}	|tj|df W d    q1 s0    Y  qd S )	Nr   r   c                      s   t dI d H  d S r   r   r   r   r   r   r     s    zKBaseTaskTests.test_cancel_with_message_then_future_exception.<locals>.sleepc                     sJ     } tdI d H  | j   t| gI d H \}}|   d S r   r:   r   r   rS   r   r  r   r   r   r   r9     s
    
zJBaseTaskTests.test_cancel_with_message_then_future_exception.<locals>.coror   r   r   r  r   r   r   .test_cancel_with_message_then_future_exceptionw  s     
(
z<BaseTaskTests.test_cancel_with_message_then_future_exceptionc                    s   t     dd  fdd} | }t j} | W d    n1 sd0    Y  |j}|j	d t
|}|t jddf d S )Nc                      s   t dI d H  d S r   r   r   r   r   r   r     s    zJBaseTaskTests.test_cancel_with_message_before_starting_task.<locals>.sleepc                     s:      } | d t| gI d H \}}|   d S )NrP   )r:   rS   r   r   r  r   r5   r$   r   r   r   r9     s    
zIBaseTaskTests.test_cancel_with_message_before_starting_task.<locals>.coror   r   r   )r   ri   rz   r:   rT   rU   rV   r  rI   r   r!   )r$   r9   rM   r  r   r  r   r
  r   -test_cancel_with_message_before_starting_task  s    
(
z;BaseTaskTests.test_cancel_with_message_before_starting_taskc                 C   s   |  t tjdd }W d    n1 s.0    Y  | | j| }t| j |  | 	tj
| jj| | |  | |  | |  d S )Nc                   s   s   d V  d V  dS Nr   r   r   r   r   r   rM     s    z-BaseTaskTests.test_cancel_yield.<locals>.task)r}   r~   r   r   r:   r5   r   r   rS   rT   rU   rV   rQ   ru   r   r   r$   rM   rX   r   r   r   test_cancel_yield  s    (zBaseTaskTests.test_cancel_yieldc                    s   |  | j  fdd}| | j| }t| j    | tj | j	| W d    n1 sj0    Y  | 
   | 
|  d S )Nc                      s    I d H  dS r  r   r   r   r   r   rM     s    
z4BaseTaskTests.test_cancel_inner_future.<locals>.task)r?   r5   r:   r   r   rS   rT   r   rU   rV   rQ   r   r  r   r  r   test_cancel_inner_future  s    *z&BaseTaskTests.test_cancel_inner_futurec                    s   |  | j  fdd}| | j| }t| j    |  | tj | j	| W d    n1 sr0    Y  | 
|  | 
   | 
|  d S )Nc                      s    I d H  dS r  r   r   r  r   r   rM     s    
zBBaseTaskTests.test_cancel_both_task_and_inner_future.<locals>.task)r?   r5   r:   r   r   rS   rT   r   rU   rV   rQ   ru   r   r  r   r  r   &test_cancel_both_task_and_inner_future  s    *z4BaseTaskTests.test_cancel_both_task_and_inner_futurec                    s   |  | j |  | j fdd}| | j| }t| j | |j   d  t| j | |j |  | 	
  | j|}| |d | |
  d S )Nc                      s4    I d H  zI d H  W n t jy.   Y dS 0 d S r+   r   rU   r   fut1fut2r   r   rM     s
    
z5BaseTaskTests.test_cancel_task_catching.<locals>.taskr,   r?   r5   r:   r   r   rw   _fut_waiterr   rS   rQ   r   rV   rI   r   r$   rM   rX   resr   r  r   test_cancel_task_catching  s    
z'BaseTaskTests.test_cancel_task_catchingc                    s   |  | j |  | j|  | j fdd}| | j| }t| j | |j   d  t| j | |j |  | 	
  t| j | |j d | j|}| |d | 
  | |
  d S )Nc                     s<    I d H  zI d H  W n t jy,   Y n0 I d H } | S r
   r  )r  r  r  Zfut3r   r   rM     s    

z5BaseTaskTests.test_cancel_task_ignoring.<locals>.taskr,   r  r  r   r  r   test_cancel_task_ignoring  s&    	

z'BaseTaskTests.test_cancel_task_ignoringc                    s   t  } |  fdd} ||     t j|j  	
   	   j    d S )Nc                      s(       j tdI d H  dS )Nrc   r   )rS   rQ   _must_cancelr   r   r   r$   rX   r   r   rM     s    z4BaseTaskTests.test_cancel_current_task.<locals>.taskr   ri   rz   r:   r   r   rT   rU   rV   rQ   ru   r  rS   r$   r5   rM   r   r  r   test_cancel_current_task  s    

z&BaseTaskTests.test_cancel_current_taskc                    s   t  } |  fdd} ||     t j|j  	
   	   j    dS )z+coroutine end right after task is cancelledc                      s       j dS r  )rS   rQ   r  r   r  r   r   rM   3  s    z.BaseTaskTests.test_cancel_at_end.<locals>.taskNr  r   r   r  r   test_cancel_at_end.  s    

z BaseTaskTests.test_cancel_at_endc                    s   t  fdd d    fddfdd}| t j  W d    n1 s0    Y  	  	   	  d S )Nc                      s      S r
   r1   r   r<   r   r   r@   I  rA   z8BaseTaskTests.test_cancel_awaited_task.<locals>.<lambda>c                      s    I d H  d S r
   r   r   rg   r   r   nestedN  s    z6BaseTaskTests.test_cancel_awaited_task.<locals>.nestedc                      s4      tdI d H    I d H  d S r   r:   r   r   rS   r   )r5   r$  nested_taskr$   rM   r   r   r9   Q  s    z4BaseTaskTests.test_cancel_awaited_task.<locals>.coro)
r   ri   r   r?   r:   rT   rU   rV   rQ   r   r   r   )rh   r5   r$  r&  r$   rM   r   test_cancel_awaited_taskA  s    
(z&BaseTaskTests.test_cancel_awaited_taskc                 C   s"   ||vrt d|d| dd S )Nztext z not found in:
>>>z<<<rk   )r$   textsubstrr   r   r   assert_text_containse  s    z"BaseTaskTests.assert_text_containsc                    s   t     dd  fdd} | }z | W n4 t jyz   t }|d |d Y n0 	d d S )Nc                      s   t dI d H  d S r   r   r   r   r   r   r$  o  s    zEBaseTaskTests.test_cancel_traceback_for_future_result.<locals>.nestedc                     s4      } tdI d H  |   | I d H  d S r   r%  rn   r5   r$  r$   r   r   r9   s  s    zCBaseTaskTests.test_cancel_traceback_for_future_result.<locals>.coroawait asyncio.sleep(10)zawait task  # search targetCancelledError did not occur
r   ri   rz   r:   rV   rU   	traceback
format_excr+  failr$   r9   rM   r   r   r,  r   'test_cancel_traceback_for_future_resulti  s    
z5BaseTaskTests.test_cancel_traceback_for_future_resultc                    s   t     dd  fdd} | }z | W n4 t jyz   t }|d |d Y n0 	d d S )Nc                      s   t dI d H  d S r   r   r   r   r   r   r$    s    zHBaseTaskTests.test_cancel_traceback_for_future_exception.<locals>.nestedc                     sH      } tdI d H  |   t| gI d H \}}|   d S r   r  r   r,  r   r   r9     s
    zFBaseTaskTests.test_cancel_traceback_for_future_exception.<locals>.coror-  z!task.exception()  # search targetr.  r/  r3  r   r,  r   *test_cancel_traceback_for_future_exception  s    

z8BaseTaskTests.test_cancel_traceback_for_future_exceptionc                    s   fdd} | d fdd} | }t} | W d    n1 s`0    Y  t|jd |	  d 
d   |  tj j| d S )	Nc                  3   s@   d V }   d|  dV }   d|  dV }   d|  dV  d S )N皙?皙?333333?r   r   r#   r   r   r     s    z:BaseTaskTests.test_stop_while_run_in_complete.<locals>.genr   c                      s6   dk r2t dI d H  d7 dkr    q d S )Nr   r6  r   r   )r   r   stopr   )r5   r   r   r   rM     s
    z;BaseTaskTests.test_stop_while_run_in_complete.<locals>.taskz+Event loop stopped before Future completed.r   r8  )rD   r:   rT   rk   rV   rI   rH   r  r   ru   r   timerS   r   rU   )r$   r   rM   rX   r  r   )r5   r$   r   r   test_stop_while_run_in_complete  s    	
(z-BaseTaskTests.test_stop_while_run_in_completec                 C   sZ   dd }|  | j| }| td d|_W d    n1 s@0    Y  | j| d S )Nc                      s   d S r
   r   r   r   r   r   r9     s    z.BaseTaskTests.test_log_traceback.<locals>.corozcan only be set to FalseT)r:   r5   rj   r   Z_log_tracebackrV   r$   r9   rM   r   r   r   test_log_traceback  s
    $z BaseTaskTests.test_log_tracebackc                    s   fdd} |}|td |td fdd}||| }|d d|  ||| }d|  |d d S )Nc                  3   s.   d V }   d|  dV }   d|  dV  d S Nr6  r   333333?r   r   r#   r   r   r     s
    z$BaseTaskTests.test_wait.<locals>.genr6  r?  c                     s>   t  gI d H \} }| t g |t  dS r+   r   r   rI   setru   r   r   r   r$   r   r   r     s    z$BaseTaskTests.test_wait.<locals>.foor,   rD   r:   r   r   rV   rI   r   r:  r$   r   r5   r   r  r   rC  r   	test_wait  s    
zBaseTaskTests.test_waitc                    sx   fdd} |}|td |td fdd}t| ||| }|d d S )Nc                  3   s.   d V }   d|  dV }   d|  dV  d S )N{Gz?r   Q?r   r   r#   r   r   r     s
    z5BaseTaskTests.test_wait_with_global_loop.<locals>.genrG  rH  c                     s>   t  gI d H \} }| t g |t  dS r+   r@  rB  rC  r   r   r     s    z5BaseTaskTests.test_wait_with_global_loop.<locals>.foor,   )rD   r:   r   r   rz   rV   rI   rE  r   rC  r   test_wait_with_global_loop  s    

z(BaseTaskTests.test_wait_with_global_loopc              	   C   s   |  t tjdd }W d    n1 s.0    Y  |d}| | jt|||dg}|  t  | j|\}}W d    n1 s0    Y  | | | 	t
dd |D ddh d S )Nc                 S   s   | S r
   r   sr   r   r   r9     s    z:BaseTaskTests.test_wait_duplicate_coroutines.<locals>.cororo   spamc                 s   s   | ]}|  V  qd S r
   rv   r`   r   r   r   r   	<genexpr>  rA   z?BaseTaskTests.test_wait_duplicate_coroutines.<locals>.<genexpr>)r}   r~   r   r   r:   r5   r   rV   r   rI   rA  )r$   r9   crM   ru   r   r   r   r   test_wait_duplicate_coroutines  s    (.
z,BaseTaskTests.test_wait_duplicate_coroutinesc                 C   sR   |  t| jjtt  td}tj|gdd}|  t| jj| |  d S )Nr   r   return_when)	rT   r   r5   rV   r   r   rA  r   r1   )r$   Z
sleep_coroZ	wait_coror   r   r   test_wait_errors  s    

zBaseTaskTests.test_wait_errorsc                    s    fdd}  |} |td} |td} |tj||gtjd}||\}} |h|  |h|  |	   
|	   |   d|  |d |t||g d S )Nc                  3   s.   d V }   d|  dV }   d|  dV  d S )Nr   r   r6  r   r   r#   r   r   r   $  s
    z4BaseTaskTests.test_wait_first_completed.<locals>.genr   r6  rR  r   )rD   r:   r   r   r   FIRST_COMPLETEDrV   rI   r   ru   rQ   assertIsNonerv   r   r:  advance_time)r$   r   r5   r   r   rM   ru   r   r   r#   r   test_wait_first_completed"  s"    

z'BaseTaskTests.test_wait_first_completedc                 C   s   dd }dd }|  | j| }|  | j| }|  | jtj||gtjd}| j|\}}| ||h| | |  | 	|
  | |  | 	|
  d S )Nc                      s   t dI d H  d S r   r   r   r   r   r   coro1C  s    z2BaseTaskTests.test_wait_really_done.<locals>.coro1c                      s$   t dI d H  t dI d H  d S r   r   r   r   r   r   coro2F  s    z2BaseTaskTests.test_wait_really_done.<locals>.coro2rR  )r:   r5   r   r   rU  rV   rI   rQ   ru   rV  rv   )r$   rY  rZ  r   r   rM   ru   r   r   r   r   test_wait_really_done?  s    z#BaseTaskTests.test_wait_really_donec           	         s    fdd}  |} |td}dd } || } |tj||gtjd}||\}} |h|  |h|  d|	  |
d |t||g d S )	Nc                  3   s   d V }   d|  dV  d S r   r   r   r#   r   r   r   Y  s    z4BaseTaskTests.test_wait_first_exception.<locals>.genr   c                      s   t dd S )Nerr)ZeroDivisionErrorr   r   r   r   r   c  s    z4BaseTaskTests.test_wait_first_exception.<locals>.excrR  r   r   rD   r:   r   r   r   FIRST_EXCEPTIONrV   rI   r   r:  rW  	r$   r   r5   r   r   r   rM   ru   r   r   r#   r   test_wait_first_exceptionW  s    

z'BaseTaskTests.test_wait_first_exceptionc           	         s    fdd}  |} |td}dd } || }tj||gtjd}||\}} |h|  |h|  d|	  |
d |t||g d S )	Nc                  3   s.   d V }   d|  dV }   d|  dV  d S )Nr   r   rG  r   r   r#   r   r   r   v  s
    z<BaseTaskTests.test_wait_first_exception_in_wait.<locals>.genr   c                      s   t dI d H  tdd S )NrG  r\  r   r   r]  r   r   r   r   r     s    z<BaseTaskTests.test_wait_first_exception_in_wait.<locals>.excrR  rG  r   r^  r`  r   r#   r   !test_wait_first_exception_in_waitt  s    

z/BaseTaskTests.test_wait_first_exception_in_waitc                    s   fdd} |}|td dd }||  fdd}|||  d|  |||  d|  d S )	Nc                  3   s.   d V }   d|  dV }   d|  dV  d S r>  r   r   r#   r   r   r     s
    z3BaseTaskTests.test_wait_with_exception.<locals>.genr6  c                      s   t dI d H  tdd S )Nr?  Zreallyrb  r   r   r   r   sleeper  s    z7BaseTaskTests.test_wait_with_exception.<locals>.sleeperc                     s\   t  gI d H \} }t| d |t  tdd | D }t|d d S )Nr   c                 s   s   | ]}|  d ur|V  qd S r
   )r  rN  r   r   r   rO    rA   zFBaseTaskTests.test_wait_with_exception.<locals>.foo.<locals>.<genexpr>r   )r   r   rI   lenrA  )ru   r   errorsrC  r   r   r     s
    z3BaseTaskTests.test_wait_with_exception.<locals>.foor?  )rD   r:   r   r   rV   r   r:  )r$   r   r5   rd  r   r   rC  r   test_wait_with_exception  s    
z&BaseTaskTests.test_wait_with_exceptionc                    s   fdd} |}|td |td fdd}|||  d|  |d |t g d S )	Nc                  3   s@   d V }   d|  dV }   d|  dV }   d|  dV  d S )Nr6  r   r?  )\(?r   r   r#   r   r   r     s    z1BaseTaskTests.test_wait_with_timeout.<locals>.genr6  r?  c                     sD   t j gddI d H \} }| t g |tg d S )Nrh  timeoutr@  rB  rC  r   r   r     s    z1BaseTaskTests.test_wait_with_timeout.<locals>.foorh  r   )	rD   r:   r   r   rV   r   r:  rW  r   )r$   r   r5   r   r   rC  r   test_wait_with_timeout  s    	

z$BaseTaskTests.test_wait_with_timeoutc                    s    fdd}  |} |td} |td}|tj||gdd\}} |t|g  |t|g  d|	  |
d |t||g d S )Nc                  3   s@   d V }   d|  dV }   d|  dV }   d|  dV  d S r>  r   r   r#   r   r   r     s    z8BaseTaskTests.test_wait_concurrent_complete.<locals>.genr6  r?  ri  r   )rD   r:   r   r   rV   r   rI   rA  r   r:  rW  )r$   r   r5   r   r   ru   r   r   r#   r   test_wait_concurrent_complete  s    	

z+BaseTaskTests.test_wait_concurrent_completec                    s~   fdd} |}|td |td fdd}||| }|d d|  d S )Nc                  3   s.   d V }   d|  dV }   d|  dV  d S r>  r   r   r#   r   r   r     s
    z;BaseTaskTests.test_wait_with_iterator_of_tasks.<locals>.genr6  r?  c                     sB   t t gI d H \} }| t g |t  dS r+   )r   r   iterrI   rA  rB  rC  r   r   r     s    z;BaseTaskTests.test_wait_with_iterator_of_tasks.<locals>.foor,   rD  rE  r   rC  r    test_wait_with_iterator_of_tasks  s    
z.BaseTaskTests.test_wait_with_iterator_of_tasksc                    s  dd }|  |d_t d| t& tjfdd}W d    n1 sX0    Y  |dd |dd	|d
d fdd}| t$}| | }W d    n1 s0    Y  | 	|j
d jt | d
  | d|d d v  | d	|d d v  | 	|d d | t$ | | }W d    n1 sf0    Y  | d
  d S )Nc                   s   s   dV  dV  dV  dV  d S Nr   rG  r   r   r   r   r   r     s    z,BaseTaskTests.test_as_completed.<locals>.geng      ?Fc                 3   s@   t | E d H   | s<d v r<d v r<dd |S )Nr   r   TgQ?)r   r   addrW  )dtr   )	completedr5   time_shiftedr   r   rd    s    

z0BaseTaskTests.test_as_completed.<locals>.sleeperrG  r   r   r?  rP  c                     s2   g } t j gdD ]}| |I d H  q| S r;   )r   as_completedappend)valuesr   )r   r   rP  r5   r   r   r     s    z,BaseTaskTests.test_as_completed.<locals>.foor   r   )rD   Zslow_callback_durationrA  r}   r~   r   r   rV   r:   rI   warningsr   __file__r   r:  rQ   )r$   r   rd  r   wr  r   )r   r   rP  rr  r5   rs  r   test_as_completed  s,    
0	


24zBaseTaskTests.test_as_completedc                    s  dd }|  |tdd tdd fdd}| t$ | | }W d    n1 sz0    Y  | t	|d	| | |d
 d | |d d
 d	 | 
|d d tj | d  d t g d S )Nc                   s   s   d V  dV  dV  dV  d S )Nr   r6  r   r   r   r   r   r   2  s    z9BaseTaskTests.test_as_completed_with_timeout.<locals>.genr6  r   r?  r   c                     s   g } t j gddD ]d}| r*d z|I d H }| d|f W q t jyz } z| d|f W Y d }~qd }~0 0 q| S )NQ?rj  r5   g{Gz?r   r   )r   rt  rW  ru  TimeoutError)rv  r   r.   r   r   r   r5   r   r   r   =  s    

&z9BaseTaskTests.test_as_completed_with_timeout.<locals>.foor   r   )r   r   r   r{  r   )rD   r   r   r   r}   r~   rV   r:   rI   re  rK   r}  r   r:  rW  r   )r$   r   r   r  r   r~  r   test_as_completed_with_timeout0  s    
2
z,BaseTaskTests.test_as_completed_with_timeoutc                    sp   dd } |tdd  fdd}t$ |  W d    n1 sb0    Y  d S )Nc                   s   s   d V  dV  dV  d S ro  r   r   r   r   r   r   W  s    z@BaseTaskTests.test_as_completed_with_unused_timeout.<locals>.genrG  r   c                     s2   t j gddD ]} | I d H }|d qd S )Nr   r|  r   )r   rt  rI   )r   r.   r   r5   r$   r   r   r   `  s    
z@BaseTaskTests.test_as_completed_with_unused_timeout.<locals>.foo)rD   r   r   r}   r~   rV   r:   )r$   r   r   r   r  r   %test_as_completed_with_unused_timeoutU  s    
z3BaseTaskTests.test_as_completed_with_unused_timeoutc           	      C   s   dd }|  |}tdd}tdd}||h}| t" ttj||d}W d    n1 sd0    Y  | t|d |	|d	 }| |d | 
d|  |d |	|d
 }| |d | 
d|  d S )Nc                   s   s   dV  dV  dV  d S )Nr   皙?r   r   r   r   r   r   j  s    z9BaseTaskTests.test_as_completed_reverse_wait.<locals>.genr  r   r6  r   r<   r   r   r   )rD   r   r   r}   r~   listrt  rI   re  rV   r   r:  rW  )	r$   r   r5   r   r   fsfutsr   r   r   r   r   test_as_completed_reverse_waith  s    
0
z,BaseTaskTests.test_as_completed_reverse_waitc           
         s    fdd}  |}tdd}tdd}||h} t" ttj||d}W d    n1 sh0    Y   t|d t	|} t |
|\}}	W d    n1 s0    Y   tdd	 |D ddh d S )
Nc                  3   s.   d V }   d|  dV }   d|  dV  d S )Nr  r   r   r   r#   r   r   r     s
    z7BaseTaskTests.test_as_completed_concurrent.<locals>.genr  r   r   r<   r   c                 s   s   | ]}|  V  qd S r
   rM  rN  r   r   r   rO    rA   z=BaseTaskTests.test_as_completed_concurrent.<locals>.<genexpr>)rD   r   r   r}   r~   r  rt  rI   re  r   rV   rA  )
r$   r   r5   r   r   r  r  waiterru   r   r   r#   r   test_as_completed_concurrent  s    
0
,z*BaseTaskTests.test_as_completed_concurrentc                    s    t tjdd  W d    n1 s.0    Y   t$ tj fdd}W d    n1 sl0    Y   t, j| }j| W d    n1 s0    Y  | }t	|ddh t
|d d S )Nc                 S   s   | S r
   r   rJ  r   r   r   r9     s    zBBaseTaskTests.test_as_completed_duplicate_coroutines.<locals>.coroc                  3   s@   g }  d}t j|| dgjdD ]}| |E d H  q&| S )NhamrL  r<   )r   rt  r5   ru  )rv   rP  r   r9   r$   r   r   r     s    
zDBaseTaskTests.test_as_completed_duplicate_coroutines.<locals>.runnerr  rL  r   )r}   r~   r   r   r:   r5   rV   rv   rI   rA  re  )r$   r   rh   rv   r   r  r   &test_as_completed_duplicate_coroutines  s    (.*z4BaseTaskTests.test_as_completed_duplicate_coroutinesc                    sl    fdd}  |}dd } ||dd}||  |   | d  d|  d S )Nc                  3   s.   d V }   d|  dV }   d|  dV  d S )Nr  r6  r   r   r#   r   r   r     s
    z%BaseTaskTests.test_sleep.<locals>.genc                    s.   t | d I d H  t | d |I d H }|S )Nr   r   )rq  argr  r   r   r   rd    s    z)BaseTaskTests.test_sleep.<locals>.sleeperr6  yeah)	rD   r:   rV   rQ   ru   rI   rv   r   r:  )r$   r   r5   rd  rX   r   r#   r   
test_sleep  s    

zBaseTaskTests.test_sleepc                    s   fdd} |}|tdd}d  |j fdd}||_t|  j |	  t| 
 j d S )Nc                  3   s   d V }   d|  dV  d S r   r   r   r#   r   r   r     s    z,BaseTaskTests.test_sleep_cancel.<locals>.genr   r  c                    s   | |g|R    S r
   r   )delaycallbackr   )handleorig_call_laterr   r   
call_later  s    z3BaseTaskTests.test_sleep_cancel.<locals>.call_later)rD   r:   r   r   r  r   r   r   Z
_cancelledrS   rQ   )r$   r   r5   rX   r  r   )r  r  r$   r   test_sleep_cancel  s    


zBaseTaskTests.test_sleep_cancelc                    sZ   fdd} | dd  fdd}| } |d d   d S )	Nc                  3   s.   d V }   d|  dV }   d|  dV  d S )Nr6  r     r   r   r#   r   r   r     s
    z9BaseTaskTests.test_task_cancel_sleeping_task.<locals>.genc                    s   t | I d H  d S r
   r   )rq  r   r   r   r     s    z;BaseTaskTests.test_task_cancel_sleeping_task.<locals>.sleepc                     sL     d}  d| j z| I d H  W n tjyB   Y dS 0 dS d S )Nr  r6  r   zslept in)r:   r  rS   r   rU   )rd  r
  r   r   doit  s    z:BaseTaskTests.test_task_cancel_sleeping_task.<locals>.doitr   r6  )rD   rI   rV   r   r:  )r$   r   r  Zdoerr   r
  r   test_task_cancel_sleeping_task  s    

z,BaseTaskTests.test_task_cancel_sleeping_taskc                    s   |  | j  fdd}| | j| }t| j | |j  |  t| j | t	j
| jj| | |j |    d S )Nc                      s    I d H  d S r
   r   r   rg   r   r   r9     s    z:BaseTaskTests.test_task_cancel_waiter_future.<locals>.coro)r?   r5   r:   r   r   rw   r  rS   rT   r   rU   rV   rV  rQ   r   r<  r   rg   r   test_task_cancel_waiter_future  s    z,BaseTaskTests.test_task_cancel_waiter_futurec                 C   s   dd }| }|  | j|}| td |d W d    n1 sH0    Y  | td |t  W d    n1 s0    Y  | | j|d d S )Nc                      s   dS )Nr   r   r   r   r   r   rt     s    z4BaseTaskTests.test_task_set_methods.<locals>.notmuchznot support set_resultrs   znot support set_exceptionr   )	r:   r5   rj   rk   r   set_exceptionr   rI   rV   )r$   rt   r   rM   r   r   r   test_task_set_methods  s    (*
z#BaseTaskTests.test_task_set_methodsc                 C   sP   |  t tjdd }W d    n1 s.0    Y  | t| jj|  d S )Nc                   s   s   d V  dV  dS )Nr   r   r   r   r   r   r   rt   "  s    z/BaseTaskTests.test_step_result.<locals>.notmuchr}   r~   r   r   rT   rk   r5   rV   )r$   rt   r   r   r   test_step_result   s    (zBaseTaskTests.test_step_resultc                    s   G dd dt j}|| jd d  fdd}| | j| }t| j |  j t } 	| t| j | 
| | |  | |  d S )Nc                       s(   e Zd Z fddZ fddZ  ZS )z2BaseTaskTests.test_step_result_future.<locals>.Futc                    s   d| _ t j|i | d S NF)cb_addedrB   r   )r$   r   kwdsrG   r   r   r   /  s    z;BaseTaskTests.test_step_result_future.<locals>.Fut.__init__c                    s   d| _ t j|i | d S NT)r  rB   r   r$   r   kwargsrG   r   r   r   3  s    zDBaseTaskTests.test_step_result_future.<locals>.Fut.add_done_callbackr'   r(   r)   r   r   __classcell__r   r   rG   r   Fut.  s   r  r<   c                      s    I d H d S r
   r   r   rh   rv   r   r   wait_for_future:  s    z>BaseTaskTests.test_step_result_future.<locals>.wait_for_future)r   r=   r5   r:   r   r   rQ   r  objectr   rw   ru   rV  rv   )r$   r  r  rX   r  r   r  r   test_step_result_future+  s    	
z%BaseTaskTests.test_step_result_futurec                    s   fdd} |}dd t   fdd}|| }t| |  |  ttj| 	|  |
  |   d S )Nc                  3   s   d V }   d|  dV  d S r   r   r   r#   r   r   r   K  s    z;BaseTaskTests.test_baseexception_during_cancel.<locals>.genc                      s   t dI d H  d S r   r   r   r   r   r   rd  R  s    z?BaseTaskTests.test_baseexception_during_cancel.<locals>.sleeperc                      s.   z I d H  W n t jy(    Y n0 d S r
   r  r   )base_excrd  r   r   notmutchW  s    z@BaseTaskTests.test_baseexception_during_cancel.<locals>.notmutch)rD   
SystemExitr:   r   r   rS   r   ru   rT   rQ   r   rw   r  )r$   r   r5   r  rM   r   )r  r$   rd  r    test_baseexception_during_cancelI  s    

z.BaseTaskTests.test_baseexception_during_cancelc                 C   s   dd }|  t| dd }|  t| | t tjdd }W d    n1 s^0    Y  | t| |  tt  d S )Nc                   S   s   d S r
   r   r   r   r   r   fnj  s    z2BaseTaskTests.test_iscoroutinefunction.<locals>.fnc                   s   s
   d V  d S r
   r   r   r   r   r   fn1o  s    z3BaseTaskTests.test_iscoroutinefunction.<locals>.fn1c                   s   s
   d V  d S r
   r   r   r   r   r   fn2t  s    z3BaseTaskTests.test_iscoroutinefunction.<locals>.fn2)	r   r   iscoroutinefunctionr}   r~   r   rQ   r   Mock)r$   r  r  r  r   r   r   test_iscoroutinefunctioni  s    (z&BaseTaskTests.test_iscoroutinefunctionc                    s   |  | j | t" tj fdd}W d    n1 s>0    Y  | }| t | j| W d    n1 sz0    Y  | 	 
  d S )Nc                   3   s
    V  d S r
   r   r   rg   r   r   r    s    z?BaseTaskTests.test_yield_vs_yield_from.<locals>.wait_for_future)r?   r5   r}   r~   r   r   rT   rk   rV   r   ru   r$   r  rM   r   rg   r   test_yield_vs_yield_from{  s    ,*z&BaseTaskTests.test_yield_vs_yield_fromc                    s   |  t tjdd  W d    n1 s.0    Y  |  t" tj fdd}W d    n1 sj0    Y  | }| t| jj| d S )Nc                   s   s
   d V  d S r
   r   r   r   r   r   r9     s    z>BaseTaskTests.test_yield_vs_yield_from_generator.<locals>.coroc                  3   s(     } z| V  W |    n
|    0 d S r
   r#  )r   r   r   r   r    s    zIBaseTaskTests.test_yield_vs_yield_from_generator.<locals>.wait_for_futurer  r  r   r   r   "test_yield_vs_yield_from_generator  s    (,z0BaseTaskTests.test_yield_vs_yield_from_generatorc                 C   sz   |  t tjdd }W d    n1 s.0    Y  | t| | }| t| | j|}| 	|d d S )Nc                   S   s   dS Nro   r   r   r   r   r   r     s    z;BaseTaskTests.test_coroutine_non_gen_function.<locals>.funcro   )
r}   r~   r   r   rQ   r  iscoroutiner5   rV   rI   )r$   r   r9   r  r   r   r   test_coroutine_non_gen_function  s    (z-BaseTaskTests.test_coroutine_non_gen_functionc                    s   |  | j | t" tj fdd}W d    n1 s>0    Y   fdd}| | j| }| | j| }| j|}| |d | 	|
  d S )Nc                      s    S r
   r   r   rg   r   r   r     s    zIBaseTaskTests.test_coroutine_non_gen_function_return_future.<locals>.funcc                      s     d d S r  r   r   rg   r   r   r9     s    zIBaseTaskTests.test_coroutine_non_gen_function_return_future.<locals>.cororo   )r?   r5   r}   r~   r   r   r:   rV   rI   rV  rv   )r$   r   r9   r   r   r  r   rg   r   -test_coroutine_non_gen_function_return_future  s    ,z;BaseTaskTests.test_coroutine_non_gen_function_return_futurec                    sZ     tj jd  fdd}  j| j j   tj jd d S )Nr<   c                    s:     tj| d   td    t  d S r;   )rw   r   current_taskr<   rL   r   r   r9     s    z-BaseTaskTests.test_current_task.<locals>.coro)rV  r   r  r5   r:   rV   r   r   rL   r   test_current_task  s
    zBaseTaskTests.test_current_taskc                    s    tjjd j j fdd} fdd}j|jj|jjtf  tjjd d S )Nr<   c                    sD    tj| du   I d H   tj| du  d d S Nr<   TrQ   r   r  r   r<   )r  r  r$   task1r   r   rY    s    
zFBaseTaskTests.test_current_task_with_interleaving_tasks.<locals>.coro1c                    sD    tj| du   d I d H   tj| du  d S r  r  r<   )r  r  r$   task2r   r   rZ    s    

zFBaseTaskTests.test_current_task_with_interleaving_tasks.<locals>.coro2)rV  r   r  r5   r?   r:   rV   r   )r$   rY  rZ  r   )r  r  r$   r  r  r   )test_current_task_with_interleaving_tasks  s    z7BaseTaskTests.test_current_task_with_interleaving_tasksc                    s~   d jfdd  fdd}tj| jd}tj |  j| d 	
  d S )Nr   c                      s<   zI d H  W n t jy,    d7   Y n0 d d S )Nr   zgot past sleep() in inner())r   rU   r2  r   )proofr$   r  r   r   inner  s    z<BaseTaskTests.test_yield_future_passes_cancel.<locals>.innerc                      s:   z  I d H  W n t jy,   d7 Y n
0 d7 d S )Nrc   r   r  r   r  r  r   r   r     s
    z<BaseTaskTests.test_yield_future_passes_cancel.<locals>.outerr<   e   )r?   r5   r   rp   r   r   rS   rV   rI   rQ   r   r$   r   r   r   r  r  r$   r  r   test_yield_future_passes_cancel  s    
	z-BaseTaskTests.test_yield_future_passes_cancelc                    s   d jfdd  fdd}tj| jd}tj |  tjjj	| 
d  tj d d S )Nr   c                      s   I d H   d7  d S Nr   r   r   r  r  r   r   r  	  s    
zCBaseTaskTests.test_yield_wait_does_not_shield_cancel.<locals>.innerc                     sN    t( t  gI d H \} }W d    n1 s80    Y  d7 d S Nrc   )r}   r~   r   r   )dp)r  r  r$   r   r   r     s    6zCBaseTaskTests.test_yield_wait_does_not_shield_cancel.<locals>.outerr<   r   r?   r5   r   rp   r   r   rS   rT   rU   rV   r   rI   r  r   r  r   &test_yield_wait_does_not_shield_cancel  s    
z4BaseTaskTests.test_yield_wait_does_not_shield_cancelc                 C   s<   |  | j}t|}|d | j|}| |d d S r+   )r?   r5   r   shieldr   rV   rI   )r$   r  r   r  r   r   r   test_shield_result  s
    

z BaseTaskTests.test_shield_resultc                 C   sT   |  | j}t|}t| j td}|| t| j | |	 | d S )Nr   )
r?   r5   r   r  r   r   rk   r  rw   r  )r$   r  r   r   r   r   r   test_shield_exception$  s    

z#BaseTaskTests.test_shield_exceptionc                 C   sH   |  | j}t|}t| j |  t| j | |  d S r
   )	r?   r5   r   r  r   r   rS   rQ   r   r$   r  r   r   r   r   test_shield_cancel_inner-  s    
z&BaseTaskTests.test_shield_cancel_innerc                 C   sh   |  | j}t|}t| j |  t| j | |  | 	d|j
d u rXdnt|j
 d S r   )r?   r5   r   r  r   r   rS   rQ   r   rI   Z
_callbacksre  r  r   r   r   test_shield_cancel_outer5  s    
z&BaseTaskTests.test_shield_cancel_outerc                 C   s8   |  | j}|d | jt|}| |d d S r+   )r?   r5   r   rV   r   r  rI   )r$   rh   r  r   r   r   test_shield_shortcut>  s    
z"BaseTaskTests.test_shield_shortcutc                    s   d|  | jfdd  fdd}tj| | jd}t| j |  | tj | j	| W d    n1 s0    Y  
d  t| j | d d S )Nr   c                      s   I d H   d7  d S r  r   r   r  r   r   r  I  s    
z/BaseTaskTests.test_shield_effect.<locals>.innerc                      s   t   I d H  d7 d S r  )r   r  r   r  r   r   r   N  s    z/BaseTaskTests.test_shield_effect.<locals>.outerr<   r   r  r  r   )r  r  r  r   test_shield_effectD  s    *
z BaseTaskTests.test_shield_effectc                 C   s   |  | j}|  | j}t||}t|}t| j |  t| j | |	  |
d |
d t| j | | ddg d S Nr   r   )r?   r5   r   rd   r  r   r   rS   rQ   r   r   rI   rv   )r$   child1child2parentr   r   r   r   test_shield_gather\  s    


z BaseTaskTests.test_shield_gatherc                 C   s   |  | j}|  | j}t|}t|}t||}t| j |  t| j | |	 tj
 | |  | |  |d |d t| j d S r  )r?   r5   r   r  rd   r   r   rS   rK   r  rU   rQ   r   r   )r$   r  r  r   r   r  r   r   r   test_gather_shieldj  s    



z BaseTaskTests.test_gather_shieldc                 C   sZ   |  | j}| t| jjtj|| jd t }| t| jjtj|| jd |  d S r;   )	r?   r5   rT   r   rV   r   rt  r   r1   r$   rh   r9   r   r   r   test_as_completed_invalid_args{  s    z,BaseTaskTests.test_as_completed_invalid_argsc                 C   sf   |  | j}| t| jjt| t }| t| jjt| |  | t	| jjtg  d S r
   )
r?   r5   rT   r   rV   r   r   r   r1   r   r  r   r   r   test_wait_invalid_args  s    z$BaseTaskTests.test_wait_invalid_argsc                    sl    fdd}t d |  W d    n1 s00    Y  t d |  W d    n1 s^0    Y  d S )Nc                     s    t& tj fdd} W d    n1 s60    Y  j  d  |  t 	j
tj j 	jtj j j
 d S )Nc                   3   s    j  E d H  d S r
   )rQ   
gi_runningr   )rh   r   r$   r   r   r9     s    zKBaseTaskTests.test_corowrapper_mocks_generator.<locals>.check.<locals>.coro)r}   r~   r   r   r?   r5   r   rQ   r  rK   gi_frametypes	FrameTyper   r  r   CodeTyperV   rV  r   r#   )rh   r   r   check  s    0
z=BaseTaskTests.test_corowrapper_mocks_generator.<locals>.checkFTr   r$   r  r   r#   r    test_corowrapper_mocks_generator  s
    
$
z.BaseTaskTests.test_corowrapper_mocks_generatorc              	      s  t d  t" tjfdd}W d    n1 s<0    Y   t$ tj fddW d    n1 sz0    Y   t tjdd W d    n1 s0    Y    j| } j|} |d W d    n1 s0    Y  d S )	NTc                   3   s     E d H S r
   r   r   )r   r   r   r     s    z5BaseTaskTests.test_yield_from_corowrapper.<locals>.t1c                  3   s(      j}   j|  | E d H S r
   )r?   r5   r:   r  )r$   t3r   r   r     s    z5BaseTaskTests.test_yield_from_corowrapper.<locals>.t2c                 S   s   |  d d S )Nr   r   r   r  r  r   r   r   r    s    z5BaseTaskTests.test_yield_from_corowrapper.<locals>.t3r  )	r   r}   r~   r   r   r:   r5   rV   rI   )r$   r   rM   valr   )r$   r   r  r   test_yield_from_corowrapper  s    
,.(z)BaseTaskTests.test_yield_from_corowrapperc                    s8   dd   fdd}|  |dd |  |dd d S )Nc                  s   s
   d V } | S r
   r   )r   r   r   r   r     s    z;BaseTaskTests.test_yield_from_corowrapper_send.<locals>.fooc              
      sb   t j  }|d  z||  W n. tyT } z|jd W  Y d }~S d }~0 0 tdd S )Nr   zStopIteration was expected)r   r   CoroWrapperr/   r-   r   AssertionError)r  cwexr   r   r   call  s    
 z<BaseTaskTests.test_yield_from_corowrapper_send.<locals>.call)r   r   rL  )rI   )r$   r  r   r  r    test_yield_from_corowrapper_send  s    
z.BaseTaskTests.test_yield_from_corowrapper_sendc                 C   s0   t  }dd }tj| }||d< d |_d S )Nc                   s   s   g E d H  d S r
   r   r   r   r   r   r     rA   z3BaseTaskTests.test_corowrapper_weakref.<locals>.foor  )weakrefWeakValueDictionaryr   r   r  r   )r$   Zwdr   r  r   r   r   test_corowrapper_weakref  s
    z&BaseTaskTests.test_corowrapper_weakrefc                 C   s   dd }t d}tj| }|d  | ||| tj| }|d  | ||t | tj| }|d  |t d}| |t  | |j	d tj| }|d  |t dd }| |t  | |j	d d S )Nc               
   s   s>   d } z
| V } W q t y6 } z|} W Y d }~qd }~0 0 qd S r
   )r   )valueer   r   r   r     s
    
z1BaseTaskTests.test_corowrapper_throw.<locals>.foor   r  )
r   r   r   r  r/   rw   r0   rK   rI   r   )r$   r   r  r  r   r   r   test_corowrapper_throw  s$    



z$BaseTaskTests.test_corowrapper_throwc                    s   j j} t" tj fdd}W d    n1 s:0    Y  t } j	d  j
| | j}tj| jd} tj jd|h td   j   t jjd |jjd= d }|j}d }t   tj jdt  | jdtj|d |  d S )	Nc                 3   s      | }|E d H  tdd S )Nzcode never reached)r?   r   )r5   futurer#   r   r   kill_me  s    

z>BaseTaskTests.test_log_destroyed_pending_task.<locals>.kill_meTr<   r   r  z%Task was destroyed but it is pending!)messagerM   r   )r7   r8   r}   r~   r   r   r   r  r5   r   set_exception_handlerrp   rI   	all_tasksrz   Z	_run_oncere  _readyr  f_localsr   r   
gc_collectrA  Zassert_called_withANYZ
reset_mock)r$   r8   r  Zmock_handlerr9   rM   r   r   r#   r   test_log_destroyed_pending_task  s2    ,



z-BaseTaskTests.test_log_destroyed_pending_taskzasyncio.base_events.loggerc                    sH   t   dd   fdd}|  |jj d S )Nc                      s   t d S r
   )r   r   r   r   r   r9   ?  s    zBBaseTaskTests.test_tb_logger_not_called_after_cancel.<locals>.coroc                     s.      } tdI d H  |   d } d S )Nr  r%  rn   r9   r5   r$   r   r   r   B  s    zDBaseTaskTests.test_tb_logger_not_called_after_cancel.<locals>.runner)r   ri   rz   rV   r   errorcalled)r$   m_logr   r   r  r   &test_tb_logger_not_called_after_cancel:  s    
z4BaseTaskTests.test_tb_logger_not_called_after_cancelzasyncio.coroutines.loggerc           	   	   C   s   t dH | t tjdd }W d    n1 s80    Y  W d    n1 sV0    Y  t}t jd }|  t	
  | |jj |jjd d }t|\}}dt|jt||t||f }| |t|tj d S )NTc                   S   s   d S r
   r   r   r   r   r   	coro_noopO  s    z=BaseTaskTests.test_coroutine_never_yielded.<locals>.coro_noopr   r   z^<CoroWrapper %s\(?\)? .* at %s:%s, .*> was never yielded from\nCoroutine object created at \(most recent call last, truncated to \d+ last lines\):\n.*\n  File "%s", line %s, in test_coroutine_never_yielded\n    coro_noop\(\)$)r   r}   r~   r   r   rx  sys	_getframef_linenor   r  rQ   r  r  	call_argsr   r   r   r   r)   r   compileDOTALL)	r$   r	  r  Ztb_filename	tb_linenor  Zfunc_filenameZfunc_linenoZregexr   r   r   test_coroutine_never_yieldedK  s$    
F



z*BaseTaskTests.test_coroutine_never_yieldedc                    sl    fdd}t d |  W d   n1 s00    Y  t d |  W d   n1 s^0    Y  dS )zReturn of @asyncio.coroutine()-wrapped function generator object
        from @asyncio.coroutine()-wrapped function should have same effect as
        returning generator object or Future.c                     sZ     t" tj fdd} W d    n1 s20    Y   j|  } |d d S )Nc                     s>     t tjdd } W d    n1 s.0    Y  |  S )Nc                   S   s   dS r  r   r   r   r   r   
inner_coror  s    ziBaseTaskTests.test_return_coroutine_from_coroutine.<locals>.check.<locals>.outer_coro.<locals>.inner_coro)r}   r~   r   r   )r  r#   r   r   
outer_coroo  s    (zUBaseTaskTests.test_return_coroutine_from_coroutine.<locals>.check.<locals>.outer_coror   r}   r~   r   r   r5   rV   rI   )r  rv   r#   r   r   r  m  s
    ,zABaseTaskTests.test_return_coroutine_from_coroutine.<locals>.checkFNTr  r  r   r#   r   $test_return_coroutine_from_coroutinei  s
    
$
z2BaseTaskTests.test_return_coroutine_from_coroutinec                 C   sh   | j d | | j t }t jd }| |jt	 | 
|jd d d t|df | j | d S )NTr   r   test_task_source_traceback)r5   r   r:   r   r  r  r  rK   r   r  rI   rx  rV   )r$   rM   r   r   r   r   r    s    z(BaseTaskTests.test_task_source_tracebackc                    s   t  }| |j | |fdd}| t  t j| |d}W d   n1 sZ0    Y  t j||dd  fdd}	| 
d | |  d |   |  dg dS )	zYEnsure that a gathering future refuses to be cancelled once all
        children are donec                   3   s
    E d H S r
   r   r   rg   r   r   
child_coro  s    z6BaseTaskTests.test_cancel_gather_1.<locals>.child_coror<   Nc                    s      d S r
   rS   )ra   )cancel_resultgather_taskr   r   cancelling_callback  s    z?BaseTaskTests.test_cancel_gather_1.<locals>.cancelling_callbackr,   F)r   ri   r   r1   r?   r}   r~   rd   rp   r   r   rV   rI   r   r   rv   )r$   r5   r  Zgather_futurer  r   )r  rh   r  r   test_cancel_gather_1  s    
.


z"BaseTaskTests.test_cancel_gather_1c                    s   g d}|D ]\ }j  d t j fdd fdd}z|  W nd tjy } zJ|jd t	|\}}}||ftj|f 
|d W Y d }~nd }~0 0 d	 W d    q1 s0    Y  qd S )
Nr   r   c                     sd   d} | d7 }  t, tjtdd dI d H  W d    n1 sH0    Y  | dkrd S qd S )Nr   r  T)return_exceptionsr5   r   )r}   r~   r   rd   r   )r:  )r5   r$   r   r   ro     s    *z0BaseTaskTests.test_cancel_gather_2.<locals>.testc                     s6     } tdI d H  | j   | I d H  d S )Nr7  r%  )Zqwer   r5   r$   ro   r   r   main  s    
z0BaseTaskTests.test_cancel_gather_2.<locals>.mainr   )r   r   z1gather did not propagate the cancellation request)r   r   ri   r   r1   rV   rU   rI   r   r!   assertInr2  )r$   r  r  r"  r   exc_typeZexc_argsr   r   r!  r   test_cancel_gather_2  s"    
"z"BaseTaskTests.test_cancel_gather_2c                    s(   dd   fdd}j |  d S )Nc                      s   dd  d S )Nr   r   r   r   r   r   r   r     s    z3BaseTaskTests.test_exception_traceback.<locals>.fooc                     s4    j  } tdI d H  |  j d S r   )r:   r5   r   r   r   r  __traceback__rn   r   r$   r   r   r"    s    z4BaseTaskTests.test_exception_traceback.<locals>.mainr   r$   r"  r   r'  r   test_exception_traceback  s    z&BaseTaskTests.test_exception_tracebackc              	   C   s   dd }|| j _| t tjdd }W d    n1 s>0    Y  | |jj | 	t
< | }z| | j | W |  n
|  0 W d    n1 s0    Y  t  | |jj |jjd d }| d| | t| j t  d S )Nc                 _   s   t d S r
   r   )r  r   r  r   r   r   r     s    z8BaseTaskTests.test_error_in_call_soon.<locals>.call_soonc                   S   s   d S r
   r   r   r   r   r   r9     s    z3BaseTaskTests.test_error_in_call_soon.<locals>.coror   z$Task was destroyed but it is pending)r5   r   r}   r~   r   r   r   r  r  rT   r   r:   r1   r[   r\   rQ   r  r#  rI   r   rA  )r$   r	  r   r9   r   r  r   r   r   test_error_in_call_soon  s     (2z%BaseTaskTests.test_error_in_call_soonc                 C   sx   |  td | | jd W d    n1 s00    Y  |  td | | jd W d    n1 sj0    Y  d S )Nz!a coroutine was expected, got 123r   )rj   r   r:   r5   r#   r   r   r   "test_create_task_with_noncoroutine	  s    ,z0BaseTaskTests.test_create_task_with_noncoroutinec                 C   s   |  t tjdd }W d    n1 s.0    Y  | | j| }| || j | j| | | j| }| || j | j| d S )Nc                   S   s   d S r
   r   r   r   r   r   r9   	  s    zDBaseTaskTests.test_create_task_with_oldstyle_coroutine.<locals>.coro)	r}   r~   r   r   r:   r5   rK   r8   rV   r<  r   r   r   (test_create_task_with_oldstyle_coroutine	  s    (z6BaseTaskTests.test_create_task_with_oldstyle_coroutinec                 C   s`   dd }|  | j| }| || j | j| |  | j| }| || j | j| d S )Nc                      s   d S r
   r   r   r   r   r   r9   	  s    z@BaseTaskTests.test_create_task_with_async_function.<locals>.coro)r:   r5   rK   r8   rV   r<  r   r   r   $test_create_task_with_async_function	  s    z2BaseTaskTests.test_create_task_with_async_functionc                 C   sh   |  | jt }| || j | | j|d |  | jt }| || j | | j|d d S r+   )r:   r5   r*   rK   r8   rI   rV   rL   r   r   r   (test_create_task_with_asynclike_function,	  s    z6BaseTaskTests.test_create_task_with_asynclike_functionc                    s(   dd   fdd}j |  d S )Nc                      s   dS r  r   r   r   r   r   r  9	  s    z2BaseTaskTests.test_bare_create_task.<locals>.innerc                     s4   t   } | j | I d H }d| d S r  )r   r   rK   r8   rI   )rM   retr  r$   r   r   r9   <	  s    
z1BaseTaskTests.test_bare_create_task.<locals>.coror   r   r   r0  r   test_bare_create_task7	  s    z#BaseTaskTests.test_bare_create_taskc                    s(   dd   fdd}j |  d S )Nc                      s   d S r
   r   r   r   r   r   r  F	  s    z<BaseTaskTests.test_bare_create_named_task.<locals>.coro_noopc                     s.   t j  dd} |  d | I d H  d S )NzNo-op)r6   )r   r   rI   r   rn   r  r$   r   r   r9   I	  s    z7BaseTaskTests.test_bare_create_named_task.<locals>.coror   r   r   r2  r   test_bare_create_named_taskD	  s    z)BaseTaskTests.test_bare_create_named_taskc                    sj   t jddd  fdd fdd}t z$| }| W   n
  0 d S )Ncvarnopedefaultc                      s.   t dI d H    d  d d S )NrG  r5  something else)r   r   rI   getrA  r   r4  r$   r   r   subS	  s    z)BaseTaskTests.test_context_1.<locals>.subc                     sV      d  }  d    d | I d H     d d S )Nr5  yes)rI   r9  r:   rA  )Zsubtaskr4  r5   r$   r;  r   r   r"  X	  s    

z*BaseTaskTests.test_context_1.<locals>.main)contextvars
ContextVarr   ri   r:   rV   r1   r$   r"  rM   r   r=  r   test_context_1P	  s    zBaseTaskTests.test_context_1c                    sj   t jddd  fdd}t z$| }| W   n
  0   d d S )Nr4  r5  r6  c               	      s    fdd}    d tdD ]}}||   d|  |jd  |I d H     d|  tdD ]F} d| d|  t	dI d H     d| d|  qq$d S )	Nc                    s     d d S )Nr8  )rA  rg   )r4  r   r   fut_on_donek	  s    z?BaseTaskTests.test_context_2.<locals>.main.<locals>.fut_on_doner5  r   r<  r   -MbP?)
rI   r9  re   r?   r   rA  r   r   r   r   )rB  jrh   ir4  r5   r$   r   r   r"  j	  s    


z*BaseTaskTests.test_context_2.<locals>.main)	r>  r?  r   ri   r:   rV   r1   rI   r9  r@  r   rG  r   test_context_2g	  s    zBaseTaskTests.test_context_2c                    sj   t jddd  fddfdd}t z|  W   n
  0   d d S )Nr4  r   r6  c                    sL   t dD ]>} | |  ttddI d H    | |  qd S )Nr   rD  r  )re   rA  r   r   randomuniformrI   r9  )numrF  r:  r   r   r;  	  s    z)BaseTaskTests.test_context_3.<locals>.subc                     sF   g } t dD ]$} tdd}| | qtj|  I d H  d S )Nrc   r   r   )re   r   rI  randintru  r   rd   )r   rF  rM   )r5   r;  r   r   r"  	  s
    z*BaseTaskTests.test_context_3.<locals>.main)r>  r?  r   ri   rV   r1   rI   r9  r(  r   r=  r   test_context_3	  s    zBaseTaskTests.test_context_3c                 C   sP   t  }t }z2| ||}|| | | | W |  n
|  0 d S r
   )r   ri   r   r:   rV   rw   Zget_coror1   )r$   r5   r9   rM   r   r   r   test_get_coro	  s    
zBaseTaskTests.test_get_coro)r4   )xr'   r(   r)   r8   r=   r:   r?   rC   rN   rY   rZ   rf   rm   rq   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r	  r  r  r  r  r  r  r!  r"  r'  r+  r4  r5  r;  r=  rF  rI  rQ  rT  rX  r[  ra  rc  rg  rk  rl  rn  rz  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   patchr
  r  r  r  r  r%  r)  r*  r+  r,  r-  r.  r1  r3  rA  rH  rM  rN  r  r   r   rG   r   r3   e   s   
	3	>""$#	 /% 		(.

+
"r3   c                 C   sv   | j }| j}|d u s|d u r | S G dd d}G dd d||}G dd d||}dd }|| _ || _|| _d | _| S )	Nc                       s(   e Zd Z fddZ fddZ  ZS )z(add_subclass_tests.<locals>.CommonFuturec                    s&   t dd | _t j|i | d S )Nc                   S   s   dS r   r   r   r   r   r   r@   	  rA   zCadd_subclass_tests.<locals>.CommonFuture.__init__.<locals>.<lambda>)collectionsdefaultdictcallsrB   r   r  rG   r   r   r   	  s    z1add_subclass_tests.<locals>.CommonFuture.__init__c                    s$   | j d  d7  < t j|i |S )Nr   r   )rR  rB   r   r  rG   r   r   r   	  s    z:add_subclass_tests.<locals>.CommonFuture.add_done_callbackr  r   r   rG   r   CommonFuture	  s   rS  c                   @   s   e Zd ZdS )z add_subclass_tests.<locals>.TaskNr'   r(   r)   r   r   r   r   r8   	  s   r8   c                   @   s   e Zd ZdS )z"add_subclass_tests.<locals>.FutureNrT  r   r   r   r   r=   	  s   r=   c                    sv   j jd  fdd}j| jd}j|}|d t|jddi t jddi d S )Nr<   c                      s   j  fdd  I d H S )Nc                      s
     dS NrL  r  r   rg   r   r   r@   	  rA   zYadd_subclass_tests.<locals>.test_subclasses_ctask_cfuture.<locals>.func.<locals>.<lambda>)r5   r   r   rh   r$   r   r   r   	  s    zGadd_subclass_tests.<locals>.test_subclasses_ctask_cfuture.<locals>.funcrL  r   r   )r=   r5   r8   rV   rI   dictrR  )r$   r   rM   rv   r   rV  r   test_subclasses_ctask_cfuture	  s    z9add_subclass_tests.<locals>.test_subclasses_ctask_cfuture)r8   r=   rX  r  )clsZBaseTaskZ
BaseFuturerS  r8   r=   rX  r   r   r   add_subclass_tests	  s    	rZ  c                   @   s   e Zd Zdd Zdd ZdS )SetMethodsTestc                 C   s   t | j}t  | j_}dd }| }| | j|}||d | | j	|d |
  |jd d d }| tjd |W d    n1 s0    Y  |  d S )Nc                      s   t dI d H  dS Nr6  r   r   r   r   r   r   r   	  s    z@SetMethodsTest.test_set_result_causes_invalid_state.<locals>.foorL  r   r  step\(\): already done)r   r=   r   r  r5   call_exception_handlerr:   r   rI   rV   assert_called_oncer  rj   r   InvalidStateErrorr1   )r$   r=   exc_handlerr   r9   rM   r   r   r   r   $test_set_result_causes_invalid_state	  s"    

"z3SetMethodsTest.test_set_result_causes_invalid_statec                 C   s   G dd dt }t| j}t  | j_}dd }| }| | j|}|||  | 	| | j
| W d    n1 s0    Y  |  |jd d d }| tjd |W d    n1 s0    Y  |  d S )Nc                   @   s   e Zd ZdS )zESetMethodsTest.test_set_exception_causes_invalid_state.<locals>.MyExcNrT  r   r   r   r   MyExc
  s   rc  c                      s   t dI d H  dS r\  r   r   r   r   r   r   

  s    zCSetMethodsTest.test_set_exception_causes_invalid_state.<locals>.foor   r  r]  )r   r   r=   r   r  r5   r^  r:   r  rT   rV   r_  r  rj   r   r`  r1   )r$   rc  r=   ra  r   r9   rM   r   r   r   r   'test_set_exception_causes_invalid_state
  s     
*"z6SetMethodsTest.test_set_exception_causes_invalid_stateN)r'   r(   r)   rb  rd  r   r   r   r   r[  	  s   r[  _CFuture_CTaskzrequires the C _asyncio modulec                   @   s:   e Zd ZeeddZeeddZej	dd Z
dd ZdS )CTask_CFuture_Testsrf  Nre  c                 C   s|   t td}dd }| | j| }| j| | }tdD ]"}|j| | jd | j| q>| j| | ddd d S )	Ngettotalrefcountc                      s   d S r
   r   r   r   r   r   r9   *
  s    z@CTask_CFuture_Tests.test_refleaks_in_task___init__.<locals>.cororc   r<   r   r   )Zdelta)	r   Zget_attributer  r:   r5   rV   re   r   r   )r$   rh  r9   rM   Zrefs_beforerF  r   r   r   test_refleaks_in_task___init__'
  s    z2CTask_CFuture_Tests.test_refleaks_in_task___init__c                 C   sV   dd }|  | j| }| j| | t |`W d    n1 sH0    Y  d S )Nc                      s   d S r
   r   r   r   r   r   r9   5
  s    zHCTask_CFuture_Tests.test_del__log_destroy_pending_segfault.<locals>.coro)r:   r5   rV   rT   AttributeErrorr   r<  r   r   r   &test_del__log_destroy_pending_segfault4
  s
    z:CTask_CFuture_Tests.test_del__log_destroy_pending_segfault)r'   r(   r)   getattrr   r8   r   r=   r   Zrefcount_testri  rk  r   r   r   r   rg  
  s
   
rg  c                   @   s$   e Zd ZeeddZeeddZdS )CTask_CFuture_SubclassTestsrf  Nre  )r'   r(   r)   rl  r   r8   r   r=   r   r   r   r   rm  =
  s   rm  c                   @   s   e Zd ZeeddZejZdS )CTaskSubclass_PyFuture_Testsrf  N	r'   r(   r)   rl  r   r8   r   	_PyFuturer=   r   r   r   r   rn  G
  s   rn  c                   @   s   e Zd ZeeddZejZdS )PyTask_CFutureSubclass_Testsre  N)	r'   r(   r)   rl  r   r=   r   _PyTaskr8   r   r   r   r   rq  P
  s   rq  c                   @   s   e Zd ZeeddZejZdS )CTask_PyFuture_Testsrf  Nro  r   r   r   r   rs  Y
  s   rs  c                   @   s   e Zd ZejZeeddZdS )PyTask_CFuture_Testsre  N)	r'   r(   r)   r   rr  r8   rl  r   r=   r   r   r   r   rt  a
  s   rt  c                   @   s   e Zd ZejZejZdS )PyTask_PyFuture_TestsN	r'   r(   r)   r   rr  r8   r   rp  r=   r   r   r   r   ru  i
  s   ru  c                   @   s   e Zd ZejZejZdS )PyTask_PyFuture_SubclassTestsNrv  r   r   r   r   rw  p
  s   rw  c                   @   s   e Zd Zdd ZdS )CTask_Future_Testsc                    s   G dd dt j} fdd}t  | _zF|| jd | jd jd | j| }| j|}W | j  n| j  0 | 	|d d S )	Nc                   @   s   e Zd Zedd ZdS )z+CTask_Future_Tests.test_foobar.<locals>.Futc                 S   s   t d S r
   )rj  r#   r   r   r   ry   |
  s    z4CTask_Future_Tests.test_foobar.<locals>.Fut.get_loopN)r'   r(   r)   propertyry   r   r   r   r   r  {
  s   r  c                      s    I d H  dS rU  r   r   rg   r   r   r9   
  s    
z,CTask_Future_Tests.test_foobar.<locals>.coror<   r6  r   rL  )
r   r=   ri   r5   r  r   r   rV   r1   rI   )r$   r  r9   rM   r  r   rg   r   test_foobarz
  s    
zCTask_Future_Tests.test_foobarN)r'   r(   r)   rz  r   r   r   r   rx  v
  s   rx  c                   @   sl   e Zd ZdZdZd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d Zdd ZdS )BaseTaskIntrospectionTestsNc                    s`   G  fddd}| }t   | t t  | | | t |h | | d S )Nc                       s$   e Zd Ze fddZdd ZdS )zBBaseTaskIntrospectionTests.test__register_task_1.<locals>.TaskLikec                    s    S r
   r   r#   r<   r   r   rx   
  s    zHBaseTaskIntrospectionTests.test__register_task_1.<locals>.TaskLike._loopc                 S   s   dS r  r   r#   r   r   r   ru   
  s    zGBaseTaskIntrospectionTests.test__register_task_1.<locals>.TaskLike.doneN)r'   r(   r)   ry  rx   ru   r   r<   r   r   TaskLike
  s   r|  r   r  rI   r   r   rA  _register_task_unregister_taskr$   r|  rM   r   r<   r   test__register_task_1
  s    
z0BaseTaskIntrospectionTests.test__register_task_1c                    s`   G  fddd}| }t   | t t  | | | t |h | | d S )Nc                       s    e Zd Z fddZdd ZdS )zBBaseTaskIntrospectionTests.test__register_task_2.<locals>.TaskLikec                    s    S r
   r   r#   r<   r   r   ry   
  s    zKBaseTaskIntrospectionTests.test__register_task_2.<locals>.TaskLike.get_loopc                 S   s   dS r  r   r#   r   r   r   ru   
  s    zGBaseTaskIntrospectionTests.test__register_task_2.<locals>.TaskLike.doneNr'   r(   r)   ry   ru   r   r<   r   r   r|  
  s   r|  r}  r  r   r<   r   test__register_task_2
  s    
z0BaseTaskIntrospectionTests.test__register_task_2c                    s`   G  fddd}| }t   | t t  | | | t t  | | d S )Nc                       s    e Zd Z fddZdd ZdS )zBBaseTaskIntrospectionTests.test__register_task_3.<locals>.TaskLikec                    s    S r
   r   r#   r<   r   r   ry   
  s    zKBaseTaskIntrospectionTests.test__register_task_3.<locals>.TaskLike.get_loopc                 S   s   dS r  r   r#   r   r   r   ru   
  s    zGBaseTaskIntrospectionTests.test__register_task_3.<locals>.TaskLike.doneNr  r   r<   r   r   r|  
  s   r|  r}  r  r   r<   r   test__register_task_3
  s    
z0BaseTaskIntrospectionTests.test__register_task_3c                 C   sN   t  }t  }| t| | || | t|| | || d S r
   )r   r  rV  r   r  _enter_taskrw   _leave_taskr$   rM   r5   r   r   r   test__enter_task
  s    z+BaseTaskIntrospectionTests.test__enter_taskc                 C   s|   t  }t  }t  }| || | t | || W d    n1 sP0    Y  | t|| | || d S r
   )	r   r  r  rT   rk   rw   r   r  r  r$   r  r  r5   r   r   r   test__enter_task_failure
  s    *z3BaseTaskIntrospectionTests.test__enter_task_failurec                 C   s<   t  }t  }| || | || | t| d S r
   )r   r  r  r  rV  r   r  r  r   r   r   test__leave_task
  s
    z+BaseTaskIntrospectionTests.test__leave_taskc                 C   s|   t  }t  }t  }| || | t | || W d    n1 sP0    Y  | t|| | || d S r
   )	r   r  r  rT   rk   r  rw   r   r  r  r   r   r   test__leave_task_failure1
  s    *z4BaseTaskIntrospectionTests.test__leave_task_failure1c                 C   sZ   t  }t  }| t | || W d    n1 s<0    Y  | t| d S r
   )r   r  rT   rk   r  rV  r   r  r  r   r   r   test__leave_task_failure2
  s
    *z4BaseTaskIntrospectionTests.test__leave_task_failure2c                    sJ   t  }t    fdd|_| | | | | t t  d S )Nc                      s    S r
   r   r   r<   r   r   r@   
  rA   zBBaseTaskIntrospectionTests.test__unregister_task.<locals>.<lambda>)	r   r  ry   r~  r  rI   r   r   rA  rL   r   r<   r   test__unregister_task
  s    

z0BaseTaskIntrospectionTests.test__unregister_taskc                 C   s2   t  }t  }| | | t|t  d S r
   )r   r  r  rI   r   r   rA  r  r   r   r   $test__unregister_task_not_registered
  s    
z?BaseTaskIntrospectionTests.test__unregister_task_not_registered)r'   r(   r)   r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   r{  
  s   

r{  c                   @   s4   e Zd ZeejZeejZeej	Z
eejZdS )PyIntrospectionTestsN)r'   r(   r)   staticmethodr   Z_py_register_taskr~  Z_py_unregister_taskr  Z_py_enter_taskr  Z_py_leave_taskr  r   r   r   r   r     s   


r  _c_register_taskc                   @   sP   e Zd Zeedr<eejZeejZ	eej
ZeejZnd Z Z	 ZZdS )CIntrospectionTestsr  N)r'   r(   r)   rR   r   r  r  r~  Z_c_unregister_taskr  Z_c_enter_taskr  Z_c_leave_taskr  r   r   r   r   r    s   



r  c                       s<   e Zd Z fddZdd Zdd Zdd Zd	d
 Z  ZS )BaseCurrentLoopTestsc                    s$   t    t | _| | j d S r
   rB   rC   r   ri   r5   rz   r#   rG   r   r   rC     s    

zBaseCurrentLoopTests.setUpc                 C   s   t d S r
   )NotImplementedErrorr   r   r   r   r:     s    zBaseCurrentLoopTests.new_taskc                 C   s   |  tj| jd d S r;   )rV  r   r  r5   r#   r   r   r   !test_current_task_no_running_loop  s    z6BaseCurrentLoopTests.test_current_task_no_running_loopc                 C   s6   |  t t  W d    n1 s(0    Y  d S r
   )rT   rk   r   r  r#   r   r   r   *test_current_task_no_running_loop_implicit   s    z?BaseCurrentLoopTests.test_current_task_no_running_loop_implicitc                    s>    fdd}  |  j  tj jd d S )Nc                      s<     tj jd   td    t  d S r;   )rw   r   r  r5   r   rL   r   r   r9   %  s    zGBaseCurrentLoopTests.test_current_task_with_implicit_loop.<locals>.coror<   )r:   r5   rV   rV  r   r  r   r   rL   r   $test_current_task_with_implicit_loop$  s    z9BaseCurrentLoopTests.test_current_task_with_implicit_loop)	r'   r(   r)   rC   r:   r  r  r  r  r   r   rG   r   r    s
   r  c                   @   s   e Zd Zdd ZdS )PyCurrentLoopTestsc                 C   s   t j|| jdS r;   )r   rr  r5   r   r   r   r   r:   2  s    zPyCurrentLoopTests.new_taskNr'   r(   r)   r:   r   r   r   r   r  0  s   r  c                   @   s   e Zd Zdd ZdS )CCurrentLoopTestsc                 C   s   t td|| jdS )Nrf  r<   )rl  r   r5   r   r   r   r   r:   :  s    zCCurrentLoopTests.new_taskNr  r   r   r   r   r  6  s   r  c                   @   s"   e Zd Zdd Zejdd ZdS )GenericTaskTestsc                 C   s   |  ttjtj d S r
   )rQ   
issubclassr   r8   r=   r#   r   r   r   test_future_subclass@  s    z%GenericTaskTests.test_future_subclassc                 C   sh   zdd l }dd l}dd l}W n ty8   | d Y n,0 zdd l}W n tyb   | d Y n0 d S )Nr   zC modules are not availablez_asyncio module is missing)
_functools_json_pickleImportErrorZskipTest_asyncior2  )r$   r  r  r  r  r   r   r   test_asyncio_module_compiledC  s    z-GenericTaskTests.test_asyncio_module_compiledN)r'   r(   r)   r  r   Zcpython_onlyr  r   r   r   r   r  >  s   r  c                       sT   e Zd Z fddZdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
  ZS )GatherTestsBasec                    s2   t    |  | _|  | _| j| jdd d S )NFcleanup)rB   rC   rD   one_looprl   rz   r#   rG   r   r   rC   X  s    


zGatherTestsBase.setUpc                 C   s   |j rt| q d S r
   )r  r   r   r>   r   r   r   	_run_loop^  s    zGatherTestsBase._run_loopc                    s    fddt dD \}}}tj |||i |}t }|| |d |d   j	  
|jd  |  |d   j	 ||  
| g d d S )Nc                    s   g | ]} j  qS r   r  rF   r`   rF  r#   r   r   rb   c  rA   z2GatherTestsBase._check_success.<locals>.<listcomp>r   r   r   F)r   r   r   )re   r   rd   wrap_futuresr   MockCallbackr   r   r  r  rI   r  r   ru   assert_called_once_withrv   )r$   r  r   r   rP  rh   cbr   r#   r   _check_successb  s    




zGatherTestsBase._check_successc                 C   s   |    | j dd d S )NFr   r  r#   r   r   r   test_successq  s    zGatherTestsBase.test_successc                 C   s   | j dd d S )NTr  r  r#   r   r   r   test_result_exception_successu  s    z-GatherTestsBase.test_result_exception_successc           	         s    fddt dD \}}}}}tj ||||| }t }|| t }|d |	|  
 j  |  ||  | | |d |  |	t  |  d S )Nc                    s   g | ]} j  qS r   r  r  r#   r   r   rb   y  rA   z6GatherTestsBase.test_one_exception.<locals>.<listcomp>r   r   r   )re   r   rd   r  r   r  r   r]  r   r  r  r  rQ   ru   r  rw   r  rS   rk   )	r$   r   r   rP  r  r  rh   r  r   r   r#   r   test_one_exceptionx  s     




z"GatherTestsBase.test_one_exceptionc           	         s    fddt dD \}}}}tj ||||ddi}t }|| t }t }|	d |
| |	d   j  |  |
|   j  |  ||  | dd||g d S )Nc                    s   g | ]} j  qS r   r  r  r#   r   r   rb     rA   z:GatherTestsBase.test_return_exceptions.<locals>.<listcomp>   r   Tr   r   )re   r   rd   r  r   r  r   r]  rk   r   r  r  r  r   ru   rQ   r  rI   rv   )	r$   r   r   rP  r  rh   r  r   Zexc2r   r#   r   test_return_exceptions  s$    





z&GatherTestsBase.test_return_exceptionsc                 C   s   d d}tdd|\}}}| | d td|ddd\}}}| | d td|ddd\}}}| | d	 tdd|ddd\}}}| | d tdd
dd|\}}}| | d	 d S )N
)zimport asyncio.coroutinesz print(asyncio.coroutines._DEBUG)z-E-cs   False )PYTHONASYNCIODEBUGZPYTHONDEVMODE1s   Truez-Xdev)joinr	   rI   rstrip)r$   r   stsstdoutstderrr   r   r   test_env_var_debug  s,    

z"GatherTestsBase.test_env_var_debug)r'   r(   r)   rC   r  r  r  r  r  r  r  r  r   r   rG   r   r  V  s   r  c                   @   sD   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S )FutureGatherTestsc                 G   s   |S r
   r   )r$   r   r   r   r   r    s    zFutureGatherTests.wrap_futuresc                 C   s   t | j | t jd  t j| }| |t j | |j| j | 	| j | 
|  | | g  | t" t j|d| ji}W d    n1 s0    Y  | |j| j d S )Nr5   )r   rz   r  r   rd   rK   r=   rw   rx   r  rQ   ru   rI   rv   r}   r~   rl   )r$   Zseq_or_iterrh   r   r   r   _check_empty_sequence  s    
0z'FutureGatherTests._check_empty_sequencec                 C   s2   |  g  |  d |  t  |  td d S )Nr   r  )r  rA  rm  r#   r   r   r   test_constructor_empty_sequence  s    

z1FutureGatherTests.test_constructor_empty_sequencec              	   C   s   | j  }| j }| t t|| W d    n1 s@0    Y  | tJ | t  tj|| jd W d    n1 s0    Y  W d    n1 s0    Y  d S r;   )	r  rF   rl   rT   r   r   rd   r}   r~   )r$   r  r  r   r   r   %test_constructor_heterogenous_futures  s    

*z7FutureGatherTests.test_constructor_heterogenous_futuresc                    s    fddt dD }tj| } |j j   j  |   	t
" tj|d ji}W d    n1 s|0    Y   |j j   j  |  d S )Nc                    s   g | ]} j  qS r   )rl   rF   r  r#   r   r   rb     rA   zIFutureGatherTests.test_constructor_homogenous_futures.<locals>.<listcomp>r   r5   )re   r   rd   rw   rx   rl   r  r   ru   r}   r~   )r$   Zchildrenrh   r   r#   r   #test_constructor_homogenous_futures  s    
0z5FutureGatherTests.test_constructor_homogenous_futuresc                    s    fddt dD \}}}}}t|||||}t }|| |d |    j	  
|  ||  |   | tj |d |  |t  |  d S )Nc                    s   g | ]} j  qS r   r  r  r#   r   r   rb     rA   z;FutureGatherTests.test_one_cancellation.<locals>.<listcomp>r   r   r   )re   r   rd   r   r  r   r   rS   r  r  rQ   ru   r  r   r   rK   r  rU   r  rk   )r$   r   r   rP  r  r  rh   r  r   r#   r   test_one_cancellation  s     



z'FutureGatherTests.test_one_cancellationc              	      s   fddt dD \}}}}}}tj||||||dd}t }|| |d t }	||	 |	   
 j  |  |d |	  t }
||
  j|} |d tj  |d	 tj d  |d< |d	<  |d|	d dd |
g || d S )
Nc                    s   g | ]} j  qS r   r  r  r#   r   r   rb     s   zLFutureGatherTests.test_result_exception_one_cancellation.<locals>.<listcomp>r   Tr  r   r   r   r  )re   r   rd   r   r  r   r   r]  r  rS   r  r  r   ru   rk   rV   rK   rU   rI   r  )r$   r   r   rP  r  r  r   rh   r  ZzdeZrter  r   r#   r   &test_result_exception_one_cancellation  s,    





z8FutureGatherTests.test_result_exception_one_cancellationN)
r'   r(   r)   r  r  r  r  r  r  r  r   r   r   r   r    s   	r  c                       sT   e Zd Z fddZdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
  ZS )CoroutineGatherTestsc                    s   t    t| j d S r
   )rB   rC   r   rz   r  r#   rG   r   r   rC     s    
zCoroutineGatherTests.setUpc                 G   s*   g }|D ]}|fdd}| |  q|S )Nc                    s
   | I d H S r
   r   rg   r   r   r   r9     s    z/CoroutineGatherTests.wrap_futures.<locals>.coro)ru  )r$   r   Zcorosrh   r9   r   r   r   r    s
    z!CoroutineGatherTests.wrap_futuresc                    s    fdd}| j | S )Nc                      s   t j i S r
   r   rd   r   r   r  r   r   r9   "  s    z*CoroutineGatherTests._gather.<locals>.coro)r  rV   )r$   r   r  r9   r   r  r   _gather!  s    zCoroutineGatherTests._gatherc                 C   s   dd }| }| }t ||}| |j| j | j| | j| jdd | }| }| t	" t j||| jd}W d    n1 s0    Y  | |j| j | j| d S )Nc                      s   dS r   r   r   r   r   r   r9   '  s    zBCoroutineGatherTests.test_constructor_loop_selection.<locals>.coroFr  r<   )
r   rd   rw   rx   r  rV   rz   rl   r}   r~   )r$   r9   Zgen1Zgen2rh   Zgen3Zgen4r  r   r   r   test_constructor_loop_selection&  s    0z4CoroutineGatherTests.test_constructor_loop_selectionc                 C   s   |  t tjdd }W d    n1 s.0    Y  |d}|  t* tj|||d|| jd}W d    n1 sz0    Y  | | j | | g d d S )Nc                 S   s   | S r
   r   rJ  r   r   r   r9   9  s    z<CoroutineGatherTests.test_duplicate_coroutines.<locals>.coror   defr<   )r   r   r  r   )	r}   r~   r   r   rd   r  r  rI   rv   )r$   r9   rP  rh   r   r   r   test_duplicate_coroutines7  s    (8z.CoroutineGatherTests.test_duplicate_coroutinesc                    s  d| j  fdd}tj| | j d tj| | j dd  fdd}tj| | j d}t| j  | |  | tj	 | j 
| W d    n1 s0    Y  |   |   |    |   t| j  | d d S )Nr   c                      s   I d H   d7  d S r  r   r   r  r   r   r  G  s    
z?CoroutineGatherTests.test_cancellation_broadcast.<locals>.innerr<   c                      s"   t  I d H  d7 d S r  r  r   )r  r  gathererr  r   r   r   P  s    
z?CoroutineGatherTests.test_cancellation_broadcast.<locals>.outer)r  rF   r   rp   r   r   rQ   rS   rT   rU   rV   r   r   rI   )r$   r  r   r   r   )r  r  r  r  r  r   test_cancellation_broadcastB  s$    
*z0CoroutineGatherTests.test_cancellation_broadcastc                    s   dd | j   | j   fdd}tj| | j d}t| j   d  t| j  d  t| j  | | t	 d S )Nc                    s   | I d H  t dd S )Nzshould not be ignoredr(  r  r   r   r   r  e  s    
z:CoroutineGatherTests.test_exception_marking.<locals>.innerc                      s   t  I d H  d S r
   r  r   r   r   r  r   r   r   l  s    z:CoroutineGatherTests.test_exception_marking.<locals>.outerr<   )
r  rF   r   rp   r   r   r   rK   r  rk   r  r   r  r   test_exception_markingb  s    



z+CoroutineGatherTests.test_exception_markingc              	   C   s   t dn dd }|d}| t | |i  W d    n1 sH0    Y  | | j | jj  W d    n1 s~0    Y  d S )Nz8asyncio.base_events.BaseEventLoop.call_exception_handlerc                    s   | S r
   r   rJ  r   r   r   r9   {  s    z2CoroutineGatherTests.test_issue46672.<locals>.coror   )	r   rO  rT   r   r  r  r  r^  Zassert_not_called)r$   r9   rP  r   r   r   test_issue46672w  s    *z$CoroutineGatherTests.test_issue46672)r'   r(   r)   rC   r  r  r  r  r  r  r  r  r   r   rG   r   r    s    r  c                       s\   e Zd ZdZ fddZdddZddd	Zd
d Zdd Zdd Z	dd Z
dd Z  ZS )RunCoroutineThreadsafeTestsz/Test case for asyncio.run_coroutine_threadsafe.c                    s$   t    t | _| | j d S r
   r  r#   rG   r   r   rC     s    

z!RunCoroutineThreadsafeTests.setUpFc                    sH   t dI dH  |rtd|r@t | j  t dI dH  || S )z"Wait 0.05 second and return a + b.r  NFail!r   )r   r   rk   r  r5   rS   )r$   r   r   r2  rS   r   r   r   rp    s    zRunCoroutineThreadsafeTests.addNc                 C   sl   | j dd||d}t|| j}|r4| j|jd z ||W | pN|  S  n| pd|  0 dS )z$Run add coroutine in the event loop.r   r   )r2  rS   N)	rp  r   Zrun_coroutine_threadsafer5   Zcall_soon_threadsafer/   rv   ru   rS   )r$   r2  rS   rj  advance_coror9   r  r   r   r   target  s    
z"RunCoroutineThreadsafeTests.targetc                 C   s,   | j d| j}| j |}| |d dS )z9Test coroutine submission from a thread to an event loop.Nr   )r5   run_in_executorr  rV   rI   )r$   r  rv   r   r   r   test_run_coroutine_threadsafe  s    z9RunCoroutineThreadsafeTests.test_run_coroutine_threadsafec                 C   s\   | j d| jd}| t}| j | W d   n1 s>0    Y  | d|jj dS )z]Test coroutine submission from a thread to an event loop
        when an exception is raised.NTr  )	r5   r  r  rT   rk   rV   r#  r  r   )r$   r  exc_contextr   r   r   ,test_run_coroutine_threadsafe_with_exception  s    *zHRunCoroutineThreadsafeTests.test_run_coroutine_threadsafe_with_exceptionc                    s    fdd} j d|} tj  j | W d   n1 sH0    Y  t j  t j D ]} 	|
  qjdS )zZTest coroutine submission from a thread to an event loop
        when a timeout is raised.c                      s    j ddS )Nr   ri  r  r   r#   r   r   r@     rA   zXRunCoroutineThreadsafeTests.test_run_coroutine_threadsafe_with_timeout.<locals>.<lambda>N)r5   r  rT   r   r}  rV   r   r   r   rQ   ru   )r$   r  r  rM   r   r#   r   *test_run_coroutine_threadsafe_with_timeout  s    *zFRunCoroutineThreadsafeTests.test_run_coroutine_threadsafe_with_timeoutc                    sV    fdd} j d|} tj  j | W d   n1 sH0    Y  dS )z\Test coroutine submission from a thread to an event loop
        when the task is cancelled.c                      s    j ddS )NTr  r  r   r#   r   r   r@     rA   zZRunCoroutineThreadsafeTests.test_run_coroutine_threadsafe_task_cancelled.<locals>.<lambda>N)r5   r  rT   r   rU   rV   )r$   r  r  r   r#   r   ,test_run_coroutine_threadsafe_task_cancelled  s    zHRunCoroutineThreadsafeTests.test_run_coroutine_threadsafe_task_cancelledc                    s   dd } j d fdd}t } j |   j j j    j |  t	} j 
| W d   n1 s0    Y   t|jd |j\\}}} |d |j dS )zjTest coroutine submission from a thread to an event loop
        when the task factory raise an exception.c                 S   s   t d S r
   )	NameError)r5   r9   r   r   r   task_factory  s    zfRunCoroutineThreadsafeTests.test_run_coroutine_threadsafe_task_factory_exception.<locals>.task_factoryNc                      s    j ddS )NT)r  r  r   r#   r   r   r@     rA   zbRunCoroutineThreadsafeTests.test_run_coroutine_threadsafe_task_factory_exception.<locals>.<lambda>r   r  )r5   r  r   r  r  r   rE   Zget_task_factoryrT   r  rV   rI   re  Zcall_args_listr  r  )r$   r  r_   r  r  r5   r    r  r   r#   r   4test_run_coroutine_threadsafe_task_factory_exception  s    
*zPRunCoroutineThreadsafeTests.test_run_coroutine_threadsafe_task_factory_exception)FF)FFNF)r'   r(   r)   __doc__rC   rp  r  r  r  r  r  r  r  r   r   rG   r   r    s   

  
r  c                       s8   e Zd Z fddZ fddZdd Zdd Z  ZS )	
SleepTestsc                    s$   t    t | _| | j d S r
   r  r#   rG   r   r   rC     s    

zSleepTests.setUpc                    s   | j   d | _ t   d S r
   r5   r1   rB   tearDownr#   rG   r   r   r    s    
zSleepTests.tearDownc                    s>   dfdd  fdd}j |  d d S )Nr   c                    s    | 7  d S r
   r   rK  rM  r   r   
inc_result  s    z.SleepTests.test_sleep_zero.<locals>.inc_resultc                     sF   j  d d tjdddI d H } d  |  d S )Nr   r   r   rM  )r5   r   rI   r   r   r  r  rv   r$   r   r   r9     s
    z(SleepTests.test_sleep_zero.<locals>.coro   )r5   rV   rI   r   r   r  r   test_sleep_zero  s
    zSleepTests.test_sleep_zeroc                 C   sF   |  t( | jtjd| jd W d    n1 s80    Y  d S NrG  r<   )r}   r~   r5   rV   r   r   r#   r   r   r    test_loop_argument_is_deprecated  s    z+SleepTests.test_loop_argument_is_deprecated)r'   r(   r)   rC   r  r  r  r  r   r   rG   r   r    s   r  c                       s@   e Zd Z fddZ fddZdd Zdd Zd	d
 Z  ZS )	WaitTestsc                    s$   t    t | _| | j d S r
   r  r#   rG   r   r   rC     s    

zWaitTests.setUpc                    s   | j   d | _ t   d S r
   r  r#   rG   r   r   r    s    
zWaitTests.tearDownc                 C   sJ   |  t, | jtjt g| jd W d    n1 s<0    Y  d S r;   )r}   r~   r5   rV   r   r   r   r#   r   r   r   (test_loop_argument_is_deprecated_in_wait  s    z2WaitTests.test_loop_argument_is_deprecated_in_waitc                 C   sJ   |  t, | jtjt d| jd W d    n1 s<0    Y  d S r  )r}   r~   r5   rV   r   r   r   r#   r   r   r   ,test_loop_argument_is_deprecated_in_wait_for  s    z6WaitTests.test_loop_argument_is_deprecated_in_wait_forc                 C   s   |  t& | jtt g W d    n1 s60    Y  | jt }|  t( | jt|t g W d    n1 s0    Y  d S r
   )r}   r~   r5   rV   r   r   r   r   rL   r   r   r   test_coro_is_deprecated_in_wait#  s    "z)WaitTests.test_coro_is_deprecated_in_wait)	r'   r(   r)   rC   r  r  r  r  r  r   r   rG   r   r    s
   r  c                       s@   e Zd Z fddZ fddZdd Zdd Zd	d
 Z  ZS )CompatibilityTestsc                    s$   t    t | _| | j d S r
   r  r#   rG   r   r   rC   3  s    

zCompatibilityTests.setUpc                    s   | j   d | _ t   d S r
   r  r#   rG   r   r   r  8  s    
zCompatibilityTests.tearDownc                 C   sV   |  t tjdd }W d    n1 s.0    Y  | j| }| d| d S )Nc                   s   s   t dE d H  dS )Nr   rs   r   r   r   r   r   r9   @  s    z:CompatibilityTests.test_yield_from_awaitable.<locals>.corors   r  )r$   r9   rv   r   r   r   test_yield_from_awaitable=  s
    (z,CompatibilityTests.test_yield_from_awaitablec                    s   |  t tjdd  W d    n1 s.0    Y  |  t tjdd W d    n1 sf0    Y   fdd}| j| }| ddg| d S )	Nc                   S   s   dS )Nok1r   r   r   r   r   rY  K  s    z;CompatibilityTests.test_await_old_style_coro.<locals>.coro1c                   s   s   t dE d H  dS )Nr   ok2r   r   r   r   r   rZ  P  s    z;CompatibilityTests.test_await_old_style_coro.<locals>.coro2c                      s   t    I d H S r
   r  r   rY  rZ  r   r   r  U  s    z;CompatibilityTests.test_await_old_style_coro.<locals>.innerr  r  r  )r$   r  rv   r   r  r   test_await_old_style_coroH  s    ((z,CompatibilityTests.test_await_old_style_coroc                 C   s   t d}tdd|dd d S )Nz
            import asyncio

            async def native_coro():
                pass

            @asyncio.coroutine
            def old_style_coro():
                yield from native_coro()

            asyncio.run(old_style_coro())
        z-Wignore::DeprecationWarningr  r  )r  )textwrapdedentr	   )r$   r   r   r   r   test_debug_mode_interop[  s    
z*CompatibilityTests.test_debug_mode_interop)	r'   r(   r)   rC   r  r  r  r  r  r   r   rG   r   r  /  s
   r  __main__)F)Cr  rP  
contextlibr>  r   r[   r   rI  r   r  r  r  r0  Zunittestr  r   r   r   r   r   r   Ztest.test_asyncior   r   ro   r   Ztest.support.script_helperr	   r   r   contextmanagerr   r   r!   r"   r*   r3   rZ  r[  Z
skipUnlessrR   ZTestCaserg  rm  rn  rq  rs  rt  ru  rw  rx  r{  r  r  r  r  r  r  r  r  r  r  r  r  r  r'   r"  r   r   r   r   <module>   s   

	                  \:5p
fWsd"#?
