B
    ÐH/\á+  ã               @   s  d dl Z d dlZd dlZd dlmZ d dlZd dlZd dlmZ d dl	m
Z
mZ d dlmZ dZG dd„ dejƒ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e d¡G dd„ dejƒƒZG dd„ dejƒZedkre ¡  dS )é    N)Úsocket)ÚWaiterÚget_hub)ÚNativeStrIOgš™™™™™¹?c               @   s   e Zd Zdd„ ZdS )ÚTestCloseSocketWhilePollingc          	   C   sl   t   ¡ }|  |¡ tƒ j d¡}| |j¡ |  t j¡  z| 	d¡ W d | ¡  X W d Q R X t
 d¡ d S )Nr   )z
python.orgéQ   )r   Z_close_on_teardownr   ÚloopÚtimerÚstartÚcloseÚassertRaisesÚerrorZconnectÚgeventÚsleep)ÚselfZsockÚt© r   ú5lib/python3.7/site-packages/gevent/tests/test__hub.pyÚtest'   s    
z TestCloseSocketWhilePolling.testN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   %   s   r   c               @   s   e Zd Zdd„ ZdS )ÚTestExceptionInMainloopc          	      sÂ   t   ¡ }t t¡ t   ¡ | }td }|  |t| t| ¡ t d¡‰ ‡ fdd„}tƒ j 	d¡X}| 
|¡ |  ¡  t   ¡ }t t¡ t   ¡ | }| jˆ d |  |t| t| ¡ W d Q R X d S )NgÍÌÌÌÌÌì?z'TestExceptionInMainloop.test_sleep/failc                  s   ˆ ‚d S )Nr   r   )r   r   r   ÚfailB   s    z0TestExceptionInMainloop.test_sleep.<locals>.failgü©ñÒMbP?)Úvalue)Útimer   r   ÚDELAYZassertTimeWithinRangeÚ	greentestZExpectedExceptionr   r   r	   r
   Zexpect_one_errorZassert_error)r   r
   ZdelayZdelay_ranger   r   r   )r   r   Ú
test_sleep7   s    



z"TestExceptionInMainloop.test_sleepN)r   r   r   r   r   r   r   r   r   5   s   r   c               @   s   e Zd Zdd„ Zdd„ ZdS )Ú	TestSleepc             C   s   t  |¡ d S )N)r   r   )r   Útimeoutr   r   r   ÚwaitU   s    zTestSleep.waitc             C   s   t  d¡ d S )Nr   )r   r   )r   r   r   r   Útest_simpleX   s    zTestSleep.test_simpleN)r   r   r   r!   r"   r   r   r   r   r   S   s   r   c                   s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚTestWaiterGetc                s   t t| ƒ ¡  tƒ | _d S )N)Úsuperr#   ÚsetUpr   Úwaiter)r   )Ú	__class__r   r   r%   ^   s    zTestWaiterGet.setUpc          	   C   s4   t ƒ j |¡}| | jjd ¡ | j ¡ S Q R X d S )N)r   r   r	   r
   r&   ÚswitchÚget)r   r    Zevtr   r   r   r!   b   s    zTestWaiterGet.wait)r   r   r   r%   r!   Ú__classcell__r   r   )r'   r   r#   \   s   r#   c               @   s   e Zd Zdd„ ZdS )Ú
TestWaiterc             C   sÂ   t ƒ }|  t|ƒd¡ | d¡ |  t|ƒd¡ |  | ¡ d¡ t ƒ }| t¡ t dt|ƒ¡slt	t|ƒƒ‚|  
t|j¡ t ƒ }t |j¡}d|_t d¡ t|ƒ}|  | d¡|¡ | ¡  d S )Nz<Waiter greenlet=None>é   z<Waiter greenlet=None value=25>z6^<Waiter greenlet=None exc_info=.*ZeroDivisionError.*$ZANamer   z!<Waiter greenlet=<Greenlet "AName)r   ÚassertEqualÚstrr(   r)   ÚthrowÚZeroDivisionErrorÚreÚmatchÚAssertionErrorr   r   ÚspawnÚnamer   Ú
assertTrueÚ
startswithÚkill)r   r&   ÚgZ
str_waiterr   r   r   r   j   s"    


zTestWaiter.testN)r   r   r   r   r   r   r   r   r+   h   s   r+   z
Racy on CIc                   s”   e Zd Zdd„ Z‡ fdd„Zdd„ Zdd„ Zd	d
„ Zddd„Ze	j
dd„ ƒZe	j
dd„ ƒZdd„ Ze	j
dd„ ƒZe	j
dd„ ƒZe	j
dd„ ƒZ‡  ZS )ÚTestPeriodicMonitoringThreadc             C   sJ   t ƒ }y|`W n tk
r"   Y nX |jd k	rF|j ¡  |j ¡  |`d S )N)r   Úexception_streamÚAttributeErrorZ_threadpoolÚ
threadpoolÚjoinr8   )r   Úhubr   r   r   Ú
_reset_hub„   s    


z'TestPeriodicMonitoringThread._reset_hubc                s:   t t| ƒ ¡  tjj| _dtj_d| _tƒ | _|  	¡  d S )NTr   )
r$   r:   r%   r   ÚconfigÚmonitor_threadÚmonitor_firedÚsetÚmonitored_hubsr@   )r   )r'   r   r   r%      s    
z"TestPeriodicMonitoringThread.setUpc             C   s>   t ƒ }| js"|jr"|j ¡  d |_| jtj_d | _|  ¡  d S )N)r   rB   Úperiodic_monitoring_threadr8   r   rA   rE   r@   )r   r?   r   r   r   ÚtearDown˜   s    

z%TestPeriodicMonitoringThread.tearDownc             C   s(   |  j d7  _ | jd k	r$| j |¡ d S )Né   )rC   rE   Úadd)r   r?   r   r   r   Ú_monitor¢   s    
z%TestPeriodicMonitoringThread._monitorc             C   s   |   dtjj¡ d S )Ngš™™™™™¹?)r-   r   rA   Zmax_blocking_time)r   r   r   r   Útest_config§   s    z(TestPeriodicMonitoringThread.test_configTc                sb   |   ˆj¡ ddlm} |ƒ ‰ ˆ  ¡  ‡ ‡‡fdd„}ˆ |d¡ ˆ  ¡  ˆ  ¡  ˆ |d ¡ d S )Nr   )Ú	Conditionc                s(   ˆ   ¡  ˆ  ¡  ˆ  ¡  ˆr$ˆ ¡  d S )N)ÚacquireZ	notifyAllÚreleaser8   )Z_hub)Úcondr8   Úmonitorr   r   Úmonitor_cond°   s
    zJTestPeriodicMonitoringThread._run_monitoring_threads.<locals>.monitor_condg{®Gáz„?)r6   Z
should_runZ	threadingrL   rM   Úadd_monitoring_functionr!   rN   )r   rP   r8   rL   rQ   r   )rO   r8   rP   r   Ú_run_monitoring_threadsª   s    	z4TestPeriodicMonitoringThread._run_monitoring_threadsc             C   s@   ddl m} tƒ }| ¡  |  |ƒ ¡ |j ¡  |  |ƒ ¡ d S )Nr   )Úgettrace)ZgreenletrT   r   Ú start_periodic_monitoring_threadÚassertIsNotNonerF   r8   ÚassertIsNone)r   rT   r?   r   r   r   Útest_kill_removes_trace¿   s    
z4TestPeriodicMonitoringThread.test_kill_removes_tracec             C   s,  t ƒ }tƒ  }|_| ¡ }|  |¡ |  dt| ¡ ƒ¡ | | j	d¡ |  dt| ¡ ƒ¡ |  | j	| ¡ d j
¡ |  d| ¡ d j¡ t |jj¡ |j|ks¤t‚zt d¡ |  |¡ W d | | j	d ¡ |  dt|jƒ¡ |j|ksìt‚| ¡  |`X |  | jd¡ | ¡ }|  d|¡ |  d|¡ d S )	Né   gš™™™™™¹?é   éÿÿÿÿg333333Ó?rH   zappears to be blockedÚPeriodicMonitoringThread)r   r   r;   rU   rV   r-   ÚlenZmonitoring_functionsrR   rJ   ZfunctionZperiodr   r   r   Zapprox_timer_resolutionr3   r   rS   Z_monitoring_functionsr8   ZassertGreaterEqualrC   ÚgetvalueÚassertIn)r   r?   ÚstreamrP   Údatar   r   r   Útest_blocking_this_threadÈ   s.    

z6TestPeriodicMonitoringThread.test_blocking_this_threadc             C   sf   t ƒ }|j}| t ¡}tƒ  }|_|  |j¡ | tjd¡ |  	|j¡ |j}| 
| jd¡ |||fS )N)g{®Gáz„?gš™™™™™¹?)r   r=   Zapplyr   r;   rW   rF   r   r   rV   rR   rJ   )r   r?   r=   Ú
worker_hubr`   Úworker_monitorr   r   r   Ú_prep_worker_threadé   s    
z0TestPeriodicMonitoringThread._prep_worker_threadc             C   sB   |   ¡ \}}}|  |¡ | ¡  |  || j¡ |  | ¡ d¡ d S )NÚ )re   rS   r8   r_   rE   r-   r^   )r   rc   r`   rd   r   r   r   Ú*test_blocking_threadpool_thread_task_queueú   s
    
zGTestPeriodicMonitoringThread.test_blocking_threadpool_thread_task_queuec             C   sd   t ƒ }|j}|  ¡ \}}}| tjd¡}|  |¡ | ¡  | ¡  |  	|| j
¡ |  | ¡ d¡ d S )Ng333333Ó?rf   )r   r=   re   r4   r   r   rS   r)   r8   r_   rE   r-   r^   )r   r?   r=   rc   r`   rd   Útaskr   r   r   Ú,test_blocking_threadpool_thread_one_greenlet  s    
zITestPeriodicMonitoringThread.test_blocking_threadpool_thread_one_greenletc             C   s|   t ƒ }|j}|  ¡ \}}}dd„ }| |¡}| j|dd | ¡  | ¡  |  || j¡ | 	¡ }|  d|¡ |  d|¡ d S )Nc              S   s   t  tjd¡} |  ¡  d S )Ngffffffæ?)r   r4   r   r   r>   )r9   r   r   r   rh   -  s    zYTestPeriodicMonitoringThread.test_blocking_threadpool_thread_multi_greenlet.<locals>.taskF)r8   zappears to be blockedr\   )
r   r=   re   r4   rS   r)   r8   r_   rE   r^   )r   r?   r=   rc   r`   rd   rh   ra   r   r   r   Ú.test_blocking_threadpool_thread_multi_greenlet$  s    
zKTestPeriodicMonitoringThread.test_blocking_threadpool_thread_multi_greenlet)T)r   r   r   r@   r%   rG   rJ   rK   rS   r   Zignores_leakcheckrX   rb   re   rg   ri   rj   r*   r   r   )r'   r   r:      s   

	!r:   c               @   s   e Zd Zdd„ ZdS )ÚTestLoopInterfacec             C   s0   ddl m} ddlm} tƒ j}| ||¡ d S )Nr   )Úverify)ÚILoop)Zzope.interfacerl   Zgevent._interfacesrm   r   r   ZverifyObject)r   rl   rm   r   r   r   r   Útest_implemensts_ILoopB  s    z(TestLoopInterface.test_implemensts_ILoopN)r   r   r   rn   r   r   r   r   rk   @  s   rk   Ú__main__)r1   r   ZunittestZgevent.testingZtestingr   Zgevent.testing.timingr   r   Z
gevent.hubr   r   Zgevent._compatr   r   ZTestCaser   r   ZtimingZAbstractGenericWaitTestCaser   r#   r+   ZskipOnCIr:   rk   r   Úmainr   r   r   r   Ú<module>   s(   	
 ?
