
    <eG"                        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mZ  ej        d           dZ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j                  Z G d dej                  Zedk    r ej                     dS dS )    N)support)threading_helper)
lock_testsT)module
      g{Gz?c                     	 t           j        r1t          5  t          |            d d d            d S # 1 swxY w Y   d S d S N)r   verbose_print_mutexprint)args    '/usr/lib/python3.11/test/test_thread.pyverbose_printr      s    <  	 	#JJJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 s   266c                       e Zd Zd ZdS )BasicThreadTestc                 H   t          j                    | _        | j                                         t          j                    | _        t          j                    | _        d| _        d| _        d| _        t          j
                    } | j        t          j        g|R   d S 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0!!!#133"022.00(:ASAAAAAA    N)__name__
__module____qualname__r#    r$   r   r   r      s(        
B 
B 
B 
B 
Br$   r   c                   t    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d
S )ThreadRunningTestsc                    | 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            d S # 1 swxY w Y   d S )N   zcreating task %s)r   r   r   r   start_new_threadtaskr   r   r!   s    r   newtaskzThreadRunningTests.newtask,   s     	 	OOq OO,t>???#DI/ABBBLLALLLLALL	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A(A==BBc           	         | j         5  t          j                    dz  }d d d            n# 1 swxY w Y   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
                                         d d d            d S # 1 swxY w Y   d S )	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   sh    	. 	.MOOg-E	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.UUUE%)<L<L<L<LMNNN
5nu,--- 	* 	*LLALL|x''DLA,=,='')))	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*s   +//	ACC"Cc                 "   t          j                    5  t          t                    D ]}|                                  t          d           | j                                         t          d           d d d            d S # 1 swxY w Y   d S )Nz waiting for tasks to complete...all tasks done)r   wait_threads_exitranger;   r0   r   r   r   r!   is     r   test_starting_threadsz(ThreadRunningTests.test_starting_threads?   s    /11 	, 	,8__  <===O##%%%*+++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,s   A#BBBc                     |                      t          j                    dd           t          j        d           |                      t          j                    dd           d S )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   s]    *,,a1NOOO!*,,a1RSSSSSr$   )ntposixztest meant for nt and posixc                    	 t          j        d           nD# t          $ r t          d           Y n)t           j        $ r |                     d           Y nw xY wd}dD ]S}t          j        |           |                     t          j                    |||z             t          d|z             TdD ]}t          d|z             d	| _        d	| _        t          j
                    5  t          t                    D ]}|                                  t          d
           | j                                         t          d           d d d            n# 1 swxY w Y   t          j        d	           d S )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))rM   rN   ztrying stack_size = (%d)r   z!waiting for all tasks to completer@   )r   rH   
ValueErrorr   errorskipTestrG   r   r   r   rA   rB   r;   r0   r   r   )r!   fail_msgtssrD   s       r   test_nt_and_posix_stack_sizez/ThreadRunningTests.test_nt_and_posix_stack_sizeO   s   	"d#### 	0 	0 	0 / 0 0 0 0 0| 	" 	" 	"MM ! " " " " "	" <( 	C 	CCc"""V.00#x#~FFF;cABBBB% 
	0 
	0C4s:;;;DODL!355 0 0x # #ALLNNNNABBB''))).///0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 	!s'    A#AA*A#EE	 E	c                    t          j                    }t          j                                                     g fd}t	          j                    5  t          j        |d           st          j        t                     | 
                    t          j                    |dz                                               g t          j        |fd          }~s.t          j        t                     t          j                     .| 
                    t          j                    |           d d d            d S # 1 swxY w Y   d S )Nc                                           d                                                                                d S r
   )appendr   r<   )mutstarteds   r   r.   z,ThreadRunningTests.test__count.<locals>.tasku   s3    NN4   KKMMMKKMMMMMr$   r(   r,   c                 .                         d           S r
   )rW   )_dones    r   <lambda>z0ThreadRunningTests.test__count.<locals>.<lambda>   s    T[[->-> r$   )r   _countr   r   r   rA   r-   r9   r:   
POLL_SLEEPrG   r<   weakrefrefr   
gc_collect)r!   origr.   wrr\   rX   rY   s       @@@r   test__countzThreadRunningTests.test__countn   s   }"$$	 	 	 	 	 	
 /11 	4 	4#D"--- '
:&&&  'V]__dQh777KKMMM DT#>#>#>#>??B %
:&&&"$$$  % V]__d333!	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4s   C#E		EEc                 x   fd}t          j                    t          j                    5 }t	          j                    5                                   t          j        |d                                            d d d            n# 1 swxY w Y   |                     t          |j
        j                  d           |                     |j
        j        |           |                     |j
        j        d           |                     |j
        j                   d d d            d S # 1 swxY w Y   d S )Nc                  J                                       t          d          )Ntask failed)r<   rO   )rY   s   r   r.   z:ThreadRunningTests.test_unraisable_exception.<locals>.task   s!    OO]+++r$   r(   rh   z&Exception ignored in thread started by)r   r   r   catch_unraisable_exceptionr   rA   r   r-   rG   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1 
	>R!355 " "!!!'b111!!!" " " " " " " " " " " " " " "
 S!899=IIIMM"-.555R]2EG G G  !<===
	> 
	> 
	> 
	> 
	> 
	> 
	> 
	> 
	> 
	> 
	> 
	> 
	> 
	> 
	> 
	> 
	> 
	>s6   D/>B?D/B	D/B	BD//D36D3N)r%   r&   r'   r0   r.   rE   rI   unittestskipIfosnamerT   re   rs   r(   r$   r   r*   r*   *   s          	* 	* 	*, , ,T T T X_RWO35RSS  TS<4 4 4<> > > > >r$   r*   c                       e Zd Zd Zd ZdS )Barrierc                     || _         d| _        t          j                    | _        t          j                    | _        | j                                         d S r   )num_threadswaitingr   r   checkin_mutexcheckout_mutexr   )r!   r{   s     r   __init__zBarrier.__init__   sN    &$244$244##%%%%%r$   c                    | j                                          | j        dz   | _        | j        | j        k    r*| j        dz
  | _        | j                                         d S | j                                          | j                                         | j        dz
  | _        | j        dk    r| j                                          d S | j                                         d S )Nr,   r   )r}   r   r|   r{   r~   r<   r/   s    r   enterzBarrier.enter   s    ""$$$|a'<4++++a/DL'')))F""$$$##%%%|a'<1&&(((F##%%%%%r$   N)r%   r&   r'   r   r   r(   r$   r   ry   ry      s2        & & && & & & &r$   ry   c                       e Zd Zd Zd ZdS )BarrierTestc                 z   t          j                    5  t          t                    | _        t          | _        t          t                    D ]}t          j        | j	        |f           t          d           | j                                         t          d           d d d            d S # 1 swxY w Y   d S )Nzwaiting for tasks to endz
tasks done)r   rA   ry   r;   barr   rB   r   r-   task2r   r   r   rC   s     r   test_barrierzBarrierTest.test_barrier   s    /11 	( 	(x((DH#DL8__ : :'
QD99994555O##%%%,'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(s   BB00B47B4c           
      R   t          t                    D ]}|dk    rd}n5| j        5  t          j                    dz  }d d d            n# 1 swxY w Y   t	          d|dt          |dz            d           t          j        |           t	          d|d|           | j        	                                 t	          d|z             | j
        5  | xj        d	z  c_        | j        dk    }d d d            n# 1 swxY w Y   |r| j                                         d S d S )
Nr   r2   r3   r4   r5   r6   z
 entering ztask %s leaving barrierr,   )rB   NUMTRIPSr   r7   r   r8   r9   r:   r   r   r   r   r   r<   )r!   r=   rD   r>   finisheds        r   r   zBarrierTest.task2   s   x 	= 	=Azz & 6 6"MOOg5E6 6 6 6 6 6 6 6 6 6 6 6 6 6 6M 55%"4"4"4"46 7 7 7JuM555!!<===HNN3e;<<<< 	) 	)LLALL |q(H	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)  	&O##%%%%%	& 	&s#   A

A	A	C??DDN)r%   r&   r'   r   r   r(   r$   r   r   r      s2        ( ( (& & & & &r$   r   c                       e Zd Zej        ZdS )	LockTestsN)r%   r&   r'   r   r   locktyper(   r$   r   r   r      s        #HHHr$   r   c                   d    e Zd Zd Z ej                    ej        d                         Zd Z	dS )TestForkInThreadc                 F    t          j                    \  | _        | _        d S r
   )rv   piperead_fdwrite_fdr/   s    r   r#   zTestForkInThread.setUp   s    &(gii#dmmmr$   c                    d fd}t          j                    5  t          j        || j        | j        f           |                     t          j        | j        d          d           t          j	        | j                   d d d            n# 1 swxY w Y   | 
                               t          j        d           d S )Nc                     t          j                    rd S 	 t          j        |            t          j        |d           t          j        d           d S # t          j        d           w xY w)N   OKr   )rv   forkclosewrite_exit)r   r   pids     r   fork_threadz7TestForkInThread.test_forkinthread.<locals>.fork_thread   sg     '))C !!!5)))s   )A A/   r   r   )exitcode)r   rA   r   r-   r   r   rG   rv   readr   rp   r   wait_process)r!   r   r   s     @r   test_forkinthreadz"TestForkInThread.test_forkinthread   s    	 	 	 	 	  /11 	$ 	$#K$,1NOOORWT\155u===HT]###	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$
 	S!!!S1------s   A)BBBc                     	 t          j        | j                   n# t          $ r Y nw xY w	 t          j        | j                   d S # t          $ r Y d S w xY wr
   )rv   r   r   OSErrorr   r/   s    r   tearDownzTestForkInThread.tearDown  s{    	HT\"""" 	 	 	D		HT]##### 	 	 	DD	s    
))A 
AAN)
r%   r&   r'   r#   r   requires_forkr   reap_threadsr   r   r(   r$   r   r   r      sg        0 0 0 W". . #" .6	 	 	 	 	r$   r   __main__)rv   rt   r7   testr   test.supportr   _threadr   r9   r`   r   requires_working_threadingr;   r   r_   r   r   r   TestCaser   r*   ry   r   r   r   r%   mainr(   r$   r   <module>r      s   				         ) ) ) ) ) )             +  +4 8 8 8 8
#v#%%  B B B B Bh' B B Br> r> r> r> r> r> r> r>j& & & & & & & &2#& #& #& #& #&/ #& #& #&J$ $ $ $ $
$ $ $ $* * * * *x( * * *Z zHMOOOOO r$   