o
    U˟i10                     @   s   d dl mZ e  d dl mZ d dlZd dlZd dlZdZG dd dej	Z
G dd deZG d	d
 d
ejeZG dd deZG dd deZG dd deZG dd dejZG dd dejeZedkrne  dS dS )    )monkey)queueN   c                   @      e Zd Zdd Zdd ZdS )_TriggerThreadc                 C   s0   || _ || _ddlm} | | _tj|  d S )Nr   )Event)fnargsgevent.eventr   startedEvent	threadingThread__init__)selfr   r	   r    r   g/var/www/apps/myagent/mysuperagent/venv/lib/python3.10/site-packages/gevent/tests/test__monkey_queue.pyr      s
   z_TriggerThread.__init__c                 C   s$   t d | j  | j| j  d S )N{Gz?)timesleepr   setr   r	   r   r   r   r   run   s   
	
z_TriggerThread.runN)__name__
__module____qualname__r   r   r   r   r   r   r      s    r   c                   @   r   )BlockingTestMixinc                 C   sd   t ||| _| j  || | _| jj s| d|  | jd | j r/| d|  | 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_argsr   r   r   do_blocking_test3   s   


z"BlockingTestMixin.do_blocking_testc              
   C   s   t ||| _| j  z;| | ||  W d    n1 s w   Y  W | jd | j r8| d|  | jj sE| d d S d S | jd | j rY| d|  | jj se| d w 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_classr   r   r   do_exceptional_blocking_testB   s.   



z.BlockingTestMixin.do_exceptional_blocking_testN)r   r   r   r)   r,   r   r   r   r   r   1   s    r   c                   @   sR   e Zd ZejZdd Zdd Zdd Zdd Zd	d
 Z	dd Z
dd Zdd ZdS )BaseQueueTestc                 C   s   d| _ t | _d S )Nr   )cumr   Lockcumlockr   r   r   r   setUpU   s   zBaseQueueTest.setUpc                 C   sf  |  std|d |d |d |d tdddd}| | f}| |||jj ||jj fd tg dg d	g d
d}| | | g}| |||jj d t	t
d D ]}|| | |  d qe| | d |d | | d z|jddd | d W n
 tjy   Y nw z|jddd | d W n
 tjy   Y nw | | t
 | |jd|jd | |jd|jd t	t
D ]}|  q| |  d z|jdd | d W n tjy   Y nw z|jdd | d W n tjy   Y n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!)r4   r5   r6   )r5   r4   r3   )r5   r4   r6   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r?   r   r?   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   )rH   RuntimeErrorputdictpeekgetassertEqual	__class__r   range
QUEUE_SIZEassertFalsefull
assertTruer"   r7   Fullqsizer)   Empty)r   qtarget_first_itemsactual_first_itemtarget_orderactual_orderir   r   r   simple_queue_testY   s   








zBaseQueueTest.simple_queue_testc                 C   s6   	 |  }|d u r|  d S |  j|7  _|  qN)rM   	task_doner.   )r   rX   xr   r   r   worker   s   zBaseQueueTest.workerc                 C   s~   d| _ dD ]}tj| j|fd  qtdD ]}|| q|  | | j t	tdd dD ]}|d  q1|  d S )Nr   )r   r:   )targetr	   d   z0q.join() did not block until all tasks were done)
r.   r   r   rb   r   rP   rJ   r#   rN   sumr   rX   r]   r   r   r   queue_join_test   s   zBaseQueueTest.queue_join_testc                 C   s8   t  }z|  W n
 ty   Y d S w | d d S N(Did not detect task count going negative)r7   JoinableQueuer`   
ValueErrorr"   r   rX   r   r   r   test_queue_task_done   s   z"BaseQueueTest.test_queue_task_donec                 C   sL   t  }| | | | z|  W n
 ty   Y d S w | d d S rh   )r7   rj   rg   r`   rk   r"   rl   r   r   r   test_queue_join   s   

zBaseQueueTest.test_queue_joinc                 C   s   g d}t j|d}|D ]}| |jdd | ||  |  qz|  W n	 ty3   Y nw | d | 	|jdd d S )N)r:         itemsgMbP?rB   ri   )
r7   rj   rR   r#   rN   rM   r`   rk   r"   rT   )r   lrX   r]   r   r   r   test_queue_task_done_with_items   s   

z-BaseQueueTest.test_queue_task_done_with_itemsc                 C   s"   |  t}| | | | d S r_   )	type2testrQ   r^   rl   r   r   r   test_simple_queue   s   

zBaseQueueTest.test_simple_queueN)r   r   r   r7   ru   r1   r^   rb   rg   rm   rn   rt   rv   r   r   r   r   r-   R   s    =
r-   c                   @   s   e Zd ZejZdS )LifoQueueTestN)r   r   r   r7   r8   ru   r   r   r   r   rw      s    
rw   c                   @   s   e Zd ZejZdd ZdS )PriorityQueueTestc                 C   s<   d}d}| j ||gd}| ||  | ||  d S )N)ro   b)r:   arq   )ru   assertTupleEqual
get_nowait)r   item1item2rX   r   r   r   
test__init   s
   zPriorityQueueTest.test__initN)r   r   r   r7   r9   ru   r   r   r   r   r   rx      s    rx   c                   @   s   e Zd ZdS )FailingQueueExceptionN)r   r   r   r   r   r   r   r      s    r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )FailingQueuec                 G   s$   d| _ d| _tjj| g|R   d S )NF)fail_next_putfail_next_getr7   r   )r   r	   r   r   r   r      s   zFailingQueue.__init__c                 C   s"   | j r
d| _ tdtj| |S NFzYou Lose)r   r   r7   _put)r   itemr   r   r   r      s   zFailingQueue._putc                 C   s    | j r
d| _ tdtj| S r   )r   r   r7   _getr   r   r   r   r      s   zFailingQueue._getN)r   r   r   r   r   r   r   r   r   r   r      s    r   c                   @   r   )FailingQueueTestc                 C   s  |  stdttd D ]}|| qd|_| t |jddd W d    n1 s0w   Y  d|_| t |jddd W d    n1 sOw   Y  |d	 | |	 d
 d|_| t | 
|jd|jd W d    n1 s~w   Y  |d	 d|_| |jd|jdt |d	 | |	 d
 |  | |	 d |d	 | |	 d
 | 
|jd|jd ttD ]}|  q| |  d |d d|_| t |  W d    n1 sw   Y  | |  d d|_| t |jdd W d    n	1 sw   Y  | |  d |  | |  d d|_| |jd|jdt | |  d |  | |  d d S )Nr2   r:   Toopsr   r@   g?rB   r=   r>   rD   r   rE   r<   rF   firstr;   rG   )rH   rI   rP   rQ   rJ   r   r*   r   rT   rS   r)   rM   r,   rR   r   rf   r   r   r   failing_queue_test  sj   






z#FailingQueueTest.failing_queue_testc                 C   s    t t}| | | | d S r_   )r   rQ   r   rl   r   r   r   test_failing_queueB  s   
z#FailingQueueTest.test_failing_queueN)r   r   r   r   r   r   r   r   r   r     s    =r   __main__)geventr   	patch_allr   r7   r   r   unittestrQ   r   r   objectr   TestCaser-   rw   rx   	Exceptionr   r   r   r   mainr   r   r   r   <module>   s&   "! G