a
    4¤Xh/  ã                   @   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_queueZensure_futureÚgetÚputÚfuture)Úselfr   r   © r   úT/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/site-packages/asgiref/testing.pyÚ__init__   s    


ÿz ApplicationCommunicator.__init__r   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   Úwait   s&    2

û

zApplicationCommunicator.waitTc                 C   s(   | j  ¡ s| j  ¡  n|r$| j  ¡  d S r   )r   r   r   r   )r   Ú
exceptionsr   r   r   Ústop*   s    
zApplicationCommunicator.stopc                 C   s(   z| j dd W n ty"   Y n0 d S )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  ¡  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_output@   s    

@

z&ApplicationCommunicator.receive_outputçš™™™™™¹?ç{®Gáz„?c                 Ã   sB   t  ¡ }t  ¡ | |k r8| 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)ÚtimeÚ	monotonicr   Úemptyr	   Úsleep)r   r   ÚintervalÚstartr   r   r   Úreceive_nothingW   s    
z'ApplicationCommunicator.receive_nothingN)r   )T)r   )r#   r$   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r"   r+   r   r   r   r   r      s   	


r   )r	   r%   Zcompatibilityr   r   r   r   r   r   r   r   Ú<module>   s   