
    fhE                     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                       e Zd ZdZd Zed             Zed             Ze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                 f    d | _         t          |          | _        || _        d | _        d | _        d S N)_futurer   applicationscope_input_queue_output_queue)selfr   r   s      F/var/www/histauto/venv/lib/python3.11/site-packages/asgiref/testing.py__init__z ApplicationCommunicator.__init__   s6    4[AA
 !    c                 N    | j         t          j                    | _         | j         S r	   )r   asyncioQueuer   s    r   input_queuez#ApplicationCommunicator.input_queue   s"    $ 'D  r   c                 N    | j         t          j                    | _         | j         S r	   )r   r   r   r   s    r   output_queuez$ApplicationCommunicator.output_queue   s"    %!(D!!r   c                     | j         dt          j                                        t          j        |                     | j        | j        j	        | j
        j                            | _         | j         S r	   )r
   contextvarsContextrunr   create_taskr   r   r   getr   putr   s    r   futurezApplicationCommunicator.future$   sd    <
 '.0044#  J 0 4d6G6K  DL |r   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.wait3   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         }|d S |                                s|                                 d S |r|                                 d S d S r	   )r
   r&   r'   r$   )r   
exceptionsr!   s      r   stopzApplicationCommunicator.stopF   sW    >F{{}} 	MMOOOOO 	MMOOOOO	 	r   c                 V    	 |                      d           d S # t          $ r Y d S w xY w)NF)r*   )r+   RuntimeErrorr   s    r   __del__zApplicationCommunicator.__del__Q   sC    	III''''' 	 	 	DD	s    
((c                    K   | j                                         r| j                                          | j                            |           d{V  dS )z;
        Sends a single message to the application
        N)r!   r&   r$   r   r    )r   messages     r   
send_inputz"ApplicationCommunicator.send_inputY   sc      
 ; 	!K    ""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_outputd   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                   K   | j                                         r| j                                          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)	r!   r&   r$   time	monotonicr   emptyr   sleep)r   r   intervalstarts       r   receive_nothingz'ApplicationCommunicator.receive_nothing{   s      
 ; 	!K      n&00$**,, u-))))))))) n&00  &&(((r   N)r   )T)r6   r7   )__name__
__module____qualname____doc__r   propertyr   r   r!   r(   r+   r.   r1   r5   r?    r   r   r   r   	   s         
" " " ! ! X!
 " " X"
   X   &	 	 	 	  	, 	, 	,   .) ) ) ) ) )r   r   )r   r   r9   compatibilityr   r   r#   r   rE   r   r   <module>rG      s          4 4 4 4 4 4 - - - - - -@) @) @) @) @) @) @) @) @) @)r   