
    i10                        d dl mZ  ej                     d dl mZ d dlZd dlZd dlZdZ G d dej	                  Z
 G d de          Z G d	 d
ej        e          Z G d de          Z G d de          Z G d de          Z G d dej                  Z G d dej        e          Zedk    r ej                     dS dS )    )monkey)queueN   c                       e Zd Zd Zd ZdS )_TriggerThreadc                     || _         || _        ddlm}  |            | _        t
          j                            |            d S )Nr   )Event)fnargsgevent.eventr	   startedEvent	threadingThread__init__)selfr
   r   r	   s       J/usr/local/lib/python3.11/dist-packages/gevent/tests/test__monkey_queue.pyr   z_TriggerThread.__init__   sM    	&&&&&&!EGG!!$'''''    c                 ~    t          j        d           | j                                          | j        | j          d S )N{Gz?)timesleepr   setr
   r   r   s    r   runz_TriggerThread.run   s@     	
4r   N)__name__
__module____qualname__r   r    r   r   r   r      s2        ( ( (    r   r   c                       e Zd Zd Zd ZdS )BlockingTestMixinc                    t          ||          | _        | j                                          || | _        | j        j                                        s|                     d|z             | j                            d           | j                                        r|                     d|z             | j        S )Nz,blocking function '%r' appeared not to block
   ,trigger function '%r' appeared to not return)	r   tstartresultr   isSetfailjoinis_alive)r   
block_func
block_argstrigger_functrigger_argss        r   do_blocking_testz"BlockingTestMixin.do_blocking_test3   s    l;; j*-v"((** 	"IID ! " " "B6?? 	$IID"# $ $ ${r   c                    t          ||          | _        | j                                         	 |                     |          5   ||  d d d            n# 1 swxY w Y   | j                            d           | j                                        r|                     d|z             | j        j                                        s|                     d           d S d S # | j                            d           | j                                        r|                     d|z             | j        j                                        s|                     d           w w xY w)Nr"   r#   z(trigger thread ended but event never set)	r   r$   r%   assertRaisesr)   r*   r(   r   r'   )r   r+   r,   r-   r.   expected_exception_classs         r   do_exceptional_blocking_testz.BlockingTestMixin.do_exceptional_blocking_testB   s   l;;		F""#;<< ( (
J''( ( ( ( ( ( ( ( ( ( ( ( ( ( ( FKKOOOv   (		H&' ( ( (6&,,.. F		DEEEEEF F	 FKKOOOv   (		H&' ( ( (6&,,.. F		DEEEEFs0   C$ AC$ AC$ AC$ $BE%N)r   r   r   r/   r3   r   r   r   r    r    1   s7          F F F F Fr   r    c                   L    e Zd Zej        Zd Zd Zd Zd Zd Z	d Z
d Zd Zd	S )
BaseQueueTestc                 D    d| _         t          j                    | _        d S )Nr   )cumr   Lockcumlockr   s    r   setUpzBaseQueueTest.setUpU   s     ~''r   c                 j   |                                 st          d          |                    d           |                    d           |                    d           |                    d           t          ddd          }|                                |                                f}|                     |||j        j                 ||j        j                 fd           t          g dg d	g d
          }|                                |                                |                                g}|                     |||j        j                 d           t          t          dz
            D ]?}|                    |           |                     |                                 d           @|                     |                                d           |                    d           |                     |                                d           	 |                    dd           |                     d           n# t          j        $ r Y nw xY w	 |                    dd           |                     d           n# t          j        $ r Y nw xY w|                     |                                t                     |                     |j        d|j        d           |                     |j        d|j        d           t          t                    D ]}|                                 |                     |                                 d           	 |                    d           |                     d           n# t          j        $ r Y nw xY w	 |                    d           |                     d           n# t          j        $ r Y nw xY w|                     |j        d|j        d           |                     |j        d|j        d           d S ) N&Call this function with an empty queueo   M       )Queue	LifoQueuePriorityQueuez#q.peek() and q.get() are not equal!)r>   r?   r@   )r?   r>   r=   )r?   r>   r@   z&Didn't seem to queue the correct data!   Queue should not be emptyQueue should not be full  Queue should be fullx  r   blockz(Didn't appear to block with a full queuer   timeoutz+Didn't appear to time-out with a full queuerI   r   rI   Tr"   Queue should be emptyz*Didn't appear to block with an empty queuez-Didn't appear to time-out with an empty queueempty)Tr"   )rR   RuntimeErrorputdictpeekgetassertEqual	__class__r   range
QUEUE_SIZEassertFalsefull
assertTruer(   rA   Fullqsizer/   Empty)r   qtarget_first_itemsactual_first_itemtarget_orderactual_orderis          r   simple_queue_testzBaseQueueTest.simple_queue_testY   s   wwyy 	IGHHH	c


	c


	c


	c


!   VVXXquuww/*,Q[-AB,Q[-ABD>	@ 	@ 	@ ///&5oo*9//; ; ; !%%''2|AK4H'IA	C 	C 	Cz!|$$ 	E 	EAEE!HHHQWWYY(CDDDD#=>>>	c


"8999	EE#QEII@AAAAz 	 	 	D		EE#tE$$$IICDDDDz 	 	 	D	J///aeVQUB777ae_aeR@@@z"" 	 	AEEGGGG		#:;;;	EEENNNIIBCCCC{ 	 	 	D		EE$EIIEFFFF{ 	 	 	D	 	aeR
;;;aeZ
CCCCCsH   ",I I! I!%,J J$#J$,+N N*)N*.+O O,+O,c                     	 |                                 }||                                 d S | xj        |z  c_        |                                 QN)rW   	task_doner7   )r   rb   xs      r   workerzBaseQueueTest.worker   sL    	AyHHMHHKKMMM	r   c                    d| _         dD ]0}t          j        | j        |f                                           1t          d          D ]}|                    |           |                                 |                     | j         t          t          d                    d           dD ]}|                    d            |                                 d S )Nr   )r   rD   )targetr   d   z0q.join() did not block until all tasks were done)
r7   r   r   rm   r%   rZ   rT   r)   rX   sumr   rb   rg   s      r   queue_join_testzBaseQueueTest.queue_join_test   s     	D 	DADKqd;;;AACCCCs 	 	AEE!HHHH	3uSzz??K	M 	M 	M 	 	AEE$KKKK	r   c                     t          j                    }	 |                                 |                     d           d S # t          $ r Y d S w xY wN(Did not detect task count going negative)rA   JoinableQueuerk   r(   
ValueErrorr   rb   s     r   test_queue_task_donez"BaseQueueTest.test_queue_task_done   sb    !!	BKKMMM II@AAAAA  	 	 	DD	s   A   
AAc                     t          j                    }|                     |           |                     |           	 |                                 |                     d           d S # t
          $ r Y d S w xY wru   )rA   rw   rs   rk   r(   rx   ry   s     r   test_queue_joinzBaseQueueTest.test_queue_join   s     !!QQ	BKKMMM II@AAAAA  	 	 	DD	s   A* *
A87A8c                    g d}t          j        |          }|D ]g}|                     |                    d                     |                     ||                                           |                                 h	 |                                 |                     d           n# t          $ r Y nw xY w| 	                    |                    d                     d S )N)rD         itemsgMbP?rL   rv   )
rA   rw   r\   r)   rX   rW   rk   r(   rx   r^   )r   lrb   rg   s       r   test_queue_task_done_with_itemsz-BaseQueueTest.test_queue_task_done_with_items   s    
 IIa((( 	 	AQVVEV22333Q(((KKMMMM	BKKMMM II@AAAA  	 	 	D	 	u--.....s   B/ /
B<;B<c                     |                      t                    }|                     |           |                     |           d S rj   )	type2testr[   rh   ry   s     r   test_simple_queuezBaseQueueTest.test_simple_queue   sC     NN:&&q!!!q!!!!!r   N)r   r   r   rA   r   r:   rh   rm   rs   rz   r|   r   r   r   r   r   r5   r5   R   s        I( ( (;D ;D ;Dz    	B 	B 	BB B B/ / /(" " " " "r   r5   c                       e Zd Zej        ZdS )LifoQueueTestN)r   r   r   rA   rB   r   r   r   r   r   r      s        IIIr   r   c                   "    e Zd Zej        Zd ZdS )PriorityQueueTestc                     d}d}|                      ||g          }|                     ||                                           |                     ||                                           d S )N)r~   b)rD   ar   )r   assertTupleEqual
get_nowait)r   item1item2rb   s       r   
test__initzPriorityQueueTest.test__init   se    NN%N00eQ\\^^444eQ\\^^44444r   N)r   r   r   rA   rC   r   r   r   r   r   r   r      s+        #I5 5 5 5 5r   r   c                       e Zd ZdS )FailingQueueExceptionN)r   r   r   r   r   r   r   r      s        Dr   r   c                        e Zd Zd Zd Zd ZdS )FailingQueuec                 P    d| _         d| _        t          j        j        | g|R   d S )NF)fail_next_putfail_next_getrA   r   )r   r   s     r   r   zFailingQueue.__init__   s3    ""T)D))))))r   c                 |    | j         rd| _         t          d          t          j                            | |          S NFzYou Lose)r   r   rA   _put)r   items     r   r   zFailingQueue._put   s;     	4!&D'
333{d+++r   c                 z    | j         rd| _         t          d          t          j                            |           S r   )r   r   rA   _getr   s    r   r   zFailingQueue._get   s9     	4!&D'
333{%%%r   N)r   r   r   r   r   r   r   r   r   r   r      sA        * * *, , ,
& & & & &r   r   c                       e Zd Zd Zd ZdS )FailingQueueTestc                 	   |                                 st          d          t          t          dz
            D ]}|                    |           d|_        |                     t                    5  |                    dd           d d d            n# 1 swxY w Y   d|_        |                     t                    5  |                    dd           d d d            n# 1 swxY w Y   |                    d	           |                     |	                                d
           d|_        |                     t                    5  | 
                    |j        d|j        d           d d d            n# 1 swxY w Y   |                    d	           d|_        |                     |j        d|j        dt                     |                    d	           |                     |	                                d
           |                                 |                     |	                                d           |                    d	           |                     |	                                d
           | 
                    |j        d|j        d           t          t                    D ]}|                                 |                     |                                 d           |                    d           d|_        |                     t                    5  |                                 d d d            n# 1 swxY w Y   |                     |                                 d           d|_        |                     t                    5  |                    d           d d d            n# 1 swxY w Y   |                     |                                 d           |                                 |                     |                                 d           d|_        |                     |j        d|j        dt                     |                     |                                 d           |                                 |                     |                                 d           d S )Nr<   rD   Toopsr   rJ   g?rL   rG   rH   rN   r   rO   rF   rP   firstrE   rQ   )rR   rS   rZ   r[   rT   r   r1   r   r^   r]   r/   rW   r3   r\   r   rr   s      r   failing_queue_testz#FailingQueueTest.failing_queue_test  s@   wwyy 	IGHHHz!|$$ 	 	AEE!HHHH455 	# 	#EE&E"""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 455 	' 	'EE&#E&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	'	c


"8999455 	< 	<!!!%;;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<
 	
c


))!%!%*?	A 	A 	A 	
c


"8999	#=>>>	c


"8999aeVQUB777z"" 	 	AEEGGGG		#:;;;	g455 	 	EEGGG	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	$?@@@455 	 	EE#E	 	 	 	 	 	 	 	 	 	 	 	 	 	 	$?@@@			#:;;;))!%QUJ*?	A 	A 	A 	$?@@@			#:;;;;;sZ   4BBBC((C,/C,#F  FF0MMM%OOOc                     t          t                    }|                     |           |                     |           d S rj   )r   r[   r   ry   s     r   test_failing_queuez#FailingQueueTest.test_failing_queueB  s?     $$""""""""r   N)r   r   r   r   r   r   r   r   r   r     s3        ;< ;< ;<z# # # # #r   r   __main__)geventr   	patch_allr   rA   r   r   unittestr[   r   r   objectr    TestCaser5   r   r   	Exceptionr   r   r   r   mainr   r   r   <module>r      s              ! ! ! ! ! !       
    Y%   DF F F F F F F FBM" M" M" M" M"H%'8 M" M" M"^         M      5 5 5 5 5 5 5 5	 	 	 	 	I 	 	 	& & & & &5; & & & D# D# D# D# D#x(*; D# D# D#N zHMOOOOO r   