B
    u9aGL  ã               @   s„  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 yd dlZW n ek
rx   dZY nX dZdd„ ZG dd„ dejƒZG dd	„ d	ƒZG d
d„ deƒZG dd„ deejƒZG dd„ deejƒZG dd„ deejƒZG dd„ deƒZG dd„ dejƒZG dd„ deejƒZG dd„ dƒZG dd„ deejƒZe edkd¡G dd„ deejƒƒZe dkr€e !¡  dS ) é    N)Úsupporté   c             C   s   | j dko|  ¡ | j kS )Nr   )ÚmaxsizeÚqsize)Úq© r   ú /usr/lib/python3.7/test_queue.pyÚqfull   s    r	   c               @   s   e Zd Zdd„ Zdd„ ZdS )Ú_TriggerThreadc             C   s&   || _ || _t ¡ | _tj | ¡ d S )N)ÚfnÚargsÚ	threadingZEventÚstartedEventÚThreadÚ__init__)Úselfr   r   r   r   r   r      s    
z_TriggerThread.__init__c             C   s$   t  d¡ | j ¡  | j| jŽ  d S )Ngš™™™™™¹?)Ú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               @   s   e Zd Zdd„ Zdd„ ZdS )ÚBlockingTestMixinc          
   C   sN   t ||ƒ}| ¡  z(||Ž | _|j ¡ s6|  d| ¡ | jS t |d¡ X d S )Nz*blocking function %r appeared not to blocké
   )r
   ÚstartÚresultr   Úis_setÚfailr   Újoin_thread)r   Ú
block_funcÚ
block_argsÚtrigger_funcÚtrigger_argsÚthreadr   r   r   Údo_blocking_test<   s    


z"BlockingTestMixin.do_blocking_testc          
   C   sp   t ||ƒ}| ¡  z6y||Ž  W n |k
r6   ‚ Y nX |  d| ¡ W d t |d¡ |j ¡ sj|  d¡ X d S )Nzexpected exception of kind %rr   z(trigger thread ended but event never set)r
   r   r   r   r   r   r   )r   r    r!   r"   r#   Zexpected_exception_classr$   r   r   r   Údo_exceptional_blocking_testJ   s    

z.BlockingTestMixin.do_exceptional_blocking_testN)r   r   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d„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚBaseQueueTestMixinc             C   s   d| _ t ¡ | _d S )Nr   )Úcumr   ZLockÚcumlock)r   r   r   r   ÚsetUp]   s    zBaseQueueTestMixin.setUpc             C   s|  |  ¡ rtdƒ‚|  | ¡ ¡ |  | ¡ ¡ | d¡ | d¡ | d¡ tdddgdddgdddgd}| ¡ | ¡ | ¡ g}|  	|||j
j d¡ x.ttd ƒD ]}| |¡ |  |  ¡ d¡ q¤W |  t|ƒ d	¡ d
t }dt }| |¡ |  t|ƒd¡ |  | ¡ ¡ |  | ¡ ¡ y|j|dd |  d¡ W n tjk
rR   Y nX y|j|dd |  d¡ W n tjk
rˆ   Y nX |  |j|f|jd¡ |  |j|ddf|jd¡ xttƒD ]}| ¡  qÄW |  |  ¡  d¡ y|jdd |  d¡ W n tjk
r   Y nX y|jdd |  d¡ W n tjk
rN   Y nX |  |jd|jd¡ |  |jd|jd¡ d S )Nz&Call this function with an empty queueéo   iM  éÞ   )ÚQueueÚ	LifoQueueÚPriorityQueuez&Didn't seem to queue the correct data!é   zQueue should not be emptyzQueue should not be fullé   é   zQueue should be fullr   )Úblockz(Didn't appear to block with a full queueg{®Gáz„?)Útimeoutz+Didn't appear to time-out with a full queuer   Tr   z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   )r   ÚRuntimeErrorÚ
assertTruer5   ÚassertFalseÚfullÚputÚdictÚgetÚassertEqualÚ	__class__r   ÚrangeÚ
QUEUE_SIZEr	   r   ÚqueueÚFullr%   ÚEmpty)r   r   Ztarget_orderZactual_orderÚiÚlastr9   r   r   r   Úbasic_queue_testa   sd    





z#BaseQueueTestMixin.basic_queue_testc          	   C   sN   xH|  ¡ }|dk r| ¡  d S | j |  j|7  _W d Q R X | ¡  qW d S )Nr   )r<   Ú	task_doner)   r(   )r   r   Úxr   r   r   Úworkerš   s    zBaseQueueTestMixin.workerc             C   s²   d| _ g }x0dD ](}tj| j|fd}| ¡  | |¡ qW xtdƒD ]}| |¡ qFW | ¡  |  	| j t
tdƒƒd¡ xdD ]}| d¡ q~W | ¡  x|D ]}| ¡  qžW d S )Nr   )r   r0   )Útargetr   éd   z0q.join() did not block until all tasks were doneéÿÿÿÿ)r(   r   r   rI   r   Úappendr?   r:   Újoinr=   Úsum)r   r   ZthreadsrD   r$   r   r   r   Úqueue_join_test¤   s     


z"BaseQueueTestMixin.queue_join_testc             C   s8   |   ¡ }y| ¡  W n tk
r(   Y nX |  d¡ d S )Nz(Did not detect task count going negative)Ú	type2testrG   Ú
ValueErrorr   )r   r   r   r   r   Útest_queue_task_done¶   s    z'BaseQueueTestMixin.test_queue_task_donec             C   sL   |   ¡ }|  |¡ |  |¡ y| ¡  W n tk
r<   Y nX |  d¡ d S )Nz(Did not detect task count going negative)rQ   rP   rG   rR   r   )r   r   r   r   r   Útest_queue_joinÀ   s    

z"BaseQueueTestMixin.test_queue_joinc             C   s"   |   t¡}|  |¡ |  |¡ d S )N)rQ   r@   rF   )r   r   r   r   r   Ú
test_basicÍ   s    

zBaseQueueTestMixin.test_basicc          	   C   sV   |   t¡}|  t¡ |jddd W d Q R X |  t¡ |jddd W d Q R X d S )Nr0   rL   )r4   )rQ   r@   ÚassertRaisesrR   r:   r<   )r   r   r   r   r   Ú&test_negative_timeout_raises_exceptionÔ   s
    
z9BaseQueueTestMixin.test_negative_timeout_raises_exceptionc          	   C   s†   |   t¡}xttƒD ]}| d¡ qW |  tj¡ | d¡ W d Q R X xttƒD ]}| ¡  qRW |  tj¡ | ¡  W d Q R X d S )Nr0   )	rQ   r@   r?   Ú
put_nowaitrV   rA   rB   Ú
get_nowaitrC   )r   r   rD   r   r   r   Útest_nowaitÛ   s    
zBaseQueueTestMixin.test_nowaitc          	   C   s†   |   d¡}| d¡ | d¡ | d¡ |  tj¡ | d¡ W d Q R X |  | ¡ d¡ d|_|  tj¡ | d¡ W d Q R X d S )Né   r0   r1   é   )	rQ   r:   rV   rA   rB   rX   r=   r   r   )r   r   r   r   r   Útest_shrinking_queueç   s    



z'BaseQueueTestMixin.test_shrinking_queueN)r   r   r   r*   rF   rI   rP   rS   rT   rU   rW   rZ   r]   r   r   r   r   r'   \   s   9

r'   c               @   s   e Zd ZejZdS )Ú	QueueTestN)r   r   r   rA   r-   rQ   r   r   r   r   r^   ô   s   r^   c               @   s   e Zd ZejZdS )ÚLifoQueueTestN)r   r   r   rA   r.   rQ   r   r   r   r   r_   ÷   s   r_   c               @   s   e Zd ZejZdS )ÚPriorityQueueTestN)r   r   r   rA   r/   rQ   r   r   r   r   r`   ú   s   r`   c               @   s   e Zd ZdS )ÚFailingQueueExceptionN)r   r   r   r   r   r   r   ra      s   ra   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| f|žŽ  d S )NF)Úfail_next_putÚfail_next_getrA   r-   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)rc   ra   rA   r-   Ú_put)r   Úitemr   r   r   re     s    zFailingQueue._putc             C   s    | j rd| _ tdƒ‚tj | ¡S )NFzYou Lose)rd   ra   rA   r-   Ú_get)r   r   r   r   rg     s    zFailingQueue._getN)r   r   r   r   re   rg   r   r   r   r   rb     s   rb   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚFailingQueueTestc             C   sô  |  ¡ rtdƒ‚xttd ƒD ]}| |¡ qW d|_y|jddd |  d¡ W n tk
rf   Y nX d|_y|jddd	 |  d¡ W n tk
rž   Y nX | d
¡ |  t	|ƒd¡ d|_y"|  
|jd|jd¡ |  d¡ W n tk
rö   Y nX | d
¡ d|_y$|  |jd|jdt¡ |  d¡ W n tk
rB   Y nX | d
¡ |  t	|ƒd¡ | ¡  |  t	|ƒ d¡ | d
¡ |  t	|ƒd¡ |  
|jd|jd¡ xttƒD ]}| ¡  q°W |  |  ¡  d¡ | d¡ d|_y| ¡  |  d¡ W n tk
r   Y nX |  |  ¡ d¡ d|_y|jdd	 |  d¡ W n tk
rX   Y nX |  |  ¡ d¡ | ¡  |  |  ¡  d¡ d|_y$|  |jd|jdt¡ |  d¡ W n tk
rÄ   Y nX |  |  ¡ d¡ | ¡  |  |  ¡  d¡ d S )Nz&Call this function with an empty queuer0   TZoopsr   )r3   z)The queue didn't fail when it should havegš™™™™™¹?)r4   rE   zQueue should be full)r9   r   )r9   Tr   zQueue should not be fullzQueue should be emptyÚfirstzQueue should not be empty)r5   )r   r6   r?   r@   r:   rc   r   ra   r7   r	   r%   r<   r&   rd   )r   r   rD   r   r   r   Úfailing_queue_test  sŠ    




z#FailingQueueTest.failing_queue_testc             C   s    t tƒ}|  |¡ |  |¡ d S )N)rb   r@   rj   )r   r   r   r   r   Útest_failing_queuef  s    
z#FailingQueueTest.test_failing_queueN)r   r   r   rj   rk   r   r   r   r   rh     s   Qrh   c               @   st   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d„ Zdd„ ZdS )ÚBaseSimpleQueueTestc             C   s   |   ¡ | _d S )N)rQ   r   )r   r   r   r   r*   p  s    zBaseSimpleQueueTest.setUpc             C   sT   xNy|  ¡ }W n tk
r"   d S X | |¡ | ¡ dkrt | ¡ d ¡ qW d S )Ng      à?gü©ñÒMbP?)ÚpopÚ
IndexErrorr:   Úrandomr   r   )r   r   ÚseqÚrndÚvalr   r   r   Úfeeds  s    
zBaseSimpleQueueTest.feedc             C   s(   x"|  ¡ }||krd S | |¡ qW d S )N)r<   rM   )r   r   ÚresultsÚsentinelrr   r   r   r   Úconsume}  s
    zBaseSimpleQueueTest.consumec             C   sZ   xTx8y|j dd}W n  tjk
r4   t d¡ Y qX P qW ||krHd S | |¡ qW d S )NF)r3   gñhãˆµøä>)r<   rA   rC   r   r   rM   )r   r   rt   ru   rr   r   r   r   Úconsume_nonblock„  s    z$BaseSimpleQueueTest.consume_nonblockc             C   sP   xJx.y|j dd}W n tjk
r*   Y qX P qW ||kr>d S | |¡ qW d S )Ngñhãˆµøä>)r4   )r<   rA   rC   rM   )r   r   rt   ru   rr   r   r   r   Úconsume_timeout‘  s    z#BaseSimpleQueueTest.consume_timeoutc       	   	      sº   g ‰d ‰|ˆg|  ‰ˆ  ¡  t d¡‰g ‰‡fdd„‰‡‡‡‡‡fdd„t|ƒD ƒ}‡ ‡‡‡‡fdd„t|ƒD ƒ}t || ¡ W d Q R X |  ˆ¡ |  ˆ ¡ ¡ |  	ˆ 
¡ d¡ ˆS )Né*   c                s   ‡‡ fdd„}|S )Nc           
      s@   yˆ| |Ž W n, t k
r: } zˆ  |¡ W d d }~X Y nX d S )N)ÚBaseExceptionrM   )r   ÚkwargsÚe)Ú
exceptionsÚfr   r   Úwrapper¨  s    zHBaseSimpleQueueTest.run_threads.<locals>.log_exceptions.<locals>.wrapperr   )r~   r   )r}   )r~   r   Úlog_exceptions§  s    z7BaseSimpleQueueTest.run_threads.<locals>.log_exceptionsc                s$   g | ]}t jˆˆ ƒˆˆˆfd ‘qS ))rJ   r   )r   r   )Ú.0rD   )Ú	feed_funcr€   r   rq   rp   r   r   ú
<listcomp>¯  s   z3BaseSimpleQueueTest.run_threads.<locals>.<listcomp>c                s$   g | ]}t jˆˆ ƒˆˆˆfd ‘qS ))rJ   r   )r   r   )r   rD   )Úconsume_funcr€   r   rt   ru   r   r   rƒ   ²  s   r   )Úreversero   ZRandomr?   r   Zstart_threadsr8   r7   r5   r=   r   )	r   Z	n_feedersZn_consumersr   Úinputsr‚   r„   ZfeedersZ	consumersr   )	r„   r}   r‚   r€   r   rt   rq   ru   rp   r   Úrun_threadsž  s"    


zBaseSimpleQueueTest.run_threadsc          	   C   s¶  | j }|  | ¡ ¡ |  | ¡ d¡ | d¡ |  | ¡ ¡ |  | ¡ d¡ | d¡ | d¡ | d¡ |  | ¡ ¡ |  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ |  | 	¡ d¡ |  | ¡ d¡ |  |jddd¡ |  | ¡ ¡ |  | ¡ d¡ |  |jdd	d¡ |  | ¡ ¡ |  | ¡ d¡ |  
tj¡ |jdd W d Q R X |  
tj¡ |jd
d	 W d Q R X |  
tj¡ | 	¡  W d Q R X |  | ¡ ¡ |  | ¡ d¡ d S )Nr   r0   r1   r[   r\   F)r3   gš™™™™™¹?)r4   gü©ñÒMbP?)r   r7   r5   r=   r   r:   r8   rX   r<   rY   rV   rA   rC   )r   r   r   r   r   rU   ¿  s:    



zBaseSimpleQueueTest.test_basicc          	   C   s6   | j }| d¡ |  t¡ |jdd W d Q R X d S )Nr0   rL   )r4   )r   r:   rV   rR   r<   )r   r   r   r   r   rW   ä  s    
z:BaseSimpleQueueTest.test_negative_timeout_raises_exceptionc             C   s:   | j }ttdƒƒ}|  dd||| j| j¡}|  ||¡ d S )NrK   r0   )r   Úlistr?   r‡   rs   rv   r=   )r   r   r†   rt   r   r   r   Ú
test_orderê  s    zBaseSimpleQueueTest.test_orderc             C   sB   d}| j }ttdƒƒ}|  ||||| j| j¡}|  t|ƒ|¡ d S )Né2   i'  )r   rˆ   r?   r‡   rs   rv   r=   Úsorted)r   ÚNr   r†   rt   r   r   r   Útest_many_threadsó  s
    z%BaseSimpleQueueTest.test_many_threadsc             C   sB   d}| j }ttdƒƒ}|  ||||| j| j¡}|  t|ƒ|¡ d S )NrŠ   i'  )r   rˆ   r?   r‡   rs   rw   r=   r‹   )r   rŒ   r   r†   rt   r   r   r   Útest_many_threads_nonblockþ  s    z.BaseSimpleQueueTest.test_many_threads_nonblockc             C   sB   d}| j }ttdƒƒ}|  ||||| j| j¡}|  t|ƒ|¡ d S )NrŠ   iè  )r   rˆ   r?   r‡   rs   rx   r=   r‹   )r   rŒ   r   r†   rt   r   r   r   Útest_many_threads_timeout  s    z-BaseSimpleQueueTest.test_many_threads_timeoutc             C   sf   G dd„ dƒ}d}| j }xt|ƒD ]}| |ƒ ¡ q"W x*t|ƒD ]}t | ¡ ¡}|  |ƒ ¡ q@W d S )Nc               @   s   e Zd ZdS )z.BaseSimpleQueueTest.test_references.<locals>.CN)r   r   r   r   r   r   r   ÚC  s   r   é   )r   r?   r:   ÚweakrefÚrefr<   ZassertIsNone)r   r   rŒ   r   rD   Zwrr   r   r   Útest_references  s    z#BaseSimpleQueueTest.test_referencesN)r   r   r   r*   rs   rv   rw   rx   r‡   rU   rW   r‰   r   rŽ   r   r”   r   r   r   r   rl   n  s   
!%	

rl   c               @   s   e Zd ZejZdS )ÚPySimpleQueueTestN)r   r   r   rA   Z_PySimpleQueuerQ   r   r   r   r   r•   !  s   r•   zNo _queue module foundc                   s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚCSimpleQueueTestc                s   t j| _tƒ  ¡  d S )N)Ú_queueÚSimpleQueuerQ   Úsuperr*   )r   )r>   r   r   r*   (  s    zCSimpleQueueTest.setUpc             C   s   |   | jtj¡ d S )N)ZassertIsrQ   rA   r˜   )r   r   r   r   Útest_is_default,  s    z CSimpleQueueTest.test_is_defaultc                s€   | j ‰t ¡ ‰ d}g }G ‡ ‡fdd„dtƒ}x6|ƒ }ˆ tˆ ƒ¡ ~| ˆ ¡ ¡ |d |kr.P q.W |  |t	t
|d ƒƒ¡ d S )Ni'  c                   s"   e Zd Zdd„ Z‡ ‡fdd„ZdS )z2CSimpleQueueTest.test_reentrancy.<locals>.Circularc             S   s
   | | _ d S )N)Zcircular)r   r   r   r   r   ;  s    z;CSimpleQueueTest.test_reentrancy.<locals>.Circular.__init__c                s   ˆ  tˆ ƒ¡ d S )N)r:   Únext)r   )Úgenr   r   r   Ú__del__>  s    z:CSimpleQueueTest.test_reentrancy.<locals>.Circular.__del__N)r   r   r   r   r   r   )rœ   r   r   r   ÚCircular:  s   rž   rL   r0   )r   Ú	itertoolsÚcountÚobjectr:   r›   rM   r<   r=   rˆ   r?   )r   rŒ   rt   rž   Úor   )rœ   r   r   Útest_reentrancy/  s    z CSimpleQueueTest.test_reentrancy)r   r   r   r*   rš   r£   Ú__classcell__r   r   )r>   r   r–   %  s   r–   Ú__main__)"ÚcollectionsrŸ   rA   ro   Úsysr   r   Zunittestr’   Ztestr   r—   ÚImportErrorr@   r	   r   r
   r   r'   ZTestCaser^   r_   r`   Ú	Exceptionra   r-   rb   rh   rl   r•   ZskipIfr–   r   Úmainr   r   r   r   Ú<module>   s@   
 " [ 4&
