o
    KXh}/                     @   s^   d dl Z d dlZd dlmZ G dd deZdd ZG dd dejZe	d	kr-e
  dS dS )
    N)supportc                   @   s   e Zd ZdS )MyExceptionN)__name__
__module____qualname__ r   r   G/opt/python-3.10.19/usr/lib/python3.10/unittest/test/test_async_case.pyr      s    r   c                   C   s   t d  d S N)asyncioZset_event_loop_policyr   r   r   r   tearDownModule
   s   r   c                   @   s`   e Zd Z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S )TestAsyncCaseNc                 C   s   |  tj d S r	   )
addCleanupr   
gc_collectselfr   r   r   setUp   s   zTestAsyncCase.setUpc                    s   g dG  fdddt j}g  |d}| }| |jg  | |jg  |   g  |d}|  |   |  |   d S )N)r   
asyncSetUptestasyncTearDowntearDowncleanup6cleanup5cleanup4cleanup3cleanup2cleanup1c                       s   e Zd Z fddZ fddZ fddZ fddZ fd	d
Z fddZ fddZ	 fddZ
 fddZ fddZ fddZdS )z+TestAsyncCase.test_full_cycle.<locals>.Testc                    s2   |   g   d | | j | | j d S )Nr   )assertEqualappendr   on_cleanup1addAsyncCleanupon_cleanup2r   eventsr   r   r   #   s   
z1TestAsyncCase.test_full_cycle.<locals>.Test.setUpc                    <   |   d d   d | | j | | j d S )N   r   )r   r   r   on_cleanup3r   on_cleanup4r   r"   Zexpectedr   r   r   )   
   
z6TestAsyncCase.test_full_cycle.<locals>.Test.asyncSetUpc                    r#   )N   r   )r   r   r   on_cleanup5r   on_cleanup6r   r'   r   r   	test_func/   r(   z5TestAsyncCase.test_full_cycle.<locals>.Test.test_funcc                    $   |   d d   d d S )N   r   r   r   r   r'   r   r   r   5      z9TestAsyncCase.test_full_cycle.<locals>.Test.asyncTearDownc                    "   |   d d   d d S )N   r   r/   r   r'   r   r   r   9      z4TestAsyncCase.test_full_cycle.<locals>.Test.tearDownc                    r1   )N
   r   r/   r   r'   r   r   r   =   r3   z7TestAsyncCase.test_full_cycle.<locals>.Test.on_cleanup1c                    r-   )N	   r   r/   r   r'   r   r   r    A   r0   z7TestAsyncCase.test_full_cycle.<locals>.Test.on_cleanup2c                    r1   )N   r   r/   r   r'   r   r   r%   E   r3   z7TestAsyncCase.test_full_cycle.<locals>.Test.on_cleanup3c                    r-   )N   r   r/   r   r'   r   r   r&   I   r0   z7TestAsyncCase.test_full_cycle.<locals>.Test.on_cleanup4c                    r1   )N   r   r/   r   r'   r   r   r*   M   r3   z7TestAsyncCase.test_full_cycle.<locals>.Test.on_cleanup5c                    r-   )N   r   r/   r   r'   r   r   r+   Q   r0   z7TestAsyncCase.test_full_cycle.<locals>.Test.on_cleanup6N)r   r   r   r   r   r,   r   r   r   r    r%   r&   r*   r+   r   r'   r   r   Test"   s    r:   r,   )unittestIsolatedAsyncioTestCaserunr   errorsZfailuresdebug
doCleanupsr   r:   r   resultr   r'   r   test_full_cycle   s   3zTestAsyncCase.test_full_cyclec                    s   G  fdddt j}g  |d}| }|  ddg | |jd d | | d|jd d  g  |d}| |j z|	  W n	 t
yN   Y nw | d	 |  dg |  |  ddg d S )
Nc                       <   e Zd Z fddZ fddZ fddZ fddZd	S )
z3TestAsyncCase.test_exception_in_setup.<locals>.Testc                         d | | j t Nr   r   r   
on_cleanupr   r   r!   r   r   r   e      
z>TestAsyncCase.test_exception_in_setup.<locals>.Test.asyncSetUpc                         d d S Nr   r   r   r!   r   r   r,   j      z=TestAsyncCase.test_exception_in_setup.<locals>.Test.test_funcc                    rJ   Nr   rL   r   r!   r   r   r   m   rM   zATestAsyncCase.test_exception_in_setup.<locals>.Test.asyncTearDownc                    rJ   NcleanuprL   r   r!   r   r   rH   p   rM   z>TestAsyncCase.test_exception_in_setup.<locals>.Test.on_cleanupNr   r   r   r   r,   r   rH   r   r!   r   r   r:   d   s
    r:   r,   r   rP   r   r   r$    Expected a MyException exceptionr;   r<   r=   r   ZassertIsr>   assertInr   _tearDownAsyncioLoopr?   r   failr@   rA   r   r!   r   test_exception_in_setupc   s&   
z%TestAsyncCase.test_exception_in_setupc                    s   G  fdddt j}g  |d}| }|  g d | |jd d | | d|jd d  g  |d}| |j z|	  W n	 t
yN   Y nw | d |  d	d
g |  |  g d d S )Nc                       rD   )
z2TestAsyncCase.test_exception_in_test.<locals>.Testc                    rJ   rF   rL   r   r!   r   r   r      rM   z=TestAsyncCase.test_exception_in_test.<locals>.Test.asyncSetUpc                    rE   rK   rG   r   r!   r   r   r,      rI   z<TestAsyncCase.test_exception_in_test.<locals>.Test.test_funcc                    rJ   rN   rL   r   r!   r   r   r      rM   z@TestAsyncCase.test_exception_in_test.<locals>.Test.asyncTearDownc                    rJ   rO   rL   r   r!   r   r   rH      rM   z=TestAsyncCase.test_exception_in_test.<locals>.Test.on_cleanupNrQ   r   r!   r   r   r:      s
    r:   r,   r   r   r   rP   r   r   r$   rR   r   r   r   r   rP   rS   rA   r   r!   r   test_exception_in_test   &   
z$TestAsyncCase.test_exception_in_testc                    s   G  fdddt j}g  |d}| }|  g d | |jd d | | d|jd d  g  |d}| |j z|	  W n	 t
yN   Y nw | d |  g d	 |  |  g d d S )
Nc                       rD   )
z7TestAsyncCase.test_exception_in_tear_down.<locals>.Testc                    rJ   rF   rL   r   r!   r   r   r      rM   zBTestAsyncCase.test_exception_in_tear_down.<locals>.Test.asyncSetUpc                    s     d | | j d S rK   )r   r   rH   r   r!   r   r   r,      s   
zATestAsyncCase.test_exception_in_tear_down.<locals>.Test.test_funcc                         d t rN   r   r   r   r!   r   r   r         
zETestAsyncCase.test_exception_in_tear_down.<locals>.Test.asyncTearDownc                    rJ   rO   rL   r   r!   r   r   rH      rM   zBTestAsyncCase.test_exception_in_tear_down.<locals>.Test.on_cleanupNrQ   r   r!   r   r   r:      s
    r:   r,   rX   r   r   r$   rR   )r   r   r   rS   rA   r   r!   r   test_exception_in_tear_down   r[   z)TestAsyncCase.test_exception_in_tear_downc                    s   G  fdddt j}g  |d}| }|  g d | |jd d | | d|jd d  | d|jd d  g  |d}| |j z|	  W n	 t
yY   Y nw | d	 |  g d
 |  |  g d d S )Nc                       sH   e Zd Z fddZ fddZ fddZ fddZ fd	d
ZdS )z;TestAsyncCase.test_exception_in_tear_clean_up.<locals>.Testc                    rJ   rF   rL   r   r!   r   r   r      rM   zFTestAsyncCase.test_exception_in_tear_clean_up.<locals>.Test.asyncSetUpc                    s(     d | | j | | j d S rK   )r   r   r   r    r   r!   r   r   r,      s   
zETestAsyncCase.test_exception_in_tear_clean_up.<locals>.Test.test_funcc                    rJ   rN   rL   r   r!   r   r   r      rM   zITestAsyncCase.test_exception_in_tear_clean_up.<locals>.Test.asyncTearDownc                         d td)Nr   z
some errorr]   r   r!   r   r   r         
zGTestAsyncCase.test_exception_in_tear_clean_up.<locals>.Test.on_cleanup1c                    r`   )Nr   zother errorr]   r   r!   r   r   r       ra   zGTestAsyncCase.test_exception_in_tear_clean_up.<locals>.Test.on_cleanup2N)r   r   r   r   r,   r   r   r    r   r!   r   r   r:      s    r:   r,   )r   r   r   r   r   r   zMyException: other errorr$   zMyException: some errorrR   )r   r   r   r   rS   rA   r   r!   r   test_exception_in_tear_clean_up   s(   
z-TestAsyncCase.test_exception_in_tear_clean_upc                    s>   g  G  fdddt j}|d}|  |  g d d S )Nc                       s,   e Zd Zdd Z fddZ fddZdS )z:TestAsyncCase.test_cleanups_interleave_order.<locals>.Testc                    s>   |  | jd |  | jd |  | jd |  | jd d S )Nr$   r)   r.   r2   )r   on_sync_cleanupon_async_cleanupr   r   r   r   r,      s
   zDTestAsyncCase.test_cleanups_interleave_order.<locals>.Test.test_funcc                         d|  d S )Nzsync_cleanup rL   r   valr!   r   r   rc        zJTestAsyncCase.test_cleanups_interleave_order.<locals>.Test.on_sync_cleanupc                    re   )Nzasync_cleanup rL   rf   r!   r   r   rd     rh   zKTestAsyncCase.test_cleanups_interleave_order.<locals>.Test.on_async_cleanupN)r   r   r   r,   rc   rd   r   r!   r   r   r:      s    r:   r,   )zasync_cleanup 4zsync_cleanup 3zasync_cleanup 2zsync_cleanup 1)r;   r<   r=   r   )r   r:   r   r   r!   r   test_cleanups_interleave_order   s
   z,TestAsyncCase.test_cleanups_interleave_orderc                    sz   g  G  fdddt j}|d}| }| |  |d}|  |  ddg |d}| }| |  d S )Nc                       s,   e Zd Z fddZ fddZdd ZdS )zATestAsyncCase.test_base_exception_from_async_method.<locals>.Testc                    r\   )N	test_base)r   BaseExceptionr   r!   r   r   rj     r^   zKTestAsyncCase.test_base_exception_from_async_method.<locals>.Test.test_basec                    rJ   )Ntest_no_errrL   r   r!   r   r   rl     rM   zMTestAsyncCase.test_base_exception_from_async_method.<locals>.Test.test_no_errc                    s
   t  r	   )r
   CancelledErrorr   r   r   r   test_cancel  s   zMTestAsyncCase.test_base_exception_from_async_method.<locals>.Test.test_cancelN)r   r   r   rj   rl   rn   r   r!   r   r   r:     s    r:   rj   rl   rn   )r;   r<   r=   ZassertFalseZwasSuccessfulr   r   r:   r   outputr   r!   r   %test_base_exception_from_async_method  s   z3TestAsyncCase.test_base_exception_from_async_methodc                    s8   d G  fdddt j}|d}| }|   d S )NFc                       s   e Zd Z fddZdS )z;TestAsyncCase.test_cancellation_hanging_tasks.<locals>.Testc                    s    fdd}t |  d S )Nc                      s0   zt dI d H  W d S  t jy   d  w )Nr$   T)r
   sleeprm   r   	cancelledr   r   coro/  s   z[TestAsyncCase.test_cancellation_hanging_tasks.<locals>.Test.test_leaking_task.<locals>.coro)r
   Zcreate_task)r   ru   rs   r   r   test_leaking_task.  s   	zMTestAsyncCase.test_cancellation_hanging_tasks.<locals>.Test.test_leaking_taskN)r   r   r   rv   r   rs   r   r   r:   -  s    r:   rv   )r;   r<   r=   Z
assertTruero   r   rs   r   test_cancellation_hanging_tasks+  s
   z-TestAsyncCase.test_cancellation_hanging_tasksc                    s   G  fdddt j}g  |d}| }|  g d | d|jd d  g  |d}| |j z|  W n	 t	yC   Y nw | 
d |  d	d
g |  |  g d d S )Nc                       rD   )
z8TestAsyncCase.test_debug_cleanup_same_loop.<locals>.Testc                    s2   dd }t | }| | j|  d d S )Nc                      s   t dI d H  d S )Nr   )r
   rr   r   r   r   r   ru   A  rh   zQTestAsyncCase.test_debug_cleanup_same_loop.<locals>.Test.asyncSetUp.<locals>.coror   )r
   Zensure_futurer   rP   r   )r   ru   futr!   r   r   r   @  s
   zCTestAsyncCase.test_debug_cleanup_same_loop.<locals>.Test.asyncSetUpc                    r\   rK   r]   r   r!   r   r   r,   G  r^   zBTestAsyncCase.test_debug_cleanup_same_loop.<locals>.Test.test_funcc                    rJ   rN   rL   r   r!   r   r   r   K  rM   zFTestAsyncCase.test_debug_cleanup_same_loop.<locals>.Test.asyncTearDownc                    s>   zt |gI d H   d W d S    dd l}|   )NrP   r   )r
   waitr   	traceback	print_exc)r   rx   rz   r!   r   r   rP   N  s   z@TestAsyncCase.test_debug_cleanup_same_loop.<locals>.Test.cleanupN)r   r   r   r   r,   r   rP   r   r!   r   r   r:   ?  s
    r:   r,   rX   r   r   r$   rR   r   r   rY   )r;   r<   r=   r   rT   r>   r   rU   r?   r   rV   r@   rA   r   r!   r   test_debug_cleanup_same_loop>  s$   
z*TestAsyncCase.test_debug_cleanup_same_loop)r   r   r   ZmaxDiffr   rC   rW   rZ   r_   rb   ri   rq   rw   r|   r   r   r   r   r      s    M%$$*r   __main__)r
   r;   r   r   	Exceptionr   r   ZTestCaser   r   mainr   r   r   r   <module>   s      `