B
    u9a^(  ã               @   sÎ   d 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Zddl	Z	ej
dd… dkrbe dej
 ¡‚e ¡ Ze ¡ ZejjdkoˆejjdkZd	d
„ Zdd„ Zdd„ ZG dd„ dejƒZdd„ ZedkrÊeƒ  dS )z6PyUnit testing that threads honor our signal semanticsé    N)Úsupporté   ÚwinzCan't test signal on %sZpthreadz
mutex+condc             C   s4   t   t j| ¡}t   t j|¡}t   t j|¡}|||fS )N)ÚsignalÚSIGUSR1ÚSIGUSR2ÚSIGALRM)Zfor_usr1Zfor_usr2Zfor_alrmZusr1Zusr2Zalrm© r	   ú(/usr/lib/python3.7/test_threadsignals.pyÚregisterSignals   s    r   c             C   s(   t |  d  d7  < t ¡ t |  d< d S )NÚtrippedé   Ú
tripped_by)Úsignal_blackboardÚthreadÚ	get_ident)ÚsigÚframer	   r	   r
   Úhandle_signals   s    r   c               C   s(   t  ttj¡ t  ttj¡ t ¡  d S )N)ÚosÚkillÚprocess_pidr   r   r   Úsignalled_allÚreleaser	   r	   r	   r
   Úsend_signals"   s    r   c               @   sÐ   e Zd Zdd„ Zdd„ Zdd„ Ze ed¡e e	j
 d¡oBe	jj d	¡e e	j
 d
¡d¡dd„ ƒƒƒZe ed¡e e	j
 d¡oˆe	jj d	¡e e	j
 d
¡d¡dd„ ƒƒƒZdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚThreadSignalsc          	   C   sà   t  ¡  t ¡  |  ¡  t ¡  W d Q R X ttj d dksPttj d dkrtzt 	d¡ t 
¡  W d t 	d¡ X |  ttj d d¡ |  ttj d t ¡ ¡ |  ttj d d¡ |  ttj d t ¡ ¡ t ¡  d S )Nr   r   r   r   )r   Úwait_threads_exitr   ÚacquireÚspawnSignallingThreadr   r   r   r   ÚalarmÚpauseZassertEqualr   r   r   )Úselfr	   r	   r
   Útest_signals)   s"    



zThreadSignals.test_signalsc             C   s   t  td¡ d S )Nr	   )r   Ústart_new_threadr   )r!   r	   r	   r
   r   I   s    z#ThreadSignals.spawnSignallingThreadc             C   s   t ‚d S )N)ÚKeyboardInterrupt)r!   r   r   r	   r	   r
   Úalarm_interruptL   s    zThreadSignals.alarm_interruptz/POSIX condition variables cannot be interruptedZlinuxzBIssue 34004: musl does not allow interruption of locks by signals.Zopenbsdz%lock cannot be interrupted on OpenBSDc          
   C   s€   t   t j| j¡}zPt ¡ }| ¡  t  d¡ t ¡ }| j	t
|jdd t ¡ | }|  |d¡ W d t  d¡ t   t j|¡ X d S )Nr   é   )Útimeoutg      @r   )r   r   r%   r   Úallocate_lockr   r   ÚtimeÚ	monotonicÚassertRaisesr$   Ú
assertLess)r!   ÚoldalrmÚlockÚt1Údtr	   r	   r
   Útest_lock_acquire_interruptionO   s    

z,ThreadSignals.test_lock_acquire_interruptionc          
      sÈ   t   t j| j¡}z˜t ¡ ‰ ‡ fdd„}t ¡ r t |d¡ x"ˆ jddr^ˆ  	¡  t
 d¡ q>W t  d¡ t
 ¡ }| jtˆ jdd	 t
 ¡ | }|  |d
¡ W d Q R X W d t  d¡ t   t j|¡ X d S )Nc                  s   ˆ   ¡  d S )N)r   r	   )Úrlockr	   r
   Úother_thread   s    zCThreadSignals.test_rlock_acquire_interruption.<locals>.other_threadr	   F)Úblockingg{®Gáz„?r   r&   )r'   g      @r   )r   r   r%   r   ÚRLockr   r   r#   r   r   r)   Úsleepr   r*   r+   r$   r,   )r!   r-   r3   r/   r0   r	   )r2   r
   Útest_rlock_acquire_interruptionn   s     


z-ThreadSignals.test_rlock_acquire_interruptionc          
      s¨   dˆ_ ‡fdd„}t tj|¡}zr‡ fdd„}t ¡ T t |d¡ x"ˆ jddrfˆ  ¡  t	 
d¡ qFW ˆ  ¡ }ˆ ˆj ¡ ˆ |¡ W d Q R X W d t tj|¡ X d S )	NFc                s
   dˆ _ d S )NT)Ú	sig_recvd)r   r   )r!   r	   r
   Ú
my_handler–   s    z9ThreadSignals.acquire_retries_on_intr.<locals>.my_handlerc                  s6   ˆ   ¡  t d¡ t ttj¡ t d¡ ˆ  ¡  d S )Ng      à?)	r   r)   r6   r   r   r   r   r   r   r	   )r.   r	   r
   r3   ›   s
    

z;ThreadSignals.acquire_retries_on_intr.<locals>.other_threadr	   )r4   g{®Gáz„?)r8   r   r   r   r   r   r#   r   r   r)   r6   Z
assertTrue)r!   r.   r9   Úold_handlerr3   Úresultr	   )r.   r!   r
   Úacquire_retries_on_intr”   s    
z%ThreadSignals.acquire_retries_on_intrc             C   s   |   t ¡ ¡ d S )N)r<   r   r(   )r!   r	   r	   r
   Ú!test_lock_acquire_retries_on_intr´   s    z/ThreadSignals.test_lock_acquire_retries_on_intrc             C   s   |   t ¡ ¡ d S )N)r<   r   r5   )r!   r	   r	   r
   Ú"test_rlock_acquire_retries_on_intr·   s    z0ThreadSignals.test_rlock_acquire_retries_on_intrc          
      sä   d ˆ_ d ˆ_dˆ_t ¡ ‰ ˆ  ¡  t ¡ ‰ˆ ¡  ‡fdd„}t tj|¡}z‚‡‡fdd„}‡ fdd„}t 	¡ V t 
|d¡ |ƒ  ˆ  ¡  ˆ ˆjˆj  d	¡ ˆ ˆjˆj  d
¡ ˆ ˆjd¡ W d Q R X W d t tj|¡ X d S )Nr   c                s   ˆ  j d7  _ d S )Nr   )Ú
sigs_recvd)Zsignumr   )r!   r	   r
   r9   É   s    z@ThreadSignals.test_interrupted_timed_acquire.<locals>.my_handlerc                  s$   t  ¡ ˆ_ˆ jdd t  ¡ ˆ_d S )Ng      à?)r'   )r)   r*   Ústartr   Úendr	   )r.   r!   r	   r
   Útimed_acquireÍ   s    
zCThreadSignals.test_interrupted_timed_acquire.<locals>.timed_acquirec                 s6   x(t dƒD ]} t d¡ t ttj¡ q
W ˆ  ¡  d S )Né(   g{®Gáz”?)	Úranger)   r6   r   r   r   r   r   r   )Ú_)Údoner	   r
   r   Ñ   s    
zBThreadSignals.test_interrupted_timed_acquire.<locals>.send_signalsr	   g       @g333333Ó?)r@   rA   r?   r   r(   r   r   r   r   r   r#   r,   ZassertGreater)r!   r9   r:   rB   r   r	   )rF   r.   r!   r
   Útest_interrupted_timed_acquireº   s(    
z,ThreadSignals.test_interrupted_timed_acquireN)Ú__name__Ú
__module__Ú__qualname__r"   r   r%   ÚunittestZskipIfÚUSING_PTHREAD_CONDÚsysÚplatformÚ
startswithÚthread_infoÚversionr1   r7   r<   r=   r>   rG   r	   r	   r	   r
   r   '   s*     r   c              C   sR   t jdddœt jdddœt jdddœiattttƒ} zt t	¡ W d t| Ž  X d S )Nr   )r   r   )
r   r   r   r   r   r   r   r   Zrun_unittestr   )Zoldsigsr	   r	   r
   Ú	test_mainé   s    rR   Ú__main__)Ú__doc__rK   r   r   rM   Ztestr   Ú_threadr   r)   rN   ZSkipTestÚgetpidr   r(   r   rP   Únamer.   rL   r   r   r   ZTestCaser   rR   rH   r	   r	   r	   r
   Ú<module>   s*   	 C