a
    ÙwhE  ã                   @   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   úI/var/www/battlenet.uz/venv/lib/python3.9/site-packages/asgiref/testing.pyÚ__init__   s
    
z ApplicationCommunicator.__init__c                 C   s   | j d u rt ¡ | _ | j S r   )r
   ÚasyncioÚQueue©r   r   r   r   Úinput_queue   s    

z#ApplicationCommunicator.input_queuec                 C   s   | j d u rt ¡ | _ | j S r   )r   r   r   r   r   r   r   Úoutput_queue   s    

z$ApplicationCommunicator.output_queuec                 C   s8   | j d u r2t ¡  tj|  | j| jj	| j
j¡¡| _ | j S r   )r   ÚcontextvarsÚContextÚrunr   Zcreate_taskr   r	   r   Úgetr   Úputr   r   r   r   Úfuture$   s    
ÿþzApplicationCommunicator.futurer   c                 Ã   sì   zªt |ƒ4 I dH šF z| jI dH  | j ¡  W n tjyB   Y n0 W d  ƒI dH  qn1 I dH sd0    Y  W | j ¡ sè| j ¡  z| jI dH  W qè tjy¨   Y qè0 n<| j ¡ sæ| j ¡  z| jI dH  W n tjyä   Y n0 0 dS )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   s&    2

û

zApplicationCommunicator.waitTc                 C   s4   | j }|d u rd S | ¡ s$| ¡  n|r0| ¡  d S r   )r   r   r   r   )r   Ú
exceptionsr   r   r   r   ÚstopF   s    
zApplicationCommunicator.stopc                 C   s(   z| j dd W n ty"   Y n0 d S )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  ¡  zRt|ƒ4 I dH š( | j ¡ I dH W  d  ƒI dH  W S 1 I dH sZ0    Y  W np tjyÖ } zV| j  ¡ rŽ| j  ¡  n0| j  ¡  z| j I dH  W n tj	y¼   Y n0 |‚W Y d}~n
d}~0 0 dS )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    

@

z&ApplicationCommunicator.receive_outputçš™™™™™¹?ç{®Gáz„?c                 Ã   sV   | j  ¡ r| j  ¡  t ¡ }t ¡ | |k rL| j ¡ s:dS t |¡I dH  q| 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*   r+   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r   r    r"   r$   r&   r)   r2   r   r   r   r   r   	   s   	





r   )r   r   r,   Zcompatibilityr   r   r   r   r   r   r   r   Ú<module>   s
   