o
    h9                     @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddlZ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 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 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& 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- G d%d& d&eZ.G d'd( d(eZ/G d)d* d*eZ0G d+d, d,eZ1G d-d. d.eZ2e'G d/d0 d0eZ3G d1d2 d2eZ4G d3d4 d4eZ5e6d5krIdd6l7m8Z8 e8e9 dS dS )7z3
Tests for testing utils (psutil.tests namespace).
    N)FREEBSD)NETBSD)POSIX)open_binary)	open_text)supports_ipv6)
CI_TESTING)COVERAGE)HAS_CONNECTIONS_UNIX)
PYTHON_EXE)PYTHON_EXE_ENV)PsutilTestCase)TestMemoryLeak)bind_socket)bind_unix_socket)
call_until)chdir)create_sockets)get_free_port)is_namedtuple)mock)process_namespace)reap_children)retry)retry_on_failure)
safe_mkdir)safe_rmpath)	serialrun)system_namespace)tcp_socketpair)	terminate)unix_socketpair)wait_for_file)wait_for_pidc                   @   sx   e Zd Zeddd Zeddd Zeddd Zeddd	 Zedd
d Z	eddd Z
dS )TestRetryDecoratorz
time.sleepc                    sF   t ddd d fdd}ttd | | d | |jd d S )N      retriesintervalZlogfunc                          r    dd   sdS Nr&   r   pop queuer.   r/Users/merlin/projects/employee-monitoring-system/venv/lib/python3.10/site-packages/psutil/tests/test_testutils.pyfooF   
   z2TestRetryDecorator.test_retry_success.<locals>.foo   )r   listrangeassertEqual
call_countselfsleepr2   r.   r/   r1   test_retry_successB   s
   z%TestRetryDecorator.test_retry_successc                    sD   t ddd d fdd}ttd | t| | |jd d S )Nr%   r&   r'   c                      r*   r+   r,   r.   r/   r.   r1   r2   T   r3   z2TestRetryDecorator.test_retry_failure.<locals>.foo   )r   r5   r6   assertRaisesZeroDivisionErrorr7   r8   r9   r.   r/   r1   test_retry_failureQ   s
   z%TestRetryDecorator.test_retry_failurec                 C   s2   t tdddd }| t| | |jd d S )Nr&   )	exceptionr)   c                   S   s   t N)	TypeErrorr.   r.   r.   r1   r2   a   s   z2TestRetryDecorator.test_exception_arg.<locals>.foor   )r   
ValueErrorr>   rC   r7   r8   r9   r.   r.   r1   test_exception_arg_   s   

z%TestRetryDecorator.test_exception_argc                 C   s4   t dd d ddd }| t| | |jd d S )Nr%   r'   c                   S      dd  d S r+   r.   r.   r.   r.   r1   r2   l      z4TestRetryDecorator.test_no_interval_arg.<locals>.foor   r   r>   r?   r7   r8   r9   r.   r.   r1   test_no_interval_argh   s   
z'TestRetryDecorator.test_no_interval_argc                 C   s4   t ddd ddd }| t| | |jd d S )Nr%   r&   r'   c                   S   rF   r+   r.   r.   r.   r.   r1   r2   v   rG   z0TestRetryDecorator.test_retries_arg.<locals>.foorH   r9   r.   r.   r1   test_retries_args   s   
z#TestRetryDecorator.test_retries_argc                 C   s   | j ttddd d S )Nr%   r&   )r(   timeout)r>   rD   r   )r:   r;   r.   r.   r1   test_retries_and_timeout_args}   s   z0TestRetryDecorator.test_retries_and_timeout_argsN)__name__
__module____qualname__r   patchr<   r@   rE   rI   rJ   rL   r.   r.   r.   r1   r$   @   s    





	r$   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )TestSyncTestUtilsc                 C   sf   t t  tt d }tjdtdgd | 	tj
t | W d    d S 1 s,w   Y  d S )Ni psutil.tests.retry.__iter__r   return_value)r#   osgetpidmaxpsutilZpidsr   rP   iterr>   ZNoSuchProcess)r:   Znopidr.   r.   r1   test_wait_for_pid   s
   "z#TestSyncTestUtils.test_wait_for_pidc                 C   sX   |   }t|d}|d W d    n1 sw   Y  t| tj|r*J d S )Nwr2   
get_testfnopenwriter"   rU   pathexistsr:   testfnfr.   r.   r1   test_wait_for_file   s   z$TestSyncTestUtils.test_wait_for_filec                 C   sR   |   }t|d W d    n1 sw   Y  t|dd tj|r'J d S )Nr[   T)empty)r]   r^   r"   rU   r`   ra   r:   rc   r.   r.   r1   test_wait_for_file_empty   s   z*TestSyncTestUtils.test_wait_for_file_emptyc                 C   sP   |   }tjdtdgd | tt| W d    d S 1 s!w   Y  d S )NrR   r   rS   )r]   r   rP   rY   r>   IOErrorr"   rg   r.   r.   r1   test_wait_for_file_no_file   s   "z,TestSyncTestUtils.test_wait_for_file_no_filec                 C   s\   |   }t|d}|d W d    n1 sw   Y  t|dd tj|s,J d S )Nr[   r2   F)deleter\   rb   r.   r.   r1   test_wait_for_file_no_delete   s   z.TestSyncTestUtils.test_wait_for_file_no_deletec                 C   s   t dd d}| |d d S )Nc                   S      dS )Nr&   r.   r.   r.   r.   r1   <lambda>       z3TestSyncTestUtils.test_call_until.<locals>.<lambda>zret == 1r&   )r   r7   )r:   retr.   r.   r1   test_call_until   s   z!TestSyncTestUtils.test_call_untilN)	rM   rN   rO   rZ   re   rh   rj   rl   rq   r.   r.   r.   r1   rQ      s    rQ   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestFSTestUtilsc                 C   <   t t}| |jd W d    d S 1 sw   Y  d S )Nrt)r   __file__r7   moder:   rd   r.   r.   r1   test_open_text      
"zTestFSTestUtils.test_open_textc                 C   rs   )Nrb)r   ru   r7   rv   rw   r.   r.   r1   test_open_binary   ry   z TestFSTestUtils.test_open_binaryc                 C   s<   |   }t| tj|sJ t| tj|sJ d S rB   )r]   r   rU   r`   isdirrg   r.   r.   r1   test_safe_mkdir   s
   zTestFSTestUtils.test_safe_mkdirc              	   C   s   |   }t|d  t| tj|rJ t| t| t| tj|r,J tj	dt
tjdd'}| t
 t| W d    n1 sLw   Y  |jsVJ W d    d S 1 saw   Y  d S )Nr[   zpsutil.tests.os.stat )Zside_effect)r]   r^   closer   rU   r`   ra   mkdirr   rP   OSErrorerrnoEINVALr>   called)r:   rc   mr.   r.   r1   test_safe_rmpath   s"   


"z TestFSTestUtils.test_safe_rmpathc                 C   sp   |   }t }t| t| | t tj|| W d    n1 s)w   Y  | t | d S rB   )r]   rU   getcwdr   r   r7   r`   join)r:   rc   baser.   r.   r1   
test_chdir   s   

zTestFSTestUtils.test_chdirN)rM   rN   rO   rx   r{   r}   r   r   r.   r.   r.   r1   rr      s    rr   c                   @   s:   e Zd Zdd Zdd Zee ddd Zdd	 Z	d
S )TestProcessUtilsc                 C   sN   |   }t|j}| sJ t  | rJ tjjrJ tjjr%J d S rB   )	spawn_testprocrX   Processpid
is_runningr   testsZ_pids_startedZ_subprocesses_started)r:   Zsubppr.   r.   r1   test_reap_children   s   z#TestProcessUtils.test_reap_childrenc                 C   s   |   \}}| |j|j | sJ | sJ t  }| ||g t jdd}| t|d | 	|| | 	|| | |
 t  | |
 |j t| | r`J | sfJ t| | rpJ d S )NT)	recursive   )Zspawn_children_pairassertNotEqualr   r   rX   r   childrenr7   lenZassertInppidrU   rV   r    )r:   childZ
grandchildr   r.   r.   r1   test_spawn_children_pair   s"   z)TestProcessUtils.test_spawn_children_pair
POSIX onlyc                 C   s"   |   \}}| | tj d S rB   )spawn_zombier7   statusrX   ZSTATUS_ZOMBIE)r:   parentzombier.   r.   r1   test_spawn_zombie   s   z"TestProcessUtils.test_spawn_zombiec                 C   s   |   }t| | | t| t|   j}t| | | t| tddg}tj|tj	tj	t
d}t| | | t| |   j}t| | | t| trq|  \}}t| t| | | | | d S d S )Nz-czimport time; time.sleep(60);)stdoutstderrenv)r   r    ZassertProcessGonerX   r   r   r   Popen
subprocessPIPEr   r   r   )r:   r   cmdr   r   r   r.   r.   r1   test_terminate   s4   






zTestProcessUtils.test_terminateN)
rM   rN   rO   r   r   unittestskipIfr   r   r   r.   r.   r.   r1   r      s    	
r   c                   @   s`   e Zd Zdd Zee ddd Zdd Zee dee	p"e
dd	d
 Zdd ZdS )TestNetUtilsc                 C   sT   t  }ttd|fd}| | d | W d    d S 1 s#w   Y  d S )Nr~   addrr&   )r   
contextlibclosingr   r7   getsockname)r:   portsr.   r.   r1   r     s   "zTestNetUtils.bind_socketr   c                 C   s   |   }t|}t|3 | |jtj | |jtj	 | |
 | tj|s.J tt|js9J W d    n1 sCw   Y  |   }t|tjd}t| | |jtj W d    d S 1 slw   Y  d S )N)type)r]   r   r   r   r7   familysocketAF_UNIXr   SOCK_STREAMr   rU   r`   ra   statS_ISSOCKst_mode
SOCK_DGRAM)r:   namesockr.   r.   r1   test_bind_unix_socket"  s   "z"TestNetUtils.test_bind_unix_socketc              	   C   s   dt  f}ttj|d\}}t|> t|  | | | | | | | 	| | W d    n1 s<w   Y  W d    d S W d    d S 1 sTw   Y  d S )Nz	127.0.0.1r   )
r   r   r   AF_INETr   r   r7   r   getpeernamer   )r:   r   serverclientr.   r.   r1   tcp_tcp_socketpair2  s   
"zTestNetUtils.tcp_tcp_socketpairz*/var/run/log UNIX socket opened by defaultc                 C   s   t  }| }|jddrJ |  }t|\}}zDtj|s#J t	
t	|js.J | | | d | t|jddd | | | | | | W |  |  d S |  |  w )Nunix)kindr   )rX   r   num_fdsconnectionsr]   r!   rU   r`   ra   r   r   r   r7   r   r   r   r   )r:   r   r   r   r   r   r.   r.   r1   test_unix_socketpair=  s    
z!TestNetUtils.test_unix_socketpairc                 C   s   t  c}tt}tt}|D ]}||j  d7  < ||tjtj  d7  < q| 	|tj
 d t r?| 	|tj d trLtrL| 	|tj d | 	|tj d | 	|tj d W d    d S 1 siw   Y  d S )Nr&   r   )r   collectionsdefaultdictintr   
getsockoptr   
SOL_SOCKETSO_TYPEZassertGreaterEqualr   r   AF_INET6r   r
   r   r   r   )r:   socksZfamstypesr   r.   r.   r1   test_create_socketsQ  s   

"z TestNetUtils.test_create_socketsN)rM   rN   rO   r   r   r   r   r   r   r   r   r   r   r.   r.   r.   r1   r     s    

r   c                   @   s`   e Zd Ze dd Zdd Ze eedee	ddd Z
d	d
 Zdd Zdd ZdS )TestMemLeakClassc                    s8    fdd}ddi | j |ddd |  d d d S )	Nc                      s    d  d7  < d S )Ncntr&   r.   r.   r   r.   r1   fung  s   z(TestMemLeakClass.test_times.<locals>.funr   r   
      )timeswarmup_times   )executer7   )r:   r   r.   r   r1   
test_timese  s   zTestMemLeakClass.test_timesc                 C   s|   | j t| jdd dd | j t| jdd dd | j t| jdd dd | j t| jd	d dd
 | j t| jdd dd d S )Nc                   S   rm   Nr   r.   r.   r.   r.   r1   rn   n  ro   z1TestMemLeakClass.test_param_err.<locals>.<lambda>r   r   c                   S   rm   r   r.   r.   r.   r.   r1   rn   o  ro   c                   S   rm   r   r.   r.   r.   r.   r1   rn   p  ro   )r   c                   S   rm   r   r.   r.   r.   r.   r1   rn   q  ro   )	tolerancec                   S   rm   r   r.   r.   r.   r.   r1   rn   r  ro   )r(   )r>   rD   r   r:   r.   r.   r1   test_param_errm  s
   zTestMemLeakClass.test_param_errzskipped on CIzskipped during test coveragec                 C   s4   g }|fdd}z| j td| j|dd W ~d S ~w )Nc                 S   s   |  dd  d S NZxxxxxxxxxxxxxxxxxxxxxxxxi   appendlsr.   r.   r1   r   z     z+TestMemLeakClass.test_leak_mem.<locals>.funz	extra-mem2   r   )assertRaisesRegexAssertionErrorr   )r:   r   r   r.   r.   r1   test_leak_memt  s   
zTestMemLeakClass.test_leak_memc                    s8    fdd}g  t rdnd}td| j| d S )Nc                     s"   t t} | j  |  d S rB   )r^   ru   Z
addCleanupr   r   )rd   boxr:   r.   r1   r     s   z1TestMemLeakClass.test_unclosed_files.<locals>.funfdhandlez	unclosed )r   r   r   r   )r:   r   r   r.   r   r1   test_unclosed_files  s   z$TestMemLeakClass.test_unclosed_filesc                    s>    fdd}g  d}| j ||ddd | t |d  d S )Nc                      s     dd  d S r   r   r.   r   r.   r1   r     r   z,TestMemLeakClass.test_tolerance.<locals>.fund   r   i  )r   r   r   r&   )r   r7   r   )r:   r   r   r.   r   r1   test_tolerance  s   
zTestMemLeakClass.test_tolerancec                 C   s   dd }|  t| | t |  t| W d    n1 s w   Y  dd }| t |  t| W d    d S 1 s@w   Y  d S )Nc                   S   rF   r+   r.   r.   r.   r.   r1   fun_1  s   z2TestMemLeakClass.test_execute_w_exc.<locals>.fun_1c                   S   s   d S rB   r.   r.   r.   r.   r1   fun_2  s   z2TestMemLeakClass.test_execute_w_exc.<locals>.fun_2)Zexecute_w_excr?   r>   r   r   )r:   r   r   r.   r.   r1   test_execute_w_exc  s   "z#TestMemLeakClass.test_execute_w_excN)rM   rN   rO   r   r   r   r   r   r   r	   r   r   r   r   r.   r.   r.   r1   r   b  s    


	r   c                   @   s   e Zd Zdd Zdd ZdS )TestTestingUtilsc                 C   sL   t  }t|}|  dd ||jD d d }| | |  d S )Nc                 S      g | ]
}|d  dkr|qS )r&   r   r.   .0xr.   r.   r1   
<listcomp>      z;TestTestingUtils.test_process_namespace.<locals>.<listcomp>r   )rX   r   r   testrY   gettersr7   r   )r:   r   nsr   r.   r.   r1   test_process_namespace  s
   z'TestTestingUtils.test_process_namespacec                 C   s:   t  }dd ||jD d d }| | t  d S )Nc                 S   r   )r&   net_if_addrsr.   r   r.   r.   r1   r     r   z:TestTestingUtils.test_system_namespace.<locals>.<listcomp>r   )r   rY   r   r7   rX   r  )r:   r  r   r.   r.   r1   test_system_namespace  s   z&TestTestingUtils.test_system_namespaceN)rM   rN   rO   r  r  r.   r.   r.   r1   r     s    r   c                   @   s   e Zd Zdd ZdS )TestOtherUtilsc                 C   s.   t tdddddsJ t t rJ d S )Nr2   za b cr&   r   r4   )r   r   
namedtupletupler   r.   r.   r1   test_is_namedtuple  s   z!TestOtherUtils.test_is_namedtupleN)rM   rN   rO   r  r.   r.   r.   r1   r    s    r  __main__)run_from_name):__doc__r   r   r   rU   r   r   r   r   rX   Zpsutil.testsr   r   r   Zpsutil._commonr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   rQ   rr   r   r   r   r   r  rM   Zpsutil.tests.runnerr
  ru   r.   r.   r.   r1   <module>   st   B'-EGB
