
    ۇehs@                        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             Zd Z G d	 d
 eej                            Z G d dej        e          Zdad dZd dZd dZd Zd ddddddddej         ej!        ej"        ddZ# G d de          Z$ G d de$          Z%d Z& G d de$          Z' G d de$          Z(dS )!    Ncluster)
connection)pool   )fuzzerc               #      K   d } t          j        d          }|                    |            	 d V  |                    |            d S # |                    |            w xY w)Nc                 V    |                                  }|                    d           S )Nz
Executing )
getMessage
startswith)
log_recordmsgs     L/var/www/api/venv/lib/python3.11/site-packages/asyncpg/_testbase/__init__.pyfltz.silence_asyncio_long_exec_warning.<locals>.flt    s(    ##%%>>,////    asyncio)logging	getLogger	addFilterremoveFilter)r   loggers     r   !silence_asyncio_long_exec_warningr      s}      0 0 0 y))F
S!C     C    s   A A"c                       fd}|S )Nc                     | _         | S N)__timeout__)functimeouts    r   wrapzwith_timeout.<locals>.wrap-   s    "r    )r   r   s   ` r   with_timeoutr!   ,   s#         Kr   c                   8     e Zd ZdZed             Z fdZ xZS )TestCaseMetaNc              #   P  K   | D ]U}t          |          D ]C}|                    d          st          ||          }t          j        |          s=||fV  DV|                                D ]6\  }}|                    d          st          j        |          s0||fV  7d S )Ntest_)dirr   getattrinspectiscoroutinefunctionitems)basesnsbasemethnamemeths        r   _iter_methodszTestCaseMeta._iter_methods7   s       		% 		%DII % %**733 tX..2488 n$$$$% !hhjj 	! 	!NHd&&w// .t44 D.    	! 	!r   c                                            ||          D ],\  }}t          j        |          |d fd
            }|||<   -t                                           |||          S )N)__meth__c                `    || g|R i |}t          |dj                  }|rnt          j        ||          }	 | j                            |           d S # t          j        $ r* |                     d                    |                    d w xY w| j                            |           d S )Nr   ztest timed out after {} seconds)	r'   TEST_TIMEOUTr   wait_forlooprun_until_completeTimeoutErrorfailureExceptionformat)selfr2   argskwargscoror   mclss         r   wrapperz%TestCaseMeta.__new__.<locals>.wrapperO   s    x6t666v66!(M4;LMM 	7"+D'::D4	44T:::::"/ 4 4 4"33=DD ') )* */344
 I0066666s   A 9B)r0   	functoolswrapssuper__new__)r?   namer+   r,   r.   r/   r@   	__class__s   `      r   rD   zTestCaseMeta.__new__M   s    "00;; 	# 	#NHd_T"".2 7 7 7 7 7 7 #"7 #BxLLwwtT5"555r   )__name__
__module____qualname__r4   staticmethodr0   rD   __classcell__rF   s   @r   r#   r#   4   sV        L! ! \!*6 6 6 6 6 6 6 6 6r   r#   c                       e Zd Zed             Zed             Zd Zd Zej	        d             Z
ej	        defd            Zd Zd	 Zd
S )TestCasec                     t           j                            d          r*dd l}t	          j        |                                           t	          j                    }t	          j        d            || _	        d S )N
USE_UVLOOPr   )
osenvirongetuvloopr   set_event_loop_policyEventLoopPolicynew_event_loopset_event_loopr6   )clsrT   r6   s      r   
setUpClasszTestCase.setUpClassd   si    :>>,'' 	DMMM)&*@*@*B*BCCC%''t$$$r   c                 `    | j                                          t          j        d            d S r   )r6   closer   rX   rY   s    r   tearDownClasszTestCase.tearDownClassn   s+    t$$$$$r   c                 R    | j                             | j                   g | _        d S r   )r6   set_exception_handlerloop_exception_handler_TestCase__unhandled_exceptions)r;   s    r   setUpzTestCase.setUps   s)    	''(CDDD&(###r   c                    g }| j         D ]d}t          |t                    r8t          j                            |d          }|j        d         j        dk    rO|                    |           e|rpg }t          |          D ]1\  }}|                    | 
                    ||dz                        2|                     dd                    |          z              d S d S )NF)lookup_lines_call_connection_lostr   z,unexpected exceptions in asynchronous code:

)rb   
isinstanceConnectionResetError	tracebackTracebackExceptionfrom_exceptionstackrE   append	enumerate_format_loop_exceptionfailjoin)r;   excsexctexc	formattedicontexts          r   tearDownzTestCase.tearDownw   s   . 		 		C#344  3BBe C - -:b>&*AAA KK 	&I'oo N N
7  !<!<Wa!e!L!LMMMMII?		)$$%& & & & &	& 	&r   c              #   @  K   t          j                    }	 d V  t          j                    |z
  }||k    r#t          d                    ||                    d S # t          j                    |z
  }||k    r#t          d                    ||                    w xY w)NzQrunning block took {:0.3f}s which is longer than the expected maximum of {:0.3f}s)time	monotonicAssertionErrorr:   )r;   deltastelapseds       r   assertRunUnderzTestCase.assertRunUnder   s      ^	)EEEn&&+G$<<BF=( =() ) )  n&&+G$<<BF=( =() ) ) ) ) ) )s   A ABmsg_rec              #     K   g fd}| j                                         }| j                             |           	 d V  D ]K}|                    d          }|r2t	          j        ||          r | j                             |           d S Lt          d                    |                    # | j                             |           w xY w)Nc                 2                         |           d S r   )ro   )r6   ctxcontextss     r   handlerz6TestCase.assertLoopErrorHandlerCalled.<locals>.handler   s    OOC     r   messagezFno message matching {!r} was logged with loop.call_exception_handler())r6   get_exception_handlerr`   rS   researchr~   r:   )r;   r   r   old_handlerr   r   r   s         @r   assertLoopErrorHandlerCalledz%TestCase.assertLoopErrorHandlerCalled   s     	! 	! 	! 	! 	! i5577	''000	9EEE  ggi(( 29VS11  I++K88888 !006v@ @ @
 I++K8888s   5B3 #B3 3Cc                 d    | j                             |           |                    |           d S r   )rb   ro   default_exception_handler)r;   r6   ry   s      r   ra   zTestCase.loop_exception_handler   s3    #**7333&&w/////r   c                    |                     dd          }|                     d          }|t          |          ||j        f}nd }g }t          |          D ]}|dv r||         }|dk    rAd                    t          j        |                    }	d}||	                                z  }nK	 t          |          }n:# t          $ r-}
d
                    |
t          |                    }Y d }
~
nd }
~
ww xY w|                    d	
                    ||                     |^|                    d
           t          j        d                    t          j        |           d          }|                    |           t          j        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}. {}:
{}
)rS   type__traceback__sortedrs   rk   format_listrstriprepr	Exceptionr:   ro   textwrapindentformat_exception)r;   ry   nr   r   exc_infolineskeyvaluetbexformatted_excdetailss                r   rq   zTestCase._format_loop_exception   s   ++i)LMMKK,,	 YI4KLHHH'?? 	< 	<C...CLE(((WWY2599::F$I KKEE  I I I006r4;;0G0G EEEEEI LL..sE::;;;;LL)***$O	2H=>>F FMLL'''/"''%..&99"))!Wg>>>s   0C  
C7
#C22C7N)rG   rH   rI   classmethodrZ   r^   rc   rz   
contextlibcontextmanagerr   strr   ra   rq   r    r   r   rN   rN   b   s          [ % % [%) ) )& & &. 
) 
) 
) 93 9 9 9 9.0 0 0 ?  ?  ?  ?  ?r   rN   )	metaclassc                      | di |} |j         di |pi  |                                 t          j        t          |           |S )Nr    )inittrust_local_connectionsatexitregister_shutdown_cluster)
ClusterClscluster_kwargsinitdb_optionsr   s       r   _init_clusterr      s^    j**>**GGL**N(b***##%%%
O%w///Nr   c                 @    | si } nt          |           } d| vrd| d<   | S )Nusernamepostgres)dict)r   s    r   _get_initdb_optionsr      s;     .n-- ''%/z"r   c                     t           `t          j                            d          }|rt	          j                    a n+t          t          j        ddit          |                     a t           S )NPGHOSTdata_dir_suffixz.apgtestr   r   )	_default_clusterrQ   rR   rS   
pg_clusterRunningClusterr   TempClusterr   )r   pg_hosts     r   _init_default_clusterr      sq     *..** 
	)8::,&%z   3>BB      r   c                     |                                  dk    r|                                  |                                  dk    r|                                  d S d S )Nrunningznot-initialized)
get_statusstopdestroyr   s    r   r   r     s\    y((000 10r   
   iP  g      N@)min_sizemax_sizemax_queries max_inactive_connection_lifetimeconnectsetupr   r6   
pool_classconnection_classrecord_classc                *     |	| f|||||||||
|d
|S )N)
r   r   r   r6   r   r   r   r   r   r   r    )dsnr   r   r   r   r   r   r   r6   r   r   r   connect_kwargss                r   create_poolr     sM     :)I)!    r   c                       e Zd Zed             Zei i dd            Zei dd            Zed             Ze fd            Ze fd            Z	ei fd	            Z
ed
             Z fdZ fdZej        ej        fdZ xZS )ClusterTestCasec                 R    ddi}| j                                         dk    rd|d<   |S )Nlog_connectionson)   r   offjit)r   get_pg_version)rY   settingss     r   get_server_settingsz#ClusterTestCase.get_server_settings+  s;     t
 ;%%''722 $HUOr   r   c                v    t          ||t          |                    }| j                            |           |S r   )r   r   	_clustersro   )rY   r   r   r   r   s        r   new_clusterzClusterTestCase.new_cluster8  s=    
N 3N C CE EW%%%r   server_settingsc                4    |                     d|           d S )Ndynamicportr   )start)rY   r   r   s      r   start_clusterzClusterTestCase.start_cluster?  s    9oFFFFFr   c                     t                      | _        | j                                        dk    r0| j                            d|                                            d S d S )Nr   r   r   )r   r   r   r   r   r]   s    r   setup_clusterzClusterTestCase.setup_clusterC  so    +--;!!##y00K0G0G0I0I  K K K K K 10r   c                 ~    t                                                       g | _        |                                  d S r   )rC   rZ   r   r   rY   rF   s    r   rZ   zClusterTestCase.setUpClassK  s8    r   c                     t                                                       | j        D ]3}|t          ur(|                                 |                                 4g | _        d S r   )rC   r^   r   r   r   r   )rY   r   rF   s     r   r^   zClusterTestCase.tearDownClassQ  s_    } 	" 	"G...!!!r   c                 B   | 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_specrS   popupdaterQ   rR   rY   r=   	conn_specs      r   r   z#ClusterTestCase.get_connection_specZ  s    K3355	::e 	"MM&!!!   z~~h'' 	/

50A0A 	/**(2	*%Y&&$.	&!r   c                 `    |                      |          }t          j        di |d| j        iS Nr6   r    )r   pg_connectionr   r6   r   s      r   r   zClusterTestCase.connectg  s9    ++F33	$@@y@@sx@@@@r   c                 V    t                                                       g | _        d S r   )rC   rc   _poolsr;   rF   s    r   rc   zClusterTestCase.setUpl  s    r   c                     t                                                       | j        D ]}|                                 g | _        d S r   )rC   rz   r  	terminate)r;   r   rF   s     r   rz   zClusterTestCase.tearDownp  sH    K 	 	DNNr   c                     |                      |          }t          d| j        ||d|}| j                            |           |S )N)r6   r   r   r    )r   r   r6   r  ro   )r;   r   r   r=   r   r   s         r   r   zClusterTestCase.create_poolv  s_    ,,V44	 K	j,<K K@IK K4   r   )rG   rH   rI   r   r   r   r   r   rZ   r^   r   r   rc   rz   pg_poolPoolr   
Connectionr   rK   rL   s   @r   r   r   *  s       
 
 [
 79"     [ 79 G G G G [G K K [K     [
     [ (* 
 
 
 [
 A A [A         &-\%2%=       r   r   c                        e Zd Ze fd            Zed             Ze fd            Ze fd            Ze fd            Z fdZ	 xZ
S )ProxiedClusterTestCasec                 l    t          t                                                                }d|d<   |S )N	127.0.0.1listen_addresses)r   rC   r   )rY   r   rF   s     r   r   z*ProxiedClusterTestCase.get_server_settings  s0    335566'2#$r   c                 
    dd iS )Nzfuzzing-moder    r]   s    r   get_proxy_settingsz)ProxiedClusterTestCase.get_proxy_settings  s    %%r   c                 P   t                                                       | j                                        }|                    d          }|sd}n|                    d          rd}t          j        ||d                   | _        | j        	                                 d S )Nr   r  /r   )backend_hostbackend_port)
rC   rZ   r   r   rS   r   r   TCPFuzzingProxyproxyr   )rY   r   r   rF   s      r   rZ   z!ProxiedClusterTestCase.setUpClass  s    K3355	}}V$$ 	DD__S!! 	D*"6*
 
 
	 		r   c                 z    | j                                          t                                                       d S r   )r  r   rC   r^   r   s    r   r^   z$ProxiedClusterTestCase.tearDownClass  s1    	r   c                     t                                          |          }| j        j        |d<   | j        j        |d<   |S )Nr   r   )rC   r   r  listening_addrlistening_port)rY   r=   r   rF   s      r   r   z*ProxiedClusterTestCase.get_connection_spec  s>    GG//77	I4	&I4	&r   c                 z    | j                                          t                                                       d S r   )r  resetrC   rz   r  s    r   rz   zProxiedClusterTestCase.tearDown  s3    
r   )rG   rH   rI   r   r   r  rZ   r^   r   rz   rK   rL   s   @r   r  r    s            [
 & & [&     [         [      [        r   r  c                  4      st          d           fd}|S )Nz$no connection options were specifiedc                     | _         | S r   )__connect_options__)r   optionss    r   r   z%with_connection_options.<locals>.wrap  s    #* r   )
ValueError)r   r   s   ` r   with_connection_optionsr"    s;     A?@@@     Kr   c                   (     e Zd Z fdZ fdZ xZS )ConnectedTestCasec                 .   t                                                       t          | | j                  j        }t          |di           }| j                             | j        di |          | _        | j        	                                | _
        d S )Nr  r    )rC   rc   r'   _testMethodName__func__r6   r7   r   conget_server_versionserver_version)r;   	test_funcoptsrF   s      r   rc   zConnectedTestCase.setUp  s     D$"677@	y"7<<9//0D0Dt0D0DEE"h99;;r   c                    	 | j                             | j                                                   d | _        t	                                                       d S # t	                                                       w xY wr   )r6   r7   r(  r\   rC   rz   r  s    r   rz   zConnectedTestCase.tearDown  si    	I(()9)9:::DHGGEGGs   8A "A?)rG   rH   rI   rc   rz   rK   rL   s   @r   r$  r$    sQ        < < < < <        r   r$  c                       e Zd Zed             Zei fd            Zei fd            Zed             Zed             ZdS )HotStandbyTestCasec                    |                      t          j                  | _        |                     | j        ddd           d }	 | j                            | j                            dd| j                            }| j                            |                    d                     | j        	                    d           | j        
                                }|                      t          j        |dd	
          | _        |                     | j        ddi           |.| j                            |                                           d S d S # |-| j                            |                                           w w xY w)Nr   hot_standby)max_wal_senders	wal_levelr   r   )r   r   r6   zT
                    CREATE ROLE replication WITH LOGIN REPLICATION
                replication)masterreplication_user)r   T)r   r   r   master_clusterr   r6   r7   r   executetrust_local_replication_byr   HotStandbyClusterstandby_clusterr\   )rY   r(  r   s      r   r   z HotStandbyTestCase.setup_cluster  s    __Z-CDD#%*  	 	
 	
 	
 	9(--"**'jsx + I IJ JC H''     
 99-HHH*>>@@I"%//,'(5    #2 # #C #!4!     ++CIIKK88888 s++CIIKK8888 s   C!E 1Fc                 8   |                                 }|                    d          r|                    d           |                    |           t          j                            d          s'|                    d          sd|vrd|d<   d|vrd|d<   |S r   )r   rS   r   r   rQ   rR   )rY   r   r=   r   s       r   get_cluster_connection_specz.HotStandbyTestCase.get_cluster_connection_spec  s    //11	::e 	"MM&!!!   z~~h'' 	/

50A0A 	/**(2	*%Y&&$.	&!r   c                     |                      | j        |          }|                      | j        |          }|d         |d         g|d         |d         g|d         |d         d|S )Nr   r   r   r   )r   r   r   r   )r=  r7  r;  )rY   r=   primary_specstandby_specs       r   r   z&HotStandbyTestCase.get_connection_spec  s    66
 
 66
 
 "&)<+?@!&)<+?@$Z0 (	
 

 
 	
r   c                 l    |                      | j        |          }t          j        di |d| j        iS r   )r=  r7  r   r   r6   r   s      r   connect_primaryz"HotStandbyTestCase.connect_primary  s>    33C4FOO	$@@y@@sx@@@@r   c                 l    |                      | j        |          }t          j        di |d| j        iS r   )r=  r;  r   r   r6   r   s      r   connect_standbyz"HotStandbyTestCase.connect_standby  sF    33
 
	 $@@y@@sx@@@@r   N)	rG   rH   rI   r   r   r=  r   rB  rD  r    r   r   r/  r/    s        *9 *9 [*9X 9; 
 
 
 [
 (* 
 
 
 [
 A A [A A A [A A Ar   r/  r   ))r   r   r   rA   r(   r   rQ   r   r   r|   rk   unittestasyncpgr   r   r   r   r   r  r   r   r   r   r!   r   rN   r#   r   r   r   r   r   r  r	  Recordr   r   r  r"  r$  r/  r    r   r   <module>rH     s               				 				         ) ) ) ) ) ) / / / / / / # # # # # #       
! 
! 
!  +6 +6 +6 +6 +644)** +6 +6 +6\u? u? u? u? u?x L u? u? u? u?p     
 
 
 
   (  !15"<!.!9$^    :R R R R Rh R R Rj( ( ( ( (_ ( ( (V         &WA WA WA WA WA WA WA WA WA WAr   