B
    18\I                @   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.aixTdarwinFr   .)
      )sysplatform
startswithmac_vertuplemapintsplit)version r   a/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/test/test_asyncio/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 )Nr1   	CONNECTED)r2   r3   AssertionErrorr6   
set_result)r&   r2   r   r   r   connection_madeV   s
    zMyBaseProto.connection_madec             C   s*   | j dkst| j |  jt|7  _d S )Nr9   )r3   r:   r4   len)r&   datar   r   r   data_received]   s    zMyBaseProto.data_receivedc             C   s   | j dkst| j d| _ d S )Nr9   EOF)r3   r:   )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)r9   r@   CLOSED)r3   r:   r7   r;   )r&   r)   r   r   r   connection_loste   s    zMyBaseProto.connection_lost)N)
r-   r.   r/   r6   r7   r8   r<   r?   rA   rC   r   r   r   r   r0   J   s   
r0   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&   r2   )	__class__r   r   r<   m   s    zMyProto.connection_made)r-   r.   r/   r<   __classcell__r   r   )rG   r   rD   l   s   rD   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 )Nr1   r   )r#   )r3   r4   r   r5   r7   )r&   r#   r   r   r   r8   u   s    zMyDatagramProto.__init__c             C   s$   || _ | jdkst| jd| _d S )Nr1   INITIALIZED)r2   r3   r:   )r&   r2   r   r   r   r<   {   s    zMyDatagramProto.connection_madec             C   s*   | j dkst| j |  jt|7  _d S )NrJ   )r3   r:   r4   r=   )r&   r>   addrr   r   r   datagram_received   s    z!MyDatagramProto.datagram_receivedc             C   s   | j dkst| j d S )NrJ   )r3   r:   )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 )NrJ   rB   )r3   r:   r7   r;   )r&   r)   r   r   r   rC      s    zMyDatagramProto.connection_lost)N)	r-   r.   r/   r7   r8   r<   rL   rM   rC   r   r   r   r   rI   r   s   
rI   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 )Nr1   r   )r#   )r3   r4   r2   r   r5   r7   )r&   r#   r   r   r   r8      s
    zMyReadPipeProto.__init__c             C   s,   || _ | jdgkst| j| jd d S )Nr1   r9   )r2   r3   r:   append)r&   r2   r   r   r   r<      s    zMyReadPipeProto.connection_madec             C   s.   | j ddgkst| j |  jt|7  _d S )Nr1   r9   )r3   r:   r4   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 )Nr1   r9   r@   )r3   r:   rO   )r&   r   r   r   rA      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@   r1   r9   rB   )r3   rO   r:   r7   r;   )r&   r)   r   r   r   rC      s    
zMyReadPipeProto.connection_lost)N)	r-   r.   r/   r7   r8   r<   r?   rA   rC   r   r   r   r   rN      s   
rN   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 )Nr1   )r#   )r3   r2   r   r5   r7   )r&   r#   r   r   r   r8      s    zMyWritePipeProto.__init__c             C   s$   || _ | jdkst| jd| _d S )Nr1   r9   )r2   r3   r:   )r&   r2   r   r   r   r<      s    z MyWritePipeProto.connection_madec             C   s0   | j dkst| j d| _ | jr,| jd  d S )Nr9   rB   )r3   r:   r7   r;   )r&   r)   r   r   r   rC      s    z MyWritePipeProto.connection_lost)N)r-   r.   r/   r7   r8   r<   rC   r   r   r   r   rP      s   
rP   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 )Nr1   )r#   c                s   i | ]}t j d |qS ))r#   )r   r5   ).0fd)r#   r   r   
<dictcomp>   s    z1MySubprocessProtocol.__init__.<locals>.<dictcomp>       )      )r3   r2   r   r5   r6   	completedrangedisconnectsr>   
returncodeEventgot_data)r&   r#   r   )r#   r   r8      s    
zMySubprocessProtocol.__init__c             C   s0   || _ | jdkst| jd| _| jd  d S )Nr1   r9   )r2   r3   r:   r6   r;   )r&   r2   r   r   r   r<      s    z$MySubprocessProtocol.connection_madec             C   s*   | j dkst| j d| _ | jd  d S )Nr9   rB   )r3   r:   rY   r;   )r&   r)   r   r   r   rC      s    z$MySubprocessProtocol.connection_lostc             C   s8   | j dkst| j | j|  |7  < | j|   d S )Nr9   )r3   r:   r>   r^   set)r&   rS   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 )Nr9   )r3   r:   r[   set_exceptionr;   )r&   rS   r)   r   r   r   pipe_connection_lost   s    z)MySubprocessProtocol.pipe_connection_lostc             C   s$   | j dkst| j | j | _d S )Nr9   )r3   r:   r2   get_returncoder\   )r&   r   r   r   process_exited   s    z#MySubprocessProtocol.process_exitedN)	r-   r.   r/   r8   r<   rC   r`   rb   rd   r   r   r   r   rQ      s   rQ   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)rE   setUpcreate_event_loopr#   r!   )r&   )rG   r   r   rf      s    

zEventLoopTestsMixin.setUpc                s4   | j  st| j  |   t  t   d S )N)	r#   	is_closed
test_utilsrun_briefly
doCleanupsr   
gc_collectrE   tearDown)r&   )rG   r   r   rm      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   )rn   r&   r   r   coro2  s    zBEventLoopTestsMixin.test_run_until_complete_nesting.<locals>.coro2)r   	coroutineassertRaisesRuntimeErrorr#   r"   )r&   rq   r   )rn   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   sleepro   )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   rw   r   )r&   r   r   cb  s    
z?EventLoopTestsMixin.test_run_until_complete_stopped.<locals>.cb)rs   rt   r#   r"   )r&   r|   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)rO   r#   r{   )arg)resultsr&   r   r   callback  s    
z5EventLoopTestsMixin.test_call_later.<locals>.callbackg?zhello worldg{Gz?g?)r#   
call_laterrv   	monotonicrun_foreverassertEqualro   )r&   r   rx   ry   r   )r   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)rO   r#   r{   )Zarg1Zarg2)r   r&   r   r   r   ,  s    z4EventLoopTestsMixin.test_call_soon.<locals>.callbackr   world)r   r   )r#   	call_soonr   r   )r&   r   r   )r   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 )NrX   )rO   r=   r#   r{   )r   )r   r&   r   r   r   8  s    
z?EventLoopTestsMixin.test_call_soon_threadsafe.<locals>.callbackc                  s   j  d   d S )Nr   )r#   call_soon_threadsafereleaser   )r   lockr&   r   r   run_in_thread=  s    zDEventLoopTestsMixin.test_call_soon_threadsafe.<locals>.run_in_thread)targetr   r   )
	threadingLockacquireThreadstartr#   r   r   joinr   )r&   r   tr   )r   r   r   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 )NrX   )rO   r=   r#   r{   )r   )r   r&   r   r   r   N  s    
zKEventLoopTestsMixin.test_call_soon_threadsafe_same_thread.<locals>.callbackr   r   )r#   r   r   r   r   )r&   r   r   )r   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)r   r   r   r   runY  s    z5EventLoopTestsMixin.test_run_in_executor.<locals>.runZyo)r#   run_in_executorr"   r   assertNotEqualr   r   )r&   r   f2res	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?)rv   rw   r   r   r   r   r   g  s    z<EventLoopTestsMixin.test_run_in_executor_cancel.<locals>.rung?)	r#   r   cancelr+   r   r   rv   rw   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extendro   r#   remove_readerfilenor+   )r>   )
bytes_readrr&   r   r   readerw  s    z8EventLoopTestsMixin.test_reader_callback.<locals>.readers   abcc                  s   t  dkS )NrU   )r=   r   )r   r   r   <lambda>  rV   z:EventLoopTestsMixin.test_reader_callback.<locals>.<lambda>s   defc                  s   t  dkS )N   )r=   r   )r   r   r   r     rV   s   abcdef)socket
socketpairsetblocking	bytearrayr#   
add_readerr   r   r(   ri   	run_untilr+   r{   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#   r{   )r>   )r&   r   r   r   writer  s    
z8EventLoopTestsMixin.test_writer_callback.<locals>.writers   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxrX   )r   r   r   r#   
add_writerr   r   ro   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   rs   
ValueErrorr"   sock_connectaddresssock_sendall	sock_recvsock_recv_intor   sock_acceptr+   ro   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+   ro   r   )r&   r   r   r>   bufr4   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)ri   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)ri   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   rs   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   rW   r   )r   r   r   listenconnectr   r#   r   r"   r   
gettimeoutgetpeernamer+   )r&   ZlistenerclientfconnrK   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 )NrW   r   r   )caughtr   r   
my_handler  s    z?EventLoopTestsMixin.test_add_signal_handler.<locals>.my_handlerZboomrW   c                  s    S )Nr   r   )r   r   r   r   +  rV   z=EventLoopTestsMixin.test_add_signal_handler.<locals>.<lambda>)rs   	TypeErrorr#   add_signal_handlerremove_signal_handlerr   signalNSIGrt   r   r   SIGINToskillgetpidri   r   ro   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 )NrW   )r#   r{   r   )r   r&   r   r   r   9  s    zLEventLoopTestsMixin.test_signal_handling_while_selecting.<locals>.my_handlerg{Gz?<   rW   )
r#   r   r   r   	setitimerITIMER_REALr   r{   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 )NrW   )r   r#   r{   )r   )r   r&   	some_argsr   r   r   J  s    zAEventLoopTestsMixin.test_signal_handling_args.<locals>.my_handlerg?r   rW   )
r#   r   r   r   r   r   r   r{   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assertIsr2   assertIsNotNoneget_extra_infor7   assertGreaterr4   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#   )rD   r#   r   )r&   r   r   r   e  rV   z<EventLoopTestsMixin.test_create_connection.<locals>.<lambda>)ri   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#   )rD   r#   r   )r&   r   r   r   p  rV   zAEventLoopTestsMixin.test_create_unix_connection.<locals>.<lambda>)r   ri   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#   )rD   r#   r   )r&   r   r   r     rV   zAEventLoopTestsMixin.test_create_connection_sock.<locals>.<lambda>)r   r   )ri   r   r#   r"   getaddrinfor   r   SOCK_STREAMr   r   r:   r  r   r   r   r   r7   r   r4   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cipherrU   r   rW   rX   
ssl_objectcompression)r   r   r   r   r   r=   strr   r  r  getpeercert)r&   r   r   r  r  r  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   ro   rG   r-   lowerr  r7   r   r4   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_AUTHri   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_effectTrW   CERTIFICATE_VERIFY_FAILED)ri   r"  r  hasattrr  r  r!  r   patchr   
call_countrs   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#   )rD   r#   r   )r&   r   r   r     rV   z@EventLoopTestsMixin.test_create_ssl_connection.<locals>.<lambda>)r  )ri   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)r0  c                  s   t  jdS )N)r#   )rD   r#   r   )r&   r   r   r     rV   zEEventLoopTestsMixin.test_create_ssl_unix_connection.<locals>.<lambda>z	127.0.0.1)server_hostname)r  )	r   ri   r   r1  r2  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#   )rD   r#   r   )r&   r   r   r     rV   zGEventLoopTestsMixin.test_create_connection_local_addr.<locals>.<lambda>
local_addrr   r   rW   )ri   r   r   find_unused_portr#   r  r   r"   r2   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#   )rD   r#   r   )r&   r   r   r   	  rV   zNEventLoopTestsMixin.test_create_connection_local_addr_in_use.<locals>.<lambda>r6  )ri   r   r#   r  r   rs   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)rE   rC   r   r{   )r&   r)   )rG   r#   r   r   rC     s    zQEventLoopTestsMixin.test_connect_accepted_socket.<locals>.MyProto.connection_lostc                s   t  | | j d S )N)rE   r?   r2   rF   )r&   r>   )rG   expected_responser   r   r?     s    zOEventLoopTestsMixin.test_connect_accepted_socket.<locals>.MyProto.data_received)r-   r.   r/   rC   r?   rH   r   )rA  r#   )rG   r   rD     s   rD   )z	127.0.0.1r   rW   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   wrap_socketr   sendallr   r+   	Exceptionprint)csockr)   )rK   
client_sslmessageresponser   r   r   &  s    



z@EventLoopTestsMixin.test_connect_accepted_socket.<locals>.clientT)r   daemon)r#   c                  s    S )Nr   r   )r  r   r   r   =  rV   zBEventLoopTestsMixin.test_connect_accepted_socket.<locals>.<lambda>)r  )timeoutrB   )r#   r0   r   r   r   r   r   r   r   acceptr"   connect_accepted_socketr   r2   r+   r   join_threadr   is_aliver   r3   r4   r=   )	r&   Z
server_sslrH  rD   Zlsockr   threadr   _r   )rK   rH  rA  r#   rI  r  rJ  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   )rU   r   z=SSL not supported with proactor event loops before Python 3.5)r   r   version_infor   r#   r   r   unittestSkipTestri   simple_server_sslcontextsimple_client_sslcontextrS  )r&   server_context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 )NrW   )ssl_handshake_timeoutz1ssl_handshake_timeout is only meaningful with ssl)	r   
addCleanupr+   r#   rN  rD   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  )hostr8  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   )rR   rc  r   r   r   
<listcomp>q  s    zDEventLoopTestsMixin.create_server_multiple_hosts.<locals>.<listcomp>c             S   s   g | ]}|d ddfqS )rh  r   r   )rR   rc  r   r   r   ri  t  s    c                  s
   t  jS )N)rD   r#   r   )r&   r   r   r   x  rV   zBEventLoopTestsMixin.create_server_multiple_hosts.<locals>.<lambda>rh  c             S   s   h | ]}|  d  qS )r   )getsockbyname)rR   r   r   r   r   	<setcomp>{  s    zCEventLoopTestsMixin.create_server_multiple_hosts.<locals>.<setcomp>)r   rr   r_   r   rb  rj  r$  r#   r  r   Mock_start_serving_stop_servingcreate_serverr"   r]  r+   socketsr   )	r&   r
  hostsZ	mock_sockrg  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)rs  r   rb  )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)rs  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     rV   z8EventLoopTestsMixin.test_create_server.<locals>.<lambda>z0.0.0.0r   rW   z	127.0.0.1s   xxxr9   c                  s
    j dkS )Nr   )r4   r   )r  r   r   r     rV   rU   r   r  rB   )rD   r#   ro  r"   r   r=   rp  r   r   r   rD  r6   r3   ri   r   r4   r   r2   r   r+   r7   )r&   r   rr  r   rc  r8  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     rV   zCEventLoopTestsMixin.test_create_server_reuse_port.<locals>.<lambda>z0.0.0.0r   rW   c                  s    S )Nr   r   )r  r   r   r     rV   T)
reuse_port)rD   r#   ro  r"   r   r=   rp  r   
getsockoptr   
SOL_SOCKETrx  r+   ri   rj   ro   )r&   r   rr  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     rV   z7EventLoopTestsMixin._make_unix_server.<locals>.<lambda>)ri   gen_unix_socket_pathr]  r#   create_unix_serverr"   )r&   factorykwargsr   rr  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     rV   z=EventLoopTestsMixin.test_create_unix_server.<locals>.<lambda>rW   s   xxxr9   c                  s
    j dkS )Nr   )r4   r   )r  r   r   r     rV   rU   rB   )rD   r#   r  r   r=   rp  r   r   r   rD  r"   r6   r3   ri   r   r4   r2   r+   r7   )r&   rr  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     rV   zOEventLoopTestsMixin.test_create_unix_server_path_socket_error.<locals>.<lambda>z/test)r   z3path and sock can not be specified at the same time)rD   r#   r   r  r^  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  
SSLContextPROTOCOL_TLS_SERVERoptionsOP_NO_SSLv2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  )r  r#   ro  r"   rp  r   r   )
r&   r  r  r  r  r   rr  r   rc  r8  r   r   r   _make_ssl_server  s    
z$EventLoopTestsMixin._make_ssl_serverc             C   s   |  ||}| j||dS )N)r  )r  r  )r&   r  r  r  r  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     rV   z<EventLoopTestsMixin.test_create_server_ssl.<locals>.<lambda>)r  s   xxxr9   c                  s
    j dkS )Nr   )r4   r   )r  r   r   r     rV   rU   )r  rB   )rD   r#   r  ri   ONLYCERTONLYKEYr  r0   r"  r"   rF   r6   r   r3   r   r4   r  r2   r+   r7   )r&   rr  rc  r8  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   .  rV   zAEventLoopTestsMixin.test_create_unix_server_ssl.<locals>.<lambda>ra  )r  r4  s   xxxr9   c                  s
    j dkS )Nr   )r4   r   )r  r   r   r   9  rV   rU   rB   )rD   r#   r  ri   r  r  r  r0   r"  r"   rF   r6   r   r3   r   r4   r2   r+   r7   )r&   rr  r}  r  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  rV   zJEventLoopTestsMixin.test_create_server_ssl_verify_failed.<locals>.<lambda>check_hostnameT)r  call_exception_handlerz(?i)certificate.verify.failed)rD   r#   r  ri   SIGNED_CERTFILEr  r  PROTOCOL_TLS_CLIENTr  r  CERT_REQUIREDverify_moder&  r  r  r   r'  objectr*  r^  r)  r"   rj   assertIsNoner2   r+   )r&   rr  rc  r8  sslcontext_clientr  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  rV   zOEventLoopTestsMixin.test_create_unix_server_ssl_verify_failed.<locals>.<lambda>r  Tinvalid)r  r4  r  z(?i)certificate.verify.failed)rD   r#   r  ri   r  r  r  r  r  r  r  r  r&  r  r  r   r'  r  r*  r^  r)  r"   rj   r  r2   r+   )r&   rr  r}  r  r  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     rV   zIEventLoopTestsMixin.test_create_server_ssl_match_failed.<locals>.<lambda>)r  r  T)r  r  z=IP address mismatch, certificate is not valid for '127.0.0.1')rD   r#   r  ri   r  r  r  r  r  r  r  r  load_verify_locations
SIGNING_CAr&  r  r  r   r'  r  r*  r^  CertificateErrorr"   r  r2   r+   )r&   rr  rc  r8  r  r  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     rV   zJEventLoopTestsMixin.test_create_unix_server_ssl_verified.<locals>.<lambda>)r  r  T	localhost)r  r4  )rD   r#   r  ri   r  r  r  r  r  r  r  r  r  r  r&  r  r  r"   r2   r+   r7   )r&   rr  r}  r  r  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     rV   zEEventLoopTestsMixin.test_create_server_ssl_verified.<locals>.<lambda>)r  r  Tr  )r  r4  )r  r  )rD   r#   r  ri   r  r  r  r  r  r  r  r  r  r  r&  r  r  r"   r  PEERCERTr2   r+   r7   )r&   rr  rc  r8  r  r  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)rE   r<   r;   )r&   r2   )rG   r  r   r   r<     s    zPEventLoopTestsMixin.test_create_server_sock.<locals>.TestMyProto.connection_made)r-   r.   r/   r<   rH   r   )r  )rG   r   TestMyProto  s   r  )r	  rW   )z0.0.0.0r   )r   r   z0.0.0.0z	127.0.0.1s   xxx)r   r5   r#   rD   r   r  
setsockoptr{  SO_REUSEADDRr   ro  r"   rp  r   r   r   r   r(   r+   )	r&   r  sock_obr   rr  r   rc  r8  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	  rW   )z0.0.0.0r   )r   r   )rc  r8  )r   r  r  r{  r  r   r#   ro  rD   r"   rp  r   rs   r;  r   r+  r<  r=  r+   )r&   r  r   rr  r   rc  r8  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)rE   r<   r;   )r&   r2   )rG   f_protor   r   r<     s    zVEventLoopTestsMixin.test_create_server_dual_stack.<locals>.TestMyProto.connection_made)r-   r.   r/   r<   rH   r   )r  )rG   r   r    s   r  r   )rc  r8  rW   r   z	127.0.0.1s   xxxz::1)r   r5   r#   rD   r   r7  ro  r"   r;  r<  r=  assertGreaterEqualr   r   r(   r2   r+   ru  )	r&   r  Z	try_countr8  r   rr  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#   ro  rD   r"   rp  r   r   r   r(   r+   rs   r   )r&   r   rr  r   rc  r8  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#   )rE   r8   r#   )Z
inner_self)rG   r&   r   r   r8   ?  s    zWEventLoopTestsMixin.test_create_datagram_endpoint.<locals>.TestMyDatagramProto.__init__c                s$   t  || | jd| | d S )Ns   resp:)rE   rL   r2   sendto)r&   r>   rK   )rG   r   r   rL   B  s    z`EventLoopTestsMixin.test_create_datagram_endpoint.<locals>.TestMyDatagramProto.datagram_received)r-   r.   r/   r8   rL   rH   r   )r&   )rG   r   TestMyDatagramProto>  s   r  )z	127.0.0.1r   )r6  r   rJ   c                  s   t  jdS )N)r#   )rI   r#   r   )r&   r   r   r   Q  rV   zCEventLoopTestsMixin.test_create_datagram_endpoint.<locals>.<lambda>)remote_addrs   xxxc                  s    j S )N)r4   r   )rr  r   r   r   [  rV   rU   c                  s    j S )N)r4   r   )r   r   r   r   ]  rV      rB   )rI   r#   create_datagram_endpointr"   r   r   r   r   r   r3   r   r2   r  ri   r   r4   r   r+   r7   )r&   r  r_  Zs_transportrc  r8  r2   r   )r   r&   rr  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#   )rI   r#   r   )r&   r   r   r     rV   zHEventLoopTestsMixin.test_create_datagram_endpoint_sock.<locals>.<lambda>)r   )r   r   r   r#   r   r   rU  rV  r"   r  r   
SOCK_DGRAMr   r   r:   r  r   r   r   rI   r+   r7   )r&   r   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 BaseSelectorEventLooprW   r   )rg   r   r	   BaseSelectorEventLoopr+   skipTestr   _internal_fdsr  _csock_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     rV   zEEventLoopTestsMixin.test_read_pipe.<locals>.connect.<locals>.<lambda>r1   r9   r   )r#   connect_read_piper   r2   r   r3   r4   )r   p)pipeobjr  r&   r   r   r     s    z3EventLoopTestsMixin.test_read_pipe.<locals>.connect   1c                  s
    j dkS )NrW   )r4   r   )r  r   r   r     rV   z4EventLoopTestsMixin.test_read_pipe.<locals>.<lambda>rW   s   2345c                  s
    j dkS )Nr   )r4   r   )r  r   r   r     rV   r1   r9   r   r@   rB   pipe)rN   r#   r   r  ioopenr"   rF   ri   r   r   r4   r3   r+   r7   r   r2   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     rV   zSEventLoopTestsMixin.test_unclosed_pipe_transport.<locals>.connect.<locals>.<lambda>c                  s    S )Nr   r   )write_protor   r   r     rV   )r  connect_write_pipe)read_transportrR  write_transport)r#   r  rpipeobjwpipeobjr  r   r   r     s
    zAEventLoopTestsMixin.test_unclosed_pipe_transport.<locals>.connectr  )rg   rN   rP   r   r  r  r  r"   r+   r>  repr_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     rV   zKEventLoopTestsMixin.test_read_pty_output.<locals>.connect.<locals>.<lambda>r1   r9   r   )r#   r  r   r2   r   r3   r4   )r   r  )master_read_objr  r&   r   r   r     s    z9EventLoopTestsMixin.test_read_pty_output.<locals>.connectr  c                  s    j S )N)r4   r   )r  r   r   r     rV   z:EventLoopTestsMixin.test_read_pty_output.<locals>.<lambda>rW   s   2345c                  s
    j dkS )Nr   )r4   r   )r  r   r   r     rV   r1   r9   r   r@   rB   r  )rN   r#   r   openptyr  r  r"   rF   ri   r   r   r4   r3   r+   r2   r7   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     rV   z5EventLoopTestsMixin.test_write_pipe.<locals>.<lambda>r9   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 )NrW   r   r   )r>   r   r   r   r     rV   s   2345c                  s    dkS )Nr   r   r   )r>   r   r   r   r     rV   s   12345r  rB   )r   r  r  r  rP   r#   r  r"   r   r2   r   r3   rF   r   ri   r   r+   r   r   r7   )r&   r  r  r   r2   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   ,  rV   zIEventLoopTestsMixin.test_write_pipe_disconnect_on_close.<locals>.<lambda>r9   r  rB   )r   r   r   r  r  detachrP   r#   r  r"   r   r2   r   r3   rF   r   r+   r7   )r&   ZrsockZwsockr  r   r2   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  rV   z4EventLoopTestsMixin.test_write_pty.<locals>.<lambda>r9   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 )NrW   r   r   )r>   r   r   r   r   S  rV   r   )rL  s   2345c                  s    dkS )Nr   r   r   )r>   r   r   r   r   X  rV   s   12345r  rB   )r   r  r  r  rP   r#   r  r"   r   r2   r   r3   rF   r   ri   r   r+   r   r   r7   )r&   r  slave_write_objr   r2   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  rV   z<EventLoopTestsMixin.test_bidirectional_pty.<locals>.<lambda>r1   r9   r  c                  s    S )Nr   r   )r  r   r   r   ~  rV   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 )NrW   r   r   )r>   r   r   r   r     rV   r   )rL     ac                  s
    j dkS )NrW   )r4   r   )r  r   r   r     rV   rW   s   2345c                  s    dkS )Nr   r   r   )r>   r   r   r   r     rV   s   12345s   bcdec                  s
    j dkS )Nr   )r4   r   )r  r   r   r     rV   r   r@   rB   )r   r  dupttyZsetrawr  r  rN   r#   r  r"   r   r2   r   r3   r4   rP   r  r   rF   ri   r   r+   r7   )
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 )	NFrW   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   CancelledErrorr{   )r   )r   r&   r   r   main  s    
z:EventLoopTestsMixin.test_prompt_cancellation.<locals>.main)r#   g?r  )r   r   r   r#   create_taskr   getattrro   pendingrv   r   r   re  r   
assertLessr   resultrs   r  r   rn  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 )NrW   )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   rw   )r#   )r&   r   r   wait  s    z7EventLoopTestsMixin.test_timeout_rounding.<locals>.wait   )Zclock_resolutionselector)	r#   r  r  r"   assertLessEqual_clock_resolution	_selectorrG   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     rV   zCEventLoopTestsMixin.test_remove_fds_after_closing.<locals>.<lambda>)
rg   r   r   r]  r+   r   r   r   r   r   )r&   r#   r   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     rV   z@EventLoopTestsMixin.test_add_fds_after_closing.<locals>.<lambda>)	rg   r   r   r]  r+   rs   rt   r   r   )r&   r#   r   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   rr   r#   rs   rt   r"   )r&   r  r_  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     rV   z0EventLoopTestsMixin.test_close.<locals>.<lambda>)r#   g      ?g        )r#   r+   r   rr   r]  rs   rt   r   r5   r"   r   r   r   call_atrv   r  r   r   SIGTERMr   r,   next)r&   r  funcr_  futitr   r   r   
test_close  s2    
"zEventLoopTestsMixin.test_close)T)TN)TN)NN)N)N)N)]r-   r.   r/   rf   rm   ru   rz   r~   r   r   r   r   r   r   r   r   r   r   r   r   skip_unless_bind_unix_socketr   r   r   rU  
skipUnlessr&  r   r   r   r   r  r  r  r  r  r  r/  skipIfr  r3  r5  r:  r@  rS  r[  r`  r   r'  rs  rt  rv  rw  r   r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  IPV6_ENABLEDr  r  r  r  r  r   r   r  r  r  r  r  requires_mac_verr  r  r  r  r  r   r  r
  rH   r   r   )rG   r   re      s   		$,
  

$


8
$	


 ).!!"&(E#	re   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&   r\   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&   r\   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.pyr9   r   s   Python The WinnerrW   )r   r}  r   dirname__file__r#   subprocess_execr1  r2  rQ   r   
executabler"   r   r6   r   r3   get_pipe_transportrF   r^   r  ri   r*  r+   rY   r  r\   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.pyr9   r   s   Python rW   s
   The Winners   Python The Winner)r   r}  r   r  r  r#   r  r1  r2  rQ   r   r  r"   r   r6   r   r3   r  rF   r^   r  clearr>   ri   r*  r+   rY   r  r\   )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)r7   )rR   r   r   r   r   	<genexpr>}  s    z=SubprocessTestsMixin.test_subprocess_shell.<locals>.<genexpr>rW   s   
s   PythonrX   rV   )r#   subprocess_shellr1  r2  rQ   r"   r   r6   r  r+   rY   r   r\   ro   allr[   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  r1  r2  rQ   r"   r   rY   r   r\   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   rW   rX   r&  )r#   r  r1  r2  rQ   r"   r   r  r  rY   r   r\   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  r1  r2  rQ   r   r  r"   r   r6   r   rY   r  r\   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  r1  r2  rQ   r   r  r"   r   r6   	terminaterY   r  r\   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  r1  r2  rQ   r   r  r"   r   r6   send_signalrY   r   r\   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:testrW   rX   s   ERR:test)r   r}  r   r  r  r#   r  r1  r2  rQ   r   r  r"   r   r6   r  rF   rY   r+   r   r>   ro   r   r\   )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   rW   rX   s   tests   OUT:testERR:testrV   )r   r}  r   r  r  r#   r  r1  r2  rQ   r   r  
subprocessSTDOUTr"   r   r6   r  r   r  rF   rY   ro   r>   r   r   r+   r\   )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   rW   s   tests   OUT:tests   xxxrX   r   s   ERR:BrokenPipeErrors   ERR:OSError)r   r}  r   r  r  r#   r  r1  r2  rQ   r   r  r"   r   r6   r  rF   r^   r  r   r>   r+   r[   r   ri   r*  rY   r  r\   )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%  start_new_sessionr&  )
r#   r  r1  r2  rQ   r"   r   rY   r   r\   )r&   r   rR  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)rf  )r&   r   r   r   '  s    zGSubprocessTestsMixin.test_subprocess_exec_invalid_args.<locals>.connectT)universal_newlinesi   )bufsize)shell)rs   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 )Nr7  )r#   r  r   r8  )cmdrf  )r&   r   r   r   5  s
    zHSubprocessTestsMixin.test_subprocess_shell_invalid_args.<locals>.connectZlsz-lT)r9  i   )r:  F)r;  )N)rs   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+  rU  r  r   r   r/  r0  r3  r4  r6  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   TESTFNrF   DATArE   
setUpClass)clsfp)rG   r   r   rB  M  s    zSendfileBase.setUpClassc                s   t t j t   d S )N)r   r  r@  rE   tearDownClass)rC  )rG   r   r   rE  S  s    zSendfileBase.tearDownClassc                s*   t tjd| _| | jj t   d S )Nr  )r  r   r@  filer]  r+   rE   rf   )r&   )rG   r   r   rf   X  s    zSendfileBase.setUpc             C   s   | j |S )N)r#   r"   )r&   r_  r   r   r   run_loop]  s    zSendfileBase.run_loop)r-   r.   r/   rA  BUF_SIZEclassmethodrB  rE  rf   rG  rH   r   r   )rG   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  r2   )r&   r#   r   r   r   r8   e  s
    
z"SockSendfileMixin.MyProto.__init__c             C   s   d| _ || _d S )NT)rK  r2   )r&   r2   r   r   r   r<   l  s    z)SockSendfileMixin.MyProto.connection_madec             C   s   | j | d S )N)r>   r   )r&   r>   r   r   r   r?   p  s    z'SockSendfileMixin.MyProto.data_receivedc             C   s   d| _ | jd  d S )NT)rL  r  r;   )r&   r)   r   r   r   rC   s  s    z)SockSendfileMixin.MyProto.connection_lostc                s   | j I d H  d S )N)r  )r&   r   r   r   wait_closedw  s    z%SockSendfileMixin.MyProto.wait_closedN)r-   r.   r/   r8   r<   r?   rC   rN  r   r   r   r   rD   c  s
   rD   c                s   t j| _dt _t   d S )Ni @  )r   !SENDFILE_FALLBACK_READBUFFER_SIZE_SockSendfileMixin__old_bufsizerE   rB  )rC  )rG   r   r   rB  z  s    zSockSendfileMixin.setUpClassc                s   | j t_t   d S )N)rP  r   rO  rE   rE  )rC  )rG   r   r   rE    s    zSockSendfileMixin.tearDownClassTc             C   s.   t  t jt j}|d |r*| |j |S )NF)r   rb  r  r   r]  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{  	SO_RCVBUFrH  )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)high)r  r   r{  	SO_SNDBUFrH  set_write_buffer_limits)r&   r   r2   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)rQ  c                  s    S )Nr   r   )r  r   r   r     rV   z8SockSendfileMixin.prepare_socksendfile.<locals>.<lambda>)r   z	127.0.0.1c                  s<    j d k	r" j          d S )N)r2   r+   rG  rN  r   )r  r&   rr  r   r   rQ    s
    

z7SockSendfileMixin.prepare_socksendfile.<locals>.cleanup)rD   r#   r   r7  rR  r   HOSTrG  ro  rT  r   rX  r]  )r&   r8  srv_sockr   rQ  r   )r  r&   rr  r   prepare_socksendfile  s    




z&SockSendfileMixin.prepare_socksendfilec             C   sv   |   \}}| | j|| j}|  | |  | |t| j	 | |j
| j	 | | j t| j	 d S )N)r[  rG  r#   sock_sendfilerF  r+   rN  r   r=   rA  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[  rG  r#   r\  rF  r+   rN  r   r>   rA  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[  tempfileTemporaryFilerG  r#   r\  r+   rN  r   rF  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[  rG  r#   r   r\  rF  r+   rN  r   r=   rA  r>   r]  )r&   r   r   r  r^  r9  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   rD   rI  rB  rE  rR  rT  rX  r[  r_  r`  rc  rd  rH   r   r   )rG   r   rJ  a  s   



rJ  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)rE   r8   r   r>   close_after)r&   r#   rf  )rG   r   r   r8     s    z&SendfileMixin.MySendfileProto.__init__c                s8   | j | t | | jr4| j| jkr4| j  d S )N)r>   r   rE   r?   rf  r4   r2   r+   )r&   r>   )rG   r   r   r?     s    z+SendfileMixin.MySendfileProto.data_received)Nr   )r-   r.   r/   r8   r?   rH   r   r   )rG   r   MySendfileProto  s   rg  Fr   )is_sslrf  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#   rf  zNo ssl modulec                  s    S )Nr   r   )	srv_protor   r   r   	  rV   z0SendfileMixin.prepare_sendfile.<locals>.<lambda>)r   r  )r#   c                  s    S )Nr   r   )	cli_protor   r   r   	  rV   )r   r  r4  )r2   c                  sF   j    j   j  j     d S )N)r2   r+   rG  r7   rN  r   )rj  r&   rr  ri  r   r   rQ  	  s    

z/SendfileMixin.prepare_sendfile.<locals>.cleanup)r   r7  rg  r#   r  r  ri   rW  rX  r   rb  r  r   rY  rG  ro  rT  r   r  rX  r]  )r&   rh  rf  r8  Zsrv_ctxZcli_ctxrZ  r4  Zcli_sockr  r  rQ  r   )rj  r&   rr  ri  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#   )rI   r#   r   )r&   r   r   r   &	  rV   z;SendfileMixin.test_sendfile_not_supported.<locals>.<lambda>)r
  znot supportedr   )rG  r#   r  r   rb  r^  rt   sendfilerF  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)rk  rG  r#   rl  r2   rF  r+   r7   r   r=   rA  r4   r>   r]  )r&   ri  rj  r^  r   r   r   test_sendfile1	  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  rF  offsetcount)r&   r   r   sendfile_native?	  s    zCSendfileMixin.test_sendfile_force_fallback.<locals>.sendfile_native)rk  r#   rp  rG  rl  r2   rF  r+   r7   r   r=   rA  r4   r>   r]  )r&   ri  rj  rs  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   ro  rp  r#   )r  rF  rq  rr  )r&   r   r   rs  U	  s    zMSendfileMixin.test_sendfile_force_unsupported_native.<locals>.sendfile_nativeznot supportedF)fallbackr   )r   r   r   r#   r   ProactorEventLoopr  rk  rp  r^  r   SendfileNotAvailableErrorrG  rl  r2   rF  r+   r7   r   r4   r]  )r&   ri  rj  rs  r   )r&   r   &test_sendfile_force_unsupported_nativeO	  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)rh  )rk  rG  r#   rl  r2   rF  r+   r7   r   r=   rA  r4   r>   r]  )r&   ri  rj  r^  r   r   r   test_sendfile_sslg	  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   )rk  r2   r+   r^  rt   rG  r#   rl  rF  r7   r   r4   r]  )r&   ri  rj  r   r   r    test_sendfile_for_closing_transpr	  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)rk  r2   rF   rG  r#   rl  rF  r+   r7   r   r=   rA  r>   r]  )r&   ri  rj  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)rh  s   zxcvbnmi   s
   0987654321)rk  r2   rF   rG  r#   rl  rF  r+   r7   r   r=   rA  r>   r]  )r&   ri  rj  r{  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  )rk  rG  r#   rl  r2   rF  r+   r7   r   r4   r>   rA  r]  )r&   ri  rj  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)rh  i  r  iL  )rk  rG  r#   rl  r2   rF  r+   r7   r   r4   r>   rA  r]  )r&   ri  rj  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)rf  )rk  r=   rA  rG  r#   rl  r2   rF  r+   r7   r   r4   r>   r]  )r&   ri  rj  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)rh  rf  )rk  r=   rA  rG  r#   rl  r2   rF  r7   r   r4   r>   r]  )r&   ri  rj  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   )rf  )rk  rs   ConnectionErrorrG  r#   rl  r2   rF  r7   ro   r4   r=   rA  r]  
is_closing)r&   ri  rj  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   ro  rp  r#   )r  rF  rq  rr  )r&   r   r   rs  	  s    zcSendfileMixin.test_sendfile_fallback_close_peer_in_the_middle_of_receiving.<locals>.sendfile_nativei   )rf  )r#   rp  rk  rs   r  rG  rl  r2   rF  r7   ro   r4   r=   rA  r]  )r&   rs  ri  rj  r   )r&   r   <test_sendfile_fallback_close_peer_in_the_middle_of_receiving	  s    "&zJSendfileMixin.test_sendfile_fallback_close_peer_in_the_middle_of_receivingrl  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)r;   r#   rl  r2   rF  r   )rj  r  r&   r   r   r_  	  s    
z=SendfileMixin.test_sendfile_prevents_bare_write.<locals>.corozsendfile is in progresss   data)rk  r#   rM  r  rG  r^  rt   r2   rF   r   r=   rA  )r&   ri  r_  r   r^  r   )rj  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   	  rV   zPSendfileMixin.test_sendfile_no_fallback_for_fallback_transport.<locals>.<lambda>zfallback is disabledF)ru  )r   rl  r  r$  r   _SendfileModeFALLBACK_sendfile_compatibler^  rt   r#   r"   rl  )r&   r2   r   r   r   0test_sendfile_no_fallback_for_fallback_transport	  s    
z>SendfileMixin.test_sendfile_no_fallback_for_fallback_transportN)r-   r.   r/   r0   rg  rk  rU  r  r   r   rm  rn  rt  rx  ry  rz  r}  r~  r  r  r  r  r  r  r&  r   r  r  r   r   r   r   re    s&   +	re  c               @   s   e Zd Zdd ZdS )SelectEventLoopTestsc             C   s   t  S )N)r   SelectorEventLoop)r&   r   r   r   rg   
  s    z&SelectEventLoopTests.create_event_loopN)r-   r.   r/   rg   r   r   r   r   r  
  s   r  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   rv  )r&   r   r   r   rg   
  s    z(ProactorEventLoopTests.create_event_loopc             C   s   t dd S )Nz(IocpEventLoop does not have add_reader())rU  rV  )r&   r   r   r   r   
  s    z+ProactorEventLoopTests.test_reader_callbackc             C   s   t dd S )Nz(IocpEventLoop does not have add_reader())rU  rV  )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())rU  rV  )r&   r   r   r   r   
  s    z+ProactorEventLoopTests.test_writer_callbackc             C   s   t dd S )Nz(IocpEventLoop does not have add_writer())rU  rV  )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())rU  rV  )r&   r   r   r   r  
  s    z4ProactorEventLoopTests.test_create_datagram_endpointc             C   s   t dd S )Nz(IocpEventLoop does not have add_reader())rU  rV  )r&   r   r   r   r  #
  s    z4ProactorEventLoopTests.test_remove_fds_after_closingN)
r-   r.   r/   rg   r   r  r   r  r  r  r   r   r   r   r  

  s   r  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)rE   rf   r   SafeChildWatcherattach_loopr#   set_child_watcher)r&   watcher)rG   r   r   rf   +
  s    
zUnixEventLoopTestsMixin.setUpc                s   t d  t   d S )N)r   r  rE   rm   )r&   )rG   r   r   rm   1
  s    
z UnixEventLoopTestsMixin.tearDown)r-   r.   r/   rf   rm   rH   r   r   )rG   r   r  (
  s   r  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   r  	selectorsr  )r&   r   r   r   rg   ;
  s    z&KqueueEventLoopTests.create_event_loopr   	   Zopenbsdztest hangs on OpenBSDc                s   t    d S )N)rE   r  )r&   )rG   r   r   r  A
  s    z)KqueueEventLoopTests.test_read_pty_outputc                s   t    d S )N)rE   r  )r&   )rG   r   r   r  K
  s    z#KqueueEventLoopTests.test_write_pty)r-   r.   r/   rg   r   r  rU  r  r   r   r   r  r  rH   r   r   )rG   r   r  7
  s
   
r  EpollSelectorc               @   s   e Zd Zdd ZdS )EPollEventLoopTestsc             C   s   t t S )N)r   r  r  r  )r&   r   r   r   rg   T
  s    z%EPollEventLoopTests.create_event_loopN)r-   r.   r/   rg   r   r   r   r   r  P
  s   r  PollSelectorc               @   s   e Zd Zdd ZdS )PollEventLoopTestsc             C   s   t t S )N)r   r  r  r  )r&   r   r   r   rg   \
  s    z$PollEventLoopTests.create_event_loopN)r-   r.   r/   rg   r   r   r   r   r  X
  s   r  c               @   s   e Zd Zdd ZdS )r  c             C   s   t t S )N)r   r  r  SelectSelector)r&   r   r   r   rg   d
  s    z&SelectEventLoopTests.create_event_loopN)r-   r.   r/   rg   r   r   r   r   r  `
  s   c              O   s   d S )Nr   )r   r  r   r   r   nooph
  s    r  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)rE   rf   r   rl  r#   	get_debugreturn_value)r&   )rG   r   r   rf   n
  s    

zHandleTests.setUpc             C   s`   dd }d}t ||| j}| |j| | |j| | |  |  | 	|  d S )Nc              W   s   | S )Nr   )r   r   r   r   r   t
  s    z)HandleTests.test_handle.<locals>.callbackr   )
r   Handler#   r   	_callback_argsr   r  r   ro   )r&   r   r   hr   r   r   test_handles
  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   r   
  s    z:HandleTests.test_callback_with_exception.<locals>.callbackr   zException in callback.*)rI  r+  handlesource_traceback)r   rl  r#   r  r   r  _runassert_called_withri   MockPatternANY_source_traceback)r&   r   r  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   
  rV   z1HandleTests.test_handle_weakref.<locals>.<lambda>r   r  )weakrefWeakValueDictionaryr   r  r#   )r&   wdr  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)rW   rX   z<Handle noop(1, 2) at %s:%s>z<Handle cancelled>r   z<Handle noop() at %s:%s>rW   rX   )rU   z%^<Handle noop\(1, 2\)\(3\) at %s:%s>$)x)rX   rU   z'^<Handle noop\(x=1\)\(2, 3\) at %s:%s>$)rU      z*<function HandleTests.test_handle_repr .*>z%functools.partialmethod\(%s, , \)\(\)z^<Handle %s at %s:%s>$)r#   r  r  r   r  r  ri   get_function_sourcer   r  r   rr   r1  r2  reescapeassertRegexr   rT  r  test_handle_reprpartialmethod)r&   r  filenamelinenor|   regexmethodZcb_regexr   r   r   r  
  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 )NTrW   )rW   rX   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  r  ri   r  r   r  r   )r&   create_filenamecreate_linenor  r  r  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 )NrW   r   rU   test_handle_source_traceback)r   r  r  r   r  listr   r  )r  r  )r&   r   r   check_source_traceback
  s    zHHandleTests.test_handle_source_traceback.<locals>.check_source_tracebackr   )	r   get_event_loop_policyr    r   r!   r   r  r   r   )r&   r#   r  r  r   )r&   r   r  
  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-   ro   r   iscoroutiner   r   _format_coroutiner/   
cr_runningcr_code)r&   r_  r   r   r   +test_coroutine_like_object_debug_formatting
  s$    z7HandleTests.test_coroutine_like_object_debug_formatting)r-   r.   r/   rf   r  r  r  r  r  r  rU  r  r&  collectionsabcr  rH   r   r   )rG   r   r  l
  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)rE   rf   r   rl  r#   )r&   )rG   r   r   rf   "  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   (  rV   z&TimerTests.test_hash.<locals>.<lambda>r   )rv   r   r   TimerHandler   rl  r   hash)r&   whenr  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   .  rV   z&TimerTests.test_when.<locals>.<lambda>r   )rv   r   r   r  r   rl  r   r  )r&   r  r  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   r   3  s    z'TimerTests.test_timer.<locals>.callback)rW   rX   rU   )rv   r   r   r  r   rl  r   r  r  r   r  r   ro   r  rs   r:   r#   )r&   r   r   r  r  r   r   r   
test_timer2  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  r  ri   r  r   r  r   )r&   r  srcr   r   r   test_timer_reprH  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 )NTrW   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   r  r  r   r  r  ri   r  r   r  r   )r&   r  r  r  r  r  r   r   r   test_timer_repr_debugV  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   r   l  s    z2TimerTests.test_timer_comparison.<locals>.callbackr   g      $@)rv   r   r   r  r#   r   ro   r   r  r   NotImplemented__eq____ne__)r&   r   r  h1h2Zh3r   r   r   test_timer_comparisonk  s>    z TimerTests.test_timer_comparison)r-   r.   r/   rf   r  r  r  r  r  r  rH   r   r   )rG   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 )NrW   )r   rl  r   AbstractEventLooprs   NotImplementedErrorr   r"   r{   rp   rh   r+   r  r   r  r   rv   r   set_default_executorr   r   r   r   r   r   set_exception_handlerdefault_exception_handlerr  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   rl  r   r  rs   r  r   r  getnameinfor  ro  r  r   r   r   r   r   r\  rl  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 )NrW   )r   rl  r   r   r  r<   rC   r?   rA   DatagramProtocolrM   rL   r8  r`   rb   rd   )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   AbstractEventLoopPolicyrs   r  get_event_loopr!   r  r    get_child_watcherr  )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  _local_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'  r  r!   r  r  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!   rs   rt   r  )r&   r  r   r   r   "test_get_event_loop_after_set_none8  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  rs   rt   r  )r  )r&   r   r   r   @  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_loopH  s    zPolicyTests.test_new_event_loopc             C   sh   t  }| }| t|jt  | }|| | ||  | 	||  |
  |
  d S )N)r   r  r  rs   r:   r!   r  r    r   assertIsNotr+   )r&   r  Zold_loopr#   r   r   r   test_set_event_loopO  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)	rs   r:   r   set_event_loop_policyr  r  r  r   r  )r&   
old_policyr  r   r   r   test_set_event_loop_policya  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   rE   rf   r    r#   r!   r   r   r  r  r  )r&   r  )rG   r   r   rf   t  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   r  rE   rm   r#   r+   r!   r
  r   r	  r  r  r  r  r  r  )r&   )rG   r   r   rm     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)
concurrentfuturesProcessPoolExecutorr#   r   r$   shutdown)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)rE  r   r  r  r  r    rs   r  r!   r^  rt   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  rf   rm   r   r   r  r  rH   r   r   )rG   r   r  m  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   _py__get_running_loopr  _py__set_running_loopr  _py_get_running_loopr  _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   _c__get_running_loopr  _c__set_running_loopr  _c_get_running_loopr  _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     rV   z*TestServer.test_get_loop.<locals>.<lambda>z0.0.0.0r   )
r   r    r]  r+   rD   r"   ro  r   get_looprN  )r&   r#   rr  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)rs   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    r]  r+   rs   r  r"   r   r,  rN  )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)rs   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__collections.abcr  Zconcurrent.futuresr  r1  r  r   r   r  r   r   r  ImportErrorr1  r   ra  r   rv   r<  rU  r   r  r  r   r   r   r   r   r   r	   Ztest.test_asyncior
   ri   r  r   r   r$   r%   r   r0   rD   r  rI   rN   BaseProtocolrP   r8  rQ   re   r  r?  rJ  re  TestCaser  r  r  r  r&  r  r  r  r  r  r  r  r  r  r  r  _asyncior#  r(  r+  r-   r  r   r   r   r   <module>   s   

	" )            T     
 5tf[y
