o
    h\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)rd 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 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old_add_callbacktest L/var/www/vscode/kcb/lib/python3.10/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   )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)calledr1   r"   r   r"   r#   r   A   s   z5TestIOLoop.test_add_callback_wakeup.<locals>.callbackc                      s    d_ j  t _d S NF)r7   r+   r$   time
start_timer"   r   r   r"   r#   schedule_callbackE   s   z>TestIOLoop.test_add_callback_wakeup.<locals>.schedule_callback   )places)r+   r.   r:   waitassertAlmostEqualr;   
assertTruer7   )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?)r:   sleep	stop_timer+   r$   r1   r"   r8   r"   r#   targetR   s   

z@TestIOLoop.test_add_callback_wakeup_other_thread.<locals>.targetrG   皙?)
	threadingThreadr+   r$   r2   r@   r:   rF   r3   join)r   rG   threaddeltar"   r8   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/   r0   r1   r@   r8   r"   r"   r#   test_add_timeout_timedelta_   s   z%TestIOLoop.test_add_timeout_timedeltac              
   C   s~   t  \}}z,| j| dd tj | t| jj| dd tj W | j|  |	  d S | j|  |	  w )Nc                 S      d S r%   r"   fdeventsr"   r"   r#   r&   g   r'   z.TestIOLoop.test_multiple_add.<locals>.<lambda>c                 S   rR   r%   r"   rS   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   s4   t  \}}z| j|  W |  d S |  w r%   )r   r+   r[   rW   r\   r]   r"   r"   r#   test_remove_without_addv   s   
z"TestIOLoop.test_remove_without_addc                 C   s@   t   | j| j W d    n1 sw   Y  |   d S r%   )r   r+   add_callback_from_signalr1   r@   r8   r"   r"   r#   test_add_callback_from_signal   s   z(TestIOLoop.test_add_callback_from_signalc                 C   sb   t  }tj|jd}|  t  ||j W d    n1 s"w   Y  |  |  d S )NrH   )	r   rJ   rK   r2   r   rb   r1   rL   r\   )r   other_iolooprM   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all_fds)r$   r1   r2   setr\   r"   closingrd   r"   r#   rG      s   z:TestIOLoop.test_add_callback_while_closing.<locals>.targetrH     c                   S   rR   r%   r"   r"   r"   r"   r#   r&      r'   z<TestIOLoop.test_add_callback_while_closing.<locals>.<lambda>)rJ   Eventr   rK   r2   r@   ranger$   )r   rG   rM   ir"   ri   r#   test_add_callback_while_closing   s   z*TestIOLoop.test_add_callback_while_closingc                    s   t  \}}z9 fdd} j| |tj  j j d t	
|jd     j|  W |  |  d S |  |  w )Nc                    s     |tj    d S r%   )assertEqualr   rX   r1   rS   r8   r"   r#   handler   s   z5TestIOLoop.test_read_while_writeable.<locals>.handlerrD   s   asdf)socket
socketpairr+   rV   rW   r   rX   r.   r:   	functoolspartialsendr@   r[   r\   )r   clientserverrq   r"   r8   r#   test_read_while_writeable   s   
z$TestIOLoop.test_read_while_writeablec                 C   s.   | j | j  | j}|   | j | d S r%   )r+   r.   r:   r1   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   rR   r%   r"   r"   r"   r"   r#   r&      r'   z8TestIOLoop.test_remove_timeout_cleanup.<locals>.<lambda>c                      s    j  jS r%   r+   r$   r1   r"   r8   r"   r#   r&      s    )rm   r+   r.   r:   rz   r$   r@   )r   rn   timeoutr"   r8   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+   rz   r"   r   r   	t2_handler"   r#   t1   s   z7TestIOLoop.test_remove_timeout_from_timeout.<locals>.t1rD   c                      s   d d< d S )NTr   r"   r"   r   r"   r#   t2      z7TestIOLoop.test_remove_timeout_from_timeout.<locals>.t2g{Gz?gQ?T)r+   r:   r.   r1   rE   r@   rp   )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.   r:   appendr/   r0   call_at
call_laterr1   r@   rp   sorted)r   resultsr"   r"   r#   test_timeout_with_arguments   s   z&TestIOLoop.test_timeout_with_argumentsc                 C   6   | j | j  dd }| |d u  | j | d S )Nc                   S   rR   r%   r"   r"   r"   r"   r#   r&      r'   z4TestIOLoop.test_add_timeout_return.<locals>.<lambda>)r+   r.   r:   assertFalserz   r{   r"   r"   r#   test_add_timeout_return   s   z"TestIOLoop.test_add_timeout_returnc                 C   r   )Nc                   S   rR   r%   r"   r"   r"   r"   r#   r&      r'   z0TestIOLoop.test_call_at_return.<locals>.<lambda>)r+   r   r:   r   rz   r{   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   rR   r%   r"   r"   r"   r"   r#   r&      r'   z3TestIOLoop.test_call_later_return.<locals>.<lambda>)r+   r   r   rz   r{   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 r9   )sockobjclosed)r   r   r"   r"   r#   __init__  s   
zATestIOLoop.test_close_file_object.<locals>.SocketWrapper.__init__c                 S   s
   | j  S r%   )r   rW   r8   r"   r"   r#   rW        
z?TestIOLoop.test_close_file_object.<locals>.SocketWrapper.filenoc                 S   s   d| _ | j  d S r6   )r   r   r\   r8   r"   r"   r#   r\     s   z>TestIOLoop.test_close_file_object.<locals>.SocketWrapper.closeN)__name__
__module____qualname__r   rW   r\   r"   r"   r"   r#   SocketWrapper  s    r   c                 S   rR   r%   r"   rS   r"   r"   r#   r&     r'   z3TestIOLoop.test_close_file_object.<locals>.<lambda>Trf   N)objectr   r   rV   rX   r\   rB   r   )r   r   r   r_   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 s5w   Y  j
 j |tj tt }|d|f 	  W d   n1 shw   Y   d   d   j
    dS )z>The handler callback receives the same fd object it passed in.c                    s*     |   \}}|    d S r%   )r   acceptr\   r1   )rT   rU   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+   rV   r   rX   
contextlibrj   rr   connectr@   r[   rW   assertIsrp   r\   )r   r_   r   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 s-w   Y  | j|  |	  d S )Nc                 S   rR   r%   r"   rS   r"   r"   r#   f;     z+TestIOLoop.test_mixed_fd_fileobj.<locals>.f)
r   r+   rV   r   rX   rY   rZ   rW   r[   r\   )r   r   r_   r   r"   r"   r#   test_mixed_fd_fileobj8  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y   d d< Y nw   d S r   )r+   r2   rZ   r1   r"   got_exceptionreturned_from_startr   r"   r#   r   K  s   
z+TestIOLoop.test_reentrant.<locals>.callbackr   N)r+   r$   r@   rB   r   r   r   r"   r   r#   test_reentrantF  s   zTestIOLoop.test_reentrantc                 C   sV   | j dd  | j | j ttd |   W d   dS 1 s$w   Y  dS )z-Uncaught exceptions get logged by the IOLoop.c                   S      dd S Nr   r   r"   r"   r"   r"   r#   r&   Z      z3TestIOLoop.test_exception_logging.<locals>.<lambda>Exception in callbackN)r+   r$   r1   r   r   r@   r8   r"   r"   r#   test_exception_loggingX  s
   
"z!TestIOLoop.test_exception_loggingc                    sV   t j fdd} j| ttd    W d   dS 1 s$w   Y  dS )z:The IOLoop examines exceptions from Futures and logs them.c                      s    j  j dd  d S r   r~   r"   r8   r"   r#   r   b  s   z:TestIOLoop.test_exception_logging_future.<locals>.callbackr   N)r   	coroutiner+   r$   r   r   r@   r   r"   r8   r#   test_exception_logging_future_  s   
"z(TestIOLoop.test_exception_logging_futurec                    sP    fdd} j | ttd    W d   dS 1 s!w   Y  dS )z=The IOLoop examines exceptions from awaitables and logs them.c                      s"    j  j j j dd  d S r   r~   r"   r8   r"   r#   r   n  s   z?TestIOLoop.test_exception_logging_native_coro.<locals>.callbackr   N)r+   r$   r   r   r@   r   r"   r8   r#   "test_exception_logging_native_corok  s
   
"z-TestIOLoop.test_exception_logging_native_coroc                 C   s   | j dd  | j | j ttd |   W d    n1 s#w   Y  | j dd  | j | j ttd |   W d    d S 1 sLw   Y  d S )Nc                   S   r   r   r"   r"   r"   r"   r#   r&   {  r   z0TestIOLoop.test_spawn_callback.<locals>.<lambda>r   c                   S   r   r   r"   r"   r"   r"   r#   r&     r   )r+   r$   r1   r   r   r@   spawn_callbackr8   r"   r"   r#   test_spawn_callbackx  s   

"zTestIOLoop.test_spawn_callbackc                    s   t  \zGd d g   fdd}j|jj j|jj jdj   	 dg W 
  
  d S 
  
  w )Ns   abcc                    s8     | d | u rj d S j d S )Ni   )r   recvr+   r[   rS   chunksrw   r   rx   r"   r#   handle_read  s   z@TestIOLoop.test_remove_handler_from_handler.<locals>.handle_readrI   )rr   rs   rv   r+   rV   rX   r   r1   r@   rp   r\   )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)rm   r   r\   )rn   r4   r"   r"   r#   r     s   

z*TestIOLoop.test_init_close_race.<locals>.fc                    s   g | ]	}j d  qS r%   )r+   run_in_executor).0rn   r   r   r"   r#   
<listcomp>  s    z3TestIOLoop.test_init_close_race.<locals>.<listcomp>r>   )r   multirm   r8   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 s'w   Y  |  d S )NF)asyncio_loopr   )asyncionew_event_loopr   r   rY   RuntimeErrorr\   )r   r   r4   r"   r"   r#   test_explicit_asyncio_loop  s   z%TestIOLoop.test_explicit_asyncio_loopN)"r   r   r   r5   r   rC   rO   rQ   r`   ra   rc   re   ro   r   ry   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   r   )   sF    

	

r   c                   @   ,   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_currentr8   r"   r"   r#   setUp  s   zTestIOLoopCurrent.setUpc                 C   s   | j d ur| j   d S d S r%   r+   r\   r8   r"   r"   r#   tearDown  s   
zTestIOLoopCurrent.tearDownc                    st   t dd _ t jdd tdD ]$} 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+   r1   r"   r8   r"   r#   r     s   
z-TestIOLoopCurrent.test_non_current.<locals>.f)	r   r+   assertIsNoner   rm   r$   r2   r   r   )r   rn   r   r"   r8   r#   test_non_current  s   
z"TestIOLoopCurrent.test_non_currentc                 C   s"   t dd| _| | jt   d S )NTr   )r   r+   r   r   r8   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   r8   	__class__r"   r#   r     s   
zTestIOLoopCurrentAsync.setUpc                 c   s>    t d}|tjV  W d    d S 1 sw   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                    s|   t d/}dd } j|| fdd   } |   | d u  W d    d S 1 s7w   Y  d S )Nr   c                   S   rR   r%   r"   r"   r"   r"   r#   dummy  r   z8TestIOLoopFutures.test_add_future_threads.<locals>.dummyc                    s
     | S r%   r1   )futurer8   r"   r#   r&     s   
 z;TestIOLoopFutures.test_add_future_threads.<locals>.<lambda>)	r   r   r+   
add_futurer   r@   rB   doneresult)r   poolr   r   r"   r8   r#   test_add_future_threads  s   "z)TestIOLoopFutures.test_add_future_threadsc                 c   sX    t  }t  }dd }t d |||t d |||gV }| ||g| d S )Nc                 S      |    |  | S r%   rh   r@   
self_eventother_eventr"   r"   r#   	sync_func  s   z=TestIOLoopFutures.test_run_in_executor_gen.<locals>.sync_func)rJ   rl   r   r   r   rp   )r   event1event2r   resr"   r"   r#   test_run_in_executor_gen  s   z*TestIOLoopFutures.test_run_in_executor_genc                 #   sP    t  }t  }dd   fdd}||||||gV }| ||g| d S )Nc                 S   r   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)rJ   rl   rp   )r   r   r   r   r   r"   r   r#   test_run_in_executor_native  s   z-TestIOLoopFutures.test_run_in_executor_nativec                 #   s|    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%   )rh   r"   )eventr"   r#   r   3  r   z>TestIOLoopFutures.test_set_default_executor.<locals>.sync_funcr   )r   r   rJ   rl   r   r   set_default_executorr   rp   rB   is_set)r   r   r   executorr4   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+   r8   r"   r"   r#   r   ?  s   zTestIOLoopRunSync.setUpc                 C   s   | j   d S r%   r   r8   r"   r"   r#   r   B  s   zTestIOLoopRunSync.tearDownc                 C   sB   |  tj | jdd  W d    d S 1 sw   Y  d S )Nc                   S   s   dS N*   r"   r"   r"   r"   r#   r&   G  r'   z4TestIOLoopRunSync.test_sync_result.<locals>.<lambda>)rY   r   BadYieldErrorr+   run_syncr8   r"   r"   r#   test_sync_resultE  s   "z"TestIOLoopRunSync.test_sync_resultc                 C   s@   |  t | jdd  W d    d S 1 sw   Y  d S )Nc                   S   r   r   r"   r"   r"   r"   r#   r&   K  r   z7TestIOLoopRunSync.test_sync_exception.<locals>.<lambda>)rY   ZeroDivisionErrorr+   r  r8   r"   r"   r#   test_sync_exceptionI  s   "z%TestIOLoopRunSync.test_sync_exceptionc                 C   s&   t jdd }| | j|d d S )Nc                   s   s    t jV  t dr  )r   momentReturnr"   r"   r"   r#   r   N  s   
z.TestIOLoopRunSync.test_async_result.<locals>.fr  )r   r   rp   r+   r  r   r   r"   r"   r#   test_async_resultM  s   
z#TestIOLoopRunSync.test_async_resultc                 C   sJ   t jdd }| t | j| W d    d S 1 sw   Y  d S )Nc                   s   s    t jV  dd  d S r   r   r  r"   r"   r"   r#   r   V  s   z1TestIOLoopRunSync.test_async_exception.<locals>.f)r   r   rY   r
  r+   r  r  r"   r"   r#   test_async_exceptionU  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"   r8   r"   r#   r   _  s   z)TestIOLoopRunSync.test_current.<locals>.f)r+   r  r  r"   r8   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   rE   r"   r"   r"   r#   r   e  s   z)TestIOLoopRunSync.test_timeout.<locals>.frD   )r   )r   r   rY   r	   r+   r  r  r"   r"   r#   test_timeoutd  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#   f1l  s   z3TestIOLoopRunSync.test_native_coroutine.<locals>.f1c                      s     I d H  d S r%   r"   r"   r  r"   r#   f2p  s   z3TestIOLoopRunSync.test_native_coroutine.<locals>.f2)r   r   r+   r  )r   r  r"   r  r#   test_native_coroutinek  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.
        rk   )_next_timeout_update_nextr   )r   pc	durationsr   r   dr"   r"   r#   simulate_callsw  s   
z'TestPeriodicCallbackMath.simulate_callsc                 C   rR   r%   r"   r8   r"   r"   r#   r     r   zTestPeriodicCallbackMath.dummyc                 C   s.   t | jd}| | |dgd g d d S )N'  r            i    r
   r   rp   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  )r0  r   r!  )ir   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    d S 1 s9w   Y  d S )
N)      ?r   r   g      ?)r"  g     @r$  g     E@r   r  r4  )jitterc                      s
     dS )Nr   )popr"   random_timesr"   r#   mock_random  r   z9TestPeriodicCallbackMath.test_jitter.<locals>.mock_randomzrandom.random)lenr
   r   r   patchrp   r  )r   r.  r-  r  r9  r"   r7  r#   test_jitter  s   "z$TestPeriodicCallbackMath.test_jitterc                 C   s.   t dd tjddd}d}| |j| d S )Nc                   S   rR   r%   r"   r"   r"   r"   r#   r&     r'   z9TestPeriodicCallbackMath.test_timedelta.<locals>.<lambda>r      )minutesr   i8D )r
   r/   r0   rp   callback_time)r   r  expected_callback_timer"   r"   r#   test_timedelta  s   z'TestPeriodicCallbackMath.test_timedeltaN)
r   r   r   r  r   r(  r/  r3  r<  rA  r"   r"   r"   r#   r  v  s    r  c                   @   s(   e Zd Zdd Zd	ddZd	ddZdS )
TestPeriodicCallbackAsyncc                    sF   d d fdd}t |d}|    |   d d S )Nr   returnc                      s     d7   dkr   d S d S )Nr   r   r   r"   r   r   r"   r#   r     s   z?TestPeriodicCallbackAsync.test_periodic_plain.<locals>.callbackr*   r   rC  N)r
   r2   r@   r1   rp   )r   r   r  r"   rD  r#   test_periodic_plain  s   
z-TestPeriodicCallbackAsync.test_periodic_plainrC  Nc                    s^   ddg t jd	 fdd}t|d     d d  d d d S )
Nr   rC  %Generator[Future[None], object, None]c                   3   sX     d  d7  < t dV   d  d7  <  d dkr*  jj d S d S Nr   r   g?r   r   rE   r1   r+   r$   r"   countsr  r   r"   r#   r     s   z>TestPeriodicCallbackAsync.test_periodic_coro.<locals>.callbackr*   r   r   )rC  rG  )r   r   r
   r2   r@   rp   r   r"   rJ  r#   test_periodic_coro  s   
z,TestPeriodicCallbackAsync.test_periodic_coroc                    sX   ddg d fdd}t |d     d d  d d d S )	Nr   rC  c                      s\    d  d7  < t dI d H   d  d7  <  d dkr,  jj d S d S rH  rI  r"   rJ  r"   r#   r     s   z?TestPeriodicCallbackAsync.test_periodic_async.<locals>.callbackr*   r   r   rE  )r
   r2   r@   rp   r   r"   rJ  r#   test_periodic_async  s   
z-TestPeriodicCallbackAsync.test_periodic_asyncrE  )r   r   r   rF  rL  rM  r"   r"   r"   r#   rB    s    
rB  c                   @   r   )
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
executablerL   r   
subprocesscheck_outputstrip)r   
statements	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()))AsyncIOLooprW  rp   r   clsr"   r"   r#   test_default  s   

z$TestIOLoopConfiguration.test_defaultc                 C   s   |  dd}| |d d S )Nz8IOLoop.configure("tornado.platform.asyncio.AsyncIOLoop")rX  rY  r[  r\  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()rX  rY  r[  r\  r"   r"   r#   test_asyncio_main  s   z)TestIOLoopConfiguration.test_asyncio_mainN)r   r   r   rW  r^  r_  r`  r"   r"   r"   r#   rN    s
    rN  __main__)6r   concurrent.futuresr   
concurrentr   collections.abcr   r   r/   rt   rr   rR  rP  rJ   r:   r,   unittestr   tornado.escaper   tornador   tornado.ioloopr   r	   r
   tornado.logr   tornado.testingr   r   r   r   r   tornado.test.utilr   r   r   tornado.concurrentr   typingTYPE_CHECKINGr   r   TestCaser   r   r   r  r  rB  rN  r   mainr"   r"   r"   r#   <module>   sN       "P8S4 