o
    ǋ|c                     @   s0   d dl Z d dlZddlmZ G dd deZdS )    N   )TestCasec                       s   e Zd Zd# fdd	Zdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zd$ fdd	Z fdd Zd!d" Z  ZS )%IsolatedAsyncioTestCaserunTestc                    s   t  | d | _d | _d S N)super__init___asyncioTestLoop_asyncioCallsQueue)selfZ
methodName	__class__ %  /home/conda/feedstock_root/build_artifacts/python-split_1669104742391/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehol/lib/python3.10/unittest/async_case.pyr   !   s   
z IsolatedAsyncioTestCase.__init__c                       d S r   r   r   r   r   r   
asyncSetUp&      z"IsolatedAsyncioTestCase.asyncSetUpc                    r   r   r   r   r   r   r   asyncTearDown)   r   z%IsolatedAsyncioTestCase.asyncTearDownc                O      | j |g|R i | d S r   )Z
addCleanup)r   funcargskwargsr   r   r   addAsyncCleanup,   s   z'IsolatedAsyncioTestCase.addAsyncCleanupc                 C   s   |    | | j d S r   )ZsetUp
_callAsyncr   r   r   r   r   
_callSetUp;   s   z"IsolatedAsyncioTestCase._callSetUpc                 C   s   |  | d S r   _callMaybeAsync)r   methodr   r   r   _callTestMethod?   s   z'IsolatedAsyncioTestCase._callTestMethodc                 C   s   |  | j |   d S r   )r   r   ZtearDownr   r   r   r   _callTearDownB   s   z%IsolatedAsyncioTestCase._callTearDownc                 O   r   r   r   )r   functionr   r   r   r   r   _callCleanupF   s   z$IsolatedAsyncioTestCase._callCleanupc                O   s^   | j d us	J d||i |}t|sJ |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   r   r   r   retfutr   r   r   r   I   s   
z"IsolatedAsyncioTestCase._callAsyncc                O   sT   | j d us	J d||i |}t|r(| j  }| j||f | j |S |S )Nr#   r$   r)   r   r   r   r   Q   s   

z'IsolatedAsyncioTestCase._callMaybeAsyncc              
      s   t   | _}|d  	 | I d H }|  |d u rd S |\}}z|I d H }| s2|| W n' ttfy=     t	t j
fyZ } z| sP|| W Y d }~nd }~ww qr   )asyncioQueuer
   
set_resultget	task_done	cancelled
SystemExitKeyboardInterruptBaseExceptionCancelledErrorset_exception)r   r+   queuequeryZ	awaitabler*   exr   r   r   _asyncioLoopRunner[   s,   



z*IsolatedAsyncioTestCase._asyncioLoopRunnerc                 C   s\   | j d u s	J dt }t| |d || _ | }|| || _|	| d S )Nz%asyncio test loop already initializedT)
r	   r,   Znew_event_loopset_event_loopZ	set_debugr&   Zcreate_taskr:   Z_asyncioCallsTaskr(   )r   loopr+   r   r   r   _setupAsyncioLoopn   s   

z)IsolatedAsyncioTestCase._setupAsyncioLoopc              
   C   s$  | j d us	J d| j }d | _ | jd  || j  zct|}|s8W ||  td  |	  d S |D ]}|
  q:|tj|ddi |D ]}| rUqN| d urf|d| |d qN||  W ||  td  |	  d S ||  td  |	  w )Nr#   Zreturn_exceptionsTz(unhandled exception during test shutdown)message	exceptiontask)r	   r
   r'   r(   joinr,   Z	all_tasksZshutdown_default_executorr;   closecancelZgatherr1   r?   Zcall_exception_handlerZshutdown_asyncgens)r   r<   Z	to_cancelr@   r   r   r   _tearDownAsyncioLoopx   sD   





z,IsolatedAsyncioTestCase._tearDownAsyncioLoopNc                    s*   |    zt |W |   S |   w r   )r=   r   runrD   )r   resultr   r   r   rE      s   zIsolatedAsyncioTestCase.runc                    s   |    t   |   d S r   )r=   r   debugrD   r   r   r   r   rG      s   
zIsolatedAsyncioTestCase.debugc                 C   s   | j d ur|   d S d S r   )r	   rD   r   r   r   r   __del__   s   
zIsolatedAsyncioTestCase.__del__)r   r   )__name__
__module____qualname__r   r   r   r   r   r   r    r"   r   r   r:   r=   rD   rE   rG   rH   __classcell__r   r   r   r   r      s"    

$r   )r,   r%   caser   r   r   r   r   r   <module>   s    