
vÅÏ^/  ã               @   sO   d  d l  Z  d  d l Z d d l m Z d d l m Z Gd d „  d ƒ Z d S)é    Né   )Úguarantee_single_callable)Útimeoutc               @   sy   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 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   sg   t  | ƒ |  _ | |  _ t j ƒ  |  _ t j ƒ  |  _ t j |  j | |  j j |  j j	 ƒ ƒ |  _
 d  S)N)r   ÚapplicationÚscopeÚasyncioÚQueueÚinput_queueÚoutput_queueZensure_futureÚgetÚputÚfuture)Úselfr   r   © r   ú2/tmp/pip-build-8lau8j11/asgiref/asgiref/testing.pyÚ__init__   s    	z ApplicationCommunicator.__init__r   c          !   ã   s¤   zX t  | ƒ 4Id Hš: y |  j Id H|  j j ƒ  Wn t j k
 rJ Yn XWd QId HRXWd |  j j ƒ  sŸ |  j j ƒ  y |  j Id HWn t j k
 rž Yn XXd 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    zApplicationCommunicator.waitTc             C   s6   |  j  j ƒ  s |  j  j ƒ  n | r2 |  j  j ƒ  d  S)N)r   r   r   r   )r   Ú
exceptionsr   r   r   Ústop*   s    zApplicationCommunicator.stopc             C   s-   y |  j  d d ƒ Wn t k
 r( Yn Xd  S)Nr   F)r   ÚRuntimeError)r   r   r   r   Ú__del__1   s    zApplicationCommunicator.__del__c             ã   s   |  j  j | ƒ Id 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  j ƒ  r |  j  j ƒ  y5 t | ƒ 4Id Hš |  j j ƒ  Id HSWd QId HRXWn‚ t j k
 rÕ } z_ |  j  j ƒ  rˆ |  j  j ƒ  n5 |  j  j ƒ  y |  j  Id HWn t j	 k
 r¼ Yn X| ‚ WYd d } ~ Xn Xd 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_outputgš™™™™™¹?g{®Gáz„?c             ã   s[   t  j ƒ  } x? t  j ƒ  | | k  rM |  j j ƒ  s8 d St j | ƒ Id Hq W|  j 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)Ú__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   