o
    4@Hh>L                     @  s  d dl mZ d dlZd dlmZmZ d dlZd dlZd dlmZm	Z	m
Z
 ddlmZmZmZmZ ejdk r;d dlmZ erCd d	lmZ dAddZdBddZdAddZdAddZdAddZdAddZdAddZdAddZdAddZdAd d!ZdAd"d#ZdAd$d%Z dAd&d'Z!dAd(d)Z"dAd*d+Z#dAd,d-Z$dAd.d/Z%dCd1d2Z&dAd3d4Z'dAd5d6Z(dAd7d8Z)dAd9d:Z*dAd;d<Z+dAd=d>Z,dAd?d@Z-dS )D    )annotationsN)TYPE_CHECKINGUnion)EndOfChannelas_safe_channelopen_memory_channel   )MatcherRaisesGroupassert_checkpointswait_all_tasks_blocked)      )ExceptionGroup)AsyncGeneratorreturnNonec                    s8  t t td W d    n1 sw   Y  t jtdd td W d    n1 s0w   Y  ttttd f  d\} }t|  t| | 	d t
  | dI d H  W d    n1 sew   Y  t tj | 	d  W d    n1 sw   Y  t
  | I d H dksJ W d    n1 sw   Y  | dksJ t tj |  W d    n1 sw   Y  | 	d |  I d H  t tj | dI d H  W d    n1 sw   Y  t tj | 	d W d    n	1 sw   Y  t tj |   W d    n	1 s!w   Y  |  I d H  | dks6J t t | I d H  W d    n	1 sNw   Y  | I d H  t tj | I d H  W d    n	1 ssw   Y  t tj |  W d    n	1 sw   Y  | I d H  d S )	Ng      ?z^max_buffer_size must be >= 0$matchr      lastztoo late)pytestraises	TypeErrorr   
ValueErrorr   intstrreprsend_nowaitr   sendtrio
WouldBlockreceivereceive_nowaitacloseClosedResourceErrorcloner   )sr r*   X/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/trio/_tests/test_channel.pytest_channel   s`   






r,   autojump_clocktrio.abc.Clockc                   sh   t t d\}}td}| I d H  W d    n1 s w   Y  |js*J |dI d H  d S )Nr   
   zTest for PR #553)r   r   r!   move_on_afterr#   cancelled_caughtr    )r-   r(   r)   timeout_scoper*   r*   r+   test_553?   s   
r3   c               
     s   ddd} t t d	\}}t 4 I d H P}|4 I d H  td
D ]}|| | | q"W d   I d H  n1 I d H s>w   Y  dd |2 I d H }|  |ttdks[J W d   I d H  d S 1 I d H slw   Y  d S )Nsend_channeltrio.MemorySendChannel[int]ir   r   r   c              	     sl   | 4 I d H " t d| d|d  D ]
}| |I d H  qW d   I d H  d S 1 I d H s/w   Y  d S )Nr   r   )ranger    )r4   r6   jr*   r*   r+   producerH   s   .z1test_channel_multiple_producers.<locals>.producerr   r/   c                      g | z3 d H W }|q6 S Nr*   ).0valuer*   r*   r+   
<listcomp>V       z3test_channel_multiple_producers.<locals>.<listcomp>   )r4   r5   r6   r   r   r   )	r   r   r!   open_nurseryr7   
start_soonr'   sortlist)r9   r4   receive_channelnurseryr6   gotr*   r*   r+   test_channel_multiple_producersG   s   
(.rH   c               
     s(  t  g  d fdd} t 4 I d H S}tjt d	\}}|4 I d H . td
D ]	}|| || q+t I d H  tdD ]
}||I d H  q?W d   I d H  n1 I d H sZw   Y  W d   I d H  n1 I d H sow   Y  t td
ks~J t	 dksJ t  t tdksJ d S )NrE   trio.MemoryReceiveChannel[int]r6   r   r   r   c                   s.   | 2 z3 d H W } |  | q6 d S r;   )addappend)rE   r6   r=   receivedsuccessful_receiversr*   r+   consumer`   s
   
z1test_channel_multiple_consumers.<locals>.consumerr      r/   )rE   rI   r6   r   r   r   )
setr!   rA   r   r   r7   rB   r   r    len)rO   rF   r4   rE   r6   r*   rL   r+   test_channel_multiple_consumers\   s$   *(	rS   c               	     s@  ddd} t d  d	\}}t 4 I d H  }|| |tj t I d H  | I d H  W d   I d H  n1 I d H s<w   Y  ttj |	d  W d    n1 sWw   Y  ttj |
d I d H  W d    n1 suw   Y  tt |  W d    n1 sw   Y  tt | I d H  W d    n1 sw   Y  t d  d	\}}t 4 I d H  }|| |tj t I d H  | I d H  W d   I d H  n1 I d H sw   Y  ttj |	d  W d    n	1 sw   Y  ttj |
d I d H  W d    n	1 s w   Y  ddd}t t d	\}}t 4 I d H }||| t I d H  | I d H  W d   I d H  n1 I d H s_w   Y  ttj |  W d    n	1 szw   Y  ttj | I d H  W d    d S 1 sw   Y  d S )Nr(   trio.MemorySendChannel[None]expecttype[BaseException]r   r   c                   B   t | | d I d H  W d    d S 1 sw   Y  d S r;   r   r   r    r(   rU   r*   r*   r+   
send_blockt      "z%test_close_basics.<locals>.send_blockr   r)   rI   c                   B   t tj |  I d H  W d    d S 1 sw   Y  d S r;   r   r   r!   r&   r#   r)   r*   r*   r+   receive_block      "z(test_close_basics.<locals>.receive_blockr(   rT   rU   rV   r   r   )r)   rI   r   r   )r   r!   rA   rB   r&   r   r%   r   r   r   r    r   r$   r#   BrokenResourceErrorr   )rZ   r(   r)   rF   r_   _s2r2r*   r*   r+   test_close_basicss   sZ   
(
(
*
$re   c               	     s,  ddd} t d  d	\}}t 4 I d H }|| |tj t I d H  |  W d   I d H  n1 I d H s9w   Y  ttj |	d  W d    n1 sTw   Y  ttj |
d I d H  W d    n1 srw   Y  tt |  W d    n1 sw   Y  tt | I d H  W d    n1 sw   Y  t d  d	\}}t 4 I d H }|| |tj t I d H  |  W d   I d H  n1 I d H sw   Y  ttj |	d  W d    n1 sw   Y  ttj |
d I d H  W d    n	1 sw   Y  ddd}t d  d	\}}t 4 I d H }||| t I d H  |  W d   I d H  n1 I d H sUw   Y  ttj |  W d    n	1 spw   Y  ttj | I d H  W d    d S 1 sw   Y  d S )Nr(   rT   rU   rV   r   r   c                   rW   r;   rX   rY   r*   r*   r+   rZ      r[   z#test_close_sync.<locals>.send_blockr   r)   trio.MemoryReceiveChannel[None]c                   r\   r;   r]   r^   r*   r*   r+   r_      r`   z&test_close_sync.<locals>.receive_blockra   )r)   rf   r   r   )r   r!   rA   rB   r&   r   closer   r   r   r    r   r$   r#   rb   )rZ   r(   r)   rF   r_   r*   r*   r+   test_close_sync   sZ   

(

(

*
$rh   c                    s  t d  d\} }| }| }| d  | I d H  | W d    n1 s*w   Y  ttj |  W d    n1 sDw   Y  ttj |  W d    n1 s^w   Y  | d  | I d H  ttj | d  W d    d S 1 sw   Y  d S )Nr/   )	r   r'   r   r%   r   r   r!   r&   rb   )r(   r)   rd   r3r*   r*   r+   $test_receive_channel_clone_and_close   s(   



"rj   c               	     s   t t d\ }   d	 fdd}d	fdd}t 4 I d H .}|| || t I d H    I d H  |  I d H dksFJ W d   I d H  d S 1 I d H sWw   Y  d S )
Nr   r   r   c                     sD   t tj  dI d H  W d    d S 1 sw   Y  d S )Nnope)r   r   r!   r&   r    r*   )s1r*   r+   send_will_close  s   "z9test_close_multiple_send_handles.<locals>.send_will_closec                     s     dI d H  d S Nok)r    r*   )s2r*   r+   send_will_succeed  s   z;test_close_multiple_send_handles.<locals>.send_will_succeedro   r   r   )	r   r   r'   r!   rA   rB   r   r%   r#   )r)   rm   rq   rF   r*   )rl   rp   r+    test_close_multiple_send_handles  s   

.rs   c               	     s   t t d\}    d	 fdd}d	fdd}t 4 I d H +}|| || t I d H    I d H  | dI d H  W d   I d H  d S 1 I d H sTw   Y  d S )
Nr   r   r   c                     sB   t tj   I d H  W d    d S 1 sw   Y  d S r;   r]   r*   )r1r*   r+   receive_will_close  r`   z?test_close_multiple_receive_handles.<locals>.receive_will_closec                     s      I d H dksJ d S rn   r#   r*   )rd   r*   r+   receive_will_succeed!  s   zAtest_close_multiple_receive_handles.<locals>.receive_will_succeedro   rr   )	r   r   r'   r!   rA   rB   r   r%   r    )r(   ru   rw   rF   r*   )rt   rd   r+   #test_close_multiple_receive_handles  s   

.rx   c                    s~   t t td\} }|  tdD ]}| | qW d    n1 s$w   Y  dd |2 I d H }|ttdks=J d S )Ninfr/   c                   r:   r;   r*   )r<   r6   r*   r*   r+   r>   4  r?   z%test_inf_capacity.<locals>.<listcomp>)r   r   floatr7   r   rD   )r    r#   r6   rG   r*   r*   r+   test_inf_capacity,  s   r{   c               	     sn  t d  d\} }|  | ksJ |  }|jdksJ |jdks%J |jdks,J |jdks3J |jdks:J |jdksAJ | d  |  jdksOJ | 	 }|  jdks\J | 
 I d H  | jdkslJ |	 }| jdksyJ |
 I d H  | jdksJ t 4 I d H ;}|d  |  jdksJ ||jd  ||jd  t I d H  |  jdksJ |j  W d   I d H  n1 I d H sw   Y  |  jdksJ z	 |  q tjy   Y nw t 4 I d H &}||j t I d H  |  jdksJ |j  W d   I d H  n1 I d H s&w   Y  |  jdks5J d S )Nr   r   r   )r   
statisticscurrent_buffer_usedmax_buffer_sizeopen_send_channelsopen_receive_channelstasks_waiting_sendtasks_waiting_receiver   r'   r%   r!   rA   rB   r    r   cancel_scopecancelr$   r"   r#   )r(   r)   statsrp   rd   rF   r*   r*   r+   test_statistics8  sX   

(*r   c               	     s  t ttd f  d\} }| d | dksJ | d | dks'J d  d
 fdd}t 4 I d H 6}||| t I d H  | d t	
tj |  W d    n1 s_w   Y  W d   I d H  n1 I d H stw   Y   dksJ t ttd f  d\} }| d t	
tj | d  W d    n1 sw   Y  t 4 I d H G}|| jd t I d H  | dksJ t	
tj | d	 W d    n1 sw   Y  | I d H dksJ W d   I d H  d S 1 I d H sw   Y  d S )Nr   r   r)   %trio.MemoryReceiveChannel[int | None]r   r   c                   s   |   I d H  d S r;   rv   r^   resultr*   r+   
do_receivex  s   z)test_channel_fairness.<locals>.do_receiver   )r)   r   r   r   )r   r   r   r   r$   r!   rA   rB   r   r   r   r"   r    r#   )r(   r)   r   rF   r*   r   r+   test_channel_fairnessj  s>   



(
0r   c               	     sF  t t d\} }ttj |  W d    n1 sw   Y  ttj | d W d    n1 s9w   Y  dd	d
}t 4 I d H 0}|	|| d t
  | I d H dksbJ W d    n1 slw   Y  W d   I d H  n1 I d H sw   Y  ttj |  W d    d S 1 sw   Y  d S )Nr   r   r(   r5   vr   r   r   c                   s>   t   | |I d H  W d    d S 1 sw   Y  d S r;   )r   r    )r(   r   r*   r*   r+   do_send  s   "z test_unbuffered.<locals>.do_send)r(   r5   r   r   r   r   )r   r   r   r   r!   r"   r$   r   rA   rB   r   r#   )r(   r)   r   rF   r*   r*   r+   test_unbuffered  s$   

(
"r   c               	     sp   t ddd} |  4 I d H }|2 z3 d H W }|dksJ q6 W d   I d H  d S 1 I d H s1w   Y  d S )Nr   AsyncGenerator[int]c                   S s   dV  d S Nr   r*   r*   r*   r*   r+   agen  s   
z*test_as_safe_channel_exhaust.<locals>.agenr   r   r   r   r   	recv_chanxr*   r*   r+   test_as_safe_channel_exhaust  s   .r   c               	     s   t ddd} |  4 I d H ;}| I d H dksJ | I d H  ttj | I d H  W d    n1 s:w   Y  W d   I d H  d S 1 I d H sPw   Y  d S )Nr   r   c                   S s   dV  dV  d S )Nr   r   r*   r*   r*   r*   r+   r     s   
z2test_as_safe_channel_broken_resource.<locals>.agenr   r   )r   	__anext__r%   r   r   r!   r&   r   r   r*   r*   r+   $test_as_safe_channel_broken_resource  s   .r   c               
     s   t  9} tddd}| 4 I d H  |   W d   I d H  n1 I d H s)w   Y  W d    d S W d    d S 1 sAw   Y  d S )Nr   AsyncGenerator[None]c                   S 
   t d)Nz@cancel before consumption means generator should not be iteratedAssertionErrorr*   r*   r*   r+   r     s   z,test_as_safe_channel_cancelled.<locals>.agenr   r   )r!   CancelScoper   r   )csr   r*   r*   r+   test_as_safe_channel_cancelled  s   

*	"r   c               
     s   t ddd} tjtdd> |  4 I d H }|2 z3 d H W }|dks&J q6 W d   I d H  n1 I d H s8w   Y  W d    d S W d    d S 1 sPw   Y  d S )	Nr   r   c                   S    dV  t d)Nr   oae)r   r*   r*   r*   r+   r     s   z*test_as_safe_channel_no_race.<locals>.agenz^oae$r   r   r   )r   r   r   r   r   r*   r*   r+   test_as_safe_channel_no_race  s   *"r   trio.testing.MockClockc              
     s   t ddd}tdE | 4 I d H "}|2 z3 d H W }|dks$J t I d H  q6 W d   I d H  n1 I d H s=w   Y  W d    d S W d    d S 1 sUw   Y  d S )Nr   r   c                   S r   )Nr   z;buffer size 0 means we shouldn't be asked for another valuer   r*   r*   r*   r+   r     s
   z8test_as_safe_channel_buffer_size_too_small.<locals>.agenrP   r   r   )r   r!   r0   sleep_forever)r-   r   r   r   r*   r*   r+   *test_as_safe_channel_buffer_size_too_small  s   *"r   c               	     sv   t ddd} |  4 I d H }| I d H dksJ tj I d H  W d   I d H  d S 1 I d H s4w   Y  d S )Nr   r   c                   S s   dV  t r   r   r*   r*   r*   r+   r     s   z0test_as_safe_channel_no_interleave.<locals>.agenr   r   )r   r   r!   lowlevel
checkpointr   r*   r*   r+   "test_as_safe_channel_no_interleave  s   .r   c               
     s   t ddd} g }ttttddttd	dd
d9 | |4 I d H }|2 z3 d H W }|dks3J td6 W d   I d H  n1 I d H sHw   Y  W d    n1 sWw   Y  |ddgksdJ d S )Nevents	list[str]r   r   c              
   S s^   z$zdV  W n t y } z| t|  d }~ww W | d td| d td)Nr   finallyr   )BaseExceptionrK   r   r   )r   er*   r*   r+   r     s   


z2test_as_safe_channel_genexit_finally.<locals>.agenz^agen$r   z
^iterator$z^Encountered exception during cleanup of generator object, as well as exception in the contextmanager body - unable to unwrap.$r   iteratorzGeneratorExit()r   )r   r   r   r   )r   r
   r	   r   r   )r   r   r   r6   r*   r*   r+   $test_as_safe_channel_genexit_finally  s(   


(r   c                    s   t ddd} d}|  4 I d H Q}|2 zA3 d H W }|  4 I d H %}d}|2 z3 d H W }||f||fks6J |d7 }q&6 W d   I d H  n1 I d H sLw   Y  |d7 }q6 W d   I d H  d S 1 I d H shw   Y  d S )Nr   r   c                  S s   t dD ]} | V  qd S )Nr   )r7   )r6   r*   r*   r+   r     s   z.test_as_safe_channel_nested_loop.<locals>.agenr   r   r   r   )r   ii
recv_chan1r6   r   jjr8   r*   r*   r+    test_as_safe_channel_nested_loop  s    
(
.r   c               
     s   t ddd} tt/ |  4 I d H }|2 z3 d H W }q6 W d   I d H  td1 I d H s3w   Y  td1 s?w   Y  d S )Nr   r   c                  S s>   t  } |   d V  W d    d S 1 sw   Y  d S r;   )r!   r   r   )cscoper*   r*   r+   r   *  s
   
"z;test_as_safe_channel_doesnt_leak_cancellation.<locals>.agenzshould be reachabler   )r   r   r   r   )r   r   _r*   r*   r+   -test_as_safe_channel_doesnt_leak_cancellation)  s   r   c               
     s   t ddd} tttdddd$ |  4 I d H 	}tdtdg1 I d H s)w   Y  W d    d S 1 s9w   Y  d S )	Nr   r   c                   S r   )Nznot entered)NotImplementedErrorr*   r*   r*   r+   r   8  s   zBtest_as_safe_channel_dont_unwrap_user_exceptiongroup.<locals>.agenbarr   foor   )r   r
   r	   r   r   )r   r   r*   r*   r+   4test_as_safe_channel_dont_unwrap_user_exceptiongroup7  s   "r   c               
     s   t   td fdd} ddd}|  4 I d H B}t  4 I d H #}|||dI d H  |||dI d H     W d   I d H  n1 I d H sLw   Y  W d   I d H  d S 1 I d H sbw   Y  d S )Nr   r   c                    s       I d H  dV  dV  d S )Nr   r   )waitr*   eventr*   r+   r   E  s   
z4test_as_safe_channel_multiple_receiver.<locals>.agenr   trio.abc.ReceiveChannel[int]r=   r   task_statustrio.TaskStatusr   c                   s$   |   |  I d H |ksJ d S r;   )startedr#   )r   r=   r   r*   r*   r+   handle_valueK  s   z<test_as_safe_channel_multiple_receiver.<locals>.handle_valuer   r   r   )r   r   r=   r   r   r   r   r   )r!   Eventr   rA   startrQ   )r   r   r   rF   r*   r   r+   &test_as_safe_channel_multiple_receiverB  s   

*.r   c                    s  t d	dd} g }t b}ttjL | |4 I d H 1}|2 z"3 d H W }|  z
tj I d H  W q! tjyC   |	d  w 6 W d   I d H  n1 I d H sUw   Y  W d    n1 sdw   Y  W d    n1 ssw   Y  |ddgksJ d S )
Nr   r   r   r   c                 S sr   zd V  W zt j I d H  W d S  t jy   | d  w z
t j I d H  W w  t jy8   | d  w )Nagen cancel)r!   r   r   	CancelledrK   )r   r*   r*   r+   r   [  s   

z/test_as_safe_channel_multi_cancel.<locals>.agenzbody cancelr   )r   r   r   r   )
r   r!   r   r   r   r   r   r   r   rK   )r   r   r   r   r   r*   r*   r+   !test_as_safe_channel_multi_cancelZ  s,   

(
r   rr   )r-   r.   r   r   )r-   r   r   r   ).
__future__r   systypingr   r   r   r!   r   r   r   testingr	   r
   r   r   version_infoexceptiongroupr   collections.abcr   r,   r3   rH   rS   re   rh   rj   rs   rx   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r*   r*   r*   r+   <module>   sH    


+



:
:




2
*











