U
    luf6                     @   s   d dl Z d dlmZ d dlm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 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dkre  dS )    N)	timedelta)random)genqueues)TimeoutError)gen_testAsyncTestCasec                   @   s(   e Zd Zdd Zdd Zedd ZdS )QueueBasicTestc                 C   s   t jdd}| tt|t| | tt|t| |  t|t|fD ]D}| 	|
d | d| | d| | d| | d| qT|d  |d  |d  t|t|fD ](}| d| | d	| | d
| qd S )N   maxsizez<Queuez	maxsize=1z
getters[1]puttersZtasksgettersz
putters[1]ztasks=2)r   QueueZassertInhexidreprZassertNotInstrget
assertTrue
startswithput)selfqZq_str r   </tmp/pip-unpacked-wheel-bmg6zs32/tornado/test/queues_test.pytest_repr_and_str   s"    


z QueueBasicTest.test_repr_and_strc                    sH   t   dD ]} | q fddtdD }| dddg| d S )N)r
         c                    s   g | ]}   qS r   )
get_nowait.0_r   r   r   
<listcomp>4   s     z-QueueBasicTest.test_order.<locals>.<listcomp>r   r
   r   )r   r   
put_nowaitrangeassertEqual)r   iitemsr   r#   r   
test_order/   s
    zQueueBasicTest.test_orderc                 c   s   | j ttjd d | j ttjdd tjdd}| |  | |  | 	d|j
 | |d  | |d  | |  | |  |d}| |  | 	d| V  | |  | |  | |  d S )Nr   r   r   r
   )assertRaises	TypeErrorr   r   
ValueErrorr   emptyassertFalsefullr'   r   r   doner   )r   r   Zput2r   r   r   test_maxsize7   s     
zQueueBasicTest.test_maxsizeN)__name__
__module____qualname__r   r*   r   r3   r   r   r   r   r	      s   r	   c                   @   s|   e Zd Zedd Zdd Zdd Zedd Zed	d
 Zedd Z	edd Z
edd Zedd Zedd ZdS )QueueGetTestc                 c   s(   t  }|d | d| V  d S Nr   )r   r   r%   r'   r   r   r   r   r   r   test_blocking_getM   s    
zQueueGetTest.test_blocking_getc                 C   s&   t  }|d | d|  d S r8   )r   r   r%   r'   r   r9   r   r   r   test_nonblocking_getS   s    
z!QueueGetTest.test_nonblocking_getc                 C   s   t  }| t j|j d S N)r   r   r,   Z
QueueEmptyr   r9   r   r   r   test_nonblocking_get_exceptionX   s    z+QueueGetTest.test_nonblocking_get_exceptionc                 c   s@   t d}|d |d}| d| V  | |V  d S Nr
   r   )r   r   r%   r   r'   r   ZassertIsNoner   r   r   r   r   r   test_get_with_putters\   s
    


z"QueueGetTest.test_get_with_puttersc                 c   sr   t  }|d | jd|jd | jd|jd | d|jtdddV  | d|jtdddV  d S )Nr   {Gz?r
   {Gz?r   secondstimeout)	r   r   r   io_loop
call_laterr%   r'   r   r   r9   r   r   r   test_blocking_get_waitd   s    
z#QueueGetTest.test_blocking_get_waitc              	   c   sZ   t  }|jtddd}| }| t |V  W 5 Q R X |d | d|V  d S )NrA   rC   rE   r   )r   r   r   r   r,   r   r%   r'   )r   r   Zget_timeoutr   r   r   r   test_get_timeoutm   s    
zQueueGetTest.test_get_timeoutc                 c   sB   t  }|jtddd}|d tdV  | d|V  d S )NrA   rC   rE   r   rB   )r   r   r   r   r   r   sleepr'   )r   r   r   r   r   r   test_get_timeout_preemptedx   s
    
z'QueueGetTest.test_get_timeout_preemptedc                 #   s   t d  fddtdD } d}| dt j tdV  | dt j | 	|
   d | d  V  | dt j |dd  D ]}| t|j qd S )Nr
   c                    s   g | ]}  |td dqS rA   rC   r   r   r!   r(   r#   r   r   r$      s     zBQueueGetTest.test_get_clears_timed_out_putters.<locals>.<listcomp>
   rB      r   )r   r   r&   r   r'   len_puttersr   rK   r0   r2   r   r,   r   resultr   r   r   Zputterr   r#   r   !test_get_clears_timed_out_putters   s    


z.QueueGetTest.test_get_clears_timed_out_puttersc                 #   s   t    fddtdD }t  }| dt j t	
dV  | dt j | |     | dt j |D ]}| t|j qd S )Nc              	      s"   g | ]}t  td dqS rM   asyncioensure_futurer   r   r    r#   r   r   r$      s    zBQueueGetTest.test_get_clears_timed_out_getters.<locals>.<listcomp>rP   rQ   rB   r   )r   r   r&   rX   rY   r   r'   rR   _gettersr   rK   r0   r2   r,   r   rT   r   r   r   getterr   r#   r   !test_get_clears_timed_out_getters   s    
z.QueueGetTest.test_get_clears_timed_out_gettersc                 #   sL   t   tdD ]} | q fdd}| V }| |ttd d S )N   c                     s6   g }  2 z&3 d H W }|  | |dkr|   S q6 d S )N   )append)resultsr(   r#   r   r   f   s
    
z&QueueGetTest.test_async_for.<locals>.f)r   r   r&   r   r'   list)r   r(   rb   ra   r   r#   r   test_async_for   s    zQueueGetTest.test_async_forN)r4   r5   r6   r   r:   r;   r=   r@   rI   rJ   rL   rV   r]   rd   r   r   r   r   r7   L   s$   







r7   c                   @   s   e Zd Zedd Zdd Zedd Zedd Zed	d
 Zedd Z	edd Z
edd Zedd Zedd ZdS )QueuePutTestc                 C   s&   t  }|d | d|  d S r8   )r   r   r   r'   r   r9   r   r   r   test_blocking_put   s    
zQueuePutTest.test_blocking_putc                 C   s*   t d}|d | t j|jd d S r>   )r   r   r   r,   	QueueFullr%   r9   r   r   r   test_nonblocking_put_exception   s    

z+QueuePutTest.test_nonblocking_put_exceptionc                 c   sP   t  }| }| }|dV  | d|V  |dV  | d|V  d S Nr   r
   )r   r   r   r   r'   r   r   Zget0Zget1r   r   r   test_put_with_getters   s    z"QueuePutTest.test_put_with_gettersc                 c   s\   t  }| }| }|d tjV  | d|V  |d tjV  | d|V  d S ri   )r   r   r   r%   r   momentr'   rj   r   r   r   !test_nonblocking_put_with_getters   s    

z.QueuePutTest.test_nonblocking_put_with_gettersc                 #   sr   t d  d  fdd}| jd| | jd|  d dg}| tdd |D  |V  d S )	Nr
   r   c                      s       d S r<   )r   r   r#   r   r   get_and_discard   s    z<QueuePutTest.test_blocking_put_wait.<locals>.get_and_discardrA   rB   c                 s   s   | ]}|  V  qd S r<   )r2   )r!   rb   r   r   r   	<genexpr>   s     z6QueuePutTest.test_blocking_put_wait.<locals>.<genexpr>)r   r   r%   rG   rH   r   r0   any)r   rn   Zfuturesr   r#   r   test_blocking_put_wait   s    

z#QueuePutTest.test_blocking_put_waitc              	   c   sz   t d}|d |jdtddd}|d}| t |V  W 5 Q R X | d|  | d|	 V  |V  d S )Nr
   r   rA   rC   rE   r   )
r   r   r%   r   r   r,   r   r'   r   r   )r   r   Zput_timeoutr   r   r   r   test_put_timeout   s    


zQueuePutTest.test_put_timeoutc                 c   sF   t d}|d |jdtddd}|  tdV  |V  d S )Nr
   r   rA   rC   rE   rB   )r   r   r%   r   r   r   r   rK   r?   r   r   r   test_put_timeout_preempted   s    

z'QueuePutTest.test_put_timeout_preemptedc                 #   s   t d  fddtdD } d}| dt j tdV  | dt j | 	|
   d | dt j |dd  D ]}| t|j qd S )Nr
   c                    s   g | ]}  |td dqS rM   rN   rO   r#   r   r   r$      s     zBQueuePutTest.test_put_clears_timed_out_putters.<locals>.<listcomp>rP   rB   rQ   r   )r   r   r&   r   r'   rR   rS   r   rK   r0   r2   r,   r   rT   rU   r   r#   r   !test_put_clears_timed_out_putters   s    


z.QueuePutTest.test_put_clears_timed_out_puttersc                 #   s   t    fddtdD }t  }   | dt j t	
dV  | dt j | |   d | dt j | d|V  |D ]}| t|j qd S )Nc              	      s"   g | ]}t  td dqS rM   rW   r    r#   r   r   r$     s    zBQueuePutTest.test_put_clears_timed_out_getters.<locals>.<listcomp>rP      rB   r   r
   )r   r   r&   rX   rY   r   r'   rR   rZ   r   rK   r0   r2   r   r,   r   rT   r[   r   r#   r   !test_put_clears_timed_out_getters
  s    

z.QueuePutTest.test_put_clears_timed_out_gettersc                 c   s   t jdd}| |  | |  |d |d | |  | |  | t j|jd | 	d|
  | |  | |  |dV  |d}| |  | 	d| V  |V  | |  d S )Ng?r   r   r
   r   r   )r   r   r   r/   r0   r1   r%   r,   rg   r'   r   r   r2   r   r?   r   r   r   test_float_maxsize  s"    


zQueuePutTest.test_float_maxsizeN)r4   r5   r6   r   rf   rh   rk   rm   rq   rr   rs   rt   rv   rw   r   r   r   r   re      s&   

	





re   c                   @   sJ   e Zd ZejZdd Zedd Zedd Z	edd Z
ed	d
 ZdS )QueueJoinTestc                 C   s   |   }| t|j d S r<   )queue_classr,   r.   	task_doner9   r   r   r   test_task_done_underflow9  s    z&QueueJoinTest.test_task_done_underflowc                 #   sj      tdD ]} | qd_tj fdd}|  |    V  ttdj d S )Nd   r   c                  3   s8      V }  j| 7  _   tt d V  q d S )NrA   )r   accumulatorrz   r   rK   r   itemr   r   r   r   workerE  s    
z,QueueJoinTest.test_task_done.<locals>.worker)	ry   r&   r%   r}   r   	coroutinejoinr'   sum)r   r(   r   r   r   r   test_task_done=  s    
zQueueJoinTest.test_task_donec                 c   sv   |   }|d t| }| |  | V  | |  tj	V  | |  |
  | |  d S r8   )ry   r%   rX   rY   r   r0   r2   r   r   rl   rz   r   )r   r   r   r   r   r   test_task_done_delayS  s    

z"QueueJoinTest.test_task_done_delayc                 c   s    |   }| V  | V  d S r<   )ry   r   r9   r   r   r   test_join_empty_queuea  s    
z#QueueJoinTest.test_join_empty_queuec              	   c   s@   |   }|d | t |jtdddV  W 5 Q R X d S )Nr   rA   rC   rE   )ry   r   r,   r   r   r   r9   r   r   r   test_join_timeoutg  s    
zQueueJoinTest.test_join_timeoutN)r4   r5   r6   r   r   ry   r{   r   r   r   r   r   r   r   r   r   rx   6  s   


rx   c                   @   s   e Zd ZejZedd ZdS )PriorityQueueJoinTestc                 c   s   | j dd}|d |d | |  |d |d | d|  | d| V  | d|  | d| V  | |  d S )Nr   r   )r
   a)r   b)r   c)r   d	ry   r%   r   r1   r   r'   r   r   r/   r9   r   r   r   r*   r  s    



z PriorityQueueJoinTest.test_orderN)r4   r5   r6   r   PriorityQueuery   r   r*   r   r   r   r   r   o  s   r   c                   @   s   e Zd ZejZedd ZdS )LifoQueueJoinTestc                 c   s   | j dd}|d |d | |  |d |d | d|  | d| V  | d|  | d| V  | |  d S )Nr   r   r
   r   r   r   r9   r   r   r   r*     s    



zLifoQueueJoinTest.test_orderN)r4   r5   r6   r   	LifoQueuery   r   r*   r   r   r   r   r     s   r   c                   @   s   e Zd Zedd ZdS )ProducerConsumerTestc                 #   sf   t jddg  tj fdd}tjfdd}|  | V   V  | ttd  d S )Nr   r   c                   3   s      V    q d S r<   )r`   r   rz   r   historyr   r   r   consumer  s    z=ProducerConsumerTest.test_producer_consumer.<locals>.consumerc                  3   s   t dD ]}  | V  qd S )NrP   )r&   r   r~   r#   r   r   producer  s    z=ProducerConsumerTest.test_producer_consumer.<locals>.producerrP   )r   r   r   r   r   r'   rc   r&   )r   r   r   r   r   r   test_producer_consumer  s    
z+ProducerConsumerTest.test_producer_consumerN)r4   r5   r6   r   r   r   r   r   r   r     s   r   __main__)rX   datetimer   r   ZunittestZtornador   r   Ztornado.genr   Ztornado.testingr   r   r	   r7   re   rx   r   r   r   r4   mainr   r   r   r   <module>   s    5e 9