a
    õ£Bek  ã                   @   s¶  d dl Z d dlmZ d dlmZ d dlmZ d dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlZd dlZd dlZd dlmZ d dlZd dlmZ d dlmZ d dlmZmZmZ d d	lmZ d d
lmZmZmZm Z m!Z! d dl"m#Z#m$Z$m%Z% d dl&m'Z' d dl(Z(e(j)�rd dl(m*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j,ƒZ0G dd„ dej,ƒZ1G dd„ deƒZ2G dd„ dej,ƒZ3e4dk�r²e 5¡  dS )é    N)ÚThreadPoolExecutor)Úfutures)Ú	Generator)Úmock)Ú
native_str)Úgen)ÚIOLoopÚTimeoutErrorÚPeriodicCallback)Úapp_log)ÚAsyncTestCaseÚbind_unused_portÚ	ExpectLogÚgen_testÚsetup_with_context_manager)Úignore_deprecationÚskipIfNonUnixÚskipOnTravis)ÚFuture)ÚListc                   @   s  e Zd Zdd„ Zedd„ ƒZedd„ ƒZdd„ Zd	d
„ Z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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/d0„ Zd1d2„ Zed3d4„ ƒZeed5d6„ ƒƒZ d7d8„ Z!d9S ):Ú
TestIOLoopc                    s~   d| _ | j}| ‰|j‰ ‡ ‡fdd„}t ||¡|_| dd„ ¡ | dd„ ¡ | tjdd�|j¡ | 	¡  |  
| j d	¡ d S )
Nr   c                    s(   ˆ j d7  _ ˆ |g|¢R i |¤Ž d S ©Né   ©Úcalls)ÚselfÚcallbackÚargsÚkwargs©Zold_add_callbackÚtest© ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/tornado/test/ioloop_test.pyÚadd_callback2   s    zBTestIOLoop.test_add_callback_return_sequence.<locals>.add_callbackc                   S   s   i S ©Nr!   r!   r!   r!   r"   Ú<lambda>7   ó    z>TestIOLoop.test_add_callback_return_sequence.<locals>.<lambda>c                   S   s   g S r$   r!   r!   r!   r!   r"   r%   8   r&   é2   )Zmillisecondsé
   )r   Úio_loopr#   ÚtypesÚ
MethodTypeÚadd_timeoutÚdatetimeÚ	timedeltaÚstopÚstartÚ
assertLess)r   Úloopr#   r!   r   r"   Ú!test_add_callback_return_sequence*   s    z,TestIOLoop.test_add_callback_return_sequencec                    s\   ‡fdd„‰ ‡ ‡fdd„}ˆj  ˆj  ¡ |¡ ˆ ¡  ˆjt ¡ ˆjdd� ˆ ˆj¡ d S )Nc                      s   dˆ _ ˆ  ¡  d S ©NT)Úcalledr/   r!   ©r   r!   r"   r   A   s    z5TestIOLoop.test_add_callback_wakeup.<locals>.callbackc                      s    dˆ_ ˆj ˆ ¡ t ¡ ˆ_d S ©NF)r5   r)   r#   ÚtimeÚ
start_timer!   ©r   r   r!   r"   Úschedule_callbackE   s    z>TestIOLoop.test_add_callback_wakeup.<locals>.schedule_callbacké   )Zplaces)r)   r,   r8   ÚwaitZassertAlmostEqualr9   Ú
assertTruer5   )r   r;   r!   r:   r"   Útest_add_callback_wakeup=   s    z#TestIOLoop.test_add_callback_wakeupc                    sT   ‡ fdd„}t j|d�}ˆ j |j¡ ˆ  ¡  t ¡ ˆ j }ˆ  |d¡ | 	¡  d S )Nc                      s&   t  d¡ t   ¡ ˆ _ˆ j ˆ j¡ d S )Nç{®Gáz„?)r8   ÚsleepÚ	stop_timer)   r#   r/   r!   r6   r!   r"   ÚtargetR   s    

z@TestIOLoop.test_add_callback_wakeup_other_thread.<locals>.target©rC   çš™™™™™¹?)
Ú	threadingÚThreadr)   r#   r0   r=   r8   rB   r1   Újoin)r   rC   ÚthreadÚdeltar!   r6   r"   Ú%test_add_callback_wakeup_other_threadP   s    z0TestIOLoop.test_add_callback_wakeup_other_threadc                 C   s$   | j  tjdd�| j¡ |  ¡  d S )Nr   )Úmicroseconds)r)   r,   r-   r.   r/   r=   r6   r!   r!   r"   Útest_add_timeout_timedelta_   s    z%TestIOLoop.test_add_timeout_timedeltac              
   C   s€   t ƒ \}}zV| j | ¡ dd„ tj¡ |  t| jj| ¡ dd„ tj¡ W | j | ¡ ¡ | 	¡  n| j | ¡ ¡ | 	¡  0 d S )Nc                 S   s   d S r$   r!   ©ÚfdÚeventsr!   r!   r"   r%   g   r&   z.TestIOLoop.test_multiple_add.<locals>.<lambda>c                 S   s   d S r$   r!   rN   r!   r!   r"   r%   o   r&   )
r   r)   Úadd_handlerÚfilenor   ÚREADÚassertRaisesÚ	ExceptionÚremove_handlerÚclose©r   ÚsockÚportr!   r!   r"   Útest_multiple_addc   s     
ÿû
ÿzTestIOLoop.test_multiple_addc                 C   s6   t ƒ \}}z| j | ¡ ¡ W | ¡  n
| ¡  0 d S r$   )r   r)   rV   rR   rW   rX   r!   r!   r"   Útest_remove_without_addv   s    
z"TestIOLoop.test_remove_without_addc                 C   s   | j  | j¡ |  ¡  d S r$   )r)   Úadd_callback_from_signalr/   r=   r6   r!   r!   r"   Útest_add_callback_from_signal   s    z(TestIOLoop.test_add_callback_from_signalc                 C   s<   t ƒ }tj|jd�}| ¡  | |j¡ | ¡  | ¡  d S )NrD   )r   rF   rG   r0   r]   r/   rH   rW   )r   Úother_iolooprI   r!   r!   r"   Ú*test_add_callback_from_signal_other_thread…   s    z5TestIOLoop.test_add_callback_from_signal_other_threadc                    sX   t  ¡ ‰ ‡ ‡fdd„}tƒ ‰t j|d�}| ¡  ˆ  ¡  tdƒD ]}ˆ dd„ ¡ q@d S )Nc                      s,   ˆ  ˆj¡ ˆ ¡  ˆ  ¡  ˆjdd� d S )NT©Zall_fds)r#   r/   r0   ÚsetrW   r!   ©Úclosingr_   r!   r"   rC   –   s    z:TestIOLoop.test_add_callback_while_closing.<locals>.targetrD   éè  c                   S   s   d S r$   r!   r!   r!   r!   r"   r%   ¡   r&   z<TestIOLoop.test_add_callback_while_closing.<locals>.<lambda>)rF   ÚEventr   rG   r0   r=   Úranger#   )r   rC   rI   Úir!   rc   r"   Útest_add_callback_while_closing�   s    z*TestIOLoop.test_add_callback_while_closingc                    s”   t  ¡ \}}zp‡ fdd„}ˆ j | ¡ |tj¡ ˆ j ˆ j ¡ d t	 
|jd¡¡ ˆ  ¡  ˆ j | ¡ ¡ W | ¡  | ¡  n| ¡  | ¡  0 d S )Nc                    s   ˆ   |tj¡ ˆ  ¡  d S r$   )ÚassertEqualr   rS   r/   rN   r6   r!   r"   Úhandler«   s    z5TestIOLoop.test_read_while_writeable.<locals>.handlerr@   s   asdf)ÚsocketÚ
socketpairr)   rQ   rR   r   rS   r,   r8   Ú	functoolsÚpartialÚsendr=   rV   rW   )r   ÚclientÚserverrk   r!   r6   r"   Útest_read_while_writeable£   s    ÿ
ÿz$TestIOLoop.test_read_while_writeablec                 C   s.   | j  | j  ¡ | j¡}|  ¡  | j  |¡ d S r$   )r)   r,   r8   r/   r=   Úremove_timeout©r   Úhandler!   r!   r"   Útest_remove_timeout_after_fire¹   s    z)TestIOLoop.test_remove_timeout_after_firec                    sV   t dƒD ],}ˆ j ˆ j ¡ d dd„ ¡}ˆ j |¡ qˆ j ‡ fdd„¡ ˆ  ¡  d S )NiÐ  i  c                   S   s   d S r$   r!   r!   r!   r!   r"   r%   Æ   r&   z8TestIOLoop.test_remove_timeout_cleanup.<locals>.<lambda>c                      s   ˆ j  ˆ j¡S r$   ©r)   r#   r/   r!   r6   r!   r"   r%   É   r&   )rg   r)   r,   r8   rt   r#   r=   )r   rh   Útimeoutr!   r6   r"   Útest_remove_timeout_cleanup¿   s
    z&TestIOLoop.test_remove_timeout_cleanupc                    sŒ   ddg‰ ˆj  ¡ }‡ ‡‡fdd„}ˆj  |d |¡ ‡ fdd„}ˆj  |d |¡‰ˆj  |d ˆj¡ t d¡ ˆ ¡  ˆ ˆ d	dg¡ d S )
NFc                      s   dˆ d< ˆj  ˆ¡ d S ©NTr   )r)   rt   r!   ©r   r   Z	t2_handler!   r"   Út1Ô   s    z7TestIOLoop.test_remove_timeout_from_timeout.<locals>.t1r@   c                      s   dˆ d< d S )NTr   r!   r!   r   r!   r"   Út2Ú   s    z7TestIOLoop.test_remove_timeout_from_timeout.<locals>.t2g{®Gáz”?g¸…ëQ¸ž?T)r)   r8   r,   r/   rA   r=   rj   )r   Únowr}   r~   r!   r|   r"   Ú test_remove_timeout_from_timeoutÌ   s    

z+TestIOLoop.test_remove_timeout_from_timeoutc                 C   s�   g }| j  | j  ¡ |jd¡ | j  tjdd�|jd¡ | j  | j  ¡ |jd¡ | j  d|jd¡ | j  d| j¡ |  	¡  |  
t|ƒg d¢¡ d S )Nr   r   )Úsecondsr<   é   é   )r   r<   r‚   rƒ   )r)   r,   r8   Úappendr-   r.   Úcall_atÚ
call_laterr/   r=   rj   Úsorted)r   Úresultsr!   r!   r"   Útest_timeout_with_argumentsã   s    z&TestIOLoop.test_timeout_with_argumentsc                 C   s6   | j  | j  ¡ dd„ ¡}|  |d u ¡ | j  |¡ d S )Nc                   S   s   d S r$   r!   r!   r!   r!   r"   r%   ó   r&   z4TestIOLoop.test_add_timeout_return.<locals>.<lambda>)r)   r,   r8   ÚassertFalsert   ru   r!   r!   r"   Útest_add_timeout_returnð   s    z"TestIOLoop.test_add_timeout_returnc                 C   s6   | j  | j  ¡ dd„ ¡}|  |d u ¡ | j  |¡ d S )Nc                   S   s   d S r$   r!   r!   r!   r!   r"   r%   ø   r&   z0TestIOLoop.test_call_at_return.<locals>.<lambda>)r)   r…   r8   rŠ   rt   ru   r!   r!   r"   Útest_call_at_return÷   s    zTestIOLoop.test_call_at_returnc                 C   s0   | j  ddd„ ¡}|  |d u ¡ | j  |¡ d S )Nr   c                   S   s   d S r$   r!   r!   r!   r!   r"   r%   ý   r&   z3TestIOLoop.test_call_later_return.<locals>.<lambda>)r)   r†   rŠ   rt   ru   r!   r!   r"   Útest_call_later_returnü   s    z!TestIOLoop.test_call_later_returnc                 C   sX   G dd„ dt ƒ}tƒ \}}||ƒ}tƒ }| |dd„ tj¡ |jdd� |  |j¡ dS )z¦When a file object is used instead of a numeric file descriptor,
        the object should be closed (by IOLoop.close(all_fds=True),
        not just the fd.
        c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )z8TestIOLoop.test_close_file_object.<locals>.SocketWrapperc                 S   s   || _ d| _d S r7   )ÚsockobjÚclosed)r   rŽ   r!   r!   r"   Ú__init__
  s    zATestIOLoop.test_close_file_object.<locals>.SocketWrapper.__init__c                 S   s
   | j  ¡ S r$   )rŽ   rR   r6   r!   r!   r"   rR     s    z?TestIOLoop.test_close_file_object.<locals>.SocketWrapper.filenoc                 S   s   d| _ | j ¡  d S r4   )r�   rŽ   rW   r6   r!   r!   r"   rW     s    z>TestIOLoop.test_close_file_object.<locals>.SocketWrapper.closeN)Ú__name__Ú
__module__Ú__qualname__r�   rR   rW   r!   r!   r!   r"   ÚSocketWrapper	  s   r”   c                 S   s   d S r$   r!   rN   r!   r!   r"   r%     r&   z3TestIOLoop.test_close_file_object.<locals>.<lambda>Tra   N)Úobjectr   r   rQ   rS   rW   r>   r�   )r   r”   rŽ   rZ   Zsocket_wrapperr)   r!   r!   r"   Útest_close_file_object  s    
z!TestIOLoop.test_close_file_objectc                    s  t ƒ \‰}g ‰ ‡ ‡‡fdd„}ˆj ˆ|tj¡ t t ¡ ¡�&}| d|f¡ ˆ 	¡  W d  ƒ n1 sj0    Y  ˆj 
ˆ¡ ˆj ˆ ¡ |tj¡ t t ¡ ¡�&}| d|f¡ ˆ 	¡  W d  ƒ n1 sÐ0    Y  ˆ ˆ d ˆ¡ ˆ ˆ d ˆ ¡ ¡ ˆj 
ˆ ¡ ¡ ˆ ¡  dS )z>The handler callback receives the same fd object it passed in.c                    s*   ˆ   | ¡ ˆ ¡ \}}| ¡  ˆ ¡  d S r$   )r„   ÚacceptrW   r/   )rO   rP   ÚconnÚaddr©Úfdsr   Úserver_sockr!   r"   Úhandle_connection!  s    
zGTestIOLoop.test_handler_callback_file_object.<locals>.handle_connectionz	127.0.0.1Nr   r   )r   r)   rQ   r   rS   Ú
contextlibrd   rl   Úconnectr=   rV   rR   ÚassertIsrj   rW   )r   rZ   r�   Zclient_sockr!   rš   r"   Ú!test_handler_callback_file_object  s     
&&z,TestIOLoop.test_handler_callback_file_objectc                 C   s€   t ƒ \}}dd„ }| j ||tj¡ |  t¡�& | j | ¡ |tj¡ W d   ƒ n1 sZ0    Y  | j | ¡ ¡ | 	¡  d S )Nc                 S   s   d S r$   r!   rN   r!   r!   r"   Úf8  s    z+TestIOLoop.test_mixed_fd_fileobj.<locals>.f)
r   r)   rQ   r   rS   rT   rU   rR   rV   rW   )r   rœ   rZ   r¢   r!   r!   r"   Útest_mixed_fd_fileobj5  s    
4z TestIOLoop.test_mixed_fd_fileobjc                    sP   dg‰dg‰ ‡ ‡‡fdd„}ˆj  |¡ ˆ ¡  ˆ ˆ d ¡ ˆ ˆd ¡ dS )z:Calling start() twice should raise an error, not deadlock.Fc                      s>   zˆj  ¡  dˆd< W n ty0   dˆ d< Y n0 ˆ ¡  d S r{   )r)   r0   rU   r/   r!   ©Zgot_exceptionZreturned_from_startr   r!   r"   r   H  s    
z+TestIOLoop.test_reentrant.<locals>.callbackr   N)r)   r#   r=   r>   rŠ   ©r   r   r!   r¤   r"   Útest_reentrantC  s    zTestIOLoop.test_reentrantc                 C   sT   | j  dd„ ¡ | j  | j¡ ttdƒ� |  ¡  W d  ƒ n1 sF0    Y  dS )z-Uncaught exceptions get logged by the IOLoop.c                   S   s   dd S ©Nr   r   r!   r!   r!   r!   r"   r%   W  r&   z3TestIOLoop.test_exception_logging.<locals>.<lambda>úException in callbackN)r)   r#   r/   r   r   r=   r6   r!   r!   r"   Útest_exception_loggingU  s    z!TestIOLoop.test_exception_loggingc                    sT   t j‡ fdd„ƒ}ˆ j |¡ ttdƒ� ˆ  ¡  W d  ƒ n1 sF0    Y  dS )z:The IOLoop examines exceptions from Futures and logs them.c                      s   ˆ j  ˆ j¡ dd  d S r§   rx   r!   r6   r!   r"   r   _  s    z:TestIOLoop.test_exception_logging_future.<locals>.callbackr¨   N)r   Ú	coroutiner)   r#   r   r   r=   r¥   r!   r6   r"   Útest_exception_logging_future\  s
    z(TestIOLoop.test_exception_logging_futurec                    sN   ‡ fdd„}ˆ j  |¡ ttdƒ� ˆ  ¡  W d  ƒ n1 s@0    Y  dS )z=The IOLoop examines exceptions from awaitables and logs them.c                   “   s    ˆ j  ˆ j jˆ j¡ dd  d S r§   rx   r!   r6   r!   r"   r   k  s    z?TestIOLoop.test_exception_logging_native_coro.<locals>.callbackr¨   N)r)   r#   r   r   r=   r¥   r!   r6   r"   Ú"test_exception_logging_native_coroh  s    z-TestIOLoop.test_exception_logging_native_coroc                 C   s¤   | j  dd„ ¡ | j  | j¡ ttdƒ� |  ¡  W d   ƒ n1 sF0    Y  | j  dd„ ¡ | j  | j¡ ttdƒ� |  ¡  W d   ƒ n1 s–0    Y  d S )Nc                   S   s   dd S r§   r!   r!   r!   r!   r"   r%   x  r&   z0TestIOLoop.test_spawn_callback.<locals>.<lambda>r¨   c                   S   s   dd S r§   r!   r!   r!   r!   r"   r%   ~  r&   )r)   r#   r/   r   r   r=   Zspawn_callbackr6   r!   r!   r"   Útest_spawn_callbacku  s    &zTestIOLoop.test_spawn_callbackc                    s°   t  ¡ \‰‰zŒˆ d¡ ˆ d¡ g ‰ ‡ ‡‡‡fdd„}ˆj ˆ|ˆjj¡ ˆj ˆ|ˆjj¡ ˆj dˆj¡ ˆ ¡  ˆ 	ˆ dg¡ W ˆ 
¡  ˆ 
¡  nˆ 
¡  ˆ 
¡  0 d S )Ns   abcc                    s6   ˆ   |  d¡¡ | ˆu r&ˆj ˆ¡ nˆj ˆ¡ d S )Ni   )r„   Úrecvr)   rV   rN   ©Úchunksrq   r   rr   r!   r"   Úhandle_readŽ  s    z@TestIOLoop.test_remove_handler_from_handler.<locals>.handle_readrE   )rl   rm   rp   r)   rQ   rS   r†   r/   r=   rj   rW   )r   r±   r!   r¯   r"   Ú test_remove_handler_from_handlerƒ  s    


ÿz+TestIOLoop.test_remove_handler_from_handlerc                 #   s,   dd„ ‰ t  ‡ ‡fdd„tdƒD ƒ¡V  d S )Nc                  S   s$   t dƒD ]} tdd�}| ¡  qd S )Nr(   F©Úmake_current)rg   r   rW   )rh   r2   r!   r!   r"   r¢   ¨  s    
z*TestIOLoop.test_init_close_race.<locals>.fc                    s   g | ]}ˆj  d ˆ ¡‘qS r$   )r)   Úrun_in_executor)Ú.0rh   ©r¢   r   r!   r"   Ú
<listcomp>­  r&   z3TestIOLoop.test_init_close_race.<locals>.<listcomp>r<   )r   Úmultirg   r6   r!   r·   r"   Útest_init_close_race   s    zTestIOLoop.test_init_close_racec                 C   sd   t  ¡ }t|dd�}|j|u s"J ‚|  t¡� t|dd� W d   ƒ n1 sN0    Y  | ¡  d S )NF)Úasyncio_loopr´   )ÚasyncioÚnew_event_loopr   r»   rT   ÚRuntimeErrorrW   )r   r»   r2   r!   r!   r"   Útest_explicit_asyncio_loop¯  s    *z%TestIOLoop.test_explicit_asyncio_loopN)"r‘   r’   r“   r3   r   r?   rK   rM   r[   r\   r^   r`   ri   r   rs   rw   rz   r€   r‰   r‹   rŒ   r�   r–   r¡   r£   r¦   r©   r«   r¬   r­   r²   r   rº   r¿   r!   r!   r!   r"   r   )   sD   

	

r   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚTestIOLoopCurrentc                 C   s   t | tƒ ƒ d | _t ¡  d S r$   )r   r   r)   r   Úclear_currentr6   r!   r!   r"   ÚsetUp¼  s    zTestIOLoopCurrent.setUpc                 C   s   | j d ur| j  ¡  d S r$   ©r)   rW   r6   r!   r!   r"   ÚtearDownÁ  s    
zTestIOLoopCurrent.tearDownc                    st   t dd�ˆ _ˆ  t jdd�¡ tdƒD ]H}‡ fdd„}ˆ j |¡ ˆ j ¡  ˆ  ˆ jˆ j¡ ˆ  t jdd�¡ q&d S )NFr³   )Úinstancer‚   c                      s&   t  ¡ ˆ _ˆ jd usJ ‚ˆ j ¡  d S r$   )r   ÚcurrentÚcurrent_io_loopr)   r/   r!   r6   r!   r"   r¢   Í  s    
z-TestIOLoopCurrent.test_non_current.<locals>.f)	r   r)   ZassertIsNonerÆ   rg   r#   r0   r    rÇ   )r   rh   r¢   r!   r6   r"   Útest_non_currentÅ  s    
z"TestIOLoopCurrent.test_non_currentc                 C   s"   t dd�| _|  | jt  ¡ ¡ d S )NTr³   )r   r)   r    rÆ   r6   r!   r!   r"   Útest_force_currentØ  s    z$TestIOLoopCurrent.test_force_currentN)r‘   r’   r“   rÂ   rÄ   rÈ   rÉ   r!   r!   r!   r"   rÀ   »  s   rÀ   c                       s(   e Zd Z‡ fdd„Zedd„ ƒZ‡  ZS )ÚTestIOLoopCurrentAsyncc                    s   t ƒ  ¡  t| tƒ ƒ d S r$   )ÚsuperrÂ   r   r   r6   ©Ú	__class__r!   r"   rÂ   Þ  s    
zTestIOLoopCurrentAsync.setUpc                 c   s:   t dƒ�}| tj¡V  W d   ƒ n1 s,0    Y  d S r   )r   Úsubmitr   rÁ   )r   Úer!   r!   r"   Útest_clear_without_currentâ  s    
z1TestIOLoopCurrentAsync.test_clear_without_current)r‘   r’   r“   rÂ   r   rÐ   Ú__classcell__r!   r!   rÌ   r"   rÊ   Ý  s   rÊ   c                   @   s8   e Zd Zdd„ Zedd„ ƒZedd„ ƒZedd„ ƒZd	S )
ÚTestIOLoopFuturesc                    sz   t  d¡�\}dd„ }ˆ j | |¡‡ fdd„¡ ˆ  ¡ }ˆ  | ¡ ¡ ˆ  | ¡ d u ¡ W d   ƒ n1 sl0    Y  d S )Nr   c                   S   s   d S r$   r!   r!   r!   r!   r"   Údummyï  s    z8TestIOLoopFutures.test_add_future_threads.<locals>.dummyc                    s
   ˆ   | ¡S r$   ©r/   )Úfuturer6   r!   r"   r%   ó  r&   z;TestIOLoopFutures.test_add_future_threads.<locals>.<lambda>)	r   r   r)   Z
add_futurerÎ   r=   r>   ÚdoneÚresult)r   ÚpoolrÓ   rÕ   r!   r6   r"   Útest_add_future_threadsì  s    ÿz)TestIOLoopFutures.test_add_future_threadsc                 c   sV   t  ¡ }t  ¡ }dd„ }t ¡  d |||¡t ¡  d |||¡gV }|  ||g|¡ d S )Nc                 S   s   |   ¡  | ¡  | S r$   ©rb   r=   ©Z
self_eventZother_eventr!   r!   r"   Ú	sync_funcþ  s    z=TestIOLoopFutures.test_run_in_executor_gen.<locals>.sync_func)rF   rf   r   rÆ   rµ   rj   )r   Úevent1Úevent2rÜ   Úresr!   r!   r"   Útest_run_in_executor_genù  s    þz*TestIOLoopFutures.test_run_in_executor_genc                 #   sN   t  ¡ }t  ¡ }dd„ ‰ ‡ fdd„}|||ƒ|||ƒgV }|  ||g|¡ d S )Nc                 S   s   |   ¡  | ¡  | S r$   rÚ   rÛ   r!   r!   r"   rÜ     s    z@TestIOLoopFutures.test_run_in_executor_native.<locals>.sync_funcc                 “   s   t  ¡  d ˆ | |¡I d H S r$   )r   rÆ   rµ   rÛ   ©rÜ   r!   r"   Úasync_wrapper  s    ÿzDTestIOLoopFutures.test_run_in_executor_native.<locals>.async_wrapper)rF   rf   rj   )r   rÝ   rÞ   râ   rß   r!   rá   r"   Útest_run_in_executor_native  s    z-TestIOLoopFutures.test_run_in_executor_nativec                 #   sz   dg‰ G ‡ fdd„dt jƒ}t ¡ ‰‡fdd„}|dƒ}t ¡ }| |¡ | d |¡V  |  dˆ d ¡ |  	ˆ 
¡ ¡ d S )Nr   c                       s   e Zd Z‡ ‡fdd„Z‡  ZS )z?TestIOLoopFutures.test_set_default_executor.<locals>.MyExecutorc                    s$   ˆd  d7  < t ƒ j|g|¢R Ž S )Nr   r   )rË   rÎ   )r   Úfuncr   )rÍ   Úcountr!   r"   rÎ   *  s    zFTestIOLoopFutures.test_set_default_executor.<locals>.MyExecutor.submit)r‘   r’   r“   rÎ   rÑ   r!   )rå   rÌ   r"   Ú
MyExecutor)  s   ræ   c                      s   ˆ   ¡  d S r$   )rb   r!   )Úeventr!   r"   rÜ   0  s    z>TestIOLoopFutures.test_set_default_executor.<locals>.sync_funcr   )r   r   rF   rf   r   rÆ   Úset_default_executorrµ   rj   r>   Úis_set)r   ræ   rÜ   Úexecutorr2   r!   )rå   rç   r"   Útest_set_default_executor%  s    
z+TestIOLoopFutures.test_set_default_executorN)r‘   r’   r“   rÙ   r   rà   rã   rë   r!   r!   r!   r"   rÒ   ë  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S )ÚTestIOLoopRunSyncc                 C   s   t dd�| _d S )NFr³   )r   r)   r6   r!   r!   r"   rÂ   <  s    zTestIOLoopRunSync.setUpc                 C   s   | j  ¡  d S r$   rÃ   r6   r!   r!   r"   rÄ   ?  s    zTestIOLoopRunSync.tearDownc                 C   s@   |   tj¡�  | j dd„ ¡ W d   ƒ n1 s20    Y  d S )Nc                   S   s   dS ©Né*   r!   r!   r!   r!   r"   r%   D  r&   z4TestIOLoopRunSync.test_sync_result.<locals>.<lambda>)rT   r   ZBadYieldErrorr)   Úrun_syncr6   r!   r!   r"   Útest_sync_resultB  s    z"TestIOLoopRunSync.test_sync_resultc                 C   s>   |   t¡�  | j dd„ ¡ W d   ƒ n1 s00    Y  d S )Nc                   S   s   dd S r§   r!   r!   r!   r!   r"   r%   H  r&   z7TestIOLoopRunSync.test_sync_exception.<locals>.<lambda>)rT   ÚZeroDivisionErrorr)   rï   r6   r!   r!   r"   Útest_sync_exceptionF  s    z%TestIOLoopRunSync.test_sync_exceptionc                 C   s&   t jdd„ ƒ}|  | j |¡d¡ d S )Nc                   s   s   t jV  t  d¡‚d S rí   )r   ÚmomentÚReturnr!   r!   r!   r"   r¢   K  s    z.TestIOLoopRunSync.test_async_result.<locals>.frî   )r   rª   rj   r)   rï   ©r   r¢   r!   r!   r"   Útest_async_resultJ  s    
z#TestIOLoopRunSync.test_async_resultc                 C   sH   t jdd„ ƒ}|  t¡� | j |¡ W d   ƒ n1 s:0    Y  d S )Nc                   s   s   t jV  dd  d S r§   ©r   ró   r!   r!   r!   r"   r¢   S  s    z1TestIOLoopRunSync.test_async_exception.<locals>.f)r   rª   rT   rñ   r)   rï   rõ   r!   r!   r"   Útest_async_exceptionR  s    
z&TestIOLoopRunSync.test_async_exceptionc                    s   ‡ fdd„}ˆ j  |¡ d S )Nc                      s   ˆ   t ¡ ˆ j¡ d S r$   )r    r   rÆ   r)   r!   r6   r!   r"   r¢   \  s    z)TestIOLoopRunSync.test_current.<locals>.f)r)   rï   rõ   r!   r6   r"   Útest_current[  s    zTestIOLoopRunSync.test_currentc                 C   s(   t jdd„ ƒ}| jt| jj|dd� d S )Nc                   s   s   t  d¡V  d S r   )r   rA   r!   r!   r!   r"   r¢   b  s    z)TestIOLoopRunSync.test_timeout.<locals>.fr@   )ry   )r   rª   rT   r	   r)   rï   rõ   r!   r!   r"   Útest_timeouta  s    
zTestIOLoopRunSync.test_timeoutc                    s*   t jdd„ ƒ‰ ‡ fdd„}| j |¡ d S )Nc                   s   s   t jV  d S r$   r÷   r!   r!   r!   r"   Úf1i  s    z3TestIOLoopRunSync.test_native_coroutine.<locals>.f1c                   “   s   ˆ ƒ I d H  d S r$   r!   r!   ©rû   r!   r"   Úf2m  s    z3TestIOLoopRunSync.test_native_coroutine.<locals>.f2)r   rª   r)   rï   )r   rý   r!   rü   r"   Útest_native_coroutineh  s    
z'TestIOLoopRunSync.test_native_coroutineN)r‘   r’   r“   rÂ   rÄ   rð   rò   rö   rø   rù   rú   rþ   r!   r!   r!   r"   rì   ;  s   	rì   c                   @   sD   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S )ÚTestPeriodicCallbackMathc                 C   s<   g }d}||_ |D ]$}| |¡ | |j ¡ |j | }q|S )a9  Simulate a series of calls to the PeriodicCallback.

        Pass a list of call durations in seconds (negative values
        work to simulate clock adjustments during the call, or more or
        less equivalently, between calls). This method returns the
        times at which each call would be made.
        re   )Z_next_timeoutZ_update_nextr„   )r   ÚpcZ	durationsr   r   Údr!   r!   r"   Úsimulate_callst  s    
z'TestPeriodicCallbackMath.simulate_callsc                 C   s   d S r$   r!   r6   r!   r!   r"   rÓ   …  s    zTestPeriodicCallbackMath.dummyc                 C   s.   t | jdƒ}|  |  |dgd ¡g d¢¡ d S )Né'  r   é   ©éò  éü  é  i  é  ©r
   rÓ   rj   r  ©r   r   r!   r!   r"   Ú
test_basicˆ  s    ÿz#TestPeriodicCallbackMath.test_basicc                 C   s4   g d¢}g d¢}t | jdƒ}|  |  ||¡|¡ d S )N)é	   r  r(   é   é   r  é#   r  r   r   r   )r  r  r  r	  i.  iL  ij  i’  iº  iÄ  iÎ  r  r
  )r   Úcall_durationsÚexpectedr   r!   r!   r"   Útest_overrunŽ  s    z%TestPeriodicCallbackMath.test_overrunc                 C   sH   t | jdƒ}|  |  |g d¢¡g d¢¡ |  |  |g d¢¡g d¢¡ d S )Nr  )éþÿÿÿéÿÿÿÿéýÿÿÿr  r   r  )iœÿÿÿr   r   )r  r  r  r
  r  r!   r!   r"   Útest_clock_backwards£  s
    ÿ	z-TestPeriodicCallbackMath.test_clock_backwardsc                    s~   g d¢‰ g d¢}dgt ˆ ƒ }t| jddd�}‡ fdd„}t d	|¡�$ |  |  ||¡|¡ W d   ƒ n1 sp0    Y  d S )
N)ç      à?r   r   g      è?)r  g     ô�@r  g     E�@r   r  r  )Újitterc                      s
   ˆ   d¡S )Nr   )Úpopr!   ©Zrandom_timesr!   r"   Úmock_randomº  s    z9TestPeriodicCallbackMath.test_jitter.<locals>.mock_randomzrandom.random)Úlenr
   rÓ   r   Úpatchrj   r  )r   r  r  r   r  r!   r  r"   Útest_jitter´  s    z$TestPeriodicCallbackMath.test_jitterc                 C   s.   t dd„ tjddd�ƒ}d}|  |j|¡ d S )Nc                   S   s   d S r$   r!   r!   r!   r!   r"   r%   Á  r&   z9TestPeriodicCallbackMath.test_timedelta.<locals>.<lambda>r   é   )Úminutesr�   i8D )r
   r-   r.   rj   Zcallback_time)r   r   Zexpected_callback_timer!   r!   r"   Útest_timedeltaÀ  s    z'TestPeriodicCallbackMath.test_timedeltaN)
r‘   r’   r“   r  rÓ   r  r  r  r  r"  r!   r!   r!   r"   rÿ   s  s   rÿ   c                   @   s0   e Zd Zdd„ Zddœdd„Zddœdd„ZdS )	ÚTestPeriodicCallbackAsyncc                    sJ   d‰ d dœ‡ ‡fdd„}t |dƒ}| ¡  ˆ ¡  | ¡  ˆ ˆ d¡ d S )Nr   ©Úreturnc                      s   ˆ d7 ‰ ˆ dkrˆ  ¡  d S )Nr   r‚   rÔ   r!   ©rå   r   r!   r"   r   Ê  s    z?TestPeriodicCallbackAsync.test_periodic_plain.<locals>.callbackr(   r‚   )r
   r0   r=   r/   rj   )r   r   r   r!   r&  r"   Útest_periodic_plainÇ  s    
z-TestPeriodicCallbackAsync.test_periodic_plainNr$  c                    sb   ddg‰ t jddœ‡ ‡‡fdd„ƒ}t|dƒ‰ˆ ¡  ˆ ¡  ˆ ˆ d d¡ ˆ ˆ d d¡ d S )	Nr   z%Generator[Future[None], object, None]r$  c                   3   sR   ˆ d  d7  < t  d¡V  ˆ d  d7  < ˆ d dkrNˆ ¡  ˆj ˆj¡ d S ©Nr   r   gš™™™™™™?r‚   ©r   rA   r/   r)   r#   r!   ©Úcountsr   r   r!   r"   r   Ù  s    z>TestPeriodicCallbackAsync.test_periodic_coro.<locals>.callbackr(   r‚   r   )r   rª   r
   r0   r=   rj   r¥   r!   r*  r"   Útest_periodic_coroÖ  s    
z,TestPeriodicCallbackAsync.test_periodic_coroc                    s\   ddg‰ d dœ‡ ‡‡fdd„}t |dƒ‰ˆ ¡  ˆ ¡  ˆ ˆ d d¡ ˆ ˆ d d¡ d S )Nr   r$  c                   “   sV   ˆ d  d7  < t  d¡I d H  ˆ d  d7  < ˆ d dkrRˆ ¡  ˆj ˆj¡ d S r(  r)  r!   r*  r!   r"   r   ë  s    z?TestPeriodicCallbackAsync.test_periodic_async.<locals>.callbackr(   r‚   r   )r
   r0   r=   rj   r¥   r!   r*  r"   Útest_periodic_asyncè  s    
z-TestPeriodicCallbackAsync.test_periodic_async)r‘   r’   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d„ Zd	S )
ÚTestIOLoopConfigurationc                 G   s4   ddgt |ƒ }tjdd |¡g}tt |¡ƒ ¡ S )Nz!from tornado.ioloop import IOLoopz*classname = lambda x: x.__class__.__name__z-cz; )ÚlistÚsysÚ
executablerH   r   Ú
subprocessÚcheck_outputÚstrip)r   Z
statementsZ	stmt_listr   r!   r!   r"   Ú
run_pythonû  s    þýz"TestIOLoopConfiguration.run_pythonc                 C   s0   |   d¡}|  |d¡ |   d¡}|  |d¡ d S )Nú"print(classname(IOLoop.current()))ÚAsyncIOMainLoopzprint(classname(IOLoop()))ZAsyncIOLoop©r5  rj   ©r   Úclsr!   r!   r"   Útest_default  s    

z$TestIOLoopConfiguration.test_defaultc                 C   s   |   dd¡}|  |d¡ d S )Nz8IOLoop.configure("tornado.platform.asyncio.AsyncIOLoop")r6  r7  r8  r9  r!   r!   r"   Útest_asyncio
  s
    þz$TestIOLoopConfiguration.test_asyncioc                 C   s   |   ddd¡}|  |d¡ d S )Nz4from tornado.platform.asyncio import AsyncIOMainLoopzAsyncIOMainLoop().install()r6  r7  r8  r9  r!   r!   r"   Útest_asyncio_main  s    ýz)TestIOLoopConfiguration.test_asyncio_mainN)r‘   r’   r“   r5  r;  r<  r=  r!   r!   r!   r"   r.  ú  s   r.  Ú__main__)6r¼   Úconcurrent.futuresr   Ú
concurrentr   Úcollections.abcr   rž   r-   rn   rl   r2  r0  rF   r8   r*   Zunittestr   Ztornado.escaper   Útornador   Ztornado.ioloopr   r	   r
   Ztornado.logr   Ztornado.testingr   r   r   r   r   Ztornado.test.utilr   r   r   Ztornado.concurrentr   ÚtypingÚTYPE_CHECKINGr   r   ZTestCaserÀ   rÊ   rÒ   rì   rÿ   r#  r.  r‘   Úmainr!   r!   r!   r"   Ú<module>   sJ      "P8S4 
