B
    v9au                @   s  d Z ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZyddlZW n ek
rp   dZY nX ddlZddlZddlZddlZddlZddlZddlZddlmZ ddlZejdkrddlZddlZddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlm Z! ddl"m#Z# dd Z$dd Z%G dd dZ&G dd dej'Z(G dd de(Z)G dd dej*Z+G dd dej'Z,G dd dej-Z.G dd dej/Z0G dd  d Z1G d!d" d"Z2G d#d$ d$Z3G d%d& d&e3Z4G d'd( d(e3Z5ejdkrJG d)d* d*e1e5e4e!j6Z7G d+d, d,e1e5e4e2e!j6Z8nddl9Z9G d-d. d.e1e5e4Z:e;e9d/rG d0d1 d1e:e2e!j6Z<e;e9d2rG d3d4 d4e:e2e!j6Z=e;e9d5rG d6d7 d7e:e2e!j6Z>G d8d* d*e:e2e!j6Z7d9d: Z?G d;d< d<e!j6Z@G d=d> d>ej6ZAG d?d@ d@ej6ZBG dAdB dBej6ZCG dCdD dDej6ZDG dEdF dFZEG dGdH dHeEej6ZFyddlGZGW n ek
r   Y nX G dIdJ dJeEej6ZHG dKdL dLej6ZIG dMdN dNej6ZJeKdOkreL  dS )PzTests for events.py.    N)mockwin32)base_events)	constants)
coroutines)events)proactor_events)selector_events)utils)supportc              C   sF   t jdrdS t jdkrdS t d } ttt| d} | dk S )z4Return True if the platform is Mac OS 10.4 or older.ZaixTdarwinFr   .)
      )sysplatform
startswithZmac_vertuplemapintsplit)version r   !/usr/lib/python3.7/test_events.pybroken_unix_getsockname(   s    
r   c              C   s&   dd } t  }t | ||  S )Nc                  s   dS )Nhellor   r   r   r   r   doit4   s    z8_test_get_event_loop_new_process__sub_proc.<locals>.doit)asyncionew_event_loopset_event_looprun_until_complete)r   loopr   r   r   *_test_get_event_loop_new_process__sub_proc3   s    
r"   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
CoroLikec             C   s   d S )Nr   )selfvr   r   r   send=   s    zCoroLike.sendc             G   s   d S )Nr   )r$   excr   r   r   throw@   s    zCoroLike.throwc             C   s   d S )Nr   )r$   r   r   r   closeC   s    zCoroLike.closec             C   s   d S )Nr   )r$   r   r   r   	__await__F   s    zCoroLike.__await__N)__name__
__module____qualname__r&   r(   r)   r*   r   r   r   r   r#   <   s   r#   c               @   s>   e Zd ZdZdZdddZdd Zdd Zdd	 Zd
d Z	dS )MyBaseProtoNc             C   s:   d | _ d| _d| _|d k	r6tj|d| _tj|d| _d S )NINITIALr   )r!   )	transportstatenbytesr   Future	connecteddone)r$   r!   r   r   r   __init__N   s    zMyBaseProto.__init__c             C   s"   || _ d| _| jr| jd  d S )N	CONNECTED)r0   r1   r4   
set_result)r$   r0   r   r   r   connection_madeV   s    zMyBaseProto.connection_madec             C   s   |  j t|7  _ d S )N)r2   len)r$   datar   r   r   data_received]   s    zMyBaseProto.data_receivedc             C   s
   d| _ d S )NEOF)r1   )r$   r   r   r   eof_receiveda   s    zMyBaseProto.eof_receivedc             C   s   d| _ | jr| jd  d S )NCLOSED)r1   r5   r8   )r$   r'   r   r   r   connection_loste   s    zMyBaseProto.connection_lost)N)
r+   r,   r-   r4   r5   r6   r9   r<   r>   r@   r   r   r   r   r.   J   s   
r.   c                   s   e Zd Z fddZ  ZS )MyProtoc                s   t  | |d d S )Ns%   GET / HTTP/1.0
Host: example.com

)superr9   write)r$   r0   )	__class__r   r   r9   m   s    zMyProto.connection_made)r+   r,   r-   r9   __classcell__r   r   )rD   r   rA   l   s   rA   c               @   s:   e Zd ZdZdddZdd Zdd Zdd	 Zd
d ZdS )MyDatagramProtoNc             C   s&   d| _ d| _|d k	r"tj|d| _d S )Nr/   r   )r!   )r1   r2   r   r3   r5   )r$   r!   r   r   r   r6   u   s    zMyDatagramProto.__init__c             C   s   || _ d| _d S )NINITIALIZED)r0   r1   )r$   r0   r   r   r   r9   {   s    zMyDatagramProto.connection_madec             C   s   |  j t|7  _ d S )N)r2   r:   )r$   r;   addrr   r   r   datagram_received   s    z!MyDatagramProto.datagram_receivedc             C   s   d S )Nr   )r$   r'   r   r   r   error_received   s    zMyDatagramProto.error_receivedc             C   s   d| _ | jr| jd  d S )Nr?   )r1   r5   r8   )r$   r'   r   r   r   r@      s    zMyDatagramProto.connection_lost)N)	r+   r,   r-   r5   r6   r9   rI   rJ   r@   r   r   r   r   rF   r   s   
rF   c               @   s:   e Zd ZdZdddZdd Zdd Zdd	 Zd
d ZdS )MyReadPipeProtoNc             C   s.   dg| _ d| _d | _|d k	r*tj|d| _d S )Nr/   r   )r!   )r1   r2   r0   r   r3   r5   )r$   r!   r   r   r   r6      s
    zMyReadPipeProto.__init__c             C   s   || _ | jd d S )Nr7   )r0   r1   append)r$   r0   r   r   r   r9      s    zMyReadPipeProto.connection_madec             C   s   |  j t|7  _ d S )N)r2   r:   )r$   r;   r   r   r   r<      s    zMyReadPipeProto.data_receivedc             C   s   | j d d S )Nr=   )r1   rL   )r$   r   r   r   r>      s    zMyReadPipeProto.eof_receivedc             C   s8   d| j kr| j d | j d | jr4| jd  d S )Nr=   r?   )r1   rL   r5   r8   )r$   r'   r   r   r   r@      s
    
zMyReadPipeProto.connection_lost)N)	r+   r,   r-   r5   r6   r9   r<   r>   r@   r   r   r   r   rK      s   
rK   c               @   s*   e Zd ZdZdddZdd Zdd ZdS )	MyWritePipeProtoNc             C   s&   d| _ d | _|d k	r"tj|d| _d S )Nr/   )r!   )r1   r0   r   r3   r5   )r$   r!   r   r   r   r6      s    zMyWritePipeProto.__init__c             C   s   || _ d| _d S )Nr7   )r0   r1   )r$   r0   r   r   r   r9      s    z MyWritePipeProto.connection_madec             C   s   d| _ | jr| jd  d S )Nr?   )r1   r5   r8   )r$   r'   r   r   r   r@      s    z MyWritePipeProto.connection_lost)N)r+   r,   r-   r5   r6   r9   r@   r   r   r   r   rM      s   
rM   c               @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )MySubprocessProtocolc                sr   d| _ d | _tj d| _tj d| _ fddtdD | _ddd| _d | _	tj
 dtj
 dd| _d S )Nr/   )r!   c                s   i | ]}t j d |qS ))r!   )r   r3   ).0fd)r!   r   r   
<dictcomp>   s    z1MySubprocessProtocol.__init__.<locals>.<dictcomp>       )      )r1   r0   r   r3   r4   	completedrangedisconnectsr;   
returncodeZEventgot_data)r$   r!   r   )r!   r   r6      s    
zMySubprocessProtocol.__init__c             C   s   || _ d| _| jd  d S )Nr7   )r0   r1   r4   r8   )r$   r0   r   r   r   r9      s    z$MySubprocessProtocol.connection_madec             C   s   d| _ | jd  d S )Nr?   )r1   rV   r8   )r$   r'   r   r   r   r@      s    z$MySubprocessProtocol.connection_lostc             C   s$   | j |  |7  < | j|   d S )N)r;   rZ   set)r$   rP   r;   r   r   r   pipe_data_received   s    z'MySubprocessProtocol.pipe_data_receivedc             C   s*   |r| j | | n| j | | d S )N)rX   Zset_exceptionr8   )r$   rP   r'   r   r   r   pipe_connection_lost   s    z)MySubprocessProtocol.pipe_connection_lostc             C   s   | j  | _d S )N)r0   Zget_returncoderY   )r$   r   r   r   process_exited   s    z#MySubprocessProtocol.process_exitedN)	r+   r,   r-   r6   r9   r@   r\   r]   r^   r   r   r   r   rN      s   rN   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)rB   setUpcreate_event_loopr!   r   )r$   )rD   r   r   r`      s    

zEventLoopTestsMixin.setUpc                s4   | j  st| j  |   t  t   d S )N)	r!   	is_closed
test_utilsrun_brieflyZ
doCleanupsr   Z
gc_collectrB   tearDown)r$   )rD   r   r   re      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   )rf   r$   r   r   coro2  s    zBEventLoopTestsMixin.test_run_until_complete_nesting.<locals>.coro2)r   	coroutineassertRaisesRuntimeErrorr!   r    )r$   ri   r   )rf   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   sleeprg   )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   ro   r   )r$   r   r   cb  s    
z?EventLoopTestsMixin.test_run_until_complete_stopped.<locals>.cb)rk   rl   r!   r    )r$   rt   Ztaskr   )r$   r   test_run_until_complete_stopped  s    z3EventLoopTestsMixin.test_run_until_complete_stoppedc                sv   g   fdd}j d|d t }j   t } dg d||   koddkn  ||  d S )Nc                s     |  j  d S )N)rL   r!   rs   )arg)resultsr$   r   r   callback  s    
z5EventLoopTestsMixin.test_call_later.<locals>.callbackg?zhello worldg{Gz?g?)r!   
call_laterrn   	monotonicrun_foreverassertEqualrg   )r$   rx   rp   rq   r   )rw   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)rL   r!   rs   )Zarg1Zarg2)rw   r$   r   r   rx   ,  s    z4EventLoopTestsMixin.test_call_soon.<locals>.callbackr   world)r   r~   )r!   	call_soonr{   r|   )r$   rx   r   )rw   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 )NrU   )rL   r:   r!   rs   )rv   )rw   r$   r   r   rx   8  s    
z?EventLoopTestsMixin.test_call_soon_threadsafe.<locals>.callbackc                  s   j  d   d S )Nr   )r!   call_soon_threadsafereleaser   )rx   lockr$   r   r   run_in_thread=  s    zDEventLoopTestsMixin.test_call_soon_threadsafe.<locals>.run_in_thread)targetr~   r   )
	threadingZLockacquireThreadstartr!   r   r{   joinr|   )r$   r   tr   )rx   r   rw   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 )NrU   )rL   r:   r!   rs   )rv   )rw   r$   r   r   rx   N  s    
zKEventLoopTestsMixin.test_call_soon_threadsafe_same_thread.<locals>.callbackr   r~   )r!   r   r   r{   r|   )r$   rx   r   )rw   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)rv   r   r   r   runY  s    z5EventLoopTestsMixin.test_run_in_executor.<locals>.runZyo)r!   run_in_executorr    r|   ZassertNotEqualr   r   )r$   r   f2resZ	thread_idr   r   r   test_run_in_executorX  s
    z(EventLoopTestsMixin.test_run_in_executorc                s`   d  fdd}dd }| j d |}|  | j   || j _|| j _td |   d S )NFc                 s   d d S )NTr   )args)calledr   r   patched_call_soonc  s    zJEventLoopTestsMixin.test_run_in_executor_cancel.<locals>.patched_call_soonc               S   s   t d d S )Ng?)rn   ro   r   r   r   r   r   g  s    z<EventLoopTestsMixin.test_run_in_executor_cancel.<locals>.rung?)	r!   r   cancelr)   r   r   rn   ro   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extendrg   r!   remove_readerfilenor)   )r;   )
bytes_readrr$   r   r   readerw  s    z8EventLoopTestsMixin.test_reader_callback.<locals>.readers   abcc                  s   t  dkS )NrR   )r:   r   )r   r   r   <lambda>  rS   z:EventLoopTestsMixin.test_reader_callback.<locals>.<lambda>s   defc                  s   t  dkS )N   )r:   r   )r   r   r   r     rS   s   abcdef)socket
socketpairsetblocking	bytearrayr!   
add_readerr   r   r&   rc   	run_untilr)   rs   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!   rs   )r;   )r$   r   r   r   writer  s    
z8EventLoopTestsMixin.test_writer_callback.<locals>.writers   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxrU   )r   r   r   r!   
add_writerr   r{   rg   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   rk   
ValueErrorr    sock_connectaddresssock_sendall	sock_recvsock_recv_intor   sock_acceptr)   rg   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)   rg   r   )r$   r   r   r;   bufr2   r   r   r   _basetest_sock_recv_into  s    

"z,EventLoopTestsMixin._basetest_sock_recv_intoc          	   C   s@   t  .}t }| || t }| || W d Q R X d S )N)rc   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)rc   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   rk   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   rT   r   )r   r   r   listenconnectr   r!   r   r    r|   Z
gettimeoutZgetpeernamer)   )r$   ZlistenerclientfconnrH   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 )NrT   r   r   )caughtr   r   
my_handler  s    z?EventLoopTestsMixin.test_add_signal_handler.<locals>.my_handlerZboomrT   c                  s    S )Nr   r   )r   r   r   r   +  rS   z=EventLoopTestsMixin.test_add_signal_handler.<locals>.<lambda>)rk   	TypeErrorr!   add_signal_handlerremove_signal_handlerr   signalNSIGrl   r   r   SIGINToskillgetpidrc   r   rg   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 )NrT   )r!   rs   r   )r   r$   r   r   r   9  s    zLEventLoopTestsMixin.test_signal_handling_while_selecting.<locals>.my_handlerg{Gz?<   rT   )
r!   r   r   r   	setitimerITIMER_REALry   rs   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 )NrT   )r|   r!   rs   )r   )r   r$   	some_argsr   r   r   J  s    zAEventLoopTestsMixin.test_signal_handling_args.<locals>.my_handlerg?r   rT   )
r!   r   r   r   r   r   ry   rs   r{   r|   )r$   r   r   )r   r$   r   r   test_signal_handling_argsE  s    
z-EventLoopTestsMixin.test_signal_handling_argsTc             C   sv   | j |\}}| |tj | |tj | |j| |rN| |	d | j |j
 | |jd |  d S )Nsocknamer   )r!   r    assertIsInstancer   	TransportProtocolassertIsr0   assertIsNotNoneget_extra_infor5   assertGreaterr2   r)   )r$   connection_futcheck_socknametrprr   r   r   _basetest_create_connectionW  s    z/EventLoopTestsMixin._basetest_create_connectionc          	      s>   t  ,} jj fddf|j } | W d Q R X d S )Nc                  s   t  jdS )N)r!   )rA   r!   r   )r$   r   r   r   e  rS   z<EventLoopTestsMixin.test_create_connection.<locals>.<lambda>)rc   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!   )rA   r!   r   )r$   r   r   r   p  rS   zAEventLoopTestsMixin.test_create_unix_connection.<locals>.<lambda>)r   rc   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}xZ|D ]R\}}}}}y4tj|||d}|d  j j	|| W n   Y q2X P q2W  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   protoFc                  s   t  jdS )N)r!   )rA   r!   r   )r$   r   r   r     rS   zAEventLoopTestsMixin.test_create_connection_sock.<locals>.<lambda>)r   r   )rc   r   r!   r    getaddrinfor   r   SOCK_STREAMr   r   r   r   r   r   r   r5   r   r2   r)   )r$   r   r   infosr   r   r   cnamer   r   r   r   r   )r$   r   test_create_connection_socks  s,    

z/EventLoopTestsMixin.test_create_connection_sockNc             C   s  |r|  |d |r,| ||d n|  |d | ||d |d}| |t | t|d| | |d t | |d t | |d t |d	}|  | | | |d
 | |	 |d | |
 |d | | |d
 d S )Nr   peernamepeercertcipherrR   r   rT   rU   Z
ssl_objectcompression)r   r   r|   r   r   r:   strr   r  r  Zgetpeercert)r$   r   r   r  r  r  Zsslobjr   r   r   check_ssl_extra_info  s0    






z(EventLoopTestsMixin.check_ssl_extra_infoc             C   sx   | j |\}}| |tj | |tj | d|jj	 k | 
||| | j |j | |jd |  d S )Nsslr   )r!   r    r   r   r   r   rg   rD   r+   lowerr  r5   r   r2   r)   )r$   r   r   r  r   r   r   r   r   _basetest_create_ssl_connection  s    z3EventLoopTestsMixin._basetest_create_ssl_connectionc       	   
      s   |t  d} ||| ttdrtjjfd d d d fdd}tjd|d,}|dd} |||  	|j
d	 W d Q R X  tj2}|dd}t    ||| W d Q R X W d Q R X  	|jjd
 d S )N)r	  Purpose)cafilecapathcadatac               s     | tjj t S )z
                A ssl.create_default_context() replacement that doesn't enable
                cert validation.
                )r|   r	  r  SERVER_AUTHrc   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_effectTrT   ZCERTIFICATE_VERIFY_FAILED)rc   r  r  hasattrr	  r  r  r   patchr|   Z
call_countrk   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!   )rA   r!   r   )r$   r   r   r     rS   z@EventLoopTestsMixin.test_create_ssl_connection.<locals>.<lambda>)r  )rc   r   	functoolspartialr!   r   r   r  )r$   r   r   r   )r$   r   test_create_ssl_connection  s    
z.EventLoopTestsMixin.test_create_ssl_connectionc          	      sX   t   }tjdd:}tj jj fdd|jdd} j||||jd W d Q R X d S )NT)r  c                  s   t  jdS )N)r!   )rA   r!   r   )r$   r   r   r     rS   zEEventLoopTestsMixin.test_create_ssl_unix_connection.<locals>.<lambda>z	127.0.0.1)server_hostname)r  )	r   rc   r   r  r  r!   r   r   r  )r$   r   r   r   r   )r$   r   test_create_ssl_unix_connection  s    z3EventLoopTestsMixin.test_create_ssl_unix_connectionc          	      s   t  n}t } jj fddf|jd|jd |fi} j|\}}|j	dd } 
|| |  W d Q R X d S )Nc                  s   t  jdS )N)r!   )rA   r!   r   )r$   r   r   r     rS   zGEventLoopTestsMixin.test_create_connection_local_addr.<locals>.<lambda>
local_addrr   r   rT   )rc   r   r   find_unused_portr!   r   r   r    r0   r   r|   r)   )r$   r   portr   r   r   expectedr   )r$   r   !test_create_connection_local_addr  s    
z5EventLoopTestsMixin.test_create_connection_local_addrc          
      s   t  t} jj fddf|jd|ji} t} j| W d Q R X  |j	j
t
j  t|j|j	j W d Q R X d S )Nc                  s   t  jdS )N)r!   )rA   r!   r   )r$   r   r   r   	  rS   zNEventLoopTestsMixin.test_create_connection_local_addr_in_use.<locals>.<lambda>r#  )rc   r   r!   r   r   rk   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)rB   r@   r   rs   )r$   r'   )rD   r!   r   r   r@     s    zQEventLoopTestsMixin.test_connect_accepted_socket.<locals>.MyProto.connection_lostc                s   t  | | j d S )N)rB   r<   r0   rC   )r$   r;   )rD   expected_responser   r   r<     s    zOEventLoopTestsMixin.test_connect_accepted_socket.<locals>.MyProto.data_received)r+   r,   r-   r@   r<   rE   r   )r.  r!   )rD   r   rA     s   rA   )z	127.0.0.1r   rT   s	   test datas   rogerc           
      sv   yDt   } d k	r| } |   |  | d|   W n, tk
rp } ztd| W d d }~X Y nX d S )Nc   z8Failure in client thread in test_connect_accepted_socket)r   Zwrap_socketr   sendallr   r)   	Exceptionprint)Zcsockr'   )rH   
client_sslmessageresponser   r   r   &  s    



z@EventLoopTestsMixin.test_connect_accepted_socket.<locals>.clientT)r   Zdaemon)r!   c                  s    S )Nr   r   )r   r   r   r   =  rS   zBEventLoopTestsMixin.test_connect_accepted_socket.<locals>.<lambda>)r	  )timeoutr?   )r!   r.   r   r   r   r   r   r   r   Zacceptr    connect_accepted_socketr{   r0   r)   r   Zjoin_threadr   Zis_aliver|   r1   r2   r:   )	r$   Z
server_sslr3  rA   Zlsockr   Zthreadr   _r   )rH   r3  r.  r!   r4  r   r5  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   )rR   r   z=SSL not supported with proactor event loops before Python 3.5)r   r   version_infor   r!   r   r   unittestSkipTestrc   simple_server_sslcontextsimple_client_sslcontextr9  )r$   Zserver_contextZclient_contextr   r   r    test_ssl_connect_accepted_socketH  s    

z4EventLoopTestsMixin.test_ssl_connect_accepted_socketc          	   C   sN   t   }| |j | jjt|dd}| td | j| W d Q R X d S )NrT   )Zssl_handshake_timeoutz1ssl_handshake_timeout is only meaningful with ssl)	r   
addCleanupr)   r!   r7  rA   assertRaisesRegexr   r    )r$   r   coror   r   r   9test_connect_accepted_socket_ssl_timeout_for_plain_socketW  s    zMEventLoopTestsMixin.test_connect_accepted_socket_ssl_timeout_for_plain_socketzasyncio.base_events.socketc       	         s   t j fddfdd}t|} tjkrJdd |D | j_ndd |D | j_|j_t	
 j_t	
 j_jfdd	|d
}j|}|j dd |jD }|| d S )Nc                s>    t jkr  t jdd| |ffgS  t jdd| |ddffgS d S )Nr    r   )r   AF_INETr   )hostr%  r   kw)r   r   r   r   c  s    
zEEventLoopTestsMixin.create_server_multiple_hosts.<locals>.getaddrinfoc                 s   t j | |jdS )N)r!   )r   Taskr!   )r   kwds)r   r$   r   r   getaddrinfo_taskj  s    zJEventLoopTestsMixin.create_server_multiple_hosts.<locals>.getaddrinfo_taskc             S   s   g | ]}|d fqS )P   r   )rO   rF  r   r   r   
<listcomp>q  s    zDEventLoopTestsMixin.create_server_multiple_hosts.<locals>.<listcomp>c             S   s   g | ]}|d ddfqS )rK  r   r   )rO   rF  r   r   r   rL  t  s    c                  s
   t  jS )N)rA   r!   r   )r$   r   r   r   x  rS   zBEventLoopTestsMixin.create_server_multiple_hosts.<locals>.<lambda>rK  c             S   s   h | ]}|  d  qS )r   )getsockbyname)rO   r   r   r   r   	<setcomp>{  s    zCEventLoopTestsMixin.create_server_multiple_hosts.<locals>.<setcomp>)r   rj   r[   r   rE  rM  r  r!   r   r   MockZ_start_serving_stop_servingcreate_serverr    r@  r)   socketsr|   )	r$   r   ZhostsZ	mock_sockrJ  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)rT  r   rE  )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)rT  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     rS   z8EventLoopTestsMixin.test_create_server.<locals>.<lambda>z0.0.0.0r   rT   z	127.0.0.1s   xxxr7   c                  s
    j dkS )Nr   )r2   r   )r   r   r   r     rS   rR   r   r  r?   )rA   r!   rQ  r    r|   r:   rR  r   r   r   r0  r4   r1   rc   r   r2   r   r0   r   r)   r5   )r$   r   rS  r   rF  r%  r   r   )r   r   test_create_server  s,    



z&EventLoopTestsMixin.test_create_serverSO_REUSEPORTzNo SO_REUSEPORTc                s   t | j | j fdddd}| j|}| t|jd |jd }| |t	j
t	j |  t| j t | j | jj fdddddd}| j|}| t|jd |jd }| |t	j
t	j |  d S )	Nc                  s    S )Nr   r   )r   r   r   r     rS   zCEventLoopTestsMixin.test_create_server_reuse_port.<locals>.<lambda>z0.0.0.0r   rT   c                  s    S )Nr   r   )r   r   r   r     rS   T)Z
reuse_port)rA   r!   rQ  r    r|   r:   rR  r   Z
getsockoptr   
SOL_SOCKETrY  r)   rc   rd   rg   )r$   r   rS  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     rS   z7EventLoopTestsMixin._make_unix_server.<locals>.<lambda>)rc   Zgen_unix_socket_pathr@  r!   create_unix_serverr    )r$   factorykwargsr   rS  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     rS   z=EventLoopTestsMixin.test_create_unix_server.<locals>.<lambda>rT   s   xxxr7   c                  s
    j dkS )Nr   )r2   r   )r   r   r   r     rS   rR   r?   )rA   r!   rb  r|   r:   rR  r   r   r   r0  r    r4   r1   rc   r   r2   r0   r)   r5   )r$   rS  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     rS   zOEventLoopTestsMixin.test_create_unix_server_path_socket_error.<locals>.<lambda>z/test)r   z3path and sock can not be specified at the same time)rA   r!   r   r_  rA  r   r    )r$   r   r   r   )r   r   )test_create_unix_server_path_socket_error  s    z=EventLoopTestsMixin.test_create_unix_server_path_socket_errorc             C   s,   t t j}| jt jO  _||| |S )N)r	  
SSLContextZPROTOCOL_TLS_SERVERoptionsOP_NO_SSLv2Zload_cert_chain)r$   certfilekeyfile
sslcontextr   r   r   _create_ssl_context  s    z'EventLoopTestsMixin._create_ssl_contextc       
      C   sX   |  ||}| jj|dd|d}| j|}|jd }| \}}	| |d |||	fS )Nz	127.0.0.1r   )r	  )rk  r!   rQ  r    rR  r   r|   )
r$   r`  rh  ri  rj  r   rS  r   rF  r%  r   r   r   _make_ssl_server  s    
z$EventLoopTestsMixin._make_ssl_serverc             C   s   |  ||}| j||dS )N)r	  )rk  rb  )r$   r`  rh  ri  rj  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     rS   z<EventLoopTestsMixin.test_create_server_ssl.<locals>.<lambda>)r	  s   xxxr7   c                  s
    j dkS )Nr   )r2   r   )r   r   r   r     rS   rR   )r  r?   )rA   r!   rl  rc   ONLYCERTONLYKEYr   r.   r  r    rC   r4   r|   r1   r   r2   r  r0   r)   r5   )r$   rS  rF  r%  f_cr   r   r   )r   r   test_create_server_ssl	  s"    

z*EventLoopTestsMixin.test_create_server_sslc                s   t | jd |  fddtjtj\}}| jjt|t dd}| j	|\}}|
d | j	 j | d j t| j fdd | d	 j  j  | j	 j | d
 j |  |  d S )N)r!   c                  s    S )Nr   r   )r   r   r   r   .  rS   zAEventLoopTestsMixin.test_create_unix_server_ssl.<locals>.<lambda>rD  )r	  r!  s   xxxr7   c                  s
    j dkS )Nr   )r2   r   )r   r   r   r   9  rS   rR   r?   )rA   r!   rm  rc   rn  ro  r   r.   r  r    rC   r4   r|   r1   r   r2   r0   r)   r5   )r$   rS  r\  rp  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  rS   zJEventLoopTestsMixin.test_create_server_ssl_verify_failed.<locals>.<lambda>check_hostnameT)r	  call_exception_handlerz(?i)certificate.verify.failed)rA   r!   rl  rc   SIGNED_CERTFILEr	  re  PROTOCOL_TLS_CLIENTrf  rg  CERT_REQUIREDverify_moder  rs  r   r   r  objectr  rA  r  r    rd   assertIsNoner0   r)   )r$   rS  rF  r%  sslcontext_clientrp  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  rS   zOEventLoopTestsMixin.test_create_unix_server_ssl_verify_failed.<locals>.<lambda>rs  TZinvalid)r	  r!  rt  z(?i)certificate.verify.failed)rA   r!   rm  rc   ru  r	  re  rv  rf  rg  rw  rx  r  rs  r   r   r  ry  r  rA  r  r    rd   rz  r0   r)   )r$   rS  r\  r{  rp  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     rS   zIEventLoopTestsMixin.test_create_server_ssl_match_failed.<locals>.<lambda>)r  rs  T)r	  rt  z=IP address mismatch, certificate is not valid for '127.0.0.1')rA   r!   rl  rc   ru  r	  re  rv  rf  rg  rw  rx  load_verify_locations
SIGNING_CAr  rs  r   r   r  ry  r  rA  ZCertificateErrorr    rz  r0   r)   )r$   rS  rF  r%  r{  rp  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     rS   zJEventLoopTestsMixin.test_create_unix_server_ssl_verified.<locals>.<lambda>)r  rs  T	localhost)r	  r!  )rA   r!   rm  rc   ru  r	  re  rv  rf  rg  rw  rx  r~  r  r  rs  r   r    r0   r)   r5   )r$   rS  r\  r{  rp  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     rS   zEEventLoopTestsMixin.test_create_server_ssl_verified.<locals>.<lambda>)r  rs  Tr  )r	  r!  )r  r  )rA   r!   rl  rc   ru  r	  re  rv  rf  rg  rw  rx  r~  r  r  rs  r   r    r  ZPEERCERTr0   r)   r5   )r$   rS  rF  r%  r{  rp  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)rB   r9   r8   )r$   r0   )rD   r   r   r   r9     s    zPEventLoopTestsMixin.test_create_server_sock.<locals>.TestMyProto.connection_made)r+   r,   r-   r9   rE   r   )r   )rD   r   TestMyProto  s   r  )r   rT   )z0.0.0.0r   )r   r   z0.0.0.0z	127.0.0.1s   xxx)r   r3   r!   rA   r   r   
setsockoptrZ  SO_REUSEADDRr   rQ  r    rR  r   r   r|   r   r&   r)   )	r$   r  sock_obr   rS  r   rF  r%  r   r   )r   r   test_create_server_sock  s     


z+EventLoopTestsMixin.test_create_server_sockc          	   C   s   t j t jd}|t jt jd |d | jjt|d}| j	|}|j
d }| \}}| jjt||d}| t}| j	| W d Q R X | |jjtj |  d S )N)r   rT   )z0.0.0.0r   )r   r   )rF  r%  )r   r   r  rZ  r  r   r!   rQ  rA   r    rR  r   rk   r(  r|   r  r)  r*  r)   )r$   r  r   rS  r   rF  r%  r  r   r   r   test_create_server_addr_in_use  s    

z2EventLoopTestsMixin.test_create_server_addr_in_usezIPv6 not supported or enabledc       	   
      s<  t j| jd G  fdddt}d}xzy*t }| jj|d |d}| j|}W nH tk
r } z*|j	t	j
kr|d7 }| d| w(n W d d }~X Y q(X P q(W t }|d|f |d	 | j }|j  |  t j| jd ttj}|d
|f |d	 | j }|j  |  |  d S )N)r!   c                   s   e Zd Z fddZ  ZS )zFEventLoopTestsMixin.test_create_server_dual_stack.<locals>.TestMyProtoc                s   t  | |  d S )N)rB   r9   r8   )r$   r0   )rD   f_protor   r   r9     s    zVEventLoopTestsMixin.test_create_server_dual_stack.<locals>.TestMyProto.connection_made)r+   r,   r-   r9   rE   r   )r  )rD   r   r    s   r  r   )rF  r%  rT   r   z	127.0.0.1s   xxxz::1)r   r3   r!   rA   r   r$  rQ  r    r(  r)  r*  ZassertGreaterEqualr   r   r&   r0   r)   rV  )	r$   r  Z	try_countr%  r   rS  Zexr   r   r   )r  r   test_create_server_dual_stack  s:    



z1EventLoopTestsMixin.test_create_server_dual_stackc             C   s   | j tdd}| j |}|jd }| \}}t }|d|f |d |	  |	  t }| 
t|jd|f |	  d S )Nz0.0.0.0r   z	127.0.0.1s   xxx)r!   rQ  rA   r    rR  r   r   r   r&   r)   rk   r   )r$   r   rS  r   rF  r%  r   r   r   r   test_server_close+  s    

z%EventLoopTestsMixin.test_server_closec                sn  G fdddt }jj|dd}j|\}|d\}}|tj | dj	 
j| jjfdd||fd	}j|\} |tj  t  d j	 
 j| |d
 tjfdd dj tj fdd d j |d |  j j d j	 j  d S )Nc                   s*   e Zd Z fddZ fddZ  ZS )zNEventLoopTestsMixin.test_create_datagram_endpoint.<locals>.TestMyDatagramProtoc                s   t  jjd d S )N)r!   )rB   r6   r!   )Z
inner_self)rD   r$   r   r   r6   ?  s    zWEventLoopTestsMixin.test_create_datagram_endpoint.<locals>.TestMyDatagramProto.__init__c                s$   t  || | jd| | d S )Ns   resp:)rB   rI   r0   sendto)r$   r;   rH   )rD   r   r   rI   B  s    z`EventLoopTestsMixin.test_create_datagram_endpoint.<locals>.TestMyDatagramProto.datagram_received)r+   r,   r-   r6   rI   rE   r   )r$   )rD   r   TestMyDatagramProto>  s   r  )z	127.0.0.1r   )r#  r   rG   c                  s   t  jdS )N)r!   )rF   r!   r   )r$   r   r   r   Q  rS   zCEventLoopTestsMixin.test_create_datagram_endpoint.<locals>.<lambda>)Zremote_addrs   xxxc                  s    j S )N)r2   r   )rS  r   r   r   [  rS   rR   c                  s    j S )N)r2   r   )r   r   r   r   ]  rS      r?   )rF   r!   create_datagram_endpointr    r   r   r   r   r|   r1   r   r0   r  rc   r   r2   r   r)   r5   )r$   r  rB  Zs_transportrF  r%  r0   r   )r   r$   rS  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}xN|D ]F\}}}}}y(t
j
|||d}|d || W n   Y qLX P qLW  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   Fc                  s   t  jdS )N)r!   )rF   r!   r   )r$   r   r   r     rS   zHEventLoopTestsMixin.test_create_datagram_endpoint_sock.<locals>.<lambda>)r   )r   r   r   r!   r   r   r;  r<  r    r   r   Z
SOCK_DGRAMr   r   r  r   r   r   rF   r)   r5   )r$   r   Zlocal_addressr   r   r   r   r  r   r   r   r   r   )r$   r   "test_create_datagram_endpoint_sockk  s0    

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 BaseSelectorEventLooprT   r   )ra   r   r	   ZBaseSelectorEventLoopr)   skipTestr|   Z_internal_fdsrz  Z_csockZ_ssock)r$   r!   r   r   r   test_internal_fds  s    
z%EventLoopTestsMixin.test_internal_fdsr   zDon't support pipes for Windowsc                s   t jdt \}}t|dd  fdd}j|  t|d t	jfdd 
d	j t|d
 t	jfdd 
ddgj 
dj t| jj 
ddddgj jd d S )N)r!   rbi   c                 s^   j fdd I d H \} }| | j ddgj dj d S )Nc                  s    S )Nr   r   )r   r   r   r     rS   zEEventLoopTestsMixin.test_read_pipe.<locals>.connect.<locals>.<lambda>r/   r7   r   )r!   connect_read_piper   r0   r|   r1   r2   )r   p)pipeobjr   r$   r   r   r     s    z3EventLoopTestsMixin.test_read_pipe.<locals>.connect   1c                  s
    j dkS )NrT   )r2   r   )r   r   r   r     rS   z4EventLoopTestsMixin.test_read_pipe.<locals>.<lambda>rT   s   2345c                  s
    j dkS )Nr   )r2   r   )r   r   r   r     rS   r/   r7   r   r=   r?   pipe)rK   r!   r   r  ioopenr    rC   rc   r   r|   r2   r1   r)   r5   r   r0   r   )r$   rpipewpiper   r   )r  r   r$   r   test_read_pipe  s"    
z"EventLoopTestsMixin.test_read_pipec                s   |    t dt dt \}}t|ddt|dd fdd} | \}}   | 	dt
| | 	dt
|     d |_d |_d S )N)r!   r  i   r   c                 sD     fddI d H \} } fddI d H \}}| |fS )Nc                  s    S )Nr   r   )
read_protor   r   r     rS   zSEventLoopTestsMixin.test_unclosed_pipe_transport.<locals>.connect.<locals>.<lambda>c                  s    S )Nr   r   )write_protor   r   r     rS   )r  connect_write_pipe)read_transportr8  write_transport)r!   r  rpipeobjwpipeobjr  r   r   r     s
    zAEventLoopTestsMixin.test_unclosed_pipe_transport.<locals>.connectr  )ra   rK   rM   r   r  r  r  r    r)   r+  reprZ_pipe)r$   r  r  r   r  r  r   )r!   r  r  r  r  r   test_unclosed_pipe_transport  s    

z0EventLoopTestsMixin.test_unclosed_pipe_transportc                s  t jdt \}}t|dd  fdd}j|  t|d t	jfdd 
d	j t|d
 t	jfdd 
ddgj 
dj t| j  jj 
ddddgj jd d S )N)r!   r  r   c                 s^   j fdd I d H \} }| | j ddgj dj d S )Nc                  s    S )Nr   r   )r   r   r   r     rS   zKEventLoopTestsMixin.test_read_pty_output.<locals>.connect.<locals>.<lambda>r/   r7   r   )r!   r  r   r0   r|   r1   r2   )r   r  )master_read_objr   r$   r   r   r     s    z9EventLoopTestsMixin.test_read_pty_output.<locals>.connectr  c                  s    j S )N)r2   r   )r   r   r   r     rS   z:EventLoopTestsMixin.test_read_pty_output.<locals>.<lambda>rT   s   2345c                  s
    j dkS )Nr   )r2   r   )r   r   r   r     rS   r/   r7   r   r=   r?   r  )rK   r!   r   openptyr  r  r    rC   rc   r   r|   r2   r1   r)   r0   r5   r   r   )r$   masterslaver   r   )r  r   r$   r   test_read_pty_output  s$    

z(EventLoopTestsMixin.test_read_pty_outputc                s6  t  \}t|dd}t| jd| jfdd|}| j|\}}| | | |j	 | 
dj |d t  fdd	t| j fd
d | 
d  |d t| j fdd | 
d  | 
dj t  | j	d j	  | jj | 
dj d S )Nwbi   )r!   c                  s    S )Nr   r   )r   r   r   r     rS   z5EventLoopTestsMixin.test_write_pipe.<locals>.<lambda>r7   r  c                s   t  d}| |7 } t| S )Ni   )r   r   r:   )r;   chunk)r  r   r   r     s    z3EventLoopTestsMixin.test_write_pipe.<locals>.readerc                  s    dkS )NrT   r   r   )r;   r   r   r   r     rS   s   2345c                  s    dkS )Nr   r   r   )r;   r   r   r   r     rS   s   12345r  r?   )r   r  r  r  rM   r!   r  r    r   r0   r|   r1   rC   r   rc   r   r)   r   r   r5   )r$   r  r  r   r0   r  r   )r;   r   r   r  r   test_write_pipe  s,    



z#EventLoopTestsMixin.test_write_pipec                s   t  \}}|d t| dd}t| jd | j fdd|}| j	|\}}| 
|  | 
| j | d j |d | j	| j|d}| d| |  | j	 j | d	 j d S )
NFr  i   )r!   c                  s    S )Nr   r   )r   r   r   r   ,  rS   zIEventLoopTestsMixin.test_write_pipe_disconnect_on_close.<locals>.<lambda>r7   r  r?   )r   r   r   r  r  detachrM   r!   r  r    r   r0   r|   r1   rC   r   r)   r5   )r$   ZrsockZwsockr  r   r0   r  r;   r   )r   r   #test_write_pipe_disconnect_on_close$  s    

z7EventLoopTestsMixin.test_write_pipe_disconnect_on_closer   r   c                s>  t  \}t|dd}t| jd| jfdd|}| j|\}}| | | |j	 | 
dj |d t  fdd	tj| j fd
ddd | 
d  |d tj| j fdddd | 
d  | 
dj t  | j	d j	  | jj | 
dj d S )Nr  r   )r!   c                  s    S )Nr   r   )r   r   r   r   E  rS   z4EventLoopTestsMixin.test_write_pty.<locals>.<lambda>r7   r  c                s   t  d}| |7 } t| S )Ni   )r   r   r:   )r;   r  )r  r   r   r   N  s    z2EventLoopTestsMixin.test_write_pty.<locals>.readerc                  s    dkS )NrT   r   r   )r;   r   r   r   r   S  rS   r   )r6  s   2345c                  s    dkS )Nr   r   r   )r;   r   r   r   r   X  rS   s   12345r  r?   )r   r  r  r  rM   r!   r  r    r   r0   r|   r1   rC   r   rc   r   r)   r   r   r5   )r$   r  slave_write_objr   r0   r  r   )r;   r  r   r   r   test_write_pty;  s0    



z"EventLoopTestsMixin.test_write_ptyc       
         s  t  \}t |}t| t|dd}t| jd| j	fdd|}| j
|\}}| | | |j | ddgj | dj t|dd}t| jd| jfd	d|}| j
|\}	}| | | |	j | dj t  fd
d|	d tj| j fdddd | d  | ddgj | dj t d tj| jfdddd | ddgj | dj | dj |	d tj| j fdddd | d  | ddgj | dj t d tj| jfdddd | ddgj | dj | dj t  |  | j
j | ddddgj |	  | j
j | dj d S )Nr  r   )r!   c                  s    S )Nr   r   )r  r   r   r   s  rS   z<EventLoopTestsMixin.test_bidirectional_pty.<locals>.<lambda>r/   r7   r  c                  s    S )Nr   r   )r  r   r   r   ~  rS   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 )NrT   r   r   )r;   r   r   r   r     rS   r   )r6     ac                  s
    j dkS )NrT   )r2   r   )r  r   r   r     rS   rT   s   2345c                  s    dkS )Nr   r   r   )r;   r   r   r   r     rS   s   12345s   bcdec                  s
    j dkS )Nr   )r2   r   )r  r   r   r     rS   r   r=   r?   )r   r  dupttyZsetrawr  r  rK   r!   r  r    r   r0   r|   r1   r2   rM   r  r   rC   rc   r   r)   r5   )
r$   Z
read_slaveZwrite_slaveZslave_read_objZread_connectr  r  r  Zwrite_connectr  r   )r;   r  r  r   r  r   test_bidirectional_ptyg  sh    




z*EventLoopTestsMixin.test_bidirectional_ptyc                s   t  \}}|d jj|d t dd }|d k	rL|j  fdd}t	
 }tj| jd}j  t	
 | }|d | d tj j |d k	rʈ|j j| |  |  d S )	NFrT   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   CancelledErrorrs   )r   )r   r$   r   r   main  s    
z:EventLoopTestsMixin.test_prompt_cancellation.<locals>.main)r!   g?r  )r   r   r   r!   create_taskr   getattrrg   Zpendingrn   rz   r   rH  r{   Z
assertLessr|   resultrk   r  r   rP  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 )NrT   )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   ro   )r!   )r$   r   r   wait  s    z7EventLoopTestsMixin.test_timeout_rounding.<locals>.wait   )Zclock_resolutionZselector)	r!   r  r  r    ZassertLessEqualZ_clock_resolutionZ	_selectorrD   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     rS   zCEventLoopTestsMixin.test_remove_fds_after_closing.<locals>.<lambda>)
ra   r   r   r@  r)   r   r   r   r   r   )r$   r!   rx   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     rS   z@EventLoopTestsMixin.test_add_fds_after_closing.<locals>.<lambda>)	ra   r   r   r@  r)   rk   rl   r   r   )r$   r!   rx   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   rj   r!   rk   rl   r    )r$   r  rB  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     rS   z0EventLoopTestsMixin.test_close.<locals>.<lambda>)r!   g      ?g        )r!   r)   r   rj   r@  rk   rl   r{   r3   r    r   r   ry   call_atrn   r  r   r   SIGTERMr   r*   next)r$   r  funcrB  futitr   r   r   
test_close  s2    
"zEventLoopTestsMixin.test_close)T)TN)TN)NN)N)N)N)]r+   r,   r-   r`   re   rm   rr   ru   r}   r   r   r   r   r   r   r   r   r   r   r   Zskip_unless_bind_unix_socketr   r   r   r;  
skipUnlessr  r   r   r   r   r   r   r   r  r  r  r  skipIfr	  r   r"  r'  r-  r9  r?  rC  r   r  rT  rU  rW  rX  r   r[  rb  rc  rd  rk  rl  rm  rq  rr  r|  r}  r  r  r  r  r  ZIPV6_ENABLEDr  r  r  r  r  r   r   r  r  r  r  r  requires_mac_verr  r  r  r  r  r  r  r  rE   r   r   )rD   r   r_      s   		$,
  

$


8
$	


 ).!!"&(E#	r_   c               @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zeejdkddd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#S )$SubprocessTestsMixinc             C   s,   t jdkr| |t n| tj | d S )Nr   )r   r   r   r   r|   r   r  )r$   rY   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$   rY   r   r   r   check_killed=  s    
z!SubprocessTestsMixin.check_killedc          	   C   s   t jt jtd}| jtt	| jt
j|}| j|\}}| |t	 | j|j | d|j |d}|d | j|jd   t  |  W d Q R X | j|j | |j | d|jd  d S )Nzecho.pyr7   r   s   Python The WinnerrT   )r   r\  r   dirname__file__r!   subprocess_execr  r  rN   r   
executabler    r   r4   r|   r1   get_pipe_transportrC   rZ   r  rc   r  r)   rV   r  rY   r;   )r$   progr   transpr   stdinr   r   r   test_subprocess_execD  s     



z)SubprocessTestsMixin.test_subprocess_execc          	   C   s   t jt jtd}| jtt	| jt
j|}| j|\}}| |t	 | j|j | d|j |d}|d | j|jd   |jd   | d|jd  |d | j|jd   | d|jd  t  |  W d Q R X | j|j | |j d S )Nzecho.pyr7   r   s   Python rT   s
   The Winners   Python The Winner)r   r\  r   r  r  r!   r  r  r  rN   r   r  r    r   r4   r|   r1   r  rC   rZ   r  clearr;   rc   r  r)   rV   r  rY   )r$   r  r   r  r   r  r   r   r   test_subprocess_interactiveX  s(    




z0SubprocessTestsMixin.test_subprocess_interactivec             C   s   | j tt| j d}| j |\}}| |t | j |j |d	  | j |j
 | d|j | tdd |j D  | |jd dd | |jd d	 |	  d S )
Nzecho Pythonr   c             s   s   | ]}|  V  qd S )N)r5   )rO   r   r   r   r   	<genexpr>}  s    z=SubprocessTestsMixin.test_subprocess_shell.<locals>.<genexpr>rT   s   
s   PythonrU   rS   )r!   subprocess_shellr  r  rN   r    r   r4   r  r)   rV   r|   rY   rg   allrX   valuesr;   rstrip)r$   r   r  r   r   r   r   test_subprocess_shellr  s    z*SubprocessTestsMixin.test_subprocess_shellc             C   sd   | j jtt| j dd d d d}| j |\}}| |t | j |j | d|j	 |
  d S )Nzexit 7)r  stdoutstderr   )r!   r  r  r  rN   r    r   rV   r|   rY   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   rT   rU   r  )r!   r  r  r  rN   r    r   rz  r  rV   r|   rY   r)   )r$   r   r  r   r   r   r   "test_subprocess_close_after_finish  s    z7SubprocessTestsMixin.test_subprocess_close_after_finishc             C   s   t jt jtd}| jtt	| jt
j|}| j|\}}| |t	 | j|j |  | j|j | |j |  d S )Nzecho.py)r   r\  r   r  r  r!   r  r  r  rN   r   r  r    r   r4   r   rV   r  rY   r)   )r$   r  r   r  r   r   r   r   test_subprocess_kill  s    
z)SubprocessTestsMixin.test_subprocess_killc             C   s   t jt jtd}| jtt	| jt
j|}| j|\}}| |t	 | j|j |  | j|j | |j |  d S )Nzecho.py)r   r\  r   r  r  r!   r  r  r  rN   r   r  r    r   r4   Z	terminaterV   r  rY   r)   )r$   r  r   r  r   r   r   r   test_subprocess_terminate  s    
z.SubprocessTestsMixin.test_subprocess_terminater   zDon't have SIGHUPc          
   C   s   t  t jt j}ztjtjtd}| j	t
t| jtj|}| j|\}}| |t | j|j |t j | j|j | t j |j |  W d t  t j| X d S )Nzecho.py)r   SIGHUPSIG_DFLr   r\  r   r  r  r!   r  r  r  rN   r   r  r    r   r4   Zsend_signalrV   r|   rY   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:testrT   rU   s   ERR:test)r   r\  r   r  r  r!   r  r  r  rN   r   r  r    r   r4   r  rC   rV   r)   r|   r;   rg   r   rY   )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   rT   rU   s   tests   OUT:testERR:testrS   )r   r\  r   r  r  r!   r  r  r  rN   r   r  
subprocessZSTDOUTr    r   r4   r  r   rz  rC   rV   rg   r;   r   r|   r)   rY   )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   rT   s   tests   OUT:tests   xxxrU   r   s   ERR:BrokenPipeErrors   ERR:OSError)r   r\  r   r  r  r!   r  r  r  rN   r   r  r    r   r4   r  rC   rZ   r  r|   r;   r)   rX   r   rc   r  rV   r  rY   )r$   r  r   r  r   r  r  r   r   r   #test_subprocess_close_client_stream  s.    






z8SubprocessTestsMixin.test_subprocess_close_client_streamc             c   s`   | j jtt| j dd d d dd}| j |V \}}| |t | j |j | d|j	 d S )Nzexit 7T)r  r  r  Zstart_new_sessionr  )
r!   r  r  r  rN   r    r   rV   r|   rY   )r$   r   r8  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)rI  )r$   r   r   r   '  s    zGSubprocessTestsMixin.test_subprocess_exec_invalid_args.<locals>.connectT)universal_newlinesi   )bufsize)shell)rk   r   r!   r    )r$   r   r   )r$   r   !test_subprocess_exec_invalid_args%  s    z6SubprocessTestsMixin.test_subprocess_exec_invalid_argsc          	      s   d fdd	}  t  j|ddg W d Q R X   t  j|dd W d Q R X   t  j|dd W d Q R X   t  j|d	d
 W d Q R X d S )Nc                s&   | sd}  j jtj| f|I d H  d S )Nr  )r!   r  r   r  )cmdrI  )r$   r   r   r   5  s
    zHSubprocessTestsMixin.test_subprocess_shell_invalid_args.<locals>.connectZlsz-lT)r  i   )r  F)r  )N)rk   r   r!   r    )r$   r   r   )r$   r   "test_subprocess_shell_invalid_args3  s    z7SubprocessTestsMixin.test_subprocess_shell_invalid_argsN)r+   r,   r-   r  r  r  r  r  r  r  r  r  r;  r  r   r   r  r  r  r  r  r  r  r   r   r   r   r  4  s    
!r  c                   sP   e Zd Zdd ZdZe fddZe fddZ fdd	Zd
d Z	  Z
S )SendfileBases   SendfileBaseDatai   i   c          	      s2   t tjd}|| j W d Q R X t   d S )Nr  )r  r   TESTFNrC   DATArB   
setUpClass)clsfp)rD   r   r   r  N  s    zSendfileBase.setUpClassc                s   t t j t   d S )N)r   r^  r  rB   tearDownClass)r  )rD   r   r   r  T  s    zSendfileBase.tearDownClassc                s*   t tjd| _| | jj t   d S )Nr  )r  r   r  filer@  r)   rB   r`   )r$   )rD   r   r   r`   Y  s    zSendfileBase.setUpc             C   s   | j |S )N)r!   r    )r$   rB  r   r   r   run_loop^  s    zSendfileBase.run_loop)r+   r,   r-   r  BUF_SIZEclassmethodr  r  r`   r
  rE   r   r   )rD   r   r  F  s   r  c                   s   e Zd ZG dd dejZe fddZe fddZddd	Z	d
d Z
dddZdd Zdd Zdd Zdd Zdd Z  ZS )SockSendfileMixinc               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )zSockSendfileMixin.MyProtoc             C   s(   d| _ d| _t | _| | _d | _d S )NF)startedclosedr   r;   create_futurer  r0   )r$   r!   r   r   r   r6   f  s
    
z"SockSendfileMixin.MyProto.__init__c             C   s   d| _ || _d S )NT)r  r0   )r$   r0   r   r   r   r9   m  s    z)SockSendfileMixin.MyProto.connection_madec             C   s   | j | d S )N)r;   r   )r$   r;   r   r   r   r<   q  s    z'SockSendfileMixin.MyProto.data_receivedc             C   s   d| _ | jd  d S )NT)r  r  r8   )r$   r'   r   r   r   r@   t  s    z)SockSendfileMixin.MyProto.connection_lostc                s   | j I d H  d S )N)r  )r$   r   r   r   wait_closedx  s    z%SockSendfileMixin.MyProto.wait_closedN)r+   r,   r-   r6   r9   r<   r@   r  r   r   r   r   rA   d  s
   rA   c                s   t j| _dt _t   d S )Ni @  )r   !SENDFILE_FALLBACK_READBUFFER_SIZE_SockSendfileMixin__old_bufsizerB   r  )r  )rD   r   r   r  {  s    zSockSendfileMixin.setUpClassc                s   | j t_t   d S )N)r  r   r  rB   r  )r  )rD   r   r   r    s    zSockSendfileMixin.tearDownClassTc             C   s.   t  t jt j}|d |r*| |j |S )NF)r   rE  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   rZ  Z	SO_RCVBUFr  )r$   r   r   r   r   reduce_receive_buffer_size  s    z,SockSendfileMixin.reduce_receive_buffer_sizeNc             C   s.   | tjtj| j |d k	r*|j| jd d S )N)Zhigh)r  r   rZ  Z	SO_SNDBUFr  Zset_write_buffer_limits)r$   r   r0   r   r   r   reduce_send_buffer_size  s    z)SockSendfileMixin.reduce_send_buffer_sizec                s    j t }jdd}|tj|f jj fdd|d	|  }j
|d|f |  fdd}| | fS )	NF)r  c                  s    S )Nr   r   )r   r   r   r     rS   z8SockSendfileMixin.prepare_socksendfile.<locals>.<lambda>)r   z	127.0.0.1c                  s<    j d k	r" j          d S )N)r0   r)   r
  r  r   )r   r$   rS  r   r   r    s
    

z7SockSendfileMixin.prepare_socksendfile.<locals>.cleanup)rA   r!   r   r$  r  r   HOSTr
  rQ  r  r   r  r@  )r$   r%  srv_sockr   r  r   )r   r$   rS  r   prepare_socksendfile  s    




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

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