
    _f                     P    d dl Z d dlZd dlZddlmZ ddlmZ  G d d          ZdS )    N   )guarantee_single_callable)timeoutc                   D    e Zd ZdZd ZddZddZd Zd Zdd	Z	ddZ
dS )ApplicationCommunicatorz}
    Runs an ASGI application in a test mode, allowing sending of
    messages to it and retrieval of messages it sends.
    c                 Z   t          |          | _        || _        t          j                    | _        t          j                    | _        t          j                    	                    t          j
        |                     || j        j        | j        j                            | _        d S N)r   applicationscopeasyncioQueueinput_queueoutput_queuecontextvarsContextruncreate_taskgetputfuture)selfr
   r   s      A/var/www/html/env/lib/python3.11/site-packages/asgiref/testing.py__init__z ApplicationCommunicator.__init__   s    4[AA
"=??#MOO
 ")++//UD$4$8$:K:OPP
 
    r   c                 ^  K   	 t          |          4 d{V  	 | j         d{V  | j                                         n# t          j        $ r Y nw xY wddd          d{V  n# 1 d{V swxY w Y   | j                                        s?| j                                         	 | j         d{V  dS # t          j        $ r Y dS w xY wdS # | j                                        s=| j                                         	 | j         d{V  w # t          j        $ r Y w w xY ww xY w)zV
        Waits for the application to stop itself and returns any exceptions.
        N)async_timeoutr   resultr   CancelledErrordonecancel)r   r   s     r   waitzApplicationCommunicator.wait   s     	$W--        +%%%%%%%K&&((((-   D	                           ;##%% ""$$$+%%%%%%%%%-   DD	 4;##%% ""$$$+%%%%%%%%-   D	s   C A)&AA)AA)AA)C )
A33C 6A37C -B< <CC3D,DD,D(%D,'D((D,Tc                     | j                                         s| j                                          d S |r| j                                          d S d S r	   )r   r   r    r   )r   
exceptionss     r   stopzApplicationCommunicator.stop0   s^    {!! 	!K      	!K     	! 	!r   c                 V    	 |                      d           d S # t          $ r Y d S w xY w)NF)r#   )r$   RuntimeError)r   s    r   __del__zApplicationCommunicator.__del__7   sC    	III''''' 	 	 	DD	s    
((c                 J   K   | j                             |           d{V  dS )z;
        Sends a single message to the application
        N)r   r   )r   messages     r   
send_inputz"ApplicationCommunicator.send_input?   s7      
 ""7+++++++++++r   c                 2  K   | j                                         r| j                                          	 t          |          4 d{V  | j                                         d{V cddd          d{V  S # 1 d{V swxY w Y   dS # t          j        $ rw}| j                                         r| j                                          n=| j                                          	 | j          d{V  n# t          j	        $ r Y nw xY w|d}~ww xY w)zX
        Receives a single message from the application, with optional timeout.
        N)
r   r   r   r   r   r   r   TimeoutErrorr    r   )r   r   es      r   receive_outputz&ApplicationCommunicator.receive_outputF   s     
 ; 	!K   	$W-- 5 5 5 5 5 5 5 5!.22444444445 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5# 
	 
	 
	{!! ""$$$$""$$$+%%%%%%%%-   DG
	s`   B A=*B =
BB 
BB DAD,C:9D:D	DDDD皙?{Gz?c                 0  K   t          j                    }t          j                    |z
  |k     rO| j                                        sdS t	          j        |           d{V  t          j                    |z
  |k     O| j                                        S )zO
        Checks that there is no message to receive in the given time.
        FN)time	monotonicr   emptyr   sleep)r   r   intervalstarts       r   receive_nothingz'ApplicationCommunicator.receive_nothing]   s      
   n&00$**,, u-))))))))) n&00  &&(((r   N)r   )T)r/   r0   )__name__
__module____qualname____doc__r   r!   r$   r'   r*   r.   r8    r   r   r   r   	   s         

 
 
   &! ! ! !  , , ,   .
) 
) 
) 
) 
) 
)r   r   )r   r   r2   compatibilityr   r   r   r   r=   r   r   <module>r?      s          4 4 4 4 4 4 - - - - - -^) ^) ^) ^) ^) ^) ^) ^) ^) ^)r   