U
    ”¼pd²  ã                   @   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‡  ZS )!ÚIsolatedAsyncioTestCaseÚrunTestc                    s   t ƒ  |¡ d | _d | _d S ©N)ÚsuperÚ__init__Ú_asyncioTestLoopÚ_asyncioCallsQueue)ÚselfZ
methodName©Ú	__class__© ú)/usr/lib/python3.8/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 |f|˜|Ž 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   Ú_callTearDownC   s    z%IsolatedAsyncioTestCase._callTearDownc                 O   s   | j |f|ž|Ž d S r   r   )r   Zfunctionr   r   r   r   r   Ú_callCleanupG   s    z$IsolatedAsyncioTestCase._callCleanupc                O   sL   | j d k	st‚|||Ž}t |¡s&t‚| j  ¡ }| j ||f¡ | j  |¡S r   ©r	   ÚAssertionErrorÚinspectZisawaitableÚcreate_futurer
   Ú
put_nowaitÚrun_until_complete©r   r   r   r   ÚretÚfutr   r   r   r   J   s    

z"IsolatedAsyncioTestCase._callAsyncc                O   sP   | j d k	st‚|||Ž}t |¡rH| j  ¡ }| j ||f¡ | j  |¡S |S d S r   r   r%   r   r   r   r   R   s    


z'IsolatedAsyncioTestCase._callMaybeAsyncc              
   Ã   s¼   t  ¡  | _}| d ¡ | ¡ I d H }| ¡  |d kr:d S |\}}z |I d H }| ¡ s`| |¡ W q ttfk
r|   ‚ Y q t	t j
fk
r´ } z| ¡ s¤| |¡ W 5 d }~X Y qX qd S r   )ÚasyncioZQueuer
   Z
set_resultÚgetZ	task_doneÚ	cancelledÚ
SystemExitÚKeyboardInterruptÚBaseExceptionZCancelledErrorZset_exception)r   r'   ZqueueZqueryZ	awaitabler&   Zexr   r   r   Ú_asyncioLoopRunner\   s     

z*IsolatedAsyncioTestCase._asyncioLoopRunnerc                 C   sX   | j d kst‚t ¡ }t |¡ | d¡ || _ | ¡ }| |  |¡¡| _	| 
|¡ d S )NT)r	   r    r(   Znew_event_loopÚset_event_loopZ	set_debugr"   Zcreate_taskr.   Z_asyncioCallsTaskr$   )r   Úloopr'   r   r   r   Ú_setupAsyncioLoopo   s    

z)IsolatedAsyncioTestCase._setupAsyncioLoopc              	   C   sØ   | j d k	st‚| j }d | _ | j d ¡ | | j ¡ ¡ zˆt 	|¡}|sNW ¢vd S |D ]}| 
¡  qR| tj||ddœŽ¡ |D ]0}| ¡ rŠq|| ¡ d k	r|| d| ¡ |dœ¡ q|| | ¡ ¡ W 5 t d ¡ | ¡  X d S )NT)r0   Zreturn_exceptionsz(unhandled exception during test shutdown)ÚmessageÚ	exceptionÚtask)r	   r    r
   r#   r$   Újoinr(   r/   ÚcloseZ	all_tasksZcancelZgatherr*   r3   Zcall_exception_handlerZshutdown_asyncgens)r   r0   Z	to_cancelr4   r   r   r   Ú_tearDownAsyncioLoopy   s4    

ÿý

z,IsolatedAsyncioTestCase._tearDownAsyncioLoopNc                    s(   |   ¡  ztƒ  |¡W ¢S |  ¡  X d S r   )r1   r7   r   Úrun)r   Úresultr   r   r   r8   ›   s    zIsolatedAsyncioTestCase.run)r   )N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r   r   r   r.   r1   r7   r8   Ú__classcell__r   r   r   r   r      s   

"r   )r(   r!   Zcaser   r   r   r   r   r   Ú<module>   s   