o
    )ZhrB                     @   s   d dl Z d dlmZ d dlZd dlZd dlmZmZ d dlm	Z	 d dl
mZm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edkrbe  dS dS )    N)	timedelta)genlocks)TimeoutError)gen_testAsyncTestCasec                       s   e Zd Z fddZdd Zdd Zdd Zed	d
 Zdd Z	dd Z
dd Zedd Zedd Zedd Zedd Zedd Zedd Z  ZS )ConditionTestc                    s   t    g | _d S N)supersetUphistoryself	__class__ N/var/www/html/lang_env/lib/python3.10/site-packages/tornado/test/locks_test.pyr      s   

zConditionTest.setUpc                    s    fdd}  | dS )z=Record the resolution of a Future returned by Condition.wait.c                    s(      sjd d S j d S )Ntimeout)resultr   append_futurekeyr   r   r   callback   s   z+ConditionTest.record_done.<locals>.callbackN)add_done_callback)r   r   r   r   r   r   r   record_done   s   zConditionTest.record_donec                 C   s   | j | j |   dS )zRun all queued callbacks on the IOLoop.

        In these tests, this method is used after calling notify() to
        preserve the pre-5.0 behavior in which callbacks ran
        synchronously.
        N)io_loopZadd_callbackstopwaitr   r   r   r   loop_briefly(   s   zConditionTest.loop_brieflyc                 C   sD   t  }| dt| | dt| |  | dt| d S )N	Conditionwaiters)r   r"   assertInreprassertNotInr    r   cr   r   r   	test_repr2   s
   zConditionTest.test_reprc                 c   s(    t  }| jd|j | V  d S )N{Gz?)r   r"   r   
call_laternotifyr    r'   r   r   r   test_notify9   s   zConditionTest.test_notifyc                 C   sz   t  }| | d | | d |d |   | jd |d |   | jd | g d| j d S )Nwait1wait2   notify1notify2)r.   r1   r/   r2   )	r   r"   r   r    r,   r!   r   r   assertEqualr'   r   r   r   test_notify_1?   s   

zConditionTest.test_notify_1c                 C   s   t  }tdD ]
}| | | q|d |   | ttd| j	 |d |   | ttd| j	 |d |   | ttd| j	 d S )N      r0         )
r   r"   ranger   r    r,   r!   r3   listr   r   r(   ir   r   r   test_notify_nK   s   


zConditionTest.test_notify_nc                 C   sb   t  }tdD ]
}| | | q|  |   | jd | 	t
tddg | j d S )Nr7   
notify_all)r   r"   r9   r   r    r>   r!   r   r   r3   r:   r;   r   r   r   test_notify_all\   s    zConditionTest.test_notify_allc                 c   sF    t  }|tdd}| jd|j tdV  | 	|V  d S )Nr*   seconds{Gz?Q?)
r   r"   r    r   r   r+   r,   r   sleepassertFalser   r(   r    r   r   r   test_wait_timeouth   s   zConditionTest.test_wait_timeoutc                 c   s@    t  }| jd|j |tdd}tdV  |V  d S )Nr*   rB   r@   rC   )	r   r"   r   r+   r,   r    r   r   rD   rF   r   r   r   test_wait_timeout_preemptedp   s   
z)ConditionTest.test_wait_timeout_preemptedc                 c   s    t  }| | d | |tddd | | d | | d tdV  | dg| j |	d tdV  | g d	| j | g d	| j |	  d V  | g d
| j d S )Nr   r*   r@   r0   r8   r6   rB   r   r   r   r8   )r   r   r8   r6   )
r   r"   r   r    r   r   rD   r3   r   r,   r'   r   r   r   test_notify_n_with_timeoutz   s   
z(ConditionTest.test_notify_n_with_timeoutc                 c   s    t  }| | d | |tddd | | d tdV  | dg| j |	  d V  | g d| j d S )	Nr   r*   r@   r0   r8   rB   r   rI   )
r   r"   r   r    r   r   rD   r3   r   r>   r'   r   r   r   test_notify_all_with_timeout   s   z*ConditionTest.test_notify_all_with_timeoutc                 #   sb    t    fddtdD }|d  fdd  d d V  | tdd	 |D  d S )
Nc                    s   g | ]	}t   qS r   )asyncioensure_futurer    .0r   r(   r   r   
<listcomp>   s    z4ConditionTest.test_nested_notify.<locals>.<listcomp>r6   r0   c                    s      S r	   )r,   r   rP   r   r   <lambda>   s    z2ConditionTest.test_nested_notify.<locals>.<lambda>r8   c                 s       | ]}|  V  qd S r	   done)rO   fr   r   r   	<genexpr>       z3ConditionTest.test_nested_notify.<locals>.<genexpr>)r   r"   r9   r   r,   
assertTrueall)r   futuresr   rP   r   test_nested_notify   s   
z ConditionTest.test_nested_notifyc                 c   s    t  }tdD ]
}|tdd q	t| }| dt|j	 t
dV  | dt|j	 | |  |  | |  d S )Ne   r*   r@   f   rB   r0   )r   r"   r9   r    r   rL   rM   r3   len_waitersr   rD   rE   rU   r,   rY   )r   r(   r   r   r   r   r   test_garbage_collection   s   z%ConditionTest.test_garbage_collection)__name__
__module____qualname__r   r   r!   r)   r   r-   r4   r=   r?   rG   rH   rJ   rK   r\   ra   __classcell__r   r   r   r   r      s,    



	


r   c                   @   s8   e Zd Zdd Zdd Zedd Zdd Zd	d
 ZdS )	EventTestc                 C   s\   t  }| dt|v  | dt|v  |  | dt|v  | dt|v  d S )Nclearset)r   EventrY   strrE   rh   )r   eventr   r   r   r)      s   zEventTest.test_reprc                 C   sp   t  }t| }|  t| }|  t| }| |  | |  | 	|  d S r	   )
r   ri   rL   rM   r    rh   rg   rY   rU   rE   )r   eZfuture_0Zfuture_1Zfuture_2r   r   r   
test_event   s   zEventTest.test_eventc                 c   sr    t  }| t |tddV  W d    n1 sw   Y  | jtdd|j |tddV  d S )Nr*   r@   r0   )	r   ri   assertRaisesr   r    r   r   Zadd_timeoutrh   r   rl   r   r   r   test_event_timeout   s   zEventTest.test_event_timeoutc                 C   s*   t  }|  |  | |  d S r	   )r   ri   rh   rY   is_setro   r   r   r   test_event_set_multiple   s   z!EventTest.test_event_set_multiplec                 C   sT   t  }t| }|  t| }|  | |  | |  d S r	   )	r   ri   rL   rM   r    rg   rh   rY   rU   )r   rl   f0f1r   r   r   test_event_wait_clear   s   zEventTest.test_event_wait_clearN)	rb   rc   rd   r)   rm   r   rp   rr   ru   r   r   r   r   rf      s    
	rf   c                   @   sP   e Zd Zdd Zdd Zdd Zedd Zed	d
 Zdd Z	edd Z
dS )SemaphoreTestc                 C   s   | j ttjdd d S )Nvalue)rn   
ValueErrorr   	Semaphorer   r   r   r   test_negative_value   s   z!SemaphoreTest.test_negative_valuec                 C   sl   t  }| dt| | dt| |  | dt| | dt| |  | dt| d S )Nr{   zunlocked,value:1lockedr#   )r   r{   r$   r%   acquirer&   r   semr   r   r   r)      s   zSemaphoreTest.test_reprc                 C   s   t  }t| }| |  t| }| |  t| }|  | |  | |  |  | |  |  | t|   | 	dt
|j d S )Nr   )r   r{   rL   rM   r~   rY   rU   rE   releaser3   r_   r`   )r   r   rs   rt   f2r   r   r   test_acquire  s   zSemaphoreTest.test_acquirec                 c   s    t d}| V  | V  |tdd}| jd|j tdV  | 	tj
 |V  W d    n1 s:w   Y  |  t| }| |  |  | |  d S )Nr8   r*   r@   rB   g333333?)r   r{   r~   r   r   r+   r   r   rD   rn   r   rL   rM   rE   rU   rY   )r   r   r~   rV   r   r   r   test_acquire_timeout  s   


z"SemaphoreTest.test_acquire_timeoutc                 c   sL    t d}| V  | jd|j |tdd}tdV  |V  d S )Nr0   r*   rB   r@   rC   )	r   r{   r~   r   r+   r   r   r   rD   )r   r   r~   r   r   r   test_acquire_timeout_preempted'  s   


z,SemaphoreTest.test_acquire_timeout_preemptedc                 C   s|   t  }|  |  | t|   | t|   | t|   | t|   d S r	   )	r   r{   r   rY   rL   rM   r~   rU   rE   r   r   r   r   test_release_unacquired2  s   z%SemaphoreTest.test_release_unacquiredc                 #   s    t jdd  fddtdD }t  }| dt j t	
dV  | dt j | |     | |  |D ]	}| t|j qEd S )	Nr   rx   c              	      s"   g | ]}t  td dqS )r*   r@   )rL   rM   r~   r   rN   r   r   r   rQ   B  s    z9SemaphoreTest.test_garbage_collection.<locals>.<listcomp>r]   r^   rB   r0   )r   r{   r9   rL   rM   r~   r3   r_   r`   r   rD   rE   rU   r   rY   rn   r   r   )r   r[   r   r   r   r   ra   >  s   
z%SemaphoreTest.test_garbage_collectionN)rb   rc   rd   r|   r)   r   r   r   r   r   ra   r   r   r   r   rv      s    



rv   c                   @   sh   e Zd Zedd Zedd Zedd Zedd Zed	d
 Zedd Z	edd Z
dd ZdS )SemaphoreContextManagerTestc                 c   s^    t  }| V }| |d u  W d    n1 sw   Y  | t|   d S r	   )r   r{   r~   rY   rL   rM   rU   )r   r   yieldedr   r   r   test_context_managerY  s   z0SemaphoreContextManagerTest.test_context_managerc                 #   s<    t   fdd}| V   t   d S )Nc               	      sN   4 I d H }   | d u  W d   I d H  d S 1 I d H s w   Y  d S r	   )rY   )r   r   r   r   rV   g  s   .zGSemaphoreContextManagerTest.test_context_manager_async_await.<locals>.f)r   r{   rY   rL   rM   r~   rU   )r   rV   r   r   r    test_context_manager_async_awaitb  s
   z<SemaphoreContextManagerTest.test_context_manager_async_awaitc              	   c   s    t  }| t! | V  dd  W d    n1 sw   Y  W d    n1 s.w   Y  | t|   d S )Nr0   r   )	r   r{   rn   ZeroDivisionErrorr~   rY   rL   rM   rU   r   r   r   r   test_context_manager_exceptionp  s   
z:SemaphoreContextManagerTest.test_context_manager_exceptionc                 c   sX    t  }|tddV  W d    n1 sw   Y  | t|   d S )Nr*   r@   )r   r{   r~   r   rY   rL   rM   rU   r   r   r   r   test_context_manager_timeoutz  s   z8SemaphoreContextManagerTest.test_context_manager_timeoutc              	   c   s    t jdd}| tj! |tddV  W d    n1 s"w   Y  W d    n1 s1w   Y  | t	| 
  d S )Nr   rx   r*   r@   )r   r{   rn   r   r   r~   r   rE   rL   rM   rU   r   r   r   r   "test_context_manager_timeout_error  s   z>SemaphoreContextManagerTest.test_context_manager_timeout_errorc                 #   sr    t  g tjfdd  fddtdD V  g }tdD ]}|d| d| g q#| | d S )Nc                 3   sZ      V   d|   tdV   d|   W d    d S 1 s&w   Y  d S )Nacquired %dr*   
release %d)r~   r   r   rD   )index)r   r   r   r   rV     s   "zESemaphoreContextManagerTest.test_context_manager_contended.<locals>.fc                       g | ]} |qS r   r   rO   r<   rV   r   r   rQ         zNSemaphoreContextManagerTest.test_context_manager_contended.<locals>.<listcomp>r8   r   r   )r   r{   r   	coroutiner9   extendr3   )r   Zexpected_historyr<   r   )rV   r   r   r   test_context_manager_contended  s   z:SemaphoreContextManagerTest.test_context_manager_contendedc              	   c   n    |  tj& t V  W d    n1 sw   Y  W d    d S W d    d S 1 s0w   Y  d S r	   )rn   r   BadYieldErrorr   r{   r   r   r   r   test_yield_sem     "z*SemaphoreContextManagerTest.test_yield_semc              	   C   h   |  t% t  W d    n1 sw   Y  W d    d S W d    d S 1 s-w   Y  d S r	   )rn   RuntimeErrorr   r{   r   r   r   r   test_context_manager_misuse     
"z7SemaphoreContextManagerTest.test_context_manager_misuseN)rb   rc   rd   r   r   r   r   r   r   r   r   r   r   r   r   r   r   X  s     


	

	

r   c                   @   s   e Zd Zdd ZdS )BoundedSemaphoreTestc                 C   sj   t  }| t|j |  t| }| |	  |  | 
|	  |  | t|j d S r	   )r   BoundedSemaphorern   rz   r   r~   rL   rM   rE   rU   rY   )r   r   r   r   r   r   r     s   z,BoundedSemaphoreTest.test_release_unacquiredN)rb   rc   rd   r   r   r   r   r   r     s    r   c                   @   s\   e Zd Zdd Zdd Zedd Zedd Zed	d
 Zdd Z	edd Z
dd ZdS )	LockTestsc                 C   s$   t  }t| |  t| d S r	   )r   Lockr%   r~   r   lockr   r   r   r)     s   zLockTests.test_reprc                 C   sV   t  }| t|   t| }| |  |  | |  d S r	   )	r   r   rY   rL   rM   r~   rU   rE   r   )r   r   r   r   r   r   test_acquire_release  s   zLockTests.test_acquire_releasec                 #   s    t  | t   d}g tjfdd  fddt	|D }| 
tdd |D    |V  | tt	| d S )N   c                 3   s<      V   |  W d    d S 1 sw   Y  d S r	   )r~   r   idxr   r   r   r   rV     s   "z&LockTests.test_acquire_fifo.<locals>.fc                    r   r   r   r   r   r   r   rQ     r   z/LockTests.test_acquire_fifo.<locals>.<listcomp>c                 s   rS   r	   rT   )rO   r   r   r   r   rW     rX   z.LockTests.test_acquire_fifo.<locals>.<genexpr>)r   r   rY   rL   rM   r~   rU   r   r   r9   rE   anyr   r3   r:   r   Nr[   r   rV   r   r   r   test_acquire_fifo  s   zLockTests.test_acquire_fifoc                 #   st    t  | t   d}g fdd  fddt|D }  |V  | 	t
t| d S )Nr   c              	      sJ   4 I d H    |  W d   I d H  d S 1 I d H sw   Y  d S r	   )r   r   r   r   r   rV     s   .z1LockTests.test_acquire_fifo_async_with.<locals>.fc                    r   r   r   r   r   r   r   rQ     r   z:LockTests.test_acquire_fifo_async_with.<locals>.<listcomp>)r   r   rY   rL   rM   r~   rU   r9   r   r3   r:   r   r   r   r   test_acquire_fifo_async_with  s   z&LockTests.test_acquire_fifo_async_withc                 c   sn    t  }|  | tj |jtdddV  W d    n1 s$w   Y  | t	| 
  d S )Nr*   r@   )r   )r   r   r~   rn   r   r   r   rE   rL   rM   rU   r   r   r   r   r     s   zLockTests.test_acquire_timeoutc                 C   s8   t  }| t|j |  |  | t|j d S r	   )r   r   rn   r   r   r~   r   r   r   r   test_multi_release  s
   zLockTests.test_multi_releasec              	   c   r   r	   )rn   r   r   r   r   r   r   r   r   test_yield_lock  r   zLockTests.test_yield_lockc              	   C   r   r	   )rn   r   r   r   r   r   r   r   r     r   z%LockTests.test_context_manager_misuseN)rb   rc   rd   r)   r   r   r   r   r   r   r   r   r   r   r   r   r     s    


	
r   __main__)rL   datetimer   typingZunittesttornador   r   Ztornado.genr   Ztornado.testingr   r   r   rf   rv   r   r   r   rb   mainr   r   r   r   <module>   s"    //dYU