B
    v9au                @   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yddlZW n ek
rp   dZY nX 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Zejdkrd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 Z$dd Z%G dd dZ&G dd dej'Z(G dd de(Z)G dd dej*Z+G dd dej'Z,G dd dej-Z.G dd dej/Z0G dd  d Z1G d!d" d"Z2G d#d$ d$Z3G d%d& d&e3Z4G d'd( d(e3Z5ejdkrJG d)d* d*e1e5e4e!j6Z7G d+d, d,e1e5e4e2e!j6Z8nddl9Z9G d-d. d.e1e5e4Z:e;e9d/rG d0d1 d1e:e2e!j6Z<e;e9d2rG d3d4 d4e:e2e!j6Z=e;e9d5rG d6d7 d7e:e2e!j6Z>G d8d* d*e:e2e!j6Z7d9d: Z?G d;d< d<e!j6Z@G d=d> d>ej6ZAG d?d@ d@ej6ZBG dAdB dBej6ZCG dCdD dDej6ZDG dEdF dFZEG dGdH dHeEej6ZFyddlGZGW n ek
r   Y nX G dIdJ dJeEej6ZHG dKdL dLej6ZIG dMdN dNej6ZJeKdOkreL  dS )PzTests for events.py.    N)mockwin32)base_events)	constants)
coroutines)events)proactor_events)selector_events)utils)supportc              C   sF   t jdrdS t jdkrdS t d } ttt| d} | dk S )z4Return True if the platform is Mac OS 10.4 or older.ZaixTdarwinFr   .)
      )sysplatform
startswithZmac_vertuplemapintsplit)version r   !/usr/lib/python3.7/test_events.pybroken_unix_getsockname(   s    
r   c              C   s&   dd } t  }t | ||  S )Nc                  s   dS )Nhellor   r   r   r   r   doit4   s    z8_test_get_event_loop_new_process__sub_proc.<locals>.doit)asyncionew_event_loopset_event_looprun_until_complete)r   loopr   r   r   *_test_get_event_loop_new_process__sub_proc3   s    
r"   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
CoroLikec             C   s   d S )Nr   )selfvr   r   r   send=   s    zCoroLike.sendc             G   s   d S )Nr   )r$   excr   r   r   throw@   s    zCoroLike.throwc             C   s   d S )Nr   )r$   r   r   r   closeC   s    zCoroLike.closec             C   s   d S )Nr   )r$   r   r   r   	__await__F   s    zCoroLike.__await__N)__name__
__module____qualname__r&   r(   r)   r*   r   r   r   r   r#   <   s   r#   c               @   s>   e Zd ZdZdZdddZdd Zdd Zdd	 Zd
d Z	dS )MyBaseProtoNc             C   s:   d | _ d| _d| _|d k	r6tj|d| _tj|d| _d S )NINITIALr   )r!   )	transportstatenbytesr   Future	connecteddone)r$   r!   r   r   r   __init__N   s    zMyBaseProto.__init__c             C   s6   || _ | jdkst| jd| _| jr2| jd  d S )Nr/   	CONNECTED)r0   r1   AssertionErrorr4   
set_result)r$   r0   r   r   r   connection_madeV   s
    zMyBaseProto.connection_madec             C   s*   | j dkst| j |  jt|7  _d S )Nr7   )r1   r8   r2   len)r$   datar   r   r   data_received]   s    zMyBaseProto.data_receivedc             C   s   | j dkst| j d| _ d S )Nr7   EOF)r1   r8   )r$   r   r   r   eof_receiveda   s    zMyBaseProto.eof_receivedc             C   s0   | j dkst| j d| _ | jr,| jd  d S )N)r7   r>   CLOSED)r1   r8   r5   r9   )r$   r'   r   r   r   connection_loste   s    zMyBaseProto.connection_lost)N)
r+   r,   r-   r4   r5   r6   r:   r=   r?   rA   r   r   r   r   r.   J   s   
r.   c                   s   e Zd Z fddZ  ZS )MyProtoc                s   t  | |d d S )Ns%   GET / HTTP/1.0
Host: example.com

)superr:   write)r$   r0   )	__class__r   r   r:   m   s    zMyProto.connection_made)r+   r,   r-   r:   __classcell__r   r   )rE   r   rB   l   s   rB   c               @   s:   e Zd ZdZdddZdd Zdd Zdd	 Zd
d ZdS )MyDatagramProtoNc             C   s&   d| _ d| _|d k	r"tj|d| _d S )Nr/   r   )r!   )r1   r2   r   r3   r5   )r$   r!   r   r   r   r6   u   s    zMyDatagramProto.__init__c             C   s$   || _ | jdkst| jd| _d S )Nr/   INITIALIZED)r0   r1   r8   )r$   r0   r   r   r   r:   {   s    zMyDatagramProto.connection_madec             C   s*   | j dkst| j |  jt|7  _d S )NrH   )r1   r8   r2   r;   )r$   r<   addrr   r   r   datagram_received   s    z!MyDatagramProto.datagram_receivedc             C   s   | j dkst| j d S )NrH   )r1   r8   )r$   r'   r   r   r   error_received   s    zMyDatagramProto.error_receivedc             C   s0   | j dkst| j d| _ | jr,| jd  d S )NrH   r@   )r1   r8   r5   r9   )r$   r'   r   r   r   rA      s    zMyDatagramProto.connection_lost)N)	r+   r,   r-   r5   r6   r:   rJ   rK   rA   r   r   r   r   rG   r   s   
rG   c               @   s:   e Zd ZdZdddZdd Zdd Zdd	 Zd
d ZdS )MyReadPipeProtoNc             C   s.   dg| _ d| _d | _|d k	r*tj|d| _d S )Nr/   r   )r!   )r1   r2   r0   r   r3   r5   )r$   r!   r   r   r   r6      s
    zMyReadPipeProto.__init__c             C   s,   || _ | jdgkst| j| jd d S )Nr/   r7   )r0   r1   r8   append)r$   r0   r   r   r   r:      s    zMyReadPipeProto.connection_madec             C   s.   | j ddgkst| j |  jt|7  _d S )Nr/   r7   )r1   r8   r2   r;   )r$   r<   r   r   r   r=      s    zMyReadPipeProto.data_receivedc             C   s(   | j ddgkst| j | j d d S )Nr/   r7   r>   )r1   r8   rM   )r$   r   r   r   r?      s    zMyReadPipeProto.eof_receivedc             C   sR   d| j kr| j d | j dddgks0t| j | j d | jrN| jd  d S )Nr>   r/   r7   r@   )r1   rM   r8   r5   r9   )r$   r'   r   r   r   rA      s    
zMyReadPipeProto.connection_lost)N)	r+   r,   r-   r5   r6   r:   r=   r?   rA   r   r   r   r   rL      s   
rL   c               @   s*   e Zd ZdZdddZdd Zdd ZdS )	MyWritePipeProtoNc             C   s&   d| _ d | _|d k	r"tj|d| _d S )Nr/   )r!   )r1   r0   r   r3   r5   )r$   r!   r   r   r   r6      s    zMyWritePipeProto.__init__c             C   s$   || _ | jdkst| jd| _d S )Nr/   r7   )r0   r1   r8   )r$   r0   r   r   r   r:      s    z MyWritePipeProto.connection_madec             C   s0   | j dkst| j d| _ | jr,| jd  d S )Nr7   r@   )r1   r8   r5   r9   )r$   r'   r   r   r   rA      s    z MyWritePipeProto.connection_lost)N)r+   r,   r-   r5   r6   r:   rA   r   r   r   r   rN      s   
rN   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 )MySubprocessProtocolc                sr   d| _ d | _tj d| _tj d| _ fddtdD | _ddd| _d | _	tj
 dtj
 dd| _d S )Nr/   )r!   c                s   i | ]}t j d |qS ))r!   )r   r3   ).0fd)r!   r   r   
<dictcomp>   s    z1MySubprocessProtocol.__init__.<locals>.<dictcomp>       )      )r1   r0   r   r3   r4   	completedrangedisconnectsr<   
returncodeZEventgot_data)r$   r!   r   )r!   r   r6      s    
zMySubprocessProtocol.__init__c             C   s0   || _ | jdkst| jd| _| jd  d S )Nr/   r7   )r0   r1   r8   r4   r9   )r$   r0   r   r   r   r:      s    z$MySubprocessProtocol.connection_madec             C   s*   | j dkst| j d| _ | jd  d S )Nr7   r@   )r1   r8   rW   r9   )r$   r'   r   r   r   rA      s    z$MySubprocessProtocol.connection_lostc             C   s8   | j dkst| j | j|  |7  < | j|   d S )Nr7   )r1   r8   r<   r[   set)r$   rQ   r<   r   r   r   pipe_data_received   s    z'MySubprocessProtocol.pipe_data_receivedc             C   s>   | j dkst| j |r*| j| | n| j| | d S )Nr7   )r1   r8   rY   Zset_exceptionr9   )r$   rQ   r'   r   r   r   pipe_connection_lost   s    z)MySubprocessProtocol.pipe_connection_lostc             C   s$   | j dkst| j | j | _d S )Nr7   )r1   r8   r0   Zget_returncoderZ   )r$   r   r   r   process_exited   s    z#MySubprocessProtocol.process_exitedN)	r+   r,   r-   r6   r:   rA   r]   r^   r_   r   r   r   r   rO      s   rO   c                   sP  e Zd Z fddZ f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d Zdd Zdd Zdd Zdd Zdd  Zejd!d" Zd#d$ Zd%d& Zeeed'd(d)d* Zeeed+d,d-d. Zeeed+d,d/d0 Zdd2d3Zd4d5 Z ejd6d7 Z!d8d9 Z"d1d:i fd;d<Z#dd=d>Z$dd?d@Z%e&e'd:kdAdBdC Z(eje&e'd:kdAdDdE Z)dFdG Z*dHdI Z+ddJdKZ,e&e'd:kdAdLdM Z-dNdO Z.e/0dPdQdR Z1dSdT Z2dUdV Z3dWdX Z4eee5dYdZd[d\ Z6d]d^ Z7ejd_d` Z8eee5dadbdcdd Z9ddedfZ:ddgdhZ;ddidjZ<e&e'd:kdAdkdl Z=eje&e'd:kdAdmdn Z>e&e'd:kdAdodp Z?eje&e'd:kdAdqdr Z@e&e'd:kdAdsdt ZAeje&e'd:kdAdudv ZBe&e'd:kdAdwdx ZCdydz ZDd{d| ZEeejFd}d~d ZGdd ZHdd ZIdd ZJdd ZKeeLjMdkddd ZNeeLjMdkddd ZOeeLjMdkddd ZPeeLjMdkddd ZQeeLjMdkddd ZReeLjMdkdeSdddd ZTeeLjMdkdeSdddd ZUdd ZVdd ZWdd ZXdd ZYdd ZZdd Z[  Z\S )EventLoopTestsMixinc                s$   t    |  | _| | j d S )N)rC   setUpcreate_event_loopr!   r   )r$   )rE   r   r   ra      s    

zEventLoopTestsMixin.setUpc                s4   | j  st| j  |   t  t   d S )N)	r!   	is_closed
test_utilsrun_brieflyZ
doCleanupsr   Z
gc_collectrC   tearDown)r$   )rE   r   r   rf      s
    
zEventLoopTestsMixin.tearDownc                s:   t jdd  t j fdd}tjj|  d S )Nc               s   s
   d V  d S )Nr   r   r   r   r   coro1   s    zBEventLoopTestsMixin.test_run_until_complete_nesting.<locals>.coro1c                  s"    j  j   d S )N)
assertTruer!   
is_runningr    r   )rg   r$   r   r   coro2  s    zBEventLoopTestsMixin.test_run_until_complete_nesting.<locals>.coro2)r   	coroutineassertRaisesRuntimeErrorr!   r    )r$   rj   r   )rg   r$   r   test_run_until_complete_nesting   s    z3EventLoopTestsMixin.test_run_until_complete_nestingc             C   sX   | j  }| j tjd| j d | j  }| d||   koFdkn  ||  d S )Ng?)r!   g{Gz?g?)r!   timer    r   sleeprh   )r$   t0t1r   r   r   test_run_until_complete  s    

z+EventLoopTestsMixin.test_run_until_completec                s(    fdd}| }  t jj| d S )Nc                  s$    j   tjd j dI d H  d S )Ng?)r!   )r!   stopr   rp   r   )r$   r   r   cb  s    
z?EventLoopTestsMixin.test_run_until_complete_stopped.<locals>.cb)rl   rm   r!   r    )r$   ru   Ztaskr   )r$   r   test_run_until_complete_stopped  s    z3EventLoopTestsMixin.test_run_until_complete_stoppedc                sv   g   fdd}j d|d t }j   t } dg d||   koddkn  ||  d S )Nc                s     |  j  d S )N)rM   r!   rt   )arg)resultsr$   r   r   callback  s    
z5EventLoopTestsMixin.test_call_later.<locals>.callbackg?zhello worldg{Gz?g?)r!   
call_laterro   	monotonicrun_foreverassertEqualrh   )r$   ry   rq   rr   r   )rx   r$   r   test_call_later  s    
z#EventLoopTestsMixin.test_call_laterc                s>   g   fdd}j |dd j    dg d S )Nc                s     | |f j  d S )N)rM   r!   rt   )Zarg1Zarg2)rx   r$   r   r   ry   ,  s    z4EventLoopTestsMixin.test_call_soon.<locals>.callbackr   world)r   r   )r!   	call_soonr|   r}   )r$   ry   r   )rx   r$   r   test_call_soon)  s
    
z"EventLoopTestsMixin.test_call_soonc          	      s   g t  fdd  fdd}  t j|d}|   j d j  W d Q R X |  	ddg d S )Nc                s$     |  t dkr j  d S )NrV   )rM   r;   r!   rt   )rw   )rx   r$   r   r   ry   8  s    
z?EventLoopTestsMixin.test_call_soon_threadsafe.<locals>.callbackc                  s   j  d   d S )Nr   )r!   call_soon_threadsafereleaser   )ry   lockr$   r   r   run_in_thread=  s    zDEventLoopTestsMixin.test_call_soon_threadsafe.<locals>.run_in_thread)targetr   r   )
	threadingZLockacquireThreadstartr!   r   r|   joinr}   )r$   r   tr   )ry   r   rx   r$   r   test_call_soon_threadsafe4  s    z-EventLoopTestsMixin.test_call_soon_threadsafec                sL   g   fdd}j |d j |d j    ddg d S )Nc                s$     |  t dkr j  d S )NrV   )rM   r;   r!   rt   )rw   )rx   r$   r   r   ry   N  s    
zKEventLoopTestsMixin.test_call_soon_threadsafe_same_thread.<locals>.callbackr   r   )r!   r   r   r|   r}   )r$   ry   r   )rx   r$   r   %test_call_soon_threadsafe_same_threadK  s    
z9EventLoopTestsMixin.test_call_soon_threadsafe_same_threadc             C   sH   dd }| j d |d}| j |\}}| |d | |t  d S )Nc             S   s   | t  fS )N)r   	get_ident)rw   r   r   r   runY  s    z5EventLoopTestsMixin.test_run_in_executor.<locals>.runZyo)r!   run_in_executorr    r}   ZassertNotEqualr   r   )r$   r   f2resZ	thread_idr   r   r   test_run_in_executorX  s
    z(EventLoopTestsMixin.test_run_in_executorc                s`   d  fdd}dd }| j d |}|  | j   || j _|| j _td |   d S )NFc                 s   d d S )NTr   )args)calledr   r   patched_call_soonc  s    zJEventLoopTestsMixin.test_run_in_executor_cancel.<locals>.patched_call_soonc               S   s   t d d S )Ng?)ro   rp   r   r   r   r   r   g  s    z<EventLoopTestsMixin.test_run_in_executor_cancel.<locals>.rung?)	r!   r   cancelr)   r   r   ro   rp   assertFalse)r$   r   r   r   r   )r   r   test_run_in_executor_cancel`  s    

z/EventLoopTestsMixin.test_run_in_executor_cancelc                s   t  \}d t   fdd}j | j|jd t	
j fdd j|jd t	
j fdd j|j jjj j   d	 d S )
NFc                 sV   y d} W n tk
r"   d S X | r4 |  nj    d S )Ni   )recvBlockingIOErrorextendrh   r!   remove_readerfilenor)   )r<   )
bytes_readrr$   r   r   readerw  s    z8EventLoopTestsMixin.test_reader_callback.<locals>.readers   abcc                  s   t  dkS )NrS   )r;   r   )r   r   r   <lambda>  rT   z:EventLoopTestsMixin.test_reader_callback.<locals>.<lambda>s   defc                  s   t  dkS )N   )r;   r   )r   r   r   r     rT   s   abcdef)socket
socketpairsetblocking	bytearrayr!   
add_readerr   r   r&   rd   	run_untilr)   rt   r|   r}   )r$   wr   r   )r   r   r$   r   test_reader_callbackr  s    

z(EventLoopTestsMixin.test_reader_callbackc                s   t  \}d  fdd}d} j ||  j    j   	 j  
  |t|d }|
   || d S )NFc                s    |   j  d S )N)r&   r!   rt   )r<   )r$   r   r   r   writer  s    
z8EventLoopTestsMixin.test_writer_callback.<locals>.writers   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxrV   )r   r   r   r!   
add_writerr   r|   rh   remove_writerr   r)   r   r;   r}   )r$   r   r   r<   readr   )r$   r   r   test_writer_callback  s    

z(EventLoopTestsMixin.test_writer_callbackc          	   C   s  t | jtjs| jd |d | t | j| j	||j
 W d Q R X | t | j| j|d W d Q R X | t | j| j|d W d Q R X | t | j| j|t  W d Q R X | t | j| j| W d Q R X |d | j| j	||j
 | j| j|d | j| j|d}| j| j|d |  | |d d S )NTs   GET / HTTP/1.0

i   Fs   HTTP/1.0 200 OK)
isinstancer!   r   BaseProactorEventLoop	set_debugr   rl   
ValueErrorr    sock_connectaddresssock_sendall	sock_recvsock_recv_intor   sock_acceptr)   rh   r   )r$   httpdsockr<   r   r   r   _basetest_sock_client_ops  s:    

z-EventLoopTestsMixin._basetest_sock_client_opsc          	   C   s   | d | j| j||j | j| j|d td}t|B}| j| j||d d }| j| j|||d   W d Q R X |	  | 
|d d S )NFs   GET / HTTP/1.0

i   s   HTTP/1.0 200 OK)r   r!   r    r   r   r   r   
memoryviewr   r)   rh   r   )r$   r   r   r<   bufr2   r   r   r   _basetest_sock_recv_into  s    

"z,EventLoopTestsMixin._basetest_sock_recv_intoc          	   C   s@   t  .}t }| || t }| || W d Q R X d S )N)rd   run_test_serverr   r   r   )r$   r   r   r   r   r   test_sock_client_ops  s
    
z(EventLoopTestsMixin.test_sock_client_opsc          	   C   sH   t  6}ttj}| || ttj}| || W d Q R X d S )N)rd   run_test_unix_serverr   AF_UNIXr   r   )r$   r   r   r   r   r   test_unix_sock_client_ops  s
    
z-EventLoopTestsMixin.test_unix_sock_client_opsc          	   C   sx   d }zt   }|d | }W d |  X t   }|d | t | j| j	|| W d Q R X |  d S )N)z	127.0.0.1r   F)
r   bindgetsocknamer)   r   rl   ConnectionRefusedErrorr!   r    r   )r$   r   sr   r   r   r   test_sock_client_fail  s    


z)EventLoopTestsMixin.test_sock_client_failc             C   s   t   }|d |d |d t   }||  | j|}| j|\}}| 	|
 d | 	||  | 	| |  |  |  |  d S )NF)z	127.0.0.1r   rU   r   )r   r   r   listenconnectr   r!   r   r    r}   Z
gettimeoutZgetpeernamer)   )r$   ZlistenerclientfconnrI   r   r   r   test_sock_accept  s    


z$EventLoopTestsMixin.test_sock_acceptSIGKILLz
No SIGKILLc                sX  d  fdd}|  t| jjd| |  t| jjd |  t| jjtjd | |  t| jjtjd  |  t| jjd| |  t| jjd |  t| jjd| |  t| jjd |  t| jjtj	| | 
| jtj	 | jtj| tt tj t| j fdd | | jtj | ttjtj | 
| jtj d S )	Nr   c                  s    d7  d S )NrU   r   r   )caughtr   r   
my_handler  s    z?EventLoopTestsMixin.test_add_signal_handler.<locals>.my_handlerZboomrU   c                  s    S )Nr   r   )r   r   r   r   +  rT   z=EventLoopTestsMixin.test_add_signal_handler.<locals>.<lambda>)rl   	TypeErrorr!   add_signal_handlerremove_signal_handlerr   signalNSIGrm   r   r   SIGINToskillgetpidrd   r   rh   r}   	getsignaldefault_int_handler)r$   r   r   )r   r   test_add_signal_handler  s<    z+EventLoopTestsMixin.test_add_signal_handlerSIGALRMz
No SIGALRMc                s^   d  fdd}j tj| ttjdd j dj j j   	 d d S )Nr   c                  s    d7  j   d S )NrU   )r!   rt   r   )r   r$   r   r   r   9  s    zLEventLoopTestsMixin.test_signal_handling_while_selecting.<locals>.my_handlerg{Gz?<   rU   )
r!   r   r   r   	setitimerITIMER_REALrz   rt   r|   r}   )r$   r   r   )r   r$   r   $test_signal_handling_while_selecting4  s    
z8EventLoopTestsMixin.test_signal_handling_while_selectingc                sj   dd  fdd}j jtj|f  ttjdd j dj j j   	 d d S )N)*   r   c                 s"    d7   |  j  d S )NrU   )r}   r!   rt   )r   )r   r$   	some_argsr   r   r   J  s    zAEventLoopTestsMixin.test_signal_handling_args.<locals>.my_handlerg?r   rU   )
r!   r   r   r   r   r   rz   rt   r|   r}   )r$   r   r   )r   r$   r   r   test_signal_handling_argsE  s    
z-EventLoopTestsMixin.test_signal_handling_argsTc             C   sv   | j |\}}| |tj | |tj | |j| |rN| |	d | j |j
 | |jd |  d S )Nsocknamer   )r!   r    assertIsInstancer   	TransportProtocolassertIsr0   assertIsNotNoneget_extra_infor5   assertGreaterr2   r)   )r$   connection_futcheck_socknametrprr   r   r   _basetest_create_connectionW  s    z/EventLoopTestsMixin._basetest_create_connectionc          	      s>   t  ,} jj fddf|j } | W d Q R X d S )Nc                  s   t  jdS )N)r!   )rB   r!   r   )r$   r   r   r   e  rT   z<EventLoopTestsMixin.test_create_connection.<locals>.<lambda>)rd   r   r!   create_connectionr   r   )r$   r   conn_futr   )r$   r   test_create_connectionb  s    
z*EventLoopTestsMixin.test_create_connectionc          	      sD   t   }t *} j fdd|j} || W d Q R X d S )Nc                  s   t  jdS )N)r!   )rB   r!   r   )r$   r   r   r   p  rT   zAEventLoopTestsMixin.test_create_unix_connection.<locals>.<lambda>)r   rd   r   r!   create_unix_connectionr   r   )r$   r   r   r   r   )r$   r   test_create_unix_connectionh  s
    
z/EventLoopTestsMixin.test_create_unix_connectionc          	      s
  t  }d } j jj|jdtji}xf|D ]R\}}}}}y4tj|||d}|d  j j	|| W n   Y q2X P q2W dst
d jj fdd|d}	 j|	\}
} |
tj  |tj  j|j  |jd |
  W d Q R X d S )	Ntype)familyr   protoFzCan not create socket.c                  s   t  jdS )N)r!   )rB   r!   r   )r$   r   r   r     rT   zAEventLoopTestsMixin.test_create_connection_sock.<locals>.<lambda>)r   r   )rd   r   r!   r    getaddrinfor   r   SOCK_STREAMr   r   r8   r   r   r   r   r   r5   r   r2   r)   )r$   r   r   infosr   r   r   cnamer   r   r   r   r   )r$   r   test_create_connection_socks  s.    

z/EventLoopTestsMixin.test_create_connection_sockNc             C   s  |r|  |d |r,| ||d n|  |d | ||d |d}| |t | t|d| | |d t | |d t | |d t |d	}|  | | | |d
 | |	 |d | |
 |d | | |d
 d S )Nr   peernamepeercertcipherrS   r   rU   rV   Z
ssl_objectcompression)r   r   r}   r   r   r;   strr   r  r  Zgetpeercert)r$   r   r   r  r  r  Zsslobjr   r   r   check_ssl_extra_info  s0    






z(EventLoopTestsMixin.check_ssl_extra_infoc             C   sx   | j |\}}| |tj | |tj | d|jj	 k | 
||| | j |j | |jd |  d S )Nsslr   )r!   r    r   r   r   r   rh   rE   r+   lowerr	  r5   r   r2   r)   )r$   r   r   r  r   r   r   r   r   _basetest_create_ssl_connection  s    z3EventLoopTestsMixin._basetest_create_ssl_connectionc       	   
      s   |t  d} ||| ttdrtjjfd d d d fdd}tjd|d,}|dd} |||  	|j
d	 W d Q R X  tj2}|dd}t    ||| W d Q R X W d Q R X  	|jjd
 d S )N)r
  Purpose)cafilecapathcadatac               s     | tjj t S )z
                A ssl.create_default_context() replacement that doesn't enable
                cert validation.
                )r}   r
  r  SERVER_AUTHrd   dummy_ssl_context)Zpurposer  r  r  )r$   r   r   _dummy_ssl_create_context  s    zREventLoopTestsMixin._test_create_ssl_connection.<locals>._dummy_ssl_create_contextzssl.create_default_context)side_effectTrU   ZCERTIFICATE_VERIFY_FAILED)rd   r  r  hasattrr
  r  r  r   patchr}   Z
call_countrl   SSLErrordisable_logger	exceptionreason)	r$   r   r   r   r  r   r  mcmr   )r$   r   _test_create_ssl_connection  s&    
	



z/EventLoopTestsMixin._test_create_ssl_connectionzNo ssl modulec          	      sN   t jdd8}tj jj fddf|j } j|||jd W d Q R X d S )NT)use_sslc                  s   t  jdS )N)r!   )rB   r!   r   )r$   r   r   r     rT   z@EventLoopTestsMixin.test_create_ssl_connection.<locals>.<lambda>)r  )rd   r   	functoolspartialr!   r   r   r  )r$   r   r   r   )r$   r   test_create_ssl_connection  s    
z.EventLoopTestsMixin.test_create_ssl_connectionc          	      sX   t   }tjdd:}tj jj fdd|jdd} j||||jd W d Q R X d S )NT)r  c                  s   t  jdS )N)r!   )rB   r!   r   )r$   r   r   r     rT   zEEventLoopTestsMixin.test_create_ssl_unix_connection.<locals>.<lambda>z	127.0.0.1)server_hostname)r  )	r   rd   r   r  r   r!   r   r   r  )r$   r   r   r   r   )r$   r   test_create_ssl_unix_connection  s    z3EventLoopTestsMixin.test_create_ssl_unix_connectionc          	      s   t  n}t } jj fddf|jd|jd |fi} j|\}}|j	dd } 
|| |  W d Q R X d S )Nc                  s   t  jdS )N)r!   )rB   r!   r   )r$   r   r   r     rT   zGEventLoopTestsMixin.test_create_connection_local_addr.<locals>.<lambda>
local_addrr   r   rU   )rd   r   r   find_unused_portr!   r   r   r    r0   r   r}   r)   )r$   r   portr   r   r   expectedr   )r$   r   !test_create_connection_local_addr  s    
z5EventLoopTestsMixin.test_create_connection_local_addrc          
      s   t  t} jj fddf|jd|ji} t} j| W d Q R X  |j	j
t
j  t|j|j	j W d Q R X d S )Nc                  s   t  jdS )N)r!   )rB   r!   r   )r$   r   r   r   	  rT   zNEventLoopTestsMixin.test_create_connection_local_addr_in_use.<locals>.<lambda>r$  )rd   r   r!   r   r   rl   OSErrorr    r}   r  errno
EADDRINUSEassertInr  strerror)r$   r   r   r  r   )r$   r   (test_create_connection_local_addr_in_use  s    
z<EventLoopTestsMixin.test_create_connection_local_addr_in_usec       	         s  | j G fdddt}t }|d |d |  dd d fdd}tj|d	d
}|  |	 \}}|d_ 
jfdd||d   j  |  tj|dd | |  | jd | jt |  d S )Nc                   s,   e Zd Z fddZ fddZ  ZS )zAEventLoopTestsMixin.test_connect_accepted_socket.<locals>.MyProtoc                s   t  | j d S )N)rC   rA   r   rt   )r$   r'   )rE   r!   r   r   rA     s    zQEventLoopTestsMixin.test_connect_accepted_socket.<locals>.MyProto.connection_lostc                s   t  | | j d S )N)rC   r=   r0   rD   )r$   r<   )rE   expected_responser   r   r=     s    zOEventLoopTestsMixin.test_connect_accepted_socket.<locals>.MyProto.data_received)r+   r,   r-   rA   r=   rF   r   )r/  r!   )rE   r   rB     s   rB   )z	127.0.0.1r   rU   s	   test datas   rogerc           
      sv   yDt   } d k	r| } |   |  | d|   W n, tk
rp } ztd| W d d }~X Y nX d S )Nc   z8Failure in client thread in test_connect_accepted_socket)r   Zwrap_socketr   sendallr   r)   	Exceptionprint)Zcsockr'   )rI   
client_sslmessageresponser   r   r   &  s    



z@EventLoopTestsMixin.test_connect_accepted_socket.<locals>.clientT)r   Zdaemon)r!   c                  s    S )Nr   r   )r   r   r   r   =  rT   zBEventLoopTestsMixin.test_connect_accepted_socket.<locals>.<lambda>)r
  )timeoutr@   )r!   r.   r   r   r   r   r   r   r   Zacceptr    connect_accepted_socketr|   r0   r)   r   Zjoin_threadr   Zis_aliver}   r1   r2   r;   )	r$   Z
server_sslr4  rB   Zlsockr   Zthreadr   _r   )rI   r4  r/  r!   r5  r   r6  r   test_connect_accepted_socket  s4    




z0EventLoopTestsMixin.test_connect_accepted_socketc             C   sL   t jdkr,t jdk r,t| jtjr,tdt	
 }t	 }| || d S )Nr   )rS   r   z=SSL not supported with proactor event loops before Python 3.5)r   r   version_infor   r!   r   r   unittestSkipTestrd   simple_server_sslcontextsimple_client_sslcontextr:  )r$   Zserver_contextZclient_contextr   r   r    test_ssl_connect_accepted_socketH  s    

z4EventLoopTestsMixin.test_ssl_connect_accepted_socketc          	   C   sN   t   }| |j | jjt|dd}| td | j| W d Q R X d S )NrU   )Zssl_handshake_timeoutz1ssl_handshake_timeout is only meaningful with ssl)	r   
addCleanupr)   r!   r8  rB   assertRaisesRegexr   r    )r$   r   coror   r   r   9test_connect_accepted_socket_ssl_timeout_for_plain_socketW  s    zMEventLoopTestsMixin.test_connect_accepted_socket_ssl_timeout_for_plain_socketzasyncio.base_events.socketc       	         s   t j fddfdd}t|} tjkrJdd |D | j_ndd |D | j_|j_t	
 j_t	
 j_jfdd	|d
}j|}|j dd |jD }|| d S )Nc                s>    t jkr  t jdd| |ffgS  t jdd| |ddffgS d S )Nr    r   )r   AF_INETr   )hostr&  r   kw)r   r   r   r   c  s    
zEEventLoopTestsMixin.create_server_multiple_hosts.<locals>.getaddrinfoc                 s   t j | |jdS )N)r!   )r   Taskr!   )r   kwds)r   r$   r   r   getaddrinfo_taskj  s    zJEventLoopTestsMixin.create_server_multiple_hosts.<locals>.getaddrinfo_taskc             S   s   g | ]}|d fqS )P   r   )rP   rG  r   r   r   
<listcomp>q  s    zDEventLoopTestsMixin.create_server_multiple_hosts.<locals>.<listcomp>c             S   s   g | ]}|d ddfqS )rL  r   r   )rP   rG  r   r   r   rM  t  s    c                  s
   t  jS )N)rB   r!   r   )r$   r   r   r   x  rT   zBEventLoopTestsMixin.create_server_multiple_hosts.<locals>.<lambda>rL  c             S   s   h | ]}|  d  qS )r   )getsockbyname)rP   r   r   r   r   	<setcomp>{  s    zCEventLoopTestsMixin.create_server_multiple_hosts.<locals>.<setcomp>)r   rk   r\   r   rF  rN  r  r!   r   r   MockZ_start_serving_stop_servingcreate_serverr    rA  r)   socketsr}   )	r$   r   ZhostsZ	mock_sockrK  Zunique_hostsr   serverZserver_hostsr   )r   r   r$   r   create_server_multiple_hostsa  s    
z0EventLoopTestsMixin.create_server_multiple_hostsc             C   s   |  tjdddg d S )Nz1.2.3.4z5.6.7.8)rU  r   rF  )r$   r   r   r   &test_create_server_multiple_hosts_ipv4~  s    z:EventLoopTestsMixin.test_create_server_multiple_hosts_ipv4c             C   s   |  tjdddg d S )Nz::1z::2)rU  r   AF_INET6)r$   r   r   r   &test_create_server_multiple_hosts_ipv6  s    z:EventLoopTestsMixin.test_create_server_multiple_hosts_ipv6c                s&  t | j | j fdddd}| j|}| t|jd |jd }| \}}| |d t }|	d|f |
d | j j | d j t| j fd	d | d
 j |  jd | d jdd   j  | j j | d j |  |  d S )Nc                  s    S )Nr   r   )r   r   r   r     rT   z8EventLoopTestsMixin.test_create_server.<locals>.<lambda>z0.0.0.0r   rU   z	127.0.0.1s   xxxr7   c                  s
    j dkS )Nr   )r2   r   )r   r   r   r     rT   rS   r   r  r@   )rB   r!   rR  r    r}   r;   rS  r   r   r   r1  r4   r1   rd   r   r2   r   r0   r   r)   r5   )r$   r   rT  r   rG  r&  r   r   )r   r   test_create_server  s,    



z&EventLoopTestsMixin.test_create_serverSO_REUSEPORTzNo SO_REUSEPORTc                s   t | j | j fdddd}| j|}| t|jd |jd }| |t	j
t	j |  t| j t | j | jj fdddddd}| j|}| t|jd |jd }| |t	j
t	j |  d S )	Nc                  s    S )Nr   r   )r   r   r   r     rT   zCEventLoopTestsMixin.test_create_server_reuse_port.<locals>.<lambda>z0.0.0.0r   rU   c                  s    S )Nr   r   )r   r   r   r     rT   T)Z
reuse_port)rB   r!   rR  r    r}   r;   rS  r   Z
getsockoptr   
SOL_SOCKETrZ  r)   rd   re   rh   )r$   r   rT  r   r   )r   r   test_create_server_reuse_port  s*    



z1EventLoopTestsMixin.test_create_server_reuse_portc                s@   t   |  fdd | jj| f|}| j|}| fS )Nc                  s   t j ot  S )N)r   pathexistsunlinkr   )r]  r   r   r     rT   z7EventLoopTestsMixin._make_unix_server.<locals>.<lambda>)rd   Zgen_unix_socket_pathrA  r!   create_unix_serverr    )r$   factorykwargsr   rT  r   )r]  r   _make_unix_server  s
    z%EventLoopTestsMixin._make_unix_serverc                s   t | jd |  fdd\}}| t|jd ttj}|| |	d | j
 j | d j t| j fdd | d j  j  | j
 j | d	 j |  |  d S )
N)r!   c                  s    S )Nr   r   )r   r   r   r     rT   z=EventLoopTestsMixin.test_create_unix_server.<locals>.<lambda>rU   s   xxxr7   c                  s
    j dkS )Nr   )r2   r   )r   r   r   r     rT   rS   r@   )rB   r!   rc  r}   r;   rS  r   r   r   r1  r    r4   r1   rd   r   r2   r0   r)   r5   )r$   rT  r]  r   r   )r   r   test_create_unix_server  s    


z+EventLoopTestsMixin.test_create_unix_serverr   zNo UNIX Socketsc          
      sf   t | jd t }|D | jj fddd|d}| td | j| W d Q R X W d Q R X d S )N)r!   c                  s    S )Nr   r   )r   r   r   r     rT   zOEventLoopTestsMixin.test_create_unix_server_path_socket_error.<locals>.<lambda>z/test)r   z3path and sock can not be specified at the same time)rB   r!   r   r`  rB  r   r    )r$   r   r   r   )r   r   )test_create_unix_server_path_socket_error  s    z=EventLoopTestsMixin.test_create_unix_server_path_socket_errorc             C   s,   t t j}| jt jO  _||| |S )N)r
  
SSLContextZPROTOCOL_TLS_SERVERoptionsOP_NO_SSLv2Zload_cert_chain)r$   certfilekeyfile
sslcontextr   r   r   _create_ssl_context  s    z'EventLoopTestsMixin._create_ssl_contextc       
      C   sX   |  ||}| jj|dd|d}| j|}|jd }| \}}	| |d |||	fS )Nz	127.0.0.1r   )r
  )rl  r!   rR  r    rS  r   r}   )
r$   ra  ri  rj  rk  r   rT  r   rG  r&  r   r   r   _make_ssl_server  s    
z$EventLoopTestsMixin._make_ssl_serverc             C   s   |  ||}| j||dS )N)r
  )rl  rc  )r$   ra  ri  rj  rk  r   r   r   _make_ssl_unix_server  s    z)EventLoopTestsMixin._make_ssl_unix_serverc                s   t | jd |  fddtjtj\}}}| jjt||t d}| j	|\}}|
d | j	 j | d j t| j fdd | d j | j|||fd	  j  | j	 j | d
 j |  |  d S )N)r!   c                  s    S )Nr   r   )r   r   r   r     rT   z<EventLoopTestsMixin.test_create_server_ssl.<locals>.<lambda>)r
  s   xxxr7   c                  s
    j dkS )Nr   )r2   r   )r   r   r   r     rT   rS   )r  r@   )rB   r!   rm  rd   ONLYCERTONLYKEYr   r.   r  r    rD   r4   r}   r1   r   r2   r	  r0   r)   r5   )r$   rT  rG  r&  f_cr   r   r   )r   r   test_create_server_ssl	  s"    

z*EventLoopTestsMixin.test_create_server_sslc                s   t | jd |  fddtjtj\}}| jjt|t dd}| j	|\}}|
d | j	 j | d j t| j fdd | d	 j  j  | j	 j | d
 j |  |  d S )N)r!   c                  s    S )Nr   r   )r   r   r   r   .  rT   zAEventLoopTestsMixin.test_create_unix_server_ssl.<locals>.<lambda>rE  )r
  r"  s   xxxr7   c                  s
    j dkS )Nr   )r2   r   )r   r   r   r   9  rT   rS   r@   )rB   r!   rn  rd   ro  rp  r   r.   r  r    rD   r4   r}   r1   r   r2   r0   r)   r5   )r$   rT  r]  rq  r   r   r   )r   r   test_create_unix_server_ssl)  s"    


z/EventLoopTestsMixin.test_create_unix_server_sslc                s   t | jd |  fddtj\}}}ttj}| jtj	O  _tj
|_t|dr\d|_| jjt |||d}tj| jdL t , | tjd | j| W d Q R X W d Q R X t| j W d Q R X |  j |  d S )	N)r!   c                  s    S )Nr   r   )r   r   r   r   L  rT   zJEventLoopTestsMixin.test_create_server_ssl_verify_failed.<locals>.<lambda>check_hostnameT)r
  call_exception_handlerz(?i)certificate.verify.failed)rB   r!   rm  rd   SIGNED_CERTFILEr
  rf  PROTOCOL_TLS_CLIENTrg  rh  CERT_REQUIREDverify_moder  rt  r   r   r  objectr  rB  r  r    re   assertIsNoner0   r)   )r$   rT  rG  r&  sslcontext_clientrq  r   )r   r   $test_create_server_ssl_verify_failedH  s$    

 z8EventLoopTestsMixin.test_create_server_ssl_verify_failedc                s   t | jd |  fddtj\}}ttj}| jtj	O  _tj
|_t|drZd|_| jjt ||dd}tj| jdL t , | tjd	 | j| W d Q R X W d Q R X t| j W d Q R X |  j |  d S )
N)r!   c                  s    S )Nr   r   )r   r   r   r   j  rT   zOEventLoopTestsMixin.test_create_unix_server_ssl_verify_failed.<locals>.<lambda>rt  TZinvalid)r
  r"  ru  z(?i)certificate.verify.failed)rB   r!   rn  rd   rv  r
  rf  rw  rg  rh  rx  ry  r  rt  r   r   r  rz  r  rB  r  r    re   r{  r0   r)   )r$   rT  r]  r|  rq  r   )r   r   )test_create_unix_server_ssl_verify_failede  s&    


 z=EventLoopTestsMixin.test_create_unix_server_ssl_verify_failedc                s   t | jd |  fddtj\}}}ttj}| jtj	O  _tj
|_|jtjd t|drjd|_| jjt |||d}tj| jd@ t , | tjd	 | j| W d Q R X W d Q R X W d Q R X |  j |  d S )
N)r!   c                  s    S )Nr   r   )r   r   r   r     rT   zIEventLoopTestsMixin.test_create_server_ssl_match_failed.<locals>.<lambda>)r  rt  T)r
  ru  z=IP address mismatch, certificate is not valid for '127.0.0.1')rB   r!   rm  rd   rv  r
  rf  rw  rg  rh  rx  ry  load_verify_locations
SIGNING_CAr  rt  r   r   r  rz  r  rB  ZCertificateErrorr    r{  r0   r)   )r$   rT  rG  r&  r|  rq  r   )r   r   #test_create_server_ssl_match_failed  s(    


*z7EventLoopTestsMixin.test_create_server_ssl_match_failedc                s   t | jd |  fddtj\}}ttj}| jtj	O  _tj
|_|jtjd t|drhd|_| jjt ||dd}| j|\}} j  |  |  | j j d S )	N)r!   c                  s    S )Nr   r   )r   r   r   r     rT   zJEventLoopTestsMixin.test_create_unix_server_ssl_verified.<locals>.<lambda>)r  rt  T	localhost)r
  r"  )rB   r!   rn  rd   rv  r
  rf  rw  rg  rh  rx  ry  r  r  r  rt  r   r    r0   r)   r5   )r$   rT  r]  r|  rq  r   r   r   )r   r   $test_create_unix_server_ssl_verified  s"    


z8EventLoopTestsMixin.test_create_unix_server_ssl_verifiedc                s   t | jd |  fddtj\}}}ttj}| jtj	O  _tj
|_|jtjd t|drjd|_| jjt |||dd}| j|\}}| j|||ftjd	  j  |  |  | j j d S )
N)r!   c                  s    S )Nr   r   )r   r   r   r     rT   zEEventLoopTestsMixin.test_create_server_ssl_verified.<locals>.<lambda>)r  rt  Tr  )r
  r"  )r  r  )rB   r!   rm  rd   rv  r
  rf  rw  rg  rh  rx  ry  r  r  r  rt  r   r    r	  ZPEERCERTr0   r)   r5   )r$   rT  rG  r&  r|  rq  r   r   r   )r   r   test_create_server_ssl_verified  s&    


z3EventLoopTestsMixin.test_create_server_ssl_verifiedc       	         s   t j| jd G  fdddt}tjtjd}|tjtjd |	d | jj
||d}| j|}|jd }| || | \}}| |d	 t }|d
|f |d |  |  d S )N)r!   c                   s   e Zd Z fddZ  ZS )z@EventLoopTestsMixin.test_create_server_sock.<locals>.TestMyProtoc                s   t  | |  d S )N)rC   r:   r9   )r$   r0   )rE   r   r   r   r:     s    zPEventLoopTestsMixin.test_create_server_sock.<locals>.TestMyProto.connection_made)r+   r,   r-   r:   rF   r   )r   )rE   r   TestMyProto  s   r  )r   rU   )z0.0.0.0r   )r   r   z0.0.0.0z	127.0.0.1s   xxx)r   r3   r!   rB   r   r   
setsockoptr[  SO_REUSEADDRr   rR  r    rS  r   r   r}   r   r&   r)   )	r$   r  sock_obr   rT  r   rG  r&  r   r   )r   r   test_create_server_sock  s     


z+EventLoopTestsMixin.test_create_server_sockc          	   C   s   t j t jd}|t jt jd |d | jjt|d}| j	|}|j
d }| \}}| jjt||d}| t}| j	| W d Q R X | |jjtj |  d S )N)r   rU   )z0.0.0.0r   )r   r   )rG  r&  )r   r   r  r[  r  r   r!   rR  rB   r    rS  r   rl   r)  r}   r  r*  r+  r)   )r$   r  r   rT  r   rG  r&  r  r   r   r   test_create_server_addr_in_use  s    

z2EventLoopTestsMixin.test_create_server_addr_in_usezIPv6 not supported or enabledc       	   
      s<  t j| jd G  fdddt}d}xzy*t }| jj|d |d}| j|}W nH tk
r } z*|j	t	j
kr|d7 }| d| w(n W d d }~X Y q(X P q(W t }|d|f |d	 | j }|j  |  t j| jd ttj}|d
|f |d	 | j }|j  |  |  d S )N)r!   c                   s   e Zd Z fddZ  ZS )zFEventLoopTestsMixin.test_create_server_dual_stack.<locals>.TestMyProtoc                s   t  | |  d S )N)rC   r:   r9   )r$   r0   )rE   f_protor   r   r:     s    zVEventLoopTestsMixin.test_create_server_dual_stack.<locals>.TestMyProto.connection_made)r+   r,   r-   r:   rF   r   )r  )rE   r   r    s   r  r   )rG  r&  rU   r   z	127.0.0.1s   xxxz::1)r   r3   r!   rB   r   r%  rR  r    r)  r*  r+  ZassertGreaterEqualr   r   r&   r0   r)   rW  )	r$   r  Z	try_countr&  r   rT  Zexr   r   r   )r  r   test_create_server_dual_stack  s:    



z1EventLoopTestsMixin.test_create_server_dual_stackc             C   s   | j tdd}| j |}|jd }| \}}t }|d|f |d |	  |	  t }| 
t|jd|f |	  d S )Nz0.0.0.0r   z	127.0.0.1s   xxx)r!   rR  rB   r    rS  r   r   r   r&   r)   rl   r   )r$   r   rT  r   rG  r&  r   r   r   r   test_server_close+  s    

z%EventLoopTestsMixin.test_server_closec                sn  G fdddt }jj|dd}j|\}|d\}}|tj | dj	 
j| jjfdd||fd	}j|\} |tj  t  d j	 
 j| |d
 tjfdd dj tj fdd d j |d |  j j d j	 j  d S )Nc                   s*   e Zd Z fddZ fddZ  ZS )zNEventLoopTestsMixin.test_create_datagram_endpoint.<locals>.TestMyDatagramProtoc                s   t  jjd d S )N)r!   )rC   r6   r!   )Z
inner_self)rE   r$   r   r   r6   ?  s    zWEventLoopTestsMixin.test_create_datagram_endpoint.<locals>.TestMyDatagramProto.__init__c                s$   t  || | jd| | d S )Ns   resp:)rC   rJ   r0   sendto)r$   r<   rI   )rE   r   r   rJ   B  s    z`EventLoopTestsMixin.test_create_datagram_endpoint.<locals>.TestMyDatagramProto.datagram_received)r+   r,   r-   r6   rJ   rF   r   )r$   )rE   r   TestMyDatagramProto>  s   r  )z	127.0.0.1r   )r$  r   rH   c                  s   t  jdS )N)r!   )rG   r!   r   )r$   r   r   r   Q  rT   zCEventLoopTestsMixin.test_create_datagram_endpoint.<locals>.<lambda>)Zremote_addrs   xxxc                  s    j S )N)r2   r   )rT  r   r   r   [  rT   rS   c                  s    j S )N)r2   r   )r   r   r   r   ]  rT      r@   )rG   r!   create_datagram_endpointr    r   r   r   r   r}   r1   r   r0   r  rd   r   r2   r   r)   r5   )r$   r  rC  Zs_transportrG  r&  r0   r   )r   r$   rT  r   test_create_datagram_endpoint=  s6    


z1EventLoopTestsMixin.test_create_datagram_endpointc                s   t jdkr"t jtjr"tdd }d} j jj	|dt
ji}xZ|D ]F\}}}}}y(t
j
|||d}|d || W n   Y qLX P qLW dstd jj fdd	|d
}	 j|	\}
} |
tj  |t |
   j|j d S )Nr   z.UDP is not supported with proactor event loops)z	127.0.0.1r   r   )r   r   r   FzCan not create socket.c                  s   t  jdS )N)r!   )rG   r!   r   )r$   r   r   r     rT   zHEventLoopTestsMixin.test_create_datagram_endpoint_sock.<locals>.<lambda>)r   )r   r   r   r!   r   r   r<  r=  r    r   r   Z
SOCK_DGRAMr   r   r8   r  r   r   r   rG   r)   r5   )r$   r   Zlocal_addressr  r   r   r   r  r   r   r   r   r   )r$   r   "test_create_datagram_endpoint_sockk  s2    

z6EventLoopTestsMixin.test_create_datagram_endpoint_sockc             C   sf   |   }t|tjs&|  | d | d|j |  | d|j | |j	 | |j
 d S )Nz#loop is not a BaseSelectorEventLooprU   r   )rb   r   r	   ZBaseSelectorEventLoopr)   skipTestr}   Z_internal_fdsr{  Z_csockZ_ssock)r$   r!   r   r   r   test_internal_fds  s    
z%EventLoopTestsMixin.test_internal_fdsr   zDon't support pipes for Windowsc                s   t jdt \}}t|dd  fdd}j|  t|d t	jfdd 
d	j t|d
 t	jfdd 
ddgj 
dj t| jj 
ddddgj jd d S )N)r!   rbi   c                 s^   j fdd I d H \} }| | j ddgj dj d S )Nc                  s    S )Nr   r   )r   r   r   r     rT   zEEventLoopTestsMixin.test_read_pipe.<locals>.connect.<locals>.<lambda>r/   r7   r   )r!   connect_read_piper   r0   r}   r1   r2   )r   p)pipeobjr   r$   r   r   r     s    z3EventLoopTestsMixin.test_read_pipe.<locals>.connect   1c                  s
    j dkS )NrU   )r2   r   )r   r   r   r     rT   z4EventLoopTestsMixin.test_read_pipe.<locals>.<lambda>rU   s   2345c                  s
    j dkS )Nr   )r2   r   )r   r   r   r     rT   r/   r7   r   r>   r@   pipe)rL   r!   r   r  ioopenr    rD   rd   r   r}   r2   r1   r)   r5   r   r0   r   )r$   rpipewpiper   r   )r  r   r$   r   test_read_pipe  s"    
z"EventLoopTestsMixin.test_read_pipec                s   |    t dt dt \}}t|ddt|dd fdd} | \}}   | 	dt
| | 	dt
|     d |_d |_d S )N)r!   r  i   r   c                 sD     fddI d H \} } fddI d H \}}| |fS )Nc                  s    S )Nr   r   )
read_protor   r   r     rT   zSEventLoopTestsMixin.test_unclosed_pipe_transport.<locals>.connect.<locals>.<lambda>c                  s    S )Nr   r   )write_protor   r   r     rT   )r  connect_write_pipe)read_transportr9  write_transport)r!   r  rpipeobjwpipeobjr  r   r   r     s
    zAEventLoopTestsMixin.test_unclosed_pipe_transport.<locals>.connectr  )rb   rL   rN   r   r  r  r  r    r)   r,  reprZ_pipe)r$   r  r  r   r  r  r   )r!   r  r  r  r  r   test_unclosed_pipe_transport  s    

z0EventLoopTestsMixin.test_unclosed_pipe_transportc                s  t jdt \}}t|dd  fdd}j|  t|d t	jfdd 
d	j t|d
 t	jfdd 
ddgj 
dj t| j  jj 
ddddgj jd d S )N)r!   r  r   c                 s^   j fdd I d H \} }| | j ddgj dj d S )Nc                  s    S )Nr   r   )r   r   r   r     rT   zKEventLoopTestsMixin.test_read_pty_output.<locals>.connect.<locals>.<lambda>r/   r7   r   )r!   r  r   r0   r}   r1   r2   )r   r  )master_read_objr   r$   r   r   r     s    z9EventLoopTestsMixin.test_read_pty_output.<locals>.connectr  c                  s    j S )N)r2   r   )r   r   r   r     rT   z:EventLoopTestsMixin.test_read_pty_output.<locals>.<lambda>rU   s   2345c                  s
    j dkS )Nr   )r2   r   )r   r   r   r     rT   r/   r7   r   r>   r@   r  )rL   r!   r   openptyr  r  r    rD   rd   r   r}   r2   r1   r)   r0   r5   r   r   )r$   masterslaver   r   )r  r   r$   r   test_read_pty_output  s$    

z(EventLoopTestsMixin.test_read_pty_outputc                s6  t  \}t|dd}t| jd| jfdd|}| j|\}}| | | |j	 | 
dj |d t  fdd	t| j fd
d | 
d  |d t| j fdd | 
d  | 
dj t  | j	d j	  | jj | 
dj d S )Nwbi   )r!   c                  s    S )Nr   r   )r   r   r   r     rT   z5EventLoopTestsMixin.test_write_pipe.<locals>.<lambda>r7   r  c                s   t  d}| |7 } t| S )Ni   )r   r   r;   )r<   chunk)r  r   r   r     s    z3EventLoopTestsMixin.test_write_pipe.<locals>.readerc                  s    dkS )NrU   r   r   )r<   r   r   r   r     rT   s   2345c                  s    dkS )Nr   r   r   )r<   r   r   r   r     rT   s   12345r  r@   )r   r  r  r  rN   r!   r  r    r   r0   r}   r1   rD   r   rd   r   r)   r   r   r5   )r$   r  r  r   r0   r  r   )r<   r   r   r  r   test_write_pipe  s,    



z#EventLoopTestsMixin.test_write_pipec                s   t  \}}|d t| dd}t| jd | j fdd|}| j	|\}}| 
|  | 
| j | d j |d | j	| j|d}| d| |  | j	 j | d	 j d S )
NFr  i   )r!   c                  s    S )Nr   r   )r   r   r   r   ,  rT   zIEventLoopTestsMixin.test_write_pipe_disconnect_on_close.<locals>.<lambda>r7   r  r@   )r   r   r   r  r  detachrN   r!   r  r    r   r0   r}   r1   rD   r   r)   r5   )r$   ZrsockZwsockr  r   r0   r  r<   r   )r   r   #test_write_pipe_disconnect_on_close$  s    

z7EventLoopTestsMixin.test_write_pipe_disconnect_on_closer   r   c                s>  t  \}t|dd}t| jd| jfdd|}| j|\}}| | | |j	 | 
dj |d t  fdd	tj| j fd
ddd | 
d  |d tj| j fdddd | 
d  | 
dj t  | j	d j	  | jj | 
dj d S )Nr  r   )r!   c                  s    S )Nr   r   )r   r   r   r   E  rT   z4EventLoopTestsMixin.test_write_pty.<locals>.<lambda>r7   r  c                s   t  d}| |7 } t| S )Ni   )r   r   r;   )r<   r  )r  r   r   r   N  s    z2EventLoopTestsMixin.test_write_pty.<locals>.readerc                  s    dkS )NrU   r   r   )r<   r   r   r   r   S  rT   r   )r7  s   2345c                  s    dkS )Nr   r   r   )r<   r   r   r   r   X  rT   s   12345r  r@   )r   r  r  r  rN   r!   r  r    r   r0   r}   r1   rD   r   rd   r   r)   r   r   r5   )r$   r  slave_write_objr   r0   r  r   )r<   r  r   r   r   test_write_pty;  s0    



z"EventLoopTestsMixin.test_write_ptyc       
         s  t  \}t |}t| t|dd}t| jd| j	fdd|}| j
|\}}| | | |j | ddgj | dj t|dd}t| jd| jfd	d|}| j
|\}	}| | | |	j | dj t  fd
d|	d tj| j fdddd | d  | ddgj | dj t d tj| jfdddd | ddgj | dj | dj |	d tj| j fdddd | d  | ddgj | dj t d tj| jfdddd | ddgj | dj | dj t  |  | j
j | ddddgj |	  | j
j | dj d S )Nr  r   )r!   c                  s    S )Nr   r   )r  r   r   r   s  rT   z<EventLoopTestsMixin.test_bidirectional_pty.<locals>.<lambda>r/   r7   r  c                  s    S )Nr   r   )r  r   r   r   ~  rT   c                s   t  d}| |7 } t| S )Ni   )r   r   r;   )r<   r  )r  r   r   r     s    z:EventLoopTestsMixin.test_bidirectional_pty.<locals>.readerr  c                  s    dkS )NrU   r   r   )r<   r   r   r   r     rT   r   )r7     ac                  s
    j dkS )NrU   )r2   r   )r  r   r   r     rT   rU   s   2345c                  s    dkS )Nr   r   r   )r<   r   r   r   r     rT   s   12345s   bcdec                  s
    j dkS )Nr   )r2   r   )r  r   r   r     rT   r   r>   r@   )r   r  dupttyZsetrawr  r  rL   r!   r  r    r   r0   r}   r1   r2   rN   r  r   rD   rd   r   r)   r5   )
r$   Z
read_slaveZwrite_slaveZslave_read_objZread_connectr  r  r  Zwrite_connectr  r   )r<   r  r  r   r  r   test_bidirectional_ptyg  sh    




z*EventLoopTestsMixin.test_bidirectional_ptyc                s   t  \}}|d jj|d t dd }|d k	rL|j  fdd}t	
 }tj| jd}j  t	
 | }|d | d tj j |d k	rʈ|j j| |  |  d S )	NFrU   ovc                 sR   z@yj  j  I d H  W n tjk
r8   d} Y nX d } W d j   X | S )N	cancelled)r!   r   r   r   CancelledErrorrt   )r   )r   r$   r   r   main  s    
z:EventLoopTestsMixin.test_prompt_cancellation.<locals>.main)r!   g?r  )r   r   r   r!   create_taskr   getattrrh   Zpendingro   r{   r   rI  r|   Z
assertLessr}   resultrl   r  r   rQ  r)   )r$   r   r   r  r  r   r   elapsedr   )r   r$   r   test_prompt_cancellation  s&    

z,EventLoopTestsMixin.test_prompt_cancellationc                sh    fdd}j j dj _|j _fdd}j |  j jdj jj jjjd d S )Nc                  s   j  jd7  _   d S )NrU   )r!   _run_once_counterr   )orig_run_oncer$   r   r   	_run_once  s    z<EventLoopTestsMixin.test_timeout_rounding.<locals>._run_oncer   c                 sn    j } tjd| dI d H  tjd| dI d H  tjd| dI d H  tjd| dI d H  tjd| dI d H  d S )Ng{Gz?)r!   g-C6?gư>g:0yE>g|=)r!   r   rp   )r!   )r$   r   r   wait  s    z7EventLoopTestsMixin.test_timeout_rounding.<locals>.wait   )Zclock_resolutionZselector)	r!   r  r  r    ZassertLessEqualZ_clock_resolutionZ	_selectorrE   r+   )r$   r  r  r   )r  r$   r   test_timeout_rounding  s    z)EventLoopTestsMixin.test_timeout_roundingc             C   sx   |   }dd }t \}}| |j | |j ||| ||| |  | || | |	| d S )Nc               S   s   d S )Nr   r   r   r   r   r     rT   zCEventLoopTestsMixin.test_remove_fds_after_closing.<locals>.<lambda>)
rb   r   r   rA  r)   r   r   r   r   r   )r$   r!   ry   r   r   r   r   r   test_remove_fds_after_closing  s    z1EventLoopTestsMixin.test_remove_fds_after_closingc          	   C   s   |   }dd }t \}}| |j | |j |  | t ||| W d Q R X | t ||| W d Q R X d S )Nc               S   s   d S )Nr   r   r   r   r   r     rT   z@EventLoopTestsMixin.test_add_fds_after_closing.<locals>.<lambda>)	rb   r   r   rA  r)   rl   rm   r   r   )r$   r!   ry   r   r   r   r   r   test_add_fds_after_closing  s    z.EventLoopTestsMixin.test_add_fds_after_closingc          	      sB   t j fdd}| j} t  j| W d Q R X d S )Nc                s    j   d S )N)r!   r)   )r!   )r$   r   r   
close_loop  s    zEEventLoopTestsMixin.test_close_running_event_loop.<locals>.close_loop)r   rk   r!   rl   rm   r    )r$   r  rC  r   )r$   r   test_close_running_event_loop  s    
z1EventLoopTestsMixin.test_close_running_event_loopc          	   C   s  | j   tjdd }dd }| }| |j | t | j   W d Q R X | t  tj| j d}| j 	| W d Q R X | t | j 
| W d Q R X | t | j | W d Q R X | t | j d| W d Q R X | t | j | j  d | W d Q R X | t | j | W d Q R X | t | j tj| W d Q R X | t  | j d | }t| W d Q R X d S )Nc               S   s   d S )Nr   r   r   r   r   test  s    z,EventLoopTestsMixin.test_close.<locals>.testc               S   s   dS )NFr   r   r   r   r   r     rT   z0EventLoopTestsMixin.test_close.<locals>.<lambda>)r!   g      ?g        )r!   r)   r   rk   rA  rl   rm   r|   r3   r    r   r   rz   call_atro   r  r   r   SIGTERMr   r*   next)r$   r  funcrC  futitr   r   r   
test_close  s2    
"zEventLoopTestsMixin.test_close)T)TN)TN)NN)N)N)N)]r+   r,   r-   ra   rf   rn   rs   rv   r~   r   r   r   r   r   r   r   r   r   r   r   Zskip_unless_bind_unix_socketr   r   r   r<  
skipUnlessr  r   r   r   r   r   r   r   r  r	  r  r  skipIfr
  r!  r#  r(  r.  r:  r@  rD  r   r  rU  rV  rX  rY  r   r\  rc  rd  re  rl  rm  rn  rr  rs  r}  r~  r  r  r  r  r  ZIPV6_ENABLEDr  r  r  r  r  r   r   r  r  r  r  r  requires_mac_verr  r  r  r  r  r  r  r  rF   r   r   )rE   r   r`      s   		$,
  

$


8
$	


 ).!!"&(E#	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d Z	dd Z
dd Zeejdkddd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#S )$SubprocessTestsMixinc             C   s,   t jdkr| |t n| tj | d S )Nr   )r   r   r   r   r}   r   r  )r$   rZ   r   r   r   check_terminated6  s    
z%SubprocessTestsMixin.check_terminatedc             C   s,   t jdkr| |t n| tj | d S )Nr   )r   r   r   r   r}   r   r   )r$   rZ   r   r   r   check_killed=  s    
z!SubprocessTestsMixin.check_killedc          	   C   s   t jt jtd}| jtt	| jt
j|}| j|\}}| |t	 | j|j | d|j |d}|d | j|jd   t  |  W d Q R X | j|j | |j | d|jd  d S )Nzecho.pyr7   r   s   Python The WinnerrU   )r   r]  r   dirname__file__r!   subprocess_execr  r   rO   r   
executabler    r   r4   r}   r1   get_pipe_transportrD   r[   r  rd   r  r)   rW   r  rZ   r<   )r$   progr   transpr   stdinr   r   r   test_subprocess_execD  s     



z)SubprocessTestsMixin.test_subprocess_execc          	   C   s   t jt jtd}| jtt	| jt
j|}| j|\}}| |t	 | j|j | d|j |d}|d | j|jd   |jd   | d|jd  |d | j|jd   | d|jd  t  |  W d Q R X | j|j | |j d S )Nzecho.pyr7   r   s   Python rU   s
   The Winners   Python The Winner)r   r]  r   r  r  r!   r  r  r   rO   r   r  r    r   r4   r}   r1   r  rD   r[   r  clearr<   rd   r  r)   rW   r  rZ   )r$   r  r   r  r   r  r   r   r   test_subprocess_interactiveX  s(    




z0SubprocessTestsMixin.test_subprocess_interactivec             C   s   | j tt| j d}| j |\}}| |t | j |j |d	  | j |j
 | d|j | tdd |j D  | |jd dd | |jd d	 |	  d S )
Nzecho Pythonr   c             s   s   | ]}|  V  qd S )N)r5   )rP   r   r   r   r   	<genexpr>}  s    z=SubprocessTestsMixin.test_subprocess_shell.<locals>.<genexpr>rU   s   
s   PythonrV   rT   )r!   subprocess_shellr  r   rO   r    r   r4   r  r)   rW   r}   rZ   rh   allrY   valuesr<   rstrip)r$   r   r  r   r   r   r   test_subprocess_shellr  s    z*SubprocessTestsMixin.test_subprocess_shellc             C   sd   | j jtt| j dd d d d}| j |\}}| |t | j |j | d|j	 |
  d S )Nzexit 7)r  stdoutstderr   )r!   r  r  r   rO   r    r   rW   r}   rZ   r)   )r$   r   r  r   r   r   r   test_subprocess_exitcode  s    z-SubprocessTestsMixin.test_subprocess_exitcodec             C   s   | j jtt| j dd d d d}| j |\}}| |t | |d | |d | |d | j |j	 | 
d|j | |  d S )Nzexit 7)r  r  r  r   rU   rV   r  )r!   r  r  r   rO   r    r   r{  r  rW   r}   rZ   r)   )r$   r   r  r   r   r   r   "test_subprocess_close_after_finish  s    z7SubprocessTestsMixin.test_subprocess_close_after_finishc             C   s   t jt jtd}| jtt	| jt
j|}| j|\}}| |t	 | j|j |  | j|j | |j |  d S )Nzecho.py)r   r]  r   r  r  r!   r  r  r   rO   r   r  r    r   r4   r   rW   r  rZ   r)   )r$   r  r   r  r   r   r   r   test_subprocess_kill  s    
z)SubprocessTestsMixin.test_subprocess_killc             C   s   t jt jtd}| jtt	| jt
j|}| j|\}}| |t	 | j|j |  | j|j | |j |  d S )Nzecho.py)r   r]  r   r  r  r!   r  r  r   rO   r   r  r    r   r4   Z	terminaterW   r  rZ   r)   )r$   r  r   r  r   r   r   r   test_subprocess_terminate  s    
z.SubprocessTestsMixin.test_subprocess_terminater   zDon't have SIGHUPc          
   C   s   t  t jt j}ztjtjtd}| j	t
t| jtj|}| j|\}}| |t | j|j |t j | j|j | t j |j |  W d t  t j| X d S )Nzecho.py)r   SIGHUPSIG_DFLr   r]  r   r  r  r!   r  r  r   rO   r   r  r    r   r4   Zsend_signalrW   r}   rZ   r)   )r$   Zold_handlerr  r   r  r   r   r   r   test_subprocess_send_signal  s    
z0SubprocessTestsMixin.test_subprocess_send_signalc             C   s   t jt jtd}| jtt	| jt
j|}| j|\}}| |t	 | j|j |d}|d | j|j |  | d|jd  | |jd d|jd  | d|j d S )Nzecho2.pyr   s   tests   OUT:testrU   rV   s   ERR:test)r   r]  r   r  r  r!   r  r  r   rO   r   r  r    r   r4   r  rD   rW   r)   r}   r<   rh   r   rZ   )r$   r  r   r  r   r  r   r   r   test_subprocess_stderr  s    


z+SubprocessTestsMixin.test_subprocess_stderrc             C   s   t jt jtd}| jjtt	| jt
j|tjd}| j|\}}| |t	 | j|j |d}| |d | |d |d | j|j | |jd d|jd  | d|jd  |  | d|j d S )	Nzecho2.py)r  r   rU   rV   s   tests   OUT:testERR:testrT   )r   r]  r   r  r  r!   r  r  r   rO   r   r  
subprocessZSTDOUTr    r   r4   r  r   r{  rD   rW   rh   r<   r   r}   r)   rZ   )r$   r  r   r  r   r  r   r   r   )test_subprocess_stderr_redirect_to_stdout  s"    

z>SubprocessTestsMixin.test_subprocess_stderr_redirect_to_stdoutc          	   C   sF  t jt jtd}| jtt	| jt
j|}| j|\}}| |t	 | j|j |d}|d}|d | j|jd   | d|jd  |  | j|jd  |d | j|jd   t
jdkr| d	|jd  n| d
|jd  t  |  W d Q R X | j|j | |j d S )Nzecho3.pyr   rU   s   tests   OUT:tests   xxxrV   r   s   ERR:BrokenPipeErrors   ERR:OSError)r   r]  r   r  r  r!   r  r  r   rO   r   r  r    r   r4   r  rD   r[   r  r}   r<   r)   rY   r   rd   r  rW   r  rZ   )r$   r  r   r  r   r  r  r   r   r   #test_subprocess_close_client_stream  s.    






z8SubprocessTestsMixin.test_subprocess_close_client_streamc             c   s`   | j jtt| j dd d d dd}| j |V \}}| |t | j |j | d|j	 d S )Nzexit 7T)r  r  r  Zstart_new_sessionr  )
r!   r  r  r   rO   r    r   rW   r}   rZ   )r$   r   r9  r   r   r   r   "test_subprocess_wait_no_same_group  s    z7SubprocessTestsMixin.test_subprocess_wait_no_same_groupc          	      s    fdd}  t  j|dd W d Q R X   t  j|dd W d Q R X   t  j|dd W d Q R X d S )Nc                 s    j jtjdf| I d H  d S )Npwd)r!   r  r   SubprocessProtocol)rJ  )r$   r   r   r   '  s    zGSubprocessTestsMixin.test_subprocess_exec_invalid_args.<locals>.connectT)universal_newlinesi   )bufsize)shell)rl   r   r!   r    )r$   r   r   )r$   r   !test_subprocess_exec_invalid_args%  s    z6SubprocessTestsMixin.test_subprocess_exec_invalid_argsc          	      s   d fdd	}  t  j|ddg W d Q R X   t  j|dd W d Q R X   t  j|dd W d Q R X   t  j|d	d
 W d Q R X d S )Nc                s&   | sd}  j jtj| f|I d H  d S )Nr  )r!   r  r   r  )cmdrJ  )r$   r   r   r   5  s
    zHSubprocessTestsMixin.test_subprocess_shell_invalid_args.<locals>.connectZlsz-lT)r  i   )r  F)r  )N)rl   r   r!   r    )r$   r   r   )r$   r   "test_subprocess_shell_invalid_args3  s    z7SubprocessTestsMixin.test_subprocess_shell_invalid_argsN)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  4  s    
!r  c                   sP   e Zd Zdd ZdZe fddZe fddZ fdd	Zd
d Z	  Z
S )SendfileBases   SendfileBaseDatai   i   c          	      s2   t tjd}|| j W d Q R X t   d S )Nr  )r  r   TESTFNrD   DATArC   
setUpClass)clsfp)rE   r   r   r  N  s    zSendfileBase.setUpClassc                s   t t j t   d S )N)r   r_  r  rC   tearDownClass)r  )rE   r   r   r	  T  s    zSendfileBase.tearDownClassc                s*   t tjd| _| | jj t   d S )Nr  )r  r   r  filerA  r)   rC   ra   )r$   )rE   r   r   ra   Y  s    zSendfileBase.setUpc             C   s   | j |S )N)r!   r    )r$   rC  r   r   r   run_loop^  s    zSendfileBase.run_loop)r+   r,   r-   r  BUF_SIZEclassmethodr  r	  ra   r  rF   r   r   )rE   r   r  F  s   r  c                   s   e Zd ZG dd dejZe fddZe fddZddd	Z	d
d Z
dddZdd Zdd Zdd Zdd Zdd Z  ZS )SockSendfileMixinc               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )zSockSendfileMixin.MyProtoc             C   s(   d| _ d| _t | _| | _d | _d S )NF)startedclosedr   r<   create_futurer  r0   )r$   r!   r   r   r   r6   f  s
    
z"SockSendfileMixin.MyProto.__init__c             C   s   d| _ || _d S )NT)r  r0   )r$   r0   r   r   r   r:   m  s    z)SockSendfileMixin.MyProto.connection_madec             C   s   | j | d S )N)r<   r   )r$   r<   r   r   r   r=   q  s    z'SockSendfileMixin.MyProto.data_receivedc             C   s   d| _ | jd  d S )NT)r  r  r9   )r$   r'   r   r   r   rA   t  s    z)SockSendfileMixin.MyProto.connection_lostc                s   | j I d H  d S )N)r  )r$   r   r   r   wait_closedx  s    z%SockSendfileMixin.MyProto.wait_closedN)r+   r,   r-   r6   r:   r=   rA   r  r   r   r   r   rB   d  s
   rB   c                s   t j| _dt _t   d S )Ni @  )r   !SENDFILE_FALLBACK_READBUFFER_SIZE_SockSendfileMixin__old_bufsizerC   r  )r  )rE   r   r   r  {  s    zSockSendfileMixin.setUpClassc                s   | j t_t   d S )N)r  r   r  rC   r	  )r  )rE   r   r   r	    s    zSockSendfileMixin.tearDownClassTc             C   s.   t  t jt j}|d |r*| |j |S )NF)r   rF  r   r   rA  r)   )r$   cleanupr   r   r   r   make_socket  s
    
zSockSendfileMixin.make_socketc             C   s   | tjtj| j d S )N)r  r   r[  Z	SO_RCVBUFr  )r$   r   r   r   r   reduce_receive_buffer_size  s    z,SockSendfileMixin.reduce_receive_buffer_sizeNc             C   s.   | tjtj| j |d k	r*|j| jd d S )N)Zhigh)r  r   r[  Z	SO_SNDBUFr  Zset_write_buffer_limits)r$   r   r0   r   r   r   reduce_send_buffer_size  s    z)SockSendfileMixin.reduce_send_buffer_sizec                s    j t }jdd}|tj|f jj fdd|d	|  }j
|d|f |  fdd}| | fS )	NF)r  c                  s    S )Nr   r   )r   r   r   r     rT   z8SockSendfileMixin.prepare_socksendfile.<locals>.<lambda>)r   z	127.0.0.1c                  s<    j d k	r" j          d S )N)r0   r)   r  r  r   )r   r$   rT  r   r   r    s
    

z7SockSendfileMixin.prepare_socksendfile.<locals>.cleanup)rB   r!   r   r%  r  r   HOSTr  rR  r  r   r  rA  )r$   r&  srv_sockr   r  r   )r   r$   rT  r   prepare_socksendfile  s    




z&SockSendfileMixin.prepare_socksendfilec             C   sv   |   \}}| | j|| j}|  | |  | |t| j	 | |j
| j	 | | j t| j	 d S )N)r  r  r!   sock_sendfiler
  r)   r  r}   r;   r  r<   tell)r$   r   r   retr   r   r   test_sock_sendfile_success  s    z,SockSendfileMixin.test_sock_sendfile_successc             C   sv   |   \}}| | j|| jdd}|  | |  | |j| j	dd  | | j
 d | |d d S )Ni  i  i  )r  r  r!   r  r
  r)   r  r}   r<   r  r  )r$   r   r   r  r   r   r   (test_sock_sendfile_with_offset_and_count  s    
z:SockSendfileMixin.test_sock_sendfile_with_offset_and_countc          	   C   sp   |   \}}t }| | j||dd }W d Q R X |  | |  | |d | | j	
 d d S )Nr   )r  tempfileZTemporaryFiler  r!   r  r)   r  r}   r
  r  )r$   r   r   r   r  r   r   r   test_sock_sendfile_zero_size  s    
z.SockSendfileMixin.test_sock_sendfile_zero_sizec             C   s   dd }|   \}}| | j|| | | j|| j}| | j|| |  | |  | |t	| j
 || j
 | }| |j| | | j t	| j
 d S )Ns   mix_regular_sendi   )r  r  r!   r   r  r
  r)   r  r}   r;   r  r<   r  )r$   r   r   r   r  r'  r   r   r   (test_sock_sendfile_mix_with_regular_send  s    z:SockSendfileMixin.test_sock_sendfile_mix_with_regular_send)T)N)r+   r,   r-   r   r   rB   r  r  r	  r  r  r  r  r  r   r"  r#  rF   r   r   )rE   r   r  b  s   



r  c               @   s   e Zd ZG dd deZdddddZeej	dk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d Zdd Zd d! Zd"d# Zd$d% Zeeed& d'd(d) Zd*d+ Zd,S )-SendfileMixinc                   s*   e Zd Zd fdd	Z fddZ  ZS )zSendfileMixin.MySendfileProtoNr   c                s   t  | t | _|| _d S )N)rC   r6   r   r<   close_after)r$   r!   r%  )rE   r   r   r6     s    z&SendfileMixin.MySendfileProto.__init__c                s8   | j | t | | jr4| j| jkr4| j  d S )N)r<   r   rC   r=   r%  r2   r0   r)   )r$   r<   )rE   r   r   r=     s    z+SendfileMixin.MySendfileProto.data_received)Nr   )r+   r,   r-   r6   r=   rF   r   r   )rE   r   MySendfileProto  s   r&  Fr   )is_sslr%  c               s$  t  }jj|d|r<ts*d t }t }nd }d }t		t	j
t	j}|t j|f jjfdd||d| |rt j}nd }t		t	j
t	j}|t j|f jjd jj fdd|||d\}	}
j||	d	  fd
d}|  fS )N)r!   r%  zNo ssl modulec                  s    S )Nr   r   )	srv_protor   r   r   	  rT   z0SendfileMixin.prepare_sendfile.<locals>.<lambda>)r   r
  )r!   c                  s    S )Nr   r   )	cli_protor   r   r   	  rT   )r   r
  r"  )r0   c                  sF   j    j   j  j     d S )N)r0   r)   r  r5   r  r   )r)  r$   rT  r(  r   r   r  	  s    

z/SendfileMixin.prepare_sendfile.<locals>.cleanup)r   r%  r&  r!   r
  r  rd   r>  r?  r   rF  r   r   r  r  rR  r  r   r   r  rA  )r$   r'  r%  r&  Zsrv_ctxZcli_ctxr  r"  Zcli_sockr   r   r  r   )r)  r$   rT  r(  r   prepare_sendfile  s8    




	
zSendfileMixin.prepare_sendfiler   zUDP sockets are not supportedc          	      sx      jj fddtjd\}}zD td    j| j W d Q R X  	d j
  W d |  X d S )Nc                  s   t  jdS )N)r!   )rG   r!   r   )r$   r   r   r   '	  rT   z;SendfileMixin.test_sendfile_not_supported.<locals>.<lambda>)r   znot supportedr   )r  r!   r  r   rF  rB  rm   sendfiler
  r}   r  r)   )r$   r   r   r   )r$   r   test_sendfile_not_supported#	  s    
z)SendfileMixin.test_sendfile_not_supportedc             C   s   |   \}}| | j|j| j}|j  | |j | |t	| j
 | |jt	| j
 | |j| j
 | | j t	| j
 d S )N)r*  r  r!   r+  r0   r
  r)   r5   r}   r;   r  r2   r<   r  )r$   r(  r)  r  r   r   r   test_sendfile2	  s    
zSendfileMixin.test_sendfilec                s      \}} fdd}| j_  j|j j}|j   |j  	|t
 j  	|jt
 j  	|j j  	 j t
 j d S )Nc                s   t j j| |||S )N)r   BaseEventLoop_sendfile_nativer!   )r  r
  offsetcount)r$   r   r   sendfile_native@	  s    zCSendfileMixin.test_sendfile_force_fallback.<locals>.sendfile_native)r*  r!   r/  r  r+  r0   r
  r)   r5   r}   r;   r  r2   r<   r  )r$   r(  r)  r2  r  r   )r$   r   test_sendfile_force_fallback=	  s    
z*SendfileMixin.test_sendfile_force_fallbackc          	      s   t jdkr"t jtjr" d   \}} fdd}| j_ 	t
jd"   jj|j jdd W d Q R X |j   |j  |jd   j d d S )	Nr   zFails on proactor event loopc                s   t j j| |||S )N)r   r.  r/  r!   )r  r
  r0  r1  )r$   r   r   r2  V	  s    zMSendfileMixin.test_sendfile_force_unsupported_native.<locals>.sendfile_nativeznot supportedF)fallbackr   )r   r   r   r!   r   ProactorEventLoopr  r*  r/  rB  r   ZSendfileNotAvailableErrorr  r+  r0   r
  r)   r5   r}   r2   r  )r$   r(  r)  r2  r   )r$   r   &test_sendfile_force_unsupported_nativeP	  s    


z4SendfileMixin.test_sendfile_force_unsupported_nativec             C   s   | j dd\}}| | j|j| j}|j  | |j | |t	| j
 | |jt	| j
 | |j| j
 | | j t	| j
 d S )NT)r'  )r*  r  r!   r+  r0   r
  r)   r5   r}   r;   r  r2   r<   r  )r$   r(  r)  r  r   r   r   test_sendfile_sslh	  s    
zSendfileMixin.test_sendfile_sslc          	   C   sv   |   \}}|j  | td | | j|j| j W d Q R X | |j	 | 
|jd | 
| j d d S )Nz
is closingr   )r*  r0   r)   rB  rm   r  r!   r+  r
  r5   r}   r2   r  )r$   r(  r)  r   r   r    test_sendfile_for_closing_transps	  s    
"z.SendfileMixin.test_sendfile_for_closing_transpc             C   s   |   \}}dd }dd }|j| | | j|j| j}|j| |j  | |j | 	|t
| j | 	|j|| j |  | 	| j t
| j d S )Ns   PREFIX__i   s   --SUFFIX)r*  r0   rD   r  r!   r+  r
  r)   r5   r}   r;   r  r<   r  )r$   r(  r)  PREFIXSUFFIXr  r   r   r   test_sendfile_pre_and_post_data|	  s    
z-SendfileMixin.test_sendfile_pre_and_post_datac             C   s   | j dd\}}dd }dd }|j| | | j|j| j}|j| |j  | |j | 	|t
| j | 	|j|| j |  | 	| j t
| j d S )NT)r'  s   zxcvbnmi   s
   0987654321)r*  r0   rD   r  r!   r+  r
  r)   r5   r}   r;   r  r<   r  )r$   r(  r)  r9  r:  r  r   r   r   #test_sendfile_ssl_pre_and_post_data	  s    
z1SendfileMixin.test_sendfile_ssl_pre_and_post_datac             C   s   |   \}}| | j|j| jdd}|j  | |j | |d | |j	d | |j
| jdd  | | j d d S )Ni  d   iL  )r*  r  r!   r+  r0   r
  r)   r5   r}   r2   r<   r  r  )r$   r(  r)  r  r   r   r   test_sendfile_partial	  s    
z#SendfileMixin.test_sendfile_partialc             C   s   | j dd\}}| | j|j| jdd}|j  | |j | |d | |j	d | |j
| jdd  | | j d d S )NT)r'  i  r=  iL  )r*  r  r!   r+  r0   r
  r)   r5   r}   r2   r<   r  r  )r$   r(  r)  r  r   r   r   test_sendfile_ssl_partial	  s    
z'SendfileMixin.test_sendfile_ssl_partialc             C   s   | j t| jd\}}| | j|j| j}|j  | |j	 | 
|t| j | 
|jt| j | 
|j| j | 
| j t| j d S )N)r%  )r*  r;   r  r  r!   r+  r0   r
  r)   r5   r}   r2   r<   r  )r$   r(  r)  r  r   r   r   (test_sendfile_close_peer_after_receiving	  s    
z6SendfileMixin.test_sendfile_close_peer_after_receivingc             C   s   | j dt| jd\}}| | j|j| j}| |j | 	|t| j | 	|j
t| j | 	|j| j | 	| j t| j d S )NT)r'  r%  )r*  r;   r  r  r!   r+  r0   r
  r5   r}   r2   r<   r  )r$   r(  r)  r  r   r   r   ,test_sendfile_ssl_close_peer_after_receiving	  s    z:SendfileMixin.test_sendfile_ssl_close_peer_after_receivingc          	   C   s   | j dd\}}| t | | j|j| j W d Q R X | |j | 	d|j
  koht| jk n  |j
 | 	d| j   kot| jk n  | j  | 	|j  d S )Ni   )r%  )r*  rl   ConnectionErrorr  r!   r+  r0   r
  r5   rh   r2   r;   r  r  
is_closing)r$   r(  r)  r   r   r   3test_sendfile_close_peer_in_the_middle_of_receiving	  s    "&zASendfileMixin.test_sendfile_close_peer_in_the_middle_of_receivingc          	      s    fdd}| j _ jdd\}} t   j |j j W d Q R X  |j	  
d|j  ko|t jk n  |j  
d j   kot jk n   j  d S )Nc                s   t j j| |||S )N)r   r.  r/  r!   )r  r
  r0  r1  )r$   r   r   r2  	  s    zcSendfileMixin.test_sendfile_fallback_close_peer_in_the_middle_of_receiving.<locals>.sendfile_nativei   )r%  )r!   r/  r*  rl   rB  r  r+  r0   r
  r5   rh   r2   r;   r  r  )r$   r2  r(  r)  r   )r$   r   <test_sendfile_fallback_close_peer_in_the_middle_of_receiving	  s    "&zJSendfileMixin.test_sendfile_fallback_close_peer_in_the_middle_of_receivingr+  z"Don't have native sendfile supportc          	      s     \} j  fdd}j| } td  jd W d Q R X |}	|t
j d S )Nc                  s"    d  j jjI d H S )N)r9   r!   r+  r0   r
  r   )r)  r  r$   r   r   rC  	  s    
z=SendfileMixin.test_sendfile_prevents_bare_write.<locals>.corozsendfile is in progresss   data)r*  r!   r  r  r  rB  rm   r0   rD   r}   r;   r  )r$   r(  rC  r   r  r   )r)  r  r$   r   !test_sendfile_prevents_bare_write	  s    


z/SendfileMixin.test_sendfile_prevents_bare_writec          	   C   sT   t  }dd |j_tjj|_| t	d  | j
| j
j|d dd W d Q R X d S )Nc               S   s   dS )NFr   r   r   r   r   r   	  rT   zPSendfileMixin.test_sendfile_no_fallback_for_fallback_transport.<locals>.<lambda>zfallback is disabledF)r4  )r   rP  rC  r  r   Z_SendfileModeZFALLBACKZ_sendfile_compatiblerB  rm   r!   r    r+  )r$   r0   r   r   r   0test_sendfile_no_fallback_for_fallback_transport	  s    
z>SendfileMixin.test_sendfile_no_fallback_for_fallback_transportN)r+   r,   r-   r.   r&  r*  r<  r  r   r   r,  r-  r3  r6  r7  r8  r;  r<  r>  r?  r@  rA  rD  rE  r  r   rF  rG  r   r   r   r   r$    s&   +	r$  c               @   s   e Zd Zdd ZdS )SelectEventLoopTestsc             C   s   t  S )N)r   SelectorEventLoop)r$   r   r   r   rb   
  s    z&SelectEventLoopTests.create_event_loopN)r+   r,   r-   rb   r   r   r   r   rH  
  s   rH  c               @   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 )ProactorEventLoopTestsc             C   s   t  S )N)r   r5  )r$   r   r   r   rb   
  s    z(ProactorEventLoopTests.create_event_loopc             C   s   t dd S )Nz(IocpEventLoop does not have add_reader())r<  r=  )r$   r   r   r   r   
  s    z+ProactorEventLoopTests.test_reader_callbackc             C   s   t dd S )Nz(IocpEventLoop does not have add_reader())r<  r=  )r$   r   r   r   test_reader_callback_cancel
  s    z2ProactorEventLoopTests.test_reader_callback_cancelc             C   s   t dd S )Nz(IocpEventLoop does not have add_writer())r<  r=  )r$   r   r   r   r   
  s    z+ProactorEventLoopTests.test_writer_callbackc             C   s   t dd S )Nz(IocpEventLoop does not have add_writer())r<  r=  )r$   r   r   r   test_writer_callback_cancel
  s    z2ProactorEventLoopTests.test_writer_callback_cancelc             C   s   t dd S )Nz6IocpEventLoop does not have create_datagram_endpoint())r<  r=  )r$   r   r   r   r   
  s    z4ProactorEventLoopTests.test_create_datagram_endpointc             C   s   t dd S )Nz(IocpEventLoop does not have add_reader())r<  r=  )r$   r   r   r   r  $
  s    z4ProactorEventLoopTests.test_remove_fds_after_closingN)
r+   r,   r-   rb   r   rK  r   rL  r  r  r   r   r   r   rJ  
  s   rJ  c                   s(   e Zd Z fddZ fddZ  ZS )UnixEventLoopTestsMixinc                s,   t    t }|| j t| d S )N)rC   ra   r   SafeChildWatcherattach_loopr!   set_child_watcher)r$   watcher)rE   r   r   ra   ,
  s    
zUnixEventLoopTestsMixin.setUpc                s   t d  t   d S )N)r   rP  rC   rf   )r$   )rE   r   r   rf   2
  s    
z UnixEventLoopTestsMixin.tearDown)r+   r,   r-   ra   rf   rF   r   r   )rE   r   rM  )
  s   rM  KqueueSelectorc                   s\   e Zd Zdd Zeddeej	
dd fddZedd fd	d
Z  ZS )KqueueEventLoopTestsc             C   s   t t S )N)r   rI  	selectorsrR  )r$   r   r   r   rb   <
  s    z&KqueueEventLoopTests.create_event_loopr   	   Zopenbsdztest hangs on OpenBSDc                s   t    d S )N)rC   r  )r$   )rE   r   r   r  B
  s    z)KqueueEventLoopTests.test_read_pty_outputc                s   t    d S )N)rC   r  )r$   )rE   r   r   r  L
  s    z#KqueueEventLoopTests.test_write_pty)r+   r,   r-   rb   r   r  r<  r  r   r   r   r  r  rF   r   r   )rE   r   rS  8
  s
   
rS  EpollSelectorc               @   s   e Zd Zdd ZdS )EPollEventLoopTestsc             C   s   t t S )N)r   rI  rT  rV  )r$   r   r   r   rb   U
  s    z%EPollEventLoopTests.create_event_loopN)r+   r,   r-   rb   r   r   r   r   rW  Q
  s   rW  PollSelectorc               @   s   e Zd Zdd ZdS )PollEventLoopTestsc             C   s   t t S )N)r   rI  rT  rX  )r$   r   r   r   rb   ]
  s    z$PollEventLoopTests.create_event_loopN)r+   r,   r-   rb   r   r   r   r   rY  Y
  s   rY  c               @   s   e Zd Zdd ZdS )rH  c             C   s   t t S )N)r   rI  rT  ZSelectSelector)r$   r   r   r   rb   e
  s    z&SelectEventLoopTests.create_event_loopN)r+   r,   r-   rb   r   r   r   r   rH  a
  s   c              O   s   d S )Nr   )r   rb  r   r   r   noopi
  s    rZ  c                   sh   e Zd Z fddZdd Zdd Zdd Zd	d
 Zdd Zdd Z	e
eejdddd Z  ZS )HandleTestsc                s"   t    t | _d| jj_d S )NT)rC   ra   r   rP  r!   	get_debugreturn_value)r$   )rE   r   r   ra   o
  s    

zHandleTests.setUpc             C   s`   dd }d}t ||| j}| |j| | |j| | |  |  | 	|  d S )Nc              W   s   | S )Nr   )r   r   r   r   ry   u
  s    z)HandleTests.test_handle.<locals>.callbackr   )
r   Handler!   r   	_callback_argsr   r  r   rh   )r$   ry   r   hr   r   r   test_handlet
  s    zHandleTests.test_handlec             C   s\   dd }t  | _t  | j_t|d| j}|  | jjt	dt j
||jd d S )Nc               S   s
   t  d S )N)r   r   r   r   r   ry   
  s    z:HandleTests.test_callback_with_exception.<locals>.callbackr   zException in callback.*)r5  r  ZhandleZsource_traceback)r   rP  r!   ru  r   r^  Z_runassert_called_withrd   ZMockPatternANY_source_traceback)r$   ry   ra  r   r   r   test_callback_with_exception
  s    
z(HandleTests.test_callback_with_exceptionc             C   s(   t  }tdd d| j}||d< d S )Nc               S   s   d S )Nr   r   r   r   r   r   
  rT   z1HandleTests.test_handle_weakref.<locals>.<lambda>r   ra  )weakrefZWeakValueDictionaryr   r^  r!   )r$   Zwdra  r   r   r   test_handle_weakref
  s    zHandleTests.test_handle_weakrefc             C   sx  d| j j_ttd| j }tt\}}| t	|d||f  |
  | t	|d tt}t|d| j }| t	|d||f  ttdd}t|d	| j }d
t||f }| t	|| tjtdd}t|d| j }dt||f }| t	|| tjdkrttj}t|}t|\}}t|d| j }d}d| }d|t||f }| t	|| d S )NF)rU   rV   z<Handle noop(1, 2) at %s:%s>z<Handle cancelled>r   z<Handle noop() at %s:%s>rU   rV   )rS   z%^<Handle noop\(1, 2\)\(3\) at %s:%s>$)x)rV   rS   z'^<Handle noop\(x=1\)\(2, 3\) at %s:%s>$)rS      z*<function HandleTests.test_handle_repr .*>z%functools.partialmethod\(%s, , \)\(\)z^<Handle %s at %s:%s>$)r!   r\  r]  r   r^  rZ  rd   get_function_sourcer}   r  r   rk   r  r   reescapeZassertRegexr   r;  r[  test_handle_reprpartialmethod)r$   ra  filenamelinenoru   ZregexmethodZcb_regexr   r   r   rn  
  sD    





zHandleTests.test_handle_reprc             C   s   d| j j_t}t jd }tt	d| j }t
t	\}}| t|d||||f  |  | t|d||||f  |  | t|d||||f  d S )NTrU   )rU   rV   z-<Handle noop(1, 2) at %s:%s created at %s:%s>z7<Handle cancelled noop(1, 2) at %s:%s created at %s:%s>)r!   r\  r]  r  r   	_getframef_linenor   r^  rZ  rd   rk  r}   r  r   )r$   create_filenamecreate_linenora  rp  rq  r   r   r   test_handle_repr_debug
  s$    

z"HandleTests.test_handle_repr_debugc                s|   t   }|d  |  fdd}|t}|| |t}|| |dt}|| |dt}|| d S )NTc                sB   t djd } | jt  | jd d d t|df d S )NrU   r   rS   test_handle_source_traceback)r   rs  rt  r   re  listr}   r  )ra  rq  )r$   r   r   check_source_traceback
  s    zHHandleTests.test_handle_source_traceback.<locals>.check_source_tracebackr   )	r   get_event_loop_policyr   r   r   r   rZ  r   rz   )r$   r!   rz  ra  r   )r$   r   rx  
  s    

	

z(HandleTests.test_handle_source_traceback	CoroutinezNo collections.abc.Coroutinec             C   s   t  }d|_| t| | t|d d|_| t|d d|_	| t|d d  |_|_| t|d t  }d|_| t|d	 t  }d|_d |_
| t|d d S )
NZAAAzAAA()ZBBBzBBB()TzBBB() runningz%<CoroLike without __name__>() runningr#   z
CoroLike())r#   r+   rh   r   Ziscoroutiner}   r   Z_format_coroutiner-   
cr_runningcr_code)r$   rC  r   r   r   +test_coroutine_like_object_debug_formatting
  s$    z7HandleTests.test_coroutine_like_object_debug_formatting)r+   r,   r-   ra   rb  rf  rh  rn  rw  rx  r<  r  r  collectionsabcr  rF   r   r   )rE   r   r[  m
  s   1r[  c                   sL   e Zd Z fddZdd Zdd Zdd Zd	d
 Zdd Zdd Z	  Z
S )
TimerTestsc                s   t    t | _d S )N)rC   ra   r   rP  r!   )r$   )rE   r   r   ra   #  s    
zTimerTests.setUpc             C   s8   t  }t|dd dt }| t|t| d S )Nc               S   s   dS )NFr   r   r   r   r   r   )  rT   z&TimerTests.test_hash.<locals>.<lambda>r   )ro   r{   r   TimerHandler   rP  r}   hash)r$   whenra  r   r   r   	test_hash'  s    
zTimerTests.test_hashc             C   s4   t  }t|dd dt }| ||  d S )Nc               S   s   dS )NFr   r   r   r   r   r   /  rT   z&TimerTests.test_when.<locals>.<lambda>r   )ro   r{   r   r  r   rP  r}   r  )r$   r  ra  r   r   r   	test_when-  s    
zTimerTests.test_whenc             C   s   dd }d}t  }t|||t }| |j| | |j| | 	|
  |  | |
  | |j | |j | ttjd ||| j d S )Nc              W   s   | S )Nr   )r   r   r   r   ry   4  s    z'TimerTests.test_timer.<locals>.callback)rU   rV   rS   )ro   r{   r   r  r   rP  r   r_  r`  r   r  r   rh   r{  rl   r8   r!   )r$   ry   r   r  ra  r   r   r   
test_timer3  s    
zTimerTests.test_timerc             C   sV   d| j j_tdtd| j }tt}| t	|d|  |
  | t	|d d S )NF{   r   z&<TimerHandle when=123 noop() at %s:%s>z <TimerHandle cancelled when=123>)r!   r\  r]  r   r  rZ  rd   rk  r}   r  r   )r$   ra  srcr   r   r   test_timer_reprI  s    




zTimerTests.test_timer_reprc             C   s   d| j j_t}t jd }tdt	d| j }t
t	\}}| t|d||||f  |  | t|d||||f  d S )NTrU   r  r   z7<TimerHandle when=123 noop() at %s:%s created at %s:%s>zA<TimerHandle cancelled when=123 noop() at %s:%s created at %s:%s>)r!   r\  r]  r  r   rs  rt  r   r  rZ  rd   rk  r}   r  r   )r$   ru  rv  ra  rp  rq  r   r   r   test_timer_repr_debugW  s    


z TimerTests.test_timer_repr_debugc             C   s  dd }t  }t||d| j}t||d| j}| ||k  | ||k  | ||k | ||k | ||k | ||k | ||k | ||k | ||k | ||k |  | ||k t||d| j}t|d |d| j}| ||k  | ||k  | ||k | ||k | ||k | ||k | ||k | ||k | ||k | ||k t|d| j}| 	t
|| | 	t
|| d S )Nc              W   s   | S )Nr   )r   r   r   r   ry   m  s    z2TimerTests.test_timer_comparison.<locals>.callbackr   g      $@)ro   r{   r   r  r!   r   rh   r   r^  r   NotImplemented__eq____ne__)r$   ry   r  Zh1Zh2Zh3r   r   r   test_timer_comparisonl  s>    z TimerTests.test_timer_comparison)r+   r,   r-   ra   r  r  r  r  r  r  rF   r   r   )rE   r   r  !  s   r  c               @   s   e Zd Zdd Zdd ZdS )AbstractEventLoopTestsc             C   s  t  }t }| t|j | t|jd  | t|j | t|j	 | t|j
 | t|j | t|jd  | t|jd d  | t|j|| | t|jd  | t|j | t|jd  | t|j| | t|jd| | t|jd | t|jd| | t|jd | t|jd| | t|jd | t|jd | t|j| | t|j| | t|j| | t|j | t|j| d S )NrU   )r   rP  r   AbstractEventLooprl   NotImplementedErrorr|   r    rt   ri   rc   r)   r  rz   r  r   ro   r   Zset_default_executorr   r   r   r   r   r   Zset_exception_handlerZdefault_exception_handlerru  r\  r   )r$   r   r!   r   r   r   test_not_implemented  sh    






z+AbstractEventLoopTests.test_not_implementedc                s,    fdd}t  }||  |  d S )Nc           	      s  t  } t } t || | I d H  W d Q R X  t |ddI d H  W d Q R X  t |dI d H  W d Q R X  t |	| I d H  W d Q R X  t |
| I d H  W d Q R X  t || I d H  W d Q R X  t || dI d H  W d Q R X  t || dI d H  W d Q R X  t || dI d H  W d Q R X  t || | I d H  W d Q R X  t || I d H  W d Q R X  t || | I d H  W d Q R X  t || | I d H  W d Q R X  t || t jjI d H  W d Q R X  t || t jjI d H  W d Q R X  t || t jI d H  W d Q R X  t || I d H  W d Q R X d S )Nr  i  )r  i  r   )r   rP  r   r  rl   r  r   r   Zgetnameinfor   rR  r  r   r   r   r   r   r  r+  r  sentinelr  r  r  r  )r   r!   )r$   r   r   inner  sH      z@AbstractEventLoopTests.test_not_implemented_async.<locals>.inner)r   r   r    r)   )r$   r  r!   r   )r$   r   test_not_implemented_async  s    'z1AbstractEventLoopTests.test_not_implemented_asyncN)r+   r,   r-   r  r  r   r   r   r   r    s   6r  c               @   s   e Zd Zdd ZdS )ProtocolsAbsTestsc             C   s   t  }t }| || | || | || | |  t	 }| || | || | |
| | ||| t }| || | || | |d| | |d| | |  d S )NrU   )r   rP  r   r   r{  r:   rA   r=   r?   DatagramProtocolrK   rJ   r  r]   r^   r_   )r$   r   r  Zdpspr   r   r   
test_empty  s"    zProtocolsAbsTests.test_emptyN)r+   r,   r-   r  r   r   r   r   r    s   r  c               @   s^   e Zd Zdd Zdd Zdd Zdd Zed	d
d Z	dd Z
dd Zdd Zdd ZdS )PolicyTestsc             C   sZ   t  }| t|j | t|jt  | t|j | t|j | t|j	t  d S )N)
r   AbstractEventLoopPolicyrl   r  get_event_loopr   rz  r   Zget_child_watcherrP  )r$   policyr   r   r   test_event_loop_policy  s    
z"PolicyTests.test_event_loop_policyc             C   sX   t  }| |jj | }| |t j | |jj| | ||  |	  d S )N)
r   DefaultEventLoopPolicyr{  _localZ_loopr  r   r  r   r)   )r$   r  r!   r   r   r   test_get_event_loop  s    zPolicyTests.test_get_event_loopc          	   C   sF   t  }tjj|d|jd}| }|| W d Q R X |  d S )Nr   )wraps)	r   r  r   r  rz  r   r  rc  r)   )r$   r  Zm_set_event_loopr!   r   r   r   (test_get_event_loop_calls_set_event_loop)  s    z4PolicyTests.test_get_event_loop_calls_set_event_loopc             C   s$   t  }|d  | t|j d S )N)r   r  r   rl   rm   r  )r$   r  r   r   r   "test_get_event_loop_after_set_none9  s    
z.PolicyTests.test_get_event_loop_after_set_nonez'asyncio.events.threading.current_threadc                s,    fdd}t j|d}|  |  d S )Nc                 s   t  }  t| j d S )N)r   r  rl   rm   r  )r  )r$   r   r   r   A  s    z1PolicyTests.test_get_event_loop_thread.<locals>.f)r   )r   r   r   r   )r$   Zm_current_threadr   thr   )r$   r   test_get_event_loop_thread>  s    z&PolicyTests.test_get_event_loop_threadc             C   s*   t  }| }| |t j |  d S )N)r   r  r   r   r  r)   )r$   r  r!   r   r   r   test_new_event_loopI  s    zPolicyTests.test_new_event_loopc             C   sh   t  }| }| t|jt  | }|| | ||  | 	||  |
  |
  d S )N)r   r  r  rl   r8   r   rz  r   r   assertIsNotr)   )r$   r  Zold_loopr!   r   r   r   test_set_event_loopP  s    
zPolicyTests.test_set_event_loopc             C   s*   t  }| |t j | |t   d S )N)r   r{  r   r  r   )r$   r  r   r   r   test_get_event_loop_policy]  s    z&PolicyTests.test_get_event_loop_policyc             C   sL   |  ttjt  t }t }t| | |t  | || d S )N)	rl   r8   r   set_event_loop_policyrz  r{  r  r   r  )r$   
old_policyr  r   r   r   test_set_event_loop_policyb  s    
z&PolicyTests.test_set_event_loop_policyN)r+   r,   r-   r  r  r  r  r   r  r  r  r  r  r  r   r   r   r   r    s   	r  c                   sR   e Zd ZdZdZdZdZ fddZ fddZe	j
dkrBdd Zd	d
 Z  ZS )GetEventLoopTestsMixinNc                s   t j| _t j| _t j| _t j| _t	| j
t _t	| jt _t	| jt _t	| jt _t	| j
t_t	| jt_t	| jt_t	| jt_t   t | _t| j tjdkrt }|| j t| d S )Nr   )r   _get_running_loop_get_running_loop_saved_set_running_loop_set_running_loop_savedget_running_loopget_running_loop_savedr  get_event_loop_savedr   _get_running_loop_impl_set_running_loop_implget_running_loop_implget_event_loop_implr   rC   ra   r   r!   r   r   r   rN  rO  rP  )r$   rQ  )rE   r   r   ra   u  s&    


zGetEventLoopTestsMixin.setUpc          	      s~   z"t jdkrtd  t   W d | j  td  | j	t
_| jt
_| jt
_| jt
_| j	t_| jt_| jt_| jt_X d S )Nr   )r   r   r   rP  rC   rf   r!   r)   r   r  r   r  r  r  r  r  r  r  )r$   )rE   r   r   rf     s    



zGetEventLoopTestsMixin.tearDownr   c                s0   t d  fdd}  j| d d S )Nzmultiprocessing.synchronizec                 s*   t j }  j| tI d H }|   |S )N)
concurrentZfuturesProcessPoolExecutorr!   r   r"   Zshutdown)Zpoolr  )r$   r   r   r    s
    
zDGetEventLoopTestsMixin.test_get_event_loop_new_process.<locals>.mainr   )r   import_moduler}   r!   r    )r$   r  r   )r$   r   test_get_event_loop_new_process  s
    
z6GetEventLoopTestsMixin.test_get_event_loop_new_processc          	      s  G dd dt  G  fdddtj}t }z t|  t   t  W d Q R X td    t  W d Q R X 	t
d t d  W d Q R X t d  fdd}|  t   t  W d Q R X td    t  W d Q R X W d t| d k	rN  X 	t
d t d  W d Q R X t d  d S )Nc               @   s   e Zd ZdS )zRGetEventLoopTestsMixin.test_get_event_loop_returns_running_loop.<locals>.TestErrorN)r+   r,   r-   r   r   r   r   	TestError  s   r  c                   s   e Zd Z fddZdS )zOGetEventLoopTestsMixin.test_get_event_loop_returns_running_loop.<locals>.Policyc                s    d S )Nr   )r$   )r  r   r   r    s    z^GetEventLoopTestsMixin.test_get_event_loop_returns_running_loop.<locals>.Policy.get_event_loopN)r+   r,   r-   r  r   )r  r   r   Policy  s   r  z
no runningc                  s4    t    t    t   d S )N)r   r   r  r  r  r   )r!   r$   r   r   r    s    zMGetEventLoopTestsMixin.test_get_event_loop_returns_running_loop.<locals>.func)r2  r   r  r{  r  r   rl   r  r   rB  rm   r   r  r  r    r)   )r$   r  r  r  r   )r  r!   r$   r   (test_get_event_loop_returns_running_loop  s8    





z?GetEventLoopTestsMixin.test_get_event_loop_returns_running_loop)r+   r,   r-   r  r  r  r  ra   rf   r   r   r  r  rF   r   r   )rE   r   r  n  s   
r  c               @   s$   e Zd ZejZejZejZ	ej
ZdS )TestPyGetEventLoopN)r+   r,   r-   r   Z_py__get_running_loopr  Z_py__set_running_loopr  Z_py_get_running_loopr  Z_py_get_event_loopr  r   r   r   r   r    s   r  c               @   s$   e Zd ZejZejZejZ	ej
ZdS )TestCGetEventLoopN)r+   r,   r-   r   Z_c__get_running_loopr  Z_c__set_running_loopr  Z_c_get_running_loopr  Z_c_get_event_loopr  r   r   r   r   r    s   r  c               @   s   e Zd Zdd ZdS )
TestServerc                sb   t  }| |j t| || fdddd}| | | |  ||	  d S )Nc                  s    S )Nr   r   )r   r   r   r     rT   z*TestServer.test_get_loop.<locals>.<lambda>z0.0.0.0r   )
r   r   rA  r)   rB   r    rR  r}   get_loopr  )r$   r!   rT  r   )r   r   test_get_loop  s    zTestServer.test_get_loopN)r+   r,   r-   r  r   r   r   r   r    s   r  c               @   s$   e Zd Zdd Zdd Zdd ZdS )TestAbstractServerc          	   C   s&   |  t t   W d Q R X d S )N)rl   r  r   AbstractServerr)   )r$   r   r   r   r    s    zTestAbstractServer.test_closec          	   C   s@   t  }| |j | t |t 	  W d Q R X d S )N)
r   r   rA  r)   rl   r  r    r   r  r  )r$   r!   r   r   r   test_wait_closed  s    z#TestAbstractServer.test_wait_closedc          	   C   s&   |  t t   W d Q R X d S )N)rl   r  r   r  r  )r$   r   r   r   r    s    z TestAbstractServer.test_get_loopN)r+   r,   r-   r  r  r  r   r   r   r   r  	  s   r  __main__)M__doc__Zcollections.abcr  concurrent.futuresr  r  r  r   r   rl  r   r   r
  ImportErrorr  r   r!  r   ro   r*  r<  r   rg  r  r   r   r   r   r   r   r	   Ztest.test_asyncior
   rd   r  r   r   r"   r#   r   r.   rB   r  rG   rL   ZBaseProtocolrN   r  rO   r`   r  r  r  r$  ZTestCaserH  rJ  rT  rM  r  rS  rW  rY  rZ  r[  r  r  r  r  r  r  Z_asyncior  r  r  r+   r  r   r   r   r   <module>   s   

	" )            T     
 5tf[y
