o
    diH                     @   sX   d dl Z d dlmZ d dlZd dlm Z ddlmZ g dZ	e
dZG dd deZdS )	    N)TestCase   )
async_test)ABC   c                   @   s   e Z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edd Zedd Zedd Zedd ZdS )AsyncioTestc                 C   s   |  tjt d S N)assertEqualaitasyncioaio)self r   N/home/ubuntu/.local/lib/python3.10/site-packages/aioitertools/tests/asyncio.pytest_import   s   zAsyncioTest.test_importc                    s   dd  g d}g d} fdd|D }t t|I d H }| ||  fdd|D }g }t|2 z3 d H W }|| q76 | || d S )Nc                    s   t |I d H  | S r
   r   sleep)numberdurationr   r   r   sleepy      z-AsyncioTest.test_as_completed.<locals>.sleepy))r   g333333?)   皙?)r         ?)r   r   r   c                       g | ]} | qS r   r   .0pairr   r   r   
<listcomp>       z1AsyncioTest.test_as_completed.<locals>.<listcomp>c                    r   r   r   r   r    r   r   r!       r"   )r   listr   as_completedr   append)r   pairsexpectedfuturesresultsvaluer   r    r   test_as_completed   s   zAsyncioTest.test_as_completedc                    s   ddg}dd |D }|  tj ttj|ddI d H  W d    n1 s*w   Y  dd |D }d}|  tj tj|dd2 z
3 d H W }|d	7 }qF6 W d    n1 s\w   Y  | |d	 d S )
N)g      ?)r   c                 S      g | ]}t j| qS r   r   r   argsr   r   r   r!   *       z9AsyncioTest.test_as_completed_timeout.<locals>.<listcomp>r   )timeoutc                 S   r,   r   r   r-   r   r   r   r!   .   r/   r   r   )assertRaisesr   TimeoutErrorr   r#   r   r$   r   )r   callsr(   r)   _r   r   r   test_as_completed_timeout&   s   
z%AsyncioTest.test_as_completed_timeoutc                    sz   dd }| | | g}t tdd }g }t|2 z3 d H W }|| q6 | dt| | t|t| d S )Nc                  S  *   t dD ]} | V  tdI d H  qd S N
   r   ranger   r   ir   r   r   gen7   
   z*AsyncioTest.test_as_generated.<locals>.genr8   r      )	r#   r:   r   as_generatedr%   r   lenassertListEqualsortedr   r=   gensr'   r)   r*   r   r   r   test_as_generated5   s   zAsyncioTest.test_as_generatedc                    s   dd }dd }| | g}g }|  td t|2 z3 d H W }|| q6 W d    n1 s4w   Y  | d| d S )Nc                  S  .   t dD ]} | V  tdI d H  qtdNr   r   faker:   r   r   	Exceptionr;   r   r   r   gen1F   
   z5AsyncioTest.test_as_generated_exception.<locals>.gen1c                  S  r6   r7   r9   r;   r   r   r   gen2L   r>   z5AsyncioTest.test_as_generated_exception.<locals>.gen2rI   r8   )assertRaisesRegexrK   r   r@   r%   assertNotIn)r   rL   rN   rE   r)   r*   r   r   r   test_as_generated_exceptionD   s   z'AsyncioTest.test_as_generated_exceptionc                    s   dd }dd }| | g}t tdt td }g }g }tj|dd2 z3 d H W }t|tr7|| q&|| q&6 | t|t| | 	d	t
| | |d
 t d S )Nc                  S  rG   rH   rJ   r;   r   r   r   rL   Z   rM   z<AsyncioTest.test_as_generated_return_exception.<locals>.gen1c                  S  r6   r7   r9   r;   r   r   r   rN   `   r>   z<AsyncioTest.test_as_generated_return_exception.<locals>.gen2r   r8   Treturn_exceptionsr   r   )r#   r:   r   r@   
isinstancerK   r%   rB   rC   r   rA   assertIsInstance)r   rL   rN   rE   r'   errorsr)   r*   r   r   r   "test_as_generated_return_exceptionX   s   
z.AsyncioTest.test_as_generated_return_exceptionc                    sx   ddt fdd}|d| g}ttdttd }g }t|2 z3 d H W }|| q#6 | t|t| d S )	Nr8   maxc                 S  s8   t dD ]}|| krtj|V  tdI d H  qd S )N   r   )r:   r   CancelledErrorr   )rX   r<   r   r   r   r=   t   s   z9AsyncioTest.test_as_generated_task_cancelled.<locals>.genr   r   rY   )r8   )intr#   r:   r   r@   r%   rB   rC   rD   r   r   r    test_as_generated_task_cancelledr   s   z,AsyncioTest.test_as_generated_task_cancelledc                    sj   dd  g d}g  fdd}t | }t dI d H  |  |I d H  | t|t d S )Nc                  S  r6   )NrY   r   r9   r;   r   r   r   r=      r>   z4AsyncioTest.test_as_generated_cancelled.<locals>.gen)r   r   r   r   c                     s6       g} t | 2 z3 d H W }| q6 S r
   )r   r@   r%   )rE   r*   r=   r)   r   r   foo   s   z4AsyncioTest.test_as_generated_cancelled.<locals>.foog333333?)r   ensure_futurer   cancelrB   rC   )r   r'   r^   taskr   r]   r   test_as_generated_cancelled   s   
z'AsyncioTest.test_as_generated_cancelledc                    sr   dd }|dt |dg}tt dr |t |d n||d tj| I d H }| g d| d S )Nc                       t dI d H  | S NMbP?r   argr   r   r   fn   r   z/AsyncioTest.test_gather_input_types.<locals>.fnr   r   create_taskr   )r   r   r   )r   r_   hasattrr%   ri   r   gatherr   )r   rh   fnsresultr   r   r   test_gather_input_types   s   
z#AsyncioTest.test_gather_input_typesc                    s   dd  fddt jfddtdD ddiI d H }| d | g d	| t jfd
dtdD  I d H }| d | g d	| d S )Nr   c                    s0    d7  t  tdI d H   d8  | S )Nr   re   )rX   r   r   rf   )countermax_counterr   r   rh      s   
z+AsyncioTest.test_gather_limited.<locals>.fnc                       g | ]} |qS r   r   r   r<   rh   r   r   r!      r"   z3AsyncioTest.test_gather_limited.<locals>.<listcomp>r8   limitr   )
r   r   r   r      rY            	   c                    rq   r   r   rr   rs   r   r   r!      r"   )r   rk   r:   r   )r   rm   r   )ro   rh   rp   r   test_gather_limited   s   (	"zAsyncioTest.test_gather_limitedc              	      s   dd }|d}|d}t j||||||ddI d H }| g d| |d}|d}t ||||||I d H }| g d| d S )Nc                    rc   rd   r   rf   r   r   r   rh      r   z1AsyncioTest.test_gather_limited_dupes.<locals>.fnr   r   )rt   )r   r   r   r   r   r   )r   rk   r   )r   rh   fgrm   r   r   r   test_gather_limited_dupes   s   z%AsyncioTest.test_gather_limited_dupesc                    s   G dd dt  d fdd	}|   t|ddd|d	I d H  W d    n1 s/w   Y  tj|ddd|d	dd
I d H }| |d d	 | |d   d S )Nc                   @   s   e Zd ZdS )z<AsyncioTest.test_gather_with_exceptions.<locals>.MyExceptionN)__name__
__module____qualname__r   r   r   r   MyException   s    r   Fc                    s"   t | I d H  |r | | S r
   r   )rg   failr   r   r   rh      s
   z3AsyncioTest.test_gather_with_exceptions.<locals>.fngMb`?T)r   re   rR   r   r   )F)rK   r1   r   rk   r   rU   )r   rh   rm   r   r   r   test_gather_with_exceptions   s    z'AsyncioTest.test_gather_with_exceptionsc                    s   ddfdd  fdd}t tdrt| }nt| }tdI d H  |  | tj |I d H  W d    n1 sFw   Y  |  |  d S )NFc                      s4   zdt dI d H  W d S  t jy   d  w )NTr8   )r   r   rZ   r   )	cancelledstartedr   r   _fn   s   z+AsyncioTest.test_gather_cancel.<locals>._fnc                      s   t   I d H  d S r
   )r   rk   r   )r   r   r   _gather   s   z/AsyncioTest.test_gather_cancel.<locals>._gatherri   r   )	rj   r   ri   r_   r   r`   r1   rZ   
assertTrue)r   r   ra   r   )r   r   r   r   test_gather_cancel   s   


zAsyncioTest.test_gather_cancelN)r~   r   r   r   r   r+   r5   rF   rQ   rW   r\   rb   rn   rz   r}   r   r   r   r   r   r   r	      s4    










r	   )r   unittestr   aioitertoolsr   aioitertools.asyncior   helpersr   slistr:   sranger	   r   r   r   r   <module>   s   