U
    luf[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re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  _  |f|| d S N   calls)selfcallbackargskwargsZold_add_callbacktest </tmp/pip-unpacked-wheel-bmg6zs32/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{Gz?)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   sh   t  \}}z>| j| dd tj | t	| jj| dd tj W 5 | j|  |  X 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)   remove_handlerfilenocloseadd_handlerr   READassertRaises	Exceptionr   sockportr!   r!   r"   test_multiple_addc   s     
  zTestIOLoop.test_multiple_addc                 C   s.   t  \}}z| j|  W 5 |  X d S r$   )r   rS   r)   rQ   rR   rX   r!   r!   r"   test_remove_without_addv   s    
z"TestIOLoop.test_remove_without_addc              	   C   s,   t   | j| j W 5 Q R X |   d S r$   )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   sN   t  }tj|jd}|  t  ||j W 5 Q R X |  |  d S )NrD   )	r   rF   rG   r0   r   r]   r/   rH   rS   )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   setrS   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  \}}z` fdd} j| |tj  j j	 d t
|jd     j|  W 5 |  |  X d S )Nc                    s     |tj    d S r$   )assertEqualr   rU   r/   rN   r6   r!   r"   handler   s    z5TestIOLoop.test_read_while_writeable.<locals>.handlerr@   s   asdf)socket
socketpairrS   r)   rT   rR   r   rU   r,   r8   	functoolspartialsendr=   rQ   )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{Gz?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|ddddg d S )Nr   r   )secondsr<         )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 k | 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 k | 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 k | 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 )zWhen 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   rS   r6   r!   r!   r"   rS     s    z>TestIOLoop.test_close_file_object.<locals>.SocketWrapper.closeN)__name__
__module____qualname__r   rR   rS   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   rT   rU   rS   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 5 Q R X j
 j |tj tt }|d|f 	  W 5 Q R X  d   d   j
    dS )z>The handler callback receives the same fd object it passed in.c                    s*     |   \}}|    d S r$   )r   acceptrS   r/   )rO   rP   connaddrZfdsr   server_sockr!   r"   handle_connection#  s    
zGTestIOLoop.test_handler_callback_file_object.<locals>.handle_connectionz	127.0.0.1r   r   N)r   r)   rT   r   rU   
contextlibrd   rl   connectr=   rQ   rR   assertIsrj   rS   )r   rZ   r   Zclient_sockr!   r   r"   !test_handler_callback_file_object  s     
z,TestIOLoop.test_handler_callback_file_objectc              	   C   sl   t  \}}dd }| j||tj | t | j| |tj W 5 Q R X | j|  |	  d S )Nc                 S   s   d S r$   r!   rN   r!   r!   r"   f:  s    z+TestIOLoop.test_mixed_fd_fileobj.<locals>.f)
r   r)   rT   r   rU   rV   rW   rR   rQ   rS   )r   r   rZ   r   r!   r!   r"   test_mixed_fd_fileobj7  s    
 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k
r2   d d< Y nX   d S r{   )r)   r0   rW   r/   r!   Zgot_exceptionZreturned_from_startr   r!   r"   r   J  s    
z+TestIOLoop.test_reentrant.<locals>.callbackr   N)r)   r#   r=   r>   r   r   r   r!   r   r"   test_reentrantE  s    zTestIOLoop.test_reentrantc              	   C   s@   | j dd  | j | j ttd |   W 5 Q R X dS )z-Uncaught exceptions get logged by the IOLoop.c                   S   s   dd S Nr   r   r!   r!   r!   r!   r"   r%   Y  r&   z3TestIOLoop.test_exception_logging.<locals>.<lambda>Exception in callbackN)r)   r#   r/   r   r   r=   r6   r!   r!   r"   test_exception_loggingW  s    z!TestIOLoop.test_exception_loggingc              	      s@   t j fdd} j| ttd    W 5 Q R X 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   a  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              	      s:    fdd} j | ttd    W 5 Q R X 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   m  s    z?TestIOLoop.test_exception_logging_native_coro.<locals>.callbackr   N)r)   r#   r   r   r=   r   r!   r6   r"   "test_exception_logging_native_coroj  s    z-TestIOLoop.test_exception_logging_native_coroc              	   C   s|   | j dd  | j | j ttd |   W 5 Q R X | j dd  | j | j ttd |   W 5 Q R X d S )Nc                   S   s   dd S r   r!   r!   r!   r!   r"   r%   z  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_callbackw  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 5     X d S )Ns   abcc                    s6     | d | kr&j nj d S )Ni   )r   recvr)   rQ   rN   chunksrq   r   rr   r!   r"   handle_read  s    z@TestIOLoop.test_remove_handler_from_handler.<locals>.handle_readrE   )rl   rm   rS   rp   r)   rT   rU   r   r/   r=   rj   )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   rS   )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>  s     z3TestIOLoop.test_init_close_race.<locals>.<listcomp>r<   )r   Zmultirg   r6   r!   r   r"   test_init_close_race  s    zTestIOLoop.test_init_close_racec              	   C   sP   t  }t|dd}|j|ks"t| t t|dd W 5 Q R X |  d S )NF)asyncio_loopr   )asyncioZnew_event_loopr   r   AssertionErrorrV   RuntimeErrorrS   )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 k	r| j   d S r$   r)   rS   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 k	st j  d S r$   )r   currentcurrent_io_loopr)   r   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 5 Q R X 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              	      sf   t dR}dd } j|| fdd   } |   | d k W 5 Q R X 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|f| 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   2  s    z>TestIOLoopFutures.test_set_default_executor.<locals>.sync_funcr   )r   r   rF   rf   r   r   Z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   A  s    zTestIOLoopRunSync.tearDownc              	   C   s,   |  tj | jdd  W 5 Q R X d S )Nc                   S   s   dS N*   r!   r!   r!   r!   r"   r%   F  r&   z4TestIOLoopRunSync.test_sync_result.<locals>.<lambda>)rV   r   ZBadYieldErrorr)   run_syncr6   r!   r!   r"   test_sync_resultD  s    z"TestIOLoopRunSync.test_sync_resultc              	   C   s*   |  t | jdd  W 5 Q R X d S )Nc                   S   s   dd S r   r!   r!   r!   r!   r"   r%   J  r&   z7TestIOLoopRunSync.test_sync_exception.<locals>.<lambda>)rV   ZeroDivisionErrorr)   r   r6   r!   r!   r"   test_sync_exceptionH  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   momentZReturnr!   r!   r!   r"   r   M  s    z.TestIOLoopRunSync.test_async_result.<locals>.fr   )r   r   rj   r)   r   r   r   r!   r!   r"   test_async_resultL  s    
z#TestIOLoopRunSync.test_async_resultc              	   C   s4   t jdd }| t | j| W 5 Q R X d S )Nc                   s   s   t jV  dd  d S r   r   r   r!   r!   r!   r"   r   U  s    z1TestIOLoopRunSync.test_async_exception.<locals>.f)r   r   rV   r   r)   r   r   r!   r!   r"   test_async_exceptionT  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   d  s    z)TestIOLoopRunSync.test_timeout.<locals>.fr@   )ry   )r   r   rV   r	   r)   r   r   r!   r!   r"   test_timeoutc  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"   f1k  s    z3TestIOLoopRunSync.test_native_coroutine.<locals>.f1c                      s     I d H  d S r$   r!   r!   r   r!   r"   f2o  s    z3TestIOLoopRunSync.test_native_coroutine.<locals>.f2)r   r   r)   r   )r   r   r!   r   r"   test_native_coroutinej  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_callsv  s    
z'TestPeriodicCallbackMath.simulate_callsc                 C   s   d S r$   r!   r6   r!   r!   r"   r     s    zTestPeriodicCallbackMath.dummyc                 C   s4   t | jd}| | |dgd dddddg d S )	N'  r                r
   r   rj   r   r   r   r!   r!   r"   
test_basic  s
     z#TestPeriodicCallbackMath.test_basicc                 C   sX   dddddddddddg}ddd	d
dddddddg}t | jd}| | ||| d S )N	   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   sX   t | jd}| | |dddddgdddd	d
g | | |dddgdddg d S )Nr   r   r  r  r  r  r  ir  r  r!   r!   r"   test_clock_backwards  s     	z-TestPeriodicCallbackMath.test_clock_backwardsc              	      sr   ddddg ddddg}dgt   }t| jd	dd
} fdd}td| | | ||| W 5 Q R X d S )Ng      ?r   r   g      ?r  g     @r  g     E@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   u  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r0  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")r1  r2  r3  r4  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()r1  r2  r3  r4  r!   r!   r"   test_asyncio_main  s    z)TestIOLoopConfiguration.test_asyncio_mainN)r   r   r   r0  r6  r7  r8  r!   r!   r!   r"   r)    s   r)  __main__)6r   concurrent.futuresr   Z
concurrentr   collections.abcr   r   r-   rn   rl   r-  r+  rF   r8   r*   Zunittestr   Ztornado.escaper   Z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 
