o
    ‡eP1  ã                   @   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   Zexception_streamÚdead©Úself© r   úKD:\Projects\ConvertPro\env\Lib\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__Z_threadpoolZ	_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)Z_sr   r   r   Ú<lambda>7   s    z=_AbstractTestPeriodicMonitoringThread.setUp.<locals>.<lambda>ì   ï>[= c                    s   ˆ j }ˆ  j d7  _ |S ©Né   )Útid)Z_fZ_aÚrr   r   r   Ústart_new_thread9   s   zE_AbstractTestPeriodicMonitoringThread.setUp.<locals>.start_new_thread)Úsuperr#   ÚsetUpÚmonitorr,   Ú_orig_start_new_threadÚthread_sleepÚ_orig_thread_sleepr*   r   ÚhubÚPeriodicMonitoringThreadÚpmtZperiodic_monitoring_threadÚmonitoring_functionsZpmt_default_funcsÚlenÚlen_pmt_default_funcs)r   r,   ©Ú	__class__r   r   r.   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   )r0   r/   r,   r2   r1   r5   Ú_greenlet_tracerÚprevious_trace_functionÚkillr   r   r-   r#   ÚtearDown)r   Úprevr9   r   r   r>   E   s   

z._AbstractTestPeriodicMonitoringThread.tearDown)r   r   r    r.   r>   Ú__classcell__r   r   r9   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'   )ÚassertEqualr5   Úmonitor_thread_identr   r;   r   r   r   r   Útest_constructorR   s   z-TestPeriodicMonitoringThread.test_constructorzVerifies the processc                 C   s*   | j  ¡ }|  |¡ |  || j  ¡ ¡ d S r   )r5   Ú_get_processZ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   )	rF   r3   r5   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Ú
ValueErrorr5   Úadd_monitoring_functionrB   r8   r7   r6   Úperiod©r   rO   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   rN   )rB   r5   r6   rT   Zcalculate_sleep_timeZ_calculated_sleep_timeZ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   )r3   r$   r%   ©Z_hubr   r   r   rO   ›   s   z?TestPeriodicMonitoringThread.test_call_destroyed_hub.<locals>.fçš™™™™™¹?©r5   rS   rJ   rK   rU   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   )r3   r   r   r   rO   §   ó   
z:TestPeriodicMonitoringThread.test_call_dead_hub.<locals>.frY   rZ   rU   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   )Ú
SystemExitrX   r   r   r   rO   ¯   ó   z<TestPeriodicMonitoringThread.test_call_SystemExit.<locals>.frY   )r5   rS   rU   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    ra   c                    s   ˆ ƒ ‚r   r   rX   ©ra   r   r   rO   ¹   r_   z=TestPeriodicMonitoringThread.test_call_other_error.<locals>.frY   )Ú	Exceptionr5   rS   rQ   rU   r   rb   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   rN   r)   )	ÚosZ
gevent.hubr   r5   ÚpidrC   r3   rB   Úgetpid)r   re   r   Zold_tidr   r   r   Útest_hub_reinitÀ   s   
z,TestPeriodicMonitoringThread.test_hub_reinitN)r   r   r    rD   r   rH   rL   rV   rW   r[   r]   r`   rd   rh   r   r   r   r   rA   O   s    
	rA   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   rO   Õ   s   z:TestPeriodicMonitorBlocking.test_previous_trace.<locals>.fÚeventrk   )rn   rk   )r5   r=   rI   r   r   r/   r4   r3   rB   r;   rF   r<   rU   r   rl   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)   ÚswitchrP   r   é   )rB   r5   r;   Zgreenlet_switch_counterrI   Zactive_greenletÚobjectrF   )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)ÚIEventLoopBlockedrq   rN   )Zgevent.eventsrw   rx   rj   rJ   r5   Zmonitor_blockingr3   rs   r;   Ú
assertTruer   ZverifyObjectZ ignore_current_greenlet_blockingZ!monitor_current_greenlet_blockingr   )r   rw   rx   Úeventsrt   ru   r   r   r   Útest_monitor_blockingõ   s*   





z1TestPeriodicMonitorBlocking.test_monitor_blockingN)r   r   r    ro   rv   r{   r   r   r   r   ri   Í   s    ri   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     r\   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#   r.   ÚGEVENT_CONFIGÚmax_memory_usageÚ_old_maxr5   rE   r   r   r   r   r.   +  s   
zTestPeriodicMonitorMemory.setUpc                 C   s   | j t_t | ¡ d S r   )r‚   r€   r   r#   r>   r   r   r   r   r>   2  s   z"TestPeriodicMonitorMemory.tearDownc                 C   s:   |   | j ¡ ¡ | j ¡  |  | jd t| j ¡ ƒ¡ d S r(   )ry   r5   Úcan_monitor_memory_usageÚinstall_monitor_memory_usagerB   r8   r7   r6   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   rM   r   r   r   r   r   r   r&   A  r   zKTestPeriodicMonitorMemory.test_cannot_monitor_and_install.<locals>.<lambda>T)Úrecordr)   )Úwarningsr5   rE   rJ   rƒ   Úcatch_warningsr„   rB   r7   rF   r/   Z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 )NrN   )rB   r5   Ú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   )rz   rP   r)   rr   )Úgeventrz   r}   r€   r   r5   rŒ   ZassertIsInstanceZMemoryUsageThresholdExceededrB   Z	mem_usageZmax_allowedZmemory_infor|   rI   ZMemoryUsageUnderThresholdÚrepr)r   rz   rn   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   r5   rŒ   rI   )r   rn   r   r   r   Útest_monitor_initial_belowy  s   z4TestPeriodicMonitorMemory.test_monitor_initial_belowN)r   r   r    r}   r.   r>   r…   r‹   r   r   r’   r   r   r   r   r   %  s    	+r   Ú__main__)r$   ZunittestZgreenletr   r   Zgevent.monkeyr   Zgevent._compatr   r   Zgevent.testingr   Zgevent.testing.skippingr   rŽ   r	   r/   r
   r€   r   rs   r   r#   ZTestCaserA   ri   r|   r   r   Úmainr   r   r   r   Ú<module>   s8   
!ÿ~ÿO	ÿ[ÿ