B
    H/\F                 @   s,  d dl m Z  d dlZd dlZd dlmZ d dlmZ d dlmZ	 d dl
Zd dlZd dlmZ d dlZG dd dejZdd	 ZG d
d deZG dd de	jZG dd deZd;ddZdd Zdd Zdd Zd\ZZZdZdZe	jre	jse	j se	j!rdZne	j"se	j#rdZG dd de	jZ$e	j%G d d! d!e$Z&e	j%G d"d# d#e$Z'e	j%G d$d% d%e$Z(G d&d' d'e$Z)G d(d) d)e	jZ*G d*d+ d+ejj+j,Z-G d,d- d-ejj+j,Z.G d.d/ d/e	jZ/G d0d1 d1e	jZ0d2d3 Z1G d4d5 d5e	jZ2d6d7 Z3G d8d9 d9e	jZ4e5d:kr(e	6  dS )<    )timeN)Event)Queue)ExpectedExceptionc               @   s\   e Zd ZejjZdd Zdd Zdd Z	de	_
dd	 Zd
d Zdd Zeddd ZdS )TestCoroutinePoolc                s4   t    fdd}| d}||d    d S )Nc                s       d S )N)set)_)done 6lib/python3.7/site-packages/gevent/tests/test__pool.py	some_work   s    z5TestCoroutinePool.test_apply_async.<locals>.some_work   )x)r   klassapply_asyncwait)selfr   poolr
   )r	   r   test_apply_async   s
    
z"TestCoroutinePool.test_apply_asyncc                s4   d  fdd}|  d}||}|  | d S )Nzreturn valuec                  s    S )Nr
   r
   )valuer
   r   r      s    z/TestCoroutinePool.test_apply.<locals>.some_workr   )r   applyassertEqual)r   r   r   resultr
   )r   r   
test_apply   s
    

zTestCoroutinePool.test_applyc             C   sD   |  d}dd }y|| W n tk
r4   Y nX | d d S )N   c               S   s
   t  d S )N)r   r
   r
   r
   r   raiser)   s    z3TestCoroutinePool.test_apply_raises.<locals>.raiserz$Should have raised ExpectedException)r   r   r   Zfail)r   r   r   r
   r
   r   test_apply_raises&   s    
z#TestCoroutinePool.test_apply_raisesFc                sb   t   g  fdd} fdd}| d}||}|| |  | dddg d S )	Nc                  s    t d d    d S )NgMbP?prod)geventsleepappendr   r
   )evtresultsr
   r   producer:   s    

z7TestCoroutinePool.test_multiple_coros.<locals>.producerc                  s     d     d d S )Ncons1cons2)r    r   r
   )r!   r"   r
   r   consumer?   s    
z7TestCoroutinePool.test_multiple_coros.<locals>.consumerr   r$   r   r%   )r   r   spawnr   getr   )r   r#   r&   r   r	   r
   )r!   r"   r   test_multiple_coros6   s    


z%TestCoroutinePool.test_multiple_corosc                sJ   g fdd  fdd}|  d}|| td | g  d S )Nc                  s     d d S )NT)r    r
   )timer_firedr
   r   
fire_timerM   s    z<TestCoroutinePool.dont_test_timer_cancel.<locals>.fire_timerc                  s   t d  d S )Nr   )r   Ztimerr
   )r+   r
   r   r   P   s    z;TestCoroutinePool.dont_test_timer_cancel.<locals>.some_workr   r   )r   r   r   r   r   )r   r   r   r
   )r+   r*   r   dont_test_timer_cancelJ   s    


z(TestCoroutinePool.dont_test_timer_cancelc             C   sJ   |  d}||jdd df}| |d t }||j |  d S )Nr   c             S   s   | d S )Nr   r
   )ar
   r
   r   <lambda>Z   s    z2TestCoroutinePool.test_reentrant.<locals>.<lambda>)      )r   r   r   r   r   r   r   )r   r   r   r!   r
   r
   r   test_reentrantX   s    
z TestCoroutinePool.test_reentrantzDoes not work on PyPyc          	   C   s   dd l }| jdd}|j}zzt |_|t}td | t	|j
 W d Q R X td | | d tjd |tjd W d Q R X W d ||_|  X d S )Nr   r   )sizer   g?)r   )sysr   stderrFakeFiler'   crashr   TimeoutassertRaisesRuntimeErrorr(   r   r   
free_count	start_newr   join)r   r3   r   Z
normal_errZwaiterr
   r
   r   test_stderr_raising`   s    

z%TestCoroutinePool.test_stderr_raisingN)__name__
__module____qualname__r   r   Poolr   r   r   r   error_fatalr)   r,   r1   	greentestZ
skipOnPyPyr=   r
   r
   r
   r   r      s   

r   c              O   s   t dd S )NZWhoa)r9   )_argsZ_kwr
   r
   r   r6      s    r6   c               @   s   e Zd Zdd ZdS )r5   c             G   s   t dd S )NZWhaaa)r9   )r   rD   r
   r
   r   write   s    zFakeFile.writeN)r>   r?   r@   rE   r
   r
   r
   r   r5      s   r5   c               @   sf   e Zd ZejjZdd Zdd Zdd Z	e
jdd Ze
jd	d
 Ze
jdd Zdd Zdd ZdS )PoolBasicTestsc             C   s  | j dd}| | d g }||jd}| | d |  | |dg td | | d ||jd | d|  | |dg ||jd | d|  | |dg ||jd | |dg td | t	|ddd	d
g d S )Nr   )r2   r   r   )r   )   )   g{Gz?rG   rH   )
r   r   r:   r'   r    r(   r   r   r   sorted)r   prfirstr
   r
   r   test_execute_async   s&    

z!PoolBasicTests.test_execute_asyncc             C   sZ   | j dd}|tjd}|| |  | | | t|d | |j	j
d d S )Nr   )r2   i  r   )r   r'   r   r   discardkillassertFalser   lenZ
_semaphoreZcounter)r   rJ   rL   r
   r
   r   test_discard   s    

zPoolBasicTests.test_discardc          
   C   s   | j dd}ttjd}zttjd}z| | d | t|d || | | d | t|d | tj	& t	d || W d Q R X W d Q R X | | d | t|d W d |
  X W d |
  X d S )Nr   )r2   i  r   g?)r   r   r'   r   r   r:   rQ   addr8   r7   rO   )r   rJ   rL   secondr
   r
   r   test_add_method   s"    
zPoolBasicTests.test_add_methodc          	   C   s~   | j dd}ttjd}zTttjd}z6|| | tjj |j|dd W d Q R X W d |  X W d |  X d S )Nr   )r2   i  F)Zblocking)	r   r   r'   r   rS   r8   r   PoolFullrO   )r   rJ   rL   rT   r
   r
   r   test_add_method_non_blocking   s    
z+PoolBasicTests.test_add_method_non_blockingc          	   C   s~   | j dd}ttjd}zTttjd}z6|| | tjj |j|dd W d Q R X W d |  X W d |  X d S )Nr   )r2   i  g?)timeout)	r   r   r'   r   rS   r8   r   rV   rO   )r   rJ   rL   rT   r
   r
   r   test_add_method_timeout   s    
z&PoolBasicTests.test_add_method_timeoutc          	   C   s~   | j dd}ttjd}zTttjd}z6|| | tjj |j	|dd W d Q R X W d |
  X W d |
  X d S )Nr   )r2   i  g?)rX   )r   r   r'   r   ZGreenletrS   r8   r   rV   startrO   )r   rJ   rL   rT   r
   r
   r   test_start_method_timeout   s    
z(PoolBasicTests.test_start_method_timeoutc             C   s(   |   }|dd d}| |d d S )Nc             S   s   d| fS )Nfoor
   )r-   r
   r
   r   r.      s    z+PoolBasicTests.test_apply.<locals>.<lambda>)r   )r\   r   )r   r   r   )r   rJ   r   r
   r
   r   r      s    zPoolBasicTests.test_applyc             C   s   d| _ | t| jd d S )NF)switch_expectedr8   
ValueErrorr   )r   r
   r
   r   test_init_error   s    zPoolBasicTests.test_init_errorN)r>   r?   r@   r   r   rA   r   rM   rR   rU   rC   ignores_leakcheckrW   rY   r[   r   r`   r
   r
   r
   r   rF      s   	rF   c               @   s   e Zd Zdd Zdd ZdS )TimingWrapperc             C   s   || _ d | _d S )N)funcelapsed)r   rc   r
   r
   r   __init__  s    zTimingWrapper.__init__c             O   s&   t  }z| j||S t  | | _X d S )N)r   rc   rd   )r   argskwdstr
   r
   r   __call__  s    zTimingWrapper.__call__N)r>   r?   r@   re   ri   r
   r
   r
   r   rb     s   rb           c             C   s   t | | |  S )N)r   r   )r   r   r
   r
   r   sqr  s    
rk   c             C   s   | |  S )Nr
   )r   r
   r
   r   squared  s    rl   c             C   s   t t d  | |  S )Ng?)r   r   random)r   r
   r
   r   sqr_random_sleep  s    rn   c              c   s&   xt dD ]
} | V  q
W td d S )NrG   g?)ranger   r   )ir
   r
   r   final_sleep  s    
rq   )gˡE?gQ?gQ?
   i     d   c               @   s   e Zd Zej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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d0d1 Zd2S )3TestPoolr   c             C   s    t j|  tj| j| _d S )N)rC   TestCasesetUpr   r   rA   r2   )r   r
   r
   r   rw   4  s    zTestPool.setUpc             C   s   | j   d S )N)r   r<   )r   r
   r
   r   cleanup8  s    zTestPool.cleanupc             C   s6   | j j}| |tdd | |tdddid d S )N)r/   rs   r
   r   rG   	   )r   r   r   rk   )r   Zpapplyr
   r
   r   r   ;  s    zTestPool.test_applyc             C   sT   | j j}| |tttttttt | |ttdttttd d S )Nrt   )r   mapr   rk   ro   SMALL_RANGElistrl   )r   Zpmapr
   r
   r   test_map@  s    $zTestPool.test_mapc             C   s>   | j tdtf}t|j}| | d | |jtd d S )N   1   r   )	r   r   rk   TIMEOUT1rb   r(   r   assertTimeoutAlmostEqualrd   )r   resr(   r
   r
   r   
test_asyncE  s    
zTestPool.test_asyncc             C   s`   g }| j jtdtf|jd}t|j}| | d | |j	td t
d | |dg d S )Nr~   )callbackr   r   r   )r   r   rk   r   r    rb   r(   r   r   rd   r   r   )r   r   r   r(   r
   r
   r   test_async_callbackK  s    

zTestPool.test_async_callbackc             C   sP   | j tdtd f}t|j}| jtj|td | 	|j
td | j   d S )Nr0   g?)rX   r   )r   r   rk   TIMEOUT2rb   r(   r8   r   r7   r   rd   r<   )r   r   r(   r
   r
   r   test_async_timeoutT  s
    
zTestPool.test_async_timeoutc             C   s4   | j ttt}| t|ttttt d S )N)r   imaprk   ro   r{   r   r|   rz   )r   itr
   r
   r   test_imap_list_small[  s    zTestPool.test_imap_list_smallc             C   sJ   | j ttt}x$ttD ]}| t|||  qW | tt| d S )N)	r   r   rk   ro   r{   r   nextr8   StopIteration)r   r   rp   r
   r
   r   test_imap_it_small_  s    zTestPool.test_imap_it_smallc             C   sJ   | j ttt}x$ttD ]}| t|||  qW | tt| d S )N)	r   r   rk   ro   LARGE_RANGEr   r   r8   r   )r   r   rp   r
   r
   r   test_imap_it_largee  s    zTestPool.test_imap_it_largec             C   s4   | j ttt}| t|ttttt d S )N)	r   r   rn   ro   r{   r   r|   rz   rl   )r   r   r
   r
   r   test_imap_randomk  s    zTestPool.test_imap_randomc             C   sd   | j ttt}| t|ttt	tt | j ttt}| t|ttt	tt d S )N)
r   imap_unorderedrk   ro   r   r   rI   r|   rz   rl   )r   r   r
   r
   r   test_imap_unorderedo  s    zTestPool.test_imap_unorderedc             C   s4   | j ttt}| t|ttt	tt d S )N)
r   r   rn   ro   r{   r   rI   r|   rz   rl   )r   r   r
   r
   r   test_imap_unordered_randomv  s    z#TestPool.test_imap_unordered_randomc             C   s"   | j tg }| t|g  d S )N)r   r   rk   r   r|   )r   r   r
   r
   r   test_empty_imap_unorderedz  s    z"TestPool.test_empty_imap_unorderedc             C   s"   | j tg }| t|g  d S )N)r   r   rk   r   r|   )r   r   r
   r
   r   test_empty_imap~  s    zTestPool.test_empty_imapc             C   s   |  | jtg g  d S )N)r   r   rz   rk   )r   r
   r
   r   test_empty_map  s    zTestPool.test_empty_mapc             C   sX   | j tjdg| jpdd  }td t| j j}|  | |jdd |	  d S )Ng?rr   r   g        g      ?)
r   Z	map_asyncr   r   r2   rb   rO   ZassertTimeWithinRangerd   r<   )r   r   rO   r
   r
   r   test_terminate  s     
zTestPool.test_terminatec             C   s   t t|d  t|S )Ng      $@)r   r   floatstr)r   r   r
   r
   r   r     s    zTestPool.sleepc             C   sL   t | j| jdddg}| jjdkr2dddg}n
dddg}| || d S )Nrr   r   r   Z1012)r|   r   r   r   r2   r   )r   r   Zexpectedr
   r
   r   test_imap_unordered_sleep  s
    
z"TestPool.test_imap_unordered_sleepc             C   sH   t  }|d td|jt t| jdd |}| |dg d S )N{   g?c             S   s   | S )Nr
   )r   r
   r
   r   r.     s    z,TestPool.test_imap_no_stop.<locals>.<lambda>)	r   putr   spawn_laterr   r|   r   r   r   )r   qr   r
   r
   r   test_imap_no_stop  s
    
zTestPool.test_imap_no_stopc             C   sH   t  }|d td|jt t| jdd |}| |dg d S )Ni  g?c             S   s   | S )Nr
   )r   r
   r
   r   r.     s    z6TestPool.test_imap_unordered_no_stop.<locals>.<lambda>)	r   r   r   r   r   r|   r   r   r   )r   r   r   r
   r
   r   test_imap_unordered_no_stop  s
    
z$TestPool.test_imap_unordered_no_stopc             C   s*   t | jtt }| |dddg d S )Nr   r   rH   )r|   r   r   rk   rq   r   )r   r   r
   r
   r   test_imap_final_sleep  s    zTestPool.test_imap_final_sleepc             C   s*   t | jtt }| |dddg d S )Nr   r   rH   )r|   r   r   rk   rq   r   )r   r   r
   r
   r   test_imap_unordered_final_sleep  s    z(TestPool.test_imap_unordered_final_sleepc                s   t td}dg  fdd} fdd}xNjjjjfD ]:}d d< ||||dd}||}| }t|| q>W d S )	Nrt   r   c                s    d  d7  < | S )Nr   r   r
   )rp   Z_j)runningr
   r   short_running_func  s    zFTestPool.test_imap_unordered_bounded_queue.<locals>.short_running_funcc                s    fdd}|S )Nc                 sj   g } x`t  D ]T\}}d |d kd  | | td t jdkt j qW | S )Nr   r   g{Gz?)	enumerate
assertTruer    r   r   rQ   Zqueue)r   rp   r   )mappingr   r   r
   r   reader  s    

 zOTestPool.test_imap_unordered_bounded_queue.<locals>.make_reader.<locals>.readerr
   )r   r   )r   r   )r   r   make_reader  s    z?TestPool.test_imap_unordered_bounded_queue.<locals>.make_readerr   )maxsize)r|   ro   r   r   r   r   rI   )r   iterabler   r   methr   r   lr
   )r   r   r   !test_imap_unordered_bounded_queue  s    z*TestPool.test_imap_unordered_bounded_queueN)r>   r?   r@   rC   ZLARGE_TIMEOUTZ__timeout__r2   rw   rx   r   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r
   r
   r   ru   0  s4   	
ru   c               @   s   e Zd ZdZdS )	TestPool2r   N)r>   r?   r@   r2   r
   r
   r
   r   r     s   r   c               @   s   e Zd ZdZdS )	TestPool3rG   N)r>   r?   r@   r2   r
   r
   r
   r   r     s   r   c               @   s   e Zd ZdZdS )
TestPool10rr   N)r>   r?   r@   r2   r
   r
   r
   r   r     s   r   c               @   s   e Zd ZdZdS )TestPoolUnlimitN)r>   r?   r@   r2   r
   r
   r
   r   r     s   r   c               @   s   e Zd ZdZdd ZdS )	TestPool0r   c             C   sD   t jjdd}| d|  | |  | d|jdd d S )Nr   )r2   g{Gz?)rX   )r   r   rA   r   r:   r   ZfullZwait_available)r   rJ   r
   r
   r   test_wait_full  s    zTestPool0.test_wait_fullN)r>   r?   r@   r2   r   r
   r
   r
   r   r     s   r   c               @   s   e Zd Zdd ZdS )TestJoinSleepc             C   s8   t j }|t jd}z|j|d W d |  X d S )Nrr   )rX   )r   r   rA   r'   r   r<   rO   )r   rX   rJ   gr
   r
   r   r     s
    
zTestJoinSleep.waitN)r>   r?   r@   r   r
   r
   r
   r   r     s   r   c               @   s   e Zd Zdd ZdS )TestJoinSleep_raise_errorc             C   s:   t j }|t jd}z|j|dd W d |  X d S )Nrr   T)rX   Zraise_error)r   r   rA   r'   r   r<   rO   )r   rX   rJ   r   r
   r
   r   r     s
    
zTestJoinSleep_raise_error.waitN)r>   r?   r@   r   r
   r
   r
   r   r     s   r   c               @   s   e Zd ZdZdd ZdS )TestJoinEmptyFc             C   s"   t j }| }| |d d S )Nzempty should return true)r   r   rA   r<   r   )r   rJ   r   r
   r
   r   test  s    
zTestJoinEmpty.testN)r>   r?   r@   r^   r   r
   r
   r
   r   r     s   r   c               @   s    e Zd ZdZdd Zdd ZdS )	TestSpawnTc             C   s   t jd}| t|d |t jd | t|d |t jd | t|d t tjsfdnd | t|d d S )Nr   r   g?gRQ?g      ?)	r   r   rA   r   rQ   r'   r   rC   ZRUNNING_ON_APPVEYOR)r   rJ   r
   r
   r   r     s    zTestSpawn.testc             C   sx   t jd}| t|d |t jd | t|d |d}| |d | }| 	|d | t|d d S )Nr   r   g?g{Gz?z*waiting on a full pool should return falsez waiting to finish should be true)
r   r   rA   r   rQ   r'   r   r<   rP   r   )r   rJ   r   r
   r
   r   testSpawnAndWait  s    
zTestSpawn.testSpawnAndWaitN)r>   r?   r@   r^   r   r   r
   r
   r
   r   r     s   
r   c               c   s   dV  dV  t d S )Nr   r   )r   r
   r
   r
   r   
error_iter&  s    r   c               @   s    e Zd ZdZdd Zdd ZdS )TestErrorInIteratorFc             C   s2   t jd}| t|jdd t  t d d S )NrG   c             S   s   d S )Nr
   )r   r
   r
   r   r.   1  s    z*TestErrorInIterator.test.<locals>.<lambda>gMbP?)r   r   rA   r8   r   rz   r   r   )r   rJ   r
   r
   r   r   /  s    zTestErrorInIterator.testc                s2   t jd  fdd}| t| t d d S )NrG   c                  s   t  dd t S )Nc             S   s   d S )Nr
   )r   r
   r
   r   r.   8  s    zGTestErrorInIterator.test_unordered.<locals>.unordered.<locals>.<lambda>)r|   r   r   r
   )rJ   r
   r   	unordered7  s    z5TestErrorInIterator.test_unordered.<locals>.unorderedgMbP?)r   r   rA   r8   r   r   )r   r   r
   )rJ   r   test_unordered4  s    z"TestErrorInIterator.test_unorderedN)r>   r?   r@   rB   r   r   r
   r
   r
   r   r   ,  s   r   c             C   s   d|  S )Ng      ?r
   )r   r
   r
   r   	divide_by>  s    r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	TestErrorInHandlerFc             C   s(   t jd}| t|jtdddg d S )NrG   r   r   r   )r   r   rA   r8   ZeroDivisionErrorrz   r   )r   rJ   r
   r
   r   r}   E  s    zTestErrorInHandler.test_mapc             C   s^   t jd}|tdddg}| t|d | tt| | t|d | t	t| d S )Nr   r   r   g      ?g      ?)
r   r   rA   r   r   r   r   r8   r   r   )r   rJ   r   r
   r
   r   	test_imapI  s    zTestErrorInHandler.test_imapc             C   s^   t jd}|tdddg}| t|d | tt| | t|d | t	t| d S )Nr   r   r   g      ?g      ?)
r   r   rA   r   r   r   r   r8   r   r   )r   rJ   r   r
   r
   r   r   Q  s    z&TestErrorInHandler.test_imap_unorderedN)r>   r?   r@   rB   r}   r   r   r
   r
   r
   r   r   B  s   r   __main__)rj   )7r   r   Zgevent.poolZgevent.eventr   Zgevent.queuer   Zgevent.testingZtestingrC   Zgevent.testing.timingrm   r   Zunittestrv   r   r6   objectr5   rF   rb   rk   rl   rn   rq   r   r   ZTIMEOUT3r{   r   ZPYPYZWINZRUN_LEAKCHECKSZRUN_COVERAGEZRUNNING_ON_CIZEXPECT_POOR_TIMER_RESOLUTIONru   ra   r   r   r   r   r   ZtimingZAbstractGenericWaitTestCaser   r   r   r   r   r   r   r   r>   mainr
   r
   r
   r   <module>   sZ   qx

 %
	
