B
    u9aq  ã               @   s  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 )é    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               @   s.   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
S )ÚBunchFc                s|   ˆ ˆ_ |ˆ_g ˆ_g ˆ_| ˆ_t ¡ ˆ_ˆj ¡  ‡ ‡fdd„}y xt	|ƒD ]}t
|dƒ qNW W n   dˆ_‚ Y nX d S )Nc           	      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   T)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__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               @   st   e 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 )ÚBaseLockTestsc             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,   Úacquirer/   r0   )r   r-   r   r   r   Útest_locked_reprc   s    zBaseLockTests.test_locked_reprc             C   s   |   ¡ }| ¡  ~d S )N)r,   r2   )r   r-   r   r   r   Útest_acquire_destroyi   s    z"BaseLockTests.test_acquire_destroyc             C   s   |   ¡ }| ¡  | ¡  ~d S )N)r,   r2   Úrelease)r   r-   r   r   r   Útest_acquire_releasen   s    z"BaseLockTests.test_acquire_releasec             C   s$   |   ¡ }|  | d¡¡ | ¡  d S )NF)r,   Ú
assertTruer2   r5   )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   r2   r   )r-   Úresultr   r   r   }   s    z3BaseLockTests.test_try_acquire_contended.<locals>.fé   r   )r,   r2   r
   r   ÚassertFalser5   )r   r   r   )r-   r9   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)r2   r5   r   )r-   r   r   r   ‡   s    z/BaseLockTests.test_acquire_contended.<locals>.fr   )
r,   r2   r
   r   r	   ÚassertEqualr   r   r5   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)r2   r5   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   rC   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)r2   r5   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      à?)rJ   )r   Ú	monotonicr   r2   )Út1Út2)r-   Úresultsr   r   r   Ç   s    z%BaseLockTests.test_timeout.<locals>.fg      à?)r,   rD   Ú
ValueErrorr2   ÚOverflowErrorr   r5   r   rK   r7   r(   r
   r   r;   r*   )r   rL   rM   r   r   )r-   rN   r   Útest_timeout¶   s     zBaseLockTests.test_timeoutc             C   s"   |   ¡ }t |¡}|  |ƒ ¡ d S )N)r,   ÚweakrefÚrefZassertIsNotNone)r   r-   rS   r   r   r   Útest_weakref_existsÐ   s    
z!BaseLockTests.test_weakref_existsc             C   s$   |   ¡ }t |¡}~|  |ƒ ¡ d S )N)r,   rR   rS   ZassertIsNone)r   r-   rS   r   r   r   Útest_weakref_deletedÕ   s    
z"BaseLockTests.test_weakref_deletedN)r!   r"   r#   r.   r1   r3   r4   r6   r8   r<   rA   rF   rI   rQ   rT   rU   r   r   r   r   r+   U   s   
r+   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )Ú	LockTestsc          	      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)r2   r   r   )r-   Úphaser   r   r   æ   s    
z#LockTests.test_reacquire.<locals>.fr   r   r:   é   )r,   r   r   r   r   r	   r>   r5   )r   r   r   )r-   rW   r   Útest_reacquireá   s    



zLockTests.test_reacquirec                sB   |   ¡ ‰ ˆ  ¡  ‡ fdd„}t|dƒ}| ¡  ˆ  ¡  ˆ  ¡  d S )Nc                  s   ˆ   ¡  d S )N)r5   r   )r-   r   r   r   û   s    z*LockTests.test_different_thread.<locals>.fr:   )r,   r2   r
   r   r5   )r   r   r@   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„?)rJ   F)Zblocking)r,   r2   r;   r5   Úlockedr7   )r   r-   r   r   r   Útest_state_after_timeout  s    z"LockTests.test_state_after_timeoutN)r!   r"   r#   rY   rZ   r\   r   r   r   r   rV   Ü   s   rV   c               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )Ú
RLockTestsc             C   s<   |   ¡ }| ¡  | ¡  | ¡  | ¡  | ¡  | ¡  d S )N)r,   r2   r5   )r   r-   r   r   r   rY     s    zRLockTests.test_reacquirec             C   sX   |   ¡ }|  t|j¡ | ¡  | ¡  | ¡  | ¡  | ¡  | ¡  |  t|j¡ d S )N)r,   rD   ÚRuntimeErrorr5   r2   )r   r-   r   r   r   Útest_release_unacquired  s    z"RLockTests.test_release_unacquiredc             C   sX   |   ¡ }|  t|j¡ | ¡  | ¡  | ¡  | ¡  | ¡  | ¡  |  t|j¡ d S )N)r,   rD   r^   Ú_release_saver2   r5   )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)r2   r   )r-   r   r   r   5  s    z+RLockTests.test_different_thread.<locals>.fr:   T)r,   r
   rD   r^   r5   r    r   )r   r   r@   r   )r-   r   rZ   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-   r9   r   r   r   F  s    z$RLockTests.test__is_owned.<locals>.fr:   r   )r,   r;   rb   r2   r7   r
   r   r5   )r   r   r   )r-   r9   r   Útest__is_owned>  s    zRLockTests.test__is_ownedN)r!   r"   r#   rY   r_   ra   rZ   rc   r   r   r   r   r]     s
   	r]   c               @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )Ú
EventTestsc             C   sr   |   ¡ }|  | ¡ ¡ | ¡  |  | ¡ ¡ | ¡  |  | ¡ ¡ | ¡  |  | ¡ ¡ | ¡  |  | ¡ ¡ d S )N)Ú	eventtyper;   Zis_setÚsetr7   Ú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   )rh   Úresults1Úresults2r   r   r   f  s    z#EventTests._check_notify.<locals>.fr   T)r
   r   r	   r>   r   rf   r   )r   rh   r?   r   r@   r   )rh   rk   rl   r   Ú_check_notifya  s    
zEventTests._check_notifyc             C   s0   |   ¡ }|  |¡ | ¡  | ¡  |  |¡ d S )N)re   rm   rf   rg   )r   rh   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   rj   r   rK   )rL   ÚrrM   )rh   rk   rl   r   r   r     s
    
z"EventTests.test_timeout.<locals>.fFg      à?T)re   r
   r   r>   r;   r*   rf   r7   )r   r?   r   ro   Údtr   )rh   rk   rl   r   rQ   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   rj   r   )rh   rN   rJ   r   r   r   š  s    z(EventTests.test_set_and_clear.<locals>.fT)	re   r
   r   r   r   rf   rg   r   r>   )r   r?   r   r@   r   )rh   rN   rJ   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)re   Z_condr;   r2   Z_reset_internal_locks)r   rh   r   r   r   Útest_reset_internal_locks¤  s    z$EventTests.test_reset_internal_locksN)	r!   r"   r#   ri   rm   rn   rQ   rr   rs   r   r   r   r   rd   P  s   rd   c               @   sL   e 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 )ÚConditionTestsc          	   C   s¦   |   ¡ }| ¡  | ¡  | ¡  | ¡  t ¡ }|   |¡}| ¡  |  | d¡¡ | ¡  |  | d¡¡ |  | d¡¡ | ¡  | |  | d¡¡ W d Q R X d S )NF)Úcondtyper2   r5   r   ZLockr;   r7   )r   Úcondr-   r   r   r   Útest_acquire³  s    
zConditionTests.test_acquirec             C   s   |   ¡ }|  t|j¡ d S )N)ru   rD   r^   rj   )r   rv   r   r   r   Útest_unacquired_waitÆ  s    z#ConditionTests.test_unacquired_waitc             C   s   |   ¡ }|  t|j¡ d S )N)ru   rD   r^   Únotify)r   rv   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)r2   r   rj   r5   )r9   )rv   Ú	phase_numÚreadyrk   rl   r   r   r   ß  s    

z'ConditionTests._check_notify.<locals>.fé   r:   )Tr:   rX   é   )TrX   )Tr}   )r
   r   r   r	   rg   r>   r2   ry   r5   Z
notify_allr   )r   rv   r?   r   r@   r   )rv   r{   r|   rk   rl   r   rm   Î  sX    






zConditionTests._check_notifyc             C   s    |   ¡ }|  |¡ |  |¡ d S )N)ru   rm   )r   rv   r   r   r   rn     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      à?)r2   r   rK   rj   r5   r   )rL   r9   rM   )rv   rN   r   r   r   "  s    
z&ConditionTests.test_timeout.<locals>.fg      à?)ru   r
   r   r>   r   r*   r;   )r   r?   r   rp   r9   r   )rv   rN   r   rQ     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 )Nrq   r   r   )Ústater   r   Ú<lambda>9  ó    z8ConditionTests.test_waitfor.<locals>.f.<locals>.<lambda>rq   )Úwait_forr7   r>   )r9   )rv   r   r   r   r   r   7  s    
z&ConditionTests.test_waitfor.<locals>.fr:   rq   g{®Gáz„?)ru   r
   r   r   r   r   ry   r   )r   r   r@   r   r   )rv   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 )Nrq   r   r   )r   r   r   r€   L  r   z@ConditionTests.test_waitfor_timeout.<locals>.f.<locals>.<lambda>gš™™™™™¹?)rJ   )r   rK   r‚   r;   r*   r   )rp   r9   )rv   r   r   Úsuccessr   r   r   I  s    
z.ConditionTests.test_waitfor_timeout.<locals>.fr:   r}   g{®Gáz„?)
ru   r
   r   r   r   r   ry   r   r>   r   )r   r   r@   r   r   )rv   r   r   r„   r   Útest_waitfor_timeoutE  s    

z#ConditionTests.test_waitfor_timeoutN)r!   r"   r#   rw   rx   rz   rm   rn   rQ   rƒ   r…   r   r   r   r   rt   ®  s   Jrt   c               @   sT   e 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 )ÚBaseSemaphoreTestsc             C   s,   | j t| jdd | j t| jtj d d S )Néÿÿÿÿ)Úvalue)rD   rO   ÚsemtypeÚsysÚmaxsize)r   r   r   r   r.   b  s    z#BaseSemaphoreTests.test_constructorc             C   sH   |   d¡}| ¡  | ¡  |   d¡}| ¡  | ¡  | ¡  | ¡  d S )Nr:   rX   )r‰   r2   r5   )r   Úsemr   r   r   rw   f  s    

zBaseSemaphoreTests.test_acquirec             C   s   |   ¡ }| ¡  ~d S )N)r‰   r2   )r   rŒ   r   r   r   r4   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   r2   r   )r{   rk   rl   rŒ   Úsem_resultsr   r   r   }  s    
z4BaseSemaphoreTests.test_acquire_contended.<locals>.fé   r:   é   rX   é   FTé   )r‰   r2   r
   r   r   r	   r>   r   r5   Úsortedr;   r   )r   r?   r   r@   r   r   )r{   rk   rl   rŒ   r   r   rA   u  s:    



$.z)BaseSemaphoreTests.test_acquire_contendedc             C   sV   |   d¡}|  | d¡¡ |  | d¡¡ |  | d¡¡ | ¡  |  | d¡¡ d S )NrX   F)r‰   r7   r2   r;   r5   )r   rŒ   r   r   r   r8   š  s    
z#BaseSemaphoreTests.test_try_acquirec                sV   |   d¡‰ˆ ¡  g ‰ ‡ ‡fdd„}t|dƒ ¡  |  tˆ ƒdgd dgd  ¡ d S )	Nrq   c                  s$   ˆ   ˆ d¡¡ ˆ   ˆ d¡¡ d S )NF)r   r2   r   )rN   rŒ   r   r   r   ¦  s    z8BaseSemaphoreTests.test_try_acquire_contended.<locals>.fr=   Fr   Tr}   )r‰   r2   r
   r   r>   r”   )r   r   r   )rN   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 )NrX   Fg      ð?)rJ   g{®Gázt?g      à?)
r‰   rD   rO   r2   r7   r;   r5   r   rK   r*   )r   rŒ   Útrp   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)r2   r5   r   )rŒ   r   r   r   À  s    z0BaseSemaphoreTests.test_default_value.<locals>.fr:   )	r‰   r2   r
   r   r	   r;   r   r5   r   )r   r   r@   r   )rŒ   r   Útest_default_value¼  s    
z%BaseSemaphoreTests.test_default_valuec                sb   ˆ   d¡‰d‡ ‡fdd„	}|ƒ  ˆ  ˆ d¡¡ ˆ ¡  ˆ  t|t¡ ˆ  ˆ d¡¡ ˆ ¡  d S )NrX   c          
      sT   ˆF ˆ   ˆ d¡¡ ˆ ¡  ˆ ˆ  ˆ d¡¡ | r<| ‚W d Q R X W d Q R X d S )NF)r7   r2   r5   r;   )rB   )r   rŒ   r   r   rC   Ì  s    z+BaseSemaphoreTests.test_with.<locals>._withF)N)r‰   r7   r2   r5   rD   rE   )r   rC   r   )r   rŒ   r   rF   Ê  s    
zBaseSemaphoreTests.test_withN)r!   r"   r#   r.   rw   r4   rA   r8   r<   r–   r—   rF   r   r   r   r   r†   ]  s   
%r†   c               @   s   e Zd Zdd„ ZdS )ÚSemaphoreTestsc             C   s.   |   d¡}| ¡  | ¡  | ¡  | ¡  d S )Nr:   )r‰   r5   r2   )r   rŒ   r   r   r   r_   à  s
    
z&SemaphoreTests.test_release_unacquiredN)r!   r"   r#   r_   r   r   r   r   r˜   Û  s   r˜   c               @   s   e Zd Zdd„ ZdS )ÚBoundedSemaphoreTestsc             C   s8   |   ¡ }|  t|j¡ | ¡  | ¡  |  t|j¡ d S )N)r‰   rD   rO   r5   r2   )r   rŒ   r   r   r   r_   î  s
    z-BoundedSemaphoreTests.test_release_unacquiredN)r!   r"   r#   r_   r   r   r   r   r™   é  s   r™   c               @   s†   e 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 )"ÚBarrierTestsr=   g       @c             C   s   | j | j| jd| _d S )N)rJ   )Ú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   r@   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   rj   Ú	n_waitingr;   Úbroken)r   rN   r   Úmr   r   r   r   Ú	multipass  s    
zBarrierTests.multipassr:   c                s&   g g g‰‡ ‡‡fdd„}ˆ  |¡ d S )Nc                  s   ˆ  ˆˆ ¡ d S )N)r£   r   )ÚpassesrN   r   r   r   r     s    z$BarrierTests.test_barrier.<locals>.f)rŸ   )r   r¤   r   r   )r¤   rN   r   r   Útest_barrier  s    zBarrierTests.test_barrierc             C   s
   |   d¡S )NrŽ   )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 )Nc                 s   ˆj  ¡ } ˆ  | ¡ d S )N)r   rj   r   )ro   )rN   r   r   r   r   )  s    
z(BarrierTests.test_wait_return.<locals>.f)rŸ   r>   Úsumr   r?   )r   r   r   )rN   r   r   Útest_wait_return$  s    
zBarrierTests.test_wait_returnc                s<   g ‰‡fdd„}ˆ  ˆj|¡‰ ‡ ‡‡fdd„}ˆ |¡ d S )Nc                  s   ˆ   d¡ d S )NT)r   r   )rN   r   r   Úaction5  s    z(BarrierTests.test_action.<locals>.actionc                  s   ˆ   ¡  ˆ tˆƒd¡ d S )Nr:   )rj   r>   r   r   )r   rN   r   r   r   r   8  s    z#BarrierTests.test_action.<locals>.f)r›   r?   rŸ   )r   r©   r   r   )r   rN   r   r   Útest_action0  s
    zBarrierTests.test_actionc                sZ   g ‰ g ‰‡ ‡‡fdd„}ˆ  |¡ ˆ tˆ ƒd¡ ˆ tˆƒˆjd ¡ ˆ ˆjj¡ d S )Nc                 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 )NrX   T)r   rj   r?   r^   r   r   ÚBrokenBarrierErrorrž   )r   )rk   rl   r   r   r   r   D  s    


z"BarrierTests.test_abort.<locals>.fr   r:   )rŸ   r>   r   r?   r7   r   r¡   )r   r   r   )rk   rl   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 )Nc                 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 )NrX   r:   gü©ñÒMbP?T)
r   rj   r?   r    r   r   Úresetr   r   r«   )r   )rk   rl   Úresults3r   r   r   r   ]  s    


z"BarrierTests.test_reset.<locals>.fr   r:   )rŸ   r>   r   r?   )r   r   r   )rk   rl   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 )Nc                 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 )NrX   T)	r   rj   r?   r^   r   r   r«   rž   r­   )r   )Úbarrier2rk   rl   r®   r   r   r   r   |  s     




z,BarrierTests.test_abort_and_reset.<locals>.fr   r:   )r›   r?   rŸ   r>   r   )r   r   r   )r°   rk   rl   r®   r   r   Útest_abort_and_resett  s    
z!BarrierTests.test_abort_and_resetc                s   ‡ fdd„}ˆ   |¡ d S )Nc                 s:   ˆ j  ¡ } | ˆ jd kr"t d¡ ˆ  tjˆ j jd¡ d S )NrX   g      ð?g      à?)r   rj   r?   r   r   rD   r   r«   )r   )r   r   r   r   š  s
    

z$BarrierTests.test_timeout.<locals>.f)rŸ   )r   r   r   )r   r   rQ   –  s    zBarrierTests.test_timeoutc                s,   ˆj ˆjdd‰ ‡ ‡fdd„}ˆ |¡ d S )Ng333333Ó?)rJ   c                 s4   ˆ   ¡ } | ˆjd kr t d¡ ˆ tjˆ j ¡ d S )NrX   g      ð?)rj   r?   r   r   rD   r   r«   )r   )r   r   r   r   r   ª  s    
z,BarrierTests.test_default_timeout.<locals>.f)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›   rj   )r   r@   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±   rQ   r²   r³   r   r   r   r   rš   ÷  s    
	"rš   )rŠ   r   Ú_threadr   r   r   ZunittestrR   Ztestr   r	   Úobjectr
   ZTestCaser$   r+   rV   r]   rd   rt   r†   r˜   r™   rš   r   r   r   r   Ú<module>   s(   1 1C^ 0~