o
    g$                  
   @   s  d dl mZ d dlmZ d dlmZ d dlmZmZ d dl	Z	d dl
Z
d dlmZ d dlmZmZ d dlmZmZmZ d d	lmZ d d
lmZmZ G dd deZdededededdf
ddZG dd dZdZe
 deeeef ddf fddZdeeef ddfddZ dS )    )futures)Queue)Thread)	GeneratorTupleN)to_proto_submit)PushLogsRequestPushLogsResponse)LogServiceServicerLogServiceStub add_LogServiceServicer_to_server) RetryOnRpcErrorClientInterceptor)	OperationOperationRecordc                       sT   e Zd ZU eed< eed< dededdf fddZdedej	de
fd	d
Z  ZS )FlakyLogServiceServicernum_requests_to_failreceived_requestsreturnNc                    s   t    || _|| _d S N)super__init__r   r   )selfr   r   	__class__ U/var/www/visachat/venv/lib/python3.10/site-packages/chromadb/test/proto/test_utils.pyr      s   

z FlakyLogServiceServicer.__init__requestcontextc                 C   sd   | j dkr$|  j d8  _ |tjj |d | jd|d t S | jd|d tddS )Nr      zService unavailablefailed)statusr   success)record_count)	r   set_codegrpc
StatusCodeUNAVAILABLEset_detailsr   putr	   )r   r   r   r   r   r   PushLogs   s   


z FlakyLogServiceServicer.PushLogs)__name__
__module____qualname__int__annotations__r   r   r   r$   ServicerContextr	   r)   __classcell__r   r   r   r   r      s   
 r   r   r   started_queue
stop_queuer   c                 C   sT   t tjdd}tt| || |d |  |d |	  |
d d S )Nr   )max_workersz
[::]:50051r   )r$   serverr   ThreadPoolExecutorr   r   add_insecure_portstartr(   getstop)r   r   r1   r2   r4   r   r   r   start_server+   s   


r:   c                   @   s>   e Zd ZU eed< deddfddZdededdfd	d
ZdS )LogServiceRetryClientstubgrpc_urlr   Nc                 C   s2   t |}t g}t j|g|R  }t|| _d S r   )r$   insecure_channelr   intercept_channelr   r<   )r   r=   channelinterceptorsr   r   r   r   @   s   
zLogServiceRetryClient.__init__collection_idrecordc                 C   s&   t |}t||gd}| j| d S )N)rB   records)r   r   r<   r)   )r   rB   rC   proto_recordr   r   r   r   push_logF   s   zLogServiceRetryClient.push_log)	r*   r+   r,   r   r.   strr   r   rF   r   r   r   r   r;   =   s   
 r;      c                  c   s`    t  } t  }t  }ttt| ||fd}|  |  td}|| fV  |d |  d S )N)targetargslocalhost:50051r   )	r   r   r:   NUM_REQUESTS_TO_FAILr7   r8   r;   r(   join)r   r1   r2   server_threadclientr   r   r   -client_for_flaky_server_and_received_requestsO   s   


rP   c              
   C   s   | \}}t d}|dtdd d d tjd g }| s'||  | rt|t	d ks1J t
dd |d t	 D s@J |t	 d d	ksJJ d S )
NrK   test1)id	embeddingencodingmetadata	operationr   c                 s   s    | ]	}|d  dkV  qdS )r    r   Nr   ).0rr   r   r   	<genexpr>}   s    z)test_retry_interceptor.<locals>.<genexpr>r    r!   )r;   rF   r   r   ADDemptyappendr8   lenrL   all)rP   rO   r   requestsr   r   r   test_retry_interceptorg   s&   ra   )!
concurrentr   queuer   	threadingr   typingr   r   r$   pytestchromadb.proto.convertr   chromadb.proto.logservice_pb2r   r	   "chromadb.proto.logservice_pb2_grpcr
   r   r   chromadb.proto.utilsr   chromadb.typesr   r   r   r-   r:   r;   rL   fixturerP   ra   r   r   r   r   <module>   sD    

