B
    u9a9#  ã               @   sn   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eedƒsHe d¡‚G dd„ dej	ƒZ
edkrje ¡  dS )z
Tests for kqueue wrapper.
é    NÚkqueueztest works only on BSDc               @   sD   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	dS )Ú
TestKQueuec             C   sT   t  ¡ }|  | ¡ dk| ¡ ¡ |  |j ¡ | ¡  |  |j¡ |  t|j¡ d S )Nr   )Úselectr   Ú
assertTrueÚfilenoÚclosedÚcloseÚassertRaisesÚ
ValueError)ÚselfÚkq© r   ú!/usr/lib/python3.7/test_kqueue.pyÚtest_create_queue   s    zTestKQueue.test_create_queuec       
      C   s*  ddl m}m}m}m} t tjtj¡}|  	tj
|¡ t |¡}t d¡}|  |j|¡ |  |jtj¡ |  |jtj¡ |  |jd¡ |  |jd¡ |  |jd¡ |  ||¡ |  ||¡ |  ||k ¡ |  ||k¡ xD||||fD ]4}|  t||d ¡ |  t||d¡ |  t||d¡ qäW t |tj¡}|  |j|¡ |  |jtj¡ |  |jtj¡ |  |jd¡ |  |jd¡ |  |jd¡ |  ||¡ |  ||¡ t |tjtj¡}|  |j|¡ |  |jtj¡ |  |jtj¡ |  |jd¡ |  |jd¡ |  |jd¡ |  ||¡ |  ||¡ t ddddd	d
¡}|  |jd¡ |  |jd¡ |  |jd¡ |  |jd¡ |  |jd	¡ |  |jd
¡ |  ||¡ |  ||¡ d}	t |	ddd|	d |	¡}|  |j|	¡ |  |jd¡ |  |jd¡ |  |jd¡ |  |j|	d ¡ |  |j|	¡ |  ||¡ |  ||¡ d}	t dd|	¡}|  |jd¡ |  |jd¡ |  |j|	¡ |  |jd¡ |  |jd¡ |  |jd¡ |  ||¡ |  ||¡ d}	t ddd|	¡}|  |jd¡ |  |jd¡ |  |jd¡ |  |j|	¡ |  |jd¡ |  |jd¡ |  ||¡ |  ||¡ d S )Nr   )ÚltÚleÚgtÚgeiè  é   Úevé   é   é   é   é   iÿ  iÿÿ  l   ÿÿ )Úoperatorr   r   r   r   ÚosÚopenÚdevnullÚO_WRONLYÚ
addCleanupr   r   ÚkeventÚassertEqualÚidentÚfilterÚKQ_FILTER_READÚflagsÚ	KQ_EV_ADDZfflagsÚdataZudataZassertNotEqualr   r	   Ú	TypeErrorÚKQ_FILTER_WRITEZKQ_EV_ONESHOT)
r   r   r   r   r   Úfdr   ÚotherÚopZbignumr   r   r   Útest_create_event   s˜    

zTestKQueue.test_create_eventc          
   C   s  t   ¡ }| d¡ | ¡  t   ¡ }| d¡ y| d| ¡ d f¡ W n6 tk
r| } z|  |jd t	j
¡ W d d }~X Y nX | ¡ \}}t ¡ }tj | ¡ ¡}t | ¡ tjtjtjB ¡}| |gd¡ t | ¡ tjtjtjB ¡}| |gd¡ t | ¡ tjtjtjB ¡}| |gd¡ t | ¡ tjtjtjB ¡}| |gd¡ | d dd¡}	tdd„ |	D ƒƒ}	|  |	t| ¡ tjf| ¡ tjfgƒ¡ | d	¡ | d
¡ xDtdƒD ].}
| d dd¡}	t|	ƒdkrÒP t d¡ q°W |  d¡ tdd„ |	D ƒƒ}	|  |	t| ¡ tjf| ¡ tjf| ¡ tjf| ¡ tjfgƒ¡ t | ¡ tjtj¡}| |gd¡ t | ¡ tjtj¡}| |gd¡ t | ¡ tjtj¡}| |gdd¡ | g dd¡}	tdd„ |	D ƒƒ}	|  |	t| ¡ tjfgƒ¡ | ¡  | ¡  | ¡  d S )N)z	127.0.0.1r   Fz	127.0.0.1r   r   r   c             s   s   | ]}|j |jfV  qd S )N)r#   r$   )Ú.0Úer   r   r   ú	<genexpr>”   s    z.TestKQueue.test_queue_event.<locals>.<genexpr>s   Hello!s   world!!!é
   g      ð?z'timeout waiting for event notificationsc             s   s   | ]}|j |jfV  qd S )N)r#   r$   )r/   r0   r   r   r   r1   ¥   s    g®Gáz®ï?c             s   s   | ]}|j |jfV  qd S )N)r#   r$   )r/   r0   r   r   r   r1   »   s    )ÚsocketZbindZlistenZsetblockingZconnectZgetsocknameÚOSErrorr"   ÚargsÚerrnoZEINPROGRESSZacceptr   r   Zfromfdr   r!   r*   r'   ÚKQ_EV_ENABLEÚcontrolr%   ÚsetÚsendÚrangeÚlenÚtimeZsleepZfailZKQ_EV_DELETEr   )r   ZserverSocketZclientr0   ZserverZaddrr   Zkq2r   ZeventsÚir   r   r   Útest_queue_eventp   s„    

&









zTestKQueue.test_queue_eventc             C   s´   t  ¡ }t ¡ \}}| d¡ t  |t jt jt jB ¡}t  |t jt jt jB ¡}| 	||gdd¡}|  
|¡ |  |d jt j@ ¡ |  | |d j¡d¡ | ¡  | ¡  | ¡  d S )Ns   foor   r   )r   r   r3   Ú
socketpairr:   r!   r%   r'   r7   r8   r   ÚassertFalser&   ZKQ_EV_ERRORr"   Zrecvr(   r   )r   r   ÚaÚbZevent1Zevent2Úrr   r   r   ÚtestPairÃ   s    

zTestKQueue.testPairc                s–   t  ¡ }t ¡ \}}t  |t jt jt jB ¡‰ | ˆ gd¡ | ˆ fd¡ G ‡ fdd„dƒ}| |ƒ d¡ | t	ˆ gƒd¡ | 
¡  | 
¡  | 
¡  d S )Nr   c                   s    e Zd Zdd„ Z‡ fdd„ZdS )z+TestKQueue.test_issue30058.<locals>.BadListc             S   s   dS )Nr   r   )r   r   r   r   Ú__len__Þ   s    z3TestKQueue.test_issue30058.<locals>.BadList.__len__c             3   s   xt dƒD ]
}ˆ V  q
W d S )Néd   )r;   )r   r>   )r   r   r   Ú__iter__à   s    z4TestKQueue.test_issue30058.<locals>.BadList.__iter__N)Ú__name__Ú
__module__Ú__qualname__rF   rH   r   )r   r   r   ÚBadListÝ   s   rL   )r   r   r3   r@   r!   r%   r'   r7   r8   Úiterr   )r   r   rB   rC   rL   r   )r   r   Útest_issue30058Ó   s    zTestKQueue.test_issue30058c             C   s‚   t tdƒ}|  |j¡ | ¡ }t ¡ }|  | ¡ t¡ |  	|j
¡ | ¡  |  |j
¡ |  t|j¡ | ¡  |  t|jd d¡ d S )NÚrbr   )r   Ú__file__r    r   r   r   r   ZassertIsInstanceÚintrA   r   r   r	   r
   r8   )r   Z	open_filer+   r   r   r   r   Ú
test_closeë   s    
zTestKQueue.test_closec             C   s.   t  ¡ }|  |j¡ |  t | ¡ ¡d¡ d S )NF)r   r   r    r   r"   r   Úget_inheritabler   )r   r   r   r   r   Útest_fd_non_inheritable   s    z"TestKQueue.test_fd_non_inheritableN)
rI   rJ   rK   r   r.   r?   rE   rN   rR   rT   r   r   r   r   r      s   YSr   Ú__main__)Ú__doc__r6   r   r   r3   r=   ZunittestÚhasattrZSkipTestZTestCaser   rI   Úmainr   r   r   r   Ú<module>   s   

 y