File: //usr/lib/python3/dist-packages/asgiref/__pycache__/testing.cpython-310.pyc
o
�6�a/ � @ s: d dl Z d dlZddlmZ ddlmZ G dd� d�ZdS )� N� )�guarantee_single_callable)�timeoutc @ sP e Zd ZdZdd� Zddd�Zddd �Zd
d� Zdd
� Zddd�Z ddd�Z
dS )�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�| �|| jj| jj ��| _
d S �N)r �application�scope�asyncio�Queue�input_queue�output_queue�
ensure_future�get�put�future)�selfr r � r �1/usr/lib/python3/dist-packages/asgiref/testing.py�__init__ s
�z ApplicationCommunicator.__init__r c � s� �zXt |�4 I dH �# z
| jI dH | j�� W n
tjy" Y nw W d �I dH n1 I dH s3w Y W | j�� sX| j�� z | jI dH W dS tjyW Y dS w dS | j�� sw| j�� z| jI dH W w tjyv 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 ���(��
��
��zApplicationCommunicator.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
�zApplicationCommunicator.stopc C s( z | j dd� W d S ty Y d S w )NF)r )r �RuntimeError)r r r r �__del__1 s
�zApplicationCommunicator.__del__c � s �| j �|�I dH dS )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 dH � | j�� I dH W d �I dH W S 1 I dH s.w Y W dS tjyh } z&| j �� rJ| j �� |�| j �� z | j I dH W |� tj yc Y |�w d}~ww )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 � sR �t �� }t �� | |k r$| j�� sdS t�|�I dH 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&