o
    Qœ_rB  ã                   @   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__© ú9/usr/lib/python3/dist-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 )zËRun 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_loopÚ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ç{®Gáz„?)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/   r2   r0   r3   )	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é   é   r1   é   é   )
r   r#   Úranger   r!   r-   r"   r4   Ú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 )Nr8   Ú
notify_all)r   r#   r:   r   r!   r?   r"   r   r   r4   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ç{®Gáz”?ç¸…ë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+   rC   rA   rD   )	r   r#   r   r,   r-   r!   r   r   rE   rG   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+   rA   r1   r9   r7   rC   r   ©r   r   r9   )r   r   r9   r7   )
r   r#   r   r!   r   r   rE   r4   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+   rA   r1   r9   rC   r   rJ   )
r   r#   r   r!   r   r   rE   r4   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>r7   r1   c                    s   ˆ   ¡ S r	   )r-   r   rQ   r   r   Ú<lambda>¬   s    z2ConditionTest.test_nested_notify.<locals>.<lambda>r9   c                 s   ó   � | ]}|  ¡ V  qd S r	   ©Údone)rP   Úfr   r   r   Ú	<genexpr>¯   ó   € z3ConditionTest.test_nested_notify.<locals>.<genexpr>)r   r#   r:   r   r-   Ú
assertTrueÚall)r   Úfuturesr   rQ   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 )Née   r+   rA   éf   rC   r1   )r   r#   r:   r!   r   rM   rN   r4   ÚlenÚ_waitersr   rE   rF   rV   r-   rZ   )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.   r5   r>   r@   rH   rI   rK   rL   r]   rb   Ú__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   ÚEventrZ   ÚstrrF   ri   )r   Úeventr   r   r   r*   Æ   s   zEventTest.test_reprc                 C   sp   t  ¡ }t | ¡ ¡}| ¡  t | ¡ ¡}| ¡  t | ¡ ¡}|  | ¡ ¡ |  | ¡ ¡ |  	| ¡ ¡ d S r	   )
r   rj   rM   rN   r!   ri   rh   rZ   rV   rF   )r   ÚeÚfuture_0Úfuture_1Ú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+   rA   r1   )	r   rj   ÚassertRaisesr   r!   r   r   Úadd_timeoutri   ©r   rm   r   r   r   Útest_event_timeoutÚ   s   €ÿzEventTest.test_event_timeoutc                 C   s*   t  ¡ }| ¡  | ¡  |  | ¡ ¡ d S r	   )r   rj   ri   rZ   Úis_setrt   r   r   r   Útest_event_set_multipleä   s   z!EventTest.test_event_set_multiplec                 C   sT   t  ¡ }t | ¡ ¡}| ¡  t | ¡ ¡}| ¡  |  | ¡ ¡ |  | ¡ ¡ d S r	   )	r   rj   rM   rN   r!   rh   ri   rZ   rV   )r   rm   Úf0Úf1r   r   r   Útest_event_wait_clearê   s   zEventTest.test_event_wait_clearN)	rc   rd   re   r*   rq   r   ru   rw   rz   r   r   r   r   rg   Å   s    
	rg   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)rr   Ú
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€   rM   rN   rƒ   rZ   rV   rF   Úreleaser4   r`   ra   )r   r…   rx   ry   Ú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 )Nr9   r+   rA   rC   g333333Ó?)r   r€   rƒ   r   r   r,   r†   r   rE   rr   r   rM   rN   rF   rV   rZ   )r   r…   rƒ   rW   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 )Nr1   r+   rC   rA   rD   )	r   r€   rƒ   r   r,   r†   r   r   rE   )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†   rZ   rM   rN   rƒ   rV   rF   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   r}   c              	      s"   g | ]}t  ˆ  td d�¡¡‘qS )r+   rA   )rM   rN   rƒ   r   rO   ©r…   r   r   rR   B  s    ÿÿz9SemaphoreTest.test_garbage_collection.<locals>.<listcomp>r^   r_   rC   r1   )r   r€   r:   rM   rN   rƒ   r4   r`   ra   r   rE   rF   rV   r†   rZ   rr   r   r   )r   r\   r   r   rŒ   r   rb   >  s   €
þÿz%SemaphoreTest.test_garbage_collectionN)rc   rd   re   r�   r*   rˆ   r   r‰   rŠ   r‹   rb   r   r   r   r   r{   ô   s    



r{   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ƒ   rZ   rM   rN   rV   )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	   )rZ   )rŽ   r„   r   r   rW   g  s   €.ÿzGSemaphoreContextManagerTest.test_context_manager_async_await.<locals>.f)r   r€   rZ   rM   rN   rƒ   rV   )r   rW   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 )Nr1   r   )	r   r€   rr   ÚZeroDivisionErrorrƒ   rZ   rM   rN   rV   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+   rA   )r   r€   rƒ   r   rZ   rM   rN   rV   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   r}   r+   rA   )r   r€   rr   r   r   rƒ   r   rF   rM   rN   rV   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   rE   )Úindex)r   r…   r   r   rW   ’  s   €"ýzESemaphoreContextManagerTest.test_context_manager_contended.<locals>.fc                    ó   g | ]}ˆ |ƒ‘qS r   r   ©rP   r=   ©rW   r   r   rR   ™  ó    zNSemaphoreContextManagerTest.test_context_manager_contended.<locals>.<listcomp>r9   r•   r–   )r   r€   r   Ú	coroutiner:   Úextendr4   )r   Úexpected_historyr=   r   )rW   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	   )rr   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	   )rr   ÚRuntimeErrorr   r€   r   r   r   r   Útest_context_manager_misuse©  ó   
ÿÿ"ÿz7SemaphoreContextManagerTest.test_context_manager_misuseN)rc   rd   re   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   ÚBoundedSemaphorerr   r   r†   rƒ   rM   rN   rF   rV   rZ   )r   r…   r   r   r   r   r‹   ²  s   z,BoundedSemaphoreTest.test_release_unacquiredN)rc   rd   re   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«   rZ   rM   rN   rƒ   rV   rF   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   rW   Ø  s   €"ÿz&LockTests.test_acquire_fifo.<locals>.fc                    r˜   r   r   r™   rš   r   r   rR   Ý  r›   z/LockTests.test_acquire_fifo.<locals>.<listcomp>c                 s   rT   r	   rU   )rP   r   r   r   r   rX   Þ  rY   z.LockTests.test_acquire_fifo.<locals>.<genexpr>)r   r«   rZ   rM   rN   rƒ   rV   r   rœ   r:   rF   Úanyr†   r4   r;   ©r   ÚNr\   r   ©rW   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   rW   ì  s   €.ÿz1LockTests.test_acquire_fifo_async_with.<locals>.fc                    r˜   r   r   r™   rš   r   r   rR   ð  r›   z:LockTests.test_acquire_fifo_async_with.<locals>.<listcomp>)r   r«   rZ   rM   rN   rƒ   rV   r:   r†   r4   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+   rA   )r   )r   r«   rƒ   rr   r   r   r   rF   rM   rN   rV   r¬   r   r   r   r‰   õ  s   €ÿzLockTests.test_acquire_timeoutc                 C   s8   t  ¡ }|  t|j¡ | ¡  | ¡  |  t|j¡ d S r	   )r   r«   rr   r¥   r†   rƒ   r¬   r   r   r   Útest_multi_releaseÿ  s
   zLockTests.test_multi_releasec              	   c   r    r	   )rr   r   r¡   r   r«   r   r   r   r   Útest_yield_lock  r£   zLockTests.test_yield_lockc              	   C   r¤   r	   )rr   r¥   r   r«   r   r   r   r   r¦     r§   z%LockTests.test_context_manager_misuseN)rc   rd   re   r*   r®   r   r·   r¸   r‰   r¹   rº   r¦   r   r   r   r   rª   Á  s    


	
rª   Ú__main__)rM   Údatetimer   ÚtypingÚunittestÚtornador   r   Útornado.genr   Útornado.testingr   r   r   rg   r{   r�   r¨   rª   rc   Úmainr   r   r   r   Ú<module>   s"    //dYUÿ