a
    	3jp                     @   s   d Z ddlZddlmZ ddlZddlZdZeeZdd ZG dd dej	Z
G d	d
 d
ej	ZG dd dej	ZG dd dej	Zedkre  dS )zTests for lock.py    N)mockzv^<(?P<class>.*?) object at (?P<address>.*?)\[(?P<extras>(set|unset|locked|unlocked)(, value:\d)?(, waiters:\d+)?)\]>\Zc                   C   s   t d  d S N)asyncioZset_event_loop_policy r   r   X/www/server/python_manager/versions/3.9.10/lib/python3.9/test/test_asyncio/test_locks.pytearDownModule   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 )	LockTestsc                    s   t  }| t tj|d}W d    n1 s40    Y  | |j| | t  tjt d}W d    n1 s|0    Y  | |jt  d S Nloop)	r   MockassertWarnsDeprecationWarningr   LockassertIs_loopget_running_loop)selfr   lockr   r   r   test_ctor_loop   s    *.zLockTests.test_ctor_loopc                    s   t  }| |jt   d S r   )r   r   r   r   r   r   r   r   r   r   test_ctor_noloop"   s    zLockTests.test_ctor_noloopc                    s   |  t  tjt d}W d    n1 s00    Y  | t|d | t	t| |
 I d H  | t|d | t	t| d S )Nr
   z[unlocked]>	[locked]>)r   r   r   r   r   
assertTruereprendswithRGX_REPRmatchacquirer   r   r   r   	test_repr&   s    .zLockTests.test_reprc                    sP   t  }| td |I d H  W d    n1 s40    Y  | |  d S )Nz/object Lock can't be used in 'await' expression)r   r   assertRaisesRegex	TypeErrorassertFalselockedr   r   r   r   	test_lock0   s    (zLockTests.test_lockc              
      s   t  t  t  t  g}|D ]}t dI d H  | |  | t	d: |I d H  W d    n1 sp0    Y  W d    n1 s0    Y  | |  q d S )Ng{Gz?z.object \w+ can't be used in 'await' expression)
r   r   	Condition	SemaphoreBoundedSemaphoresleepr"   r#   r    r!   )r   Z
primitivesr   r   r   r   test_lock_by_with_statement;   s    <z%LockTests.test_lock_by_with_statementc                    sv  t   g }|   I d H   fdd} fdd} fdd}t ||}t ||}t dI d H  | g |    t dI d H  | dg| t dI d H  | dg| t ||}   t dI d H  | dd	g|    t dI d H  | g d
| | |  | |	  | |  | |	  | |  | |	  d S )Nc                    s      I d H r| d dS N   Tr   appendresultr   r   r   c1T   s    
z"LockTests.test_acquire.<locals>.c1c                    s      I d H r| d dS N   Tr,   r.   r0   r   r   c2Y   s    
z"LockTests.test_acquire.<locals>.c2c                    s      I d H r| d dS N   Tr,   r.   r0   r   r   c3^   s    
z"LockTests.test_acquire.<locals>.c3r   r+   r3   r+   r3   r6   )
r   r   r   r   create_taskr(   assertEqualreleasedoner/   r   r/   r1   r4   r7   t1t2t3r   r0   r   test_acquireN   s8    zLockTests.test_acquirec                    s   |  t  tjt d}W d    n1 s00    Y  | | I d H  t| }t |j	 | 
tj |I d H  W d    n1 s0    Y  | |j d S r	   )r   r   r   r   r   r   r   r9   	call_sooncancelassertRaisesCancelledErrorr"   _waiters)r   r   Ztaskr   r   r   test_acquire_cancel   s    .(zLockTests.test_acquire_cancelc                    sR  |  t  tjt d W d    n1 s00    Y   fdd}t  }t|d|}tdI d H  |  	  t|dd }tdI d H  | 
t jd t|dd }tdI d H  | 
t jd	 |d  |  |  jd   tdI d H  |  	  | |  | |  |I d H  d S )
Nr
   c                    s<      I d H  z|d ur"|I d H  W    n
   0 d S r   )r   r;   )nameZblockerr0   r   r   lockit   s
    z*LockTests.test_cancel_race.<locals>.lockitAr   Br+   Cr3   )r   r   r   r   r   Zcreate_futurer9   r(   r   r#   r:   lenrF   
set_resultrC   	cancelledr"   r<   )r   rI   fatatbtcr   r0   r   test_cancel_race   s*    .
zLockTests.test_cancel_racec                    s@  t  }| t t j|dW d    n1 s40    Y  dd  fdd}fdd} I d H  t | t | }t | }t dI d H  || | 	t j
 I d H  W d    n1 s0    Y  | d |  d |  o|  |  t dI d H  | |  d S )	Nr
   r   c                      s"    d7    I d H  d7 d S Nr+   )r   r   )
call_countr   
lock_countr   r   rI      s    z2LockTests.test_cancel_release_race.<locals>.lockitc                      s         d S r   )rC   r;   r   )r   r>   r   r   trigger   s    z3LockTests.test_cancel_release_race.<locals>.triggerr+   r6   )r   r   r   r   r   r   r9   r(   rB   rD   rE   r:   r   rO   r<   rC   )r   r   rI   rX   r?   r@   r   )rV   r   rW   r>   r   test_cancel_release_race   s*    *
(z"LockTests.test_cancel_release_racec                    s   |  t  tjt d}W d    n1 s00    Y  | I d H  | |  t| }t	dI d H  | 
t|jd t| }|  |  t	dI d H  | |  | |  |I d H  d S )Nr
   r   r+   )r   r   r   r   r   r   r   r#   r9   r(   r:   rM   rF   rC   r;   rO   )r   r   rR   rS   r   r   r   test_finished_waiter_cancelled   s    .z(LockTests.test_finished_waiter_cancelledc                    sL   |  t  tjt d}W d    n1 s00    Y  | t|j d S r	   )r   r   r   r   r   rD   RuntimeErrorr;   r   r   r   r   test_release_not_acquired  s    .z#LockTests.test_release_not_acquiredc                    sp   |  t  tjt d}W d    n1 s00    Y  | I d H  | |  |  | 	|  d S r	   )
r   r   r   r   r   r   r   r#   r;   r"   r   r   r   r   test_release_no_waiters  s    .z!LockTests.test_release_no_waitersc              	      sn   t  }| |  |4 I d H $ | |  W d   I d H  q\1 I d H sR0    Y  | |  d S r   )r   r   r"   r#   r   r   r   r   r   test_context_manager  s
    8zLockTests.test_context_managerN)__name__
__module____qualname__r   r   r   r$   r)   rA   rG   rT   rY   rZ   r\   r]   r^   r   r   r   r   r      s   

30.	r   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 )
EventTestsc                    s   t  }| t tj|d}W d    n1 s40    Y  | |j| | t  tjt d}W d    n1 s|0    Y  | |jt  d S r	   )	r   r   r   r   r   Eventr   r   r   )r   r   evr   r   r   r     s    *.zEventTests.test_ctor_loopc                    s,   t t   t  }| |jt   d S r   )r   Zset_event_loopr   rc   r   r   r   rd   r   r   r   r   '  s    zEventTests.test_ctor_noloopc                    s   |  t  tjt d}W d    n1 s00    Y  | t|d t	t|}| 
|dd |  | t|d | t	t| |jt  | dt|v  | t	t| d S )Nr
   z[unset]>extrasZunsetz[set]>	waiters:1)r   r   r   rc   r   r   r   r   r   r   r:   groupsetrF   r-   r   r   )r   rd   r   r   r   r   r   ,  s    .zEventTests.test_reprc                    s6  |  t  tjt d W d    n1 s00    Y  |    g } fdd} fdd} fdd}t||}t||}tdI d H  | 	g | t||} 
  tdI d H  | 	g d	| | |  | |  | |  | |  | |  | |  d S )
Nr
   c                    s      I d H r| d d S rU   waitr-   r.   rd   r   r   r1   B  s    z EventTests.test_wait.<locals>.c1c                    s      I d H r| d d S )Nr3   rj   r.   rl   r   r   r4   F  s    z EventTests.test_wait.<locals>.c2c                    s      I d H r| d d S )Nr6   rj   r.   rl   r   r   r7   J  s    z EventTests.test_wait.<locals>.c3r   )r6   r+   r3   )r   r   r   rc   r   r"   is_setr9   r(   r:   ri   r   r<   ZassertIsNoner/   r=   r   rl   r   	test_wait;  s*    .zEventTests.test_waitc                    s,   t  }|  | I d H }| | d S r   )r   rc   ri   rk   r   )r   rd   resr   r   r   test_wait_on_seta  s    zEventTests.test_wait_on_setc                    sl   t  }t | }t  |j | t j |I d H  W d    n1 sR0    Y  | 	|j
 d S r   )r   rc   r9   rk   r   rB   rC   rD   rE   r"   rF   )r   rd   rk   r   r   r   test_wait_cancelh  s    (zEventTests.test_wait_cancelc                    sF   t  }| |  |  | |  |  | |  d S r   )r   rc   r"   rm   ri   r   clearre   r   r   r   
test_clearq  s    zEventTests.test_clearc                    s   t   g } fdd}t ||}t dI d H  | g |       |          | dt	 j
 t dI d H  | dg| | dt	 j
 | |  | |  d S )Nc                    s      I d H r| d dS r*   rj   r.   rl   r   r   r1     s    
z.EventTests.test_clear_with_waiters.<locals>.c1r   r+   )r   rc   r9   r(   r:   ri   rr   r"   rm   rM   rF   r   r<   r/   r   r/   r1   tr   rl   r   test_clear_with_waiters{  s"    z"EventTests.test_clear_with_waitersN)r_   r`   ra   r   r   r   rn   rp   rq   rs   rv   r   r   r   r   rb     s   
&	
rb   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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 )&ConditionTestsc                    sr   t  }| tL tj|d}| |j| tjt d}| |jt  W d    n1 sd0    Y  d S r	   )	r   r   r   r   r   r%   r   r   r   )r   r   condr   r   r   r     s    zConditionTests.test_ctor_loopc                    s   t  }| |jt   d S r   )r   r%   r   r   r   r   rx   r   r   r   r     s    zConditionTests.test_ctor_noloopc                    s  t   g } fdd} fdd} fdd}t ||}t ||}t ||}t dI d H  | g | |    |   I d H   	  t dI d H  | g | |     
  t dI d H  | dg| |     	d	 t dI d H  | dg| |     
  t dI d H  | dd	g| |     
  t dI d H  | g d
| |    | |  | |  | |  | |  | |  | |  d S )Nc                    s*      I d H    I d H r&| d dS r*   r   rk   r-   r.   rx   r   r   r1     s    
z$ConditionTests.test_wait.<locals>.c1c                    s*      I d H    I d H r&| d dS r2   rz   r.   r{   r   r   r4     s    
z$ConditionTests.test_wait.<locals>.c2c                    s*      I d H    I d H r&| d dS r5   rz   r.   r{   r   r   r7     s    
z$ConditionTests.test_wait.<locals>.c3r   r+   r3   r8   )r   r%   r9   r(   r:   r"   r#   r   r   notifyr;   r<   r/   r=   r   r{   r   rn     sL    
zConditionTests.test_waitc                    s   t  }| I d H  t | }t  |j | t j	 |I d H  W d    n1 s`0    Y  | 
|j | |  d S r   )r   r%   r   r9   rk   r   rB   rC   rD   rE   r"   rF   r   r#   )r   rx   rk   r   r   r   rq     s    (zConditionTests.test_wait_cancelc                    s   t  }| I d H  | |  t | }t dI d H  | |  | I d H  | |  |	  t 
 |j t 
 |j z|I d H  W n t jy   Y n0 | |  d S )Nr   )r   r%   r   r   r#   r9   rk   r(   r"   r|   r   rB   rC   r;   rE   )r   rx   Z	wait_taskr   r   r   test_wait_cancel_contested  s     z)ConditionTests.test_wait_cancel_contestedc                    s   |  t  tjt d W d    n1 s00    Y  d fdd}t| }tdI d H    I d H     tdI d H  |	  tdI d H   
  tdI d H  | |  |  d S )Nr
   Fc                	      sN    4 I d H ( d   I d H  W d   I d H  qJ1 I d H s@0    Y  d S )NT)rk   r   rx   Zwaitedr   r   wait_on_cond  s    zBConditionTests.test_wait_cancel_after_notify.<locals>.wait_on_condr   )r   r   r   r%   r   r9   r(   r   r|   rC   r;   r   rO   )r   r   waiterr   r~   r   test_wait_cancel_after_notify
  s    .z,ConditionTests.test_wait_cancel_after_notifyc                    sD   t  }| t | I d H  W d    n1 s60    Y  d S r   )r   r%   rD   r[   rk   ry   r   r   r   test_wait_unacquired$  s    z#ConditionTests.test_wait_unacquiredc                    s   t   dfddg } fdd}t ||}t dI d H  | g |   I d H        t dI d H  | g | d  I d H        t dI d H  | dg| | |	  | |
  d S )	NFc                      s    S r   r   r   )presultr   r   	predicate-  s    z/ConditionTests.test_wait_for.<locals>.predicatec                    s4      I d H   I d H r0| d    dS r*   )r   wait_forr-   r;   r.   )rx   r   r   r   r1   2  s
    
z(ConditionTests.test_wait_for.<locals>.c1r   Tr+   r   r%   r9   r(   r:   r   r|   r;   r   r<   r/   rt   r   )rx   r   r   r   test_wait_for)  s*    zConditionTests.test_wait_forc                    sn   t  }|dd I d H }| g d| | t$ |dd I d H  W d    n1 s`0    Y  d S )Nc                   S   s   g dS )Nr8   r   r   r   r   r   <lambda>R      z9ConditionTests.test_wait_for_unacquired.<locals>.<lambda>r8   c                   S   s   dS )NFr   r   r   r   r   r   V  r   )r   r%   r   r:   rD   r[   )r   rx   ro   r   r   r   test_wait_for_unacquiredN  s
    z'ConditionTests.test_wait_for_unacquiredc                    sV  t   g } fdd} fdd} fdd}t ||}t ||}t ||}t dI d H  | g |   I d H   d    t dI d H  | dg|   I d H   d  d	    t dI d H  | g d
| | |	  | |
  | |	  | |
  | |	  | |
  d S )Nc                    s2      I d H    I d H r.| d    dS r*   r   rk   r-   r;   r.   r{   r   r   r1   \  s
    
z&ConditionTests.test_notify.<locals>.c1c                    s2      I d H    I d H r.| d    dS r2   r   r.   r{   r   r   r4   c  s
    
z&ConditionTests.test_notify.<locals>.c2c                    s2      I d H    I d H r.| d    dS r5   r   r.   r{   r   r   r7   j  s
    
z&ConditionTests.test_notify.<locals>.c3r   r+   i   r8   r   r=   r   r{   r   test_notifyX  s6    


zConditionTests.test_notifyc                    s   t   g } fdd} fdd}t ||}t ||}t dI d H  | g |   I d H        t dI d H  | ddg| | |	  | |
  | |	  | |
  d S )Nc                    s2      I d H    I d H r.| d    dS r*   r   r.   r{   r   r   r1     s
    
z*ConditionTests.test_notify_all.<locals>.c1c                    s2      I d H    I d H r.| d    dS r2   r   r.   r{   r   r   r4     s
    
z*ConditionTests.test_notify_all.<locals>.c2r   r+   r3   )r   r%   r9   r(   r:   r   
notify_allr;   r   r<   r/   )r   r/   r1   r4   r>   r?   r   r{   r   test_notify_all  s"    zConditionTests.test_notify_allc                 C   s   t  }| t|j d S r   )r   r%   rD   r[   r|   ry   r   r   r   test_notify_unacquired  s    z%ConditionTests.test_notify_unacquiredc                 C   s   t  }| t|j d S r   )r   r%   rD   r[   r   ry   r   r   r   test_notify_all_unacquired  s    z)ConditionTests.test_notify_all_unacquiredc                    s   t  }| dt|v  | tt| | I d H  | dt|v  |jt	
  | dt|v  | tt| |jt	
  | dt|v  | tt| d S )Nunlockedr#   rg   	waiters:2)r   r%   r   r   r   r   r   rF   r-   r   r   ry   r   r   r   r     s    zConditionTests.test_reprc              	      sn   t  }| |  |4 I d H $ | |  W d   I d H  q\1 I d H sR0    Y  | |  d S r   )r   r%   r"   r#   r   ry   r   r   r   r^     s
    8z#ConditionTests.test_context_managerc                    sN   d fdd	}| I d H  |t  I d H  t  }||t |I d H  d S )Nc              	      s4  | d u rt  } |d u r"t | } |j|   |    |  |4 I d H 2  |    |  W d   I d H  q1 I d H s0    Y   |    |  | 4 I d H 4  |    |  W d   I d H  q1 I d H s
0    Y   |    |  d S r   )r   r   r%   r   _lockr"   r#   r   )r   rx   r   r   r   f  s"    
8<z,ConditionTests.test_explicit_lock.<locals>.f)NN)r   r   r%   )r   r   r   r   r   r   test_explicit_lock  s
    z!ConditionTests.test_explicit_lockc                    sL   t    j  fdd} fdd}| I d H  | I d H  d S )Nc               	      s    t tj d W d    n1 s,0    Y  t }  | _| 4 I d H L td t|  W d    n1 s~0    Y  W d   I d H  q1 I d H s0    Y  d S )Nr
   "loop argument must agree with lock)r   r   r   r   r   r    
ValueErrorr%   r0   r   r   r   r   wrong_loop_in_lock  s    *z?ConditionTests.test_ambiguous_loops.<locals>.wrong_loop_in_lockc               
      s   t  } | 4 I d H z tdH t t j|  d W d    n1 sR0    Y  W d    n1 sp0    Y  W d   I d H  q1 I d H s0    Y  d S )Nr   r
   )r   r   r    r   r   r   r%   r0   r   r   r   wrong_loop_in_cond  s    z?ConditionTests.test_ambiguous_loops.<locals>.wrong_loop_in_cond)r   Znew_event_loopZ
addCleanupclose)r   r   r   r   r   r   test_ambiguous_loops  s    z#ConditionTests.test_ambiguous_loopsc              	      s   t  }|4 I d H Z | t j( t j| ddI d H  W d    n1 sP0    Y  W d   I d H  q1 I d H sz0    Y  d S )Ng      ?)timeout)r   r%   rD   TimeoutErrorr   rk   )r   Z	conditionr   r   r   test_timeout_in_block  s    z$ConditionTests.test_timeout_in_blockN)r_   r`   ra   r   r   rn   rq   r}   r   r   r   r   r   r   r   r   r   r^   r   r   r   r   r   r   r   rw     s$   	?%
4$"rw   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d Zdd Zdd Zdd ZdS )SemaphoreTestsc                    s   t  }| t tj|d}W d    n1 s40    Y  | |j| | t  tjt d}W d    n1 s|0    Y  | |jt  d S r	   )	r   r   r   r   r   r&   r   r   r   )r   r   semr   r   r   r     s    *.zSemaphoreTests.test_ctor_loopc                    s   t  }| |jt   d S r   )r   r&   r   r   r   r   r   r   r   r   r     s    zSemaphoreTests.test_ctor_noloopc                    sN   |  t" tjdt d}W d    n1 s20    Y  | |  d S )Nr   r
   )r   r   r   r&   r   r   r#   r   r   r   r   test_initial_value_zero"  s    0z&SemaphoreTests.test_initial_value_zeroc                    s   t  }| t|d | tt| | I d H  | t|d | dt|v | tt| |j	t
  | dt|v  | tt| |j	t
  | dt|v  | tt| d S )Nz[unlocked, value:1]>r   waitersrg   r   )r   r&   r   r   r   r   r   r   rF   r-   r   r   r   r   r   r   r   '  s    zSemaphoreTests.test_reprc                    sl   t  }| d|j | td |I d H  W d    n1 sB0    Y  | |  | d|j d S )Nr+   z4object Semaphore can't be used in 'await' expression)r   r&   r:   _valuer    r!   r"   r#   r   r   r   r   test_semaphore9  s    (zSemaphoreTests.test_semaphorec                 C   s   |  ttjd d S )N)rD   r   r   r&   r   r   r   r   test_semaphore_valueF  s    z#SemaphoreTests.test_semaphore_valuec                    s  t d g }|   I d H  |   I d H  |     fdd} fdd} fdd} fdd	}t ||}t ||}t ||}t d
I d H  | dg| |    | dt	 j
 | d
 j t ||}	      | d j t d
I d H  | d
 j | dt	| |    | dt	 j
 | d
 j | |  | |  |||	g}
dd |
D }| dt	|    t j|
 I d H  d S )Nr6   c                    s      I d H  | d dS r*   r,   r.   r   r   r   r1   Q  s    
z'SemaphoreTests.test_acquire.<locals>.c1c                    s      I d H  | d dS r2   r,   r.   r   r   r   r4   V  s    
z'SemaphoreTests.test_acquire.<locals>.c2c                    s      I d H  | d dS r5   r,   r.   r   r   r   r7   [  s    
z'SemaphoreTests.test_acquire.<locals>.c3c                    s      I d H  | d dS )N   Tr,   r.   r   r   r   c4`  s    
z'SemaphoreTests.test_acquire.<locals>.c4r   r+   r3   c                 S   s    g | ]}|  r| r|qS r   )r<   r/   .0ru   r   r   r   
<listcomp>  r   z/SemaphoreTests.test_acquire.<locals>.<listcomp>)r   r&   r   r   r"   r#   r9   r(   r:   rM   rF   r   r;   r<   r/   Zgather)r   r/   r1   r4   r7   r   r>   r?   r@   t4Z
race_tasksZ
done_tasksr   r   r   rA   I  sD    

zSemaphoreTests.test_acquirec                    s   t  }| I d H  t | }t  |j | t j |I d H  W d    n1 s`0    Y  | 	|j
 ptdd |j
D  d S )Nc                 s   s   | ]}|  V  qd S r   r<   )r   r   r   r   r   	<genexpr>  r   z5SemaphoreTests.test_acquire_cancel.<locals>.<genexpr>)r   r&   r   r9   r   rB   rC   rD   rE   r   rF   all)r   r   r   r   r   r   rG     s    (z"SemaphoreTests.test_acquire_cancelc                    s   t jdd}t | }t | }t | }t | }t dI d H  |  |  |  t dI d H  tdd ||fD }| |d | 	|
  | |
  |  |  t dI d H  d S )Nr   valuec                 s   s   | ]}|  V  qd S r   r   r   r   r   r   r     r   zCSemaphoreTests.test_acquire_cancel_before_awoken.<locals>.<genexpr>r+   )r   r&   r9   r   r(   rC   r;   sumr:   r   r<   r"   )r   r   r>   r?   r@   r   Znum_doner   r   r   !test_acquire_cancel_before_awoken  s"    z0SemaphoreTests.test_acquire_cancel_before_awokenc                    sx   t jdd}t | }t | }t dI d H  |  |  t dI d H  | |  | |	  d S )Nr   r   )
r   r&   r9   r   r(   rC   r;   r   r#   r<   )r   r   r>   r?   r   r   r   test_acquire_hang  s    z SemaphoreTests.test_acquire_hangc                    sL   |  t  tjt d}W d    n1 s00    Y  | t|j d S r	   )r   r   r   r'   r   rD   r   r;   r   r   r   r   r\     s    .z(SemaphoreTests.test_release_not_acquiredc                    s>   t  }| I d H  | |  |  | |  d S r   )r   r&   r   r   r#   r;   r"   r   r   r   r   r]     s
    z&SemaphoreTests.test_release_no_waitersN)r_   r`   ra   r   r   r   r   r   r   rA   rG   r   r   r\   r]   r   r   r   r   r     s   
=r   __main__)__doc__Zunittestr   rer   ZSTR_RGX_REPRcompiler   r   ZIsolatedAsyncioTestCaser   rb   rw   r   r_   mainr   r   r   r   <module>   s&   
  }  | 4