
    dafw#                        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Zd dl	Z	d dl
Z
d dlZd dlmZ  ej                  d       dZdZ ej                          Zd	 Z G d
 dej&                        Z G d de      Z G d d      Z G d de      Z G d dej0                        Z G d dej&                        Zedk(  r ej6                          yy)    N)support)threading_helper)
lock_testsT)module
      c                 r    t         j                  rt        5  t        |        ddd       yy# 1 sw Y   yxY w)z2Helper function for printing out debugging output.N)r   verbose_print_mutexprint)args    +/root/Python-3.12.4/Lib/test/test_thread.pyverbose_printr      s2     	#J	 	 	 	s   -6c                       e Zd Zd Zy)BasicThreadTestc                 b   t        j                         | _        | j                  j                          t        j                         | _        t        j                         | _        d| _        d| _        d| _        t        j                         } | j                  t        j                  g|  y Nr   )threadallocate_lock
done_mutexacquirerunning_mutexrandom_mutexcreatedrunning
next_identr   threading_setup
addCleanupthreading_cleanup)selfkeys     r   setUpzBasicThreadTest.setUp   s     ..0!#113"002..0(::ASA    N)__name__
__module____qualname__r"    r#   r   r   r      s    
Br#   r   c                   z    e Zd Zd Zd Zd Zd Z ej                  e	j                  dvd      d        Zd Zd	 Zy
)ThreadRunningTestsc                 J   | j                   5  | xj                  dz  c_        t        d| j                  z         t        j                  | j
                  | j                  f       | xj                  dz  c_        | xj                  dz  c_        d d d        y # 1 sw Y   y xY w)N   zcreating task %s)r   r   r   r   start_new_threadtaskr   r   r    s    r   newtaskzThreadRunningTests.newtask,   sw     	OOq O,t>?##DII/ABLLALLLAL	 	 	s   BBB"c           	         | j                   5  t        j                         dz  }d d d        t        d|dt        dz        d       t	        j
                  |       t        d|z         | j                  5  | xj                  dz  c_        | j                  t        k(  r)| j                  dk(  r| j                  j                          d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)	N     @task  will run for     .Ausztask %s doner+   r   )r   randomr   roundtimesleepr   r   r   NUMTASKSr   release)r    identdelays      r   r-   zThreadRunningTests.task4   s     	.MMOg-E	.UE%)<LMN

5nu,- 	*LLAL||x'DLLA,='')	* 	*	. 	.
	* 	*s   C:AC!C!C*c                     t        j                         5  t        t              D ]  }| j	                           t        d       | j                  j                          t        d       d d d        y # 1 sw Y   y xY w)Nz waiting for tasks to complete...all tasks done)r   wait_threads_exitranger:   r/   r   r   r   r    is     r   test_starting_threadsz(ThreadRunningTests.test_starting_threads?   sa    //1 	,8_ <=OO##%*+	, 	, 	,s   AA33A<c                     | j                  t        j                         dd       t        j                  d       | j                  t        j                         dd       y )Nr   zinitial stack size is not 0zstack_size not reset to default)assertEqualr   
stack_sizer.   s    r   test_stack_sizez"ThreadRunningTests.test_stack_sizeH   sH    **,a1NO!**,a1RSr#   )ntposixztest meant for nt and posixc                    	 t        j                  d       d}dD ]M  }t        j                  |       | j                  t        j                         |||z         t        d|z         O dD ]  }t        d|z         d	| _        d	| _        t        j                         5  t        t              D ]  }| j                           t        d
       | j                  j                          t        d       d d d         t        j                  d	       y # t        $ r t        d       Y t         j                  $ r | j                  d       Y ;w xY w# 1 sw Y   xY w)Ni   z3caught expected ValueError setting stack_size(4096)z4platform does not support changing thread stack sizez&stack_size(%d) failed - should succeed)      r   zsuccessfully set stack_size(%d))rL   rM   ztrying stack_size = (%d)r   z!waiting for all tasks to completer?   )r   rG   
ValueErrorr   errorskipTestrF   r   r   r   r@   rA   r:   r/   r   r   )r    fail_msgtssrC   s       r   test_nt_and_posix_stack_sizez/ThreadRunningTests.test_nt_and_posix_stack_sizeO   sF   	"d# <( 	CCc"V..0#x#~F;cAB	C
 & 
	0C4s:;DODL!335 0x #ALLN# AB'')./0 0	
	0 	!3  	0 / 0|| 	"MM ! "	"0 0s$   D  AEE,#EEE	c                    t        j                         }t        j                         j                          g fd}t	        j
                         5  t        j                  |d       t        j                  t        j                        D ]  }s n | j                  t        j                         |dz          j                          g t        j                  |fd      }~t        j                  t        j                        D ]  }r nt        j                           | j                  t        j                         |       d d d        y # 1 sw Y   y xY w)Nc                  h    j                  d         j                           j                          y N)appendr   r;   )mutstarteds   r   r-   z,ThreadRunningTests.test__count.<locals>.tasku   s!    NN4 KKMKKMr#   r'   r+   c                 &    j                  d       S rV   )rW   )_dones    r   <lambda>z0ThreadRunningTests.test__count.<locals>.<lambda>   s    T[[-> r#   )r   _countr   r   r   r@   r,   r   sleeping_retryLONG_TIMEOUTrF   r;   weakrefref
gc_collect)r    origr-   r[   wrr\   rX   rY   s        @@@r   test__countzThreadRunningTests.test__countn   s   }}""$	
 //1 	4##D"-++G,@,@A  V]]_dQh7 KKM
 DT#>?B++G,@,@A %""$% V]]_d3+	4 	4 	4s   ?EB:EE$c                    fd}t        j                         t        j                         5 }t	        j
                         5  j                          t        j                  |d       j                          d d d        | j                  t        |j                  j                        d       | j                  |j                  j                  |       | j                  |j                  j                  d       | j                  |j                  j                          d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nc                  :     j                          t        d      )Ntask failed)r;   rN   )rY   s   r   r-   z:ThreadRunningTests.test_unraisable_exception.<locals>.task   s    OO]++r#   r'   ri   z&Exception ignored in thread started by)r   r   r   catch_unraisable_exceptionr   r@   r   r,   rF   str
unraisable	exc_valueassertIsobjecterr_msgassertIsNotNoneexc_traceback)r    r-   cmrY   s      @r   test_unraisable_exceptionz,ThreadRunningTests.test_unraisable_exception   s    	, &&(//1 
	>R!335 "!''b1!"
 S!8!89=IMM"--..5R]]22EG  !<!<=
	> 
	>" "
	> 
	>s$   D87D,;B(D8,D5	1D88EN)r$   r%   r&   r/   r-   rD   rH   unittestskipIfosnamerS   rf   rt   r'   r#   r   r)   r)   *   sN    	*,T X__RWWO35RS T<!4F>r#   r)   c                       e Zd Zd Zd Zy)Barrierc                     || _         d| _        t        j                         | _        t        j                         | _        | j
                  j                          y r   )num_threadswaitingr   r   checkin_mutexcheckout_mutexr   )r    r|   s     r   __init__zBarrier.__init__   sE    &$224$224##%r#   c                    | j                   j                          | j                  dz   | _        | j                  | j                  k(  r/| j                  dz
  | _        | j                  j                          y | j                   j                          | j                  j                          | j                  dz
  | _        | j                  dk(  r| j                   j                          y | j                  j                          y )Nr+   r   )r~   r   r}   r|   r   r;   r.   s    r   enterzBarrier.enter   s    ""$||a'<<4+++++a/DL'')""$##%||a'<<1&&(##%r#   N)r$   r%   r&   r   r   r'   r#   r   rz   rz      s    &&r#   rz   c                       e Zd Zd Zd Zy)BarrierTestc                 ^   t        j                         5  t        t              | _        t        | _        t        t              D ]#  }t        j                  | j                  |f       % t        d       | j                  j                          t        d       d d d        y # 1 sw Y   y xY w)Nzwaiting for tasks to endz
tasks done)r   r@   rz   r:   barr   rA   r   r,   task2r   r   r   rB   s     r   test_barrierzBarrierTest.test_barrier   s    //1 	(x(DH#DL8_ :''

QD9:45OO##%,'	( 	( 	(s   BB##B,c           
      H   t        t              D ]  }|dk(  rd}n,| j                  5  t        j                         dz  }d d d        t	        d|dt        dz        d       t        j                  |       t	        d|d|       | j                  j                          t	        d|z          | j                  5  | xj                  d	z  c_        | j                  dk(  }d d d        r| j                  j                          y y # 1 sw Y   xY w# 1 sw Y   3xY w)
Nr   r1   r2   r3   r4   r5   z
 entering ztask %s leaving barrierr+   )rA   NUMTRIPSr   r6   r   r7   r8   r9   r   r   r   r   r   r;   )r    r<   rC   r=   finisheds        r   r   zBarrierTest.task2   s    x 	=Az && 6"MMOg5E6 %"46 7JJu5!<=HHNN3e;<	=  	)LLAL ||q(H	) OO##% 6 6	) 	)s   D%DD	D!N)r$   r%   r&   r   r   r'   r#   r   r   r      s    (&r#   r   c                   $    e Zd Zej                  Zy)	LockTestsN)r$   r%   r&   r   r   locktyper'   r#   r   r   r      s    ##Hr#   r   c                   d    e Zd Zd Z ej
                         ej                  d               Zd Z	y)TestForkInThreadc                 F    t        j                         \  | _        | _        y rV   )rw   piperead_fdwrite_fdr.   s    r   r"   zTestForkInThread.setUp   s    &(ggi#dmr#   c                    d fd}t        j                         5  t        j                  || j                  | j
                  f       | j                  t        j                  | j                  d      d       t        j                  | j
                         d d d        | j                         t        j                  d       y # 1 sw Y   2xY w)Nc                 ^   t        j                  t        d      5  t        j                         xr
	 d d d        y 	 d d d        	 t        j
                  |        t        j                  |d       t        j                  d       y # 1 sw Y   KxY w# t        j                  d       w xY w)Nignore)categoryaction   OKr   )warningscatch_warningsDeprecationWarningrw   forkclosewrite_exit)r   r   pids     r   fork_threadz7TestForkInThread.test_forkinthread.<locals>.fork_thread   s     ((2D08:  779$C$  %!5)  s   B	+B 	BB,   r   r   )exitcode)r   r@   r   r,   r   r   rF   rw   readr   rq   r   wait_process)r    r   r   s     @r   test_forkinthreadz"TestForkInThread.test_forkinthread   s     	$ //1 	$##K$,,1NORWWT\\15u=HHT]]#	$
 	S!S1-	$ 	$s   A<C

Cc                     	 t        j                  | j                         	 t        j                  | j                         y # t        $ r Y ,w xY w# t        $ r Y y w xY wrV   )rw   r   r   OSErrorr   r.   s    r   tearDownzTestForkInThread.tearDown	  sS    	HHT\\"	HHT]]#	  		
  		s"   A A 	AA	AAN)
r$   r%   r&   r"   r   requires_forkr   reap_threadsr   r   r'   r#   r   r   r      s9    0 W"". # .:	r#   r   __main__)rw   ru   r6   testr   test.supportr   _threadr   r8   r   ra   r   requires_working_threadingr:   r   r   r   r   TestCaser   r)   rz   r   r   r   r$   mainr'   r#   r   <module>r      s    	    )      +  + +4 8#v##%Bh'' Bw> w>t& &2#&/ #&J$
$$ $,x(( ,^ zHMMO r#   