B
    H/\v2                 @   s  d dl Z d dlmZ d dlmZmZ d dlZd dlmZm	Z	 d dlm
Z
 d dlmZ d dlmZmZ d dlmZ d dlmZ G d	d
 d
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ZG dd deZG dd de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ZG dd  d 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Z#e$ed)rG d*d+ d+eZ%[e&d,kre  dS )-    N)TestCasemain)get_hubLoopExit)util)queue)EmptyFull)AsyncResult)AbstractGenericGetTestCasec               @   sd   e Zd 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d Zdd Zdd ZdS )	TestQueuec             C   s<   d| _ t }|d | | d | | d d S )NFhi)switch_expectedr   QueueputassertEqualpeekget)selfq r   7lib/python3.7/site-packages/gevent/tests/test__queue.pytest_send_first   s
    
zTestQueue.test_send_firstc                sD   t  } t|j  fdd}t||}td |  d S )Nc                s    j t| jdd d S )Ng{Gz?)timeout)assertRaisesr   r   )r   )r   r   r   waiter   s    z)TestQueue.test_peek_empty.<locals>.waiterg?)	r   r   r   r   r   geventspawnsleepjoin)r   r   r   gr   )r   r   test_peek_empty   s    
zTestQueue.test_peek_emptyc             C   s\   t  }t|j}|  td |d |  | 	|j
d k | | d d S )Nr      )r   r   r   r   r   startr   r   r   
assertTrueZ	exceptionr   )r   r   r    r   r   r   test_peek_multi_greenlet#   s    

z"TestQueue.test_peek_multi_greenletc                sV   t  } fdd}t||}td |d td |jdddksRtd S )Nc          	      s4   t tjsdnd  |  d W d Q R X dS )Ng?g      ?hi2OK)r   Timeout	greentestZRUNNING_ON_APPVEYORr   r   )r   )r   r   r   r   0   s    z(TestQueue.test_send_last.<locals>.waiterg{Gz?r&   r   )r   r'   )r   r   r   r   r   r   r   AssertionError)r   r   r   pr   )r   r   test_send_last-   s    


zTestQueue.test_send_lastc                s   t d}g   fdd}t||}td |  ddg | | d td |  dddg | | d | | d |jddd	kstd S )
N   c                s@   |  d  d |  d  d |  d  d dS )Nabcr'   )r   append)r   )resultsr   r   putter?   s    





z'TestQueue.test_max_size.<locals>.putterr   r.   r/   r0   )r   r'   )r   r   r   r   r   r   r   r*   )r   r   r3   r+   r   )r2   r   test_max_size;   s    
	

zTestQueue.test_max_sizec          	   C   s   t  }dd }dd }t }t }t|||}td | |   t|||}| |	 d | |	 d t
d t||g W d Q R X d S )	Nc             S   s   | d | d d S )Nr   done)r   set)evtr   r   r   r   senderU   s    
z,TestQueue.test_zero_max_size.<locals>.senderc             S   s   |  }| | d S )N)r   r6   )r7   r   xr   r   r   receiverY   s    z.TestQueue.test_zero_max_size.<locals>.receivergMbP?r   r5   r   )r   Channelr
   r   r   r   r$   readyr   r   r(   Zjoinall)r   r   r8   r:   e1e2Zp1Zp2r   r   r   test_zero_max_sizeR   s    
zTestQueue.test_zero_max_sizec                s   t  }dd }ddddg}dd |D  x&t|D ]\}}t|| |  q4W td	 t  fd
d}||d  | | d ||d  | | d ||d  ||d  | | d d S )Nc             S   s   | |   d S )N)r6   r   )r   r7   r   r   r   r   m   s    z/TestQueue.test_multiple_waiters.<locals>.waiter1234c             S   s   g | ]
}t  qS r   )r
   ).0r9   r   r   r   
<listcomp>q   s    z3TestQueue.test_multiple_waiters.<locals>.<listcomp>g{Gz?c           
      s@   x6 D ].} t dd |  }| W d Q R X qW tS )NgMbP?F)r   r(   r   addlen)er9   )evtsr2   r   r   collect_pending_resultsy   s
    
z@TestQueue.test_multiple_waiters.<locals>.collect_pending_resultsr   r"   r-         )	r   r   	enumerater   r   r   r6   r   r   )r   r   r   Zsendingsi_rJ   r   )rI   r2   r   test_multiple_waitersi   s     
zTestQueue.test_multiple_waitersc             C   sR   t  }dd }t }t||| | | d |d | | d d S )Nc          	   S   sT   t dt < y|  }|| W n tk
rD   |d Y nX W d Q R X d S )Nr   z	timed out)r   r(   RuntimeErrorr   r6   )r   r7   resultr   r   r   
do_receive   s    z6TestQueue.test_waiters_that_cancel.<locals>.do_receivez	timed outr   )r   r   r
   r   r   r   r   r   )r   r   rS   r7   r   r   r   test_waiters_that_cancel   s    
z"TestQueue.test_waiters_that_cancelc             C   s0   t  }dd }t|| | | d d S )Nc             S   s   |  d d S )Nsent)r   )r   r   r   r   do_send   s    z0TestQueue.test_senders_that_die.<locals>.do_sendrU   )r   r   r   r   r   r   )r   r   rV   r   r   r   test_senders_that_die   s    zTestQueue.test_senders_that_diec             C   sx   dd }dd }t  }t }t }t||| t||| td |d | | d | | d d S )Nc             S   s   | |   d S )N)r6   r   )r   r7   r   r   r   r      s    z3TestQueue.test_two_waiters_one_dies.<locals>.waiterc          	   S   sT   t dt < y|  }|| W n tk
rD   |d Y nX W d Q R X d S )Nr   z	timed out)r   r(   rQ   r   r6   )r   r7   rR   r   r   r   rS      s    z7TestQueue.test_two_waiters_one_dies.<locals>.do_receiveg?r   z	timed out)	r   r   r
   r   r   r   r   r   r   )r   r   rS   r   Z	dying_evtZwaiting_evtr   r   r   test_two_waiters_one_dies   s    

z#TestQueue.test_two_waiters_one_diesc             C   s   dd }t  }t }t }t||| t||| td |d | | d | | d | | d d S )Nc          	   S   sT   t dt < y|  }|| W n tk
rD   |d Y nX W d Q R X d S )Nr   z	timed out)r   r(   rQ   r   r6   )r   r7   rR   r   r   r   rS      s    z4TestQueue.test_two_bogus_waiters.<locals>.do_receiveg?rU   z	timed out)	r   r   r
   r   r   r   r   r   r   )r   rS   r   r=   r>   r   r   r   test_two_bogus_waiters   s    

z TestQueue.test_two_bogus_waitersN)__name__
__module____qualname__r   r!   r%   r,   r4   r?   rP   rT   rW   rX   rY   r   r   r   r   r      s   
	r   c               @   s$   e Zd Zdd Zdd Zdd ZdS )TestChannelc                sx   t   g  fdd}t|}d  d d  d d | dddddg |  d S )Nc                  s            d S )N)r1   r   r   )channeleventsr   r   another_greenlet   s    z/TestChannel.test_send.<locals>.another_greenletZsendinghelloz
sent helloworldz
sent world)r   r;   r   r   r1   r   r   r   )r   r`   r    r   )r^   r_   r   	test_send   s    





zTestChannel.test_sendc                s   t   g  fdd}t|}d       | dddddg td | dddddd	g |  d S )
Nc                  s6    d  d  d  d  d d S )Nzsending hellora   zsending worldrb   z
sent world)r1   r   r   )r^   r_   r   r   r`      s
    



z/TestChannel.test_wait.<locals>.another_greenletZwaitingzsending hellora   zsending worldrb   r   z
sent world)r   r;   r   r   r1   r   r   r   )r   r`   r    r   )r^   r_   r   	test_wait   s    


zTestChannel.test_waitc             C   s.   t  }t|jt t|}| |g  d S )N)r   r;   r   r   r   StopIterationlistr   )r   r^   rr   r   r   test_iterable   s    zTestChannel.test_iterableN)rZ   r[   r\   rc   rd   rh   r   r   r   r   r]      s   r]   c               @   s   e Zd Zdd ZdS )TestJoinableQueuec             C   sX   t  }t }t|j| | }| || | d|j	 |
  | d|j	 d S )Nr"   r   )r   JoinableQueueobjectr   r   r   r   assertIsr   Zunfinished_tasksZ	task_done)r   r^   XrR   r   r   r   test_task_done  s    z TestJoinableQueue.test_task_doneN)rZ   r[   r\   rn   r   r   r   r   ri     s   ri   c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )
TestNoWaitc                s   g  t d} fdd}t jj}||tt|jd ||tt|jd t	
d t dksnt  d d kst t d t jst d S )Nr"   c                s     | |  d S )N)r1   )funcargs)rR   r   r   store_result  s    z7TestNoWait.test_put_nowait_simple.<locals>.store_resultr-   rK   r   )r   r   r   looprun_callbackr   wrap_errorsr	   
put_nowaitr   r   rG   r*   
isinstance)r   r   rr   rt   r   )rR   r   test_put_nowait_simple  s    


z!TestNoWait.test_put_nowait_simplec                s   g  t d}|d  fdd}t jj}||tt|j	 ||tt|j	 t
d t dkstt  d dkst t d t jst d S )Nr"   rL   c                s     | |  d S )N)r1   )rp   rq   )rR   r   r   rr   %  s    z7TestNoWait.test_get_nowait_simple.<locals>.store_resultr   r-   )r   r   r   r   rs   rt   r   ru   r   
get_nowaitr   r   rG   r*   rw   )r   r   rr   rt   r   )rR   r   test_get_nowait_simple   s    



z!TestNoWait.test_get_nowait_simplec                s   g  t d}t|jd} fdd}| s8t|td | sRt|t	 j
||j td | s~t| dgkst | st||jst|| st|d S )Nr"      c                s     | |  d S )N)r1   )rp   rq   )rR   r   r   rr   7  s    z7TestNoWait.test_get_nowait_unlock.<locals>.store_resultr   )r   r   r   r   r   emptyr*   r   fullr   rs   rt   ry   r<   dead)r   r   r+   rr   r   )rR   r   test_get_nowait_unlock2  s    


z!TestNoWait.test_get_nowait_unlockc                s   g  t  }t|jd} fdd}| s6t|| sFt|td | s`t|| spt|t	 j
||j td | st|| st| dgkst | st||jst|| st|d S )Nr{   c                s     | |  d S )N)r1   )rp   rq   )rR   r   r   rr   J  s    z?TestNoWait.test_get_nowait_unlock_channel.<locals>.store_resultgMbP?)r   r;   r   r   r   r|   r*   r}   r   r   rs   rt   ry   r<   r~   )r   r   r+   rr   r   )rR   r   test_get_nowait_unlock_channelE  s"    

z)TestNoWait.test_get_nowait_unlock_channelc                s   g  t  }t|j} fdd}| | | | | | t	d | | | | | | t
 j||jd | | | t	d |  d g | | | | | | | | | d S )Nc                s     | |  d S )N)r1   )rp   rq   )rR   r   r   rr   a  s    z7TestNoWait.test_put_nowait_unlock.<locals>.store_resultgMbP?
   )r   r   r   r   r   r$   r|   ZassertFalser}   r   r   rs   rt   rv   r<   r   )r   r   r+   rr   r   )rR   r   test_put_nowait_unlock\  s     

z!TestNoWait.test_put_nowait_unlockN)rZ   r[   r\   rx   rz   r   r   r   r   r   r   r   ro     s
   ro   c               @   s   e Zd Zdd ZdS )TestJoinEmptyc             C   s   d| _ t }|  dS )zFTest that join() exits immediately if not jobs were put into the queueFN)r   r   rj   r   )r   r   r   r   r   test_issue_45x  s    zTestJoinEmpty.test_issue_45N)rZ   r[   r\   r   r   r   r   r   r   v  s   r   c               @   s   e Zd Zdd ZdS )AbstractTestWeakRefMixinc             C   s,   dd l }|  }||}| ||  d S )Nr   )weakref_makeOnerefrl   )r   r   Zoner   r   r   r   test_weak_reference  s    
z,AbstractTestWeakRefMixin.test_weak_referenceN)rZ   r[   r\   r   r   r   r   r   r   ~  s   r   c               @   s&   e Zd ZeZejZdd Zdd Z	dS )TestGetInterruptc             C   s   |   j|dS )N)r   )r   r   )r   r   r   r   r   wait  s    zTestGetInterrupt.waitc             C   s   |   S )N)kind)r   r   r   r   r     s    zTestGetInterrupt._makeOneN)
rZ   r[   r\   r   r(   r   r   r   r   r   r   r   r   r   r     s   r   c               @   s   e Zd ZejZdS )TestGetInterruptJoinableQueueN)rZ   r[   r\   r   rj   r   r   r   r   r   r     s   r   c               @   s   e Zd ZejZdS )TestGetInterruptLifoQueueN)rZ   r[   r\   r   	LifoQueuer   r   r   r   r   r     s   r   c               @   s   e Zd ZejZdS )TestGetInterruptPriorityQueueN)rZ   r[   r\   r   PriorityQueuer   r   r   r   r   r     s   r   c               @   s   e Zd ZejZdS )TestGetInterruptChannelN)rZ   r[   r\   r   r;   r   r   r   r   r   r     s   r   c                   s6   e Zd ZejZeZ fddZdd Z	dd Z
  ZS )TestPutInterruptc                s   t t|   |  | _d S )N)superr   setUpr   r   )r   )	__class__r   r   r     s    zTestPutInterrupt.setUpc             C   s,   x| j  s| j d qW | j jd|dS )Nr"   r-   )r   )r   r}   r   )r   r   r   r   r   r     s    zTestPutInterrupt.waitc             C   s
   |  dS )Nr"   )r   )r   r   r   r   r     s    zTestPutInterrupt._makeOne)rZ   r[   r\   r   r   r   r	   r(   r   r   r   __classcell__r   r   )r   r   r     s
   r   c               @   s   e Zd ZejZdS )TestPutInterruptJoinableQueueN)rZ   r[   r\   r   rj   r   r   r   r   r   r     s   r   c               @   s   e Zd ZejZdS )TestPutInterruptLifoQueueN)rZ   r[   r\   r   r   r   r   r   r   r   r     s   r   c               @   s   e Zd ZejZdS )TestPutInterruptPriorityQueueN)rZ   r[   r\   r   r   r   r   r   r   r   r     s   r   c               @   s   e Zd ZejZdd ZdS )TestPutInterruptChannelc             C   s   |   S )N)r   )r   r   r   r   r     s    z TestPutInterruptChannel._makeOneN)rZ   r[   r\   r   r;   r   r   r   r   r   r   r     s   r   SimpleQueuec               @   s"   e Zd ZejZdd ZeZeZdS )TestGetInterruptSimpleQueuec             C   s   t dd S )NzNot supported)unittestZSkipTest)r   r   r   r   test_raises_timeout_Timeout  s    z7TestGetInterruptSimpleQueue.test_raises_timeout_TimeoutN)	rZ   r[   r\   r   r   r   r   Z*test_raises_timeout_Timeout_exc_customizedZtest_outer_timeout_is_not_lostr   r   r   r   r     s   r   __main__)'r   Zgevent.testingZtestingr)   r   r   r   Z
gevent.hubr   r   r   r   Zgevent.queuer   r	   Zgevent.eventr
   Zgevent.testing.timingr   r   r]   ri   ro   r   rk   r   r   r   r   r   r   r   r   r   r   r   hasattrr   rZ   r   r   r   r   <module>   s>    A2h	

