B
    u9a/  ã               @   sl   d dl Z d dlZd dlZd dlZd dlmZ d dlZe d¡ZG dd„ dej	ƒZ
d	dd„Zedkrheƒ  dS )
é    N)ÚsupportÚresourcec               @   s¶   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	e
 ejdkd¡dd„ ƒZdd„ Ze
 eedƒd¡e ddd¡dd„ ƒƒZe
 eedƒd¡e ddd¡dd„ ƒƒZdS )ÚResourceTestc             C   sF   |   ttj¡ |   ttjdd¡ |   ttj¡ |   ttjddd¡ d S )Né*   )ÚassertRaisesÚ	TypeErrorr   Ú	getrlimitÚ	setrlimit)Úself© r   ú#/usr/lib/python3.7/test_resource.pyÚ	test_args   s    zResourceTest.test_argsc             C   sN   yt  t j¡\}}W n tk
r(   Y n"X |  t j|¡ t  t j||f¡ d S )N)r   r   ÚRLIMIT_FSIZEÚAttributeErrorÚassertEqualZRLIM_INFINITYr	   )r
   ÚcurÚmaxr   r   r   Útest_fsize_ismax   s    zResourceTest.test_fsize_ismaxc             C   s   yt  t j¡\}}W n tk
r(   Y nôX zÊyt  t jd|f¡ d}W n tk
r^   d}Y nX ttjdƒ}zz| 	d¡ y:| 	d¡ | 
¡  x"tdƒD ]}t d¡ | 
¡  q–W W n tk
rÌ   |sÈ‚ Y nX |rät  t j||f¡ W d | ¡  X W d |rt  t j||f¡ t tj¡ X d S )	Ni   TFÚwbs   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXó   Yé   gš™™™™™¹?)r   r   r   r   r	   Ú
ValueErrorÚopenr   ZTESTFNÚwriteÚflushÚrangeÚtimeZsleepÚOSErrorÚcloseÚunlink)r
   r   r   Z	limit_setÚfÚir   r   r   Útest_fsize_enforced"   s8    	



z ResourceTest.test_fsize_enforcedc          	   C   s–   dd }yt  t j¡\}}W n tk
r0   Y nbX yt  t j||f¡ W n ttfk
r`   Y nX yt  t j||f¡ W n ttfk
r   Y nX d S )Né
   é2   )r   r   r   r   r	   ÚOverflowErrorr   )r
   Ztoo_bigr   r   r   r   r   Útest_fsize_toobigQ   s    zResourceTest.test_fsize_toobigc          	   C   s   |   ttj¡ |   ttjdd¡ t tj¡}t tj¡}yt tj¡}W n ttfk
r`   Y nX yt tj	¡}W n ttfk
rŠ   Y nX d S )Nr   )
r   r   r   Z	getrusageZRUSAGE_SELFZRUSAGE_CHILDRENZRUSAGE_BOTHr   r   ZRUSAGE_THREAD)r
   Z	usageselfZusagechildrenZ	usagebothZusage_threadr   r   r   Útest_getrusageb   s    zResourceTest.test_getrusagec             C   sH   yt  t j¡}W n tk
r$   Y n X G dd„ dƒ}t  t j|ƒ ¡ d S )Nc               @   s   e Zd Zdd„ Zdd„ ZdS )z9ResourceTest.test_setrusage_refcount.<locals>.BadSequencec             S   s   dS )Né   r   )r
   r   r   r   Ú__len__y   s    zAResourceTest.test_setrusage_refcount.<locals>.BadSequence.__len__c             S   s    |dkrt ttdƒƒƒS t‚d S )N)r   é   i@B )ÚlenÚtupler   Ú
IndexError)r
   Úkeyr   r   r   Ú__getitem__{   s    zEResourceTest.test_setrusage_refcount.<locals>.BadSequence.__getitem__N)Ú__name__Ú
__module__Ú__qualname__r)   r/   r   r   r   r   ÚBadSequencex   s   r3   )r   r   Z
RLIMIT_CPUr   r	   )r
   Úlimitsr3   r   r   r   Útest_setrusage_refcountr   s    z$ResourceTest.test_setrusage_refcountc             C   s$   t  ¡ }|  |t¡ |  |d¡ d S )Nr   )r   ZgetpagesizeÚassertIsInstanceÚintZassertGreaterEqual)r
   Zpagesizer   r   r   Útest_pagesize‚   s    zResourceTest.test_pagesizeZlinuxztest requires Linuxc          
   C   s>   x8dD ]0}t  t¡ |  ttd| ƒt¡ W d Q R X qW d S )N)ZMSGQUEUEZNICEZRTPRIOZRTTIMEZ
SIGPENDINGÚRLIMIT_)Ú
contextlibÚsuppressr   r6   Úgetattrr   r7   )r
   Úattrr   r   r   Útest_linux_constants‡   s    
z!ResourceTest.test_linux_constantsc          
   C   s>   x8dD ]0}t  t¡ |  ttd| ƒt¡ W d Q R X qW d S )N)ZSWAPZSBSIZEZNPTSr9   )r:   r;   r   r6   r<   r   r7   )r
   r=   r   r   r   Útest_freebsd_contants   s    
z"ResourceTest.test_freebsd_contantsÚprlimitz
no prlimitr(   é   é$   c             C   s`   |   ttj¡ |   ttjdtj¡ t tj¡}|  t dtj¡|¡ |  t dtj|¡|¡ d S )Néÿÿÿÿr   )r   r   r   r@   ÚProcessLookupErrorÚ	RLIMIT_ASr   r   )r
   Úlimitr   r   r   Útest_prlimit’   s    

zResourceTest.test_prlimitc                s<   G ‡ fdd„dƒ}t  t j¡‰ |  t  dt j|ƒ ¡ˆ ¡ d S )Nc                   s    e Zd Zdd„ Z‡ fdd„ZdS )z2ResourceTest.test_prlimit_refcount.<locals>.BadSeqc             S   s   dS )Nr(   r   )r
   r   r   r   r)   ¢   s    z:ResourceTest.test_prlimit_refcount.<locals>.BadSeq.__len__c                s   ˆ | d S )Nr*   r   )r
   r.   )r4   r   r   r/   ¤   s    z>ResourceTest.test_prlimit_refcount.<locals>.BadSeq.__getitem__N)r0   r1   r2   r)   r/   r   )r4   r   r   ÚBadSeq¡   s   rH   r   )r   r   rE   r   r@   )r
   rH   r   )r4   r   Útest_prlimit_refcountž   s    z"ResourceTest.test_prlimit_refcountN)r0   r1   r2   r   r   r"   r&   r'   r5   r8   ÚunittestZ
skipUnlessÚsysÚplatformr>   r?   Úhasattrr   r   Zrequires_linux_versionrG   rI   r   r   r   r   r      s   /r   c             C   s   t  t¡ d S )N)r   Zrun_unittestr   )Úverboser   r   r   Ú	test_main¬   s    rO   Ú__main__)N)r:   rK   ÚosrJ   Ztestr   r   Úimport_moduler   ZTestCaser   rO   r0   r   r   r   r   Ú<module>   s   
 !
