o
    U˟iG                     @   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rxe	js~e	j s~e	j!rdZne	j"se	j#rd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 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 _/var/www/apps/myagent/mysuperagent/venv/lib/python3.10/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 r   r   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 }z|| W n
 ty   Y d S w | d d S )N   c                   S   s   t  r   r   r   r   r   r   raiser)   s   z3TestCoroutinePool.test_apply_raises.<locals>.raiserz$Should have raised ExpectedException)r   r   r   fail)r   r   r#   r   r   r   test_apply_raises&   s   
z#TestCoroutinePool.test_apply_raisesFc                    s`   t   g  fdd} fdd}| d}||}|| |  | g d d S )Nc                      s    t d d    d S )NMbP?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   consumer?   s   
z7TestCoroutinePool.test_multiple_coros.<locals>.consumerr   )r/   r'   r0   )r   r   spawnr   getr   )r   r.   r1   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      z<TestCoroutinePool.dont_test_timer_cancel.<locals>.fire_timerc                      s   t d  d S )Nr   )r(   timerr   )r6   r   r   r   P   s   z;TestCoroutinePool.dont_test_timer_cancel.<locals>.some_workr   r   )r   r   r(   r)   r   r   r   )r6   r5   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       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}zct |_|t}td | t	|j
 W d    n1 s.w   Y  td | | d tjd |tjd W d    n1 sXw   Y  W ||_|  d S W ||_|  d S ||_|  w )Nr   r"   sizer   皙?)r   )sysr   stderrFakeFiler2   crashr(   TimeoutassertRaisesRuntimeErrorr3   r)   r   
free_count	start_newr   join)r   rE   r   
normal_errwaiterr   r   r   test_stderr_raising`   s*   


z%TestCoroutinePool.test_stderr_raisingN)__name__
__module____qualname__r(   r   Poolr   r   r!   r%   error_fatalr4   r9   rA   	greentest
skipOnPyPyrQ   r   r   r   r   r      s    

r   c                  O      t d)Nz*Raising an error from the crash() functionrK   )_args_kwr   r   r   rH         rH   c                   @      e Zd Zdd ZdS )rG   c                 G   rY   )NzWriting to the file failedrZ   )r   r[   r   r   r   write   r]   zFakeFile.writeN)rR   rS   rT   r_   r   r   r   r   rG          rG   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	|g d	 d S )
Nr   rB   r"   r   )r   )   )   {Gz?)r"   r   rb   rc   )
r   r   rL   r2   r*   r3   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"   rB     r   )r   r2   r(   r)   discardkillassertFalser   len
_semaphorecounter)r   rf   rh   r   r   r   test_discard   s   

zPoolBasicTests.test_discardc              	   C   s$  | j dd}ttjd}zttjd}zk| | d | t|d || | | d | t|d | tj	" t	d || W d    n1 sWw   Y  W d    n1 sfw   Y  | | d | t|d W |
  n|
  w W |
  d S |
  w )Nr"   rB   rj   r   rD   )r   r(   r2   r)   r   rL   rn   addrJ   rI   rl   r   rf   rh   secondr   r   r   test_add_method   s(   
zPoolBasicTests.test_add_methodc                 C      | j dd}ttjd}z=ttjd}z)|| | tjj |j|dd W d    n1 s4w   Y  W |  n|  w W |  d S |  w )Nr"   rB   rj   F)blocking	r   r(   r2   r)   rr   rJ   r   PoolFullrl   rs   r   r   r   test_add_method_non_blocking      
z+PoolBasicTests.test_add_method_non_blockingc                 C   rv   Nr"   rB   rj   rD   timeoutrx   rs   r   r   r   test_add_method_timeout   r{   z&PoolBasicTests.test_add_method_timeoutc                 C   s   | j dd}ttjd}z=ttjd}z)|| | tjj |j	|dd W d    n1 s4w   Y  W |
  n|
  w W |
  d S |
  w r|   )r   r(   r2   r)   Greenletrr   rJ   r   ry   startrl   rs   r   r   r   test_start_method_timeout   r{   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<      r=   z+PoolBasicTests.test_apply.<locals>.<lambda>)r"   )r   r"   r   )r   rf   r    r   r   r   r!      s   zPoolBasicTests.test_applyc                 C   s   d| _ | t| jd d S )NF)switch_expectedrJ   
ValueErrorr   r   r   r   r   test_init_error   s   zPoolBasicTests.test_init_errorN)rR   rS   rT   r(   r   rU   r   ri   rq   ru   rW   ignores_leakcheckrz   r   r   r!   r   r   r   r   r   ra      s    	


ra   c                   @   s   e Zd Zdd Zdd ZdS )TimingWrapperc                 C   s   || _ d | _d S r   )funcelapsed)r   r   r   r   r   __init__	  s   
zTimingWrapper.__init__c                 O   s4   t  }z| j|i |W t  | | _S t  | | _w r   )r   r   r   )r   argskwdstr   r   r   __call__  s   zTimingWrapper.__call__N)rR   rS   rT   r   r   r   r   r   r   r     s    r           c                 C   s   t | | |  S r   )r(   r)   )r   r   r   r   r   sqr  s   
r   c                 C   s   | |  S r   r   r   r   r   r   squared  r]   r   c                 C   s   t t d  | |  S )NrD   )r(   r)   randomr   r   r   r   sqr_random_sleep     r   c                   c   s    t dE d H  td d S )Nrb   皙?)ranger(   r)   r   r   r   r   final_sleep#  s   r   )gˡE?gQ?gQ?
   rj      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 r   )rW   TestCasesetUpr(   r   rU   rC   r   r   r   r   r   8  s   zTestPool.setUpc                 C   s   | j   d S r   )r   rN   r   r   r   r   cleanup<  r7   zTestPool.cleanupc                 C   s6   | j j}| |tdd | |tdddid d S )Nr>   r   r   r   rb   	   )r   r   r   r   )r   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 )Nr   )r   mapr   r   r   SMALL_RANGElistr   )r   pmapr   r   r   test_mapD  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   r   TIMEOUT1r   r3   r   assertTimeoutAlmostEqualr   r   resr3   r   r   r   
test_asyncI  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   r   r   r*   r   r3   r   r   r   r(   r)   )r   r    r   r3   r   r   r   test_async_callbackO  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 )Nr@   r   r}   r"   )r   r   r   TIMEOUT2r   r3   rJ   r(   rI   r   r   rN   r   r   r   r   test_async_timeoutX  s
   
zTestPool.test_async_timeoutc                 C   s4   | j ttt}| t|ttttt d S r   )r   imapr   r   r   r   r   r   r   itr   r   r   test_imap_list_small_     "zTestPool.test_imap_list_smallc                 C   F   | j ttt}ttD ]}| t|||  q| tt| d S r   )	r   r   r   r   r   r   nextrJ   StopIterationr   r   ir   r   r   test_imap_it_smallc     zTestPool.test_imap_it_smallc                 C   r   r   )	r   r   r   r   LARGE_RANGEr   r   rJ   r   r   r   r   r   test_imap_it_largei  r   zTestPool.test_imap_it_largec                 C   s4   | j ttt}| t|ttttt d S r   )	r   r   r   r   r   r   r   r   r   r   r   r   r   test_imap_randomo  r   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 r   )
r   imap_unorderedr   r   r   r   re   r   r   r   r   r   r   r   test_imap_unordereds  s   "zTestPool.test_imap_unorderedc                 C   s4   | j ttt}| t|ttt	tt d S r   )
r   r   r   r   r   r   re   r   r   r   r   r   r   r   test_imap_unordered_randomz  r   z#TestPool.test_imap_unordered_randomc                 C   "   | j tg }| t|g  d S r   )r   r   r   r   r   r   r   r   r   test_empty_imap_unordered~     z"TestPool.test_empty_imap_unorderedc                 C   r   r   )r   r   r   r   r   r   r   r   r   test_empty_imap  r   zTestPool.test_empty_imapc                 C   s   |  | jtg g  d S r   )r   r   r   r   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 )NrD   r   r   r         ?)
r   	map_asyncr(   r)   rC   r   rl   assertTimeWithinRanger   rN   )r   r    rl   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)     r   zTestPool.sleepc                 C   sF   t | j| jg d}| jjdkrg d}ng d}| || d S )N)r   r"   r   r"   )1012)r   r   r   )r   r   r   r)   rC   r   )r   r    expectedr   r   r   test_imap_unordered_sleep  s
   
z"TestPool.test_imap_unordered_sleepc                 C   H   t  }|d td|jt t| jdd |}| |dg d S )N{   rD   c                 S      | S r   r   r
   r   r   r   r<         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  
   
zTestPool.test_imap_no_stopc                 C   r   )Ni  rD   c                 S   r   r   r   r
   r   r   r   r<     r   z6TestPool.test_imap_unordered_no_stop.<locals>.<lambda>)	r   r   r(   r   r   r   r   r   r   r   r   r   r   test_imap_unordered_no_stop  r   z$TestPool.test_imap_unordered_no_stopc                 C   (   t | jtt }| |g d d S N)r   r"   rc   )r   r   r   r   r   r   r   r    r   r   r   test_imap_final_sleep     zTestPool.test_imap_final_sleepc                 C   r   r   )r   r   r   r   r   r   r   r   r   r   test_imap_unordered_final_sleep  r   z(TestPool.test_imap_unordered_final_sleepc                    s|   t td}dg  fdd} fdd}jjjjfD ]}d d< ||||dd}||}| }t|| qd S )	Nr   r   c                    s    d  d7  < | S )Nr   r"   r   )r   _j)runningr   r   short_running_func  s   zFTestPool.test_imap_unordered_bounded_queue.<locals>.short_running_funcc                    s    fdd}|S )Nc                     sf   g } t  D ]*\}}d |d kd  | | td t jdkt j q| S )Nr   r   rd   )	enumerate
assertTruer*   r(   r)   rn   queue)r    r   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   r   r   r   r   r   re   )r   iterabler   r   methr   r   lr   r   r   !test_imap_unordered_bounded_queue  s   z*TestPool.test_imap_unordered_bounded_queueN)rR   rS   rT   rW   LARGE_TIMEOUT__timeout__rC   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   r   r   r   4  s6    	
r   c                   @      e Zd ZdZdS )	TestPool2r   NrR   rS   rT   rC   r   r   r   r   r         r   c                   @   r   )	TestPool3rb   Nr   r   r   r   r   r     r   r   c                   @   r   )
TestPool10r   Nr   r   r   r   r   r     r   r   c                   @   s   e Zd ZdZdS )TestPoolUnlimitNr   r   r   r   r   r     s    r   c                   @      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   rB   rd   r}   )r(   r   rU   r   rL   r   fullwait_availabler   rf   r   r   r   test_wait_full  s   zTestPool0.test_wait_fullN)rR   rS   rT   rC   r  r   r   r   r   r         r   c                   @   r^   )TestJoinSleepc                 C   s>   t j }|t jd}z|j|d W |  d S |  w )Nr   r}   r(   r   rU   r2   r)   rN   rl   r   r~   rf   gr   r   r   r     s
   
zTestJoinSleep.waitNrR   rS   rT   r   r   r   r   r   r    r`   r  c                   @   r^   )TestJoinSleep_raise_errorc                 C   s@   t j }|t jd}z|j|dd W |  d S |  w )Nr   T)r~   raise_errorr  r  r   r   r   r      s
   
zTestJoinSleep_raise_error.waitNr
  r   r   r   r   r    r`   r  c                   @   r   )TestJoinEmptyFc                 C   s"   t j }| }| |d d S )Nzempty should return true)r(   r   rU   rN   r   r   rf   r   r   r   r   test  s   
zTestJoinEmpty.testN)rR   rS   rT   r   r  r   r   r   r   r  	  r  r  c                   @       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s3dnd | t|d d S )Nr"   r   rD   gRQ?r   )	r(   r   rU   r   rn   r2   r)   rW   EXPECT_POOR_TIMER_RESOLUTIONr  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   rD   rd   z*waiting on a full pool should return falsez waiting to finish should be true)
r(   r   rU   r   rn   r2   r)   rN   rm   r   r  r   r   r   testSpawnAndWait  s   
zTestSpawn.testSpawnAndWaitN)rR   rS   rT   r   r  r  r   r   r   r   r    s    
r  c                   c   s    dV  dV  t )Nr"   r   r   r   r   r   r   
error_iter*  s   r  c                   @   r  )TestErrorInIteratorFc                 C   s2   t jd}| t|jdd t  t d d S )Nrb   c                 S      d S r   r   r   r   r   r   r<   5  r   z*TestErrorInIterator.test.<locals>.<lambda>r&   )r(   r   rU   rJ   r   r   r  r)   r  r   r   r   r  3  s   zTestErrorInIterator.testc                    s2   t jd  fdd}| t| t d d S )Nrb   c                      s   t  dd t S )Nc                 S   r  r   r   r   r   r   r   r<   <  r   zGTestErrorInIterator.test_unordered.<locals>.unordered.<locals>.<lambda>)r   r   r  r   rf   r   r   	unordered;  s   z5TestErrorInIterator.test_unordered.<locals>.unorderedr&   )r(   r   rU   rJ   r   r)   )r   r  r   r  r   test_unordered8  s   z"TestErrorInIterator.test_unorderedN)rR   rS   rT   rV   r  r  r   r   r   r   r  0  s    r  c                 C   s   d|  S )N      ?r   r   r   r   r   	divide_byB  r]   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g d d S )Nrb   r"   r   r   )r(   r   rU   rJ   ZeroDivisionErrorr   r  r  r   r   r   r   I  s   zTestErrorInHandler.test_mapc                 C   \   t jd}|tg d}| t|d | tt| | t|d | t	t| d S Nr"   r  r  r   )
r(   r   rU   r   r  r   r   rJ   r  r   r   rf   r   r   r   r   	test_imapM     zTestErrorInHandler.test_imapc                 C   r  r   )
r(   r   rU   r   r  r   r   rJ   r  r   r!  r   r   r   r   U  r#  z&TestErrorInHandler.test_imap_unorderedN)rR   rS   rT   rV   r   r"  r   r   r   r   r   r  F  s
    r  __main__)r   )7r   r(   gevent.poolgevent.eventr   gevent.queuer   gevent.testingtestingrW   gevent.testing.timingr   r   unittestr   r   rH   objectrG   ra   r   r   r   r   r   r   r   TIMEOUT3r   r   PYPYWINRUN_LEAKCHECKSRUN_COVERAGERUNNING_ON_CIr  r   r   r   r   r   r   r   timingAbstractGenericWaitTestCaser  r  r  r  r  r  r  r  rR   mainr   r   r   r   <module>   s^    vx

 %
	
