o
    )Zh\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Zold_add_callbacktest O/var/www/html/lang_env/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   )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   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   rN   r$   r!   rO   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)   rW   rS   rX   rY   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_signalr/   r=   r6   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 )NrD   )	r   rF   rG   r0   r   r^   r/   rH   rX   )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   setrX   r!   closingr`   r!   r"   rC      s   z:TestIOLoop.test_add_callback_while_closing.<locals>.targetrD     c                   S   rN   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!   rd   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   rT   r/   rO   r6   r!   r"   handler   s   z5TestIOLoop.test_read_while_writeable.<locals>.handlerr@   s   asdf)socket
socketpairr)   rR   rS   r   rT   r,   r8   	functoolspartialsendr=   rW   rX   )r   clientserverrl   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   rN   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%      s    )rh   r)   r,   r8   ru   r#   r=   )r   ri   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)   ru   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      z7TestIOLoop.test_remove_timeout_from_timeout.<locals>.t2g{Gz?gQ?T)r)   r8   r,   r/   rA   r=   rk   )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=   rk   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   rN   r$   r!   r!   r!   r!   r"   r%      r&   z4TestIOLoop.test_add_timeout_return.<locals>.<lambda>)r)   r,   r8   assertFalseru   rv   r!   r!   r"   test_add_timeout_return   s   z"TestIOLoop.test_add_timeout_returnc                 C   r   )Nc                   S   rN   r$   r!   r!   r!   r!   r"   r%      r&   z0TestIOLoop.test_call_at_return.<locals>.<lambda>)r)   r   r8   r   ru   rv   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   rN   r$   r!   r!   r!   r!   r"   r%      r&   z3TestIOLoop.test_call_later_return.<locals>.<lambda>)r)   r   r   ru   rv   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   rS   r6   r!   r!   r"   rS        
z?TestIOLoop.test_close_file_object.<locals>.SocketWrapper.filenoc                 S   s   d| _ | j  d S r4   )r   r   rX   r6   r!   r!   r"   rX     s   z>TestIOLoop.test_close_file_object.<locals>.SocketWrapper.closeN)__name__
__module____qualname__r   rS   rX   r!   r!   r!   r"   SocketWrapper  s    r   c                 S   rN   r$   r!   rO   r!   r!   r"   r%     r&   z3TestIOLoop.test_close_file_object.<locals>.<lambda>Trb   N)objectr   r   rR   rT   rX   r>   r   )r   r   r   r[   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 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   acceptrX   r/   )rP   rQ   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)   rR   r   rT   
contextlibre   rm   connectr=   rW   rS   assertIsrk   rX   )r   r[   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 s-w   Y  | j|  |	  d S )Nc                 S   rN   r$   r!   rO   r!   r!   r"   f;     z+TestIOLoop.test_mixed_fd_fileobj.<locals>.f)
r   r)   rR   r   rT   rU   rV   rS   rW   rX   )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)   r0   rV   r/   r!   Zgot_exceptionZreturned_from_startr   r!   r"   r   K  s   
z+TestIOLoop.test_reentrant.<locals>.callbackr   N)r)   r#   r=   r>   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#   r/   r   r   r=   r6   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   ry   r!   r6   r!   r"   r   b  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                    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   ry   r!   r6   r!   r"   r   n  s   z?TestIOLoop.test_exception_logging_native_coro.<locals>.callbackr   N)r)   r#   r   r   r=   r   r!   r6   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#   r/   r   r   r=   Zspawn_callbackr6   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)   rW   rO   chunksrr   r   rs   r!   r"   handle_read  s   z@TestIOLoop.test_remove_handler_from_handler.<locals>.handle_readrE   )rm   rn   rq   r)   rR   rT   r   r/   r=   rk   rX   )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)rh   r   rX   )ri   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).0ri   r   r   r!   r"   
<listcomp>  s    z3TestIOLoop.test_init_close_race.<locals>.<listcomp>r<   )r   multirh   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 s'w   Y  |  d S )NF)asyncio_loopr   )asyncioZnew_event_loopr   r   rU   RuntimeErrorrX   )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_   ra   rj   r   rt   rx   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_currentr6   r!   r!   r"   setUp  s   zTestIOLoopCurrent.setUpc                 C   s   | j d ur| j   d S d S r$   r)   rX   r6   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)   r/   r!   r6   r!   r"   r     s   
z-TestIOLoopCurrent.test_non_current.<locals>.f)	r   r)   ZassertIsNoner   rh   r#   r0   r   r   )r   ri   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    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   rN   r$   r!   r!   r!   r!   r"   dummy  r   z8TestIOLoopFutures.test_add_future_threads.<locals>.dummyc                    s
     | S r$   r/   )futurer6   r!   r"   r%     s   
 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   sX    t  }t  }dd }t d |||t d |||gV }| ||g| d S )Nc                 S      |    |  | S r$   rc   r=   Z
self_eventZother_eventr!   r!   r"   	sync_func  s   z=TestIOLoopFutures.test_run_in_executor_gen.<locals>.sync_func)rF   rg   r   r   r   rk   )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)rF   rg   rk   )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$   )rc   r!   )eventr!   r"   r   3  r   z>TestIOLoopFutures.test_set_default_executor.<locals>.sync_funcr   )r   r   rF   rg   r   r   Zset_default_executorr   rk   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   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>)rU   r   ZBadYieldErrorr)   run_syncr6   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>)rU   ZeroDivisionErrorr)   r   r6   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   rk   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   rU   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!   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   e  s   z)TestIOLoopRunSync.test_timeout.<locals>.fr@   )rz   )r   r   rU   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.
        rf   )Z_next_timeoutZ_update_nextr   )r   pcZ	durationsr   r   dr!   r!   r"   simulate_callsw  s   
z'TestPeriodicCallbackMath.simulate_callsc                 C   rN   r$   r!   r6   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   rk   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  )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
  r  )Zjitterc                      s
     dS )Nr   )popr!   Zrandom_timesr!   r"   mock_random  r   z9TestPeriodicCallbackMath.test_jitter.<locals>.mock_randomzrandom.random)lenr
   r   r   patchrk   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   rN   r$   r!   r!   r!   r!   r"   r%     r&   z9TestPeriodicCallbackMath.test_timedelta.<locals>.<lambda>r      )minutesr   i8D )r
   r-   r.   rk   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  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   r*  N)r
   r0   r=   r/   rk   )r   r   r  r!   r+  r"   test_periodic_plain  s   
z-TestPeriodicCallbackAsync.test_periodic_plainr*  Nc                    s^   ddg t jd	 fdd}t|d     d d  d d d S )
Nr   r*  %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   rA   r/   r)   r#   r!   countsr  r   r!   r"   r     s   z>TestPeriodicCallbackAsync.test_periodic_coro.<locals>.callbackr(   r   r   )r*  r.  )r   r   r
   r0   r=   rk   r   r!   r1  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   r*  c                      s\    d  d7  < t dI d H   d  d7  <  d dkr,  jj d S d S r/  r0  r!   r1  r!   r"   r     s   z?TestPeriodicCallbackAsync.test_periodic_async.<locals>.callbackr(   r   r   r,  )r
   r0   r=   rk   r   r!   r1  r"   test_periodic_async  s   
z-TestPeriodicCallbackAsync.test_periodic_asyncr,  )r   r   r   r-  r3  r4  r!   r!   r!   r"   r)    s    
r)  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
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r<  rk   r   clsr!   r!   r"   test_default  s   

z$TestIOLoopConfiguration.test_defaultc                 C   s   |  dd}| |d d S )Nz8IOLoop.configure("tornado.platform.asyncio.AsyncIOLoop")r=  r>  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()r=  r>  r?  r@  r!   r!   r"   test_asyncio_main  s   z)TestIOLoopConfiguration.test_asyncio_mainN)r   r   r   r<  rB  rC  rD  r!   r!   r!   r"   r5    s
    r5  __main__)6r   concurrent.futuresr   Z
concurrentr   collections.abcr   r   r-   ro   rm   r9  r7  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)  r5  r   mainr!   r!   r!   r"   <module>   sN       "P8S4 