
    R?g\k                     f   S SK r S SKJr  S SKJr  S SKJr  S SKrS SKrS SK	r	S SK
r
S SKrS SKrS SKrS SKrS SKrS SKJr  S SKrS SKJr  S SKJr  S SKJrJrJr  S S	KJr  S S
KJrJrJrJ r J!r!  S SK"J#r#J$r$J%r%  S SK&J'r'  S SK(r(\(RR                  (       a  S SK(J*r*   " S S\5      r+ " S S\RX                  5      r- " S S\5      r. " S S\5      r/ " S S\RX                  5      r0 " S S\RX                  5      r1 " S S\5      r2 " S S\RX                  5      r3\4S:X  a  \Rj                  " 5         gg)    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                       \ rS rSrS r\S 5       r\S 5       rS rS r	S r
S rS	 rS
 r\S 5       rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS r\S 5       r\\ S 5       5       r!S r"Sr#g) 
TestIOLoop)   c                   ^^ SU l         U R                  nU mUR                  mUU4S jn[        R                  " X!5      Ul        UR                  S 5        UR                  S 5        UR                  [        R                  " SS9UR                  5        UR                  5         U R                  U R                   S5        g )Nr   c                 H   > T=R                   S-  sl         T" U/UQ70 UD6  g N   calls)selfcallbackargskwargsold_add_callbacktests       :/usr/lib/python3/dist-packages/tornado/test/ioloop_test.pyadd_callbackBTestIOLoop.test_add_callback_return_sequence.<locals>.add_callback2   s"    JJ!OJX777    c                      0 $ N r,   r)   r&   <lambda>>TestIOLoop.test_add_callback_return_sequence.<locals>.<lambda>7       "r)   c                      / $ r+   r,   r,   r)   r&   r-   r.   8   r/   r)   2   )milliseconds
   )r   io_loopr'   types
MethodTypeadd_timeoutdatetime	timedeltastopstart
assertLess)r    loopr'   r$   r%   s      @@r&   !test_add_callback_return_sequence,TestIOLoop.test_add_callback_return_sequence*   s    
||,,	8 ",,\@*%*%++<diiH



B'r)   c                 <  ^ ^ U 4S jmUU 4S jnT R                   R                  T R                   R                  5       U5        T R                  5         T R	                  [        R                  " 5       T R
                  SS9  T R                  T R                  5        g )Nc                  4   > ST l         T R                  5         g NT)calledr:   r    s   r&   r!   5TestIOLoop.test_add_callback_wakeup.<locals>.callbackA   s    DKIIKr)   c                  ~   > STl         TR                  R                  T 5        [        R                  " 5       Tl        g NF)rC   r4   r'   time
start_time)r!   r    s   r&   schedule_callback>TestIOLoop.test_add_callback_wakeup.<locals>.schedule_callbackE   s)    DKLL%%h/"iikDOr)      )places)r4   r7   rH   waitassertAlmostEqualrI   
assertTruerC   )r    rJ   r!   s   ` @r&   test_add_callback_wakeup#TestIOLoop.test_add_callback_wakeup=   si    		* 	  !2!2!46GH		tyy{DOOAF$r)   c                 ,  ^  U 4S jn[         R                  " US9nT R                  R                  UR                  5        T R                  5         [        R                  " 5       T R                  -
  nT R                  US5        UR                  5         g )Nc                     > [         R                  " S5        [         R                   " 5       T l        T R                  R	                  T R
                  5        g )N{Gz?)rH   sleep	stop_timer4   r'   r:   rD   s   r&   target@TestIOLoop.test_add_callback_wakeup_other_thread.<locals>.targetR   s2    JJt!YY[DNLL%%dii0r)   rX   皙?)
	threadingThreadr4   r'   r;   rN   rH   rW   r<   join)r    rX   threaddeltas   `   r&   %test_add_callback_wakeup_other_thread0TestIOLoop.test_add_callback_wakeup_other_threadP   sd    	1 !!0!!&,,/				dnn,s#r)   c                     U R                   R                  [        R                  " SS9U R                  5        U R                  5         g )Nr   )microseconds)r4   r7   r8   r9   r:   rN   rD   s    r&   test_add_timeout_timedelta%TestIOLoop.test_add_timeout_timedelta_   s.      !3!3!CTYYO		r)   c                    [        5       u  p U R                  R                  UR                  5       S [        R
                  5        U R                  [        U R                  R                  UR                  5       S [        R
                  5        U R                  R                  UR                  5       5        UR                  5         g ! U R                  R                  UR                  5       5        UR                  5         f = f)Nc                     g r+   r,   fdeventss     r&   r-   .TestIOLoop.test_multiple_add.<locals>.<lambda>g   s    $r)   c                     g r+   r,   ri   s     r&   r-   rl   o   s    4r)   )
r   r4   add_handlerfilenor	   READassertRaises	Exceptionremove_handlercloser    sockports      r&   test_multiple_addTestIOLoop.test_multiple_addc   s    %'
	LL$$6
 ((' LL''6JJL LL''6JJLs   BC ;Dc                     [        5       u  p U R                  R                  UR                  5       5        UR	                  5         g ! UR	                  5         f = fr+   )r   r4   rs   ro   rt   ru   s      r&   test_remove_without_add"TestIOLoop.test_remove_without_addv   s>     &'
	LL''6JJLDJJLs   )A Ac                     [        5          U R                  R                  U R                  5        S S S 5        U R	                  5         g ! , (       d  f       N= fr+   )r   r4   add_callback_from_signalr:   rN   rD   s    r&   test_add_callback_from_signal(TestIOLoop.test_add_callback_from_signal   s7      !LL11$))< "		 "!s   &A


Ac                 2   [        5       n[        R                  " UR                  S9nUR                  5         [	        5          UR                  UR                  5        S S S 5        UR                  5         UR                  5         g ! , (       d  f       N/= f)NrZ   )	r	   r\   r]   r;   r   r~   r:   r^   rt   )r    other_ioloopr_   s      r&   *test_add_callback_from_signal_other_thread5TestIOLoop.test_add_callback_from_signal_other_thread   sg     x!!););<!11,2C2CD " "!s   B
Bc                   ^^ [         R                  " 5       mUU4S jn[        5       m[         R                  " US9nUR	                  5         TR                  5         [        S5       H  nTR                  S 5        M     g )Nc                     > TR                  TR                  5        TR                  5         T R                  5         TR	                  SS9  g )NTall_fds)r'   r:   r;   setrt   )closingr   s   r&   rX   :TestIOLoop.test_add_callback_while_closing.<locals>.target   s=    %%l&7&78 KKMt,r)   rZ     c                      g r+   r,   r,   r)   r&   r-   <TestIOLoop.test_add_callback_while_closing.<locals>.<lambda>   s    dr)   )r\   Eventr	   r]   r;   rN   ranger'   )r    rX   r_   ir   r   s       @@r&   test_add_callback_while_closing*TestIOLoop.test_add_callback_while_closing   s]     //#	- x!!0tA%%l3 r)   c                 \  ^  [         R                  " 5       u  p U 4S jnT R                  R                  UR	                  5       U[
        R                  5        T R                  R                  T R                  R                  5       S-   [        R                  " UR                  S5      5        T R                  5         T R                  R                  UR	                  5       5        UR                  5         UR                  5         g ! UR                  5         UR                  5         f = f)Nc                 f   > TR                  U[        R                  5        TR                  5         g r+   )assertEqualr	   rp   r:   )rj   rk   r    s     r&   handler5TestIOLoop.test_read_while_writeable.<locals>.handler   s       5		r)   rU   s   asdf)socket
socketpairr4   rn   ro   r	   rp   r7   rH   	functoolspartialsendrN   rs   rt   )r    clientserverr   s   `   r&   test_read_while_writeable$TestIOLoop.test_read_while_writeable   s    
  **,	 LL$$V]]_gv{{KLL$$!!#d*I,=,=fkk7,S IIKLL''8LLNLLN LLNLLNs   CD	 	"D+c                     U R                   R                  U R                   R                  5       U R                  5      nU R	                  5         U R                   R                  U5        g r+   )r4   r7   rH   r:   rN   remove_timeoutr    handles     r&   test_remove_timeout_after_fire)TestIOLoop.test_remove_timeout_after_fire   sD    ))$,,*;*;*=tyyI		##F+r)   c                 .  ^  [        S5       HV  nT R                  R                  T R                  R                  5       S-   S 5      nT R                  R	                  U5        MX     T R                  R                  U 4S j5        T R                  5         g )Ni  i  c                      g r+   r,   r,   r)   r&   r-   8TestIOLoop.test_remove_timeout_cleanup.<locals>.<lambda>   s    SWr)   c                  N   > T R                   R                  T R                  5      $ r+   r4   r'   r:   rD   s   r&   r-   r      s    $,,*C*CDII*Nr)   )r   r4   r7   rH   r   r'   rN   )r    r   timeouts   `  r&   test_remove_timeout_cleanup&TestIOLoop.test_remove_timeout_cleanup   sk     tAll..t||/@/@/BT/I<XGLL''0  	!!"NO		r)   c                   ^ ^^ SS/mT R                   R                  5       nUU U4S jnT R                   R                  US-   U5        U4S jnT R                   R                  US-   U5      mT R                   R                  US-   T R                  5        [        R                  " S5        T R                  5         T R                  TSS/5        g )NFc                  F   > ST S'   TR                   R                  T5        g NTr   )r4   r   )r   r    	t2_handles   r&   t17TestIOLoop.test_remove_timeout_from_timeout.<locals>.t1   s    E!HLL''	2r)   rU   c                     > ST S'   g )NTr   r,   r   s   r&   t27TestIOLoop.test_remove_timeout_from_timeout.<locals>.t2   s    E!Hr)   g{Gz?gQ?T)r4   rH   r7   r:   rV   rN   r   )r    nowr   r   r   r   s   `   @@r&    test_remove_timeout_from_timeout+TestIOLoop.test_remove_timeout_from_timeout   s    
 ll!	3 	  tR0	 LL,,S4Z<	  tTYY7

4		u.r)   c                 j   / nU R                   R                  U R                   R                  5       UR                  S5        U R                   R                  [        R
                  " SS9UR                  S5        U R                   R                  U R                   R                  5       UR                  S5        U R                   R                  SUR                  S5        U R                   R                  SU R                  5        U R                  5         U R                  [        U5      / SQ5        g )Nr   r   )secondsrL         )r   rL   r   r   )r4   r7   rH   appendr8   r9   call_at
call_laterr:   rN   r   sorted)r    resultss     r&   test_timeout_with_arguments&TestIOLoop.test_timeout_with_arguments   s      !2!2!4gnnaH  !3!3A!>PQRT\\..0'..!D7>>15499-		 	,7r)   c                     U R                   R                  U R                   R                  5       S 5      nU R                  US L 5        U R                   R	                  U5        g )Nc                      g r+   r,   r,   r)   r&   r-   4TestIOLoop.test_add_timeout_return.<locals>.<lambda>   s    tr)   )r4   r7   rH   assertFalser   r   s     r&   test_add_timeout_return"TestIOLoop.test_add_timeout_return   sK     ))$,,*;*;*=|L4(##F+r)   c                     U R                   R                  U R                   R                  5       S 5      nU R                  US L 5        U R                   R	                  U5        g )Nc                      g r+   r,   r,   r)   r&   r-   0TestIOLoop.test_call_at_return.<locals>.<lambda>   s    4r)   )r4   r   rH   r   r   r   s     r&   test_call_at_returnTestIOLoop.test_call_at_return   sI    %%dll&7&7&9<H4(##F+r)   c                     U R                   R                  SS 5      nU R                  US L 5        U R                   R                  U5        g )Nr   c                      g r+   r,   r,   r)   r&   r-   3TestIOLoop.test_call_later_return.<locals>.<lambda>   s    Dr)   )r4   r   r   r   r   s     r&   test_call_later_return!TestIOLoop.test_call_later_return   s<    ((L94(##F+r)   c                      " S S[         5      n[        5       u  p#U" U5      n[        5       nUR                  US [        R                  5        UR                  SS9  U R                  UR                  5        g)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                   &    \ rS rSrS rS rS rSrg)8TestIOLoop.test_close_file_object.<locals>.SocketWrapperi  c                     Xl         SU l        g rG   )sockobjclosed)r    r   s     r&   __init__ATestIOLoop.test_close_file_object.<locals>.SocketWrapper.__init__  s    &#r)   c                 6    U R                   R                  5       $ r+   )r   ro   rD   s    r&   ro   ?TestIOLoop.test_close_file_object.<locals>.SocketWrapper.fileno  s    ||**,,r)   c                 F    SU l         U R                  R                  5         g rB   )r   r   rt   rD   s    r&   rt   >TestIOLoop.test_close_file_object.<locals>.SocketWrapper.close  s    """$r)   )r   r   N)__name__
__module____qualname____firstlineno__r   ro   rt   __static_attributes__r,   r)   r&   SocketWrapperr     s    $-%r)   r   c                     g r+   r,   ri   s     r&   r-   3TestIOLoop.test_close_file_object.<locals>.<lambda>  s    tr)   Tr   N)objectr   r	   rn   rp   rt   rP   r   )r    r   r   rw   socket_wrapperr4   s         r&   test_close_file_object!TestIOLoop.test_close_file_object  sb    
	%F 
	% )*&w/(N,CV[[Qd#--.r)   c                   ^ ^^ [        5       u  mn/ mUU U4S jnT R                  R                  TU[        R                  5        [
        R                  " [        R                  " 5       5       nUR                  SU45        T R                  5         SSS5        T R                  R                  T5        T R                  R                  TR                  5       U[        R                  5        [
        R                  " [        R                  " 5       5       nUR                  SU45        T R                  5         SSS5        T R                  TS   T5        T R                  TS   TR                  5       5        T R                  R                  TR                  5       5        TR                  5         g! , (       d  f       GN*= f! , (       d  f       N= f)z>The handler callback receives the same fd object it passed in.c                    > TR                  U 5        TR                  5       u  p#UR                  5         TR                  5         g r+   )r   acceptrt   r:   )rj   rk   connaddrfdsr    server_socks       r&   handle_connectionGTestIOLoop.test_handler_callback_file_object.<locals>.handle_connection$  s0    JJrN$++-JDJJLIIKr)   z	127.0.0.1Nr   r   )r   r4   rn   r	   rp   
contextlibr   r   connectrN   rs   ro   assertIsr   rt   )r    rw   r   client_sockr   r   s   `   @@r&   !test_handler_callback_file_object,TestIOLoop.test_handler_callback_file_object  sG   ,.T	 	  .?M0Kd 34IIK 1 	##K0  !3!3!57H&++V0Kd 34IIK 1 	c!fk*Q!3!3!56##K$6$6$89 10
 10s   /$F6$G6
G
Gc                    [        5       u  pS nU R                  R                  X[        R                  5        U R                  [        5         U R                  R                  UR                  5       U[        R                  5        S S S 5        U R                  R                  UR                  5       5        UR                  5         g ! , (       d  f       NH= f)Nc                     g r+   r,   ri   s     r&   f+TestIOLoop.test_mixed_fd_fileobj.<locals>.f;  s    r)   )
r   r4   rn   r	   rp   rq   rr   ro   rs   rt   )r    r   rw   r   s       r&   test_mixed_fd_fileobj TestIOLoop.test_mixed_fd_fileobj8  s    ,.	 	  =y) LL$$[%7%7%91fkkJ * 	##K$6$6$89 *)s   :C
Cc                    ^ ^^ S/mS/mUUU 4S jnT R                   R                  U5        T R                  5         T R                  TS   5        T R	                  TS   5        g)z:Calling start() twice should raise an error, not deadlock.Fc                     >  TR                   R                  5         STS'   TR                  5         g ! [         a    ST S'    N"f = fr   )r4   r;   rr   r:   )got_exceptionreturned_from_startr    s   r&   r!   +TestIOLoop.test_reentrant.<locals>.callbackK  sI    (""$)-#A& IIK  (#'a (s   3 AAr   N)r4   r'   rN   rP   r   )r    r!   r  r  s   ` @@r&   test_reentrantTestIOLoop.test_reentrantF  sY    $g	 	!!(+		a(),Q/0r)   c                     U R                   R                  S 5        U R                   R                  U R                  5        [        [        S5         U R                  5         SSS5        g! , (       d  f       g= f)z-Uncaught exceptions get logged by the IOLoop.c                      SS-  $ Nr   r   r,   r,   r)   r&   r-   3TestIOLoop.test_exception_logging.<locals>.<lambda>Z      !a%r)   Exception in callbackN)r4   r'   r:   r   r   rN   rD   s    r&   test_exception_logging!TestIOLoop.test_exception_loggingX  sJ    !!-0!!$)),w 78IIK 988s   A,,
A:c                    ^  [         R                  U 4S j5       nT R                  R                  U5        [	        [
        S5         T R                  5         SSS5        g! , (       d  f       g= f)z:The IOLoop examines exceptions from Futures and logs them.c                  Z   > T R                   R                  T R                  5        SS-    g r  r   rD   s   r&   r!   :TestIOLoop.test_exception_logging_future.<locals>.callbackb  s     LL%%dii0Er)   r  N)r   	coroutiner4   r'   r   r   rN   r    r!   s   ` r&   test_exception_logging_future(TestIOLoop.test_exception_logging_future_  sO     
	 
	 	!!(+w 78IIK 988s   A  
A.c                    ^  U 4S jnT R                   R                  U5        [        [        S5         T R	                  5         SSS5        g! , (       d  f       g= f)z=The IOLoop examines exceptions from awaitables and logs them.c                     >#    T R                   R                  T R                   R                  T R                  5        SS-    g 7fr  r   rD   s   r&   r!   ?TestIOLoop.test_exception_logging_native_coro.<locals>.callbackn  s1      LL%%dll&?&?KEs   AAr  N)r4   r'   r   r   rN   r  s   ` r&   "test_exception_logging_native_coro-TestIOLoop.test_exception_logging_native_corok  s<    	 	!!(+w 78IIK 988s   A
Ac                    U R                   R                  S 5        U R                   R                  U R                  5        [        [        S5         U R                  5         S S S 5        U R                   R                  S 5        U R                   R                  U R                  5        [        [        S5         U R                  5         S S S 5        g ! , (       d  f       Ny= f! , (       d  f       g = f)Nc                      SS-  $ r  r,   r,   r)   r&   r-   0TestIOLoop.test_spawn_callback.<locals>.<lambda>{  r  r)   r  c                      SS-  $ r  r,   r,   r)   r&   r-   r!    s    AEr)   )r4   r'   r:   r   r   rN   spawn_callbackrD   s    r&   test_spawn_callbackTestIOLoop.test_spawn_callbackx  s     	!!-0!!$)),w 78IIK 9 	##M2!!$)),w 78IIK 98 98 98s   C<C'
C$'
C5c                 x  ^ ^^^ [         R                  " 5       u  mm TR                  S5        TR                  S5        / mUUU U4S jnT R                  R	                  TUT R                  R
                  5        T R                  R	                  TUT R                  R
                  5        T R                  R                  ST R                  5        T R                  5         T R                  TS/5        TR                  5         TR                  5         g ! TR                  5         TR                  5         f = f)Ns   abcc                    > TR                  U R                  S5      5        U TL a  TR                  R                  T5        g TR                  R                  T5        g )Ni   )r   recvr4   rs   )rj   rk   chunksr   r    r   s     r&   handle_read@TestIOLoop.test_remove_handler_from_handler.<locals>.handle_read  sB    bggdm,<LL//7LL//7r)   r[   )r   r   r   r4   rn   rp   r   r:   rN   r   rt   )r    r*  r)  r   r   s   ` @@@r&    test_remove_handler_from_handler+TestIOLoop.test_remove_handler_from_handler  s      **,	KKKK F8 8 LL$$V[$,,:K:KLLL$$V[$,,:K:KLLL##C3IIK VfX.LLNLLN LLNLLNs   CD "D9c           	   #      #    S n[         R                  " [        S5       Vs/ s H  o R                  R	                  S U5      PM      sn5      v   g s  snf 7f)Nc                  Z    [        S5       H  n [        SS9nUR                  5         M     g )Nr3   Fmake_current)r   r	   rt   )r   r=   s     r&   r   *TestIOLoop.test_init_close_race.<locals>.f  s#    2Y51

 r)   rL   )r   multir   r4   run_in_executor)r    r   r   s      r&   test_init_close_raceTestIOLoop.test_init_close_race  s@     	
 iiaQ155dA>QRRQs   !A%AAc                     [         R                  " 5       n[        USS9nUR                  UL d   eU R	                  [
        5         [        USS9  S S S 5        UR                  5         g ! , (       d  f       N= f)NF)asyncio_loopr1  )asyncionew_event_loopr	   r8  rq   RuntimeErrorrt   )r    r8  r=   s      r&   test_explicit_asyncio_loop%TestIOLoop.test_explicit_asyncio_loop  sa    --/<eD  L000|,5A - 	

 -,s   A**
A8)rC   r   rI   rW   N)$r   r   r   r   r>   r   rQ   ra   re   rx   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r	  r  r  r  r$  r,  r   r5  r<  r   r,   r)   r&   r   r   )   s    (& % %$  &
4&  *,/.8,,
,
/821$
  8 S  Sr)   r   c                   ,    \ rS rSrS rS rS rS rSrg)TestIOLoopCurrenti  c                 d    [        U [        5       5        S U l        [        R                  " 5         g r+   )r   r   r4   r	   clear_currentrD   s    r&   setUpTestIOLoopCurrent.setUp  s#    "4);)=>r)   c                 T    U R                   b  U R                   R                  5         g g r+   r4   rt   rD   s    r&   tearDownTestIOLoopCurrent.tearDown  s!    <<#LL  $r)   c                   ^  [        SS9T l        T R                  [         R                  " SS95        [	        S5       H  nU 4S jnT R                  R                  U5        T R                  R                  5         T R                  T R                  T R                  5        T R                  [         R                  " SS95        M     g )NFr0  )instancer   c                     > [         R                  " 5       T l        T R                  c   eT R                  R	                  5         g r+   )r	   currentcurrent_io_loopr4   r:   rD   s   r&   r   -TestIOLoopCurrent.test_non_current.<locals>.f  s2    '-~~'7$||///!!#r)   )	r	   r4   assertIsNonerK  r   r'   r;   r   rL  )r    r   r   s   `  r&   test_non_current"TestIOLoopCurrent.test_non_current  s    51&..%89 qA$
 LL%%a(LL MM$..=fnne<= r)   c                 ~    [        SS9U l        U R                  U R                  [         R                  " 5       5        g )NTr0  )r	   r4   r   rK  rD   s    r&   test_force_current$TestIOLoopCurrent.test_force_current  s'    40dllFNN$45r)   )rL  r4   N)	r   r   r   r   rB  rF  rO  rR  r   r,   r)   r&   r?  r?    s    
!>&6r)   r?  c                   8   ^  \ rS rSrU 4S jr\S 5       rSrU =r$ )TestIOLoopCurrentAsynci  c                 J   > [         TU ]  5         [        U [        5       5        g r+   )superrB  r   r   )r    	__class__s    r&   rB  TestIOLoopCurrentAsync.setUp  s    "4);)=>r)   c              #      #    [        S5       nUR                  [        R                  5      v   S S S 5        g ! , (       d  f       g = f7fr   )r   submitr	   rA  )r    es     r&   test_clear_without_current1TestIOLoopCurrentAsync.test_clear_without_current  s1     
  "a((6//00 #""s   A
"9	A

AA
r,   )	r   r   r   r   rB  r   r]  r   __classcell__)rX  s   @r&   rU  rU    s    ? 1 1r)   rU  c                   J    \ rS rSrS r\S 5       r\S 5       r\S 5       rSr	g)TestIOLoopFuturesi  c                 j  ^  [         R                  " S5       nS nT R                  R                  UR	                  U5      U 4S j5        T R                  5       nT R                  UR                  5       5        T R                  UR                  5       S L 5        S S S 5        g ! , (       d  f       g = f)Nr   c                      g r+   r,   r,   r)   r&   dummy8TestIOLoopFutures.test_add_future_threads.<locals>.dummy  s    r)   c                 &   > TR                  U 5      $ r+   r:   )futurer    s    r&   r-   ;TestIOLoopFutures.test_add_future_threads.<locals>.<lambda>  s    499V3Dr)   )	r   r   r4   
add_futurer[  rN   rP   doneresult)r    poolrd  rh  s   `   r&   test_add_future_threads)TestIOLoopFutures.test_add_future_threads  s}    ''*d LL##E"$D YY[FOOFKKM*OOFMMOt34 +**s   BB$$
B2c              #   (  #    [         R                  " 5       n[         R                  " 5       nS n[        R                  " 5       R	                  S X1U5      [        R                  " 5       R	                  S X2U5      /v nU R                  X/U5        g 7f)Nc                 F    U R                  5         UR                  5         U $ r+   r   rN   
self_eventother_events     r&   	sync_func=TestIOLoopFutures.test_run_in_executor_gen.<locals>.sync_func  s     NN r)   )r\   r   r	   rK  r4  r   )r    event1event2rv  ress        r&   test_run_in_executor_gen*TestIOLoopFutures.test_run_in_executor_gen  sw     ""	 NN,,T9fMNN,,T9fM
 

 	&)3/s   BBc              #      ^#    [         R                  " 5       n[         R                  " 5       nS mU4S jnU" X5      U" X!5      /v nU R                  X/U5        g 7f)Nc                 F    U R                  5         UR                  5         U $ r+   rr  rs  s     r&   rv  @TestIOLoopFutures.test_run_in_executor_native.<locals>.sync_func  s    NNr)   c                 l   >#    [         R                  " 5       R                  S TX5      I S h  vN $  N7fr+   )r	   rK  r4  )rt  ru  rv  s     r&   async_wrapperDTestIOLoopFutures.test_run_in_executor_native.<locals>.async_wrapper  s1     )99i   s   *424)r\   r   r   )r    rx  ry  r  rz  rv  s        @r&   test_run_in_executor_native-TestIOLoopFutures.test_run_in_executor_native  sT     ""		
 #62M&4QRR&)3/s   AAc              #   p  ^^#    S/m " U4S jS[         R                  5      n[        R                  " 5       mU4S jnU" S5      n[        R
                  " 5       nUR                  U5        UR                  S U5      v   U R                  STS   5        U R                  TR                  5       5        g 7f)Nr   c                   ,   >^  \ rS rSrU U4S jrSrU =r$ )?TestIOLoopFutures.test_set_default_executor.<locals>.MyExecutori,  c                 @   > TS==   S-  ss'   [         TU ]  " U/UQ76 $ )Nr   r   )rW  r[  )r    funcr"   rX  counts      r&   r[  FTestIOLoopFutures.test_set_default_executor.<locals>.MyExecutor.submit-  s$    aAw~d2T22r)   r,   )r   r   r   r   r[  r   r_  )rX  r  s   @r&   
MyExecutorr  ,  s    3 3r)   r  c                  &   > T R                  5         g r+   )r   )events   r&   rv  >TestIOLoopFutures.test_set_default_executor.<locals>.sync_func3  s    IIKr)   r   )r   r   r\   r   r	   rK  set_default_executorr4  r   rP   is_set)r    r  rv  executorr=   r  r  s        @@r&   test_set_default_executor+TestIOLoopFutures.test_set_default_executor(  s     	333 	3
 !	 a=~~!!(+""433E!H%'s   B2B6r,   N)
r   r   r   r   rn  r   r{  r  r  r   r,   r)   r&   ra  ra    sC    5 0 0* 0 0* ( (r)   ra  c                   J    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rSrg)TestIOLoopRunSynci>  c                      [        SS9U l        g )NFr0  )r	   r4   rD   s    r&   rB  TestIOLoopRunSync.setUp?  s    51r)   c                 8    U R                   R                  5         g r+   rE  rD   s    r&   rF  TestIOLoopRunSync.tearDownB  s    r)   c                     U R                  [        R                  5         U R                  R	                  S 5        S S S 5        g ! , (       d  f       g = f)Nc                      gN*   r,   r,   r)   r&   r-   4TestIOLoopRunSync.test_sync_result.<locals>.<lambda>G  s    "r)   )rq   r   BadYieldErrorr4   run_syncrD   s    r&   test_sync_result"TestIOLoopRunSync.test_sync_resultE  s4    s001LL!!*- 211s   A
Ac                     U R                  [        5         U R                  R                  S 5        S S S 5        g ! , (       d  f       g = f)Nc                      SS-  $ r  r,   r,   r)   r&   r-   7TestIOLoopRunSync.test_sync_exception.<locals>.<lambda>K  r  r)   )rq   ZeroDivisionErrorr4   r  rD   s    r&   test_sync_exception%TestIOLoopRunSync.test_sync_exceptionI  s/    01LL!!-0 211s	   <
A
c                     [         R                  S 5       nU R                  U R                  R	                  U5      S5        g )Nc               3   Z   #    [         R                  v   [         R                  " S5      e7fr  )r   momentReturnr,   r)   r&   r   .TestIOLoopRunSync.test_async_result.<locals>.fN  s     ****R. s   )+r  )r   r  r   r4   r  r    r   s     r&   test_async_result#TestIOLoopRunSync.test_async_resultM  s9    		! 
	! 	..q126r)   c                     [         R                  S 5       nU R                  [        5         U R                  R                  U5        S S S 5        g ! , (       d  f       g = f)Nc               3   :   #    [         R                  v   SS-    g 7fr  r   r  r,   r)   r&   r   1TestIOLoopRunSync.test_async_exception.<locals>.fV  s     **Es   )r   r  rq   r  r4   r  r  s     r&   test_async_exception&TestIOLoopRunSync.test_async_exceptionU  sG    		 
	 01LL!!!$ 211s   A
Ac                 H   ^  U 4S jnT R                   R                  U5        g )Nc                  d   > T R                  [        R                  " 5       T R                  5        g r+   )r   r	   rK  r4   rD   s   r&   r   )TestIOLoopRunSync.test_current.<locals>.f_  s    MM&..*DLL9r)   )r4   r  r  s   ` r&   test_currentTestIOLoopRunSync.test_current^  s    	: 	a r)   c                     [         R                  S 5       nU R                  [        U R                  R
                  USS9  g )Nc               3   <   #    [         R                  " S5      v   g 7fr   )r   rV   r,   r)   r&   r   )TestIOLoopRunSync.test_timeout.<locals>.fe  s     ))A,s   rU   )r   )r   r  rq   r
   r4   r  r  s     r&   test_timeoutTestIOLoopRunSync.test_timeoutd  s;    		 
	 	,(=(=q$Or)   c                 t   ^ [         R                  S 5       mU4S jnU R                  R                  U5        g )Nc               3   0   #    [         R                  v   g 7fr+   r  r,   r)   r&   f13TestIOLoopRunSync.test_native_coroutine.<locals>.f1l  s     **s   c                  0   >#    T " 5       I S h  vN   g  N7fr+   r,   )r  s   r&   f23TestIOLoopRunSync.test_native_coroutine.<locals>.f2p  s     $JJs   )r   r  r4   r  )r    r  r  s     @r&   test_native_coroutine'TestIOLoopRunSync.test_native_coroutinek  s2    		 
		 	b!r)   )r4   N)r   r   r   r   rB  rF  r  r  r  r  r  r  r  r   r,   r)   r&   r  r  >  s0    2.17%!P"r)   r  c                   >    \ rS rSrS rS rS rS rS rS r	S r
S	rg
)TestPeriodicCallbackMathiv  c                     / nSnXAl         U H>  nUR                  U5        UR                  UR                   5        UR                   U-   nM@     U$ )a  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.
r   )_next_timeout_update_nextr   )r    pc	durationsr   r   ds         r&   simulate_calls'TestPeriodicCallbackMath.simulate_callsw  sS     AOOC LL))*""Q&C  r)   c                     g r+   r,   rD   s    r&   rd  TestPeriodicCallbackMath.dummy  s    r)   c                     [        U R                  S5      nU R                  U R                  US/S-  5      / SQ5        g )N'  r            i    r   rd  r   r  r    r  s     r&   
test_basic#TestPeriodicCallbackMath.test_basic  s9    djj%0QC!G,.L	
r)   c                     / SQn/ SQn[        U R                  S5      nU R                  U R                  X15      U5        g )N)	   r  r3         r  #   r  r   r   r   )r  r  r  r  i.  iL  ij  i  i  i  i  r  r  )r    call_durationsexpectedr  s       r&   test_overrun%TestPeriodicCallbackMath.test_overrun  s>     A
 djj%0,,R@(Kr)   c                     [        U R                  S5      nU R                  U R                  U/ SQ5      / SQ5        U R                  U R                  U/ SQ5      / SQ5        g )Nr  )r  r   r  )ir   r   )r  r  r  r  r  s     r&   test_clock_backwards-TestPeriodicCallbackMath.test_clock_backwards  sV    djj%0
 	$78:X	
 	,,R>@RSr)   c                   ^ / SQm/ SQnS/[        T5      -  n[        U R                  SSS9nU4S jn[        R                  " SU5         U R                  U R                  X25      U5        S S S 5        g ! , (       d  f       g = f)	N)      ?r   r   g      ?)r  g     @r  g     E@r   r  r  )jitterc                  &   > T R                  S5      $ )Nr   )pop)random_timess   r&   mock_random9TestPeriodicCallbackMath.test_jitter.<locals>.mock_random  s    ##A&&r)   zrandom.random)lenr   rd  r   patchr   r  )r    r  r  r  r  r  s        @r&   test_jitter$TestPeriodicCallbackMath.test_jitter  sl    (0s<00djj%<	' ZZ5T00DhO 655s   "A66
Bc                     [        S [        R                  " SSS95      nSnU R                  UR                  U5        g )Nc                      g r+   r,   r,   r)   r&   r-   9TestPeriodicCallbackMath.test_timedelta.<locals>.<lambda>  s    dr)   r      )minutesr   i8D )r   r8   r9   r   callback_time)r    r  expected_callback_times      r&   test_timedelta'TestPeriodicCallbackMath.test_timedelta  s9    lH,>,>qRT,UV!&))+ABr)   r,   N)r   r   r   r   r  rd  r  r  r  r  r  r   r,   r)   r&   r  r  v  s)    "
L*T"
PCr)   r  c                   .    \ rS rSrS rSS jrSS jrSrg)TestPeriodicCallbackAsynci  c                    ^ ^ SmSUU 4S jjn[        US5      nUR                  5         T R                  5         UR                  5         T R	                  TS5        g )Nr   c                  >   > T S-  m T S:X  a  TR                  5         g g )Nr   r   rg  )r  r    s   r&   r!   ?TestPeriodicCallbackAsync.test_periodic_plain.<locals>.callback  s!    QJEz		 r)   r3   r   returnN)r   r;   rN   r:   r   )r    r!   r  r  s   `  @r&   test_periodic_plain-TestPeriodicCallbackAsync.test_periodic_plain  sK    	 	 h+

		
	"r)   Nc                    ^ ^^ SS/m[         R                  SUUU 4S jj5       n[        US5      mTR                  5         T R	                  5         T R                  TS   S5        T R                  TS   S5        g )Nr   c               3      >#    T S==   S-  ss'   [         R                  " S5      v   T S==   S-  ss'   T S   S:X  a6  TR                  5         TR                  R	                  TR                  5        g g 7fNr   r   g?r   r   rV   r:   r4   r'   countsr  r    s   r&   r!   >TestPeriodicCallbackAsync.test_periodic_coro.<locals>.callback  s^     1INI))E""1INIayA~	))$))4 s   A3A6r3   r   r   )r  z%Generator[Future[None], object, None])r   r  r   r;   rN   r   r    r!   r  r  s   ` @@r&   test_periodic_coro,TestPeriodicCallbackAsync.test_periodic_coro  so    Q		5 	5 
	5 h+

		A&A&r)   c                    ^ ^^ SS/mSUUU 4S jjn[        US5      mTR                  5         T R                  5         T R                  TS   S5        T R                  TS   S5        g )Nr   c                     >#    T S==   S-  ss'   [         R                  " S5      I S h  vN   T S==   S-  ss'   T S   S:X  a6  TR                  5         TR                  R	                  TR                  5        g g  NQ7fr  r  r  s   r&   r!   ?TestPeriodicCallbackAsync.test_periodic_async.<locals>.callback  sf     1INI))E"""1INIayA~	))$))4  #s   'A>A<AA>r3   r   r   r  )r   r;   rN   r   r  s   ` @@r&   test_periodic_async-TestPeriodicCallbackAsync.test_periodic_async  s^    Q	5 	5 h+

		A&A&r)   r,   r  )r   r   r   r   r  r  r  r   r,   r)   r&   r  r    s    #'$'r)   r  c                   ,    \ rS rSrS rS rS rS rSrg)TestIOLoopConfigurationi  c                     SS/[        U5      -   n[        R                  SSR                  U5      /n[	        [
        R                  " U5      5      R                  5       $ )Nz!from tornado.ioloop import IOLoopz*classname = lambda x: x.__class__.__name__z-cz; )listsys
executabler^   r   
subprocesscheck_outputstrip)r    
statements	stmt_listr"   s       r&   
run_python"TestIOLoopConfiguration.run_python  sX    /8
 	 dii	&:;*11$78>>@@r)   c                     U R                  S5      nU R                  US5        U R                  S5      nU R                  US5        g )N"print(classname(IOLoop.current()))AsyncIOMainLoopzprint(classname(IOLoop()))AsyncIOLoopr*  r   r    clss     r&   test_default$TestIOLoopConfiguration.test_default  sA    ooBC/0oo:;m,r)   c                 L    U R                  SS5      nU R                  US5        g )Nz8IOLoop.configure("tornado.platform.asyncio.AsyncIOLoop")r-  r.  r0  r1  s     r&   test_asyncio$TestIOLoopConfiguration.test_asyncio  s)    ooF0
 	/0r)   c                 N    U R                  SSS5      nU R                  US5        g )Nz4from tornado.platform.asyncio import AsyncIOMainLoopzAsyncIOMainLoop().install()r-  r.  r0  r1  s     r&   test_asyncio_main)TestIOLoopConfiguration.test_asyncio_main  s,    ooB)0

 	/0r)   r,   N)	r   r   r   r   r*  r3  r6  r9  r   r,   r)   r&   r   r     s    A-11r)   r   __main__)6r9  concurrent.futuresr   
concurrentr   collections.abcr   r   r8   r   r   r%  r#  r\   rH   r5   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?  rU  ra  r  r  r  r   r   mainr,   r)   r&   <module>rK     s    1  %      
      %  A A   
 & 	P Pj6)) 6D1] 1M( M(`5")) 5"pPCx00 PCf1' 1'h1h// 1@ zMMO r)   