a
    åzeà  ã                   @   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__© ú)/usr/lib/python3.9/unittest/async_case.pyr   !   s    z IsolatedAsyncioTestCase.__init__c                 Ã   s   d S r   r   ©r   r   r   r   Ú
asyncSetUp&   s    z"IsolatedAsyncioTestCase.asyncSetUpc                 Ã   s   d S r   r   r   r   r   r   ÚasyncTearDown)   s    z%IsolatedAsyncioTestCase.asyncTearDownc                O   s   | 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   s   | j |g|¢R i |¤Ž d S r   r   )r   Úfunctionr   r   r   r   r   Ú_callCleanupF   s    z$IsolatedAsyncioTestCase._callCleanupc                O   s4   ||i |¤Ž}| j  ¡ }| j ||f¡ | j  |¡S r   )r	   Úcreate_futurer
   Ú
put_nowaitÚrun_until_complete©r   r   r   r   ÚretÚfutr   r   r   r   I   s    
z"IsolatedAsyncioTestCase._callAsyncc                O   sF   ||i |¤Ž}t  |¡r>| j ¡ }| j ||f¡ | j |¡S |S d S r   )ÚinspectZisawaitabler	   r    r
   r!   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 }| ¡ s`| |¡ W q ttfyz   ‚ Y q t	t j
fy´ } z| ¡ s | |¡ W Y d }~qd }~0 0 qd S 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   sJ   t  ¡ }t  |¡ | d¡ || _| ¡ }| |  |¡¡| _| 	|¡ d S )NT)
r'   Znew_event_loopÚset_event_loopZ	set_debugr	   r    Zcreate_taskr5   Z_asyncioCallsTaskr"   )r   Úloopr%   r   r   r   Ú_setupAsyncioLoopn   s    

z)IsolatedAsyncioTestCase._setupAsyncioLoopc              
   C   s  | j }d | _ | j d ¡ | | j ¡ ¡ zÄt |¡}|s^W | | ¡ ¡ t d ¡ | 	¡  d S |D ]}| 
¡  qb| tj|ddiŽ¡ |D ]0}| ¡ r˜qŠ| ¡ d urŠ| d| ¡ |dœ¡ qŠ| | ¡ ¡ W | | ¡ ¡ t d ¡ | 	¡  n"| | ¡ ¡ t d ¡ | 	¡  0 d S )NZreturn_exceptionsTz(unhandled exception during test shutdown)ÚmessageÚ	exceptionÚtask)r	   r
   r!   r"   Újoinr'   Z	all_tasksZshutdown_default_executorr6   ÚcloseÚcancelZgatherr,   r:   Zcall_exception_handlerZshutdown_asyncgens)r   r7   Z	to_cancelr;   r   r   r   Ú_tearDownAsyncioLoopx   sB    

é
ÿý


þ
z,IsolatedAsyncioTestCase._tearDownAsyncioLoopNc                    s.   |   ¡  ztƒ  |¡W |  ¡  S |  ¡  0 d S r   )r8   r   Úrunr?   )r   Úresultr   r   r   r@   œ   s    þzIsolatedAsyncioTestCase.runc                    s   |   ¡  tƒ  ¡  |  ¡  d S r   )r8   r   Údebugr?   r   r   r   r   rB   £   s    
zIsolatedAsyncioTestCase.debugc                 C   s   | j d ur|  ¡  d S r   )r	   r?   r   r   r   r   Ú__del__¨   s    
zIsolatedAsyncioTestCase.__del__)r   )N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r   r   r   r5   r8   r?   r@   rB   rC   Ú__classcell__r   r   r   r   r      s    

$r   )r'   r&   Zcaser   r   r   r   r   r   Ú<module>   s   