
    grB                        d dl Z d dlmZ d dlZd dlZd dlmZmZ d dlm	Z	 d dl
mZmZ  G d de      Z G d d	e      Z G d
 de      Z G d de      Z G d de      Z G d de      Zedk(  r ej(                          yy)    N)	timedelta)genlocks)TimeoutError)gen_testAsyncTestCasec                        e Zd Z fdZd Zd Zd Zed        Zd Z	d Z
d Zed	        Zed
        Zed        Zed        Zed        Zed        Z xZS )ConditionTestc                 0    t         |           g | _        y N)supersetUphistory)self	__class__s    L/var/www/openai/venv/lib/python3.12/site-packages/tornado/test/locks_test.pyr   zConditionTest.setUp   s        c                 :      fd}j                  |       y)z=Record the resolution of a Future returned by Condition.wait.c                     j                         sj                  j                  d       y j                  j                         y )Ntimeout)resultr   append)_futurekeyr   s    r   callbackz+ConditionTest.record_done.<locals>.callback   s0    ==?##I.##C(r   N)add_done_callback)r   r   r   r   s   ``` r   record_donezConditionTest.record_done   s    	) 	  *r   c                 n    | j                   j                  | j                         | j                          y)zRun all queued callbacks on the IOLoop.

        In these tests, this method is used after calling notify() to
        preserve the pre-5.0 behavior in which callbacks ran
        synchronously.
        N)io_loopadd_callbackstopwaitr   s    r   loop_brieflyzConditionTest.loop_briefly(   s#     	!!$)),		r   c                     t        j                         }| j                  dt        |             | j	                  dt        |             |j                          | j                  dt        |             y )N	Conditionwaiters)r   r'   assertInreprassertNotInr#   r   cs     r   	test_reprzConditionTest.test_repr2   sO    OOk47+DG,	ia)r   c              #      K   t        j                         }| j                  j                  d|j                         |j                          y w)N{Gz?)r   r'   r    
call_laternotifyr#   r,   s     r   test_notifyzConditionTest.test_notify9   s5     OOahh/ffhs   AAc                    t        j                         }| j                  |j                         d       | j                  |j                         d       |j	                  d       | j                          | j                  j                  d       |j	                  d       | j                          | j                  j                  d       | j                  g d| j                         y )Nwait1wait2   notify1notify2)r5   r8   r6   r9   )	r   r'   r   r#   r2   r%   r   r   assertEqualr,   s     r   test_notify_1zConditionTest.test_notify_1?   s    OO7+7+	I&	I&A4<<Pr   c                 f   t        j                         }t        d      D ]"  }| j                  |j	                         |       $ |j                  d       | j                          | j                  t        t        d            | j                         |j                  d       | j                          | j                  t        t        d            | j                         |j                  d       | j                          | j                  t        t        d            | j                         y )N      r7         )
r   r'   ranger   r#   r2   r%   r:   listr   r   r-   is      r   test_notify_nzConditionTest.test_notify_nK   s    OOqAQVVXq)  	
 	eAh6	eAh6	eAh6r   c                 f   t        j                         }t        d      D ]"  }| j                  |j	                         |       $ |j                          | j                          | j                  j                  d       | j                  t        t        d            dgz   | j                         y )Nr?   
notify_all)r   r'   rA   r   r#   rG   r%   r   r   r:   rB   rC   s      r   test_notify_allzConditionTest.test_notify_all\   s    OOqAQVVXq)  	
L) 	eAh<.8$,,Gr   c              #   
  K   t        j                         }|j                  t        d            }| j                  j                  d|j                         t        j                  d       | j                  |       y w)Nr0   seconds{Gz?Q?)
r   r'   r#   r   r    r1   r2   r   sleepassertFalser   r-   r#   s      r   test_wait_timeoutzConditionTest.test_wait_timeouth   s[     OOvvi-.ahh/iio*&s   BBc              #      K   t        j                         }| j                  j                  d|j                         |j                  t        d            }t        j                  d       | y w)Nr0   rL   rJ   rM   )	r   r'   r    r1   r2   r#   r   r   rN   rP   s      r   test_wait_timeout_preemptedz)ConditionTest.test_wait_timeout_preemptedp   sR     OO 	ahh/vvi-.iio
s   A2A4c              #     K   t        j                         }| j                  |j                         d       | j                  |j                  t	        d            d       | j                  |j                         d       | j                  |j                         d       t        j                  d       | j                  dg| j                         |j                  d       t        j                  d       | j                  g d	| j                         | j                  g d	| j                         |j                          d  | j                  g d
| j                         y w)Nr   r0   rJ   r7   r@   r>   rL   r   r   r   r@   )r   r   r@   r>   )
r   r'   r   r#   r   r   rN   r:   r   r2   r,   s     r   test_notify_n_with_timeoutz(ConditionTest.test_notify_n_with_timeoutz   s      OO1%	$ 78!<1%1% iio)dll3	iio*DLL9*DLL9	
-t||<s   E+E-c              #     K   t        j                         }| j                  |j                         d       | j                  |j                  t	        d            d       | j                  |j                         d       t        j                  d       | j                  dg| j                         |j                          d  | j                  g d| j                         y w)	Nr   r0   rJ   r7   r@   rL   r   rU   )
r   r'   r   r#   r   r   rN   r:   r   rG   r,   s     r   test_notify_all_with_timeoutz*ConditionTest.test_notify_all_with_timeout   s     OO1%	$ 78!<1% iio)dll3	*DLL9s   C'C)c              #   L  K   t        j                         t        d      D cg c]%  }t        j                  j                               ' }}|d   j                  fd       j                  d       d  | j                  t        d |D                     y c c}w w)Nr>   r7   c                 $    j                         S r   )r2   )r   r-   s    r   <lambda>z2ConditionTest.test_nested_notify.<locals>.<lambda>   s    qxxzr   r@   c              3   <   K   | ]  }|j                           y wr   done).0fs     r   	<genexpr>z3ConditionTest.test_nested_notify.<locals>.<genexpr>   s     6gAFFHg   )
r   r'   rA   asyncioensure_futurer#   r   r2   
assertTrueall)r   r   futuresr-   s      @r   test_nested_notifyz ConditionTest.test_nested_notify   s      OO =B!HEHq7((2HE 	
$$%9:	6g667 Fs   "B$*BAB$c              #   .  K   t        j                         }t        d      D ]  }|j                  t	        d              t        j                  |j                               }| j                  dt        |j                               t        j                  d       | j                  dt        |j                               | j                  |j                                |j                          | j                  |j                                y w)Ne   r0   rJ   f   rL   r7   )r   r'   rA   r#   r   rc   rd   r:   len_waitersr   rN   rO   r^   r2   re   )r   r-   r   r   s       r   test_garbage_collectionz%ConditionTest.test_garbage_collection   s      OOsAFF9T*+  &&qvvx0c!**o. iioC

O, 	'	
&s   DD)__name__
__module____qualname__r   r   r%   r.   r   r3   r;   rE   rH   rQ   rS   rV   rX   rh   rn   __classcell__)r   s   @r   r
   r
      s    
+*  

Q7"
H ' '   = =. : : 8 8 ' 'r   r
   c                   4    e Zd Zd Zd Zed        Zd Zd Zy)	EventTestc                 ,   t        j                         }| j                  dt        |      v        | j	                  dt        |      v        |j                          | j	                  dt        |      v        | j                  dt        |      v        y )Nclearset)r   Eventre   strrO   rw   )r   events     r   r.   zEventTest.test_repr   sn    3u:-.#e*,-		CJ./U+,r   c                    t        j                         }t        j                  |j	                               }|j                          t        j                  |j	                               }|j                          t        j                  |j	                               }| j                  |j                                | j                  |j                                | j                  |j                                y r   )
r   rx   rc   rd   r#   rw   rv   re   r^   rO   )r   efuture_0future_1future_2s        r   
test_eventzEventTest.test_event   s    KKM((2	((2		((2(()r   c              #   \  K   t        j                         }| j                  t              5  |j	                  t        d             d d d        | j                  j                  t        d      |j                         |j	                  t        d             y # 1 sw Y   WxY ww)Nr0   rJ   r7   )	r   rx   assertRaisesr   r#   r   r    add_timeoutrw   r   r|   s     r   test_event_timeoutzEventTest.test_event_timeout   ss     KKM|,&&4011 - 	  4!8!%%@ffYq)** -,s   *B,B 
AB, B)%B,c                     t        j                         }|j                          |j                          | j                  |j	                                y r   )r   rx   rw   re   is_setr   s     r   test_event_set_multiplez!EventTest.test_event_set_multiple   s1    KKM		
#r   c                 t   t        j                         }t        j                  |j	                               }|j                          t        j                  |j	                               }|j                          | j                  |j                                | j                  |j                                y r   )	r   rx   rc   rd   r#   rv   rw   re   r^   )r   r|   f0f1s       r   test_event_wait_clearzEventTest.test_event_wait_clear   so    KKM""1668,		""1668,		"	"r   N)	ro   rp   rq   r.   r   r   r   r   r    r   r   rt   rt      s*    -
* + +$#r   rt   c                   T    e Zd Zd Zd Zd Zed        Zed        Zd Z	ed        Z
y)	SemaphoreTestc                 P    | j                  t        t        j                  d       y )Nvalue)r   
ValueErrorr   	Semaphorer$   s    r   test_negative_valuez!SemaphoreTest.test_negative_value   s    *eooR@r   c                 z   t        j                         }| j                  dt        |             | j                  dt        |             |j	                          | j                  dt        |             | j                  dt        |             |j	                          | j                  dt        |             y )Nr   zunlocked,value:1lockedr(   )r   r   r)   r*   acquirer+   r   sems     r   r.   zSemaphoreTest.test_repr   s}    ook49-($s)4hS	*DI.ic+r   c                 ^   t        j                         }t        j                  |j	                               }| j                  |j                                t        j                  |j	                               }| j                  |j                                t        j                  |j	                               }|j                          | j                  |j                                | j                  |j                                |j                          | j                  |j                                |j                          | j                  t        j                  |j	                               j                                | j                  dt        |j                               y )Nr   )r   r   rc   rd   r   re   r^   rO   releaser:   rl   rm   )r   r   r   r   f2s        r   test_acquirezSemaphoreTest.test_acquire  s
   oo""3;;=1	" ""3;;=1#""3;;=1	"#	"--ckkm<AACDC-.r   c              #     K   t        j                  d      }|j                          |j                          |j                  t        d            }| j                  j                  d|j                         t        j                  d       | j                  t        j                        5  | d d d        |j                          t        j                  |j                               }| j                  |j                                |j                          | j                  |j                                y # 1 sw Y   xY ww)Nr@   r0   rJ   rL   g333333?)r   r   r   r   r    r1   r   r   rN   r   r   rc   rd   rO   r^   re   )r   r   r   r`   s       r   test_acquire_timeoutz"SemaphoreTest.test_acquire_timeout  s     ooa kkmkkm++i56ckk2iins//0M 1 	!!#++-0"! 10s   B1E3E8B
EEEc              #     K   t        j                  d      }|j                          | j                  j	                  d|j
                         |j                  t        d            }t        j                  d       | y w)Nr7   r0   rL   rJ   rM   )	r   r   r   r    r1   r   r   r   rN   )r   r   r   s      r   test_acquire_timeout_preemptedz,SemaphoreTest.test_acquire_timeout_preempted'  s`     ooa kkm 	ckk2++i56iios   BBc                 l   t        j                         }|j                          |j                          | j                  t	        j
                  |j                               j                                | j                  t	        j
                  |j                               j                                | j                  t	        j
                  |j                               j                                | j                  t	        j
                  |j                               j                                y r   )	r   r   r   re   rc   rd   r   r^   rO   r   s     r   test_release_unacquiredz%SemaphoreTest.test_release_unacquired2  s    oo 	--ckkm<AACD--ckkm<AACD--ckkm<AACD..s{{}=BBDEr   c           
   #     K   t        j                  d      }t        d      D cg c]0  }t        j                  |j                  t        d                  2 }}t        j                  |j                               }| j                  dt        |j                               t        j                  d       | j                  dt        |j                               | j                  |j                                |j                          | j                  |j                                |D ]"  }| j!                  t"        |j$                         $ y c c}w w)	Nr   r   rj   r0   rJ   rk   rL   r7   )r   r   rA   rc   rd   r   r   r:   rl   rm   r   rN   rO   r^   r   re   r   r   r   )r   r   r   rg   r   s        r   rn   z%SemaphoreTest.test_garbage_collection>  s     ooA& 3Z
 !!#++i.E"FG 	 

 &&s{{}5c#,,/0 iioC-. 	'& FlFMM: %
s   $E5EDEN)ro   rp   rq   r   r.   r   r   r   r   r   rn   r   r   r   r   r      sT    A,/( " "   
F ; ;r   r   c                       e Zd Zed        Zed        Zed        Zed        Zed        Zed        Z	ed        Z
d Zy	)
SemaphoreContextManagerTestc              #   (  K   t        j                         }|j                         5 }| j                  |d u        d d d        | j                  t	        j
                  |j                               j                                y # 1 sw Y   JxY wwr   )r   r   r   re   rc   rd   r^   )r   r   yieldeds      r   test_context_managerz0SemaphoreContextManagerTest.test_context_managerY  se     ooKKM!gOOGtO, " 	--ckkm<AACD	 "!s   'BBA	BBBc              #       K   t        j                          fd} |         j                  t        j                  j                               j                                y w)Nc                     K   4 d {   } j                  | d u        d d d       d {    y 7 (7 # 1 d {  7  sw Y   y xY wwr   )re   )r   r   r   s    r   r`   zGSemaphoreContextManagerTest.test_context_manager_async_await.<locals>.fg  s1     sg40 sssssss:   A3A7A5AAA	A A	A)r   r   re   rc   rd   r   r^   )r   r`   r   s   ` @r    test_context_manager_async_awaitz<SemaphoreContextManagerTest.test_context_manager_async_awaitb  sK      oo	1 c	 	--ckkm<AACDs   A%A)c              #   `  K   t        j                         }| j                  t              5  |j	                         5  ddz   d d d        d d d        | j                  t        j                  |j	                               j                                y # 1 sw Y   RxY w# 1 sw Y   VxY ww)Nr7   r   )	r   r   r   ZeroDivisionErrorr   re   rc   rd   r^   r   s     r   test_context_manager_exceptionz:SemaphoreContextManagerTest.test_context_manager_exceptionp  sw     oo01%A & 2
 	--ckkm<AACD	 &% 21s4   *B.B"BB"A	B.B	B""B+'B.c              #     K   t        j                         }|j                  t        d            5  	 d d d        | j	                  t        j                  |j                               j                                y # 1 sw Y   JxY ww)Nr0   rJ   )r   r   r   r   re   rc   rd   r^   r   s     r   test_context_manager_timeoutz8SemaphoreContextManagerTest.test_context_manager_timeoutz  s^     ooKK	$ 788 9 	--ckkm<AACD	 98s   2BA?A	B?BBc              #     K   t        j                  d      }| j                  t        j                        5  |j                  t        d            5  	 d d d        d d d        | j                  t        j                  |j                               j                                y # 1 sw Y   RxY w# 1 sw Y   VxY ww)Nr   r   r0   rJ   )r   r   r   r   r   r   r   rO   rc   rd   r^   r   s     r   "test_context_manager_timeout_errorz>SemaphoreContextManagerTest.test_context_manager_timeout_error  s     ooA&s//0Id$;<< = 1
 	..s{{}=BBDE	 =< 10s5   6CB5B)B5 A	C)B2	.B55B>:Cc              #   4  K   t        j                         g t        j                  fd       }t	        d      D cg c]
  } ||       c} g }t	        d      D ]  }|j                  d|z  d|z  g        | j                  |       y c c}w w)Nc              3      K   j                         5  j                  d| z         t        j                  d       j                  d| z         d d d        y # 1 sw Y   y xY ww)Nacquired %dr0   
release %d)r   r   r   rN   )indexr   r   s    r   r`   zESemaphoreContextManagerTest.test_context_manager_contended.<locals>.f  sN     %}u45iio%|e34 &%%s   A+A A	A+A($A+r@   r   r   )r   r   r   	coroutinerA   extendr:   )r   r`   rD   expected_historyr   r   s       @@r   test_context_manager_contendedz:SemaphoreContextManagerTest.test_context_manager_contended  s     oo		5 
	5 #1X&XqtX&&qA##]Q%6q8H$IJ  	)73 's   =BBABc              #      K   | j                  t        j                        5  t        j                         5  	 d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wwr   )r   r   BadYieldErrorr   r   r$   s    r   test_yield_semz*SemaphoreContextManagerTest.test_yield_sem  sH      s001)) * 21)) 212    A$AAA	A$A	AA!A$c                     | j                  t              5  t        j                         5  	 d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wr   )r   RuntimeErrorr   r   r$   s    r   test_context_manager_misusez7SemaphoreContextManagerTest.test_context_manager_misuse  s<     |," # -,"" -,   A
>A
A	A

AN)ro   rp   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   X  s    E E E E E E E E F F 4 4&  r   r   c                       e Zd Zd Zy)BoundedSemaphoreTestc                    t        j                         }| j                  t        |j                         |j                          t        j                  |j                               }| j                  |j                                |j	                          | j                  |j                                |j	                          | j                  t        |j                         y r   )r   BoundedSemaphorer   r   r   r   rc   rd   rO   r^   re   )r   r   r   s      r   r   z,BoundedSemaphoreTest.test_release_unacquired  s    $$&*ckk2&&s{{}5'&*ckk2r   N)ro   rp   rq   r   r   r   r   r   r     s    3r   r   c                   d    e Zd Zd Zd Zed        Zed        Zed        Zd Z	ed        Z
d Zy	)
	LockTestsc                 x    t        j                         }t        |       |j                          t        |       y r   )r   Lockr*   r   r   locks     r   r.   zLockTests.test_repr  s$    zz|T
T
r   c                    t        j                         }| j                  t        j                  |j                               j                                t        j                  |j                               }| j                  |j                                |j                          | j                  |j                                y r   )	r   r   re   rc   rd   r   r^   rO   r   )r   r   r   s      r   test_acquire_releasezLockTests.test_acquire_release  sx    zz|--dlln=BBDE&&t||~6'&r   c              #     K   t        j                         | j                  t        j                  j                               j                                d}g t        j                  fd       }t        |      D cg c]
  } ||       }}| j                  t        d |D                     j                          | | j                  t        t        |                   y c c}w w)N   c              3   ~   K   j                         5  j                  |        d d d        y # 1 sw Y   y xY wwr   )r   r   idxr   r   s    r   r`   z&LockTests.test_acquire_fifo.<locals>.f  s*     &s# '&&s   =1	=:=c              3   <   K   | ]  }|j                           y wr   r]   )r_   r   s     r   ra   z.LockTests.test_acquire_fifo.<locals>.<genexpr>  s     AvV[[]rb   )r   r   re   rc   rd   r   r^   r   r   rA   rO   anyr   r:   rB   r   Nr`   rD   rg   r   r   s        @@r   test_acquire_fifozLockTests.test_acquire_fifo  s     zz|--dlln=BBDE		$ 
	$ "'q*A1Q4*AAABeAh1	 +s   A?C3C.A!C3c              #     K   t        j                         | j                  t        j                  j                               j                                d}g fd}t        |      D cg c]
  } ||       }}j                          | | j                  t        t        |                   y c c}w w)Nr   c                    K   4 d {    j                  |        d d d       d {    y 7 &7 # 1 d {  7  sw Y   y xY wwr   )r   r   s    r   r`   z1LockTests.test_acquire_fifo_async_with.<locals>.f  s+     ts# tttttts9   A
1A
5A
3A
A
A>AA
)r   r   re   rc   rd   r   r^   rA   r   r:   rB   r   s        @@r   test_acquire_fifo_async_withz&LockTests.test_acquire_fifo_async_with  s      zz|--dlln=BBDE	$ "'q*A1Q4*eAh1 +s   A,B?0B:?A B?c              #   x  K   t        j                         }|j                          | j                  t        j
                        5  |j                  t        d             d d d        | j                  t        j                  |j                               j                                y # 1 sw Y   JxY ww)Nr0   rJ   )r   )r   r   r   r   r   r   r   rO   rc   rd   r^   r   s     r   r   zLockTests.test_acquire_timeout  s{     zz|s//0,,y'>,?? 1 	..t||~>CCEF	 10s   AB:B.%A	B:.B73B:c                     t        j                         }| j                  t        |j                         |j                          |j	                          | j                  t        |j                         y r   )r   r   r   r   r   r   r   s     r   test_multi_releasezLockTests.test_multi_release  sG    zz|,5,5r   c              #      K   | j                  t        j                        5  t        j                         5  	 d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wwr   )r   r   r   r   r   r$   s    r   test_yield_lockzLockTests.test_yield_lock  sG      s001

$ % 21$$ 21r   c                     | j                  t              5  t        j                         5  	 d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wr   )r   r   r   r   r$   s    r   r   z%LockTests.test_context_manager_misuse  s:     |,  -, -,r   N)ro   rp   rq   r.   r   r   r   r   r   r   r   r   r   r   r   r   r     sh    ' 2 2" 2 2" G G6  r   r   __main__)rc   datetimer   typingunittesttornador   r   tornado.genr   tornado.testingr   r   r
   rt   r   r   r   r   ro   mainr   r   r   <module>r      s         $ 3k'M k'\,# ,#^a;M a;HV- Vr3= 3 R Rj zHMMO r   