B
    u9aq  ã               @   s  d Z ddlZddlZddlmZmZ ddlZddlZddlZddl	m
Z
 dd„ ZG dd„ deƒZG d	d
„ d
ejƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )z/
Various tests for synchronization primitives.
é    N)Ústart_new_threadÚTIMEOUT_MAX)Úsupportc               C   s   t  d¡ d S )Ng{®Gáz„?)ÚtimeÚsleep© r   r   ú /usr/lib/python3.7/lock_tests.pyÚ_wait   s    r	   c               @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )ÚBunchz
    A bunch of threads.
    Fc                s|   ˆ ˆ_ |ˆ_g ˆ_g ˆ_| ˆ_t ¡ ˆ_ˆj ¡  ‡ ‡fdd„}y xt	|ƒD ]}t
|dƒ qNW W n   dˆ_‚ Y nX dS )z¹
        Construct a bunch of `n` threads running the same function `f`.
        If `wait_before_exit` is True, the threads won't terminate until
        do_finish() is called.
        c           	      sD   t  ¡ } ˆj | ¡ z
ˆ ƒ  W d ˆj | ¡ xˆjs<tƒ  q.W X d S )N)Ú	threadingÚ	get_identÚstartedÚappendÚfinishedÚ	_can_exitr	   )Útid)ÚfÚselfr   r   Útask%   s    
zBunch.__init__.<locals>.taskr   TN)r   Únr   r   r   r   Úwait_threads_exitÚwait_threadÚ	__enter__Úranger   )r   r   r   Zwait_before_exitr   Úir   )r   r   r   Ú__init__   s    


zBunch.__init__c             C   s    xt | jƒ| jk rtƒ  qW d S )N)Úlenr   r   r	   )r   r   r   r   Úwait_for_started6   s    zBunch.wait_for_startedc             C   s0   xt | jƒ| jk rtƒ  qW | j d d d ¡ d S )N)r   r   r   r	   r   Ú__exit__)r   r   r   r   Úwait_for_finished:   s    
zBunch.wait_for_finishedc             C   s
   d| _ d S )NT)r   )r   r   r   r   Ú	do_finish@   s    zBunch.do_finishN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r    r   r   r   r   r
      s
   
r
   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚBaseTestCasec             C   s   t  ¡ | _d S )N)r   Zthreading_setupÚ_threads)r   r   r   r   ÚsetUpE   s    zBaseTestCase.setUpc             C   s   t j| jŽ  t  ¡  d S )N)r   Zthreading_cleanupr&   Zreap_children)r   r   r   r   ÚtearDownH   s    zBaseTestCase.tearDownc             C   s$   |   ||d ¡ |  ||d ¡ d S )Ng333333ã?g      $@)ZassertGreaterEqualÚ
assertLess)r   ÚactualZexpectedr   r   r   ÚassertTimeoutL   s    zBaseTestCase.assertTimeoutN)r!   r"   r#   r'   r(   r+   r   r   r   r   r%   D   s   r%   c               @   sx   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚBaseLockTestsz;
    Tests for both recursive and non-recursive locks.
    c             C   s   |   ¡ }~d S )N)Úlocktype)r   Úlockr   r   r   Útest_constructorZ   s    zBaseLockTests.test_constructorc             C   s   |   ¡ }|  t|ƒd¡ ~d S )Nz<unlocked .* object (.*)?at .*>)r-   ÚassertRegexÚrepr)r   r.   r   r   r   Ú	test_repr^   s    zBaseLockTests.test_reprc             C   s&   |   ¡ }| ¡  |  t|ƒd¡ ~d S )Nz<locked .* object (.*)?at .*>)r-   Úacquirer0   r1   )r   r.   r   r   r   Útest_locked_reprc   s    zBaseLockTests.test_locked_reprc             C   s   |   ¡ }| ¡  ~d S )N)r-   r3   )r   r.   r   r   r   Útest_acquire_destroyi   s    z"BaseLockTests.test_acquire_destroyc             C   s   |   ¡ }| ¡  | ¡  ~d S )N)r-   r3   Úrelease)r   r.   r   r   r   Útest_acquire_releasen   s    z"BaseLockTests.test_acquire_releasec             C   s$   |   ¡ }|  | d¡¡ | ¡  d S )NF)r-   Ú
assertTruer3   r6   )r   r.   r   r   r   Útest_try_acquiret   s    zBaseLockTests.test_try_acquirec                sJ   |   ¡ ‰ ˆ  ¡  g ‰‡ ‡fdd„}t|dƒ ¡  |  ˆd ¡ ˆ  ¡  d S )Nc                  s   ˆ  ˆ  d¡¡ d S )NF)r   r3   r   )r.   Úresultr   r   r   }   s    z3BaseLockTests.test_try_acquire_contended.<locals>.fé   r   )r-   r3   r
   r   ÚassertFalser6   )r   r   r   )r.   r:   r   Útest_try_acquire_contendedy   s    z(BaseLockTests.test_try_acquire_contendedc                sp   |   ¡ ‰ ˆ  ¡  d}‡ fdd„}t||ƒ}| ¡  tƒ  |  t|jƒd¡ ˆ  ¡  | 	¡  |  t|jƒ|¡ d S )Né   c                  s   ˆ   ¡  ˆ  ¡  d S )N)r3   r6   r   )r.   r   r   r   ‡   s    z/BaseLockTests.test_acquire_contended.<locals>.fr   )
r-   r3   r
   r   r	   ÚassertEqualr   r   r6   r   )r   ÚNr   Úbr   )r.   r   Útest_acquire_contendedƒ   s    
z$BaseLockTests.test_acquire_contendedc                sV   |   ¡ ‰ ‡ fdd„}d‡ fdd„	}|ƒ  t|dƒ ¡  |  t|t¡ t|dƒ ¡  d S )Nc                  s   ˆ   ¡  ˆ  ¡  d S )N)r3   r6   r   )r.   r   r   r   •   s    z"BaseLockTests.test_with.<locals>.fc          	      s    ˆ  | d k	r| ‚W d Q R X d S )Nr   )Úerr)r.   r   r   Ú_with˜   s    z&BaseLockTests.test_with.<locals>._withr;   )N)r-   r
   r   ÚassertRaisesÚ	TypeError)r   r   rD   r   )r.   r   Ú	test_with“   s    zBaseLockTests.test_withc                s`   |   ¡ ‰ ‡ fdd„}tt ¡ ƒ}t|dƒ ¡  tt ¡ ƒ|kr\t d¡ |  |tt ¡ ƒ¡ d S )Nc                  s   ˆ   ¡  ˆ  ¡  d S )N)r3   r6   r   )r.   r   r   r   §   s    z)BaseLockTests.test_thread_leak.<locals>.fé   gš™™™™™Ù?)	r-   r   r   Ú	enumerater
   r   r   r   r?   )r   r   r   r   )r.   r   Útest_thread_leak£   s    
zBaseLockTests.test_thread_leakc                sÜ   |   ¡ ‰ |  tˆ jdd¡ | jtˆ jdd | jtˆ jdd | jtˆ jtd d ˆ jtd ˆ  ¡  t ¡ }|  	ˆ jdd¡ t ¡ }|  
|| d¡ g ‰‡ ‡fdd„}t|dƒ ¡  |  ˆd ¡ |  ˆd d	¡ d S )
Nr   r;   iœÿÿÿ)Útimeoutg}Ã”%­I²Tr>   c                 s4   t  ¡ } ˆ ˆ jdd¡ t  ¡ }ˆ ||  ¡ d S )Ng      à?)rK   )r   Ú	monotonicr   r3   )Út1Út2)r.   Úresultsr   r   r   Ç   s    z%BaseLockTests.test_timeout.<locals>.fg      à?)r-   rE   Ú
ValueErrorr3   ÚOverflowErrorr   r6   r   rL   r8   r)   r
   r   r<   r+   )r   rM   rN   r   r   )r.   rO   r   Útest_timeout¶   s     zBaseLockTests.test_timeoutc             C   s"   |   ¡ }t |¡}|  |ƒ ¡ d S )N)r-   ÚweakrefÚrefZassertIsNotNone)r   r.   rT   r   r   r   Útest_weakref_existsÐ   s    
z!BaseLockTests.test_weakref_existsc             C   s$   |   ¡ }t |¡}~|  |ƒ ¡ d S )N)r-   rS   rT   ZassertIsNone)r   r.   rT   r   r   r   Útest_weakref_deletedÕ   s    
z"BaseLockTests.test_weakref_deletedN)r!   r"   r#   r$   r/   r2   r4   r5   r7   r9   r=   rB   rG   rJ   rR   rU   rV   r   r   r   r   r,   U   s   
r,   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú	LockTestszn
    Tests for non-recursive, weak locks
    (which can be acquired and released from different threads).
    c          	      sš   |   ¡ ‰ g ‰‡ ‡fdd„}t ¡ n t|dƒ xtˆƒdkrDtƒ  q0W tƒ  |  tˆƒd¡ ˆ  ¡  xtˆƒdkrztƒ  qfW |  tˆƒd¡ W d Q R X d S )Nc                  s(   ˆ   ¡  ˆ d ¡ ˆ   ¡  ˆ d ¡ d S )N)r3   r   r   )r.   Úphaser   r   r   æ   s    
z#LockTests.test_reacquire.<locals>.fr   r   r;   é   )r-   r   r   r   r   r	   r?   r6   )r   r   r   )r.   rX   r   Útest_reacquireá   s    



zLockTests.test_reacquirec                sB   |   ¡ ‰ ˆ  ¡  ‡ fdd„}t|dƒ}| ¡  ˆ  ¡  ˆ  ¡  d S )Nc                  s   ˆ   ¡  d S )N)r6   r   )r.   r   r   r   û   s    z*LockTests.test_different_thread.<locals>.fr;   )r-   r3   r
   r   r6   )r   r   rA   r   )r.   r   Útest_different_thread÷   s    
zLockTests.test_different_threadc             C   sN   |   ¡ }| ¡  |  |jdd¡ | ¡  |  | ¡ ¡ |  |jdd¡ d S )Ng{®Gáz„?)rK   F)Zblocking)r-   r3   r<   r6   Úlockedr8   )r   r.   r   r   r   Útest_state_after_timeout  s    z"LockTests.test_state_after_timeoutN)r!   r"   r#   r$   rZ   r[   r]   r   r   r   r   rW   Ü   s   rW   c               @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )Ú
RLockTestsz$
    Tests for recursive locks.
    c             C   s<   |   ¡ }| ¡  | ¡  | ¡  | ¡  | ¡  | ¡  d S )N)r-   r3   r6   )r   r.   r   r   r   rZ     s    zRLockTests.test_reacquirec             C   sX   |   ¡ }|  t|j¡ | ¡  | ¡  | ¡  | ¡  | ¡  | ¡  |  t|j¡ d S )N)r-   rE   ÚRuntimeErrorr6   r3   )r   r.   r   r   r   Útest_release_unacquired  s    z"RLockTests.test_release_unacquiredc             C   sX   |   ¡ }|  t|j¡ | ¡  | ¡  | ¡  | ¡  | ¡  | ¡  |  t|j¡ d S )N)r-   rE   r_   Ú_release_saver3   r6   )r   r.   r   r   r   Útest_release_save_unacquired&  s    z'RLockTests.test_release_save_unacquiredc                sJ   |   ¡ ‰ ‡ fdd„}t|ddƒ}z|  tˆ j¡ W d | ¡  X | ¡  d S )Nc                  s   ˆ   ¡  d S )N)r3   r   )r.   r   r   r   5  s    z+RLockTests.test_different_thread.<locals>.fr;   T)r-   r
   rE   r_   r6   r    r   )r   r   rA   r   )r.   r   r[   2  s    
z RLockTests.test_different_threadc                s    |   ¡ ‰ |  ˆ  ¡ ¡ ˆ  ¡  |  ˆ  ¡ ¡ ˆ  ¡  |  ˆ  ¡ ¡ g ‰‡ ‡fdd„}t|dƒ ¡  |  ˆd ¡ ˆ  ¡  |  ˆ  ¡ ¡ ˆ  ¡  |  ˆ  ¡ ¡ d S )Nc                  s   ˆ  ˆ  ¡ ¡ d S )N)r   Ú	_is_ownedr   )r.   r:   r   r   r   F  s    z$RLockTests.test__is_owned.<locals>.fr;   r   )r-   r<   rc   r3   r8   r
   r   r6   )r   r   r   )r.   r:   r   Útest__is_owned>  s    zRLockTests.test__is_ownedN)	r!   r"   r#   r$   rZ   r`   rb   r[   rd   r   r   r   r   r^     s   	r^   c               @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )Ú
EventTestsz"
    Tests for Event objects.
    c             C   sr   |   ¡ }|  | ¡ ¡ | ¡  |  | ¡ ¡ | ¡  |  | ¡ ¡ | ¡  |  | ¡ ¡ | ¡  |  | ¡ ¡ d S )N)Ú	eventtyper<   Zis_setÚsetr8   Úclear)r   Úevtr   r   r   Útest_is_setU  s    zEventTests.test_is_setc                s|   d}g ‰g ‰‡ ‡‡fdd„}t ||ƒ}| ¡  tƒ  |  tˆƒd¡ ˆ  ¡  | ¡  |  ˆdg| ¡ |  ˆdg| ¡ d S )Nr>   c                  s    ˆ  ˆ  ¡ ¡ ˆ  ˆ  ¡ ¡ d S )N)r   Úwaitr   )ri   Úresults1Úresults2r   r   r   f  s    z#EventTests._check_notify.<locals>.fr   T)r
   r   r	   r?   r   rg   r   )r   ri   r@   r   rA   r   )ri   rl   rm   r   Ú_check_notifya  s    
zEventTests._check_notifyc             C   s0   |   ¡ }|  |¡ | ¡  | ¡  |  |¡ d S )N)rf   rn   rg   rh   )r   ri   r   r   r   Útest_notifyr  s
    
zEventTests.test_notifyc                s¼   |   ¡ ‰ g ‰g ‰d}‡ ‡‡fdd„}t||ƒ ¡  |  ˆdg| ¡ x&ˆD ]\}}|  |¡ |  |d¡ qJW g ‰g ‰ˆ  ¡  t||ƒ ¡  |  ˆdg| ¡ xˆD ]\}}|  |¡ q¢W d S )Nr>   c                 s@   ˆ  ˆ  d¡¡ t ¡ } ˆ  d¡}t ¡ }ˆ  |||  f¡ d S )Ng        g      à?)r   rk   r   rL   )rM   ÚrrN   )ri   rl   rm   r   r   r     s
    
z"EventTests.test_timeout.<locals>.fFg      à?T)rf   r
   r   r?   r<   r+   rg   r8   )r   r@   r   rp   Údtr   )ri   rl   rm   r   rR   z  s"    
zEventTests.test_timeoutc                sn   |   ¡ ‰ g ‰d‰d}‡ ‡‡fdd„}t||ƒ}| ¡  t ˆ¡ ˆ  ¡  ˆ  ¡  | ¡  |  ˆdg| ¡ d S )Ng      Ð?r>   c                  s   ˆ  ˆ  ˆd ¡¡ d S )Né   )r   rk   r   )ri   rO   rK   r   r   r   š  s    z(EventTests.test_set_and_clear.<locals>.fT)	rf   r
   r   r   r   rg   rh   r   r?   )r   r@   r   rA   r   )ri   rO   rK   r   Útest_set_and_clear“  s    

zEventTests.test_set_and_clearc          	   C   s\   |   ¡ }|j |  |j d¡¡ W d Q R X | ¡  |j |  |j d¡¡ W d Q R X d S )NF)rf   Z_condr<   r3   Z_reset_internal_locks)r   ri   r   r   r   Útest_reset_internal_locks¤  s    z$EventTests.test_reset_internal_locksN)
r!   r"   r#   r$   rj   rn   ro   rR   rs   rt   r   r   r   r   re   P  s   re   c               @   sP   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚConditionTestsz(
    Tests for condition variables.
    c          	   C   s¦   |   ¡ }| ¡  | ¡  | ¡  | ¡  t ¡ }|   |¡}| ¡  |  | d¡¡ | ¡  |  | d¡¡ |  | d¡¡ | ¡  | |  | d¡¡ W d Q R X d S )NF)Úcondtyper3   r6   r   ZLockr<   r8   )r   Úcondr.   r   r   r   Útest_acquire³  s    
zConditionTests.test_acquirec             C   s   |   ¡ }|  t|j¡ d S )N)rv   rE   r_   rk   )r   rw   r   r   r   Útest_unacquired_waitÆ  s    z#ConditionTests.test_unacquired_waitc             C   s   |   ¡ }|  t|j¡ d S )N)rv   rE   r_   Únotify)r   rw   r   r   r   Útest_unacquired_notifyÊ  s    z%ConditionTests.test_unacquired_notifyc                sê  d}g ‰g ‰g ‰d‰‡ ‡‡‡‡fdd„}t ||ƒ}| ¡  xtˆƒdk rPtƒ  q<W ˆ ¡  |  ˆg ¡ ˆ  ¡  ˆ  d¡ tƒ  d‰ˆ  ¡  xtˆƒdk r tƒ  qŒW |  ˆdgd ¡ |  ˆg ¡ xtˆƒdk rÖtƒ  qÂW ˆ  ¡  ˆ  d¡ tƒ  d‰ˆ  ¡  x tˆƒtˆƒ d	k rtƒ  qþW |  ˆdgd d
gd  ¡ |  ˆd
gd ¡ xtˆƒdk rftƒ  qNW ˆ  ¡  ˆ  	¡  tƒ  d‰ˆ  ¡  xtˆƒdk r¤tƒ  qŒW |  ˆdgd d
gd  ¡ |  ˆd
gd dgd  ¡ | 
¡  d S )Nr>   r   c                 sd   ˆ   ¡  ˆ ˆ¡ ˆ  ¡ } ˆ  ¡  ˆ | ˆf¡ ˆ   ¡  ˆ ˆ¡ ˆ  ¡ } ˆ  ¡  ˆ | ˆf¡ d S )N)r3   r   rk   r6   )r:   )rw   Ú	phase_numÚreadyrl   rm   r   r   r   ß  s    

z'ConditionTests._check_notify.<locals>.fé   r;   )Tr;   rY   é   )TrY   )Tr~   )r
   r   r   r	   rh   r?   r3   rz   r6   Z
notify_allr   )r   rw   r@   r   rA   r   )rw   r|   r}   rl   rm   r   rn   Î  sX    






zConditionTests._check_notifyc             C   s    |   ¡ }|  |¡ |  |¡ d S )N)rv   rn   )r   rw   r   r   r   ro     s    
zConditionTests.test_notifyc                sh   |   ¡ ‰ g ‰d}‡ ‡fdd„}t||ƒ ¡  |  tˆƒ|¡ x&ˆD ]\}}|  |d¡ |  |¡ qBW d S )Nr>   c                 s@   ˆ   ¡  t ¡ } ˆ  d¡}t ¡ }ˆ  ¡  ˆ ||  |f¡ d S )Ng      à?)r3   r   rL   rk   r6   r   )rM   r:   rN   )rw   rO   r   r   r   "  s    
z&ConditionTests.test_timeout.<locals>.fg      à?)rv   r
   r   r?   r   r+   r<   )r   r@   r   rq   r:   r   )rw   rO   r   rR     s    zConditionTests.test_timeoutc          
      sv   ˆ  ¡ ‰ d‰‡ ‡‡fdd„}t|dƒ}| ¡  x:tdƒD ].}t d¡ ˆ  ˆd7 ‰ˆ  ¡  W d Q R X q8W | ¡  d S )Nr   c           	      s<   ˆ . ˆ   ‡fdd„¡} ˆ | ¡ ˆ ˆd¡ W d Q R X d S )Nc                  s   ˆ dkS )Nrr   r   r   )Ústater   r   Ú<lambda>9  ó    z8ConditionTests.test_waitfor.<locals>.f.<locals>.<lambda>rr   )Úwait_forr8   r?   )r:   )rw   r   r€   r   r   r   7  s    
z&ConditionTests.test_waitfor.<locals>.fr;   rr   g{®Gáz„?)rv   r
   r   r   r   r   rz   r   )r   r   rA   r   r   )rw   r   r€   r   Útest_waitfor4  s    

zConditionTests.test_waitforc          
      sŒ   ˆ  ¡ ‰ d‰g ‰‡ ‡‡‡fdd„}t|dƒ}| ¡  x:tdƒD ].}t d¡ ˆ  ˆd7 ‰ˆ  ¡  W d Q R X q>W | ¡  ˆ t	ˆƒd¡ d S )Nr   c           	      s^   ˆ P t  ¡ } ˆ j‡fdd„dd}t  ¡ |  } ˆ |¡ ˆ | d¡ ˆ d ¡ W d Q R X d S )Nc                  s   ˆ dkS )Nrr   r   r   )r€   r   r   r   L  r‚   z@ConditionTests.test_waitfor_timeout.<locals>.f.<locals>.<lambda>gš™™™™™¹?)rK   )r   rL   rƒ   r<   r+   r   )rq   r:   )rw   r   r€   Úsuccessr   r   r   I  s    
z.ConditionTests.test_waitfor_timeout.<locals>.fr;   r~   g{®Gáz„?)
rv   r
   r   r   r   r   rz   r   r?   r   )r   r   rA   r   r   )rw   r   r€   r…   r   Útest_waitfor_timeoutE  s    

z#ConditionTests.test_waitfor_timeoutN)r!   r"   r#   r$   rx   ry   r{   rn   ro   rR   r„   r†   r   r   r   r   ru   ®  s   Jru   c               @   sX   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚBaseSemaphoreTestszB
    Common tests for {bounded, unbounded} semaphore objects.
    c             C   s,   | j t| jdd | j t| jtj d d S )Néÿÿÿÿ)Úvalue)rE   rP   ÚsemtypeÚsysÚmaxsize)r   r   r   r   r/   b  s    z#BaseSemaphoreTests.test_constructorc             C   sH   |   d¡}| ¡  | ¡  |   d¡}| ¡  | ¡  | ¡  | ¡  d S )Nr;   rY   )rŠ   r3   r6   )r   Úsemr   r   r   rx   f  s    

zBaseSemaphoreTests.test_acquirec             C   s   |   ¡ }| ¡  ~d S )N)rŠ   r3   )r   r   r   r   r   r5   p  s    z'BaseSemaphoreTests.test_acquire_destroyc                sŠ  |   d¡‰ˆ ¡  d}g ‰g ‰g ‰d‰ ‡ ‡‡‡‡fdd„}t|dƒ}| ¡  xtˆƒtˆƒ dk rjtƒ  qNW |  ˆˆ dgd ¡ d‰ xtdƒD ]}ˆ ¡  qW xtˆƒtˆƒ dk r¾tƒ  q¢W |  t	ˆˆ ƒdgd dgd  ¡ d	‰ xtdƒD ]}ˆ ¡  qòW x"tˆƒtˆƒ d
k r$tƒ  qW |  t	ˆˆ ƒdgd dgd  d	gd  ¡ |  
ˆ d¡¡ ˆ ¡  | ¡  |  ˆdgd ¡ d S )Né   é
   r   c                  s4   ˆ  ˆ ¡ ¡ ˆ  ˆ ¡ ˆ  ˆ ¡ ¡ ˆ  ˆ ¡ d S )N)r   r3   r   )r|   rl   rm   r   Úsem_resultsr   r   r   }  s    
z4BaseSemaphoreTests.test_acquire_contended.<locals>.fé   r;   é   rY   é   FTé   )rŠ   r3   r
   r   r   r	   r?   r   r6   Úsortedr<   r   )r   r@   r   rA   r   r   )r|   rl   rm   r   r   r   rB   u  s:    



$.z)BaseSemaphoreTests.test_acquire_contendedc             C   sV   |   d¡}|  | d¡¡ |  | d¡¡ |  | d¡¡ | ¡  |  | d¡¡ d S )NrY   F)rŠ   r8   r3   r<   r6   )r   r   r   r   r   r9   š  s    
z#BaseSemaphoreTests.test_try_acquirec                sV   |   d¡‰ˆ ¡  g ‰ ‡ ‡fdd„}t|dƒ ¡  |  tˆ ƒdgd dgd  ¡ d S )	Nrr   c                  s$   ˆ   ˆ d¡¡ ˆ   ˆ d¡¡ d S )NF)r   r3   r   )rO   r   r   r   r   ¦  s    z8BaseSemaphoreTests.test_try_acquire_contended.<locals>.fr>   FrŽ   Tr~   )rŠ   r3   r
   r   r?   r•   )r   r   r   )rO   r   r   r=   ¢  s    
z-BaseSemaphoreTests.test_try_acquire_contendedc             C   s¤   |   d¡}| jt|jddd |  |jdd¡ |  |jdd¡ |  |jdd¡ | ¡  |  |jdd¡ t ¡ }|  |jdd¡ t ¡ | }|  	|d¡ d S )NrY   Fg      ð?)rK   g{®Gázt?g      à?)
rŠ   rE   rP   r3   r8   r<   r6   r   rL   r+   )r   r   Útrq   r   r   r   Útest_acquire_timeout¯  s    
z'BaseSemaphoreTests.test_acquire_timeoutc                sT   |   ¡ ‰ ˆ  ¡  ‡ fdd„}t|dƒ}| ¡  tƒ  |  |j¡ ˆ  ¡  | ¡  d S )Nc                  s   ˆ   ¡  ˆ  ¡  d S )N)r3   r6   r   )r   r   r   r   À  s    z0BaseSemaphoreTests.test_default_value.<locals>.fr;   )	rŠ   r3   r
   r   r	   r<   r   r6   r   )r   r   rA   r   )r   r   Útest_default_value¼  s    
z%BaseSemaphoreTests.test_default_valuec                sb   ˆ   d¡‰d‡ ‡fdd„	}|ƒ  ˆ  ˆ d¡¡ ˆ ¡  ˆ  t|t¡ ˆ  ˆ d¡¡ ˆ ¡  d S )NrY   c          
      sT   ˆF ˆ   ˆ d¡¡ ˆ ¡  ˆ ˆ  ˆ d¡¡ | r<| ‚W d Q R X W d Q R X d S )NF)r8   r3   r6   r<   )rC   )r   r   r   r   rD   Ì  s    z+BaseSemaphoreTests.test_with.<locals>._withF)N)rŠ   r8   r3   r6   rE   rF   )r   rD   r   )r   r   r   rG   Ê  s    
zBaseSemaphoreTests.test_withN)r!   r"   r#   r$   r/   rx   r5   rB   r9   r=   r—   r˜   rG   r   r   r   r   r‡   ]  s   
%r‡   c               @   s   e Zd ZdZdd„ ZdS )ÚSemaphoreTestsz)
    Tests for unbounded semaphores.
    c             C   s.   |   d¡}| ¡  | ¡  | ¡  | ¡  d S )Nr;   )rŠ   r6   r3   )r   r   r   r   r   r`   à  s
    
z&SemaphoreTests.test_release_unacquiredN)r!   r"   r#   r$   r`   r   r   r   r   r™   Û  s   r™   c               @   s   e Zd ZdZdd„ ZdS )ÚBoundedSemaphoreTestsz'
    Tests for bounded semaphores.
    c             C   s8   |   ¡ }|  t|j¡ | ¡  | ¡  |  t|j¡ d S )N)rŠ   rE   rP   r6   r3   )r   r   r   r   r   r`   î  s
    z-BoundedSemaphoreTests.test_release_unacquiredN)r!   r"   r#   r$   r`   r   r   r   r   rš   é  s   rš   c               @   sŠ   e Zd ZdZdZdZdd„ Zdd„ Zdd	„ Zd
d„ Z	d"dd„Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!S )#ÚBarrierTestsz$
    Tests for Barrier objects.
    r>   g       @c             C   s   | j | j| jd| _d S )N)rK   )Úbarriertyper@   ÚdefaultTimeoutÚbarrier)r   r   r   r   r'   þ  s    zBarrierTests.setUpc             C   s   | j  ¡  d S )N)rž   Úabort)r   r   r   r   r(      s    zBarrierTests.tearDownc             C   s"   t || jd ƒ}|ƒ  | ¡  d S )Nr;   )r
   r@   r   )r   r   rA   r   r   r   Úrun_threads  s    zBarrierTests.run_threadsc             C   s®   | j j}|  || j¡ xtt|ƒD ]h}|d  d¡ |  t|d ƒ|| ¡ | j  ¡  |d  d¡ |  t|d ƒ|d | ¡ | j  ¡  q W |  | j jd¡ |  	| j j
¡ d S )Nr   Tr;   )rž   Zpartiesr?   r@   r   r   r   rk   Ú	n_waitingr<   Úbroken)r   rO   r   Úmr   r   r   r   Ú	multipass  s    
zBarrierTests.multipassr;   c                s&   g g g‰‡ ‡‡fdd„}ˆ  |¡ dS )z;
        Test that a barrier is passed in lockstep
        c                  s   ˆ  ˆˆ ¡ d S )N)r¤   r   )ÚpassesrO   r   r   r   r     s    z$BarrierTests.test_barrier.<locals>.fN)r    )r   r¥   r   r   )r¥   rO   r   r   Útest_barrier  s    zBarrierTests.test_barrierc             C   s
   |   d¡S )zC
        Test that a barrier works for 10 consecutive runs
        r   )r¦   )r   r   r   r   Útest_barrier_10  s    zBarrierTests.test_barrier_10c                s:   g ‰ ‡ ‡fdd„}ˆ  |¡ ˆ tˆ ƒttˆjƒƒ¡ dS )z9
        test the return value from barrier.wait
        c                 s   ˆj  ¡ } ˆ  | ¡ d S )N)rž   rk   r   )rp   )rO   r   r   r   r   )  s    
z(BarrierTests.test_wait_return.<locals>.fN)r    r?   Úsumr   r@   )r   r   r   )rO   r   r   Útest_wait_return$  s    
zBarrierTests.test_wait_returnc                s<   g ‰‡fdd„}ˆ  ˆj|¡‰ ‡ ‡‡fdd„}ˆ |¡ dS )z,
        Test the 'action' callback
        c                  s   ˆ   d¡ d S )NT)r   r   )rO   r   r   Úaction5  s    z(BarrierTests.test_action.<locals>.actionc                  s   ˆ   ¡  ˆ tˆƒd¡ d S )Nr;   )rk   r?   r   r   )rž   rO   r   r   r   r   8  s    z#BarrierTests.test_action.<locals>.fN)rœ   r@   r    )r   rª   r   r   )rž   rO   r   r   Útest_action0  s
    zBarrierTests.test_actionc                sZ   g ‰ g ‰‡ ‡‡fdd„}ˆ  |¡ ˆ tˆ ƒd¡ ˆ tˆƒˆjd ¡ ˆ ˆjj¡ dS )zK
        Test that an abort will put the barrier in a broken state
        c                 sv   y4ˆj  ¡ } | ˆjd krt‚ˆj  ¡  ˆ  d¡ W n< tjk
rT   ˆ d¡ Y n tk
rp   ˆj  ¡  Y nX d S )NrY   T)rž   rk   r@   r_   r   r   ÚBrokenBarrierErrorrŸ   )r   )rl   rm   r   r   r   r   D  s    


z"BarrierTests.test_abort.<locals>.fr   r;   N)r    r?   r   r@   r8   rž   r¢   )r   r   r   )rl   rm   r   r   Ú
test_abort>  s    
zBarrierTests.test_abortc                sd   g ‰ g ‰g ‰‡ ‡‡‡fdd„}ˆ  |¡ ˆ tˆ ƒd¡ ˆ tˆƒˆjd ¡ ˆ tˆƒˆj¡ dS )zL
        Test that a 'reset' on a barrier frees the waiting threads
        c                 s˜   ˆj  ¡ } | ˆjd krFx ˆj jˆjd k r8t d¡ qW ˆj  ¡  n:yˆj  ¡  ˆ  d¡ W n  tj	k
r~   ˆ d¡ Y nX ˆj  ¡  ˆ d¡ d S )NrY   r;   gü©ñÒMbP?T)
rž   rk   r@   r¡   r   r   Úresetr   r   r¬   )r   )rl   rm   Úresults3r   r   r   r   ]  s    


z"BarrierTests.test_reset.<locals>.fr   r;   N)r    r?   r   r@   )r   r   r   )rl   rm   r¯   r   r   Ú
test_resetV  s    
zBarrierTests.test_resetc                sr   g ‰g ‰g ‰ˆ  ˆj¡‰ ‡ ‡‡‡‡fdd„}ˆ |¡ ˆ tˆƒd¡ ˆ tˆƒˆjd ¡ ˆ tˆƒˆj¡ dS )zF
        Test that a barrier can be reset after being broken.
        c                 s®   y4ˆj  ¡ } | ˆjd krt‚ˆj  ¡  ˆ d¡ W n< tjk
rT   ˆ d¡ Y n tk
rp   ˆj  ¡  Y nX ˆ  ¡ ˆjd krŽˆj  ¡  ˆ  ¡  ˆj  ¡  ˆ d¡ d S )NrY   T)	rž   rk   r@   r_   r   r   r¬   rŸ   r®   )r   )Úbarrier2rl   rm   r¯   r   r   r   r   |  s     




z,BarrierTests.test_abort_and_reset.<locals>.fr   r;   N)rœ   r@   r    r?   r   )r   r   r   )r±   rl   rm   r¯   r   r   Útest_abort_and_resett  s    
z!BarrierTests.test_abort_and_resetc                s   ‡ fdd„}ˆ   |¡ dS )z$
        Test wait(timeout)
        c                 s:   ˆ j  ¡ } | ˆ jd kr"t d¡ ˆ  tjˆ j jd¡ d S )NrY   g      ð?g      à?)rž   rk   r@   r   r   rE   r   r¬   )r   )r   r   r   r   š  s
    

z$BarrierTests.test_timeout.<locals>.fN)r    )r   r   r   )r   r   rR   –  s    zBarrierTests.test_timeoutc                s,   ˆj ˆjdd‰ ‡ ‡fdd„}ˆ |¡ dS )z4
        Test the barrier's default timeout
        g333333Ó?)rK   c                 s4   ˆ   ¡ } | ˆjd kr t d¡ ˆ tjˆ j ¡ d S )NrY   g      ð?)rk   r@   r   r   rE   r   r¬   )r   )rž   r   r   r   r   ª  s    
z,BarrierTests.test_default_timeout.<locals>.fN)rœ   r@   r    )r   r   r   )rž   r   r   Útest_default_timeout¤  s    z!BarrierTests.test_default_timeoutc             C   s   |   d¡}| ¡  | ¡  d S )Nr;   )rœ   rk   )r   rA   r   r   r   Útest_single_thread²  s    
zBarrierTests.test_single_threadN)r;   )r!   r"   r#   r$   r@   r   r'   r(   r    r¤   r¦   r§   r©   r«   r­   r°   r²   rR   r³   r´   r   r   r   r   r›   ÷  s"   
	"r›   )r$   r‹   r   Ú_threadr   r   r   ZunittestrS   Ztestr   r	   Úobjectr
   ZTestCaser%   r,   rW   r^   re   ru   r‡   r™   rš   r›   r   r   r   r   Ú<module>   s*   1 1C^ 0~