o
    U˟iP1                     @   s  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mZ d dlm	Z	 d dl
mZ d dlmZ d d	lmZ d d
lmZ eedZG dd deZG dd deZG dd deejZG dd deejZG dd deZedG dd deejZedkre  dS dS )    N)gettrace)settrace)get_original)thread_mod_name)NativeStrIO)verify)skipWithoutPSUtil)_monitor)config	get_identc                   @   sD   e Zd ZdZdZdd Zdd ZeZdd Ze	dd	 Z
d
d ZdS )MockHubNc                 C   s   t  | _t | _d| _d S )NF)r   thread_identr   exception_streamdeadself r   c/var/www/apps/myagent/mysuperagent/venv/lib/python3.10/site-packages/gevent/tests/test___monitor.py__init__   s   
zMockHub.__init__c                 C   s   | j  S Nr   r   r   r   r   __bool__   s   zMockHub.__bool__c                  G   s    r   r   )r   argsr   r   r   handle_error$   s   zMockHub.handle_errorc                 C      | S r   r   r   r   r   r   loop'   s   zMockHub.loopc                 C      dS )zmock loop.reinitNr   r   r   r   r   reinit+       zMockHub.reinit)__name__
__module____qualname___threadpool	_resolverr   r   __nonzero__r   propertyr   r   r   r   r   r   r      s    
r   c                       s(   e Zd Z fddZ fddZ  ZS )%_AbstractTestPeriodicMonitoringThreadc                    s   t t   tj _tj _dd t_d _ fdd}|t_t	  _
t j
 _ j j
_ j d d   _t j _d S )Nc                 S   s   t  S r   )gccollect)_sr   r   r   <lambda>7   s    z=_AbstractTestPeriodicMonitoringThread.setUp.<locals>.<lambda>   >[= c                    s    j }  j d7  _ |S N   )tid)_f_arr   r   r   start_new_thread9   s   zE_AbstractTestPeriodicMonitoringThread.setUp.<locals>.start_new_thread)superr&   setUpmonitorr2   _orig_start_new_threadthread_sleep_orig_thread_sleepr.   r   hubPeriodicMonitoringThreadpmtperiodic_monitoring_threadmonitoring_functionspmt_default_funcslenlen_pmt_default_funcs)r   r2   	__class__r   r   r4   3   s   

z+_AbstractTestPeriodicMonitoringThread.setUpc                    sV   | j t_| jt_| jjj}| j  t	 |u sJ t	 |ft
d  tt|   d S r   )r6   r5   r2   r8   r7   r;   _greenlet_tracerprevious_trace_functionkillr   r   r3   r&   tearDown)r   prevrA   r   r   rF   E   s   

z._AbstractTestPeriodicMonitoringThread.tearDown)r   r    r!   r4   rF   __classcell__r   r   rA   r   r&   .   s    r&   c                   @   sd   e Zd Zdd Zed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 )TestPeriodicMonitoringThreadc                 C   s&   |  d| jj |  t | jj d S )Nr+   )assertEqualr;   monitor_thread_identr   rC   r   r   r   r   test_constructorR   s   z-TestPeriodicMonitoringThread.test_constructorzVerifies the processc                 C   s*   | j  }| | | || j   d S r   )r;   _get_processassertIsNotNoneassertIs)r   procr   r   r   test_get_processV   s   

z-TestPeriodicMonitoringThread.test_get_processc                 C   sJ   |  | j| jj | `t  | | jj | | jj | t  d S r   )	rO   r9   r;   r'   r(   assertIsNoneassertFalse
should_runr   r   r   r   r   test_hub_wref]   s   z*TestPeriodicMonitoringThread.test_hub_wrefc                 C   s   |  t| jjd d |  t| jjdd d dd }| j|d | | jd t| j  | d| j d j | j|d | | jd t| j  | d| j d j | j|d  | | jt| j  d S )Nr-   c                   S      d S r   r   r   r   r   r   r*   l   r   zKTestPeriodicMonitoringThread.test_add_monitoring_function.<locals>.<lambda>c                   S   r   )zDoes nothingNr   r   r   r   r   fn   r   zDTestPeriodicMonitoringThread.test_add_monitoring_function.<locals>.f   )	assertRaises
ValueErrorr;   add_monitoring_functionrJ   r@   r?   r=   periodr   rX   r   r   r   test_add_monitoring_functioni   s   z9TestPeriodicMonitoringThread.test_add_monitoring_functionc                 C   s   |  | j d j| j  d| j_|  | jj| j  d| j d _d| j_| j  |  | j d j| j  |  | j d j| jj d S )Nr   rW   )rJ   r;   r=   r]   calculate_sleep_time_calculated_sleep_timeinactive_sleep_timer   r   r   r   test_calculate_sleep_time   s(   
z6TestPeriodicMonitoringThread.test_calculate_sleep_timec                    s4    fdd} j |d       j j d S )Nc                    s   d } d  _ t  d S r   )r9   r'   r(   _hubr   r   r   rX      s   z?TestPeriodicMonitoringThread.test_call_destroyed_hub.<locals>.f皙?r;   r\   rS   rT   r^   r   r   r   test_call_destroyed_hub   s   z4TestPeriodicMonitoringThread.test_call_destroyed_hubc                 C   s0   dd }| j |d |    | | j j d S )Nc                 S   s
   d| _ d S )NTr   )r9   r   r   r   rX         
z:TestPeriodicMonitoringThread.test_call_dead_hub.<locals>.frf   rg   r^   r   r   r   test_call_dead_hub   s   z/TestPeriodicMonitoringThread.test_call_dead_hubc                 C   s"   dd }| j |d |    d S )Nc                 S   s   t  r   )
SystemExitrd   r   r   r   rX         z<TestPeriodicMonitoringThread.test_call_SystemExit.<locals>.frf   )r;   r\   r^   r   r   r   test_call_SystemExit   s   z1TestPeriodicMonitoringThread.test_call_SystemExitc                    sb   G dd dt   fdd}| j|d |   |   W d    d S 1 s*w   Y  d S )Nc                   @   s   e Zd ZdS )zGTestPeriodicMonitoringThread.test_call_other_error.<locals>.MyExceptionN)r   r    r!   r   r   r   r   MyException   s    rn   c                    s     r   r   rd   rn   r   r   rX      rl   z=TestPeriodicMonitoringThread.test_call_other_error.<locals>.frf   )	Exceptionr;   r\   rZ   r^   r   ro   r   test_call_other_error   s   
"z2TestPeriodicMonitoringThread.test_call_other_errorc                 C   sZ   dd l }ddlm} d| j_| jj}|| j | | | jj | |d | jj d S )Nr   )r   rW   r-   )	os
gevent.hubr   r;   pidrK   r9   rJ   getpid)r   rr   r   old_tidr   r   r   test_hub_reinit   s   
z,TestPeriodicMonitoringThread.test_hub_reinitN)r   r    r!   rL   r   rQ   rU   r_   rc   rh   rj   rm   rq   rw   r   r   r   r   rI   O   s    
	rI   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestPeriodicMonitorBlockingc                    s   | j   | t  g   fdd}t| t| j| _ | t | j j	 | 
| j j	j| | j 	dd | dg  d S )Nc                     s     |  d S r   )appendr   calledr   r   rX      s   z:TestPeriodicMonitorBlocking.test_previous_trace.<locals>.feventrz   )r}   rz   )r;   rE   rR   r   r   r5   r:   r9   rJ   rC   rO   rD   r^   r   r{   r   test_previous_trace   s   
z/TestPeriodicMonitorBlocking.test_previous_tracec                 C   s   |  d| jjj | jdd  |  d| jjj | | jjj t }t }| jd||f |  d| jjj | || jjj | jdd |  d| jjj | | jjj d S )Nr   unknownr-   switchrY   r      )rJ   r;   rC   greenlet_switch_counterrR   active_greenletobjectrO   )r   origintargetr   r   r   test__greenlet_tracer   s   z1TestPeriodicMonitorBlocking.test__greenlet_tracerc                 C   s  ddl m} ddl m} g }||j | | j| j t }t }| j	d||f | | j| j | | | 
| j| j | 
| t||d  |d d = | j  | | j| j | | | j  | 
| j| j d| j_| 
| j| j d S )Nr   )subscribers)IEventLoopBlockedr   rW   )gevent.eventsr   r   ry   rS   r;   monitor_blockingr9   r   rC   
assertTruer   verifyObject ignore_current_greenlet_blocking!monitor_current_greenlet_blockingr   )r   r   r   eventsr   r   r   r   r   test_monitor_blocking   s*   





z1TestPeriodicMonitorBlocking.test_monitor_blockingN)r   r    r!   r~   r   r   r   r   r   r   rx      s    rx   c                   @   s   e Zd Zdd Zdd ZdS )MockProcessc                 C   s
   || _ d S r   )rss)r   r   r   r   r   r     ri   zMockProcess.__init__c                 C   r   r   r   r   r   r   r   memory_full_info!  s   zMockProcess.memory_full_infoN)r   r    r!   r   r   r   r   r   r   r     s    r   zAccessess memory infoc                   @   sH   e Zd 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S )TestPeriodicMonitorMemoryr   c                    s,   t   tj _d t_ fdd j_d S )Nc                      s
   t  jS r   )r   r   r   r   r   r   r*   0  s   
 z1TestPeriodicMonitorMemory.setUp.<locals>.<lambda>)r&   r4   GEVENT_CONFIGmax_memory_usage_old_maxr;   rM   r   r   r   r   r4   +  s   
zTestPeriodicMonitorMemory.setUpc                 C   s   | j t_t|  d S r   )r   r   r   r&   rF   r   r   r   r   rF   2  s   z"TestPeriodicMonitorMemory.tearDownc                 C   s:   |  | j  | j  | | jd t| j  d S r,   )r   r;   can_monitor_memory_usageinstall_monitor_memory_usagerJ   r@   r?   r=   r   r   r   r   test_can_monitor_and_install6  s   
 z6TestPeriodicMonitorMemory.test_can_monitor_and_installc                 C   s   dd l }dd | j_| | j  |jdd}| j  W d    n1 s(w   Y  | dt| | 	t
j|d j d S )Nr   c                   S   rV   r   r   r   r   r   r   r*   A  r   zKTestPeriodicMonitorMemory.test_cannot_monitor_and_install.<locals>.<lambda>T)recordr-   )warningsr;   rM   rS   r   catch_warningsr   rJ   r?   rO   r5   MonitorWarningcategory)r   r   wsr   r   r   test_cannot_monitor_and_install?  s   z9TestPeriodicMonitorMemory.test_cannot_monitor_and_installc                 C   s   |  d| jd  d S )NrW   )rJ   r;   monitor_memory_usager   r   r   r   test_monitor_no_allowedK  s   z1TestPeriodicMonitorMemory.test_monitor_no_allowedc                 C   s  ddl m} d| _dt_| jd }| ||j | 	d|j
 | 	d|j | |jt | jd }| | d| _| jd }| ||j | 	d|j
 d| _| jd }| ||j | 	d|j
 t| | jd }| | d| _| jd }| ||j | 	d|j
 d S )Nr   )r   rY   r-   r   )geventr   r   r   r   r;   r   assertIsInstanceMemoryUsageThresholdExceededrJ   	mem_usagemax_allowedmemory_infor   rR   MemoryUsageUnderThresholdrepr)r   r   r}   r   r   r   test_monitor_greaterN  s2   

z.TestPeriodicMonitorMemory.test_monitor_greaterc                 C   s&   d| _ dt_| jd }| | d S )Nr-   
   )r   r   r   r;   r   rR   )r   r}   r   r   r   test_monitor_initial_belowy  s   z4TestPeriodicMonitorMemory.test_monitor_initial_belowN)r   r    r!   r   r4   rF   r   r   r   r   r   r   r   r   r   r   %  s    	+r   __main__)r'   unittestgreenletr   r   gevent.monkeyr   gevent._compatr   r   gevent.testingr   gevent.testing.skippingr   r   r	   r5   r
   r   r   r   r   r&   TestCaserI   rx   r   r   r   mainr   r   r   r   <module>   s8   
!~O	[