o
    ˜»áfà  ã                   @   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)ÚselfÚ
methodName©Ú	__class__© ú*/usr/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   )Ú
addCleanup)r   ÚfuncÚargsÚkwargsr   r   r   ÚaddAsyncCleanup,   s   z'IsolatedAsyncioTestCase.addAsyncCleanupc                 C   s   |   ¡  |  | j¡ d S r   )Ú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   Ú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	   ÚinspectÚ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   r0   ÚqueueÚqueryÚ	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	   r1   Únew_event_loopÚset_event_loopÚ	set_debugr+   Úcreate_taskr@   Ú_asyncioCallsTaskr-   )r   Úloopr0   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'   Úreturn_exceptionsTz(unhandled exception during test shutdown)ÚmessageÚ	exceptionÚtask)r	   r
   r,   r-   Újoinr1   Ú	all_tasksÚshutdown_default_executorrB   ÚcloseÚcancelÚgatherr6   rJ   Úcall_exception_handlerÚshutdown_asyncgens)r   rF   Ú	to_cancelrK   r   r   r   Ú_tearDownAsyncioLoopx   sD   

ë
ÿý€
þ

z,IsolatedAsyncioTestCase._tearDownAsyncioLoopNc                    s*   |   ¡  ztƒ  |¡W |  ¡  S |  ¡  w r   )rG   r   ÚrunrU   )r   Úresultr   r   r   rV   œ   s   zIsolatedAsyncioTestCase.runc                    s   |   ¡  tƒ  ¡  |  ¡  d S r   )rG   r   ÚdebugrU   r   r   r   r   rX   £   s   
zIsolatedAsyncioTestCase.debugc                 C   s   | j d ur|  ¡  d S d S r   )r	   rU   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@   rG   rU   rV   rX   rY   Ú__classcell__r   r   r   r   r      s"    

$r   )r1   r)   Úcaser   r   r   r   r   r   Ú<module>   s    