B
    u9a­!  ã               @   sä   d dl Z d dlZd dl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
 dZdZdZe ¡ Zdd„ ZG d	d
„ d
ejƒZG dd„ deƒZG dd„ dƒZG dd„ deƒZG dd„ de
jƒZG dd„ dejƒZedkràe ¡  dS )é    N)Úsupport)Ú
lock_testsé
   é   g{®Gáz„?c          	   C   s"   t jrt t| ƒ W dQ R X dS )z2Helper function for printing out debugging output.N)r   ÚverboseÚ_print_mutexÚprint)Úarg© r
   ú!/usr/lib/python3.7/test_thread.pyÚverbose_print   s    r   c               @   s   e Zd Zdd„ ZdS )ÚBasicThreadTestc             C   sX   t  ¡ | _| j ¡  t  ¡ | _t  ¡ | _d| _d| _d| _t	 
¡ }| jt	jf|žŽ  d S )Nr   )ÚthreadÚallocate_lockÚ
done_mutexÚacquireÚrunning_mutexÚrandom_mutexÚcreatedÚrunningÚ
next_identr   Zthreading_setupZ
addCleanupZthreading_cleanup)ÚselfÚkeyr
   r
   r   ÚsetUp   s    



zBasicThreadTest.setUpN)Ú__name__Ú
__module__Ú__qualname__r   r
   r
   r
   r   r      s   r   c               @   sV   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Ze e	j
d	kd
¡dd„ ƒZdd„ Zdd„ ZdS )ÚThreadRunningTestsc          	   C   s`   | j P |  jd7  _td| j ƒ t | j| jf¡ |  jd7  _|  jd7  _W d Q R X d S )Né   zcreating task %s)r   r   r   r   Ústart_new_threadÚtaskr   r   )r   r
   r
   r   Únewtask*   s    zThreadRunningTests.newtaskc          	   C   sŽ   | j  t ¡ d }W d Q R X td|t|d ƒf ƒ t |¡ td| ƒ | j2 |  jd8  _| jt	kr€| jdkr€| j
 ¡  W d Q R X d S )Ng     ˆÃ@ztask %s will run for %susg    €„.Aztask %s doner   r   )r   Úrandomr   ÚroundÚtimeÚsleepr   r   r   ÚNUMTASKSr   Úrelease)r   ÚidentÚdelayr
   r
   r   r    2   s    
zThreadRunningTests.taskc          	   C   sL   t  ¡ : xttƒD ]}|  ¡  qW tdƒ | j ¡  tdƒ W d Q R X d S )Nz waiting for tasks to complete...zall tasks done)r   Úwait_threads_exitÚranger&   r!   r   r   r   )r   Úir
   r
   r   Útest_starting_threads=   s    

z(ThreadRunningTests.test_starting_threadsc             C   s2   |   t ¡ dd¡ t d¡ |   t ¡ dd¡ d S )Nr   zinitial stack size is not 0zstack_size not reset to default)ÚassertEqualr   Ú
stack_size)r   r
   r
   r   Útest_stack_sizeF   s    
z"ThreadRunningTests.test_stack_size)ÚntÚposixztest meant for nt and posixc          
   C   s  yt  d¡ W n: tk
r*   tdƒ Y n  t jk
rH   |  d¡ Y nX d}x8dD ]0}t  |¡ |  t  ¡ ||| ¡ td| ƒ qTW xldD ]d}td| ƒ d	| _d	| _t	 
¡ : xttƒD ]}|  ¡  q¾W td
ƒ | j ¡  tdƒ W d Q R X qŽW t  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)i   i   r   zsuccessfully set stack_size(%d))i   i   ztrying stack_size = (%d)r   z!waiting for all tasks to completezall tasks done)r   r/   Ú
ValueErrorr   ÚerrorZskipTestr.   r   r   r   r*   r+   r&   r!   r   r   )r   Zfail_msgZtssr,   r
   r
   r   Útest_nt_and_posix_stack_sizeM   s,    




z/ThreadRunningTests.test_nt_and_posix_stack_sizec          	      s¼   t  ¡ }t  ¡ ‰ˆ ¡  g ‰‡‡fdd„}t ¡ € t  |d¡ xˆsRt t	¡ qBW |  
t  ¡ |d ¡ ˆ ¡  g ‰ t |‡ fdd„¡}~xˆ sœt t	¡ qŒW |  
t  ¡ |¡ W d Q R X d S )Nc                  s   ˆ  d ¡ ˆ  ¡  ˆ  ¡  d S )N)Úappendr   r'   r
   )ÚmutÚstartedr
   r   r    s   s    
z,ThreadRunningTests.test__count.<locals>.taskr
   r   c                s
   ˆ   d ¡S )N)r6   )Ú_)Údoner
   r   Ú<lambda>ƒ   ó    z0ThreadRunningTests.test__count.<locals>.<lambda>)r   Ú_countr   r   r   r*   r   r$   r%   Ú
POLL_SLEEPr.   r'   ÚweakrefÚref)r   Zorigr    Zwrr
   )r:   r7   r8   r   Útest__countl   s"    
zThreadRunningTests.test__countc          
      s†   ‡fdd„}‡ fdd„}t  ¡ ‰t d¡B}|j‰ ||_ˆ ¡  t ¡  t  |d¡ ˆ ¡  W d Q R X W d Q R X |  d| 	¡ ¡ d S )Nc                  s   ˆ   ¡  t‚d S )N)r'   ÚSyntaxErrorr
   )r8   r
   r   r    ‹   s    zCThreadRunningTests.test_save_exception_state_on_error.<locals>.taskc                s0   yt ‚W n t k
r   Y nX ˆ | f|žŽ  d S )N)r3   )r   Úargs)Ú
real_writer
   r   ÚmywriteŽ   s
    zFThreadRunningTests.test_save_exception_state_on_error.<locals>.mywriteÚstderrr
   Z	Traceback)
r   r   r   Zcaptured_outputÚwriter   r*   r   ZassertInÚgetvalue)r   r    rE   rF   r
   )rD   r8   r   Ú"test_save_exception_state_on_error‰   s    
z5ThreadRunningTests.test_save_exception_state_on_errorN)r   r   r   r!   r    r-   r0   ÚunittestZskipIfÚosÚnamer5   rA   rI   r
   r
   r
   r   r   (   s   	r   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚBarrierc             C   s.   || _ d| _t ¡ | _t ¡ | _| j ¡  d S )Nr   )Únum_threadsÚwaitingr   r   Úcheckin_mutexÚcheckout_mutexr   )r   rN   r
   r
   r   Ú__init__    s
    

zBarrier.__init__c             C   s‚   | j  ¡  | jd | _| j| jkr<| jd | _| j ¡  d S | j  ¡  | j ¡  | jd | _| jdkrt| j  ¡  d S | j ¡  d S )Nr   r   )rP   r   rO   rN   rQ   r'   )r   r
   r
   r   Úenter§   s    





zBarrier.enterN)r   r   r   rR   rS   r
   r
   r
   r   rM   Ÿ   s   rM   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚBarrierTestc          	   C   sd   t  ¡ R ttƒ| _t| _x ttƒD ]}t | j	|f¡ q$W t
dƒ | j ¡  t
dƒ W d Q R X d S )Nzwaiting for tasks to endz
tasks done)r   r*   rM   r&   Úbarr   r+   r   r   Útask2r   r   r   )r   r,   r
   r
   r   Útest_barrierº   s    


zBarrierTest.test_barrierc          
   C   sÂ   x„t tƒD ]x}|dkrd}n| j t ¡ d }W d Q R X td|t|d ƒf ƒ t |¡ td||f ƒ | j 	¡  td| ƒ q
W | j
 |  jd8  _| jdk}W d Q R X |r¾| j ¡  d S )Nr   g     ˆÃ@ztask %s will run for %susg    €„.Aztask %s entering %sztask %s leaving barrierr   )r+   ÚNUMTRIPSr   r"   r   r#   r$   r%   rU   rS   r   r   r   r'   )r   r(   r,   r)   Zfinishedr
   r
   r   rV   Ä   s     

zBarrierTest.task2N)r   r   r   rW   rV   r
   r
   r
   r   rT   ¸   s   
rT   c               @   s   e Zd ZejZdS )Ú	LockTestsN)r   r   r   r   r   Zlocktyper
   r
   r
   r   rY   Ý   s   rY   c               @   s<   e Zd Zdd„ Ze eedƒd¡ej	dd„ ƒƒZ
dd„ Zd	S )
ÚTestForkInThreadc             C   s   t  ¡ \| _| _d S )N)rK   ÚpipeÚread_fdÚwrite_fd)r   r
   r
   r   r   â   s    zTestForkInThread.setUpÚforkzneed os.forkc          	      sZ   d‰‡ ‡fdd„}t  ¡ * t |d¡ ˆ  t ˆ jd¡dd¡ W d Q R X ˆ  ˆd¡ d S )	Nznot setc           	      s^   t  ¡ } | dkr>zt  ˆ j¡ t  ˆ jd¡ W d t  d¡ X nt  ˆ j¡ t  | d¡\} ‰d S )Nr   s   OK)rK   r^   Úcloser\   rG   r]   Ú_exitÚwaitpid)Úpid)r   Ústatusr
   r   Úthread1ê   s    z3TestForkInThread.test_forkinthread.<locals>.thread1r
   é   s   OKzUnable to fork() in threadr   )r   r*   r   r   r.   rK   Úreadr\   )r   rd   r
   )r   rc   r   Útest_forkinthreadå   s    
z"TestForkInThread.test_forkinthreadc             C   sP   yt  | j¡ W n tk
r$   Y nX yt  | j¡ W n tk
rJ   Y nX d S )N)rK   r_   r\   ÚOSErrorr]   )r   r
   r
   r   ÚtearDown  s    zTestForkInThread.tearDownN)r   r   r   r   rJ   Z
skipUnlessÚhasattrrK   r   Zreap_threadsrg   ri   r
   r
   r
   r   rZ   á   s   rZ   Ú__main__)rK   rJ   r"   Ztestr   Ú_threadr   r$   Úsysr?   r   r&   rX   r>   r   r   r   ZTestCaser   r   rM   rT   rY   rZ   r   Úmainr
   r
   r
   r   Ú<module>   s*   w%,