U
    gY                     @  sv  U d dl mZ d dlZd dlZd dlmZmZ d dlmZm	Z	m
Z
mZmZmZmZ ddlmZmZ ddlmZmZmZmZmZ ddlmZ d	d
lmZ erd dlmZ d dlmZm Z  edZ!ej"dk rd dl#m$Z$ ededZ%ededZ&e
g e	ee%e&f  f Z'de(d< G dd dee%e&f Z)ed,ddddddZ*ddd d!d"d#Z+d$d%d d!d&d'Z,d(d)d d!d*d+Z-dS )-    )annotationsN)contextmanagersuppress)TYPE_CHECKING	AwaitableCallable	GeneratorGenericTupleTypeVar   )CancelScope_core)AsyncResourceHalfCloseableStreamReceiveStream
SendStreamStream)aclose_forcefully   )assert_checkpoints)TracebackType)	ParamSpec	TypeAliasArgsT)      )BaseExceptionGroupRes1)boundRes2r   StreamMakerc                   @  s>   e Zd ZdddddZddddZd	d
dddddZdS )_ForceCloseBothztuple[Res1, Res2]None)bothreturnc                 C  s   |\| _ | _d S N_first_second)selfr$    r+   ?/tmp/pip-unpacked-wheel-ks04xdmi/trio/testing/_check_streams.py__init__&   s    z_ForceCloseBoth.__init__r%   c                   s   | j | jfS r&   r'   )r*   r+   r+   r,   
__aenter__)   s    z_ForceCloseBoth.__aenter__ztype[BaseException] | NonezBaseException | NonezTracebackType | None)exc_type	exc_value	tracebackr%   c                   s,   zt | jI d H  W 5 t | jI d H  X d S r&   )r   r)   r(   )r*   r0   r1   r2   r+   r+   r,   	__aexit__,   s    z_ForceCloseBoth.__aexit__N)__name__
__module____qualname__r-   r/   r3   r+   r+   r+   r,   r"   %   s   r"   Fztype[BaseException]boolzGenerator[(None, None, None)])expected_excwrappedr%   c              
   c  s   d}z
d V  W nr t k
rb } z6|s,tdt|jdks>tt|jd | sRtW 5 d }~X Y n. | k
r   |r|tdY nX td|  d S )NTz:caught exceptiongroup, but expected an unwrapped exceptionr   r   z0caught exception, but expected an exceptiongroupzexpected exception: )r   AssertionErrorlen
exceptions
isinstance)r8   r9   Z__tracebackhide__excr+   r+   r,   _assert_raises:   s    
$r?   z&StreamMaker[SendStream, ReceiveStream]z-StreamMaker[SendStream, ReceiveStream] | Noner#   )stream_makerclogged_stream_makerr%   c                   s  t |  I dH 4 I dH d\tts.ttts<tdddfdddGddd	fd
ddddfdd dddddt 4 I dH }|d | d W 5 Q I dH R X ddfdd}t 4 I dH }|| | d W 5 Q I dH R X t 4 I dH "}|td | d W 5 Q I dH R X t 4 I dH "}|t	d | d W 5 Q I dH R X t
t dI dH  W 5 Q R X t
t dI dH  W 5 Q R X t
t dI dH  W 5 Q R X t 4 I dH (}|d  I dH dks*tW 5 Q I dH R X t 4 I dH *}|d dI dH dksptW 5 Q I dH R X t
tjdd@ t 4 I dH }|d  |d  W 5 Q I dH R X W 5 Q R X d!dd"fd#d$}t 4 I dH  }|||j |d  W 5 Q I dH R X ddfd%d&}t 4 I dH }|| | W 5 Q I dH R X t
tj d'I dH  W 5 Q R X t
tj d(I dH  W 5 Q R X I dH  I dH  I dH  t
tj d'I dH  W 5 Q R X t
tj d)I dH  W 5 Q R X t
tj& t   I dH  W 5 Q R X W 5 Q R X I dH  I dH  W 5 Q I dH R X t |  I dH 4 I dH l\ddfd*d+}dd fd,d-}t 4 I dH }|| || W 5 Q I dH R X W 5 Q I dH R X t |  I dH 4 I dH d\tI dH  t
tj d'I dH  qRW 5 Q R X t
tj d(I dH  W 5 Q R X W 5 Q I dH R X t |  I dH 4 I dH `\tI dH  t
tj d.I dH  W 5 Q R X ttj  d)I dH  W 5 Q R X W 5 Q I dH R X t |  I dH 4 I dH \t }|   I dH  W 5 Q R X t }|   I dH  W 5 Q R X t
tj d.I dH  W 5 Q R X t
tj d(I dH  W 5 Q R X W 5 Q I dH R X t |  I dH 4 I dH \d/d0d1dd2d3d4}	t L}|  t 4 I dH "}||	d ||	d  W 5 Q I dH R X W 5 Q R X t 4 I dH }| | W 5 Q I dH R X W 5 Q I dH R X t |  I dH 4 I dH ^\fd5d6}
t 4 I dH ,}||
 t I dH  tI dH  W 5 Q I dH R X W 5 Q I dH R X |dk	r|t | I dH 4 I dH \g d!dd7fd8d9}ddfd:d;}t 4 I dH ,}|||j t I dH  || W 5 Q I dH R X d<d=d>gkstW 5 Q I dH R X t | I dH 4 I dH \t
tjdd@ t 4 I dH }|j |j W 5 Q I dH R X W 5 Q R X t
tjddB t 4 I dH  }|j |jd. W 5 Q I dH R X W 5 Q R X W 5 Q I dH R X t | I dH 4 I dH b\t
tjddD t 4 I dH "}|jd. |jd. W 5 Q I dH R X W 5 Q R X W 5 Q I dH R X t | I dH 4 I dH d\ddfd?d@}ddfdAd;}t 4 I dH }|| || W 5 Q I dH R X W 5 Q I dH R X t | I dH 4 I dH V\tI dH  z$t   I dH  W 5 Q R X W n tjk
rX   Y nX W 5 Q I dH R X dBddCdDdE}t | I dH 4 I dH ^\t 4 I dH 8}|| t
tj dFI dH  W 5 Q R X W 5 Q I dH R X W 5 Q I dH R X t | I dH 4 I dH \\t 4 I dH 6}|| t
tj  I dH  W 5 Q R X W 5 Q I dH R X W 5 Q I dH R X dS )Ha  Perform a number of generic tests on a custom one-way stream
    implementation.

    Args:
      stream_maker: An async (!) function which returns a connected
          (:class:`~trio.abc.SendStream`, :class:`~trio.abc.ReceiveStream`)
          pair.
      clogged_stream_maker: Either None, or an async function similar to
          stream_maker, but with the extra property that the returned stream
          is in a state where ``send_all`` and
          ``wait_send_all_might_not_block`` will block until ``receive_some``
          has been called. This allows for more thorough testing of some edge
          cases, especially around ``wait_send_all_might_not_block``.

    Raises:
      AssertionError: if a test fails.

    Nbytes | bytearray | memoryviewr#   )datar%   c              	     s.   t    | I d H d ks tW 5 Q R X d S r&   )r   send_allr:   )rC   sr+   r,   do_send_allg   s    z)check_one_way_stream.<locals>.do_send_allz
int | Nonebytes | bytearray)	max_bytesr%   c              
     s.   t    | I d H W  5 Q R  S Q R X d S r&   )r   receive_some)rI   rr+   r,   do_receive_somek   s    z-check_one_way_stream.<locals>.do_receive_somebytes)expectedr%   c                   s    dI d H | kst d S Nr   )r:   )rO   )rM   r+   r,   checked_receive_1o   s    z/check_one_way_stream.<locals>.checked_receive_1r   )resourcer%   c              	     s$   t   |  I d H  W 5 Q R X d S r&   )r   aclose)rR   r+   r+   r,   	do_acloser   s    z'check_one_way_stream.<locals>.do_aclose   xr.   c                     s     dI d H   dI d H  d S )N       yr+   r+   rG   r+   r,   send_empty_then_y{   s    z/check_one_way_stream.<locals>.send_empty_then_yrW      1   2r   g      ?Tr9   r   r   )scoper%   c              	     s,   t     I d H  W 5 Q R X |   d S r&   )r   wait_send_all_might_not_blockcancel)r^   rE   r+   r,   *simple_check_wait_send_all_might_not_block   s    zHcheck_one_way_stream.<locals>.simple_check_wait_send_all_might_not_blockc                	     s*   t tj  dI d H  qW 5 Q R X d S )Nd   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)r?   r   BrokenResourceErrorr+   rX   r+   r,   expect_broken_stream_on_send   s    z:check_one_way_stream.<locals>.expect_broken_stream_on_sendrb   i   rV   c                     s    dI d H   I d H  d S )NrW   r+   r+   )rT   rG   rF   r+   r,   send_then_close   s    z-check_one_way_stream.<locals>.send_then_closec                     s<   t  I d H   dI d H   dI d H  I d H  d S )NrW   rV   )r   wait_all_tasks_blockedr+   )rQ   rT   rL   r+   r,   receive_send_then_close   s    z5check_one_way_stream.<locals>.receive_send_then_closes   123z"Callable[ArgsT, Awaitable[object]]z
ArgsT.argszArgsT.kwargs)afnargskwargsr%   c              	     s*   t tj | ||I d H  W 5 Q R X d S r&   )r?   r   Z	Cancelled)rh   ri   rj   r+   r+   r,   expect_cancelled'  s    z.check_one_way_stream.<locals>.expect_cancelledc                	     s*   t tj  dI d H  W 5 Q R X d S )N
   )r?   r   ClosedResourceErrorrJ   r+   rK   r+   r,   receive_expecting_closed=  s    z6check_one_way_stream.<locals>.receive_expecting_closed)cancel_scoper%   c              	     s@     d t   I d H  W 5 Q R X   d |   d S )Nwaiter sleepingwaiter wokeup)appendr   r_   r`   )ro   )recordrF   r+   r,   waiterK  s
    

z$check_one_way_stream.<locals>.waiterc                     s.   t  I d H  d  dI d H  qd S )Nreceiver startingiA  )r   rf   rr   rJ   r+   )rL   rs   r+   r,   receiverR  s    
z&check_one_way_stream.<locals>.receiverrp   ru   rq   c                	     s@   z$t     I d H  W 5 Q R X W n tjk
r:   Y nX d S r&   )r   r_   r   rc   r+   rE   r+   r,   sender  s
    z$check_one_way_stream.<locals>.senderc                     s    t  I d H  t I d H  d S r&   r   rf   r   r+   rK   r+   r,   rv     s    r   rF   r%   c                   s    t  I d H  t| I d H  d S r&   rx   rE   r+   r+   r,   
close_soon  s    z(check_one_way_stream.<locals>.close_soons   xyzzy)N)r"   r=   r   r:   r   r   open_nursery
start_soon	bytearray
memoryviewr?   
ValueErrorrJ   	TypeErrorBusyResourceErrorro   rc   rm   r   r_   r   r   r   r`   rS   rf   rD   )r@   rA   nurseryrY   ra   rd   re   rg   r^   rk   rn   rt   rv   rw   rz   r+   )rQ   rT   rM   rG   rL   rs   rF   r,   check_one_way_streamM   s@    



&(&
"

*((

(
(,
.
&88
*:r   zStreamMaker[Stream, Stream]z"StreamMaker[Stream, Stream] | Nonec                   s  t I dH  ddfdd}dk	r>ddfdd}nd}t ||I dH  t I dH 4 I dH B\}t|tstttstd}d	 td
}|d| }||d}ddddd fdd}	ddddd fdd}
t	
 4 I dH Z}||	||d
 ||	|ddd d ||
||ddd d ||
|d W 5 Q I dH R X ddfdd}t	
 4 I dH }|| ||j W 5 Q I dH R X W 5 Q I dH R X dS )u  Perform a number of generic tests on a custom two-way stream
    implementation.

    This is similar to :func:`check_one_way_stream`, except that the maker
    functions are expected to return objects implementing the
    :class:`~trio.abc.Stream` interface.

    This function tests a *superset* of what :func:`check_one_way_stream`
    checks – if you call this, then you don't need to also call
    :func:`check_one_way_stream`.

    Nztuple[Stream, Stream]r.   c                     s     I d H d d d S Nr\   r+   r+   )r@   r+   r,   flipped_stream_maker  s    z2check_two_way_stream.<locals>.flipped_stream_makerc                     s     I d H d d d S r   r+   r+   )rA   r+   r,   flipped_clogged_stream_maker  s    z:check_two_way_stream.<locals>.flipped_clogged_stream_makeri   i @  r      littler   rB   intr#   )rF   rC   seedr%   c                   sL   t |}t|}|rH|d }| |d | I d H  ||d  }qd S rP   )randomRandomr~   randintrD   )rF   rC   r   rL   m
chunk_sizeCHUNK_SIZE_MAXr+   r,   rw     s    
z$check_two_way_stream.<locals>.senderrH   c                   sZ   t |}t }t|t|k rJ| |d I d H }|s@t||7 }q||ksVtd S rP   )r   r   r}   r;   rJ   r   r:   )rF   rC   r   rL   gotchunkr   r+   r,   rv     s    

z&check_two_way_stream.<locals>.receiverr\   r   r   r   c                     s*     dI d H dkst  I d H  d S )Nrl   rV   )rJ   r:   rS   r+   )s2r+   r,   expect_receive_some_empty  s    z7check_two_way_stream.<locals>.expect_receive_some_empty)r   r"   r=   r   r:   r   r   getrandbitsto_bytesr   r{   r|   rS   )r@   rA   r   r   s1ZDUPLEX_TEST_SIZErL   iZ	test_datarw   rv   r   r   r+   )r   rA   r   r@   r,   check_two_way_stream  s2     
	 
r   z5StreamMaker[HalfCloseableStream, HalfCloseableStream]z<StreamMaker[HalfCloseableStream, HalfCloseableStream] | Nonec                   sf  t | |I dH  t|  I dH 4 I dH  \}}t|ts>tt|tsLtddddd}ddddd	}t 4 I dH }||| ||| W 5 Q I dH R X ttj	 |
d
I dH  W 5 Q R X t  | I dH  W 5 Q R X t 4 I dH }||| ||| W 5 Q I dH R X W 5 Q I dH R X |dk	rbt| I dH 4 I dH n\}}ttjddP t 4 I dH .}||j
d t I dH  ||j W 5 Q I dH R X W 5 Q R X W 5 Q I dH R X t| I dH 4 I dH l\}}ttjddN t 4 I dH ,}||j t I dH  ||j W 5 Q I dH R X W 5 Q R X W 5 Q I dH R X dS )u  Perform a number of generic tests on a custom half-closeable stream
    implementation.

    This is similar to :func:`check_two_way_stream`, except that the maker
    functions are expected to return objects that implement the
    :class:`~trio.abc.HalfCloseableStream` interface.

    This function tests a *superset* of what :func:`check_two_way_stream`
    checks – if you call this, then you don't need to also call
    :func:`check_two_way_stream`.

    Nr   r#   ry   c              	     s4   |  dI d H  t  |  I d H  W 5 Q R X d S )NrU   )rD   r   send_eofrE   r+   r+   r,   send_x_then_eof  s    z4check_half_closeable_stream.<locals>.send_x_then_eof)rL   r%   c                   sB   t  I d H  | dI d H dks&t| dI d H dks>td S )Nrl   rU   rV   )r   rf   rJ   r:   rK   r+   r+   r,   expect_x_then_eof  s    z6check_half_closeable_stream.<locals>.expect_x_then_eofrW   Tr]   rU   )r   r"   r=   r   r:   r   r{   r|   r?   rm   rD   r   r   r   rf   r_   )r@   rA   r   r   r   r   r   r+   r+   r,   check_half_closeable_stream  s:     ,
6r   )F).
__future__r   r   sys
contextlibr   r   typingr   r   r   r   r	   r
   r    r   r   _abcr   r   r   r   r   Z_highlevel_genericr   Z_checkpointsr   typesr   Ztyping_extensionsr   r   r   version_infoZexceptiongroupr   r   r    r!   __annotations__r"   r?   r   r   r   r+   r+   r+   r,   <module>   s6    $

    `P