o 6a/ @s:ddlZddlZddlmZddlmZGdddZdS)N)guarantee_single_callable)timeoutc@sPeZdZdZddZdddZddd Zd d Zd d ZdddZ dddZ dS)ApplicationCommunicatorz} Runs an ASGI application in a test mode, allowing sending of messages to it and retrieval of messages it sends. cCsFt||_||_t|_t|_t|||jj|jj |_ dSN) r applicationscopeasyncioQueue input_queue output_queue ensure_futuregetputfuture)selfrrr1/usr/lib/python3/dist-packages/asgiref/testing.py__init__s    z ApplicationCommunicator.__init__rcszXt|4IdH#z |jIdH|jWn tjy"YnwWdIdHn 1IdHs3wYW|jsX|jz |jIdHWdStjyWYdSwdS|jsw|jz|jIdHWwtjyvYwww)zV Waits for the application to stop itself and returns any exceptions. N) async_timeoutrresultr CancelledErrordonecancel)rrrrrwaits6 (   zApplicationCommunicator.waitTcCs.|js |jdS|r|jdSdSr)rrrr)r exceptionsrrrstop*s zApplicationCommunicator.stopcCs(z |jddWdStyYdSw)NF)r)r RuntimeError)rrrr__del__1s  zApplicationCommunicator.__del__cs|j|IdHdS)z; Sends a single message to the application N)r r)rmessagerrr send_input9sz"ApplicationCommunicator.send_inputc s|jr |jz*t|4IdH|jIdHWdIdHWS1IdHs.wYWdStjyh}z&|jrJ|j||jz |jIdHW|tj ycY|wd}~ww)zX Receives a single message from the application, with optional timeout. N) rrrrr rr TimeoutErrorrr)rrerrrreceive_output@s*  4   z&ApplicationCommunicator.receive_output皙?{Gz?csRt}t||kr$|jsdSt|IdHt||ks |jS)zO Checks that there is no message to receive in the given time. FN)time monotonicr emptyr sleep)rrintervalstartrrrreceive_nothingWs  z'ApplicationCommunicator.receive_nothingN)r)T)r$r%) __name__ __module__ __qualname____doc__rrrrr r#r,rrrrrs  r)r r& compatibilityrrrrrrrrs