U
    l/eH                     @   sZ   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dg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   >/tmp/pip-unpacked-wheel-n_uj7zou/aioitertools/tests/asyncio.pytest_import   s    zAsyncioTest.test_importc                    s   dd  dddg}dddg} fd	d
|D }t t|I d H }| ||  fdd
|D }g }t|2 z3 d H W }|| qp6 | || d S )Nc                    s   t |I d H  | S r
   r   sleep)numberdurationr   r   r   sleepy   s    z-AsyncioTest.test_as_completed.<locals>.sleepy)r   g333333?)   皙?)r         ?r   r   r   c                    s   g | ]} | qS r   r   .0pairr   r   r   
<listcomp>   s     z1AsyncioTest.test_as_completed.<locals>.<listcomp>c                    s   g | ]} | qS r   r   r   r   r   r   r       s     )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 5 Q R X dd |D }d}|  tj, tj|dd2 z3 d H W }|d	7 }qv6 W 5 Q R X | |d	 d S )
N)g      ?)r   c                 S   s   g | ]}t j| qS r   r   r   argsr   r   r   r   *   s     z9AsyncioTest.test_as_completed_timeout.<locals>.<listcomp>r   )timeoutc                 S   s   g | ]}t j| qS r   r   r)   r   r   r   r   .   s     r   r   )assertRaisesr   TimeoutErrorr   r    r   r!   r   )r   Zcallsr%   r&   _r   r   r   test_as_completed_timeout&   s    $z%AsyncioTest.test_as_completed_timeoutc                    sx   dd }| | | g}t tdd }g }t|2 z3 d H W }|| q66 | dt| | t|t| d S )Nc                  S  s(   t dD ]} | V  tdI d H  qd S N
   r   ranger   r   ir   r   r   gen7   s    z*AsyncioTest.test_as_generated.<locals>.genr1   r      )	r    r3   r   as_generatedr"   r   lenassertListEqualsortedr   r6   gensr$   r&   r'   r   r   r   test_as_generated5   s    zAsyncioTest.test_as_generatedc              	      sl   dd }dd }| | g}g }|  td* t|2 z3 d H W }|| q86 W 5 Q R X | d| d S )Nc                  S  s0   t dD ]} | V  tdI d H  qtdd S Nr   r   faker3   r   r   	Exceptionr4   r   r   r   gen1F   s    z5AsyncioTest.test_as_generated_exception.<locals>.gen1c                  S  s(   t dD ]} | V  tdI d H  qd S r0   r2   r4   r   r   r   gen2L   s    z5AsyncioTest.test_as_generated_exception.<locals>.gen2r@   r1   )assertRaisesRegexrB   r   r8   r"   ZassertNotIn)r   rC   rD   r=   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rl|| qJ|| qJ6 | t|t| | 	d	t
| | |d
 t d S )Nc                  S  s0   t dD ]} | V  tdI d H  qtdd S r?   rA   r4   r   r   r   rC   Z   s    z<AsyncioTest.test_as_generated_return_exception.<locals>.gen1c                  S  s(   t dD ]} | V  tdI d H  qd S r0   r2   r4   r   r   r   rD   `   s    z<AsyncioTest.test_as_generated_return_exception.<locals>.gen2r   r1   TZreturn_exceptionsr   r   )r    r3   r   r8   
isinstancerB   r"   r:   r;   r   r9   assertIsInstance)r   rC   rD   r=   r$   errorsr&   r'   r   r   r   "test_as_generated_return_exceptionX   s    
z.AsyncioTest.test_as_generated_return_exceptionc                    sv   dt ddd}|d| g}ttdttd }g }t|2 z3 d H W }|| qD6 | t|t| d S )	Nr1   )maxc                 S  s6   t dD ](}|| krtj|V  tdI d H  qd S )N   r   )r3   r   CancelledErrorr   )rL   r5   r   r   r   r6   t   s
    z9AsyncioTest.test_as_generated_task_cancelled.<locals>.genr   r   rM   )r1   )intr    r3   r   r8   r"   r:   r;   r<   r   r   r    test_as_generated_task_cancelledr   s    z,AsyncioTest.test_as_generated_task_cancelledc                    sl   dd  ddddg}g  fdd}t | }t dI d H  |  |I d H  | t|t d S )Nc                  S  s(   t dD ]} | V  tdI d H  qd S )NrM   r   r2   r4   r   r   r   r6      s    z4AsyncioTest.test_as_generated_cancelled.<locals>.genr   r   c                     s4       g} t | 2 z3 d H W }| q6 S r
   )r   r8   r"   )r=   r'   r6   r&   r   r   foo   s    z4AsyncioTest.test_as_generated_cancelled.<locals>.foog333333?)r   ensure_futurer   cancelr:   r;   )r   r$   rR   taskr   rQ   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 }| dddg| d S )Nc                    s   t dI d H  | S NMbP?r   argr   r   r   fn   s    z/AsyncioTest.test_gather_input_types.<locals>.fnr   r   create_taskr   )r   rS   hasattrr"   r\   r   gatherr   )r   r[   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 | dd	dd
ddddddg
| t jfddtdD  I d H }| d | dd	dd
ddddddg
| d S )Nr   c                    s.    d7  t  tdI d H   d8  | S )Nr   rX   )rL   r   r   rY   )countermax_counterr   r   r[      s
    
z+AsyncioTest.test_gather_limited.<locals>.fnc                    s   g | ]} |qS r   r   r   r5   r[   r   r   r      s     z3AsyncioTest.test_gather_limited.<locals>.<listcomp>r1   limitr   r   r      rM            	   c                    s   g | ]} |qS r   r   rd   re   r   r   r      s     )r   r^   r3   r   )r   r`   r   )rb   r[   rc   r   test_gather_limited   s    	( " zAsyncioTest.test_gather_limitedc              	      s   dd }|d}|d}t j||||||ddI d H }| ddddddg| |d}|d}t ||||||I d H }| ddddddg| d S )Nc                    s   t dI d H  | S rW   r   rY   r   r   r   r[      s    z1AsyncioTest.test_gather_limited_dupes.<locals>.fnr   r   )rf   )r   r^   r   )r   r[   fgr`   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 5 Q R X 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   rs   Fc                    s    t | I d H  |r | | S r
   r   )rZ   failrs   r   r   r[      s    z3AsyncioTest.test_gather_with_exceptions.<locals>.fngMb`?T)rt   rX   rG   r   r   )F)rB   r,   r   r^   r   rI   )r   r[   r`   r   ru   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 5 Q R X |  |  d S )NFc                      s:   zdt dI d H  W n t jk
r4   d  Y nX d S )NTr1   )r   r   rN   r   )	cancelledstartedr   r   _fn   s    z+AsyncioTest.test_gather_cancel.<locals>._fnc                      s   t   I d H  d S r
   )r   r^   r   )ry   r   r   _gather   s    z/AsyncioTest.test_gather_cancel.<locals>._gatherr\   r   )	r]   r   r\   rS   r   rT   r,   rN   
assertTrue)r   rz   rU   r   )ry   rw   rx   r   test_gather_cancel   s    


zAsyncioTest.test_gather_cancelN)rp   rq   rr   r   r   r(   r/   r>   rF   rK   rP   rV   ra   rl   ro   rv   r|   r   r   r   r   r	      s2   










r	   )r   Zunittestr   Zaioitertoolsr   Zaioitertools.asyncior   Zhelpersr   slistr3   sranger	   r   r   r   r   <module>   s   
