
    Jn6h                     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                   B    e Zd ZdZ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                 v   t        |      | _        || _        t        j                         | _        t        j                         | _        t        j                         j                  t        j                  | j                  || j
                  j                  | j                  j                              | _        y N)r   applicationscopeasyncioQueueinput_queueoutput_queuecontextvarsContextruncreate_taskgetputfuture)selfr
   r   s      `/var/www/django-portfolio.reubendavern.link/venv/lib/python3.12/site-packages/asgiref/testing.py__init__z ApplicationCommunicator.__init__   s    4[A
"==?#MMO
 "))+//UD$4$4$8$8$:K:K:O:OP
    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.wait   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                   j                         s| j                   j                          y |r| j                   j                          y y r	   )r   r   r    r   )r   
exceptionss     r   stopzApplicationCommunicator.stop0   s:    {{!KK KK  r   c                 H    	 | j                  d       y # t        $ r Y y w xY w)NF)r#   )r$   RuntimeError)r   s    r   __del__zApplicationCommunicator.__del__7   s'    	III' 		s    	!!c                 V   K   | j                   j                  |       d{    y7 w)z;
        Sends a single message to the application
        N)r   r   )r   messages     r   
send_inputz"ApplicationCommunicator.send_input?   s"     
 ""7+++s   )')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_outputF   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                 D  K   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)time	monotonicr   emptyr   sleep)r   r   intervalstarts       r   receive_nothingz'ApplicationCommunicator.receive_nothing]   s     
  nn&0$$**,--))) nn&0   &&(( *s   A"B $B%B B N)r   )T)g?g{Gz?)__name__
__module____qualname____doc__r   r!   r$   r'   r*   r.   r6    r   r   r   r   	   s*    

&!,.
)r   r   )r   r   r0   compatibilityr   r   r   r   r;   r   r   <module>r=      s        4 -^) ^)r   