
    sghE                     H    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      Zy)    N   )guarantee_single_callable)timeoutc                   r    e Zd ZdZd Zed        Zed        Zed        ZddZ	ddZ
d Zd	 Zdd
ZddZy)ApplicationCommunicatorz}
    Runs an ASGI application in a test mode, allowing sending of
    messages to it and retrieval of messages it sends.
    c                 \    d | _         t        |      | _        || _        d | _        d | _        y N)_futurer   applicationscope_input_queue_output_queue)selfr   r   s      S/var/www/enzed_healthcare/enzed_env/lib/python3.12/site-packages/asgiref/testing.py__init__z ApplicationCommunicator.__init__   s-    4[A
 !    c                 d    | j                   t        j                         | _         | j                   S r	   )r   asyncioQueuer   s    r   input_queuez#ApplicationCommunicator.input_queue   s(    $ 'D   r   c                 d    | j                   t        j                         | _         | j                   S r	   )r   r   r   r   s    r   output_queuez$ApplicationCommunicator.output_queue   s(    %!(D!!!r   c                 &   | j                   zt        j                         j                  t        j
                  | j                  | j                  | j                  j                  | j                  j                              | _         | j                   S r	   )r
   contextvarsContextrunr   create_taskr   r   r   getr   putr   s    r   futurezApplicationCommunicator.future$   so    <<
 '..044##  JJ 0 0 4 4d6G6G6K6KDL ||r   c                   K   	 t        |      4 d{    	 | j                   d{    | j                  j                          ddd      d{    | j                  j                         s0| j                  j                          	 | j                   d{    yy7 7 {# t        j                  $ r Y tw xY w7 l# 1 d{  7  sw Y   |xY w7 :# t        j                  $ r Y yw xY w# | j                  j                         sJ| j                  j                          	 | j                   d{  7   w # t        j                  $ r Y w w xY ww xY ww)zV
        Waits for the application to stop itself and returns any exceptions.
        N)async_timeoutr!   resultr   CancelledErrordonecancel)r   r   s     r   waitzApplicationCommunicator.wait3   s'    	$W-  ++%%KK&&(  ;;##%""$++%% &%-- 	    &-- 	 ;;##%""$++%%-- 	 &s   EC1 B"C1 CB&B$B&C1 B?C1 4EC CC  E"C1 $B&&B<9C;B<<C?C1 CC
CC1 C C.+E-C..E15E'D=6D97D=<E=EEEEEc                     | j                   }|y |j                         s|j                          y |r|j                          y y r	   )r
   r&   r'   r$   )r   
exceptionsr!   s      r   stopzApplicationCommunicator.stopF   s6    >{{}MMOMMO r   c                 H    	 | j                  d       y # t        $ r Y y w xY w)NF)r*   )r+   RuntimeErrorr   s    r   __del__zApplicationCommunicator.__del__Q   s'    	III' 		s    	!!c                    K   | j                   j                         r| j                   j                          | j                  j	                  |       d{    y7 w)z;
        Sends a single message to the application
        N)r!   r&   r$   r   r    )r   messages     r   
send_inputz"ApplicationCommunicator.send_inputY   sD     
 ;;KK  ""7+++s   AAAAc                 r  K   | j                   j                         r| j                   j                          	 t        |      4 d{    | j                  j                          d{   cddd      d{    S 7 77 7 	# 1 d{  7  sw Y   yxY w# t        j                  $ r}| j                   j                         r| j                   j                          || j                   j                          	 | j                    d{  7   |# t        j                  $ r Y |w xY wd}~ww xY 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   es      r   receive_outputz&ApplicationCommunicator.receive_outputd   s    
 ;;KK 	$W- 5 5!..22445 5 545 5 5 5## 
	{{!""$ G ""$++%% G -- G
	s   5D7B A?B B)B*B-B 9B:B >D7?B BB BBBB D7B D4-AD/>DDDD/D,(D/+D,,D//D44D7c                   K   | j                   j                         r| j                   j                          t        j                         }t        j                         |z
  |k  rS| j
                  j                         syt        j                  |       d{    t        j                         |z
  |k  rS| j
                  j                         S 7 9w)zO
        Checks that there is no message to receive in the given time.
        FN)	r!   r&   r$   time	monotonicr   emptyr   sleep)r   r   intervalstarts       r   receive_nothingz'ApplicationCommunicator.receive_nothing{   s     
 ;;KK   nn&0$$**,--))) nn&0   &&(( *s   BCCC8CN)r   )T)g?g{Gz?)__name__
__module____qualname____doc__r   propertyr   r   r!   r(   r+   r.   r1   r5   r=    r   r   r   r   	   sf    
" ! !
 " "
  &		,.)r   r   )r   r   r7   compatibilityr   r   r#   r   rC   r   r   <module>rE      s        4 -@) @)r   