o
    'hE                     @   sB   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 dZdS )    N   )guarantee_single_callable)timeoutc                   @   st   e Zd ZdZdd Zedd Zedd Zedd	 ZdddZ	dddZ
dd Zdd ZdddZd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                 C   s&   d | _ t|| _|| _d | _d | _d S N)_futurer   applicationscope_input_queue_output_queue)selfr   r	    r   C/var/www/html/myenv/lib/python3.10/site-packages/asgiref/testing.py__init__   s
   

z ApplicationCommunicator.__init__c                 C      | j d u r
t | _ | j S r   )r
   asyncioQueuer   r   r   r   input_queue      

z#ApplicationCommunicator.input_queuec                 C   r   r   )r   r   r   r   r   r   r   output_queue   r   z$ApplicationCommunicator.output_queuec                 C   s8   | j d u rt tj| | j| jj	| j
j| _ | j S r   )r   contextvarsContextrunr   create_taskr   r	   r   getr   putr   r   r   r   future$   s   
zApplicationCommunicator.futurer   c                    s   zXt |4 I dH # z| jI dH  | j  W n
 tjy"   Y nw W d  I dH  n1 I dH s3w   Y  W | j sX| j  z	| jI dH  W dS  tjyW   Y dS w dS | j sw| j  z| jI dH  W w  tjyv   Y w w w )zV
        Waits for the application to stop itself and returns any exceptions.
        N)async_timeoutr   resultr   CancelledErrordonecancel)r   r   r   r   r   wait3   s6   (


zApplicationCommunicator.waitTc                 C   s:   | j }|d u r	d S | s|  d S |r|  d S d S r   )r   r!   r"   r   )r   
exceptionsr   r   r   r   stopF   s   zApplicationCommunicator.stopc                 C   s(   z	| j dd W d S  ty   Y d S w )NF)r$   )r%   RuntimeErrorr   r   r   r   __del__Q   s
   zApplicationCommunicator.__del__c                    s,   | j  r| j   | j|I dH  dS )z;
        Sends a single message to the application
        N)r   r!   r   r   r   )r   messager   r   r   
send_inputY   s   

z"ApplicationCommunicator.send_inputc                    s   | j  r| j   z*t|4 I dH  | j I dH W  d  I dH  W S 1 I dH s.w   Y  W dS  tjyh } z&| j  rJ| j   || j   z	| j I dH  W | tj	yc   Y |w d}~ww )zX
        Receives a single message from the application, with optional timeout.
        N)
r   r!   r   r   r   r   r   TimeoutErrorr"   r    )r   r   er   r   r   receive_outputd   s*   

4


z&ApplicationCommunicator.receive_output皙?{Gz?c                    sf   | j  r| j   t }t | |k r.| j sdS t|I dH  t | |k s| 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startr   r   r   receive_nothing{   s   



z'ApplicationCommunicator.receive_nothingN)r   )T)r-   r.   )__name__
__module____qualname____doc__r   propertyr   r   r   r#   r%   r'   r)   r,   r5   r   r   r   r   r   	   s    	





r   )r   r   r/   compatibilityr   r   r   r   r   r   r   r   <module>   s    