U
    g                     @  s  U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
mZ d dlmZmZmZmZmZmZmZmZmZmZ d dlZd dlZddlmZmZmZmZmZmZm Z m!Z! ddl"m#Z# ddl$m%Z% dd	l&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- dd
l.m/Z/ er d dl0m1Z1 ddl2m3Z3 eee4eej5ee6 f f  Z7edZ8ddddZ9ddddZ:ddddZ;ddddZ<ddddZ=dpdddddZ>dddd Z?ddd!d"Z@ddd#d$ZAddd%d&ZBd'd(dd)d*d+ZCejDEd,d-d.d/gejDEd0d1d2gejDEd3d1d2gd4d5d5dd6d7d8ZFddd9d:ZGddd;d<ZHd(dd=d>d?ZIddd@dAZJdddBdCZKeLdDZMdEeNdD< dddFdGZOdddHdIZPdddJdKZQdddLdMZRdddNdOZSdddPdQZTdddRdSZUdddTdUZVdddVdWZWdddXdYZXdddZd[ZYddd\d]ZZddd^d_Z[ddd`daZ\dddbdcZ]dddddeZ^dddfdgZ_dddhdiZ`e%dddjdkZadddldmZbdddndoZcdS )q    )annotationsNpartial)
TYPE_CHECKINGAsyncGenerator	AwaitableCallableListNoReturnTupleTypeTypeVarUnion   )CancelScopeCapacityLimiterEvent_core
fail_aftermove_on_aftersleepsleep_forever)ki_self)slow)active_thread_countcurrent_default_thread_limiterfrom_thread_check_cancelledfrom_thread_runfrom_thread_run_syncto_thread_run_syncwait_all_threads_completed)wait_all_tasks_blocked)Outcome)TaskTNonereturnc                    s   t   ddddddd fdd} t }d	d
ddd}| t|d|dI d H  d	dddd}| t|dtf|dI d H  d	d
ddd}| t|d|dI d H  d	dddd}| t|dtf|dI d H  d S )NzCallable[..., threading.Thread]zCallable[..., T | Awaitable[T]]ztuple[str, T]_core.TrioToken | Noner%   )do_in_trio_threadfnexpected
trio_tokenr'   c                   sp   g dd fdd}t j|dd}|  | rRtd tdI d H  q2d	|fd
f|gksltd S )Nr%   r&   c               
     st   z2 dt f  d}  d| f W n< tk
rn } zt|  dt|f W 5 d }~X Y nX d S )Nstartr,   goterror)append	threadingcurrent_threadBaseExceptionprinttype)xexcr)   r*   recordr,    </tmp/pip-unpacked-wheel-ks04xdmi/trio/_tests/test_threads.pythreadfnF   s    z<test_do_in_trio_thread.<locals>.check_case.<locals>.threadfnT)targetdaemonZyawn{Gz?r-   f)r2   Threadr-   is_aliver5   r   AssertionError)r)   r*   r+   r,   r=   child_threadtrio_threadr9   r<   
check_case>   s    	z*test_do_in_trio_thread.<locals>.check_case
RecordTypeint)r:   r'   c                 S  s"   t  rt| dt f dS )NrA   r   )r   currently_ki_protectedrD   r1   r2   r3   r:   r;   r;   r<   f1X   s    z"test_do_in_trio_thread.<locals>.f1)r/   r   r.   r
   c                 S  s&   t  rt| dt f td S NrA   )r   rK   rD   r1   r2   r3   
ValueErrorrL   r;   r;   r<   f2_   s    z"test_do_in_trio_thread.<locals>.f2r0   c                   s0   t  rtt  I d H  | dt f dS )NrA      )r   rK   rD   
checkpointr1   r2   r3   rL   r;   r;   r<   f3f   s    z"test_do_in_trio_thread.<locals>.f3)r/   rQ   c                   s4   t  rtt  I d H  | dt f td S rN   )r   rK   rD   rR   r1   r2   r3   KeyErrorrL   r;   r;   r<   f4n   s    z"test_do_in_trio_thread.<locals>.f4)N)r2   r3   r   current_trio_tokenr   rO   r   rT   )rH   tokenrM   rP   rS   rU   r;   rF   r<   test_do_in_trio_thread;   s     rX   c               	     sR   t t tdd  W 5 Q R X dddd} t t t|  W 5 Q R X d S )Nc                   S  s   d S Nr;   r;   r;   r;   r<   <lambda>y       z9test_do_in_trio_thread_from_trio_thread.<locals>.<lambda>r%   r&   c                     s   d S rY   r;   r;   r;   r;   r<   foo{   s    z4test_do_in_trio_thread_from_trio_thread.<locals>.foo)pytestraisesRuntimeErrorr   r   )r\   r;   r;   r<   'test_do_in_trio_thread_from_trio_threadw   s
    r`   c                    s6   t   dd fdd} t|   ddhks2td S )Nr%   r&   c                    s   t   ddddddfdddd fdd} tj| d	}|  td
 | rttdI d H  q\td |  td d S )Nr%   r&   c               
   S  sD   t d t rtt d z
t  W 5 dd l} t d|   X d S )Nzin Trio threadr   r   finally)r5   r   rK   rD   sysexc_infor   )rb   r;   r;   r<   trio_thread_fn   s    
zTtest_run_in_trio_thread_ki.<locals>.check_run_in_trio_thread.<locals>.trio_thread_fnc                     s
      d S rY   r;   r;   )rd   r;   r<   trio_thread_afn   s    zUtest_run_in_trio_thread_ki.<locals>.check_run_in_trio_thread.<locals>.trio_thread_afnc                     s|   zt d td W n& tk
r>   t d  d Y nX ztd W n& tk
rv   t d  d Y nX d S )Nrunningr.   ok1ok2)r5   r   KeyboardInterruptaddr   r;   )r:   rW   re   rd   r;   r<   external_thread_fn   s    zXtest_run_in_trio_thread_ki.<locals>.check_run_in_trio_thread.<locals>.external_thread_fn)r>   Zwaitingr@   zwaited, joiningdone)	r   rV   r2   rB   r-   r5   rC   r   join)rk   threadrL   )rW   re   rd   r<   check_run_in_trio_thread   s    z<test_run_in_trio_thread_ki.<locals>.check_run_in_trio_threadrg   rh   )setr   runrD   )ro   r;   rL   r<   test_run_in_trio_thread_ki   s    '
rr   c                    sp   g t   dd fdddddfddd	d fd
d} t| }|  ddgksltd S )Nr%   r&   c                     s*    d    tdd I d H  d S )Nsleepingc                 S  s   t jjS rY   )r   ZAbortZ	SUCCEEDED_r;   r;   r<   rZ      r[   zMtest_await_in_trio_thread_while_main_exits.<locals>.trio_fn.<locals>.<lambda>)r1   rp   r   Zwait_task_rescheduledr;   )evr:   r;   r<   trio_fn   s    
z;test_await_in_trio_thread_while_main_exits.<locals>.trio_fn_core.TrioTokenrW   r'   c                   s6   zt | d W n  tjk
r0    d Y nX d S )Nr.   	cancelled)r   r   	Cancelledr1   rW   )r:   rw   r;   r<   	thread_fn   s    z=test_await_in_trio_thread_while_main_exits.<locals>.thread_fnthreading.Threadc                    s@   t  } tj| fd}|    I d H  dgks<t|S )N)r>   argsrs   )r   rV   r2   rB   r-   waitrD   )rW   rn   )rv   r:   r}   r;   r<   main   s    z8test_await_in_trio_thread_while_main_exits.<locals>.mainrs   rz   )r   r   rq   rm   rD   )r   rn   r;   )rv   r:   r}   rw   r<   *test_await_in_trio_thread_while_main_exits   s    
r   c                    s   d} d|  fddddddddfd	d
 t I d H  t d dI d H  t  d|  I d H  ddd fdd}|dI d H  |dI d H  |dI d H  |dI d H  d S )Nz0 from trio._tests.test_threads.test_named_threadinnerstrr~   namer'   c                 S  s   t  j| kstt  S rY   )r2   r3   r   rD   r   r;   r;   r<   r      s    z test_named_thread.<locals>.inner"Callable[[None], threading.Thread]c                   s
   t  | S rY   r   r   r   r;   r<   rA      s    ztest_named_thread.<locals>.fZthread_namer%   c                   s,   t  | | dI d H }td|js(td S )Nr   Trio thread [0-9]*)r   rematchr   rD   )r   rn   rA   r;   r<   test_thread_name   s    z+test_named_thread.<locals>.test_thread_name fobiedooname_longer_than_15_characters   💙r   )Zendingr   r;   rA   r   r<   test_named_thread   s    r   z
int | None
str | None)identr'   c              
   C  s  dd l }dd l}|jd}|s$d}z||}W n& tk
rX   tdtj  Y d S X t	|dd }|snt
|j|j|jg|_|j|_|jddd}| d krt } || |ddkst
z|j W S  tk
r } ztd	|jd
|  W 5 d }~X Y nX d S )Nr   Zpthreadzlibc.sozno pthread on pthread_getname_npr[      )sizezvalue: z, exception: )ctypesZctypes.utilutilZfind_libraryCDLL	Exceptionr5   rb   platformgetattrrD   c_void_pc_char_pc_size_targtypesc_intrestypecreate_string_bufferr2   	get_identvaluedecodeUnicodeDecodeErrorr]   Zfail)r   r   Zlibpthread_pathZ
libpthreadr   Zname_bufferer;   r;   r<   _get_thread_name   s2    r   c                    s   ddddddddfdd d	} t  | I d H  t  | d d
I d H  ddddd fdd}|dI d H  |dI d H  |dI d H  |dddI d H  d S )Nr   r~   r   c                 S  sH   t  }|d kr,tjdkr,tdtj  n|| d d ks@tt S )Nlinuxzno pthread OS support on    )r   rb   r   r]   skiprD   r2   r3   )r   os_thread_namer;   r;   r<   r     s
    z#test_named_thread_os.<locals>.innerr   c                   s
   t  | S rY   r   r   r   r;   r<   rA   #  s    ztest_named_thread_os.<locals>.fz4None from trio._tests.test_threads.test_named_threadr   r   r%   )r   r+   r'   c                   sP   |d kr| }t  || dI d H }t|j}|d k	s<tdtd|sLtd S )Nr   z'should skip earlier if this is the caser   )r   r   r   rD   r   r   )r   r+   rn   r   r   r;   r<   r   ,  s    
z.test_named_thread_os.<locals>.test_thread_namer   r   r   r   ?)r+   )Nr   )defaultr   r;   r   r<   test_named_thread_os  s    		r   c                    s>   ddl m}  |  }|d kr:tjdks(ttdtj  d S )Nr   )get_os_thread_name_funcr   zno pthread_setname_np on )Ztrio._core._thread_cacher   rb   r   rD   r]   r   )r   kr;   r;   r<   test_has_pthread_setname_np<  s
    r   c               	     s   t  } ddddd}t|dI d H \}}|dks8t|| ksDtddd	d
}tjtdd}t|I d H  W 5 Q R X t|jj	 |jj	d | kstd S )Nr$   ztuple[T, threading.Thread])r7   r'   c                 S  s   | t  fS rY   )r2   r3   )r7   r;   r;   r<   rA   H  s    z$test_run_in_worker_thread.<locals>.f   r
   r&   c                   S  s   t t d S rY   )rO   r2   r3   r;   r;   r;   r<   gO  s    z$test_run_in_worker_thread.<locals>.gz1^<Thread\(Trio thread \d+, started daemon \d+\)>$r   r   )
r2   r3   r   rD   r]   r^   rO   r5   r   r   )rG   rA   r7   rE   r   excinfor;   r;   r<   test_run_in_worker_threadE  s    r   c               
     s  d gdddfdd dddd fd	d
} g t  }t 4 I d H 8}|| |d t I d H  dgksxt|j  W 5 Q I d H R X d dkst|	d  d dkrt
d qg d d< t 4 I d H z}|| |d t I d H  |j  tjdd$ tdD ]}t I d H  qW 5 Q R X dgksLt|	d  W 5 Q I d H R X t }|  | |dI d H  W 5 Q R X |jstd S )Nzstdlib_queue.Queue[str]r%   )qr'   c                   s   d d< |    d d< d S )Nblockingr   finished)getr   )registerr;   r<   rA   ^  s    z1test_run_in_worker_thread_cancellation.<locals>.fzstdlib_queue.Queue[None]bool)r   abandon_on_cancelr'   c              	     s4    d zt | |dI d H W S  d X d S )Nr-   exitr   )r1   r   )r   r   rA   r:   r;   r<   childd  s    
z5test_run_in_worker_thread_cancellation.<locals>.childTr-   r   r   r@   FZshield
   )stdlib_queueQueuer   open_nursery
start_soonr!   rD   cancel_scopecancelputtimer   r   rangerR   cancelled_caught)r   r   nurseryru   scoper;   )rA   r:   r   r<   &test_run_in_worker_thread_cancellation[  s:    


r   zpytest.CaptureFixture[str]zpytest.MonkeyPatch)capfdmonkeypatchr'   c                   s   | tjdd t  t dd fddddfdd}t|  d   }| rvt	
d qb|  \}}d	|kstd	|kstd S )
NZIDLE_TIMEOUTr@   r%   r&   c                     s       t  d S rY   )r   r   r2   r3   r;   )q1q2r;   r<   r}     s    z6test_run_in_worker_thread_abandoned.<locals>.thread_fnc               
     sX   dd fdd} t  4 I d H &}||  t I d H  |j  W 5 Q I d H R X d S )Nr%   r&   c                     s   t  ddI d H  d S )NTr   r   r;   r}   r;   r<   r     s    z@test_run_in_worker_thread_abandoned.<locals>.main.<locals>.child)r   r   r   r!   r   r   )r   r   r   r;   r<   r     s
    
z1test_run_in_worker_thread_abandoned.<locals>.mainzException in thread)setattrr   _thread_cacher   r   rq   r   r   rC   r   r   Z
readouterrrD   )r   r   r   rn   outerrr;   )r   r   r}   r<   #test_run_in_worker_thread_abandoned  s    	

r   MAXrQ      r   r   FTuse_default_limiterrJ   r   )r   r   r   r'   c           
   
     s  d|  }t  t  |r4t }|j}| |_d nt| }| }|z~G dd dd_d_d_d_	t
 dddfdd	d
dd fdd}t
 4 I d H }td g }t|D ],}	|t  |||d  t I d H  qΈ r(td |d  I d H  t I d H  j	| ksD| j| krVtdI d H  q(  W 5 Q I d H R X j| ks~t r|jdkrtdI d H  qj|kstjdkstW 5 ||_X d S )Nr   c                   @  s.   e Zd ZU ded< ded< ded< ded< dS )z0test_run_in_worker_thread_limiter.<locals>.staterJ   ran
high_waterrf   parkedN)__name__
__module____qualname____annotations__r;   r;   r;   r<   state  s   
r   r   r   r%   )r   r'   c              	     s   t d t| jd @  jd7  _ jd7  _tjj_ jd7  _W 5 Q R X    "  jd8  _ jd8  _W 5 Q R X t d d S )Nzthread_fn startr.   r   zthread_fn exiting)	r5   r   r   r   rf   maxr   r   r   r   )gatelockr   rW   r;   r<   r}     s    z4test_run_in_worker_thread_limiter.<locals>.thread_fnr   )eventr'   c              	     sB   t  }t| dI d H  W 5 Q R X td|j |   d S )N)r   limiterzrun_thread finished, cancelled:)r   r   r   r5   r   rp   )r   r   )r   limiter_argr}   r;   r<   
run_thread  s    
z5test_run_in_worker_thread_limiter.<locals>.run_threadZspawningz'waiting for first cancellation to clearr@   )r2   r   Lockr   Ztotal_tokensr   r   r   rf   r   r   rV   r   r5   r   r1   r   r!   r   Z
statisticsZtasks_waitingr   rp   rD   borrowed_tokens)
r   r   r   ZCOUNTcZorig_total_tokensr   r   eventsru   r;   )r   r   r   r   r   r}   rW   r<   !test_run_in_worker_thread_limiter  sX    r   c                    sB   g  G  fddd} t dd |  dI d H   ddgks>td S )Nc                      s4   e Zd Zddd fddZddd fddZdS )	z?test_run_in_worker_thread_custom_limiter.<locals>.CustomLimiterr#   r%   borrowerr'   c                   s     d || _d S Nacquire)r1   	_borrowerselfr   rL   r;   r<   acquire_on_behalf_of:  s    
zTtest_run_in_worker_thread_custom_limiter.<locals>.CustomLimiter.acquire_on_behalf_ofc                   s     d || jkstd S )Nrelease)r1   r   rD   r  rL   r;   r<   release_on_behalf_of>  s    
zTtest_run_in_worker_thread_custom_limiter.<locals>.CustomLimiter.release_on_behalf_ofNr   r   r   r  r  r;   rL   r;   r<   CustomLimiter9  s   r  c                   S  s   d S rY   r;   r;   r;   r;   r<   rZ   D  r[   z:test_run_in_worker_thread_custom_limiter.<locals>.<lambda>r   r   r  r   rD   )r  r;   rL   r<   (test_run_in_worker_thread_custom_limiter4  s    r
  c               	     s   g G fddd} |  }t jtdd}tdd |dI d H  W 5 Q R X |jjd ks\tdd	gksltg i  t jtdd }t fd
d|dI d H  W 5 Q R X t|jjtstdd	gkstd S )Nc                      s4   e Zd Zddd fddZddd fddZd	S )
zCtest_run_in_worker_thread_limiter_error.<locals>.BadCapacityLimiterr#   r%   r   c                   s     d d S r   )r1   r  rL   r;   r<   r  L  s    zXtest_run_in_worker_thread_limiter_error.<locals>.BadCapacityLimiter.acquire_on_behalf_ofr
   c                   s     d tdd S )Nr  zrelease on behalf)r1   rO   r  rL   r;   r<   r  O  s    
zXtest_run_in_worker_thread_limiter_error.<locals>.BadCapacityLimiter.release_on_behalf_ofNr  r;   rL   r;   r<   BadCapacityLimiterK  s   r  z^release on behalf$r   c                   S  s   d S rY   r;   r;   r;   r;   r<   rZ   V  r[   z9test_run_in_worker_thread_limiter_error.<locals>.<lambda>r  r   r  c                     s    d S )Nr7   r;   r;   )dr;   r<   rZ   _  r[   )	r]   r^   rO   r   r   __context__rD   
isinstancerT   )r  bsr   r;   )r  r:   r<   'test_run_in_worker_thread_limiter_errorH  s     $r  )r   r'   c              	     s   dddddd}|  tjjd| t }|jdks8ttt	}t
dd	 I d H  W 5 Q R X d
t|jksrt|jdkstd S )Nobjectr
   )r  r   r'   c                 W  s   t dd S )Nz!the engines canna take it captain)r_   )r  r   r;   r;   r<   	bad_starth  s    z:test_run_in_worker_thread_fail_to_spawn.<locals>.bad_startstart_thread_soonr   c                   S  s   d S rY   r;   r;   r;   r;   r<   rZ   r  r[   z9test_run_in_worker_thread_fail_to_spawn.<locals>.<lambda>Zengines)r   r   r   ZThreadCacher   r   rD   r]   r^   r_   r   r   r   )r   r  r   r   r;   r;   r<   'test_run_in_worker_thread_fail_to_spawnd  s    r  c                    s4   dddd} t  }t| I d H }||ks0td S )Nrx   r&   c                  S  s   t tj} | S rY   r   r   rV   callee_tokenr;   r;   r<   r}   {  s    
z5test_trio_to_thread_run_sync_token.<locals>.thread_fnr   rV   r   rD   r}   Zcaller_tokenr  r;   r;   r<   "test_trio_to_thread_run_sync_tokenx  s    r  c               	     s:   dddd} t jtdd t| I d H  W 5 Q R X d S )Nr%   r&   c                     s   d S rY   r;   r;   r;   r;   r<   async_fn  s    z=test_trio_to_thread_run_sync_expected_error.<locals>.async_fnzexpected a sync functionr   )r]   r^   	TypeErrorr   r  r;   r;   r<   +test_trio_to_thread_run_sync_expected_error  s    r  trio_test_contextvarzcontextvars.ContextVar[str]c                    s   t  } td dddd}t|I d H \}}|dks>t|| ksJtdddd}t|I d H \}}}t }|dkst|d	kst|dkstd
t dkstd S )Nr   ztuple[str, threading.Thread]r&   c               	   S  s4   t  } ttj t  W 5 Q R X | t fS rY   )	r  r   r]   r^   sniffioAsyncLibraryNotFoundErrorcurrent_async_libraryr2   r3   )r   r;   r;   r<   rA     s    z3test_trio_to_thread_run_sync_contextvars.<locals>.fz!tuple[str, str, threading.Thread]c               	   S  sH   t  } t d t  }ttj t  W 5 Q R X | |t	 fS )Nworker)
r  r   rp   r]   r^   r   r!  r"  r2   r3   )parent_valueinner_valuer;   r;   r<   r     s    
z3test_trio_to_thread_run_sync_contextvars.<locals>.gr#  zRThe contextvar value set on the worker would not propagate back to the main threadtrio)	r2   r3   r  rp   r   rD   r   r   r"  )rG   rA   r   rE   r   r$  r%  current_valuer;   r;   r<   (test_trio_to_thread_run_sync_contextvars  s    

r(  c               	     sv   dddd} t | I d H }t|ts*tdddd dd fdd	}tjtd
d t |I d H  W 5 Q R X d S )Nfloatr&   c                  S  s   t tj} | S rY   )r   r   current_time)	trio_timer;   r;   r<   thread_fn_1  s    
z3test_trio_from_thread_run_sync.<locals>.thread_fn_1r%   c                     s   d S rY   r;   r;   r;   r;   r<   r    s    z0test_trio_from_thread_run_sync.<locals>.async_fnc                     s   t   d S rY   )r   r;   r  r;   r<   thread_fn_2  s    z3test_trio_from_thread_run_sync.<locals>.thread_fn_2zexpected a synchronous functionr   )r   r  r)  rD   r]   r^   r  )r,  r+  r-  r;   r  r<   test_trio_from_thread_run_sync  s    r.  c               	     s   g ddfdd dd fdd} t | I d H  ddgksHtddd	d
}tjtdd t t|I d H  W 5 Q R X d S )Nr%   r&   c                     s   t    d d S )Nback in trio)r   r*  r1   r;   rL   r;   r<   back_in_trio_fn  s    z2test_trio_from_thread_run.<locals>.back_in_trio_fnc                     s    d t  d S )N	in thread)r1   r   r;   r0  r:   r;   r<   r}     s    
z,test_trio_from_thread_run.<locals>.thread_fnr1  r/  c                   S  s   d S rY   r;   r;   r;   r;   r<   sync_fn  s    z*test_trio_from_thread_run.<locals>.sync_fnzappears to be synchronousr   )r   rD   r]   r^   r  r   )r}   r3  r;   r2  r<   test_trio_from_thread_run  s    r4  c                    s4   dddd} t  }t| I d H }||ks0td S )Nrx   r&   c                  S  s   t tj} | S rY   r  r  r;   r;   r<   r}     s    
z.test_trio_from_thread_token.<locals>.thread_fnr  r  r;   r;   r<   test_trio_from_thread_token  s    r5  c                    s8   ddddd} t  }t| |I d H }||ks4td S )Nrx   ry   c                 S  s   t tj| d}|S )Nr.   r  )rW   r  r;   r;   r<   r}     s    z4test_trio_from_thread_token_kwarg.<locals>.thread_fnr  r  r;   r;   r<   !test_trio_from_thread_token_kwarg  s    r6  c                	     s$   t t ttj W 5 Q R X d S rY   r]   r^   r_   r   r   r*  r;   r;   r;   r<   test_from_thread_no_token  s    r8  c                    s   t d dddd} t| I d H \}}}}}t  }||  krLdksRn t||  krp|  krpdksvn tt dkst|dkstd S )	Nr   tuple[str, str, str, str, str]r&   c               	   S  s   t  } t d t  }ttj t  W 5 Q R X dddd}t|\}}t  }ttj t  W 5 Q R X | ||||fS )Nr#  tuple[str, str]r&   c                  S  s2   t  } t d t  }t dks*t| |fS Nback_in_mainr&  r  r   rp   r   r"  rD   back_parent_valueback_current_valuer;   r;   r<   r<    s
    
zStest_trio_from_thread_run_sync_contextvars.<locals>.thread_fn.<locals>.back_in_main)	r  r   rp   r]   r^   r   r!  r"  r   )thread_parent_valuethread_current_valuer<  r?  r@  thread_after_valuer;   r;   r<   r}     s     
z=test_trio_from_thread_run_sync_contextvars.<locals>.thread_fnr#  r&  r<  r  rp   r   r   rD   r   r"  r}   rA  rB  rC  r?  r@  r'  r;   r;   r<   *test_trio_from_thread_run_sync_contextvars  s    
 $rF  c                    s   t d dddd} t| I d H \}}}}}t  }||  krLdksRn t||  krp|  krpdksvn t|dkstt dkstd S )	Nr   r9  r&   c               	   S  s   t  } t d t  }ttj t  W 5 Q R X dddd}t|\}}t  }ttj t  W 5 Q R X | ||||fS )Nr#  r:  r&   c                    s2   t  } t d t  }t dks*t| |fS r;  r=  r>  r;   r;   r<   async_back_in_main9  s
    
zTtest_trio_from_thread_run_contextvars.<locals>.thread_fn.<locals>.async_back_in_main)	r  r   rp   r]   r^   r   r!  r"  r   )rA  rB  rG  r?  r@  rC  r;   r;   r<   r}   2  s     
z8test_trio_from_thread_run_contextvars.<locals>.thread_fnr#  r<  r&  rD  rE  r;   r;   r<   %test_trio_from_thread_run_contextvars/  s    
 $rH  c                	   C  s(   t t ttjdd W 5 Q R X d S )NzNot TrioTokentyper.   r7  r;   r;   r;   r<   4test_run_fn_as_system_task_catched_badly_typed_tokenZ  s
    rI  c               	     s<   dddd} t  }tt t| |d W 5 Q R X d S )Nr%   r&   c                   S  s
   t  d S rY   )rD   r;   r;   r;   r<   
not_calledc  s    z7test_from_thread_inside_trio_thread.<locals>.not_calledr.   )r   rV   r]   r^   r_   r   )rJ  r,   r;   r;   r<   #test_from_thread_inside_trio_threadb  s    rK  c                    s^   g g dddfdd ddd fd	d
} t | d t | d ddgksZtd S )Nr(   zAsyncGenerator[(None, None)]ry   c                  st   z
d V  W 5 t jddP zttttd| dI d H  W n  t jk
rX    d Y nX  d W 5 Q R X X d S )NTr   r   r.   r   clean)r   r   r   r   r   r   ZRunFinishedErrorr1   r|   rL   r;   r<   ageno  s    
z2test_from_thread_run_during_shutdown.<locals>.agenr   r%   )use_system_taskr'   c                   s2     | rt nd  d d I d H  d S )Nr   )r1   r   rV   asend)rN  )rM  saver;   r<   r   }  s    z2test_from_thread_run_during_shutdown.<locals>.mainTFr   rL  )r   rq   rD   )r   r;   )rM  r:   rP  r<   $test_from_thread_run_during_shutdownk  s    rQ  c                    s4   t  } t| t jstt| }| | ks0td S rY   )r   rV   r  Z	TrioTokenrD   weakrefref)rW   Zweak_referencer;   r;   r<   "test_trio_token_weak_referenceable  s    
rT  c               	     s<   G dd d} t t tt|  dI d H  W 5 Q R X d S )Nc                   @  s   e Zd ZddddZdS )z4test_unsafe_abandon_on_cancel_kwarg.<locals>.BadBoolr   r&   c                 S  s   t d S rY   )NotImplementedError)r  r;   r;   r<   __bool__  s    z=test_unsafe_abandon_on_cancel_kwarg.<locals>.BadBool.__bool__N)r   r   r   rV  r;   r;   r;   r<   BadBool  s   rW  r   )r]   r^   rU  r   rJ   )rW  r;   r;   r<   #test_unsafe_abandon_on_cancel_kwarg  s    rX  c                    sL   t  } dddd}| ttt jI d H ks0t| tt|I d H ksHtd S )Nz
_core.Taskr&   c                     s   t  S rY   )r   current_taskr;   r;   r;   r<   async_current_task  s    z8test_from_thread_reuses_task.<locals>.async_current_task)r   rY  r   r   rD   r   )ZtaskrZ  r;   r;   r<   test_from_thread_reuses_task  s    r[  c                    s0   d  dd fdd}  t | I d H ks,td S )NrJ   r&   c                     s   t   ttt jS rY   )r2   r   r   r   r;   tidr;   r<   get_tid_then_reenter  s    z6test_recursive_to_thread.<locals>.get_tid_then_reenterr	  )r^  r;   r\  r<   test_recursive_to_thread  s    r_  c               	     s  t  dd fdd} t  t| I d H  W 5 Q R X  jrHt rTtt  t| ddI d H  W 5 Q R X  jstttj	ddI d H rtd	dd
ddd fdd}t  t|I d H  W 5 Q R X  jrt rtt  t|ddI d H  W 5 Q R X  js.tttj	ddI d H rLtdd fdd}t  tt
|I d H  W 5 Q R X  jstd S )Nr%   r&   c                     sF   t  j zt t W n  tjk
r6   d Y nX d d S NTF)r   r   r   r   r{   r   r;   )r   queuer;   r<   
sync_check  s    
z3test_from_thread_host_cancelled.<locals>.sync_checkTr   r   timeoutr   c                     s   dS NTr;   r;   r;   r;   r<   no_checkpoint  s    z6test_from_thread_host_cancelled.<locals>.no_checkpointc                     sJ   t  j ztstW n  tjk
r:   d Y nX d d S r`  )r   r   r   rD   r   r{   r   r;   r   rf  ra  r;   r<   async_check  s    
z4test_from_thread_host_cancelled.<locals>.async_checkc                	     s,       td t I d H  W 5 Q R X d S )Nr   )r   r   r   r;   r   r;   r<   async_time_bomb  s    
z8test_from_thread_host_cancelled.<locals>.async_time_bomb)r   r   r   r   r   r   rD   
get_nowaitr   r   r   )rb  rh  ri  r;   rg  r<   test_from_thread_host_cancelled  s0    	



	



rk  c               
     s<  t  ddddfdd} dd fdd	g t  t 4 I d H P}|| d
t  t I d H  d dkst	j
dddkst	   W 5 Q I d H R X d dkst	g t  t }t 4 I d H X}|| d
| t I d H  d dkst	j
dddks$t	|     W 5 Q I d H R X |jsPt	dks^t	d dkspt	dd fdd	g t  t }t 4 I d H B}|| d| t I d H  d dkst	|     W 5 Q I d H R X |jst	dkst	d dks"t	j
dddks8t	d S )Nr   r   r%   )r   r   r'   c              
     sx   |j  d zLz$t | dI d H W W 4W  5 Q R  S  tjk
rX    d  Y nX W 5  d X W 5 Q R X d S )Nr-   r   r   r   )r1   r   r   r{   )r   r   r   r;   r<   r     s    
$
z/test_from_thread_check_cancelled.<locals>.childr&   c                     sD   z
t   W n  tjk
r*   d Y nX d    t  S Nr{   Not Cancelled)r   r   r{   r   r   r;   rv   r   r;   r<   rA     s    

z+test_from_thread_check_cancelled.<locals>.fFr   r-   r   rc  rm  r   r   r   c                     sB       z
t  W n  tjk
r2   d Y nX d d S rl  )r   r   r   r{   r   r;   rn  r;   r<   rA     s    
Tr{   )r   r   r2   r   r   r   r   r   r!   rD   r   rp   r   r   )r   r   r   r;   )rv   rA   r   r:   r<    test_from_thread_check_cancelled  sN    	ro  c                	     sb   t t t  W 5 Q R X t  tt fdd t t  jdd	  W 5 Q R X d S )Nc                   s
     | S rY   )r   rt   r   r;   r<   rZ   ;  r[   zLtest_from_thread_check_cancelled_raises_in_foreign_threads.<locals>.<lambda>r   rc  )
r]   r^   r_   r   r   r   r   r  r   unwrapr;   r;   r   r<   :test_from_thread_check_cancelled_raises_in_foreign_threads7  s    rq  c                    s`   dddd} t d@ t 4 I d H }tdD ]}||  q2W 5 Q I d H R X W 5 Q R X d S )Nr%   r&   c                     s   t ttdddI d H  q d S )Nr   Fr   )r   r   r   r;   r;   r;   r<   r   F  s    z+test_reentry_doesnt_deadlock.<locals>.childr      )r   r   r   r   r   )r   r   ru   r;   r;   r<   test_reentry_doesnt_deadlock@  s
    
rs  c               
     s.  d t  } t  }t  }t  }dddddd}dd fdd	}t 4 I d H }||| | |||| t I d H  || t I d H   rtt d
kst|   | I d H  t I d H   rtt dkst|  | I d H  t I d H   s
tt dkstW 5 Q I d H R X d S )NFr   r%   )r   e_exitr'   c                   s,   dd fdd}t |I d H  |  d S )Nr%   r&   c                     s   t  j d S rY   )r   r   r;   r   r;   r<   rn   Y  s    zCtest_wait_all_threads_completed.<locals>.wait_event.<locals>.thread)r   rp   )r   rt  rn   r;   ru  r<   
wait_eventX  s    z3test_wait_all_threads_completed.<locals>.wait_eventr&   c                     s   t  I d H  d d S re  )r    r;   Zno_threads_leftr;   r<   wait_no_threads_left_  s    z=test_wait_all_threads_completed.<locals>.wait_no_threads_leftr   r   r   )	r   r   r   r   r!   rD   r   rp   r   )e1e2Z	e1_exitedZ	e2_exitedrv  rx  r   r;   rw  r<   test_wait_all_threads_completedP  s2    

r{  c                     s   t  I d H  t dkstd S )Nr   )r    r   rD   r;   r;   r;   r<   *test_wait_all_threads_completed_no_threadsz  s    r|  )N)d
__future__r   Zcontextvarsra  r   r   rb   r2   r   rR  	functoolsr   typingr   r   r   r   r	   r
   r   r   r   r   r]   r   r   r   r   r   r   r   r   r   r   Z_core._tests.test_kir   Z_core._tests.tutilr   _threadsr   r   r   r   r   r   r    Ztestingr!   Zoutcomer"   Zlowlevelr#   r   rB   r4   rI   r$   rX   r`   rr   r   r   r   r   r   r   r   r   markZparametrizer   r
  r  r  r  r  Z
ContextVarr  r   r(  r.  r4  r5  r6  r8  rF  rH  rI  rK  rQ  rT  rX  r[  r_  rk  ro  rq  rs  r{  r|  r;   r;   r;   r<   <module>   s    0(
$	<0.#	<&t	%++	
;N	*