
    dafb)                     X   d Z ddlZddlZddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ ddlmZ dd	lmZ ej$                  Zd
 Z G d d      Z G d deej,                        Z ej0                   ee
d      d       G d deej,                               Z G d d      Zd Zd Zd Z G d d      Z G d de      Z  G d de      Z! G d de ej,                        Z" G d  d!e!ej,                        Z# G d" d#e ej,                        Z$ G d$ d%e!ej,                        Z% ej0                   ee
d      d       G d& d'e ej,                               Z& ej0                   ee
d      d       G d( d)e!ej,                               Z'e(d*k(  r ejR                          yy)+zTests for base_events.py    N)GenericAlias)mock)base_events)tasks)utils)get_innermost_context)assert_python_okc                  .    t        j                  d        y N)asyncioset_event_loop_policy     D/root/Python-3.12.4/Lib/test/test_asyncio/test_eager_task_factory.pytearDownModuler      s    !!$'r   c                   d     e Zd ZdZd Z fdZd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Z xZS )EagerTaskFactoryLoopTestsNc                 H      fd} j                   j                        S )aN  
        Helper method to run the `coro` coroutine in the test event loop.
        It helps with making sure the event loop is running before starting
        to execute `coro`. This is important for testing the eager step
        functionality, since an eager step is taken only if the event loop
        is already running.
        c                     K   j                  t        j                         j                                  d {   S 7 wr   )
assertTruer   get_event_loop
is_running)coroselfs   r   coro_runnerz7EagerTaskFactoryLoopTests.run_coro.<locals>.coro_runner%   s/     OOG224??AB::s   7A?A)looprun_until_complete)r   r   r   s   `` r   run_coroz"EagerTaskFactoryLoopTests.run_coro   s    	 yy++D11r   c                    t         |           t        j                         | _        t        j
                  | j                        | _        | j                  j                  | j                         | j                  | j                         y r   )
supersetUpr   new_event_loopr   create_eager_task_factoryTaskeager_task_factoryset_task_factoryset_event_loopr   	__class__s    r   r!   zEagerTaskFactoryLoopTests.setUp+   s]    **,	")"C"CDII"N		""4#:#:;DII&r   c                       j                   j                          j                   j                  j	                          j                         d  fd} j                   |              y )Nc                     K   y wr   r   r   r   r   noopzCEagerTaskFactoryLoopTests.test_eager_task_factory_set.<locals>.noop6   s     $   c                     K   j                   j                                } j                  | j                         |  d {    y 7 wr   )r   create_taskassertIsInstancer$   )tr,   r   s    r   runzBEagerTaskFactoryLoopTests.test_eager_task_factory_set.<locals>.run8   s8     		%%df-A!!!TYY/GGs   AAAA)assertIsNotNoner%   assertIsr   get_task_factoryr   )r   r2   r,   s   ` @r   test_eager_task_factory_setz5EagerTaskFactoryLoopTests.test_eager_task_factory_set2   sP    T445dii002D4K4KL	
 	cer   c                 f     d  fd} j                   j                   |             d       y )Nc                 .   K   | j                  |       y wr   )
set_result)futvals     r   r9   zQEagerTaskFactoryLoopTests.test_await_future_during_eager_step.<locals>.set_resultA   s     NN3s   c                     K   j                   j                         } j                   j                   | d            }j                  |j	                                |  d {   S 7 w)N
my message)r   create_futurer/   r   done)r:   r1   r   r9   s     r   r2   zJEagerTaskFactoryLoopTests.test_await_future_during_eager_step.<locals>.runD   sP     ))))+C		%%jl&CDAOOAFFH%99s   A!A+$A)%A+r=   assertEqualr   )r   r2   r9   s   ` @r   #test_await_future_during_eager_stepz=EagerTaskFactoryLoopTests.test_await_future_during_eager_step?   s*    	 	 	su-|<r   c                 f     d  fd} j                   j                   |             d       y )Nc                     K   yw)Nhellor   r   r   r   r   z=EagerTaskFactoryLoopTests.test_eager_completion.<locals>.coroO   s	     r-   c                     K   j                   j                                } j                  | j                                |  d {   S 7 wr   )r   r/   r   r?   )r1   r   r   s    r   r2   z<EagerTaskFactoryLoopTests.test_eager_completion.<locals>.runR   s8     		%%df-AOOAFFH%7N7s   AAA	ArE   r@   r   r2   r   s   ` @r   test_eager_completionz/EagerTaskFactoryLoopTests.test_eager_completionM   s*    		 	su-w7r   c                 f     d  fd} j                   j                   |             d       y )Nc                  J   K   t        j                  d       d {    y7 wN皙?finished after blockingr   sleepr   r   r   r   zCEagerTaskFactoryLoopTests.test_block_after_eager_step.<locals>.coro\         --$$$, %   #!#c                     K   j                   j                                } j                  | j                                |  d {   }j	                  | j                                |S 7 %wr   )r   r/   assertFalser?   r   r1   resultr   r   s     r   r2   zBEagerTaskFactoryLoopTests.test_block_after_eager_step.<locals>.run`   sT     		%%df-AQVVX&WFOOAFFH%M s   AA0A.	&A0rM   r@   rG   s   ` @r   test_block_after_eager_stepz5EagerTaskFactoryLoopTests.test_block_after_eager_stepZ   s+    	-	 	su-/HIr   c                 f     d  fd} j                   j                   |             d       y )Nc                     K   yw)Nfinished without blockingr   r   r   r   r   zPEagerTaskFactoryLoopTests.test_cancellation_after_eager_completion.<locals>.corok   s	     .r-   c                     K   j                   j                                } | j                          |  d {   }j                  | j	                                |S 7 %wr   )r   r/   cancelrS   	cancelledrT   s     r   r2   zOEagerTaskFactoryLoopTests.test_cancellation_after_eager_completion.<locals>.runn   sK     		%%df-AHHJWFQ[[]+M s   6A!A&A!rY   r@   rG   s   ` @r   (test_cancellation_after_eager_completionzBEagerTaskFactoryLoopTests.test_cancellation_after_eager_completioni   s+    	/	 	su-/JKr   c                      d  fd} j                  t        j                        5 } j                   |              d d d         j	                  dj
                  j                  d          y # 1 sw Y   3xY w)Nc                  J   K   t        j                  d       d {    y7 wrK   rN   r   r   r   r   zQEagerTaskFactoryLoopTests.test_cancellation_after_eager_step_blocks.<locals>.coroz   rP   rQ   c                     K   j                   j                                } | j                  d       j                  | j	                         d       |  d {   }y 7 w)Ncancellation messager   )r   r/   r[   assertGreater
cancellingrT   s     r   r2   zPEagerTaskFactoryLoopTests.test_cancellation_after_eager_step_blocks.<locals>.run~   sI     		%%df-AHH+,q||~q1WFWs   AA"A A"ra   r   )assertRaisesr   CancelledErrorr   rA   	exceptionargs)r   r2   cmr   s   `  @r   )test_cancellation_after_eager_step_blockszCEagerTaskFactoryLoopTests.test_cancellation_after_eager_step_blocksx   sj    	-	 w556 	!"MM#% 	! 	/1B1B11EF	! 	!s   A44A=c                 X     d  fd fd} j                   |              d y )Nc                     K   t        j                          t        j                  d       d {    j                  t        j                                 y 7 )w)NrL   )r   current_taskrO   r4   )captured_current_taskr   s   r   r   z9EagerTaskFactoryLoopTests.test_current_task.<locals>.coro   sE     $+$8$8$:!--$$$MM'..02GH %s   -AA*Ac                     K   j                   j                                } j                  |        |  d {    y 7 wr   )r   r/   r4   )r1   rm   r   r   s    r   r2   z8EagerTaskFactoryLoopTests.test_current_task.<locals>.run   s3     		%%df-AMM/3GGs   8AAAr   )r   r2   rm   r   s   ` @@r   test_current_taskz+EagerTaskFactoryLoopTests.test_current_task   s*     $	I	
 	ce $r   c                 R     d fd fd} j                   |              y )Nc                  6   K   t        j                          y wr   )r   	all_tasks)captured_all_taskss   r   r   zLEagerTaskFactoryLoopTests.test_all_tasks_with_eager_completion.<locals>.coro   s     !(!2!2!4s   c                     K   j                   j                                } j                  |        j                  | t	        j
                                y wr   )r   r/   assertInassertNotInr   rs   )r1   rt   r   r   s    r   r2   zKEagerTaskFactoryLoopTests.test_all_tasks_with_eager_completion.<locals>.run   sE     		%%df-AMM!/0Q 1 1 34s   AAro   )r   r2   rt   r   s   ` @@r   $test_all_tasks_with_eager_completionz>EagerTaskFactoryLoopTests.test_all_tasks_with_eager_completion   s#    !	5	5
 	cer   c                 R     d fd fd} j                   |              y )Nc                 p   K   t        j                         |  d {    |j                  d        y 7 wr   )r   rs   r9   )fut1fut2captured_eager_all_taskss     r   r   zDEagerTaskFactoryLoopTests.test_all_tasks_with_blocking.<locals>.coro   s.     '.'8'8':$JJOOD! s   646c                    K   j                   j                         } j                   j                         }j                   j                   | |            }j                  |       j                  |t	        j
                                | j                  d        | d {    j                  |t	        j
                                y 7 )wr   )r   r>   r/   rv   r   rs   r9   rw   )r{   r|   r1   r}   r   r   s      r   r2   zCEagerTaskFactoryLoopTests.test_all_tasks_with_blocking.<locals>.run   s     99**,D99**,D		%%d4&67AMM!56MM!W..01OOD!JJQ 1 1 34 s   B#C&C'*Cro   )r   r2   r}   r   s   ` @@r   test_all_tasks_with_blockingz6EagerTaskFactoryLoopTests.test_all_tasks_with_blocking   s#    #' 	"	5 	cer   c                      t        j                  dd      dd fd fd} j                   |              y )Ncvr   )defaultFc                    K   j                  j                         d       j                  d       j                  j                         d       d t        j                  d       d {    j                  j                         d       j                  d       j                  j                         d       dy 7 Xw)N      TrL      )rA   getsetr   rO   )coro_first_step_rancoro_second_step_ranr   r   s   r   r   z9EagerTaskFactoryLoopTests.test_context_vars.<locals>.coro   s      RVVXq)FF1IRVVXq)"&--$$$RVVXq)FF1IRVVXq)#' 	 %s   A,C
/C0AC
c                  n  K   j                  d       j                  j                                } j                         j	                         j                  j                         d       |  d {    j                         j                  j                         d       y 7 6w)Nr   )r   r   r/   r   rS   rA   r   )r1   r   r   r   r   r   s    r   r2   z8EagerTaskFactoryLoopTests.test_context_vars.<locals>.run   s     FF1I		%%df-AOO/012RVVXq)GGOO01RVVXq) s   A9B5<B3=7B5)contextvars
ContextVarr   )r   r2   r   r   r   r   s   ` @@@@r   test_context_varsz+EagerTaskFactoryLoopTests.test_context_vars   s@    ##D!4#$	(	* 	* 	cer   )__name__
__module____qualname__r$   r   r!   r6   rB   rH   rV   r]   ri   rp   rx   r   r   __classcell__r)   s   @r   r   r      sG    D2'=8JLG"%$*r   r   c                   $    e Zd Zej                  Zy)PyEagerTaskFactoryLoopTestsNr   r   r   r   _PyTaskr$   r   r   r   r   r          ==Dr   r   _CTaskzrequires the C _asyncio modulec                   &    e Zd Z eedd      Zd Zy)CEagerTaskFactoryLoopTestsr   Nc                 J    d}t        d|      \  }}}| j                  |       y )Na   if 1:
        from _asyncio import _swap_current_task

        class DummyTask:
            pass

        class DummyLoop:
            pass

        l = DummyLoop()
        _swap_current_task(l, DummyTask())
        t = _swap_current_task(l, None)
        z-c)r	   rS   )r   code_outerrs        r   test_issue105987z+CEagerTaskFactoryLoopTests.test_issue105987   s+     'tT23r   )r   r   r   getattrr   r$   r   r   r   r   r   r      s     5(D)Dr   r   c                       e Zd Zd Zd Zy)AsyncTaskCounterc                     d _         d _        dd fd
|rt        j                        }nfd}|j	                  |       y )Nr   F)eager_startc                 L    | sxj                   dz  c_         | |d<    |i |S )Nr   r   
task_count)r   rg   kwargsr   
task_classs      r   CountingTaskz/AsyncTaskCounter.__init__.<locals>.CountingTask   s.    1$$/F=!t.v..r   c                      |fd| i|S )Nr   r   )r   r   r   r   s      r   factoryz*AsyncTaskCounter.__init__.<locals>.factory  s    #D>t>v>>r   )suspense_countr   r   r#   r&   )r   r   r   eagerr   r   s   ` `  @r   __init__zAsyncTaskCounter.__init__   sB    ,1 	/ 77EG?g&r   c                     | j                   S r   r   r   s    r   r   zAsyncTaskCounter.get  s    r   N)r   r   r   r   r   r   r   r   r   r      s    '"r   r   c                 L   K   | dk(  rydt        | dz
         d {   z   S 7 wNr   r   )awaitable_chain)depths    r   r   r     s*     z_UQY/////s   $"$c                   K   |dk(  ry t        j                         4 d {   }t        |       D cg c]   }|j                  t	        | |dz
              " }}d d d       d {    y 7 Ic c}w 7 # 1 d {  7  sw Y   y xY wwr   )r   	TaskGroupranger/   recursive_taskgroups)widthr   tgr   futuress        r   r   r     s     z  " 
 
b 5\
 NN/uqyAB
 

 
 


 
 
 
sU   B	A+B	A4%A-A4B	%A2&B	-A42B	4B:A=;BB	c           
         K   |dk(  ry t        j                  t        |       D cg c]  }t        | |dz
         c}  d {    y c c}w 7 
wr   )r   gatherr   recursive_gather)r   r   r   s      r   r   r      sL     z
..6;El	C
5%!)
,	C  	Cs   #AAAAAc                   <     e Zd ZdZdZdZ fdZd Zd Zd Z	 xZ
S )BaseTaskCountingTestsNc                     t         |           t        j                         | _        t        | j                  | j                  | j                        | _        | j                  | j                         y )N)r   r   )
r    r!   r   r"   r   r   r$   r   counterr'   r(   s    r   r!   zBaseTaskCountingTests.setUp/  sJ    **,	'		diitzzZDII&r   c                     | j                   j                  t        d            }| j                  |d       | j                  | j                  j                         | j                  rd       y d       y )Nd   r   r   )r   r   r   rA   r   r   r   )r   observed_depths     r   test_awaitables_chainz+BaseTaskCountingTests.test_awaitables_chain5  sS    55oc6JK-))+$**QD!Dr   c                     | j                   j                  t        dd            }| j                  | j                  j                         | j                         y N      )r   r   r   rA   r   r   expected_task_count)r   	num_taskss     r   test_recursive_taskgroupsz/BaseTaskCountingTests.test_recursive_taskgroups:  sA    II001Ea1KL	))+T-E-EFr   c                     | j                   j                  t        dd             | j                  | j                  j                         | j                         y r   )r   r   r   rA   r   r   r   r   s    r   test_recursive_gatherz+BaseTaskCountingTests.test_recursive_gather>  s?    		$$%5a%;<))+T-E-EFr   )r   r   r   r$   r   r   r!   r   r   r   r   r   s   @r   r   r   )  s*    DE'E
GGr   r   c                       e Zd ZdZdZy)BaseNonEagerTaskFactoryTestsFi  Nr   r   r   r   r   r   r   r   r   r   C  s    Er   r   c                       e Zd ZdZdZy)BaseEagerTaskFactoryTestsTr   Nr   r   r   r   r   r   H  s    Er   r   c                   $    e Zd Zej                  Zy)NonEagerTestsNr   r   r   r   r$   r   r   r   r   r   M      <<Dr   r   c                   $    e Zd Zej                  Zy)
EagerTestsNr   r   r   r   r   r   Q  r   r   r   c                   $    e Zd Zej                  Zy)NonEagerPyTaskTestsNr   r   r   r   r   r   U  r   r   r   c                   $    e Zd Zej                  Zy)EagerPyTaskTestsNr   r   r   r   r   r   Y  r   r   r   c                        e Zd Z eedd      Zy)NonEagerCTaskTestsr   Nr   r   r   r   r   r$   r   r   r   r   r   ]       5(D)Dr   r   c                        e Zd Z eedd      Zy)EagerCTaskTestsr   Nr   r   r   r   r   r   c  r   r   r   __main__)*__doc__r   r   gctimeunittesttypesr   r   r   r   test.test_asyncior   
test_utilstest.test_asyncio.test_tasksr   test.support.script_helperr	   ANYMOCK_ANYr   r   TestCaser   
skipUnlesshasattrr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   mainr   r   r   <module>r      s      	       1 > 788(C CL";Z=P=P  WUH-57!:J<O<O 7* ,0
G G4#8 
 5 
0*2E2E *J,?,? 6
8K8K 0*2E2E  WUH-57*5z7J7J *7* WUH-57*/1D1D *7* zHMMO r   