o
    hs@                     @   sz  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	Z	d dl
Z
d 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 ejdd Zd	d
 ZG dd deejZG dd dejedZdad(ddZd(ddZd(ddZdd Zd(ddddddddej ej!ej"dddZ#G dd deZ$G d d! d!e$Z%d"d# Z&G d$d% d%e$Z'G d&d' d'e$Z(dS ))    Ncluster)
connection)pool   )fuzzerc               	   c   sB    dd } t d}||  zd V  W ||  d S ||  w )Nc                 S   s   |   }|d S )Nz
Executing )
getMessage
startswith)
log_recordmsg r   \/var/www/html/pro-man-master/venv/lib/python3.10/site-packages/asyncpg/_testbase/__init__.pyflt    s   z.silence_asyncio_long_exec_warning.<locals>.fltasyncio)logging	getLogger	addFilterremoveFilter)r   loggerr   r   r   !silence_asyncio_long_exec_warning   s   

r   c                    s    fdd}|S )Nc                    
    | _ | S N)__timeout__functimeoutr   r   wrap-      zwith_timeout.<locals>.wrapr   )r   r   r   r   r   with_timeout,   s   r   c                       s,   e Zd ZdZedd Z fddZ  ZS )TestCaseMetaNc                 c   s~    | D ]}t |D ]}|dsq	t||}t|sq	||fV  q	q| D ]\}}|ds1q't|s7q'||fV  q'd S )Ntest_)dirr	   getattrinspectiscoroutinefunctionitems)basesnsbasemethnamemethr   r   r   _iter_methods7   s"   





zTestCaseMeta._iter_methodsc                    sL     ||D ]\}}t||d fdd
}|||< qt  |||S )N)__meth__c                   sz   || g|R i |}t |d j}|r5t||}z	| j| W d S  tjy4   | d|d w | j| d S )Nr   ztest timed out after {} seconds)	r#   TEST_TIMEOUTr   wait_forlooprun_until_completeTimeoutErrorfailureExceptionformat)selfr-   argskwargscoror   mclsr   r   wrapperO   s    z%TestCaseMeta.__new__.<locals>.wrapper)r,   	functoolswrapssuper__new__)r:   namer'   r(   r*   r+   r;   	__class__r9   r   r?   M   s
   
zTestCaseMeta.__new__)__name__
__module____qualname__r.   staticmethodr,   r?   __classcell__r   r   rA   r   r    4   s
    
r    c                   @   sf   e Zd Zedd Zedd Zdd Zdd Zej	d	d
 Z
ej	defddZdd Zdd ZdS )TestCasec                 C   s>   t jdrdd l}t|  t }td  || _	d S )N
USE_UVLOOPr   )
osenvirongetuvloopr   set_event_loop_policyEventLoopPolicynew_event_loopset_event_loopr0   )clsrM   r0   r   r   r   
setUpClassd   s   

zTestCase.setUpClassc                 C   s   | j   td  d S r   )r0   closer   rQ   rR   r   r   r   tearDownClassn      
zTestCase.tearDownClassc                 C   s   | j | j g | _d S r   )r0   set_exception_handlerloop_exception_handler_TestCase__unhandled_exceptionsr5   r   r   r   setUps   s   
zTestCase.setUpc                 C   s   g }| j D ]}t|trtjj|dd}|jd jdkrq|| q|rGg }t	|D ]\}}|| 
||d  q+| dd|  d S d S )NF)lookup_lines_call_connection_lostr   z,unexpected exceptions in asynchronous code:

)rZ   
isinstanceConnectionResetError	tracebackTracebackExceptionfrom_exceptionstackr@   append	enumerate_format_loop_exceptionfailjoin)r5   excsexctexc	formattedicontextr   r   r   tearDownw   s&   

zTestCase.tearDownc                 c   sb    t  }zd V  W t  | }||krtd||d S t  | }||kr0td||w )NzQrunning block took {:0.3f}s which is longer than the expected maximum of {:0.3f}s)time	monotonicAssertionErrorr4   )r5   deltastelapsedr   r   r   assertRunUnder   s*   zTestCase.assertRunUndermsg_rec              	   #   s    g   fdd}| j  }| j | z&d V   D ]}|d}|r3t||r3 W | j | d S qtd|| j | w )Nc                    s     | d S r   )rg   )r0   ctxcontextsr   r   handler   s   z6TestCase.assertLoopErrorHandlerCalled.<locals>.handlermessagezFno message matching {!r} was logged with loop.call_exception_handler())r0   get_exception_handlerrX   rL   researchru   r4   )r5   rz   r~   old_handlerr{   r   r   r|   r   assertLoopErrorHandlerCalled   s$   

z%TestCase.assertLoopErrorHandlerCalledc                 C   s   | j | || d S r   )rZ   rg   default_exception_handler)r5   r0   rq   r   r   r   rY      s   zTestCase.loop_exception_handlerc                 C   s*  | dd}| d}|d urt|||jf}nd }g }t|D ]J}|dv r'q || }|dkr@dt|}	d}||	 7 }n!zt|}W n t	y` }
 zd
|
t|}W Y d }
~
nd }
~
ww |d	
|| q |d ur|d
 tdtj| d}|| td|d}d
|||S )Nr   z!Unhandled exception in event loop	exception>   r   r   source_traceback z+Object created at (most recent call last):
z,Exception in __repr__ {!r}; value type: {!r}z
[{}]: {}

z[exception]:
z  z    z{:02d}. {}:
{}
)rL   type__traceback__sortedrk   rc   format_listrstriprepr	Exceptionr4   rg   textwrapindentformat_exception)r5   rq   nr   r   exc_infolineskeyvaluetbexformatted_excdetailsr   r   r   ri      s>   


zTestCase._format_loop_exceptionN)rC   rD   rE   classmethodrS   rV   r\   rr   
contextlibcontextmanagerry   strr   rY   ri   r   r   r   r   rH   b   s    
	

rH   )	metaclassc                 C   s:   | di |}|j di |pi  |  tt| |S )Nr   )inittrust_local_connectionsatexitregister_shutdown_cluster)
ClusterClscluster_kwargsinitdb_optionsr   r   r   r   _init_cluster   s
   r   c                 C   s&   | si } nt | } d| vrd| d< | S )Nusernamepostgres)dict)r   r   r   r   _get_initdb_options   s   r   c                 C   s@   t d u rtjd}|rt a t S ttjddit| da t S )NPGHOSTdata_dir_suffixz.apgtestr   r   )	_default_clusterrJ   rK   rL   
pg_clusterRunningClusterr   TempClusterr   )r   pg_hostr   r   r   _init_default_cluster   s   
r   c                 C   s0   |   dkr
|   |   dkr|   d S d S )Nrunningznot-initialized)
get_statusstopdestroyr   r   r   r   r     s
   r   
   iP  g      N@)min_sizemax_sizemax_queries max_inactive_connection_lifetimeconnectsetupr   r0   
pool_classconnection_classrecord_classc                K   s&   |	| f|||||||||
|d
|S )N)
r   r   r   r0   r   r   r   r   r   r   r   )dsnr   r   r   r   r   r   r   r0   r   r   r   connect_kwargsr   r   r   create_pool  s    r   c                       s   e Zd Zedd Zei i dddZei dddZed	d
 Ze fddZe fddZ	ei fddZ
edd Z fddZ fddZejejfddZ  ZS )ClusterTestCasec                 C   s"   ddi}| j  dkrd|d< |S )Nlog_connectionson)   r   offjit)r   get_pg_versionrR   settingsr   r   r   get_server_settings+  s
   z#ClusterTestCase.get_server_settingsr   c                C   s    t ||t|}| j| |S r   )r   r   	_clustersrg   )rR   r   r   r   r   r   r   r   new_cluster8  s
   zClusterTestCase.new_clusterserver_settingsc                C   s   |j d|d d S )Ndynamicportr   )start)rR   r   r   r   r   r   start_cluster?  s   zClusterTestCase.start_clusterc                 C   s2   t  | _| j dkr| jjd|  d d S d S )Nr   r   r   )r   r   r   r   r   rU   r   r   r   setup_clusterC  s   
zClusterTestCase.setup_clusterc                    s   t    g | _|   d S r   )r>   rS   r   r   rU   rA   r   r   rS   K  s   
zClusterTestCase.setUpClassc                    s8   t    | jD ]}|tur|  |  qg | _d S r   )r>   rV   r   r   r   r   )rR   r   rA   r   r   rV   Q  s   


zClusterTestCase.tearDownClassc                 C   sb   | j  }|dr|d || tjds/|ds/d|vr'd|d< d|vr/d|d< |S Nr   hostr   databaser   user)r   get_connection_specrL   popupdaterJ   rK   rR   r7   	conn_specr   r   r   r   Z  s   



z#ClusterTestCase.get_connection_specc                 K   s$   |  |}tjdi |d| jiS Nr0   r   )r   pg_connectionr   r0   r   r   r   r   r   g  s   
zClusterTestCase.connectc                    s   t    g | _d S r   )r>   r\   _poolsr[   rA   r   r   r\   l  s   

zClusterTestCase.setUpc                    s(   t    | jD ]}|  qg | _d S r   )r>   rr   r   	terminate)r5   r   rA   r   r   rr   p  s   



zClusterTestCase.tearDownc                 K   s2   |  |}td| j||d|}| j| |S )N)r0   r   r   r   )r   r   r0   r   rg   )r5   r   r   r7   r   r   r   r   r   r   v  s   

zClusterTestCase.create_pool)rC   rD   rE   r   r   r   r   r   rS   rV   r   r   r\   rr   pg_poolPoolr   
Connectionr   rG   r   r   rA   r   r   *  s,    


r   c                       sh   e Zd Ze fddZedd Ze fddZe fddZe fd	d
Z fddZ	  Z
S )ProxiedClusterTestCasec                    s   t t  }d|d< |S )N	127.0.0.1listen_addresses)r   r>   r   r   rA   r   r   r     s   z*ProxiedClusterTestCase.get_server_settingsc                 C   s   dd iS )Nzfuzzing-moder   rU   r   r   r   get_proxy_settings  s   z)ProxiedClusterTestCase.get_proxy_settingsc                    sX   t    | j }|d}|sd}n|drd}tj||d d| _| j	  d S )Nr   r   /r   )backend_hostbackend_port)
r>   rS   r   r   rL   r	   r   TCPFuzzingProxyproxyr   )rR   r   r   rA   r   r   rS     s   



z!ProxiedClusterTestCase.setUpClassc                       | j   t   d S r   )r   r   r>   rV   rU   rA   r   r   rV     rW   z$ProxiedClusterTestCase.tearDownClassc                    s(   t  |}| jj|d< | jj|d< |S )Nr   r   )r>   r   r   listening_addrlistening_portr   rA   r   r   r     s   z*ProxiedClusterTestCase.get_connection_specc                    r   r   )r   resetr>   rr   r[   rA   r   r   rr     s   
zProxiedClusterTestCase.tearDown)rC   rD   rE   r   r   r   rS   rV   r   rr   rG   r   r   rA   r   r     s    
r   c                     s    st d fdd}|S )Nz$no connection options were specifiedc                    r   r   )__connect_options__r   optionsr   r   r     r   z%with_connection_options.<locals>.wrap)
ValueError)r   r   r   r   r   with_connection_options  s   r   c                       s(   e Zd Z fddZ fddZ  ZS )ConnectedTestCasec                    sN   t    t| | jj}t|di }| j| jdi || _| j	 | _
d S )Nr   r   )r>   r\   r#   _testMethodName__func__r0   r1   r   conget_server_versionserver_version)r5   	test_funcoptsrA   r   r   r\     s
   
zConnectedTestCase.setUpc                    s6   z| j | j  d | _W t   d S t   w r   )r0   r1   r  rT   r>   rr   r[   rA   r   r   rr     s   zConnectedTestCase.tearDown)rC   rD   rE   r\   rr   rG   r   r   rA   r   r     s    	r   c                   @   sP   e Zd Zedd Zei fddZei fddZedd Zed	d
 ZdS )HotStandbyTestCasec              
   C   s   |  tj| _| j| jdddd d }zI| j| jjdd| jd}| j|d | j	d | j
 }| j tj|dd	d
| _| j| jddid W |d ur\| j|  d S d S |d urk| j|  w w )Nr   hot_standby)max_wal_senders	wal_levelr   r   )r   r   r0   zT
                    CREATE ROLE replication WITH LOGIN REPLICATION
                replication)masterreplication_user)r   T)r   r   r   master_clusterr   r0   r1   r   executetrust_local_replication_byr   HotStandbyClusterstandby_clusterrT   )rR   r  r   r   r   r   r     sF   
z HotStandbyTestCase.setup_clusterc                 C   s`   |  }|dr|d || tjds.|ds.d|vr&d|d< d|vr.d|d< |S r   )r   rL   r   r   rJ   rK   )rR   r   r7   r   r   r   r   get_cluster_connection_spec  s   


z.HotStandbyTestCase.get_cluster_connection_specc                 C   sN   |  | j|}|  | j|}|d |d g|d |d g|d |d d|S )Nr   r   r   r   )r   r   r   r   )r  r  r  )rR   r7   primary_specstandby_specr   r   r   r     s   z&HotStandbyTestCase.get_connection_specc                 K   (   |  | j|}tjdi |d| jiS r   )r  r  r   r   r0   r   r   r   r   connect_primary  s   z"HotStandbyTestCase.connect_primaryc                 K   r  r   )r  r  r   r   r0   r   r   r   r   connect_standby  s
   z"HotStandbyTestCase.connect_standbyN)	rC   rD   rE   r   r   r  r   r  r  r   r   r   r   r    s    
,
r  r   ))r   r   r   r<   r$   r   rJ   r   r   rs   rc   unittestasyncpgr   r   r   r   r   r   r   r   r   r   r   r   rH   r    r   r   r   r   r   r   r   Recordr   r   r   r   r   r  r   r   r   r   <module>   sZ   
.x


U+