a
    zeU                     @   s   d Z ddlZddlmZ ddlZddlmZ ddlmZ dd Z	G dd	 d	ej
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ZG dd deeZG dd deeZG dd deeZedkre  dS )zTests for queues.py    N)mock)GenericAlias)utilsc                   C   s   t d  d S N)asyncioZset_event_loop_policy r   r   3/usr/lib/python3.9/test/test_asyncio/test_queues.pytearDownModule   s    r	   c                       s   e Zd Z fddZ  ZS )_QueueTestBasec                    s   t    |  | _d S r   )supersetUpnew_test_looploopself	__class__r   r   r      s    
z_QueueTestBase.setUp)__name__
__module____qualname__r   __classcell__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 )QueueBasicTestsc                    s@  fdd} |t tjd}W d   n1 sB0    Y   |d | tt| |v }	||  fdd}t 
|  W d   n1 s0    Y   fdd	}t( 
|  tjd}W d   n1 s0    Y  |d
 d |v  dS )zTest Queue's repr or str.

        fn is repr or str. expect_id is True if we expect the Queue's id to
        appear in fn(Queue()).
        c                  3   s.   d V }   d|  dV }   d|  dV  d S N皙?g?assertAlmostEqualZwhenr   r   r   gen   s
    z.QueueBasicTests._test_repr_or_str.<locals>.genr   Nz<Queuec                     sJ   t jd} |   t dI d H  d | v  | d d S )Nr   r   z_getters[1]r   )r   Queuecreate_taskgetsleep
assertTrue
put_nowaitqfnr   r   r   r   
add_getter-   s
    z5QueueBasicTests._test_repr_or_str.<locals>.add_getterc                     sV   t jdd} | d | d t dI d H  d | v  |   d S )N   maxsizer      r   z_putters[1])r   r   r$   r    putr"   r#   
get_nowaitr%   r'   r   r   
add_putter:   s    
z5QueueBasicTests._test_repr_or_str.<locals>.add_putterr*   z
_queue=[1])r   assertWarnsDeprecationWarningr   r   r#   
startswithhexidassertEqualrun_until_completer$   )r   r(   Z	expect_idr   r&   Zid_is_presentr)   r0   r   r'   r   _test_repr_or_str   s     
*
*,
z!QueueBasicTests._test_repr_or_strc                 C   s   t  }| t tj|d}W d    n1 s40    Y  | |j| | t tj| jd}W d    n1 sz0    Y  | |j| j d S Nr   )	r   ZMockr1   r2   r   r   assertIs_loopr   )r   r   r&   r   r   r   test_ctor_loopK   s    *,zQueueBasicTests.test_ctor_loopc                 C   s(   t | j t  }| |j| j d S r   )r   Zset_event_loopr   r   r:   r;   r   r&   r   r   r   test_ctor_noloopU   s    z QueueBasicTests.test_ctor_noloopc                 C   s   |  td d S NT)r8   reprr   r   r   r   	test_reprZ   s    zQueueBasicTests.test_reprc                 C   s   |  td d S )NF)r8   strr   r   r   r   test_str]   s    zQueueBasicTests.test_strc                 C   s*   t jt }| |jtf | |t d S r   )r   r   intr6   __args__ZassertIsInstancer   r=   r   r   r   test_generic_alias`   s    
z"QueueBasicTests.test_generic_aliasc                 C   s   |  t tj| jd}W d    n1 s.0    Y  | |  |d | |  | 	d|
  | |  d S Nr   r*   )r1   r2   r   r   r   r#   emptyr$   assertFalser6   r/   r=   r   r   r   
test_emptye   s    ,
zQueueBasicTests.test_emptyc                 C   s   |  t tj| jd}W d    n1 s.0    Y  | |  |  t  tjd| jd}W d    n1 sv0    Y  |d | |  d S )Nr   r*   r+   )	r1   r2   r   r   r   rI   fullr$   r#   r=   r   r   r   	test_fulln   s    ,.
zQueueBasicTests.test_fullc                    sv   |  t tj| jd W d    n1 s.0    Y  dD ]} | q< fddtdD }| g d| d S )Nr   r*      r-   c                    s   g | ]}   qS r   r/   .0_r%   r   r   
<listcomp>~       z.QueueBasicTests.test_order.<locals>.<listcomp>rN   )r1   r2   r   r   r   r$   ranger6   r   iitemsr   r%   r   
test_orderx   s    ,zQueueBasicTests.test_orderc                    s   fdd} |t tjddW d    n1 sD0    Y  dj g   fdd fdd}|  d		  d S )
Nc                  3   s.   d V }   d|  dV }   d|  dV  d S )N{Gz?{Gz?r   r   r   r   r   r      s
    z)QueueBasicTests.test_maxsize.<locals>.genr-   r+   c                     s,   t dD ]} | I d H   |  qdS )NrN   T)rU   r.   append)rW   )have_been_putr&   r   r   putter   s    z,QueueBasicTests.test_maxsize.<locals>.putterc                     s     } tdI d H  ddg  d  tdI d H  g d  d  d  |   |   d S )NrZ   r   r*   )r   r*   r-   r-   )r    r   r"   r6   r/   r#   doneresult)tr]   r   r^   r&   r   r   r   test   s    z*QueueBasicTests.test_maxsize.<locals>.testr[   )
r   r1   r2   r   r   r6   r,   r7   r   timer   r   rc   r   rb   r   test_maxsize   s    
,zQueueBasicTests.test_maxsizeN)r   r   r   r8   r<   r>   rA   rC   rF   rJ   rL   rY   rf   r   r   r   r   r      s   3
	
	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 )QueueGetTestsc                    sl   |  t tj| jd W d    n1 s.0    Y   d  fdd}| j| }| d| d S )Nr   r*   c                      s      I d H S r   r!   r   r%   r   r   	queue_get   s    z2QueueGetTests.test_blocking_get.<locals>.queue_get)r1   r2   r   r   r   r$   r7   r6   )r   ri   resr   r%   r   test_blocking_get   s    ,
zQueueGetTests.test_blocking_getc                 C   s   |  t  tjd| jd}W d    n1 s00    Y  |d | j }|j| | j	|
 }| d| | |  | |  d S )Nr*   r   )r1   r2   r   r   r   r$   Zcreate_future_puttersr\   r7   r!   r6   r#   r_   ZassertIsNoner`   )r   r&   waiterrj   r   r   r   test_get_with_putters   s    .

z#QueueGetTests.test_get_with_puttersc                    s   fdd} |t( tjdtjdW d    n1 sN0    Y  d  fdd fdd}| }d	| d
	  d S )Nc                  3   s   d V }   d|  dV  d S NrZ   r   r   r   r   r   r      s    z1QueueGetTests.test_blocking_get_wait.<locals>.genr   Fc                     s       I d H } d | S r?   )setr!   )rj   finishedr&   startedr   r   ri      s    z7QueueGetTests.test_blocking_get_wait.<locals>.queue_getc                     sL    djd  }  I d H    | I d H }  |S NrZ   r*   )
call_laterr$   r    waitrI   r#   )Zqueue_get_taskrj   rr   r   r&   ri   r   rs   r   r   	queue_put   s    


z7QueueGetTests.test_blocking_get_wait.<locals>.queue_putr*   rZ   )
r   r1   r2   r   r   Eventr7   r6   r   rd   )r   r   rx   rj   r   rw   r   test_blocking_get_wait   s    
*	z$QueueGetTests.test_blocking_get_waitc                 C   sV   |  t tj| jd}W d    n1 s.0    Y  |d | d|  d S rG   r1   r2   r   r   r   r$   r6   r/   r=   r   r   r   test_nonblocking_get   s    ,
z"QueueGetTests.test_nonblocking_getc                 C   sL   |  t tj| jd}W d    n1 s.0    Y  | tj|j d S r9   )r1   r2   r   r   r   assertRaisesZ
QueueEmptyr/   r=   r   r   r   test_nonblocking_get_exception   s    ,z,QueueGetTests.test_nonblocking_get_exceptionc                    s   fdd} | t tj dW d    n1 sB0    Y  fdd fdd}d |  d	   d S )
Nc                  3   s.   d V }   d|  dV }   d|  dV  d S )NrZ   gZd;?g?r   r   r   r   r   r      s
    z-QueueGetTests.test_get_cancelled.<locals>.genr   c                      s   t   dI d H S )Ng&1?)r   wait_forr!   r   r%   r   r   ri     s    z3QueueGetTests.test_get_cancelled.<locals>.queue_getc                     s0      } tdI d H  d | I d H S rt   )r    r   r"   r$   )Zget_task)r   r&   ri   r   r   rc     s    
z.QueueGetTests.test_get_cancelled.<locals>.testr*   gQ?)	r   r1   r2   r   r   r6   r7   r   rd   re   r   )r   r&   ri   r   r   test_get_cancelled   s    
*z QueueGetTests.test_get_cancelledc                 C   s   |  t tj| jd}W d    n1 s.0    Y  | j| }| j| }t| j |	  t| j | 
|  |d t| j | | d d S Nr   a)r1   r2   r   r   r   r    r!   
test_utilsrun_brieflycancelr#   r_   r$   r6   r`   )r   r&   t1t2r   r   r   test_get_cancelled_race  s    ,
z%QueueGetTests.test_get_cancelled_racec                 C   s   |  t  tj| jdd}W d    n1 s00    Y  | j|d | j|d t| j | 	| j
| d | 	| j
| d d S )Nr*   r   r,   r   b)r1   r2   r   r   r   r    r.   r   r   r6   r7   r!   r=   r   r   r   test_get_with_waiting_putters  s    .z+QueueGetTests.test_get_with_waiting_puttersc                 C   s   dd }dd }d}d}|  t  tj|| jd}W d    n1 sH0    Y  |  t6 | jtj||||||| jd W d    n1 s0    Y  d S )Nc                    s    t |D ]}|  I d H  qd S r   )rU   r!   )queueZnum_expectedrR   r   r   r   consumer(  s    z<QueueGetTests.test_why_are_getters_waiting.<locals>.consumerc                    s"   t |D ]}| |I d H  qd S r   )rU   r.   )r   Z	num_itemsrW   r   r   r   producer,  s    z<QueueGetTests.test_why_are_getters_waiting.<locals>.producerr*      r   r1   r2   r   r   r   r7   Zgather)r   r   r   
queue_sizeZproducer_num_itemsr&   r   r   r   test_why_are_getters_waiting%  s    .z*QueueGetTests.test_why_are_getters_waitingc                 C   s   dd }|  || _dd }| t  tj| jdd}W d    n1 sL0    Y  | j| j|| | t	|j
d d S )Nc                   s   s   dV  dV  d S r   r   r   r   r   r   a_generator>  s    zXQueueGetTests.test_cancelled_getters_not_being_held_in_self_getters.<locals>.a_generatorc                    s4   zt |  dI d H }W n t jy.   Y n0 d S )Nr   )r   r   r!   TimeoutError)r   itemr   r   r   r   D  s    zUQueueGetTests.test_cancelled_getters_not_being_held_in_self_getters.<locals>.consumerr   r   r   )r   r   r1   r2   r   r   r7   r    r6   lenZ_getters)r   r   r   r   r   r   r   5test_cancelled_getters_not_being_held_in_self_getters=  s    .zCQueueGetTests.test_cancelled_getters_not_being_held_in_self_gettersN)r   r   r   rk   rn   rz   r|   r~   r   r   r   r   r   r   r   r   r   rg      s   "	rg   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d Zdd Zdd Zdd ZdS )QueuePutTestsc                    sV   |  t tj| jd W d    n1 s.0    Y   fdd}| j|  d S )Nr   c                      s     dI d H  d S )Nr*   r.   r   r%   r   r   rx   V  s    z2QueuePutTests.test_blocking_put.<locals>.queue_put)r1   r2   r   r   r   r7   r   rx   r   r%   r   test_blocking_putR  s    ,zQueuePutTests.test_blocking_putc                    s   fdd} |t* tjddtjdW d    n1 sP0    Y  d  fdd fd	d
}|  d  d S )Nc                  3   s   d V }   d|  dV  d S ro   r   r   r   r   r   r   ^  s    z1QueuePutTests.test_blocking_put_wait.<locals>.genr*   r+   r   Fc                      s0      dI d H  dI d H  d d S )Nr*   r-   T)rp   r.   r   rq   r   r   rx   j  s    z7QueuePutTests.test_blocking_put_wait.<locals>.queue_putc                     sJ    dj  }  I d H    | I d H    d S ro   )ru   r/   r    rv   rI   r#   )Zqueue_put_taskrr   r   r&   rx   r   rs   r   r   ri   q  s    

z7QueuePutTests.test_blocking_put_wait.<locals>.queue_getrZ   )	r   r1   r2   r   r   ry   r7   r   rd   )r   r   ri   r   r   r   test_blocking_put_wait\  s    
*z$QueuePutTests.test_blocking_put_waitc                 C   sV   |  t tj| jd}W d    n1 s.0    Y  |d | d|  d S rG   r{   r=   r   r   r   test_nonblocking_put|  s    ,
z"QueuePutTests.test_nonblocking_putc                 C   s   dd }|  |}| t tj|d}W d    n1 s>0    Y  || }|td |	d |	d |
  z|| W n, tjy   || }|| Y n0 | }| d| d S )Nc                   s   s   dV  dV  d S NrZ   r   r   r   r   r   r   r     s    zBQueuePutTests.test_get_cancel_drop_one_pending_reader.<locals>.genr   rZ   r*   r-   )r   r1   r2   r   r   r    r!   r7   r"   r$   r   CancelledErrorr`   r6   )r   r   r   r&   readerr`   r   r   r   'test_get_cancel_drop_one_pending_reader  s     
*

z5QueuePutTests.test_get_cancel_drop_one_pending_readerc                 C   s   dd }|  |}|d | t tj|d}W d    n1 sH0    Y  || }|| }|| }|t	d |
d |
d |  z|| W n tjy   Y n0 || | | | hddh d S )Nc                   s   s   dV  dV  d S r   r   r   r   r   r   r     s    zDQueuePutTests.test_get_cancel_drop_many_pending_readers.<locals>.genTr   rZ   r*   r-   )r   Z	set_debugr1   r2   r   r   r    r!   r7   r"   r$   r   r   r6   r`   )r   r   r   r&   Zreader1Zreader2Zreader3r   r   r   )test_get_cancel_drop_many_pending_readers  s$    

*


z7QueuePutTests.test_get_cancel_drop_many_pending_readersc                 C   s   dd }|  |}| t tjd|d}W d    n1 s@0    Y  |d ||d}|t	d |
 }| |d |  z|| W n. tjy   ||d}|| Y n0 |
 }| |d | | d d S )Nc                   s   s   dV  dV  d S r   r   r   r   r   r   r     s    z/QueuePutTests.test_put_cancel_drop.<locals>.genr*   r   r-   rZ   r   )r   r1   r2   r   r   r$   r    r.   r7   r"   r/   r6   r   r   qsize)r   r   r   r&   writerZvalue1Zvalue2r   r   r   test_put_cancel_drop  s$    
,
z"QueuePutTests.test_put_cancel_dropc                 C   sZ   |  t  tjd| jd}W d    n1 s00    Y  |d | tj|jd d S )Nr*   r+   r-   )r1   r2   r   r   r   r$   r}   	QueueFullr=   r   r   r   test_nonblocking_put_exception  s    .
z,QueuePutTests.test_nonblocking_put_exceptionc                    s    t  tjdjd W d    n1 s00    Y   d  d    tj	 jd  t  tjdjd W d    n1 s0    Y   fdd}j
|  d S )Ng?r+   r*   r-   rN   c                      s2     dI d H    dI d H     d S )Nr*   r-   )r.   r#   rK   r   r&   r   r   r   rx     s    z3QueuePutTests.test_float_maxsize.<locals>.queue_put)r1   r2   r   r   r   r$   r#   rK   r}   r   r7   r   r   r   r   test_float_maxsize  s    .

.z QueuePutTests.test_float_maxsizec                    s   |  t tj| jd W d    n1 s.0    Y   fdd} fdd}| j| }| d| j|  | |	  | |
  d S )Nr   c                      s     dI d H  dS )Nr*   Tr   r   r%   r   r   rx     s    z3QueuePutTests.test_put_cancelled.<locals>.queue_putc                      s      I d H S r   rh   r   r%   r   r   rc     s    z.QueuePutTests.test_put_cancelled.<locals>.testr*   )r1   r2   r   r   r   r    r6   r7   r#   r_   r`   )r   rx   rc   ra   r   r%   r   test_put_cancelled  s    ,z QueuePutTests.test_put_cancelledc                 C   s   |  t  tj| jdd}W d    n1 s00    Y  | j|d}| j|d}| j|d}t| j | 	|
  | |
  |  t| j | 	|
  | | d t| j | | d | j| d S )Nr*   r   r   r   X)r1   r2   r   r   r   r    r.   r   r   r#   r_   rI   r   r6   r/   r7   )r   r&   Zput_aZput_bZput_cr   r   r   test_put_cancelled_race  s    .z%QueuePutTests.test_put_cancelled_racec                 C   s~   |  t tj| jd}W d    n1 s.0    Y  | j| }t| j | j	|
d | | j	|d d S r   )r1   r2   r   r   r   r    r!   r   r   r7   r.   r6   )r   r&   ra   r   r   r   test_put_with_waiting_getters  s    ,z+QueuePutTests.test_put_with_waiting_gettersc                    s   |  t  tjd| jd W d    n1 s00    Y   fdd} fdd}|d}|d}|d}|d	}|  t2 | jtj| ||||| jd W d    n1 s0    Y  d S )
Nr-   r   c                    s     | I d H  d S r   r   r   r   r   r   r^   (  s    z:QueuePutTests.test_why_are_putters_waiting.<locals>.putterc                     s2   t dI d H    } t| D ]}  }q d S )Nr   )r   r"   r   rU   r/   )numrR   r   r   r   r   getter+  s    z:QueuePutTests.test_why_are_putters_waiting.<locals>.getterr   r*   rN   r   )r   r^   r   t0r   r   Zt3r   r   r   test_why_are_putters_waiting"  s    .z*QueuePutTests.test_why_are_putters_waitingc                 C   s   dd }|  |}| t tj|dd}W d    n1 s@0    Y  |d ||d}|t	d | 
t|jd |  | tj || W d    n1 s0    Y  | 
t|jd d S )Nc                   s   s   dV  dV  d S r   r   r   r   r   r   r   :  s    zUQueuePutTests.test_cancelled_puts_not_being_held_in_self_putters.<locals>.a_generatorr*   r   rZ   r   )r   r1   r2   r   r   r$   r    r.   r7   r"   r6   r   rl   r   r}   r   )r   r   r   r   put_taskr   r   r   2test_cancelled_puts_not_being_held_in_self_putters9  s    
,
(z@QueuePutTests.test_cancelled_puts_not_being_held_in_self_puttersc                 C   s   dd }|  |}| t tjd|d}W d    n1 s@0    Y  |d ||d}|t	d |
  |  | tj || W d    n1 s0    Y  d S )Nc                   s   s   dV  dV  d S r   r   r   r   r   r   r   R  s    zKQueuePutTests.test_cancelled_put_silence_value_error_exception.<locals>.genr*   r   rZ   )r   r1   r2   r   r   r$   r    r.   r7   r"   r/   r   r}   r   )r   r   r   r   r   r   r   r   0test_cancelled_put_silence_value_error_exceptionQ  s    
,
z>QueuePutTests.test_cancelled_put_silence_value_error_exceptionN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   P  s   
  r   c                   @   s   e Zd Zdd ZdS )LifoQueueTestsc                    sv   |  t tj| jd W d    n1 s.0    Y  dD ]} | q< fddtdD }| g d| d S )Nr   rM   c                    s   g | ]}   qS r   rO   rP   r%   r   r   rS   v  rT   z-LifoQueueTests.test_order.<locals>.<listcomp>rN   )r-   rN   r*   )r1   r2   r   	LifoQueuer   r$   rU   r6   rV   r   r%   r   rY   p  s    ,zLifoQueueTests.test_orderNr   r   r   rY   r   r   r   r   r   n  s   r   c                   @   s   e Zd Zdd ZdS )PriorityQueueTestsc                    sv   |  t tj| jd W d    n1 s.0    Y  dD ]} | q< fddtdD }| g d| d S )Nr   rM   c                    s   g | ]}   qS r   rO   rP   r%   r   r   rS     rT   z1PriorityQueueTests.test_order.<locals>.<listcomp>rN   )r*   r-   rN   )r1   r2   r   PriorityQueuer   r$   rU   r6   rV   r   r%   r   rY   |  s    ,zPriorityQueueTests.test_orderNr   r   r   r   r   r   z  s   r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 ZdS )
_QueueJoinTestMixinNc                 C   sJ   |  t | j| jd}W d    n1 s.0    Y  | t|j d S r9   )r1   r2   q_classr   r}   
ValueError	task_doner=   r   r   r   test_task_done_underflow  s    ,z,_QueueJoinTestMixin.test_task_done_underflowc                    s    t jjdW d    n1 s.0    Y  tdD ]}| q@d d fddfdd}j| }ttd  d	tt	|D ]}d qjt
| d S )
Nr   d   r   Tc                     s(   r$  I d H }  | 7    q d S r   )r!   r   r   )accumulatorr&   runningr   r   worker  s    z2_QueueJoinTestMixin.test_task_done.<locals>.workerc                     s*   fddt dD }   I d H  | S )Nc                    s   g | ]} j  qS r   )r   r    )rQ   index)r   r   r   r   rS     s   zD_QueueJoinTestMixin.test_task_done.<locals>.test.<locals>.<listcomp>r-   )rU   join)tasks)r&   r   r   r   r   rc     s
    z0_QueueJoinTestMixin.test_task_done.<locals>.testF)r1   r2   r   r   rU   r$   r7   r6   sumr   r   rv   )r   rW   rc   r   r   )r   r&   r   r   r   r   test_task_done  s    ,z"_QueueJoinTestMixin.test_task_donec                    sV   |  t | j| jd W d    n1 s.0    Y   fdd}| j|  d S )Nr   c                      s       I d H     I d H  d S r   )r   r   r%   r   r   r     s    z7_QueueJoinTestMixin.test_join_empty_queue.<locals>.join)r1   r2   r   r   r7   )r   r   r   r%   r   test_join_empty_queue  s    ,z)_QueueJoinTestMixin.test_join_empty_queuec                 C   sb   |  t | j| jd}W d    n1 s.0    Y  | | d d|_| | d d S )Nr   z	maxsize=0r-   zmaxsize=0 tasks=2)r1   r2   r   r   r6   _format_unfinished_tasksr=   r   r   r   test_format  s
    ,z_QueueJoinTestMixin.test_format)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 )QueueJoinTestsN)r   r   r   r   r   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZejZdS )LifoQueueJoinTestsN)r   r   r   r   r   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZejZdS )PriorityQueueJoinTestsN)r   r   r   r   r   r   r   r   r   r   r     s   r   __main__)__doc__Zunittestr   r   typesr   Ztest.test_asyncior   r   r	   ZTestCaser
   r   rg   r   r   r   r   r   r   r   r   mainr   r   r   r   <module>   s,     %   C