o
    Eh:m                     @   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$ 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+Z/G dd dej+Z0G dd deZ1G dd dej+Z2e3dkre4  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)Future)Listc                   @   s   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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 Y/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/tornado/test/ioloop_test.pyadd_callback1   s   zBTestIOLoop.test_add_callback_return_sequence.<locals>.add_callbackc                   S   s   i S Nr!   r!   r!   r!   r"   <lambda>6       z>TestIOLoop.test_add_callback_return_sequence.<locals>.<lambda>c                   S   s   g S r$   r!   r!   r!   r!   r"   r%   7   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)calledr0   r!   r   r!   r"   r   ?   s   z5TestIOLoop.test_add_callback_wakeup.<locals>.callbackc                      s    d_ j  t _d S NF)r6   r*   r#   time
start_timer!   r   r   r!   r"   schedule_callbackC   s   z>TestIOLoop.test_add_callback_wakeup.<locals>.schedule_callback   )places)r*   r-   r9   waitassertAlmostEqualr:   
assertTruer6   )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?)r9   sleep	stop_timer*   r#   r0   r!   r7   r!   r"   targetO   s   

z@TestIOLoop.test_add_callback_wakeup_other_thread.<locals>.targetrF   皙?)
	threadingThreadr*   r#   r1   r?   r9   rE   r2   join)r   rF   threaddeltar!   r7   r"   %test_add_callback_wakeup_other_threadN   s   z0TestIOLoop.test_add_callback_wakeup_other_threadc                 C   s$   | j tjdd| j |   d S )Nr   )microseconds)r*   r-   r.   r/   r0   r?   r7   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%   d   r&   z.TestIOLoop.test_multiple_add.<locals>.<lambda>c                 S   rQ   r$   r!   rR   r!   r!   r"   r%   l   r&   )
r   r*   add_handlerfilenor   READassertRaises	Exceptionremove_handlercloser   sockportr!   r!   r"   test_multiple_add`   s    

zTestIOLoop.test_multiple_addc                 C   s4   t  \}}z| j|  W |  d S |  w r$   )r   r*   rZ   rV   r[   r\   r!   r!   r"   test_remove_without_adds   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_signalr0   r?   r7   r!   r!   r"   test_add_callback_from_signal|   s   z(TestIOLoop.test_add_callback_from_signalc                 C   sf   t dd}tj|jd}|  t  ||j W d    n1 s$w   Y  |  |  d S )NFmake_currentrG   )	r   rI   rJ   r1   r   ra   r0   rK   r[   )r   other_iolooprL   r!   r!   r"   *test_add_callback_from_signal_other_thread   s   
z5TestIOLoop.test_add_callback_from_signal_other_threadc                    s\   t    fdd}tddt j|d}|     tdD ]	}dd  q"d S )	Nc                      s,    j      jdd d S )NTall_fds)r#   r0   r1   setr[   r!   closingre   r!   r"   rF      s   z:TestIOLoop.test_add_callback_while_closing.<locals>.targetFrc   rG     c                   S   rQ   r$   r!   r!   r!   r!   r"   r%      r&   z<TestIOLoop.test_add_callback_while_closing.<locals>.<lambda>)rI   Eventr   rJ   r1   r?   ranger#   )r   rF   rL   ir!   rj   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   rW   r0   rR   r7   r!   r"   handler   s   z5TestIOLoop.test_read_while_writeable.<locals>.handlerrC   s   asdf)socket
socketpairr*   rU   rV   r   rW   r-   r9   	functoolspartialsendr?   rZ   r[   )r   clientserverrr   r!   r7   r"   test_read_while_writeable   s   
z$TestIOLoop.test_read_while_writeablec                 C   s.   | j | j  | j}|   | j | d S r$   )r*   r-   r9   r0   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   rQ   r$   r!   r!   r!   r!   r"   r%      r&   z8TestIOLoop.test_remove_timeout_cleanup.<locals>.<lambda>c                      s    j  jS r$   r*   r#   r0   r!   r7   r!   r"   r%      s    )rn   r*   r-   r9   r{   r#   r?   )r   ro   timeoutr!   r7   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*   r{   r!   r   r   	t2_handler!   r"   t1   s   z7TestIOLoop.test_remove_timeout_from_timeout.<locals>.t1rC   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*   r9   r-   r0   rD   r?   rq   )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-   r9   appendr.   r/   call_at
call_laterr0   r?   rq   sorted)r   resultsr!   r!   r"   test_timeout_with_arguments   s   z&TestIOLoop.test_timeout_with_argumentsc                 C   2   | j | j  dd }| | | j | d S )Nc                   S   rQ   r$   r!   r!   r!   r!   r"   r%      r&   z4TestIOLoop.test_add_timeout_return.<locals>.<lambda>)r*   r-   r9   assertIsNotNoner{   r|   r!   r!   r"   test_add_timeout_return   s   
z"TestIOLoop.test_add_timeout_returnc                 C   r   )Nc                   S   rQ   r$   r!   r!   r!   r!   r"   r%      r&   z0TestIOLoop.test_call_at_return.<locals>.<lambda>)r*   r   r9   r   r{   r|   r!   r!   r"   test_call_at_return   s   
zTestIOLoop.test_call_at_returnc                 C   s,   | j ddd }| | | j | d S )Nr   c                   S   rQ   r$   r!   r!   r!   r!   r"   r%      r&   z3TestIOLoop.test_call_later_return.<locals>.<lambda>)r*   r   r   r{   r|   r!   r!   r"   test_call_later_return   s   
z!TestIOLoop.test_call_later_returnc                    sZ   G dd d}t  \}}||tdd   fdd  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 r8   )sockobjclosed)r   r   r!   r!   r"   __init__
  s   
zATestIOLoop.test_close_file_object.<locals>.SocketWrapper.__init__c                 S   s
   | j  S r$   )r   rV   r7   r!   r!   r"   rV        
z?TestIOLoop.test_close_file_object.<locals>.SocketWrapper.filenoc                 S   s   d| _ | j  d S r5   )r   r   r[   r7   r!   r!   r"   r[     s   z>TestIOLoop.test_close_file_object.<locals>.SocketWrapper.closeN)__name__
__module____qualname__r   rV   r[   r!   r!   r!   r"   SocketWrapper	  s    r   Frc   c                      s     dd tjS )Nc                 S   rQ   r$   r!   rR   r!   r!   r"   r%     r&   zETestIOLoop.test_close_file_object.<locals>.<lambda>.<locals>.<lambda>)rU   r   rW   r!   r*   socket_wrapperr!   r"   r%     s    z3TestIOLoop.test_close_file_object.<locals>.<lambda>Trg   N)r   r   run_syncr[   rA   r   )r   r   r   r^   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[   r0   )rS   rT   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*   rU   r   rW   
contextlibrk   rs   connectr?   rZ   rV   assertIsrq   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   rQ   r$   r!   rR   r!   r!   r"   f<     z+TestIOLoop.test_mixed_fd_fileobj.<locals>.f)
r   r*   rU   r   rW   rX   rY   rV   rZ   r[   )r   r   r^   r   r!   r!   r"   test_mixed_fd_fileobj9  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*   r1   rY   r0   r!   got_exceptionreturned_from_startr   r!   r"   r   L  s   
z+TestIOLoop.test_reentrant.<locals>.callbackr   N)r*   r#   r?   rA   assertFalser   r   r!   r   r"   test_reentrantG  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%   [      z3TestIOLoop.test_exception_logging.<locals>.<lambda>Exception in callbackN)r*   r#   r0   r   r   r?   r7   r!   r!   r"   test_exception_loggingY  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!   r7   r!   r"   r   c  s   z:TestIOLoop.test_exception_logging_future.<locals>.callbackr   N)r   	coroutiner*   r#   r   r   r?   r   r!   r7   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!   r7   r!   r"   r   o  s   z?TestIOLoop.test_exception_logging_native_coro.<locals>.callbackr   N)r*   r#   r   r   r?   r   r!   r7   r"   "test_exception_logging_native_corol  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#   r0   r   r   r?   spawn_callbackr7   r!   r!   r"   test_spawn_callbacky  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*   rZ   rR   chunksrx   r   ry   r!   r"   handle_read  s   z@TestIOLoop.test_remove_handler_from_handler.<locals>.handle_readrH   )rs   rt   rw   r*   rU   rW   r   r0   r?   rq   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)   Frc   )rn   r   r[   )ro   r3   r!   r!   r"   r     s   

z*TestIOLoop.test_init_close_race.<locals>.fc                    s   g | ]	}j d  qS r$   )r*   run_in_executor).0ro   r   r   r!   r"   
<listcomp>  s    z3TestIOLoop.test_init_close_race.<locals>.<listcomp>r=   )r   multirn   r7   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_looprd   )asyncionew_event_loopr   r   rX   RuntimeErrorr[   )r   r   r3   r!   r!   r"   test_explicit_asyncio_loop  s   z%TestIOLoop.test_explicit_asyncio_loopN)!r   r   r   r4   rB   rN   rP   r_   r`   rb   rf   rp   r   rz   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r!   r!   r"   r   (   sB    	
 
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_currentr7   r!   r!   r"   setUp  s   zTestIOLoopCurrent.setUpc                 C   s   | j d ur| j   d S d S r$   r*   r[   r7   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 )NFrc   )instancer   c                      s&   t   _ jd usJ  j  d S r$   )r   currentcurrent_io_loopr*   r0   r!   r7   r!   r"   r     s   
z-TestIOLoopCurrent.test_non_current.<locals>.f)	r   r*   assertIsNoner   rn   r#   r1   r   r   )r   ro   r   r!   r7   r"   test_non_current  s   
z"TestIOLoopCurrent.test_non_currentc                 C   s"   t dd| _| | jt   d S )NTrc   )r   r*   r   r   r7   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   r7   	__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                    sx   t d-}dd } j|| fdd   } |   |	  W d    d S 1 s5w   Y  d S )Nr   c                   S   rQ   r$   r!   r!   r!   r!   r"   dummy  r   z8TestIOLoopFutures.test_add_future_threads.<locals>.dummyc                    s
     | S r$   r0   )futurer7   r!   r"   r%     s   
 z;TestIOLoopFutures.test_add_future_threads.<locals>.<lambda>)
r   r   r*   
add_futurer   r?   rA   doner   result)r   poolr   r   r!   r7   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$   ri   r?   
self_eventother_eventr!   r!   r"   	sync_func  s   z=TestIOLoopFutures.test_run_in_executor_gen.<locals>.sync_func)rI   rm   r   r   r   rq   )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)rI   rm   rq   )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$   )ri   r!   )eventr!   r"   r   4  r   z>TestIOLoopFutures.test_set_default_executor.<locals>.sync_funcr   )r   r   rI   rm   r   r   set_default_executorr   rq   rA   is_set)r   r   r   executorr3   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                   @   s\   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d ZdS )TestIOLoopRunSyncc                 C   s   t dd| _d S )NFrc   )r   r*   r7   r!   r!   r"   r   @  s   zTestIOLoopRunSync.setUpc                 C   s   | j   d S r$   r   r7   r!   r!   r"   r   C  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%   H  r&   z4TestIOLoopRunSync.test_sync_result.<locals>.<lambda>)rX   r   BadYieldErrorr*   r   r7   r!   r!   r"   test_sync_resultF  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%   L  r   z7TestIOLoopRunSync.test_sync_exception.<locals>.<lambda>)rX   ZeroDivisionErrorr*   r   r7   r!   r!   r"   test_sync_exceptionJ  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   O  s   
z.TestIOLoopRunSync.test_async_result.<locals>.fr  )r   r   rq   r*   r   r   r   r!   r!   r"   test_async_resultN  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   W  s   z1TestIOLoopRunSync.test_async_exception.<locals>.f)r   r   rX   r	  r*   r   r  r!   r!   r"   test_async_exceptionV  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!   r7   r!   r"   r   `  s   z)TestIOLoopRunSync.test_current.<locals>.f)r*   r   r  r!   r7   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   rD   r!   r!   r!   r"   r   f  s   z)TestIOLoopRunSync.test_timeout.<locals>.frC   )r   )r   r   rX   r	   r*   r   r  r!   r!   r"   test_timeoute  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"   f1m  s   z3TestIOLoopRunSync.test_native_coroutine.<locals>.f1c                      s     I d H  d S r$   r!   r!   r  r!   r"   f2q  s   z3TestIOLoopRunSync.test_native_coroutine.<locals>.f2)r   r   r*   r   )r   r  r!   r  r"   test_native_coroutinel  s   
z'TestIOLoopRunSync.test_native_coroutinec                 C   sT   dd }|  t}| j| W d    n1 sw   Y  dt|jv s(J d S )Nc                      s2   t dI d H  t   t dI d H  d S )NrH   r)   )r   rD   r   r   r0   r!   r!   r!   r"   r   w  s   z1TestIOLoopRunSync.test_stop_no_timeout.<locals>.fzEvent loop stopped)rX   r   r*   r   str	exception)r   r   cmr!   r!   r"   test_stop_no_timeoutv  s
   z&TestIOLoopRunSync.test_stop_no_timeoutN)r   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.
        rl   )_next_timeout_update_nextr   )r   pc	durationsr   r   dr!   r!   r"   simulate_calls  s   
z'TestPeriodicCallbackMath.simulate_callsc                 C   rQ   r$   r!   r7   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   rq   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"  )r3  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"  r7  )jitterc                      s
     dS )Nr   )popr!   random_timesr!   r"   mock_random  r   z9TestPeriodicCallbackMath.test_jitter.<locals>.mock_randomzrandom.random)lenr
   r   r   patchrq   r!  )r   r1  r0  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   rQ   r$   r!   r!   r!   r!   r"   r%     r&   z9TestPeriodicCallbackMath.test_timedelta.<locals>.<lambda>r      )minutesr   i8D )r
   r.   r/   rq   callback_time)r   r  expected_callback_timer!   r!   r"   test_timedelta  s   z'TestPeriodicCallbackMath.test_timedeltaN)
r   r   r   r!  r   r+  r2  r6  r?  rD  r!   r!   r!   r"   r    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   rF  N)r
   r1   r?   r0   rq   )r   r   r  r!   rG  r"   test_periodic_plain  s   
z-TestPeriodicCallbackAsync.test_periodic_plainrF  Nc                    s^   ddg t jd	 fdd}t|d     d d  d d d S )
Nr   rF  %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   rD   r0   r*   r#   r!   countsr  r   r!   r"   r     s   z>TestPeriodicCallbackAsync.test_periodic_coro.<locals>.callbackr)   r   r   )rF  rJ  )r   r   r
   r1   r?   rq   r   r!   rM  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   rF  c                      s\    d  d7  < t dI d H   d  d7  <  d dkr,  jj d S d S rK  rL  r!   rM  r!   r"   r     s   z?TestPeriodicCallbackAsync.test_periodic_async.<locals>.callbackr)   r   r   rH  )r
   r1   r?   rq   r   r!   rM  r"   test_periodic_async  s   
z-TestPeriodicCallbackAsync.test_periodic_asyncrH  )r   r   r   rI  rO  rP  r!   r!   r!   r"   rE    s    
rE  c                   @   s>   e Zd Zdd Zdd Zdd Zeej	dkd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
executablerK   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rZ  rq   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_asyncio)r      z*implicit event loop creation not availablec                 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   rZ  ra  rb  unittestskipIfrS  version_inford  r!   r!   r!   r"   rQ    s    
rQ  __main__)5r   concurrent.futuresr   
concurrentr   collections.abcr   r   r.   ru   rs   rU  rS  rI   r9   r+   re  r   tornado.escaper   tornador   tornado.ioloopr   r	   r
   tornado.logr   tornado.testingr   r   r   r   r   tornado.test.utilr   r   tornado.concurrentr   typingTYPE_CHECKINGr   r   TestCaser   r   r   r  r  rE  rQ  r   mainr!   r!   r!   r"   <module>   sN       "PBS4#