o
    ACeU                     @  s>  U d dl mZ d dlZd dlmZ d dlmZmZ d dl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	r_d dlmZ d dlmZm Z  ed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Z'd,d!d"Z(d-d%d&Z)d.d)d*Z*dS )/    )annotationsN)	Generator)contextmanagersuppress)TYPE_CHECKING	AwaitableCallableGenericTupleTypeVar   )CancelScope_core)AsyncResourceHalfCloseableStreamReceiveStream
SendStreamStream)aclose_forcefully   )assert_checkpoints)TracebackType)	ParamSpec	TypeAliasArgsTRes1)boundRes2r   StreamMakerc                   @  s*   e Zd ZdddZdddZdddZdS )_ForceCloseBothbothtuple[Res1, Res2]returnNonec                 C  s   |\| _ | _d S N_first_second)selfr     r)   ?usr/lib/python3.10/site-packages/trio/testing/_check_streams.py__init__   s   z_ForceCloseBoth.__init__c                   s   | j | jfS r$   r%   )r(   r)   r)   r*   
__aenter__   s   z_ForceCloseBoth.__aenter__exc_typetype[BaseException] | None	exc_valueBaseException | None	tracebackTracebackType | Nonec                   s<   zt | jI d H  W t | jI d H  d S t | jI d H  w r$   )r   r&   r'   )r(   r-   r/   r1   r)   r)   r*   	__aexit__!   s   &z_ForceCloseBoth.__aexit__N)r    r!   r"   r#   )r"   r!   )r-   r.   r/   r0   r1   r2   r"   r#   )__name__
__module____qualname__r+   r,   r3   r)   r)   r)   r*   r      s    

r   exctype[BaseException]r"   Generator[None, None, None]c                 c  s4    d}zd V  W n
 | y   Y d S w t d|  )NTzexpected exception: )AssertionError)r7   Z__tracebackhide__r)   r)   r*   _assert_raises-   s   
r;   stream_maker&StreamMaker[SendStream, ReceiveStream]clogged_stream_maker-StreamMaker[SendStream, ReceiveStream] | Noner#   c                   s  t |  I dH 4 I dH \ttsJ ttsJ dFfdddGdHfdddIfdd dJddt 4 I dH }|d | d W d  I dH  n1 I dH s_w   Y  dKfdd}t 4 I dH }|| | d W d  I dH  n1 I dH sw   Y  t 4 I dH }|td | d W d  I dH  n1 I dH sw   Y  t 4 I dH }|td | d W d  I dH  n1 I dH sw   Y  t	t
 dI dH  W d   n	1 sw   Y  t	t
 dI dH  W d   n	1 s!w   Y  t	t dI dH  W d   n	1 s>w   Y  t 4 I dH }|d  I dH dks]J W d  I dH  n1 I dH snw   Y  t 4 I dH }|d dI dH dksJ W d  I dH  n1 I dH sw   Y  t	tj3 t 4 I dH }|d |d W d  I dH  n1 I dH sw   Y  W d   n	1 sw   Y  dLfd!d"}t 4 I dH }|||j |d W d  I dH  n1 I dH sw   Y  dKfd#d$}t 4 I dH }|| | W d  I dH  n1 I dH sDw   Y  t	tj d%I dH  W d   n	1 saw   Y  t	tj d&I dH  W d   n	1 s~w   Y  I dH  I dH  I dH  t	tj d%I dH  W d   n	1 sw   Y  t	tj d'I dH  W d   n	1 sw   Y  t	tj# t   I dH  W d   n	1 sw   Y  W d   n	1 sw   Y  I dH  I dH  W d  I dH  n1 I dH s"w   Y  t |  I dH 4 I dH H\dKfd(d)}dK fd*d+}t 4 I dH }|| || W d  I dH  n1 I dH slw   Y  W d  I dH  n1 I dH sw   Y  t |  I dH 4 I dH J\tI dH  t	tj 	 d%I dH  q1 sw   Y  t	tj d&I dH  W d   n	1 sw   Y  W d  I dH  n1 I dH sw   Y  t |  I dH 4 I dH N\tI dH  t	tj d-I dH  W d   n	1 sw   Y  ttj  d'I dH  W d   n	1 s4w   Y  W d  I dH  n1 I dH sJw   Y  t |  I dH 4 I dH \t }|   I dH  W d   n	1 syw   Y  t }|   I dH  W d   n	1 sw   Y  t	tj d-I dH  W d   n	1 sw   Y  t	tj d&I dH  W d   n	1 sw   Y  W d  I dH  n1 I dH sw   Y  t |  I dH 4 I dH \dMd4d5}	t 9}|  t 4 I dH }||	d ||	d W d  I dH  n1 I dH s3w   Y  W d   n	1 sCw   Y  t 4 I dH }| | W d  I dH  n1 I dH snw   Y  W d  I dH  n1 I dH sw   Y  t |  I dH 4 I dH E\fd6d7}
t 4 I dH }||
 t I dH  tI dH  W d  I dH  n1 I dH sw   Y  W d  I dH  n1 I dH sw   Y  |durt | I dH 4 I dH Z\g dNfd9d:}dKfd;d<}t 4 I dH }|||j t I dH  || W d  I dH  n1 I dH s9w   Y  g d=ksGJ W d  I dH  n1 I dH sXw   Y  t | I dH 4 I dH \t	tj3 t 4 I dH }|j |j W d  I dH  n1 I dH sw   Y  W d   n	1 sw   Y  t	tj4 t 4 I dH }|j |jd- W d  I dH  n1 I dH sw   Y  W d   n	1 sw   Y  W d  I dH  n1 I dH s w   Y  t | I dH 4 I dH P\t	tj5 t 4 I dH }|jd- |jd- W d  I dH  n1 I dH sBw   Y  W d   n	1 sRw   Y  W d  I dH  n1 I dH shw   Y  t | I dH 4 I dH D\dKfd>d?}dKfd@d<}t 4 I dH }|| || W d  I dH  n1 I dH sw   Y  W d  I dH  n1 I dH sw   Y  t | I dH 4 I dH =\tI dH  zt   I dH  W d   n	1 sw   Y  W n tjy   Y nw W d  I dH  n1 I dH sw   Y  dOdCdD}t | I dH 4 I dH P\t 4 I dH /}|| t	tj dEI dH  W d   n	1 sZw   Y  W d  I dH  n1 I dH spw   Y  W d  I dH  n1 I dH sw   Y  t | I dH 4 I dH P\t 4 I dH .}|| t	tj  I dH  W d   n	1 sw   Y  W d  I dH  n1 I dH sw   Y  W d  I dH  dS 1 I dH sw   Y  dS dS )Pa  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databytes | bytearray | memoryviewr"   r#   c                   sF   t    | I d H d u sJ W d    d S 1 sw   Y  d S r$   )r   send_all)r@   sr)   r*   do_send_allR   s   "z)check_one_way_stream.<locals>.do_send_all	max_bytes
int | Nonebytes | bytearrayc                   s<   t    | I d H W  d    S 1 sw   Y  d S r$   )r   receive_some)rF   rr)   r*   do_receive_someV   s   $z-check_one_way_stream.<locals>.do_receive_someexpectedbytesc                   s    dI d H | ksJ d S Nr   r)   )rM   )rL   r)   r*   checked_receive_1Z   s   z/check_one_way_stream.<locals>.checked_receive_1resourcer   c                   s<   t   |  I d H  W d    d S 1 sw   Y  d S r$   )r   aclose)rQ   r)   r)   r*   	do_aclose]   s   "z'check_one_way_stream.<locals>.do_aclose   xc                     s"    dI d H   dI d H  d S )N       yr)   r)   rE   r)   r*   send_empty_then_yf   s   z/check_one_way_stream.<locals>.send_empty_then_yrV      1   2r   g      ?r   scoper   c                   sB   t     I d H  W d    n1 sw   Y  |   d S r$   )r   wait_send_all_might_not_blockcancel)r\   rC   r)   r*   *simple_check_wait_send_all_might_not_block   s
   zHcheck_one_way_stream.<locals>.simple_check_wait_send_all_might_not_blockc                     s4   t tj
 	  dI d H  q1 sw   Y  d S )NTd   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)r;   r   BrokenResourceErrorr)   rW   r)   r*   expect_broken_stream_on_send   s   z:check_one_way_stream.<locals>.expect_broken_stream_on_sendr`   i   rU   c                     s"   dI d H   I d H  d S )NrV   r)   r)   )rS   rE   rD   r)   r*   send_then_close      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 )NrV   rU   )r   wait_all_tasks_blockedr)   )rP   rS   rK   r)   r*   receive_send_then_close   s
   z5check_one_way_stream.<locals>.receive_send_then_closeTs   123afn"Callable[ArgsT, Awaitable[object]]args
ArgsT.argskwargsArgsT.kwargsc                   sF   t tj | |i |I d H  W d    d S 1 sw   Y  d S r$   )r;   r   Z	Cancelled)rg   ri   rk   r)   r)   r*   expect_cancelled  s   "z.check_one_way_stream.<locals>.expect_cancelledc                     sB   t tj  dI d H  W d    d S 1 sw   Y  d S )N
   )r;   r   ClosedResourceErrorrI   r)   rJ   r)   r*   receive_expecting_closed'  s   "z6check_one_way_stream.<locals>.receive_expecting_closedcancel_scopec                   sV     d t   I d H  W d    n1 sw   Y    d |   d S )Nwaiter sleepingwaiter wokeup)appendr   r]   r^   )rq   )recordrD   r)   r*   waiter5  s   

z$check_one_way_stream.<locals>.waiterc                     s.   t  I d H  d 	  dI d H  q)Nreceiver startingTiA  )r   re   rt   rI   r)   )rK   ru   r)   r*   receiver<  s   
z&check_one_way_stream.<locals>.receiver)rr   rw   rs   c                     sX   zt     I d H  W d    W d S 1 sw   Y  W d S  tjy+   Y d S w r$   )r   r]   r   ra   r)   rC   r)   r*   senderj  s   &z$check_one_way_stream.<locals>.senderc                     s"   t  I d H  t I d H  d S r$   r   re   r   r)   rJ   r)   r*   rx   q  rd   rD   r   c                   s"   t  I d H  t| I d H  d S r$   rz   rC   r)   r)   r*   
close_soon  rd   z(check_one_way_stream.<locals>.close_soons   xyzzy)r@   rA   r"   r#   r$   )rF   rG   r"   rH   )rM   rN   r"   r#   )rQ   r   r"   r#   r"   r#   )r\   r   r"   r#   )rg   rh   ri   rj   rk   rl   r"   r#   )rq   r   r"   r#   )rD   r   r"   r#   )r   
isinstancer   r   r   open_nursery
start_soon	bytearray
memoryviewr;   
ValueErrorrI   	TypeErrorBusyResourceErrorrq   ra   ro   r   r]   r   r   r   r^   rR   re   rB   )r<   r>   nurseryrX   r_   rb   rc   rf   r\   rm   rp   rv   rx   ry   r{   r)   )rP   rS   rL   rE   rK   ru   rD   r*   check_one_way_stream8   s   
(
(((


***
*
**~

,* *
 *

 *

*,*
,*
***	* ** *

,**

 ,* ,0r   StreamMaker[Stream, Stream]"StreamMaker[Stream, Stream] | Nonec              
     s  t I dH  dfdd}durdfdd}nd}t ||I dH  t I dH 4 I dH \}t|ts<J ttsCJ d}d	 td
}|d| }||d}d  fdd}	d! fdd}
t	 4 I dH 5}|
|	||d
 |
|	|ddd d |
|
||ddd d |
|
|d W d  I dH  n1 I dH sw   Y  d"fdd}t	 4 I dH }|
| |
|j W d  I dH  n1 I dH sw   Y  W d  I dH  dS 1 I dH sw   Y  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`.

    Nr"   tuple[Stream, Stream]c                          I d H d d d S Nr[   r)   r)   )r<   r)   r*   flipped_stream_maker     z2check_two_way_stream.<locals>.flipped_stream_makerc                     r   r   r)   r)   )r>   r)   r*   flipped_clogged_stream_maker  r   z:check_two_way_stream.<locals>.flipped_clogged_stream_makeri   i @  r      littlerD   r   r@   rA   seedintr#   c                   sT   t |}t|}|r(|d }| |d | I d H  ||d  }|sd S d S rO   )randomRandomr   randintrB   )rD   r@   r   rK   m
chunk_sizeCHUNK_SIZE_MAXr)   r*   ry     s   
z$check_two_way_stream.<locals>.senderrH   c                   sj   t |}t }t|t|k r-| |d I d H }|s!J ||7 }t|t|k s||ks3J d S rO   )r   r   r   lenrI   r   )rD   r@   r   rK   Zgotchunkr   r)   r*   rx     s   
z&check_two_way_stream.<locals>.receiverr[   r   r      c                     s,     dI d H dksJ   I d H  d S )Nrn   rU   )rI   rR   r)   )s2r)   r*   expect_receive_some_empty  s   z7check_two_way_stream.<locals>.expect_receive_some_empty)r"   r   )rD   r   r@   rA   r   r   r"   r#   )rD   r   r@   rH   r   r   r"   r#   r|   )r   r   r}   r   r   r   getrandbitsto_bytesr   r~   r   rR   )r<   r>   r   r   s1ZDUPLEX_TEST_SIZErK   iZ	test_datary   rx   r   r   r)   )r   r>   r   r<   r*   check_two_way_stream  s:   

	(
*.r   5StreamMaker[HalfCloseableStream, HalfCloseableStream]<StreamMaker[HalfCloseableStream, HalfCloseableStream] | Nonec                   sj  t | |I dH  t|  I dH 4 I dH \}}t|tsJ t|ts&J ddd}dd	d
}t 4 I dH }||| ||| W d  I dH  n1 I dH sUw   Y  ttj |	dI dH  W d   n1 srw   Y  t
  | I dH  W d   n1 sw   Y  t 4 I dH }||| ||| W d  I dH  n1 I dH sw   Y  W d  I dH  n1 I dH sw   Y  |durt| I dH 4 I dH V\}}ttj; t 4 I dH }||j	d t I dH  ||j W d  I dH  n1 I dH sw   Y  W d   n	1 s(w   Y  W d  I dH  n1 I dH s>w   Y  t| I dH 4 I dH V\}}ttj: t 4 I dH }||j t I dH  ||j W d  I dH  n1 I dH sw   Y  W d   n	1 sw   Y  W d  I dH  dS 1 I dH sw   Y  dS 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`.

    NrD   r   r"   r#   c                   sL   |  dI d H  t  |  I d H  W d    d S 1 sw   Y  d S )NrT   )rB   r   send_eofrC   r)   r)   r*   send_x_then_eof  s
   "z4check_half_closeable_stream.<locals>.send_x_then_eofrK   c                   sD   t  I d H  | dI d H dksJ | dI d H dks J d S )Nrn   rT   rU   )r   re   rI   rJ   r)   r)   r*   expect_x_then_eof  s   z6check_half_closeable_stream.<locals>.expect_x_then_eofrV   rT   )rD   r   r"   r#   )rK   r   r"   r#   )r   r   r}   r   r   r~   r   r;   ro   rB   r   r   r   re   r]   )r<   r>   r   r   r   r   r   r)   r)   r*   check_half_closeable_stream  sX   

(*(
* ** 0r   )r7   r8   r"   r9   )r<   r=   r>   r?   r"   r#   )r<   r   r>   r   r"   r#   )r<   r   r>   r   r"   r#   )+
__future__r   r   collections.abcr   
contextlibr   r   typingr   r   r   r	   r
   r    r   r   _abcr   r   r   r   r   Z_highlevel_genericr   Z_checkpointsr   typesr   typing_extensionsr   r   r   r   r   r   __annotations__r   r;   r   r   r   r)   r)   r)   r*   <module>   s0     

  
_N