o
    ~îj: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(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+ƒZ/G dd„ dej+ƒZ0G dd„ deƒZ1G dd„ dej+ƒZ2e3dkr×e 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© úb/raid/bulk_storage/M2HATS/real-image-venv/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ç{®Gáz„?)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{®Gáz”?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 )
z¦When a file object is used instead of a numeric file descriptor,
        the object should be closed (by IOLoop.close(all_fds=True),
        not just the fd.
        c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )z8TestIOLoop.test_close_file_object.<locals>.SocketWrapperc                 S   s   || _ d| _d S 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$  )iœÿÿÿr   r   )r%  r&  r'  r)  r*  r!   r!   r"   Útest_clock_backwards±  s
   ÿ 	z-TestPeriodicCallbackMath.test_clock_backwardsc                    s€   g d¢‰ g d¢}dgt ˆ ƒ }t| jddd�}‡ fdd„}t d	|¡� |  |  ||¡|¡ W d   ƒ 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#ÿ