
    <|hP1                        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e      Z G d de      Z G d deej.                        Z G d deej.                        Z G d de      Z ed       G d deej.                               Zedk(  r ej:                          yy)    N)gettrace)settrace)get_original)thread_mod_name)NativeStrIO)verify)skipWithoutPSUtil)_monitor)config	get_identc                   @    e Zd ZdZdZd Zd ZeZd Ze	d        Z
d Zy)MockHubNc                 N    t               | _        t               | _        d| _        y )NF)r   thread_identr   exception_streamdeadselfs    Z/var/www/html/test/engine/venv/lib/python3.12/site-packages/gevent/tests/test___monitor.py__init__zMockHub.__init__   s    %K +	    c                     | j                    S Nr   r   s    r   __bool__zMockHub.__bool__   s    99}r   c                       r    )r   argss     r   handle_errorzMockHub.handle_error$   s    r   c                     | S r   r   r   s    r   loopzMockHub.loop'   s    r   c                      y)zmock loop.reinitNr   r   s    r   reinitzMockHub.reinit+       r   )__name__
__module____qualname___threadpool	_resolverr   r   __nonzero__r   propertyr!   r#   r   r   r   r   r      s;    KI
 K  r   r   c                   (     e Zd Z fdZ fdZ xZS )%_AbstractTestPeriodicMonitoringThreadc                     t         t                   t        j                   _        t        j                   _        d t        _        d _         fd}|t        _        t                _
        t        j                   j                         _         j                   j                  _         j                  j                         d d   _        t!         j                         _        y )Nc                 *    t        j                         S r   )gccollect)_ss    r   <lambda>z=_AbstractTestPeriodicMonitoringThread.setUp.<locals>.<lambda>7   s    "**, r      >[= c                 J    j                   }xj                   dz  c_         |S N   )tid)_f_arr   s      r   start_new_threadzE_AbstractTestPeriodicMonitoringThread.setUp.<locals>.start_new_thread9   s    AHHMHHr   )superr-   setUpmonitorr<   _orig_start_new_threadthread_sleep_orig_thread_sleepr8   r   hubPeriodicMonitoringThreadpmtperiodic_monitoring_threadmonitoring_functionspmt_default_funcslenlen_pmt_default_funcs)r   r<   	__class__s   ` r   r>   z+_AbstractTestPeriodicMonitoringThread.setUp3   s    3T@B&-&>&>#")"6"66	
 $4 933DHH=.2hh+!%!>!>!@!C%()?)?%@"r   c                 B   | j                   t        _        | j                  t        _        | j
                  j                  j                  }| j
                  j                          t               |u sJ t               |f       t        d        t        t        | 7          y r   )r@   r?   r<   rB   rA   rE   _greenlet_tracerprevious_trace_functionkillr   r   r=   r-   tearDown)r   prevrK   s     r   rP   z._AbstractTestPeriodicMonitoringThread.tearDownE   sr    #'#>#> #66xx((@@zT!5HJ#55!3TCEr   )r%   r&   r'   r>   rP   __classcell__)rK   s   @r   r-   r-   .   s    
A$F Fr   r-   c                   ^    e Zd Zd Z ed      d        Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zy)TestPeriodicMonitoringThreadc                     | j                  d| j                  j                         | j                  t               | j                  j                         y )Nr4   )assertEqualrE   monitor_thread_identr   rM   r   s    r   test_constructorz-TestPeriodicMonitoringThread.test_constructorR   s8    TXX%B%BCTXX%>%>?r   zVerifies the processc                     | j                   j                         }| j                  |       | j                  || j                   j                                y r   )rE   _get_processassertIsNotNoneassertIs)r   procs     r   test_get_processz-TestPeriodicMonitoringThread.test_get_processV   s=    xx$$&T"dDHH1134r   c                 V   | j                  | j                  | j                  j                         | `t        j                          | j                  | j                  j                         | j                  | j                  j                         | j                  t                      y r   )	r\   rC   rE   r0   r1   assertIsNoneassertFalse
should_runr   r   s    r   test_hub_wrefz*TestPeriodicMonitoringThread.test_hub_wref]   sj    dhh-H


$((,,' 	,,-(*%r   c                    | j                  t        | j                  j                  d d       | j                  t        | j                  j                  d d       d }| j                  j                  |d       | j	                  | j
                  dz   t        | j                  j                                      | j	                  d| j                  j                         d   j                         | j                  j                  |d       | j	                  | j
                  dz   t        | j                  j                                      | j	                  d| j                  j                         d   j                         | j                  j                  |d        | j	                  | j
                  t        | j                  j                                      y )Nr7   c                       y r   r   r   r   r   r3   zKTestPeriodicMonitoringThread.test_add_monitoring_function.<locals>.<lambda>l   r$   r   c                       y)zDoes nothingNr   r   r   r   fzDTestPeriodicMonitoringThread.test_add_monitoring_function.<locals>.fn   r$   r      )	assertRaises
ValueErrorrE   add_monitoring_functionrV   rJ   rI   rG   periodr   rh   s     r   test_add_monitoring_functionz9TestPeriodicMonitoringThread.test_add_monitoring_functioni   sX   *dhh&F&FaP*dhh&F&FVXY	 	((A.33a7TXX=Z=Z=\9]^DHH99;A>EEF 	((A.33a7TXX=Z=Z=\9]^DHH99;A>EEF 	((D133S9V9V9X5YZr   c                    | j                  | j                  j                         d   j                  | j                  j	                                d| j                  _        | j                  | j                  j                  | j                  j	                                d| j                  j                         d   _        d| j                  _        | j                  j                          | j                  | j                  j                         d   j                  | j                  j	                                | j                  | j                  j                         d   j                  | j                  j
                         y )Nr   rf   )rV   rE   rG   rm   calculate_sleep_time_calculated_sleep_timeinactive_sleep_timer   s    r   test_calculate_sleep_timez6TestPeriodicMonitoringThread.test_calculate_sleep_time   s   HH))+A.55HH))+	- +,'HH((HH))+	
 57%%'*1*+'%%'HH))+A.55HH))+	- 	HH))+A.55HH++	-r   c                       fd} j                   j                  |d        j                           j                   j                   j                         y )Nc                 @    d } d _         t        j                          y r   )rC   r0   r1   )_hubr   s    r   rh   z?TestPeriodicMonitoringThread.test_call_destroyed_hub.<locals>.f   s    DDHJJLr   皙?rE   rl   ra   rb   rn   s   ` r   test_call_destroyed_hubz4TestPeriodicMonitoringThread.test_call_destroyed_hub   s?    	
 	((C0
,,-r   c                     d }| j                   j                  |d       | j                          | j                  | j                   j                         y )Nc                     d| _         y )NTr   )rC   s    r   rh   z:TestPeriodicMonitoringThread.test_call_dead_hub.<locals>.f   s	    CHr   rx   ry   rn   s     r   test_call_dead_hubz/TestPeriodicMonitoringThread.test_call_dead_hub   s<    	((C0
,,-r   c                 b    d }| j                   j                  |d       | j                          y )Nc                     t               r   )
SystemExit)rw   s    r   rh   z<TestPeriodicMonitoringThread.test_call_SystemExit.<locals>.f   s
    ,r   rx   )rE   rl   rn   s     r   test_call_SystemExitz1TestPeriodicMonitoringThread.test_call_SystemExit   s&    	 	((C0
r   c                      G d dt               fd}| j                  j                  |d       | j                        5  | j                          d d d        y # 1 sw Y   y xY w)Nc                       e Zd Zy)GTestPeriodicMonitoringThread.test_call_other_error.<locals>.MyExceptionN)r%   r&   r'   r   r   r   MyExceptionr      s    r   r   c                             r   r   )rw   r   s    r   rh   z=TestPeriodicMonitoringThread.test_call_other_error.<locals>.f   s    -r   rx   )	ExceptionrE   rl   rj   )r   rh   r   s     @r   test_call_other_errorz2TestPeriodicMonitoringThread.test_call_other_error   sV    	) 		  	((C0{+ 	HHJ	 	 	s   AA&c                 D   dd l }ddlm} d| j                  _        | j                  j
                  } || j                         | j                  |j                         | j                  j                         | j                  |dz   | j                  j
                         y )Nr   )r#   rf   r7   )	os
gevent.hubr#   rE   pidrW   rC   rV   getpid)r   r   r#   old_tids       r   test_hub_reinitz,TestPeriodicMonitoringThread.test_hub_reinit   sj    %((//txxdhhll31dhh&C&CDr   N)r%   r&   r'   rX   r	   r^   rc   ro   rt   rz   r}   r   r   r   r   r   r   rT   rT   O   sL    @ -.5 /5	&[,-2
..		Er   rT   c                       e Zd Zd Zd Zd Zy)TestPeriodicMonitorBlockingc                    | j                   j                          | j                  t                      g fd}t	        |       t        j                  | j                        | _         | j                  t               | j                   j                         | j                  | j                   j                  j                  |       | j                   j                  dd       | j                  dg       y )Nc                  (    j                  |        y r   )append)r   calleds    r   rh   z:TestPeriodicMonitorBlocking.test_previous_trace.<locals>.f   s    MM$r   eventr   )r   r   )rE   rO   r`   r   r   r?   rD   rC   rV   rM   r\   rN   )r   rh   r   s     @r   test_previous_tracez/TestPeriodicMonitorBlocking.test_previous_trace   s    (*%	  	33DHH=TXX%>%>?dhh//GGK!!'95./8r   c                 t   | j                  d| j                  j                  j                         | j                  j                  dd        | j                  d| j                  j                  j                         | j	                  | j                  j                  j
                         t               }t               }| j                  j                  d||f       | j                  d| j                  j                  j                         | j                  || j                  j                  j
                         | j                  j                  dd       | j                  d| j                  j                  j                         | j	                  | j                  j                  j
                         y )Nr   unknownr7   switchri   r      )rV   rE   rM   greenlet_switch_counterr`   active_greenletobjectr\   )r   origintargets      r   test__greenlet_tracerz1TestPeriodicMonitorBlocking.test__greenlet_tracer   s-   DHH55MMN!!)T2DHH55MMN$((33CCD!!(VV,<=DHH55MMNfdhh77GGH 	!!)R0DHH55MMN$((33CCDr   c                 V   ddl m} ddl m} g }|j                  |j                         | j	                  | j
                  j                  | j                               t               }t               }| j
                  j                  d||f       | j	                  | j
                  j                  | j                               | j	                  |       | j                  | j
                  j                  | j                               | j                  |       t        j                  ||d          |d d = | j
                  j                          | j	                  | j
                  j                  | j                               | j	                  |       | j
                  j                          | j                  | j
                  j                  | j                               d| j                  _        | j                  | j
                  j                  | j                               y )Nr   )subscribers)IEventLoopBlockedr   rf   )gevent.eventsr   r   r   ra   rE   monitor_blockingrC   r   rM   
assertTruer   verifyObject ignore_current_greenlet_blocking!monitor_current_greenlet_blockingr   )r   r   r   eventsr   r   s         r   test_monitor_blockingz1TestPeriodicMonitorBlocking.test_monitor_blocking   s    	.36==)22488<= !!(VV,<= 	22488<=  	11$((;<-vay91I 	11322488<=  	22411$((;< !#11$((;<r   N)r%   r&   r'   r   r   r   r   r   r   r   r      s    9$E&$=r   r   c                       e Zd Zd Zd Zy)MockProcessc                     || _         y r   )rss)r   r   s     r   r   zMockProcess.__init__  s	    r   c                     | S r   r   r   s    r   memory_full_infozMockProcess.memory_full_info!  s    r   N)r%   r&   r'   r   r   r   r   r   r   r     s    r   r   zAccessess memory infoc                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
TestPeriodicMonitorMemoryr   c                      t         j                          t        j                   _        d t        _         fd j
                  _        y )Nc                  .    t         j                        S r   )r   r   r   s   r   r3   z1TestPeriodicMonitorMemory.setUp.<locals>.<lambda>0  s    DHH(= r   )r-   r>   GEVENT_CONFIGmax_memory_usage_old_maxrE   rZ   r   s   `r   r>   zTestPeriodicMonitorMemory.setUp+  s2    -33D9%66)-& =r   c                 X    | j                   t        _        t        j	                  |        y r   )r   r   r   r-   rP   r   s    r   rP   z"TestPeriodicMonitorMemory.tearDown2  s    )-&-66t<r   c                 
   | j                  | j                  j                                | j                  j                          | j	                  | j
                  dz   t        | j                  j                                      y r6   )r   rE   can_monitor_memory_usageinstall_monitor_memory_usagerV   rJ   rI   rG   r   s    r   test_can_monitor_and_installz6TestPeriodicMonitorMemory.test_can_monitor_and_install6  s\     	99;< 	--/33a7TXX=Z=Z=\9]^r   c                    dd l }d | j                  _        | j                  | j                  j	                                |j                  d      5 }| j                  j                          d d d        | j                  dt                     | j                  t        j                  |d   j                         y # 1 sw Y   RxY w)Nr   c                       y r   r   r   r   r   r3   zKTestPeriodicMonitorMemory.test_cannot_monitor_and_install.<locals>.<lambda>A  r$   r   T)recordr7   )warningsrE   rZ   ra   r   catch_warningsr   rV   rI   r\   r?   MonitorWarningcategory)r   r   wss      r   test_cannot_monitor_and_installz9TestPeriodicMonitorMemory.test_cannot_monitor_and_install?  s     ,::<= $$D$1 	4RHH113	4 	CG$g,,benn=		4 	4s   B>>Cc                 Z    | j                  d| j                  j                  d              y )Nrf   )rV   rE   monitor_memory_usager   s    r   test_monitor_no_allowedz1TestPeriodicMonitorMemory.test_monitor_no_allowedK  s!    TXX::4@Ar   c                 4   ddl m} d| _        dt        _        | j
                  j                  d       }| j                  ||j                         | j                  d|j                         | j                  d|j                         | j                  |j                  t               | j
                  j                  d       }| j                  |       d| _        | j
                  j                  d       }| j                  ||j                         | j                  d|j                         d| _        | j
                  j                  d       }| j                  ||j                         | j                  d|j                         t!        |       | j
                  j                  d       }| j                  |       d| _        | j
                  j                  d       }| j                  ||j                         | j                  d|j                         y )Nr   )r   ri   r7   r   )geventr   r   r   r   rE   r   assertIsInstanceMemoryUsageThresholdExceededrV   	mem_usagemax_allowedmemory_infor   r`   MemoryUsageUnderThresholdrepr)r   r   r   s      r   test_monitor_greaterz.TestPeriodicMonitorMemory.test_monitor_greaterN  s   !)*& --d3eV%H%HIEOO,E--.e//= --d3%  --d3eV%H%HIEOO, --d3eV%E%EFEOO, 	U --d3%  --d3eV%H%HIEOO,r   c                     d| _         dt        _        | j                  j	                  d       }| j                  |       y )Nr7   
   )r   r   r   rE   r   r`   )r   r   s     r   test_monitor_initial_belowz4TestPeriodicMonitorMemory.test_monitor_initial_belowy  s5    )+& --d3% r   N)r%   r&   r'   r   r>   rP   r   r   r   r   r   r   r   r   r   r   %  s/     C>=_
>B(-V!r   r   __main__)r0   unittestgreenletr   r   gevent.monkeyr   gevent._compatr   r   gevent.testingr   gevent.testing.skippingr	   r   r
   r?   r   r   r   r   r   r-   TestCaserT   r   r   r   r%   mainr   r   r   <module>r      s    
    & * & ! 5 & *+6	f 0FF FBzE#H#+#4#4zE|L="G"*"3"3L=^&  *+Y! E ( 1 1Y! ,Y!v zHMMO r   