o
    ÑtPfP1  ã                   @   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r‡e ¡  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   ú\/var/www/html/humari/django-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	ÿ[ÿ