U
    lufrB                     @   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re  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__ ;/tmp/pip-unpacked-wheel-bmg6zs32/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 n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   s~   t  }| | d | | d |d |   | jd |d |   | jd | ddddg| j d S )NZwait1Zwait2   Znotify1Znotify2)	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      r.         )
r   r"   ranger   r    r,   r!   r/   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 )Nr3   
notify_all)r   r"   r5   r   r    r:   r!   r   r   r/   r6   r7   r   r   r   test_notify_all\   s    zConditionTest.test_notify_allc                 c   sD   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*   r>   r<   r?   )	r   r"   r   r+   r,   r    r   r   r@   rB   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  | dddg| j | dddg| j |	  d V  | ddddg| j d S )	Nr   r*   r<   r.   r4   r2   r>   r   )
r   r"   r   r    r   r   r@   r/   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  | dddg| j d S )Nr   r*   r<   r.   r4   r>   r   )
r   r"   r   r    r   r   r@   r/   r   r:   r'   r   r   r   test_notify_all_with_timeout   s    z*ConditionTest.test_notify_all_with_timeoutc                 #   s`   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>r2   r.   c                    s      S r	   )r,   r   rK   r   r   <lambda>       z2ConditionTest.test_nested_notify.<locals>.<lambda>r4   c                 s   s   | ]}|  V  qd S r	   done)rJ   fr   r   r   	<genexpr>   s     z3ConditionTest.test_nested_notify.<locals>.<genexpr>)r   r"   r5   r   r,   
assertTrueall)r   futuresr   rK   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   r>   r.   )r   r"   r5   r    r   rG   rH   r/   len_waitersr   r@   rA   rP   r,   rS   )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-   r0   r9   r;   rC   rD   rE   rF   rV   r[   __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|k | dt|k |  | dt|k | dt|k d S )Nclearset)r   EventrS   strrA   rb   )r   eventr   r   r   r)      s    zEventTest.test_reprc                 C   sp   t  }t| }|  t| }|  t| }| |  | |  | 	|  d S r	   )
r   rc   rG   rH   r    rb   ra   rS   rP   rA   )r   eZfuture_0Zfuture_1Zfuture_2r   r   r   
test_event   s    zEventTest.test_eventc              	   c   s\   t  }| t |tddV  W 5 Q R X | jtdd|j |tddV  d S )Nr*   r<   r.   )	r   rc   assertRaisesr   r    r   r   Zadd_timeoutrb   r   rf   r   r   r   test_event_timeout   s
    zEventTest.test_event_timeoutc                 C   s*   t  }|  |  | |  d S r	   )r   rc   rb   rS   is_setri   r   r   r   test_event_set_multiple   s    z!EventTest.test_event_set_multiplec                 C   sT   t  }t| }|  t| }|  | |  | |  d S r	   )	r   rc   rG   rH   r    ra   rb   rS   rP   )r   rf   f0f1r   r   r   test_event_wait_clear   s    zEventTest.test_event_wait_clearN)	r\   r]   r^   r)   rg   r   rj   rl   ro   r   r   r   r   r`      s   
	r`   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)rh   
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 )Nru   zunlocked,value:1lockedr#   )r   ru   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   ru   rG   rH   rx   rS   rP   rA   releaser/   rY   rZ   )r   rz   rm   rn   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 5 Q R X |  t| }| |  |  | |  d S )Nr4   r*   r<   r>   g333333?)r   ru   rx   r   r   r+   r{   r   r@   rh   r   rG   rH   rA   rP   rS   )r   rz   rx   rQ   r   r   r   test_acquire_timeout  s    


z"SemaphoreTest.test_acquire_timeoutc                 c   sJ   t d}| V  | jd|j |tdd}tdV  |V  d S )Nr.   r*   r>   r<   r?   )	r   ru   rx   r   r+   r{   r   r   r@   )r   rz   rx   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   ru   r{   rS   rG   rH   rx   rP   rA   ry   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 qd S )	Nr   rr   c              	      s"   g | ]}t  td dqS )r*   r<   )rG   rH   rx   r   rI   rz   r   r   rL   B  s   z9SemaphoreTest.test_garbage_collection.<locals>.<listcomp>rW   rX   r>   r.   )r   ru   r5   rG   rH   rx   r/   rY   rZ   r   r@   rA   rP   r{   rS   rh   r   r   )r   rU   r   r   r   r   r[   >  s    
z%SemaphoreTest.test_garbage_collectionN)r\   r]   r^   rv   r)   r}   r   r~   r   r   r[   r   r   r   r   rp      s   



rp   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   sH   t  }| V }| |d k W 5 Q R X | t|   d S r	   )r   ru   rx   rS   rG   rH   rP   )r   rz   yieldedr   r   r   test_context_managerY  s    z0SemaphoreContextManagerTest.test_context_managerc                 #   s:   t   fdd}| V   t   d S )Nc               
      s0   4 I d H }   | d k W 5 Q I d H R X d S r	   )rS   )r   ry   r   r   rQ   g  s    zGSemaphoreContextManagerTest.test_context_manager_async_await.<locals>.f)r   ru   rS   rG   rH   rx   rP   )r   rQ   r   ry   r    test_context_manager_async_awaitb  s    z<SemaphoreContextManagerTest.test_context_manager_async_awaitc              
   c   sX   t  }| t$ | V  dd  W 5 Q R X W 5 Q R X | t|   d S )Nr.   r   )	r   ru   rh   ZeroDivisionErrorrx   rS   rG   rH   rP   ry   r   r   r   test_context_manager_exceptionp  s
    z:SemaphoreContextManagerTest.test_context_manager_exceptionc              	   c   sB   t  }|tddV  W 5 Q R X | t|   d S )Nr*   r<   )r   ru   rx   r   rS   rG   rH   rP   ry   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 5 Q R X W 5 Q R X | t	| 
  d S )Nr   rr   r*   r<   )r   ru   rh   r   r   rx   r   rA   rG   rH   rP   ry   r   r   r   "test_context_manager_timeout_error  s
    z>SemaphoreContextManagerTest.test_context_manager_timeout_errorc                 #   sp   t  g tjfdd  fddtdD V  g }tdD ]}|d| d| g qD| | d S )Nc              	   3   sB     V .  d|   tdV   d|   W 5 Q R X d S )Nacquired %dr*   
release %d)rx   r   r   r@   )index)r   rz   r   r   rQ     s    zESemaphoreContextManagerTest.test_context_manager_contended.<locals>.fc                    s   g | ]} |qS r   r   rJ   r8   rQ   r   r   rL     s     zNSemaphoreContextManagerTest.test_context_manager_contended.<locals>.<listcomp>r4   r   r   )r   ru   r   	coroutiner5   extendr/   )r   Zexpected_historyr8   r   )rQ   r   rz   r   test_context_manager_contended  s    z:SemaphoreContextManagerTest.test_context_manager_contendedc              
   c   s2   |  tj t V  W 5 Q R X W 5 Q R X d S r	   )rh   r   BadYieldErrorr   ru   r   r   r   r   test_yield_sem  s    z*SemaphoreContextManagerTest.test_yield_semc              
   C   s.   |  t t  W 5 Q R X W 5 Q R X d S r	   )rh   RuntimeErrorr   ru   r   r   r   r   test_context_manager_misuse  s    
z7SemaphoreContextManagerTest.test_context_manager_misuseN)r\   r]   r^   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   BoundedSemaphorerh   rt   r{   rx   rG   rH   rA   rP   rS   )r   rz   r   r   r   r   r     s    z,BoundedSemaphoreTest.test_release_unacquiredN)r\   r]   r^   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%   rx   r   lockr   r   r   r)     s    zLockTests.test_reprc                 C   sV   t  }| t|   t| }| |  |  | |  d S r	   )	r   r   rS   rG   rH   rx   rP   rA   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 5 Q R X d S r	   )rx   r   idxr   r   r   r   rQ     s    z&LockTests.test_acquire_fifo.<locals>.fc                    s   g | ]} |qS r   r   r   r   r   r   rL     s     z/LockTests.test_acquire_fifo.<locals>.<listcomp>c                 s   s   | ]}|  V  qd S r	   rO   )rJ   r   r   r   r   rR     s     z.LockTests.test_acquire_fifo.<locals>.<genexpr>)r   r   rS   rG   rH   rx   rP   r   r   r5   rA   anyr{   r/   r6   r   NrU   r   rQ   r   r   r   test_acquire_fifo  s    zLockTests.test_acquire_fifoc                 #   sr   t  | t   d}g fdd  fddt|D }  |V  | 	t
t| d S )Nr   c              
      s,   4 I d H    |  W 5 Q I d H R X d S r	   )r   r   r   r   r   rQ     s    z1LockTests.test_acquire_fifo_async_with.<locals>.fc                    s   g | ]} |qS r   r   r   r   r   r   rL     s     z:LockTests.test_acquire_fifo_async_with.<locals>.<listcomp>)r   r   rS   rG   rH   rx   rP   r5   r{   r/   r6   r   r   r   r   test_acquire_fifo_async_with  s    z&LockTests.test_acquire_fifo_async_withc              	   c   sX   t  }|  | tj |jtdddV  W 5 Q R X | t	| 
  d S )Nr*   r<   )r   )r   r   rx   rh   r   r   r   rA   rG   rH   rP   r   r   r   r   r~     s
    zLockTests.test_acquire_timeoutc                 C   s8   t  }| t|j |  |  | t|j d S r	   )r   r   rh   r   r{   rx   r   r   r   r   test_multi_release  s
    zLockTests.test_multi_releasec              
   c   s2   |  tj t V  W 5 Q R X W 5 Q R X d S r	   )rh   r   r   r   r   r   r   r   r   test_yield_lock  s    zLockTests.test_yield_lockc              
   C   s.   |  t t  W 5 Q R X W 5 Q R X d S r	   )rh   r   r   r   r   r   r   r   r     s    
z%LockTests.test_context_manager_misuseN)r\   r]   r^   r)   r   r   r   r   r~   r   r   r   r   r   r   r   r     s   


	
r   __main__)rG   datetimer   typingZunittestZtornador   r   Ztornado.genr   Ztornado.testingr   r   r   r`   rp   r   r   r   r\   mainr   r   r   r   <module>   s    //dYU