
    MhfB                     ,   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                     dS dS )    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                 V    t                                                       g | _        d S N)supersetUphistory)self	__class__s    W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/tornado/test/locks_test.pyr   zConditionTest.setUp   s        c                 D      fd}                     |           dS )z=Record the resolution of a Future returned by Condition.wait.c                                                      sj                            d           d S j                                       d S )Ntimeout)resultr   append)_futurekeyr   s    r   callbackz+ConditionTest.record_done.<locals>.callback   sK    ==?? )##I.....##C(((((r   N)add_done_callback)r   r   r   r   s   ``` r   record_donezConditionTest.record_done   sD    	) 	) 	) 	) 	) 	) 	) 	  *****r   c                 l    | j                             | j                   |                                  dS )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                    }|                     dt          |                     |                     dt          |                     |                                 |                     dt          |                     d S )N	Conditionwaiters)r   r'   assertInreprassertNotInr#   r   cs     r   	test_reprzConditionTest.test_repr2   sr    Ok477+++DGG,,,	ia)))))r   c              #      K   t          j                    }| j                            d|j                   |                                V  d S )N{Gz?)r   r'   r    
call_laternotifyr#   r,   s     r   test_notifyzConditionTest.test_notify9   sB      Oah///ffhhr   c                    t          j                    }|                     |                                d           |                     |                                d           |                    d           |                                  | j                            d           |                    d           |                                  | j                            d           |                     g d| j                   d S )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    O7+++7+++	I&&&	I&&&AAA4<PPPPPr   c                    t          j                    }t          d          D ]*}|                     |                                |           +|                    d           |                                  |                     t          t          d                    | j	                   |                    d           |                                  |                     t          t          d                    | j	                   |                    d           |                                  |                     t          t          d                    | j	                   d S )N      r7         )
r   r'   ranger   r#   r2   r%   r:   listr   r   r-   is      r   test_notify_nzConditionTest.test_notify_nK   s   Oq 	* 	*AQVVXXq))))	 	eAhh666	eAhh666	eAhh66666r   c                    t          j                    }t          d          D ]*}|                     |                                |           +|                                 |                                  | j                            d           | 	                    t          t          d                    dgz   | j                   d S )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    Oq 	* 	*AQVVXXq))))	L))) 	eAhh<.8$,GGGGGr   c              #     K   t          j                    }|                    t          d                    }| j                            d|j                   t          j        d          V  | 	                    |V            d S )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{      Ovvi---..ah///ioo**&&&&&r   c              #      K   t          j                    }| j                            d|j                   |                    t          d                    }t          j        d          V  |V  d S )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   sl      O 	ah///vvi---..ioo




r   c              #   &  K   t          j                    }|                     |                                d           |                     |                    t	          d                    d           |                     |                                d           |                     |                                d           t          j        d          V  |                     dg| j                   |	                    d           t          j        d          V  |                     g d	| j                   |                     g d	| j                   |	                                 d V  |                     g d
| j                   d S )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   sh      O1%%%	$ 7 7 788!<<<1%%%1%%% ioo)dl333	ioo***DL999***DL999	


---t|<<<<<r   c              #     K   t          j                    }|                     |                                d           |                     |                    t	          d                    d           |                     |                                d           t          j        d          V  |                     dg| j                   |	                                 d V  |                     g d| j                   d S )	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      O1%%%	$ 7 7 788!<<<1%%% ioo)dl333	***DL99999r   c              #   .  K   t          j                    fdt          d          D             }|d                             fd                               d           d V  |                     t          d |D                                  d S )Nc                 \    g | ](}t          j                                                  )S  )asyncioensure_futurer#   ).0r   r-   s     r   
<listcomp>z4ConditionTest.test_nested_notify.<locals>.<listcomp>   s,    EEEq7(22EEEr   r>   r7   c                 ,                                     S r   )r2   )r   r-   s    r   <lambda>z2ConditionTest.test_nested_notify.<locals>.<lambda>   s    qxxzz r   r@   c              3   >   K   | ]}|                                 V  d S r   done)r^   fs     r   	<genexpr>z3ConditionTest.test_nested_notify.<locals>.<genexpr>   s*      66AFFHH666666r   )r   r'   rA   r   r2   
assertTrueall)r   futuresr-   s     @r   test_nested_notifyz ConditionTest.test_nested_notify   s       O FEEEE!HHEEE 	
$$%9%9%9%9:::	66g6666677777r   c              #   v  K   t          j                    }t          d          D ]%}|                    t	          d                     &t          j        |                                          }|                     dt          |j	                             t          j        d          V  |                     dt          |j	                             |                     |                                           |                                 |                     |                                           d S )Ne   r0   rJ   f   rL   r7   )r   r'   rA   r#   r   r\   r]   r:   len_waitersr   rN   rO   rd   r2   rg   )r   r-   r   r   s       r   test_garbage_collectionz%ConditionTest.test_garbage_collection   s       Os 	, 	,AFF9T***++++&qvvxx00c!*oo... iooC
OO,,, 	'''	


&&&&&r   )__name__
__module____qualname__r   r   r%   r.   r   r3   r;   rE   rH   rQ   rS   rV   rX   rj   rp   __classcell__)r   s   @r   r
   r
      sU           
+ 
+ 
+  * * *   X

Q 
Q 
Q7 7 7"
H 
H 
H ' ' X'   X = = X=. : : X: 8 8 X8 ' ' X' ' ' ' 'r   r
   c                   <    e Zd Zd Zd Zed             Zd Zd ZdS )	EventTestc                 t   t          j                    }|                     dt          |          v            |                     dt          |          v            |                                 |                     dt          |          v            |                     dt          |          v            d S )Nclearset)r   Eventrg   strrO   ry   )r   events     r   r.   zEventTest.test_repr   s    3u::-...#e**,---		CJJ.///U+,,,,,r   c                 J   t          j                    }t          j        |                                          }|                                 t          j        |                                          }|                                 t          j        |                                          }|                     |                                           |                     |                                           | 	                    |                                           d S r   )
r   rz   r\   r]   r#   ry   rx   rg   rd   rO   )r   efuture_0future_1future_2s        r   
test_eventzEventTest.test_event   s    KMM(22	(22				(22(((((()))))r   c              #     K   t          j                    }|                     t                    5  |                    t          d                    V  d d d            n# 1 swxY w Y   | j                            t          d          |j                   |                    t          d                    V  d S )Nr0   rJ   r7   )	r   rz   assertRaisesr   r#   r   r    add_timeoutry   r   r~   s     r   test_event_timeoutzEventTest.test_event_timeout   s      KMM|,, 	2 	2&&400011111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	  4!8!8!8!%@@@ffYq)))*******s   &A""A&)A&c                     t          j                    }|                                 |                                 |                     |                                           d S r   )r   rz   ry   rg   is_setr   s     r   test_event_set_multiplez!EventTest.test_event_set_multiple   sG    KMM		

#####r   c                    t          j                    }t          j        |                                          }|                                 t          j        |                                          }|                                 |                     |                                           |                     |                                           d S r   )	r   rz   r\   r]   r#   rx   ry   rg   rd   )r   r~   f0f1s       r   test_event_wait_clearzEventTest.test_event_wait_clear   s    KMM"16688,,				"16688,,			"""		"""""r   N)	rq   rr   rs   r.   r   r   r   r   r   r[   r   r   rv   rv      sk        - - -
* 
* 
* + + X+$ $ $# # # # #r   rv   c                   h    e Zd Zd Zd Zd Zed             Zed             Zd Z	ed             Z
dS )	SemaphoreTestc                 T    |                      t          t          j        d           d S )Nvalue)r   
ValueErrorr   	Semaphorer$   s    r   test_negative_valuez!SemaphoreTest.test_negative_value   s%    *eoR@@@@@r   c                    t          j                    }|                     dt          |                     |                     dt          |                     |                                 |                     dt          |                     |                     dt          |                     |                                 |                     dt          |                     d S )Nr   zunlocked,value:1lockedr(   )r   r   r)   r*   acquirer+   r   sems     r   r.   zSemaphoreTest.test_repr   s    ok499---($s))444hS		***DII...ic+++++r   c                    t          j                    }t          j        |                                          }|                     |                                           t          j        |                                          }|                     |                                           t          j        |                                          }|                                 |                     |                                           |                     |                                           |                                 |                     |                                           |                                 |                     t          j        |                                                                                     | 	                    dt          |j                             d S )Nr   )r   r   r\   r]   r   rg   rd   rO   releaser:   rn   ro   )r   r   r   r   f2s        r   test_acquirezSemaphoreTest.test_acquire  s]   o"3;;==11		""" "3;;==11###"3;;==11		"""###		"""-ckkmm<<AACCDDDC--.....r   c              #     K   t          j        d          }|                                V  |                                V  |                    t          d                    }| j                            d|j                   t          j        d          V  | 	                    t          j
                  5  |V  d d d            n# 1 swxY w Y   |                                 t          j        |                                          }|                     |                                           |                                 |                     |                                           d S )Nr@   r0   rJ   rL   g333333?)r   r   r   r   r    r1   r   r   rN   r   r   r\   r]   rO   rd   rg   )r   r   r   re   s       r   test_acquire_timeoutz"SemaphoreTest.test_acquire_timeout  sl     oa  kkmmkkmm++i55566ck222inns/00 	 	MMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	!#++--00"""!!!!!s   ;CCCc              #     K   t          j        d          }|                                V  | j                            d|j                   |                    t          d                    }t          j        d          V  |V  d S )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      oa  kkmm 	ck222++i55566ioor   c                    t          j                    }|                                 |                                 |                     t	          j        |                                                                                     |                     t	          j        |                                                                                     |                     t	          j        |                                                                                     |                     t	          j        |                                                                                     d S r   )	r   r   r   rg   r\   r]   r   rd   rO   r   s     r   test_release_unacquiredz%SemaphoreTest.test_release_unacquired2  s    o 	-ckkmm<<AACCDDD-ckkmm<<AACCDDD-ckkmm<<AACCDDD.s{{}}==BBDDEEEEEr   c              #     K   t          j        d          fdt          d          D             }t          j                                                  }|                     dt          j                             t          j
        d          V  |                     dt          j                             |                     |                                                                            |                     |                                           |D ]"}|                     t           |j                   #d S )Nr   r   c           	      z    g | ]7}t          j                            t          d                               8S )r0   rJ   )r\   r]   r   r   )r^   r   r   s     r   r_   z9SemaphoreTest.test_garbage_collection.<locals>.<listcomp>B  sK     
 
 
 !#++i.E.E.E"F"FGG
 
 
r   rl   rm   rL   r7   )r   r   rA   r\   r]   r   r:   rn   ro   r   rN   rO   rd   r   rg   r   r   r   )r   ri   r   r   s      @r   rp   z%SemaphoreTest.test_garbage_collection>  s4      oA&&&
 
 
 
3ZZ
 
 

 &s{{}}55c#,//000 iooC--... 	'''&&&  	; 	;FlFM::::	; 	;r   N)rq   rr   rs   r   r.   r   r   r   r   r   rp   r[   r   r   r   r      s        A A A, , ,/ / /( " " X"    X
F 
F 
F ; ; X; ; ;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d	S )
SemaphoreContextManagerTestc              #   L  K   t          j                    }|                                V 5 }|                     |           d d d            n# 1 swxY w Y   |                     t          j        |                                                                                     d S r   )r   r   r   assertIsNonerg   r\   r]   rd   )r   r   yieldeds      r   test_context_managerz0SemaphoreContextManagerTest.test_context_managerY  s      oKKMM!! 	'gg&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	-ckkmm<<AACCDDDDDs   AAAc              #       K   t          j                     fd} |            V                       t          j                                                                                             d S )Nc                     K   4 d {V }                      |            d d d           d {V  d S # 1 d {V swxY w Y   d S r   )r   )r   r   r   s    r   re   zGSemaphoreContextManagerTest.test_context_manager_async_await.<locals>.fg  s       + + + + + + +g!!'***+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +   5
??)r   r   rg   r\   r]   r   rd   )r   re   r   s   ` @r    test_context_manager_async_awaitz<SemaphoreContextManagerTest.test_context_manager_async_awaitb  s       o	+ 	+ 	+ 	+ 	+ 	+ acc			 	-ckkmm<<AACCDDDDDr   c              #     K   t          j                    }|                     t                    5  |                                V 5  ddz   d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |                     t          j        |                                                                                     d S )Nr7   r   )	r   r   r   ZeroDivisionErrorr   rg   r\   r]   rd   r   s     r   test_context_manager_exceptionz:SemaphoreContextManagerTest.test_context_manager_exceptionp  s(     o011 	 	%%  A              	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 	-ckkmm<<AACCDDDDDs5   A0AA0A	A0 A	!A00A47A4c              #   B  K   t          j                    }|                    t          d                    V 5  	 d d d            n# 1 swxY w Y   |                     t          j        |                                                                                     d S )Nr0   rJ   )r   r   r   r   rg   r\   r]   rd   r   s     r   test_context_manager_timeoutz8SemaphoreContextManagerTest.test_context_manager_timeoutz  s      oKK	$ 7 7 78888 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	-ckkmm<<AACCDDDDDs   A		AAc              #     K   t          j        d          }|                     t          j                  5  |                    t          d                    V 5  	 d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |                     t          j	        |                                          
                                           d S )Nr   r   r0   rJ   )r   r   r   r   r   r   r   rO   r\   r]   rd   r   s     r   "test_context_manager_timeout_errorz>SemaphoreContextManagerTest.test_context_manager_timeout_error  s:     oA&&&s/00 	 	Id$;$;$;<<<<                	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 	.s{{}}==BBDDEEEEEs5   &BA+B+A/	/B2A/	3BB	Bc              #   2  K   t          j                    g t          j        fd            fdt	          d          D             V  g }t	          d          D ]}|                    d|z  d|z  g            |                     |           d S )Nc              3      K                                    V 5                      d| z             t          j        d          V                      d| z             d d d            d S # 1 swxY w Y   d S )Nacquired %dr0   
release %d)r   r   r   rN   )indexr   r   s    r   re   zESemaphoreContextManagerTest.test_context_manager_contended.<locals>.f  s      %% 5 5}u4555ioo%%%|e34445 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5s   AA..A25A2c                 &    g | ]} |          S r[   r[   r^   rD   re   s     r   r_   zNSemaphoreContextManagerTest.test_context_manager_contended.<locals>.<listcomp>  s!    &&&qqtt&&&r   r@   r   r   )r   r   r   	coroutinerA   extendr:   )r   expected_historyrD   re   r   r   s      @@@r   test_context_manager_contendedz:SemaphoreContextManagerTest.test_context_manager_contended  s      o		5 	5 	5 	5 	5 
	5 '&&&U1XX&&&&&&q 	K 	KA##]Q%6q8H$IJJJJ)733333r   c              #      K   |                      t          j                  5  t          j                    V 5  	 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S r   )r   r   BadYieldErrorr   r   r$   s    r   test_yield_semz*SemaphoreContextManagerTest.test_yield_sem  s       s011 	 	))))                	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	3   AAAA
	
AA
	AA"%A"c                     |                      t                    5  t          j                    5  	 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S r   )r   RuntimeErrorr   r   r$   s    r   test_context_manager_misusez7SemaphoreContextManagerTest.test_context_manager_misuse  s     |,, 	 	""                	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	1   A=AA	AA	AAAN)rq   rr   rs   r   r   r   r   r   r   r   r   r   r[   r   r   r   r   X  s        E E XE E E XE E E XE E E XE F F XF 4 4 X4&   X    r   r   c                       e Zd Zd ZdS )BoundedSemaphoreTestc                    t          j                    }|                     t          |j                   |                                 t          j        |                                          }|                     |	                                           |                                 | 
                    |	                                           |                                 |                     t          |j                   d S r   )r   BoundedSemaphorer   r   r   r   r\   r]   rO   rd   rg   )r   r   r   s      r   r   z,BoundedSemaphoreTest.test_release_unacquired  s    $&&*ck222&s{{}}55'''&&&*ck22222r   N)rq   rr   rs   r   r[   r   r   r   r     s#        3 3 3 3 3r   r   c                   ~    e Zd Zd Zd Zed             Zed             Zed             Zd Z	ed             Z
d Zd	S )
	LockTestsc                     t          j                    }t          |           |                                 t          |           d S r   )r   Lockr*   r   r   locks     r   r.   zLockTests.test_repr  s4    z||T


T




r   c                    t          j                    }|                     t          j        |                                                                                     t          j        |                                          }|                     |                                           |                                 |                     |                                           d S r   )	r   r   rg   r\   r]   r   rd   rO   r   )r   r   r   s      r   test_acquire_releasezLockTests.test_acquire_release  s    z||-dllnn==BBDDEEE&t||~~66'''&&&&&r   c              #   "  K   t          j                    |                     t          j                                                                                             d}g t          j        fd            fdt          |          D             }| 
                    t          d |D                                                                   |V  |                     t          t          |                               d S )N   c              3      K                                    V 5                      |            d d d            d S # 1 swxY w Y   d S r   )r   r   idxr   r   s    r   re   z&LockTests.test_acquire_fifo.<locals>.f  s      && $ $s###$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $s   =AAc                 &    g | ]} |          S r[   r[   r   s     r   r_   z/LockTests.test_acquire_fifo.<locals>.<listcomp>  !    ***A11Q44***r   c              3   >   K   | ]}|                                 V  d S r   rc   )r^   r   s     r   rf   z.LockTests.test_acquire_fifo.<locals>.<genexpr>  s*      AAvV[[]]AAAAAAr   )r   r   rg   r\   r]   r   rd   r   r   rA   rO   anyr   r:   rB   r   Nri   re   r   r   s      @@@r   test_acquire_fifozLockTests.test_acquire_fifo  s     z||-dllnn==BBDDEEE		$ 	$ 	$ 	$ 	$ 
	$ +***q***AAAAAAABBBeAhh11111r   c              #     K   t          j                    |                     t          j                                                                                             d}g fdfdt          |          D             }                                 |V  | 	                    t          t          |                               d S )Nr   c                    K   4 d {V                       |            d d d           d {V  d S # 1 d {V swxY w Y   d S r   )r   r   s    r   re   z1LockTests.test_acquire_fifo_async_with.<locals>.f  s       $ $ $ $ $ $ $ $s###$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $r   c                 &    g | ]} |          S r[   r[   r   s     r   r_   z:LockTests.test_acquire_fifo_async_with.<locals>.<listcomp>  r   r   )r   r   rg   r\   r]   r   rd   rA   r   r:   rB   r   s      @@@r   test_acquire_fifo_async_withz&LockTests.test_acquire_fifo_async_with  s       z||-dllnn==BBDDEEE	$ 	$ 	$ 	$ 	$ 	$ +***q***eAhh11111r   c              #     K   t          j                    }|                                 |                     t          j                  5  |                    t          d                    V  d d d            n# 1 swxY w Y   |                     t          j	        |                                          
                                           d S )Nr0   rJ   )r   )r   r   r   r   r   r   r   rO   r\   r]   rd   r   s     r   r   zLockTests.test_acquire_timeout  s      z||s/00 	@ 	@,,y'>'>'>,?????	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	.t||~~>>CCEEFFFFFs   	'A<<B B c                     t          j                    }|                     t          |j                   |                                 |                                 |                     t          |j                   d S r   )r   r   r   r   r   r   r   s     r   test_multi_releasezLockTests.test_multi_release  s]    z||,555,55555r   c              #      K   |                      t          j                  5  t          j                    V 5  	 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S r   )r   r   r   r   r   r$   s    r   test_yield_lockzLockTests.test_yield_lock  s       s011 	 	
$$                	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	r   c                     |                      t                    5  t          j                    5  	 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S r   )r   r   r   r   r$   s    r   r   z%LockTests.test_context_manager_misuse  s     |,, 	 	                	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	r   N)rq   rr   rs   r.   r   r   r   r   r   r   r   r   r[   r   r   r   r     s          ' ' ' 2 2 X2" 2 2 X2" G G XG6 6 6   X    r   r   __main__)r\   datetimer   typingunittesttornador   r   tornado.genr   tornado.testingr   r   r
   rv   r   r   r   r   rq   mainr[   r   r   <module>r      s                    $ $ $ $ $ $ 3 3 3 3 3 3 3 3k' k' k' k' k'M k' k' k'\,# ,# ,# ,# ,# ,# ,# ,#^a; a; a; a; a;M a; a; a;HV V V V V- V V Vr3 3 3 3 3= 3 3 3 R R R R R R R Rj zHMOOOOO r   