a
    ze7                     @   s   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d dl	m
Z
 G d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ZdS )    N)supportc                   @   s   e Zd Zdd ZddddZdd Zd	d
 Zdd Zej	de
jdddddZej	e
jfddZdd Zdd Zejdd Zdd ZdS )FunctionalTestCaseMixinc                 C   s   t  S N)asyncioZnew_event_loopself r   2/usr/lib/python3.9/test/test_asyncio/functional.pynew_loop   s    z FunctionalTestCaseMixin.new_loopg{Gz?)delayc                C   s   | j t| d S r   )loopZrun_until_completer   sleep)r   r   r   r   r	   run_loop_briefly   s    z(FunctionalTestCaseMixin.run_loop_brieflyc                 C   s   | j | | j| d S r   )._FunctionalTestCaseMixin__unhandled_exceptionsappendr   Zdefault_exception_handler)r   r   contextr   r   r	   loop_exception_handler   s    z.FunctionalTestCaseMixin.loop_exception_handlerc                 C   s,   |   | _td  | j| j g | _d S r   )r
   r   r   set_event_loopZset_exception_handlerr   r   r   r   r   r	   setUp   s    

zFunctionalTestCaseMixin.setUpc              	   C   sZ   zB| j   | jr0td t| j | d W td  d | _ ntd  d | _ 0 d S )Nz2Unexpected calls to loop.call_exception_handler():z1unexpected calls to loop.call_exception_handler())r   closer   printpprintfailr   r   r   r   r   r	   tearDown    s    


z FunctionalTestCaseMixin.tearDownN   
   )familyaddrtimeoutbacklogmax_clientsc          	      C   s   |d u rPt tdrL|tjkrLt }|j}W d    qP1 s@0    Y  nd}tj|||d}|d u rptd|dkrtd|| t	| ||||S )NAF_UNIX)z	127.0.0.1r   )r   r   timeout is requiredr   #only blocking sockets are supported)
hasattrsocketr!   tempfileNamedTemporaryFilenamecreate_serverRuntimeError
settimeoutTestThreadedServer)	r   Zserver_progr   r   r   r   r    tmpsockr   r   r	   
tcp_server-   s    
&

z"FunctionalTestCaseMixin.tcp_serverc                 C   sF   t  |t j}|d u rtd|dkr.td|| t| |||S )Nr"   r   r#   )r%   SOCK_STREAMr*   r+   TestThreadedClient)r   Zclient_progr   r   r.   r   r   r	   
tcp_clientE   s    
z"FunctionalTestCaseMixin.tcp_clientc                 O   s$   t tdst| j|dtji|S Nr!   r   )r$   r%   NotImplementedErrorr/   r!   r   argskwargsr   r   r	   unix_serverT   s    
z#FunctionalTestCaseMixin.unix_serverc                 O   s$   t tdst| j|dtji|S r3   )r$   r%   r4   r2   r!   r5   r   r   r	   unix_clientY   s    
z#FunctionalTestCaseMixin.unix_clientc                 c   s   t  p}tj|d}z,|V  W zt| W qj tyB   Y qj0 n$zt| W n tyf   Y n0 0 W d    n1 s~0    Y  d S )Nr.   )r&   TemporaryDirectoryospathjoinunlinkOSError)r   Ztdfnr   r   r	   unix_sock_name^   s    
z&FunctionalTestCaseMixin.unix_sock_namec              	   C   s*   z| j   W | | n| | 0 d S r   )r   stopr   r   exr   r   r	   _abort_socket_testj   s    z*FunctionalTestCaseMixin._abort_socket_test)__name__
__module____qualname__r
   r   r   r   r   r%   AF_INETr   ZLOOPBACK_TIMEOUTr/   r2   r8   r9   
contextlibcontextmanagerrA   rE   r   r   r   r	   r      s&   

r   c                   @   s<   e Zd Zdd Zdd Zddddd	Zd
d Zdd ZdS )TestSocketWrapperc                 C   s
   || _ d S r   )_TestSocketWrapper__sockr   r.   r   r   r	   __init__x   s    zTestSocketWrapper.__init__c                 C   s<   d}t ||k r8| |t | }|dkr.t||7 }q|S )N    )lenrecvConnectionAbortedError)r   nbufdatar   r   r	   recv_all{   s    
zTestSocketWrapper.recv_allFN)server_sideserver_hostnamec                C   s^   |j | j||dd}z2z|  W n   |   Y n0 W | j  n| j  0 || _d S )NF)rX   rY   Zdo_handshake_on_connect)Zwrap_socketrM   Zdo_handshaker   )r   Zssl_contextrX   rY   Zssl_sockr   r   r	   	start_tls   s    
zTestSocketWrapper.start_tlsc                 C   s   t | j|S r   )getattrrM   )r   r(   r   r   r	   __getattr__   s    zTestSocketWrapper.__getattr__c                 C   s   d t| j| jS )Nz	<{} {!r}>)formattyperF   rM   r   r   r   r	   __repr__   s    zTestSocketWrapper.__repr__)rF   rG   rH   rO   rW   rZ   r\   r_   r   r   r   r	   rL   v   s   
rL   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )SocketThreadc                 C   s   d| _ |   d S NF)_activer=   r   r   r   r	   rB      s    zSocketThread.stopc                 C   s   |    | S r   )startr   r   r   r	   	__enter__   s    zSocketThread.__enter__c                 G   s   |    d S r   )rB   )r   excr   r   r	   __exit__   s    zSocketThread.__exit__N)rF   rG   rH   rB   rd   rf   r   r   r   r	   r`      s   r`   c                   @   s   e Zd Zdd Zdd ZdS )r1   c                 C   s:   t j| d d d d| _|| _|| _d| _|| _|| _d S )Nztest-clientT)		threadingThreadrO   daemon_timeout_sockrb   _prog_test)r   testr.   progr   r   r   r	   rO      s    zTestThreadedClient.__init__c              
   C   sJ   z|  t| j W n0 tyD } z| j| W Y d }~n
d }~0 0 d S r   )rl   rL   rk   	Exceptionrm   rE   rC   r   r   r	   run   s    zTestThreadedClient.runN)rF   rG   rH   rO   rq   r   r   r   r	   r1      s   
r1   c                       sH   e Zd Zdd Z fddZdd Zdd Zd	d
 Zedd Z	  Z
S )r,   c                 C   sh   t j| d d d d| _d| _d| _|| _|| _|| _d| _	|| _
t \| _| _| jd || _d S )Nztest-serverTr   F)rg   rh   rO   ri   _clientsZ_finished_clients_max_clientsrj   rk   rb   rl   r%   
socketpair_s1_s2setblockingrm   )r   rn   r.   ro   r   r    r   r   r	   rO      s    zTestThreadedServer.__init__c                    sX   zF| j r:| j  dkr:z| j d W n ty8   Y n0 W t   nt   0 d S )Ns   stop)rv   filenosendr?   superrB   r   	__class__r   r	   rB      s    zTestThreadedServer.stopc                 C   sn   zR| j $ | j d |   W d    n1 s20    Y  W | j  | j  n| j  | j  0 d S ra   )rk   rw   _runru   r   rv   r   r   r   r	   rq      s    (

zTestThreadedServer.runc                 C   s<  | j r8| j| jkrd S t| j| jgg g | j\}}}| j|v rFd S | j|v r z| j \}}W n6 tyx   Y q Y q  t	j
y   | j sY d S  Y q 0 |  jd7  _|| j z2| | | W d    n1 s0    Y  W q  ty4 } z4d| _ z W | j| n| j| 0 W Y d }~q d }~0 0 q d S )Nr   F)rb   rr   rs   selectrk   ru   rj   acceptBlockingIOErrorr%   r   r+   _handle_clientrp   rm   rE   )r   rwxconnr   rD   r   r   r	   r~      s6    


,zTestThreadedServer._runc                 C   s   |  t| d S r   )rl   rL   rN   r   r   r	   r     s    z!TestThreadedServer._handle_clientc                 C   s
   | j  S r   )rk   getsocknamer   r   r   r	   r     s    zTestThreadedServer.addr)rF   rG   rH   rO   rB   rq   r~   r   propertyr   __classcell__r   r   r|   r	   r,      s   
	"r,   )r   Zasyncio.eventsrJ   r;   r   r   r%   r&   rg   rn   r   r   rL   rh   r`   r1   r,   r   r   r   r	   <module>   s   i(