a
    „'fù`  ã                   @   st  d dl mZ d dlmZmZ d dlZd dlZd dlZd dl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lmZ d dlmZ ejdd	„ ƒZG d
d„ dejƒZG dd„ deƒZG dd„ deƒZdCdd„Zdd„ Zd\ZZZG dd„ deƒZdZdZejrej sej!sej"r dZG dd„ deƒZ#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ƒZ)d'd(„ Z*G d)d*„ d*eƒZ+G d+d,„ d,eƒZ,G d-d.„ d.eƒZ-G d/d0„ d0eƒZ.G d1d2„ d2eƒZ/G d3d4„ d4eƒZ0d5d6„ Z1G d7d8„ d8eƒZ2d d9l	m3Z3 e 4e5e	j6d:ƒd;¡G d<d=„ d=eƒƒZ7G d>d?„ d?ejƒZ8G d@dA„ dAeƒZ9e:dBkrpe ;¡  dS )Dé    )Úprint_function)ÚtimeÚsleepN)Ú
ThreadPool)ÚInvalidThreadUseError©ÚExpectedException)ÚPYPYc                  c   s:   t  ¡ } t  ¡  zd V  W | r6t  ¡  n| r4t  ¡  0 d S ©N)ÚgcÚ	isenabledÚdisableÚenable)Zwas_enabled© r   úk/var/www/staging/api/virtual_environments/venv/lib/python3.9/site-packages/gevent/tests/test__threadpool.pyÚdisabled_gc   s    
ÿr   c                   @   s<   e Zd ZejZdZdZeZ	dd„ Z
ejfdd„Zdd„ ZdS )	ÚTestCaseNr   c                 C   s   | j S r
   )ÚClassUnderTest©Úselfr   r   r   Ú_FUT*   s    zTestCase._FUTc                 C   s2   |   ¡ |ƒ | _}|  j|f7  _|r.|| j_|S r
   )r   ÚpoolÚ
_all_poolsÚsize)r   ÚmaxsizeÚcreate_all_worker_threadsr   r   r   r   Ú_makeOne-   s
    zTestCase._makeOnec                 C   s\   d | _ | jd }| _|D ]"}t|dd ƒp0t|dƒ}|ƒ  ~qtjrXtdƒD ]}t ¡  qJd S )Nr   ÚkillÚshutdowné   )r   r   ÚgetattrÚ	greentestÚRUN_LEAKCHECKSÚranger   Úcollect)r   Z	all_poolsr   r   Ú_r   r   r   Úcleanup5   s    zTestCase.cleanup)Ú__name__Ú
__module__Ú__qualname__r!   ZLARGE_TIMEOUTZ__timeout__r   r   r   r   r   r"   r   r&   r   r   r   r   r   #   s   r   c                   @   s2   e Zd Zdd„ Zdd„ Zdd„ Zde_dd	„ Zd
S )ÚPoolBasicTestsc                 C   s²   |   d¡}g }| |jd¡}| ¡  |  |dg¡ t d¡ | |jd¡ |  |dg¡ | |jd¡ |  |dg¡ | |jd¡ |  |dg¡ t d¡ |  t	|ƒg d¢¡ d S )	Né   é   r   )r+   )r   )é   ç{®Gáz„?)r,   r+   r   r-   )
r   ÚspawnÚappendÚgetÚassertEqualÚgeventr   Úapply_asyncÚassertEqualFlakyRaceConditionÚsorted)r   r   ÚrÚfirstr   r   r   Útest_execute_asyncI   s    


z!PoolBasicTests.test_execute_asyncc                 C   s*   |   d¡}| dd„ d¡}|  |d¡ d S )Nr,   c                 S   s   d| fS )NÚfoor   )Úar   r   r   Ú<lambda>^   ó    z+PoolBasicTests.test_apply.<locals>.<lambda>)r,   )r:   r,   )r   Úapplyr2   )r   r   Úresultr   r   r   Ú
test_apply\   s    
zPoolBasicTests.test_applyc                 C   sJ   |   d¡}dd„ }|  t¡ | |¡ W d   ƒ n1 s<0    Y  d S )Nr,   c                   S   s
   t ƒ ‚d S r
   r   r   r   r   r   Úraiserd   s    z0PoolBasicTests.test_apply_raises.<locals>.raiser)r   ÚassertRaisesr   r>   )r   r   rA   r   r   r   Útest_apply_raisesa   s    
z PoolBasicTests.test_apply_raisesFc                 C   s>   d| _ |  t¡ |  d¡ W d   ƒ n1 s00    Y  d S )NFéÿÿÿÿ)Úswitch_expectedrB   Ú
ValueErrorr   r   r   r   r   Útest_init_valueerrorn   s    z#PoolBasicTests.test_init_valueerrorN)r'   r(   r)   r9   r@   rC   Úerror_fatalrG   r   r   r   r   r*   G   s
   r*   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚTimingWrapperc                 C   s   || _ d | _d S r
   )ÚfuncÚelapsed)r   Zthe_funcr   r   r   Ú__init__y   s    zTimingWrapper.__init__c                 O   s8   t ƒ }z| j|i |¤ŽW t ƒ | | _S t ƒ | | _0 d S r
   )r   rJ   rK   )r   ÚargsÚkwdsÚtr   r   r   Ú__call__}   s    þzTimingWrapper.__call__N)r'   r(   r)   rL   rP   r   r   r   r   rI   w   s   rI   ç        c                 C   s   t |ƒ | |  S r
   )r   )ÚxÚwaitr   r   r   Úsqr…   s    rT   c                 C   s   t t ¡ d ƒ | |  S )Nçš™™™™™¹?)r   Úrandom©rR   r   r   r   Úsqr_random_sleepŠ   s    rX   )gË¡E¶óý´?gìQ¸…ë¡?gìQ¸…ëÁ?c                   @   s*   e Zd ZdZdZdd„ Zejdd„ ƒZdS )Ú_AbstractPoolTestr,   Fc                 C   s   t j | ¡ |  | j¡ d S r
   )r!   r   ÚsetUpr   r   r   r   r   r   rZ   —   s    z_AbstractPoolTest.setUpc                    sv   ˆ j j}ˆ jr‡ fdd„}ˆ  |ttdƒƒttttdƒƒƒ¡ ˆ  |ttdƒƒttttdƒƒƒ¡ ˆ j  ¡  ˆ ` ~d S )Nc                    s   t ˆ j | |¡ƒS r
   )Úlistr   Úmap)ÚfÚir   r   r   r<   Ÿ   r=   z,_AbstractPoolTest.test_map.<locals>.<lambda>é
   éd   )r   r\   Ú
MAP_IS_GENr2   rT   r#   r[   r   )r   Zpmapr   r   r   Útest_map›   s    $$
z_AbstractPoolTest.test_mapN)	r'   r(   r)   r   ra   rZ   r!   Úignores_leakcheckrb   r   r   r   r   rY   ‘   s
   rY   r_   iè  é2   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d„ Zdd „ Zd!S )"ÚTestPoolc                 C   s„   ddl m} ddlm} | j |¡}|  ||¡ t|ƒ}|  d|¡ |  d|¡ |  d|¡ ddl	m
} d |ƒ ¡}|  d	|¡ d S )
Nr   )Ú
getcurrent)Ú_WorkerGreenletZThreadPoolWorkerZthread_identzhub=)Úformat_run_infoÚ
z<ThreadPoolWorker)Zgreenletrf   Úgevent.threadpoolrg   r   r>   ÚassertIsInstanceÚreprZassertInZgevent.utilrh   Újoin)r   rf   rg   Zworker_greenletr7   rh   Úinfor   r   r   Útest_greenlet_classµ   s    zTestPool.test_greenlet_classc                 C   s@   | j j}|  |tdƒtdƒ¡ |  |tdddiƒtdd¡ d S )N)é   rp   r   rR   r   rW   )r   r>   r2   rT   )r   Zpapplyr   r   r   r@   Å   s    zTestPool.test_applyc                 C   s>   | j  tdtf¡}t|jƒ}|  |ƒ d¡ |  |jtd¡ d S )Né   é1   r,   )	r   r4   rT   ÚTIMEOUT1rI   r1   r2   ÚassertTimeoutAlmostEqualrK   ©r   Úresr1   r   r   r   Ú
test_asyncÊ   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 )Nrq   )Úcallbackrr   r,   r   )r   r4   rT   rs   r0   rI   r1   r2   rt   rK   r3   r   )r   r?   rv   r1   r   r   r   Útest_async_callbackÐ   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 )Né   çš™™™™™É?©Útimeoutr,   )r   r4   rT   ÚTIMEOUT2rI   r1   rB   r3   ÚTimeoutrt   rK   rm   ru   r   r   r   Útest_async_timeoutÙ   s
    
zTestPool.test_async_timeoutc                 C   s4   | j  tttƒ¡}|  t|ƒtttttƒƒƒ¡ d S r
   )r   ÚimaprT   r#   ÚSMALL_RANGEr2   r[   r\   ©r   Úitr   r   r   Útest_imap_list_smallà   s    zTestPool.test_imap_list_smallc                 C   sF   | j  tttƒ¡}ttƒD ]}|  t|ƒ|| ¡ q|  tt|¡ d S r
   )	r   r   rT   r#   r‚   r2   ÚnextrB   ÚStopIteration©r   r„   r^   r   r   r   Útest_imap_it_smallä   s    zTestPool.test_imap_it_smallc                 C   sF   | j  tttƒ¡}ttƒD ]}|  t|ƒ|| ¡ q|  tt|¡ d S r
   )	r   r   rT   r#   ÚLARGE_RANGEr2   r†   rB   r‡   rˆ   r   r   r   Útest_imap_it_largeê   s    zTestPool.test_imap_it_largec                 C   sN   | j  tttƒ¡}ttƒD ] }|  t|ƒ|| ¡ t ¡  q|  	t
t|¡ d S r
   )r   r   rT   r#   r‚   r2   r†   r   r$   rB   r‡   rˆ   r   r   r   Útest_imap_gcð   s
    
zTestPool.test_imap_gcc                 C   sŽ   | j  tttƒ¡}g }ttƒD ]}| t|ƒ¡ t ¡  q|  	t
¡ t|ƒ W d   ƒ n1 sb0    Y  |  t|ƒdd„ ttƒD ƒ¡ d S )Nc                 S   s   g | ]}|| ‘qS r   r   ©Ú.0rR   r   r   r   Ú
<listcomp>ÿ   r=   z3TestPool.test_imap_unordered_gc.<locals>.<listcomp>)r   Úimap_unorderedrT   r#   r‚   r0   r†   r   r$   rB   r‡   r2   r6   )r   r„   r?   r%   r   r   r   Útest_imap_unordered_gc÷   s    
&zTestPool.test_imap_unordered_gcc                 C   s4   | j  tttƒ¡}|  t|ƒtttttƒƒƒ¡ d S r
   )	r   r   rX   r#   r‚   r2   r[   r\   rT   rƒ   r   r   r   Útest_imap_random  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 r
   )	r   r   rT   r#   rŠ   r2   r6   r[   r\   rƒ   r   r   r   Útest_imap_unordered  s    zTestPool.test_imap_unorderedc                 C   s4   | j  tttƒ¡}|  t|ƒttt	ttƒƒƒ¡ d S r
   )
r   r   rX   r#   r‚   r2   r6   r[   r\   rT   rƒ   r   r   r   Útest_imap_unordered_random  s    z#TestPool.test_imap_unordered_randomc                 C   sŠ   | j pd}| j tdg|d  ¡}t d¡ zN| jd| j  d dd | j ¡  W d   ƒ n1 sf0    Y  W | ¡  n
| ¡  0 d S )Nr_   rU   r+   ç      à?r   )Zmin_time)r   r   Z	map_asyncr   r3   Zruns_in_given_timer   rm   )r   r   r?   r   r   r   Útest_terminate  s    

*zTestPool.test_terminatec                 C   s   t t|ƒd ƒ t|ƒS )Ng      $@)r   ÚfloatÚstr)r   rR   r   r   r   r     s    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   r   r2   )r   r?   Úexpectedr   r   r   Útest_imap_unordered_sleep  s
    
z"TestPool.test_imap_unordered_sleepN)r'   r(   r)   ro   r@   rw   ry   r€   r…   r‰   r‹   rŒ   r‘   r’   r“   r”   r–   r   r   r   r   r   r   re   ³   s    	

re   c                   @   s   e Zd ZdZejdd„ ƒZdS )Ú	TestPool2r+   c                    s6   | j ‰‡ ‡fdd„}dd„ ‰ ˆ |¡}|  |d¡ d S )Nc                      s
   ˆ  ˆ ¡S r
   )r>   r   ©ÚbÚpr   r   r;   /  s    z)TestPool2.test_recursive_apply.<locals>.ac                   S   s   t  ¡  t  d¡ dS )Nçü©ñÒMbP?ÚB©r3   r   r   r   r   r   r    2  s    
z)TestPool2.test_recursive_apply.<locals>.br£   )r   r>   r2   )r   r;   r?   r   rŸ   r   Útest_recursive_apply+  s
    
zTestPool2.test_recursive_applyN)r'   r(   r)   r   r!   rc   r¥   r   r   r   r   rž   (  s   rž   c                   @   s   e Zd ZdZdS )Ú	TestPool3r   N©r'   r(   r)   r   r   r   r   r   r¦   >  s   r¦   c                   @   s   e Zd ZdZdS )Ú
TestPool10r_   Nr§   r   r   r   r   r¨   B  s   r¨   c                   @   s2   e Zd ZdZe ejo ejo ejd¡dd„ ƒZ	dS )ÚTestJoinEmptyFzqThis sometimes appears to crash in PyPy2 5.9.0, but never crashes on macOS or local Ubunto with same PyPy versionc                 C   s   |   d¡}| ¡  d S ©Nr,   )r   rm   ©r   r   r   r   r   Útest[  s    
zTestJoinEmpty.testN)
r'   r(   r)   rE   r!   ZskipIfr	   ZLIBUVZRUNNING_ON_TRAVISr¬   r   r   r   r   r©   X  s
   ÿr©   c                   @   s,   e Zd ZdZejdd„ ƒZejdd„ ƒZdS )Ú	TestSpawnTc                    s¼   |   d¡}|  t|ƒd¡ g ‰ ‡ fdd„}| |dd¡ |  t|ƒd¡ | |dd¡ |  t|ƒd¡ t d	¡ |  ˆ dg¡ |  t|ƒd¡ t d	¡ |  ˆ ddg¡ |  t|ƒd¡ d S )
Nr,   r   c                    s   t |ƒˆ  | ¡gS r
   )r   r0   )ÚitemÚseconds©Úlogr   r   r<   m  r=   z'TestSpawn.test_basics.<locals>.<lambda>r;   rU   r    r+   g333333Ã?)r   r2   Úlenr/   r3   r   )r   r   Zsleep_n_logr   r°   r   Útest_basicsh  s    


zTestSpawn.test_basicsc                    s`   |   d¡}|   d¡‰ ‡ fdd„}| |¡}|  t¡ | ¡  W d   ƒ n1 sR0    Y  d S )Nr,   r+   c                      s   ˆ   dd„ ¡ d S )Nc                   S   s   dS )NZHir   r   r   r   r   r<   „  r=   zMTestSpawn.test_cannot_spawn_from_other_thread.<locals>.func.<locals>.<lambda>)r/   r   ©Zpool2r   r   rJ   ƒ  s    z;TestSpawn.test_cannot_spawn_from_other_thread.<locals>.func)r   r/   rB   r   r1   )r   Zpool1rJ   rv   r   r´   r   Ú#test_cannot_spawn_from_other_thread{  s    


z-TestSpawn.test_cannot_spawn_from_other_threadN)r'   r(   r)   rE   r!   rc   r³   rµ   r   r   r   r   r­   e  s
   
r­   c                   c   s   dV  dV  t j‚d S )Nr,   r+   )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   s6   |   d¡| _|  tj| jjdd„ tƒ ¡ t d¡ d S )Nr   c                 S   s   d S r
   r   rW   r   r   r   r<   –  r=   z*TestErrorInIterator.test.<locals>.<lambda>r¢   )	r   r   rB   r!   r   r\   r¶   r3   r   r   r   r   r   r¬   ”  s    zTestErrorInIterator.testc                    s4   ˆ   d¡ˆ _‡ fdd„}ˆ  tj|¡ t d¡ d S )Nr   c                      s   t ˆ j dd„ tƒ ¡ƒS )Nc                 S   s   d S r
   r   rW   r   r   r   r<     r=   zGTestErrorInIterator.test_unordered.<locals>.unordered.<locals>.<lambda>)r[   r   r   r¶   r   r   r   r   Ú	unorderedœ  s    z5TestErrorInIterator.test_unordered.<locals>.unorderedr¢   )r   r   rB   r!   r   r3   r   )r   r¸   r   r   r   Útest_unordered™  s    z"TestErrorInIterator.test_unorderedN)r'   r(   r)   rH   r¬   r¹   r   r   r   r   r·     s   r·   c                   @   s"   e Zd Zdd„ Zejdd„ ƒZdS )ÚTestMaxsizec                 C   sv   |   d¡| _g }t | jj|jd¡ t d| jj|jd¡ t d¡ |  |g ¡ d| j_t d¡ |  	|ddg¡ d S )Nr   r,   r.   r+   g{®Gáz”?)
r   r   r3   r/   r0   Zspawn_laterr   r2   r   r5   )r   Údoner   r   r   Útest_inc¥  s    

zTestMaxsize.test_incc                 C   sn   |   d¡ }| _| td¡ | td¡ | td¡ t d¡ |  |jd¡ d|_t d¡ |  |jd¡ d S )Nr   rU   r{   g333333Ó?r+   r   )	r   r   r/   r   r3   ÚassertGreaterEqualr   r   r5   r«   r   r   r   Útest_setzero³  s    

zTestMaxsize.test_setzeroN)r'   r(   r)   r¼   r!   rc   r¾   r   r   r   r   rº   £  s   rº   c                   @   s   e Zd Ze ¡ dd„ ƒZdS )ÚTestSizec                 C   sê   | j ddd }| _|  |jd¡ d|_|  |jd¡ d|_|  |jd¡ d|_|  |jd¡ |  t¡ d|_W d   ƒ n1 s„0    Y  |  t¡ d|_W d   ƒ n1 s´0    Y  d|_|  |jd¡ d|_|  |jd¡ d S )Nr+   F©r   r   r,   rD   r   )r   r   r2   r   rB   rF   r«   r   r   r   r¬   Â  s     $$zTestSize.testN)r'   r(   r)   r!   Zreraises_flaky_race_conditionr¬   r   r   r   r   r¿   À  s   r¿   c                   @   s   e Zd Zdd„ ZdS )ÚTestRefc                 C   sÎ   |   d¡ }| _g }tƒ }||_|j}~tƒ Œ | |tƒ fdtƒ i¡}|  |t¡ t	 
d¡ | t |¡¡ ~~tr†t ¡  t ¡  |D ]}|  |ƒ ¡ qŠ|  dt|ƒ¡ W d   ƒ n1 sÀ0    Y  d S )Nr+   Úkwarg1rU   r-   )r   r   Ú	SomeClassÚrefsrJ   r   r>   ÚObjectrk   r3   r   r0   ÚweakrefÚrefr	   r   r$   ÚassertIsNoner2   r²   )r   r   rÄ   ÚobjrJ   r?   r7   r   r   r   r¬   Û  s$    
zTestRef.testN©r'   r(   r)   r¬   r   r   r   r   rÁ   Ù  s   rÁ   c                   @   s   e Zd ZdS )rÅ   N©r'   r(   r)   r   r   r   r   rÅ   ÷  s   rÅ   c                   @   s   e Zd ZdZddd„ZdS )rÃ   Nc                 C   s&   t ƒ }| j dd„ |||fD ƒ¡ |S )Nc                 S   s   g | ]}t  |¡‘qS r   )rÆ   rÇ   r   r   r   r   r     r=   z"SomeClass.func.<locals>.<listcomp>)rÅ   rÄ   Úextend)r   Zarg1rÂ   r?   r   r   r   rJ   ÿ  s    zSomeClass.func)N)r'   r(   r)   rÄ   rJ   r   r   r   r   rÃ   û  s   rÃ   c                   C   s   d S r
   r   r   r   r   r   Únoop  s    rÍ   c                   @   s   e Zd Zdd„ ZdS )ÚTestRefCountc                 C   s*   |   d¡}| t¡ t d¡ | ¡  d S )Nr,   r   )r   r/   rÍ   r3   r   r   r«   r   r   r   r¬     s    


zTestRefCount.testNrÊ   r   r   r   r   rÎ   	  s   rÎ   )ÚmonkeyÚThreadPoolExecutorzRequires ThreadPoolExecutorc                   @   sr   e Zd ZdZdZedd„ ƒZdZedd„ ƒZedd	„ ƒZ	ed
d„ ƒZ
ejdd„ ƒZejdd„ ƒZejdd„ ƒZdS )ÚTestTPEr,   Tc                 C   s   t jjS r
   )r3   Ú
threadpoolrÐ   r   r   r   r   r     s    zTestTPE.ClassUnderTestFc                 C   s   ddl m} |S )Nr   )ÚTimeoutError)Úconcurrent.futuresrÓ   )r   ÚFutureTimeoutErrorr   r   r   rÕ   #  s    zTestTPE.FutureTimeoutErrorc                 C   s   ddl m} |S )Nr   )rS   )rÔ   rS   )r   Úcf_waitr   r   r   rÖ   (  s    zTestTPE.cf_waitc                 C   s   ddl m} |S )Nr   )Úas_completed)rÔ   r×   )r   Úcf_as_completedr   r   r   rØ   -  s    zTestTPE.cf_as_completedc           	         s`  |   t d¡| j¡ | j}g ‰ dd„ }dd„ }| |¡}d|_| |¡ | j| j	|j
dd d	d
„ }t |¡}|   | 
¡ d¡ |  | ¡ ¡ |  | ¡ ¡ t ¡  |   |jd¡ |  | ¡ ¡ |   |jd¡ | ‡ fdd„¡ |   ˆ dg¡ |  |f¡\}}|   t|ƒ|g¡ |   t|  |f¡ƒ|g¡ |   |jd¡ t ¡  |   |jd¡ | ¡  ~~| `d S )NÚ	threadingc                   S   s   t  d¡ dS ©Nr•   é*   r¤   r   r   r   r   Úfn:  s    
zTestTPE.test_future.<locals>.fnc                 S   s   |  j d7  _ t d¡‚d S )Nr,   zExpected, ignored)Ú
calledbackr!   r   )Úfuturer   r   r   rx   >  s    z%TestTPE.test_future.<locals>.callbackr   r¢   r|   c                   S   s   dS ©Néà  r   r   r   r   r   ÚspawnedG  s    z$TestTPE.test_future.<locals>.spawnedrÛ   r,   rà   c                    s
   ˆ   d¡S )NT©r0   )r]   ©rÝ   r   r   r<   [  r=   z%TestTPE.test_future.<locals>.<lambda>T)r2   rÏ   Úis_module_patchedÚMONKEY_PATCHEDr   ÚsubmitrÝ   Úadd_done_callbackrB   rÕ   r?   r3   r/   Ú
assertTruer»   ZassertFalseÚ	cancelledr   ÚreadyÚvaluerÖ   r[   rØ   r   )	r   r   rÜ   rx   rÞ   rá   Úspawned_greenletr»   Ú	_not_doner   rã   r   Útest_future2  s@    ÿ


zTestTPE.test_futurec           
      C   sÐ   |   t d¡| j¡ | j}dd„ }| |¡}| jrª| j|fdd\}}|   t|ƒ|g¡ dd„ }t 	|¡}|  |f¡\}}	|   t|ƒ|g¡ |  
| ¡ ¡ |   |jd¡ n|  t| j|f¡ | ¡  ~~| `d S )	NrÙ   c                   S   s   t  d¡ dS rÚ   r¤   r   r   r   r   rÜ   v  s    
z4TestTPE.test_future_wait_module_function.<locals>.fnr¢   r|   c                   S   s   dS rß   r   r   r   r   r   rá   €  s    z9TestTPE.test_future_wait_module_function.<locals>.spawnedrà   )r2   rÏ   rä   rå   r   ræ   rÖ   r[   r3   r/   rè   rê   rë   rB   ÚAttributeErrorr   )
r   r   rÜ   rÞ   Ú_doneÚnot_donerá   rì   r»   rí   r   r   r   Ú test_future_wait_module_functionn  s(    ÿ

z(TestTPE.test_future_wait_module_functionc                 C   sŒ   |   t d¡| j¡ | j}dd„ }| |¡}dd„ }t |¡}t |f¡}|   t	|ƒ|g¡ |  
| ¡ ¡ |   |jd¡ | ¡  ~~| `d S )NrÙ   c                   S   s   t  d¡ dS rÚ   r¤   r   r   r   r   rÜ   ™  s    
z4TestTPE.test_future_wait_gevent_function.<locals>.fnc                   S   s   dS rß   r   r   r   r   r   rá   Ÿ  s    z9TestTPE.test_future_wait_gevent_function.<locals>.spawnedrà   )r2   rÏ   rä   rå   r   ræ   r3   r/   rS   r[   rè   rê   rë   r   )r   r   rÜ   rÞ   rá   rì   r»   r   r   r   Ú test_future_wait_gevent_function’  s     ÿ

z(TestTPE.test_future_wait_gevent_functionN)r'   r(   r)   r   ra   Úpropertyr   rå   rÕ   rÖ   rØ   r!   rc   rî   rò   ró   r   r   r   r   rÑ     s"   




;
#rÑ   c                   @   s   e Zd Zdd„ ZdS )ÚTestThreadResultc                    sÀ   g ‰G dd„ dt ƒ‰ ‡ ‡fdd„}tj d t ¡ |¡‰‡‡fdd„}t |¡ ¡  |  ˆ ¡( tdƒD ]}t 	d¡ qfW d   ƒ n1 sŠ0    Y  |  
ˆd	d	g¡ |  ˆj¡ |  ˆj¡ d S )
Nc                   @   s   e Zd ZdS )zMTestThreadResult.test_exception_in_on_async_doesnt_crash.<locals>.MyExceptionNrË   r   r   r   r   ÚMyExceptionµ  s   rö   c                      s   ˆ  d¡ ˆ ‚d S rª   râ   r   )rö   Úcalledr   r   Úbad_when_ready¸  s    
zPTestThreadResult.test_exception_in_on_async_doesnt_crash.<locals>.bad_when_readyc                      s   ˆ   d¡ ˆ d¡ d S )Nr,   rÛ   )r0   Úsetr   )r÷   Útrr   r   Úwake¾  s    
zFTestThreadResult.test_exception_in_on_async_doesnt_crash.<locals>.wakerp   r¢   r,   )Ú	Exceptionr3   rÒ   ZThreadResultZget_hubr/   r1   rB   r#   r   r2   rÈ   rë   ÚassertIsNotNoneZreceiver)r   rø   rû   r%   r   )rö   r÷   rú   r   Ú'test_exception_in_on_async_doesnt_crash±  s    *z8TestThreadResult.test_exception_in_on_async_doesnt_crashN)r'   r(   r)   rþ   r   r   r   r   rõ   ¯  s   rõ   c                       sX   e Zd ZdZdZ‡ fdd„Z‡ fdd„Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Z‡  ZS )ÚTestWorkerProfileAndTraceNc                    s*   t t| ƒ ¡  tj ¡ | _tj ¡ | _d S r
   )	Úsuperrÿ   rZ   r3   rÒ   Ú_get_thread_profileÚold_profileÚ_get_thread_traceÚ	old_tracer   ©Ú	__class__r   r   rZ   Ü  s    zTestWorkerProfileAndTrace.setUpc                    s2   dd l }| | j¡ | | j¡ tt| ƒ ¡  d S ©Nr   )rÙ   Ú
setprofiler  Úsettracer  r   rÿ   ÚtearDown©r   rÙ   r  r   r   r
  á  s    z"TestWorkerProfileAndTrace.tearDownc                 C   s(   dd l }| | ¡ |  tj ¡ | ¡ d S r  )rÙ   r  ÚassertIsr3   rÒ   r  r  r   r   r   Útest_get_profileç  s    
z*TestWorkerProfileAndTrace.test_get_profilec                 C   s(   dd l }| | ¡ |  tj ¡ | ¡ d S r  )rÙ   r	  r  r3   rÒ   r  r  r   r   r   Útest_get_traceì  s    
z(TestWorkerProfileAndTrace.test_get_tracec           
         sð   dd l }dd l}t|d| ƒ}t|d| ƒ‰dg‰‡fdd„}‡‡fdd„}g ‰g ‰ | ‰G ‡ ‡‡fdd	„d	tƒ}|| _| jd
dd}t||ƒs’J ‚||ƒ | |¡}	|  |	d¡ |  	ˆd d
¡ | 
¡  |  ˆ|d |g¡ |  ˆ ||d g¡ d S )Nr   rù   r1   c                     s   ˆ d  d7  < d S )Nr   r,   r   )Ú_args)r÷   r   r   rx   ù  s    zETestWorkerProfileAndTrace._test_func_called_in_task.<locals>.callbackc                      s   ˆ  ˆ ¡ dS )Né¥  )rý   r   )Úgetterr¬   r   r   Útaskü  s    
zATestWorkerProfileAndTrace._test_func_called_in_task.<locals>.taskc                       s&   e Zd ZG ‡ ‡‡fdd„dejƒZdS )zATestWorkerProfileAndTrace._test_func_called_in_task.<locals>.Poolc                       s(   e Zd Z‡‡fdd„Z‡ ‡fdd„ZdS )zQTestWorkerProfileAndTrace._test_func_called_in_task.<locals>.Pool._WorkerGreenletc                    s<   ˆ   |¡ ˆ   ˆƒ ¡ tjj| |g|¢R Ž  ˆ   ˆƒ ¡ d S r
   )r0   r   rg   Ú_before_run_task©r   rJ   rM   )Úbefore_taskr  r   r   r    s    
zbTestWorkerProfileAndTrace._test_func_called_in_task.<locals>.Pool._WorkerGreenlet._before_run_taskc                    s<   ˆ   |¡ ˆ   ˆƒ ¡ tjj| |g|¢R Ž  ˆ   ˆƒ ¡ d S r
   )r0   r   rg   Ú_after_run_taskr  )Ú
after_taskr  r   r   r    s    
zaTestWorkerProfileAndTrace._test_func_called_in_task.<locals>.Pool._WorkerGreenlet._after_run_taskN)r'   r(   r)   r  r  r   ©r  r  r  r   r   rg     s   rg   N)r'   r(   r)   r   rg   r   r  r   r   ÚPool  s   r  r,   TrÀ   r  )rÙ   Úsysr    r   r   r   Ú
isinstancer>   r2   r½   r   )
r   rJ   rÙ   r  Úsetterrx   r  r  r   rv   r   )r  r  r÷   r  r¬   r   Ú_test_func_called_in_taskñ  s*    

z3TestWorkerProfileAndTrace._test_func_called_in_taskc                 C   s   |   d¡ d S )NZprofile©r  r   r   r   r   Útest_profile_called_in_task0  s    z5TestWorkerProfileAndTrace.test_profile_called_in_taskc                 C   s   |   d¡ d S )NÚtracer  r   r   r   r   Útest_trace_called_in_task3  s    z3TestWorkerProfileAndTrace.test_trace_called_in_task)r'   r(   r)   r  r  rZ   r
  r  r  r  r  r!  Ú__classcell__r   r   r  r   rÿ   Ô  s   ?rÿ   Ú__main__)rQ   )<Ú
__future__r   r   r   Ú
contextlibrV   rÆ   r   rj   r3   r   Zgevent.exceptionsr   Zgevent.testingÚtestingr!   r   r	   Úcontextmanagerr   r   r*   ÚobjectrI   rT   rX   rs   r~   ZTIMEOUT3rY   r‚   rŠ   ZWINZRUN_COVERAGEr"   re   rž   rc   r¦   r¨   r©   r­   r¶   r·   rº   r¿   rÁ   rÅ   rÃ   rÍ   rÎ   rÏ   Z
skipUnlessÚhasattrrÒ   rÑ   rõ   rÿ   r'   Úmainr   r   r   r   Ú<module>   sj   

$0

 u%


þ %d
