o
    ".h/                     @   s:   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                   @   sP   e Zd ZdZ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   sF   t || _|| _t | _t | _t| || jj| jj	| _
d S N)r   applicationscopeasyncioQueueinput_queueoutput_queueensure_futuregetputfuture)selfr   r    r   N/var/www/supernova/superenvnew/lib/python3.10/site-packages/asgiref/testing.py__init__   s   



z ApplicationCommunicator.__init__r   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   wait   s6   (


zApplicationCommunicator.waitTc                 C   s.   | j  s| j   d S |r| j   d S d S r   )r   r   r   r   )r   
exceptionsr   r   r   stop*   s
   
zApplicationCommunicator.stopc                 C   s(   z	| j dd W d S  ty   Y d S w )NF)r   )r   RuntimeError)r   r   r   r   __del__1   s
   zApplicationCommunicator.__del__c                    s   | j |I dH  dS )z;
        Sends a single message to the application
        N)r   r   )r   messager   r   r   
send_input9   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_output@   s*   

4


z&ApplicationCommunicator.receive_output皙?{Gz?c                    sR   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)time	monotonicr   emptyr	   sleep)r   r   intervalstartr   r   r   receive_nothingW   s   

z'ApplicationCommunicator.receive_nothingN)r   )T)r$   r%   )__name__
__module____qualname____doc__r   r   r   r   r    r#   r,   r   r   r   r   r      s    
	

r   )r	   r&   compatibilityr   r   r   r   r   r   r   r   <module>   s
    