
    :"ris@                        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j2                              Z G d dej2                  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jB                  ejD                  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(y)!    Ncluster)
connection)pool   )fuzzerc               #      K   d } t        j                  d      }|j                  |        	 d  |j                  |        y # |j                  |        w xY ww)Nc                 F    | j                         }|j                  d       S )Nz
Executing )
getMessage
startswith)
log_recordmsgs     b/var/www/fortnox.pascalinesoft.com/venv/lib/python3.12/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      sO     0 y)F
S!C C s   *AA A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                   2     e Zd ZdZed        Z fdZ xZS )TestCaseMetaNc              #   :  K   | D ]L  }t        |      D ]<  }|j                  d      st        ||      }t        j                  |      s7||f > N |j                         D ]3  \  }}|j                  d      st        j                  |      s.||f 5 y w)Ntest_)dirr   getattrinspectiscoroutinefunctionitems)basesnsbasemethnamemeths        r   _iter_methodszTestCaseMeta._iter_methods7   s      		%DI %**73tX.2248n$%		% !hhj 	!NHd&&w/..t4D. 	!s   BBc                       j                  ||      D ]*  \  }}t        j                  |      |d fd
       }|||<   , t            |||      S )N)__meth__c                \    || g|i |}t        |dj                        }|r3t        j                  ||      }	 | j                  j                  |       y | j                  j                  |       y # t        j                  $ r" | j                  dj                  |            d w xY w)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    6t6v6!(M4;L;LM"++D':D4		44T: II006 #// 4"33=DD ')*/344s   A6 65B+)r0   	functoolswrapssuper__new__)r?   namer+   r,   r.   r/   r@   	__class__s   `      r   rD   zTestCaseMeta.__new__M   sc    "00; 	#NHd__T".2 7 #7 #BxL	#" wtT5"55r   )__name__
__module____qualname__r4   staticmethodr0   rD   __classcell__rF   s   @r   r#   r#   4   s$    L! !*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y
)TestCasec                     t         j                  j                  d      r'dd l}t	        j
                  |j                                t	        j                         }t	        j                  d        || _	        y )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   sO    ::>>,'))&*@*@*BC%%'t$r   c                 b    | j                   j                          t        j                  d        y r   )r6   closer   rX   rY   s    r   tearDownClasszTestCase.tearDownClassn   s    t$r   c                 \    | j                   j                  | j                         g | _        y r   )r6   set_exception_handlerloop_exception_handler_TestCase__unhandled_exceptions)r;   s    r   setUpzTestCase.setUps   s"    		''(C(CD&(#r   c                    g }| j                   D ]a  }t        |t              r>t        j                  j                  |d      }|j                  d   j                  dk(  rQ|j                  |       c |r]g }t        |      D ])  \  }}|j                  | j                  ||dz                + | j                  ddj                  |      z          y y )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 33BBe C -::b>&&*AA KK		 I'o N
7  !<!<Wa!e!LMN II?		)$%& r   c              #   $  K   t        j                         }	 d  t        j                         |z
  }||kD  rt        dj                  ||            y # t        j                         |z
  }||kD  rt        dj                  ||            w xY w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     ^^	)nn&+G$<<BF=() )  nn&+G$<<BF=() ) s   BA 8B9BBmsg_rec              #     K   g fd}| j                   j                         }| j                   j                  |       	 d  D ]I  }|j                  d      }|st	        j
                  ||      s. | j                   j                  |       y  t        dj                  |            # | j                   j                  |       w xY ww)Nc                 (    j                  |       y 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     	! ii557		''0	9 ggi(299VS1 II++K8
 !006v@ @
 II++K8s/   =CB- B- 5B- 6CB- -C

Cc                 \    | j                   j                  |       |j                  |       y r   )rb   ro   default_exception_handler)r;   r6   ry   s      r   ra   zTestCase.loop_exception_handler   s$    ##**73&&w/r   c                    |j                  dd      }|j                  d      }|t        |      ||j                  f}nd }g }t        |      D ]x  }|dv r||   }|dk(  r:dj	                  t        j                  |            }	d}||	j                         z  }n	 t        |      }|j                  d	j                  ||             z |W|j                  d
       t        j                  dj	                  t        j                  |       d      }|j                  |       t        j                  dj	                  |      d      }dj                  |||      S # t        $ r%}
dj                  |
t        |            }Y d }
~
d }
~
ww xY w)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   st   ++i)LMKK,	 YI4K4KLHH'? 	<C..CLE((WWY2259:F$I KE LL..sE:;	<  LL)*$OO	22H=>FMLL'//"''%.&9"))!Wg>> ! I006r4;0G Is   E	F#FFN)rG   rH   rI   classmethodrZ   r^   rc   rz   
contextlibcontextmanagerr   strr   ra   rq   r    r   r   rN   rN   b   sw      % %)&. 
) 
) 93 9 9.0 ?r   rN   )	metaclassc                      | di |} |j                   di |xs i  |j                          t        j                  t        |       |S )Nr    )inittrust_local_connectionsatexitregister_shutdown_cluster)
ClusterClscluster_kwargsinitdb_optionsr   s       r   _init_clusterr      sE    *>*GGLL*N(b*##%
OO%w/Nr   c                 8    | 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         bt        j                  j                  d      }|rt	        j
                         a t         S 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      sh     **..*)88:   -&&%z   3>B  r   c                     | j                         dk(  r| j                          | j                         dk7  r| j                          y y )Nrunningznot-initialized)
get_statusstopdestroyr   s    r   r   r     s=    y(00 1r   
   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     sA     )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                   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+  s7     t
 ;;%%'72 $HUOr   r   c                h    t        ||t        |            }| j                  j                  |       |S r   )r   r   	_clustersro   )rY   r   r   r   r   s        r   new_clusterzClusterTestCase.new_cluster8  s0    
N 3N CEW%r   server_settingsc                *    |j                  d|       y )Ndynamicportr   )start)rY   r   r   s      r   start_clusterzClusterTestCase.start_cluster?  s    9oFr   c                     t               | _        | j                  j                         dk7  r,| j                  j                  d| j	                                y y )Nr   r   r   )r   r   r   r   r   r]   s    r   setup_clusterzClusterTestCase.setup_clusterC  sL    +-;;!!#y0KK0G0G0I  K 1r   c                 P    t         |           g | _        | j                          y r   )rC   rZ   r   r   rY   rF   s    r   rZ   zClusterTestCase.setUpClassK  s!    r   c                     t         |           | j                  D ]+  }|t        us|j	                          |j                          - g | _        y r   )rC   r^   r   r   r   r   )rY   r   rF   s     r   r^   zClusterTestCase.tearDownClassQ  sF    }} 	"G..!	" r   c                 $   | j                   j                         }|j                  d      r|j                  d       |j	                  |       t
        j                  j                  d      s#|j                  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    KK335	::eMM&! zz~~h'

50A*(2	*%Y&$.	&!r   c                 j    | j                  |      }t        j                  di |d| j                  iS Nr6   r    )r   pg_connectionr   r6   r   s      r   r   zClusterTestCase.connectg  s/    ++F3	$$@y@sxx@@r   c                 0    t         |           g | _        y r   )rC   rc   _poolsr;   rF   s    r   rc   zClusterTestCase.setUpl  s    r   c                 r    t         |           | j                  D ]  }|j                           g | _        y r   )rC   rz   r  	terminate)r;   r   rF   s     r   rz   zClusterTestCase.tearDownp  s2    KK 	DNN	r   c                     | j                  |      }t        d| j                  ||d|}| j                  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  sN    ,,V4	 K		j,<K@I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 K K  
   (* 
 
 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                 @    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  s%    356'2#$r   c                 
    dd iS )Nzfuzzing-moder    r]   s    r   get_proxy_settingsz)ProxiedClusterTestCase.get_proxy_settings  s    %%r   c                    t         |           | j                  j                         }|j	                  d      }|sd}n|j                  d      rd}t        j                  ||d         | _        | j                  j                          y )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  st    KK335	}}V$D__S!D**"6*
	 			r   c                 V    | j                   j                          t        |           y r   )r  r   rC   r^   r   s    r   r^   z$ProxiedClusterTestCase.tearDownClass  s    		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@    G/7	II44	&II44	&r   c                 V    | j                   j                          t        |           y r   )r  resetrC   rz   r  s    r   rz   zProxiedClusterTestCase.tearDown  s    

r   )rG   rH   rI   r   r   r  rZ   r^   r   rz   rK   rL   s   @r   r  r    so     
 & &         r   r  c                  ,      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    ?@@ 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                   | j                  di |      | _        | j                  j                         | _
        y )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  sn     D$"6"67@@	y"7<99//0Dt0DE"hh99;r   c                     	 | j                   j                  | j                  j                                d | _        t        |           y # t        |           w xY wr   )r6   r7   r(  r\   rC   rz   r  s    r   rz   zConnectedTestCase.tearDown  sC    	II(()9:DHGEGs   :A A)rG   rH   rI   rc   rz   rK   rL   s   @r   r$  r$    s    < r   r$  c                   d    e Zd Zed        Zei fd       Zei fd       Zed        Zed        Zy)HotStandbyTestCasec                    | j                  t        j                        | _        | j	                  | j                  ddd       d }	 | j
                  j                  | j                  j                  dd| j
                              }| j
                  j                  |j                  d             | j                  j                  d       | j                  j                         }| j                  t        j                  |dd	
      | _        | j	                  | j                  ddi       |*| j
                  j                  |j                                y y # |*| j
                  j                  |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  sn    __Z-C-CD#%* 	 	
 	9((--""**'jsxx + IJC HH''  
 99-H**>>@I"%//,,'(5  #2 #C ##!4!   ++CIIK8 s++CIIK8 s   C(E .Fc                    |j                         }|j                  d      r|j                  d       |j                  |       t        j
                  j                  d      s#|j                  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	::eMM&! zz~~h'

50A*(2	*%Y&$.	&!r   c                     | j                  | j                  |      }| 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                     | j                  | j                  |      }t        j                  di |d| j                  iS r   )r=  r7  r   r   r6   r   s      r   connect_primaryz"HotStandbyTestCase.connect_primary  s7    33C4F4FO	$$@y@sxx@@r   c                     | j                  | j                  |      }t        j                  di |d| j                  iS r   )r=  r;  r   r   r6   r   s      r   connect_standbyz"HotStandbyTestCase.connect_standby  s=    33
	 $$@y@sxx@@r   N)	rG   rH   rI   r   r   r=  r   rB  rD  r    r   r   r/  r/    so    *9 *9X 9; 
 
 (* 
 
 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         	 	      ) / #  
! 
!+64))* +6\u?x  L u?p  
(!15"<<!.!9!9$^^:Rh Rj(_ (V &WA WAr   