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   úO/var/www/html/premium_crap/venv/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çš™™™™™¹?ç{®Gáz„?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    