
    p[iv                     L   d Z ddlZddlmZ ddlmZ ddlZ	 ddlZdZddlmZ dd	lmZ ej                  j!                  ed
      Zd Zd Zej                  j                  d        Zej                  j                  d        Zej,                  d        Zd ZdefdZeej6                  j9                  ej:                  dd      d               Zeej6                  j9                  ej:                  dd      d               Zy# e	$ r dZ ej                         ZY w xY w)z3Tests for closing the clients and context managers.    N)mock)credentialsFT   )_api_client)Clientz(aiohttp is not installed, skipping test.)reasonc                      dt         _        t        ddd      } | j                          | j                  j
                  j                  sJ yz@Tests that the httpx client is closed when the client is closed.FTtest_projectglobalvertexaiprojectlocationN)
api_clienthas_aiohttpr   closer   _httpx_client	is_closedclients    d/mnt/data/GridWatch/venv/lib/python3.12/site-packages/google/genai/tests/client/test_client_close.pytest_close_httpx_clientr   '   sC     *&
 	,,.				)	)	3	33	3    c                      dt         _        t        ddd      5 } 	 | j                  j                  j
                  rJ 	 ddd        j                  j                  j
                  sJ y# 1 sw Y   ,xY wr
   )r   r   r   r   r   r   r   s    r   !test_httpx_client_context_managerr   3   ss     * : !!//99999: 
			)	)	3	33	3: :s   $A**A3c                     K   dt         _        t        ddd      j                  } | j	                          d{    | j
                  j                  j                  sJ y7 'wzFTests that the httpx async client is closed when the client is closed.FTr   r   r   N)r   r   r   aioacloser   _async_httpx_clientr   async_clients    r   test_aclose_httpx_clientr$   A   sa      !* C	 
 			!	!	5	5	?	??	? s   7A#A!(A#c                  @  K   dt         _        t        ddd      j                  4 d{   } 	 | j                  j
                  j                  rJ ddd      d{     j                  j
                  j                  sJ y7 Z7 )# 1 d{  7  sw Y   9xY wwr   )r   r   r   r   r   r!   r   r"   s    r   'test_async_httpx_client_context_managerr&   N   s      !* C	F F '';;EEEEF F 
	!	!	5	5	?	??	?F F F F FsD   (BBB$B	BB(BB	BBBBc               #   ~   K   t        j                  t        j                  j                        } | | _        |  y w)Nspec)r   MockaiohttpClientSessionrequestreturn_value)mock_aiohttp_responses    r   mock_requestr0   ]   s0     )))>)>)F)FG'<$s   ;=c                  \    t        j                  dt        j                  d      dfd      S )Nzgoogle.auth.defaultmagic_tokenr   T)r.   autospec)r   patchr   Credentials r   r   _patch_auth_defaultr7   d   s,    	++M:NK
 r   statusc                    K   t        j                  t        j                        }| |_        dt        |       i|_        i |j                  _        d|j                  _        |S w)z&Has to return a coroutine hence async.r(   zstatus-codetest)
r   r*   r+   ClientResponser8   strheadersjsonr.   text)r8   responses     r   _aiohttp_async_responserA   l   sO     YYG223((/#S[1(!(--%(--	/s   A"A$r-   )r3   c                 Z     dt         _         fd}t        j                   |              y)CTests that the aiohttp session is closed when the client is closed.Tc                    K   t        j                  t         j                  j                  ddddd d d       t              t        d      f_        t               5  t        ddd	      j                  } | j                  j                  t        j                  d
di d d              d {   }| j                  j                  J | j                  j                  j                  rJ | j!                          d {    | j                  j                  j                  sJ 	 d d d        y 7 7 2# 1 sw Y   y xY wwN	localhostP   FT)connection_keyos_error   r   r   r   GETzhttps://example.com)methodurlr=   datatimeout)r+   ClientConnectorErrorclient_reqrepConnectionKeyOSErrorrA   side_effectr7   r   r   r   _async_request_oncer   HttpRequest_aiohttp_sessionclosedr    r#   _r0   s     r   runz(test_aclose_aiohttp_session.<locals>.run{   s7    $$"00>>RdD$ 		
 	 $ L 
	 >  		  ((<<

 
 ' a %%66BBB))::AAAA!!!%%66====+> > ")> >sD   AD>AD2+D.,AD2=D0>&D2%	D>.D20D22D;7D>Nr   r   asyncior[   r0   r[   s   ` r   test_aclose_aiohttp_sessionr_   v   s#      *>B 
++cer   c                 Z     dt         _         fd}t        j                   |              y)rC   Tc                    K   t        j                  t         j                  j                  ddddd d d       t              t        d      f_        t               5  t        ddd	      j                  4 d {   } | j                  j                  t        j                  d
di d d              d {   }| j                  j                  J | j                  j                  j                  rJ d d d       d {     j                  j                  j                  sJ 	 d d d        y 7 7 |7 4# 1 d {  7  sw Y   DxY w# 1 sw Y   y xY wwrE   )r+   rP   rQ   rR   rS   rA   rT   r7   r   r   r   rU   r   rV   rW   rX   rY   s     r   r[   z1test_aiohttp_session_context_manager.<locals>.run   sZ    $$"00>>RdD$ 		
 	 $ L 
	 >  		D D  **>>"")
 
 ''88DDD++<<CCCC#D D& %%66====)> >D
D D D D> >sx   AEE
9D/:E
=7D54D15>D53E
>D3?&E
&	E/E
1D53E
5E	;D><E	E

EENr\   r^   s   ` r   $test_aiohttp_session_context_managerrb      s#      *>@ 
++cer   ) __doc__r]   unittestr   google.oauth2r   pytestr+   AIOHTTP_NOT_INSTALLEDImportError	MagicMock r   r   r   markskipifrequires_aiohttpr   r   r$   r&   fixturer0   r7   intrA   r4   objectr,   r_   rb   r6   r   r   <module>rq      sZ    :   %  *  ;;%%"L &  
	44 	@ 	@ @ @  #  7(()dC$ D $N 7(()dC# D #Q  DNN's   D D#"D#