a
    óñ"hç'  ã                   @   sZ   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rVe 
¡  dS )
é    N)Úsupportc                   @   s   e Zd ZdS )ÚMyExceptionN)Ú__name__Ú
__module__Ú__qualname__© r   r   úE/opt/python-3.9.21/usr/lib/python3.9/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                   @   sX   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S )ÚTestAsyncCaseNc                 C   s   t  ¡  d S r	   )r   Ú
gc_collect©Úselfr   r   r   ÚtearDown   s    zTestAsyncCase.tearDownc                    s’   G ‡ fdd„dt jƒ}g ‰ |dƒ}| ¡ }|  |jg ¡ |  |jg ¡ g d¢}|  ˆ |¡ g ‰ |dƒ}| ¡  |  ˆ |¡ | ¡  |  ˆ |¡ d S )Nc                       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	dS )z+TestAsyncCase.test_full_cycle.<locals>.Testc                    s   |   ˆ g ¡ ˆ  d¡ d S )NÚsetUp©ÚassertEqualÚappendr   ©Úeventsr   r   r      s    z1TestAsyncCase.test_full_cycle.<locals>.Test.setUpc                 “   s(   |   ˆ dg¡ ˆ  d¡ |  | j¡ d S )Nr   Ú
asyncSetUp)r   r   ÚaddAsyncCleanupÚon_cleanup1r   r   r   r   r      s    
z6TestAsyncCase.test_full_cycle.<locals>.Test.asyncSetUpc                 “   s*   |   ˆ ddg¡ ˆ  d¡ |  | j¡ d S )Nr   r   Útest)r   r   r   Úon_cleanup2r   r   r   r   Ú	test_func!   s
    ÿ
z5TestAsyncCase.test_full_cycle.<locals>.Test.test_funcc                 “   s   |   ˆ g d¢¡ ˆ  d¡ d S )N)r   r   r   ÚasyncTearDownr   r   r   r   r   r   '   s    z9TestAsyncCase.test_full_cycle.<locals>.Test.asyncTearDownc                    s   |   ˆ g d¢¡ ˆ  d¡ d S )N)r   r   r   r   r   r   r   r   r   r   r   -   s    z4TestAsyncCase.test_full_cycle.<locals>.Test.tearDownc                 “   s   |   ˆ g d¢¡ ˆ  d¡ d S )N)r   r   r   r   r   Úcleanup2Úcleanup1r   r   r   r   r   r   4   s    z7TestAsyncCase.test_full_cycle.<locals>.Test.on_cleanup1c                 “   s   |   ˆ g d¢¡ ˆ  d¡ d S )N)r   r   r   r   r   r   r   r   r   r   r   r   =   s    z7TestAsyncCase.test_full_cycle.<locals>.Test.on_cleanup2N)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   ÚTest   s   	r    r   )r   r   r   r   r   r   r   )ÚunittestÚIsolatedAsyncioTestCaseÚrunr   ÚerrorsZfailuresÚdebugÚ
doCleanups)r   r    r   ÚresultZexpectedr   r   r   Útest_full_cycle   s    .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ƒ}z| ¡  W n ty   Y n0 |  	d	¡ |  ˆ dg¡ | 
¡  |  ˆ ddg¡ d S )
Nc                       s<   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                 “   s    ˆ   d¡ |  | j¡ tƒ ‚d S ©Nr   ©r   r   Ú
on_cleanupr   r   r   r   r   r   W   s    
z>TestAsyncCase.test_exception_in_setup.<locals>.Test.asyncSetUpc                 “   s   ˆ   d¡ d S ©Nr   ©r   r   r   r   r   r   \   s    z=TestAsyncCase.test_exception_in_setup.<locals>.Test.test_funcc                 “   s   ˆ   d¡ d S ©Nr   r-   r   r   r   r   r   _   s    zATestAsyncCase.test_exception_in_setup.<locals>.Test.asyncTearDownc                 “   s   ˆ   d¡ d S ©NÚcleanupr-   r   r   r   r   r+   b   s    z>TestAsyncCase.test_exception_in_setup.<locals>.Test.on_cleanupN©r   r   r   r   r   r   r+   r   r   r   r   r    V   s   r    r   r   r0   r   r   é   ú Expected a MyException exception©r!   r"   r#   r   ZassertIsr$   ZassertInr%   r   Zfailr&   ©r   r    r   r'   r   r   r   Útest_exception_in_setupU   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ƒ}z| ¡  W n ty   Y n0 |  	d¡ |  ˆ d	d
g¡ | 
¡  |  ˆ g d¢¡ d S )Nc                       s<   e Zd Z‡ fdd„Z‡ fdd„Z‡ fdd„Z‡ fdd„Zd	S )
z2TestAsyncCase.test_exception_in_test.<locals>.Testc                 “   s   ˆ   d¡ d S r)   r-   r   r   r   r   r   {   s    z=TestAsyncCase.test_exception_in_test.<locals>.Test.asyncSetUpc                 “   s    ˆ   d¡ |  | j¡ tƒ ‚d S r,   r*   r   r   r   r   r   ~   s    
z<TestAsyncCase.test_exception_in_test.<locals>.Test.test_funcc                 “   s   ˆ   d¡ d S r.   r-   r   r   r   r   r   ƒ   s    z@TestAsyncCase.test_exception_in_test.<locals>.Test.asyncTearDownc                 “   s   ˆ   d¡ d S r/   r-   r   r   r   r   r+   †   s    z=TestAsyncCase.test_exception_in_test.<locals>.Test.on_cleanupNr1   r   r   r   r   r    z   s   r    r   ©r   r   r   r0   r   r   r2   r3   r   r   )r   r   r0   r4   r5   r   r   r   Útest_exception_in_testy   s"    
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ƒ}z| ¡  W n ty   Y n0 |  	d¡ |  ˆ g d	¢¡ | 
¡  |  ˆ g d¢¡ d S )
Nc                       s<   e Zd Z‡ fdd„Z‡ fdd„Z‡ fdd„Z‡ fdd„Zd	S )
z7TestAsyncCase.test_exception_in_tear_down.<locals>.Testc                 “   s   ˆ   d¡ d S r)   r-   r   r   r   r   r   ž   s    zBTestAsyncCase.test_exception_in_tear_down.<locals>.Test.asyncSetUpc                 “   s   ˆ   d¡ |  | j¡ d S r,   )r   r   r+   r   r   r   r   r   ¡   s    
zATestAsyncCase.test_exception_in_tear_down.<locals>.Test.test_funcc                 “   s   ˆ   d¡ tƒ ‚d S r.   ©r   r   r   r   r   r   r   ¥   s    
zETestAsyncCase.test_exception_in_tear_down.<locals>.Test.asyncTearDownc                 “   s   ˆ   d¡ d S r/   r-   r   r   r   r   r+   ©   s    zBTestAsyncCase.test_exception_in_tear_down.<locals>.Test.on_cleanupNr1   r   r   r   r   r       s   r    r   r7   r   r   r2   r3   )r   r   r   r4   r5   r   r   r   Útest_exception_in_tear_downœ   s"    
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ƒ}z| ¡  W n ty¦   Y n0 |  	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                 “   s   ˆ   d¡ d S r)   r-   r   r   r   r   r   Á   s    zFTestAsyncCase.test_exception_in_tear_clean_up.<locals>.Test.asyncSetUpc                 “   s&   ˆ   d¡ |  | j¡ |  | j¡ d S r,   )r   r   r   r   r   r   r   r   r   Ä   s    
zETestAsyncCase.test_exception_in_tear_clean_up.<locals>.Test.test_funcc                 “   s   ˆ   d¡ d S r.   r-   r   r   r   r   r   É   s    zITestAsyncCase.test_exception_in_tear_clean_up.<locals>.Test.asyncTearDownc                 “   s   ˆ   d¡ tdƒ‚d S )Nr   z
some errorr9   r   r   r   r   r   Ì   s    
zGTestAsyncCase.test_exception_in_tear_clean_up.<locals>.Test.on_cleanup1c                 “   s   ˆ   d¡ tdƒ‚d S )Nr   zother errorr9   r   r   r   r   r   Ð   s    
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 errorr2   zMyException: some errorr3   )r   r   r   r   r4   r5   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 )Nr2   é   é   é   )r   Úon_sync_cleanupÚon_async_cleanupr   r   r   r   r   ì   s    zDTestAsyncCase.test_cleanups_interleave_order.<locals>.Test.test_funcc                 “   s   ˆ   d|› ¡ d S )Nzsync_cleanup r-   ©r   Úvalr   r   r   r?   ò   s    zJTestAsyncCase.test_cleanups_interleave_order.<locals>.Test.on_sync_cleanupc                 “   s   ˆ   d|› ¡ d S )Nzasync_cleanup r-   rA   r   r   r   r@   õ   s    zKTestAsyncCase.test_cleanups_interleave_order.<locals>.Test.on_async_cleanupN)r   r   r   r   r?   r@   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                 “   s   ˆ   d¡ tƒ ‚ˆ   d¡ d S )NÚ	test_baseznot it)r   ÚBaseExceptionr   r   r   r   rD     s    
zKTestAsyncCase.test_base_exception_from_async_method.<locals>.Test.test_basec                 “   s   ˆ   d¡ d S )NÚtest_no_errr-   r   r   r   r   rF     s    zMTestAsyncCase.test_base_exception_from_async_method.<locals>.Test.test_no_errc                 Ó   s   t  ¡ ‚d S r	   )r
   ÚCancelledErrorr   r   r   r   Útest_cancel
  s    zMTestAsyncCase.test_base_exception_from_async_method.<locals>.Test.test_cancelN)r   r   r   rD   rF   rH   r   r   r   r   r      s   r    rD   rF   rH   )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                   “   s4   zt  d¡I d H  W n t jy.   d‰ ‚ Y n0 d S )Nr2   T)r
   ÚsleeprG   r   ©Ú	cancelledr   r   Úcoro  s
    z[TestAsyncCase.test_cancellation_hanging_tasks.<locals>.Test.test_leaking_task.<locals>.coro)r
   Zcreate_task)r   rO   rM   r   r   Útest_leaking_task  s    	zMTestAsyncCase.test_cancellation_hanging_tasks.<locals>.Test.test_leaking_taskN)r   r   r   rP   r   rM   r   r   r      s   r    rP   )r!   r"   r#   Z
assertTruerI   r   rM   r   Útest_cancellation_hanging_tasks  s
    z-TestAsyncCase.test_cancellation_hanging_tasks)r   r   r   ZmaxDiffr   r(   r6   r8   r:   r;   rC   rK   rQ   r   r   r   r   r      s   ?$##)r   Ú__main__)r
   r!   r   r   Ú	Exceptionr   r   ZTestCaser   r   Úmainr   r   r   r   Ú<module>   s     "