a XC?h��@s0ddlZddlZddlmZGdd�de�ZdS)�N�)�TestCasecs�eZdZd#�fdd� Zdd�Zdd�Zdd �Zd d �Zd d �Zdd�Z dd�Z dd�Z dd�Z dd�Z dd�Zdd�Zd$�fdd� Z�fdd �Zd!d"�Z�ZS)%�IsolatedAsyncioTestCase�runTestcst��|�d|_d|_dS�N)�super�__init__�_asyncioTestLoop�_asyncioCallsQueue)�selfZ methodName�� __class__��8/opt/alt/python39/lib64/python3.9/unittest/async_case.pyr!s z IsolatedAsyncioTestCase.__init__c�sdSrr�r rrr� asyncSetUp&sz"IsolatedAsyncioTestCase.asyncSetUpc�sdSrrrrrr� asyncTearDown)sz%IsolatedAsyncioTestCase.asyncTearDowncOs|j|g|�Ri|��dSr)Z addCleanup)r �func�args�kwargsrrr�addAsyncCleanup,s z'IsolatedAsyncioTestCase.addAsyncCleanupcCs|��|�|j�dSr)ZsetUp� _callAsyncrrrrr� _callSetUp;sz"IsolatedAsyncioTestCase._callSetUpcCs|�|�dSr��_callMaybeAsync)r �methodrrr�_callTestMethod?sz'IsolatedAsyncioTestCase._callTestMethodcCs|�|j�|��dSr)rrZtearDownrrrr� _callTearDownBs z%IsolatedAsyncioTestCase._callTearDowncOs|j|g|�Ri|��dSrr)r Zfunctionrrrrr� _callCleanupFsz$IsolatedAsyncioTestCase._callCleanupcOs^|jdusJd��||i|��}t�|�s8J|�d���|j��}|j�||f�|j�|�S)N�$asyncio test loop is not initializedz returned non-awaitable�r �inspectZ isawaitable� create_futurer � put_nowait�run_until_complete�r rrr�ret�futrrrrIs  z"IsolatedAsyncioTestCase._callAsynccOsX|jdusJd��||i|��}t�|�rP|j��}|j�||f�|j�|�S|SdS)Nrr r%rrrrQs   z'IsolatedAsyncioTestCase._callMaybeAsyncc �s�t��|_}|�d�|��IdH}|��|dur:dS|\}}z |IdH}|��s`|�|�Wqttfyz�Yqt tj fy�}z|��s�|� |�WYd}~qd}~00qdSr) �asyncioZQueuer Z set_result�getZ task_done� cancelled� SystemExit�KeyboardInterrupt� BaseExceptionZCancelledErrorZ set_exception)r r'ZqueueZqueryZ awaitabler&Zexrrr�_asyncioLoopRunner[s   z*IsolatedAsyncioTestCase._asyncioLoopRunnercCs\|jdusJd��t��}t�|�|�d�||_|��}|�|�|��|_|� |�dS)Nz%asyncio test loop already initializedT) r r(Znew_event_loop�set_event_loopZ set_debugr"Z create_taskr.Z_asyncioCallsTaskr$)r �loopr'rrr�_setupAsyncioLoopns  z)IsolatedAsyncioTestCase._setupAsyncioLoopc Cs&|jdusJd��|j}d|_|j�d�|�|j���z�t�|�}|spW|�|���t�d�|� �dS|D] }|� �qt|�tj |ddi��|D]0}|� �r�q�|� �dur�|�d|� �|d��q�|�|���W|�|���t�d�|� �n"|�|���t�d�|� �0dS)NrZreturn_exceptionsTz(unhandled exception during test shutdown)�message� exception�task)r r r#r$�joinr(Z all_tasksZshutdown_default_executorr/�closeZcancelZgatherr*r3Zcall_exception_handlerZshutdown_asyncgens)r r0Z to_cancelr4rrr�_tearDownAsyncioLoopxsD   � � �   � z,IsolatedAsyncioTestCase._tearDownAsyncioLoopNcs.|��zt��|�W|��S|��0dSr)r1r�runr7)r �resultr rrr8�s  �zIsolatedAsyncioTestCase.runcs|��t���|��dSr)r1r�debugr7rr rrr:�s zIsolatedAsyncioTestCase.debugcCs|jdur|��dSr)r r7rrrr�__del__�s zIsolatedAsyncioTestCase.__del__)r)N)�__name__� __module__� __qualname__rrrrrrrrrrr.r1r7r8r:r;� __classcell__rrr rrs   $ r)r(r!Zcaserrrrrr�s