U
    ³©Ža/  ć                   @   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   ś~/home/adriano.carvalho/ftp/files/BrinquedotecaVirtual/brinquedotecavirtual/venv/lib/python3.8/site-packages/asgiref/testing.pyŚ__init__   s    


’z ApplicationCommunicator.__init__r   c                 Ć   s   zXt|4 I dH 8 z| j I dH  | j  ”  W n tjk
rD   Y nX W 5 Q I dH R X W 5 | j  ” s| j  ”  z| j I dH  W n tjk
r   Y nX X dS )zV
        Waits for the application to stop itself and returns any exceptions.
        N)r   ŚdoneŚcancelr	   ŚCancelledErrorŚasync_timeoutŚresult)r   r   r   r   r   Śwait   s    

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k
r$   Y nX 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  ”  zFt|4 I dH & | j ” I dH W  5 Q I dH R £ W S Q I dH R X W np tjk
rŹ } zP| j  ” r| j  ”  n2| j  ”  z| j I dH  W n tj	k
r“   Y nX |W 5 d}~X Y nX 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    

4

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   